File: | build/source/lld/MachO/SyntheticSections.cpp |
Warning: | line 1644, column 29 The result of the left shift is undefined due to shifting by '32', which is greater or equal to the width of type 'int' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- SyntheticSections.cpp ---------------------------------------------===// | |||
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 | #include "SyntheticSections.h" | |||
10 | #include "ConcatOutputSection.h" | |||
11 | #include "Config.h" | |||
12 | #include "ExportTrie.h" | |||
13 | #include "InputFiles.h" | |||
14 | #include "MachOStructs.h" | |||
15 | #include "OutputSegment.h" | |||
16 | #include "SymbolTable.h" | |||
17 | #include "Symbols.h" | |||
18 | ||||
19 | #include "lld/Common/CommonLinkerContext.h" | |||
20 | #include "llvm/ADT/STLExtras.h" | |||
21 | #include "llvm/Config/llvm-config.h" | |||
22 | #include "llvm/Support/EndianStream.h" | |||
23 | #include "llvm/Support/FileSystem.h" | |||
24 | #include "llvm/Support/LEB128.h" | |||
25 | #include "llvm/Support/Parallel.h" | |||
26 | #include "llvm/Support/Path.h" | |||
27 | #include "llvm/Support/xxhash.h" | |||
28 | ||||
29 | #if defined(__APPLE__) | |||
30 | #include <sys/mman.h> | |||
31 | ||||
32 | #define COMMON_DIGEST_FOR_OPENSSL | |||
33 | #include <CommonCrypto/CommonDigest.h> | |||
34 | #else | |||
35 | #include "llvm/Support/SHA256.h" | |||
36 | #endif | |||
37 | ||||
38 | #ifdef LLVM_HAVE_LIBXAR | |||
39 | #include <fcntl.h> | |||
40 | extern "C" { | |||
41 | #include <xar/xar.h> | |||
42 | } | |||
43 | #endif | |||
44 | ||||
45 | using namespace llvm; | |||
46 | using namespace llvm::MachO; | |||
47 | using namespace llvm::support; | |||
48 | using namespace llvm::support::endian; | |||
49 | using namespace lld; | |||
50 | using namespace lld::macho; | |||
51 | ||||
52 | // Reads `len` bytes at data and writes the 32-byte SHA256 checksum to `output`. | |||
53 | static void sha256(const uint8_t *data, size_t len, uint8_t *output) { | |||
54 | #if defined(__APPLE__) | |||
55 | // FIXME: Make LLVM's SHA256 faster and use it unconditionally. See PR56121 | |||
56 | // for some notes on this. | |||
57 | CC_SHA256(data, len, output); | |||
58 | #else | |||
59 | ArrayRef<uint8_t> block(data, len); | |||
60 | std::array<uint8_t, 32> hash = SHA256::hash(block); | |||
61 | static_assert(hash.size() == CodeSignatureSection::hashSize); | |||
62 | memcpy(output, hash.data(), hash.size()); | |||
63 | #endif | |||
64 | } | |||
65 | ||||
66 | InStruct macho::in; | |||
67 | std::vector<SyntheticSection *> macho::syntheticSections; | |||
68 | ||||
69 | SyntheticSection::SyntheticSection(const char *segname, const char *name) | |||
70 | : OutputSection(SyntheticKind, name) { | |||
71 | std::tie(this->segname, this->name) = maybeRenameSection({segname, name}); | |||
72 | isec = makeSyntheticInputSection(segname, name); | |||
73 | isec->parent = this; | |||
74 | syntheticSections.push_back(this); | |||
75 | } | |||
76 | ||||
77 | // dyld3's MachOLoaded::getSlide() assumes that the __TEXT segment starts | |||
78 | // from the beginning of the file (i.e. the header). | |||
79 | MachHeaderSection::MachHeaderSection() | |||
80 | : SyntheticSection(segment_names::text, section_names::header) { | |||
81 | // XXX: This is a hack. (See D97007) | |||
82 | // Setting the index to 1 to pretend that this section is the text | |||
83 | // section. | |||
84 | index = 1; | |||
85 | isec->isFinal = true; | |||
86 | } | |||
87 | ||||
88 | void MachHeaderSection::addLoadCommand(LoadCommand *lc) { | |||
89 | loadCommands.push_back(lc); | |||
90 | sizeOfCmds += lc->getSize(); | |||
91 | } | |||
92 | ||||
93 | uint64_t MachHeaderSection::getSize() const { | |||
94 | uint64_t size = target->headerSize + sizeOfCmds + config->headerPad; | |||
95 | // If we are emitting an encryptable binary, our load commands must have a | |||
96 | // separate (non-encrypted) page to themselves. | |||
97 | if (config->emitEncryptionInfo) | |||
98 | size = alignTo(size, target->getPageSize()); | |||
99 | return size; | |||
100 | } | |||
101 | ||||
102 | static uint32_t cpuSubtype() { | |||
103 | uint32_t subtype = target->cpuSubtype; | |||
104 | ||||
105 | if (config->outputType == MH_EXECUTE && !config->staticLink && | |||
106 | target->cpuSubtype == CPU_SUBTYPE_X86_64_ALL && | |||
107 | config->platform() == PLATFORM_MACOS && | |||
108 | config->platformInfo.minimum >= VersionTuple(10, 5)) | |||
109 | subtype |= CPU_SUBTYPE_LIB64; | |||
110 | ||||
111 | return subtype; | |||
112 | } | |||
113 | ||||
114 | static bool hasWeakBinding() { | |||
115 | return config->emitChainedFixups ? in.chainedFixups->hasWeakBinding() | |||
116 | : in.weakBinding->hasEntry(); | |||
117 | } | |||
118 | ||||
119 | static bool hasNonWeakDefinition() { | |||
120 | return config->emitChainedFixups ? in.chainedFixups->hasNonWeakDefinition() | |||
121 | : in.weakBinding->hasNonWeakDefinition(); | |||
122 | } | |||
123 | ||||
124 | void MachHeaderSection::writeTo(uint8_t *buf) const { | |||
125 | auto *hdr = reinterpret_cast<mach_header *>(buf); | |||
126 | hdr->magic = target->magic; | |||
127 | hdr->cputype = target->cpuType; | |||
128 | hdr->cpusubtype = cpuSubtype(); | |||
129 | hdr->filetype = config->outputType; | |||
130 | hdr->ncmds = loadCommands.size(); | |||
131 | hdr->sizeofcmds = sizeOfCmds; | |||
132 | hdr->flags = MH_DYLDLINK; | |||
133 | ||||
134 | if (config->namespaceKind == NamespaceKind::twolevel) | |||
135 | hdr->flags |= MH_NOUNDEFS | MH_TWOLEVEL; | |||
136 | ||||
137 | if (config->outputType == MH_DYLIB && !config->hasReexports) | |||
138 | hdr->flags |= MH_NO_REEXPORTED_DYLIBS; | |||
139 | ||||
140 | if (config->markDeadStrippableDylib) | |||
141 | hdr->flags |= MH_DEAD_STRIPPABLE_DYLIB; | |||
142 | ||||
143 | if (config->outputType == MH_EXECUTE && config->isPic) | |||
144 | hdr->flags |= MH_PIE; | |||
145 | ||||
146 | if (config->outputType == MH_DYLIB && config->applicationExtension) | |||
147 | hdr->flags |= MH_APP_EXTENSION_SAFE; | |||
148 | ||||
149 | if (in.exports->hasWeakSymbol || hasNonWeakDefinition()) | |||
150 | hdr->flags |= MH_WEAK_DEFINES; | |||
151 | ||||
152 | if (in.exports->hasWeakSymbol || hasWeakBinding()) | |||
153 | hdr->flags |= MH_BINDS_TO_WEAK; | |||
154 | ||||
155 | for (const OutputSegment *seg : outputSegments) { | |||
156 | for (const OutputSection *osec : seg->getSections()) { | |||
157 | if (isThreadLocalVariables(osec->flags)) { | |||
158 | hdr->flags |= MH_HAS_TLV_DESCRIPTORS; | |||
159 | break; | |||
160 | } | |||
161 | } | |||
162 | } | |||
163 | ||||
164 | uint8_t *p = reinterpret_cast<uint8_t *>(hdr) + target->headerSize; | |||
165 | for (const LoadCommand *lc : loadCommands) { | |||
166 | lc->writeTo(p); | |||
167 | p += lc->getSize(); | |||
168 | } | |||
169 | } | |||
170 | ||||
171 | PageZeroSection::PageZeroSection() | |||
172 | : SyntheticSection(segment_names::pageZero, section_names::pageZero) {} | |||
173 | ||||
174 | RebaseSection::RebaseSection() | |||
175 | : LinkEditSection(segment_names::linkEdit, section_names::rebase) {} | |||
176 | ||||
177 | namespace { | |||
178 | struct RebaseState { | |||
179 | uint64_t sequenceLength; | |||
180 | uint64_t skipLength; | |||
181 | }; | |||
182 | } // namespace | |||
183 | ||||
184 | static void emitIncrement(uint64_t incr, raw_svector_ostream &os) { | |||
185 | assert(incr != 0)(static_cast <bool> (incr != 0) ? void (0) : __assert_fail ("incr != 0", "lld/MachO/SyntheticSections.cpp", 185, __extension__ __PRETTY_FUNCTION__)); | |||
186 | ||||
187 | if ((incr >> target->p2WordSize) <= REBASE_IMMEDIATE_MASK && | |||
188 | (incr % target->wordSize) == 0) { | |||
189 | os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_IMM_SCALED | | |||
190 | (incr >> target->p2WordSize)); | |||
191 | } else { | |||
192 | os << static_cast<uint8_t>(REBASE_OPCODE_ADD_ADDR_ULEB); | |||
193 | encodeULEB128(incr, os); | |||
194 | } | |||
195 | } | |||
196 | ||||
197 | static void flushRebase(const RebaseState &state, raw_svector_ostream &os) { | |||
198 | assert(state.sequenceLength > 0)(static_cast <bool> (state.sequenceLength > 0) ? void (0) : __assert_fail ("state.sequenceLength > 0", "lld/MachO/SyntheticSections.cpp" , 198, __extension__ __PRETTY_FUNCTION__)); | |||
199 | ||||
200 | if (state.skipLength == target->wordSize) { | |||
201 | if (state.sequenceLength <= REBASE_IMMEDIATE_MASK) { | |||
202 | os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_IMM_TIMES | | |||
203 | state.sequenceLength); | |||
204 | } else { | |||
205 | os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ULEB_TIMES); | |||
206 | encodeULEB128(state.sequenceLength, os); | |||
207 | } | |||
208 | } else if (state.sequenceLength == 1) { | |||
209 | os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB); | |||
210 | encodeULEB128(state.skipLength - target->wordSize, os); | |||
211 | } else { | |||
212 | os << static_cast<uint8_t>( | |||
213 | REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB); | |||
214 | encodeULEB128(state.sequenceLength, os); | |||
215 | encodeULEB128(state.skipLength - target->wordSize, os); | |||
216 | } | |||
217 | } | |||
218 | ||||
219 | // Rebases are communicated to dyld using a bytecode, whose opcodes cause the | |||
220 | // memory location at a specific address to be rebased and/or the address to be | |||
221 | // incremented. | |||
222 | // | |||
223 | // Opcode REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB is the most generic | |||
224 | // one, encoding a series of evenly spaced addresses. This algorithm works by | |||
225 | // splitting up the sorted list of addresses into such chunks. If the locations | |||
226 | // are consecutive or the sequence consists of a single location, flushRebase | |||
227 | // will use a smaller, more specialized encoding. | |||
228 | static void encodeRebases(const OutputSegment *seg, | |||
229 | MutableArrayRef<Location> locations, | |||
230 | raw_svector_ostream &os) { | |||
231 | // dyld operates on segments. Translate section offsets into segment offsets. | |||
232 | for (Location &loc : locations) | |||
233 | loc.offset = | |||
234 | loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset); | |||
235 | // The algorithm assumes that locations are unique. | |||
236 | Location *end = | |||
237 | llvm::unique(locations, [](const Location &a, const Location &b) { | |||
238 | return a.offset == b.offset; | |||
239 | }); | |||
240 | size_t count = end - locations.begin(); | |||
241 | ||||
242 | os << static_cast<uint8_t>(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | | |||
243 | seg->index); | |||
244 | assert(!locations.empty())(static_cast <bool> (!locations.empty()) ? void (0) : __assert_fail ("!locations.empty()", "lld/MachO/SyntheticSections.cpp", 244 , __extension__ __PRETTY_FUNCTION__)); | |||
245 | uint64_t offset = locations[0].offset; | |||
246 | encodeULEB128(offset, os); | |||
247 | ||||
248 | RebaseState state{1, target->wordSize}; | |||
249 | ||||
250 | for (size_t i = 1; i < count; ++i) { | |||
251 | offset = locations[i].offset; | |||
252 | ||||
253 | uint64_t skip = offset - locations[i - 1].offset; | |||
254 | assert(skip != 0 && "duplicate locations should have been weeded out")(static_cast <bool> (skip != 0 && "duplicate locations should have been weeded out" ) ? void (0) : __assert_fail ("skip != 0 && \"duplicate locations should have been weeded out\"" , "lld/MachO/SyntheticSections.cpp", 254, __extension__ __PRETTY_FUNCTION__ )); | |||
255 | ||||
256 | if (skip == state.skipLength) { | |||
257 | ++state.sequenceLength; | |||
258 | } else if (state.sequenceLength == 1) { | |||
259 | ++state.sequenceLength; | |||
260 | state.skipLength = skip; | |||
261 | } else if (skip < state.skipLength) { | |||
262 | // The address is lower than what the rebase pointer would be if the last | |||
263 | // location would be part of a sequence. We start a new sequence from the | |||
264 | // previous location. | |||
265 | --state.sequenceLength; | |||
266 | flushRebase(state, os); | |||
267 | ||||
268 | state.sequenceLength = 2; | |||
269 | state.skipLength = skip; | |||
270 | } else { | |||
271 | // The address is at some positive offset from the rebase pointer. We | |||
272 | // start a new sequence which begins with the current location. | |||
273 | flushRebase(state, os); | |||
274 | emitIncrement(skip - state.skipLength, os); | |||
275 | state.sequenceLength = 1; | |||
276 | state.skipLength = target->wordSize; | |||
277 | } | |||
278 | } | |||
279 | flushRebase(state, os); | |||
280 | } | |||
281 | ||||
282 | void RebaseSection::finalizeContents() { | |||
283 | if (locations.empty()) | |||
284 | return; | |||
285 | ||||
286 | raw_svector_ostream os{contents}; | |||
287 | os << static_cast<uint8_t>(REBASE_OPCODE_SET_TYPE_IMM | REBASE_TYPE_POINTER); | |||
288 | ||||
289 | llvm::sort(locations, [](const Location &a, const Location &b) { | |||
290 | return a.isec->getVA(a.offset) < b.isec->getVA(b.offset); | |||
291 | }); | |||
292 | ||||
293 | for (size_t i = 0, count = locations.size(); i < count;) { | |||
294 | const OutputSegment *seg = locations[i].isec->parent->parent; | |||
295 | size_t j = i + 1; | |||
296 | while (j < count && locations[j].isec->parent->parent == seg) | |||
297 | ++j; | |||
298 | encodeRebases(seg, {locations.data() + i, locations.data() + j}, os); | |||
299 | i = j; | |||
300 | } | |||
301 | os << static_cast<uint8_t>(REBASE_OPCODE_DONE); | |||
302 | } | |||
303 | ||||
304 | void RebaseSection::writeTo(uint8_t *buf) const { | |||
305 | memcpy(buf, contents.data(), contents.size()); | |||
306 | } | |||
307 | ||||
308 | NonLazyPointerSectionBase::NonLazyPointerSectionBase(const char *segname, | |||
309 | const char *name) | |||
310 | : SyntheticSection(segname, name) { | |||
311 | align = target->wordSize; | |||
312 | } | |||
313 | ||||
314 | void macho::addNonLazyBindingEntries(const Symbol *sym, | |||
315 | const InputSection *isec, uint64_t offset, | |||
316 | int64_t addend) { | |||
317 | if (config->emitChainedFixups) { | |||
318 | if (needsBinding(sym)) | |||
319 | in.chainedFixups->addBinding(sym, isec, offset, addend); | |||
320 | else if (isa<Defined>(sym)) | |||
321 | in.chainedFixups->addRebase(isec, offset); | |||
322 | else | |||
323 | llvm_unreachable("cannot bind to an undefined symbol")::llvm::llvm_unreachable_internal("cannot bind to an undefined symbol" , "lld/MachO/SyntheticSections.cpp", 323); | |||
324 | return; | |||
325 | } | |||
326 | ||||
327 | if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) { | |||
328 | in.binding->addEntry(dysym, isec, offset, addend); | |||
329 | if (dysym->isWeakDef()) | |||
330 | in.weakBinding->addEntry(sym, isec, offset, addend); | |||
331 | } else if (const auto *defined = dyn_cast<Defined>(sym)) { | |||
332 | in.rebase->addEntry(isec, offset); | |||
333 | if (defined->isExternalWeakDef()) | |||
334 | in.weakBinding->addEntry(sym, isec, offset, addend); | |||
335 | else if (defined->interposable) | |||
336 | in.binding->addEntry(sym, isec, offset, addend); | |||
337 | } else { | |||
338 | // Undefined symbols are filtered out in scanRelocations(); we should never | |||
339 | // get here | |||
340 | llvm_unreachable("cannot bind to an undefined symbol")::llvm::llvm_unreachable_internal("cannot bind to an undefined symbol" , "lld/MachO/SyntheticSections.cpp", 340); | |||
341 | } | |||
342 | } | |||
343 | ||||
344 | void NonLazyPointerSectionBase::addEntry(Symbol *sym) { | |||
345 | if (entries.insert(sym)) { | |||
346 | assert(!sym->isInGot())(static_cast <bool> (!sym->isInGot()) ? void (0) : __assert_fail ("!sym->isInGot()", "lld/MachO/SyntheticSections.cpp", 346 , __extension__ __PRETTY_FUNCTION__)); | |||
347 | sym->gotIndex = entries.size() - 1; | |||
348 | ||||
349 | addNonLazyBindingEntries(sym, isec, sym->gotIndex * target->wordSize); | |||
350 | } | |||
351 | } | |||
352 | ||||
353 | void macho::writeChainedRebase(uint8_t *buf, uint64_t targetVA) { | |||
354 | assert(config->emitChainedFixups)(static_cast <bool> (config->emitChainedFixups) ? void (0) : __assert_fail ("config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 354, __extension__ __PRETTY_FUNCTION__)); | |||
355 | assert(target->wordSize == 8 && "Only 64-bit platforms are supported")(static_cast <bool> (target->wordSize == 8 && "Only 64-bit platforms are supported") ? void (0) : __assert_fail ("target->wordSize == 8 && \"Only 64-bit platforms are supported\"" , "lld/MachO/SyntheticSections.cpp", 355, __extension__ __PRETTY_FUNCTION__ )); | |||
356 | auto *rebase = reinterpret_cast<dyld_chained_ptr_64_rebase *>(buf); | |||
357 | rebase->target = targetVA & 0xf'ffff'ffff; | |||
358 | rebase->high8 = (targetVA >> 56); | |||
359 | rebase->reserved = 0; | |||
360 | rebase->next = 0; | |||
361 | rebase->bind = 0; | |||
362 | ||||
363 | // The fixup format places a 64 GiB limit on the output's size. | |||
364 | // Should we handle this gracefully? | |||
365 | uint64_t encodedVA = rebase->target | ((uint64_t)rebase->high8 << 56); | |||
366 | if (encodedVA != targetVA) | |||
367 | error("rebase target address 0x" + Twine::utohexstr(targetVA) + | |||
368 | " does not fit into chained fixup. Re-link with -no_fixup_chains"); | |||
369 | } | |||
370 | ||||
371 | static void writeChainedBind(uint8_t *buf, const Symbol *sym, int64_t addend) { | |||
372 | assert(config->emitChainedFixups)(static_cast <bool> (config->emitChainedFixups) ? void (0) : __assert_fail ("config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 372, __extension__ __PRETTY_FUNCTION__)); | |||
373 | assert(target->wordSize == 8 && "Only 64-bit platforms are supported")(static_cast <bool> (target->wordSize == 8 && "Only 64-bit platforms are supported") ? void (0) : __assert_fail ("target->wordSize == 8 && \"Only 64-bit platforms are supported\"" , "lld/MachO/SyntheticSections.cpp", 373, __extension__ __PRETTY_FUNCTION__ )); | |||
374 | auto *bind = reinterpret_cast<dyld_chained_ptr_64_bind *>(buf); | |||
375 | auto [ordinal, inlineAddend] = in.chainedFixups->getBinding(sym, addend); | |||
376 | bind->ordinal = ordinal; | |||
377 | bind->addend = inlineAddend; | |||
378 | bind->reserved = 0; | |||
379 | bind->next = 0; | |||
380 | bind->bind = 1; | |||
381 | } | |||
382 | ||||
383 | void macho::writeChainedFixup(uint8_t *buf, const Symbol *sym, int64_t addend) { | |||
384 | if (needsBinding(sym)) | |||
385 | writeChainedBind(buf, sym, addend); | |||
386 | else | |||
387 | writeChainedRebase(buf, sym->getVA() + addend); | |||
388 | } | |||
389 | ||||
390 | void NonLazyPointerSectionBase::writeTo(uint8_t *buf) const { | |||
391 | if (config->emitChainedFixups) { | |||
392 | for (const auto &[i, entry] : llvm::enumerate(entries)) | |||
393 | writeChainedFixup(&buf[i * target->wordSize], entry, 0); | |||
394 | } else { | |||
395 | for (const auto &[i, entry] : llvm::enumerate(entries)) | |||
396 | if (auto *defined = dyn_cast<Defined>(entry)) | |||
397 | write64le(&buf[i * target->wordSize], defined->getVA()); | |||
398 | } | |||
399 | } | |||
400 | ||||
401 | GotSection::GotSection() | |||
402 | : NonLazyPointerSectionBase(segment_names::data, section_names::got) { | |||
403 | flags = S_NON_LAZY_SYMBOL_POINTERS; | |||
404 | } | |||
405 | ||||
406 | TlvPointerSection::TlvPointerSection() | |||
407 | : NonLazyPointerSectionBase(segment_names::data, | |||
408 | section_names::threadPtrs) { | |||
409 | flags = S_THREAD_LOCAL_VARIABLE_POINTERS; | |||
410 | } | |||
411 | ||||
412 | BindingSection::BindingSection() | |||
413 | : LinkEditSection(segment_names::linkEdit, section_names::binding) {} | |||
414 | ||||
415 | namespace { | |||
416 | struct Binding { | |||
417 | OutputSegment *segment = nullptr; | |||
418 | uint64_t offset = 0; | |||
419 | int64_t addend = 0; | |||
420 | }; | |||
421 | struct BindIR { | |||
422 | // Default value of 0xF0 is not valid opcode and should make the program | |||
423 | // scream instead of accidentally writing "valid" values. | |||
424 | uint8_t opcode = 0xF0; | |||
425 | uint64_t data = 0; | |||
426 | uint64_t consecutiveCount = 0; | |||
427 | }; | |||
428 | } // namespace | |||
429 | ||||
430 | // Encode a sequence of opcodes that tell dyld to write the address of symbol + | |||
431 | // addend at osec->addr + outSecOff. | |||
432 | // | |||
433 | // The bind opcode "interpreter" remembers the values of each binding field, so | |||
434 | // we only need to encode the differences between bindings. Hence the use of | |||
435 | // lastBinding. | |||
436 | static void encodeBinding(const OutputSection *osec, uint64_t outSecOff, | |||
437 | int64_t addend, Binding &lastBinding, | |||
438 | std::vector<BindIR> &opcodes) { | |||
439 | OutputSegment *seg = osec->parent; | |||
440 | uint64_t offset = osec->getSegmentOffset() + outSecOff; | |||
441 | if (lastBinding.segment != seg) { | |||
442 | opcodes.push_back( | |||
443 | {static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | | |||
444 | seg->index), | |||
445 | offset}); | |||
446 | lastBinding.segment = seg; | |||
447 | lastBinding.offset = offset; | |||
448 | } else if (lastBinding.offset != offset) { | |||
449 | opcodes.push_back({BIND_OPCODE_ADD_ADDR_ULEB, offset - lastBinding.offset}); | |||
450 | lastBinding.offset = offset; | |||
451 | } | |||
452 | ||||
453 | if (lastBinding.addend != addend) { | |||
454 | opcodes.push_back( | |||
455 | {BIND_OPCODE_SET_ADDEND_SLEB, static_cast<uint64_t>(addend)}); | |||
456 | lastBinding.addend = addend; | |||
457 | } | |||
458 | ||||
459 | opcodes.push_back({BIND_OPCODE_DO_BIND, 0}); | |||
460 | // DO_BIND causes dyld to both perform the binding and increment the offset | |||
461 | lastBinding.offset += target->wordSize; | |||
462 | } | |||
463 | ||||
464 | static void optimizeOpcodes(std::vector<BindIR> &opcodes) { | |||
465 | // Pass 1: Combine bind/add pairs | |||
466 | size_t i; | |||
467 | int pWrite = 0; | |||
468 | for (i = 1; i < opcodes.size(); ++i, ++pWrite) { | |||
469 | if ((opcodes[i].opcode == BIND_OPCODE_ADD_ADDR_ULEB) && | |||
470 | (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND)) { | |||
471 | opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB; | |||
472 | opcodes[pWrite].data = opcodes[i].data; | |||
473 | ++i; | |||
474 | } else { | |||
475 | opcodes[pWrite] = opcodes[i - 1]; | |||
476 | } | |||
477 | } | |||
478 | if (i == opcodes.size()) | |||
479 | opcodes[pWrite] = opcodes[i - 1]; | |||
480 | opcodes.resize(pWrite + 1); | |||
481 | ||||
482 | // Pass 2: Compress two or more bind_add opcodes | |||
483 | pWrite = 0; | |||
484 | for (i = 1; i < opcodes.size(); ++i, ++pWrite) { | |||
485 | if ((opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) && | |||
486 | (opcodes[i - 1].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) && | |||
487 | (opcodes[i].data == opcodes[i - 1].data)) { | |||
488 | opcodes[pWrite].opcode = BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB; | |||
489 | opcodes[pWrite].consecutiveCount = 2; | |||
490 | opcodes[pWrite].data = opcodes[i].data; | |||
491 | ++i; | |||
492 | while (i < opcodes.size() && | |||
493 | (opcodes[i].opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) && | |||
494 | (opcodes[i].data == opcodes[i - 1].data)) { | |||
495 | opcodes[pWrite].consecutiveCount++; | |||
496 | ++i; | |||
497 | } | |||
498 | } else { | |||
499 | opcodes[pWrite] = opcodes[i - 1]; | |||
500 | } | |||
501 | } | |||
502 | if (i == opcodes.size()) | |||
503 | opcodes[pWrite] = opcodes[i - 1]; | |||
504 | opcodes.resize(pWrite + 1); | |||
505 | ||||
506 | // Pass 3: Use immediate encodings | |||
507 | // Every binding is the size of one pointer. If the next binding is a | |||
508 | // multiple of wordSize away that is within BIND_IMMEDIATE_MASK, the | |||
509 | // opcode can be scaled by wordSize into a single byte and dyld will | |||
510 | // expand it to the correct address. | |||
511 | for (auto &p : opcodes) { | |||
512 | // It's unclear why the check needs to be less than BIND_IMMEDIATE_MASK, | |||
513 | // but ld64 currently does this. This could be a potential bug, but | |||
514 | // for now, perform the same behavior to prevent mysterious bugs. | |||
515 | if ((p.opcode == BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB) && | |||
516 | ((p.data / target->wordSize) < BIND_IMMEDIATE_MASK) && | |||
517 | ((p.data % target->wordSize) == 0)) { | |||
518 | p.opcode = BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED; | |||
519 | p.data /= target->wordSize; | |||
520 | } | |||
521 | } | |||
522 | } | |||
523 | ||||
524 | static void flushOpcodes(const BindIR &op, raw_svector_ostream &os) { | |||
525 | uint8_t opcode = op.opcode & BIND_OPCODE_MASK; | |||
526 | switch (opcode) { | |||
527 | case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: | |||
528 | case BIND_OPCODE_ADD_ADDR_ULEB: | |||
529 | case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: | |||
530 | os << op.opcode; | |||
531 | encodeULEB128(op.data, os); | |||
532 | break; | |||
533 | case BIND_OPCODE_SET_ADDEND_SLEB: | |||
534 | os << op.opcode; | |||
535 | encodeSLEB128(static_cast<int64_t>(op.data), os); | |||
536 | break; | |||
537 | case BIND_OPCODE_DO_BIND: | |||
538 | os << op.opcode; | |||
539 | break; | |||
540 | case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: | |||
541 | os << op.opcode; | |||
542 | encodeULEB128(op.consecutiveCount, os); | |||
543 | encodeULEB128(op.data, os); | |||
544 | break; | |||
545 | case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: | |||
546 | os << static_cast<uint8_t>(op.opcode | op.data); | |||
547 | break; | |||
548 | default: | |||
549 | llvm_unreachable("cannot bind to an unrecognized symbol")::llvm::llvm_unreachable_internal("cannot bind to an unrecognized symbol" , "lld/MachO/SyntheticSections.cpp", 549); | |||
550 | } | |||
551 | } | |||
552 | ||||
553 | // Non-weak bindings need to have their dylib ordinal encoded as well. | |||
554 | static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) { | |||
555 | if (config->namespaceKind == NamespaceKind::flat || dysym.isDynamicLookup()) | |||
556 | return static_cast<int16_t>(BIND_SPECIAL_DYLIB_FLAT_LOOKUP); | |||
557 | assert(dysym.getFile()->isReferenced())(static_cast <bool> (dysym.getFile()->isReferenced() ) ? void (0) : __assert_fail ("dysym.getFile()->isReferenced()" , "lld/MachO/SyntheticSections.cpp", 557, __extension__ __PRETTY_FUNCTION__ )); | |||
558 | return dysym.getFile()->ordinal; | |||
559 | } | |||
560 | ||||
561 | static int16_t ordinalForSymbol(const Symbol &sym) { | |||
562 | if (const auto *dysym = dyn_cast<DylibSymbol>(&sym)) | |||
563 | return ordinalForDylibSymbol(*dysym); | |||
564 | assert(cast<Defined>(&sym)->interposable)(static_cast <bool> (cast<Defined>(&sym)-> interposable) ? void (0) : __assert_fail ("cast<Defined>(&sym)->interposable" , "lld/MachO/SyntheticSections.cpp", 564, __extension__ __PRETTY_FUNCTION__ )); | |||
565 | return BIND_SPECIAL_DYLIB_FLAT_LOOKUP; | |||
566 | } | |||
567 | ||||
568 | static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) { | |||
569 | if (ordinal <= 0) { | |||
570 | os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | | |||
571 | (ordinal & BIND_IMMEDIATE_MASK)); | |||
572 | } else if (ordinal <= BIND_IMMEDIATE_MASK) { | |||
573 | os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | ordinal); | |||
574 | } else { | |||
575 | os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB); | |||
576 | encodeULEB128(ordinal, os); | |||
577 | } | |||
578 | } | |||
579 | ||||
580 | static void encodeWeakOverride(const Defined *defined, | |||
581 | raw_svector_ostream &os) { | |||
582 | os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | | |||
583 | BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) | |||
584 | << defined->getName() << '\0'; | |||
585 | } | |||
586 | ||||
587 | // Organize the bindings so we can encoded them with fewer opcodes. | |||
588 | // | |||
589 | // First, all bindings for a given symbol should be grouped together. | |||
590 | // BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM is the largest opcode (since it | |||
591 | // has an associated symbol string), so we only want to emit it once per symbol. | |||
592 | // | |||
593 | // Within each group, we sort the bindings by address. Since bindings are | |||
594 | // delta-encoded, sorting them allows for a more compact result. Note that | |||
595 | // sorting by address alone ensures that bindings for the same segment / section | |||
596 | // are located together, minimizing the number of times we have to emit | |||
597 | // BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB. | |||
598 | // | |||
599 | // Finally, we sort the symbols by the address of their first binding, again | |||
600 | // to facilitate the delta-encoding process. | |||
601 | template <class Sym> | |||
602 | std::vector<std::pair<const Sym *, std::vector<BindingEntry>>> | |||
603 | sortBindings(const BindingsMap<const Sym *> &bindingsMap) { | |||
604 | std::vector<std::pair<const Sym *, std::vector<BindingEntry>>> bindingsVec( | |||
605 | bindingsMap.begin(), bindingsMap.end()); | |||
606 | for (auto &p : bindingsVec) { | |||
607 | std::vector<BindingEntry> &bindings = p.second; | |||
608 | llvm::sort(bindings, [](const BindingEntry &a, const BindingEntry &b) { | |||
609 | return a.target.getVA() < b.target.getVA(); | |||
610 | }); | |||
611 | } | |||
612 | llvm::sort(bindingsVec, [](const auto &a, const auto &b) { | |||
613 | return a.second[0].target.getVA() < b.second[0].target.getVA(); | |||
614 | }); | |||
615 | return bindingsVec; | |||
616 | } | |||
617 | ||||
618 | // Emit bind opcodes, which are a stream of byte-sized opcodes that dyld | |||
619 | // interprets to update a record with the following fields: | |||
620 | // * segment index (of the segment to write the symbol addresses to, typically | |||
621 | // the __DATA_CONST segment which contains the GOT) | |||
622 | // * offset within the segment, indicating the next location to write a binding | |||
623 | // * symbol type | |||
624 | // * symbol library ordinal (the index of its library's LC_LOAD_DYLIB command) | |||
625 | // * symbol name | |||
626 | // * addend | |||
627 | // When dyld sees BIND_OPCODE_DO_BIND, it uses the current record state to bind | |||
628 | // a symbol in the GOT, and increments the segment offset to point to the next | |||
629 | // entry. It does *not* clear the record state after doing the bind, so | |||
630 | // subsequent opcodes only need to encode the differences between bindings. | |||
631 | void BindingSection::finalizeContents() { | |||
632 | raw_svector_ostream os{contents}; | |||
633 | Binding lastBinding; | |||
634 | int16_t lastOrdinal = 0; | |||
635 | ||||
636 | for (auto &p : sortBindings(bindingsMap)) { | |||
637 | const Symbol *sym = p.first; | |||
638 | std::vector<BindingEntry> &bindings = p.second; | |||
639 | uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM; | |||
640 | if (sym->isWeakRef()) | |||
641 | flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT; | |||
642 | os << flags << sym->getName() << '\0' | |||
643 | << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER); | |||
644 | int16_t ordinal = ordinalForSymbol(*sym); | |||
645 | if (ordinal != lastOrdinal) { | |||
646 | encodeDylibOrdinal(ordinal, os); | |||
647 | lastOrdinal = ordinal; | |||
648 | } | |||
649 | std::vector<BindIR> opcodes; | |||
650 | for (const BindingEntry &b : bindings) | |||
651 | encodeBinding(b.target.isec->parent, | |||
652 | b.target.isec->getOffset(b.target.offset), b.addend, | |||
653 | lastBinding, opcodes); | |||
654 | if (config->optimize > 1) | |||
655 | optimizeOpcodes(opcodes); | |||
656 | for (const auto &op : opcodes) | |||
657 | flushOpcodes(op, os); | |||
658 | } | |||
659 | if (!bindingsMap.empty()) | |||
660 | os << static_cast<uint8_t>(BIND_OPCODE_DONE); | |||
661 | } | |||
662 | ||||
663 | void BindingSection::writeTo(uint8_t *buf) const { | |||
664 | memcpy(buf, contents.data(), contents.size()); | |||
665 | } | |||
666 | ||||
667 | WeakBindingSection::WeakBindingSection() | |||
668 | : LinkEditSection(segment_names::linkEdit, section_names::weakBinding) {} | |||
669 | ||||
670 | void WeakBindingSection::finalizeContents() { | |||
671 | raw_svector_ostream os{contents}; | |||
672 | Binding lastBinding; | |||
673 | ||||
674 | for (const Defined *defined : definitions) | |||
675 | encodeWeakOverride(defined, os); | |||
676 | ||||
677 | for (auto &p : sortBindings(bindingsMap)) { | |||
678 | const Symbol *sym = p.first; | |||
679 | std::vector<BindingEntry> &bindings = p.second; | |||
680 | os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM) | |||
681 | << sym->getName() << '\0' | |||
682 | << static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER); | |||
683 | std::vector<BindIR> opcodes; | |||
684 | for (const BindingEntry &b : bindings) | |||
685 | encodeBinding(b.target.isec->parent, | |||
686 | b.target.isec->getOffset(b.target.offset), b.addend, | |||
687 | lastBinding, opcodes); | |||
688 | if (config->optimize > 1) | |||
689 | optimizeOpcodes(opcodes); | |||
690 | for (const auto &op : opcodes) | |||
691 | flushOpcodes(op, os); | |||
692 | } | |||
693 | if (!bindingsMap.empty() || !definitions.empty()) | |||
694 | os << static_cast<uint8_t>(BIND_OPCODE_DONE); | |||
695 | } | |||
696 | ||||
697 | void WeakBindingSection::writeTo(uint8_t *buf) const { | |||
698 | memcpy(buf, contents.data(), contents.size()); | |||
699 | } | |||
700 | ||||
701 | StubsSection::StubsSection() | |||
702 | : SyntheticSection(segment_names::text, section_names::stubs) { | |||
703 | flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS; | |||
704 | // The stubs section comprises machine instructions, which are aligned to | |||
705 | // 4 bytes on the archs we care about. | |||
706 | align = 4; | |||
707 | reserved2 = target->stubSize; | |||
708 | } | |||
709 | ||||
710 | uint64_t StubsSection::getSize() const { | |||
711 | return entries.size() * target->stubSize; | |||
712 | } | |||
713 | ||||
714 | void StubsSection::writeTo(uint8_t *buf) const { | |||
715 | size_t off = 0; | |||
716 | for (const Symbol *sym : entries) { | |||
717 | uint64_t pointerVA = | |||
718 | config->emitChainedFixups ? sym->getGotVA() : sym->getLazyPtrVA(); | |||
719 | target->writeStub(buf + off, *sym, pointerVA); | |||
720 | off += target->stubSize; | |||
721 | } | |||
722 | } | |||
723 | ||||
724 | void StubsSection::finalize() { isFinal = true; } | |||
725 | ||||
726 | static void addBindingsForStub(Symbol *sym) { | |||
727 | assert(!config->emitChainedFixups)(static_cast <bool> (!config->emitChainedFixups) ? void (0) : __assert_fail ("!config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 727, __extension__ __PRETTY_FUNCTION__)); | |||
728 | if (auto *dysym = dyn_cast<DylibSymbol>(sym)) { | |||
729 | if (sym->isWeakDef()) { | |||
730 | in.binding->addEntry(dysym, in.lazyPointers->isec, | |||
731 | sym->stubsIndex * target->wordSize); | |||
732 | in.weakBinding->addEntry(sym, in.lazyPointers->isec, | |||
733 | sym->stubsIndex * target->wordSize); | |||
734 | } else { | |||
735 | in.lazyBinding->addEntry(dysym); | |||
736 | } | |||
737 | } else if (auto *defined = dyn_cast<Defined>(sym)) { | |||
738 | if (defined->isExternalWeakDef()) { | |||
739 | in.rebase->addEntry(in.lazyPointers->isec, | |||
740 | sym->stubsIndex * target->wordSize); | |||
741 | in.weakBinding->addEntry(sym, in.lazyPointers->isec, | |||
742 | sym->stubsIndex * target->wordSize); | |||
743 | } else if (defined->interposable) { | |||
744 | in.lazyBinding->addEntry(sym); | |||
745 | } else { | |||
746 | llvm_unreachable("invalid stub target")::llvm::llvm_unreachable_internal("invalid stub target", "lld/MachO/SyntheticSections.cpp" , 746); | |||
747 | } | |||
748 | } else { | |||
749 | llvm_unreachable("invalid stub target symbol type")::llvm::llvm_unreachable_internal("invalid stub target symbol type" , "lld/MachO/SyntheticSections.cpp", 749); | |||
750 | } | |||
751 | } | |||
752 | ||||
753 | void StubsSection::addEntry(Symbol *sym) { | |||
754 | bool inserted = entries.insert(sym); | |||
755 | if (inserted) { | |||
756 | sym->stubsIndex = entries.size() - 1; | |||
757 | ||||
758 | if (config->emitChainedFixups) | |||
759 | in.got->addEntry(sym); | |||
760 | else | |||
761 | addBindingsForStub(sym); | |||
762 | } | |||
763 | } | |||
764 | ||||
765 | StubHelperSection::StubHelperSection() | |||
766 | : SyntheticSection(segment_names::text, section_names::stubHelper) { | |||
767 | flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS; | |||
768 | align = 4; // This section comprises machine instructions | |||
769 | } | |||
770 | ||||
771 | uint64_t StubHelperSection::getSize() const { | |||
772 | return target->stubHelperHeaderSize + | |||
773 | in.lazyBinding->getEntries().size() * target->stubHelperEntrySize; | |||
774 | } | |||
775 | ||||
776 | bool StubHelperSection::isNeeded() const { return in.lazyBinding->isNeeded(); } | |||
777 | ||||
778 | void StubHelperSection::writeTo(uint8_t *buf) const { | |||
779 | target->writeStubHelperHeader(buf); | |||
780 | size_t off = target->stubHelperHeaderSize; | |||
781 | for (const Symbol *sym : in.lazyBinding->getEntries()) { | |||
782 | target->writeStubHelperEntry(buf + off, *sym, addr + off); | |||
783 | off += target->stubHelperEntrySize; | |||
784 | } | |||
785 | } | |||
786 | ||||
787 | void StubHelperSection::setUp() { | |||
788 | Symbol *binder = symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr, | |||
789 | /*isWeakRef=*/false); | |||
790 | if (auto *undefined = dyn_cast<Undefined>(binder)) | |||
791 | treatUndefinedSymbol(*undefined, | |||
792 | "lazy binding (normally in libSystem.dylib)"); | |||
793 | ||||
794 | // treatUndefinedSymbol() can replace binder with a DylibSymbol; re-check. | |||
795 | stubBinder = dyn_cast_or_null<DylibSymbol>(binder); | |||
796 | if (stubBinder == nullptr) | |||
797 | return; | |||
798 | ||||
799 | in.got->addEntry(stubBinder); | |||
800 | ||||
801 | in.imageLoaderCache->parent = | |||
802 | ConcatOutputSection::getOrCreateForInput(in.imageLoaderCache); | |||
803 | inputSections.push_back(in.imageLoaderCache); | |||
804 | // Since this isn't in the symbol table or in any input file, the noDeadStrip | |||
805 | // argument doesn't matter. | |||
806 | dyldPrivate = | |||
807 | make<Defined>("__dyld_private", nullptr, in.imageLoaderCache, 0, 0, | |||
808 | /*isWeakDef=*/false, | |||
809 | /*isExternal=*/false, /*isPrivateExtern=*/false, | |||
810 | /*includeInSymtab=*/true, | |||
811 | /*isThumb=*/false, /*isReferencedDynamically=*/false, | |||
812 | /*noDeadStrip=*/false); | |||
813 | dyldPrivate->used = true; | |||
814 | } | |||
815 | ||||
816 | ObjCStubsSection::ObjCStubsSection() | |||
817 | : SyntheticSection(segment_names::text, section_names::objcStubs) { | |||
818 | flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS; | |||
819 | align = target->objcStubsAlignment; | |||
820 | } | |||
821 | ||||
822 | void ObjCStubsSection::addEntry(Symbol *sym) { | |||
823 | assert(sym->getName().startswith(symbolPrefix) && "not an objc stub")(static_cast <bool> (sym->getName().startswith(symbolPrefix ) && "not an objc stub") ? void (0) : __assert_fail ( "sym->getName().startswith(symbolPrefix) && \"not an objc stub\"" , "lld/MachO/SyntheticSections.cpp", 823, __extension__ __PRETTY_FUNCTION__ )); | |||
824 | StringRef methname = sym->getName().drop_front(symbolPrefix.size()); | |||
825 | offsets.push_back( | |||
826 | in.objcMethnameSection->getStringOffset(methname).outSecOff); | |||
827 | Defined *newSym = replaceSymbol<Defined>( | |||
828 | sym, sym->getName(), nullptr, isec, | |||
829 | /*value=*/symbols.size() * target->objcStubsFastSize, | |||
830 | /*size=*/target->objcStubsFastSize, | |||
831 | /*isWeakDef=*/false, /*isExternal=*/true, /*isPrivateExtern=*/true, | |||
832 | /*includeInSymtab=*/true, /*isThumb=*/false, | |||
833 | /*isReferencedDynamically=*/false, /*noDeadStrip=*/false); | |||
834 | symbols.push_back(newSym); | |||
835 | } | |||
836 | ||||
837 | void ObjCStubsSection::setUp() { | |||
838 | Symbol *objcMsgSend = symtab->addUndefined("_objc_msgSend", /*file=*/nullptr, | |||
839 | /*isWeakRef=*/false); | |||
840 | objcMsgSend->used = true; | |||
841 | in.got->addEntry(objcMsgSend); | |||
842 | assert(objcMsgSend->isInGot())(static_cast <bool> (objcMsgSend->isInGot()) ? void ( 0) : __assert_fail ("objcMsgSend->isInGot()", "lld/MachO/SyntheticSections.cpp" , 842, __extension__ __PRETTY_FUNCTION__)); | |||
843 | objcMsgSendGotIndex = objcMsgSend->gotIndex; | |||
844 | ||||
845 | size_t size = offsets.size() * target->wordSize; | |||
846 | uint8_t *selrefsData = bAlloc().Allocate<uint8_t>(size); | |||
847 | for (size_t i = 0, n = offsets.size(); i < n; ++i) | |||
848 | write64le(&selrefsData[i * target->wordSize], offsets[i]); | |||
849 | ||||
850 | in.objcSelrefs = | |||
851 | makeSyntheticInputSection(segment_names::data, section_names::objcSelrefs, | |||
852 | S_LITERAL_POINTERS | S_ATTR_NO_DEAD_STRIP, | |||
853 | ArrayRef<uint8_t>{selrefsData, size}, | |||
854 | /*align=*/target->wordSize); | |||
855 | in.objcSelrefs->live = true; | |||
856 | ||||
857 | for (size_t i = 0, n = offsets.size(); i < n; ++i) { | |||
858 | in.objcSelrefs->relocs.push_back( | |||
859 | {/*type=*/target->unsignedRelocType, | |||
860 | /*pcrel=*/false, /*length=*/3, | |||
861 | /*offset=*/static_cast<uint32_t>(i * target->wordSize), | |||
862 | /*addend=*/offsets[i] * in.objcMethnameSection->align, | |||
863 | /*referent=*/in.objcMethnameSection->isec}); | |||
864 | } | |||
865 | ||||
866 | in.objcSelrefs->parent = | |||
867 | ConcatOutputSection::getOrCreateForInput(in.objcSelrefs); | |||
868 | inputSections.push_back(in.objcSelrefs); | |||
869 | in.objcSelrefs->isFinal = true; | |||
870 | } | |||
871 | ||||
872 | uint64_t ObjCStubsSection::getSize() const { | |||
873 | return target->objcStubsFastSize * symbols.size(); | |||
874 | } | |||
875 | ||||
876 | void ObjCStubsSection::writeTo(uint8_t *buf) const { | |||
877 | assert(in.objcSelrefs->live)(static_cast <bool> (in.objcSelrefs->live) ? void (0 ) : __assert_fail ("in.objcSelrefs->live", "lld/MachO/SyntheticSections.cpp" , 877, __extension__ __PRETTY_FUNCTION__)); | |||
878 | assert(in.objcSelrefs->isFinal)(static_cast <bool> (in.objcSelrefs->isFinal) ? void (0) : __assert_fail ("in.objcSelrefs->isFinal", "lld/MachO/SyntheticSections.cpp" , 878, __extension__ __PRETTY_FUNCTION__)); | |||
879 | ||||
880 | uint64_t stubOffset = 0; | |||
881 | for (size_t i = 0, n = symbols.size(); i < n; ++i) { | |||
882 | Defined *sym = symbols[i]; | |||
883 | target->writeObjCMsgSendStub(buf + stubOffset, sym, in.objcStubs->addr, | |||
884 | stubOffset, in.objcSelrefs->getVA(), i, | |||
885 | in.got->addr, objcMsgSendGotIndex); | |||
886 | stubOffset += target->objcStubsFastSize; | |||
887 | } | |||
888 | } | |||
889 | ||||
890 | LazyPointerSection::LazyPointerSection() | |||
891 | : SyntheticSection(segment_names::data, section_names::lazySymbolPtr) { | |||
892 | align = target->wordSize; | |||
893 | flags = S_LAZY_SYMBOL_POINTERS; | |||
894 | } | |||
895 | ||||
896 | uint64_t LazyPointerSection::getSize() const { | |||
897 | return in.stubs->getEntries().size() * target->wordSize; | |||
898 | } | |||
899 | ||||
900 | bool LazyPointerSection::isNeeded() const { | |||
901 | return !in.stubs->getEntries().empty(); | |||
902 | } | |||
903 | ||||
904 | void LazyPointerSection::writeTo(uint8_t *buf) const { | |||
905 | size_t off = 0; | |||
906 | for (const Symbol *sym : in.stubs->getEntries()) { | |||
907 | if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) { | |||
908 | if (dysym->hasStubsHelper()) { | |||
909 | uint64_t stubHelperOffset = | |||
910 | target->stubHelperHeaderSize + | |||
911 | dysym->stubsHelperIndex * target->stubHelperEntrySize; | |||
912 | write64le(buf + off, in.stubHelper->addr + stubHelperOffset); | |||
913 | } | |||
914 | } else { | |||
915 | write64le(buf + off, sym->getVA()); | |||
916 | } | |||
917 | off += target->wordSize; | |||
918 | } | |||
919 | } | |||
920 | ||||
921 | LazyBindingSection::LazyBindingSection() | |||
922 | : LinkEditSection(segment_names::linkEdit, section_names::lazyBinding) {} | |||
923 | ||||
924 | void LazyBindingSection::finalizeContents() { | |||
925 | // TODO: Just precompute output size here instead of writing to a temporary | |||
926 | // buffer | |||
927 | for (Symbol *sym : entries) | |||
928 | sym->lazyBindOffset = encode(*sym); | |||
929 | } | |||
930 | ||||
931 | void LazyBindingSection::writeTo(uint8_t *buf) const { | |||
932 | memcpy(buf, contents.data(), contents.size()); | |||
933 | } | |||
934 | ||||
935 | void LazyBindingSection::addEntry(Symbol *sym) { | |||
936 | assert(!config->emitChainedFixups && "Chained fixups always bind eagerly")(static_cast <bool> (!config->emitChainedFixups && "Chained fixups always bind eagerly") ? void (0) : __assert_fail ("!config->emitChainedFixups && \"Chained fixups always bind eagerly\"" , "lld/MachO/SyntheticSections.cpp", 936, __extension__ __PRETTY_FUNCTION__ )); | |||
937 | if (entries.insert(sym)) { | |||
938 | sym->stubsHelperIndex = entries.size() - 1; | |||
939 | in.rebase->addEntry(in.lazyPointers->isec, | |||
940 | sym->stubsIndex * target->wordSize); | |||
941 | } | |||
942 | } | |||
943 | ||||
944 | // Unlike the non-lazy binding section, the bind opcodes in this section aren't | |||
945 | // interpreted all at once. Rather, dyld will start interpreting opcodes at a | |||
946 | // given offset, typically only binding a single symbol before it finds a | |||
947 | // BIND_OPCODE_DONE terminator. As such, unlike in the non-lazy-binding case, | |||
948 | // we cannot encode just the differences between symbols; we have to emit the | |||
949 | // complete bind information for each symbol. | |||
950 | uint32_t LazyBindingSection::encode(const Symbol &sym) { | |||
951 | uint32_t opstreamOffset = contents.size(); | |||
952 | OutputSegment *dataSeg = in.lazyPointers->parent; | |||
953 | os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | | |||
954 | dataSeg->index); | |||
955 | uint64_t offset = | |||
956 | in.lazyPointers->addr - dataSeg->addr + sym.stubsIndex * target->wordSize; | |||
957 | encodeULEB128(offset, os); | |||
958 | encodeDylibOrdinal(ordinalForSymbol(sym), os); | |||
959 | ||||
960 | uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM; | |||
961 | if (sym.isWeakRef()) | |||
962 | flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT; | |||
963 | ||||
964 | os << flags << sym.getName() << '\0' | |||
965 | << static_cast<uint8_t>(BIND_OPCODE_DO_BIND) | |||
966 | << static_cast<uint8_t>(BIND_OPCODE_DONE); | |||
967 | return opstreamOffset; | |||
968 | } | |||
969 | ||||
970 | ExportSection::ExportSection() | |||
971 | : LinkEditSection(segment_names::linkEdit, section_names::export_) {} | |||
972 | ||||
973 | void ExportSection::finalizeContents() { | |||
974 | trieBuilder.setImageBase(in.header->addr); | |||
975 | for (const Symbol *sym : symtab->getSymbols()) { | |||
976 | if (const auto *defined = dyn_cast<Defined>(sym)) { | |||
977 | if (defined->privateExtern || !defined->isLive()) | |||
978 | continue; | |||
979 | trieBuilder.addSymbol(*defined); | |||
980 | hasWeakSymbol = hasWeakSymbol || sym->isWeakDef(); | |||
981 | } | |||
982 | } | |||
983 | size = trieBuilder.build(); | |||
984 | } | |||
985 | ||||
986 | void ExportSection::writeTo(uint8_t *buf) const { trieBuilder.writeTo(buf); } | |||
987 | ||||
988 | DataInCodeSection::DataInCodeSection() | |||
989 | : LinkEditSection(segment_names::linkEdit, section_names::dataInCode) {} | |||
990 | ||||
991 | template <class LP> | |||
992 | static std::vector<MachO::data_in_code_entry> collectDataInCodeEntries() { | |||
993 | std::vector<MachO::data_in_code_entry> dataInCodeEntries; | |||
994 | for (const InputFile *inputFile : inputFiles) { | |||
995 | if (!isa<ObjFile>(inputFile)) | |||
996 | continue; | |||
997 | const ObjFile *objFile = cast<ObjFile>(inputFile); | |||
998 | ArrayRef<MachO::data_in_code_entry> entries = objFile->getDataInCode(); | |||
999 | if (entries.empty()) | |||
1000 | continue; | |||
1001 | ||||
1002 | assert(is_sorted(entries, [](const data_in_code_entry &lhs,(static_cast <bool> (is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })) ? void (0) : __assert_fail ("is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })" , "lld/MachO/SyntheticSections.cpp", 1005, __extension__ __PRETTY_FUNCTION__ )) | |||
1003 | const data_in_code_entry &rhs) {(static_cast <bool> (is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })) ? void (0) : __assert_fail ("is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })" , "lld/MachO/SyntheticSections.cpp", 1005, __extension__ __PRETTY_FUNCTION__ )) | |||
1004 | return lhs.offset < rhs.offset;(static_cast <bool> (is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })) ? void (0) : __assert_fail ("is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })" , "lld/MachO/SyntheticSections.cpp", 1005, __extension__ __PRETTY_FUNCTION__ )) | |||
1005 | }))(static_cast <bool> (is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })) ? void (0) : __assert_fail ("is_sorted(entries, [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { return lhs.offset < rhs.offset; })" , "lld/MachO/SyntheticSections.cpp", 1005, __extension__ __PRETTY_FUNCTION__ )); | |||
1006 | // For each code subsection find 'data in code' entries residing in it. | |||
1007 | // Compute the new offset values as | |||
1008 | // <offset within subsection> + <subsection address> - <__TEXT address>. | |||
1009 | for (const Section *section : objFile->sections) { | |||
1010 | for (const Subsection &subsec : section->subsections) { | |||
1011 | const InputSection *isec = subsec.isec; | |||
1012 | if (!isCodeSection(isec)) | |||
1013 | continue; | |||
1014 | if (cast<ConcatInputSection>(isec)->shouldOmitFromOutput()) | |||
1015 | continue; | |||
1016 | const uint64_t beginAddr = section->addr + subsec.offset; | |||
1017 | auto it = llvm::lower_bound( | |||
1018 | entries, beginAddr, | |||
1019 | [](const MachO::data_in_code_entry &entry, uint64_t addr) { | |||
1020 | return entry.offset < addr; | |||
1021 | }); | |||
1022 | const uint64_t endAddr = beginAddr + isec->getSize(); | |||
1023 | for (const auto end = entries.end(); | |||
1024 | it != end && it->offset + it->length <= endAddr; ++it) | |||
1025 | dataInCodeEntries.push_back( | |||
1026 | {static_cast<uint32_t>(isec->getVA(it->offset - beginAddr) - | |||
1027 | in.header->addr), | |||
1028 | it->length, it->kind}); | |||
1029 | } | |||
1030 | } | |||
1031 | } | |||
1032 | ||||
1033 | // ld64 emits the table in sorted order too. | |||
1034 | llvm::sort(dataInCodeEntries, | |||
1035 | [](const data_in_code_entry &lhs, const data_in_code_entry &rhs) { | |||
1036 | return lhs.offset < rhs.offset; | |||
1037 | }); | |||
1038 | return dataInCodeEntries; | |||
1039 | } | |||
1040 | ||||
1041 | void DataInCodeSection::finalizeContents() { | |||
1042 | entries = target->wordSize == 8 ? collectDataInCodeEntries<LP64>() | |||
1043 | : collectDataInCodeEntries<ILP32>(); | |||
1044 | } | |||
1045 | ||||
1046 | void DataInCodeSection::writeTo(uint8_t *buf) const { | |||
1047 | if (!entries.empty()) | |||
1048 | memcpy(buf, entries.data(), getRawSize()); | |||
1049 | } | |||
1050 | ||||
1051 | FunctionStartsSection::FunctionStartsSection() | |||
1052 | : LinkEditSection(segment_names::linkEdit, section_names::functionStarts) {} | |||
1053 | ||||
1054 | void FunctionStartsSection::finalizeContents() { | |||
1055 | raw_svector_ostream os{contents}; | |||
1056 | std::vector<uint64_t> addrs; | |||
1057 | for (const InputFile *file : inputFiles) { | |||
1058 | if (auto *objFile = dyn_cast<ObjFile>(file)) { | |||
1059 | for (const Symbol *sym : objFile->symbols) { | |||
1060 | if (const auto *defined = dyn_cast_or_null<Defined>(sym)) { | |||
1061 | if (!defined->isec || !isCodeSection(defined->isec) || | |||
1062 | !defined->isLive()) | |||
1063 | continue; | |||
1064 | // TODO: Add support for thumbs, in that case | |||
1065 | // the lowest bit of nextAddr needs to be set to 1. | |||
1066 | addrs.push_back(defined->getVA()); | |||
1067 | } | |||
1068 | } | |||
1069 | } | |||
1070 | } | |||
1071 | llvm::sort(addrs); | |||
1072 | uint64_t addr = in.header->addr; | |||
1073 | for (uint64_t nextAddr : addrs) { | |||
1074 | uint64_t delta = nextAddr - addr; | |||
1075 | if (delta == 0) | |||
1076 | continue; | |||
1077 | encodeULEB128(delta, os); | |||
1078 | addr = nextAddr; | |||
1079 | } | |||
1080 | os << '\0'; | |||
1081 | } | |||
1082 | ||||
1083 | void FunctionStartsSection::writeTo(uint8_t *buf) const { | |||
1084 | memcpy(buf, contents.data(), contents.size()); | |||
1085 | } | |||
1086 | ||||
1087 | SymtabSection::SymtabSection(StringTableSection &stringTableSection) | |||
1088 | : LinkEditSection(segment_names::linkEdit, section_names::symbolTable), | |||
1089 | stringTableSection(stringTableSection) {} | |||
1090 | ||||
1091 | void SymtabSection::emitBeginSourceStab(StringRef sourceFile) { | |||
1092 | StabsEntry stab(N_SO); | |||
1093 | stab.strx = stringTableSection.addString(saver().save(sourceFile)); | |||
1094 | stabs.emplace_back(std::move(stab)); | |||
1095 | } | |||
1096 | ||||
1097 | void SymtabSection::emitEndSourceStab() { | |||
1098 | StabsEntry stab(N_SO); | |||
1099 | stab.sect = 1; | |||
1100 | stabs.emplace_back(std::move(stab)); | |||
1101 | } | |||
1102 | ||||
1103 | void SymtabSection::emitObjectFileStab(ObjFile *file) { | |||
1104 | StabsEntry stab(N_OSO); | |||
1105 | stab.sect = target->cpuSubtype; | |||
1106 | SmallString<261> path(!file->archiveName.empty() ? file->archiveName | |||
1107 | : file->getName()); | |||
1108 | std::error_code ec = sys::fs::make_absolute(path); | |||
1109 | if (ec) | |||
1110 | fatal("failed to get absolute path for " + path); | |||
1111 | ||||
1112 | if (!file->archiveName.empty()) | |||
1113 | path.append({"(", file->getName(), ")"}); | |||
1114 | ||||
1115 | StringRef adjustedPath = saver().save(path.str()); | |||
1116 | adjustedPath.consume_front(config->osoPrefix); | |||
1117 | ||||
1118 | stab.strx = stringTableSection.addString(adjustedPath); | |||
1119 | stab.desc = 1; | |||
1120 | stab.value = file->modTime; | |||
1121 | stabs.emplace_back(std::move(stab)); | |||
1122 | } | |||
1123 | ||||
1124 | void SymtabSection::emitEndFunStab(Defined *defined) { | |||
1125 | StabsEntry stab(N_FUN); | |||
1126 | stab.value = defined->size; | |||
1127 | stabs.emplace_back(std::move(stab)); | |||
1128 | } | |||
1129 | ||||
1130 | void SymtabSection::emitStabs() { | |||
1131 | if (config->omitDebugInfo) | |||
1132 | return; | |||
1133 | ||||
1134 | for (const std::string &s : config->astPaths) { | |||
1135 | StabsEntry astStab(N_AST); | |||
1136 | astStab.strx = stringTableSection.addString(s); | |||
1137 | stabs.emplace_back(std::move(astStab)); | |||
1138 | } | |||
1139 | ||||
1140 | // Cache the file ID for each symbol in an std::pair for faster sorting. | |||
1141 | using SortingPair = std::pair<Defined *, int>; | |||
1142 | std::vector<SortingPair> symbolsNeedingStabs; | |||
1143 | for (const SymtabEntry &entry : | |||
1144 | concat<SymtabEntry>(localSymbols, externalSymbols)) { | |||
1145 | Symbol *sym = entry.sym; | |||
1146 | assert(sym->isLive() &&(static_cast <bool> (sym->isLive() && "dead symbols should not be in localSymbols, externalSymbols" ) ? void (0) : __assert_fail ("sym->isLive() && \"dead symbols should not be in localSymbols, externalSymbols\"" , "lld/MachO/SyntheticSections.cpp", 1147, __extension__ __PRETTY_FUNCTION__ )) | |||
1147 | "dead symbols should not be in localSymbols, externalSymbols")(static_cast <bool> (sym->isLive() && "dead symbols should not be in localSymbols, externalSymbols" ) ? void (0) : __assert_fail ("sym->isLive() && \"dead symbols should not be in localSymbols, externalSymbols\"" , "lld/MachO/SyntheticSections.cpp", 1147, __extension__ __PRETTY_FUNCTION__ )); | |||
1148 | if (auto *defined = dyn_cast<Defined>(sym)) { | |||
1149 | // Excluded symbols should have been filtered out in finalizeContents(). | |||
1150 | assert(defined->includeInSymtab)(static_cast <bool> (defined->includeInSymtab) ? void (0) : __assert_fail ("defined->includeInSymtab", "lld/MachO/SyntheticSections.cpp" , 1150, __extension__ __PRETTY_FUNCTION__)); | |||
1151 | ||||
1152 | if (defined->isAbsolute()) | |||
1153 | continue; | |||
1154 | ||||
1155 | // Constant-folded symbols go in the executable's symbol table, but don't | |||
1156 | // get a stabs entry. | |||
1157 | if (defined->wasIdenticalCodeFolded) | |||
1158 | continue; | |||
1159 | ||||
1160 | ObjFile *file = defined->getObjectFile(); | |||
1161 | if (!file || !file->compileUnit) | |||
1162 | continue; | |||
1163 | ||||
1164 | symbolsNeedingStabs.emplace_back(defined, defined->isec->getFile()->id); | |||
1165 | } | |||
1166 | } | |||
1167 | ||||
1168 | llvm::stable_sort(symbolsNeedingStabs, | |||
1169 | [&](const SortingPair &a, const SortingPair &b) { | |||
1170 | return a.second < b.second; | |||
1171 | }); | |||
1172 | ||||
1173 | // Emit STABS symbols so that dsymutil and/or the debugger can map address | |||
1174 | // regions in the final binary to the source and object files from which they | |||
1175 | // originated. | |||
1176 | InputFile *lastFile = nullptr; | |||
1177 | for (SortingPair &pair : symbolsNeedingStabs) { | |||
1178 | Defined *defined = pair.first; | |||
1179 | InputSection *isec = defined->isec; | |||
1180 | ObjFile *file = cast<ObjFile>(isec->getFile()); | |||
1181 | ||||
1182 | if (lastFile == nullptr || lastFile != file) { | |||
1183 | if (lastFile != nullptr) | |||
1184 | emitEndSourceStab(); | |||
1185 | lastFile = file; | |||
1186 | ||||
1187 | emitBeginSourceStab(file->sourceFile()); | |||
1188 | emitObjectFileStab(file); | |||
1189 | } | |||
1190 | ||||
1191 | StabsEntry symStab; | |||
1192 | symStab.sect = defined->isec->parent->index; | |||
1193 | symStab.strx = stringTableSection.addString(defined->getName()); | |||
1194 | symStab.value = defined->getVA(); | |||
1195 | ||||
1196 | if (isCodeSection(isec)) { | |||
1197 | symStab.type = N_FUN; | |||
1198 | stabs.emplace_back(std::move(symStab)); | |||
1199 | emitEndFunStab(defined); | |||
1200 | } else { | |||
1201 | symStab.type = defined->isExternal() ? N_GSYM : N_STSYM; | |||
1202 | stabs.emplace_back(std::move(symStab)); | |||
1203 | } | |||
1204 | } | |||
1205 | ||||
1206 | if (!stabs.empty()) | |||
1207 | emitEndSourceStab(); | |||
1208 | } | |||
1209 | ||||
1210 | void SymtabSection::finalizeContents() { | |||
1211 | auto addSymbol = [&](std::vector<SymtabEntry> &symbols, Symbol *sym) { | |||
1212 | uint32_t strx = stringTableSection.addString(sym->getName()); | |||
1213 | symbols.push_back({sym, strx}); | |||
1214 | }; | |||
1215 | ||||
1216 | std::function<void(Symbol *)> localSymbolsHandler; | |||
1217 | switch (config->localSymbolsPresence) { | |||
1218 | case SymtabPresence::All: | |||
1219 | localSymbolsHandler = [&](Symbol *sym) { addSymbol(localSymbols, sym); }; | |||
1220 | break; | |||
1221 | case SymtabPresence::None: | |||
1222 | localSymbolsHandler = [&](Symbol *) { /* Do nothing*/ }; | |||
1223 | break; | |||
1224 | case SymtabPresence::SelectivelyIncluded: | |||
1225 | localSymbolsHandler = [&](Symbol *sym) { | |||
1226 | if (config->localSymbolPatterns.match(sym->getName())) | |||
1227 | addSymbol(localSymbols, sym); | |||
1228 | }; | |||
1229 | break; | |||
1230 | case SymtabPresence::SelectivelyExcluded: | |||
1231 | localSymbolsHandler = [&](Symbol *sym) { | |||
1232 | if (!config->localSymbolPatterns.match(sym->getName())) | |||
1233 | addSymbol(localSymbols, sym); | |||
1234 | }; | |||
1235 | break; | |||
1236 | } | |||
1237 | ||||
1238 | // Local symbols aren't in the SymbolTable, so we walk the list of object | |||
1239 | // files to gather them. | |||
1240 | // But if `-x` is set, then we don't need to. localSymbolsHandler() will do | |||
1241 | // the right thing regardless, but this check is a perf optimization because | |||
1242 | // iterating through all the input files and their symbols is expensive. | |||
1243 | if (config->localSymbolsPresence != SymtabPresence::None) { | |||
1244 | for (const InputFile *file : inputFiles) { | |||
1245 | if (auto *objFile = dyn_cast<ObjFile>(file)) { | |||
1246 | for (Symbol *sym : objFile->symbols) { | |||
1247 | if (auto *defined = dyn_cast_or_null<Defined>(sym)) { | |||
1248 | if (defined->isExternal() || !defined->isLive() || | |||
1249 | !defined->includeInSymtab) | |||
1250 | continue; | |||
1251 | localSymbolsHandler(sym); | |||
1252 | } | |||
1253 | } | |||
1254 | } | |||
1255 | } | |||
1256 | } | |||
1257 | ||||
1258 | // __dyld_private is a local symbol too. It's linker-created and doesn't | |||
1259 | // exist in any object file. | |||
1260 | if (in.stubHelper && in.stubHelper->dyldPrivate) | |||
1261 | localSymbolsHandler(in.stubHelper->dyldPrivate); | |||
1262 | ||||
1263 | for (Symbol *sym : symtab->getSymbols()) { | |||
1264 | if (!sym->isLive()) | |||
1265 | continue; | |||
1266 | if (auto *defined = dyn_cast<Defined>(sym)) { | |||
1267 | if (!defined->includeInSymtab) | |||
1268 | continue; | |||
1269 | assert(defined->isExternal())(static_cast <bool> (defined->isExternal()) ? void ( 0) : __assert_fail ("defined->isExternal()", "lld/MachO/SyntheticSections.cpp" , 1269, __extension__ __PRETTY_FUNCTION__)); | |||
1270 | if (defined->privateExtern) | |||
1271 | localSymbolsHandler(defined); | |||
1272 | else | |||
1273 | addSymbol(externalSymbols, defined); | |||
1274 | } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) { | |||
1275 | if (dysym->isReferenced()) | |||
1276 | addSymbol(undefinedSymbols, sym); | |||
1277 | } | |||
1278 | } | |||
1279 | ||||
1280 | emitStabs(); | |||
1281 | uint32_t symtabIndex = stabs.size(); | |||
1282 | for (const SymtabEntry &entry : | |||
1283 | concat<SymtabEntry>(localSymbols, externalSymbols, undefinedSymbols)) { | |||
1284 | entry.sym->symtabIndex = symtabIndex++; | |||
1285 | } | |||
1286 | } | |||
1287 | ||||
1288 | uint32_t SymtabSection::getNumSymbols() const { | |||
1289 | return stabs.size() + localSymbols.size() + externalSymbols.size() + | |||
1290 | undefinedSymbols.size(); | |||
1291 | } | |||
1292 | ||||
1293 | // This serves to hide (type-erase) the template parameter from SymtabSection. | |||
1294 | template <class LP> class SymtabSectionImpl final : public SymtabSection { | |||
1295 | public: | |||
1296 | SymtabSectionImpl(StringTableSection &stringTableSection) | |||
1297 | : SymtabSection(stringTableSection) {} | |||
1298 | uint64_t getRawSize() const override; | |||
1299 | void writeTo(uint8_t *buf) const override; | |||
1300 | }; | |||
1301 | ||||
1302 | template <class LP> uint64_t SymtabSectionImpl<LP>::getRawSize() const { | |||
1303 | return getNumSymbols() * sizeof(typename LP::nlist); | |||
1304 | } | |||
1305 | ||||
1306 | template <class LP> void SymtabSectionImpl<LP>::writeTo(uint8_t *buf) const { | |||
1307 | auto *nList = reinterpret_cast<typename LP::nlist *>(buf); | |||
1308 | // Emit the stabs entries before the "real" symbols. We cannot emit them | |||
1309 | // after as that would render Symbol::symtabIndex inaccurate. | |||
1310 | for (const StabsEntry &entry : stabs) { | |||
1311 | nList->n_strx = entry.strx; | |||
1312 | nList->n_type = entry.type; | |||
1313 | nList->n_sect = entry.sect; | |||
1314 | nList->n_desc = entry.desc; | |||
1315 | nList->n_value = entry.value; | |||
1316 | ++nList; | |||
1317 | } | |||
1318 | ||||
1319 | for (const SymtabEntry &entry : concat<const SymtabEntry>( | |||
1320 | localSymbols, externalSymbols, undefinedSymbols)) { | |||
1321 | nList->n_strx = entry.strx; | |||
1322 | // TODO populate n_desc with more flags | |||
1323 | if (auto *defined = dyn_cast<Defined>(entry.sym)) { | |||
1324 | uint8_t scope = 0; | |||
1325 | if (defined->privateExtern) { | |||
1326 | // Private external -- dylib scoped symbol. | |||
1327 | // Promote to non-external at link time. | |||
1328 | scope = N_PEXT; | |||
1329 | } else if (defined->isExternal()) { | |||
1330 | // Normal global symbol. | |||
1331 | scope = N_EXT; | |||
1332 | } else { | |||
1333 | // TU-local symbol from localSymbols. | |||
1334 | scope = 0; | |||
1335 | } | |||
1336 | ||||
1337 | if (defined->isAbsolute()) { | |||
1338 | nList->n_type = scope | N_ABS; | |||
1339 | nList->n_sect = NO_SECT; | |||
1340 | nList->n_value = defined->value; | |||
1341 | } else { | |||
1342 | nList->n_type = scope | N_SECT; | |||
1343 | nList->n_sect = defined->isec->parent->index; | |||
1344 | // For the N_SECT symbol type, n_value is the address of the symbol | |||
1345 | nList->n_value = defined->getVA(); | |||
1346 | } | |||
1347 | nList->n_desc |= defined->thumb ? N_ARM_THUMB_DEF : 0; | |||
1348 | nList->n_desc |= defined->isExternalWeakDef() ? N_WEAK_DEF : 0; | |||
1349 | nList->n_desc |= | |||
1350 | defined->referencedDynamically ? REFERENCED_DYNAMICALLY : 0; | |||
1351 | } else if (auto *dysym = dyn_cast<DylibSymbol>(entry.sym)) { | |||
1352 | uint16_t n_desc = nList->n_desc; | |||
1353 | int16_t ordinal = ordinalForDylibSymbol(*dysym); | |||
1354 | if (ordinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP) | |||
1355 | SET_LIBRARY_ORDINAL(n_desc, DYNAMIC_LOOKUP_ORDINAL); | |||
1356 | else if (ordinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE) | |||
1357 | SET_LIBRARY_ORDINAL(n_desc, EXECUTABLE_ORDINAL); | |||
1358 | else { | |||
1359 | assert(ordinal > 0)(static_cast <bool> (ordinal > 0) ? void (0) : __assert_fail ("ordinal > 0", "lld/MachO/SyntheticSections.cpp", 1359, __extension__ __PRETTY_FUNCTION__)); | |||
1360 | SET_LIBRARY_ORDINAL(n_desc, static_cast<uint8_t>(ordinal)); | |||
1361 | } | |||
1362 | ||||
1363 | nList->n_type = N_EXT; | |||
1364 | n_desc |= dysym->isWeakDef() ? N_WEAK_DEF : 0; | |||
1365 | n_desc |= dysym->isWeakRef() ? N_WEAK_REF : 0; | |||
1366 | nList->n_desc = n_desc; | |||
1367 | } | |||
1368 | ++nList; | |||
1369 | } | |||
1370 | } | |||
1371 | ||||
1372 | template <class LP> | |||
1373 | SymtabSection * | |||
1374 | macho::makeSymtabSection(StringTableSection &stringTableSection) { | |||
1375 | return make<SymtabSectionImpl<LP>>(stringTableSection); | |||
1376 | } | |||
1377 | ||||
1378 | IndirectSymtabSection::IndirectSymtabSection() | |||
1379 | : LinkEditSection(segment_names::linkEdit, | |||
1380 | section_names::indirectSymbolTable) {} | |||
1381 | ||||
1382 | uint32_t IndirectSymtabSection::getNumSymbols() const { | |||
1383 | uint32_t size = in.got->getEntries().size() + | |||
1384 | in.tlvPointers->getEntries().size() + | |||
1385 | in.stubs->getEntries().size(); | |||
1386 | if (!config->emitChainedFixups) | |||
1387 | size += in.stubs->getEntries().size(); | |||
1388 | return size; | |||
1389 | } | |||
1390 | ||||
1391 | bool IndirectSymtabSection::isNeeded() const { | |||
1392 | return in.got->isNeeded() || in.tlvPointers->isNeeded() || | |||
1393 | in.stubs->isNeeded(); | |||
1394 | } | |||
1395 | ||||
1396 | void IndirectSymtabSection::finalizeContents() { | |||
1397 | uint32_t off = 0; | |||
1398 | in.got->reserved1 = off; | |||
1399 | off += in.got->getEntries().size(); | |||
1400 | in.tlvPointers->reserved1 = off; | |||
1401 | off += in.tlvPointers->getEntries().size(); | |||
1402 | in.stubs->reserved1 = off; | |||
1403 | if (in.lazyPointers) { | |||
1404 | off += in.stubs->getEntries().size(); | |||
1405 | in.lazyPointers->reserved1 = off; | |||
1406 | } | |||
1407 | } | |||
1408 | ||||
1409 | static uint32_t indirectValue(const Symbol *sym) { | |||
1410 | if (sym->symtabIndex == UINT32_MAX(4294967295U)) | |||
1411 | return INDIRECT_SYMBOL_LOCAL; | |||
1412 | if (auto *defined = dyn_cast<Defined>(sym)) | |||
1413 | if (defined->privateExtern) | |||
1414 | return INDIRECT_SYMBOL_LOCAL; | |||
1415 | return sym->symtabIndex; | |||
1416 | } | |||
1417 | ||||
1418 | void IndirectSymtabSection::writeTo(uint8_t *buf) const { | |||
1419 | uint32_t off = 0; | |||
1420 | for (const Symbol *sym : in.got->getEntries()) { | |||
1421 | write32le(buf + off * sizeof(uint32_t), indirectValue(sym)); | |||
1422 | ++off; | |||
1423 | } | |||
1424 | for (const Symbol *sym : in.tlvPointers->getEntries()) { | |||
1425 | write32le(buf + off * sizeof(uint32_t), indirectValue(sym)); | |||
1426 | ++off; | |||
1427 | } | |||
1428 | for (const Symbol *sym : in.stubs->getEntries()) { | |||
1429 | write32le(buf + off * sizeof(uint32_t), indirectValue(sym)); | |||
1430 | ++off; | |||
1431 | } | |||
1432 | ||||
1433 | if (in.lazyPointers) { | |||
1434 | // There is a 1:1 correspondence between stubs and LazyPointerSection | |||
1435 | // entries. But giving __stubs and __la_symbol_ptr the same reserved1 | |||
1436 | // (the offset into the indirect symbol table) so that they both refer | |||
1437 | // to the same range of offsets confuses `strip`, so write the stubs | |||
1438 | // symbol table offsets a second time. | |||
1439 | for (const Symbol *sym : in.stubs->getEntries()) { | |||
1440 | write32le(buf + off * sizeof(uint32_t), indirectValue(sym)); | |||
1441 | ++off; | |||
1442 | } | |||
1443 | } | |||
1444 | } | |||
1445 | ||||
1446 | StringTableSection::StringTableSection() | |||
1447 | : LinkEditSection(segment_names::linkEdit, section_names::stringTable) {} | |||
1448 | ||||
1449 | uint32_t StringTableSection::addString(StringRef str) { | |||
1450 | uint32_t strx = size; | |||
1451 | strings.push_back(str); // TODO: consider deduplicating strings | |||
1452 | size += str.size() + 1; // account for null terminator | |||
1453 | return strx; | |||
1454 | } | |||
1455 | ||||
1456 | void StringTableSection::writeTo(uint8_t *buf) const { | |||
1457 | uint32_t off = 0; | |||
1458 | for (StringRef str : strings) { | |||
1459 | memcpy(buf + off, str.data(), str.size()); | |||
1460 | off += str.size() + 1; // account for null terminator | |||
1461 | } | |||
1462 | } | |||
1463 | ||||
1464 | static_assert((CodeSignatureSection::blobHeadersSize % 8) == 0); | |||
1465 | static_assert((CodeSignatureSection::fixedHeadersSize % 8) == 0); | |||
1466 | ||||
1467 | CodeSignatureSection::CodeSignatureSection() | |||
1468 | : LinkEditSection(segment_names::linkEdit, section_names::codeSignature) { | |||
1469 | align = 16; // required by libstuff | |||
1470 | // FIXME: Consider using finalOutput instead of outputFile. | |||
1471 | fileName = config->outputFile; | |||
1472 | size_t slashIndex = fileName.rfind("/"); | |||
1473 | if (slashIndex != std::string::npos) | |||
1474 | fileName = fileName.drop_front(slashIndex + 1); | |||
1475 | ||||
1476 | // NOTE: Any changes to these calculations should be repeated | |||
1477 | // in llvm-objcopy's MachOLayoutBuilder::layoutTail. | |||
1478 | allHeadersSize = alignTo<16>(fixedHeadersSize + fileName.size() + 1); | |||
1479 | fileNamePad = allHeadersSize - fixedHeadersSize - fileName.size(); | |||
1480 | } | |||
1481 | ||||
1482 | uint32_t CodeSignatureSection::getBlockCount() const { | |||
1483 | return (fileOff + blockSize - 1) / blockSize; | |||
1484 | } | |||
1485 | ||||
1486 | uint64_t CodeSignatureSection::getRawSize() const { | |||
1487 | return allHeadersSize + getBlockCount() * hashSize; | |||
1488 | } | |||
1489 | ||||
1490 | void CodeSignatureSection::writeHashes(uint8_t *buf) const { | |||
1491 | // NOTE: Changes to this functionality should be repeated in llvm-objcopy's | |||
1492 | // MachOWriter::writeSignatureData. | |||
1493 | uint8_t *hashes = buf + fileOff + allHeadersSize; | |||
1494 | parallelFor(0, getBlockCount(), [&](size_t i) { | |||
1495 | sha256(buf + i * blockSize, | |||
1496 | std::min(static_cast<size_t>(fileOff - i * blockSize), blockSize), | |||
1497 | hashes + i * hashSize); | |||
1498 | }); | |||
1499 | #if defined(__APPLE__) | |||
1500 | // This is macOS-specific work-around and makes no sense for any | |||
1501 | // other host OS. See https://openradar.appspot.com/FB8914231 | |||
1502 | // | |||
1503 | // The macOS kernel maintains a signature-verification cache to | |||
1504 | // quickly validate applications at time of execve(2). The trouble | |||
1505 | // is that for the kernel creates the cache entry at the time of the | |||
1506 | // mmap(2) call, before we have a chance to write either the code to | |||
1507 | // sign or the signature header+hashes. The fix is to invalidate | |||
1508 | // all cached data associated with the output file, thus discarding | |||
1509 | // the bogus prematurely-cached signature. | |||
1510 | msync(buf, fileOff + getSize(), MS_INVALIDATE); | |||
1511 | #endif | |||
1512 | } | |||
1513 | ||||
1514 | void CodeSignatureSection::writeTo(uint8_t *buf) const { | |||
1515 | // NOTE: Changes to this functionality should be repeated in llvm-objcopy's | |||
1516 | // MachOWriter::writeSignatureData. | |||
1517 | uint32_t signatureSize = static_cast<uint32_t>(getSize()); | |||
1518 | auto *superBlob = reinterpret_cast<CS_SuperBlob *>(buf); | |||
1519 | write32be(&superBlob->magic, CSMAGIC_EMBEDDED_SIGNATURE); | |||
1520 | write32be(&superBlob->length, signatureSize); | |||
1521 | write32be(&superBlob->count, 1); | |||
1522 | auto *blobIndex = reinterpret_cast<CS_BlobIndex *>(&superBlob[1]); | |||
1523 | write32be(&blobIndex->type, CSSLOT_CODEDIRECTORY); | |||
1524 | write32be(&blobIndex->offset, blobHeadersSize); | |||
1525 | auto *codeDirectory = | |||
1526 | reinterpret_cast<CS_CodeDirectory *>(buf + blobHeadersSize); | |||
1527 | write32be(&codeDirectory->magic, CSMAGIC_CODEDIRECTORY); | |||
1528 | write32be(&codeDirectory->length, signatureSize - blobHeadersSize); | |||
1529 | write32be(&codeDirectory->version, CS_SUPPORTSEXECSEG); | |||
1530 | write32be(&codeDirectory->flags, CS_ADHOC | CS_LINKER_SIGNED); | |||
1531 | write32be(&codeDirectory->hashOffset, | |||
1532 | sizeof(CS_CodeDirectory) + fileName.size() + fileNamePad); | |||
1533 | write32be(&codeDirectory->identOffset, sizeof(CS_CodeDirectory)); | |||
1534 | codeDirectory->nSpecialSlots = 0; | |||
1535 | write32be(&codeDirectory->nCodeSlots, getBlockCount()); | |||
1536 | write32be(&codeDirectory->codeLimit, fileOff); | |||
1537 | codeDirectory->hashSize = static_cast<uint8_t>(hashSize); | |||
1538 | codeDirectory->hashType = kSecCodeSignatureHashSHA256; | |||
1539 | codeDirectory->platform = 0; | |||
1540 | codeDirectory->pageSize = blockSizeShift; | |||
1541 | codeDirectory->spare2 = 0; | |||
1542 | codeDirectory->scatterOffset = 0; | |||
1543 | codeDirectory->teamOffset = 0; | |||
1544 | codeDirectory->spare3 = 0; | |||
1545 | codeDirectory->codeLimit64 = 0; | |||
1546 | OutputSegment *textSeg = getOrCreateOutputSegment(segment_names::text); | |||
1547 | write64be(&codeDirectory->execSegBase, textSeg->fileOff); | |||
1548 | write64be(&codeDirectory->execSegLimit, textSeg->fileSize); | |||
1549 | write64be(&codeDirectory->execSegFlags, | |||
1550 | config->outputType == MH_EXECUTE ? CS_EXECSEG_MAIN_BINARY : 0); | |||
1551 | auto *id = reinterpret_cast<char *>(&codeDirectory[1]); | |||
1552 | memcpy(id, fileName.begin(), fileName.size()); | |||
1553 | memset(id + fileName.size(), 0, fileNamePad); | |||
1554 | } | |||
1555 | ||||
1556 | BitcodeBundleSection::BitcodeBundleSection() | |||
1557 | : SyntheticSection(segment_names::llvm, section_names::bitcodeBundle) {} | |||
1558 | ||||
1559 | class ErrorCodeWrapper { | |||
1560 | public: | |||
1561 | explicit ErrorCodeWrapper(std::error_code ec) : errorCode(ec.value()) {} | |||
1562 | explicit ErrorCodeWrapper(int ec) : errorCode(ec) {} | |||
1563 | operator int() const { return errorCode; } | |||
1564 | ||||
1565 | private: | |||
1566 | int errorCode; | |||
1567 | }; | |||
1568 | ||||
1569 | #define CHECK_EC(exp)do { ErrorCodeWrapper ec(exp); if (ec) fatal(Twine("operation failed with error code " ) + Twine(ec) + ": " + "exp"); } while (0); \ | |||
1570 | do { \ | |||
1571 | ErrorCodeWrapper ec(exp); \ | |||
1572 | if (ec) \ | |||
1573 | fatal(Twine("operation failed with error code ") + Twine(ec) + ": " + \ | |||
1574 | #exp); \ | |||
1575 | } while (0); | |||
1576 | ||||
1577 | void BitcodeBundleSection::finalize() { | |||
1578 | #ifdef LLVM_HAVE_LIBXAR | |||
1579 | using namespace llvm::sys::fs; | |||
1580 | CHECK_EC(createTemporaryFile("bitcode-bundle", "xar", xarPath))do { ErrorCodeWrapper ec(createTemporaryFile("bitcode-bundle" , "xar", xarPath)); if (ec) fatal(Twine("operation failed with error code " ) + Twine(ec) + ": " + "createTemporaryFile(\"bitcode-bundle\", \"xar\", xarPath)" ); } while (0);; | |||
1581 | ||||
1582 | #pragma clang diagnostic push | |||
1583 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" | |||
1584 | xar_t xar(xar_open(xarPath.data(), O_RDWR)); | |||
1585 | #pragma clang diagnostic pop | |||
1586 | if (!xar) | |||
1587 | fatal("failed to open XAR temporary file at " + xarPath); | |||
1588 | CHECK_EC(xar_opt_set(xar, XAR_OPT_COMPRESSION, XAR_OPT_VAL_NONE))do { ErrorCodeWrapper ec(xar_opt_set(xar, XAR_OPT_COMPRESSION , XAR_OPT_VAL_NONE)); if (ec) fatal(Twine("operation failed with error code " ) + Twine(ec) + ": " + "xar_opt_set(xar, XAR_OPT_COMPRESSION, XAR_OPT_VAL_NONE)" ); } while (0);; | |||
1589 | // FIXME: add more data to XAR | |||
1590 | CHECK_EC(xar_close(xar))do { ErrorCodeWrapper ec(xar_close(xar)); if (ec) fatal(Twine ("operation failed with error code ") + Twine(ec) + ": " + "xar_close(xar)" ); } while (0);; | |||
1591 | ||||
1592 | file_size(xarPath, xarSize); | |||
1593 | #endif // defined(LLVM_HAVE_LIBXAR) | |||
1594 | } | |||
1595 | ||||
1596 | void BitcodeBundleSection::writeTo(uint8_t *buf) const { | |||
1597 | using namespace llvm::sys::fs; | |||
1598 | file_t handle = | |||
1599 | CHECK(openNativeFile(xarPath, CD_OpenExisting, FA_Read, OF_None),check2((openNativeFile(xarPath, CD_OpenExisting, FA_Read, OF_None )), [&] { return toString("failed to open XAR file"); }) | |||
1600 | "failed to open XAR file")check2((openNativeFile(xarPath, CD_OpenExisting, FA_Read, OF_None )), [&] { return toString("failed to open XAR file"); }); | |||
1601 | std::error_code ec; | |||
1602 | mapped_file_region xarMap(handle, mapped_file_region::mapmode::readonly, | |||
1603 | xarSize, 0, ec); | |||
1604 | if (ec) | |||
1605 | fatal("failed to map XAR file"); | |||
1606 | memcpy(buf, xarMap.const_data(), xarSize); | |||
1607 | ||||
1608 | closeFile(handle); | |||
1609 | remove(xarPath); | |||
1610 | } | |||
1611 | ||||
1612 | CStringSection::CStringSection(const char *name) | |||
1613 | : SyntheticSection(segment_names::text, name) { | |||
1614 | flags = S_CSTRING_LITERALS; | |||
1615 | } | |||
1616 | ||||
1617 | void CStringSection::addInput(CStringInputSection *isec) { | |||
1618 | isec->parent = this; | |||
1619 | inputs.push_back(isec); | |||
1620 | if (isec->align > align) | |||
1621 | align = isec->align; | |||
1622 | } | |||
1623 | ||||
1624 | void CStringSection::writeTo(uint8_t *buf) const { | |||
1625 | for (const CStringInputSection *isec : inputs) { | |||
1626 | for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) { | |||
1627 | if (!piece.live) | |||
1628 | continue; | |||
1629 | StringRef string = isec->getStringRef(i); | |||
1630 | memcpy(buf + piece.outSecOff, string.data(), string.size()); | |||
1631 | } | |||
1632 | } | |||
1633 | } | |||
1634 | ||||
1635 | void CStringSection::finalizeContents() { | |||
1636 | uint64_t offset = 0; | |||
1637 | for (CStringInputSection *isec : inputs) { | |||
1638 | for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) { | |||
1639 | if (!piece.live) | |||
| ||||
1640 | continue; | |||
1641 | // See comment above DeduplicatedCStringSection for how alignment is | |||
1642 | // handled. | |||
1643 | uint32_t pieceAlign = 1 | |||
1644 | << countTrailingZeros(isec->align | piece.inSecOff); | |||
| ||||
1645 | offset = alignTo(offset, pieceAlign); | |||
1646 | piece.outSecOff = offset; | |||
1647 | isec->isFinal = true; | |||
1648 | StringRef string = isec->getStringRef(i); | |||
1649 | offset += string.size() + 1; // account for null terminator | |||
1650 | } | |||
1651 | } | |||
1652 | size = offset; | |||
1653 | } | |||
1654 | ||||
1655 | // Mergeable cstring literals are found under the __TEXT,__cstring section. In | |||
1656 | // contrast to ELF, which puts strings that need different alignments into | |||
1657 | // different sections, clang's Mach-O backend puts them all in one section. | |||
1658 | // Strings that need to be aligned have the .p2align directive emitted before | |||
1659 | // them, which simply translates into zero padding in the object file. In other | |||
1660 | // words, we have to infer the desired alignment of these cstrings from their | |||
1661 | // addresses. | |||
1662 | // | |||
1663 | // We differ slightly from ld64 in how we've chosen to align these cstrings. | |||
1664 | // Both LLD and ld64 preserve the number of trailing zeros in each cstring's | |||
1665 | // address in the input object files. When deduplicating identical cstrings, | |||
1666 | // both linkers pick the cstring whose address has more trailing zeros, and | |||
1667 | // preserve the alignment of that address in the final binary. However, ld64 | |||
1668 | // goes a step further and also preserves the offset of the cstring from the | |||
1669 | // last section-aligned address. I.e. if a cstring is at offset 18 in the | |||
1670 | // input, with a section alignment of 16, then both LLD and ld64 will ensure the | |||
1671 | // final address is 2-byte aligned (since 18 == 16 + 2). But ld64 will also | |||
1672 | // ensure that the final address is of the form 16 * k + 2 for some k. | |||
1673 | // | |||
1674 | // Note that ld64's heuristic means that a dedup'ed cstring's final address is | |||
1675 | // dependent on the order of the input object files. E.g. if in addition to the | |||
1676 | // cstring at offset 18 above, we have a duplicate one in another file with a | |||
1677 | // `.cstring` section alignment of 2 and an offset of zero, then ld64 will pick | |||
1678 | // the cstring from the object file earlier on the command line (since both have | |||
1679 | // the same number of trailing zeros in their address). So the final cstring may | |||
1680 | // either be at some address `16 * k + 2` or at some address `2 * k`. | |||
1681 | // | |||
1682 | // I've opted not to follow this behavior primarily for implementation | |||
1683 | // simplicity, and secondarily to save a few more bytes. It's not clear to me | |||
1684 | // that preserving the section alignment + offset is ever necessary, and there | |||
1685 | // are many cases that are clearly redundant. In particular, if an x86_64 object | |||
1686 | // file contains some strings that are accessed via SIMD instructions, then the | |||
1687 | // .cstring section in the object file will be 16-byte-aligned (since SIMD | |||
1688 | // requires its operand addresses to be 16-byte aligned). However, there will | |||
1689 | // typically also be other cstrings in the same file that aren't used via SIMD | |||
1690 | // and don't need this alignment. They will be emitted at some arbitrary address | |||
1691 | // `A`, but ld64 will treat them as being 16-byte aligned with an offset of `16 | |||
1692 | // % A`. | |||
1693 | void DeduplicatedCStringSection::finalizeContents() { | |||
1694 | // Find the largest alignment required for each string. | |||
1695 | for (const CStringInputSection *isec : inputs) { | |||
1696 | for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) { | |||
1697 | if (!piece.live) | |||
1698 | continue; | |||
1699 | auto s = isec->getCachedHashStringRef(i); | |||
1700 | assert(isec->align != 0)(static_cast <bool> (isec->align != 0) ? void (0) : __assert_fail ("isec->align != 0", "lld/MachO/SyntheticSections.cpp", 1700 , __extension__ __PRETTY_FUNCTION__)); | |||
1701 | uint8_t trailingZeros = countTrailingZeros(isec->align | piece.inSecOff); | |||
1702 | auto it = stringOffsetMap.insert( | |||
1703 | std::make_pair(s, StringOffset(trailingZeros))); | |||
1704 | if (!it.second && it.first->second.trailingZeros < trailingZeros) | |||
1705 | it.first->second.trailingZeros = trailingZeros; | |||
1706 | } | |||
1707 | } | |||
1708 | ||||
1709 | // Assign an offset for each string and save it to the corresponding | |||
1710 | // StringPieces for easy access. | |||
1711 | for (CStringInputSection *isec : inputs) { | |||
1712 | for (const auto &[i, piece] : llvm::enumerate(isec->pieces)) { | |||
1713 | if (!piece.live) | |||
1714 | continue; | |||
1715 | auto s = isec->getCachedHashStringRef(i); | |||
1716 | auto it = stringOffsetMap.find(s); | |||
1717 | assert(it != stringOffsetMap.end())(static_cast <bool> (it != stringOffsetMap.end()) ? void (0) : __assert_fail ("it != stringOffsetMap.end()", "lld/MachO/SyntheticSections.cpp" , 1717, __extension__ __PRETTY_FUNCTION__)); | |||
1718 | StringOffset &offsetInfo = it->second; | |||
1719 | if (offsetInfo.outSecOff == UINT64_MAX(18446744073709551615UL)) { | |||
1720 | offsetInfo.outSecOff = alignTo(size, 1ULL << offsetInfo.trailingZeros); | |||
1721 | size = | |||
1722 | offsetInfo.outSecOff + s.size() + 1; // account for null terminator | |||
1723 | } | |||
1724 | piece.outSecOff = offsetInfo.outSecOff; | |||
1725 | } | |||
1726 | isec->isFinal = true; | |||
1727 | } | |||
1728 | } | |||
1729 | ||||
1730 | void DeduplicatedCStringSection::writeTo(uint8_t *buf) const { | |||
1731 | for (const auto &p : stringOffsetMap) { | |||
1732 | StringRef data = p.first.val(); | |||
1733 | uint64_t off = p.second.outSecOff; | |||
1734 | if (!data.empty()) | |||
1735 | memcpy(buf + off, data.data(), data.size()); | |||
1736 | } | |||
1737 | } | |||
1738 | ||||
1739 | DeduplicatedCStringSection::StringOffset | |||
1740 | DeduplicatedCStringSection::getStringOffset(StringRef str) const { | |||
1741 | // StringPiece uses 31 bits to store the hashes, so we replicate that | |||
1742 | uint32_t hash = xxHash64(str) & 0x7fffffff; | |||
1743 | auto offset = stringOffsetMap.find(CachedHashStringRef(str, hash)); | |||
1744 | assert(offset != stringOffsetMap.end() &&(static_cast <bool> (offset != stringOffsetMap.end() && "Looked-up strings should always exist in section") ? void ( 0) : __assert_fail ("offset != stringOffsetMap.end() && \"Looked-up strings should always exist in section\"" , "lld/MachO/SyntheticSections.cpp", 1745, __extension__ __PRETTY_FUNCTION__ )) | |||
1745 | "Looked-up strings should always exist in section")(static_cast <bool> (offset != stringOffsetMap.end() && "Looked-up strings should always exist in section") ? void ( 0) : __assert_fail ("offset != stringOffsetMap.end() && \"Looked-up strings should always exist in section\"" , "lld/MachO/SyntheticSections.cpp", 1745, __extension__ __PRETTY_FUNCTION__ )); | |||
1746 | return offset->second; | |||
1747 | } | |||
1748 | ||||
1749 | // This section is actually emitted as __TEXT,__const by ld64, but clang may | |||
1750 | // emit input sections of that name, and LLD doesn't currently support mixing | |||
1751 | // synthetic and concat-type OutputSections. To work around this, I've given | |||
1752 | // our merged-literals section a different name. | |||
1753 | WordLiteralSection::WordLiteralSection() | |||
1754 | : SyntheticSection(segment_names::text, section_names::literals) { | |||
1755 | align = 16; | |||
1756 | } | |||
1757 | ||||
1758 | void WordLiteralSection::addInput(WordLiteralInputSection *isec) { | |||
1759 | isec->parent = this; | |||
1760 | inputs.push_back(isec); | |||
1761 | } | |||
1762 | ||||
1763 | void WordLiteralSection::finalizeContents() { | |||
1764 | for (WordLiteralInputSection *isec : inputs) { | |||
1765 | // We do all processing of the InputSection here, so it will be effectively | |||
1766 | // finalized. | |||
1767 | isec->isFinal = true; | |||
1768 | const uint8_t *buf = isec->data.data(); | |||
1769 | switch (sectionType(isec->getFlags())) { | |||
1770 | case S_4BYTE_LITERALS: { | |||
1771 | for (size_t off = 0, e = isec->data.size(); off < e; off += 4) { | |||
1772 | if (!isec->isLive(off)) | |||
1773 | continue; | |||
1774 | uint32_t value = *reinterpret_cast<const uint32_t *>(buf + off); | |||
1775 | literal4Map.emplace(value, literal4Map.size()); | |||
1776 | } | |||
1777 | break; | |||
1778 | } | |||
1779 | case S_8BYTE_LITERALS: { | |||
1780 | for (size_t off = 0, e = isec->data.size(); off < e; off += 8) { | |||
1781 | if (!isec->isLive(off)) | |||
1782 | continue; | |||
1783 | uint64_t value = *reinterpret_cast<const uint64_t *>(buf + off); | |||
1784 | literal8Map.emplace(value, literal8Map.size()); | |||
1785 | } | |||
1786 | break; | |||
1787 | } | |||
1788 | case S_16BYTE_LITERALS: { | |||
1789 | for (size_t off = 0, e = isec->data.size(); off < e; off += 16) { | |||
1790 | if (!isec->isLive(off)) | |||
1791 | continue; | |||
1792 | UInt128 value = *reinterpret_cast<const UInt128 *>(buf + off); | |||
1793 | literal16Map.emplace(value, literal16Map.size()); | |||
1794 | } | |||
1795 | break; | |||
1796 | } | |||
1797 | default: | |||
1798 | llvm_unreachable("invalid literal section type")::llvm::llvm_unreachable_internal("invalid literal section type" , "lld/MachO/SyntheticSections.cpp", 1798); | |||
1799 | } | |||
1800 | } | |||
1801 | } | |||
1802 | ||||
1803 | void WordLiteralSection::writeTo(uint8_t *buf) const { | |||
1804 | // Note that we don't attempt to do any endianness conversion in addInput(), | |||
1805 | // so we don't do it here either -- just write out the original value, | |||
1806 | // byte-for-byte. | |||
1807 | for (const auto &p : literal16Map) | |||
1808 | memcpy(buf + p.second * 16, &p.first, 16); | |||
1809 | buf += literal16Map.size() * 16; | |||
1810 | ||||
1811 | for (const auto &p : literal8Map) | |||
1812 | memcpy(buf + p.second * 8, &p.first, 8); | |||
1813 | buf += literal8Map.size() * 8; | |||
1814 | ||||
1815 | for (const auto &p : literal4Map) | |||
1816 | memcpy(buf + p.second * 4, &p.first, 4); | |||
1817 | } | |||
1818 | ||||
1819 | ObjCImageInfoSection::ObjCImageInfoSection() | |||
1820 | : SyntheticSection(segment_names::data, section_names::objCImageInfo) {} | |||
1821 | ||||
1822 | ObjCImageInfoSection::ImageInfo | |||
1823 | ObjCImageInfoSection::parseImageInfo(const InputFile *file) { | |||
1824 | ImageInfo info; | |||
1825 | ArrayRef<uint8_t> data = file->objCImageInfo; | |||
1826 | // The image info struct has the following layout: | |||
1827 | // struct { | |||
1828 | // uint32_t version; | |||
1829 | // uint32_t flags; | |||
1830 | // }; | |||
1831 | if (data.size() < 8) { | |||
1832 | warn(toString(file) + ": invalid __objc_imageinfo size"); | |||
1833 | return info; | |||
1834 | } | |||
1835 | ||||
1836 | auto *buf = reinterpret_cast<const uint32_t *>(data.data()); | |||
1837 | if (read32le(buf) != 0) { | |||
1838 | warn(toString(file) + ": invalid __objc_imageinfo version"); | |||
1839 | return info; | |||
1840 | } | |||
1841 | ||||
1842 | uint32_t flags = read32le(buf + 1); | |||
1843 | info.swiftVersion = (flags >> 8) & 0xff; | |||
1844 | info.hasCategoryClassProperties = flags & 0x40; | |||
1845 | return info; | |||
1846 | } | |||
1847 | ||||
1848 | static std::string swiftVersionString(uint8_t version) { | |||
1849 | switch (version) { | |||
1850 | case 1: | |||
1851 | return "1.0"; | |||
1852 | case 2: | |||
1853 | return "1.1"; | |||
1854 | case 3: | |||
1855 | return "2.0"; | |||
1856 | case 4: | |||
1857 | return "3.0"; | |||
1858 | case 5: | |||
1859 | return "4.0"; | |||
1860 | default: | |||
1861 | return ("0x" + Twine::utohexstr(version)).str(); | |||
1862 | } | |||
1863 | } | |||
1864 | ||||
1865 | // Validate each object file's __objc_imageinfo and use them to generate the | |||
1866 | // image info for the output binary. Only two pieces of info are relevant: | |||
1867 | // 1. The Swift version (should be identical across inputs) | |||
1868 | // 2. `bool hasCategoryClassProperties` (true only if true for all inputs) | |||
1869 | void ObjCImageInfoSection::finalizeContents() { | |||
1870 | assert(files.size() != 0)(static_cast <bool> (files.size() != 0) ? void (0) : __assert_fail ("files.size() != 0", "lld/MachO/SyntheticSections.cpp", 1870 , __extension__ __PRETTY_FUNCTION__)); // should have already been checked via isNeeded() | |||
1871 | ||||
1872 | info.hasCategoryClassProperties = true; | |||
1873 | const InputFile *firstFile; | |||
1874 | for (auto file : files) { | |||
1875 | ImageInfo inputInfo = parseImageInfo(file); | |||
1876 | info.hasCategoryClassProperties &= inputInfo.hasCategoryClassProperties; | |||
1877 | ||||
1878 | // swiftVersion 0 means no Swift is present, so no version checking required | |||
1879 | if (inputInfo.swiftVersion == 0) | |||
1880 | continue; | |||
1881 | ||||
1882 | if (info.swiftVersion != 0 && info.swiftVersion != inputInfo.swiftVersion) { | |||
1883 | error("Swift version mismatch: " + toString(firstFile) + " has version " + | |||
1884 | swiftVersionString(info.swiftVersion) + " but " + toString(file) + | |||
1885 | " has version " + swiftVersionString(inputInfo.swiftVersion)); | |||
1886 | } else { | |||
1887 | info.swiftVersion = inputInfo.swiftVersion; | |||
1888 | firstFile = file; | |||
1889 | } | |||
1890 | } | |||
1891 | } | |||
1892 | ||||
1893 | void ObjCImageInfoSection::writeTo(uint8_t *buf) const { | |||
1894 | uint32_t flags = info.hasCategoryClassProperties ? 0x40 : 0x0; | |||
1895 | flags |= info.swiftVersion << 8; | |||
1896 | write32le(buf + 4, flags); | |||
1897 | } | |||
1898 | ||||
1899 | InitOffsetsSection::InitOffsetsSection() | |||
1900 | : SyntheticSection(segment_names::text, section_names::initOffsets) { | |||
1901 | flags = S_INIT_FUNC_OFFSETS; | |||
1902 | align = 4; // This section contains 32-bit integers. | |||
1903 | } | |||
1904 | ||||
1905 | uint64_t InitOffsetsSection::getSize() const { | |||
1906 | size_t count = 0; | |||
1907 | for (const ConcatInputSection *isec : sections) | |||
1908 | count += isec->relocs.size(); | |||
1909 | return count * sizeof(uint32_t); | |||
1910 | } | |||
1911 | ||||
1912 | void InitOffsetsSection::writeTo(uint8_t *buf) const { | |||
1913 | // FIXME: Add function specified by -init when that argument is implemented. | |||
1914 | for (ConcatInputSection *isec : sections) { | |||
1915 | for (const Reloc &rel : isec->relocs) { | |||
1916 | const Symbol *referent = rel.referent.dyn_cast<Symbol *>(); | |||
1917 | assert(referent && "section relocation should have been rejected")(static_cast <bool> (referent && "section relocation should have been rejected" ) ? void (0) : __assert_fail ("referent && \"section relocation should have been rejected\"" , "lld/MachO/SyntheticSections.cpp", 1917, __extension__ __PRETTY_FUNCTION__ )); | |||
1918 | uint64_t offset = referent->getVA() - in.header->addr; | |||
1919 | // FIXME: Can we handle this gracefully? | |||
1920 | if (offset > UINT32_MAX(4294967295U)) | |||
1921 | fatal(isec->getLocation(rel.offset) + ": offset to initializer " + | |||
1922 | referent->getName() + " (" + utohexstr(offset) + | |||
1923 | ") does not fit in 32 bits"); | |||
1924 | ||||
1925 | // Entries need to be added in the order they appear in the section, but | |||
1926 | // relocations aren't guaranteed to be sorted. | |||
1927 | size_t index = rel.offset >> target->p2WordSize; | |||
1928 | write32le(&buf[index * sizeof(uint32_t)], offset); | |||
1929 | } | |||
1930 | buf += isec->relocs.size() * sizeof(uint32_t); | |||
1931 | } | |||
1932 | } | |||
1933 | ||||
1934 | // The inputs are __mod_init_func sections, which contain pointers to | |||
1935 | // initializer functions, therefore all relocations should be of the UNSIGNED | |||
1936 | // type. InitOffsetsSection stores offsets, so if the initializer's address is | |||
1937 | // not known at link time, stub-indirection has to be used. | |||
1938 | void InitOffsetsSection::setUp() { | |||
1939 | for (const ConcatInputSection *isec : sections) { | |||
1940 | for (const Reloc &rel : isec->relocs) { | |||
1941 | RelocAttrs attrs = target->getRelocAttrs(rel.type); | |||
1942 | if (!attrs.hasAttr(RelocAttrBits::UNSIGNED)) | |||
1943 | error(isec->getLocation(rel.offset) + | |||
1944 | ": unsupported relocation type: " + attrs.name); | |||
1945 | if (rel.addend != 0) | |||
1946 | error(isec->getLocation(rel.offset) + | |||
1947 | ": relocation addend is not representable in __init_offsets"); | |||
1948 | if (rel.referent.is<InputSection *>()) | |||
1949 | error(isec->getLocation(rel.offset) + | |||
1950 | ": unexpected section relocation"); | |||
1951 | ||||
1952 | Symbol *sym = rel.referent.dyn_cast<Symbol *>(); | |||
1953 | if (auto *undefined = dyn_cast<Undefined>(sym)) | |||
1954 | treatUndefinedSymbol(*undefined, isec, rel.offset); | |||
1955 | if (needsBinding(sym)) | |||
1956 | in.stubs->addEntry(sym); | |||
1957 | } | |||
1958 | } | |||
1959 | } | |||
1960 | ||||
1961 | void macho::createSyntheticSymbols() { | |||
1962 | auto addHeaderSymbol = [](const char *name) { | |||
1963 | symtab->addSynthetic(name, in.header->isec, /*value=*/0, | |||
1964 | /*isPrivateExtern=*/true, /*includeInSymtab=*/false, | |||
1965 | /*referencedDynamically=*/false); | |||
1966 | }; | |||
1967 | ||||
1968 | switch (config->outputType) { | |||
1969 | // FIXME: Assign the right address value for these symbols | |||
1970 | // (rather than 0). But we need to do that after assignAddresses(). | |||
1971 | case MH_EXECUTE: | |||
1972 | // If linking PIE, __mh_execute_header is a defined symbol in | |||
1973 | // __TEXT, __text) | |||
1974 | // Otherwise, it's an absolute symbol. | |||
1975 | if (config->isPic) | |||
1976 | symtab->addSynthetic("__mh_execute_header", in.header->isec, /*value=*/0, | |||
1977 | /*isPrivateExtern=*/false, /*includeInSymtab=*/true, | |||
1978 | /*referencedDynamically=*/true); | |||
1979 | else | |||
1980 | symtab->addSynthetic("__mh_execute_header", /*isec=*/nullptr, /*value=*/0, | |||
1981 | /*isPrivateExtern=*/false, /*includeInSymtab=*/true, | |||
1982 | /*referencedDynamically=*/true); | |||
1983 | break; | |||
1984 | ||||
1985 | // The following symbols are N_SECT symbols, even though the header is not | |||
1986 | // part of any section and that they are private to the bundle/dylib/object | |||
1987 | // they are part of. | |||
1988 | case MH_BUNDLE: | |||
1989 | addHeaderSymbol("__mh_bundle_header"); | |||
1990 | break; | |||
1991 | case MH_DYLIB: | |||
1992 | addHeaderSymbol("__mh_dylib_header"); | |||
1993 | break; | |||
1994 | case MH_DYLINKER: | |||
1995 | addHeaderSymbol("__mh_dylinker_header"); | |||
1996 | break; | |||
1997 | case MH_OBJECT: | |||
1998 | addHeaderSymbol("__mh_object_header"); | |||
1999 | break; | |||
2000 | default: | |||
2001 | llvm_unreachable("unexpected outputType")::llvm::llvm_unreachable_internal("unexpected outputType", "lld/MachO/SyntheticSections.cpp" , 2001); | |||
2002 | break; | |||
2003 | } | |||
2004 | ||||
2005 | // The Itanium C++ ABI requires dylibs to pass a pointer to __cxa_atexit | |||
2006 | // which does e.g. cleanup of static global variables. The ABI document | |||
2007 | // says that the pointer can point to any address in one of the dylib's | |||
2008 | // segments, but in practice ld64 seems to set it to point to the header, | |||
2009 | // so that's what's implemented here. | |||
2010 | addHeaderSymbol("___dso_handle"); | |||
2011 | } | |||
2012 | ||||
2013 | ChainedFixupsSection::ChainedFixupsSection() | |||
2014 | : LinkEditSection(segment_names::linkEdit, section_names::chainFixups) {} | |||
2015 | ||||
2016 | bool ChainedFixupsSection::isNeeded() const { | |||
2017 | assert(config->emitChainedFixups)(static_cast <bool> (config->emitChainedFixups) ? void (0) : __assert_fail ("config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 2017, __extension__ __PRETTY_FUNCTION__)); | |||
2018 | // dyld always expects LC_DYLD_CHAINED_FIXUPS to point to a valid | |||
2019 | // dyld_chained_fixups_header, so we create this section even if there aren't | |||
2020 | // any fixups. | |||
2021 | return true; | |||
2022 | } | |||
2023 | ||||
2024 | static bool needsWeakBind(const Symbol &sym) { | |||
2025 | if (auto *dysym = dyn_cast<DylibSymbol>(&sym)) | |||
2026 | return dysym->isWeakDef(); | |||
2027 | if (auto *defined = dyn_cast<Defined>(&sym)) | |||
2028 | return defined->isExternalWeakDef(); | |||
2029 | return false; | |||
2030 | } | |||
2031 | ||||
2032 | void ChainedFixupsSection::addBinding(const Symbol *sym, | |||
2033 | const InputSection *isec, uint64_t offset, | |||
2034 | int64_t addend) { | |||
2035 | locations.emplace_back(isec, offset); | |||
2036 | int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0; | |||
2037 | auto [it, inserted] = bindings.insert( | |||
2038 | {{sym, outlineAddend}, static_cast<uint32_t>(bindings.size())}); | |||
2039 | ||||
2040 | if (inserted) { | |||
2041 | symtabSize += sym->getName().size() + 1; | |||
2042 | hasWeakBind = hasWeakBind || needsWeakBind(*sym); | |||
2043 | if (!isInt<23>(outlineAddend)) | |||
2044 | needsLargeAddend = true; | |||
2045 | else if (outlineAddend != 0) | |||
2046 | needsAddend = true; | |||
2047 | } | |||
2048 | } | |||
2049 | ||||
2050 | std::pair<uint32_t, uint8_t> | |||
2051 | ChainedFixupsSection::getBinding(const Symbol *sym, int64_t addend) const { | |||
2052 | int64_t outlineAddend = (addend < 0 || addend > 0xFF) ? addend : 0; | |||
2053 | auto it = bindings.find({sym, outlineAddend}); | |||
2054 | assert(it != bindings.end() && "binding not found in the imports table")(static_cast <bool> (it != bindings.end() && "binding not found in the imports table" ) ? void (0) : __assert_fail ("it != bindings.end() && \"binding not found in the imports table\"" , "lld/MachO/SyntheticSections.cpp", 2054, __extension__ __PRETTY_FUNCTION__ )); | |||
2055 | if (outlineAddend == 0) | |||
2056 | return {it->second, addend}; | |||
2057 | return {it->second, 0}; | |||
2058 | } | |||
2059 | ||||
2060 | static size_t writeImport(uint8_t *buf, int format, uint32_t libOrdinal, | |||
2061 | bool weakRef, uint32_t nameOffset, int64_t addend) { | |||
2062 | switch (format) { | |||
2063 | case DYLD_CHAINED_IMPORT: { | |||
2064 | auto *import = reinterpret_cast<dyld_chained_import *>(buf); | |||
2065 | import->lib_ordinal = libOrdinal; | |||
2066 | import->weak_import = weakRef; | |||
2067 | import->name_offset = nameOffset; | |||
2068 | return sizeof(dyld_chained_import); | |||
2069 | } | |||
2070 | case DYLD_CHAINED_IMPORT_ADDEND: { | |||
2071 | auto *import = reinterpret_cast<dyld_chained_import_addend *>(buf); | |||
2072 | import->lib_ordinal = libOrdinal; | |||
2073 | import->weak_import = weakRef; | |||
2074 | import->name_offset = nameOffset; | |||
2075 | import->addend = addend; | |||
2076 | return sizeof(dyld_chained_import_addend); | |||
2077 | } | |||
2078 | case DYLD_CHAINED_IMPORT_ADDEND64: { | |||
2079 | auto *import = reinterpret_cast<dyld_chained_import_addend64 *>(buf); | |||
2080 | import->lib_ordinal = libOrdinal; | |||
2081 | import->weak_import = weakRef; | |||
2082 | import->name_offset = nameOffset; | |||
2083 | import->addend = addend; | |||
2084 | return sizeof(dyld_chained_import_addend64); | |||
2085 | } | |||
2086 | default: | |||
2087 | llvm_unreachable("Unknown import format")::llvm::llvm_unreachable_internal("Unknown import format", "lld/MachO/SyntheticSections.cpp" , 2087); | |||
2088 | } | |||
2089 | } | |||
2090 | ||||
2091 | size_t ChainedFixupsSection::SegmentInfo::getSize() const { | |||
2092 | assert(pageStarts.size() > 0 && "SegmentInfo for segment with no fixups?")(static_cast <bool> (pageStarts.size() > 0 && "SegmentInfo for segment with no fixups?") ? void (0) : __assert_fail ("pageStarts.size() > 0 && \"SegmentInfo for segment with no fixups?\"" , "lld/MachO/SyntheticSections.cpp", 2092, __extension__ __PRETTY_FUNCTION__ )); | |||
2093 | return alignTo<8>(sizeof(dyld_chained_starts_in_segment) + | |||
2094 | pageStarts.back().first * sizeof(uint16_t)); | |||
2095 | } | |||
2096 | ||||
2097 | size_t ChainedFixupsSection::SegmentInfo::writeTo(uint8_t *buf) const { | |||
2098 | auto *segInfo = reinterpret_cast<dyld_chained_starts_in_segment *>(buf); | |||
2099 | segInfo->size = getSize(); | |||
2100 | segInfo->page_size = target->getPageSize(); | |||
2101 | // FIXME: Use DYLD_CHAINED_PTR_64_OFFSET on newer OS versions. | |||
2102 | segInfo->pointer_format = DYLD_CHAINED_PTR_64; | |||
2103 | segInfo->segment_offset = oseg->addr - in.header->addr; | |||
2104 | segInfo->max_valid_pointer = 0; // not used on 64-bit | |||
2105 | segInfo->page_count = pageStarts.back().first + 1; | |||
2106 | ||||
2107 | uint16_t *starts = segInfo->page_start; | |||
2108 | for (size_t i = 0; i < segInfo->page_count; ++i) | |||
2109 | starts[i] = DYLD_CHAINED_PTR_START_NONE; | |||
2110 | ||||
2111 | for (auto [pageIdx, startAddr] : pageStarts) | |||
2112 | starts[pageIdx] = startAddr; | |||
2113 | return segInfo->size; | |||
2114 | } | |||
2115 | ||||
2116 | static size_t importEntrySize(int format) { | |||
2117 | switch (format) { | |||
2118 | case DYLD_CHAINED_IMPORT: | |||
2119 | return sizeof(dyld_chained_import); | |||
2120 | case DYLD_CHAINED_IMPORT_ADDEND: | |||
2121 | return sizeof(dyld_chained_import_addend); | |||
2122 | case DYLD_CHAINED_IMPORT_ADDEND64: | |||
2123 | return sizeof(dyld_chained_import_addend64); | |||
2124 | default: | |||
2125 | llvm_unreachable("Unknown import format")::llvm::llvm_unreachable_internal("Unknown import format", "lld/MachO/SyntheticSections.cpp" , 2125); | |||
2126 | } | |||
2127 | } | |||
2128 | ||||
2129 | // This is step 3 of the algorithm described in the class comment of | |||
2130 | // ChainedFixupsSection. | |||
2131 | // | |||
2132 | // LC_DYLD_CHAINED_FIXUPS data consists of (in this order): | |||
2133 | // * A dyld_chained_fixups_header | |||
2134 | // * A dyld_chained_starts_in_image | |||
2135 | // * One dyld_chained_starts_in_segment per segment | |||
2136 | // * List of all imports (dyld_chained_import, dyld_chained_import_addend, or | |||
2137 | // dyld_chained_import_addend64) | |||
2138 | // * Names of imported symbols | |||
2139 | void ChainedFixupsSection::writeTo(uint8_t *buf) const { | |||
2140 | auto *header = reinterpret_cast<dyld_chained_fixups_header *>(buf); | |||
2141 | header->fixups_version = 0; | |||
2142 | header->imports_count = bindings.size(); | |||
2143 | header->imports_format = importFormat; | |||
2144 | header->symbols_format = 0; | |||
2145 | ||||
2146 | buf += alignTo<8>(sizeof(*header)); | |||
2147 | ||||
2148 | auto curOffset = [&buf, &header]() -> uint32_t { | |||
2149 | return buf - reinterpret_cast<uint8_t *>(header); | |||
2150 | }; | |||
2151 | ||||
2152 | header->starts_offset = curOffset(); | |||
2153 | ||||
2154 | auto *imageInfo = reinterpret_cast<dyld_chained_starts_in_image *>(buf); | |||
2155 | imageInfo->seg_count = outputSegments.size(); | |||
2156 | uint32_t *segStarts = imageInfo->seg_info_offset; | |||
2157 | ||||
2158 | // dyld_chained_starts_in_image ends in a flexible array member containing an | |||
2159 | // uint32_t for each segment. Leave room for it, and fill it via segStarts. | |||
2160 | buf += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset)__builtin_offsetof(dyld_chained_starts_in_image, seg_info_offset ) + | |||
2161 | outputSegments.size() * sizeof(uint32_t)); | |||
2162 | ||||
2163 | // Initialize all offsets to 0, which indicates that the segment does not have | |||
2164 | // fixups. Those that do have them will be filled in below. | |||
2165 | for (size_t i = 0; i < outputSegments.size(); ++i) | |||
2166 | segStarts[i] = 0; | |||
2167 | ||||
2168 | for (const SegmentInfo &seg : fixupSegments) { | |||
2169 | segStarts[seg.oseg->index] = curOffset() - header->starts_offset; | |||
2170 | buf += seg.writeTo(buf); | |||
2171 | } | |||
2172 | ||||
2173 | // Write imports table. | |||
2174 | header->imports_offset = curOffset(); | |||
2175 | uint64_t nameOffset = 0; | |||
2176 | for (auto [import, idx] : bindings) { | |||
2177 | const Symbol &sym = *import.first; | |||
2178 | int16_t libOrdinal = needsWeakBind(sym) | |||
2179 | ? (int64_t)BIND_SPECIAL_DYLIB_WEAK_LOOKUP | |||
2180 | : ordinalForSymbol(sym); | |||
2181 | buf += writeImport(buf, importFormat, libOrdinal, sym.isWeakRef(), | |||
2182 | nameOffset, import.second); | |||
2183 | nameOffset += sym.getName().size() + 1; | |||
2184 | } | |||
2185 | ||||
2186 | // Write imported symbol names. | |||
2187 | header->symbols_offset = curOffset(); | |||
2188 | for (auto [import, idx] : bindings) { | |||
2189 | StringRef name = import.first->getName(); | |||
2190 | memcpy(buf, name.data(), name.size()); | |||
2191 | buf += name.size() + 1; // account for null terminator | |||
2192 | } | |||
2193 | ||||
2194 | assert(curOffset() == getRawSize())(static_cast <bool> (curOffset() == getRawSize()) ? void (0) : __assert_fail ("curOffset() == getRawSize()", "lld/MachO/SyntheticSections.cpp" , 2194, __extension__ __PRETTY_FUNCTION__)); | |||
2195 | } | |||
2196 | ||||
2197 | // This is step 2 of the algorithm described in the class comment of | |||
2198 | // ChainedFixupsSection. | |||
2199 | void ChainedFixupsSection::finalizeContents() { | |||
2200 | assert(target->wordSize == 8 && "Only 64-bit platforms are supported")(static_cast <bool> (target->wordSize == 8 && "Only 64-bit platforms are supported") ? void (0) : __assert_fail ("target->wordSize == 8 && \"Only 64-bit platforms are supported\"" , "lld/MachO/SyntheticSections.cpp", 2200, __extension__ __PRETTY_FUNCTION__ )); | |||
2201 | assert(config->emitChainedFixups)(static_cast <bool> (config->emitChainedFixups) ? void (0) : __assert_fail ("config->emitChainedFixups", "lld/MachO/SyntheticSections.cpp" , 2201, __extension__ __PRETTY_FUNCTION__)); | |||
2202 | ||||
2203 | if (!isUInt<32>(symtabSize)) | |||
2204 | error("cannot encode chained fixups: imported symbols table size " + | |||
2205 | Twine(symtabSize) + " exceeds 4 GiB"); | |||
2206 | ||||
2207 | if (needsLargeAddend || !isUInt<23>(symtabSize)) | |||
2208 | importFormat = DYLD_CHAINED_IMPORT_ADDEND64; | |||
2209 | else if (needsAddend) | |||
2210 | importFormat = DYLD_CHAINED_IMPORT_ADDEND; | |||
2211 | else | |||
2212 | importFormat = DYLD_CHAINED_IMPORT; | |||
2213 | ||||
2214 | for (Location &loc : locations) | |||
2215 | loc.offset = | |||
2216 | loc.isec->parent->getSegmentOffset() + loc.isec->getOffset(loc.offset); | |||
2217 | ||||
2218 | llvm::sort(locations, [](const Location &a, const Location &b) { | |||
2219 | const OutputSegment *segA = a.isec->parent->parent; | |||
2220 | const OutputSegment *segB = b.isec->parent->parent; | |||
2221 | if (segA == segB) | |||
2222 | return a.offset < b.offset; | |||
2223 | return segA->addr < segB->addr; | |||
2224 | }); | |||
2225 | ||||
2226 | auto sameSegment = [](const Location &a, const Location &b) { | |||
2227 | return a.isec->parent->parent == b.isec->parent->parent; | |||
2228 | }; | |||
2229 | ||||
2230 | const uint64_t pageSize = target->getPageSize(); | |||
2231 | for (size_t i = 0, count = locations.size(); i < count;) { | |||
2232 | const Location &firstLoc = locations[i]; | |||
2233 | fixupSegments.emplace_back(firstLoc.isec->parent->parent); | |||
2234 | while (i < count && sameSegment(locations[i], firstLoc)) { | |||
2235 | uint32_t pageIdx = locations[i].offset / pageSize; | |||
2236 | fixupSegments.back().pageStarts.emplace_back( | |||
2237 | pageIdx, locations[i].offset % pageSize); | |||
2238 | ++i; | |||
2239 | while (i < count && sameSegment(locations[i], firstLoc) && | |||
2240 | locations[i].offset / pageSize == pageIdx) | |||
2241 | ++i; | |||
2242 | } | |||
2243 | } | |||
2244 | ||||
2245 | // Compute expected encoded size. | |||
2246 | size = alignTo<8>(sizeof(dyld_chained_fixups_header)); | |||
2247 | size += alignTo<8>(offsetof(dyld_chained_starts_in_image, seg_info_offset)__builtin_offsetof(dyld_chained_starts_in_image, seg_info_offset ) + | |||
2248 | outputSegments.size() * sizeof(uint32_t)); | |||
2249 | for (const SegmentInfo &seg : fixupSegments) | |||
2250 | size += seg.getSize(); | |||
2251 | size += importEntrySize(importFormat) * bindings.size(); | |||
2252 | size += symtabSize; | |||
2253 | } | |||
2254 | ||||
2255 | template SymtabSection *macho::makeSymtabSection<LP64>(StringTableSection &); | |||
2256 | template SymtabSection *macho::makeSymtabSection<ILP32>(StringTableSection &); |
1 | //===-- llvm/Support/MathExtras.h - Useful math functions -------*- 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 contains some functions that are useful for math stuff. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_SUPPORT_MATHEXTRAS_H |
14 | #define LLVM_SUPPORT_MATHEXTRAS_H |
15 | |
16 | #include "llvm/ADT/bit.h" |
17 | #include "llvm/Support/Compiler.h" |
18 | #include <cassert> |
19 | #include <climits> |
20 | #include <cstdint> |
21 | #include <cstring> |
22 | #include <limits> |
23 | #include <type_traits> |
24 | |
25 | namespace llvm { |
26 | |
27 | /// The behavior an operation has on an input of 0. |
28 | enum ZeroBehavior { |
29 | /// The returned value is undefined. |
30 | ZB_Undefined, |
31 | /// The returned value is numeric_limits<T>::max() |
32 | ZB_Max |
33 | }; |
34 | |
35 | /// Mathematical constants. |
36 | namespace numbers { |
37 | // TODO: Track C++20 std::numbers. |
38 | // TODO: Favor using the hexadecimal FP constants (requires C++17). |
39 | constexpr double e = 2.7182818284590452354, // (0x1.5bf0a8b145749P+1) https://oeis.org/A001113 |
40 | egamma = .57721566490153286061, // (0x1.2788cfc6fb619P-1) https://oeis.org/A001620 |
41 | ln2 = .69314718055994530942, // (0x1.62e42fefa39efP-1) https://oeis.org/A002162 |
42 | ln10 = 2.3025850929940456840, // (0x1.24bb1bbb55516P+1) https://oeis.org/A002392 |
43 | log2e = 1.4426950408889634074, // (0x1.71547652b82feP+0) |
44 | log10e = .43429448190325182765, // (0x1.bcb7b1526e50eP-2) |
45 | pi = 3.1415926535897932385, // (0x1.921fb54442d18P+1) https://oeis.org/A000796 |
46 | inv_pi = .31830988618379067154, // (0x1.45f306bc9c883P-2) https://oeis.org/A049541 |
47 | sqrtpi = 1.7724538509055160273, // (0x1.c5bf891b4ef6bP+0) https://oeis.org/A002161 |
48 | inv_sqrtpi = .56418958354775628695, // (0x1.20dd750429b6dP-1) https://oeis.org/A087197 |
49 | sqrt2 = 1.4142135623730950488, // (0x1.6a09e667f3bcdP+0) https://oeis.org/A00219 |
50 | inv_sqrt2 = .70710678118654752440, // (0x1.6a09e667f3bcdP-1) |
51 | sqrt3 = 1.7320508075688772935, // (0x1.bb67ae8584caaP+0) https://oeis.org/A002194 |
52 | inv_sqrt3 = .57735026918962576451, // (0x1.279a74590331cP-1) |
53 | phi = 1.6180339887498948482; // (0x1.9e3779b97f4a8P+0) https://oeis.org/A001622 |
54 | constexpr float ef = 2.71828183F, // (0x1.5bf0a8P+1) https://oeis.org/A001113 |
55 | egammaf = .577215665F, // (0x1.2788d0P-1) https://oeis.org/A001620 |
56 | ln2f = .693147181F, // (0x1.62e430P-1) https://oeis.org/A002162 |
57 | ln10f = 2.30258509F, // (0x1.26bb1cP+1) https://oeis.org/A002392 |
58 | log2ef = 1.44269504F, // (0x1.715476P+0) |
59 | log10ef = .434294482F, // (0x1.bcb7b2P-2) |
60 | pif = 3.14159265F, // (0x1.921fb6P+1) https://oeis.org/A000796 |
61 | inv_pif = .318309886F, // (0x1.45f306P-2) https://oeis.org/A049541 |
62 | sqrtpif = 1.77245385F, // (0x1.c5bf8aP+0) https://oeis.org/A002161 |
63 | inv_sqrtpif = .564189584F, // (0x1.20dd76P-1) https://oeis.org/A087197 |
64 | sqrt2f = 1.41421356F, // (0x1.6a09e6P+0) https://oeis.org/A002193 |
65 | inv_sqrt2f = .707106781F, // (0x1.6a09e6P-1) |
66 | sqrt3f = 1.73205081F, // (0x1.bb67aeP+0) https://oeis.org/A002194 |
67 | inv_sqrt3f = .577350269F, // (0x1.279a74P-1) |
68 | phif = 1.61803399F; // (0x1.9e377aP+0) https://oeis.org/A001622 |
69 | } // namespace numbers |
70 | |
71 | /// Count number of 0's from the least significant bit to the most |
72 | /// stopping at the first 1. |
73 | /// |
74 | /// Only unsigned integral types are allowed. |
75 | /// |
76 | /// Returns std::numeric_limits<T>::digits on an input of 0. |
77 | template <typename T> unsigned countTrailingZeros(T Val) { |
78 | static_assert(std::is_unsigned_v<T>, |
79 | "Only unsigned integral types are allowed."); |
80 | return llvm::countr_zero(Val); |
81 | } |
82 | |
83 | /// Count number of 0's from the most significant bit to the least |
84 | /// stopping at the first 1. |
85 | /// |
86 | /// Only unsigned integral types are allowed. |
87 | /// |
88 | /// Returns std::numeric_limits<T>::digits on an input of 0. |
89 | template <typename T> unsigned countLeadingZeros(T Val) { |
90 | static_assert(std::is_unsigned_v<T>, |
91 | "Only unsigned integral types are allowed."); |
92 | return llvm::countl_zero(Val); |
93 | } |
94 | |
95 | /// Get the index of the first set bit starting from the least |
96 | /// significant bit. |
97 | /// |
98 | /// Only unsigned integral types are allowed. |
99 | /// |
100 | /// \param ZB the behavior on an input of 0. |
101 | template <typename T> T findFirstSet(T Val, ZeroBehavior ZB = ZB_Max) { |
102 | if (ZB == ZB_Max && Val == 0) |
103 | return std::numeric_limits<T>::max(); |
104 | |
105 | return llvm::countr_zero(Val); |
106 | } |
107 | |
108 | /// Create a bitmask with the N right-most bits set to 1, and all other |
109 | /// bits set to 0. Only unsigned types are allowed. |
110 | template <typename T> T maskTrailingOnes(unsigned N) { |
111 | static_assert(std::is_unsigned<T>::value, "Invalid type!"); |
112 | const unsigned Bits = CHAR_BIT8 * sizeof(T); |
113 | assert(N <= Bits && "Invalid bit index")(static_cast <bool> (N <= Bits && "Invalid bit index" ) ? void (0) : __assert_fail ("N <= Bits && \"Invalid bit index\"" , "llvm/include/llvm/Support/MathExtras.h", 113, __extension__ __PRETTY_FUNCTION__)); |
114 | return N == 0 ? 0 : (T(-1) >> (Bits - N)); |
115 | } |
116 | |
117 | /// Create a bitmask with the N left-most bits set to 1, and all other |
118 | /// bits set to 0. Only unsigned types are allowed. |
119 | template <typename T> T maskLeadingOnes(unsigned N) { |
120 | return ~maskTrailingOnes<T>(CHAR_BIT8 * sizeof(T) - N); |
121 | } |
122 | |
123 | /// Create a bitmask with the N right-most bits set to 0, and all other |
124 | /// bits set to 1. Only unsigned types are allowed. |
125 | template <typename T> T maskTrailingZeros(unsigned N) { |
126 | return maskLeadingOnes<T>(CHAR_BIT8 * sizeof(T) - N); |
127 | } |
128 | |
129 | /// Create a bitmask with the N left-most bits set to 0, and all other |
130 | /// bits set to 1. Only unsigned types are allowed. |
131 | template <typename T> T maskLeadingZeros(unsigned N) { |
132 | return maskTrailingOnes<T>(CHAR_BIT8 * sizeof(T) - N); |
133 | } |
134 | |
135 | /// Get the index of the last set bit starting from the least |
136 | /// significant bit. |
137 | /// |
138 | /// Only unsigned integral types are allowed. |
139 | /// |
140 | /// \param ZB the behavior on an input of 0. |
141 | template <typename T> T findLastSet(T Val, ZeroBehavior ZB = ZB_Max) { |
142 | if (ZB == ZB_Max && Val == 0) |
143 | return std::numeric_limits<T>::max(); |
144 | |
145 | // Use ^ instead of - because both gcc and llvm can remove the associated ^ |
146 | // in the __builtin_clz intrinsic on x86. |
147 | return llvm::countl_zero(Val) ^ (std::numeric_limits<T>::digits - 1); |
148 | } |
149 | |
150 | /// Macro compressed bit reversal table for 256 bits. |
151 | /// |
152 | /// http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable |
153 | static const unsigned char BitReverseTable256[256] = { |
154 | #define R2(n) n, n + 2 * 64, n + 1 * 64, n + 3 * 64 |
155 | #define R4(n) R2(n), R2(n + 2 * 16), R2(n + 1 * 16), R2(n + 3 * 16) |
156 | #define R6(n) R4(n), R4(n + 2 * 4), R4(n + 1 * 4), R4(n + 3 * 4) |
157 | R6(0), R6(2), R6(1), R6(3) |
158 | #undef R2 |
159 | #undef R4 |
160 | #undef R6 |
161 | }; |
162 | |
163 | /// Reverse the bits in \p Val. |
164 | template <typename T> T reverseBits(T Val) { |
165 | #if __has_builtin(__builtin_bitreverse8)1 |
166 | if constexpr (std::is_same_v<T, uint8_t>) |
167 | return __builtin_bitreverse8(Val); |
168 | #endif |
169 | #if __has_builtin(__builtin_bitreverse16)1 |
170 | if constexpr (std::is_same_v<T, uint16_t>) |
171 | return __builtin_bitreverse16(Val); |
172 | #endif |
173 | #if __has_builtin(__builtin_bitreverse32)1 |
174 | if constexpr (std::is_same_v<T, uint32_t>) |
175 | return __builtin_bitreverse32(Val); |
176 | #endif |
177 | #if __has_builtin(__builtin_bitreverse64)1 |
178 | if constexpr (std::is_same_v<T, uint64_t>) |
179 | return __builtin_bitreverse64(Val); |
180 | #endif |
181 | |
182 | unsigned char in[sizeof(Val)]; |
183 | unsigned char out[sizeof(Val)]; |
184 | std::memcpy(in, &Val, sizeof(Val)); |
185 | for (unsigned i = 0; i < sizeof(Val); ++i) |
186 | out[(sizeof(Val) - i) - 1] = BitReverseTable256[in[i]]; |
187 | std::memcpy(&Val, out, sizeof(Val)); |
188 | return Val; |
189 | } |
190 | |
191 | // NOTE: The following support functions use the _32/_64 extensions instead of |
192 | // type overloading so that signed and unsigned integers can be used without |
193 | // ambiguity. |
194 | |
195 | /// Return the high 32 bits of a 64 bit value. |
196 | constexpr inline uint32_t Hi_32(uint64_t Value) { |
197 | return static_cast<uint32_t>(Value >> 32); |
198 | } |
199 | |
200 | /// Return the low 32 bits of a 64 bit value. |
201 | constexpr inline uint32_t Lo_32(uint64_t Value) { |
202 | return static_cast<uint32_t>(Value); |
203 | } |
204 | |
205 | /// Make a 64-bit integer from a high / low pair of 32-bit integers. |
206 | constexpr inline uint64_t Make_64(uint32_t High, uint32_t Low) { |
207 | return ((uint64_t)High << 32) | (uint64_t)Low; |
208 | } |
209 | |
210 | /// Checks if an integer fits into the given bit width. |
211 | template <unsigned N> constexpr inline bool isInt(int64_t x) { |
212 | if constexpr (N == 8) |
213 | return static_cast<int8_t>(x) == x; |
214 | if constexpr (N == 16) |
215 | return static_cast<int16_t>(x) == x; |
216 | if constexpr (N == 32) |
217 | return static_cast<int32_t>(x) == x; |
218 | if constexpr (N < 64) |
219 | return -(INT64_C(1)1L << (N - 1)) <= x && x < (INT64_C(1)1L << (N - 1)); |
220 | (void)x; // MSVC v19.25 warns that x is unused. |
221 | return true; |
222 | } |
223 | |
224 | /// Checks if a signed integer is an N bit number shifted left by S. |
225 | template <unsigned N, unsigned S> |
226 | constexpr inline bool isShiftedInt(int64_t x) { |
227 | static_assert( |
228 | N > 0, "isShiftedInt<0> doesn't make sense (refers to a 0-bit number."); |
229 | static_assert(N + S <= 64, "isShiftedInt<N, S> with N + S > 64 is too wide."); |
230 | return isInt<N + S>(x) && (x % (UINT64_C(1)1UL << S) == 0); |
231 | } |
232 | |
233 | /// Checks if an unsigned integer fits into the given bit width. |
234 | template <unsigned N> constexpr inline bool isUInt(uint64_t x) { |
235 | static_assert(N > 0, "isUInt<0> doesn't make sense"); |
236 | if constexpr (N == 8) |
237 | return static_cast<uint8_t>(x) == x; |
238 | if constexpr (N == 16) |
239 | return static_cast<uint16_t>(x) == x; |
240 | if constexpr (N == 32) |
241 | return static_cast<uint32_t>(x) == x; |
242 | if constexpr (N < 64) |
243 | return x < (UINT64_C(1)1UL << (N)); |
244 | (void)x; // MSVC v19.25 warns that x is unused. |
245 | return true; |
246 | } |
247 | |
248 | /// Checks if a unsigned integer is an N bit number shifted left by S. |
249 | template <unsigned N, unsigned S> |
250 | constexpr inline bool isShiftedUInt(uint64_t x) { |
251 | static_assert( |
252 | N > 0, "isShiftedUInt<0> doesn't make sense (refers to a 0-bit number)"); |
253 | static_assert(N + S <= 64, |
254 | "isShiftedUInt<N, S> with N + S > 64 is too wide."); |
255 | // Per the two static_asserts above, S must be strictly less than 64. So |
256 | // 1 << S is not undefined behavior. |
257 | return isUInt<N + S>(x) && (x % (UINT64_C(1)1UL << S) == 0); |
258 | } |
259 | |
260 | /// Gets the maximum value for a N-bit unsigned integer. |
261 | inline uint64_t maxUIntN(uint64_t N) { |
262 | assert(N > 0 && N <= 64 && "integer width out of range")(static_cast <bool> (N > 0 && N <= 64 && "integer width out of range") ? void (0) : __assert_fail ("N > 0 && N <= 64 && \"integer width out of range\"" , "llvm/include/llvm/Support/MathExtras.h", 262, __extension__ __PRETTY_FUNCTION__)); |
263 | |
264 | // uint64_t(1) << 64 is undefined behavior, so we can't do |
265 | // (uint64_t(1) << N) - 1 |
266 | // without checking first that N != 64. But this works and doesn't have a |
267 | // branch. |
268 | return UINT64_MAX(18446744073709551615UL) >> (64 - N); |
269 | } |
270 | |
271 | /// Gets the minimum value for a N-bit signed integer. |
272 | inline int64_t minIntN(int64_t N) { |
273 | assert(N > 0 && N <= 64 && "integer width out of range")(static_cast <bool> (N > 0 && N <= 64 && "integer width out of range") ? void (0) : __assert_fail ("N > 0 && N <= 64 && \"integer width out of range\"" , "llvm/include/llvm/Support/MathExtras.h", 273, __extension__ __PRETTY_FUNCTION__)); |
274 | |
275 | return UINT64_C(1)1UL + ~(UINT64_C(1)1UL << (N - 1)); |
276 | } |
277 | |
278 | /// Gets the maximum value for a N-bit signed integer. |
279 | inline int64_t maxIntN(int64_t N) { |
280 | assert(N > 0 && N <= 64 && "integer width out of range")(static_cast <bool> (N > 0 && N <= 64 && "integer width out of range") ? void (0) : __assert_fail ("N > 0 && N <= 64 && \"integer width out of range\"" , "llvm/include/llvm/Support/MathExtras.h", 280, __extension__ __PRETTY_FUNCTION__)); |
281 | |
282 | // This relies on two's complement wraparound when N == 64, so we convert to |
283 | // int64_t only at the very end to avoid UB. |
284 | return (UINT64_C(1)1UL << (N - 1)) - 1; |
285 | } |
286 | |
287 | /// Checks if an unsigned integer fits into the given (dynamic) bit width. |
288 | inline bool isUIntN(unsigned N, uint64_t x) { |
289 | return N >= 64 || x <= maxUIntN(N); |
290 | } |
291 | |
292 | /// Checks if an signed integer fits into the given (dynamic) bit width. |
293 | inline bool isIntN(unsigned N, int64_t x) { |
294 | return N >= 64 || (minIntN(N) <= x && x <= maxIntN(N)); |
295 | } |
296 | |
297 | /// Return true if the argument is a non-empty sequence of ones starting at the |
298 | /// least significant bit with the remainder zero (32 bit version). |
299 | /// Ex. isMask_32(0x0000FFFFU) == true. |
300 | constexpr inline bool isMask_32(uint32_t Value) { |
301 | return Value && ((Value + 1) & Value) == 0; |
302 | } |
303 | |
304 | /// Return true if the argument is a non-empty sequence of ones starting at the |
305 | /// least significant bit with the remainder zero (64 bit version). |
306 | constexpr inline bool isMask_64(uint64_t Value) { |
307 | return Value && ((Value + 1) & Value) == 0; |
308 | } |
309 | |
310 | /// Return true if the argument contains a non-empty sequence of ones with the |
311 | /// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true. |
312 | constexpr inline bool isShiftedMask_32(uint32_t Value) { |
313 | return Value && isMask_32((Value - 1) | Value); |
314 | } |
315 | |
316 | /// Return true if the argument contains a non-empty sequence of ones with the |
317 | /// remainder zero (64 bit version.) |
318 | constexpr inline bool isShiftedMask_64(uint64_t Value) { |
319 | return Value && isMask_64((Value - 1) | Value); |
320 | } |
321 | |
322 | /// Return true if the argument is a power of two > 0. |
323 | /// Ex. isPowerOf2_32(0x00100000U) == true (32 bit edition.) |
324 | constexpr inline bool isPowerOf2_32(uint32_t Value) { |
325 | return llvm::has_single_bit(Value); |
326 | } |
327 | |
328 | /// Return true if the argument is a power of two > 0 (64 bit edition.) |
329 | constexpr inline bool isPowerOf2_64(uint64_t Value) { |
330 | return llvm::has_single_bit(Value); |
331 | } |
332 | |
333 | /// Count the number of ones from the most significant bit to the first |
334 | /// zero bit. |
335 | /// |
336 | /// Ex. countLeadingOnes(0xFF0FFF00) == 8. |
337 | /// Only unsigned integral types are allowed. |
338 | /// |
339 | /// Returns std::numeric_limits<T>::digits on an input of all ones. |
340 | template <typename T> unsigned countLeadingOnes(T Value) { |
341 | static_assert(std::is_unsigned_v<T>, |
342 | "Only unsigned integral types are allowed."); |
343 | return llvm::countl_one<T>(Value); |
344 | } |
345 | |
346 | /// Count the number of ones from the least significant bit to the first |
347 | /// zero bit. |
348 | /// |
349 | /// Ex. countTrailingOnes(0x00FF00FF) == 8. |
350 | /// Only unsigned integral types are allowed. |
351 | /// |
352 | /// Returns std::numeric_limits<T>::digits on an input of all ones. |
353 | template <typename T> unsigned countTrailingOnes(T Value) { |
354 | static_assert(std::is_unsigned_v<T>, |
355 | "Only unsigned integral types are allowed."); |
356 | return llvm::countr_one<T>(Value); |
357 | } |
358 | |
359 | /// Count the number of set bits in a value. |
360 | /// Ex. countPopulation(0xF000F000) = 8 |
361 | /// Returns 0 if the word is zero. |
362 | template <typename T> |
363 | inline unsigned countPopulation(T Value) { |
364 | static_assert(std::is_unsigned_v<T>, |
365 | "Only unsigned integral types are allowed."); |
366 | return (unsigned)llvm::popcount(Value); |
367 | } |
368 | |
369 | /// Return true if the argument contains a non-empty sequence of ones with the |
370 | /// remainder zero (32 bit version.) Ex. isShiftedMask_32(0x0000FF00U) == true. |
371 | /// If true, \p MaskIdx will specify the index of the lowest set bit and \p |
372 | /// MaskLen is updated to specify the length of the mask, else neither are |
373 | /// updated. |
374 | inline bool isShiftedMask_32(uint32_t Value, unsigned &MaskIdx, |
375 | unsigned &MaskLen) { |
376 | if (!isShiftedMask_32(Value)) |
377 | return false; |
378 | MaskIdx = llvm::countr_zero(Value); |
379 | MaskLen = llvm::popcount(Value); |
380 | return true; |
381 | } |
382 | |
383 | /// Return true if the argument contains a non-empty sequence of ones with the |
384 | /// remainder zero (64 bit version.) If true, \p MaskIdx will specify the index |
385 | /// of the lowest set bit and \p MaskLen is updated to specify the length of the |
386 | /// mask, else neither are updated. |
387 | inline bool isShiftedMask_64(uint64_t Value, unsigned &MaskIdx, |
388 | unsigned &MaskLen) { |
389 | if (!isShiftedMask_64(Value)) |
390 | return false; |
391 | MaskIdx = llvm::countr_zero(Value); |
392 | MaskLen = llvm::popcount(Value); |
393 | return true; |
394 | } |
395 | |
396 | /// Compile time Log2. |
397 | /// Valid only for positive powers of two. |
398 | template <size_t kValue> constexpr inline size_t CTLog2() { |
399 | static_assert(kValue > 0 && llvm::isPowerOf2_64(kValue), |
400 | "Value is not a valid power of 2"); |
401 | return 1 + CTLog2<kValue / 2>(); |
402 | } |
403 | |
404 | template <> constexpr inline size_t CTLog2<1>() { return 0; } |
405 | |
406 | /// Return the floor log base 2 of the specified value, -1 if the value is zero. |
407 | /// (32 bit edition.) |
408 | /// Ex. Log2_32(32) == 5, Log2_32(1) == 0, Log2_32(0) == -1, Log2_32(6) == 2 |
409 | inline unsigned Log2_32(uint32_t Value) { |
410 | return 31 - llvm::countl_zero(Value); |
411 | } |
412 | |
413 | /// Return the floor log base 2 of the specified value, -1 if the value is zero. |
414 | /// (64 bit edition.) |
415 | inline unsigned Log2_64(uint64_t Value) { |
416 | return 63 - llvm::countl_zero(Value); |
417 | } |
418 | |
419 | /// Return the ceil log base 2 of the specified value, 32 if the value is zero. |
420 | /// (32 bit edition). |
421 | /// Ex. Log2_32_Ceil(32) == 5, Log2_32_Ceil(1) == 0, Log2_32_Ceil(6) == 3 |
422 | inline unsigned Log2_32_Ceil(uint32_t Value) { |
423 | return 32 - llvm::countl_zero(Value - 1); |
424 | } |
425 | |
426 | /// Return the ceil log base 2 of the specified value, 64 if the value is zero. |
427 | /// (64 bit edition.) |
428 | inline unsigned Log2_64_Ceil(uint64_t Value) { |
429 | return 64 - llvm::countl_zero(Value - 1); |
430 | } |
431 | |
432 | /// This function takes a 64-bit integer and returns the bit equivalent double. |
433 | inline double BitsToDouble(uint64_t Bits) { |
434 | static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes"); |
435 | return llvm::bit_cast<double>(Bits); |
436 | } |
437 | |
438 | /// This function takes a 32-bit integer and returns the bit equivalent float. |
439 | inline float BitsToFloat(uint32_t Bits) { |
440 | static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes"); |
441 | return llvm::bit_cast<float>(Bits); |
442 | } |
443 | |
444 | /// This function takes a double and returns the bit equivalent 64-bit integer. |
445 | /// Note that copying doubles around changes the bits of NaNs on some hosts, |
446 | /// notably x86, so this routine cannot be used if these bits are needed. |
447 | inline uint64_t DoubleToBits(double Double) { |
448 | static_assert(sizeof(uint64_t) == sizeof(double), "Unexpected type sizes"); |
449 | return llvm::bit_cast<uint64_t>(Double); |
450 | } |
451 | |
452 | /// This function takes a float and returns the bit equivalent 32-bit integer. |
453 | /// Note that copying floats around changes the bits of NaNs on some hosts, |
454 | /// notably x86, so this routine cannot be used if these bits are needed. |
455 | inline uint32_t FloatToBits(float Float) { |
456 | static_assert(sizeof(uint32_t) == sizeof(float), "Unexpected type sizes"); |
457 | return llvm::bit_cast<uint32_t>(Float); |
458 | } |
459 | |
460 | /// A and B are either alignments or offsets. Return the minimum alignment that |
461 | /// may be assumed after adding the two together. |
462 | constexpr inline uint64_t MinAlign(uint64_t A, uint64_t B) { |
463 | // The largest power of 2 that divides both A and B. |
464 | // |
465 | // Replace "-Value" by "1+~Value" in the following commented code to avoid |
466 | // MSVC warning C4146 |
467 | // return (A | B) & -(A | B); |
468 | return (A | B) & (1 + ~(A | B)); |
469 | } |
470 | |
471 | /// Returns the next power of two (in 64-bits) that is strictly greater than A. |
472 | /// Returns zero on overflow. |
473 | constexpr inline uint64_t NextPowerOf2(uint64_t A) { |
474 | A |= (A >> 1); |
475 | A |= (A >> 2); |
476 | A |= (A >> 4); |
477 | A |= (A >> 8); |
478 | A |= (A >> 16); |
479 | A |= (A >> 32); |
480 | return A + 1; |
481 | } |
482 | |
483 | /// Returns the power of two which is less than or equal to the given value. |
484 | /// Essentially, it is a floor operation across the domain of powers of two. |
485 | inline uint64_t PowerOf2Floor(uint64_t A) { |
486 | return llvm::bit_floor(A); |
487 | } |
488 | |
489 | /// Returns the power of two which is greater than or equal to the given value. |
490 | /// Essentially, it is a ceil operation across the domain of powers of two. |
491 | inline uint64_t PowerOf2Ceil(uint64_t A) { |
492 | if (!A) |
493 | return 0; |
494 | return NextPowerOf2(A - 1); |
495 | } |
496 | |
497 | /// Returns the next integer (mod 2**64) that is greater than or equal to |
498 | /// \p Value and is a multiple of \p Align. \p Align must be non-zero. |
499 | /// |
500 | /// Examples: |
501 | /// \code |
502 | /// alignTo(5, 8) = 8 |
503 | /// alignTo(17, 8) = 24 |
504 | /// alignTo(~0LL, 8) = 0 |
505 | /// alignTo(321, 255) = 510 |
506 | /// \endcode |
507 | inline uint64_t alignTo(uint64_t Value, uint64_t Align) { |
508 | assert(Align != 0u && "Align can't be 0.")(static_cast <bool> (Align != 0u && "Align can't be 0." ) ? void (0) : __assert_fail ("Align != 0u && \"Align can't be 0.\"" , "llvm/include/llvm/Support/MathExtras.h", 508, __extension__ __PRETTY_FUNCTION__)); |
509 | return (Value + Align - 1) / Align * Align; |
510 | } |
511 | |
512 | inline uint64_t alignToPowerOf2(uint64_t Value, uint64_t Align) { |
513 | assert(Align != 0 && (Align & (Align - 1)) == 0 &&(static_cast <bool> (Align != 0 && (Align & (Align - 1)) == 0 && "Align must be a power of 2") ? void (0) : __assert_fail ("Align != 0 && (Align & (Align - 1)) == 0 && \"Align must be a power of 2\"" , "llvm/include/llvm/Support/MathExtras.h", 514, __extension__ __PRETTY_FUNCTION__)) |
514 | "Align must be a power of 2")(static_cast <bool> (Align != 0 && (Align & (Align - 1)) == 0 && "Align must be a power of 2") ? void (0) : __assert_fail ("Align != 0 && (Align & (Align - 1)) == 0 && \"Align must be a power of 2\"" , "llvm/include/llvm/Support/MathExtras.h", 514, __extension__ __PRETTY_FUNCTION__)); |
515 | return (Value + Align - 1) & -Align; |
516 | } |
517 | |
518 | /// If non-zero \p Skew is specified, the return value will be a minimal integer |
519 | /// that is greater than or equal to \p Size and equal to \p A * N + \p Skew for |
520 | /// some integer N. If \p Skew is larger than \p A, its value is adjusted to '\p |
521 | /// Skew mod \p A'. \p Align must be non-zero. |
522 | /// |
523 | /// Examples: |
524 | /// \code |
525 | /// alignTo(5, 8, 7) = 7 |
526 | /// alignTo(17, 8, 1) = 17 |
527 | /// alignTo(~0LL, 8, 3) = 3 |
528 | /// alignTo(321, 255, 42) = 552 |
529 | /// \endcode |
530 | inline uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew) { |
531 | assert(Align != 0u && "Align can't be 0.")(static_cast <bool> (Align != 0u && "Align can't be 0." ) ? void (0) : __assert_fail ("Align != 0u && \"Align can't be 0.\"" , "llvm/include/llvm/Support/MathExtras.h", 531, __extension__ __PRETTY_FUNCTION__)); |
532 | Skew %= Align; |
533 | return alignTo(Value - Skew, Align) + Skew; |
534 | } |
535 | |
536 | /// Returns the next integer (mod 2**64) that is greater than or equal to |
537 | /// \p Value and is a multiple of \c Align. \c Align must be non-zero. |
538 | template <uint64_t Align> constexpr inline uint64_t alignTo(uint64_t Value) { |
539 | static_assert(Align != 0u, "Align must be non-zero"); |
540 | return (Value + Align - 1) / Align * Align; |
541 | } |
542 | |
543 | /// Returns the integer ceil(Numerator / Denominator). |
544 | inline uint64_t divideCeil(uint64_t Numerator, uint64_t Denominator) { |
545 | return alignTo(Numerator, Denominator) / Denominator; |
546 | } |
547 | |
548 | /// Returns the integer nearest(Numerator / Denominator). |
549 | inline uint64_t divideNearest(uint64_t Numerator, uint64_t Denominator) { |
550 | return (Numerator + (Denominator / 2)) / Denominator; |
551 | } |
552 | |
553 | /// Returns the largest uint64_t less than or equal to \p Value and is |
554 | /// \p Skew mod \p Align. \p Align must be non-zero |
555 | inline uint64_t alignDown(uint64_t Value, uint64_t Align, uint64_t Skew = 0) { |
556 | assert(Align != 0u && "Align can't be 0.")(static_cast <bool> (Align != 0u && "Align can't be 0." ) ? void (0) : __assert_fail ("Align != 0u && \"Align can't be 0.\"" , "llvm/include/llvm/Support/MathExtras.h", 556, __extension__ __PRETTY_FUNCTION__)); |
557 | Skew %= Align; |
558 | return (Value - Skew) / Align * Align + Skew; |
559 | } |
560 | |
561 | /// Sign-extend the number in the bottom B bits of X to a 32-bit integer. |
562 | /// Requires 0 < B <= 32. |
563 | template <unsigned B> constexpr inline int32_t SignExtend32(uint32_t X) { |
564 | static_assert(B > 0, "Bit width can't be 0."); |
565 | static_assert(B <= 32, "Bit width out of range."); |
566 | return int32_t(X << (32 - B)) >> (32 - B); |
567 | } |
568 | |
569 | /// Sign-extend the number in the bottom B bits of X to a 32-bit integer. |
570 | /// Requires 0 < B <= 32. |
571 | inline int32_t SignExtend32(uint32_t X, unsigned B) { |
572 | assert(B > 0 && "Bit width can't be 0.")(static_cast <bool> (B > 0 && "Bit width can't be 0." ) ? void (0) : __assert_fail ("B > 0 && \"Bit width can't be 0.\"" , "llvm/include/llvm/Support/MathExtras.h", 572, __extension__ __PRETTY_FUNCTION__)); |
573 | assert(B <= 32 && "Bit width out of range.")(static_cast <bool> (B <= 32 && "Bit width out of range." ) ? void (0) : __assert_fail ("B <= 32 && \"Bit width out of range.\"" , "llvm/include/llvm/Support/MathExtras.h", 573, __extension__ __PRETTY_FUNCTION__)); |
574 | return int32_t(X << (32 - B)) >> (32 - B); |
575 | } |
576 | |
577 | /// Sign-extend the number in the bottom B bits of X to a 64-bit integer. |
578 | /// Requires 0 < B <= 64. |
579 | template <unsigned B> constexpr inline int64_t SignExtend64(uint64_t x) { |
580 | static_assert(B > 0, "Bit width can't be 0."); |
581 | static_assert(B <= 64, "Bit width out of range."); |
582 | return int64_t(x << (64 - B)) >> (64 - B); |
583 | } |
584 | |
585 | /// Sign-extend the number in the bottom B bits of X to a 64-bit integer. |
586 | /// Requires 0 < B <= 64. |
587 | inline int64_t SignExtend64(uint64_t X, unsigned B) { |
588 | assert(B > 0 && "Bit width can't be 0.")(static_cast <bool> (B > 0 && "Bit width can't be 0." ) ? void (0) : __assert_fail ("B > 0 && \"Bit width can't be 0.\"" , "llvm/include/llvm/Support/MathExtras.h", 588, __extension__ __PRETTY_FUNCTION__)); |
589 | assert(B <= 64 && "Bit width out of range.")(static_cast <bool> (B <= 64 && "Bit width out of range." ) ? void (0) : __assert_fail ("B <= 64 && \"Bit width out of range.\"" , "llvm/include/llvm/Support/MathExtras.h", 589, __extension__ __PRETTY_FUNCTION__)); |
590 | return int64_t(X << (64 - B)) >> (64 - B); |
591 | } |
592 | |
593 | /// Subtract two unsigned integers, X and Y, of type T and return the absolute |
594 | /// value of the result. |
595 | template <typename T> |
596 | std::enable_if_t<std::is_unsigned<T>::value, T> AbsoluteDifference(T X, T Y) { |
597 | return X > Y ? (X - Y) : (Y - X); |
598 | } |
599 | |
600 | /// Add two unsigned integers, X and Y, of type T. Clamp the result to the |
601 | /// maximum representable value of T on overflow. ResultOverflowed indicates if |
602 | /// the result is larger than the maximum representable value of type T. |
603 | template <typename T> |
604 | std::enable_if_t<std::is_unsigned<T>::value, T> |
605 | SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) { |
606 | bool Dummy; |
607 | bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; |
608 | // Hacker's Delight, p. 29 |
609 | T Z = X + Y; |
610 | Overflowed = (Z < X || Z < Y); |
611 | if (Overflowed) |
612 | return std::numeric_limits<T>::max(); |
613 | else |
614 | return Z; |
615 | } |
616 | |
617 | /// Add multiple unsigned integers of type T. Clamp the result to the |
618 | /// maximum representable value of T on overflow. |
619 | template <class T, class... Ts> |
620 | std::enable_if_t<std::is_unsigned_v<T>, T> SaturatingAdd(T X, T Y, T Z, |
621 | Ts... Args) { |
622 | bool Overflowed = false; |
623 | T XY = SaturatingAdd(X, Y, &Overflowed); |
624 | if (Overflowed) |
625 | return SaturatingAdd(std::numeric_limits<T>::max(), T(1), Args...); |
626 | return SaturatingAdd(XY, Z, Args...); |
627 | } |
628 | |
629 | /// Multiply two unsigned integers, X and Y, of type T. Clamp the result to the |
630 | /// maximum representable value of T on overflow. ResultOverflowed indicates if |
631 | /// the result is larger than the maximum representable value of type T. |
632 | template <typename T> |
633 | std::enable_if_t<std::is_unsigned<T>::value, T> |
634 | SaturatingMultiply(T X, T Y, bool *ResultOverflowed = nullptr) { |
635 | bool Dummy; |
636 | bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; |
637 | |
638 | // Hacker's Delight, p. 30 has a different algorithm, but we don't use that |
639 | // because it fails for uint16_t (where multiplication can have undefined |
640 | // behavior due to promotion to int), and requires a division in addition |
641 | // to the multiplication. |
642 | |
643 | Overflowed = false; |
644 | |
645 | // Log2(Z) would be either Log2Z or Log2Z + 1. |
646 | // Special case: if X or Y is 0, Log2_64 gives -1, and Log2Z |
647 | // will necessarily be less than Log2Max as desired. |
648 | int Log2Z = Log2_64(X) + Log2_64(Y); |
649 | const T Max = std::numeric_limits<T>::max(); |
650 | int Log2Max = Log2_64(Max); |
651 | if (Log2Z < Log2Max) { |
652 | return X * Y; |
653 | } |
654 | if (Log2Z > Log2Max) { |
655 | Overflowed = true; |
656 | return Max; |
657 | } |
658 | |
659 | // We're going to use the top bit, and maybe overflow one |
660 | // bit past it. Multiply all but the bottom bit then add |
661 | // that on at the end. |
662 | T Z = (X >> 1) * Y; |
663 | if (Z & ~(Max >> 1)) { |
664 | Overflowed = true; |
665 | return Max; |
666 | } |
667 | Z <<= 1; |
668 | if (X & 1) |
669 | return SaturatingAdd(Z, Y, ResultOverflowed); |
670 | |
671 | return Z; |
672 | } |
673 | |
674 | /// Multiply two unsigned integers, X and Y, and add the unsigned integer, A to |
675 | /// the product. Clamp the result to the maximum representable value of T on |
676 | /// overflow. ResultOverflowed indicates if the result is larger than the |
677 | /// maximum representable value of type T. |
678 | template <typename T> |
679 | std::enable_if_t<std::is_unsigned<T>::value, T> |
680 | SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed = nullptr) { |
681 | bool Dummy; |
682 | bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy; |
683 | |
684 | T Product = SaturatingMultiply(X, Y, &Overflowed); |
685 | if (Overflowed) |
686 | return Product; |
687 | |
688 | return SaturatingAdd(A, Product, &Overflowed); |
689 | } |
690 | |
691 | /// Use this rather than HUGE_VALF; the latter causes warnings on MSVC. |
692 | extern const float huge_valf; |
693 | |
694 | |
695 | /// Add two signed integers, computing the two's complement truncated result, |
696 | /// returning true if overflow occurred. |
697 | template <typename T> |
698 | std::enable_if_t<std::is_signed<T>::value, T> AddOverflow(T X, T Y, T &Result) { |
699 | #if __has_builtin(__builtin_add_overflow)1 |
700 | return __builtin_add_overflow(X, Y, &Result); |
701 | #else |
702 | // Perform the unsigned addition. |
703 | using U = std::make_unsigned_t<T>; |
704 | const U UX = static_cast<U>(X); |
705 | const U UY = static_cast<U>(Y); |
706 | const U UResult = UX + UY; |
707 | |
708 | // Convert to signed. |
709 | Result = static_cast<T>(UResult); |
710 | |
711 | // Adding two positive numbers should result in a positive number. |
712 | if (X > 0 && Y > 0) |
713 | return Result <= 0; |
714 | // Adding two negatives should result in a negative number. |
715 | if (X < 0 && Y < 0) |
716 | return Result >= 0; |
717 | return false; |
718 | #endif |
719 | } |
720 | |
721 | /// Subtract two signed integers, computing the two's complement truncated |
722 | /// result, returning true if an overflow ocurred. |
723 | template <typename T> |
724 | std::enable_if_t<std::is_signed<T>::value, T> SubOverflow(T X, T Y, T &Result) { |
725 | #if __has_builtin(__builtin_sub_overflow)1 |
726 | return __builtin_sub_overflow(X, Y, &Result); |
727 | #else |
728 | // Perform the unsigned addition. |
729 | using U = std::make_unsigned_t<T>; |
730 | const U UX = static_cast<U>(X); |
731 | const U UY = static_cast<U>(Y); |
732 | const U UResult = UX - UY; |
733 | |
734 | // Convert to signed. |
735 | Result = static_cast<T>(UResult); |
736 | |
737 | // Subtracting a positive number from a negative results in a negative number. |
738 | if (X <= 0 && Y > 0) |
739 | return Result >= 0; |
740 | // Subtracting a negative number from a positive results in a positive number. |
741 | if (X >= 0 && Y < 0) |
742 | return Result <= 0; |
743 | return false; |
744 | #endif |
745 | } |
746 | |
747 | /// Multiply two signed integers, computing the two's complement truncated |
748 | /// result, returning true if an overflow ocurred. |
749 | template <typename T> |
750 | std::enable_if_t<std::is_signed<T>::value, T> MulOverflow(T X, T Y, T &Result) { |
751 | // Perform the unsigned multiplication on absolute values. |
752 | using U = std::make_unsigned_t<T>; |
753 | const U UX = X < 0 ? (0 - static_cast<U>(X)) : static_cast<U>(X); |
754 | const U UY = Y < 0 ? (0 - static_cast<U>(Y)) : static_cast<U>(Y); |
755 | const U UResult = UX * UY; |
756 | |
757 | // Convert to signed. |
758 | const bool IsNegative = (X < 0) ^ (Y < 0); |
759 | Result = IsNegative ? (0 - UResult) : UResult; |
760 | |
761 | // If any of the args was 0, result is 0 and no overflow occurs. |
762 | if (UX == 0 || UY == 0) |
763 | return false; |
764 | |
765 | // UX and UY are in [1, 2^n], where n is the number of digits. |
766 | // Check how the max allowed absolute value (2^n for negative, 2^(n-1) for |
767 | // positive) divided by an argument compares to the other. |
768 | if (IsNegative) |
769 | return UX > (static_cast<U>(std::numeric_limits<T>::max()) + U(1)) / UY; |
770 | else |
771 | return UX > (static_cast<U>(std::numeric_limits<T>::max())) / UY; |
772 | } |
773 | |
774 | } // End llvm namespace |
775 | |
776 | #endif |
1 | //===-- llvm/ADT/bit.h - C++20 <bit> ----------------------------*- 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 | /// \file |
10 | /// This file implements the C++20 <bit> header. |
11 | /// |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #ifndef LLVM_ADT_BIT_H |
15 | #define LLVM_ADT_BIT_H |
16 | |
17 | #include "llvm/Support/Compiler.h" |
18 | #include <cstdint> |
19 | #include <limits> |
20 | #include <type_traits> |
21 | |
22 | #if !__has_builtin(__builtin_bit_cast)1 |
23 | #include <cstring> |
24 | #endif |
25 | |
26 | #if defined(_MSC_VER) && !defined(_DEBUG1) |
27 | #include <cstdlib> // for _byteswap_{ushort,ulong,uint64} |
28 | #endif |
29 | |
30 | #ifdef _MSC_VER |
31 | // Declare these intrinsics manually rather including intrin.h. It's very |
32 | // expensive, and bit.h is popular via MathExtras.h. |
33 | // #include <intrin.h> |
34 | extern "C" { |
35 | unsigned char _BitScanForward(unsigned long *_Index, unsigned long _Mask); |
36 | unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask); |
37 | unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask); |
38 | unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask); |
39 | } |
40 | #endif |
41 | |
42 | namespace llvm { |
43 | |
44 | // This implementation of bit_cast is different from the C++20 one in two ways: |
45 | // - It isn't constexpr because that requires compiler support. |
46 | // - It requires trivially-constructible To, to avoid UB in the implementation. |
47 | template < |
48 | typename To, typename From, |
49 | typename = std::enable_if_t<sizeof(To) == sizeof(From)>, |
50 | typename = std::enable_if_t<std::is_trivially_constructible<To>::value>, |
51 | typename = std::enable_if_t<std::is_trivially_copyable<To>::value>, |
52 | typename = std::enable_if_t<std::is_trivially_copyable<From>::value>> |
53 | [[nodiscard]] inline To bit_cast(const From &from) noexcept { |
54 | #if __has_builtin(__builtin_bit_cast)1 |
55 | return __builtin_bit_cast(To, from); |
56 | #else |
57 | To to; |
58 | std::memcpy(&to, &from, sizeof(To)); |
59 | return to; |
60 | #endif |
61 | } |
62 | |
63 | /// Reverses the bytes in the given integer value V. |
64 | template <typename T, typename = std::enable_if_t<std::is_integral_v<T>>> |
65 | [[nodiscard]] constexpr T byteswap(T V) noexcept { |
66 | if constexpr (sizeof(T) == 1) { |
67 | return V; |
68 | } else if constexpr (sizeof(T) == 2) { |
69 | uint16_t UV = V; |
70 | #if defined(_MSC_VER) && !defined(_DEBUG1) |
71 | // The DLL version of the runtime lacks these functions (bug!?), but in a |
72 | // release build they're replaced with BSWAP instructions anyway. |
73 | return _byteswap_ushort(UV); |
74 | #else |
75 | uint16_t Hi = UV << 8; |
76 | uint16_t Lo = UV >> 8; |
77 | return Hi | Lo; |
78 | #endif |
79 | } else if constexpr (sizeof(T) == 4) { |
80 | uint32_t UV = V; |
81 | #if __has_builtin(__builtin_bswap32)1 |
82 | return __builtin_bswap32(UV); |
83 | #elif defined(_MSC_VER) && !defined(_DEBUG1) |
84 | return _byteswap_ulong(UV); |
85 | #else |
86 | uint32_t Byte0 = UV & 0x000000FF; |
87 | uint32_t Byte1 = UV & 0x0000FF00; |
88 | uint32_t Byte2 = UV & 0x00FF0000; |
89 | uint32_t Byte3 = UV & 0xFF000000; |
90 | return (Byte0 << 24) | (Byte1 << 8) | (Byte2 >> 8) | (Byte3 >> 24); |
91 | #endif |
92 | } else if constexpr (sizeof(T) == 8) { |
93 | uint64_t UV = V; |
94 | #if __has_builtin(__builtin_bswap64)1 |
95 | return __builtin_bswap64(UV); |
96 | #elif defined(_MSC_VER) && !defined(_DEBUG1) |
97 | return _byteswap_uint64(UV); |
98 | #else |
99 | uint64_t Hi = llvm::byteswap<uint32_t>(UV); |
100 | uint32_t Lo = llvm::byteswap<uint32_t>(UV >> 32); |
101 | return (Hi << 32) | Lo; |
102 | #endif |
103 | } else { |
104 | static_assert(!sizeof(T *), "Don't know how to handle the given type."); |
105 | return 0; |
106 | } |
107 | } |
108 | |
109 | template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>> |
110 | [[nodiscard]] constexpr inline bool has_single_bit(T Value) noexcept { |
111 | return (Value != 0) && ((Value & (Value - 1)) == 0); |
112 | } |
113 | |
114 | namespace detail { |
115 | template <typename T, std::size_t SizeOfT> struct TrailingZerosCounter { |
116 | static unsigned count(T Val) { |
117 | if (!Val) |
118 | return std::numeric_limits<T>::digits; |
119 | if (Val & 0x1) |
120 | return 0; |
121 | |
122 | // Bisection method. |
123 | unsigned ZeroBits = 0; |
124 | T Shift = std::numeric_limits<T>::digits >> 1; |
125 | T Mask = std::numeric_limits<T>::max() >> Shift; |
126 | while (Shift) { |
127 | if ((Val & Mask) == 0) { |
128 | Val >>= Shift; |
129 | ZeroBits |= Shift; |
130 | } |
131 | Shift >>= 1; |
132 | Mask >>= Shift; |
133 | } |
134 | return ZeroBits; |
135 | } |
136 | }; |
137 | |
138 | #if defined(__GNUC__4) || defined(_MSC_VER) |
139 | template <typename T> struct TrailingZerosCounter<T, 4> { |
140 | static unsigned count(T Val) { |
141 | if (Val == 0) |
142 | return 32; |
143 | |
144 | #if __has_builtin(__builtin_ctz)1 || defined(__GNUC__4) |
145 | return __builtin_ctz(Val); |
146 | #elif defined(_MSC_VER) |
147 | unsigned long Index; |
148 | _BitScanForward(&Index, Val); |
149 | return Index; |
150 | #endif |
151 | } |
152 | }; |
153 | |
154 | #if !defined(_MSC_VER) || defined(_M_X64) |
155 | template <typename T> struct TrailingZerosCounter<T, 8> { |
156 | static unsigned count(T Val) { |
157 | if (Val == 0) |
158 | return 64; |
159 | |
160 | #if __has_builtin(__builtin_ctzll)1 || defined(__GNUC__4) |
161 | return __builtin_ctzll(Val); |
162 | #elif defined(_MSC_VER) |
163 | unsigned long Index; |
164 | _BitScanForward64(&Index, Val); |
165 | return Index; |
166 | #endif |
167 | } |
168 | }; |
169 | #endif |
170 | #endif |
171 | } // namespace detail |
172 | |
173 | /// Count number of 0's from the least significant bit to the most |
174 | /// stopping at the first 1. |
175 | /// |
176 | /// Only unsigned integral types are allowed. |
177 | /// |
178 | /// Returns std::numeric_limits<T>::digits on an input of 0. |
179 | template <typename T> [[nodiscard]] int countr_zero(T Val) { |
180 | static_assert(std::is_unsigned_v<T>, |
181 | "Only unsigned integral types are allowed."); |
182 | return llvm::detail::TrailingZerosCounter<T, sizeof(T)>::count(Val); |
183 | } |
184 | |
185 | namespace detail { |
186 | template <typename T, std::size_t SizeOfT> struct LeadingZerosCounter { |
187 | static unsigned count(T Val) { |
188 | if (!Val) |
189 | return std::numeric_limits<T>::digits; |
190 | |
191 | // Bisection method. |
192 | unsigned ZeroBits = 0; |
193 | for (T Shift = std::numeric_limits<T>::digits >> 1; Shift; Shift >>= 1) { |
194 | T Tmp = Val >> Shift; |
195 | if (Tmp) |
196 | Val = Tmp; |
197 | else |
198 | ZeroBits |= Shift; |
199 | } |
200 | return ZeroBits; |
201 | } |
202 | }; |
203 | |
204 | #if defined(__GNUC__4) || defined(_MSC_VER) |
205 | template <typename T> struct LeadingZerosCounter<T, 4> { |
206 | static unsigned count(T Val) { |
207 | if (Val == 0) |
208 | return 32; |
209 | |
210 | #if __has_builtin(__builtin_clz)1 || defined(__GNUC__4) |
211 | return __builtin_clz(Val); |
212 | #elif defined(_MSC_VER) |
213 | unsigned long Index; |
214 | _BitScanReverse(&Index, Val); |
215 | return Index ^ 31; |
216 | #endif |
217 | } |
218 | }; |
219 | |
220 | #if !defined(_MSC_VER) || defined(_M_X64) |
221 | template <typename T> struct LeadingZerosCounter<T, 8> { |
222 | static unsigned count(T Val) { |
223 | if (Val == 0) |
224 | return 64; |
225 | |
226 | #if __has_builtin(__builtin_clzll)1 || defined(__GNUC__4) |
227 | return __builtin_clzll(Val); |
228 | #elif defined(_MSC_VER) |
229 | unsigned long Index; |
230 | _BitScanReverse64(&Index, Val); |
231 | return Index ^ 63; |
232 | #endif |
233 | } |
234 | }; |
235 | #endif |
236 | #endif |
237 | } // namespace detail |
238 | |
239 | /// Count number of 0's from the most significant bit to the least |
240 | /// stopping at the first 1. |
241 | /// |
242 | /// Only unsigned integral types are allowed. |
243 | /// |
244 | /// Returns std::numeric_limits<T>::digits on an input of 0. |
245 | template <typename T> [[nodiscard]] int countl_zero(T Val) { |
246 | static_assert(std::is_unsigned_v<T>, |
247 | "Only unsigned integral types are allowed."); |
248 | return llvm::detail::LeadingZerosCounter<T, sizeof(T)>::count(Val); |
249 | } |
250 | |
251 | /// Count the number of ones from the most significant bit to the first |
252 | /// zero bit. |
253 | /// |
254 | /// Ex. countl_one(0xFF0FFF00) == 8. |
255 | /// Only unsigned integral types are allowed. |
256 | /// |
257 | /// Returns std::numeric_limits<T>::digits on an input of all ones. |
258 | template <typename T> [[nodiscard]] int countl_one(T Value) { |
259 | static_assert(std::is_unsigned_v<T>, |
260 | "Only unsigned integral types are allowed."); |
261 | return llvm::countl_zero<T>(~Value); |
262 | } |
263 | |
264 | /// Count the number of ones from the least significant bit to the first |
265 | /// zero bit. |
266 | /// |
267 | /// Ex. countr_one(0x00FF00FF) == 8. |
268 | /// Only unsigned integral types are allowed. |
269 | /// |
270 | /// Returns std::numeric_limits<T>::digits on an input of all ones. |
271 | template <typename T> [[nodiscard]] int countr_one(T Value) { |
272 | static_assert(std::is_unsigned_v<T>, |
273 | "Only unsigned integral types are allowed."); |
274 | return llvm::countr_zero<T>(~Value); |
275 | } |
276 | |
277 | /// Returns the number of bits needed to represent Value if Value is nonzero. |
278 | /// Returns 0 otherwise. |
279 | /// |
280 | /// Ex. bit_width(5) == 3. |
281 | template <typename T> [[nodiscard]] int bit_width(T Value) { |
282 | static_assert(std::is_unsigned_v<T>, |
283 | "Only unsigned integral types are allowed."); |
284 | return std::numeric_limits<T>::digits - llvm::countl_zero(Value); |
285 | } |
286 | |
287 | /// Returns the largest integral power of two no greater than Value if Value is |
288 | /// nonzero. Returns 0 otherwise. |
289 | /// |
290 | /// Ex. bit_floor(5) == 4. |
291 | template <typename T> [[nodiscard]] T bit_floor(T Value) { |
292 | static_assert(std::is_unsigned_v<T>, |
293 | "Only unsigned integral types are allowed."); |
294 | if (!Value) |
295 | return 0; |
296 | return T(1) << (llvm::bit_width(Value) - 1); |
297 | } |
298 | |
299 | /// Returns the smallest integral power of two no smaller than Value if Value is |
300 | /// nonzero. Returns 0 otherwise. |
301 | /// |
302 | /// Ex. bit_ceil(5) == 8. |
303 | /// |
304 | /// The return value is undefined if the input is larger than the largest power |
305 | /// of two representable in T. |
306 | template <typename T> [[nodiscard]] T bit_ceil(T Value) { |
307 | static_assert(std::is_unsigned_v<T>, |
308 | "Only unsigned integral types are allowed."); |
309 | if (Value < 2) |
310 | return 1; |
311 | return T(1) << llvm::bit_width<T>(Value - 1u); |
312 | } |
313 | |
314 | namespace detail { |
315 | template <typename T, std::size_t SizeOfT> struct PopulationCounter { |
316 | static int count(T Value) { |
317 | // Generic version, forward to 32 bits. |
318 | static_assert(SizeOfT <= 4, "Not implemented!"); |
319 | #if defined(__GNUC__4) |
320 | return (int)__builtin_popcount(Value); |
321 | #else |
322 | uint32_t v = Value; |
323 | v = v - ((v >> 1) & 0x55555555); |
324 | v = (v & 0x33333333) + ((v >> 2) & 0x33333333); |
325 | return int(((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24); |
326 | #endif |
327 | } |
328 | }; |
329 | |
330 | template <typename T> struct PopulationCounter<T, 8> { |
331 | static int count(T Value) { |
332 | #if defined(__GNUC__4) |
333 | return (int)__builtin_popcountll(Value); |
334 | #else |
335 | uint64_t v = Value; |
336 | v = v - ((v >> 1) & 0x5555555555555555ULL); |
337 | v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL); |
338 | v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL; |
339 | return int((uint64_t)(v * 0x0101010101010101ULL) >> 56); |
340 | #endif |
341 | } |
342 | }; |
343 | } // namespace detail |
344 | |
345 | /// Count the number of set bits in a value. |
346 | /// Ex. popcount(0xF000F000) = 8 |
347 | /// Returns 0 if the word is zero. |
348 | template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>> |
349 | [[nodiscard]] inline int popcount(T Value) noexcept { |
350 | return detail::PopulationCounter<T, sizeof(T)>::count(Value); |
351 | } |
352 | |
353 | } // namespace llvm |
354 | |
355 | #endif |