blob: 89b038fc068ceee28a9a339bf53dcd8c6af25864 [file] [log] [blame]
Benjamin Kramer43a772e2011-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 Kramer43a772e2011-09-19 17:56:04 +000015#include "llvm/ADT/STLExtras.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000016#include "llvm/ADT/StringExtras.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000017#include "llvm/ADT/Triple.h"
Benjamin Kramer699128e2011-09-21 01:13:19 +000018#include "llvm/DebugInfo/DIContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000019#include "llvm/MC/MCAsmInfo.h"
20#include "llvm/MC/MCDisassembler.h"
21#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/MC/MCInstrAnalysis.h"
24#include "llvm/MC/MCInstrDesc.h"
25#include "llvm/MC/MCInstrInfo.h"
Jim Grosbachfd93a592012-03-05 19:33:20 +000026#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000027#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000028#include "llvm/Object/MachO.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000029#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000030#include "llvm/Support/CommandLine.h"
31#include "llvm/Support/Debug.h"
32#include "llvm/Support/Format.h"
33#include "llvm/Support/GraphWriter.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000034#include "llvm/Support/MachO.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000035#include "llvm/Support/MemoryBuffer.h"
36#include "llvm/Support/TargetRegistry.h"
37#include "llvm/Support/TargetSelect.h"
38#include "llvm/Support/raw_ostream.h"
39#include "llvm/Support/system_error.h"
40#include <algorithm>
41#include <cstring>
42using namespace llvm;
43using namespace object;
44
45static cl::opt<bool>
Benjamin Kramer699128e2011-09-21 01:13:19 +000046 UseDbg("g", cl::desc("Print line information from debug info if available"));
47
48static cl::opt<std::string>
49 DSYMFile("dsym", cl::desc("Use .dSYM file for debug info"));
50
Rafael Espindola56f976f2013-04-18 18:08:55 +000051static const Target *GetTarget(const MachOObjectFile *MachOObj) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +000052 // Figure out the target triple.
Cameron Zwarich88cc16a2012-02-03 06:35:22 +000053 if (TripleName.empty()) {
54 llvm::Triple TT("unknown-unknown-unknown");
Rafael Espindola93f4a622013-04-11 03:34:37 +000055 TT.setArch(Triple::ArchType(MachOObj->getArch()));
Cameron Zwarich88cc16a2012-02-03 06:35:22 +000056 TripleName = TT.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +000057 }
58
Benjamin Kramer43a772e2011-09-19 17:56:04 +000059 // Get the target specific parser.
60 std::string Error;
61 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
62 if (TheTarget)
63 return TheTarget;
64
65 errs() << "llvm-objdump: error: unable to get target for '" << TripleName
66 << "', see --version and --triple.\n";
67 return 0;
68}
69
Owen Andersond9243c42011-10-17 21:37:35 +000070struct SymbolSorter {
71 bool operator()(const SymbolRef &A, const SymbolRef &B) {
72 SymbolRef::Type AType, BType;
73 A.getType(AType);
74 B.getType(BType);
75
76 uint64_t AAddr, BAddr;
77 if (AType != SymbolRef::ST_Function)
78 AAddr = 0;
79 else
80 A.getAddress(AAddr);
81 if (BType != SymbolRef::ST_Function)
82 BAddr = 0;
83 else
84 B.getAddress(BAddr);
85 return AAddr < BAddr;
86 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +000087};
88
Kevin Enderby273ae012013-06-06 17:20:50 +000089// Types for the storted data in code table that is built before disassembly
90// and the predicate function to sort them.
91typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
92typedef std::vector<DiceTableEntry> DiceTable;
93typedef DiceTable::iterator dice_table_iterator;
94
95static bool
96compareDiceTableEntries(const DiceTableEntry i,
97 const DiceTableEntry j) {
98 return i.first == j.first;
99}
100
101static void DumpDataInCode(const char *bytes, uint64_t Size,
102 unsigned short Kind) {
103 uint64_t Value;
104
105 switch (Kind) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000106 case MachO::DICE_KIND_DATA:
Kevin Enderby273ae012013-06-06 17:20:50 +0000107 switch (Size) {
108 case 4:
109 Value = bytes[3] << 24 |
110 bytes[2] << 16 |
111 bytes[1] << 8 |
112 bytes[0];
113 outs() << "\t.long " << Value;
114 break;
115 case 2:
116 Value = bytes[1] << 8 |
117 bytes[0];
118 outs() << "\t.short " << Value;
119 break;
120 case 1:
121 Value = bytes[0];
122 outs() << "\t.byte " << Value;
123 break;
124 }
125 outs() << "\t@ KIND_DATA\n";
126 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000127 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby273ae012013-06-06 17:20:50 +0000128 Value = bytes[0];
129 outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8";
130 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000131 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby273ae012013-06-06 17:20:50 +0000132 Value = bytes[1] << 8 |
133 bytes[0];
134 outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16";
135 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000136 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby273ae012013-06-06 17:20:50 +0000137 Value = bytes[3] << 24 |
138 bytes[2] << 16 |
139 bytes[1] << 8 |
140 bytes[0];
141 outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32";
142 break;
143 default:
144 outs() << "\t@ data in code kind = " << Kind << "\n";
145 break;
146 }
147}
148
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000149static void getSectionsAndSymbols(const MachO::mach_header Header,
150 MachOObjectFile *MachOObj,
151 std::vector<SectionRef> &Sections,
152 std::vector<SymbolRef> &Symbols,
153 SmallVectorImpl<uint64_t> &FoundFns,
154 uint64_t &BaseSegmentAddress) {
155 for (const SymbolRef &Symbol : MachOObj->symbols())
156 Symbols.push_back(Symbol);
Owen Andersond9243c42011-10-17 21:37:35 +0000157
Alexey Samsonov48803e52014-03-13 14:37:36 +0000158 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000159 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000160 Section.getName(SectName);
161 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000162 }
163
Rafael Espindola56f976f2013-04-18 18:08:55 +0000164 MachOObjectFile::LoadCommandInfo Command =
Alexey Samsonov48803e52014-03-13 14:37:36 +0000165 MachOObj->getFirstLoadCommandInfo();
Kevin Enderby273ae012013-06-06 17:20:50 +0000166 bool BaseSegmentAddressSet = false;
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000167 for (unsigned i = 0; ; ++i) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000168 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000169 // We found a function starts segment, parse the addresses for later
170 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000171 MachO::linkedit_data_command LLC =
Rafael Espindola56f976f2013-04-18 18:08:55 +0000172 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000173
Charles Davis8bdfafd2013-09-01 04:28:48 +0000174 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000175 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000176 else if (Command.C.cmd == MachO::LC_SEGMENT) {
177 MachO::segment_command SLC =
Kevin Enderby273ae012013-06-06 17:20:50 +0000178 MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000179 StringRef SegName = SLC.segname;
Kevin Enderby273ae012013-06-06 17:20:50 +0000180 if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
181 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000182 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000183 }
184 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000185
Charles Davis8bdfafd2013-09-01 04:28:48 +0000186 if (i == Header.ncmds - 1)
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000187 break;
188 else
189 Command = MachOObj->getNextLoadCommandInfo(Command);
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000190 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000191}
192
Rafael Espindola9b709252013-04-13 01:45:40 +0000193static void DisassembleInputMachO2(StringRef Filename,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000194 MachOObjectFile *MachOOF);
Rafael Espindola9b709252013-04-13 01:45:40 +0000195
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000196void llvm::DisassembleInputMachO(StringRef Filename) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000197 std::unique_ptr<MemoryBuffer> Buff;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000198
199 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
200 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n";
201 return;
202 }
203
Ahmed Charles56440fd2014-03-06 05:51:42 +0000204 std::unique_ptr<MachOObjectFile> MachOOF(static_cast<MachOObjectFile *>(
Ahmed Charles96c9d952014-03-05 10:19:29 +0000205 ObjectFile::createMachOObjectFile(Buff.release()).get()));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000206
Rafael Espindola56f976f2013-04-18 18:08:55 +0000207 DisassembleInputMachO2(Filename, MachOOF.get());
Rafael Espindola9b709252013-04-13 01:45:40 +0000208}
209
Rafael Espindola9b709252013-04-13 01:45:40 +0000210static void DisassembleInputMachO2(StringRef Filename,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000211 MachOObjectFile *MachOOF) {
Rafael Espindola9b709252013-04-13 01:45:40 +0000212 const Target *TheTarget = GetTarget(MachOOF);
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000213 if (!TheTarget) {
214 // GetTarget prints out stuff.
215 return;
216 }
Ahmed Charles56440fd2014-03-06 05:51:42 +0000217 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
218 std::unique_ptr<MCInstrAnalysis> InstrAnalysis(
219 TheTarget->createMCInstrAnalysis(InstrInfo.get()));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000220
221 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000222 std::unique_ptr<const MCRegisterInfo> MRI(
223 TheTarget->createMCRegInfo(TripleName));
224 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +0000225 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +0000226 std::unique_ptr<const MCSubtargetInfo> STI(
227 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
228 std::unique_ptr<const MCDisassembler> DisAsm(
229 TheTarget->createMCDisassembler(*STI));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000230 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000231 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
232 AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI));
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000233
234 if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +0000235 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000236 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000237 return;
238 }
239
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000240 outs() << '\n' << Filename << ":\n\n";
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000241
Charles Davis8bdfafd2013-09-01 04:28:48 +0000242 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000243
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000244 // FIXME: FoundFns isn't used anymore. Using symbols/LC_FUNCTION_STARTS to
245 // determine function locations will eventually go in MCObjectDisassembler.
246 // FIXME: Using the -cfg command line option, this code used to be able to
247 // annotate relocations with the referenced symbol's name, and if this was
248 // inside a __[cf]string section, the data it points to. This is now replaced
249 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +0000250 std::vector<SectionRef> Sections;
251 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000252 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +0000253 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000254
Kevin Enderby273ae012013-06-06 17:20:50 +0000255 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
256 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000257
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000258 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +0000259 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000260
Kevin Enderby273ae012013-06-06 17:20:50 +0000261 // Build a data in code table that is sorted on by the address of each entry.
262 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000263 if (Header.filetype == MachO::MH_OBJECT)
Kevin Enderby273ae012013-06-06 17:20:50 +0000264 Sections[0].getAddress(BaseAddress);
265 else
266 BaseAddress = BaseSegmentAddress;
267 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +0000268 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000269 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000270 uint32_t Offset;
271 DI->getOffset(Offset);
272 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
273 }
274 array_pod_sort(Dices.begin(), Dices.end());
275
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000276#ifndef NDEBUG
277 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
278#else
279 raw_ostream &DebugOut = nulls();
280#endif
281
Ahmed Charles56440fd2014-03-06 05:51:42 +0000282 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +0000283 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +0000284 // Try to find debug info and set up the DIContext for it.
285 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000286 // A separate DSym file path was specified, parse it as a macho file,
287 // get the sections and supply it to the section name parsing machinery.
288 if (!DSYMFile.empty()) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000289 std::unique_ptr<MemoryBuffer> Buf;
Rafael Espindola8c811722013-06-25 05:28:34 +0000290 if (error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile, Buf)) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000291 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n';
292 return;
293 }
Ahmed Charles96c9d952014-03-05 10:19:29 +0000294 DbgObj = ObjectFile::createMachOObjectFile(Buf.release()).get();
Benjamin Kramer699128e2011-09-21 01:13:19 +0000295 }
296
Eric Christopher7370b552012-11-12 21:40:38 +0000297 // Setup the DIContext
298 diContext.reset(DIContext::getDWARFContext(DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +0000299 }
300
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000301 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000302
303 bool SectIsText = false;
304 Sections[SectIdx].isText(SectIsText);
305 if (SectIsText == false)
306 continue;
307
Owen Andersond9243c42011-10-17 21:37:35 +0000308 StringRef SectName;
309 if (Sections[SectIdx].getName(SectName) ||
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000310 SectName != "__text")
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000311 continue; // Skip non-text sections
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000312
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000313 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000314
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000315 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
316 if (SegmentName != "__TEXT")
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000317 continue;
318
Owen Andersond9243c42011-10-17 21:37:35 +0000319 StringRef Bytes;
320 Sections[SectIdx].getContents(Bytes);
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000321 StringRefMemoryObject memoryObject(Bytes);
322 bool symbolTableWorked = false;
323
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000324 // Parse relocations.
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000325 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
326 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000327 uint64_t RelocOffset, SectionAddress;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000328 Reloc.getOffset(RelocOffset);
Owen Andersond9243c42011-10-17 21:37:35 +0000329 Sections[SectIdx].getAddress(SectionAddress);
330 RelocOffset -= SectionAddress;
331
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000332 symbol_iterator RelocSym = Reloc.getSymbol();
Owen Andersond9243c42011-10-17 21:37:35 +0000333
Rafael Espindola806f0062013-06-05 01:33:53 +0000334 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000335 }
336 array_pod_sort(Relocs.begin(), Relocs.end());
337
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000338 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000339 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +0000340 StringRef SymName;
341 Symbols[SymIdx].getName(SymName);
342
343 SymbolRef::Type ST;
344 Symbols[SymIdx].getType(ST);
345 if (ST != SymbolRef::ST_Function)
346 continue;
347
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000348 // Make sure the symbol is defined in this section.
Owen Andersond9243c42011-10-17 21:37:35 +0000349 bool containsSym = false;
350 Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym);
351 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000352 continue;
353
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000354 // Start at the address of the symbol relative to the section's address.
Cameron Zwarich54478a52012-02-03 05:42:17 +0000355 uint64_t SectionAddress = 0;
Owen Andersond9243c42011-10-17 21:37:35 +0000356 uint64_t Start = 0;
Cameron Zwarich54478a52012-02-03 05:42:17 +0000357 Sections[SectIdx].getAddress(SectionAddress);
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000358 Symbols[SymIdx].getAddress(Start);
Cameron Zwarich54478a52012-02-03 05:42:17 +0000359 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +0000360
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000361 // Stop disassembling either at the beginning of the next symbol or at
362 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +0000363 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +0000364 uint64_t NextSym = 0;
365 uint64_t NextSymIdx = SymIdx+1;
366 while (Symbols.size() > NextSymIdx) {
367 SymbolRef::Type NextSymType;
368 Symbols[NextSymIdx].getType(NextSymType);
369 if (NextSymType == SymbolRef::ST_Function) {
370 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx],
371 containsNextSym);
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000372 Symbols[NextSymIdx].getAddress(NextSym);
Cameron Zwarich54478a52012-02-03 05:42:17 +0000373 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +0000374 break;
375 }
376 ++NextSymIdx;
377 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000378
Owen Andersond9243c42011-10-17 21:37:35 +0000379 uint64_t SectSize;
380 Sections[SectIdx].getSize(SectSize);
381 uint64_t End = containsNextSym ? NextSym : SectSize;
382 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000383
384 symbolTableWorked = true;
385
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000386 outs() << SymName << ":\n";
387 DILineInfo lastLine;
388 for (uint64_t Index = Start; Index < End; Index += Size) {
389 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +0000390
Kevin Enderby273ae012013-06-06 17:20:50 +0000391 uint64_t SectAddress = 0;
392 Sections[SectIdx].getAddress(SectAddress);
393 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
394
395 // Check the data in code table here to see if this is data not an
396 // instruction to be disassembled.
397 DiceTable Dice;
398 Dice.push_back(std::make_pair(SectAddress + Index, DiceRef()));
399 dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(),
400 Dice.begin(), Dice.end(),
401 compareDiceTableEntries);
402 if (DTI != Dices.end()){
403 uint16_t Length;
404 DTI->second.getLength(Length);
405 DumpBytes(StringRef(Bytes.data() + Index, Length));
406 uint16_t Kind;
407 DTI->second.getKind(Kind);
408 DumpDataInCode(Bytes.data() + Index, Length, Kind);
409 continue;
410 }
411
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000412 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
413 DebugOut, nulls())) {
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000414 DumpBytes(StringRef(Bytes.data() + Index, Size));
415 IP->printInst(&Inst, outs(), "");
Owen Andersond9243c42011-10-17 21:37:35 +0000416
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000417 // Print debug info.
418 if (diContext) {
419 DILineInfo dli =
420 diContext->getLineInfoForAddress(SectAddress + Index);
421 // Print valid line info if it changed.
422 if (dli != lastLine && dli.getLine() != 0)
423 outs() << "\t## " << dli.getFileName() << ':'
424 << dli.getLine() << ':' << dli.getColumn();
425 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000426 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000427 outs() << "\n";
428 } else {
429 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
430 if (Size == 0)
431 Size = 1; // skip illegible bytes
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000432 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000433 }
434 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000435 if (!symbolTableWorked) {
Kevin Enderbybadd1002012-05-18 00:13:56 +0000436 // Reading the symbol table didn't work, disassemble the whole section.
437 uint64_t SectAddress;
438 Sections[SectIdx].getAddress(SectAddress);
439 uint64_t SectSize;
440 Sections[SectIdx].getSize(SectSize);
441 uint64_t InstSize;
442 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +0000443 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +0000444
Bill Wendling4e68e062012-07-19 00:17:40 +0000445 if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index,
446 DebugOut, nulls())) {
447 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
448 DumpBytes(StringRef(Bytes.data() + Index, InstSize));
449 IP->printInst(&Inst, outs(), "");
450 outs() << "\n";
451 } else {
452 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
453 if (InstSize == 0)
454 InstSize = 1; // skip illegible bytes
455 }
Kevin Enderbybadd1002012-05-18 00:13:56 +0000456 }
457 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000458 }
459}