Improve "llvm-nm -f sysv" output for Elf files

Specifically, compute and Print Type and Section columns.

This is a re-commit of rL354833, after fixing the Asan problem found a a buildbot.

Differential Revision: https://reviews.llvm.org/D59060

llvm-svn: 355742
This commit is contained in:
Sunil Srivastava 2019-03-08 22:00:50 +00:00
parent f84083b4db
commit ae8fe4e093
7 changed files with 77 additions and 18 deletions

View File

@ -41,6 +41,9 @@
namespace llvm {
namespace object {
constexpr int NumElfSymbolTypes = 8;
extern const llvm::EnumEntry<unsigned> ElfSymbolTypes[NumElfSymbolTypes];
class elf_symbol_iterator;
class ELFObjectFileBase : public ObjectFile {
@ -148,6 +151,16 @@ public:
uint8_t getELFType() const {
return getObject()->getSymbolELFType(getRawDataRefImpl());
}
StringRef getELFTypeName() const {
uint8_t Type = getELFType();
for (auto &EE : ElfSymbolTypes) {
if (EE.Value == Type) {
return EE.AltName;
}
}
return "";
}
};
class elf_symbol_iterator : public symbol_iterator {

View File

@ -35,6 +35,16 @@
using namespace llvm;
using namespace object;
const EnumEntry<unsigned> llvm::object::ElfSymbolTypes[NumElfSymbolTypes] = {
{"None", "NOTYPE", ELF::STT_NOTYPE},
{"Object", "OBJECT", ELF::STT_OBJECT},
{"Function", "FUNC", ELF::STT_FUNC},
{"Section", "SECTION", ELF::STT_SECTION},
{"File", "FILE", ELF::STT_FILE},
{"Common", "COMMON", ELF::STT_COMMON},
{"TLS", "TLS", ELF::STT_TLS},
{"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC}};
ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
: ObjectFile(Type, Source) {}

View File

@ -1,11 +1,17 @@
// RUN: llvm-mc %s -o %t -filetype=obj -triple=x86_64-pc-linux
// RUN: llvm-nm --print-size %t | FileCheck %s
// RUN: llvm-nm -f sysv %t | FileCheck -check-prefix=SYSV %s
// CHECK: 0000000000000000 ffffffffffffffff n a
// CHECK: 0000000000000000 0000000000000000 N b
// CHECK: 0000000000000004 0000000000000004 C c
// CHECK: ffffffffffffffff 0000000000000000 a d
// SYSV: a |0000000000000000| n | NOTYPE|ffffffffffffffff| |foo
// SYSV: b |0000000000000000| N | NOTYPE|0000000000000000| |foo
// SYSV: c |0000000000000004| C | OBJECT|0000000000000004| |*COM*
// SYSV: d |ffffffffffffffff| a | NOTYPE|0000000000000000| |*ABS*
.section foo
a:
.size a, 0xffffffffffffffff

View File

@ -1,6 +1,7 @@
RUN: llvm-nm -f sysv %p/Inputs/hello.obj.elf-i386
RUN: llvm-nm -f sysv %p/Inputs/hello.obj.elf-i386 | FileCheck %s --strict-whitespace
CHECK: Name Value Class Type Size Line Section
CHECK: .L.str |00000000| r | |0000000f| |
CHECK: main |00000000| T | |00000015| |
CHECK: puts | | U | | | |
CHECK: .L.str |00000000| r | OBJECT|0000000f| |.rodata.str1.1
CHECK: main |00000000| T | FUNC|00000015| |.text
CHECK: puts | | U | NOTYPE| | |*UND*

View File

@ -2,8 +2,8 @@ RUN: llvm-nm -f sysv %p/Inputs/hello.obj.elf-x86_64 | FileCheck %s --check-prefi
RUN: llvm-nm -f sysv %p/Inputs/hello.obj.macho-x86_64 2>&1 | FileCheck %s --check-prefix=MACHO --strict-whitespace
ELF: Name Value Class Type Size Line Section
ELF: main |0000000000000000| T | |0000000000000015| |
ELF: puts | | U | | | |
ELF: main |0000000000000000| T | FUNC|0000000000000015| |.text
ELF: puts | | U | NOTYPE| | |*UND*
MACHO: Name Value Class Type Size Line Section
MACHO: EH_frame0 |0000000000000068| s | |0000000000000000| |

View File

@ -262,6 +262,8 @@ struct NMSymbol {
uint64_t Size;
char TypeChar;
StringRef Name;
StringRef SectionName;
StringRef TypeName;
BasicSymbolRef Sym;
// The Sym field above points to the native symbol in the object file,
// for Mach-O when we are creating symbols from the dyld info the above
@ -882,8 +884,12 @@ static void sortAndPrintSymbolList(SymbolicFile &Obj, bool printName,
std::string PaddedName(Name);
while (PaddedName.length() < 20)
PaddedName += " ";
std::string TypeName = I->TypeName;
if (TypeName.size() < 18)
TypeName = std::string(18-TypeName.size(), ' ') + TypeName;
outs() << PaddedName << "|" << SymbolAddrStr << "| " << I->TypeChar
<< " | |" << SymbolSizeStr << "| |\n";
<< " |" << TypeName << "|" << SymbolSizeStr << "| |"
<< I->SectionName << "\n";
}
}
@ -1078,8 +1084,40 @@ static bool isObject(SymbolicFile &Obj, basic_symbol_iterator I) {
: elf_symbol_iterator(I)->getELFType() == ELF::STT_OBJECT;
}
static char getNMTypeChar(SymbolicFile &Obj, basic_symbol_iterator I) {
// For ELF object files, Set TypeName to the symbol typename, to be printed
// in the 'Type' column of the SYSV format output.
static StringRef getNMTypeName(SymbolicFile &Obj, basic_symbol_iterator I) {
if (isa<ELFObjectFileBase>(&Obj)) {
elf_symbol_iterator SymI(I);
return SymI->getELFTypeName();
}
return "";
}
// Return Posix nm class type tag (single letter), but also set SecName and
// section and name, to be used in format=sysv output.
static char getNMSectionTagAndName(SymbolicFile &Obj, basic_symbol_iterator I,
StringRef &SecName) {
uint32_t Symflags = I->getFlags();
if (isa<ELFObjectFileBase>(&Obj)) {
if (Symflags & object::SymbolRef::SF_Absolute)
SecName = "*ABS*";
else if (Symflags & object::SymbolRef::SF_Common)
SecName = "*COM*";
else if (Symflags & object::SymbolRef::SF_Undefined)
SecName = "*UND*";
else {
elf_symbol_iterator SymI(I);
Expected<elf_section_iterator> SecIOrErr = SymI->getSection();
if (!SecIOrErr) {
consumeError(SecIOrErr.takeError());
return '?';
}
elf_section_iterator secT = *SecIOrErr;
secT->getName(SecName);
}
}
if ((Symflags & object::SymbolRef::SF_Weak) && !isa<MachOObjectFile>(Obj)) {
char Ret = isObject(Obj, I) ? 'v' : 'w';
return (!(Symflags & object::SymbolRef::SF_Undefined)) ? toupper(Ret) : Ret;
@ -1201,7 +1239,8 @@ dumpSymbolNamesFromObject(SymbolicFile &Obj, bool printName,
}
S.Address = *AddressOrErr;
}
S.TypeChar = getNMTypeChar(Obj, Sym);
S.TypeName = getNMTypeName(Obj, Sym);
S.TypeChar = getNMSectionTagAndName(Obj, Sym, S.SectionName);
std::error_code EC = Sym.printName(OS);
if (EC && MachO)
OS << "bad string index";

View File

@ -1114,16 +1114,6 @@ static const EnumEntry<unsigned> ElfSymbolVisibilities[] = {
{"HIDDEN", "HIDDEN", ELF::STV_HIDDEN},
{"PROTECTED", "PROTECTED", ELF::STV_PROTECTED}};
static const EnumEntry<unsigned> ElfSymbolTypes[] = {
{"None", "NOTYPE", ELF::STT_NOTYPE},
{"Object", "OBJECT", ELF::STT_OBJECT},
{"Function", "FUNC", ELF::STT_FUNC},
{"Section", "SECTION", ELF::STT_SECTION},
{"File", "FILE", ELF::STT_FILE},
{"Common", "COMMON", ELF::STT_COMMON},
{"TLS", "TLS", ELF::STT_TLS},
{"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC}};
static const EnumEntry<unsigned> AMDGPUSymbolTypes[] = {
{ "AMDGPU_HSA_KERNEL", ELF::STT_AMDGPU_HSA_KERNEL }
};