blob: cba731835465ae307f499bada87a4b15342bb0f0 [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"
Lang Hamesa1bc0f52014-04-15 04:40:56 +000020#include "llvm/MC/MCContext.h"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000021#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"
Benjamin Kramer43a772e2011-09-19 17:56:04 +000040#include <algorithm>
41#include <cstring>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000042#include <system_error>
Benjamin Kramer43a772e2011-09-19 17:56:04 +000043using namespace llvm;
44using namespace object;
Rafael Espindolabff5d0d2014-06-13 01:25:41 +000045using std::error_code;
Benjamin Kramer43a772e2011-09-19 17:56:04 +000046
47static cl::opt<bool>
Benjamin Kramer699128e2011-09-21 01:13:19 +000048 UseDbg("g", cl::desc("Print line information from debug info if available"));
49
50static cl::opt<std::string>
51 DSYMFile("dsym", cl::desc("Use .dSYM file for debug info"));
52
Rafael Espindola56f976f2013-04-18 18:08:55 +000053static const Target *GetTarget(const MachOObjectFile *MachOObj) {
Benjamin Kramer43a772e2011-09-19 17:56:04 +000054 // Figure out the target triple.
Cameron Zwarich88cc16a2012-02-03 06:35:22 +000055 if (TripleName.empty()) {
56 llvm::Triple TT("unknown-unknown-unknown");
Rafael Espindola93f4a622013-04-11 03:34:37 +000057 TT.setArch(Triple::ArchType(MachOObj->getArch()));
Cameron Zwarich88cc16a2012-02-03 06:35:22 +000058 TripleName = TT.str();
Benjamin Kramer43a772e2011-09-19 17:56:04 +000059 }
60
Benjamin Kramer43a772e2011-09-19 17:56:04 +000061 // Get the target specific parser.
62 std::string Error;
63 const Target *TheTarget = TargetRegistry::lookupTarget(TripleName, Error);
64 if (TheTarget)
65 return TheTarget;
66
67 errs() << "llvm-objdump: error: unable to get target for '" << TripleName
68 << "', see --version and --triple.\n";
Craig Toppere6cb63e2014-04-25 04:24:47 +000069 return nullptr;
Benjamin Kramer43a772e2011-09-19 17:56:04 +000070}
71
Owen Andersond9243c42011-10-17 21:37:35 +000072struct SymbolSorter {
73 bool operator()(const SymbolRef &A, const SymbolRef &B) {
74 SymbolRef::Type AType, BType;
75 A.getType(AType);
76 B.getType(BType);
77
78 uint64_t AAddr, BAddr;
79 if (AType != SymbolRef::ST_Function)
80 AAddr = 0;
81 else
82 A.getAddress(AAddr);
83 if (BType != SymbolRef::ST_Function)
84 BAddr = 0;
85 else
86 B.getAddress(BAddr);
87 return AAddr < BAddr;
88 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +000089};
90
Kevin Enderby273ae012013-06-06 17:20:50 +000091// Types for the storted data in code table that is built before disassembly
92// and the predicate function to sort them.
93typedef std::pair<uint64_t, DiceRef> DiceTableEntry;
94typedef std::vector<DiceTableEntry> DiceTable;
95typedef DiceTable::iterator dice_table_iterator;
96
97static bool
98compareDiceTableEntries(const DiceTableEntry i,
99 const DiceTableEntry j) {
100 return i.first == j.first;
101}
102
103static void DumpDataInCode(const char *bytes, uint64_t Size,
104 unsigned short Kind) {
105 uint64_t Value;
106
107 switch (Kind) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000108 case MachO::DICE_KIND_DATA:
Kevin Enderby273ae012013-06-06 17:20:50 +0000109 switch (Size) {
110 case 4:
111 Value = bytes[3] << 24 |
112 bytes[2] << 16 |
113 bytes[1] << 8 |
114 bytes[0];
115 outs() << "\t.long " << Value;
116 break;
117 case 2:
118 Value = bytes[1] << 8 |
119 bytes[0];
120 outs() << "\t.short " << Value;
121 break;
122 case 1:
123 Value = bytes[0];
124 outs() << "\t.byte " << Value;
125 break;
126 }
127 outs() << "\t@ KIND_DATA\n";
128 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000129 case MachO::DICE_KIND_JUMP_TABLE8:
Kevin Enderby273ae012013-06-06 17:20:50 +0000130 Value = bytes[0];
131 outs() << "\t.byte " << Value << "\t@ KIND_JUMP_TABLE8";
132 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000133 case MachO::DICE_KIND_JUMP_TABLE16:
Kevin Enderby273ae012013-06-06 17:20:50 +0000134 Value = bytes[1] << 8 |
135 bytes[0];
136 outs() << "\t.short " << Value << "\t@ KIND_JUMP_TABLE16";
137 break;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000138 case MachO::DICE_KIND_JUMP_TABLE32:
Kevin Enderby273ae012013-06-06 17:20:50 +0000139 Value = bytes[3] << 24 |
140 bytes[2] << 16 |
141 bytes[1] << 8 |
142 bytes[0];
143 outs() << "\t.long " << Value << "\t@ KIND_JUMP_TABLE32";
144 break;
145 default:
146 outs() << "\t@ data in code kind = " << Kind << "\n";
147 break;
148 }
149}
150
Alexey Samsonov464d2e42014-03-17 07:28:19 +0000151static void getSectionsAndSymbols(const MachO::mach_header Header,
152 MachOObjectFile *MachOObj,
153 std::vector<SectionRef> &Sections,
154 std::vector<SymbolRef> &Symbols,
155 SmallVectorImpl<uint64_t> &FoundFns,
156 uint64_t &BaseSegmentAddress) {
157 for (const SymbolRef &Symbol : MachOObj->symbols())
158 Symbols.push_back(Symbol);
Owen Andersond9243c42011-10-17 21:37:35 +0000159
Alexey Samsonov48803e52014-03-13 14:37:36 +0000160 for (const SectionRef &Section : MachOObj->sections()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000161 StringRef SectName;
Alexey Samsonov48803e52014-03-13 14:37:36 +0000162 Section.getName(SectName);
163 Sections.push_back(Section);
Owen Andersond9243c42011-10-17 21:37:35 +0000164 }
165
Rafael Espindola56f976f2013-04-18 18:08:55 +0000166 MachOObjectFile::LoadCommandInfo Command =
Alexey Samsonov48803e52014-03-13 14:37:36 +0000167 MachOObj->getFirstLoadCommandInfo();
Kevin Enderby273ae012013-06-06 17:20:50 +0000168 bool BaseSegmentAddressSet = false;
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000169 for (unsigned i = 0; ; ++i) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000170 if (Command.C.cmd == MachO::LC_FUNCTION_STARTS) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000171 // We found a function starts segment, parse the addresses for later
172 // consumption.
Charles Davis8bdfafd2013-09-01 04:28:48 +0000173 MachO::linkedit_data_command LLC =
Rafael Espindola56f976f2013-04-18 18:08:55 +0000174 MachOObj->getLinkeditDataLoadCommand(Command);
Benjamin Kramer699128e2011-09-21 01:13:19 +0000175
Charles Davis8bdfafd2013-09-01 04:28:48 +0000176 MachOObj->ReadULEB128s(LLC.dataoff, FoundFns);
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000177 }
Charles Davis8bdfafd2013-09-01 04:28:48 +0000178 else if (Command.C.cmd == MachO::LC_SEGMENT) {
179 MachO::segment_command SLC =
Kevin Enderby273ae012013-06-06 17:20:50 +0000180 MachOObj->getSegmentLoadCommand(Command);
Charles Davis8bdfafd2013-09-01 04:28:48 +0000181 StringRef SegName = SLC.segname;
Kevin Enderby273ae012013-06-06 17:20:50 +0000182 if(!BaseSegmentAddressSet && SegName != "__PAGEZERO") {
183 BaseSegmentAddressSet = true;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000184 BaseSegmentAddress = SLC.vmaddr;
Kevin Enderby273ae012013-06-06 17:20:50 +0000185 }
186 }
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000187
Charles Davis8bdfafd2013-09-01 04:28:48 +0000188 if (i == Header.ncmds - 1)
Rafael Espindolafeef8c22013-04-19 11:36:47 +0000189 break;
190 else
191 Command = MachOObj->getNextLoadCommandInfo(Command);
Benjamin Kramer8a529dc2011-09-21 22:16:43 +0000192 }
Benjamin Kramer699128e2011-09-21 01:13:19 +0000193}
194
Rafael Espindola9b709252013-04-13 01:45:40 +0000195static void DisassembleInputMachO2(StringRef Filename,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000196 MachOObjectFile *MachOOF);
Rafael Espindola9b709252013-04-13 01:45:40 +0000197
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000198void llvm::DisassembleInputMachO(StringRef Filename) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000199 std::unique_ptr<MemoryBuffer> Buff;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000200
201 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {
202 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << "\n";
203 return;
204 }
205
Ahmed Charles56440fd2014-03-06 05:51:42 +0000206 std::unique_ptr<MachOObjectFile> MachOOF(static_cast<MachOObjectFile *>(
Ahmed Charles96c9d952014-03-05 10:19:29 +0000207 ObjectFile::createMachOObjectFile(Buff.release()).get()));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000208
Rafael Espindola56f976f2013-04-18 18:08:55 +0000209 DisassembleInputMachO2(Filename, MachOOF.get());
Rafael Espindola9b709252013-04-13 01:45:40 +0000210}
211
Rafael Espindola9b709252013-04-13 01:45:40 +0000212static void DisassembleInputMachO2(StringRef Filename,
Rafael Espindola56f976f2013-04-18 18:08:55 +0000213 MachOObjectFile *MachOOF) {
Rafael Espindola9b709252013-04-13 01:45:40 +0000214 const Target *TheTarget = GetTarget(MachOOF);
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000215 if (!TheTarget) {
216 // GetTarget prints out stuff.
217 return;
218 }
Ahmed Charles56440fd2014-03-06 05:51:42 +0000219 std::unique_ptr<const MCInstrInfo> InstrInfo(TheTarget->createMCInstrInfo());
220 std::unique_ptr<MCInstrAnalysis> InstrAnalysis(
221 TheTarget->createMCInstrAnalysis(InstrInfo.get()));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000222
223 // Set up disassembler.
Ahmed Charles56440fd2014-03-06 05:51:42 +0000224 std::unique_ptr<const MCRegisterInfo> MRI(
225 TheTarget->createMCRegInfo(TripleName));
226 std::unique_ptr<const MCAsmInfo> AsmInfo(
Rafael Espindola227144c2013-05-13 01:16:13 +0000227 TheTarget->createMCAsmInfo(*MRI, TripleName));
Ahmed Charles56440fd2014-03-06 05:51:42 +0000228 std::unique_ptr<const MCSubtargetInfo> STI(
229 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
Craig Toppere6cb63e2014-04-25 04:24:47 +0000230 MCContext Ctx(AsmInfo.get(), MRI.get(), nullptr);
Ahmed Charles56440fd2014-03-06 05:51:42 +0000231 std::unique_ptr<const MCDisassembler> DisAsm(
Lang Hamesa1bc0f52014-04-15 04:40:56 +0000232 TheTarget->createMCDisassembler(*STI, Ctx));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000233 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000234 std::unique_ptr<MCInstPrinter> IP(TheTarget->createMCInstPrinter(
235 AsmPrinterVariant, *AsmInfo, *InstrInfo, *MRI, *STI));
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000236
237 if (!InstrAnalysis || !AsmInfo || !STI || !DisAsm || !IP) {
Michael J. Spencerc1363cf2011-10-07 19:25:47 +0000238 errs() << "error: couldn't initialize disassembler for target "
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000239 << TripleName << '\n';
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000240 return;
241 }
242
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000243 outs() << '\n' << Filename << ":\n\n";
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000244
Charles Davis8bdfafd2013-09-01 04:28:48 +0000245 MachO::mach_header Header = MachOOF->getHeader();
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000246
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000247 // FIXME: FoundFns isn't used anymore. Using symbols/LC_FUNCTION_STARTS to
248 // determine function locations will eventually go in MCObjectDisassembler.
249 // FIXME: Using the -cfg command line option, this code used to be able to
250 // annotate relocations with the referenced symbol's name, and if this was
251 // inside a __[cf]string section, the data it points to. This is now replaced
252 // by the upcoming MCSymbolizer, which needs the appropriate setup done above.
Owen Andersond9243c42011-10-17 21:37:35 +0000253 std::vector<SectionRef> Sections;
254 std::vector<SymbolRef> Symbols;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000255 SmallVector<uint64_t, 8> FoundFns;
Kevin Enderby273ae012013-06-06 17:20:50 +0000256 uint64_t BaseSegmentAddress;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000257
Kevin Enderby273ae012013-06-06 17:20:50 +0000258 getSectionsAndSymbols(Header, MachOOF, Sections, Symbols, FoundFns,
259 BaseSegmentAddress);
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000260
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000261 // Sort the symbols by address, just in case they didn't come in that way.
Owen Andersond9243c42011-10-17 21:37:35 +0000262 std::sort(Symbols.begin(), Symbols.end(), SymbolSorter());
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000263
Kevin Enderby273ae012013-06-06 17:20:50 +0000264 // Build a data in code table that is sorted on by the address of each entry.
265 uint64_t BaseAddress = 0;
Charles Davis8bdfafd2013-09-01 04:28:48 +0000266 if (Header.filetype == MachO::MH_OBJECT)
Kevin Enderby273ae012013-06-06 17:20:50 +0000267 Sections[0].getAddress(BaseAddress);
268 else
269 BaseAddress = BaseSegmentAddress;
270 DiceTable Dices;
Kevin Enderby273ae012013-06-06 17:20:50 +0000271 for (dice_iterator DI = MachOOF->begin_dices(), DE = MachOOF->end_dices();
Rafael Espindola5e812af2014-01-30 02:49:50 +0000272 DI != DE; ++DI) {
Kevin Enderby273ae012013-06-06 17:20:50 +0000273 uint32_t Offset;
274 DI->getOffset(Offset);
275 Dices.push_back(std::make_pair(BaseAddress + Offset, *DI));
276 }
277 array_pod_sort(Dices.begin(), Dices.end());
278
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000279#ifndef NDEBUG
280 raw_ostream &DebugOut = DebugFlag ? dbgs() : nulls();
281#else
282 raw_ostream &DebugOut = nulls();
283#endif
284
Ahmed Charles56440fd2014-03-06 05:51:42 +0000285 std::unique_ptr<DIContext> diContext;
Rafael Espindola9b709252013-04-13 01:45:40 +0000286 ObjectFile *DbgObj = MachOOF;
Benjamin Kramer699128e2011-09-21 01:13:19 +0000287 // Try to find debug info and set up the DIContext for it.
288 if (UseDbg) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000289 // A separate DSym file path was specified, parse it as a macho file,
290 // get the sections and supply it to the section name parsing machinery.
291 if (!DSYMFile.empty()) {
Ahmed Charles56440fd2014-03-06 05:51:42 +0000292 std::unique_ptr<MemoryBuffer> Buf;
Rafael Espindola8c811722013-06-25 05:28:34 +0000293 if (error_code ec = MemoryBuffer::getFileOrSTDIN(DSYMFile, Buf)) {
Benjamin Kramer699128e2011-09-21 01:13:19 +0000294 errs() << "llvm-objdump: " << Filename << ": " << ec.message() << '\n';
295 return;
296 }
Ahmed Charles96c9d952014-03-05 10:19:29 +0000297 DbgObj = ObjectFile::createMachOObjectFile(Buf.release()).get();
Benjamin Kramer699128e2011-09-21 01:13:19 +0000298 }
299
Eric Christopher7370b552012-11-12 21:40:38 +0000300 // Setup the DIContext
301 diContext.reset(DIContext::getDWARFContext(DbgObj));
Benjamin Kramer699128e2011-09-21 01:13:19 +0000302 }
303
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000304 for (unsigned SectIdx = 0; SectIdx != Sections.size(); SectIdx++) {
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000305
306 bool SectIsText = false;
307 Sections[SectIdx].isText(SectIsText);
308 if (SectIsText == false)
309 continue;
310
Owen Andersond9243c42011-10-17 21:37:35 +0000311 StringRef SectName;
312 if (Sections[SectIdx].getName(SectName) ||
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000313 SectName != "__text")
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000314 continue; // Skip non-text sections
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000315
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000316 DataRefImpl DR = Sections[SectIdx].getRawDataRefImpl();
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000317
Rafael Espindolab0f76a42013-04-05 15:15:22 +0000318 StringRef SegmentName = MachOOF->getSectionFinalSegmentName(DR);
319 if (SegmentName != "__TEXT")
Rafael Espindolaa9f810b2012-12-21 03:47:03 +0000320 continue;
321
Owen Andersond9243c42011-10-17 21:37:35 +0000322 StringRef Bytes;
323 Sections[SectIdx].getContents(Bytes);
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000324 StringRefMemoryObject memoryObject(Bytes);
325 bool symbolTableWorked = false;
326
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000327 // Parse relocations.
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000328 std::vector<std::pair<uint64_t, SymbolRef>> Relocs;
329 for (const RelocationRef &Reloc : Sections[SectIdx].relocations()) {
Owen Andersond9243c42011-10-17 21:37:35 +0000330 uint64_t RelocOffset, SectionAddress;
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000331 Reloc.getOffset(RelocOffset);
Owen Andersond9243c42011-10-17 21:37:35 +0000332 Sections[SectIdx].getAddress(SectionAddress);
333 RelocOffset -= SectionAddress;
334
Alexey Samsonovaa4d2952014-03-14 14:22:49 +0000335 symbol_iterator RelocSym = Reloc.getSymbol();
Owen Andersond9243c42011-10-17 21:37:35 +0000336
Rafael Espindola806f0062013-06-05 01:33:53 +0000337 Relocs.push_back(std::make_pair(RelocOffset, *RelocSym));
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000338 }
339 array_pod_sort(Relocs.begin(), Relocs.end());
340
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000341 // Disassemble symbol by symbol.
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000342 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
Owen Andersond9243c42011-10-17 21:37:35 +0000343 StringRef SymName;
344 Symbols[SymIdx].getName(SymName);
345
346 SymbolRef::Type ST;
347 Symbols[SymIdx].getType(ST);
348 if (ST != SymbolRef::ST_Function)
349 continue;
350
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000351 // Make sure the symbol is defined in this section.
Owen Andersond9243c42011-10-17 21:37:35 +0000352 bool containsSym = false;
353 Sections[SectIdx].containsSymbol(Symbols[SymIdx], containsSym);
354 if (!containsSym)
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000355 continue;
356
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000357 // Start at the address of the symbol relative to the section's address.
Cameron Zwarich54478a52012-02-03 05:42:17 +0000358 uint64_t SectionAddress = 0;
Owen Andersond9243c42011-10-17 21:37:35 +0000359 uint64_t Start = 0;
Cameron Zwarich54478a52012-02-03 05:42:17 +0000360 Sections[SectIdx].getAddress(SectionAddress);
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000361 Symbols[SymIdx].getAddress(Start);
Cameron Zwarich54478a52012-02-03 05:42:17 +0000362 Start -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +0000363
Benjamin Kramer2ad2eb52011-09-20 17:53:01 +0000364 // Stop disassembling either at the beginning of the next symbol or at
365 // the end of the section.
Kevin Enderbyedd58722012-05-15 18:57:14 +0000366 bool containsNextSym = false;
Owen Andersond9243c42011-10-17 21:37:35 +0000367 uint64_t NextSym = 0;
368 uint64_t NextSymIdx = SymIdx+1;
369 while (Symbols.size() > NextSymIdx) {
370 SymbolRef::Type NextSymType;
371 Symbols[NextSymIdx].getType(NextSymType);
372 if (NextSymType == SymbolRef::ST_Function) {
373 Sections[SectIdx].containsSymbol(Symbols[NextSymIdx],
374 containsNextSym);
Danil Malyshevcbe72fc2011-11-29 17:40:10 +0000375 Symbols[NextSymIdx].getAddress(NextSym);
Cameron Zwarich54478a52012-02-03 05:42:17 +0000376 NextSym -= SectionAddress;
Owen Andersond9243c42011-10-17 21:37:35 +0000377 break;
378 }
379 ++NextSymIdx;
380 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000381
Owen Andersond9243c42011-10-17 21:37:35 +0000382 uint64_t SectSize;
383 Sections[SectIdx].getSize(SectSize);
384 uint64_t End = containsNextSym ? NextSym : SectSize;
385 uint64_t Size;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000386
387 symbolTableWorked = true;
388
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000389 outs() << SymName << ":\n";
390 DILineInfo lastLine;
391 for (uint64_t Index = Start; Index < End; Index += Size) {
392 MCInst Inst;
Owen Andersond9243c42011-10-17 21:37:35 +0000393
Kevin Enderby273ae012013-06-06 17:20:50 +0000394 uint64_t SectAddress = 0;
395 Sections[SectIdx].getAddress(SectAddress);
396 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
397
398 // Check the data in code table here to see if this is data not an
399 // instruction to be disassembled.
400 DiceTable Dice;
401 Dice.push_back(std::make_pair(SectAddress + Index, DiceRef()));
402 dice_table_iterator DTI = std::search(Dices.begin(), Dices.end(),
403 Dice.begin(), Dice.end(),
404 compareDiceTableEntries);
405 if (DTI != Dices.end()){
406 uint16_t Length;
407 DTI->second.getLength(Length);
408 DumpBytes(StringRef(Bytes.data() + Index, Length));
409 uint16_t Kind;
410 DTI->second.getKind(Kind);
411 DumpDataInCode(Bytes.data() + Index, Length, Kind);
412 continue;
413 }
414
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000415 if (DisAsm->getInstruction(Inst, Size, memoryObject, Index,
416 DebugOut, nulls())) {
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000417 DumpBytes(StringRef(Bytes.data() + Index, Size));
418 IP->printInst(&Inst, outs(), "");
Owen Andersond9243c42011-10-17 21:37:35 +0000419
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000420 // Print debug info.
421 if (diContext) {
422 DILineInfo dli =
423 diContext->getLineInfoForAddress(SectAddress + Index);
424 // Print valid line info if it changed.
Alexey Samsonovd0109992014-04-18 21:36:39 +0000425 if (dli != lastLine && dli.Line != 0)
426 outs() << "\t## " << dli.FileName << ':' << dli.Line << ':'
427 << dli.Column;
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000428 lastLine = dli;
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000429 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000430 outs() << "\n";
431 } else {
432 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
433 if (Size == 0)
434 Size = 1; // skip illegible bytes
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000435 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000436 }
437 }
Ahmed Bougachaaa790682013-05-24 01:07:04 +0000438 if (!symbolTableWorked) {
Kevin Enderbybadd1002012-05-18 00:13:56 +0000439 // Reading the symbol table didn't work, disassemble the whole section.
440 uint64_t SectAddress;
441 Sections[SectIdx].getAddress(SectAddress);
442 uint64_t SectSize;
443 Sections[SectIdx].getSize(SectSize);
444 uint64_t InstSize;
445 for (uint64_t Index = 0; Index < SectSize; Index += InstSize) {
Bill Wendling4e68e062012-07-19 00:17:40 +0000446 MCInst Inst;
Kevin Enderbybadd1002012-05-18 00:13:56 +0000447
Bill Wendling4e68e062012-07-19 00:17:40 +0000448 if (DisAsm->getInstruction(Inst, InstSize, memoryObject, Index,
449 DebugOut, nulls())) {
450 outs() << format("%8" PRIx64 ":\t", SectAddress + Index);
451 DumpBytes(StringRef(Bytes.data() + Index, InstSize));
452 IP->printInst(&Inst, outs(), "");
453 outs() << "\n";
454 } else {
455 errs() << "llvm-objdump: warning: invalid instruction encoding\n";
456 if (InstSize == 0)
457 InstSize = 1; // skip illegible bytes
458 }
Kevin Enderbybadd1002012-05-18 00:13:56 +0000459 }
460 }
Benjamin Kramer43a772e2011-09-19 17:56:04 +0000461 }
462}