comparison lld/MachO/Writer.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 0572611fdcc8
children 5f17cb93ff66
comparison
equal deleted inserted replaced
173:0572611fdcc8 207:2e18cbf3894f
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 // 6 //
7 //===----------------------------------------------------------------------===// 7 //===----------------------------------------------------------------------===//
8 8
9 #include "Writer.h" 9 #include "Writer.h"
10 #include "ConcatOutputSection.h"
10 #include "Config.h" 11 #include "Config.h"
11 #include "InputFiles.h" 12 #include "InputFiles.h"
12 #include "InputSection.h" 13 #include "InputSection.h"
13 #include "MergedOutputSection.h" 14 #include "MapFile.h"
14 #include "OutputSection.h" 15 #include "OutputSection.h"
15 #include "OutputSegment.h" 16 #include "OutputSegment.h"
16 #include "SymbolTable.h" 17 #include "SymbolTable.h"
17 #include "Symbols.h" 18 #include "Symbols.h"
18 #include "SyntheticSections.h" 19 #include "SyntheticSections.h"
19 #include "Target.h" 20 #include "Target.h"
20 21 #include "UnwindInfoSection.h"
22
23 #include "lld/Common/Arrays.h"
21 #include "lld/Common/ErrorHandler.h" 24 #include "lld/Common/ErrorHandler.h"
22 #include "lld/Common/Memory.h" 25 #include "lld/Common/Memory.h"
23 #include "llvm/BinaryFormat/MachO.h" 26 #include "llvm/BinaryFormat/MachO.h"
27 #include "llvm/Config/llvm-config.h"
24 #include "llvm/Support/LEB128.h" 28 #include "llvm/Support/LEB128.h"
25 #include "llvm/Support/MathExtras.h" 29 #include "llvm/Support/MathExtras.h"
30 #include "llvm/Support/Parallel.h"
26 #include "llvm/Support/Path.h" 31 #include "llvm/Support/Path.h"
32 #include "llvm/Support/TimeProfiler.h"
33 #include "llvm/Support/xxhash.h"
34
35 #include <algorithm>
27 36
28 using namespace llvm; 37 using namespace llvm;
29 using namespace llvm::MachO; 38 using namespace llvm::MachO;
39 using namespace llvm::sys;
30 using namespace lld; 40 using namespace lld;
31 using namespace lld::macho; 41 using namespace lld::macho;
32 42
33 namespace { 43 namespace {
34 class LCLinkEdit; 44 class LCUuid;
35 class LCDyldInfo;
36 class LCSymtab;
37 45
38 class Writer { 46 class Writer {
39 public: 47 public:
40 Writer() : buffer(errorHandler().outputBuffer) {} 48 Writer() : buffer(errorHandler().outputBuffer) {}
41 49
42 void scanRelocations(); 50 void scanRelocations();
43 void createOutputSections(); 51 void scanSymbols();
44 void createLoadCommands(); 52 template <class LP> void createOutputSections();
53 template <class LP> void createLoadCommands();
54 void finalizeAddresses();
55 void finalizeLinkEditSegment();
45 void assignAddresses(OutputSegment *); 56 void assignAddresses(OutputSegment *);
46 void createSymtabContents();
47 57
48 void openFile(); 58 void openFile();
49 void writeSections(); 59 void writeSections();
50 60 void writeUuid();
51 void run(); 61 void writeCodeSignature();
62 void writeOutputFile();
63
64 template <class LP> void run();
52 65
53 std::unique_ptr<FileOutputBuffer> &buffer; 66 std::unique_ptr<FileOutputBuffer> &buffer;
54 uint64_t addr = 0; 67 uint64_t addr = 0;
55 uint64_t fileOff = 0; 68 uint64_t fileOff = 0;
56 MachHeaderSection *headerSection = nullptr; 69 MachHeaderSection *header = nullptr;
57 BindingSection *bindingSection = nullptr;
58 LazyBindingSection *lazyBindingSection = nullptr;
59 ExportSection *exportSection = nullptr;
60 StringTableSection *stringTableSection = nullptr; 70 StringTableSection *stringTableSection = nullptr;
61 SymtabSection *symtabSection = nullptr; 71 SymtabSection *symtabSection = nullptr;
72 IndirectSymtabSection *indirectSymtabSection = nullptr;
73 CodeSignatureSection *codeSignatureSection = nullptr;
74 FunctionStartsSection *functionStartsSection = nullptr;
75
76 LCUuid *uuidCommand = nullptr;
77 OutputSegment *linkEditSegment = nullptr;
62 }; 78 };
63 79
64 // LC_DYLD_INFO_ONLY stores the offsets of symbol import/export information. 80 // LC_DYLD_INFO_ONLY stores the offsets of symbol import/export information.
65 class LCDyldInfo : public LoadCommand { 81 class LCDyldInfo : public LoadCommand {
66 public: 82 public:
67 LCDyldInfo(BindingSection *bindingSection, 83 LCDyldInfo(RebaseSection *rebaseSection, BindingSection *bindingSection,
84 WeakBindingSection *weakBindingSection,
68 LazyBindingSection *lazyBindingSection, 85 LazyBindingSection *lazyBindingSection,
69 ExportSection *exportSection) 86 ExportSection *exportSection)
70 : bindingSection(bindingSection), lazyBindingSection(lazyBindingSection), 87 : rebaseSection(rebaseSection), bindingSection(bindingSection),
71 exportSection(exportSection) {} 88 weakBindingSection(weakBindingSection),
89 lazyBindingSection(lazyBindingSection), exportSection(exportSection) {}
72 90
73 uint32_t getSize() const override { return sizeof(dyld_info_command); } 91 uint32_t getSize() const override { return sizeof(dyld_info_command); }
74 92
75 void writeTo(uint8_t *buf) const override { 93 void writeTo(uint8_t *buf) const override {
76 auto *c = reinterpret_cast<dyld_info_command *>(buf); 94 auto *c = reinterpret_cast<dyld_info_command *>(buf);
77 c->cmd = LC_DYLD_INFO_ONLY; 95 c->cmd = LC_DYLD_INFO_ONLY;
78 c->cmdsize = getSize(); 96 c->cmdsize = getSize();
97 if (rebaseSection->isNeeded()) {
98 c->rebase_off = rebaseSection->fileOff;
99 c->rebase_size = rebaseSection->getFileSize();
100 }
79 if (bindingSection->isNeeded()) { 101 if (bindingSection->isNeeded()) {
80 c->bind_off = bindingSection->fileOff; 102 c->bind_off = bindingSection->fileOff;
81 c->bind_size = bindingSection->getFileSize(); 103 c->bind_size = bindingSection->getFileSize();
82 } 104 }
105 if (weakBindingSection->isNeeded()) {
106 c->weak_bind_off = weakBindingSection->fileOff;
107 c->weak_bind_size = weakBindingSection->getFileSize();
108 }
83 if (lazyBindingSection->isNeeded()) { 109 if (lazyBindingSection->isNeeded()) {
84 c->lazy_bind_off = lazyBindingSection->fileOff; 110 c->lazy_bind_off = lazyBindingSection->fileOff;
85 c->lazy_bind_size = lazyBindingSection->getFileSize(); 111 c->lazy_bind_size = lazyBindingSection->getFileSize();
86 } 112 }
87 if (exportSection->isNeeded()) { 113 if (exportSection->isNeeded()) {
88 c->export_off = exportSection->fileOff; 114 c->export_off = exportSection->fileOff;
89 c->export_size = exportSection->getFileSize(); 115 c->export_size = exportSection->getFileSize();
90 } 116 }
91 } 117 }
92 118
119 RebaseSection *rebaseSection;
93 BindingSection *bindingSection; 120 BindingSection *bindingSection;
121 WeakBindingSection *weakBindingSection;
94 LazyBindingSection *lazyBindingSection; 122 LazyBindingSection *lazyBindingSection;
95 ExportSection *exportSection; 123 ExportSection *exportSection;
96 }; 124 };
97 125
126 class LCFunctionStarts : public LoadCommand {
127 public:
128 explicit LCFunctionStarts(FunctionStartsSection *functionStartsSection)
129 : functionStartsSection(functionStartsSection) {}
130
131 uint32_t getSize() const override { return sizeof(linkedit_data_command); }
132
133 void writeTo(uint8_t *buf) const override {
134 auto *c = reinterpret_cast<linkedit_data_command *>(buf);
135 c->cmd = LC_FUNCTION_STARTS;
136 c->cmdsize = getSize();
137 c->dataoff = functionStartsSection->fileOff;
138 c->datasize = functionStartsSection->getFileSize();
139 }
140
141 private:
142 FunctionStartsSection *functionStartsSection;
143 };
144
98 class LCDysymtab : public LoadCommand { 145 class LCDysymtab : public LoadCommand {
99 public: 146 public:
147 LCDysymtab(SymtabSection *symtabSection,
148 IndirectSymtabSection *indirectSymtabSection)
149 : symtabSection(symtabSection),
150 indirectSymtabSection(indirectSymtabSection) {}
151
100 uint32_t getSize() const override { return sizeof(dysymtab_command); } 152 uint32_t getSize() const override { return sizeof(dysymtab_command); }
101 153
102 void writeTo(uint8_t *buf) const override { 154 void writeTo(uint8_t *buf) const override {
103 auto *c = reinterpret_cast<dysymtab_command *>(buf); 155 auto *c = reinterpret_cast<dysymtab_command *>(buf);
104 c->cmd = LC_DYSYMTAB; 156 c->cmd = LC_DYSYMTAB;
105 c->cmdsize = getSize(); 157 c->cmdsize = getSize();
106 } 158
107 }; 159 c->ilocalsym = 0;
108 160 c->iextdefsym = c->nlocalsym = symtabSection->getNumLocalSymbols();
109 class LCSegment : public LoadCommand { 161 c->nextdefsym = symtabSection->getNumExternalSymbols();
162 c->iundefsym = c->iextdefsym + c->nextdefsym;
163 c->nundefsym = symtabSection->getNumUndefinedSymbols();
164
165 c->indirectsymoff = indirectSymtabSection->fileOff;
166 c->nindirectsyms = indirectSymtabSection->getNumSymbols();
167 }
168
169 SymtabSection *symtabSection;
170 IndirectSymtabSection *indirectSymtabSection;
171 };
172
173 template <class LP> class LCSegment : public LoadCommand {
110 public: 174 public:
111 LCSegment(StringRef name, OutputSegment *seg) : name(name), seg(seg) {} 175 LCSegment(StringRef name, OutputSegment *seg) : name(name), seg(seg) {}
112 176
113 uint32_t getSize() const override { 177 uint32_t getSize() const override {
114 return sizeof(segment_command_64) + 178 return sizeof(typename LP::segment_command) +
115 seg->numNonHiddenSections() * sizeof(section_64); 179 seg->numNonHiddenSections() * sizeof(typename LP::section);
116 } 180 }
117 181
118 void writeTo(uint8_t *buf) const override { 182 void writeTo(uint8_t *buf) const override {
119 auto *c = reinterpret_cast<segment_command_64 *>(buf); 183 using SegmentCommand = typename LP::segment_command;
120 buf += sizeof(segment_command_64); 184 using Section = typename LP::section;
121 185
122 c->cmd = LC_SEGMENT_64; 186 auto *c = reinterpret_cast<SegmentCommand *>(buf);
187 buf += sizeof(SegmentCommand);
188
189 c->cmd = LP::segmentLCType;
123 c->cmdsize = getSize(); 190 c->cmdsize = getSize();
124 memcpy(c->segname, name.data(), name.size()); 191 memcpy(c->segname, name.data(), name.size());
125 c->fileoff = seg->fileOff; 192 c->fileoff = seg->fileOff;
126 c->maxprot = seg->maxProt; 193 c->maxprot = seg->maxProt;
127 c->initprot = seg->initProt; 194 c->initprot = seg->initProt;
128 195
129 if (seg->getSections().empty()) 196 if (seg->getSections().empty())
130 return; 197 return;
131 198
132 c->vmaddr = seg->firstSection()->addr; 199 c->vmaddr = seg->firstSection()->addr;
133 c->vmsize = 200 c->vmsize = seg->vmSize;
134 seg->lastSection()->addr + seg->lastSection()->getSize() - c->vmaddr; 201 c->filesize = seg->fileSize;
135 c->nsects = seg->numNonHiddenSections(); 202 c->nsects = seg->numNonHiddenSections();
136 203
137 for (auto &p : seg->getSections()) { 204 for (const OutputSection *osec : seg->getSections()) {
138 StringRef s = p.first; 205 if (osec->isHidden())
139 OutputSection *section = p.second;
140 c->filesize += section->getFileSize();
141
142 if (section->isHidden())
143 continue; 206 continue;
144 207
145 auto *sectHdr = reinterpret_cast<section_64 *>(buf); 208 auto *sectHdr = reinterpret_cast<Section *>(buf);
146 buf += sizeof(section_64); 209 buf += sizeof(Section);
147 210
148 memcpy(sectHdr->sectname, s.data(), s.size()); 211 memcpy(sectHdr->sectname, osec->name.data(), osec->name.size());
149 memcpy(sectHdr->segname, name.data(), name.size()); 212 memcpy(sectHdr->segname, name.data(), name.size());
150 213
151 sectHdr->addr = section->addr; 214 sectHdr->addr = osec->addr;
152 sectHdr->offset = section->fileOff; 215 sectHdr->offset = osec->fileOff;
153 sectHdr->align = Log2_32(section->align); 216 sectHdr->align = Log2_32(osec->align);
154 sectHdr->flags = section->flags; 217 sectHdr->flags = osec->flags;
155 sectHdr->size = section->getSize(); 218 sectHdr->size = osec->getSize();
219 sectHdr->reserved1 = osec->reserved1;
220 sectHdr->reserved2 = osec->reserved2;
156 } 221 }
157 } 222 }
158 223
159 private: 224 private:
160 StringRef name; 225 StringRef name;
161 OutputSegment *seg; 226 OutputSegment *seg;
162 }; 227 };
163 228
164 class LCMain : public LoadCommand { 229 class LCMain : public LoadCommand {
165 uint32_t getSize() const override { return sizeof(entry_point_command); } 230 uint32_t getSize() const override {
166 231 return sizeof(structs::entry_point_command);
167 void writeTo(uint8_t *buf) const override { 232 }
168 auto *c = reinterpret_cast<entry_point_command *>(buf); 233
234 void writeTo(uint8_t *buf) const override {
235 auto *c = reinterpret_cast<structs::entry_point_command *>(buf);
169 c->cmd = LC_MAIN; 236 c->cmd = LC_MAIN;
170 c->cmdsize = getSize(); 237 c->cmdsize = getSize();
171 c->entryoff = config->entry->getVA() - ImageBase; 238
239 if (config->entry->isInStubs())
240 c->entryoff =
241 in.stubs->fileOff + config->entry->stubsIndex * target->stubSize;
242 else
243 c->entryoff = config->entry->getFileOffset();
244
172 c->stacksize = 0; 245 c->stacksize = 0;
173 } 246 }
174 }; 247 };
175 248
176 class LCSymtab : public LoadCommand { 249 class LCSymtab : public LoadCommand {
198 // * LC_LOAD_DYLIB 271 // * LC_LOAD_DYLIB
199 // * LC_ID_DYLIB 272 // * LC_ID_DYLIB
200 // * LC_REEXPORT_DYLIB 273 // * LC_REEXPORT_DYLIB
201 class LCDylib : public LoadCommand { 274 class LCDylib : public LoadCommand {
202 public: 275 public:
203 LCDylib(LoadCommandType type, StringRef path) : type(type), path(path) {} 276 LCDylib(LoadCommandType type, StringRef path,
277 uint32_t compatibilityVersion = 0, uint32_t currentVersion = 0)
278 : type(type), path(path), compatibilityVersion(compatibilityVersion),
279 currentVersion(currentVersion) {
280 instanceCount++;
281 }
204 282
205 uint32_t getSize() const override { 283 uint32_t getSize() const override {
206 return alignTo(sizeof(dylib_command) + path.size() + 1, 8); 284 return alignTo(sizeof(dylib_command) + path.size() + 1, 8);
207 } 285 }
208 286
211 buf += sizeof(dylib_command); 289 buf += sizeof(dylib_command);
212 290
213 c->cmd = type; 291 c->cmd = type;
214 c->cmdsize = getSize(); 292 c->cmdsize = getSize();
215 c->dylib.name = sizeof(dylib_command); 293 c->dylib.name = sizeof(dylib_command);
294 c->dylib.timestamp = 0;
295 c->dylib.compatibility_version = compatibilityVersion;
296 c->dylib.current_version = currentVersion;
216 297
217 memcpy(buf, path.data(), path.size()); 298 memcpy(buf, path.data(), path.size());
218 buf[path.size()] = '\0'; 299 buf[path.size()] = '\0';
219 } 300 }
301
302 static uint32_t getInstanceCount() { return instanceCount; }
220 303
221 private: 304 private:
222 LoadCommandType type; 305 LoadCommandType type;
223 StringRef path; 306 StringRef path;
224 }; 307 uint32_t compatibilityVersion;
308 uint32_t currentVersion;
309 static uint32_t instanceCount;
310 };
311
312 uint32_t LCDylib::instanceCount = 0;
225 313
226 class LCLoadDylinker : public LoadCommand { 314 class LCLoadDylinker : public LoadCommand {
227 public: 315 public:
228 uint32_t getSize() const override { 316 uint32_t getSize() const override {
229 return alignTo(sizeof(dylinker_command) + path.size() + 1, 8); 317 return alignTo(sizeof(dylinker_command) + path.size() + 1, 8);
244 private: 332 private:
245 // Recent versions of Darwin won't run any binary that has dyld at a 333 // Recent versions of Darwin won't run any binary that has dyld at a
246 // different location. 334 // different location.
247 const StringRef path = "/usr/lib/dyld"; 335 const StringRef path = "/usr/lib/dyld";
248 }; 336 };
337
338 class LCRPath : public LoadCommand {
339 public:
340 explicit LCRPath(StringRef path) : path(path) {}
341
342 uint32_t getSize() const override {
343 return alignTo(sizeof(rpath_command) + path.size() + 1, target->wordSize);
344 }
345
346 void writeTo(uint8_t *buf) const override {
347 auto *c = reinterpret_cast<rpath_command *>(buf);
348 buf += sizeof(rpath_command);
349
350 c->cmd = LC_RPATH;
351 c->cmdsize = getSize();
352 c->path = sizeof(rpath_command);
353
354 memcpy(buf, path.data(), path.size());
355 buf[path.size()] = '\0';
356 }
357
358 private:
359 StringRef path;
360 };
361
362 class LCMinVersion : public LoadCommand {
363 public:
364 explicit LCMinVersion(const PlatformInfo &platformInfo)
365 : platformInfo(platformInfo) {}
366
367 uint32_t getSize() const override { return sizeof(version_min_command); }
368
369 void writeTo(uint8_t *buf) const override {
370 auto *c = reinterpret_cast<version_min_command *>(buf);
371 switch (platformInfo.target.Platform) {
372 case PlatformKind::macOS:
373 c->cmd = LC_VERSION_MIN_MACOSX;
374 break;
375 case PlatformKind::iOS:
376 case PlatformKind::iOSSimulator:
377 c->cmd = LC_VERSION_MIN_IPHONEOS;
378 break;
379 case PlatformKind::tvOS:
380 case PlatformKind::tvOSSimulator:
381 c->cmd = LC_VERSION_MIN_TVOS;
382 break;
383 case PlatformKind::watchOS:
384 case PlatformKind::watchOSSimulator:
385 c->cmd = LC_VERSION_MIN_WATCHOS;
386 break;
387 default:
388 llvm_unreachable("invalid platform");
389 break;
390 }
391 c->cmdsize = getSize();
392 c->version = encodeVersion(platformInfo.minimum);
393 c->sdk = encodeVersion(platformInfo.sdk);
394 }
395
396 private:
397 const PlatformInfo &platformInfo;
398 };
399
400 class LCBuildVersion : public LoadCommand {
401 public:
402 explicit LCBuildVersion(const PlatformInfo &platformInfo)
403 : platformInfo(platformInfo) {}
404
405 const int ntools = 1;
406
407 uint32_t getSize() const override {
408 return sizeof(build_version_command) + ntools * sizeof(build_tool_version);
409 }
410
411 void writeTo(uint8_t *buf) const override {
412 auto *c = reinterpret_cast<build_version_command *>(buf);
413 c->cmd = LC_BUILD_VERSION;
414 c->cmdsize = getSize();
415 c->platform = static_cast<uint32_t>(platformInfo.target.Platform);
416 c->minos = encodeVersion(platformInfo.minimum);
417 c->sdk = encodeVersion(platformInfo.sdk);
418 c->ntools = ntools;
419 auto *t = reinterpret_cast<build_tool_version *>(&c[1]);
420 t->tool = TOOL_LD;
421 t->version = encodeVersion(llvm::VersionTuple(
422 LLVM_VERSION_MAJOR, LLVM_VERSION_MINOR, LLVM_VERSION_PATCH));
423 }
424
425 private:
426 const PlatformInfo &platformInfo;
427 };
428
429 // Stores a unique identifier for the output file based on an MD5 hash of its
430 // contents. In order to hash the contents, we must first write them, but
431 // LC_UUID itself must be part of the written contents in order for all the
432 // offsets to be calculated correctly. We resolve this circular paradox by
433 // first writing an LC_UUID with an all-zero UUID, then updating the UUID with
434 // its real value later.
435 class LCUuid : public LoadCommand {
436 public:
437 uint32_t getSize() const override { return sizeof(uuid_command); }
438
439 void writeTo(uint8_t *buf) const override {
440 auto *c = reinterpret_cast<uuid_command *>(buf);
441 c->cmd = LC_UUID;
442 c->cmdsize = getSize();
443 uuidBuf = c->uuid;
444 }
445
446 void writeUuid(uint64_t digest) const {
447 // xxhash only gives us 8 bytes, so put some fixed data in the other half.
448 static_assert(sizeof(uuid_command::uuid) == 16, "unexpected uuid size");
449 memcpy(uuidBuf, "LLD\xa1UU1D", 8);
450 memcpy(uuidBuf + 8, &digest, 8);
451
452 // RFC 4122 conformance. We need to fix 4 bits in byte 6 and 2 bits in
453 // byte 8. Byte 6 is already fine due to the fixed data we put in. We don't
454 // want to lose bits of the digest in byte 8, so swap that with a byte of
455 // fixed data that happens to have the right bits set.
456 std::swap(uuidBuf[3], uuidBuf[8]);
457
458 // Claim that this is an MD5-based hash. It isn't, but this signals that
459 // this is not a time-based and not a random hash. MD5 seems like the least
460 // bad lie we can put here.
461 assert((uuidBuf[6] & 0xf0) == 0x30 && "See RFC 4122 Sections 4.2.2, 4.1.3");
462 assert((uuidBuf[8] & 0xc0) == 0x80 && "See RFC 4122 Section 4.2.2");
463 }
464
465 mutable uint8_t *uuidBuf;
466 };
467
468 template <class LP> class LCEncryptionInfo : public LoadCommand {
469 public:
470 uint32_t getSize() const override {
471 return sizeof(typename LP::encryption_info_command);
472 }
473
474 void writeTo(uint8_t *buf) const override {
475 using EncryptionInfo = typename LP::encryption_info_command;
476 auto *c = reinterpret_cast<EncryptionInfo *>(buf);
477 buf += sizeof(EncryptionInfo);
478 c->cmd = LP::encryptionInfoLCType;
479 c->cmdsize = getSize();
480 c->cryptoff = in.header->getSize();
481 auto it = find_if(outputSegments, [](const OutputSegment *seg) {
482 return seg->name == segment_names::text;
483 });
484 assert(it != outputSegments.end());
485 c->cryptsize = (*it)->fileSize - c->cryptoff;
486 }
487 };
488
489 class LCCodeSignature : public LoadCommand {
490 public:
491 LCCodeSignature(CodeSignatureSection *section) : section(section) {}
492
493 uint32_t getSize() const override { return sizeof(linkedit_data_command); }
494
495 void writeTo(uint8_t *buf) const override {
496 auto *c = reinterpret_cast<linkedit_data_command *>(buf);
497 c->cmd = LC_CODE_SIGNATURE;
498 c->cmdsize = getSize();
499 c->dataoff = static_cast<uint32_t>(section->fileOff);
500 c->datasize = section->getSize();
501 }
502
503 CodeSignatureSection *section;
504 };
505
249 } // namespace 506 } // namespace
250 507
508 // Add stubs and bindings where necessary (e.g. if the symbol is a
509 // DylibSymbol.)
510 static void prepareBranchTarget(Symbol *sym) {
511 if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
512 if (in.stubs->addEntry(dysym)) {
513 if (sym->isWeakDef()) {
514 in.binding->addEntry(dysym, in.lazyPointers->isec,
515 sym->stubsIndex * target->wordSize);
516 in.weakBinding->addEntry(sym, in.lazyPointers->isec,
517 sym->stubsIndex * target->wordSize);
518 } else {
519 in.lazyBinding->addEntry(dysym);
520 }
521 }
522 } else if (auto *defined = dyn_cast<Defined>(sym)) {
523 if (defined->isExternalWeakDef()) {
524 if (in.stubs->addEntry(sym)) {
525 in.rebase->addEntry(in.lazyPointers->isec,
526 sym->stubsIndex * target->wordSize);
527 in.weakBinding->addEntry(sym, in.lazyPointers->isec,
528 sym->stubsIndex * target->wordSize);
529 }
530 }
531 } else {
532 llvm_unreachable("invalid branch target symbol type");
533 }
534 }
535
536 // Can a symbol's address can only be resolved at runtime?
537 static bool needsBinding(const Symbol *sym) {
538 if (isa<DylibSymbol>(sym))
539 return true;
540 if (const auto *defined = dyn_cast<Defined>(sym))
541 return defined->isExternalWeakDef();
542 return false;
543 }
544
545 static void prepareSymbolRelocation(Symbol *sym, const InputSection *isec,
546 const Reloc &r) {
547 const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
548
549 if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) {
550 prepareBranchTarget(sym);
551 } else if (relocAttrs.hasAttr(RelocAttrBits::GOT)) {
552 if (relocAttrs.hasAttr(RelocAttrBits::POINTER) || needsBinding(sym))
553 in.got->addEntry(sym);
554 } else if (relocAttrs.hasAttr(RelocAttrBits::TLV)) {
555 if (needsBinding(sym))
556 in.tlvPointers->addEntry(sym);
557 } else if (relocAttrs.hasAttr(RelocAttrBits::UNSIGNED)) {
558 // References from thread-local variable sections are treated as offsets
559 // relative to the start of the referent section, and therefore have no
560 // need of rebase opcodes.
561 if (!(isThreadLocalVariables(isec->flags) && isa<Defined>(sym)))
562 addNonLazyBindingEntries(sym, isec, r.offset, r.addend);
563 }
564 }
565
251 void Writer::scanRelocations() { 566 void Writer::scanRelocations() {
252 for (InputSection *sect : inputSections) 567 TimeTraceScope timeScope("Scan relocations");
253 for (Reloc &r : sect->relocs) 568 for (InputSection *isec : inputSections) {
254 if (auto *s = r.target.dyn_cast<Symbol *>()) 569 if (isec->shouldOmitFromOutput())
255 if (auto *dylibSymbol = dyn_cast<DylibSymbol>(s)) 570 continue;
256 target->prepareDylibSymbolRelocation(*dylibSymbol, r.type); 571
257 } 572 if (isec->segname == segment_names::ld) {
258 573 in.unwindInfo->prepareRelocations(isec);
259 void Writer::createLoadCommands() { 574 continue;
260 headerSection->addLoadCommand( 575 }
261 make<LCDyldInfo>(bindingSection, lazyBindingSection, exportSection)); 576
262 headerSection->addLoadCommand( 577 for (auto it = isec->relocs.begin(); it != isec->relocs.end(); ++it) {
263 make<LCSymtab>(symtabSection, stringTableSection)); 578 Reloc &r = *it;
264 headerSection->addLoadCommand(make<LCDysymtab>()); 579 if (target->hasAttr(r.type, RelocAttrBits::SUBTRAHEND)) {
580 // Skip over the following UNSIGNED relocation -- it's just there as the
581 // minuend, and doesn't have the usual UNSIGNED semantics. We don't want
582 // to emit rebase opcodes for it.
583 it++;
584 continue;
585 }
586 if (auto *sym = r.referent.dyn_cast<Symbol *>()) {
587 if (auto *undefined = dyn_cast<Undefined>(sym))
588 treatUndefinedSymbol(*undefined);
589 // treatUndefinedSymbol() can replace sym with a DylibSymbol; re-check.
590 if (!isa<Undefined>(sym) && validateSymbolRelocation(sym, isec, r))
591 prepareSymbolRelocation(sym, isec, r);
592 } else {
593 assert(r.referent.is<InputSection *>());
594 assert(!r.referent.get<InputSection *>()->shouldOmitFromOutput());
595 if (!r.pcrel)
596 in.rebase->addEntry(isec, r.offset);
597 }
598 }
599 }
600 }
601
602 void Writer::scanSymbols() {
603 TimeTraceScope timeScope("Scan symbols");
604 for (const Symbol *sym : symtab->getSymbols()) {
605 if (const auto *defined = dyn_cast<Defined>(sym)) {
606 if (defined->overridesWeakDef && defined->isLive())
607 in.weakBinding->addNonWeakDefinition(defined);
608 } else if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
609 // This branch intentionally doesn't check isLive().
610 if (dysym->isDynamicLookup())
611 continue;
612 dysym->getFile()->refState =
613 std::max(dysym->getFile()->refState, dysym->getRefState());
614 }
615 }
616 }
617
618 // TODO: ld64 enforces the old load commands in a few other cases.
619 static bool useLCBuildVersion(const PlatformInfo &platformInfo) {
620 static const std::map<PlatformKind, llvm::VersionTuple> minVersion = {
621 {PlatformKind::macOS, llvm::VersionTuple(10, 14)},
622 {PlatformKind::iOS, llvm::VersionTuple(12, 0)},
623 {PlatformKind::iOSSimulator, llvm::VersionTuple(13, 0)},
624 {PlatformKind::tvOS, llvm::VersionTuple(12, 0)},
625 {PlatformKind::tvOSSimulator, llvm::VersionTuple(13, 0)},
626 {PlatformKind::watchOS, llvm::VersionTuple(5, 0)},
627 {PlatformKind::watchOSSimulator, llvm::VersionTuple(6, 0)}};
628 auto it = minVersion.find(platformInfo.target.Platform);
629 return it == minVersion.end() ? true : platformInfo.minimum >= it->second;
630 }
631
632 template <class LP> void Writer::createLoadCommands() {
633 uint8_t segIndex = 0;
634 for (OutputSegment *seg : outputSegments) {
635 in.header->addLoadCommand(make<LCSegment<LP>>(seg->name, seg));
636 seg->index = segIndex++;
637 }
638
639 in.header->addLoadCommand(make<LCDyldInfo>(
640 in.rebase, in.binding, in.weakBinding, in.lazyBinding, in.exports));
641 in.header->addLoadCommand(make<LCSymtab>(symtabSection, stringTableSection));
642 in.header->addLoadCommand(
643 make<LCDysymtab>(symtabSection, indirectSymtabSection));
644 if (functionStartsSection)
645 in.header->addLoadCommand(make<LCFunctionStarts>(functionStartsSection));
646 if (config->emitEncryptionInfo)
647 in.header->addLoadCommand(make<LCEncryptionInfo<LP>>());
648 for (StringRef path : config->runtimePaths)
649 in.header->addLoadCommand(make<LCRPath>(path));
265 650
266 switch (config->outputType) { 651 switch (config->outputType) {
267 case MH_EXECUTE: 652 case MH_EXECUTE:
268 headerSection->addLoadCommand(make<LCMain>()); 653 in.header->addLoadCommand(make<LCLoadDylinker>());
269 headerSection->addLoadCommand(make<LCLoadDylinker>()); 654 in.header->addLoadCommand(make<LCMain>());
270 break; 655 break;
271 case MH_DYLIB: 656 case MH_DYLIB:
272 headerSection->addLoadCommand( 657 in.header->addLoadCommand(make<LCDylib>(LC_ID_DYLIB, config->installName,
273 make<LCDylib>(LC_ID_DYLIB, config->installName)); 658 config->dylibCompatibilityVersion,
659 config->dylibCurrentVersion));
660 break;
661 case MH_BUNDLE:
274 break; 662 break;
275 default: 663 default:
276 llvm_unreachable("unhandled output file type"); 664 llvm_unreachable("unhandled output file type");
277 } 665 }
278 666
279 uint8_t segIndex = 0; 667 uuidCommand = make<LCUuid>();
280 for (OutputSegment *seg : outputSegments) { 668 in.header->addLoadCommand(uuidCommand);
281 headerSection->addLoadCommand(make<LCSegment>(seg->name, seg)); 669
282 seg->index = segIndex++; 670 if (useLCBuildVersion(config->platformInfo))
283 } 671 in.header->addLoadCommand(make<LCBuildVersion>(config->platformInfo));
284 672 else
285 uint64_t dylibOrdinal = 1; 673 in.header->addLoadCommand(make<LCMinVersion>(config->platformInfo));
674
675 int64_t dylibOrdinal = 1;
676 DenseMap<StringRef, int64_t> ordinalForInstallName;
286 for (InputFile *file : inputFiles) { 677 for (InputFile *file : inputFiles) {
287 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 678 if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
288 headerSection->addLoadCommand( 679 if (dylibFile->isBundleLoader) {
289 make<LCDylib>(LC_LOAD_DYLIB, dylibFile->dylibName)); 680 dylibFile->ordinal = BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE;
290 dylibFile->ordinal = dylibOrdinal++; 681 // Shortcut since bundle-loader does not re-export the symbols.
682
683 dylibFile->reexport = false;
684 continue;
685 }
686
687 // Don't emit load commands for a dylib that is not referenced if:
688 // - it was added implicitly (via a reexport, an LC_LOAD_DYLINKER --
689 // if it's on the linker command line, it's explicit)
690 // - or it's marked MH_DEAD_STRIPPABLE_DYLIB
691 // - or the flag -dead_strip_dylibs is used
692 // FIXME: `isReferenced()` is currently computed before dead code
693 // stripping, so references from dead code keep a dylib alive. This
694 // matches ld64, but it's something we should do better.
695 if (!dylibFile->isReferenced() && !dylibFile->forceNeeded &&
696 (!dylibFile->explicitlyLinked || dylibFile->deadStrippable ||
697 config->deadStripDylibs))
698 continue;
699
700 // Several DylibFiles can have the same installName. Only emit a single
701 // load command for that installName and give all these DylibFiles the
702 // same ordinal.
703 // This can happen in several cases:
704 // - a new framework could change its installName to an older
705 // framework name via an $ld$ symbol depending on platform_version
706 // - symlinks (for example, libpthread.tbd is a symlink to libSystem.tbd;
707 // Foo.framework/Foo.tbd is usually a symlink to
708 // Foo.framework/Versions/Current/Foo.tbd, where
709 // Foo.framework/Versions/Current is usually a symlink to
710 // Foo.framework/Versions/A)
711 // - a framework can be linked both explicitly on the linker
712 // command line and implicitly as a reexport from a different
713 // framework. The re-export will usually point to the tbd file
714 // in Foo.framework/Versions/A/Foo.tbd, while the explicit link will
715 // usually find Foo.framwork/Foo.tbd. These are usually symlinks,
716 // but in a --reproduce archive they will be identical but distinct
717 // files.
718 // In the first case, *semantically distinct* DylibFiles will have the
719 // same installName.
720 int64_t &ordinal = ordinalForInstallName[dylibFile->installName];
721 if (ordinal) {
722 dylibFile->ordinal = ordinal;
723 continue;
724 }
725
726 ordinal = dylibFile->ordinal = dylibOrdinal++;
727 LoadCommandType lcType =
728 dylibFile->forceWeakImport || dylibFile->refState == RefState::Weak
729 ? LC_LOAD_WEAK_DYLIB
730 : LC_LOAD_DYLIB;
731 in.header->addLoadCommand(make<LCDylib>(lcType, dylibFile->installName,
732 dylibFile->compatibilityVersion,
733 dylibFile->currentVersion));
291 734
292 if (dylibFile->reexport) 735 if (dylibFile->reexport)
293 headerSection->addLoadCommand( 736 in.header->addLoadCommand(
294 make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->dylibName)); 737 make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->installName));
295 } 738 }
296 } 739 }
740
741 if (codeSignatureSection)
742 in.header->addLoadCommand(make<LCCodeSignature>(codeSignatureSection));
743
744 const uint32_t MACOS_MAXPATHLEN = 1024;
745 config->headerPad = std::max(
746 config->headerPad, (config->headerPadMaxInstallNames
747 ? LCDylib::getInstanceCount() * MACOS_MAXPATHLEN
748 : 0));
297 } 749 }
298 750
299 static size_t getSymbolPriority(const SymbolPriorityEntry &entry, 751 static size_t getSymbolPriority(const SymbolPriorityEntry &entry,
300 const InputFile &file) { 752 const InputFile *f) {
301 return std::max(entry.objectFiles.lookup(sys::path::filename(file.getName())), 753 // We don't use toString(InputFile *) here because it returns the full path
302 entry.anyObjectFile); 754 // for object files, and we only want the basename.
755 StringRef filename;
756 if (f->archiveName.empty())
757 filename = path::filename(f->getName());
758 else
759 filename = saver.save(path::filename(f->archiveName) + "(" +
760 path::filename(f->getName()) + ")");
761 return std::max(entry.objectFiles.lookup(filename), entry.anyObjectFile);
303 } 762 }
304 763
305 // Each section gets assigned the priority of the highest-priority symbol it 764 // Each section gets assigned the priority of the highest-priority symbol it
306 // contains. 765 // contains.
307 static DenseMap<const InputSection *, size_t> buildInputSectionPriorities() { 766 static DenseMap<const InputSection *, size_t> buildInputSectionPriorities() {
315 if (it == config->priorities.end()) 774 if (it == config->priorities.end())
316 return; 775 return;
317 776
318 SymbolPriorityEntry &entry = it->second; 777 SymbolPriorityEntry &entry = it->second;
319 size_t &priority = sectionPriorities[sym.isec]; 778 size_t &priority = sectionPriorities[sym.isec];
320 priority = std::max(priority, getSymbolPriority(entry, *sym.isec->file)); 779 priority = std::max(priority, getSymbolPriority(entry, sym.isec->file));
321 }; 780 };
322 781
323 // TODO: Make sure this handles weak symbols correctly. 782 // TODO: Make sure this handles weak symbols correctly.
324 for (InputFile *file : inputFiles) 783 for (const InputFile *file : inputFiles) {
325 if (isa<ObjFile>(file) || isa<ArchiveFile>(file)) 784 if (isa<ObjFile>(file))
326 for (Symbol *sym : file->symbols) 785 for (Symbol *sym : file->symbols)
327 if (auto *d = dyn_cast<Defined>(sym)) 786 if (auto *d = dyn_cast_or_null<Defined>(sym))
328 addSym(*d); 787 addSym(*d);
788 }
329 789
330 return sectionPriorities; 790 return sectionPriorities;
331 } 791 }
332 792
333 // Sorting only can happen once all outputs have been collected. Here we sort 793 // Sorting only can happen once all outputs have been collected. Here we sort
334 // segments, output sections within each segment, and input sections within each 794 // segments, output sections within each segment, and input sections within each
335 // output segment. 795 // output segment.
336 static void sortSegmentsAndSections() { 796 static void sortSegmentsAndSections() {
337 auto comparator = OutputSegmentComparator(); 797 TimeTraceScope timeScope("Sort segments and sections");
338 llvm::stable_sort(outputSegments, comparator); 798 sortOutputSegments();
339 799
340 DenseMap<const InputSection *, size_t> isecPriorities = 800 DenseMap<const InputSection *, size_t> isecPriorities =
341 buildInputSectionPriorities(); 801 buildInputSectionPriorities();
342 802
343 uint32_t sectionIndex = 0; 803 uint32_t sectionIndex = 0;
344 for (OutputSegment *seg : outputSegments) { 804 for (OutputSegment *seg : outputSegments) {
345 seg->sortOutputSections(&comparator); 805 seg->sortOutputSections();
346 for (auto &p : seg->getSections()) { 806 for (OutputSection *osec : seg->getSections()) {
347 OutputSection *section = p.second;
348 // Now that the output sections are sorted, assign the final 807 // Now that the output sections are sorted, assign the final
349 // output section indices. 808 // output section indices.
350 if (!section->isHidden()) 809 if (!osec->isHidden())
351 section->index = ++sectionIndex; 810 osec->index = ++sectionIndex;
811 if (!firstTLVDataSection && isThreadLocalData(osec->flags))
812 firstTLVDataSection = osec;
352 813
353 if (!isecPriorities.empty()) { 814 if (!isecPriorities.empty()) {
354 if (auto *merged = dyn_cast<MergedOutputSection>(section)) { 815 if (auto *merged = dyn_cast<ConcatOutputSection>(osec)) {
355 llvm::stable_sort(merged->inputs, 816 llvm::stable_sort(merged->inputs,
356 [&](InputSection *a, InputSection *b) { 817 [&](InputSection *a, InputSection *b) {
357 return isecPriorities[a] > isecPriorities[b]; 818 return isecPriorities[a] > isecPriorities[b];
358 }); 819 });
359 } 820 }
360 } 821 }
361 } 822 }
362 } 823 }
363 } 824 }
364 825
365 void Writer::createOutputSections() { 826 static NamePair maybeRenameSection(NamePair key) {
827 auto newNames = config->sectionRenameMap.find(key);
828 if (newNames != config->sectionRenameMap.end())
829 return newNames->second;
830 auto newName = config->segmentRenameMap.find(key.first);
831 if (newName != config->segmentRenameMap.end())
832 return std::make_pair(newName->second, key.second);
833 return key;
834 }
835
836 template <class LP> void Writer::createOutputSections() {
837 TimeTraceScope timeScope("Create output sections");
366 // First, create hidden sections 838 // First, create hidden sections
367 headerSection = make<MachHeaderSection>();
368 bindingSection = make<BindingSection>();
369 lazyBindingSection = make<LazyBindingSection>();
370 stringTableSection = make<StringTableSection>(); 839 stringTableSection = make<StringTableSection>();
371 symtabSection = make<SymtabSection>(*stringTableSection); 840 symtabSection = makeSymtabSection<LP>(*stringTableSection);
372 exportSection = make<ExportSection>(); 841 indirectSymtabSection = make<IndirectSymtabSection>();
842 if (config->adhocCodesign)
843 codeSignatureSection = make<CodeSignatureSection>();
844 if (config->emitFunctionStarts)
845 functionStartsSection = make<FunctionStartsSection>();
846 if (config->emitBitcodeBundle)
847 make<BitcodeBundleSection>();
373 848
374 switch (config->outputType) { 849 switch (config->outputType) {
375 case MH_EXECUTE: 850 case MH_EXECUTE:
376 make<PageZeroSection>(); 851 make<PageZeroSection>();
377 break; 852 break;
378 case MH_DYLIB: 853 case MH_DYLIB:
854 case MH_BUNDLE:
379 break; 855 break;
380 default: 856 default:
381 llvm_unreachable("unhandled output file type"); 857 llvm_unreachable("unhandled output file type");
382 } 858 }
383 859
384 // Then merge input sections into output sections/segments. 860 // Then add input sections to output sections.
385 for (InputSection *isec : inputSections) { 861 DenseMap<NamePair, ConcatOutputSection *> concatOutputSections;
386 getOrCreateOutputSegment(isec->segname) 862 for (const auto &p : enumerate(inputSections)) {
387 ->getOrCreateOutputSection(isec->name) 863 InputSection *isec = p.value();
388 ->mergeInput(isec); 864 if (isec->shouldOmitFromOutput())
389 } 865 continue;
390 866 NamePair names = maybeRenameSection({isec->segname, isec->name});
391 // Remove unneeded segments and sections. 867 ConcatOutputSection *&osec = concatOutputSections[names];
392 // TODO: Avoid creating unneeded segments in the first place 868 if (osec == nullptr) {
393 for (auto it = outputSegments.begin(); it != outputSegments.end();) { 869 osec = make<ConcatOutputSection>(names.second);
394 OutputSegment *seg = *it; 870 osec->inputOrder = p.index();
395 seg->removeUnneededSections(); 871 }
396 if (!seg->isNeeded()) 872 osec->addInput(isec);
397 it = outputSegments.erase(it); 873 }
398 else 874
399 ++it; 875 for (const auto &it : concatOutputSections) {
400 } 876 StringRef segname = it.first.first;
401 } 877 ConcatOutputSection *osec = it.second;
402 878 if (segname == segment_names::ld) {
403 void Writer::assignAddresses(OutputSegment *seg) { 879 assert(osec->name == section_names::compactUnwind);
404 addr = alignTo(addr, PageSize); 880 in.unwindInfo->setCompactUnwindSection(osec);
405 fileOff = alignTo(fileOff, PageSize); 881 } else {
406 seg->fileOff = fileOff; 882 getOrCreateOutputSegment(segname)->addOutputSection(osec);
407 883 }
408 for (auto &p : seg->getSections()) { 884 }
409 OutputSection *section = p.second; 885
410 addr = alignTo(addr, section->align); 886 for (SyntheticSection *ssec : syntheticSections) {
411 // We must align the file offsets too to avoid misaligned writes of 887 auto it = concatOutputSections.find({ssec->segname, ssec->name});
412 // structs. 888 if (it == concatOutputSections.end()) {
413 fileOff = alignTo(fileOff, section->align); 889 if (ssec->isNeeded())
414 section->addr = addr; 890 getOrCreateOutputSegment(ssec->segname)->addOutputSection(ssec);
415 section->fileOff = fileOff; 891 } else {
416 section->finalize(); 892 error("section from " + toString(it->second->firstSection()->file) +
417 893 " conflicts with synthetic section " + ssec->segname + "," +
418 addr += section->getSize(); 894 ssec->name);
419 fileOff += section->getFileSize(); 895 }
420 } 896 }
421 } 897
422
423 void Writer::openFile() {
424 Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
425 FileOutputBuffer::create(config->outputFile, fileOff,
426 FileOutputBuffer::F_executable);
427
428 if (!bufferOrErr)
429 error("failed to open " + config->outputFile + ": " +
430 llvm::toString(bufferOrErr.takeError()));
431 else
432 buffer = std::move(*bufferOrErr);
433 }
434
435 void Writer::writeSections() {
436 uint8_t *buf = buffer->getBufferStart();
437 for (OutputSegment *seg : outputSegments) {
438 for (auto &p : seg->getSections()) {
439 OutputSection *section = p.second;
440 section->writeTo(buf + section->fileOff);
441 }
442 }
443 }
444
445 void Writer::run() {
446 // dyld requires __LINKEDIT segment to always exist (even if empty). 898 // dyld requires __LINKEDIT segment to always exist (even if empty).
447 OutputSegment *linkEditSegment = 899 linkEditSegment = getOrCreateOutputSegment(segment_names::linkEdit);
448 getOrCreateOutputSegment(segment_names::linkEdit); 900 }
449 901
450 scanRelocations(); 902 void Writer::finalizeAddresses() {
451 if (in.stubHelper->isNeeded()) 903 TimeTraceScope timeScope("Finalize addresses");
452 in.stubHelper->setup(); 904 uint64_t pageSize = target->getPageSize();
453
454 // Sort and assign sections to their respective segments. No more sections nor
455 // segments may be created after these methods run.
456 createOutputSections();
457 sortSegmentsAndSections();
458
459 createLoadCommands();
460
461 // Ensure that segments (and the sections they contain) are allocated 905 // Ensure that segments (and the sections they contain) are allocated
462 // addresses in ascending order, which dyld requires. 906 // addresses in ascending order, which dyld requires.
463 // 907 //
464 // Note that at this point, __LINKEDIT sections are empty, but we need to 908 // Note that at this point, __LINKEDIT sections are empty, but we need to
465 // determine addresses of other segments/sections before generating its 909 // determine addresses of other segments/sections before generating its
466 // contents. 910 // contents.
467 for (OutputSegment *seg : outputSegments) 911 for (OutputSegment *seg : outputSegments) {
468 if (seg != linkEditSegment) 912 if (seg == linkEditSegment)
469 assignAddresses(seg); 913 continue;
470 914 assignAddresses(seg);
915 // codesign / libstuff checks for segment ordering by verifying that
916 // `fileOff + fileSize == next segment fileOff`. So we call alignTo() before
917 // (instead of after) computing fileSize to ensure that the segments are
918 // contiguous. We handle addr / vmSize similarly for the same reason.
919 fileOff = alignTo(fileOff, pageSize);
920 addr = alignTo(addr, pageSize);
921 seg->vmSize = addr - seg->firstSection()->addr;
922 seg->fileSize = fileOff - seg->fileOff;
923 }
924 }
925
926 void Writer::finalizeLinkEditSegment() {
927 TimeTraceScope timeScope("Finalize __LINKEDIT segment");
471 // Fill __LINKEDIT contents. 928 // Fill __LINKEDIT contents.
472 bindingSection->finalizeContents(); 929 std::vector<LinkEditSection *> linkEditSections{
473 lazyBindingSection->finalizeContents(); 930 in.rebase, in.binding, in.weakBinding, in.lazyBinding,
474 exportSection->finalizeContents(); 931 in.exports, symtabSection, indirectSymtabSection, functionStartsSection,
475 symtabSection->finalizeContents(); 932 };
933 parallelForEach(linkEditSections, [](LinkEditSection *osec) {
934 if (osec)
935 osec->finalizeContents();
936 });
476 937
477 // Now that __LINKEDIT is filled out, do a proper calculation of its 938 // Now that __LINKEDIT is filled out, do a proper calculation of its
478 // addresses and offsets. 939 // addresses and offsets.
479 assignAddresses(linkEditSegment); 940 assignAddresses(linkEditSegment);
480 941 // No need to page-align fileOff / addr here since this is the last segment.
942 linkEditSegment->vmSize = addr - linkEditSegment->firstSection()->addr;
943 linkEditSegment->fileSize = fileOff - linkEditSegment->fileOff;
944 }
945
946 void Writer::assignAddresses(OutputSegment *seg) {
947 seg->fileOff = fileOff;
948
949 for (OutputSection *osec : seg->getSections()) {
950 if (!osec->isNeeded())
951 continue;
952 addr = alignTo(addr, osec->align);
953 fileOff = alignTo(fileOff, osec->align);
954 osec->addr = addr;
955 osec->fileOff = isZeroFill(osec->flags) ? 0 : fileOff;
956 osec->finalize();
957
958 addr += osec->getSize();
959 fileOff += osec->getFileSize();
960 }
961 }
962
963 void Writer::openFile() {
964 Expected<std::unique_ptr<FileOutputBuffer>> bufferOrErr =
965 FileOutputBuffer::create(config->outputFile, fileOff,
966 FileOutputBuffer::F_executable);
967
968 if (!bufferOrErr)
969 error("failed to open " + config->outputFile + ": " +
970 llvm::toString(bufferOrErr.takeError()));
971 else
972 buffer = std::move(*bufferOrErr);
973 }
974
975 void Writer::writeSections() {
976 uint8_t *buf = buffer->getBufferStart();
977 for (const OutputSegment *seg : outputSegments)
978 for (const OutputSection *osec : seg->getSections())
979 osec->writeTo(buf + osec->fileOff);
980 }
981
982 // In order to utilize multiple cores, we first split the buffer into chunks,
983 // compute a hash for each chunk, and then compute a hash value of the hash
984 // values.
985 void Writer::writeUuid() {
986 TimeTraceScope timeScope("Computing UUID");
987 ArrayRef<uint8_t> data{buffer->getBufferStart(), buffer->getBufferEnd()};
988 unsigned chunkCount = parallel::strategy.compute_thread_count() * 10;
989 // Round-up integer division
990 size_t chunkSize = (data.size() + chunkCount - 1) / chunkCount;
991 std::vector<ArrayRef<uint8_t>> chunks = split(data, chunkSize);
992 std::vector<uint64_t> hashes(chunks.size());
993 parallelForEachN(0, chunks.size(),
994 [&](size_t i) { hashes[i] = xxHash64(chunks[i]); });
995 uint64_t digest = xxHash64({reinterpret_cast<uint8_t *>(hashes.data()),
996 hashes.size() * sizeof(uint64_t)});
997 uuidCommand->writeUuid(digest);
998 }
999
1000 void Writer::writeCodeSignature() {
1001 if (codeSignatureSection)
1002 codeSignatureSection->writeHashes(buffer->getBufferStart());
1003 }
1004
1005 void Writer::writeOutputFile() {
1006 TimeTraceScope timeScope("Write output file");
481 openFile(); 1007 openFile();
482 if (errorCount()) 1008 if (errorCount())
483 return; 1009 return;
484
485 writeSections(); 1010 writeSections();
1011 writeUuid();
1012 writeCodeSignature();
486 1013
487 if (auto e = buffer->commit()) 1014 if (auto e = buffer->commit())
488 error("failed to write to the output file: " + toString(std::move(e))); 1015 error("failed to write to the output file: " + toString(std::move(e)));
489 } 1016 }
490 1017
491 void macho::writeResult() { Writer().run(); } 1018 template <class LP> void Writer::run() {
1019 if (config->entry && !isa<Undefined>(config->entry))
1020 prepareBranchTarget(config->entry);
1021 scanRelocations();
1022 if (in.stubHelper->isNeeded())
1023 in.stubHelper->setup();
1024 scanSymbols();
1025 createOutputSections<LP>();
1026 // After this point, we create no new segments; HOWEVER, we might
1027 // yet create branch-range extension thunks for architectures whose
1028 // hardware call instructions have limited range, e.g., ARM(64).
1029 // The thunks are created as InputSections interspersed among
1030 // the ordinary __TEXT,_text InputSections.
1031 sortSegmentsAndSections();
1032 createLoadCommands<LP>();
1033 finalizeAddresses();
1034 finalizeLinkEditSegment();
1035 writeMapFile();
1036 writeOutputFile();
1037 }
1038
1039 template <class LP> void macho::writeResult() { Writer().run<LP>(); }
492 1040
493 void macho::createSyntheticSections() { 1041 void macho::createSyntheticSections() {
1042 in.header = make<MachHeaderSection>();
1043 in.rebase = make<RebaseSection>();
1044 in.binding = make<BindingSection>();
1045 in.weakBinding = make<WeakBindingSection>();
1046 in.lazyBinding = make<LazyBindingSection>();
1047 in.exports = make<ExportSection>();
494 in.got = make<GotSection>(); 1048 in.got = make<GotSection>();
1049 in.tlvPointers = make<TlvPointerSection>();
495 in.lazyPointers = make<LazyPointerSection>(); 1050 in.lazyPointers = make<LazyPointerSection>();
496 in.stubs = make<StubsSection>(); 1051 in.stubs = make<StubsSection>();
497 in.stubHelper = make<StubHelperSection>(); 1052 in.stubHelper = make<StubHelperSection>();
498 in.imageLoaderCache = make<ImageLoaderCacheSection>(); 1053 in.imageLoaderCache = make<ImageLoaderCacheSection>();
499 } 1054 in.unwindInfo = makeUnwindInfoSection();
1055 }
1056
1057 OutputSection *macho::firstTLVDataSection = nullptr;
1058
1059 template void macho::writeResult<LP64>();
1060 template void macho::writeResult<ILP32>();