xref: /llvm-project/llvm/tools/llvm-objdump/llvm-objdump.cpp (revision ba4a36227611e92851151e8f0e67201348cf1b14)
1 //===-- llvm-objdump.cpp - Object file dumping utility for llvm -----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This program is a utility that works like binutils "objdump", that is, it
11 // dumps out a plethora of information about an object file depending on the
12 // flags.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm-objdump.h"
17 #include "MCFunction.h"
18 #include "llvm/Object/Archive.h"
19 #include "llvm/Object/ObjectFile.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCDisassembler.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCInstPrinter.h"
27 #include "llvm/MC/MCInstrAnalysis.h"
28 #include "llvm/MC/MCInstrDesc.h"
29 #include "llvm/MC/MCInstrInfo.h"
30 #include "llvm/MC/MCSubtargetInfo.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/CommandLine.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/Format.h"
36 #include "llvm/Support/GraphWriter.h"
37 #include "llvm/Support/Host.h"
38 #include "llvm/Support/ManagedStatic.h"
39 #include "llvm/Support/MemoryBuffer.h"
40 #include "llvm/Support/MemoryObject.h"
41 #include "llvm/Support/PrettyStackTrace.h"
42 #include "llvm/Support/Signals.h"
43 #include "llvm/Support/SourceMgr.h"
44 #include "llvm/Support/TargetRegistry.h"
45 #include "llvm/Support/TargetSelect.h"
46 #include "llvm/Support/raw_ostream.h"
47 #include "llvm/Support/system_error.h"
48 #include <algorithm>
49 #include <cstring>
50 using namespace llvm;
51 using namespace object;
52 
53 static cl::list<std::string>
54 InputFilenames(cl::Positional, cl::desc("<input object files>"),cl::ZeroOrMore);
55 
56 static cl::opt<bool>
57 Disassemble("disassemble",
58   cl::desc("Display assembler mnemonics for the machine instructions"));
59 static cl::alias
60 Disassembled("d", cl::desc("Alias for --disassemble"),
61              cl::aliasopt(Disassemble));
62 
63 static cl::opt<bool>
64 Relocations("r", cl::desc("Display the relocation entries in the file"));
65 
66 static cl::opt<bool>
67 MachO("macho", cl::desc("Use MachO specific object file parser"));
68 static cl::alias
69 MachOm("m", cl::desc("Alias for --macho"), cl::aliasopt(MachO));
70 
71 cl::opt<std::string>
72 llvm::TripleName("triple", cl::desc("Target triple to disassemble for, "
73                                     "see -version for available targets"));
74 
75 cl::opt<std::string>
76 llvm::ArchName("arch", cl::desc("Target arch to disassemble for, "
77                                 "see -version for available targets"));
78 
79 static StringRef ToolName;
80 
81 static bool error(error_code ec) {
82   if (!ec) return false;
83 
84   outs() << ToolName << ": error reading file: " << ec.message() << ".\n";
85   outs().flush();
86   return true;
87 }
88 
89 static const Target *GetTarget(const ObjectFile *Obj = NULL) {
90   // Figure out the target triple.
91   llvm::Triple TT("unknown-unknown-unknown");
92   if (TripleName.empty()) {
93     if (Obj)
94       TT.setArch(Triple::ArchType(Obj->getArch()));
95   } else
96     TT.setTriple(Triple::normalize(TripleName));
97 
98   if (!ArchName.empty())
99     TT.setArchName(ArchName);
100 
101   TripleName = TT.str();
102 
103   // Get the target specific parser.
104   std::string Error;
105   const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
106   if (TheTarget)
107     return TheTarget;
108 
109   errs() << ToolName << ": error: unable to get target for '" << TripleName
110          << "', see --version and --triple.\n";
111   return 0;
112 }
113 
114 void llvm::DumpBytes(StringRef bytes) {
115   static const char hex_rep[] = "0123456789abcdef";
116   // FIXME: The real way to do this is to figure out the longest instruction
117   //        and align to that size before printing. I'll fix this when I get
118   //        around to outputting relocations.
119   // 15 is the longest x86 instruction
120   // 3 is for the hex rep of a byte + a space.
121   // 1 is for the null terminator.
122   enum { OutputSize = (15 * 3) + 1 };
123   char output[OutputSize];
124 
125   assert(bytes.size() <= 15
126     && "DumpBytes only supports instructions of up to 15 bytes");
127   memset(output, ' ', sizeof(output));
128   unsigned index = 0;
129   for (StringRef::iterator i = bytes.begin(),
130                            e = bytes.end(); i != e; ++i) {
131     output[index] = hex_rep[(*i & 0xF0) >> 4];
132     output[index + 1] = hex_rep[*i & 0xF];
133     index += 3;
134   }
135 
136   output[sizeof(output) - 1] = 0;
137   outs() << output;
138 }
139 
140 static void DisassembleObject(const ObjectFile *Obj) {
141   const Target *TheTarget = GetTarget(Obj);
142   if (!TheTarget) {
143     // GetTarget prints out stuff.
144     return;
145   }
146   const MCInstrInfo *InstrInfo = TheTarget->createMCInstrInfo();
147   OwningPtr<MCInstrAnalysis>
148     InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo));
149 
150   outs() << '\n';
151   outs() << Obj->getFileName()
152          << ":\tfile format " << Obj->getFileFormatName() << "\n\n";
153 
154   error_code ec;
155   for (section_iterator i = Obj->begin_sections(),
156                         e = Obj->end_sections();
157                         i != e; i.increment(ec)) {
158     if (error(ec)) break;
159     bool text;
160     if (error(i->isText(text))) break;
161     if (!text) continue;
162 
163     // Make a list of all the symbols in this section.
164     std::vector<std::pair<uint64_t, StringRef> > Symbols;
165     for (symbol_iterator si = Obj->begin_symbols(),
166                          se = Obj->end_symbols();
167                          si != se; si.increment(ec)) {
168       bool contains;
169       if (!error(i->containsSymbol(*si, contains)) && contains) {
170         uint64_t Address;
171         if (error(si->getOffset(Address))) break;
172         StringRef Name;
173         if (error(si->getName(Name))) break;
174         Symbols.push_back(std::make_pair(Address, Name));
175       }
176     }
177 
178     // Sort the symbols by address, just in case they didn't come in that way.
179     array_pod_sort(Symbols.begin(), Symbols.end());
180 
181     StringRef name;
182     if (error(i->getName(name))) break;
183     outs() << "Disassembly of section " << name << ':';
184 
185     // If the section has no symbols just insert a dummy one and disassemble
186     // the whole section.
187     if (Symbols.empty())
188       Symbols.push_back(std::make_pair(0, name));
189 
190     // Set up disassembler.
191     OwningPtr<const MCAsmInfo> AsmInfo(TheTarget->createMCAsmInfo(TripleName));
192 
193     if (!AsmInfo) {
194       errs() << "error: no assembly info for target " << TripleName << "\n";
195       return;
196     }
197 
198     OwningPtr<const MCSubtargetInfo> STI(
199       TheTarget->createMCSubtargetInfo(TripleName, "", ""));
200 
201     if (!STI) {
202       errs() << "error: no subtarget info for target " << TripleName << "\n";
203       return;
204     }
205 
206     OwningPtr<const MCDisassembler> DisAsm(
207       TheTarget->createMCDisassembler(*STI));
208     if (!DisAsm) {
209       errs() << "error: no disassembler for target " << TripleName << "\n";
210       return;
211     }
212 
213     int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
214     OwningPtr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
215                                 AsmPrinterVariant, *AsmInfo, *STI));
216     if (!IP) {
217       errs() << "error: no instruction printer for target " << TripleName
218              << '\n';
219       return;
220     }
221 
222     StringRef Bytes;
223     if (error(i->getContents(Bytes))) break;
224     StringRefMemoryObject memoryObject(Bytes);
225     uint64_t Size;
226     uint64_t Index;
227     uint64_t SectSize;
228     if (error(i->getSize(SectSize))) break;
229 
230     // Disassemble symbol by symbol.
231     for (unsigned si = 0, se = Symbols.size(); si != se; ++si) {
232       uint64_t Start = Symbols[si].first;
233       uint64_t End = si == se-1 ? SectSize : Symbols[si + 1].first - 1;
234       outs() << '\n' << Symbols[si].second << ":\n";
235 
236 #ifndef NDEBUG
237         raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
238 #else
239         raw_ostream &DebugOut = nulls();
240 #endif
241 
242       for (Index = Start; Index < End; Index += Size) {
243         MCInst Inst;
244 
245         if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
246                                    DebugOut, nulls())) {
247           uint64_t addr;
248           if (error(i->getAddress(addr))) break;
249           outs() << format("%8x:\t", addr + Index);
250           DumpBytes(StringRef(Bytes.data() + Index, Size));
251           IP->printInst(&Inst, outs(), "");
252           outs() << "\n";
253         } else {
254           errs() << ToolName << ": warning: invalid instruction encoding\n";
255           if (Size == 0)
256             Size = 1; // skip illegible bytes
257         }
258       }
259     }
260   }
261 }
262 
263 static void PrintRelocations(const ObjectFile *o) {
264   error_code ec;
265   for (section_iterator si = o->begin_sections(), se = o->end_sections();
266                                                   si != se; si.increment(ec)){
267     if (error(ec)) return;
268     if (si->begin_relocations() == si->end_relocations())
269       continue;
270     StringRef secname;
271     if (error(si->getName(secname))) continue;
272     outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
273     for (relocation_iterator ri = si->begin_relocations(),
274                              re = si->end_relocations();
275                              ri != re; ri.increment(ec)) {
276       if (error(ec)) return;
277 
278       uint64_t address;
279       SmallString<32> relocname;
280       SmallString<32> valuestr;
281       if (error(ri->getTypeName(relocname))) continue;
282       if (error(ri->getAddress(address))) continue;
283       if (error(ri->getValueString(valuestr))) continue;
284       outs() << address << " " << relocname << " " << valuestr << "\n";
285     }
286     outs() << "\n";
287   }
288 }
289 
290 static void DumpObject(const ObjectFile *o) {
291   if (Disassemble)
292     DisassembleObject(o);
293   if (Relocations)
294     PrintRelocations(o);
295 }
296 
297 /// @brief Dump each object file in \a a;
298 static void DumpArchive(const Archive *a) {
299   for (Archive::child_iterator i = a->begin_children(),
300                                e = a->end_children(); i != e; ++i) {
301     OwningPtr<Binary> child;
302     if (error_code ec = i->getAsBinary(child)) {
303       errs() << ToolName << ": '" << a->getFileName() << "': " << ec.message()
304              << ".\n";
305       continue;
306     }
307     if (ObjectFile *o = dyn_cast<ObjectFile>(child.get()))
308       DumpObject(o);
309     else
310       errs() << ToolName << ": '" << a->getFileName() << "': "
311               << "Unrecognized file type.\n";
312   }
313 }
314 
315 /// @brief Open file and figure out how to dump it.
316 static void DumpInput(StringRef file) {
317   // If file isn't stdin, check that it exists.
318   if (file != "-" && !sys::fs::exists(file)) {
319     errs() << ToolName << ": '" << file << "': " << "No such file\n";
320     return;
321   }
322 
323   if (MachO && Disassemble) {
324     DisassembleInputMachO(file);
325     return;
326   }
327 
328   // Attempt to open the binary.
329   OwningPtr<Binary> binary;
330   if (error_code ec = createBinary(file, binary)) {
331     errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
332     return;
333   }
334 
335   if (Archive *a = dyn_cast<Archive>(binary.get())) {
336     DumpArchive(a);
337   } else if (ObjectFile *o = dyn_cast<ObjectFile>(binary.get())) {
338     DumpObject(o);
339   } else {
340     errs() << ToolName << ": '" << file << "': " << "Unrecognized file type.\n";
341   }
342 }
343 
344 int main(int argc, char **argv) {
345   // Print a stack trace if we signal out.
346   sys::PrintStackTraceOnErrorSignal();
347   PrettyStackTraceProgram X(argc, argv);
348   llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
349 
350   // Initialize targets and assembly printers/parsers.
351   llvm::InitializeAllTargetInfos();
352   llvm::InitializeAllTargetMCs();
353   llvm::InitializeAllAsmParsers();
354   llvm::InitializeAllDisassemblers();
355 
356   cl::ParseCommandLineOptions(argc, argv, "llvm object file dumper\n");
357   TripleName = Triple::normalize(TripleName);
358 
359   ToolName = argv[0];
360 
361   // Defaults to a.out if no filenames specified.
362   if (InputFilenames.size() == 0)
363     InputFilenames.push_back("a.out");
364 
365   if (!Disassemble && !Relocations) {
366     cl::PrintHelpMessage();
367     return 2;
368   }
369 
370   std::for_each(InputFilenames.begin(), InputFilenames.end(),
371                 DumpInput);
372 
373   return 0;
374 }
375