173
|
1 //===- Writer.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 "Writer.h"
|
|
10 #include "Config.h"
|
|
11 #include "InputFiles.h"
|
|
12 #include "InputSection.h"
|
|
13 #include "MergedOutputSection.h"
|
|
14 #include "OutputSection.h"
|
|
15 #include "OutputSegment.h"
|
|
16 #include "SymbolTable.h"
|
|
17 #include "Symbols.h"
|
|
18 #include "SyntheticSections.h"
|
|
19 #include "Target.h"
|
|
20
|
|
21 #include "lld/Common/ErrorHandler.h"
|
|
22 #include "lld/Common/Memory.h"
|
|
23 #include "llvm/BinaryFormat/MachO.h"
|
|
24 #include "llvm/Support/LEB128.h"
|
|
25 #include "llvm/Support/MathExtras.h"
|
|
26 #include "llvm/Support/Path.h"
|
|
27
|
|
28 using namespace llvm;
|
|
29 using namespace llvm::MachO;
|
|
30 using namespace lld;
|
|
31 using namespace lld::macho;
|
|
32
|
|
33 namespace {
|
|
34 class LCLinkEdit;
|
|
35 class LCDyldInfo;
|
|
36 class LCSymtab;
|
|
37
|
|
38 class Writer {
|
|
39 public:
|
|
40 Writer() : buffer(errorHandler().outputBuffer) {}
|
|
41
|
|
42 void scanRelocations();
|
|
43 void createOutputSections();
|
|
44 void createLoadCommands();
|
|
45 void assignAddresses(OutputSegment *);
|
|
46 void createSymtabContents();
|
|
47
|
|
48 void openFile();
|
|
49 void writeSections();
|
|
50
|
|
51 void run();
|
|
52
|
|
53 std::unique_ptr<FileOutputBuffer> &buffer;
|
|
54 uint64_t addr = 0;
|
|
55 uint64_t fileOff = 0;
|
|
56 MachHeaderSection *headerSection = nullptr;
|
|
57 BindingSection *bindingSection = nullptr;
|
|
58 LazyBindingSection *lazyBindingSection = nullptr;
|
|
59 ExportSection *exportSection = nullptr;
|
|
60 StringTableSection *stringTableSection = nullptr;
|
|
61 SymtabSection *symtabSection = nullptr;
|
|
62 };
|
|
63
|
|
64 // LC_DYLD_INFO_ONLY stores the offsets of symbol import/export information.
|
|
65 class LCDyldInfo : public LoadCommand {
|
|
66 public:
|
|
67 LCDyldInfo(BindingSection *bindingSection,
|
|
68 LazyBindingSection *lazyBindingSection,
|
|
69 ExportSection *exportSection)
|
|
70 : bindingSection(bindingSection), lazyBindingSection(lazyBindingSection),
|
|
71 exportSection(exportSection) {}
|
|
72
|
|
73 uint32_t getSize() const override { return sizeof(dyld_info_command); }
|
|
74
|
|
75 void writeTo(uint8_t *buf) const override {
|
|
76 auto *c = reinterpret_cast<dyld_info_command *>(buf);
|
|
77 c->cmd = LC_DYLD_INFO_ONLY;
|
|
78 c->cmdsize = getSize();
|
|
79 if (bindingSection->isNeeded()) {
|
|
80 c->bind_off = bindingSection->fileOff;
|
|
81 c->bind_size = bindingSection->getFileSize();
|
|
82 }
|
|
83 if (lazyBindingSection->isNeeded()) {
|
|
84 c->lazy_bind_off = lazyBindingSection->fileOff;
|
|
85 c->lazy_bind_size = lazyBindingSection->getFileSize();
|
|
86 }
|
|
87 if (exportSection->isNeeded()) {
|
|
88 c->export_off = exportSection->fileOff;
|
|
89 c->export_size = exportSection->getFileSize();
|
|
90 }
|
|
91 }
|
|
92
|
|
93 BindingSection *bindingSection;
|
|
94 LazyBindingSection *lazyBindingSection;
|
|
95 ExportSection *exportSection;
|
|
96 };
|
|
97
|
|
98 class LCDysymtab : public LoadCommand {
|
|
99 public:
|
|
100 uint32_t getSize() const override { return sizeof(dysymtab_command); }
|
|
101
|
|
102 void writeTo(uint8_t *buf) const override {
|
|
103 auto *c = reinterpret_cast<dysymtab_command *>(buf);
|
|
104 c->cmd = LC_DYSYMTAB;
|
|
105 c->cmdsize = getSize();
|
|
106 }
|
|
107 };
|
|
108
|
|
109 class LCSegment : public LoadCommand {
|
|
110 public:
|
|
111 LCSegment(StringRef name, OutputSegment *seg) : name(name), seg(seg) {}
|
|
112
|
|
113 uint32_t getSize() const override {
|
|
114 return sizeof(segment_command_64) +
|
|
115 seg->numNonHiddenSections() * sizeof(section_64);
|
|
116 }
|
|
117
|
|
118 void writeTo(uint8_t *buf) const override {
|
|
119 auto *c = reinterpret_cast<segment_command_64 *>(buf);
|
|
120 buf += sizeof(segment_command_64);
|
|
121
|
|
122 c->cmd = LC_SEGMENT_64;
|
|
123 c->cmdsize = getSize();
|
|
124 memcpy(c->segname, name.data(), name.size());
|
|
125 c->fileoff = seg->fileOff;
|
|
126 c->maxprot = seg->maxProt;
|
|
127 c->initprot = seg->initProt;
|
|
128
|
|
129 if (seg->getSections().empty())
|
|
130 return;
|
|
131
|
|
132 c->vmaddr = seg->firstSection()->addr;
|
|
133 c->vmsize =
|
|
134 seg->lastSection()->addr + seg->lastSection()->getSize() - c->vmaddr;
|
|
135 c->nsects = seg->numNonHiddenSections();
|
|
136
|
|
137 for (auto &p : seg->getSections()) {
|
|
138 StringRef s = p.first;
|
|
139 OutputSection *section = p.second;
|
|
140 c->filesize += section->getFileSize();
|
|
141
|
|
142 if (section->isHidden())
|
|
143 continue;
|
|
144
|
|
145 auto *sectHdr = reinterpret_cast<section_64 *>(buf);
|
|
146 buf += sizeof(section_64);
|
|
147
|
|
148 memcpy(sectHdr->sectname, s.data(), s.size());
|
|
149 memcpy(sectHdr->segname, name.data(), name.size());
|
|
150
|
|
151 sectHdr->addr = section->addr;
|
|
152 sectHdr->offset = section->fileOff;
|
|
153 sectHdr->align = Log2_32(section->align);
|
|
154 sectHdr->flags = section->flags;
|
|
155 sectHdr->size = section->getSize();
|
|
156 }
|
|
157 }
|
|
158
|
|
159 private:
|
|
160 StringRef name;
|
|
161 OutputSegment *seg;
|
|
162 };
|
|
163
|
|
164 class LCMain : public LoadCommand {
|
|
165 uint32_t getSize() const override { return sizeof(entry_point_command); }
|
|
166
|
|
167 void writeTo(uint8_t *buf) const override {
|
|
168 auto *c = reinterpret_cast<entry_point_command *>(buf);
|
|
169 c->cmd = LC_MAIN;
|
|
170 c->cmdsize = getSize();
|
|
171 c->entryoff = config->entry->getVA() - ImageBase;
|
|
172 c->stacksize = 0;
|
|
173 }
|
|
174 };
|
|
175
|
|
176 class LCSymtab : public LoadCommand {
|
|
177 public:
|
|
178 LCSymtab(SymtabSection *symtabSection, StringTableSection *stringTableSection)
|
|
179 : symtabSection(symtabSection), stringTableSection(stringTableSection) {}
|
|
180
|
|
181 uint32_t getSize() const override { return sizeof(symtab_command); }
|
|
182
|
|
183 void writeTo(uint8_t *buf) const override {
|
|
184 auto *c = reinterpret_cast<symtab_command *>(buf);
|
|
185 c->cmd = LC_SYMTAB;
|
|
186 c->cmdsize = getSize();
|
|
187 c->symoff = symtabSection->fileOff;
|
|
188 c->nsyms = symtabSection->getNumSymbols();
|
|
189 c->stroff = stringTableSection->fileOff;
|
|
190 c->strsize = stringTableSection->getFileSize();
|
|
191 }
|
|
192
|
|
193 SymtabSection *symtabSection = nullptr;
|
|
194 StringTableSection *stringTableSection = nullptr;
|
|
195 };
|
|
196
|
|
197 // There are several dylib load commands that share the same structure:
|
|
198 // * LC_LOAD_DYLIB
|
|
199 // * LC_ID_DYLIB
|
|
200 // * LC_REEXPORT_DYLIB
|
|
201 class LCDylib : public LoadCommand {
|
|
202 public:
|
|
203 LCDylib(LoadCommandType type, StringRef path) : type(type), path(path) {}
|
|
204
|
|
205 uint32_t getSize() const override {
|
|
206 return alignTo(sizeof(dylib_command) + path.size() + 1, 8);
|
|
207 }
|
|
208
|
|
209 void writeTo(uint8_t *buf) const override {
|
|
210 auto *c = reinterpret_cast<dylib_command *>(buf);
|
|
211 buf += sizeof(dylib_command);
|
|
212
|
|
213 c->cmd = type;
|
|
214 c->cmdsize = getSize();
|
|
215 c->dylib.name = sizeof(dylib_command);
|
|
216
|
|
217 memcpy(buf, path.data(), path.size());
|
|
218 buf[path.size()] = '\0';
|
|
219 }
|
|
220
|
|
221 private:
|
|
222 LoadCommandType type;
|
|
223 StringRef path;
|
|
224 };
|
|
225
|
|
226 class LCLoadDylinker : public LoadCommand {
|
|
227 public:
|
|
228 uint32_t getSize() const override {
|
|
229 return alignTo(sizeof(dylinker_command) + path.size() + 1, 8);
|
|
230 }
|
|
231
|
|
232 void writeTo(uint8_t *buf) const override {
|
|
233 auto *c = reinterpret_cast<dylinker_command *>(buf);
|
|
234 buf += sizeof(dylinker_command);
|
|
235
|
|
236 c->cmd = LC_LOAD_DYLINKER;
|
|
237 c->cmdsize = getSize();
|
|
238 c->name = sizeof(dylinker_command);
|
|
239
|
|
240 memcpy(buf, path.data(), path.size());
|
|
241 buf[path.size()] = '\0';
|
|
242 }
|
|
243
|
|
244 private:
|
|
245 // Recent versions of Darwin won't run any binary that has dyld at a
|
|
246 // different location.
|
|
247 const StringRef path = "/usr/lib/dyld";
|
|
248 };
|
|
249 } // namespace
|
|
250
|
|
251 void Writer::scanRelocations() {
|
|
252 for (InputSection *sect : inputSections)
|
|
253 for (Reloc &r : sect->relocs)
|
|
254 if (auto *s = r.target.dyn_cast<Symbol *>())
|
|
255 if (auto *dylibSymbol = dyn_cast<DylibSymbol>(s))
|
|
256 target->prepareDylibSymbolRelocation(*dylibSymbol, r.type);
|
|
257 }
|
|
258
|
|
259 void Writer::createLoadCommands() {
|
|
260 headerSection->addLoadCommand(
|
|
261 make<LCDyldInfo>(bindingSection, lazyBindingSection, exportSection));
|
|
262 headerSection->addLoadCommand(
|
|
263 make<LCSymtab>(symtabSection, stringTableSection));
|
|
264 headerSection->addLoadCommand(make<LCDysymtab>());
|
|
265
|
|
266 switch (config->outputType) {
|
|
267 case MH_EXECUTE:
|
|
268 headerSection->addLoadCommand(make<LCMain>());
|
|
269 headerSection->addLoadCommand(make<LCLoadDylinker>());
|
|
270 break;
|
|
271 case MH_DYLIB:
|
|
272 headerSection->addLoadCommand(
|
|
273 make<LCDylib>(LC_ID_DYLIB, config->installName));
|
|
274 break;
|
|
275 default:
|
|
276 llvm_unreachable("unhandled output file type");
|
|
277 }
|
|
278
|
|
279 uint8_t segIndex = 0;
|
|
280 for (OutputSegment *seg : outputSegments) {
|
|
281 headerSection->addLoadCommand(make<LCSegment>(seg->name, seg));
|
|
282 seg->index = segIndex++;
|
|
283 }
|
|
284
|
|
285 uint64_t dylibOrdinal = 1;
|
|
286 for (InputFile *file : inputFiles) {
|
|
287 if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
|
|
288 headerSection->addLoadCommand(
|
|
289 make<LCDylib>(LC_LOAD_DYLIB, dylibFile->dylibName));
|
|
290 dylibFile->ordinal = dylibOrdinal++;
|
|
291
|
|
292 if (dylibFile->reexport)
|
|
293 headerSection->addLoadCommand(
|
|
294 make<LCDylib>(LC_REEXPORT_DYLIB, dylibFile->dylibName));
|
|
295 }
|
|
296 }
|
|
297 }
|
|
298
|
|
299 static size_t getSymbolPriority(const SymbolPriorityEntry &entry,
|
|
300 const InputFile &file) {
|
|
301 return std::max(entry.objectFiles.lookup(sys::path::filename(file.getName())),
|
|
302 entry.anyObjectFile);
|
|
303 }
|
|
304
|
|
305 // Each section gets assigned the priority of the highest-priority symbol it
|
|
306 // contains.
|
|
307 static DenseMap<const InputSection *, size_t> buildInputSectionPriorities() {
|
|
308 DenseMap<const InputSection *, size_t> sectionPriorities;
|
|
309
|
|
310 if (config->priorities.empty())
|
|
311 return sectionPriorities;
|
|
312
|
|
313 auto addSym = [&](Defined &sym) {
|
|
314 auto it = config->priorities.find(sym.getName());
|
|
315 if (it == config->priorities.end())
|
|
316 return;
|
|
317
|
|
318 SymbolPriorityEntry &entry = it->second;
|
|
319 size_t &priority = sectionPriorities[sym.isec];
|
|
320 priority = std::max(priority, getSymbolPriority(entry, *sym.isec->file));
|
|
321 };
|
|
322
|
|
323 // TODO: Make sure this handles weak symbols correctly.
|
|
324 for (InputFile *file : inputFiles)
|
|
325 if (isa<ObjFile>(file) || isa<ArchiveFile>(file))
|
|
326 for (Symbol *sym : file->symbols)
|
|
327 if (auto *d = dyn_cast<Defined>(sym))
|
|
328 addSym(*d);
|
|
329
|
|
330 return sectionPriorities;
|
|
331 }
|
|
332
|
|
333 // 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
|
|
335 // output segment.
|
|
336 static void sortSegmentsAndSections() {
|
|
337 auto comparator = OutputSegmentComparator();
|
|
338 llvm::stable_sort(outputSegments, comparator);
|
|
339
|
|
340 DenseMap<const InputSection *, size_t> isecPriorities =
|
|
341 buildInputSectionPriorities();
|
|
342
|
|
343 uint32_t sectionIndex = 0;
|
|
344 for (OutputSegment *seg : outputSegments) {
|
|
345 seg->sortOutputSections(&comparator);
|
|
346 for (auto &p : seg->getSections()) {
|
|
347 OutputSection *section = p.second;
|
|
348 // Now that the output sections are sorted, assign the final
|
|
349 // output section indices.
|
|
350 if (!section->isHidden())
|
|
351 section->index = ++sectionIndex;
|
|
352
|
|
353 if (!isecPriorities.empty()) {
|
|
354 if (auto *merged = dyn_cast<MergedOutputSection>(section)) {
|
|
355 llvm::stable_sort(merged->inputs,
|
|
356 [&](InputSection *a, InputSection *b) {
|
|
357 return isecPriorities[a] > isecPriorities[b];
|
|
358 });
|
|
359 }
|
|
360 }
|
|
361 }
|
|
362 }
|
|
363 }
|
|
364
|
|
365 void Writer::createOutputSections() {
|
|
366 // First, create hidden sections
|
|
367 headerSection = make<MachHeaderSection>();
|
|
368 bindingSection = make<BindingSection>();
|
|
369 lazyBindingSection = make<LazyBindingSection>();
|
|
370 stringTableSection = make<StringTableSection>();
|
|
371 symtabSection = make<SymtabSection>(*stringTableSection);
|
|
372 exportSection = make<ExportSection>();
|
|
373
|
|
374 switch (config->outputType) {
|
|
375 case MH_EXECUTE:
|
|
376 make<PageZeroSection>();
|
|
377 break;
|
|
378 case MH_DYLIB:
|
|
379 break;
|
|
380 default:
|
|
381 llvm_unreachable("unhandled output file type");
|
|
382 }
|
|
383
|
|
384 // Then merge input sections into output sections/segments.
|
|
385 for (InputSection *isec : inputSections) {
|
|
386 getOrCreateOutputSegment(isec->segname)
|
|
387 ->getOrCreateOutputSection(isec->name)
|
|
388 ->mergeInput(isec);
|
|
389 }
|
|
390
|
|
391 // Remove unneeded segments and sections.
|
|
392 // TODO: Avoid creating unneeded segments in the first place
|
|
393 for (auto it = outputSegments.begin(); it != outputSegments.end();) {
|
|
394 OutputSegment *seg = *it;
|
|
395 seg->removeUnneededSections();
|
|
396 if (!seg->isNeeded())
|
|
397 it = outputSegments.erase(it);
|
|
398 else
|
|
399 ++it;
|
|
400 }
|
|
401 }
|
|
402
|
|
403 void Writer::assignAddresses(OutputSegment *seg) {
|
|
404 addr = alignTo(addr, PageSize);
|
|
405 fileOff = alignTo(fileOff, PageSize);
|
|
406 seg->fileOff = fileOff;
|
|
407
|
|
408 for (auto &p : seg->getSections()) {
|
|
409 OutputSection *section = p.second;
|
|
410 addr = alignTo(addr, section->align);
|
|
411 // We must align the file offsets too to avoid misaligned writes of
|
|
412 // structs.
|
|
413 fileOff = alignTo(fileOff, section->align);
|
|
414 section->addr = addr;
|
|
415 section->fileOff = fileOff;
|
|
416 section->finalize();
|
|
417
|
|
418 addr += section->getSize();
|
|
419 fileOff += section->getFileSize();
|
|
420 }
|
|
421 }
|
|
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).
|
|
447 OutputSegment *linkEditSegment =
|
|
448 getOrCreateOutputSegment(segment_names::linkEdit);
|
|
449
|
|
450 scanRelocations();
|
|
451 if (in.stubHelper->isNeeded())
|
|
452 in.stubHelper->setup();
|
|
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
|
|
462 // addresses in ascending order, which dyld requires.
|
|
463 //
|
|
464 // Note that at this point, __LINKEDIT sections are empty, but we need to
|
|
465 // determine addresses of other segments/sections before generating its
|
|
466 // contents.
|
|
467 for (OutputSegment *seg : outputSegments)
|
|
468 if (seg != linkEditSegment)
|
|
469 assignAddresses(seg);
|
|
470
|
|
471 // Fill __LINKEDIT contents.
|
|
472 bindingSection->finalizeContents();
|
|
473 lazyBindingSection->finalizeContents();
|
|
474 exportSection->finalizeContents();
|
|
475 symtabSection->finalizeContents();
|
|
476
|
|
477 // Now that __LINKEDIT is filled out, do a proper calculation of its
|
|
478 // addresses and offsets.
|
|
479 assignAddresses(linkEditSegment);
|
|
480
|
|
481 openFile();
|
|
482 if (errorCount())
|
|
483 return;
|
|
484
|
|
485 writeSections();
|
|
486
|
|
487 if (auto e = buffer->commit())
|
|
488 error("failed to write to the output file: " + toString(std::move(e)));
|
|
489 }
|
|
490
|
|
491 void macho::writeResult() { Writer().run(); }
|
|
492
|
|
493 void macho::createSyntheticSections() {
|
|
494 in.got = make<GotSection>();
|
|
495 in.lazyPointers = make<LazyPointerSection>();
|
|
496 in.stubs = make<StubsSection>();
|
|
497 in.stubHelper = make<StubHelperSection>();
|
|
498 in.imageLoaderCache = make<ImageLoaderCacheSection>();
|
|
499 }
|