return true;
}
-static const Target *getTarget(const ObjectFile *Obj = NULL) {
+static const Target *getTarget(const ObjectFile *Obj = nullptr) {
// Figure out the target triple.
llvm::Triple TheTriple("unknown-unknown-unknown");
if (TripleName.empty()) {
// the best we can do here is indicate that it is mach-o.
if (Obj->isMachO())
TheTriple.setObjectFormat(Triple::MachO);
+
+ if (Obj->isCOFF()) {
+ const auto COFFObj = dyn_cast<COFFObjectFile>(Obj);
+ if (COFFObj->getArch() == Triple::thumb)
+ TheTriple.setTriple("thumbv7-windows");
+ }
}
} else
TheTriple.setTriple(Triple::normalize(TripleName));
Error);
if (!TheTarget) {
errs() << ToolName << ": " << Error;
- return 0;
+ return nullptr;
}
// Update the triple name and return the found target.
return;
}
- std::unique_ptr<MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI));
+ std::unique_ptr<const MCObjectFileInfo> MOFI(new MCObjectFileInfo);
+ MCContext Ctx(AsmInfo.get(), MRI.get(), MOFI.get());
+
+ std::unique_ptr<MCDisassembler> DisAsm(
+ TheTarget->createMCDisassembler(*STI, Ctx));
+
if (!DisAsm) {
errs() << "error: no disassembler for target " << TripleName << "\n";
return;
}
- std::unique_ptr<const MCObjectFileInfo> MOFI;
- std::unique_ptr<MCContext> Ctx;
if (Symbolize) {
- MOFI.reset(new MCObjectFileInfo);
- Ctx.reset(new MCContext(AsmInfo.get(), MRI.get(), MOFI.get()));
std::unique_ptr<MCRelocationInfo> RelInfo(
- TheTarget->createMCRelocationInfo(TripleName, *Ctx.get()));
+ TheTarget->createMCRelocationInfo(TripleName, Ctx));
if (RelInfo) {
std::unique_ptr<MCSymbolizer> Symzer(
- MCObjectSymbolizer::createObjectSymbolizer(*Ctx.get(), RelInfo, Obj));
+ MCObjectSymbolizer::createObjectSymbolizer(Ctx, std::move(RelInfo),
+ Obj));
if (Symzer)
DisAsm->setSymbolizer(std::move(Symzer));
}
}
}
+ StringRef Fmt = Obj->getBytesInAddress() > 4 ? "\t\t%016" PRIx64 ": " :
+ "\t\t\t%08" PRIx64 ": ";
+
// Create a mapping, RelocSecs = SectionRelocMap[S], where sections
// in RelocSecs contain the relocations for section S.
error_code EC;
break;
// Make a list of all the symbols in this section.
- std::vector<std::pair<uint64_t, StringRef> > Symbols;
- for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end();
- SI != SE; ++SI) {
+ std::vector<std::pair<uint64_t, StringRef>> Symbols;
+ for (const SymbolRef &Symbol : Obj->symbols()) {
bool contains;
- if (!error(Section.containsSymbol(*SI, contains)) && contains) {
+ if (!error(Section.containsSymbol(Symbol, contains)) && contains) {
uint64_t Address;
- if (error(SI->getAddress(Address)))
+ if (error(Symbol.getAddress(Address)))
break;
if (Address == UnknownAddressOrSize)
continue;
continue;
StringRef Name;
- if (error(SI->getName(Name)))
+ if (error(Symbol.getName(Name)))
break;
Symbols.push_back(std::make_pair(Address, Name));
}
// Make a list of all the relocations for this section.
std::vector<RelocationRef> Rels;
if (InlineRelocs) {
- SmallVectorImpl<SectionRef> *RelocSecs = &SectionRelocMap[Section];
- for (SmallVectorImpl<SectionRef>::iterator RelocSec = RelocSecs->begin(),
- E = RelocSecs->end();
- RelocSec != E; ++RelocSec) {
- for (relocation_iterator RI = RelocSec->relocation_begin(),
- RE = RelocSec->relocation_end();
- RI != RE; ++RI)
- Rels.push_back(*RI);
+ for (const SectionRef &RelocSec : SectionRelocMap[Section]) {
+ for (const RelocationRef &Reloc : RelocSec.relocations()) {
+ Rels.push_back(Reloc);
+ }
}
}
if (error(rel_cur->getTypeName(name))) goto skip_print_rel;
if (error(rel_cur->getValueString(val))) goto skip_print_rel;
- outs() << format("\t\t\t%8" PRIx64 ": ", SectionAddr + addr) << name
+ outs() << format(Fmt.data(), SectionAddr + addr) << name
<< "\t" << val << "\n";
skip_print_rel:
}
static void PrintRelocations(const ObjectFile *Obj) {
+ StringRef Fmt = Obj->getBytesInAddress() > 4 ? "%016" PRIx64 :
+ "%08" PRIx64;
for (const SectionRef &Section : Obj->sections()) {
if (Section.relocation_begin() == Section.relocation_end())
continue;
if (error(Section.getName(secname)))
continue;
outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
- for (relocation_iterator ri = Section.relocation_begin(),
- re = Section.relocation_end();
- ri != re; ++ri) {
+ for (const RelocationRef &Reloc : Section.relocations()) {
bool hidden;
uint64_t address;
SmallString<32> relocname;
SmallString<32> valuestr;
- if (error(ri->getHidden(hidden))) continue;
- if (hidden) continue;
- if (error(ri->getTypeName(relocname))) continue;
- if (error(ri->getOffset(address))) continue;
- if (error(ri->getValueString(valuestr))) continue;
- outs() << address << " " << relocname << " " << valuestr << "\n";
+ if (error(Reloc.getHidden(hidden)))
+ continue;
+ if (hidden)
+ continue;
+ if (error(Reloc.getTypeName(relocname)))
+ continue;
+ if (error(Reloc.getOffset(address)))
+ continue;
+ if (error(Reloc.getValueString(valuestr)))
+ continue;
+ outs() << format(Fmt.data(), address) << " " << relocname << " "
+ << valuestr << "\n";
}
outs() << "\n";
}
static void PrintCOFFSymbolTable(const COFFObjectFile *coff) {
const coff_file_header *header;
- if (error(coff->getHeader(header))) return;
- int aux_count = 0;
- const coff_symbol *symbol = 0;
- for (int i = 0, e = header->NumberOfSymbols; i != e; ++i) {
- if (aux_count--) {
- // Figure out which type of aux this is.
- if (symbol->StorageClass == COFF::IMAGE_SYM_CLASS_STATIC
- && symbol->Value == 0) { // Section definition.
+ if (error(coff->getHeader(header)))
+ return;
+
+ for (unsigned SI = 0, SE = header->NumberOfSymbols; SI != SE; ++SI) {
+ const coff_symbol *Symbol;
+ StringRef Name;
+ if (error(coff->getSymbol(SI, Symbol)))
+ return;
+
+ if (error(coff->getSymbolName(Symbol, Name)))
+ return;
+
+ outs() << "[" << format("%2d", SI) << "]"
+ << "(sec " << format("%2d", int(Symbol->SectionNumber)) << ")"
+ << "(fl 0x00)" // Flag bits, which COFF doesn't have.
+ << "(ty " << format("%3x", unsigned(Symbol->Type)) << ")"
+ << "(scl " << format("%3x", unsigned(Symbol->StorageClass)) << ") "
+ << "(nx " << unsigned(Symbol->NumberOfAuxSymbols) << ") "
+ << "0x" << format("%08x", unsigned(Symbol->Value)) << " "
+ << Name << "\n";
+
+ for (unsigned AI = 0, AE = Symbol->NumberOfAuxSymbols; AI < AE; ++AI, ++SI) {
+ if (Symbol->isSectionDefinition()) {
const coff_aux_section_definition *asd;
- if (error(coff->getAuxSymbol<coff_aux_section_definition>(i, asd)))
+ if (error(coff->getAuxSymbol<coff_aux_section_definition>(SI + 1, asd)))
return;
+
outs() << "AUX "
<< format("scnlen 0x%x nreloc %d nlnno %d checksum 0x%x "
, unsigned(asd->Length)
<< format("assoc %d comdat %d\n"
, unsigned(asd->Number)
, unsigned(asd->Selection));
- } else
+ } else if (Symbol->isFileRecord()) {
+ const coff_aux_file *AF;
+ if (error(coff->getAuxSymbol<coff_aux_file>(SI + 1, AF)))
+ return;
+
+ StringRef Name(AF->FileName,
+ Symbol->NumberOfAuxSymbols * COFF::SymbolSize);
+ outs() << "AUX " << Name.rtrim(StringRef("\0", 1)) << '\n';
+
+ SI = SI + Symbol->NumberOfAuxSymbols;
+ break;
+ } else {
outs() << "AUX Unknown\n";
- } else {
- StringRef name;
- if (error(coff->getSymbol(i, symbol))) return;
- if (error(coff->getSymbolName(symbol, name))) return;
- outs() << "[" << format("%2d", i) << "]"
- << "(sec " << format("%2d", int(symbol->SectionNumber)) << ")"
- << "(fl 0x00)" // Flag bits, which COFF doesn't have.
- << "(ty " << format("%3x", unsigned(symbol->Type)) << ")"
- << "(scl " << format("%3x", unsigned(symbol->StorageClass)) << ") "
- << "(nx " << unsigned(symbol->NumberOfAuxSymbols) << ") "
- << "0x" << format("%08x", unsigned(symbol->Value)) << " "
- << name << "\n";
- aux_count = symbol->NumberOfAuxSymbols;
+ }
}
}
}
static void PrintSymbolTable(const ObjectFile *o) {
outs() << "SYMBOL TABLE:\n";
- if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o))
+ if (const COFFObjectFile *coff = dyn_cast<const COFFObjectFile>(o)) {
PrintCOFFSymbolTable(coff);
- else {
- for (symbol_iterator si = o->symbol_begin(), se = o->symbol_end();
- si != se; ++si) {
- StringRef Name;
- uint64_t Address;
- SymbolRef::Type Type;
- uint64_t Size;
- uint32_t Flags = si->getFlags();
- section_iterator Section = o->section_end();
- if (error(si->getName(Name))) continue;
- if (error(si->getAddress(Address))) continue;
- if (error(si->getType(Type))) continue;
- if (error(si->getSize(Size))) continue;
- if (error(si->getSection(Section))) continue;
-
- bool Global = Flags & SymbolRef::SF_Global;
- bool Weak = Flags & SymbolRef::SF_Weak;
- bool Absolute = Flags & SymbolRef::SF_Absolute;
-
- if (Address == UnknownAddressOrSize)
- Address = 0;
- if (Size == UnknownAddressOrSize)
- Size = 0;
- char GlobLoc = ' ';
- if (Type != SymbolRef::ST_Unknown)
- GlobLoc = Global ? 'g' : 'l';
- char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
- ? 'd' : ' ';
- char FileFunc = ' ';
- if (Type == SymbolRef::ST_File)
- FileFunc = 'f';
- else if (Type == SymbolRef::ST_Function)
- FileFunc = 'F';
-
- const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
- "%08" PRIx64;
-
- outs() << format(Fmt, Address) << " "
- << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
- << (Weak ? 'w' : ' ') // Weak?
- << ' ' // Constructor. Not supported yet.
- << ' ' // Warning. Not supported yet.
- << ' ' // Indirect reference to another symbol.
- << Debug // Debugging (d) or dynamic (D) symbol.
- << FileFunc // Name of function (F), file (f) or object (O).
- << ' ';
- if (Absolute)
- outs() << "*ABS*";
- else if (Section == o->section_end())
- outs() << "*UND*";
- else {
- if (const MachOObjectFile *MachO =
- dyn_cast<const MachOObjectFile>(o)) {
- DataRefImpl DR = Section->getRawDataRefImpl();
- StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
- outs() << SegmentName << ",";
- }
- StringRef SectionName;
- if (error(Section->getName(SectionName)))
- SectionName = "";
- outs() << SectionName;
+ return;
+ }
+ for (const SymbolRef &Symbol : o->symbols()) {
+ StringRef Name;
+ uint64_t Address;
+ SymbolRef::Type Type;
+ uint64_t Size;
+ uint32_t Flags = Symbol.getFlags();
+ section_iterator Section = o->section_end();
+ if (error(Symbol.getName(Name)))
+ continue;
+ if (error(Symbol.getAddress(Address)))
+ continue;
+ if (error(Symbol.getType(Type)))
+ continue;
+ if (error(Symbol.getSize(Size)))
+ continue;
+ if (error(Symbol.getSection(Section)))
+ continue;
+
+ bool Global = Flags & SymbolRef::SF_Global;
+ bool Weak = Flags & SymbolRef::SF_Weak;
+ bool Absolute = Flags & SymbolRef::SF_Absolute;
+
+ if (Address == UnknownAddressOrSize)
+ Address = 0;
+ if (Size == UnknownAddressOrSize)
+ Size = 0;
+ char GlobLoc = ' ';
+ if (Type != SymbolRef::ST_Unknown)
+ GlobLoc = Global ? 'g' : 'l';
+ char Debug = (Type == SymbolRef::ST_Debug || Type == SymbolRef::ST_File)
+ ? 'd' : ' ';
+ char FileFunc = ' ';
+ if (Type == SymbolRef::ST_File)
+ FileFunc = 'f';
+ else if (Type == SymbolRef::ST_Function)
+ FileFunc = 'F';
+
+ const char *Fmt = o->getBytesInAddress() > 4 ? "%016" PRIx64 :
+ "%08" PRIx64;
+
+ outs() << format(Fmt, Address) << " "
+ << GlobLoc // Local -> 'l', Global -> 'g', Neither -> ' '
+ << (Weak ? 'w' : ' ') // Weak?
+ << ' ' // Constructor. Not supported yet.
+ << ' ' // Warning. Not supported yet.
+ << ' ' // Indirect reference to another symbol.
+ << Debug // Debugging (d) or dynamic (D) symbol.
+ << FileFunc // Name of function (F), file (f) or object (O).
+ << ' ';
+ if (Absolute) {
+ outs() << "*ABS*";
+ } else if (Section == o->section_end()) {
+ outs() << "*UND*";
+ } else {
+ if (const MachOObjectFile *MachO =
+ dyn_cast<const MachOObjectFile>(o)) {
+ DataRefImpl DR = Section->getRawDataRefImpl();
+ StringRef SegmentName = MachO->getSectionFinalSegmentName(DR);
+ outs() << SegmentName << ",";
}
- outs() << '\t'
- << format("%08" PRIx64 " ", Size)
- << Name
- << '\n';
+ StringRef SectionName;
+ if (error(Section->getName(SectionName)))
+ SectionName = "";
+ outs() << SectionName;
}
+ outs() << '\t'
+ << format("%08" PRIx64 " ", Size)
+ << Name
+ << '\n';
}
}