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