llvm.org GIT mirror llvm / a17a5fa
For llvm-objdump and Mach-O files, fix the printing of module init and term sections from .o files to look to see if the pointers have a relocation entry and if so print the symbol name from the relocation entry. If not fall back to the existing code and use the pointer value to look up that value in the symbol table. rdar://38337506 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@328037 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 2 years ago
3 changed file(s) with 48 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
0 // RUN: llvm-objdump -m -section __DATA,__mod_init_func %p/Inputs/dylibModInit.macho-x86_64 | FileCheck %s -check-prefix=MODINIT
11 // RUN: llvm-objdump -m -section __DATA,__mod_init_func -non-verbose %p/Inputs/dylibModInit.macho-x86_64 | FileCheck %s -check-prefix=NON_VERBOSE
2 // RUN: llvm-objdump -m -section __DATA,__mod_init_func %p/Inputs/objModInit.macho-x86_64 | FileCheck %s -check-prefix=OBJ_MODINIT
23
34 MODINIT: Contents of (__DATA,__mod_init_func) section
45 MODINIT: 0x0000000000001000 0x0000000000000f30 _libinit
56
67 NON_VERBOSE: Contents of (__DATA,__mod_init_func) section
78 NON_VERBOSE: 0000000000001000 30 0f 00 00 00 00 00 00
9
10 OBJ_MODINIT: Contents of (__DATA,__mod_init_func) section
11 OBJ_MODINIT: 0x0000000000000018 0x0000000000000000 _constructor_func
12831283 }
12841284 }
12851285
1286 static void DumpInitTermPointerSection(MachOObjectFile *O, const char *sect,
1286 static void DumpInitTermPointerSection(MachOObjectFile *O,
1287 const SectionRef &Section,
1288 const char *sect,
12871289 uint32_t sect_size, uint64_t sect_addr,
12881290 SymbolAddressMap *AddrMap,
12891291 bool verbose) {
12901292 uint32_t stride;
12911293 stride = (O->is64Bit()) ? sizeof(uint64_t) : sizeof(uint32_t);
1294
1295 // Collect the external relocation symbols for the pointers.
1296 std::vector> Relocs;
1297 for (const RelocationRef &Reloc : Section.relocations()) {
1298 DataRefImpl Rel;
1299 MachO::any_relocation_info RE;
1300 bool isExtern = false;
1301 Rel = Reloc.getRawDataRefImpl();
1302 RE = O->getRelocation(Rel);
1303 isExtern = O->getPlainRelocationExternal(RE);
1304 if (isExtern) {
1305 uint64_t RelocOffset = Reloc.getOffset();
1306 symbol_iterator RelocSym = Reloc.getSymbol();
1307 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
1308 }
1309 }
1310 array_pod_sort(Relocs.begin(), Relocs.end());
1311
12921312 for (uint32_t i = 0; i < sect_size; i += stride) {
12931313 const char *SymbolName = nullptr;
1314 uint64_t p;
12941315 if (O->is64Bit()) {
12951316 outs() << format("0x%016" PRIx64, sect_addr + i * stride) << " ";
12961317 uint64_t pointer_value;
12981319 if (O->isLittleEndian() != sys::IsLittleEndianHost)
12991320 sys::swapByteOrder(pointer_value);
13001321 outs() << format("0x%016" PRIx64, pointer_value);
1301 if (verbose)
1302 SymbolName = GuessSymbolName(pointer_value, AddrMap);
1322 p = pointer_value;
13031323 } else {
13041324 outs() << format("0x%08" PRIx64, sect_addr + i * stride) << " ";
13051325 uint32_t pointer_value;
13071327 if (O->isLittleEndian() != sys::IsLittleEndianHost)
13081328 sys::swapByteOrder(pointer_value);
13091329 outs() << format("0x%08" PRIx32, pointer_value);
1310 if (verbose)
1311 SymbolName = GuessSymbolName(pointer_value, AddrMap);
1312 }
1313 if (SymbolName)
1314 outs() << " " << SymbolName;
1330 p = pointer_value;
1331 }
1332 if (verbose) {
1333 // First look for an external relocation entry for this pointer.
1334 auto Reloc = find_if(Relocs, [&](const std::pair &P) {
1335 return P.first == i;
1336 });
1337 if (Reloc != Relocs.end()) {
1338 symbol_iterator RelocSym = Reloc->second;
1339 Expected SymName = RelocSym->getName();
1340 if (!SymName)
1341 report_error(O->getFileName(), SymName.takeError());
1342 outs() << " " << *SymName;
1343 } else {
1344 SymbolName = GuessSymbolName(p, AddrMap);
1345 if (SymbolName)
1346 outs() << " " << SymbolName;
1347 }
1348 }
13151349 outs() << "\n";
13161350 }
13171351 }
14621496 break;
14631497 case MachO::S_MOD_INIT_FUNC_POINTERS:
14641498 case MachO::S_MOD_TERM_FUNC_POINTERS:
1465 DumpInitTermPointerSection(O, sect, sect_size, sect_addr, &AddrMap,
1466 verbose);
1499 DumpInitTermPointerSection(O, Section, sect, sect_size, sect_addr,
1500 &AddrMap, verbose);
14671501 break;
14681502 default:
14691503 outs() << "Unknown section type ("