From 3d381a710220100722465ead9c64874621edead4 Mon Sep 17 00:00:00 2001 From: Francis Visoiu Mistrih Date: Fri, 4 Dec 2020 20:10:06 -0800 Subject: [PATCH] [llvm-nm][MachO] Don't call getFlags on redacted symbols Avoid calling getFlags on a non-existent symbol. The way this is triggered is by calling strip -N on a binary, which sets the MH_NLIST_OUTOFSYNC_WITH_DYLDINFO header flag. Then, in the LC_FUNCTION_STARTS command, nm is trying to print the stripped symbols and needs the proper checks. --- .../Inputs/redacted-function.macho-aarch64 | Bin 0 -> 32820 bytes .../AArch64/macho-redacted-function.test | 16 ++++++++++++++++ llvm/tools/llvm-nm/llvm-nm.cpp | 14 ++++++++------ 3 files changed, 24 insertions(+), 6 deletions(-) create mode 100755 llvm/test/tools/llvm-nm/AArch64/Inputs/redacted-function.macho-aarch64 create mode 100644 llvm/test/tools/llvm-nm/AArch64/macho-redacted-function.test diff --git a/llvm/test/tools/llvm-nm/AArch64/Inputs/redacted-function.macho-aarch64 b/llvm/test/tools/llvm-nm/AArch64/Inputs/redacted-function.macho-aarch64 new file mode 100755 index 0000000000000000000000000000000000000000..eebb208b421cf20b4214f7a7ef6953350e1a5dd3 GIT binary patch literal 32820 zcmeI*KWkG_7{~D^X^KUII&=^eP5p;TX+$~*I;3ilqG*X7#O08hD-ARe+5}rV7%=V@ zT%=GOEjZN8tpRtz#dqNw5Mup3xw$4*aB`CGf#;s{++|2Fbqhjg4lB5-q!Joaq*HMHpUwk+}^t_;PtDmbM)zHI#*INjU zXtfcKO#JZaQ zo!4Cdy~&o@nEH$TJTX&BLjVB;5I_I{1Q0*~0R#|0009ILKmY**5I_I{1Q0*~0R#|0 z009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{ z1Q0*~0R#|0009ILKmY**5I_I{1Q0*~0R#|0009ILKmY**5I_I{1Q0*~0R#|0009IL zKmY**5I_I{1Q0*~f&V~Y?}dHouBW!UH*I6gTBUcNPJL9}_1E3?nQcAW=!}G~Dz9Gh z-fOUXEq8Cjz1LCq+Us6d-Qz>Q4!e)NW_wnZ*2m*&>#4KK)%=x=rGkxGI@A_Jv>H8b cHlnZ?l`GM*P1MTOx}A$>wfFSHy7XTC2@Aqy_5c6? literal 0 HcmV?d00001 diff --git a/llvm/test/tools/llvm-nm/AArch64/macho-redacted-function.test b/llvm/test/tools/llvm-nm/AArch64/macho-redacted-function.test new file mode 100644 index 00000000000000..c662f54c93b6cc --- /dev/null +++ b/llvm/test/tools/llvm-nm/AArch64/macho-redacted-function.test @@ -0,0 +1,16 @@ +RUN: llvm-nm %p/Inputs/redacted-function.macho-aarch64 | FileCheck %s + +CHECK: + +# Generated with: +# $ cat /tmp/a.c +# static int i(void) { +# return 0; +# } +# +# int main(void) { +# return i(); +# } +# +# $ xcrun -sdk watchos clang -arch arm64_32 /tmp/a.c -o /tmp/redacted-function.macho-aarch64 +# $ xcrun -sdk watchos strip -N /tmp/redacted-function.macho-aarch64 diff --git a/llvm/tools/llvm-nm/llvm-nm.cpp b/llvm/tools/llvm-nm/llvm-nm.cpp index a34352d1512c5f..9680149d4f8cf3 100644 --- a/llvm/tools/llvm-nm/llvm-nm.cpp +++ b/llvm/tools/llvm-nm/llvm-nm.cpp @@ -316,18 +316,20 @@ struct NMSymbol { static bool compareSymbolAddress(const NMSymbol &A, const NMSymbol &B) { bool ADefined; // Symbol flags have been checked in the caller. - uint32_t AFlags = cantFail(A.Sym.getFlags()); - if (A.Sym.getRawDataRefImpl().p) + if (A.Sym.getRawDataRefImpl().p) { + uint32_t AFlags = cantFail(A.Sym.getFlags()); ADefined = !(AFlags & SymbolRef::SF_Undefined); - else + } else { ADefined = A.TypeChar != 'U'; + } bool BDefined; // Symbol flags have been checked in the caller. - uint32_t BFlags = cantFail(B.Sym.getFlags()); - if (B.Sym.getRawDataRefImpl().p) + if (B.Sym.getRawDataRefImpl().p) { + uint32_t BFlags = cantFail(B.Sym.getFlags()); BDefined = !(BFlags & SymbolRef::SF_Undefined); - else + } else { BDefined = B.TypeChar != 'U'; + } return std::make_tuple(ADefined, A.Address, A.Name, A.Size) < std::make_tuple(BDefined, B.Address, B.Name, B.Size); }