From d1c9751657c81d015a994c407be9ec57245736d7 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Fri, 30 Nov 2018 18:56:10 +0000 Subject: [PATCH] [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 llvm-svn: 348021 --- .../Inputs/global_downgraded_to_static.x86_64 | Bin 0 -> 8840 bytes .../Inputs/global_downgraded_to_static/1.o | Bin 0 -> 2252 bytes .../Inputs/global_downgraded_to_static/1.r.o | Bin 0 -> 1048 bytes .../Inputs/global_downgraded_to_static/2.o | Bin 0 -> 2064 bytes .../dsymutil/X86/global_downgraded_to_static.c | 24 +++++++++++++++++++++ llvm/tools/dsymutil/MachODebugMapParser.cpp | 20 +++++++++++------ 6 files changed, 37 insertions(+), 7 deletions(-) create mode 100755 llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static.x86_64 create mode 100644 llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.o create mode 100644 llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.r.o create mode 100644 llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static/2.o create mode 100644 llvm/test/tools/dsymutil/X86/global_downgraded_to_static.c diff --git a/llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static.x86_64 b/llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static.x86_64 new file mode 100755 index 0000000000000000000000000000000000000000..fa7a1d14f1d06ec76432fedf5442957671fd138d GIT binary patch literal 8840 zcmeHN&ubGw6n-%lMQa-hioc+#l%hQ(S_KPUtTdRQ)}j?ukTJ<_+hCF{&BnIiAyP!e zOD=-myj$?1|9}-d75@Q`3O(pi5E1M5?d~+Y(N@o89(gafdc4nFfbwB-ZCd>grQRP>RhR6sZw>)!9;t0OMyRfKq1z- z_#@FL>wE@PYII~`Bs7P5B6aF|Lv@07-^P32%Deg2`y2UNuMK|wm6yMc7jt75GNU=Y z2FbR(fGLjxXXM|@+Zz=*g74&b(BHI4iVVYmZuUdZ0`EinqR;;hcB4NIrSt^K`u9S^ z@c{ozx?q)rc0$wj+HAU9Dx|H2vIR^z1Z5uIZa=?MetLT7&D)bJOZP72`Z4A$!Jb_r zP<}TaMoT%0dih=x>mC-yVSSwQsrrPah_zxP{?&z=Ygba|QplrJ@ZTOlL;u4`k)3Fd z1(^I8F$@?63GHFkl!k3>XHsHUrnPk3T=@xQilhEjJ;=*~Kr3_nDt8eD&kn z+FEw;U4paR7ais(B;0cU!F}#2wA7cuUWI+`sXDPQNwjuVk?ob#nV)?Yw2r%D|-K$Y3R3s!EzC z3~_1n;?eB&m}vC+x1>Wr`i_+oXWH-$;K_JQzV~F=^-dvDIDz1ODi0#=L;FLRW4x0+ z2&E8@;St6b2Ltiq1M7Q#FMbF=gdO8Ax5d}?uQ1Q}8q+o7^L|$g<7@mHgGoJsmiFO% zR)D*B=mL5;N9MX?K3BxN1Mfyh$K$;;vF4)>7!W^SlK3aVpKGJIHp*KY-f^p%TQ8^r z{>FfHOXsRHb+?w5sj^eZmzCwrRi|dLq?U3WSmxbQF;!eQ+r%n8l+sbO)6J|;(ArBZ TutKUzF0V literal 0 HcmV?d00001 diff --git a/llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.o b/llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.o new file mode 100644 index 0000000000000000000000000000000000000000..cd00c7677fd1046b1524a3fd40f6b5e4c3e15b03 GIT binary patch literal 2252 zcmb7E%}*0S6rbIt<)fvoVj{$k^nekJ)@n!)O@v0kqL>)uGr^48c3VoyZqqIw8V|%6 z1JMJ2fCmm9J$Tcji3j!MU*XZa(J21j?o28c{6X``_1pozMXHse*A4`Owkwy zXdATX!w`i+`k+6_*+jmo?#3SFK;#;jj5JJ62 zd10|FW5{xz(QUoz;e0$Xo2afOj->t?c!Fv*sAdK{BekC6WxM2ffwz#DnXE)a#LV4g zY+uw{3Gl#6yn>ZJ6z^G0B2AC`J&q=LdMZ^iHmLr3eiCm&z_f~&H?tO{c=0|9oJYbg z2!0uS59$?2-F3=(OS+je%8hvEskv(8eT((3>1l_em!HIwut($R#bUwWroKk>8tNSo zK#PLoha>s9cvfoF`M_R&+XAm&;6>IVUUouaGw#MDP6-1>TZO0xy#1P59AwbN>16E+>xCd!eroPxH61QCRR` z_o1~y`{KA-cT4lRjZ&A=${~d z8uQ_TsCyWTZ|?+#V5w+|r3$J4YDMsnv?^N?S9vnSl&y#o3AT6aA&T-(L*MaX- zb+AMz@b1kI!I=k-rRDV`g0fS9C q1NIEPF|ht&Sj79!iZzVmPY6pnVH$)`ei#WA?wtCQi#Bg)VD=Buee{C> literal 0 HcmV?d00001 diff --git a/llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.r.o b/llvm/test/tools/dsymutil/Inputs/global_downgraded_to_static/1.r.o new file mode 100644 index 0000000000000000000000000000000000000000..6629b4c1d9eb3fe2f8fb58571a279f73bbe802f7 GIT binary patch literal 1048 zcma)5%TC)+5FID9p)^UYibX3yvY%0j59n8P**#(boQdZKvq;rZ=AJp%=br2Fz4?9h_qsx4l?elK0r^V`2Z?x& z{2_c!WM;mU7ly-=B*jmHISsQkuXEnn>~`+Bsj8-cpRCP*)9n}N*gO(s4)-`Z@CIQ% z%Y5I@R`DA}8$QzGvHI%t444Bi^=1_Q8?8?3j&Esc`q_dHeshD_PXE2Mc{BU|*Y}ME z07(Yyu!XT{5$dp^?8o+;oV&C zkCuH+6;eVN%b4kMmX+M1=VEMdIyrt>OoPC=#XUDUze)fAXYP9hUd}N*M-Z=E zl}N*vb>ivibkSU({cq(b@On9Jy?B;A>rhG%?*+%X%Jo;yZw4iehWVW6sdfWEem_o^nI;pCB*%gXkmWoH;09ptH= zdc*NF;lw(jH{gS~ZwfH^THUAb?T?I8xg zpT?`}22Z+!cqAWp;Ya7DQb21n^_ro;y$O&^xT_xY7M|KQnuTER<4eVhVNs&mN>=5C_=Zw#&5L0~9U z#1ceNM{vKibAZIkqZmE3#GhhmGgL5D!v7aEyF~=7bZ;D_B~?;e8XBKq6r~@c$PFUA zxheiPkW|!%p@CL$Jtr|F1hlv${Z?ABmuL-aQjJr`D1fHQVaV-LL~UM=G(~4ZQG~%s ks&~}gCwv_}%rz?VN0VF=mpzogg@#ooUdCC+0p^w0UpOY+fdBvi literal 0 HcmV?d00001 diff --git a/llvm/test/tools/dsymutil/X86/global_downgraded_to_static.c b/llvm/test/tools/dsymutil/X86/global_downgraded_to_static.c new file mode 100644 index 0000000..35c3247 --- /dev/null +++ b/llvm/test/tools/dsymutil/X86/global_downgraded_to_static.c @@ -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 diff --git a/llvm/tools/dsymutil/MachODebugMapParser.cpp b/llvm/tools/dsymutil/MachODebugMapParser.cpp index 48155b4..d696e1d4 100644 --- a/llvm/tools/dsymutil/MachODebugMapParser.cpp +++ b/llvm/tools/dsymutil/MachODebugMapParser.cpp @@ -511,14 +511,16 @@ void MachODebugMapParser::loadMainBinarySymbols( // 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 SectionOrErr = Sym.getSection(); if (!SectionOrErr) { // TODO: Actually report errors helpfully. @@ -538,7 +540,11 @@ void MachODebugMapParser::loadMainBinarySymbols( 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); } } -- 2.7.4