Add the "-x" flag to llvm-nm for Mach-O files that prints the fields of a symbol in hex.

(generally use for debugging the tools).  This is same functionality as darwin’s
nm(1) "-x" flag.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@213176 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Kevin Enderby 2014-07-16 17:38:26 +00:00
parent 53b0a2d8c6
commit 15f2c50e63
2 changed files with 49 additions and 6 deletions

View File

@ -24,6 +24,8 @@ RUN: llvm-nm -r %p/Inputs/macho-text-data-bss.macho-x86_64 \
RUN: | FileCheck %s -check-prefix macho-r
RUN: llvm-nm %p/Inputs/macho-text-data-bss.macho-x86_64 -s __DATA __data \
RUN: | FileCheck %s -check-prefix macho-s
RUN: llvm-nm -x %p/Inputs/macho-text-data-bss.macho-x86_64 \
RUN: | FileCheck %s -check-prefix macho-x
RUN: llvm-nm %p/Inputs/common.coff-i386 \
RUN: | FileCheck %s -check-prefix COFF-COMMON
RUN: llvm-nm %p/Inputs/relocatable-with-section-address.elf-x86-64 \
@ -107,6 +109,12 @@ macho-s-NOT: 0000000000000000 T _t
macho-s-NOT: 0000000000000070 b _b
macho-s-NOT: 0000000000000030 s EH_frame0
macho-x: 0000000000000030 0e 05 0000 00000010 EH_frame0
macho-x: 0000000000000070 0e 03 0000 0000000d _b
macho-x: 000000000000000c 0f 02 0000 00000004 _d
macho-x: 0000000000000000 0f 01 0000 00000001 _t
macho-x: 0000000000000048 0f 05 0000 00000007 _t.eh
Test that nm uses addresses even with ELF .o files.
ELF-SEC-ADDR64: 0000000000000058 D a
ELF-SEC-ADDR64-NEXT: 000000000000005c D b

View File

@ -146,6 +146,9 @@ cl::list<std::string> SegSect("s", cl::Positional, cl::ZeroOrMore,
cl::desc("Dump only symbols from this segment "
"and section name, Mach-O only"));
cl::opt<bool> FormatMachOasHex("x", cl::desc("Print symbol entry in hex, "
"Mach-O only"));
bool PrintAddress = true;
bool MultipleFiles = false;
@ -268,8 +271,10 @@ typedef std::vector<NMSymbol> SymbolListT;
static SymbolListT SymbolList;
// darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
// the OutputFormat is darwin. It produces the same output as darwin's nm(1) -m
// output.
// the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
// the darwin format it produces the same output as darwin's nm(1) -m output
// and when printing Mach-O symbols in hex it produces the same output as
// darwin's nm(1) -x format.
static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
char *SymbolAddrStr, const char *printBlanks) {
MachO::mach_header H;
@ -278,7 +283,9 @@ static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
MachO::nlist_64 STE_64;
MachO::nlist STE;
uint8_t NType;
uint8_t NSect;
uint16_t NDesc;
uint32_t NStrx;
uint64_t NValue;
if (MachO->is64Bit()) {
H_64 = MachO->MachOObjectFile::getHeader64();
@ -286,7 +293,9 @@ static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
Flags = H_64.flags;
STE_64 = MachO->getSymbol64TableEntry(I->Symb);
NType = STE_64.n_type;
NSect = STE_64.n_sect;
NDesc = STE_64.n_desc;
NStrx = STE_64.n_strx;
NValue = STE_64.n_value;
} else {
H = MachO->MachOObjectFile::getHeader();
@ -294,10 +303,34 @@ static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
Flags = H.flags;
STE = MachO->getSymbolTableEntry(I->Symb);
NType = STE.n_type;
NSect = STE.n_sect;
NDesc = STE.n_desc;
NStrx = STE.n_strx;
NValue = STE.n_value;
}
// If we are printing Mach-O symbols in hex do that and return.
if (FormatMachOasHex) {
char Str[18] = "";
const char *printFormat;
if (MachO->is64Bit())
printFormat = "%016" PRIx64;
else
printFormat = "%08" PRIx64;
format(printFormat, NValue).print(Str, sizeof(Str));
outs() << Str << ' ';
format("%02x", NType).print(Str, sizeof(Str));
outs() << Str << ' ';
format("%02x", NSect).print(Str, sizeof(Str));
outs() << Str << ' ';
format("%04x", NDesc).print(Str, sizeof(Str));
outs() << Str << ' ';
format("%08x", NStrx).print(Str, sizeof(Str));
outs() << Str << ' ';
outs() << I->Name << "\n";
return;
}
if (PrintAddress) {
if ((NType & MachO::N_TYPE) == MachO::N_INDR)
strcpy(SymbolAddrStr, printBlanks);
@ -480,11 +513,13 @@ static void sortAndPrintSymbolList(SymbolicFile *Obj, bool printName) {
if (I->Size != UnknownAddressOrSize)
format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
// If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
// nm(1) -m output, else if OutputFormat is darwin and not a Mach-O object
// fall back to OutputFormat bsd (see below).
// If OutputFormat is darwin or we are printing Mach-O symbols in hex and
// we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
// nm(1) -m output or hex, else if OutputFormat is darwin or we are
// printing Mach-O symbols in hex and not a Mach-O object fall back to
// OutputFormat bsd (see below).
MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(Obj);
if (OutputFormat == darwin && MachO) {
if ((OutputFormat == darwin || FormatMachOasHex) && MachO) {
darwinPrintSymbol(MachO, I, SymbolAddrStr, printBlanks);
} else if (OutputFormat == posix) {
outs() << I->Name << " " << I->TypeChar << " " << SymbolAddrStr