blob: 27e1623d4510c39455cf0c113dd1f4a234f745ae [file] [log] [blame]
Benjamin Kramer0b8b7712011-09-19 17:56:04 +00001//===-- MachODump.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 file implements the MachO-specific dumper for llvm-objdump.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-objdump.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000015#include "llvm/ADT/OwningPtr.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000016#include "llvm/ADT/STLExtras.h"
Ahmed Bougachaef993562013-05-24 01:07:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000018#include "llvm/ADT/Triple.h"
Benjamin Kramer8c930972011-09-21 01:13:19 +000019#include "llvm/DebugInfo/DIContext.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000020#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCDisassembler.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCInstPrinter.h"
24#include "llvm/MC/MCInstrAnalysis.h"
25#include "llvm/MC/MCInstrDesc.h"
26#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachc6449b62012-03-05 19:33:20 +000027#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000028#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000029#include "llvm/Object/MachO.h"
Rafael Espindolada2a2372013-04-13 01:45:40 +000030#include "llvm/Support/Casting.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000031#include "llvm/Support/CommandLine.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/Format.h"
34#include "llvm/Support/GraphWriter.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000035#include "llvm/Support/MachO.h"
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000036#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Support/TargetRegistry.h"
38#include "llvm/Support/TargetSelect.h"
39#include "llvm/Support/raw_ostream.h"
40#include "llvm/Support/system_error.h"
41#include <algorithm>
42#include <cstring>
43using namespace llvm;
44using namespace object;
45
46static cl::opt<bool>
Benjamin Kramer8c930972011-09-21 01:13:19 +000047 UseDbg("g", cl::desc("Print line information from debug info if available"));
48
49static cl::opt<std::string>
50 DSYMFile("dsym", cl::desc("Use .dSYM file for debug info"));
51
Rafael Espindolafd7aa382013-04-18 18:08:55 +000052static const Target *GetTarget(const MachOObjectFile *MachOObj) {
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000053 // Figure out the target triple.
Cameron Zwaricha9935052012-02-03 06:35:22 +000054 if (TripleName.empty()) {
55 llvm::Triple TT("unknown-unknown-unknown");
Rafael Espindola317d3f42013-04-11 03:34:37 +000056 TT.setArch(Triple::ArchType(MachOObj->getArch()));
Cameron Zwaricha9935052012-02-03 06:35:22 +000057 TripleName = TT.str();
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000058 }
59
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000060 // Get the target specific parser.
61 std::string Error;
62 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
63 if (TheTarget)
64 return TheTarget;
65
66 errs() << "llvm-objdump: error: unable to get target for '" << TripleName
67 << "', see --version and --triple.\n";
68 return 0;
69}
70
Owen Anderson481837a2011-10-17 21:37:35 +000071struct SymbolSorter {
72 bool operator()(const SymbolRef &A, const SymbolRef &B) {
73 SymbolRef::Type AType, BType;
74 A.getType(AType);
75 B.getType(BType);
76
77 uint64_t AAddr, BAddr;
78 if (AType != SymbolRef::ST_Function)
79 AAddr = 0;
80 else
81 A.getAddress(AAddr);
82 if (BType != SymbolRef::ST_Function)
83 BAddr = 0;
84 else
85 B.getAddress(BAddr);
86 return AAddr < BAddr;
87 }
Benjamin Kramer0b8b7712011-09-19 17:56:04 +000088};
89
Kevin Enderby54154f32013-06-06 17:20:50 +000090// Types for the storted data in code table that is built before disassembly
91// and the predicate function to sort them.
92typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
93typedef std::vector<DiceTableEntry> DiceTable;
94typedef DiceTable::iterator dice_table_iterator;
95
96static bool
97compareDiceTableEntries(const DiceTableEntry i,
98 const DiceTableEntry j) {
99 return i.first == j.first;
100}
101
102static void DumpDataInCode(const char *bytes, uint64_t Size,
103 unsigned short Kind) {
104 uint64_t Value;
105
106 switch (Kind) {
107 case macho::Data:
108 switch (Size) {
109 case 4:
110 Value = bytes[3] << 24 |
111 bytes[2] << 16 |
112 bytes[1] << 8 |
113 bytes[0];
114 outs() << "\t.long " << Value;
115 break;
116 case 2:
117 Value = bytes[1] << 8 |
118 bytes[0];
119 outs() << "\t.short " << Value;
120 break;
121 case 1:
122 Value = bytes[0];
123 outs() << "\t.byte " << Value;
124 break;
125 }
126 outs() << "\t@ KIND_DATA\n";
127 break;
128 case macho::JumpTable8:
129 Value = bytes[0];
130 outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8";
131 break;
132 case macho::JumpTable16:
133 Value = bytes[1] << 8 |
134 bytes[0];
135 outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16";
136 break;
137 case macho::JumpTable32:
138 Value = bytes[3] << 24 |
139 bytes[2] << 16 |
140 bytes[1] << 8 |
141 bytes[0];
142 outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32";
143 break;
144 default:
145 outs() << "\t@ data in code kind = " << Kind << "\n";
146 break;
147 }
148}
149
Rafael Espindolada2a2372013-04-13 01:45:40 +0000150static void
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000151getSectionsAndSymbols(const macho::Header Header,
152 MachOObjectFile *MachOObj,
Rafael Espindolada2a2372013-04-13 01:45:40 +0000153 std::vector<SectionRef> &Sections,
154 std::vector<SymbolRef> &Symbols,
Kevin Enderby54154f32013-06-06 17:20:50 +0000155 SmallVectorImpl<uint64_t> &FoundFns,
156 uint64_t &BaseSegmentAddress) {
Owen Anderson481837a2011-10-17 21:37:35 +0000157 error_code ec;
158 for (symbol_iterator SI = MachOObj->begin_symbols(),
159 SE = MachOObj->end_symbols(); SI != SE; SI.increment(ec))
160 Symbols.push_back(*SI);
161
162 for (section_iterator SI = MachOObj->begin_sections(),
163 SE = MachOObj->end_sections(); SI != SE; SI.increment(ec)) {
164 SectionRef SR = *SI;
165 StringRef SectName;
166 SR.getName(SectName);
167 Sections.push_back(*SI);
168 }
169
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000170 MachOObjectFile::LoadCommandInfo Command =
171 MachOObj->getFirstLoadCommandInfo();
Kevin Enderby54154f32013-06-06 17:20:50 +0000172 bool BaseSegmentAddressSet = false;
Rafael Espindoladb5f9272013-04-19 11:36:47 +0000173 for (unsigned i = 0; ; ++i) {
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000174 if (Command.C.Type == macho::LCT_FunctionStarts) {
Benjamin Kramer8c930972011-09-21 01:13:19 +0000175 // We found a function starts segment, parse the addresses for later
176 // consumption.
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000177 macho::LinkeditDataLoadCommand LLC =
178 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer8c930972011-09-21 01:13:19 +0000179
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000180 MachOObj->ReadULEB128s(LLC.DataOffset, FoundFns);
Benjamin Kramerafbaf482011-09-21 22:16:43 +0000181 }
Kevin Enderby54154f32013-06-06 17:20:50 +0000182 else if (Command.C.Type == macho::LCT_Segment) {
183 macho::SegmentLoadCommand SLC =
184 MachOObj->getSegmentLoadCommand(Command);
185 StringRef SegName = SLC.Name;
186 if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
187 BaseSegmentAddressSet = true;
188 BaseSegmentAddress = SLC.VMAddress;
189 }
190 }
Rafael Espindoladb5f9272013-04-19 11:36:47 +0000191
192 if (i == Header.NumLoadCommands - 1)
193 break;
194 else
195 Command = MachOObj->getNextLoadCommandInfo(Command);
Benjamin Kramerafbaf482011-09-21 22:16:43 +0000196 }
Benjamin Kramer8c930972011-09-21 01:13:19 +0000197}
198
Rafael Espindolada2a2372013-04-13 01:45:40 +0000199static void DisassembleInputMachO2(StringRef Filename,
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000200 MachOObjectFile *MachOOF);
Rafael Espindolada2a2372013-04-13 01:45:40 +0000201
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000202void llvm::DisassembleInputMachO(StringRef Filename) {
203 OwningPtr<MemoryBuffer> Buff;
204
205 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
206 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n";
207 return;
208 }
209
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000210 OwningPtr<MachOObjectFile> MachOOF(static_cast<MachOObjectFile*>(
Owen Anderson481837a2011-10-17 21:37:35 +0000211 ObjectFile::createMachOObjectFile(Buff.take())));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000212
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000213 DisassembleInputMachO2(Filename, MachOOF.get());
Rafael Espindolada2a2372013-04-13 01:45:40 +0000214}
215
Rafael Espindolada2a2372013-04-13 01:45:40 +0000216static void DisassembleInputMachO2(StringRef Filename,
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000217 MachOObjectFile *MachOOF) {
Rafael Espindolada2a2372013-04-13 01:45:40 +0000218 const Target *TheTarget = GetTarget(MachOOF);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000219 if (!TheTarget) {
220 // GetTarget prints out stuff.
221 return;
222 }
Benjamin Kramerd226ed712011-10-10 13:10:09 +0000223 OwningPtr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000224 OwningPtr<MCInstrAnalysis>
Benjamin Kramerd226ed712011-10-10 13:10:09 +0000225 InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo.get()));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000226
227 // Set up disassembler.
Rafael Espindola4a971702013-05-13 01:16:13 +0000228 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
229 OwningPtr<const MCAsmInfo> AsmInfo(
230 TheTarget->createMCAsmInfo(*MRI, TripleName));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000231 OwningPtr<const MCSubtargetInfo>
232 STI(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000233 OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000234 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Craig Topper17463b32012-04-02 06:09:36 +0000235 OwningPtr<MCInstPrinter>
236 IP(TheTarget->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *InstrInfo,
237 *MRI, *STI));
Benjamin Kramera894c8e2011-09-20 17:53:01 +0000238
239 if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencer3773fb42011-10-07 19:25:47 +0000240 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramera894c8e2011-09-20 17:53:01 +0000241 << TripleName << '\n';
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000242 return;
243 }
244
Benjamin Kramera894c8e2011-09-20 17:53:01 +0000245 outs() << '\n' << Filename << ":\n\n";
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000246
Rafael Espindolafd7aa382013-04-18 18:08:55 +0000247 macho::Header Header = MachOOF->getHeader();
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000248
Ahmed Bougachaef993562013-05-24 01:07:04 +0000249 // FIXME: FoundFns isn't used anymore. Using symbols/LC_FUNCTION_STARTS to
250 // determine function locations will eventually go in MCObjectDisassembler.
251 // FIXME: Using the -cfg command line option, this code used to be able to
252 // annotate relocations with the referenced symbol's name, and if this was
253 // inside a __[cf]string section, the data it points to. This is now replaced
254 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Anderson481837a2011-10-17 21:37:35 +0000255 std::vector<SectionRef> Sections;
256 std::vector<SymbolRef> Symbols;
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000257 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby54154f32013-06-06 17:20:50 +0000258 uint64_t BaseSegmentAddress;
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000259
Kevin Enderby54154f32013-06-06 17:20:50 +0000260 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
261 BaseSegmentAddress);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000262
Benjamin Kramer8c930972011-09-21 01:13:19 +0000263 // Make a copy of the unsorted symbol list. FIXME: duplication
Owen Anderson481837a2011-10-17 21:37:35 +0000264 std::vector<SymbolRef> UnsortedSymbols(Symbols);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000265 // Sort the symbols by address, just in case they didn't come in that way.
Owen Anderson481837a2011-10-17 21:37:35 +0000266 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000267
Kevin Enderby54154f32013-06-06 17:20:50 +0000268 // Build a data in code table that is sorted on by the address of each entry.
269 uint64_t BaseAddress = 0;
270 if (Header.FileType == macho::HFT_Object)
271 Sections[0].getAddress(BaseAddress);
272 else
273 BaseAddress = BaseSegmentAddress;
274 DiceTable Dices;
275 error_code ec;
276 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
277 DI != DE; DI.increment(ec)){
278 uint32_t Offset;
279 DI->getOffset(Offset);
280 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
281 }
282 array_pod_sort(Dices.begin(), Dices.end());
283
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000284#ifndef NDEBUG
285 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
286#else
287 raw_ostream &DebugOut = nulls();
288#endif
289
Benjamin Kramer8c930972011-09-21 01:13:19 +0000290 OwningPtr<DIContext> diContext;
Rafael Espindolada2a2372013-04-13 01:45:40 +0000291 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer8c930972011-09-21 01:13:19 +0000292 // Try to find debug info and set up the DIContext for it.
293 if (UseDbg) {
Benjamin Kramer8c930972011-09-21 01:13:19 +0000294 // A separate DSym file path was specified, parse it as a macho file,
295 // get the sections and supply it to the section name parsing machinery.
296 if (!DSYMFile.empty()) {
297 OwningPtr<MemoryBuffer> Buf;
298 if (error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile.c_str(), Buf)) {
299 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n';
300 return;
301 }
Eric Christopherd1726a42012-11-12 21:40:38 +0000302 DbgObj = ObjectFile::createMachOObjectFile(Buf.take());
Benjamin Kramer8c930972011-09-21 01:13:19 +0000303 }
304
Eric Christopherd1726a42012-11-12 21:40:38 +0000305 // Setup the DIContext
306 diContext.reset(DIContext::getDWARFContext(DbgObj));
Benjamin Kramer8c930972011-09-21 01:13:19 +0000307 }
308
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000309 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Ahmed Bougachaef993562013-05-24 01:07:04 +0000310
311 bool SectIsText = false;
312 Sections[SectIdx].isText(SectIsText);
313 if (SectIsText == false)
314 continue;
315
Owen Anderson481837a2011-10-17 21:37:35 +0000316 StringRef SectName;
317 if (Sections[SectIdx].getName(SectName) ||
Rafael Espindolacef81b32012-12-21 03:47:03 +0000318 SectName != "__text")
Benjamin Kramera894c8e2011-09-20 17:53:01 +0000319 continue; // Skip non-text sections
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000320
Rafael Espindolacef81b32012-12-21 03:47:03 +0000321 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaef993562013-05-24 01:07:04 +0000322
Rafael Espindolaf16c2bb2013-04-05 15:15:22 +0000323 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
324 if (SegmentName != "__TEXT")
Rafael Espindolacef81b32012-12-21 03:47:03 +0000325 continue;
326
Owen Anderson481837a2011-10-17 21:37:35 +0000327 StringRef Bytes;
328 Sections[SectIdx].getContents(Bytes);
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000329 StringRefMemoryObject memoryObject(Bytes);
330 bool symbolTableWorked = false;
331
Benjamin Kramera894c8e2011-09-20 17:53:01 +0000332 // Parse relocations.
Owen Anderson7d3f8b82011-11-07 17:21:36 +0000333 std::vector<std::pair<uint64_t, SymbolRef> > Relocs;
Owen Anderson481837a2011-10-17 21:37:35 +0000334 error_code ec;
335 for (relocation_iterator RI = Sections[SectIdx].begin_relocations(),
336 RE = Sections[SectIdx].end_relocations(); RI != RE; RI.increment(ec)) {
337 uint64_t RelocOffset, SectionAddress;
Rafael Espindola956ca722013-04-25 12:28:45 +0000338 RI->getOffset(RelocOffset);
Owen Anderson481837a2011-10-17 21:37:35 +0000339 Sections[SectIdx].getAddress(SectionAddress);
340 RelocOffset -= SectionAddress;
341
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000342 symbol_iterator RelocSym = RI->getSymbol();
Owen Anderson481837a2011-10-17 21:37:35 +0000343
Rafael Espindola6c1202c2013-06-05 01:33:53 +0000344 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000345 }
346 array_pod_sort(Relocs.begin(), Relocs.end());
347
Benjamin Kramera894c8e2011-09-20 17:53:01 +0000348 // Disassemble symbol by symbol.
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000349 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Owen Anderson481837a2011-10-17 21:37:35 +0000350 StringRef SymName;
351 Symbols[SymIdx].getName(SymName);
352
353 SymbolRef::Type ST;
354 Symbols[SymIdx].getType(ST);
355 if (ST != SymbolRef::ST_Function)
356 continue;
357
Benjamin Kramera894c8e2011-09-20 17:53:01 +0000358 // Make sure the symbol is defined in this section.
Owen Anderson481837a2011-10-17 21:37:35 +0000359 bool containsSym = false;
360 Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym);
361 if (!containsSym)
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000362 continue;
363
Benjamin Kramera894c8e2011-09-20 17:53:01 +0000364 // Start at the address of the symbol relative to the section's address.
Cameron Zwarichec8eac62012-02-03 05:42:17 +0000365 uint64_t SectionAddress = 0;
Owen Anderson481837a2011-10-17 21:37:35 +0000366 uint64_t Start = 0;
Cameron Zwarichec8eac62012-02-03 05:42:17 +0000367 Sections[SectIdx].getAddress(SectionAddress);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000368 Symbols[SymIdx].getAddress(Start);
Cameron Zwarichec8eac62012-02-03 05:42:17 +0000369 Start -= SectionAddress;
Owen Anderson481837a2011-10-17 21:37:35 +0000370
Benjamin Kramera894c8e2011-09-20 17:53:01 +0000371 // Stop disassembling either at the beginning of the next symbol or at
372 // the end of the section.
Kevin Enderby41854ae2012-05-15 18:57:14 +0000373 bool containsNextSym = false;
Owen Anderson481837a2011-10-17 21:37:35 +0000374 uint64_t NextSym = 0;
375 uint64_t NextSymIdx = SymIdx+1;
376 while (Symbols.size() > NextSymIdx) {
377 SymbolRef::Type NextSymType;
378 Symbols[NextSymIdx].getType(NextSymType);
379 if (NextSymType == SymbolRef::ST_Function) {
380 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx],
381 containsNextSym);
Danil Malyshevb0436a72011-11-29 17:40:10 +0000382 Symbols[NextSymIdx].getAddress(NextSym);
Cameron Zwarichec8eac62012-02-03 05:42:17 +0000383 NextSym -= SectionAddress;
Owen Anderson481837a2011-10-17 21:37:35 +0000384 break;
385 }
386 ++NextSymIdx;
387 }
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000388
Owen Anderson481837a2011-10-17 21:37:35 +0000389 uint64_t SectSize;
390 Sections[SectIdx].getSize(SectSize);
391 uint64_t End = containsNextSym ? NextSym : SectSize;
392 uint64_t Size;
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000393
394 symbolTableWorked = true;
395
Ahmed Bougachaef993562013-05-24 01:07:04 +0000396 outs() << SymName << ":\n";
397 DILineInfo lastLine;
398 for (uint64_t Index = Start; Index < End; Index += Size) {
399 MCInst Inst;
Owen Anderson481837a2011-10-17 21:37:35 +0000400
Kevin Enderby54154f32013-06-06 17:20:50 +0000401 uint64_t SectAddress = 0;
402 Sections[SectIdx].getAddress(SectAddress);
403 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
404
405 // Check the data in code table here to see if this is data not an
406 // instruction to be disassembled.
407 DiceTable Dice;
408 Dice.push_back(std::make_pair(SectAddress + Index, DiceRef()));
409 dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(),
410 Dice.begin(), Dice.end(),
411 compareDiceTableEntries);
412 if (DTI != Dices.end()){
413 uint16_t Length;
414 DTI->second.getLength(Length);
415 DumpBytes(StringRef(Bytes.data() + Index, Length));
416 uint16_t Kind;
417 DTI->second.getKind(Kind);
418 DumpDataInCode(Bytes.data() + Index, Length, Kind);
419 continue;
420 }
421
Ahmed Bougachaef993562013-05-24 01:07:04 +0000422 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
423 DebugOut, nulls())) {
Ahmed Bougachaef993562013-05-24 01:07:04 +0000424 DumpBytes(StringRef(Bytes.data() + Index, Size));
425 IP->printInst(&Inst, outs(), "");
Owen Anderson481837a2011-10-17 21:37:35 +0000426
Ahmed Bougachaef993562013-05-24 01:07:04 +0000427 // Print debug info.
428 if (diContext) {
429 DILineInfo dli =
430 diContext->getLineInfoForAddress(SectAddress + Index);
431 // Print valid line info if it changed.
432 if (dli != lastLine && dli.getLine() != 0)
433 outs() << "\t## " << dli.getFileName() << ':'
434 << dli.getLine() << ':' << dli.getColumn();
435 lastLine = dli;
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000436 }
Ahmed Bougachaef993562013-05-24 01:07:04 +0000437 outs() << "\n";
438 } else {
439 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
440 if (Size == 0)
441 Size = 1; // skip illegible bytes
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000442 }
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000443 }
444 }
Ahmed Bougachaef993562013-05-24 01:07:04 +0000445 if (!symbolTableWorked) {
Kevin Enderby59c15e92012-05-18 00:13:56 +0000446 // Reading the symbol table didn't work, disassemble the whole section.
447 uint64_t SectAddress;
448 Sections[SectIdx].getAddress(SectAddress);
449 uint64_t SectSize;
450 Sections[SectIdx].getSize(SectSize);
451 uint64_t InstSize;
452 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendlingf59083c2012-07-19 00:17:40 +0000453 MCInst Inst;
Kevin Enderby59c15e92012-05-18 00:13:56 +0000454
Bill Wendlingf59083c2012-07-19 00:17:40 +0000455 if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index,
456 DebugOut, nulls())) {
457 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
458 DumpBytes(StringRef(Bytes.data() + Index, InstSize));
459 IP->printInst(&Inst, outs(), "");
460 outs() << "\n";
461 } else {
462 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
463 if (InstSize == 0)
464 InstSize = 1; // skip illegible bytes
465 }
Kevin Enderby59c15e92012-05-18 00:13:56 +0000466 }
467 }
Benjamin Kramer0b8b7712011-09-19 17:56:04 +0000468 }
469}