[ELF] - Show copy relocated symbols in a --Map file output.

As mentioned in PR35471, copied symbols did not show
in --Map output. Patch fixes that.

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

llvm-svn: 319747
This commit is contained in:
George Rimar 2017-12-05 09:13:45 +00:00
parent 1382a32a73
commit d9ae97cdb6
3 changed files with 84 additions and 43 deletions

View File

@ -35,7 +35,7 @@ using namespace llvm::object;
using namespace lld;
using namespace lld::elf;
typedef DenseMap<const SectionBase *, SmallVector<Defined *, 4>> SymbolMapTy;
typedef DenseMap<const SectionBase *, SmallVector<Symbol *, 4>> 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<Defined *> getSymbols() {
std::vector<Defined *> V;
for (InputFile *File : ObjectFiles)
for (Symbol *B : File->getSymbols())
static std::vector<Symbol *> getSymbols() {
std::vector<Symbol *> V;
for (InputFile *File : ObjectFiles) {
for (Symbol *B : File->getSymbols()) {
if (auto *SS = dyn_cast<SharedSymbol>(B))
if (SS->CopyRelSec)
V.push_back(SS);
if (auto *DR = dyn_cast<Defined>(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<Defined *> Syms) {
static SymbolMapTy getSectionSyms(ArrayRef<Symbol *> Syms) {
SymbolMapTy Ret;
for (Defined *S : Syms)
Ret[S->Section].push_back(S);
for (Symbol *S : Syms) {
if (auto *DR = dyn_cast<Defined>(S))
Ret[DR->Section].push_back(S);
else
Ret[cast<SharedSymbol>(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<Defined *> &V = It.second;
SmallVectorImpl<Symbol *> &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<Defined *> 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<Defined *, std::string>
getSymbolStrings(ArrayRef<Defined *> Syms) {
static DenseMap<Symbol *, std::string>
getSymbolStrings(ArrayRef<Symbol *> Syms) {
std::vector<std::string> Str(Syms.size());
parallelForEachN(0, Syms.size(), [&](size_t I) {
raw_string_ostream OS(Str[I]);
@ -87,7 +96,7 @@ getSymbolStrings(ArrayRef<Defined *> Syms) {
OS << indent(2) << toString(*Syms[I]);
});
DenseMap<Defined *, std::string> Ret;
DenseMap<Symbol *, std::string> 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<Defined *> Syms = getSymbols();
std::vector<Symbol *> Syms = getSymbols();
SymbolMapTy SectionSyms = getSectionSyms(Syms);
DenseMap<Defined *, std::string> SymStr = getSymbolStrings(Syms);
DenseMap<Symbol *, std::string> 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';
}
}

View File

@ -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

View File

@ -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
// 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 <internal>:(.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 <internal>:(.dynsym)
// CHECK-NEXT: 0000000000200210 0000000000000024 8 .gnu.hash
// CHECK-NEXT: 0000000000200210 0000000000000024 8 <internal>:(.gnu.hash)
// CHECK-NEXT: 0000000000200234 0000000000000020 4 .hash
// CHECK-NEXT: 0000000000200234 0000000000000020 4 <internal>:(.hash)
// CHECK-NEXT: 0000000000200254 0000000000000057 1 .dynstr
// CHECK-NEXT: 0000000000200254 0000000000000057 1 <internal>:(.dynstr)
// CHECK-NEXT: 00000000002002b0 0000000000000030 8 .rela.dyn
// CHECK-NEXT: 00000000002002b0 0000000000000030 8 <internal>:(.rela.dyn)
// CHECK-NEXT: 00000000002002e0 0000000000000030 8 .eh_frame
// CHECK-NEXT: 00000000002002e0 0000000000000030 8 <internal>:(.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 <internal>:(.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 <internal>:(.bss)
// CHECK-NEXT: 0000000000203004 0000000000000004 0 sharedFoo
// CHECK-NEXT: 0000000000203008 0000000000000008 1 <internal>:(.bss)
// CHECK-NEXT: 0000000000203008 0000000000000008 0 sharedBar
// CHECK-NEXT: 0000000000000000 0000000000000008 1 .comment
// CHECK-NEXT: 0000000000000000 0000000000000008 1 <internal>:(.comment)
// CHECK-NEXT: 0000000000000000 0000000000000120 8 .symtab
// CHECK-NEXT: 0000000000000000 0000000000000120 8 <internal>:(.symtab)
// CHECK-NEXT: 0000000000000000 0000000000000039 1 .shstrtab
// CHECK-NEXT: 0000000000000000 0000000000000039 1 <internal>:(.shstrtab)
// CHECK-NEXT: 0000000000000000 0000000000000038 1 .strtab
// CHECK-NEXT: 0000000000000000 0000000000000038 1 <internal>:(.strtab)
// CHECK-NEXT: 0000000000000000 0000000000000168 8 .symtab
// CHECK-NEXT: 0000000000000000 0000000000000168 8 <internal>:(.symtab)
// CHECK-NEXT: 0000000000000000 000000000000006c 1 .shstrtab
// CHECK-NEXT: 0000000000000000 000000000000006c 1 <internal>:(.shstrtab)
// CHECK-NEXT: 0000000000000000 0000000000000055 1 .strtab
// CHECK-NEXT: 0000000000000000 0000000000000055 1 <internal>:(.strtab)
// RUN: not ld.lld %t1.o %t2.o %t3.o %t4.a -o %t -Map=/ 2>&1 \
// RUN: | FileCheck -check-prefix=FAIL %s