[llvm] r348021 - [dsymutil] Gather global and local symbol addresses in the main executable.

Jonas Devlieghere via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 30 10:56:11 PST 2018


Author: jdevlieghere
Date: Fri Nov 30 10:56:10 2018
New Revision: 348021

URL: http://llvm.org/viewvc/llvm-project?rev=348021&view=rev
Log:
[dsymutil] Gather global and local symbol addresses in the main executable.

Usually local symbols will have their address described in the debug
map. Global symbols have to have their address looked up in the symbol
table of the main executable. By playing with 'ld -r' and export lists,
you can get a symbol described as global by the debug map while actually
being a local symbol as far as the link in concerned. By gathering the
address of local symbols, we fix this issue.

Also, we prefer a global symbol in case of a name collision to preserve
the previous behavior.

Note that using the 'ld -r' tricks, people can actually cause symbol
names collisions that dsymutil has no way to figure out. This fixes the
simple case where there is only one symbol of a given name.

rdar://problem/32826621

Differential revision: https://reviews.llvm.org/D54922

Added:
    llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/
    llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static.x86_64   (with props)
    llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.o   (with props)
    llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.r.o   (with props)
    llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/2.o   (with props)
    llvm/trunk/test/tools/dsymutil/X86/global_downgraded_to_static.c
Modified:
    llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp

Added: llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static.x86_64
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static.x86_64?rev=348021&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static.x86_64
------------------------------------------------------------------------------
    svn:executable = *

Propchange: llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static.x86_64
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.o
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.o?rev=348021&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.o
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.r.o
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.r.o?rev=348021&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.r.o
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/2.o
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/2.o?rev=348021&view=auto
==============================================================================
Binary file - no diff available.

Propchange: llvm/trunk/test/tools/dsymutil/Inputs/global_downgraded_to_static/2.o
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream

Added: llvm/trunk/test/tools/dsymutil/X86/global_downgraded_to_static.c
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/dsymutil/X86/global_downgraded_to_static.c?rev=348021&view=auto
==============================================================================
--- llvm/trunk/test/tools/dsymutil/X86/global_downgraded_to_static.c (added)
+++ llvm/trunk/test/tools/dsymutil/X86/global_downgraded_to_static.c Fri Nov 30 10:56:10 2018
@@ -0,0 +1,24 @@
+// REQUIRES : system-darwin
+// RUN: dsymutil -oso-prepend-path %p/.. -dump-debug-map %p/../Inputs/global_downgraded_to_static.x86_64 2>&1 | FileCheck %s
+//
+//  To build:
+//    clang -g -c -DFILE1 global_downgraded_to_static.c -o 1.o
+//    clang -g -c -DFILE2 global_downgraded_to_static.c -o 2.o
+//    ld -r -exported_symbol _foo 1.o -o 1.r.o
+//    clang 1.r.o 2.o -o global_downgraded_to_static.x86_64
+
+#if defined(FILE1)
+int global_to_become_static = 42;
+// CHECK: sym: _global_to_become_static,
+// CHECK-SAME: binAddr: 0x0000000100001000
+int foo() {
+  return global_to_become_static;
+}
+#elif defined(FILE2)
+int foo(void);
+int main() {
+  return foo();
+}
+#else
+#error Define FILE1 or FILE2
+#endif

Modified: llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp?rev=348021&r1=348020&r2=348021&view=diff
==============================================================================
--- llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp (original)
+++ llvm/trunk/tools/dsymutil/MachODebugMapParser.cpp Fri Nov 30 10:56:10 2018
@@ -511,14 +511,16 @@ void MachODebugMapParser::loadMainBinary
     // Skip undefined and STAB entries.
     if ((Type == SymbolRef::ST_Debug) || (Type == SymbolRef::ST_Unknown))
       continue;
-    // The only symbols of interest are the global variables. These
-    // are the only ones that need to be queried because the address
-    // of common data won't be described in the debug map. All other
-    // addresses should be fetched for the debug map.
+    // In theory, the only symbols of interest are the global variables. These
+    // are the only ones that need to be queried because the address of common
+    // data won't be described in the debug map. All other addresses should be
+    // fetched for the debug map. In reality, by playing with 'ld -r' and
+    // export lists, you can get symbols described as N_GSYM in the debug map,
+    // but associated with a local symbol. Gather all the symbols, but prefer
+    // the global ones.
     uint8_t SymType =
         MainBinary.getSymbolTableEntry(Sym.getRawDataRefImpl()).n_type;
-    if (!(SymType & (MachO::N_EXT | MachO::N_PEXT)))
-      continue;
+    bool Extern = SymType & (MachO::N_EXT | MachO::N_PEXT);
     Expected<section_iterator> SectionOrErr = Sym.getSection();
     if (!SectionOrErr) {
       // TODO: Actually report errors helpfully.
@@ -538,7 +540,11 @@ void MachODebugMapParser::loadMainBinary
     StringRef Name = *NameOrErr;
     if (Name.size() == 0 || Name[0] == '\0')
       continue;
-    MainBinarySymbolAddresses[Name] = Addr;
+    // Override only if the new key is global.
+    if (Extern)
+      MainBinarySymbolAddresses[Name] = Addr;
+    else
+      MainBinarySymbolAddresses.try_emplace(Name, Addr);
   }
 }
 




More information about the llvm-commits mailing list