clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name MachObjectWriter.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 -mrelocation-model pic -pic-level 1 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -resource-dir /usr/local/lib/clang/13.0.0 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/AMDGPU -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Analysis -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ASMParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/BinaryFormat -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitcode -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Bitstream -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /include/llvm/CodeGen -I /include/llvm/CodeGen/PBQP -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IR -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Coroutines -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData/Coverage -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/CodeView -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/DWARF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/MSF -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/PDB -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Demangle -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/JITLink -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ExecutionEngine/Orc -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenACC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Frontend/OpenMP -I /include/llvm/CodeGen/GlobalISel -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/IRReader -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/Transforms/InstCombine -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/LTO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Linker -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/MC/MCParser -I /include/llvm/CodeGen/MIRParser -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Object -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Option -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Passes -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ProfileData -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Scalar -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/ADT -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Support -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/DebugInfo/Symbolize -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Target -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Utils -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/Vectorize -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include/llvm/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/Target/X86 -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include/llvm/Transforms/IPO -I /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/include -I /usr/src/gnu/usr.bin/clang/libLLVM/../include -I /usr/src/gnu/usr.bin/clang/libLLVM/obj -I /usr/src/gnu/usr.bin/clang/libLLVM/obj/../include -D NDEBUG -D __STDC_LIMIT_MACROS -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D LLVM_PREFIX="/usr" -D PIC -internal-isystem /usr/include/c++/v1 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/usr/src/gnu/usr.bin/clang/libLLVM/obj -ferror-limit 19 -fvisibility-inlines-hidden -fwrapv -D_RET_PROTECTOR -ret-protector -fno-rtti -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c++ /usr/src/gnu/usr.bin/clang/libLLVM/../../../llvm/llvm/lib/MC/MachObjectWriter.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | #include "llvm/ADT/DenseMap.h" |
10 | #include "llvm/ADT/Twine.h" |
11 | #include "llvm/ADT/iterator_range.h" |
12 | #include "llvm/BinaryFormat/MachO.h" |
13 | #include "llvm/MC/MCAsmBackend.h" |
14 | #include "llvm/MC/MCAsmLayout.h" |
15 | #include "llvm/MC/MCAssembler.h" |
16 | #include "llvm/MC/MCContext.h" |
17 | #include "llvm/MC/MCDirectives.h" |
18 | #include "llvm/MC/MCExpr.h" |
19 | #include "llvm/MC/MCFixupKindInfo.h" |
20 | #include "llvm/MC/MCFragment.h" |
21 | #include "llvm/MC/MCMachObjectWriter.h" |
22 | #include "llvm/MC/MCObjectWriter.h" |
23 | #include "llvm/MC/MCSection.h" |
24 | #include "llvm/MC/MCSectionMachO.h" |
25 | #include "llvm/MC/MCSymbol.h" |
26 | #include "llvm/MC/MCSymbolMachO.h" |
27 | #include "llvm/MC/MCValue.h" |
28 | #include "llvm/Support/Alignment.h" |
29 | #include "llvm/Support/Casting.h" |
30 | #include "llvm/Support/Debug.h" |
31 | #include "llvm/Support/ErrorHandling.h" |
32 | #include "llvm/Support/MathExtras.h" |
33 | #include "llvm/Support/raw_ostream.h" |
34 | #include <algorithm> |
35 | #include <cassert> |
36 | #include <cstdint> |
37 | #include <string> |
38 | #include <utility> |
39 | #include <vector> |
40 | |
41 | using namespace llvm; |
42 | |
43 | #define DEBUG_TYPE "mc" |
44 | |
45 | void MachObjectWriter::reset() { |
46 | Relocations.clear(); |
47 | IndirectSymBase.clear(); |
48 | StringTable.clear(); |
49 | LocalSymbolData.clear(); |
50 | ExternalSymbolData.clear(); |
51 | UndefinedSymbolData.clear(); |
52 | MCObjectWriter::reset(); |
53 | } |
54 | |
55 | bool MachObjectWriter::doesSymbolRequireExternRelocation(const MCSymbol &S) { |
56 | |
57 | if (S.isUndefined()) |
58 | return true; |
59 | |
60 | |
61 | |
62 | if (cast<MCSymbolMachO>(S).isWeakDefinition()) |
63 | return true; |
64 | |
65 | |
66 | return false; |
67 | } |
68 | |
69 | bool MachObjectWriter:: |
70 | MachSymbolData::operator<(const MachSymbolData &RHS) const { |
71 | return Symbol->getName() < RHS.Symbol->getName(); |
72 | } |
73 | |
74 | bool MachObjectWriter::isFixupKindPCRel(const MCAssembler &Asm, unsigned Kind) { |
75 | const MCFixupKindInfo &FKI = Asm.getBackend().getFixupKindInfo( |
76 | (MCFixupKind) Kind); |
77 | |
78 | return FKI.Flags & MCFixupKindInfo::FKF_IsPCRel; |
79 | } |
80 | |
81 | uint64_t MachObjectWriter::getFragmentAddress(const MCFragment *Fragment, |
82 | const MCAsmLayout &Layout) const { |
83 | return getSectionAddress(Fragment->getParent()) + |
84 | Layout.getFragmentOffset(Fragment); |
85 | } |
86 | |
87 | uint64_t MachObjectWriter::getSymbolAddress(const MCSymbol &S, |
88 | const MCAsmLayout &Layout) const { |
89 | |
90 | if (S.isVariable()) { |
91 | if (const MCConstantExpr *C = |
92 | dyn_cast<const MCConstantExpr>(S.getVariableValue())) |
93 | return C->getValue(); |
94 | |
95 | MCValue Target; |
96 | if (!S.getVariableValue()->evaluateAsRelocatable(Target, &Layout, nullptr)) |
97 | report_fatal_error("unable to evaluate offset for variable '" + |
98 | S.getName() + "'"); |
99 | |
100 | |
101 | if (Target.getSymA() && Target.getSymA()->getSymbol().isUndefined()) |
102 | report_fatal_error("unable to evaluate offset to undefined symbol '" + |
103 | Target.getSymA()->getSymbol().getName() + "'"); |
104 | if (Target.getSymB() && Target.getSymB()->getSymbol().isUndefined()) |
105 | report_fatal_error("unable to evaluate offset to undefined symbol '" + |
106 | Target.getSymB()->getSymbol().getName() + "'"); |
107 | |
108 | uint64_t Address = Target.getConstant(); |
109 | if (Target.getSymA()) |
110 | Address += getSymbolAddress(Target.getSymA()->getSymbol(), Layout); |
111 | if (Target.getSymB()) |
112 | Address += getSymbolAddress(Target.getSymB()->getSymbol(), Layout); |
113 | return Address; |
114 | } |
115 | |
116 | return getSectionAddress(S.getFragment()->getParent()) + |
117 | Layout.getSymbolOffset(S); |
118 | } |
119 | |
120 | uint64_t MachObjectWriter::getPaddingSize(const MCSection *Sec, |
121 | const MCAsmLayout &Layout) const { |
122 | uint64_t EndAddr = getSectionAddress(Sec) + Layout.getSectionAddressSize(Sec); |
123 | unsigned Next = Sec->getLayoutOrder() + 1; |
124 | if (Next >= Layout.getSectionOrder().size()) |
| 4 | | Assuming the condition is false | |
|
| |
125 | return 0; |
126 | |
127 | const MCSection &NextSec = *Layout.getSectionOrder()[Next]; |
128 | if (NextSec.isVirtualSection()) |
| 6 | | Assuming the condition is false | |
|
| |
129 | return 0; |
130 | return offsetToAlignment(EndAddr, Align(NextSec.getAlignment())); |
| 8 | | Calling 'offsetToAlignment' | |
|
131 | } |
132 | |
133 | void MachObjectWriter::writeHeader(MachO::HeaderFileType Type, |
134 | unsigned NumLoadCommands, |
135 | unsigned LoadCommandsSize, |
136 | bool SubsectionsViaSymbols) { |
137 | uint32_t Flags = 0; |
138 | |
139 | if (SubsectionsViaSymbols) |
140 | Flags |= MachO::MH_SUBSECTIONS_VIA_SYMBOLS; |
141 | |
142 | |
143 | |
144 | |
145 | uint64_t Start = W.OS.tell(); |
146 | (void) Start; |
147 | |
148 | W.write<uint32_t>(is64Bit() ? MachO::MH_MAGIC_64 : MachO::MH_MAGIC); |
149 | |
150 | W.write<uint32_t>(TargetObjectWriter->getCPUType()); |
151 | W.write<uint32_t>(TargetObjectWriter->getCPUSubtype()); |
152 | |
153 | W.write<uint32_t>(Type); |
154 | W.write<uint32_t>(NumLoadCommands); |
155 | W.write<uint32_t>(LoadCommandsSize); |
156 | W.write<uint32_t>(Flags); |
157 | if (is64Bit()) |
158 | W.write<uint32_t>(0); |
159 | |
160 | assert(W.OS.tell() - Start == (is64Bit() ? sizeof(MachO::mach_header_64) |
161 | : sizeof(MachO::mach_header))); |
162 | } |
163 | |
164 | void MachObjectWriter::writeWithPadding(StringRef Str, uint64_t Size) { |
165 | assert(Size >= Str.size()); |
166 | W.OS << Str; |
167 | W.OS.write_zeros(Size - Str.size()); |
168 | } |
169 | |
170 | |
171 | |
172 | |
173 | |
174 | void MachObjectWriter::writeSegmentLoadCommand( |
175 | StringRef Name, unsigned NumSections, uint64_t VMAddr, uint64_t VMSize, |
176 | uint64_t SectionDataStartOffset, uint64_t SectionDataSize, uint32_t MaxProt, |
177 | uint32_t InitProt) { |
178 | |
179 | |
180 | |
181 | uint64_t Start = W.OS.tell(); |
182 | (void) Start; |
183 | |
184 | unsigned SegmentLoadCommandSize = |
185 | is64Bit() ? sizeof(MachO::segment_command_64): |
186 | sizeof(MachO::segment_command); |
187 | W.write<uint32_t>(is64Bit() ? MachO::LC_SEGMENT_64 : MachO::LC_SEGMENT); |
188 | W.write<uint32_t>(SegmentLoadCommandSize + |
189 | NumSections * (is64Bit() ? sizeof(MachO::section_64) : |
190 | sizeof(MachO::section))); |
191 | |
192 | writeWithPadding(Name, 16); |
193 | if (is64Bit()) { |
194 | W.write<uint64_t>(VMAddr); |
195 | W.write<uint64_t>(VMSize); |
196 | W.write<uint64_t>(SectionDataStartOffset); |
197 | W.write<uint64_t>(SectionDataSize); |
198 | } else { |
199 | W.write<uint32_t>(VMAddr); |
200 | W.write<uint32_t>(VMSize); |
201 | W.write<uint32_t>(SectionDataStartOffset); |
202 | W.write<uint32_t>(SectionDataSize); |
203 | } |
204 | |
205 | W.write<uint32_t>(MaxProt); |
206 | |
207 | W.write<uint32_t>(InitProt); |
208 | W.write<uint32_t>(NumSections); |
209 | W.write<uint32_t>(0); |
210 | |
211 | assert(W.OS.tell() - Start == SegmentLoadCommandSize); |
212 | } |
213 | |
214 | void MachObjectWriter::writeSection(const MCAsmLayout &Layout, |
215 | const MCSection &Sec, uint64_t VMAddr, |
216 | uint64_t FileOffset, unsigned Flags, |
217 | uint64_t RelocationsStart, |
218 | unsigned NumRelocations) { |
219 | uint64_t SectionSize = Layout.getSectionAddressSize(&Sec); |
220 | const MCSectionMachO &Section = cast<MCSectionMachO>(Sec); |
221 | |
222 | |
223 | if (Section.isVirtualSection()) { |
224 | assert(Layout.getSectionFileSize(&Sec) == 0 && "Invalid file size!"); |
225 | FileOffset = 0; |
226 | } |
227 | |
228 | |
229 | |
230 | |
231 | uint64_t Start = W.OS.tell(); |
232 | (void) Start; |
233 | |
234 | writeWithPadding(Section.getName(), 16); |
235 | writeWithPadding(Section.getSegmentName(), 16); |
236 | if (is64Bit()) { |
237 | W.write<uint64_t>(VMAddr); |
238 | W.write<uint64_t>(SectionSize); |
239 | } else { |
240 | W.write<uint32_t>(VMAddr); |
241 | W.write<uint32_t>(SectionSize); |
242 | } |
243 | W.write<uint32_t>(FileOffset); |
244 | |
245 | assert(isPowerOf2_32(Section.getAlignment()) && "Invalid alignment!"); |
246 | W.write<uint32_t>(Log2_32(Section.getAlignment())); |
247 | W.write<uint32_t>(NumRelocations ? RelocationsStart : 0); |
248 | W.write<uint32_t>(NumRelocations); |
249 | W.write<uint32_t>(Flags); |
250 | W.write<uint32_t>(IndirectSymBase.lookup(&Sec)); |
251 | W.write<uint32_t>(Section.getStubSize()); |
252 | if (is64Bit()) |
253 | W.write<uint32_t>(0); |
254 | |
255 | assert(W.OS.tell() - Start == |
256 | (is64Bit() ? sizeof(MachO::section_64) : sizeof(MachO::section))); |
257 | } |
258 | |
259 | void MachObjectWriter::writeSymtabLoadCommand(uint32_t SymbolOffset, |
260 | uint32_t NumSymbols, |
261 | uint32_t StringTableOffset, |
262 | uint32_t StringTableSize) { |
263 | |
264 | |
265 | uint64_t Start = W.OS.tell(); |
266 | (void) Start; |
267 | |
268 | W.write<uint32_t>(MachO::LC_SYMTAB); |
269 | W.write<uint32_t>(sizeof(MachO::symtab_command)); |
270 | W.write<uint32_t>(SymbolOffset); |
271 | W.write<uint32_t>(NumSymbols); |
272 | W.write<uint32_t>(StringTableOffset); |
273 | W.write<uint32_t>(StringTableSize); |
274 | |
275 | assert(W.OS.tell() - Start == sizeof(MachO::symtab_command)); |
276 | } |
277 | |
278 | void MachObjectWriter::writeDysymtabLoadCommand(uint32_t FirstLocalSymbol, |
279 | uint32_t NumLocalSymbols, |
280 | uint32_t FirstExternalSymbol, |
281 | uint32_t NumExternalSymbols, |
282 | uint32_t FirstUndefinedSymbol, |
283 | uint32_t NumUndefinedSymbols, |
284 | uint32_t IndirectSymbolOffset, |
285 | uint32_t NumIndirectSymbols) { |
286 | |
287 | |
288 | uint64_t Start = W.OS.tell(); |
289 | (void) Start; |
290 | |
291 | W.write<uint32_t>(MachO::LC_DYSYMTAB); |
292 | W.write<uint32_t>(sizeof(MachO::dysymtab_command)); |
293 | W.write<uint32_t>(FirstLocalSymbol); |
294 | W.write<uint32_t>(NumLocalSymbols); |
295 | W.write<uint32_t>(FirstExternalSymbol); |
296 | W.write<uint32_t>(NumExternalSymbols); |
297 | W.write<uint32_t>(FirstUndefinedSymbol); |
298 | W.write<uint32_t>(NumUndefinedSymbols); |
299 | W.write<uint32_t>(0); |
300 | W.write<uint32_t>(0); |
301 | W.write<uint32_t>(0); |
302 | W.write<uint32_t>(0); |
303 | W.write<uint32_t>(0); |
304 | W.write<uint32_t>(0); |
305 | W.write<uint32_t>(IndirectSymbolOffset); |
306 | W.write<uint32_t>(NumIndirectSymbols); |
307 | W.write<uint32_t>(0); |
308 | W.write<uint32_t>(0); |
309 | W.write<uint32_t>(0); |
310 | W.write<uint32_t>(0); |
311 | |
312 | assert(W.OS.tell() - Start == sizeof(MachO::dysymtab_command)); |
313 | } |
314 | |
315 | MachObjectWriter::MachSymbolData * |
316 | MachObjectWriter::findSymbolData(const MCSymbol &Sym) { |
317 | for (auto *SymbolData : |
318 | {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData}) |
319 | for (MachSymbolData &Entry : *SymbolData) |
320 | if (Entry.Symbol == &Sym) |
321 | return &Entry; |
322 | |
323 | return nullptr; |
324 | } |
325 | |
326 | const MCSymbol &MachObjectWriter::findAliasedSymbol(const MCSymbol &Sym) const { |
327 | const MCSymbol *S = &Sym; |
328 | while (S->isVariable()) { |
329 | const MCExpr *Value = S->getVariableValue(); |
330 | const auto *Ref = dyn_cast<MCSymbolRefExpr>(Value); |
331 | if (!Ref) |
332 | return *S; |
333 | S = &Ref->getSymbol(); |
334 | } |
335 | return *S; |
336 | } |
337 | |
338 | void MachObjectWriter::writeNlist(MachSymbolData &MSD, |
339 | const MCAsmLayout &Layout) { |
340 | const MCSymbol *Symbol = MSD.Symbol; |
341 | const MCSymbol &Data = *Symbol; |
342 | const MCSymbol *AliasedSymbol = &findAliasedSymbol(*Symbol); |
343 | uint8_t SectionIndex = MSD.SectionIndex; |
344 | uint8_t Type = 0; |
345 | uint64_t Address = 0; |
346 | bool IsAlias = Symbol != AliasedSymbol; |
347 | |
348 | const MCSymbol &OrigSymbol = *Symbol; |
349 | MachSymbolData *AliaseeInfo; |
350 | if (IsAlias) { |
351 | AliaseeInfo = findSymbolData(*AliasedSymbol); |
352 | if (AliaseeInfo) |
353 | SectionIndex = AliaseeInfo->SectionIndex; |
354 | Symbol = AliasedSymbol; |
355 | |
356 | } |
357 | |
358 | |
359 | |
360 | |
361 | if (IsAlias && Symbol->isUndefined()) |
362 | Type = MachO::N_INDR; |
363 | else if (Symbol->isUndefined()) |
364 | Type = MachO::N_UNDF; |
365 | else if (Symbol->isAbsolute()) |
366 | Type = MachO::N_ABS; |
367 | else |
368 | Type = MachO::N_SECT; |
369 | |
370 | |
371 | |
372 | if (Data.isPrivateExtern()) |
373 | Type |= MachO::N_PEXT; |
374 | |
375 | |
376 | if (Data.isExternal() || (!IsAlias && Symbol->isUndefined())) |
377 | Type |= MachO::N_EXT; |
378 | |
379 | |
380 | if (IsAlias && Symbol->isUndefined()) |
381 | Address = AliaseeInfo->StringIndex; |
382 | else if (Symbol->isDefined()) |
383 | Address = getSymbolAddress(OrigSymbol, Layout); |
384 | else if (Symbol->isCommon()) { |
385 | |
386 | |
387 | Address = Symbol->getCommonSize(); |
388 | } |
389 | |
390 | |
391 | |
392 | W.write<uint32_t>(MSD.StringIndex); |
393 | W.OS << char(Type); |
394 | W.OS << char(SectionIndex); |
395 | |
396 | |
397 | |
398 | bool EncodeAsAltEntry = |
399 | IsAlias && cast<MCSymbolMachO>(OrigSymbol).isAltEntry(); |
400 | W.write<uint16_t>(cast<MCSymbolMachO>(Symbol)->getEncodedFlags(EncodeAsAltEntry)); |
401 | if (is64Bit()) |
402 | W.write<uint64_t>(Address); |
403 | else |
404 | W.write<uint32_t>(Address); |
405 | } |
406 | |
407 | void MachObjectWriter::writeLinkeditLoadCommand(uint32_t Type, |
408 | uint32_t DataOffset, |
409 | uint32_t DataSize) { |
410 | uint64_t Start = W.OS.tell(); |
411 | (void) Start; |
412 | |
413 | W.write<uint32_t>(Type); |
414 | W.write<uint32_t>(sizeof(MachO::linkedit_data_command)); |
415 | W.write<uint32_t>(DataOffset); |
416 | W.write<uint32_t>(DataSize); |
417 | |
418 | assert(W.OS.tell() - Start == sizeof(MachO::linkedit_data_command)); |
419 | } |
420 | |
421 | static unsigned ComputeLinkerOptionsLoadCommandSize( |
422 | const std::vector<std::string> &Options, bool is64Bit) |
423 | { |
424 | unsigned Size = sizeof(MachO::linker_option_command); |
425 | for (const std::string &Option : Options) |
426 | Size += Option.size() + 1; |
427 | return alignTo(Size, is64Bit ? 8 : 4); |
428 | } |
429 | |
430 | void MachObjectWriter::writeLinkerOptionsLoadCommand( |
431 | const std::vector<std::string> &Options) |
432 | { |
433 | unsigned Size = ComputeLinkerOptionsLoadCommandSize(Options, is64Bit()); |
434 | uint64_t Start = W.OS.tell(); |
435 | (void) Start; |
436 | |
437 | W.write<uint32_t>(MachO::LC_LINKER_OPTION); |
438 | W.write<uint32_t>(Size); |
439 | W.write<uint32_t>(Options.size()); |
440 | uint64_t BytesWritten = sizeof(MachO::linker_option_command); |
441 | for (const std::string &Option : Options) { |
442 | |
443 | W.OS << Option << '\0'; |
444 | BytesWritten += Option.size() + 1; |
445 | } |
446 | |
447 | |
448 | W.OS.write_zeros( |
449 | offsetToAlignment(BytesWritten, is64Bit() ? Align(8) : Align(4))); |
450 | |
451 | assert(W.OS.tell() - Start == Size); |
452 | } |
453 | |
454 | static bool isFixupTargetValid(const MCValue &Target) { |
455 | |
456 | |
457 | if (!Target.getSymA() && Target.getSymB()) |
458 | return false; |
459 | return true; |
460 | } |
461 | |
462 | void MachObjectWriter::recordRelocation(MCAssembler &Asm, |
463 | const MCAsmLayout &Layout, |
464 | const MCFragment *Fragment, |
465 | const MCFixup &Fixup, MCValue Target, |
466 | uint64_t &FixedValue) { |
467 | if (!isFixupTargetValid(Target)) { |
468 | Asm.getContext().reportError(Fixup.getLoc(), |
469 | "unsupported relocation expression"); |
470 | return; |
471 | } |
472 | |
473 | TargetObjectWriter->recordRelocation(this, Asm, Layout, Fragment, Fixup, |
474 | Target, FixedValue); |
475 | } |
476 | |
477 | void MachObjectWriter::bindIndirectSymbols(MCAssembler &Asm) { |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | |
487 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
488 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
489 | const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section); |
490 | |
491 | if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS && |
492 | Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS && |
493 | Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS && |
494 | Section.getType() != MachO::S_SYMBOL_STUBS) { |
495 | MCSymbol &Symbol = *it->Symbol; |
496 | report_fatal_error("indirect symbol '" + Symbol.getName() + |
497 | "' not in a symbol pointer or stub section"); |
498 | } |
499 | } |
500 | |
501 | |
502 | unsigned IndirectIndex = 0; |
503 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
504 | ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) { |
505 | const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section); |
506 | |
507 | if (Section.getType() != MachO::S_NON_LAZY_SYMBOL_POINTERS && |
508 | Section.getType() != MachO::S_THREAD_LOCAL_VARIABLE_POINTERS) |
509 | continue; |
510 | |
511 | |
512 | IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex)); |
513 | |
514 | Asm.registerSymbol(*it->Symbol); |
515 | } |
516 | |
517 | |
518 | IndirectIndex = 0; |
519 | for (MCAssembler::indirect_symbol_iterator it = Asm.indirect_symbol_begin(), |
520 | ie = Asm.indirect_symbol_end(); it != ie; ++it, ++IndirectIndex) { |
521 | const MCSectionMachO &Section = cast<MCSectionMachO>(*it->Section); |
522 | |
523 | if (Section.getType() != MachO::S_LAZY_SYMBOL_POINTERS && |
524 | Section.getType() != MachO::S_SYMBOL_STUBS) |
525 | continue; |
526 | |
527 | |
528 | IndirectSymBase.insert(std::make_pair(it->Section, IndirectIndex)); |
529 | |
530 | |
531 | |
532 | |
533 | bool Created; |
534 | Asm.registerSymbol(*it->Symbol, &Created); |
535 | if (Created) |
536 | cast<MCSymbolMachO>(it->Symbol)->setReferenceTypeUndefinedLazy(true); |
537 | } |
538 | } |
539 | |
540 | |
541 | void MachObjectWriter::computeSymbolTable( |
542 | MCAssembler &Asm, std::vector<MachSymbolData> &LocalSymbolData, |
543 | std::vector<MachSymbolData> &ExternalSymbolData, |
544 | std::vector<MachSymbolData> &UndefinedSymbolData) { |
545 | |
546 | DenseMap<const MCSection*, uint8_t> SectionIndexMap; |
547 | unsigned Index = 1; |
548 | for (MCAssembler::iterator it = Asm.begin(), |
549 | ie = Asm.end(); it != ie; ++it, ++Index) |
550 | SectionIndexMap[&*it] = Index; |
551 | assert(Index <= 256 && "Too many sections!"); |
552 | |
553 | |
554 | for (const MCSymbol &Symbol : Asm.symbols()) { |
555 | if (!Asm.isSymbolLinkerVisible(Symbol)) |
556 | continue; |
557 | |
558 | StringTable.add(Symbol.getName()); |
559 | } |
560 | StringTable.finalize(); |
561 | |
562 | |
563 | |
564 | |
565 | |
566 | |
567 | for (const MCSymbol &Symbol : Asm.symbols()) { |
568 | |
569 | if (!Asm.isSymbolLinkerVisible(Symbol)) |
570 | continue; |
571 | |
572 | if (!Symbol.isExternal() && !Symbol.isUndefined()) |
573 | continue; |
574 | |
575 | MachSymbolData MSD; |
576 | MSD.Symbol = &Symbol; |
577 | MSD.StringIndex = StringTable.getOffset(Symbol.getName()); |
578 | |
579 | if (Symbol.isUndefined()) { |
580 | MSD.SectionIndex = 0; |
581 | UndefinedSymbolData.push_back(MSD); |
582 | } else if (Symbol.isAbsolute()) { |
583 | MSD.SectionIndex = 0; |
584 | ExternalSymbolData.push_back(MSD); |
585 | } else { |
586 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
587 | assert(MSD.SectionIndex && "Invalid section index!"); |
588 | ExternalSymbolData.push_back(MSD); |
589 | } |
590 | } |
591 | |
592 | |
593 | for (const MCSymbol &Symbol : Asm.symbols()) { |
594 | |
595 | if (!Asm.isSymbolLinkerVisible(Symbol)) |
596 | continue; |
597 | |
598 | if (Symbol.isExternal() || Symbol.isUndefined()) |
599 | continue; |
600 | |
601 | MachSymbolData MSD; |
602 | MSD.Symbol = &Symbol; |
603 | MSD.StringIndex = StringTable.getOffset(Symbol.getName()); |
604 | |
605 | if (Symbol.isAbsolute()) { |
606 | MSD.SectionIndex = 0; |
607 | LocalSymbolData.push_back(MSD); |
608 | } else { |
609 | MSD.SectionIndex = SectionIndexMap.lookup(&Symbol.getSection()); |
610 | assert(MSD.SectionIndex && "Invalid section index!"); |
611 | LocalSymbolData.push_back(MSD); |
612 | } |
613 | } |
614 | |
615 | |
616 | llvm::sort(ExternalSymbolData); |
617 | llvm::sort(UndefinedSymbolData); |
618 | |
619 | |
620 | Index = 0; |
621 | for (auto *SymbolData : |
622 | {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData}) |
623 | for (MachSymbolData &Entry : *SymbolData) |
624 | Entry.Symbol->setIndex(Index++); |
625 | |
626 | for (const MCSection &Section : Asm) { |
627 | for (RelAndSymbol &Rel : Relocations[&Section]) { |
628 | if (!Rel.Sym) |
629 | continue; |
630 | |
631 | |
632 | unsigned Index = Rel.Sym->getIndex(); |
633 | assert(isInt<24>(Index)); |
634 | if (W.Endian == support::little) |
635 | Rel.MRE.r_word1 = (Rel.MRE.r_word1 & (~0U << 24)) | Index | (1 << 27); |
636 | else |
637 | Rel.MRE.r_word1 = (Rel.MRE.r_word1 & 0xff) | Index << 8 | (1 << 4); |
638 | } |
639 | } |
640 | } |
641 | |
642 | void MachObjectWriter::computeSectionAddresses(const MCAssembler &Asm, |
643 | const MCAsmLayout &Layout) { |
644 | uint64_t StartAddress = 0; |
645 | for (const MCSection *Sec : Layout.getSectionOrder()) { |
| 2 | | Assuming '__begin1' is not equal to '__end1' | |
|
646 | StartAddress = alignTo(StartAddress, Sec->getAlignment()); |
647 | SectionAddress[Sec] = StartAddress; |
648 | StartAddress += Layout.getSectionAddressSize(Sec); |
649 | |
650 | |
651 | |
652 | |
653 | StartAddress += getPaddingSize(Sec, Layout); |
| 3 | | Calling 'MachObjectWriter::getPaddingSize' | |
|
654 | } |
655 | } |
656 | |
657 | void MachObjectWriter::executePostLayoutBinding(MCAssembler &Asm, |
658 | const MCAsmLayout &Layout) { |
659 | computeSectionAddresses(Asm, Layout); |
| 1 | Calling 'MachObjectWriter::computeSectionAddresses' | |
|
660 | |
661 | |
662 | bindIndirectSymbols(Asm); |
663 | } |
664 | |
665 | bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( |
666 | const MCAssembler &Asm, const MCSymbol &A, const MCSymbol &B, |
667 | bool InSet) const { |
668 | |
669 | |
670 | |
671 | if (A.isVariable() || B.isVariable()) |
672 | return false; |
673 | return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, A, B, |
674 | InSet); |
675 | } |
676 | |
677 | bool MachObjectWriter::isSymbolRefDifferenceFullyResolvedImpl( |
678 | const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB, |
679 | bool InSet, bool IsPCRel) const { |
680 | if (InSet) |
681 | return true; |
682 | |
683 | |
684 | |
685 | |
686 | |
687 | |
688 | const MCSymbol &SA = findAliasedSymbol(SymA); |
689 | const MCSection &SecA = SA.getSection(); |
690 | const MCSection &SecB = *FB.getParent(); |
691 | |
692 | if (IsPCRel) { |
693 | |
694 | |
695 | |
696 | |
697 | |
698 | |
699 | |
700 | |
701 | |
702 | |
703 | |
704 | |
705 | |
706 | bool hasReliableSymbolDifference = isX86_64(); |
707 | if (!hasReliableSymbolDifference) { |
708 | if (!SA.isInSection() || &SecA != &SecB || |
709 | (!SA.isTemporary() && FB.getAtom() != SA.getFragment()->getAtom() && |
710 | Asm.getSubsectionsViaSymbols())) |
711 | return false; |
712 | return true; |
713 | } |
714 | |
715 | |
716 | |
717 | |
718 | |
719 | |
720 | else if(!FB.getAtom() && |
721 | SA.isTemporary() && SA.isInSection() && &SecA == &SecB){ |
722 | return true; |
723 | } |
724 | } |
725 | |
726 | |
727 | if (&SecA != &SecB) |
728 | return false; |
729 | |
730 | const MCFragment *FA = SA.getFragment(); |
731 | |
732 | |
733 | if (!FA) |
734 | return false; |
735 | |
736 | |
737 | if (FA->getAtom() == FB.getAtom()) |
738 | return true; |
739 | |
740 | |
741 | return false; |
742 | } |
743 | |
744 | static MachO::LoadCommandType getLCFromMCVM(MCVersionMinType Type) { |
745 | switch (Type) { |
746 | case MCVM_OSXVersionMin: return MachO::LC_VERSION_MIN_MACOSX; |
747 | case MCVM_IOSVersionMin: return MachO::LC_VERSION_MIN_IPHONEOS; |
748 | case MCVM_TvOSVersionMin: return MachO::LC_VERSION_MIN_TVOS; |
749 | case MCVM_WatchOSVersionMin: return MachO::LC_VERSION_MIN_WATCHOS; |
750 | } |
751 | llvm_unreachable("Invalid mc version min type"); |
752 | } |
753 | |
754 | uint64_t MachObjectWriter::writeObject(MCAssembler &Asm, |
755 | const MCAsmLayout &Layout) { |
756 | uint64_t StartOffset = W.OS.tell(); |
757 | |
758 | |
759 | computeSymbolTable(Asm, LocalSymbolData, ExternalSymbolData, |
760 | UndefinedSymbolData); |
761 | |
762 | unsigned NumSections = Asm.size(); |
763 | const MCAssembler::VersionInfoType &VersionInfo = |
764 | Layout.getAssembler().getVersionInfo(); |
765 | |
766 | |
767 | |
768 | unsigned NumLoadCommands = 1; |
769 | uint64_t LoadCommandsSize = is64Bit() ? |
770 | sizeof(MachO::segment_command_64) + NumSections * sizeof(MachO::section_64): |
771 | sizeof(MachO::segment_command) + NumSections * sizeof(MachO::section); |
772 | |
773 | |
774 | if (VersionInfo.Major != 0) { |
775 | ++NumLoadCommands; |
776 | if (VersionInfo.EmitBuildVersion) |
777 | LoadCommandsSize += sizeof(MachO::build_version_command); |
778 | else |
779 | LoadCommandsSize += sizeof(MachO::version_min_command); |
780 | } |
781 | |
782 | |
783 | unsigned NumDataRegions = Asm.getDataRegions().size(); |
784 | if (NumDataRegions) { |
785 | ++NumLoadCommands; |
786 | LoadCommandsSize += sizeof(MachO::linkedit_data_command); |
787 | } |
788 | |
789 | |
790 | uint64_t LOHRawSize = Asm.getLOHContainer().getEmitSize(*this, Layout); |
791 | uint64_t LOHSize = alignTo(LOHRawSize, is64Bit() ? 8 : 4); |
792 | if (LOHSize) { |
793 | ++NumLoadCommands; |
794 | LoadCommandsSize += sizeof(MachO::linkedit_data_command); |
795 | } |
796 | |
797 | |
798 | unsigned NumSymbols = LocalSymbolData.size() + ExternalSymbolData.size() + |
799 | UndefinedSymbolData.size(); |
800 | if (NumSymbols) { |
801 | NumLoadCommands += 2; |
802 | LoadCommandsSize += (sizeof(MachO::symtab_command) + |
803 | sizeof(MachO::dysymtab_command)); |
804 | } |
805 | |
806 | |
807 | for (const auto &Option : Asm.getLinkerOptions()) { |
808 | ++NumLoadCommands; |
809 | LoadCommandsSize += ComputeLinkerOptionsLoadCommandSize(Option, is64Bit()); |
810 | } |
811 | |
812 | |
813 | |
814 | uint64_t SectionDataStart = (is64Bit() ? sizeof(MachO::mach_header_64) : |
815 | sizeof(MachO::mach_header)) + LoadCommandsSize; |
816 | uint64_t SectionDataSize = 0; |
817 | uint64_t SectionDataFileSize = 0; |
818 | uint64_t VMSize = 0; |
819 | for (const MCSection &Sec : Asm) { |
820 | uint64_t Address = getSectionAddress(&Sec); |
821 | uint64_t Size = Layout.getSectionAddressSize(&Sec); |
822 | uint64_t FileSize = Layout.getSectionFileSize(&Sec); |
823 | FileSize += getPaddingSize(&Sec, Layout); |
824 | |
825 | VMSize = std::max(VMSize, Address + Size); |
826 | |
827 | if (Sec.isVirtualSection()) |
828 | continue; |
829 | |
830 | SectionDataSize = std::max(SectionDataSize, Address + Size); |
831 | SectionDataFileSize = std::max(SectionDataFileSize, Address + FileSize); |
832 | } |
833 | |
834 | |
835 | |
836 | |
837 | unsigned SectionDataPadding = |
838 | offsetToAlignment(SectionDataFileSize, is64Bit() ? Align(8) : Align(4)); |
839 | SectionDataFileSize += SectionDataPadding; |
840 | |
841 | |
842 | writeHeader(MachO::MH_OBJECT, NumLoadCommands, LoadCommandsSize, |
843 | Asm.getSubsectionsViaSymbols()); |
844 | uint32_t Prot = |
845 | MachO::VM_PROT_READ | MachO::VM_PROT_WRITE | MachO::VM_PROT_EXECUTE; |
846 | writeSegmentLoadCommand("", NumSections, 0, VMSize, SectionDataStart, |
847 | SectionDataSize, Prot, Prot); |
848 | |
849 | |
850 | uint64_t RelocTableEnd = SectionDataStart + SectionDataFileSize; |
851 | for (const MCSection &Section : Asm) { |
852 | const auto &Sec = cast<MCSectionMachO>(Section); |
853 | std::vector<RelAndSymbol> &Relocs = Relocations[&Sec]; |
854 | unsigned NumRelocs = Relocs.size(); |
855 | uint64_t SectionStart = SectionDataStart + getSectionAddress(&Sec); |
856 | unsigned Flags = Sec.getTypeAndAttributes(); |
857 | if (Sec.hasInstructions()) |
858 | Flags |= MachO::S_ATTR_SOME_INSTRUCTIONS; |
859 | writeSection(Layout, Sec, getSectionAddress(&Sec), SectionStart, Flags, |
860 | RelocTableEnd, NumRelocs); |
861 | RelocTableEnd += NumRelocs * sizeof(MachO::any_relocation_info); |
862 | } |
863 | |
864 | |
865 | if (VersionInfo.Major != 0) { |
866 | auto EncodeVersion = [](VersionTuple V) -> uint32_t { |
867 | assert(!V.empty() && "empty version"); |
868 | unsigned Update = V.getSubminor() ? *V.getSubminor() : 0; |
869 | unsigned Minor = V.getMinor() ? *V.getMinor() : 0; |
870 | assert(Update < 256 && "unencodable update target version"); |
871 | assert(Minor < 256 && "unencodable minor target version"); |
872 | assert(V.getMajor() < 65536 && "unencodable major target version"); |
873 | return Update | (Minor << 8) | (V.getMajor() << 16); |
874 | }; |
875 | uint32_t EncodedVersion = EncodeVersion( |
876 | VersionTuple(VersionInfo.Major, VersionInfo.Minor, VersionInfo.Update)); |
877 | uint32_t SDKVersion = !VersionInfo.SDKVersion.empty() |
878 | ? EncodeVersion(VersionInfo.SDKVersion) |
879 | : 0; |
880 | if (VersionInfo.EmitBuildVersion) { |
881 | |
882 | W.write<uint32_t>(MachO::LC_BUILD_VERSION); |
883 | W.write<uint32_t>(sizeof(MachO::build_version_command)); |
884 | W.write<uint32_t>(VersionInfo.TypeOrPlatform.Platform); |
885 | W.write<uint32_t>(EncodedVersion); |
886 | W.write<uint32_t>(SDKVersion); |
887 | W.write<uint32_t>(0); |
888 | } else { |
889 | MachO::LoadCommandType LCType |
890 | = getLCFromMCVM(VersionInfo.TypeOrPlatform.Type); |
891 | W.write<uint32_t>(LCType); |
892 | W.write<uint32_t>(sizeof(MachO::version_min_command)); |
893 | W.write<uint32_t>(EncodedVersion); |
894 | W.write<uint32_t>(SDKVersion); |
895 | } |
896 | } |
897 | |
898 | |
899 | uint64_t DataInCodeTableEnd = RelocTableEnd + NumDataRegions * 8; |
900 | if (NumDataRegions) { |
901 | uint64_t DataRegionsOffset = RelocTableEnd; |
902 | uint64_t DataRegionsSize = NumDataRegions * 8; |
903 | writeLinkeditLoadCommand(MachO::LC_DATA_IN_CODE, DataRegionsOffset, |
904 | DataRegionsSize); |
905 | } |
906 | |
907 | |
908 | uint64_t LOHTableEnd = DataInCodeTableEnd + LOHSize; |
909 | if (LOHSize) |
910 | writeLinkeditLoadCommand(MachO::LC_LINKER_OPTIMIZATION_HINT, |
911 | DataInCodeTableEnd, LOHSize); |
912 | |
913 | |
914 | if (NumSymbols) { |
915 | unsigned FirstLocalSymbol = 0; |
916 | unsigned NumLocalSymbols = LocalSymbolData.size(); |
917 | unsigned FirstExternalSymbol = FirstLocalSymbol + NumLocalSymbols; |
918 | unsigned NumExternalSymbols = ExternalSymbolData.size(); |
919 | unsigned FirstUndefinedSymbol = FirstExternalSymbol + NumExternalSymbols; |
920 | unsigned NumUndefinedSymbols = UndefinedSymbolData.size(); |
921 | unsigned NumIndirectSymbols = Asm.indirect_symbol_size(); |
922 | unsigned NumSymTabSymbols = |
923 | NumLocalSymbols + NumExternalSymbols + NumUndefinedSymbols; |
924 | uint64_t IndirectSymbolSize = NumIndirectSymbols * 4; |
925 | uint64_t IndirectSymbolOffset = 0; |
926 | |
927 | |
928 | if (NumIndirectSymbols) |
929 | IndirectSymbolOffset = LOHTableEnd; |
930 | |
931 | |
932 | uint64_t SymbolTableOffset = LOHTableEnd + IndirectSymbolSize; |
933 | |
934 | |
935 | uint64_t StringTableOffset = |
936 | SymbolTableOffset + NumSymTabSymbols * (is64Bit() ? |
937 | sizeof(MachO::nlist_64) : |
938 | sizeof(MachO::nlist)); |
939 | writeSymtabLoadCommand(SymbolTableOffset, NumSymTabSymbols, |
940 | StringTableOffset, StringTable.getSize()); |
941 | |
942 | writeDysymtabLoadCommand(FirstLocalSymbol, NumLocalSymbols, |
943 | FirstExternalSymbol, NumExternalSymbols, |
944 | FirstUndefinedSymbol, NumUndefinedSymbols, |
945 | IndirectSymbolOffset, NumIndirectSymbols); |
946 | } |
947 | |
948 | |
949 | for (const auto &Option : Asm.getLinkerOptions()) |
950 | writeLinkerOptionsLoadCommand(Option); |
951 | |
952 | |
953 | for (const MCSection &Sec : Asm) { |
954 | Asm.writeSectionData(W.OS, &Sec, Layout); |
955 | |
956 | uint64_t Pad = getPaddingSize(&Sec, Layout); |
957 | W.OS.write_zeros(Pad); |
958 | } |
959 | |
960 | |
961 | W.OS.write_zeros(SectionDataPadding); |
962 | |
963 | |
964 | for (const MCSection &Sec : Asm) { |
965 | |
966 | |
967 | std::vector<RelAndSymbol> &Relocs = Relocations[&Sec]; |
968 | for (const RelAndSymbol &Rel : make_range(Relocs.rbegin(), Relocs.rend())) { |
969 | W.write<uint32_t>(Rel.MRE.r_word0); |
970 | W.write<uint32_t>(Rel.MRE.r_word1); |
971 | } |
972 | } |
973 | |
974 | |
975 | for (MCAssembler::const_data_region_iterator |
976 | it = Asm.data_region_begin(), ie = Asm.data_region_end(); |
977 | it != ie; ++it) { |
978 | const DataRegionData *Data = &(*it); |
979 | uint64_t Start = getSymbolAddress(*Data->Start, Layout); |
980 | uint64_t End; |
981 | if (Data->End) |
982 | End = getSymbolAddress(*Data->End, Layout); |
983 | else |
984 | report_fatal_error("Data region not terminated"); |
985 | |
986 | LLVM_DEBUG(dbgs() << "data in code region-- kind: " << Data->Kind |
987 | << " start: " << Start << "(" << Data->Start->getName() |
988 | << ")" |
989 | << " end: " << End << "(" << Data->End->getName() << ")" |
990 | << " size: " << End - Start << "\n"); |
991 | W.write<uint32_t>(Start); |
992 | W.write<uint16_t>(End - Start); |
993 | W.write<uint16_t>(Data->Kind); |
994 | } |
995 | |
996 | |
997 | if (LOHSize) { |
998 | #ifndef NDEBUG |
999 | unsigned Start = W.OS.tell(); |
1000 | #endif |
1001 | Asm.getLOHContainer().emit(*this, Layout); |
1002 | |
1003 | W.OS.write_zeros( |
1004 | offsetToAlignment(LOHRawSize, is64Bit() ? Align(8) : Align(4))); |
1005 | assert(W.OS.tell() - Start == LOHSize); |
1006 | } |
1007 | |
1008 | |
1009 | if (NumSymbols) { |
1010 | |
1011 | for (MCAssembler::const_indirect_symbol_iterator |
1012 | it = Asm.indirect_symbol_begin(), |
1013 | ie = Asm.indirect_symbol_end(); it != ie; ++it) { |
1014 | |
1015 | |
1016 | const MCSectionMachO &Section = |
1017 | static_cast<const MCSectionMachO &>(*it->Section); |
1018 | if (Section.getType() == MachO::S_NON_LAZY_SYMBOL_POINTERS) { |
1019 | |
1020 | if (it->Symbol->isDefined() && !it->Symbol->isExternal()) { |
1021 | uint32_t Flags = MachO::INDIRECT_SYMBOL_LOCAL; |
1022 | if (it->Symbol->isAbsolute()) |
1023 | Flags |= MachO::INDIRECT_SYMBOL_ABS; |
1024 | W.write<uint32_t>(Flags); |
1025 | continue; |
1026 | } |
1027 | } |
1028 | |
1029 | W.write<uint32_t>(it->Symbol->getIndex()); |
1030 | } |
1031 | |
1032 | |
1033 | |
1034 | |
1035 | for (auto *SymbolData : |
1036 | {&LocalSymbolData, &ExternalSymbolData, &UndefinedSymbolData}) |
1037 | for (MachSymbolData &Entry : *SymbolData) |
1038 | writeNlist(Entry, Layout); |
1039 | |
1040 | |
1041 | StringTable.write(W.OS); |
1042 | } |
1043 | |
1044 | return W.OS.tell() - StartOffset; |
1045 | } |
1046 | |
1047 | std::unique_ptr<MCObjectWriter> |
1048 | llvm::createMachObjectWriter(std::unique_ptr<MCMachObjectTargetWriter> MOTW, |
1049 | raw_pwrite_stream &OS, bool IsLittleEndian) { |
1050 | return std::make_unique<MachObjectWriter>(std::move(MOTW), OS, |
1051 | IsLittleEndian); |
1052 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | #ifndef LLVM_SUPPORT_ALIGNMENT_H_ |
22 | #define LLVM_SUPPORT_ALIGNMENT_H_ |
23 | |
24 | #include "llvm/ADT/Optional.h" |
25 | #include "llvm/Support/MathExtras.h" |
26 | #include <cassert> |
27 | #ifndef NDEBUG |
28 | #include <string> |
29 | #endif // NDEBUG |
30 | |
31 | namespace llvm { |
32 | |
33 | #define ALIGN_CHECK_ISPOSITIVE(decl) \ |
34 | assert(decl > 0 && (#decl " should be defined")) |
35 | |
36 | |
37 | |
38 | |
39 | struct Align { |
40 | private: |
41 | uint8_t ShiftValue = 0; |
42 | |
43 | |
44 | friend struct MaybeAlign; |
45 | friend unsigned Log2(Align); |
46 | friend bool operator==(Align Lhs, Align Rhs); |
47 | friend bool operator!=(Align Lhs, Align Rhs); |
48 | friend bool operator<=(Align Lhs, Align Rhs); |
49 | friend bool operator>=(Align Lhs, Align Rhs); |
50 | friend bool operator<(Align Lhs, Align Rhs); |
51 | friend bool operator>(Align Lhs, Align Rhs); |
52 | friend unsigned encode(struct MaybeAlign A); |
53 | friend struct MaybeAlign decodeMaybeAlign(unsigned Value); |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | struct LogValue { |
63 | uint8_t Log; |
64 | }; |
65 | |
66 | public: |
67 | |
68 | constexpr Align() = default; |
69 | |
70 | |
71 | constexpr Align(const Align &Other) = default; |
72 | constexpr Align(Align &&Other) = default; |
73 | Align &operator=(const Align &Other) = default; |
74 | Align &operator=(Align &&Other) = default; |
75 | |
76 | explicit Align(uint64_t Value) { |
77 | assert(Value > 0 && "Value must not be 0"); |
78 | assert(llvm::isPowerOf2_64(Value) && "Alignment is not a power of 2"); |
79 | ShiftValue = Log2_64(Value); |
80 | assert(ShiftValue < 64 && "Broken invariant"); |
81 | } |
82 | |
83 | |
84 | |
85 | uint64_t value() const { return uint64_t(1) << ShiftValue; } |
| 12 | | The result of the left shift is undefined due to shifting by '255', which is greater or equal to the width of type 'uint64_t' |
|
86 | |
87 | |
88 | template <size_t kValue> constexpr static LogValue Constant() { |
89 | return LogValue{static_cast<uint8_t>(CTLog2<kValue>())}; |
90 | } |
91 | |
92 | |
93 | |
94 | template <typename T> constexpr static LogValue Of() { |
95 | return Constant<std::alignment_of<T>::value>(); |
96 | } |
97 | |
98 | |
99 | constexpr Align(LogValue CA) : ShiftValue(CA.Log) {} |
100 | }; |
101 | |
102 | |
103 | inline Align assumeAligned(uint64_t Value) { |
104 | return Value ? Align(Value) : Align(); |
105 | } |
106 | |
107 | |
108 | |
109 | struct MaybeAlign : public llvm::Optional<Align> { |
110 | private: |
111 | using UP = llvm::Optional<Align>; |
112 | |
113 | public: |
114 | |
115 | MaybeAlign() = default; |
116 | |
117 | |
118 | MaybeAlign(const MaybeAlign &Other) = default; |
119 | MaybeAlign &operator=(const MaybeAlign &Other) = default; |
120 | MaybeAlign(MaybeAlign &&Other) = default; |
121 | MaybeAlign &operator=(MaybeAlign &&Other) = default; |
122 | |
123 | |
124 | using UP::UP; |
125 | |
126 | explicit MaybeAlign(uint64_t Value) { |
127 | assert((Value == 0 || llvm::isPowerOf2_64(Value)) && |
128 | "Alignment is neither 0 nor a power of 2"); |
129 | if (Value) |
130 | emplace(Value); |
131 | } |
132 | |
133 | |
134 | Align valueOrOne() const { return hasValue() ? getValue() : Align(); } |
135 | }; |
136 | |
137 | |
138 | inline bool isAligned(Align Lhs, uint64_t SizeInBytes) { |
139 | return SizeInBytes % Lhs.value() == 0; |
140 | } |
141 | |
142 | |
143 | inline bool isAddrAligned(Align Lhs, const void *Addr) { |
144 | return isAligned(Lhs, reinterpret_cast<uintptr_t>(Addr)); |
145 | } |
146 | |
147 | |
148 | inline uint64_t alignTo(uint64_t Size, Align A) { |
149 | const uint64_t Value = A.value(); |
| |
150 | |
151 | |
152 | |
153 | |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | return (Size + Value - 1) & ~(Value - 1U); |
160 | } |
161 | |
162 | |
163 | |
164 | |
165 | |
166 | |
167 | |
168 | |
169 | |
170 | |
171 | |
172 | |
173 | inline uint64_t alignTo(uint64_t Size, Align A, uint64_t Skew) { |
174 | const uint64_t Value = A.value(); |
175 | Skew %= Value; |
176 | return ((Size + Value - 1 - Skew) & ~(Value - 1U)) + Skew; |
177 | } |
178 | |
179 | |
180 | |
181 | inline uint64_t alignTo(uint64_t Size, MaybeAlign A) { |
182 | return A ? alignTo(Size, A.getValue()) : Size; |
183 | } |
184 | |
185 | |
186 | inline uintptr_t alignAddr(const void *Addr, Align Alignment) { |
187 | uintptr_t ArithAddr = reinterpret_cast<uintptr_t>(Addr); |
188 | assert(static_cast<uintptr_t>(ArithAddr + Alignment.value() - 1) >= |
189 | ArithAddr && |
190 | "Overflow"); |
191 | return alignTo(ArithAddr, Alignment); |
192 | } |
193 | |
194 | |
195 | |
196 | inline uint64_t offsetToAlignment(uint64_t Value, Align Alignment) { |
197 | return alignTo(Value, Alignment) - Value; |
| 9 | | The value 255 is assigned to 'A.ShiftValue' | |
|
| |
198 | } |
199 | |
200 | |
201 | |
202 | inline uint64_t offsetToAlignedAddr(const void *Addr, Align Alignment) { |
203 | return offsetToAlignment(reinterpret_cast<uintptr_t>(Addr), Alignment); |
204 | } |
205 | |
206 | |
207 | inline unsigned Log2(Align A) { return A.ShiftValue; } |
208 | |
209 | |
210 | |
211 | inline Align commonAlignment(Align A, Align B) { return std::min(A, B); } |
212 | |
213 | |
214 | |
215 | inline Align commonAlignment(Align A, uint64_t Offset) { |
216 | return Align(MinAlign(A.value(), Offset)); |
217 | } |
218 | |
219 | |
220 | |
221 | inline MaybeAlign commonAlignment(MaybeAlign A, MaybeAlign B) { |
222 | return A && B ? commonAlignment(*A, *B) : A ? A : B; |
223 | } |
224 | |
225 | |
226 | |
227 | inline MaybeAlign commonAlignment(MaybeAlign A, uint64_t Offset) { |
228 | return MaybeAlign(MinAlign((*A).value(), Offset)); |
229 | } |
230 | |
231 | |
232 | inline unsigned encode(MaybeAlign A) { return A ? A->ShiftValue + 1 : 0; } |
233 | |
234 | |
235 | inline MaybeAlign decodeMaybeAlign(unsigned Value) { |
236 | if (Value == 0) |
237 | return MaybeAlign(); |
238 | Align Out; |
239 | Out.ShiftValue = Value - 1; |
240 | return Out; |
241 | } |
242 | |
243 | |
244 | |
245 | inline unsigned encode(Align A) { return encode(MaybeAlign(A)); } |
246 | |
247 | |
248 | inline bool operator==(Align Lhs, uint64_t Rhs) { |
249 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
250 | return Lhs.value() == Rhs; |
251 | } |
252 | inline bool operator!=(Align Lhs, uint64_t Rhs) { |
253 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
254 | return Lhs.value() != Rhs; |
255 | } |
256 | inline bool operator<=(Align Lhs, uint64_t Rhs) { |
257 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
258 | return Lhs.value() <= Rhs; |
259 | } |
260 | inline bool operator>=(Align Lhs, uint64_t Rhs) { |
261 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
262 | return Lhs.value() >= Rhs; |
263 | } |
264 | inline bool operator<(Align Lhs, uint64_t Rhs) { |
265 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
266 | return Lhs.value() < Rhs; |
267 | } |
268 | inline bool operator>(Align Lhs, uint64_t Rhs) { |
269 | ALIGN_CHECK_ISPOSITIVE(Rhs); |
270 | return Lhs.value() > Rhs; |
271 | } |
272 | |
273 | |
274 | inline bool operator==(MaybeAlign Lhs, uint64_t Rhs) { |
275 | return Lhs ? (*Lhs).value() == Rhs : Rhs == 0; |
276 | } |
277 | inline bool operator!=(MaybeAlign Lhs, uint64_t Rhs) { |
278 | return Lhs ? (*Lhs).value() != Rhs : Rhs != 0; |
279 | } |
280 | |
281 | |
282 | inline bool operator==(Align Lhs, Align Rhs) { |
283 | return Lhs.ShiftValue == Rhs.ShiftValue; |
284 | } |
285 | inline bool operator!=(Align Lhs, Align Rhs) { |
286 | return Lhs.ShiftValue != Rhs.ShiftValue; |
287 | } |
288 | inline bool operator<=(Align Lhs, Align Rhs) { |
289 | return Lhs.ShiftValue <= Rhs.ShiftValue; |
290 | } |
291 | inline bool operator>=(Align Lhs, Align Rhs) { |
292 | return Lhs.ShiftValue >= Rhs.ShiftValue; |
293 | } |
294 | inline bool operator<(Align Lhs, Align Rhs) { |
295 | return Lhs.ShiftValue < Rhs.ShiftValue; |
296 | } |
297 | inline bool operator>(Align Lhs, Align Rhs) { |
298 | return Lhs.ShiftValue > Rhs.ShiftValue; |
299 | } |
300 | |
301 | |
302 | bool operator<=(Align Lhs, MaybeAlign Rhs) = delete; |
303 | bool operator>=(Align Lhs, MaybeAlign Rhs) = delete; |
304 | bool operator<(Align Lhs, MaybeAlign Rhs) = delete; |
305 | bool operator>(Align Lhs, MaybeAlign Rhs) = delete; |
306 | |
307 | bool operator<=(MaybeAlign Lhs, Align Rhs) = delete; |
308 | bool operator>=(MaybeAlign Lhs, Align Rhs) = delete; |
309 | bool operator<(MaybeAlign Lhs, Align Rhs) = delete; |
310 | bool operator>(MaybeAlign Lhs, Align Rhs) = delete; |
311 | |
312 | bool operator<=(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
313 | bool operator>=(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
314 | bool operator<(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
315 | bool operator>(MaybeAlign Lhs, MaybeAlign Rhs) = delete; |
316 | |
317 | inline Align operator*(Align Lhs, uint64_t Rhs) { |
318 | assert(Rhs > 0 && "Rhs must be positive"); |
319 | return Align(Lhs.value() * Rhs); |
320 | } |
321 | |
322 | inline MaybeAlign operator*(MaybeAlign Lhs, uint64_t Rhs) { |
323 | assert(Rhs > 0 && "Rhs must be positive"); |
324 | return Lhs ? Lhs.getValue() * Rhs : MaybeAlign(); |
325 | } |
326 | |
327 | inline Align operator/(Align Lhs, uint64_t Divisor) { |
328 | assert(llvm::isPowerOf2_64(Divisor) && |
329 | "Divisor must be positive and a power of 2"); |
330 | assert(Lhs != 1 && "Can't halve byte alignment"); |
331 | return Align(Lhs.value() / Divisor); |
332 | } |
333 | |
334 | inline MaybeAlign operator/(MaybeAlign Lhs, uint64_t Divisor) { |
335 | assert(llvm::isPowerOf2_64(Divisor) && |
336 | "Divisor must be positive and a power of 2"); |
337 | return Lhs ? Lhs.getValue() / Divisor : MaybeAlign(); |
338 | } |
339 | |
340 | inline Align max(MaybeAlign Lhs, Align Rhs) { |
341 | return Lhs && *Lhs > Rhs ? *Lhs : Rhs; |
342 | } |
343 | |
344 | inline Align max(Align Lhs, MaybeAlign Rhs) { |
345 | return Rhs && *Rhs > Lhs ? *Rhs : Lhs; |
346 | } |
347 | |
348 | #ifndef NDEBUG |
349 | |
350 | inline std::string DebugStr(const Align &A) { |
351 | return std::to_string(A.value()); |
352 | } |
353 | |
354 | inline std::string DebugStr(const MaybeAlign &MA) { |
355 | if (MA) |
356 | return std::to_string(MA->value()); |
357 | return "None"; |
358 | } |
359 | #endif // NDEBUG |
360 | |
361 | #undef ALIGN_CHECK_ISPOSITIVE |
362 | |
363 | } |
364 | |
365 | #endif // LLVM_SUPPORT_ALIGNMENT_H_ |