From 31cd5da582efd6cbeb17e2c91c12b295bd53676f Mon Sep 17 00:00:00 2001 From: George Rimar Date: Tue, 5 Dec 2017 14:23:18 +0000 Subject: [PATCH] Recommit r319747 "[ELF] - Show copy relocated symbols in a --Map file output." With fix: Specify -soname for input dso to fix up the .dynstr section size in different environments. Original commit message: As mentioned in PR35471, copied symbols did not show in --Map output. Patch fixes that. Differential revision: https://reviews.llvm.org/D40785 llvm-svn: 319769 --- lld/ELF/MapFile.cpp | 41 +++++++++++------- lld/test/ELF/Inputs/map-file5.s | 12 ++++++ lld/test/ELF/map-file.s | 74 +++++++++++++++++++++------------ 3 files changed, 84 insertions(+), 43 deletions(-) create mode 100644 lld/test/ELF/Inputs/map-file5.s diff --git a/lld/ELF/MapFile.cpp b/lld/ELF/MapFile.cpp index 47156eeceecb1..c89d2375939c2 100644 --- a/lld/ELF/MapFile.cpp +++ b/lld/ELF/MapFile.cpp @@ -35,7 +35,7 @@ using namespace llvm::object; using namespace lld; using namespace lld::elf; -typedef DenseMap> SymbolMapTy; +typedef DenseMap> SymbolMapTy; // Print out the first three columns of a line. static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size, @@ -47,30 +47,39 @@ static void writeHeader(raw_ostream &OS, uint64_t Addr, uint64_t Size, static std::string indent(int Depth) { return std::string(Depth * 8, ' '); } // Returns a list of all symbols that we want to print out. -static std::vector getSymbols() { - std::vector V; - for (InputFile *File : ObjectFiles) - for (Symbol *B : File->getSymbols()) +static std::vector getSymbols() { + std::vector V; + for (InputFile *File : ObjectFiles) { + for (Symbol *B : File->getSymbols()) { + if (auto *SS = dyn_cast(B)) + if (SS->CopyRelSec) + V.push_back(SS); if (auto *DR = dyn_cast(B)) if (DR->File == File && !DR->isSection() && DR->Section && DR->Section->Live) V.push_back(DR); + } + } return V; } // Returns a map from sections to their symbols. -static SymbolMapTy getSectionSyms(ArrayRef Syms) { +static SymbolMapTy getSectionSyms(ArrayRef Syms) { SymbolMapTy Ret; - for (Defined *S : Syms) - Ret[S->Section].push_back(S); + for (Symbol *S : Syms) { + if (auto *DR = dyn_cast(S)) + Ret[DR->Section].push_back(S); + else + Ret[cast(S)->CopyRelSec].push_back(S); + } // Sort symbols by address. We want to print out symbols in the // order in the output file rather than the order they appeared // in the input files. for (auto &It : Ret) { - SmallVectorImpl &V = It.second; + SmallVectorImpl &V = It.second; std::sort(V.begin(), V.end(), - [](Defined *A, Defined *B) { return A->getVA() < B->getVA(); }); + [](Symbol *A, Symbol *B) { return A->getVA() < B->getVA(); }); } return Ret; } @@ -78,8 +87,8 @@ static SymbolMapTy getSectionSyms(ArrayRef Syms) { // Construct a map from symbols to their stringified representations. // Demangling symbols (which is what toString() does) is slow, so // we do that in batch using parallel-for. -static DenseMap -getSymbolStrings(ArrayRef Syms) { +static DenseMap +getSymbolStrings(ArrayRef Syms) { std::vector Str(Syms.size()); parallelForEachN(0, Syms.size(), [&](size_t I) { raw_string_ostream OS(Str[I]); @@ -87,7 +96,7 @@ getSymbolStrings(ArrayRef Syms) { OS << indent(2) << toString(*Syms[I]); }); - DenseMap Ret; + DenseMap Ret; for (size_t I = 0, E = Syms.size(); I < E; ++I) Ret[Syms[I]] = std::move(Str[I]); return Ret; @@ -106,9 +115,9 @@ void elf::writeMapFile() { } // Collect symbol info that we want to print out. - std::vector Syms = getSymbols(); + std::vector Syms = getSymbols(); SymbolMapTy SectionSyms = getSectionSyms(Syms); - DenseMap SymStr = getSymbolStrings(Syms); + DenseMap SymStr = getSymbolStrings(Syms); // Print out the header line. int W = Config->Is64 ? 16 : 8; @@ -129,7 +138,7 @@ void elf::writeMapFile() { writeHeader(OS, OSec->Addr + IS->OutSecOff, IS->getSize(), IS->Alignment); OS << indent(1) << toString(IS) << '\n'; - for (Defined *Sym : SectionSyms[IS]) + for (Symbol *Sym : SectionSyms[IS]) OS << SymStr[Sym] << '\n'; } } diff --git a/lld/test/ELF/Inputs/map-file5.s b/lld/test/ELF/Inputs/map-file5.s new file mode 100644 index 0000000000000..95bfd6d421090 --- /dev/null +++ b/lld/test/ELF/Inputs/map-file5.s @@ -0,0 +1,12 @@ +.bss +.type sharedFoo,@object +.globl sharedFoo +sharedFoo: +.long 0 +.size sharedFoo, 4 + +.type sharedBar,@object +.globl sharedBar +sharedBar: +.quad 0 +.size sharedBar, 8 diff --git a/lld/test/ELF/map-file.s b/lld/test/ELF/map-file.s index 148894c3c9474..2862a09578a51 100644 --- a/lld/test/ELF/map-file.s +++ b/lld/test/ELF/map-file.s @@ -4,15 +4,19 @@ // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/map-file2.s -o %t2.o // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/map-file3.s -o %t3.o // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/map-file4.s -o %t4.o +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/map-file5.s -o %t5.o +// RUN: ld.lld -shared %t5.o -o %t5.so -soname dso // RUN: rm -f %t4.a // RUN: llvm-ar rc %t4.a %t4.o -// RUN: ld.lld %t1.o %t2.o %t3.o %t4.a -o %t -M | FileCheck -strict-whitespace %s -// RUN: ld.lld %t1.o %t2.o %t3.o %t4.a -o %t -print-map | FileCheck -strict-whitespace %s -// RUN: ld.lld %t1.o %t2.o %t3.o %t4.a -o %t -Map=%t.map +// RUN: ld.lld %t1.o %t2.o %t3.o %t4.a %t5.so -o %t -M | FileCheck -strict-whitespace %s +// RUN: ld.lld %t1.o %t2.o %t3.o %t4.a %t5.so -o %t -print-map | FileCheck -strict-whitespace %s +// RUN: ld.lld %t1.o %t2.o %t3.o %t4.a %t5.so -o %t -Map=%t.map // RUN: FileCheck -strict-whitespace %s < %t.map .global _start _start: + .quad sharedFoo + .quad sharedBar call baz .global _Z1fi _Z1fi: @@ -30,33 +34,49 @@ abs = 0xAB5 labs = 0x1AB5 // CHECK: Address Size Align Out In Symbol -// CHECK-NEXT: 0000000000200158 0000000000000030 8 .eh_frame -// CHECK-NEXT: 0000000000200158 0000000000000030 8 :(.eh_frame) -// CHECK-NEXT: 0000000000201000 0000000000000015 4 .text -// CHECK-NEXT: 0000000000201000 000000000000000e 4 {{.*}}{{/|\\}}map-file.s.tmp1.o:(.text) +// CHECK-NEXT: 00000000002001c8 0000000000000048 8 .dynsym +// CHECK-NEXT: 00000000002001c8 0000000000000048 8 :(.dynsym) +// CHECK-NEXT: 0000000000200210 0000000000000024 8 .gnu.hash +// CHECK-NEXT: 0000000000200210 0000000000000024 8 :(.gnu.hash) +// CHECK-NEXT: 0000000000200234 0000000000000020 4 .hash +// CHECK-NEXT: 0000000000200234 0000000000000020 4 :(.hash) +// CHECK-NEXT: 0000000000200254 0000000000000019 1 .dynstr +// CHECK-NEXT: 0000000000200254 0000000000000019 1 :(.dynstr) +// CHECK-NEXT: 0000000000200270 0000000000000030 8 .rela.dyn +// CHECK-NEXT: 0000000000200270 0000000000000030 8 :(.rela.dyn) +// CHECK-NEXT: 00000000002002a0 0000000000000030 8 .eh_frame +// CHECK-NEXT: 00000000002002a0 0000000000000030 8 :(.eh_frame) +// CHECK-NEXT: 0000000000201000 0000000000000025 4 .text +// CHECK-NEXT: 0000000000201000 000000000000001e 4 {{.*}}{{/|\\}}map-file.s.tmp1.o:(.text) // CHECK-NEXT: 0000000000201000 0000000000000000 0 _start -// CHECK-NEXT: 0000000000201005 0000000000000000 0 f(int) -// CHECK-NEXT: 000000000020100e 0000000000000000 0 local -// CHECK-NEXT: 0000000000201010 0000000000000002 4 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.text) -// CHECK-NEXT: 0000000000201010 0000000000000000 0 foo -// CHECK-NEXT: 0000000000201011 0000000000000000 0 bar -// CHECK-NEXT: 0000000000201012 0000000000000000 1 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.text.zed) -// CHECK-NEXT: 0000000000201012 0000000000000000 0 zed -// CHECK-NEXT: 0000000000201014 0000000000000000 4 {{.*}}{{/|\\}}map-file.s.tmp3.o:(.text) -// CHECK-NEXT: 0000000000201014 0000000000000000 0 bah -// CHECK-NEXT: 0000000000201014 0000000000000001 4 {{.*}}{{/|\\}}map-file.s.tmp4.a(map-file.s.tmp4.o):(.text) -// CHECK-NEXT: 0000000000201014 0000000000000000 0 baz -// CHECK-NEXT: 0000000000202000 0000000000000004 16 .bss -// CHECK-NEXT: 0000000000202000 0000000000000004 16 {{.*}}{{/|\\}}map-file.s.tmp1.o:(COMMON) -// CHECK-NEXT: 0000000000202000 0000000000000004 0 common +// CHECK-NEXT: 0000000000201015 0000000000000000 0 f(int) +// CHECK-NEXT: 000000000020101e 0000000000000000 0 local +// CHECK-NEXT: 0000000000201020 0000000000000002 4 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.text) +// CHECK-NEXT: 0000000000201020 0000000000000000 0 foo +// CHECK-NEXT: 0000000000201021 0000000000000000 0 bar +// CHECK-NEXT: 0000000000201022 0000000000000000 1 {{.*}}{{/|\\}}map-file.s.tmp2.o:(.text.zed) +// CHECK-NEXT: 0000000000201022 0000000000000000 0 zed +// CHECK-NEXT: 0000000000201024 0000000000000000 4 {{.*}}{{/|\\}}map-file.s.tmp3.o:(.text) +// CHECK-NEXT: 0000000000201024 0000000000000000 0 bah +// CHECK-NEXT: 0000000000201024 0000000000000001 4 {{.*}}{{/|\\}}map-file.s.tmp4.a(map-file.s.tmp4.o):(.text) +// CHECK-NEXT: 0000000000201024 0000000000000000 0 baz +// CHECK-NEXT: 0000000000202000 00000000000000c0 8 .dynamic +// CHECK-NEXT: 0000000000202000 00000000000000c0 8 :(.dynamic) +// CHECK-NEXT: 0000000000203000 0000000000000010 16 .bss +// CHECK-NEXT: 0000000000203000 0000000000000004 16 {{.*}}{{/|\\}}map-file.s.tmp1.o:(COMMON) +// CHECK-NEXT: 0000000000203000 0000000000000004 0 common +// CHECK-NEXT: 0000000000203004 0000000000000004 1 :(.bss) +// CHECK-NEXT: 0000000000203004 0000000000000004 0 sharedFoo +// CHECK-NEXT: 0000000000203008 0000000000000008 1 :(.bss) +// CHECK-NEXT: 0000000000203008 0000000000000008 0 sharedBar // CHECK-NEXT: 0000000000000000 0000000000000008 1 .comment // CHECK-NEXT: 0000000000000000 0000000000000008 1 :(.comment) -// CHECK-NEXT: 0000000000000000 0000000000000120 8 .symtab -// CHECK-NEXT: 0000000000000000 0000000000000120 8 :(.symtab) -// CHECK-NEXT: 0000000000000000 0000000000000039 1 .shstrtab -// CHECK-NEXT: 0000000000000000 0000000000000039 1 :(.shstrtab) -// CHECK-NEXT: 0000000000000000 0000000000000038 1 .strtab -// CHECK-NEXT: 0000000000000000 0000000000000038 1 :(.strtab) +// CHECK-NEXT: 0000000000000000 0000000000000168 8 .symtab +// CHECK-NEXT: 0000000000000000 0000000000000168 8 :(.symtab) +// CHECK-NEXT: 0000000000000000 000000000000006c 1 .shstrtab +// CHECK-NEXT: 0000000000000000 000000000000006c 1 :(.shstrtab) +// CHECK-NEXT: 0000000000000000 0000000000000055 1 .strtab +// CHECK-NEXT: 0000000000000000 0000000000000055 1 :(.strtab) // RUN: not ld.lld %t1.o %t2.o %t3.o %t4.a -o %t -Map=/ 2>&1 \ // RUN: | FileCheck -check-prefix=FAIL %s