Update MachOObjectFile::getSymbolAddress so it returns UnknownAddressOrSize
authorKevin Enderby <enderby@apple.com>
Tue, 20 May 2014 23:04:47 +0000 (23:04 +0000)
committerKevin Enderby <enderby@apple.com>
Tue, 20 May 2014 23:04:47 +0000 (23:04 +0000)
for undefined symbols, so it matches what COFFObjectFile::getSymbolAddress
does.  This allows llvm-nm to print spaces instead of 0’s for the value
of undefined symbols in Mach-O files.

To make this change other uses of MachOObjectFile::getSymbolAddress
are updated to handle when the Value is returned as UnknownAddressOrSize.
Which is needed to keep two of the ExecutionEngine tests working for example.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209253 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Object/MachOObjectFile.cpp
test/Object/nm-trivial-object.test
test/Object/nm-universal-binary.test

index 0951460ccbb26c4f815ae378898e104a04d27e24..c6bab03d018e519263275a64ff327c12cdfc3056 100644 (file)
@@ -472,10 +472,18 @@ error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
                                              uint64_t &Res) const {
   if (is64Bit()) {
     MachO::nlist_64 Entry = getSymbol64TableEntry(Symb);
-    Res = Entry.n_value;
+    if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
+        Entry.n_value == 0)
+      Res = UnknownAddressOrSize;
+    else
+      Res = Entry.n_value;
   } else {
     MachO::nlist Entry = getSymbolTableEntry(Symb);
-    Res = Entry.n_value;
+    if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
+        Entry.n_value == 0)
+      Res = UnknownAddressOrSize;
+    else
+      Res = Entry.n_value;
   }
   return object_error::success;
 }
@@ -501,6 +509,10 @@ error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
   nlist_base Entry = getSymbolTableEntryBase(this, DRI);
   uint64_t Value;
   getSymbolAddress(DRI, Value);
+  if (Value == UnknownAddressOrSize) {
+    Result = UnknownAddressOrSize;
+    return object_error::success;
+  }
 
   BeginOffset = Value;
 
@@ -519,6 +531,8 @@ error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
     DataRefImpl DRI = Symbol.getRawDataRefImpl();
     Entry = getSymbolTableEntryBase(this, DRI);
     getSymbolAddress(DRI, Value);
+    if (Value == UnknownAddressOrSize)
+      continue;
     if (Entry.n_sect == SectionIndex && Value > BeginOffset)
       if (!EndOffset || Value < EndOffset)
         EndOffset = Value;
@@ -578,7 +592,7 @@ uint32_t MachOObjectFile::getSymbolFlags(DataRefImpl DRI) const {
     if ((MachOType & MachO::N_TYPE) == MachO::N_UNDF) {
       uint64_t Value;
       getSymbolAddress(DRI, Value);
-      if (Value)
+      if (Value && Value != UnknownAddressOrSize)
         Result |= SymbolRef::SF_Common;
     }
   }
index 111749289807cd1a830db395fd83560216278621..20ac6621e72876860c2c3e4364d86a73ee9f2069 100644 (file)
@@ -55,14 +55,14 @@ WEAK-ELF64: 0000000000000000 V x2
 ABSOLUTE-ELF64: 0000000000000123 a a1
 ABSOLUTE-ELF64: 0000000000000123 A a2
 
-macho: 00000000 U _SomeOtherFunction
+macho:          U _SomeOtherFunction
 macho: 00000000 T _main
-macho: 00000000 U _puts
+macho:          U _puts
 
 macho64: 0000000000000028 s L_.str
-macho64: 0000000000000000 U _SomeOtherFunction
+macho64:                  U _SomeOtherFunction
 macho64: 0000000000000000 T _main
-macho64: 0000000000000000 U _puts
+macho64:                  U _puts
 
 
 Test that nm uses addresses even with ELF .o files.
index faf4812e5378b20f276dece5cee9a19040bb7d33..c20c733dcd8b6284fe181c1fed7a1dd073200130 100644 (file)
@@ -13,7 +13,7 @@ CHECK-AR: 0000000000000068 s EH_frame0
 CHECK-AR: 000000000000003b s L_.str
 CHECK-AR: 0000000000000000 T _main
 CHECK-AR: 0000000000000080 S _main.eh
-CHECK-AR: 0000000000000000 U _printf
+CHECK-AR:                  U _printf
 CHECK-AR: macho-universal-archive.x86_64.i386:i386:foo.o:
 CHECK-AR: 00000008 S _bar
 CHECK-AR: 00000000 T _foo