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