1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

Slightly refactor things for llvm-objdump and the -macho option so it can be used with

options other than just -disassemble so that universal files can be used with other
options combined with -arch options.

No functional change to existing options and use.  One test case added for the
additional functionality with a universal file an a -arch option.

llvm-svn: 225383
This commit is contained in:
Kevin Enderby 2015-01-07 21:02:18 +00:00
parent fcbdffa536
commit ab9fa91a6e
4 changed files with 104 additions and 47 deletions

View File

@ -17,6 +17,8 @@
// RUN: | FileCheck %s -check-prefix=ROUTINE // RUN: | FileCheck %s -check-prefix=ROUTINE
// RUN: llvm-objdump -p %p/Inputs/exeThread.macho-x86_64 \ // RUN: llvm-objdump -p %p/Inputs/exeThread.macho-x86_64 \
// RUN: | FileCheck %s -check-prefix=THREAD // RUN: | FileCheck %s -check-prefix=THREAD
// RUN: llvm-objdump -macho -p -arch i386 %p/Inputs/macho-universal.x86_64.i386 \
// RUN: | FileCheck %s -check-prefix=FATi386
CHECK: Mach header CHECK: Mach header
CHECK: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags CHECK: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
@ -437,3 +439,7 @@ THREAD: r12 0x0000000000000000 r13 0x0000000000000000 r14 0x000000000000000
THREAD: r15 0x0000000000000000 rip 0x0000000100000d00 THREAD: r15 0x0000000000000000 rip 0x0000000100000d00
THREAD: rflags 0x0000000000000000 cs 0x0000000000000000 fs 0x0000000000000000 THREAD: rflags 0x0000000000000000 cs 0x0000000000000000 fs 0x0000000000000000
THREAD: gs 0x0000000000000000 THREAD: gs 0x0000000000000000
FATi386: Mach header
FATi386: magic cputype cpusubtype caps filetype ncmds sizeofcmds flags
FATi386: MH_MAGIC I386 ALL 0x00 EXECUTE 16 716 NOUNDEFS DYLDLINK TWOLEVEL PIE MH_NO_HEAP_EXECUTION

View File

@ -285,11 +285,57 @@ static bool checkMachOAndArchFlags(ObjectFile *O, StringRef Filename) {
return true; return true;
} }
static void DisassembleInputMachO2(StringRef Filename, MachOObjectFile *MachOOF, static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF);
StringRef ArchiveMemberName = StringRef(),
StringRef ArchitectureName = StringRef());
void llvm::DisassembleInputMachO(StringRef Filename) { // ProcessMachO() is passed a single opened Mach-O file, which may be an
// archive member and or in a slice of a universal file. It prints the
// the file name and header info and then processes it according to the
// command line options.
static void ProcessMachO(StringRef Filename, MachOObjectFile *MachOOF,
StringRef ArchiveMemberName = StringRef(),
StringRef ArchitectureName = StringRef()) {
outs() << Filename;
if (!ArchiveMemberName.empty())
outs() << '(' << ArchiveMemberName << ')';
if (!ArchitectureName.empty())
outs() << " (architecture " << ArchitectureName << ")";
outs() << ":\n";
if (Disassemble)
DisassembleMachO(Filename, MachOOF);
// TODO: These should/could be printed in Darwin's otool(1) or nm(1) style
// for -macho. Or just used a new option that maps to the otool(1)
// option like -r, -l, etc. Or just the normal llvm-objdump option
// but now for this slice so that the -arch options can be used.
// if (Relocations)
// PrintRelocations(MachOOF);
// if (SectionHeaders)
// PrintSectionHeaders(MachOOF);
// if (SectionContents)
// PrintSectionContents(MachOOF);
// if (SymbolTable)
// PrintSymbolTable(MachOOF);
// if (UnwindInfo)
// PrintUnwindInfo(MachOOF);
if (PrivateHeaders)
printMachOFileHeader(MachOOF);
if (ExportsTrie)
printExportsTrie(MachOOF);
if (Rebase)
printRebaseTable(MachOOF);
if (Bind)
printBindTable(MachOOF);
if (LazyBind)
printLazyBindTable(MachOOF);
if (WeakBind)
printWeakBindTable(MachOOF);
}
// ParseInputMachO() parses the named Mach-O file in Filename and handles the
// -arch flags selecting just those slices as specified by them and also parses
// archive files. Then for each individual Mach-O file ProcessMachO() is
// called to process the file based on the command line options.
void llvm::ParseInputMachO(StringRef Filename) {
// Check for -arch all and verifiy the -arch flags are valid. // Check for -arch all and verifiy the -arch flags are valid.
for (unsigned i = 0; i < ArchFlags.size(); ++i) { for (unsigned i = 0; i < ArchFlags.size(); ++i) {
if (ArchFlags[i] == "all") { if (ArchFlags[i] == "all") {
@ -321,7 +367,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { if (MachOObjectFile *O = dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
if (!checkMachOAndArchFlags(O, Filename)) if (!checkMachOAndArchFlags(O, Filename))
return; return;
DisassembleInputMachO2(Filename, O, O->getFileName()); ProcessMachO(Filename, O, O->getFileName());
} }
} }
return; return;
@ -346,7 +392,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
if (ObjOrErr) { if (ObjOrErr) {
ObjectFile &O = *ObjOrErr.get(); ObjectFile &O = *ObjOrErr.get();
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName); ProcessMachO(Filename, MachOOF, "", ArchitectureName);
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) { I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr; std::unique_ptr<Archive> &A = *AOrErr;
@ -362,8 +408,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
continue; continue;
if (MachOObjectFile *O = if (MachOObjectFile *O =
dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
DisassembleInputMachO2(Filename, O, O->getFileName(), ProcessMachO(Filename, O, O->getFileName(), ArchitectureName);
ArchitectureName);
} }
} }
} }
@ -390,7 +435,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
if (ObjOrErr) { if (ObjOrErr) {
ObjectFile &O = *ObjOrErr.get(); ObjectFile &O = *ObjOrErr.get();
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O)) if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&O))
DisassembleInputMachO2(Filename, MachOOF); ProcessMachO(Filename, MachOOF);
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr =
I->getAsArchive()) { I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr; std::unique_ptr<Archive> &A = *AOrErr;
@ -403,7 +448,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
continue; continue;
if (MachOObjectFile *O = if (MachOObjectFile *O =
dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) dyn_cast<MachOObjectFile>(&*ChildOrErr.get()))
DisassembleInputMachO2(Filename, O, O->getFileName()); ProcessMachO(Filename, O, O->getFileName());
} }
} }
return; return;
@ -423,7 +468,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
if (ObjOrErr) { if (ObjOrErr) {
ObjectFile &Obj = *ObjOrErr.get(); ObjectFile &Obj = *ObjOrErr.get();
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj)) if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&Obj))
DisassembleInputMachO2(Filename, MachOOF, "", ArchitectureName); ProcessMachO(Filename, MachOOF, "", ArchitectureName);
} else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) { } else if (ErrorOr<std::unique_ptr<Archive>> AOrErr = I->getAsArchive()) {
std::unique_ptr<Archive> &A = *AOrErr; std::unique_ptr<Archive> &A = *AOrErr;
outs() << "Archive : " << Filename; outs() << "Archive : " << Filename;
@ -438,8 +483,8 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
if (MachOObjectFile *O = if (MachOObjectFile *O =
dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) { dyn_cast<MachOObjectFile>(&*ChildOrErr.get())) {
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O)) if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(O))
DisassembleInputMachO2(Filename, MachOOF, MachOOF->getFileName(), ProcessMachO(Filename, MachOOF, MachOOF->getFileName(),
ArchitectureName); ArchitectureName);
} }
} }
} }
@ -450,7 +495,7 @@ void llvm::DisassembleInputMachO(StringRef Filename) {
if (!checkMachOAndArchFlags(O, Filename)) if (!checkMachOAndArchFlags(O, Filename))
return; return;
if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) { if (MachOObjectFile *MachOOF = dyn_cast<MachOObjectFile>(&*O)) {
DisassembleInputMachO2(Filename, MachOOF); ProcessMachO(Filename, MachOOF);
} else } else
errs() << "llvm-objdump: '" << Filename << "': " errs() << "llvm-objdump: '" << Filename << "': "
<< "Object is not a Mach-O file type.\n"; << "Object is not a Mach-O file type.\n";
@ -1785,9 +1830,7 @@ static void emitComments(raw_svector_ostream &CommentStream,
CommentStream.resync(); CommentStream.resync();
} }
static void DisassembleInputMachO2(StringRef Filename, MachOObjectFile *MachOOF, static void DisassembleMachO(StringRef Filename, MachOObjectFile *MachOOF) {
StringRef ArchiveMemberName,
StringRef ArchitectureName) {
const char *McpuDefault = nullptr; const char *McpuDefault = nullptr;
const Target *ThumbTarget = nullptr; const Target *ThumbTarget = nullptr;
const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget); const Target *TheTarget = GetTarget(MachOOF, &McpuDefault, &ThumbTarget);
@ -1894,13 +1937,6 @@ static void DisassembleInputMachO2(StringRef Filename, MachOObjectFile *MachOOF,
return; return;
} }
outs() << Filename;
if (!ArchiveMemberName.empty())
outs() << '(' << ArchiveMemberName << ')';
if (!ArchitectureName.empty())
outs() << " (architecture " << ArchitectureName << ")";
outs() << ":\n";
MachO::mach_header Header = MachOOF->getHeader(); MachO::mach_header Header = MachOOF->getHeader();
// FIXME: Using the -cfg command line option, this code used to be able to // FIXME: Using the -cfg command line option, this code used to be able to

View File

@ -61,8 +61,8 @@ using namespace object;
static cl::list<std::string> static cl::list<std::string>
InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore); InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
static cl::opt<bool> cl::opt<bool>
Disassemble("disassemble", llvm::Disassemble("disassemble",
cl::desc("Display assembler mnemonics for the machine instructions")); cl::desc("Display assembler mnemonics for the machine instructions"));
static cl::alias static cl::alias
Disassembled("d", cl::desc("Alias for --disassemble"), Disassembled("d", cl::desc("Alias for --disassemble"),
@ -77,20 +77,20 @@ SectionContents("s", cl::desc("Display the content of each section"));
static cl::opt<bool> static cl::opt<bool>
SymbolTable("t", cl::desc("Display the symbol table")); SymbolTable("t", cl::desc("Display the symbol table"));
static cl::opt<bool> cl::opt<bool>
ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols")); llvm::ExportsTrie("exports-trie", cl::desc("Display mach-o exported symbols"));
static cl::opt<bool> cl::opt<bool>
Rebase("rebase", cl::desc("Display mach-o rebasing info")); llvm::Rebase("rebase", cl::desc("Display mach-o rebasing info"));
static cl::opt<bool> cl::opt<bool>
Bind("bind", cl::desc("Display mach-o binding info")); llvm::Bind("bind", cl::desc("Display mach-o binding info"));
static cl::opt<bool> cl::opt<bool>
LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info")); llvm::LazyBind("lazy-bind", cl::desc("Display mach-o lazy binding info"));
static cl::opt<bool> cl::opt<bool>
WeakBind("weak-bind", cl::desc("Display mach-o weak binding info")); llvm::WeakBind("weak-bind", cl::desc("Display mach-o weak binding info"));
static cl::opt<bool> static cl::opt<bool>
MachOOpt("macho", cl::desc("Use MachO specific object file parser")); MachOOpt("macho", cl::desc("Use MachO specific object file parser"));
@ -139,9 +139,9 @@ static cl::alias
UnwindInfoShort("u", cl::desc("Alias for --unwind-info"), UnwindInfoShort("u", cl::desc("Alias for --unwind-info"),
cl::aliasopt(UnwindInfo)); cl::aliasopt(UnwindInfo));
static cl::opt<bool> cl::opt<bool>
PrivateHeaders("private-headers", llvm::PrivateHeaders("private-headers",
cl::desc("Display format specific file headers")); cl::desc("Display format specific file headers"));
static cl::alias static cl::alias
PrivateHeadersShort("p", cl::desc("Alias for --private-headers"), PrivateHeadersShort("p", cl::desc("Alias for --private-headers"),
@ -708,7 +708,7 @@ static void PrintUnwindInfo(const ObjectFile *o) {
} }
} }
static void printExportsTrie(const ObjectFile *o) { void llvm::printExportsTrie(const ObjectFile *o) {
outs() << "Exports trie:\n"; outs() << "Exports trie:\n";
if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
printMachOExportsTrie(MachO); printMachOExportsTrie(MachO);
@ -719,7 +719,7 @@ static void printExportsTrie(const ObjectFile *o) {
} }
} }
static void printRebaseTable(const ObjectFile *o) { void llvm::printRebaseTable(const ObjectFile *o) {
outs() << "Rebase table:\n"; outs() << "Rebase table:\n";
if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
printMachORebaseTable(MachO); printMachORebaseTable(MachO);
@ -730,7 +730,7 @@ static void printRebaseTable(const ObjectFile *o) {
} }
} }
static void printBindTable(const ObjectFile *o) { void llvm::printBindTable(const ObjectFile *o) {
outs() << "Bind table:\n"; outs() << "Bind table:\n";
if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
printMachOBindTable(MachO); printMachOBindTable(MachO);
@ -741,7 +741,7 @@ static void printBindTable(const ObjectFile *o) {
} }
} }
static void printLazyBindTable(const ObjectFile *o) { void llvm::printLazyBindTable(const ObjectFile *o) {
outs() << "Lazy bind table:\n"; outs() << "Lazy bind table:\n";
if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
printMachOLazyBindTable(MachO); printMachOLazyBindTable(MachO);
@ -752,7 +752,7 @@ static void printLazyBindTable(const ObjectFile *o) {
} }
} }
static void printWeakBindTable(const ObjectFile *o) { void llvm::printWeakBindTable(const ObjectFile *o) {
outs() << "Weak bind table:\n"; outs() << "Weak bind table:\n";
if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o)) if (const MachOObjectFile *MachO = dyn_cast<MachOObjectFile>(o))
printMachOWeakBindTable(MachO); printMachOWeakBindTable(MachO);
@ -832,8 +832,11 @@ static void DumpInput(StringRef file) {
return; return;
} }
if (MachOOpt && Disassemble) { // If we are using the Mach-O specific object file parser, then let it parse
DisassembleInputMachO(file); // the file and process the command line options. So the -arch flags can
// be used to select specific slices, etc.
if (MachOOpt) {
ParseInputMachO(file);
return; return;
} }

View File

@ -26,13 +26,20 @@ extern cl::opt<std::string> TripleName;
extern cl::opt<std::string> ArchName; extern cl::opt<std::string> ArchName;
extern cl::opt<std::string> MCPU; extern cl::opt<std::string> MCPU;
extern cl::list<std::string> MAttrs; extern cl::list<std::string> MAttrs;
extern cl::opt<bool> Disassemble;
extern cl::opt<bool> NoShowRawInsn; extern cl::opt<bool> NoShowRawInsn;
extern cl::opt<bool> PrivateHeaders;
extern cl::opt<bool> ExportsTrie;
extern cl::opt<bool> Rebase;
extern cl::opt<bool> Bind;
extern cl::opt<bool> LazyBind;
extern cl::opt<bool> WeakBind;
// Various helper functions. // Various helper functions.
bool error(std::error_code ec); bool error(std::error_code ec);
bool RelocAddressLess(object::RelocationRef a, object::RelocationRef b); bool RelocAddressLess(object::RelocationRef a, object::RelocationRef b);
void DumpBytes(StringRef bytes); void DumpBytes(StringRef bytes);
void DisassembleInputMachO(StringRef Filename); void ParseInputMachO(StringRef Filename);
void printCOFFUnwindInfo(const object::COFFObjectFile* o); void printCOFFUnwindInfo(const object::COFFObjectFile* o);
void printMachOUnwindInfo(const object::MachOObjectFile* o); void printMachOUnwindInfo(const object::MachOObjectFile* o);
void printMachOExportsTrie(const object::MachOObjectFile* o); void printMachOExportsTrie(const object::MachOObjectFile* o);
@ -43,6 +50,11 @@ void printMachOWeakBindTable(const object::MachOObjectFile* o);
void printELFFileHeader(const object::ObjectFile *o); void printELFFileHeader(const object::ObjectFile *o);
void printCOFFFileHeader(const object::ObjectFile *o); void printCOFFFileHeader(const object::ObjectFile *o);
void printMachOFileHeader(const object::ObjectFile *o); void printMachOFileHeader(const object::ObjectFile *o);
void printExportsTrie(const object::ObjectFile *o);
void printRebaseTable(const object::ObjectFile *o);
void printBindTable(const object::ObjectFile *o);
void printLazyBindTable(const object::ObjectFile *o);
void printWeakBindTable(const object::ObjectFile *o);
} // end namespace llvm } // end namespace llvm