llvm.org GIT mirror llvm / 15f2c50
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 Kevin Enderby 6 years ago
2 changed file(s) with 49 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
2323 RUN: | FileCheck %s -check-prefix macho-r
2424 RUN: llvm-nm %p/Inputs/macho-text-data-bss.macho-x86_64 -s __DATA __data \
2525 RUN: | FileCheck %s -check-prefix macho-s
26 RUN: llvm-nm -x %p/Inputs/macho-text-data-bss.macho-x86_64 \
27 RUN: | FileCheck %s -check-prefix macho-x
2628 RUN: llvm-nm %p/Inputs/common.coff-i386 \
2729 RUN: | FileCheck %s -check-prefix COFF-COMMON
2830 RUN: llvm-nm %p/Inputs/relocatable-with-section-address.elf-x86-64 \
106108 macho-s-NOT: 0000000000000070 b _b
107109 macho-s-NOT: 0000000000000030 s EH_frame0
108110
111 macho-x: 0000000000000030 0e 05 0000 00000010 EH_frame0
112 macho-x: 0000000000000070 0e 03 0000 0000000d _b
113 macho-x: 000000000000000c 0f 02 0000 00000004 _d
114 macho-x: 0000000000000000 0f 01 0000 00000001 _t
115 macho-x: 0000000000000048 0f 05 0000 00000007 _t.eh
116
109117 Test that nm uses addresses even with ELF .o files.
110118 ELF-SEC-ADDR64: 0000000000000058 D a
111119 ELF-SEC-ADDR64-NEXT: 000000000000005c D b
145145 cl::desc("Dump only symbols from this segment "
146146 "and section name, Mach-O only"));
147147
148 cl::opt FormatMachOasHex("x", cl::desc("Print symbol entry in hex, "
149 "Mach-O only"));
150
148151 bool PrintAddress = true;
149152
150153 bool MultipleFiles = false;
267270 static SymbolListT SymbolList;
268271
269272 // darwinPrintSymbol() is used to print a symbol from a Mach-O file when the
270 // the OutputFormat is darwin. It produces the same output as darwin's nm(1) -m
271 // output.
273 // the OutputFormat is darwin or we are printing Mach-O symbols in hex. For
274 // the darwin format it produces the same output as darwin's nm(1) -m output
275 // and when printing Mach-O symbols in hex it produces the same output as
276 // darwin's nm(1) -x format.
272277 static void darwinPrintSymbol(MachOObjectFile *MachO, SymbolListT::iterator I,
273278 char *SymbolAddrStr, const char *printBlanks) {
274279 MachO::mach_header H;
277282 MachO::nlist_64 STE_64;
278283 MachO::nlist STE;
279284 uint8_t NType;
285 uint8_t NSect;
280286 uint16_t NDesc;
287 uint32_t NStrx;
281288 uint64_t NValue;
282289 if (MachO->is64Bit()) {
283290 H_64 = MachO->MachOObjectFile::getHeader64();
285292 Flags = H_64.flags;
286293 STE_64 = MachO->getSymbol64TableEntry(I->Symb);
287294 NType = STE_64.n_type;
295 NSect = STE_64.n_sect;
288296 NDesc = STE_64.n_desc;
297 NStrx = STE_64.n_strx;
289298 NValue = STE_64.n_value;
290299 } else {
291300 H = MachO->MachOObjectFile::getHeader();
293302 Flags = H.flags;
294303 STE = MachO->getSymbolTableEntry(I->Symb);
295304 NType = STE.n_type;
305 NSect = STE.n_sect;
296306 NDesc = STE.n_desc;
307 NStrx = STE.n_strx;
297308 NValue = STE.n_value;
309 }
310
311 // If we are printing Mach-O symbols in hex do that and return.
312 if (FormatMachOasHex) {
313 char Str[18] = "";
314 const char *printFormat;
315 if (MachO->is64Bit())
316 printFormat = "%016" PRIx64;
317 else
318 printFormat = "%08" PRIx64;
319 format(printFormat, NValue).print(Str, sizeof(Str));
320 outs() << Str << ' ';
321 format("%02x", NType).print(Str, sizeof(Str));
322 outs() << Str << ' ';
323 format("%02x", NSect).print(Str, sizeof(Str));
324 outs() << Str << ' ';
325 format("%04x", NDesc).print(Str, sizeof(Str));
326 outs() << Str << ' ';
327 format("%08x", NStrx).print(Str, sizeof(Str));
328 outs() << Str << ' ';
329 outs() << I->Name << "\n";
330 return;
298331 }
299332
300333 if (PrintAddress) {
479512 if (I->Size != UnknownAddressOrSize)
480513 format(printFormat, I->Size).print(SymbolSizeStr, sizeof(SymbolSizeStr));
481514
482 // If OutputFormat is darwin and we have a MachOObjectFile print as darwin's
483 // nm(1) -m output, else if OutputFormat is darwin and not a Mach-O object
484 // fall back to OutputFormat bsd (see below).
515 // If OutputFormat is darwin or we are printing Mach-O symbols in hex and
516 // we have a MachOObjectFile, call darwinPrintSymbol to print as darwin's
517 // nm(1) -m output or hex, else if OutputFormat is darwin or we are
518 // printing Mach-O symbols in hex and not a Mach-O object fall back to
519 // OutputFormat bsd (see below).
485520 MachOObjectFile *MachO = dyn_cast(Obj);
486 if (OutputFormat == darwin && MachO) {
521 if ((OutputFormat == darwin || FormatMachOasHex) && MachO) {
487522 darwinPrintSymbol(MachO, I, SymbolAddrStr, printBlanks);
488523 } else if (OutputFormat == posix) {
489524 outs() << I->Name << " " << I->TypeChar << " " << SymbolAddrStr