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