1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 19:52:54 +01:00

[llvm-objdump] Simplify disassembleObject

* Use std::binary_search to replace some std::lower_bound
* Use llvm::upper_bound to replace some std::upper_bound
* Use format_hex and support::endian::read{16,32}

llvm-svn: 357853
This commit is contained in:
Fangrui Song 2019-04-07 05:32:16 +00:00
parent cd412ccdee
commit c65d186d1d
2 changed files with 50 additions and 81 deletions

View File

@ -21,10 +21,10 @@ mystr:
# CHECK: 4: 48 65 6c 6c .word # CHECK: 4: 48 65 6c 6c .word
# CHECK: 8: 6f 2c 20 77 .word # CHECK: 8: 6f 2c 20 77 .word
# CHECK: c: 6f 72 6c 64 .word # CHECK: c: 6f 72 6c 64 .word
# CHECK: 10: 0a 00 .short # CHECK: 10: 0a 00 .short 0x000a
# CHECK: Disassembly of section .myothersection: # CHECK: Disassembly of section .myothersection:
# CHECK: $x.2: # CHECK: $x.2:
# CHECK: 0: 01 00 00 90 adrp x1, #0 # CHECK: 0: 01 00 00 90 adrp x1, #0
# CHECK: mystr: # CHECK: mystr:
# CHECK: 4: 62 6c 61 68 .word # CHECK: 4: 62 6c 61 68 .word
# CHECK: 8: 00 .byte # CHECK: 8: 00 .byte 0x01

View File

@ -969,8 +969,6 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType); AllSymbols[*SecI].emplace_back(Address, *Name, SymbolType);
else else
AbsoluteSymbols.emplace_back(Address, *Name, SymbolType); AbsoluteSymbols.emplace_back(Address, *Name, SymbolType);
} }
if (AllSymbols.empty() && Obj->isELF()) if (AllSymbols.empty() && Obj->isELF())
addDynamicElfSymbols(Obj, AllSymbols); addDynamicElfSymbols(Obj, AllSymbols);
@ -997,19 +995,15 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
error(ExportEntry.getExportRVA(RVA)); error(ExportEntry.getExportRVA(RVA));
uint64_t VA = COFFObj->getImageBase() + RVA; uint64_t VA = COFFObj->getImageBase() + RVA;
auto Sec = std::upper_bound( auto Sec = llvm::upper_bound(
SectionAddresses.begin(), SectionAddresses.end(), VA, SectionAddresses, VA,
[](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) { [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
return LHS < RHS.first; return LHS < RHS.first;
}); });
if (Sec != SectionAddresses.begin()) if (Sec != SectionAddresses.begin()) {
--Sec; --Sec;
else
Sec = SectionAddresses.end();
if (Sec != SectionAddresses.end())
AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE); AllSymbols[Sec->second].emplace_back(VA, Name, ELF::STT_NOTYPE);
else } else
AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE); AbsoluteSymbols.emplace_back(VA, Name, ELF::STT_NOTYPE);
} }
} }
@ -1196,62 +1190,38 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
// understand what we need to dump. If the data marker is within a // understand what we need to dump. If the data marker is within a
// function, it is denoted as a word/short etc // function, it is denoted as a word/short etc
if (isArmElf(Obj) && std::get<2>(Symbols[SI]) != ELF::STT_OBJECT && if (isArmElf(Obj) && std::get<2>(Symbols[SI]) != ELF::STT_OBJECT &&
!DisassembleAll) { !DisassembleAll &&
uint64_t Stride = 0; std::binary_search(DataMappingSymsAddr.begin(),
DataMappingSymsAddr.end(), Index)) {
auto DAI = std::lower_bound(DataMappingSymsAddr.begin(), // Switch to data.
DataMappingSymsAddr.end(), Index); support::endianness Endian =
if (DAI != DataMappingSymsAddr.end() && *DAI == Index) { Obj->isLittleEndian() ? support::little : support::big;
// Switch to data. while (Index < End) {
while (Index < End) { outs() << format("%8" PRIx64 ":", SectionAddr + Index);
outs() << format("%8" PRIx64 ":", SectionAddr + Index); outs() << "\t";
outs() << "\t"; if (Index + 4 <= End) {
if (Index + 4 <= End) { dumpBytes(Bytes.slice(Index, 4), outs());
Stride = 4; outs() << "\t.word\t"
dumpBytes(Bytes.slice(Index, 4), outs()); << format_hex(support::endian::read32(Bytes.data() + Index,
outs() << "\t.word\t"; Endian),
uint32_t Data = 0; 10);
if (Obj->isLittleEndian()) { Index += 4;
const auto Word = } else if (Index + 2 <= End) {
reinterpret_cast<const support::ulittle32_t *>( dumpBytes(Bytes.slice(Index, 2), outs());
Bytes.data() + Index); outs() << "\t\t.short\t"
Data = *Word; << format_hex(support::endian::read16(Bytes.data() + Index,
} else { Endian),
const auto Word = reinterpret_cast<const support::ubig32_t *>( 6);
Bytes.data() + Index); Index += 2;
Data = *Word; } else {
} dumpBytes(Bytes.slice(Index, 1), outs());
outs() << "0x" << format("%08" PRIx32, Data); outs() << "\t\t.byte\t" << format_hex(Bytes[0], 4);
} else if (Index + 2 <= End) { ++Index;
Stride = 2;
dumpBytes(Bytes.slice(Index, 2), outs());
outs() << "\t\t.short\t";
uint16_t Data = 0;
if (Obj->isLittleEndian()) {
const auto Short =
reinterpret_cast<const support::ulittle16_t *>(
Bytes.data() + Index);
Data = *Short;
} else {
const auto Short =
reinterpret_cast<const support::ubig16_t *>(Bytes.data() +
Index);
Data = *Short;
}
outs() << "0x" << format("%04" PRIx16, Data);
} else {
Stride = 1;
dumpBytes(Bytes.slice(Index, 1), outs());
outs() << "\t\t.byte\t";
outs() << "0x" << format("%02" PRIx8, Bytes.slice(Index, 1)[0]);
}
Index += Stride;
outs() << "\n";
auto TAI = std::lower_bound(TextMappingSymsAddr.begin(),
TextMappingSymsAddr.end(), Index);
if (TAI != TextMappingSymsAddr.end() && *TAI == Index)
break;
} }
outs() << "\n";
if (std::binary_search(TextMappingSymsAddr.begin(),
TextMappingSymsAddr.end(), Index))
break;
} }
} }
@ -1348,10 +1318,9 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
// N.B. We don't walk the relocations in the relocatable case yet. // N.B. We don't walk the relocations in the relocatable case yet.
auto *TargetSectionSymbols = &Symbols; auto *TargetSectionSymbols = &Symbols;
if (!Obj->isRelocatableObject()) { if (!Obj->isRelocatableObject()) {
auto SectionAddress = std::upper_bound( auto SectionAddress = llvm::upper_bound(
SectionAddresses.begin(), SectionAddresses.end(), Target, SectionAddresses, Target,
[](uint64_t LHS, [](uint64_t LHS, const std::pair<uint64_t, SectionRef> &RHS) {
const std::pair<uint64_t, SectionRef> &RHS) {
return LHS < RHS.first; return LHS < RHS.first;
}); });
if (SectionAddress != SectionAddresses.begin()) { if (SectionAddress != SectionAddresses.begin()) {
@ -1365,20 +1334,20 @@ static void disassembleObject(const Target *TheTarget, const ObjectFile *Obj,
// Find the first symbol in the section whose offset is less than // Find the first symbol in the section whose offset is less than
// or equal to the target. If there isn't a section that contains // or equal to the target. If there isn't a section that contains
// the target, find the nearest preceding absolute symbol. // the target, find the nearest preceding absolute symbol.
auto TargetSym = std::upper_bound( auto TargetSym = llvm::upper_bound(
TargetSectionSymbols->begin(), TargetSectionSymbols->end(), *TargetSectionSymbols, Target,
Target, [](uint64_t LHS, [](uint64_t LHS,
const std::tuple<uint64_t, StringRef, uint8_t> &RHS) { const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
return LHS < std::get<0>(RHS); return LHS < std::get<0>(RHS);
}); });
if (TargetSym == TargetSectionSymbols->begin()) { if (TargetSym == TargetSectionSymbols->begin()) {
TargetSectionSymbols = &AbsoluteSymbols; TargetSectionSymbols = &AbsoluteSymbols;
TargetSym = std::upper_bound( TargetSym = llvm::upper_bound(
AbsoluteSymbols.begin(), AbsoluteSymbols.end(), AbsoluteSymbols, Target,
Target, [](uint64_t LHS, [](uint64_t LHS,
const std::tuple<uint64_t, StringRef, uint8_t> &RHS) { const std::tuple<uint64_t, StringRef, uint8_t> &RHS) {
return LHS < std::get<0>(RHS); return LHS < std::get<0>(RHS);
}); });
} }
if (TargetSym != TargetSectionSymbols->begin()) { if (TargetSym != TargetSectionSymbols->begin()) {
--TargetSym; --TargetSym;