Bug Summary

File:llvm/include/llvm/Object/ObjectFile.h
Warning:line 463, column 7
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name MachODumper.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/llvm-readobj -I /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/tools/llvm-readobj -I include -I /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/include -D _FORTIFY_SOURCE=2 -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm=build-llvm -fmacro-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm=build-llvm -fcoverage-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -O3 -Wno-unused-command-line-argument -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/build-llvm=build-llvm -fdebug-prefix-map=/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/= -ferror-limit 19 -fvisibility-inlines-hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2022-01-25-232935-20746-1 -x c++ /build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/tools/llvm-readobj/MachODumper.cpp

/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/tools/llvm-readobj/MachODumper.cpp

1//===- MachODumper.cpp - Object file dumping utility for llvm -------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the MachO-specific dumper for llvm-readobj.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ObjDumper.h"
14#include "StackMapPrinter.h"
15#include "llvm-readobj.h"
16#include "llvm/ADT/SmallString.h"
17#include "llvm/ADT/StringExtras.h"
18#include "llvm/Object/MachO.h"
19#include "llvm/Support/BinaryStreamReader.h"
20#include "llvm/Support/Casting.h"
21#include "llvm/Support/ScopedPrinter.h"
22
23using namespace llvm;
24using namespace object;
25
26namespace {
27
28class MachODumper : public ObjDumper {
29public:
30 MachODumper(const MachOObjectFile *Obj, ScopedPrinter &Writer)
31 : ObjDumper(Writer, Obj->getFileName()), Obj(Obj) {}
32
33 void printFileHeaders() override;
34 void printSectionHeaders() override;
35 void printRelocations() override;
36 void printUnwindInfo() override;
37 void printStackMap() const override;
38 void printCGProfile() override;
39
40 void printNeededLibraries() 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 StringRef getSymbolName(const SymbolRef &Symbol);
55
56 void printSymbols() override;
57 void printDynamicSymbols() override;
58 void printSymbol(const SymbolRef &Symbol);
59
60 void printRelocation(const RelocationRef &Reloc);
61
62 void printRelocation(const MachOObjectFile *Obj, const RelocationRef &Reloc);
63
64 void printSectionHeaders(const MachOObjectFile *Obj);
65
66 const MachOObjectFile *Obj;
67};
68
69} // namespace
70
71
72namespace llvm {
73
74std::unique_ptr<ObjDumper> createMachODumper(const object::MachOObjectFile &Obj,
75 ScopedPrinter &Writer) {
76 return std::make_unique<MachODumper>(&Obj, Writer);
77}
78
79} // namespace llvm
80
81const EnumEntry<uint32_t> MachOMagics[] = {
82 { "Magic", MachO::MH_MAGIC },
83 { "Cigam", MachO::MH_CIGAM },
84 { "Magic64", MachO::MH_MAGIC_64 },
85 { "Cigam64", MachO::MH_CIGAM_64 },
86 { "FatMagic", MachO::FAT_MAGIC },
87 { "FatCigam", MachO::FAT_CIGAM },
88};
89
90const EnumEntry<uint32_t> MachOHeaderFileTypes[] = {
91 { "Relocatable", MachO::MH_OBJECT },
92 { "Executable", MachO::MH_EXECUTE },
93 { "FixedVMLibrary", MachO::MH_FVMLIB },
94 { "Core", MachO::MH_CORE },
95 { "PreloadedExecutable", MachO::MH_PRELOAD },
96 { "DynamicLibrary", MachO::MH_DYLIB },
97 { "DynamicLinker", MachO::MH_DYLINKER },
98 { "Bundle", MachO::MH_BUNDLE },
99 { "DynamicLibraryStub", MachO::MH_DYLIB_STUB },
100 { "DWARFSymbol", MachO::MH_DSYM },
101 { "KextBundle", MachO::MH_KEXT_BUNDLE },
102};
103
104const EnumEntry<uint32_t> MachOHeaderCpuTypes[] = {
105 { "Any" , static_cast<uint32_t>(MachO::CPU_TYPE_ANY) },
106 { "X86" , MachO::CPU_TYPE_X86 },
107 { "X86-64" , MachO::CPU_TYPE_X86_64 },
108 { "Mc98000" , MachO::CPU_TYPE_MC98000 },
109 { "Arm" , MachO::CPU_TYPE_ARM },
110 { "Arm64" , MachO::CPU_TYPE_ARM64 },
111 { "Sparc" , MachO::CPU_TYPE_SPARC },
112 { "PowerPC" , MachO::CPU_TYPE_POWERPC },
113 { "PowerPC64" , MachO::CPU_TYPE_POWERPC64 },
114};
115
116const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX86[] = {
117 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_I386_ALL){ "CPU_SUBTYPE_I386_ALL", MachO::CPU_SUBTYPE_I386_ALL },
118 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_386){ "CPU_SUBTYPE_386", MachO::CPU_SUBTYPE_386 },
119 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486){ "CPU_SUBTYPE_486", MachO::CPU_SUBTYPE_486 },
120 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_486SX){ "CPU_SUBTYPE_486SX", MachO::CPU_SUBTYPE_486SX },
121 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_586){ "CPU_SUBTYPE_586", MachO::CPU_SUBTYPE_586 },
122 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTPRO){ "CPU_SUBTYPE_PENTPRO", MachO::CPU_SUBTYPE_PENTPRO },
123 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M3){ "CPU_SUBTYPE_PENTII_M3", MachO::CPU_SUBTYPE_PENTII_M3 },
124 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTII_M5){ "CPU_SUBTYPE_PENTII_M5", MachO::CPU_SUBTYPE_PENTII_M5 },
125 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON){ "CPU_SUBTYPE_CELERON", MachO::CPU_SUBTYPE_CELERON },
126 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_CELERON_MOBILE){ "CPU_SUBTYPE_CELERON_MOBILE", MachO::CPU_SUBTYPE_CELERON_MOBILE
}
,
127 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3){ "CPU_SUBTYPE_PENTIUM_3", MachO::CPU_SUBTYPE_PENTIUM_3 },
128 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_M){ "CPU_SUBTYPE_PENTIUM_3_M", MachO::CPU_SUBTYPE_PENTIUM_3_M },
129 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_3_XEON){ "CPU_SUBTYPE_PENTIUM_3_XEON", MachO::CPU_SUBTYPE_PENTIUM_3_XEON
}
,
130 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_M){ "CPU_SUBTYPE_PENTIUM_M", MachO::CPU_SUBTYPE_PENTIUM_M },
131 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4){ "CPU_SUBTYPE_PENTIUM_4", MachO::CPU_SUBTYPE_PENTIUM_4 },
132 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_PENTIUM_4_M){ "CPU_SUBTYPE_PENTIUM_4_M", MachO::CPU_SUBTYPE_PENTIUM_4_M },
133 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM){ "CPU_SUBTYPE_ITANIUM", MachO::CPU_SUBTYPE_ITANIUM },
134 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ITANIUM_2){ "CPU_SUBTYPE_ITANIUM_2", MachO::CPU_SUBTYPE_ITANIUM_2 },
135 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON){ "CPU_SUBTYPE_XEON", MachO::CPU_SUBTYPE_XEON },
136 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_XEON_MP){ "CPU_SUBTYPE_XEON_MP", MachO::CPU_SUBTYPE_XEON_MP },
137};
138
139const EnumEntry<uint32_t> MachOHeaderCpuSubtypesX64[] = {
140 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_ALL){ "CPU_SUBTYPE_X86_64_ALL", MachO::CPU_SUBTYPE_X86_64_ALL },
141 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_ARCH1){ "CPU_SUBTYPE_X86_ARCH1", MachO::CPU_SUBTYPE_X86_ARCH1 },
142 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_X86_64_H){ "CPU_SUBTYPE_X86_64_H", MachO::CPU_SUBTYPE_X86_64_H },
143};
144
145const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM[] = {
146 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_ALL){ "CPU_SUBTYPE_ARM_ALL", MachO::CPU_SUBTYPE_ARM_ALL },
147 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V4T){ "CPU_SUBTYPE_ARM_V4T", MachO::CPU_SUBTYPE_ARM_V4T },
148 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6){ "CPU_SUBTYPE_ARM_V6", MachO::CPU_SUBTYPE_ARM_V6 },
149 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5){ "CPU_SUBTYPE_ARM_V5", MachO::CPU_SUBTYPE_ARM_V5 },
150 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V5TEJ){ "CPU_SUBTYPE_ARM_V5TEJ", MachO::CPU_SUBTYPE_ARM_V5TEJ },
151 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_XSCALE){ "CPU_SUBTYPE_ARM_XSCALE", MachO::CPU_SUBTYPE_ARM_XSCALE },
152 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7){ "CPU_SUBTYPE_ARM_V7", MachO::CPU_SUBTYPE_ARM_V7 },
153 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7S){ "CPU_SUBTYPE_ARM_V7S", MachO::CPU_SUBTYPE_ARM_V7S },
154 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7K){ "CPU_SUBTYPE_ARM_V7K", MachO::CPU_SUBTYPE_ARM_V7K },
155 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V6M){ "CPU_SUBTYPE_ARM_V6M", MachO::CPU_SUBTYPE_ARM_V6M },
156 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7M){ "CPU_SUBTYPE_ARM_V7M", MachO::CPU_SUBTYPE_ARM_V7M },
157 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM_V7EM){ "CPU_SUBTYPE_ARM_V7EM", MachO::CPU_SUBTYPE_ARM_V7EM },
158};
159
160const EnumEntry<uint32_t> MachOHeaderCpuSubtypesARM64[] = {
161 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_ALL){ "CPU_SUBTYPE_ARM64_ALL", MachO::CPU_SUBTYPE_ARM64_ALL },
162 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64_V8){ "CPU_SUBTYPE_ARM64_V8", MachO::CPU_SUBTYPE_ARM64_V8 },
163 LLVM_READOBJ_ENUM_ENT(MachO, CPU_SUBTYPE_ARM64E){ "CPU_SUBTYPE_ARM64E", MachO::CPU_SUBTYPE_ARM64E },
164};
165
166const 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
170const 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
186const 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
215const EnumEntry<unsigned> MachOSectionTypes[] = {
216 { "Regular" , MachO::S_REGULAR },
217 { "ZeroFill" , MachO::S_ZEROFILL },
218 { "CStringLiterals" , MachO::S_CSTRING_LITERALS },
219 { "4ByteLiterals" , MachO::S_4BYTE_LITERALS },
220 { "8ByteLiterals" , MachO::S_8BYTE_LITERALS },
221 { "LiteralPointers" , MachO::S_LITERAL_POINTERS },
222 { "NonLazySymbolPointers" , MachO::S_NON_LAZY_SYMBOL_POINTERS },
223 { "LazySymbolPointers" , MachO::S_LAZY_SYMBOL_POINTERS },
224 { "SymbolStubs" , MachO::S_SYMBOL_STUBS },
225 { "ModInitFuncPointers" , MachO::S_MOD_INIT_FUNC_POINTERS },
226 { "ModTermFuncPointers" , MachO::S_MOD_TERM_FUNC_POINTERS },
227 { "Coalesced" , MachO::S_COALESCED },
228 { "GBZeroFill" , MachO::S_GB_ZEROFILL },
229 { "Interposing" , MachO::S_INTERPOSING },
230 { "16ByteLiterals" , MachO::S_16BYTE_LITERALS },
231 { "DTraceDOF" , MachO::S_DTRACE_DOF },
232 { "LazyDylibSymbolPointers" , MachO::S_LAZY_DYLIB_SYMBOL_POINTERS },
233 { "ThreadLocalRegular" , MachO::S_THREAD_LOCAL_REGULAR },
234 { "ThreadLocalZerofill" , MachO::S_THREAD_LOCAL_ZEROFILL },
235 { "ThreadLocalVariables" , MachO::S_THREAD_LOCAL_VARIABLES },
236 { "ThreadLocalVariablePointers" , MachO::S_THREAD_LOCAL_VARIABLE_POINTERS },
237 { "ThreadLocalInitFunctionPointers", MachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS }
238};
239
240const EnumEntry<unsigned> MachOSectionAttributes[] = {
241 { "LocReloc" , 1 << 0 /*S_ATTR_LOC_RELOC */ },
242 { "ExtReloc" , 1 << 1 /*S_ATTR_EXT_RELOC */ },
243 { "SomeInstructions" , 1 << 2 /*S_ATTR_SOME_INSTRUCTIONS */ },
244 { "Debug" , 1 << 17 /*S_ATTR_DEBUG */ },
245 { "SelfModifyingCode", 1 << 18 /*S_ATTR_SELF_MODIFYING_CODE*/ },
246 { "LiveSupport" , 1 << 19 /*S_ATTR_LIVE_SUPPORT */ },
247 { "NoDeadStrip" , 1 << 20 /*S_ATTR_NO_DEAD_STRIP */ },
248 { "StripStaticSyms" , 1 << 21 /*S_ATTR_STRIP_STATIC_SYMS */ },
249 { "NoTOC" , 1 << 22 /*S_ATTR_NO_TOC */ },
250 { "PureInstructions" , 1 << 23 /*S_ATTR_PURE_INSTRUCTIONS */ },
251};
252
253const EnumEntry<unsigned> MachOSymbolRefTypes[] = {
254 { "UndefinedNonLazy", 0 },
255 { "ReferenceFlagUndefinedLazy", 1 },
256 { "ReferenceFlagDefined", 2 },
257 { "ReferenceFlagPrivateDefined", 3 },
258 { "ReferenceFlagPrivateUndefinedNonLazy", 4 },
259 { "ReferenceFlagPrivateUndefinedLazy", 5 }
260};
261
262const EnumEntry<unsigned> MachOSymbolFlags[] = {
263 { "ThumbDef", 0x8 },
264 { "ReferencedDynamically", 0x10 },
265 { "NoDeadStrip", 0x20 },
266 { "WeakRef", 0x40 },
267 { "WeakDef", 0x80 },
268 { "SymbolResolver", 0x100 },
269 { "AltEntry", 0x200 },
270 { "ColdFunc", 0x400 },
271};
272
273const EnumEntry<unsigned> MachOSymbolTypes[] = {
274 { "Undef", 0x0 },
275 { "Abs", 0x2 },
276 { "Indirect", 0xA },
277 { "PreboundUndef", 0xC },
278 { "Section", 0xE }
279};
280
281namespace {
282 struct MachOSection {
283 ArrayRef<char> Name;
284 ArrayRef<char> SegmentName;
285 uint64_t Address;
286 uint64_t Size;
287 uint32_t Offset;
288 uint32_t Alignment;
289 uint32_t RelocationTableOffset;
290 uint32_t NumRelocationTableEntries;
291 uint32_t Flags;
292 uint32_t Reserved1;
293 uint32_t Reserved2;
294 uint32_t Reserved3;
295 };
296
297 struct MachOSegment {
298 std::string CmdName;
299 std::string SegName;
300 uint64_t cmdsize;
301 uint64_t vmaddr;
302 uint64_t vmsize;
303 uint64_t fileoff;
304 uint64_t filesize;
305 uint32_t maxprot;
306 uint32_t initprot;
307 uint32_t nsects;
308 uint32_t flags;
309 };
310
311 struct MachOSymbol {
312 uint32_t StringIndex;
313 uint8_t Type;
314 uint8_t SectionIndex;
315 uint16_t Flags;
316 uint64_t Value;
317 };
318}
319
320static std::string getMask(uint32_t prot)
321{
322 // TODO (davide): This always assumes prot is valid.
323 // Catch mistakes and report if needed.
324 std::string Prot;
325 Prot = "";
326 Prot += (prot & MachO::VM_PROT_READ) ? "r" : "-";
327 Prot += (prot & MachO::VM_PROT_WRITE) ? "w" : "-";
328 Prot += (prot & MachO::VM_PROT_EXECUTE) ? "x" : "-";
329 return Prot;
330}
331
332static void getSection(const MachOObjectFile *Obj,
333 DataRefImpl Sec,
334 MachOSection &Section) {
335 if (!Obj->is64Bit()) {
336 MachO::section Sect = Obj->getSection(Sec);
337 Section.Address = Sect.addr;
338 Section.Size = Sect.size;
339 Section.Offset = Sect.offset;
340 Section.Alignment = Sect.align;
341 Section.RelocationTableOffset = Sect.reloff;
342 Section.NumRelocationTableEntries = Sect.nreloc;
343 Section.Flags = Sect.flags;
344 Section.Reserved1 = Sect.reserved1;
345 Section.Reserved2 = Sect.reserved2;
346 return;
347 }
348 MachO::section_64 Sect = Obj->getSection64(Sec);
349 Section.Address = Sect.addr;
350 Section.Size = Sect.size;
351 Section.Offset = Sect.offset;
352 Section.Alignment = Sect.align;
353 Section.RelocationTableOffset = Sect.reloff;
354 Section.NumRelocationTableEntries = Sect.nreloc;
355 Section.Flags = Sect.flags;
356 Section.Reserved1 = Sect.reserved1;
357 Section.Reserved2 = Sect.reserved2;
358 Section.Reserved3 = Sect.reserved3;
359}
360
361static void getSegment(const MachOObjectFile *Obj,
362 const MachOObjectFile::LoadCommandInfo &L,
363 MachOSegment &Segment) {
364 if (!Obj->is64Bit()) {
365 MachO::segment_command SC = Obj->getSegmentLoadCommand(L);
366 Segment.CmdName = "LC_SEGMENT";
367 Segment.SegName = SC.segname;
368 Segment.cmdsize = SC.cmdsize;
369 Segment.vmaddr = SC.vmaddr;
370 Segment.vmsize = SC.vmsize;
371 Segment.fileoff = SC.fileoff;
372 Segment.filesize = SC.filesize;
373 Segment.maxprot = SC.maxprot;
374 Segment.initprot = SC.initprot;
375 Segment.nsects = SC.nsects;
376 Segment.flags = SC.flags;
377 return;
378 }
379 MachO::segment_command_64 SC = Obj->getSegment64LoadCommand(L);
380 Segment.CmdName = "LC_SEGMENT_64";
381 Segment.SegName = SC.segname;
382 Segment.cmdsize = SC.cmdsize;
383 Segment.vmaddr = SC.vmaddr;
384 Segment.vmsize = SC.vmsize;
385 Segment.fileoff = SC.fileoff;
386 Segment.filesize = SC.filesize;
387 Segment.maxprot = SC.maxprot;
388 Segment.initprot = SC.initprot;
389 Segment.nsects = SC.nsects;
390 Segment.flags = SC.flags;
391}
392
393static void getSymbol(const MachOObjectFile *Obj,
394 DataRefImpl DRI,
395 MachOSymbol &Symbol) {
396 if (!Obj->is64Bit()) {
397 MachO::nlist Entry = Obj->getSymbolTableEntry(DRI);
398 Symbol.StringIndex = Entry.n_strx;
399 Symbol.Type = Entry.n_type;
400 Symbol.SectionIndex = Entry.n_sect;
401 Symbol.Flags = Entry.n_desc;
402 Symbol.Value = Entry.n_value;
403 return;
404 }
405 MachO::nlist_64 Entry = Obj->getSymbol64TableEntry(DRI);
406 Symbol.StringIndex = Entry.n_strx;
407 Symbol.Type = Entry.n_type;
408 Symbol.SectionIndex = Entry.n_sect;
409 Symbol.Flags = Entry.n_desc;
410 Symbol.Value = Entry.n_value;
411}
412
413void MachODumper::printFileHeaders() {
414 DictScope H(W, "MachHeader");
415 if (!Obj->is64Bit()) {
416 printFileHeaders(Obj->getHeader());
417 } else {
418 printFileHeaders(Obj->getHeader64());
419 W.printHex("Reserved", Obj->getHeader64().reserved);
420 }
421}
422
423template<class MachHeader>
424void MachODumper::printFileHeaders(const MachHeader &Header) {
425 W.printEnum("Magic", Header.magic, makeArrayRef(MachOMagics));
426 W.printEnum("CpuType", Header.cputype, makeArrayRef(MachOHeaderCpuTypes));
427 uint32_t subtype = Header.cpusubtype & ~MachO::CPU_SUBTYPE_MASK;
428 switch (Header.cputype) {
429 case MachO::CPU_TYPE_X86:
430 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX86));
431 break;
432 case MachO::CPU_TYPE_X86_64:
433 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesX64));
434 break;
435 case MachO::CPU_TYPE_ARM:
436 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM));
437 break;
438 case MachO::CPU_TYPE_POWERPC:
439 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesPPC));
440 break;
441 case MachO::CPU_TYPE_SPARC:
442 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesSPARC));
443 break;
444 case MachO::CPU_TYPE_ARM64:
445 W.printEnum("CpuSubType", subtype, makeArrayRef(MachOHeaderCpuSubtypesARM64));
446 break;
447 case MachO::CPU_TYPE_POWERPC64:
448 default:
449 W.printHex("CpuSubtype", subtype);
450 }
451 W.printEnum("FileType", Header.filetype, makeArrayRef(MachOHeaderFileTypes));
452 W.printNumber("NumOfLoadCommands", Header.ncmds);
453 W.printNumber("SizeOfLoadCommands", Header.sizeofcmds);
454 W.printFlags("Flags", Header.flags, makeArrayRef(MachOHeaderFlags));
455}
456
457void MachODumper::printSectionHeaders() { return printSectionHeaders(Obj); }
458
459void MachODumper::printSectionHeaders(const MachOObjectFile *Obj) {
460 ListScope Group(W, "Sections");
461
462 int SectionIndex = -1;
463 for (const SectionRef &Section : Obj->sections()) {
464 ++SectionIndex;
465
466 MachOSection MOSection;
467 getSection(Obj, Section.getRawDataRefImpl(), MOSection);
468 DataRefImpl DR = Section.getRawDataRefImpl();
469 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName());
470 ArrayRef<char> RawName = Obj->getSectionRawName(DR);
471 StringRef SegmentName = Obj->getSectionFinalSegmentName(DR);
472 ArrayRef<char> RawSegmentName = Obj->getSectionRawFinalSegmentName(DR);
473
474 DictScope SectionD(W, "Section");
475 W.printNumber("Index", SectionIndex);
476 W.printBinary("Name", Name, RawName);
477 W.printBinary("Segment", SegmentName, RawSegmentName);
478 W.printHex("Address", MOSection.Address);
479 W.printHex("Size", MOSection.Size);
480 W.printNumber("Offset", MOSection.Offset);
481 W.printNumber("Alignment", MOSection.Alignment);
482 W.printHex("RelocationOffset", MOSection.RelocationTableOffset);
483 W.printNumber("RelocationCount", MOSection.NumRelocationTableEntries);
484 W.printEnum("Type", MOSection.Flags & 0xFF,
485 makeArrayRef(MachOSectionTypes));
486 W.printFlags("Attributes", MOSection.Flags >> 8,
487 makeArrayRef(MachOSectionAttributes));
488 W.printHex("Reserved1", MOSection.Reserved1);
489 W.printHex("Reserved2", MOSection.Reserved2);
490 if (Obj->is64Bit())
491 W.printHex("Reserved3", MOSection.Reserved3);
492
493 if (opts::SectionRelocations) {
494 ListScope D(W, "Relocations");
495 for (const RelocationRef &Reloc : Section.relocations())
496 printRelocation(Reloc);
497 }
498
499 if (opts::SectionSymbols) {
500 ListScope D(W, "Symbols");
501 for (const SymbolRef &Symbol : Obj->symbols()) {
502 if (!Section.containsSymbol(Symbol))
503 continue;
504
505 printSymbol(Symbol);
506 }
507 }
508
509 if (opts::SectionData && !Section.isBSS())
510 W.printBinaryBlock("SectionData", unwrapOrError(Obj->getFileName(),
511 Section.getContents()));
512 }
513}
514
515void MachODumper::printRelocations() {
516 ListScope D(W, "Relocations");
517
518 std::error_code EC;
519 for (const SectionRef &Section : Obj->sections()) {
520 StringRef Name = unwrapOrError(Obj->getFileName(), Section.getName());
521 bool PrintedGroup = false;
522 for (const RelocationRef &Reloc : Section.relocations()) {
523 if (!PrintedGroup) {
524 W.startLine() << "Section " << Name << " {\n";
525 W.indent();
526 PrintedGroup = true;
527 }
528
529 printRelocation(Reloc);
530 }
531
532 if (PrintedGroup) {
533 W.unindent();
534 W.startLine() << "}\n";
535 }
536 }
537}
538
539void MachODumper::printRelocation(const RelocationRef &Reloc) {
540 return printRelocation(Obj, Reloc);
541}
542
543void MachODumper::printRelocation(const MachOObjectFile *Obj,
544 const RelocationRef &Reloc) {
545 uint64_t Offset = Reloc.getOffset();
546 SmallString<32> RelocName;
547 Reloc.getTypeName(RelocName);
548
549 DataRefImpl DR = Reloc.getRawDataRefImpl();
550 MachO::any_relocation_info RE = Obj->getRelocation(DR);
551 bool IsScattered = Obj->isRelocationScattered(RE);
552 bool IsExtern = !IsScattered && Obj->getPlainRelocationExternal(RE);
553
554 StringRef TargetName;
555 if (IsExtern) {
556 symbol_iterator Symbol = Reloc.getSymbol();
557 if (Symbol != Obj->symbol_end()) {
558 TargetName = getSymbolName(*Symbol);
559 }
560 } else if (!IsScattered) {
561 section_iterator SecI = Obj->getRelocationSection(DR);
562 if (SecI != Obj->section_end())
563 TargetName = unwrapOrError(Obj->getFileName(), SecI->getName());
564 }
565 if (TargetName.empty())
566 TargetName = "-";
567
568 if (opts::ExpandRelocs) {
569 DictScope Group(W, "Relocation");
570 W.printHex("Offset", Offset);
571 W.printNumber("PCRel", Obj->getAnyRelocationPCRel(RE));
572 W.printNumber("Length", Obj->getAnyRelocationLength(RE));
573 W.printNumber("Type", RelocName, Obj->getAnyRelocationType(RE));
574 if (IsScattered) {
575 W.printHex("Value", Obj->getScatteredRelocationValue(RE));
576 } else {
577 const char *Kind = IsExtern ? "Symbol" : "Section";
578 W.printNumber(Kind, TargetName, Obj->getPlainRelocationSymbolNum(RE));
579 }
580 } else {
581 SmallString<32> SymbolNameOrOffset("0x");
582 if (IsScattered) {
583 // Scattered relocations don't really have an associated symbol for some
584 // reason, even if one exists in the symtab at the correct address.
585 SymbolNameOrOffset += utohexstr(Obj->getScatteredRelocationValue(RE));
586 } else {
587 SymbolNameOrOffset = TargetName;
588 }
589
590 raw_ostream& OS = W.startLine();
591 OS << W.hex(Offset)
592 << " " << Obj->getAnyRelocationPCRel(RE)
593 << " " << Obj->getAnyRelocationLength(RE);
594 if (IsScattered)
595 OS << " n/a";
596 else
597 OS << " " << Obj->getPlainRelocationExternal(RE);
598 OS << " " << RelocName
599 << " " << IsScattered
600 << " " << SymbolNameOrOffset
601 << "\n";
602 }
603}
604
605StringRef MachODumper::getSymbolName(const SymbolRef &Symbol) {
606 Expected<StringRef> SymbolNameOrErr = Symbol.getName();
607 if (!SymbolNameOrErr) {
608 reportError(SymbolNameOrErr.takeError(), Obj->getFileName());
609 }
610 return *SymbolNameOrErr;
611}
612
613void MachODumper::printSymbols() {
614 ListScope Group(W, "Symbols");
615
616 for (const SymbolRef &Symbol : Obj->symbols()) {
617 printSymbol(Symbol);
618 }
619}
620
621void MachODumper::printDynamicSymbols() {
622 ListScope Group(W, "DynamicSymbols");
623}
624
625void MachODumper::printSymbol(const SymbolRef &Symbol) {
626 StringRef SymbolName = getSymbolName(Symbol);
627
628 MachOSymbol MOSymbol;
629 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
630
631 StringRef SectionName = "";
632 // Don't ask a Mach-O STABS symbol for its section unless we know that
633 // STAB symbol's section field refers to a valid section index. Otherwise
634 // the symbol may error trying to load a section that does not exist.
635 // TODO: Add a whitelist of STABS symbol types that contain valid section
636 // indices.
637 if (!(MOSymbol.Type & MachO::N_STAB)) {
638 Expected<section_iterator> SecIOrErr = Symbol.getSection();
639 if (!SecIOrErr)
640 reportError(SecIOrErr.takeError(), Obj->getFileName());
641
642 section_iterator SecI = *SecIOrErr;
643 if (SecI != Obj->section_end())
644 SectionName = unwrapOrError(Obj->getFileName(), SecI->getName());
645 }
646
647 DictScope D(W, "Symbol");
648 W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
649 if (MOSymbol.Type & MachO::N_STAB) {
650 W.printHex("Type", "SymDebugTable", MOSymbol.Type);
651 } else {
652 if (MOSymbol.Type & MachO::N_PEXT)
653 W.startLine() << "PrivateExtern\n";
654 if (MOSymbol.Type & MachO::N_EXT)
655 W.startLine() << "Extern\n";
656 W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
657 makeArrayRef(MachOSymbolTypes));
658 }
659 W.printHex("Section", SectionName, MOSymbol.SectionIndex);
660 W.printEnum("RefType", static_cast<uint16_t>(MOSymbol.Flags & 0x7),
661 makeArrayRef(MachOSymbolRefTypes));
662 W.printFlags("Flags", static_cast<uint16_t>(MOSymbol.Flags & ~0x7),
663 makeArrayRef(MachOSymbolFlags));
664 W.printHex("Value", MOSymbol.Value);
665}
666
667void MachODumper::printUnwindInfo() {
668 W.startLine() << "UnwindInfo not implemented.\n";
669}
670
671void MachODumper::printStackMap() const {
672 object::SectionRef StackMapSection;
673 for (auto Sec : Obj->sections()) {
674 StringRef Name;
675 if (Expected<StringRef> NameOrErr = Sec.getName())
676 Name = *NameOrErr;
677 else
678 consumeError(NameOrErr.takeError());
679
680 if (Name == "__llvm_stackmaps") {
681 StackMapSection = Sec;
682 break;
683 }
684 }
685
686 if (StackMapSection == object::SectionRef())
687 return;
688
689 StringRef StackMapContents =
690 unwrapOrError(Obj->getFileName(), StackMapSection.getContents());
691 ArrayRef<uint8_t> StackMapContentsArray =
692 arrayRefFromStringRef(StackMapContents);
693
694 if (Obj->isLittleEndian())
695 prettyPrintStackMap(
696 W, StackMapParser<support::little>(StackMapContentsArray));
697 else
698 prettyPrintStackMap(
699 W, StackMapParser<support::big>(StackMapContentsArray));
700}
701
702void MachODumper::printCGProfile() {
703 object::SectionRef CGProfileSection;
1
Calling defaulted default constructor for 'SectionRef'
3
Returning from default constructor for 'SectionRef'
704 for (auto Sec : Obj->sections()) {
705 StringRef Name;
706 if (Expected<StringRef> NameOrErr = Sec.getName())
707 Name = *NameOrErr;
708 else
709 consumeError(NameOrErr.takeError());
710
711 if (Name == "__cg_profile") {
712 CGProfileSection = Sec;
713 break;
714 }
715 }
716 if (CGProfileSection == object::SectionRef())
4
Taking false branch
717 return;
718
719 StringRef CGProfileContents =
720 unwrapOrError(Obj->getFileName(), CGProfileSection.getContents());
5
Calling 'SectionRef::getContents'
721 BinaryStreamReader Reader(CGProfileContents, Obj->isLittleEndian()
722 ? llvm::support::little
723 : llvm::support::big);
724
725 ListScope L(W, "CGProfile");
726 while (!Reader.empty()) {
727 uint32_t FromIndex, ToIndex;
728 uint64_t Count;
729 if (Error Err = Reader.readInteger(FromIndex))
730 reportError(std::move(Err), Obj->getFileName());
731 if (Error Err = Reader.readInteger(ToIndex))
732 reportError(std::move(Err), Obj->getFileName());
733 if (Error Err = Reader.readInteger(Count))
734 reportError(std::move(Err), Obj->getFileName());
735 DictScope D(W, "CGProfileEntry");
736 W.printNumber("From", getSymbolName(*Obj->getSymbolByIndex(FromIndex)),
737 FromIndex);
738 W.printNumber("To", getSymbolName(*Obj->getSymbolByIndex(ToIndex)),
739 ToIndex);
740 W.printNumber("Weight", Count);
741 }
742}
743
744void MachODumper::printNeededLibraries() {
745 ListScope D(W, "NeededLibraries");
746
747 using LibsTy = std::vector<StringRef>;
748 LibsTy Libs;
749
750 for (const auto &Command : Obj->load_commands()) {
751 if (Command.C.cmd == MachO::LC_LOAD_DYLIB ||
752 Command.C.cmd == MachO::LC_ID_DYLIB ||
753 Command.C.cmd == MachO::LC_LOAD_WEAK_DYLIB ||
754 Command.C.cmd == MachO::LC_REEXPORT_DYLIB ||
755 Command.C.cmd == MachO::LC_LAZY_LOAD_DYLIB ||
756 Command.C.cmd == MachO::LC_LOAD_UPWARD_DYLIB) {
757 MachO::dylib_command Dl = Obj->getDylibIDLoadCommand(Command);
758 if (Dl.dylib.name < Dl.cmdsize) {
759 auto *P = static_cast<const char*>(Command.Ptr) + Dl.dylib.name;
760 Libs.push_back(P);
761 }
762 }
763 }
764
765 llvm::stable_sort(Libs);
766
767 for (const auto &L : Libs) {
768 W.startLine() << L << "\n";
769 }
770}
771
772void MachODumper::printMachODataInCode() {
773 for (const auto &Load : Obj->load_commands()) {
774 if (Load.C.cmd == MachO::LC_DATA_IN_CODE) {
775 MachO::linkedit_data_command LLC = Obj->getLinkeditDataLoadCommand(Load);
776 DictScope Group(W, "DataInCode");
777 W.printNumber("Data offset", LLC.dataoff);
778 W.printNumber("Data size", LLC.datasize);
779 ListScope D(W, "Data entries");
780 unsigned NumRegions = LLC.datasize / sizeof(MachO::data_in_code_entry);
781 for (unsigned i = 0; i < NumRegions; ++i) {
782 MachO::data_in_code_entry DICE = Obj->getDataInCodeTableEntry(
783 LLC.dataoff, i);
784 DictScope Group(W, "Entry");
785 W.printNumber("Index", i);
786 W.printNumber("Offset", DICE.offset);
787 W.printNumber("Length", DICE.length);
788 W.printNumber("Kind", DICE.kind);
789 }
790 }
791 }
792}
793
794void MachODumper::printMachOVersionMin() {
795 for (const auto &Load : Obj->load_commands()) {
796 StringRef Cmd;
797 switch (Load.C.cmd) {
798 case MachO::LC_VERSION_MIN_MACOSX:
799 Cmd = "LC_VERSION_MIN_MACOSX";
800 break;
801 case MachO::LC_VERSION_MIN_IPHONEOS:
802 Cmd = "LC_VERSION_MIN_IPHONEOS";
803 break;
804 case MachO::LC_VERSION_MIN_TVOS:
805 Cmd = "LC_VERSION_MIN_TVOS";
806 break;
807 case MachO::LC_VERSION_MIN_WATCHOS:
808 Cmd = "LC_VERSION_MIN_WATCHOS";
809 break;
810 case MachO::LC_BUILD_VERSION:
811 Cmd = "LC_BUILD_VERSION";
812 break;
813 default:
814 continue;
815 }
816
817 DictScope Group(W, "MinVersion");
818 // Handle LC_BUILD_VERSION.
819 if (Load.C.cmd == MachO::LC_BUILD_VERSION) {
820 MachO::build_version_command BVC = Obj->getBuildVersionLoadCommand(Load);
821 W.printString("Cmd", Cmd);
822 W.printNumber("Size", BVC.cmdsize);
823 W.printString("Platform",
824 MachOObjectFile::getBuildPlatform(BVC.platform));
825 W.printString("Version", MachOObjectFile::getVersionString(BVC.minos));
826 if (BVC.sdk)
827 W.printString("SDK", MachOObjectFile::getVersionString(BVC.sdk));
828 else
829 W.printString("SDK", StringRef("n/a"));
830 continue;
831 }
832
833 MachO::version_min_command VMC = Obj->getVersionMinLoadCommand(Load);
834 W.printString("Cmd", Cmd);
835 W.printNumber("Size", VMC.cmdsize);
836 SmallString<32> Version;
837 Version = utostr(MachOObjectFile::getVersionMinMajor(VMC, false)) + "." +
838 utostr(MachOObjectFile::getVersionMinMinor(VMC, false));
839 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, false);
840 if (Update != 0)
841 Version += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, false));
842 W.printString("Version", Version);
843 SmallString<32> SDK;
844 if (VMC.sdk == 0)
845 SDK = "n/a";
846 else {
847 SDK = utostr(MachOObjectFile::getVersionMinMajor(VMC, true)) + "." +
848 utostr(MachOObjectFile::getVersionMinMinor(VMC, true));
849 uint32_t Update = MachOObjectFile::getVersionMinUpdate(VMC, true);
850 if (Update != 0)
851 SDK += "." + utostr(MachOObjectFile::getVersionMinUpdate(VMC, true));
852 }
853 W.printString("SDK", SDK);
854 }
855}
856
857void MachODumper::printMachODysymtab() {
858 for (const auto &Load : Obj->load_commands()) {
859 if (Load.C.cmd == MachO::LC_DYSYMTAB) {
860 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
861 DictScope Group(W, "Dysymtab");
862 W.printNumber("ilocalsym", DLC.ilocalsym);
863 W.printNumber("nlocalsym", DLC.nlocalsym);
864 W.printNumber("iextdefsym", DLC.iextdefsym);
865 W.printNumber("nextdefsym", DLC.nextdefsym);
866 W.printNumber("iundefsym", DLC.iundefsym);
867 W.printNumber("nundefsym", DLC.nundefsym);
868 W.printNumber("tocoff", DLC.tocoff);
869 W.printNumber("ntoc", DLC.ntoc);
870 W.printNumber("modtaboff", DLC.modtaboff);
871 W.printNumber("nmodtab", DLC.nmodtab);
872 W.printNumber("extrefsymoff", DLC.extrefsymoff);
873 W.printNumber("nextrefsyms", DLC.nextrefsyms);
874 W.printNumber("indirectsymoff", DLC.indirectsymoff);
875 W.printNumber("nindirectsyms", DLC.nindirectsyms);
876 W.printNumber("extreloff", DLC.extreloff);
877 W.printNumber("nextrel", DLC.nextrel);
878 W.printNumber("locreloff", DLC.locreloff);
879 W.printNumber("nlocrel", DLC.nlocrel);
880 }
881 }
882}
883
884void MachODumper::printMachOSegment() {
885 for (const auto &Load : Obj->load_commands()) {
886 if (Load.C.cmd == MachO::LC_SEGMENT || Load.C.cmd == MachO::LC_SEGMENT_64) {
887 MachOSegment MOSegment;
888 getSegment(Obj, Load, MOSegment);
889 DictScope Group(W, "Segment");
890 W.printString("Cmd", MOSegment.CmdName);
891 W.printString("Name", MOSegment.SegName);
892 W.printNumber("Size", MOSegment.cmdsize);
893 W.printHex("vmaddr", MOSegment.vmaddr);
894 W.printHex("vmsize", MOSegment.vmsize);
895 W.printNumber("fileoff", MOSegment.fileoff);
896 W.printNumber("filesize", MOSegment.filesize);
897 W.printString("maxprot", getMask(MOSegment.maxprot));
898 W.printString("initprot", getMask(MOSegment.initprot));
899 W.printNumber("nsects", MOSegment.nsects);
900 W.printHex("flags", MOSegment.flags);
901 }
902 }
903}
904
905void MachODumper::printMachOIndirectSymbols() {
906 for (const auto &Load : Obj->load_commands()) {
907 if (Load.C.cmd == MachO::LC_DYSYMTAB) {
908 MachO::dysymtab_command DLC = Obj->getDysymtabLoadCommand();
909 DictScope Group(W, "Indirect Symbols");
910 W.printNumber("Number", DLC.nindirectsyms);
911 ListScope D(W, "Symbols");
912 for (unsigned i = 0; i < DLC.nindirectsyms; ++i) {
913 DictScope Group(W, "Entry");
914 W.printNumber("Entry Index", i);
915 W.printHex("Symbol Index", Obj->getIndirectSymbolTableEntry(DLC, i));
916 }
917 }
918 }
919}
920
921void MachODumper::printMachOLinkerOptions() {
922 for (const auto &Load : Obj->load_commands()) {
923 if (Load.C.cmd == MachO::LC_LINKER_OPTION) {
924 MachO::linker_option_command LOLC = Obj->getLinkerOptionLoadCommand(Load);
925 DictScope Group(W, "Linker Options");
926 W.printNumber("Size", LOLC.cmdsize);
927 ListScope D(W, "Strings");
928 uint64_t DataSize = LOLC.cmdsize - sizeof(MachO::linker_option_command);
929 const char *P = Load.Ptr + sizeof(MachO::linker_option_command);
930 StringRef Data(P, DataSize);
931 for (unsigned i = 0; i < LOLC.count; ++i) {
932 std::pair<StringRef,StringRef> Split = Data.split('\0');
933 W.printString("Value", Split.first);
934 Data = Split.second;
935 }
936 }
937 }
938}

/build/llvm-toolchain-snapshot-14~++20220125101009+ceec4383681c/llvm/include/llvm/Object/ObjectFile.h

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