diff --git a/llvm/test/tools/llvm-readobj/ELF/addrsig.test b/llvm/test/tools/llvm-readobj/ELF/addrsig.test --- a/llvm/test/tools/llvm-readobj/ELF/addrsig.test +++ b/llvm/test/tools/llvm-readobj/ELF/addrsig.test @@ -1,15 +1,21 @@ ## Show that llvm-readobj can dump SHT_LLVM_ADDRSIG sections. # RUN: yaml2obj --docnum=1 %s -o %t1.o -# RUN: llvm-readobj --addrsig %t1.o | FileCheck -DFILE=%t1.o %s --check-prefix LLVM -# RUN: not llvm-readelf --addrsig %t1.o 2>&1 | FileCheck -DFILE=%t1.o %s --check-prefix GNU +# RUN: llvm-readobj --addrsig %t1.o | FileCheck -DFILE=%t1.o %s --check-prefix=LLVM +# RUN: llvm-readelf --addrsig %t1.o | \ +# RUN: FileCheck -DFILE=%t1.o %s --strict-whitespace --match-full-lines --check-prefix=GNU # LLVM: Addrsig [ # LLVM-NEXT: Sym: foo (1) # LLVM-NEXT: Sym: bar (2) # LLVM-NEXT: ] -# GNU: error: '[[FILE]]': --addrsig: not implemented +# GNU:Address-significant symbols section '.llvm_addrsig' contains 2 entries: +# GNU-NEXT: Num: Name +# GNU-NEXT: 1: foo +# GNU-NEXT: 2: bar +# GNU-EMPTY: +# GNU-NOT:{{.}} --- !ELF FileHeader: @@ -17,29 +23,48 @@ Data: ELFDATA2LSB Type: ET_DYN Sections: - - Name: .llvm_addrsig - Type: SHT_LLVM_ADDRSIG + - Name: .llvm_addrsig + ShName: [[NAME=]] + Type: SHT_LLVM_ADDRSIG Symbols: [ foo, bar ] Symbols: - Name: foo - Name: bar +## Check what we print when it is impossible to read the name of the SHT_LLVM_ADDRSIG section. +## llvm-readelf reports a warning in this case. + +# RUN: yaml2obj --docnum=1 -DNAME=0xff %s -o %t1.name.o +# RUN: llvm-readobj --addrsig %t1.name.o 2>&1 | \ +# RUN: FileCheck -DFILE=%t1.name.o %s --check-prefix=LLVM --implicit-check-not=warning: +# RUN: llvm-readelf --addrsig %t1.name.o 2>&1 | \ +# RUN: FileCheck -DFILE=%t1.name.o %s --check-prefix=NAME-GNU --implicit-check-not=warning: + +# NAME-GNU: warning: '[[FILE]]': unable to get the name of SHT_LLVM_ADDRSIG section with index 1: a section [index 1] has an invalid sh_name (0xff) offset which goes past the end of the section name string table +# NAME-GNU: Address-significant symbols section '' contains 2 entries: +# NAME-GNU-NEXT: Num: Name +# NAME-GNU-NEXT: 1: foo +# NAME-GNU-NEXT: 2: bar + ## Check that llvm-readobj dumps any SHT_LLVM_ADDRSIG section when --all ## is specified for LLVM style, but not for GNU style. -## TODO: Refine the llvm-readelf check when GNU-style dumping is implemented. -# RUN: llvm-readobj --all %t1.o | FileCheck %s --check-prefix LLVM -# RUN: llvm-readelf --all %t1.o 2>&1 | FileCheck %s --implicit-check-not=warning --implicit-check-not=error +# RUN: llvm-readobj --all %t1.o | FileCheck %s --check-prefix=LLVM +# RUN: llvm-readelf --all %t1.o | FileCheck %s --implicit-check-not="Address-significant" ## Check we report a warning when the content of the SHT_LLVM_ADDRSIG section ## is broken (e.g. contains a malformed uleb128). # RUN: yaml2obj --docnum=2 %s -o %t2.1.o -# RUN: llvm-readobj --addrsig %t2.1.o 2>&1 | FileCheck %s -DFILE=%t2.1.o --check-prefix=MALFORMED +# RUN: llvm-readobj --addrsig %t2.1.o 2>&1 | FileCheck %s -DFILE=%t2.1.o --check-prefix=MALFORMED-LLVM +# RUN: llvm-readelf --addrsig %t2.1.o 2>&1 | FileCheck %s -DFILE=%t2.1.o --check-prefix=MALFORMED-GNU + +# MALFORMED-LLVM: Addrsig [ +# MALFORMED-LLVM-NEXT: warning: '[[FILE]]': unable to decode SHT_LLVM_ADDRSIG section with index 1: malformed uleb128, extends past end +# MALFORMED-LLVM-NEXT: ] -# MALFORMED: Addrsig [ -# MALFORMED-NEXT: warning: '[[FILE]]': unable to decode SHT_LLVM_ADDRSIG section with index 1: malformed uleb128, extends past end -# MALFORMED-NEXT: ] +# MALFORMED-GNU: warning: '[[FILE]]': unable to decode SHT_LLVM_ADDRSIG section with index 1: malformed uleb128, extends past end +# MALFORMED-GNU-NOT:{{.}} --- !ELF FileHeader: @@ -55,24 +80,36 @@ ## Check we report a warning when the content of the SHT_LLVM_ADDRSIG section can't be read. # RUN: yaml2obj --docnum=2 -DOFFSET=0xffffffff %s -o %t2.2.o -# RUN: llvm-readobj --addrsig %t2.2.o 2>&1 | FileCheck %s -DFILE=%t2.2.o --check-prefix=BROKEN-SEC +# RUN: llvm-readobj --addrsig %t2.2.o 2>&1 | FileCheck %s -DFILE=%t2.2.o --check-prefix=BROKEN-SEC-LLVM +# RUN: llvm-readelf --addrsig %t2.2.o 2>&1 | FileCheck %s -DFILE=%t2.2.o --check-prefix=BROKEN-SEC-GNU + +# BROKEN-SEC-LLVM: Addrsig [ +# BROKEN-SEC-LLVM-NEXT: warning: '[[FILE]]': section [index 1] has a sh_offset (0xffffffff) + sh_size (0x1) that is greater than the file size (0x168) +# BROKEN-SEC-LLVM-NEXT: ] -# BROKEN-SEC: Addrsig [ -# BROKEN-SEC-NEXT: warning: '[[FILE]]': section [index 1] has a sh_offset (0xffffffff) + sh_size (0x1) that is greater than the file size (0x168) -# BROKEN-SEC-NEXT: ] +# BROKEN-SEC-GNU: warning: '[[FILE]]': section [index 1] has a sh_offset (0xffffffff) + sh_size (0x1) that is greater than the file size (0x168) +# BROKEN-SEC-GNU-NOT:{{.}} ## Check we report a warning when SHT_LLVM_ADDRSIG references a symbol that can't be ## dumped (e.g. the index value is larger than the number of symbols in .symtab). # RUN: yaml2obj --docnum=3 %s -o %t3.o -# RUN: llvm-readobj --addrsig %t3.o 2>&1 | FileCheck %s -DFILE=%t3.o --check-prefix=INVALID-INDEX - -# INVALID-INDEX: Addrsig [ -# INVALID-INDEX-NEXT: Sym: foo (1) -# INVALID-INDEX-NEXT: warning: '[[FILE]]': unable to read the name of symbol with index 255: unable to get symbol from section [index 2]: invalid symbol index (255) -# INVALID-INDEX-NEXT: Sym: (255) -# INVALID-INDEX-NEXT: Sym: bar (2) -# INVALID-INDEX-NEXT: ] +# RUN: llvm-readobj --addrsig %t3.o 2>&1 | FileCheck %s -DFILE=%t3.o --check-prefix=INVALID-INDEX-LLVM +# RUN: llvm-readelf --addrsig %t3.o 2>&1 | FileCheck %s -DFILE=%t3.o --check-prefix=INVALID-INDEX-GNU + +# INVALID-INDEX-LLVM: Addrsig [ +# INVALID-INDEX-LLVM-NEXT: Sym: foo (1) +# INVALID-INDEX-LLVM-NEXT: warning: '[[FILE]]': unable to read the name of symbol with index 255: unable to get symbol from section [index 2]: invalid symbol index (255) +# INVALID-INDEX-LLVM-NEXT: Sym: (255) +# INVALID-INDEX-LLVM-NEXT: Sym: bar (2) +# INVALID-INDEX-LLVM-NEXT: ] + +# INVALID-INDEX-GNU: Address-significant symbols section '.llvm_addrsig' contains 3 entries: +# INVALID-INDEX-GNU-NEXT: Num: Name +# INVALID-INDEX-GNU-NEXT: 1: foo +# INVALID-INDEX-GNU-NEXT: warning: '[[FILE]]': unable to read the name of symbol with index 255: unable to get symbol from section [index 2]: invalid symbol index (255) +# INVALID-INDEX-GNU-NEXT: 2: +# INVALID-INDEX-GNU-NEXT: 3: bar --- !ELF FileHeader: diff --git a/llvm/test/tools/llvm-readobj/ELF/demangle.test b/llvm/test/tools/llvm-readobj/ELF/demangle.test --- a/llvm/test/tools/llvm-readobj/ELF/demangle.test +++ b/llvm/test/tools/llvm-readobj/ELF/demangle.test @@ -68,17 +68,17 @@ ## Check GNU output style. # RUN: llvm-readelf --symbols --relocations --dyn-symbols --dyn-relocations \ -# RUN: --elf-section-groups --demangle %t.so > %t.gnu.long +# RUN: --elf-section-groups --addrsig --demangle %t.so > %t.gnu.long # RUN: llvm-readelf --symbols --relocations --dyn-symbols --dyn-relocations \ -# RUN: --elf-section-groups -C %t.so > %t.gnu.short +# RUN: --elf-section-groups --addrsig -C %t.so > %t.gnu.short # RUN: FileCheck %s --input-file %t.gnu.long --check-prefixes=GNU-COMMON,GNU-DEMANGLE # RUN: diff %t.gnu.long %t.gnu.short ## Check that default is no demangling. # RUN: llvm-readelf --symbols --relocations --dyn-symbols --dyn-relocations \ -# RUN: --elf-section-groups %t.so > %t.gnu.default +# RUN: --elf-section-groups --addrsig %t.so > %t.gnu.default # RUN: llvm-readelf --symbols --relocations --dyn-symbols --dyn-relocations \ -# RUN: --elf-section-groups --demangle=false %t.so > %t.gnu.nodemangle +# RUN: --elf-section-groups --addrsig --demangle=false %t.so > %t.gnu.nodemangle # RUN: FileCheck %s --input-file %t.gnu.default --check-prefixes=GNU-COMMON,GNU-MANGLED # RUN: diff %t.gnu.default %t.gnu.nodemangle @@ -110,6 +110,13 @@ # GNU-DEMANGLE-SAME: [foo(char)] # GNU-MANGLED-SAME: [_Z3fooc] +# GNU-COMMON: Address-significant symbols section '.llvm_addrsig' contains 2 entries: +# GNU-COMMON: Num: Name +# GNU-DEMANGLE-NEXT: 1: foo(char) +# GNU-DEMANGLE-NEXT: 2: blah(float) +# GNU-MANGLED-NEXT: 1: _Z3fooc +# GNU-MANGLED-NEXT: 2: _Z4blahf + !ELF FileHeader: Class: ELFCLASS64 diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -4722,8 +4722,62 @@ OS << "GNUStyle::printCGProfile not implemented\n"; } +static Expected> toULEB128Array(ArrayRef Data) { + std::vector Ret; + const uint8_t *Cur = Data.begin(); + const uint8_t *End = Data.end(); + while (Cur != End) { + unsigned Size; + const char *Err; + Ret.push_back(decodeULEB128(Cur, &Size, End, &Err)); + if (Err) + return createError(Err); + Cur += Size; + } + return Ret; +} + +template +static Expected> +decodeAddrsigSection(const ELFFile &Obj, const typename ELFT::Shdr &Sec) { + Expected> ContentsOrErr = Obj.getSectionContents(Sec); + if (!ContentsOrErr) + return ContentsOrErr.takeError(); + + if (Expected> SymsOrErr = + toULEB128Array(*ContentsOrErr)) + return *SymsOrErr; + else + return createError("unable to decode " + describe(Obj, Sec) + ": " + + toString(SymsOrErr.takeError())); +} + template void GNUStyle::printAddrsig() { - reportError(createError("--addrsig: not implemented"), this->FileName); + const Elf_Shdr *Sec = this->dumper().getDotAddrsigSec(); + if (!Sec) + return; + + Expected> SymsOrErr = + decodeAddrsigSection(this->Obj, *Sec); + if (!SymsOrErr) { + this->reportUniqueWarning(SymsOrErr.takeError()); + return; + } + + StringRef Name = this->getPrintableSectionName(*Sec); + OS << "\nAddress-significant symbols section '" << Name << "'" + << " contains " << SymsOrErr->size() << " entries:\n"; + OS << " Num: Name\n"; + + Field Fields[2] = {0, 8}; + size_t SymIndex = 0; + for (uint64_t Sym : *SymsOrErr) { + Fields[0].Str = to_string(format_decimal(++SymIndex, 6)) + ":"; + Fields[1].Str = this->dumper().getStaticSymbolName(Sym); + for (const Field &Entry : Fields) + printField(Entry); + OS << "\n"; + } } template @@ -6417,39 +6471,16 @@ } } -static Expected> toULEB128Array(ArrayRef Data) { - std::vector Ret; - const uint8_t *Cur = Data.begin(); - const uint8_t *End = Data.end(); - while (Cur != End) { - unsigned Size; - const char *Err; - Ret.push_back(decodeULEB128(Cur, &Size, End, &Err)); - if (Err) - return createError(Err); - Cur += Size; - } - return Ret; -} - template void LLVMStyle::printAddrsig() { ListScope L(W, "Addrsig"); const Elf_Shdr *Sec = this->dumper().getDotAddrsigSec(); if (!Sec) return; - Expected> ContentsOrErr = - this->Obj.getSectionContents(*Sec); - if (!ContentsOrErr) { - this->reportUniqueWarning(ContentsOrErr.takeError()); - return; - } - - Expected> SymsOrErr = toULEB128Array(*ContentsOrErr); + Expected> SymsOrErr = + decodeAddrsigSection(this->Obj, *Sec); if (!SymsOrErr) { - this->reportUniqueWarning(createError("unable to decode " + - describe(this->Obj, *Sec) + ": " + - toString(SymsOrErr.takeError()))); + this->reportUniqueWarning(SymsOrErr.takeError()); return; }