Bug Summary

File:tools/llvm-readobj/MachODumper.cpp
Warning:line 415, column 10
Called C++ object pointer is null

Annotated Source Code

/build/llvm-toolchain-snapshot-6.0~svn318211/tools/llvm-readobj/MachODumper.cpp

1//===-- 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-readobj.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Error.h"
15#include "ObjDumper.h"
16#include "StackMapPrinter.h"
17#include "llvm-readobj.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringExtras.h"
20#include "llvm/Object/MachO.h"
21#include "llvm/Support/Casting.h"
22#include "llvm/Support/ScopedPrinter.h"
23
24using namespace llvm;
25using namespace object;
26
27namespace {
28
29class MachODumper : public ObjDumper {
30public:
31 MachODumper(const MachOObjectFile *Obj, ScopedPrinter &Writer)
32 : ObjDumper(Writer), Obj(Obj) {}
33
34 void printFileHeaders() override;
35 void printSections() override;
36 void printRelocations() override;
37 void printSymbols() override;
38 void printDynamicSymbols() override;
39 void printUnwindInfo() override;
40 void printStackMap() const override;
41
42 // MachO-specific.
43 void printMachODataInCode() override;
44 void printMachOVersionMin() override;
45 void printMachODysymtab() override;
46 void printMachOSegment() override;
47 void printMachOIndirectSymbols() override;
48 void printMachOLinkerOptions () override;
49
50private:
51 template<class MachHeader>
52 void printFileHeaders(const MachHeader &Header);
53
54 void printSymbol(const SymbolRef &Symbol);
55
56 void printRelocation(const RelocationRef &Reloc);
57
58 void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc);
59
60 void printSections(const MachOObjectFile *Obj);
61
62 const MachOObjectFile *Obj;
63};
64
65} // namespace
66
67
68namespace llvm {
69
70std::error_code createMachODumper(const object::ObjectFile *Obj,
71 ScopedPrinter &Writer,
72 std::unique_ptr<ObjDumper> &Result) {
73 const MachOObjectFile *MachOObj = dyn_cast<MachOObjectFile>(Obj);
74 if (!MachOObj)
75 return readobj_error::unsupported_obj_file_format;
76
77 Result.reset(new MachODumper(MachOObj, Writer));
78 return readobj_error::success;
79}
80
81} // namespace llvm
82
83static const EnumEntry<uint32_t> MachOMagics[] = {
84 { "Magic", MachO::MH_MAGIC },
85 { "Cigam", MachO::MH_CIGAM },
86 { "Magic64", MachO::MH_MAGIC_64 },
87 { "Cigam64", MachO::MH_CIGAM_64 },
88 { "FatMagic", MachO::FAT_MAGIC },
89 { "FatCigam", MachO::FAT_CIGAM },
90};
91
92static const EnumEntry<uint32_t> MachOHeaderFileTypes[] = {
93 { "Relocatable", MachO::MH_OBJECT },
94 { "Executable", MachO::MH_EXECUTE },
95 { "FixedVMLibrary", MachO::MH_FVMLIB },
96 { "Core", MachO::MH_CORE },
97 { "PreloadedExecutable", MachO::MH_PRELOAD },
98 { "DynamicLibrary", MachO::MH_DYLIB },
99 { "DynamicLinker", MachO::MH_DYLINKER },
100 { "Bundle", MachO::MH_BUNDLE },
101 { "DynamicLibraryStub", MachO::MH_DYLIB_STUB },
102 { "DWARFSymbol", MachO::MH_DSYM },
103 { "KextBundle", MachO::MH_KEXT_BUNDLE },
104};
105
106static const EnumEntry<uint32_t> MachOHeaderCpuTypes[] = {
107 { "Any" , static_cast<uint32_t>(MachO::CPU_TYPE_ANY) },
108 { "X86" , MachO::CPU_TYPE_X86 },
109 { "X86-64" , MachO::CPU_TYPE_X86_64 },
110 { "Mc98000" , MachO::CPU_TYPE_MC98000 },
111 { "Arm" , MachO::CPU_TYPE_ARM },
112 { "Arm64" , MachO::CPU_TYPE_ARM64 },
113 { "Sparc" , MachO::CPU_TYPE_SPARC },
114 { "PowerPC" , MachO::CPU_TYPE_POWERPC },
115 { "PowerPC64" , MachO::CPU_TYPE_POWERPC64 },
116};
117
118static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX86[] = {
119 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_I386_ALL){ "CPU_SUBTYPE_I386_ALL", MachO::CPU_SUBTYPE_I386_ALL },
120 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_386){ "CPU_SUBTYPE_386", MachO::CPU_SUBTYPE_386 },
121 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486){ "CPU_SUBTYPE_486", MachO::CPU_SUBTYPE_486 },
122 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486SX){ "CPU_SUBTYPE_486SX", MachO::CPU_SUBTYPE_486SX },
123 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_586){ "CPU_SUBTYPE_586", MachO::CPU_SUBTYPE_586 },
124 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTPRO){ "CPU_SUBTYPE_PENTPRO", MachO::CPU_SUBTYPE_PENTPRO },
125 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M3){ "CPU_SUBTYPE_PENTII_M3", MachO::CPU_SUBTYPE_PENTII_M3 },
126 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M5){ "CPU_SUBTYPE_PENTII_M5", MachO::CPU_SUBTYPE_PENTII_M5 },
127 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON){ "CPU_SUBTYPE_CELERON", MachO::CPU_SUBTYPE_CELERON },
128 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON_MOBILE){ "CPU_SUBTYPE_CELERON_MOBILE", MachO::CPU_SUBTYPE_CELERON_MOBILE
}
,
129 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3){ "CPU_SUBTYPE_PENTIUM_3", MachO::CPU_SUBTYPE_PENTIUM_3 },
130 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_M){ "CPU_SUBTYPE_PENTIUM_3_M", MachO::CPU_SUBTYPE_PENTIUM_3_M },
131 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_XEON){ "CPU_SUBTYPE_PENTIUM_3_XEON", MachO::CPU_SUBTYPE_PENTIUM_3_XEON
}
,
132 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_M){ "CPU_SUBTYPE_PENTIUM_M", MachO::CPU_SUBTYPE_PENTIUM_M },
133 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4){ "CPU_SUBTYPE_PENTIUM_4", MachO::CPU_SUBTYPE_PENTIUM_4 },
134 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4_M){ "CPU_SUBTYPE_PENTIUM_4_M", MachO::CPU_SUBTYPE_PENTIUM_4_M },
135 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM){ "CPU_SUBTYPE_ITANIUM", MachO::CPU_SUBTYPE_ITANIUM },
136 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM_2){ "CPU_SUBTYPE_ITANIUM_2", MachO::CPU_SUBTYPE_ITANIUM_2 },
137 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON){ "CPU_SUBTYPE_XEON", MachO::CPU_SUBTYPE_XEON },
138 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON_MP){ "CPU_SUBTYPE_XEON_MP", MachO::CPU_SUBTYPE_XEON_MP },
139};
140
141static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX64[] = {
142 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_ALL){ "CPU_SUBTYPE_X86_64_ALL", MachO::CPU_SUBTYPE_X86_64_ALL },
143 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_ARCH1){ "CPU_SUBTYPE_X86_ARCH1", MachO::CPU_SUBTYPE_X86_ARCH1 },
144 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_H){ "CPU_SUBTYPE_X86_64_H", MachO::CPU_SUBTYPE_X86_64_H },
145};
146
147static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM[] = {
148 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_ALL){ "CPU_SUBTYPE_ARM_ALL", MachO::CPU_SUBTYPE_ARM_ALL },
149 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V4T){ "CPU_SUBTYPE_ARM_V4T", MachO::CPU_SUBTYPE_ARM_V4T },
150 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6){ "CPU_SUBTYPE_ARM_V6", MachO::CPU_SUBTYPE_ARM_V6 },
151 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5){ "CPU_SUBTYPE_ARM_V5", MachO::CPU_SUBTYPE_ARM_V5 },
152 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5TEJ){ "CPU_SUBTYPE_ARM_V5TEJ", MachO::CPU_SUBTYPE_ARM_V5TEJ },
153 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_XSCALE){ "CPU_SUBTYPE_ARM_XSCALE", MachO::CPU_SUBTYPE_ARM_XSCALE },
154 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7){ "CPU_SUBTYPE_ARM_V7", MachO::CPU_SUBTYPE_ARM_V7 },
155 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7S){ "CPU_SUBTYPE_ARM_V7S", MachO::CPU_SUBTYPE_ARM_V7S },
156 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7K){ "CPU_SUBTYPE_ARM_V7K", MachO::CPU_SUBTYPE_ARM_V7K },
157 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6M){ "CPU_SUBTYPE_ARM_V6M", MachO::CPU_SUBTYPE_ARM_V6M },
158 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7M){ "CPU_SUBTYPE_ARM_V7M", MachO::CPU_SUBTYPE_ARM_V7M },
159 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7EM){ "CPU_SUBTYPE_ARM_V7EM", MachO::CPU_SUBTYPE_ARM_V7EM },
160};
161
162static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM64[] = {
163 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_ALL){ "CPU_SUBTYPE_ARM64_ALL", MachO::CPU_SUBTYPE_ARM64_ALL },
164};
165
166static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesSPARC[] = {
167 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_SPARC_ALL){ "CPU_SUBTYPE_SPARC_ALL", MachO::CPU_SUBTYPE_SPARC_ALL },
168};
169
170static const EnumEntry<uint32_t> MachOHeaderCpuSubtypesPPC[] = {
171 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_ALL){ "CPU_SUBTYPE_POWERPC_ALL", MachO::CPU_SUBTYPE_POWERPC_ALL },
172 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_601){ "CPU_SUBTYPE_POWERPC_601", MachO::CPU_SUBTYPE_POWERPC_601 },
173 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_602){ "CPU_SUBTYPE_POWERPC_602", MachO::CPU_SUBTYPE_POWERPC_602 },
174 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603){ "CPU_SUBTYPE_POWERPC_603", MachO::CPU_SUBTYPE_POWERPC_603 },
175 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603e){ "CPU_SUBTYPE_POWERPC_603e", MachO::CPU_SUBTYPE_POWERPC_603e
}
,
176 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_603ev){ "CPU_SUBTYPE_POWERPC_603ev", MachO::CPU_SUBTYPE_POWERPC_603ev
}
,
177 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604){ "CPU_SUBTYPE_POWERPC_604", MachO::CPU_SUBTYPE_POWERPC_604 },
178 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_604e){ "CPU_SUBTYPE_POWERPC_604e", MachO::CPU_SUBTYPE_POWERPC_604e
}
,
179 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_620){ "CPU_SUBTYPE_POWERPC_620", MachO::CPU_SUBTYPE_POWERPC_620 },
180 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_750){ "CPU_SUBTYPE_POWERPC_750", MachO::CPU_SUBTYPE_POWERPC_750 },
181 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7400){ "CPU_SUBTYPE_POWERPC_7400", MachO::CPU_SUBTYPE_POWERPC_7400
}
,
182 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_7450){ "CPU_SUBTYPE_POWERPC_7450", MachO::CPU_SUBTYPE_POWERPC_7450
}
,
183 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_POWERPC_970){ "CPU_SUBTYPE_POWERPC_970", MachO::CPU_SUBTYPE_POWERPC_970 },
184};
185
186static const EnumEntry<uint32_t> MachOHeaderFlags[] = {
187 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOUNDEFS){ "MH_NOUNDEFS", MachO::MH_NOUNDEFS },
188 LLVM_READOBJ_ENUM_ENT(MachO, MH_INCRLINK){ "MH_INCRLINK", MachO::MH_INCRLINK },
189 LLVM_READOBJ_ENUM_ENT(MachO, MH_DYLDLINK){ "MH_DYLDLINK", MachO::MH_DYLDLINK },
190 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDATLOAD){ "MH_BINDATLOAD", MachO::MH_BINDATLOAD },
191 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBOUND){ "MH_PREBOUND", MachO::MH_PREBOUND },
192 LLVM_READOBJ_ENUM_ENT(MachO, MH_SPLIT_SEGS){ "MH_SPLIT_SEGS", MachO::MH_SPLIT_SEGS },
193 LLVM_READOBJ_ENUM_ENT(MachO, MH_LAZY_INIT){ "MH_LAZY_INIT", MachO::MH_LAZY_INIT },
194 LLVM_READOBJ_ENUM_ENT(MachO, MH_TWOLEVEL){ "MH_TWOLEVEL", MachO::MH_TWOLEVEL },
195 LLVM_READOBJ_ENUM_ENT(MachO, MH_FORCE_FLAT){ "MH_FORCE_FLAT", MachO::MH_FORCE_FLAT },
196 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOMULTIDEFS){ "MH_NOMULTIDEFS", MachO::MH_NOMULTIDEFS },
197 LLVM_READOBJ_ENUM_ENT(MachO, MH_NOFIXPREBINDING){ "MH_NOFIXPREBINDING", MachO::MH_NOFIXPREBINDING },
198 LLVM_READOBJ_ENUM_ENT(MachO, MH_PREBINDABLE){ "MH_PREBINDABLE", MachO::MH_PREBINDABLE },
199 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLMODSBOUND){ "MH_ALLMODSBOUND", MachO::MH_ALLMODSBOUND },
200 LLVM_READOBJ_ENUM_ENT(MachO, MH_SUBSECTIONS_VIA_SYMBOLS){ "MH_SUBSECTIONS_VIA_SYMBOLS", MachO::MH_SUBSECTIONS_VIA_SYMBOLS
}
,
201 LLVM_READOBJ_ENUM_ENT(MachO, MH_CANONICAL){ "MH_CANONICAL", MachO::MH_CANONICAL },
202 LLVM_READOBJ_ENUM_ENT(MachO, MH_WEAK_DEFINES){ "MH_WEAK_DEFINES", MachO::MH_WEAK_DEFINES },
203 LLVM_READOBJ_ENUM_ENT(MachO, MH_BINDS_TO_WEAK){ "MH_BINDS_TO_WEAK", MachO::MH_BINDS_TO_WEAK },
204 LLVM_READOBJ_ENUM_ENT(MachO, MH_ALLOW_STACK_EXECUTION){ "MH_ALLOW_STACK_EXECUTION", MachO::MH_ALLOW_STACK_EXECUTION
}
,
205 LLVM_READOBJ_ENUM_ENT(MachO, MH_ROOT_SAFE){ "MH_ROOT_SAFE", MachO::MH_ROOT_SAFE },
206 LLVM_READOBJ_ENUM_ENT(MachO, MH_SETUID_SAFE){ "MH_SETUID_SAFE", MachO::MH_SETUID_SAFE },
207 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_REEXPORTED_DYLIBS){ "MH_NO_REEXPORTED_DYLIBS", MachO::MH_NO_REEXPORTED_DYLIBS },
208 LLVM_READOBJ_ENUM_ENT(MachO, MH_PIE){ "MH_PIE", MachO::MH_PIE },
209 LLVM_READOBJ_ENUM_ENT(MachO, MH_DEAD_STRIPPABLE_DYLIB){ "MH_DEAD_STRIPPABLE_DYLIB", MachO::MH_DEAD_STRIPPABLE_DYLIB
}
,
210 LLVM_READOBJ_ENUM_ENT(MachO, MH_HAS_TLV_DESCRIPTORS){ "MH_HAS_TLV_DESCRIPTORS", MachO::MH_HAS_TLV_DESCRIPTORS },
211 LLVM_READOBJ_ENUM_ENT(MachO, MH_NO_HEAP_EXECUTION){ "MH_NO_HEAP_EXECUTION", MachO::MH_NO_HEAP_EXECUTION },
212 LLVM_READOBJ_ENUM_ENT(MachO, MH_APP_EXTENSION_SAFE){ "MH_APP_EXTENSION_SAFE", MachO::MH_APP_EXTENSION_SAFE },
213};
214
215static const EnumEntry<unsigned> MachOSectionAttributes[] = {
216 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
217 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
218 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
219 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
220 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
221 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
222 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
223 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
224 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
225 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
226};
227
228static const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
229 { "UndefinedNonLazy", 0 },
230 { "ReferenceFlagUndefinedLazy", 1 },
231 { "ReferenceFlagDefined", 2 },
232 { "ReferenceFlagPrivateDefined", 3 },
233 { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
234 { "ReferenceFlagPrivateUndefinedLazy", 5 }
235};
236
237static const EnumEntry<unsigned> MachOSymbolFlags[] = {
238 { "ReferencedDynamically", 0x10 },
239 { "NoDeadStrip", 0x20 },
240 { "WeakRef", 0x40 },
241 { "WeakDef", 0x80 },
242 { "AltEntry", 0x200 },
243};
244
245static const EnumEntry<unsigned> MachOSymbolTypes[] = {
246 { "Undef", 0x0 },
247 { "Abs", 0x2 },
248 { "Indirect", 0xA },
249 { "PreboundUndef", 0xC },
250 { "Section", 0xE }
251};
252
253namespace {
254 struct MachOSection {
255 ArrayRef<char> Name;
256 ArrayRef<char> SegmentName;
257 uint64_t Address;
258 uint64_t Size;
259 uint32_t Offset;
260 uint32_t Alignment;
261 uint32_t RelocationTableOffset;
262 uint32_t NumRelocationTableEntries;
263 uint32_t Flags;
264 uint32_t Reserved1;
265 uint32_t Reserved2;
266 uint32_t Reserved3;
267 };
268
269 struct MachOSegment {
270 std::string CmdName;
271 std::string SegName;
272 uint64_t cmdsize;
273 uint64_t vmaddr;
274 uint64_t vmsize;
275 uint64_t fileoff;
276 uint64_t filesize;
277 uint32_t maxprot;
278 uint32_t initprot;
279 uint32_t nsects;
280 uint32_t flags;
281 };
282
283 struct MachOSymbol {
284 uint32_t StringIndex;
285 uint8_t Type;
286 uint8_t SectionIndex;
287 uint16_t Flags;
288 uint64_t Value;
289 };
290}
291
292static std::string getMask(uint32_t prot)
293{
294 // TODO (davide): This always assumes prot is valid.
295 // Catch mistakes and report if needed.
296 std::string Prot;
297 Prot = "";
298 Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-";
299 Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-";
300 Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-";
301 return Prot;
302}
303
304static void getSection(const MachOObjectFile *Obj,
305 DataRefImpl Sec,
306 MachOSection &Section) {
307 if (!Obj->is64Bit()) {
308 MachO::section Sect = Obj->getSection(Sec);
309 Section.Address = Sect.addr;
310 Section.Size = Sect.size;
311 Section.Offset = Sect.offset;
312 Section.Alignment = Sect.align;
313 Section.RelocationTableOffset = Sect.reloff;
314 Section.NumRelocationTableEntries = Sect.nreloc;
315 Section.Flags = Sect.flags;
316 Section.Reserved1 = Sect.reserved1;
317 Section.Reserved2 = Sect.reserved2;
318 return;
319 }
320 MachO::section_64 Sect = Obj->getSection64(Sec);
321 Section.Address = Sect.addr;
322 Section.Size = Sect.size;
323 Section.Offset = Sect.offset;
324 Section.Alignment = Sect.align;
325 Section.RelocationTableOffset = Sect.reloff;
326 Section.NumRelocationTableEntries = Sect.nreloc;
327 Section.Flags = Sect.flags;
328 Section.Reserved1 = Sect.reserved1;
329 Section.Reserved2 = Sect.reserved2;
330 Section.Reserved3 = Sect.reserved3;
331}
332
333static void getSegment(const MachOObjectFile *Obj,
334 const MachOObjectFile::LoadCommandInfo &L,
335 MachOSegment &Segment) {
336 if (!Obj->is64Bit()) {
337 MachO::segment_command SC = Obj->getSegmentLoadCommand(L);
338 Segment.CmdName = "LC_SEGMENT";
339 Segment.SegName = SC.segname;
340 Segment.cmdsize = SC.cmdsize;
341 Segment.vmaddr = SC.vmaddr;
342 Segment.vmsize = SC.vmsize;
343 Segment.fileoff = SC.fileoff;
344 Segment.filesize = SC.filesize;
345 Segment.maxprot = SC.maxprot;
346 Segment.initprot = SC.initprot;
347 Segment.nsects = SC.nsects;
348 Segment.flags = SC.flags;
349 return;
350 }
351 MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L);
352 Segment.CmdName = "LC_SEGMENT_64";
353 Segment.SegName = SC.segname;
354 Segment.cmdsize = SC.cmdsize;
355 Segment.vmaddr = SC.vmaddr;
356 Segment.vmsize = SC.vmsize;
357 Segment.fileoff = SC.fileoff;
358 Segment.filesize = SC.filesize;
359 Segment.maxprot = SC.maxprot;
360 Segment.initprot = SC.initprot;
361 Segment.nsects = SC.nsects;
362 Segment.flags = SC.flags;
363}
364
365static void getSymbol(const MachOObjectFile *Obj,
366 DataRefImpl DRI,
367 MachOSymbol &Symbol) {
368 if (!Obj->is64Bit()) {
369 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
370 Symbol.StringIndex = Entry.n_strx;
371 Symbol.Type = Entry.n_type;
372 Symbol.SectionIndex = Entry.n_sect;
373 Symbol.Flags = Entry.n_desc;
374 Symbol.Value = Entry.n_value;
375 return;
376 }
377 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
378 Symbol.StringIndex = Entry.n_strx;
379 Symbol.Type = Entry.n_type;
380 Symbol.SectionIndex = Entry.n_sect;
381 Symbol.Flags = Entry.n_desc;
382 Symbol.Value = Entry.n_value;
383}
384
385void MachODumper::printFileHeaders() {
386 DictScope H(W, "MachHeader");
387 if (!Obj->is64Bit()) {
388 printFileHeaders(Obj->getHeader());
389 } else {
390 printFileHeaders(Obj->getHeader64());
391 W.printHex("Reserved", Obj->getHeader64().reserved);
392 }
393}
394
395template<class MachHeader>
396void MachODumper::printFileHeaders(const MachHeader &Header) {
397 W.printEnum("Magic", Header.magic, makeArrayRef(MachOMagics));
398 W.printEnum("CpuType", Header.cputype, makeArrayRef(MachOHeaderCpuTypes));
399 uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK;
400 switch (Header.cputype) {
401 case MachO::CPU_TYPE_X86:
402 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX86));
403 break;
404 case MachO::CPU_TYPE_X86_64:
405 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX64));
406 break;
407 case MachO::CPU_TYPE_ARM:
408 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM));
409 break;
410 case MachO::CPU_TYPE_POWERPC:
411 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesPPC));
412 break;
413 case MachO::CPU_TYPE_SPARC:
414 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesSPARC));
415 break;
416 case MachO::CPU_TYPE_ARM64:
417 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM64));
418 break;
419 case MachO::CPU_TYPE_POWERPC64:
420 default:
421 W.printHex("CpuSubtype", subtype);
422 }
423 W.printEnum("FileType", Header.filetype, makeArrayRef(MachOHeaderFileTypes));
424 W.printNumber("NumOfLoadCommands", Header.ncmds);
425 W.printNumber("SizeOfLoadCommands", Header.sizeofcmds);
426 W.printFlags("Flags", Header.flags, makeArrayRef(MachOHeaderFlags));
427}
428
429void MachODumper::printSections() {
430 return printSections(Obj);
431}
432
433void MachODumper::printSections(const MachOObjectFile *Obj) {
434 ListScope Group(W, "Sections");
435
436 int SectionIndex = -1;
437 for (const SectionRef &Section : Obj->sections()) {
438 ++SectionIndex;
439
440 MachOSection MOSection;
441 getSection(Obj, Section.getRawDataRefImpl(), MOSection);
442 DataRefImpl DR = Section.getRawDataRefImpl();
443
444 StringRef Name;
445 error(Section.getName(Name));
446
447 ArrayRef<char> RawName = Obj->getSectionRawName(DR);
448 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
449 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
450
451 DictScope SectionD(W, "Section");
452 W.printNumber("Index", SectionIndex);
453 W.printBinary("Name", Name, RawName);
454 W.printBinary("Segment", SegmentName, RawSegmentName);
455 W.printHex("Address", MOSection.Address);
456 W.printHex("Size", MOSection.Size);
457 W.printNumber("Offset", MOSection.Offset);
458 W.printNumber("Alignment", MOSection.Alignment);
459 W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
460 W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
461 W.printEnum("Type", MOSection.Flags & 0xFF,
462 makeArrayRef(MachOSectionAttributes));
463 W.printFlags("Attributes", MOSection.Flags >> 8,
464 makeArrayRef(MachOSectionAttributes));
465 W.printHex("Reserved1", MOSection.Reserved1);
466 W.printHex("Reserved2", MOSection.Reserved2);
467 if (Obj->is64Bit())
468 W.printHex("Reserved3", MOSection.Reserved3);
469
470 if (opts::SectionRelocations) {
471 ListScope D(W, "Relocations");
472 for (const RelocationRef &Reloc : Section.relocations())
473 printRelocation(Reloc);
474 }
475
476 if (opts::SectionSymbols) {
477 ListScope D(W, "Symbols");
478 for (const SymbolRef &Symbol : Obj->symbols()) {
479 if (!Section.containsSymbol(Symbol))
480 continue;
481
482 printSymbol(Symbol);
483 }
484 }
485
486 if (opts::SectionData) {
487 bool IsBSS = Section.isBSS();
488 if (!IsBSS) {
489 StringRef Data;
490 error(Section.getContents(Data));
491
492 W.printBinaryBlock("SectionData", Data);
493 }
494 }
495 }
496}
497
498void MachODumper::printRelocations() {
499 ListScope D(W, "Relocations");
500
501 std::error_code EC;
502 for (const SectionRef &Section : Obj->sections()) {
503 StringRef Name;
504 error(Section.getName(Name));
505
506 bool PrintedGroup = false;
507 for (const RelocationRef &Reloc : Section.relocations()) {
508 if (!PrintedGroup) {
509 W.startLine() << "Section " << Name << " {\n";
510 W.indent();
511 PrintedGroup = true;
512 }
513
514 printRelocation(Reloc);
515 }
516
517 if (PrintedGroup) {
518 W.unindent();
519 W.startLine() << "}\n";
520 }
521 }
522}
523
524void MachODumper::printRelocation(const RelocationRef &Reloc) {
525 return printRelocation(Obj, Reloc);
526}
527
528void MachODumper::printRelocation(const MachOObjectFile *Obj,
529 const RelocationRef &Reloc) {
530 uint64_t Offset = Reloc.getOffset();
531 SmallString<32> RelocName;
532 Reloc.getTypeName(RelocName);
533
534 DataRefImpl DR = Reloc.getRawDataRefImpl();
535 MachO::any_relocation_info RE = Obj->getRelocation(DR);
536 bool IsScattered = Obj->isRelocationScattered(RE);
537 bool IsExtern = !IsScattered && Obj->getPlainRelocationExternal(RE);
538
539 StringRef TargetName;
540 if (IsExtern) {
541 symbol_iterator Symbol = Reloc.getSymbol();
542 if (Symbol != Obj->symbol_end()) {
543 Expected<StringRef> TargetNameOrErr = Symbol->getName();
544 if (!TargetNameOrErr)
545 error(errorToErrorCode(TargetNameOrErr.takeError()));
546 TargetName = *TargetNameOrErr;
547 }
548 } else if (!IsScattered) {
549 section_iterator SecI = Obj->getRelocationSection(DR);
550 if (SecI != Obj->section_end()) {
551 error(SecI->getName(TargetName));
552 }
553 }
554 if (TargetName.empty())
555 TargetName = "-";
556
557 if (opts::ExpandRelocs) {
558 DictScope Group(W, "Relocation");
559 W.printHex("Offset", Offset);
560 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
561 W.printNumber("Length", Obj->getAnyRelocationLength(RE));
562 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
563 if (IsScattered) {
564 W.printHex("Value", Obj->getScatteredRelocationValue(RE));
565 } else {
566 const char *Kind = IsExtern ? "Symbol" : "Section";
567 W.printNumber(Kind, TargetName, Obj->getPlainRelocationSymbolNum(RE));
568 }
569 } else {
570 SmallString<32> SymbolNameOrOffset("0x");
571 if (IsScattered) {
572 // Scattered relocations don't really have an associated symbol for some
573 // reason, even if one exists in the symtab at the correct address.
574 SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE));
575 } else {
576 SymbolNameOrOffset = TargetName;
577 }
578
579 raw_ostream& OS = W.startLine();
580 OS << W.hex(Offset)
581 << " " << Obj->getAnyRelocationPCRel(RE)
582 << " " << Obj->getAnyRelocationLength(RE);
583 if (IsScattered)
584 OS << " n/a";
585 else
586 OS << " " << Obj->getPlainRelocationExternal(RE);
587 OS << " " << RelocName
588 << " " << IsScattered
589 << " " << SymbolNameOrOffset
590 << "\n";
591 }
592}
593
594void MachODumper::printSymbols() {
595 ListScope Group(W, "Symbols");
596
597 for (const SymbolRef &Symbol : Obj->symbols()) {
598 printSymbol(Symbol);
599 }
600}
601
602void MachODumper::printDynamicSymbols() {
603 ListScope Group(W, "DynamicSymbols");
604}
605
606void MachODumper::printSymbol(const SymbolRef &Symbol) {
607 StringRef SymbolName;
608 Expected<StringRef> SymbolNameOrErr = Symbol.getName();
609 if (!SymbolNameOrErr) {
610 // TODO: Actually report errors helpfully.
611 consumeError(SymbolNameOrErr.takeError());
612 } else
613 SymbolName = *SymbolNameOrErr;
614
615 MachOSymbol MOSymbol;
616 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
617
618 StringRef SectionName = "";
619 Expected<section_iterator> SecIOrErr = Symbol.getSection();
620 error(errorToErrorCode(SecIOrErr.takeError()));
621 section_iterator SecI = *SecIOrErr;
622 if (SecI != Obj->section_end())
623 error(SecI->getName(SectionName));
624
625 DictScope D(W, "Symbol");
626 W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
627 if (MOSymbol.Type & MachO::N_STAB) {
628 W.printHex("Type", "SymDebugTable", MOSymbol.Type);
629 } else {
630 if (MOSymbol.Type & MachO::N_PEXT)
631 W.startLine() << "PrivateExtern\n";
632 if (MOSymbol.Type & MachO::N_EXT)
633 W.startLine() << "Extern\n";
634 W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
635 makeArrayRef(MachOSymbolTypes));
636 }
637 W.printHex("Section", SectionName, MOSymbol.SectionIndex);
638 W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0xF),
639 makeArrayRef(MachOSymbolRefTypes));
640 W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0xF),
641 makeArrayRef(MachOSymbolFlags));
642 W.printHex("Value", MOSymbol.Value);
643}
644
645void MachODumper::printUnwindInfo() {
646 W.startLine() << "UnwindInfo not implemented.\n";
647}
648
649void MachODumper::printStackMap() const {
650 object::SectionRef StackMapSection;
1
Calling defaulted default constructor for 'SectionRef'
3
Returning from default constructor for 'SectionRef'
651 for (auto Sec : Obj->sections()) {
652 StringRef Name;
653 Sec.getName(Name);
654 if (Name == "__llvm_stackmaps") {
655 StackMapSection = Sec;
656 break;
657 }
658 }
659
660 if (StackMapSection == object::SectionRef())
4
Taking false branch
661 return;
662
663 StringRef StackMapContents;
664 StackMapSection.getContents(StackMapContents);
5
Calling 'SectionRef::getContents'
665 ArrayRef<uint8_t> StackMapContentsArray(
666 reinterpret_cast<const uint8_t*>(StackMapContents.data()),
667 StackMapContents.size());
668
669 if (Obj->isLittleEndian())
670 prettyPrintStackMap(
671 llvm::outs(),
672 StackMapV2Parser<support::little>(StackMapContentsArray));
673 else
674 prettyPrintStackMap(llvm::outs(),
675 StackMapV2Parser<support::big>(StackMapContentsArray));
676}
677
678void MachODumper::printMachODataInCode() {
679 for (const auto &Load : Obj->load_commands()) {
680 if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
681 MachO::linkedit_data_command LLC = Obj->getLinkeditDataLoadCommand(Load);
682 DictScope Group(W, "DataInCode");
683 W.printNumber("Data offset", LLC.dataoff);
684 W.printNumber("Data size", LLC.datasize);
685 ListScope D(W, "Data entries");
686 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
687 for (unsigned i = 0; i < NumRegions; ++i) {
688 MachO::data_in_code_entry DICE = Obj->getDataInCodeTableEntry(
689 LLC.dataoff, i);
690 DictScope Group(W, "Entry");
691 W.printNumber("Index", i);
692 W.printNumber("Offset", DICE.offset);
693 W.printNumber("Length", DICE.length);
694 W.printNumber("Kind", DICE.kind);
695 }
696 }
697 }
698}
699
700void MachODumper::printMachOVersionMin() {
701 for (const auto &Load : Obj->load_commands()) {
702 StringRef Cmd;
703 switch (Load.C.cmd) {
704 case MachO::LC_VERSION_MIN_MACOSX:
705 Cmd = "LC_VERSION_MIN_MACOSX";
706 break;
707 case MachO::LC_VERSION_MIN_IPHONEOS:
708 Cmd = "LC_VERSION_MIN_IPHONEOS";
709 break;
710 case MachO::LC_VERSION_MIN_TVOS:
711 Cmd = "LC_VERSION_MIN_TVOS";
712 break;
713 case MachO::LC_VERSION_MIN_WATCHOS:
714 Cmd = "LC_VERSION_MIN_WATCHOS";
715 break;
716 case MachO::LC_BUILD_VERSION:
717 Cmd = "LC_BUILD_VERSION";
718 break;
719 default:
720 continue;
721 }
722
723 DictScope Group(W, "MinVersion");
724 // Handle LC_BUILD_VERSION.
725 if (Load.C.cmd == MachO::LC_BUILD_VERSION) {
726 MachO::build_version_command BVC = Obj->getBuildVersionLoadCommand(Load);
727 W.printString("Cmd", Cmd);
728 W.printNumber("Size", BVC.cmdsize);
729 W.printString("Platform",
730 MachOObjectFile::getBuildPlatform(BVC.platform));
731 W.printString("Version", MachOObjectFile::getVersionString(BVC.minos));
732 if (BVC.sdk)
733 W.printString("SDK", MachOObjectFile::getVersionString(BVC.sdk));
734 else
735 W.printString("SDK", StringRef("n/a"));
736 continue;
737 }
738
739 MachO::version_min_command VMC = Obj->getVersionMinLoadCommand(Load);
740 W.printString("Cmd", Cmd);
741 W.printNumber("Size", VMC.cmdsize);
742 SmallString<32> Version;
743 Version = utostr(MachOObjectFile::getVersionMinMajor(VMC, false)) + "." +
744 utostr(MachOObjectFile::getVersionMinMinor(VMC, false));
745 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, false);
746 if (Update != 0)
747 Version += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, false));
748 W.printString("Version", Version);
749 SmallString<32> SDK;
750 if (VMC.sdk == 0)
751 SDK = "n/a";
752 else {
753 SDK = utostr(MachOObjectFile::getVersionMinMajor(VMC, true)) + "." +
754 utostr(MachOObjectFile::getVersionMinMinor(VMC, true));
755 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, true);
756 if (Update != 0)
757 SDK += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, true));
758 }
759 W.printString("SDK", SDK);
760 }
761}
762
763void MachODumper::printMachODysymtab() {
764 for (const auto &Load : Obj->load_commands()) {
765 if (Load.C.cmd == MachO::LC_DYSYMTAB) {
766 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
767 DictScope Group(W, "Dysymtab");
768 W.printNumber("ilocalsym", DLC.ilocalsym);
769 W.printNumber("nlocalsym", DLC.nlocalsym);
770 W.printNumber("iextdefsym", DLC.iextdefsym);
771 W.printNumber("nextdefsym", DLC.nextdefsym);
772 W.printNumber("iundefsym", DLC.iundefsym);
773 W.printNumber("nundefsym", DLC.nundefsym);
774 W.printNumber("tocoff", DLC.tocoff);
775 W.printNumber("ntoc", DLC.ntoc);
776 W.printNumber("modtaboff", DLC.modtaboff);
777 W.printNumber("nmodtab", DLC.nmodtab);
778 W.printNumber("extrefsymoff", DLC.extrefsymoff);
779 W.printNumber("nextrefsyms", DLC.nextrefsyms);
780 W.printNumber("indirectsymoff", DLC.indirectsymoff);
781 W.printNumber("nindirectsyms", DLC.nindirectsyms);
782 W.printNumber("extreloff", DLC.extreloff);
783 W.printNumber("nextrel", DLC.nextrel);
784 W.printNumber("locreloff", DLC.locreloff);
785 W.printNumber("nlocrel", DLC.nlocrel);
786 }
787 }
788}
789
790void MachODumper::printMachOSegment() {
791 for (const auto &Load : Obj->load_commands()) {
792 if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) {
793 MachOSegment MOSegment;
794 getSegment(Obj, Load, MOSegment);
795 DictScope Group(W, "Segment");
796 W.printString("Cmd", MOSegment.CmdName);
797 W.printString("Name", MOSegment.SegName);
798 W.printNumber("Size", MOSegment.cmdsize);
799 W.printHex("vmaddr", MOSegment.vmaddr);
800 W.printHex("vmsize", MOSegment.vmsize);
801 W.printNumber("fileoff", MOSegment.fileoff);
802 W.printNumber("filesize", MOSegment.filesize);
803 W.printString("maxprot", getMask(MOSegment.maxprot));
804 W.printString("initprot", getMask(MOSegment.initprot));
805 W.printNumber("nsects", MOSegment.nsects);
806 W.printHex("flags", MOSegment.flags);
807 }
808 }
809}
810
811void MachODumper::printMachOIndirectSymbols() {
812 for (const auto &Load : Obj->load_commands()) {
813 if (Load.C.cmd == MachO::LC_DYSYMTAB) {
814 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
815 DictScope Group(W, "Indirect Symbols");
816 W.printNumber("Number", DLC.nindirectsyms);
817 ListScope D(W, "Symbols");
818 for (unsigned i = 0; i < DLC.nindirectsyms; ++i) {
819 DictScope Group(W, "Entry");
820 W.printNumber("Entry Index", i);
821 W.printHex("Symbol Index", Obj->getIndirectSymbolTableEntry(DLC, i));
822 }
823 }
824 }
825}
826
827void MachODumper::printMachOLinkerOptions() {
828 for (const auto &Load : Obj->load_commands()) {
829 if (Load.C.cmd == MachO::LC_LINKER_OPTION) {
830 MachO::linker_option_command LOLC = Obj->getLinkerOptionLoadCommand(Load);
831 DictScope Group(W, "Linker Options");
832 W.printNumber("Size", LOLC.cmdsize);
833 ListScope D(W, "Strings");
834 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command);
835 const char *P = Load.Ptr + sizeof(MachO::linker_option_command);
836 StringRef Data(P, DataSize);
837 for (unsigned i = 0; i < LOLC.count; ++i) {
838 std::pair<StringRef,StringRef> Split = Data.split('\0');
839 W.printString("Value", Split.first);
840 Data = Split.second;
841 }
842 }
843 }
844}

/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/Object/ObjectFile.h

1//===- ObjectFile.h - File format independent object file -------*- C++ -*-===//
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 declares a file format independent ObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_OBJECT_OBJECTFILE_H
15#define LLVM_OBJECT_OBJECTFILE_H
16
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/iterator_range.h"
19#include "llvm/BinaryFormat/Magic.h"
20#include "llvm/MC/SubtargetFeature.h"
21#include "llvm/Object/Binary.h"
22#include "llvm/Object/Error.h"
23#include "llvm/Object/SymbolicFile.h"
24#include "llvm/Support/Casting.h"
25#include "llvm/Support/Error.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include <cassert>
29#include <cstdint>
30#include <memory>
31#include <system_error>
32
33namespace llvm {
34
35class ARMAttributeParser;
36
37namespace object {
38
39class COFFObjectFile;
40class MachOObjectFile;
41class ObjectFile;
42class SectionRef;
43class SymbolRef;
44class symbol_iterator;
45class WasmObjectFile;
46
47using section_iterator = content_iterator<SectionRef>;
48
49/// This is a value type class that represents a single relocation in the list
50/// of relocations in the object file.
51class RelocationRef {
52 DataRefImpl RelocationPimpl;
53 const ObjectFile *OwningObject = nullptr;
54
55public:
56 RelocationRef() = default;
57 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
58
59 bool operator==(const RelocationRef &Other) const;
60
61 void moveNext();
62
63 uint64_t getOffset() const;
64 symbol_iterator getSymbol() const;
65 uint64_t getType() const;
66
67 /// @brief Get a string that represents the type of this relocation.
68 ///
69 /// This is for display purposes only.
70 void getTypeName(SmallVectorImpl<char> &Result) const;
71
72 DataRefImpl getRawDataRefImpl() const;
73 const ObjectFile *getObject() const;
74};
75
76using relocation_iterator = content_iterator<RelocationRef>;
77
78/// This is a value type class that represents a single section in the list of
79/// sections in the object file.
80class SectionRef {
81 friend class SymbolRef;
82
83 DataRefImpl SectionPimpl;
84 const ObjectFile *OwningObject = nullptr;
2
Null pointer value stored to 'StackMapSection.OwningObject'
85
86public:
87 SectionRef() = default;
88 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
89
90 bool operator==(const SectionRef &Other) const;
91 bool operator!=(const SectionRef &Other) const;
92 bool operator<(const SectionRef &Other) const;
93
94 void moveNext();
95
96 std::error_code getName(StringRef &Result) const;
97 uint64_t getAddress() const;
98 uint64_t getIndex() const;
99 uint64_t getSize() const;
100 std::error_code getContents(StringRef &Result) const;
101
102 /// @brief Get the alignment of this section as the actual value (not log 2).
103 uint64_t getAlignment() const;
104
105 bool isCompressed() const;
106 bool isText() const;
107 bool isData() const;
108 bool isBSS() const;
109 bool isVirtual() const;
110 bool isBitcode() const;
111 bool isStripped() const;
112
113 bool containsSymbol(SymbolRef S) const;
114
115 relocation_iterator relocation_begin() const;
116 relocation_iterator relocation_end() const;
117 iterator_range<relocation_iterator> relocations() const {
118 return make_range(relocation_begin(), relocation_end());
119 }
120 section_iterator getRelocatedSection() const;
121
122 DataRefImpl getRawDataRefImpl() const;
123 const ObjectFile *getObject() const;
124};
125
126/// This is a value type class that represents a single symbol in the list of
127/// symbols in the object file.
128class SymbolRef : public BasicSymbolRef {
129 friend class SectionRef;
130
131public:
132 enum Type {
133 ST_Unknown, // Type not specified
134 ST_Data,
135 ST_Debug,
136 ST_File,
137 ST_Function,
138 ST_Other
139 };
140
141 SymbolRef() = default;
142 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
143 SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
144 assert(isa<ObjectFile>(BasicSymbolRef::getObject()))((isa<ObjectFile>(BasicSymbolRef::getObject())) ? static_cast
<void> (0) : __assert_fail ("isa<ObjectFile>(BasicSymbolRef::getObject())"
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/Object/ObjectFile.h"
, 144, __PRETTY_FUNCTION__))
;
145 }
146
147 Expected<StringRef> getName() const;
148 /// Returns the symbol virtual address (i.e. address at which it will be
149 /// mapped).
150 Expected<uint64_t> getAddress() const;
151
152 /// Return the value of the symbol depending on the object this can be an
153 /// offset or a virtual address.
154 uint64_t getValue() const;
155
156 /// @brief Get the alignment of this symbol as the actual value (not log 2).
157 uint32_t getAlignment() const;
158 uint64_t getCommonSize() const;
159 Expected<SymbolRef::Type> getType() const;
160
161 /// @brief Get section this symbol is defined in reference to. Result is
162 /// end_sections() if it is undefined or is an absolute symbol.
163 Expected<section_iterator> getSection() const;
164
165 const ObjectFile *getObject() const;
166};
167
168class symbol_iterator : public basic_symbol_iterator {
169public:
170 symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
171 symbol_iterator(const basic_symbol_iterator &B)
172 : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
173 cast<ObjectFile>(B->getObject()))) {}
174
175 const SymbolRef *operator->() const {
176 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
177 return static_cast<const SymbolRef*>(&P);
178 }
179
180 const SymbolRef &operator*() const {
181 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
182 return static_cast<const SymbolRef&>(P);
183 }
184};
185
186/// This class is the base class for all object file types. Concrete instances
187/// of this object are created by createObjectFile, which figures out which type
188/// to create.
189class ObjectFile : public SymbolicFile {
190 virtual void anchor();
191
192protected:
193 ObjectFile(unsigned int Type, MemoryBufferRef Source);
194
195 const uint8_t *base() const {
196 return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
197 }
198
199 // These functions are for SymbolRef to call internally. The main goal of
200 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
201 // entry in the memory mapped object file. SymbolPimpl cannot contain any
202 // virtual functions because then it could not point into the memory mapped
203 // file.
204 //
205 // Implementations assume that the DataRefImpl is valid and has not been
206 // modified externally. It's UB otherwise.
207 friend class SymbolRef;
208
209 virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
210 std::error_code printSymbolName(raw_ostream &OS,
211 DataRefImpl Symb) const override;
212 virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
213 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
214 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
215 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
216 virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
217 virtual Expected<section_iterator>
218 getSymbolSection(DataRefImpl Symb) const = 0;
219
220 // Same as above for SectionRef.
221 friend class SectionRef;
222
223 virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
224 virtual std::error_code getSectionName(DataRefImpl Sec,
225 StringRef &Res) const = 0;
226 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
227 virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0;
228 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
229 virtual std::error_code getSectionContents(DataRefImpl Sec,
230 StringRef &Res) const = 0;
231 virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
232 virtual bool isSectionCompressed(DataRefImpl Sec) const = 0;
233 virtual bool isSectionText(DataRefImpl Sec) const = 0;
234 virtual bool isSectionData(DataRefImpl Sec) const = 0;
235 virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
236 // A section is 'virtual' if its contents aren't present in the object image.
237 virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
238 virtual bool isSectionBitcode(DataRefImpl Sec) const;
239 virtual bool isSectionStripped(DataRefImpl Sec) const;
240 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
241 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
242 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
243
244 // Same as above for RelocationRef.
245 friend class RelocationRef;
246 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
247 virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
248 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
249 virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
250 virtual void getRelocationTypeName(DataRefImpl Rel,
251 SmallVectorImpl<char> &Result) const = 0;
252
253 uint64_t getSymbolValue(DataRefImpl Symb) const;
254
255public:
256 ObjectFile() = delete;
257 ObjectFile(const ObjectFile &other) = delete;
258
259 uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
260 assert(getSymbolFlags(Symb) & SymbolRef::SF_Common)((getSymbolFlags(Symb) & SymbolRef::SF_Common) ? static_cast
<void> (0) : __assert_fail ("getSymbolFlags(Symb) & SymbolRef::SF_Common"
, "/build/llvm-toolchain-snapshot-6.0~svn318211/include/llvm/Object/ObjectFile.h"
, 260, __PRETTY_FUNCTION__))
;
261 return getCommonSymbolSizeImpl(Symb);
262 }
263
264 using symbol_iterator_range = iterator_range<symbol_iterator>;
265 symbol_iterator_range symbols() const {
266 return symbol_iterator_range(symbol_begin(), symbol_end());
267 }
268
269 virtual section_iterator section_begin() const = 0;
270 virtual section_iterator section_end() const = 0;
271
272 using section_iterator_range = iterator_range<section_iterator>;
273 section_iterator_range sections() const {
274 return section_iterator_range(section_begin(), section_end());
275 }
276
277 /// @brief The number of bytes used to represent an address in this object
278 /// file format.
279 virtual uint8_t getBytesInAddress() const = 0;
280
281 virtual StringRef getFileFormatName() const = 0;
282 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
283 virtual SubtargetFeatures getFeatures() const = 0;
284 virtual void setARMSubArch(Triple &TheTriple) const { }
285
286 /// @brief Create a triple from the data in this object file.
287 Triple makeTriple() const;
288
289 /// Returns platform-specific object flags, if any.
290 virtual std::error_code getPlatformFlags(unsigned &Result) const {
291 Result = 0;
292 return object_error::invalid_file_type;
293 }
294
295 virtual std::error_code
296 getBuildAttributes(ARMAttributeParser &Attributes) const {
297 return std::error_code();
298 }
299
300 /// Maps a debug section name to a standard DWARF section name.
301 virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; }
302
303 /// True if this is a relocatable object (.o/.obj).
304 virtual bool isRelocatableObject() const = 0;
305
306 /// @returns Pointer to ObjectFile subclass to handle this type of object.
307 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
308 /// return true.
309 /// @brief Create ObjectFile from path.
310 static Expected<OwningBinary<ObjectFile>>
311 createObjectFile(StringRef ObjectPath);
312
313 static Expected<std::unique_ptr<ObjectFile>>
314 createObjectFile(MemoryBufferRef Object, llvm::file_magic Type);
315 static Expected<std::unique_ptr<ObjectFile>>
316 createObjectFile(MemoryBufferRef Object) {
317 return createObjectFile(Object, llvm::file_magic::unknown);
318 }
319
320 static bool classof(const Binary *v) {
321 return v->isObject();
322 }
323
324 static Expected<std::unique_ptr<COFFObjectFile>>
325 createCOFFObjectFile(MemoryBufferRef Object);
326
327 static Expected<std::unique_ptr<ObjectFile>>
328 createELFObjectFile(MemoryBufferRef Object);
329
330 static Expected<std::unique_ptr<MachOObjectFile>>
331 createMachOObjectFile(MemoryBufferRef Object,
332 uint32_t UniversalCputype = 0,
333 uint32_t UniversalIndex = 0);
334
335 static Expected<std::unique_ptr<WasmObjectFile>>
336 createWasmObjectFile(MemoryBufferRef Object);
337};
338
339// Inline function definitions.
340inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
341 : BasicSymbolRef(SymbolP, Owner) {}
342
343inline Expected<StringRef> SymbolRef::getName() const {
344 return getObject()->getSymbolName(getRawDataRefImpl());
345}
346
347inline Expected<uint64_t> SymbolRef::getAddress() const {
348 return getObject()->getSymbolAddress(getRawDataRefImpl());
349}
350
351inline uint64_t SymbolRef::getValue() const {
352 return getObject()->getSymbolValue(getRawDataRefImpl());
353}
354
355inline uint32_t SymbolRef::getAlignment() const {
356 return getObject()->getSymbolAlignment(getRawDataRefImpl());
357}
358
359inline uint64_t SymbolRef::getCommonSize() const {
360 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
361}
362
363inline Expected<section_iterator> SymbolRef::getSection() const {
364 return getObject()->getSymbolSection(getRawDataRefImpl());
365}
366
367inline Expected<SymbolRef::Type> SymbolRef::getType() const {
368 return getObject()->getSymbolType(getRawDataRefImpl());
369}
370
371inline const ObjectFile *SymbolRef::getObject() const {
372 const SymbolicFile *O = BasicSymbolRef::getObject();
373 return cast<ObjectFile>(O);
374}
375
376/// SectionRef
377inline SectionRef::SectionRef(DataRefImpl SectionP,
378 const ObjectFile *Owner)
379 : SectionPimpl(SectionP)
380 , OwningObject(Owner) {}
381
382inline bool SectionRef::operator==(const SectionRef &Other) const {
383 return SectionPimpl == Other.SectionPimpl;
384}
385
386inline bool SectionRef::operator!=(const SectionRef &Other) const {
387 return SectionPimpl != Other.SectionPimpl;
388}
389
390inline bool SectionRef::operator<(const SectionRef &Other) const {
391 return SectionPimpl < Other.SectionPimpl;
392}
393
394inline void SectionRef::moveNext() {
395 return OwningObject->moveSectionNext(SectionPimpl);
396}
397
398inline std::error_code SectionRef::getName(StringRef &Result) const {
399 return OwningObject->getSectionName(SectionPimpl, Result);
400}
401
402inline uint64_t SectionRef::getAddress() const {
403 return OwningObject->getSectionAddress(SectionPimpl);
404}
405
406inline uint64_t SectionRef::getIndex() const {
407 return OwningObject->getSectionIndex(SectionPimpl);
408}
409
410inline uint64_t SectionRef::getSize() const {
411 return OwningObject->getSectionSize(SectionPimpl);
412}
413
414inline std::error_code SectionRef::getContents(StringRef &Result) const {
415 return OwningObject->getSectionContents(SectionPimpl, Result);
6
Called C++ object pointer is null
416}
417
418inline uint64_t SectionRef::getAlignment() const {
419 return OwningObject->getSectionAlignment(SectionPimpl);
420}
421
422inline bool SectionRef::isCompressed() const {
423 return OwningObject->isSectionCompressed(SectionPimpl);
424}
425
426inline bool SectionRef::isText() const {
427 return OwningObject->isSectionText(SectionPimpl);
428}
429
430inline bool SectionRef::isData() const {
431 return OwningObject->isSectionData(SectionPimpl);
432}
433
434inline bool SectionRef::isBSS() const {
435 return OwningObject->isSectionBSS(SectionPimpl);
436}
437
438inline bool SectionRef::isVirtual() const {
439 return OwningObject->isSectionVirtual(SectionPimpl);
440}
441
442inline bool SectionRef::isBitcode() const {
443 return OwningObject->isSectionBitcode(SectionPimpl);
444}
445
446inline bool SectionRef::isStripped() const {
447 return OwningObject->isSectionStripped(SectionPimpl);
448}
449
450inline relocation_iterator SectionRef::relocation_begin() const {
451 return OwningObject->section_rel_begin(SectionPimpl);
452}
453
454inline relocation_iterator SectionRef::relocation_end() const {
455 return OwningObject->section_rel_end(SectionPimpl);
456}
457
458inline section_iterator SectionRef::getRelocatedSection() const {
459 return OwningObject->getRelocatedSection(SectionPimpl);
460}
461
462inline DataRefImpl SectionRef::getRawDataRefImpl() const {
463 return SectionPimpl;
464}
465
466inline const ObjectFile *SectionRef::getObject() const {
467 return OwningObject;
468}
469
470/// RelocationRef
471inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
472 const ObjectFile *Owner)
473 : RelocationPimpl(RelocationP)
474 , OwningObject(Owner) {}
475
476inline bool RelocationRef::operator==(const RelocationRef &Other) const {
477 return RelocationPimpl == Other.RelocationPimpl;
478}
479
480inline void RelocationRef::moveNext() {
481 return OwningObject->moveRelocationNext(RelocationPimpl);
482}
483
484inline uint64_t RelocationRef::getOffset() const {
485 return OwningObject->getRelocationOffset(RelocationPimpl);
486}
487
488inline symbol_iterator RelocationRef::getSymbol() const {
489 return OwningObject->getRelocationSymbol(RelocationPimpl);
490}
491
492inline uint64_t RelocationRef::getType() const {
493 return OwningObject->getRelocationType(RelocationPimpl);
494}
495
496inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
497 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
498}
499
500inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
501 return RelocationPimpl;
502}
503
504inline const ObjectFile *RelocationRef::getObject() const {
505 return OwningObject;
506}
507
508} // end namespace object
509
510} // end namespace llvm
511
512#endif // LLVM_OBJECT_OBJECTFILE_H