blob: b8ce9c884a76c4aa1a32c29f0606a43cff40ee2c [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/OwningPtr.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000016#include "llvm/ADT/STLExtras.h"
Ahmed Bougachaaa790682013-05-24 01:07:04 +000017#include "llvm/ADT/StringExtras.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000018#include "llvm/ADT/Triple.h"
Benjamin Kramer699128e2011-09-21 01:13:19 +000019#include "llvm/DebugInfo/DIContext.h"
Benjamin Kramer43a772e2011-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 Grosbachfd93a592012-03-05 19:33:20 +000027#include "llvm/MC/MCRegisterInfo.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000028#include "llvm/MC/MCSubtargetInfo.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000029#include "llvm/Object/MachO.h"
Rafael Espindola9b709252013-04-13 01:45:40 +000030#include "llvm/Support/Casting.h"
Benjamin Kramer43a772e2011-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 Carruth4d88a1c2012-12-04 10:44:52 +000035#include "llvm/Support/MachO.h"
Benjamin Kramer43a772e2011-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 Kramer699128e2011-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 Espindola56f976f2013-04-18 18:08:55 +000052static const Target *GetTarget(const MachOObjectFile *MachOObj) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +000053 // Figure out the target triple.
Cameron Zwarich88cc16a2012-02-03 06:35:22 +000054 if (TripleName.empty()) {
55 llvm::Triple TT("unknown-unknown-unknown");
Rafael Espindola93f4a622013-04-11 03:34:37 +000056 TT.setArch(Triple::ArchType(MachOObj->getArch()));
Cameron Zwarich88cc16a2012-02-03 06:35:22 +000057 TripleName = TT.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +000058 }
59
Benjamin Kramer43a772e2011-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 Andersond9243c42011-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 Kramer43a772e2011-09-19 17:56:04 +000088};
89
Kevin Enderby273ae012013-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) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000107 case MachO::DICE_KIND_DATA:
Kevin Enderby273ae012013-06-06 17:20:50 +0000108 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;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000128 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby273ae012013-06-06 17:20:50 +0000129 Value = bytes[0];
130 outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8";
131 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000132 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby273ae012013-06-06 17:20:50 +0000133 Value = bytes[1] << 8 |
134 bytes[0];
135 outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16";
136 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000137 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby273ae012013-06-06 17:20:50 +0000138 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 Espindola9b709252013-04-13 01:45:40 +0000150static void
Charles Davis8bdfafd2013-09-01 04:28:48 +0000151getSectionsAndSymbols(const MachO::mach_header Header,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000152 MachOObjectFile *MachOObj,
Rafael Espindola9b709252013-04-13 01:45:40 +0000153 std::vector<SectionRef> &Sections,
154 std::vector<SymbolRef> &Symbols,
Kevin Enderby273ae012013-06-06 17:20:50 +0000155 SmallVectorImpl<uint64_t> &FoundFns,
156 uint64_t &BaseSegmentAddress) {
Owen Andersond9243c42011-10-17 21:37:35 +0000157 for (symbol_iterator SI = MachOObj->begin_symbols(),
Rafael Espindola5e812af2014-01-30 02:49:50 +0000158 SE = MachOObj->end_symbols();
159 SI != SE; ++SI)
Owen Andersond9243c42011-10-17 21:37:35 +0000160 Symbols.push_back(*SI);
161
162 for (section_iterator SI = MachOObj->begin_sections(),
Rafael Espindola5e812af2014-01-30 02:49:50 +0000163 SE = MachOObj->end_sections();
164 SI != SE; ++SI) {
Owen Andersond9243c42011-10-17 21:37:35 +0000165 SectionRef SR = *SI;
166 StringRef SectName;
167 SR.getName(SectName);
168 Sections.push_back(*SI);
169 }
170
Rafael Espindola56f976f2013-04-18 18:08:55 +0000171 MachOObjectFile::LoadCommandInfo Command =
172 MachOObj->getFirstLoadCommandInfo();
Kevin Enderby273ae012013-06-06 17:20:50 +0000173 bool BaseSegmentAddressSet = false;
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000174 for (unsigned i = 0; ; ++i) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000175 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000176 // We found a function starts segment, parse the addresses for later
177 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000178 MachO::linkedit_data_command LLC =
Rafael Espindola56f976f2013-04-18 18:08:55 +0000179 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000180
Charles Davis8bdfafd2013-09-01 04:28:48 +0000181 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000182 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000183 else if (Command.C.cmd == MachO::LC_SEGMENT) {
184 MachO::segment_command SLC =
Kevin Enderby273ae012013-06-06 17:20:50 +0000185 MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000186 StringRef SegName = SLC.segname;
Kevin Enderby273ae012013-06-06 17:20:50 +0000187 if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
188 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000189 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000190 }
191 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000192
Charles Davis8bdfafd2013-09-01 04:28:48 +0000193 if (i == Header.ncmds - 1)
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000194 break;
195 else
196 Command = MachOObj->getNextLoadCommandInfo(Command);
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000197 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000198}
199
Rafael Espindola9b709252013-04-13 01:45:40 +0000200static void DisassembleInputMachO2(StringRef Filename,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000201 MachOObjectFile *MachOOF);
Rafael Espindola9b709252013-04-13 01:45:40 +0000202
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000203void llvm::DisassembleInputMachO(StringRef Filename) {
204 OwningPtr<MemoryBuffer> Buff;
205
206 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
207 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n";
208 return;
209 }
210
Rafael Espindola692410e2014-01-21 23:06:54 +0000211 OwningPtr<MachOObjectFile> MachOOF(static_cast<MachOObjectFile *>(
212 ObjectFile::createMachOObjectFile(Buff.take()).get()));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000213
Rafael Espindola56f976f2013-04-18 18:08:55 +0000214 DisassembleInputMachO2(Filename, MachOOF.get());
Rafael Espindola9b709252013-04-13 01:45:40 +0000215}
216
Rafael Espindola9b709252013-04-13 01:45:40 +0000217static void DisassembleInputMachO2(StringRef Filename,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000218 MachOObjectFile *MachOOF) {
Rafael Espindola9b709252013-04-13 01:45:40 +0000219 const Target *TheTarget = GetTarget(MachOOF);
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000220 if (!TheTarget) {
221 // GetTarget prints out stuff.
222 return;
223 }
Benjamin Kramer357d7dc2011-10-10 13:10:09 +0000224 OwningPtr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000225 OwningPtr<MCInstrAnalysis>
Benjamin Kramer357d7dc2011-10-10 13:10:09 +0000226 InstrAnalysis(TheTarget->createMCInstrAnalysis(InstrInfo.get()));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000227
228 // Set up disassembler.
Rafael Espindola227144c2013-05-13 01:16:13 +0000229 OwningPtr<const MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
230 OwningPtr<const MCAsmInfo> AsmInfo(
231 TheTarget->createMCAsmInfo(*MRI, TripleName));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000232 OwningPtr<const MCSubtargetInfo>
233 STI(TheTarget->createMCSubtargetInfo(TripleName, "", ""));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000234 OwningPtr<const MCDisassembler> DisAsm(TheTarget->createMCDisassembler(*STI));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000235 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Craig Topper54bfde72012-04-02 06:09:36 +0000236 OwningPtr<MCInstPrinter>
237 IP(TheTarget->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *InstrInfo,
238 *MRI, *STI));
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000239
240 if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +0000241 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000242 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000243 return;
244 }
245
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000246 outs() << '\n' << Filename << ":\n\n";
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000247
Charles Davis8bdfafd2013-09-01 04:28:48 +0000248 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000249
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000250 // FIXME: FoundFns isn't used anymore. Using symbols/LC_FUNCTION_STARTS to
251 // determine function locations will eventually go in MCObjectDisassembler.
252 // FIXME: Using the -cfg command line option, this code used to be able to
253 // annotate relocations with the referenced symbol's name, and if this was
254 // inside a __[cf]string section, the data it points to. This is now replaced
255 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +0000256 std::vector<SectionRef> Sections;
257 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000258 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +0000259 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000260
Kevin Enderby273ae012013-06-06 17:20:50 +0000261 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
262 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000263
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000264 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +0000265 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000266
Kevin Enderby273ae012013-06-06 17:20:50 +0000267 // Build a data in code table that is sorted on by the address of each entry.
268 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000269 if (Header.filetype == MachO::MH_OBJECT)
Kevin Enderby273ae012013-06-06 17:20:50 +0000270 Sections[0].getAddress(BaseAddress);
271 else
272 BaseAddress = BaseSegmentAddress;
273 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +0000274 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000275 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000276 uint32_t Offset;
277 DI->getOffset(Offset);
278 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
279 }
280 array_pod_sort(Dices.begin(), Dices.end());
281
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000282#ifndef NDEBUG
283 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
284#else
285 raw_ostream &DebugOut = nulls();
286#endif
287
Benjamin Kramer699128e2011-09-21 01:13:19 +0000288 OwningPtr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +0000289 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +0000290 // Try to find debug info and set up the DIContext for it.
291 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000292 // A separate DSym file path was specified, parse it as a macho file,
293 // get the sections and supply it to the section name parsing machinery.
294 if (!DSYMFile.empty()) {
295 OwningPtr<MemoryBuffer> Buf;
Rafael Espindola8c811722013-06-25 05:28:34 +0000296 if (error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile, Buf)) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000297 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n';
298 return;
299 }
Rafael Espindola692410e2014-01-21 23:06:54 +0000300 DbgObj = ObjectFile::createMachOObjectFile(Buf.take()).get();
Benjamin Kramer699128e2011-09-21 01:13:19 +0000301 }
302
Eric Christopher7370b552012-11-12 21:40:38 +0000303 // Setup the DIContext
304 diContext.reset(DIContext::getDWARFContext(DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +0000305 }
306
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000307 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000308
309 bool SectIsText = false;
310 Sections[SectIdx].isText(SectIsText);
311 if (SectIsText == false)
312 continue;
313
Owen Andersond9243c42011-10-17 21:37:35 +0000314 StringRef SectName;
315 if (Sections[SectIdx].getName(SectName) ||
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000316 SectName != "__text")
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000317 continue; // Skip non-text sections
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000318
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000319 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000320
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000321 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
322 if (SegmentName != "__TEXT")
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000323 continue;
324
Owen Andersond9243c42011-10-17 21:37:35 +0000325 StringRef Bytes;
326 Sections[SectIdx].getContents(Bytes);
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000327 StringRefMemoryObject memoryObject(Bytes);
328 bool symbolTableWorked = false;
329
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000330 // Parse relocations.
Owen Anderson7635bb72011-11-07 17:21:36 +0000331 std::vector<std::pair<uint64_t, SymbolRef> > Relocs;
Owen Andersond9243c42011-10-17 21:37:35 +0000332 for (relocation_iterator RI = Sections[SectIdx].begin_relocations(),
Rafael Espindola5e812af2014-01-30 02:49:50 +0000333 RE = Sections[SectIdx].end_relocations();
334 RI != RE; ++RI) {
Owen Andersond9243c42011-10-17 21:37:35 +0000335 uint64_t RelocOffset, SectionAddress;
Rafael Espindola1e483872013-04-25 12:28:45 +0000336 RI->getOffset(RelocOffset);
Owen Andersond9243c42011-10-17 21:37:35 +0000337 Sections[SectIdx].getAddress(SectionAddress);
338 RelocOffset -= SectionAddress;
339
Rafael Espindola806f0062013-06-05 01:33:53 +0000340 symbol_iterator RelocSym = RI->getSymbol();
Owen Andersond9243c42011-10-17 21:37:35 +0000341
Rafael Espindola806f0062013-06-05 01:33:53 +0000342 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000343 }
344 array_pod_sort(Relocs.begin(), Relocs.end());
345
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000346 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000347 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +0000348 StringRef SymName;
349 Symbols[SymIdx].getName(SymName);
350
351 SymbolRef::Type ST;
352 Symbols[SymIdx].getType(ST);
353 if (ST != SymbolRef::ST_Function)
354 continue;
355
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000356 // Make sure the symbol is defined in this section.
Owen Andersond9243c42011-10-17 21:37:35 +0000357 bool containsSym = false;
358 Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym);
359 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000360 continue;
361
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000362 // Start at the address of the symbol relative to the section's address.
Cameron Zwarich54478a52012-02-03 05:42:17 +0000363 uint64_t SectionAddress = 0;
Owen Andersond9243c42011-10-17 21:37:35 +0000364 uint64_t Start = 0;
Cameron Zwarich54478a52012-02-03 05:42:17 +0000365 Sections[SectIdx].getAddress(SectionAddress);
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000366 Symbols[SymIdx].getAddress(Start);
Cameron Zwarich54478a52012-02-03 05:42:17 +0000367 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +0000368
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000369 // Stop disassembling either at the beginning of the next symbol or at
370 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +0000371 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +0000372 uint64_t NextSym = 0;
373 uint64_t NextSymIdx = SymIdx+1;
374 while (Symbols.size() > NextSymIdx) {
375 SymbolRef::Type NextSymType;
376 Symbols[NextSymIdx].getType(NextSymType);
377 if (NextSymType == SymbolRef::ST_Function) {
378 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx],
379 containsNextSym);
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000380 Symbols[NextSymIdx].getAddress(NextSym);
Cameron Zwarich54478a52012-02-03 05:42:17 +0000381 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +0000382 break;
383 }
384 ++NextSymIdx;
385 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000386
Owen Andersond9243c42011-10-17 21:37:35 +0000387 uint64_t SectSize;
388 Sections[SectIdx].getSize(SectSize);
389 uint64_t End = containsNextSym ? NextSym : SectSize;
390 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000391
392 symbolTableWorked = true;
393
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000394 outs() << SymName << ":\n";
395 DILineInfo lastLine;
396 for (uint64_t Index = Start; Index < End; Index += Size) {
397 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +0000398
Kevin Enderby273ae012013-06-06 17:20:50 +0000399 uint64_t SectAddress = 0;
400 Sections[SectIdx].getAddress(SectAddress);
401 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
402
403 // Check the data in code table here to see if this is data not an
404 // instruction to be disassembled.
405 DiceTable Dice;
406 Dice.push_back(std::make_pair(SectAddress + Index, DiceRef()));
407 dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(),
408 Dice.begin(), Dice.end(),
409 compareDiceTableEntries);
410 if (DTI != Dices.end()){
411 uint16_t Length;
412 DTI->second.getLength(Length);
413 DumpBytes(StringRef(Bytes.data() + Index, Length));
414 uint16_t Kind;
415 DTI->second.getKind(Kind);
416 DumpDataInCode(Bytes.data() + Index, Length, Kind);
417 continue;
418 }
419
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000420 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
421 DebugOut, nulls())) {
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000422 DumpBytes(StringRef(Bytes.data() + Index, Size));
423 IP->printInst(&Inst, outs(), "");
Owen Andersond9243c42011-10-17 21:37:35 +0000424
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000425 // Print debug info.
426 if (diContext) {
427 DILineInfo dli =
428 diContext->getLineInfoForAddress(SectAddress + Index);
429 // Print valid line info if it changed.
430 if (dli != lastLine && dli.getLine() != 0)
431 outs() << "\t## " << dli.getFileName() << ':'
432 << dli.getLine() << ':' << dli.getColumn();
433 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000434 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000435 outs() << "\n";
436 } else {
437 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
438 if (Size == 0)
439 Size = 1; // skip illegible bytes
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000440 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000441 }
442 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000443 if (!symbolTableWorked) {
Kevin Enderbybadd1002012-05-18 00:13:56 +0000444 // Reading the symbol table didn't work, disassemble the whole section.
445 uint64_t SectAddress;
446 Sections[SectIdx].getAddress(SectAddress);
447 uint64_t SectSize;
448 Sections[SectIdx].getSize(SectSize);
449 uint64_t InstSize;
450 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +0000451 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +0000452
Bill Wendling4e68e062012-07-19 00:17:40 +0000453 if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index,
454 DebugOut, nulls())) {
455 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
456 DumpBytes(StringRef(Bytes.data() + Index, InstSize));
457 IP->printInst(&Inst, outs(), "");
458 outs() << "\n";
459 } else {
460 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
461 if (InstSize == 0)
462 InstSize = 1; // skip illegible bytes
463 }
Kevin Enderbybadd1002012-05-18 00:13:56 +0000464 }
465 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000466 }
467}