blob: 9f4e4fc4fdbfe15ecf593e8a6085d2916712b8b2 [file] [log] [blame]
Seiya Nuta552bcb82019-08-19 21:05:31 +00001//===- MachOLayoutBuilder.cpp -----------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "MachOLayoutBuilder.h"
10#include "llvm/Support/Errc.h"
11#include "llvm/Support/ErrorHandling.h"
12
13namespace llvm {
14namespace objcopy {
15namespace macho {
16
17uint32_t MachOLayoutBuilder::computeSizeOfCmds() const {
18 uint32_t Size = 0;
19 for (const auto &LC : O.LoadCommands) {
Seiya Nuta12bd4902019-08-19 21:12:02 +000020 const MachO::macho_load_command &MLC = LC.MachOLoadCommand;
Seiya Nuta552bcb82019-08-19 21:05:31 +000021 auto cmd = MLC.load_command_data.cmd;
22 switch (cmd) {
23 case MachO::LC_SEGMENT:
24 Size += sizeof(MachO::segment_command) +
25 sizeof(MachO::section) * LC.Sections.size();
26 continue;
27 case MachO::LC_SEGMENT_64:
28 Size += sizeof(MachO::segment_command_64) +
29 sizeof(MachO::section_64) * LC.Sections.size();
30 continue;
31 }
32
33 switch (cmd) {
34#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
35 case MachO::LCName: \
36 Size += sizeof(MachO::LCStruct) + LC.Payload.size(); \
37 break;
38#include "llvm/BinaryFormat/MachO.def"
39#undef HANDLE_LOAD_COMMAND
40 }
41 }
42
43 return Size;
44}
45
46void MachOLayoutBuilder::constructStringTable() {
47 for (std::unique_ptr<SymbolEntry> &Sym : O.SymTable.Symbols)
48 StrTableBuilder.add(Sym->Name);
49 StrTableBuilder.finalize();
50}
51
52void MachOLayoutBuilder::updateSymbolIndexes() {
53 uint32_t Index = 0;
54 for (auto &Symbol : O.SymTable.Symbols)
55 Symbol->Index = Index++;
56}
57
58// Updates the index and the number of local/external/undefined symbols.
59void MachOLayoutBuilder::updateDySymTab(MachO::macho_load_command &MLC) {
60 assert(MLC.load_command_data.cmd == MachO::LC_DYSYMTAB);
61 // Make sure that nlist entries in the symbol table are sorted by the those
62 // types. The order is: local < defined external < undefined external.
63 assert(std::is_sorted(O.SymTable.Symbols.begin(), O.SymTable.Symbols.end(),
64 [](const std::unique_ptr<SymbolEntry> &A,
65 const std::unique_ptr<SymbolEntry> &B) {
66 return (A->isLocalSymbol() && !B->isLocalSymbol()) ||
67 (!A->isUndefinedSymbol() &&
68 B->isUndefinedSymbol());
69 }) &&
70 "Symbols are not sorted by their types.");
71
72 uint32_t NumLocalSymbols = 0;
73 auto Iter = O.SymTable.Symbols.begin();
74 auto End = O.SymTable.Symbols.end();
75 for (; Iter != End; ++Iter) {
76 if ((*Iter)->isExternalSymbol())
77 break;
78
79 ++NumLocalSymbols;
80 }
81
82 uint32_t NumExtDefSymbols = 0;
83 for (; Iter != End; ++Iter) {
84 if ((*Iter)->isUndefinedSymbol())
85 break;
86
87 ++NumExtDefSymbols;
88 }
89
90 MLC.dysymtab_command_data.ilocalsym = 0;
91 MLC.dysymtab_command_data.nlocalsym = NumLocalSymbols;
92 MLC.dysymtab_command_data.iextdefsym = NumLocalSymbols;
93 MLC.dysymtab_command_data.nextdefsym = NumExtDefSymbols;
94 MLC.dysymtab_command_data.iundefsym = NumLocalSymbols + NumExtDefSymbols;
95 MLC.dysymtab_command_data.nundefsym =
96 O.SymTable.Symbols.size() - (NumLocalSymbols + NumExtDefSymbols);
97}
98
99// Recomputes and updates offset and size fields in load commands and sections
100// since they could be modified.
101uint64_t MachOLayoutBuilder::layoutSegments() {
102 auto HeaderSize =
103 Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header);
Seiya Nuta12bd4902019-08-19 21:12:02 +0000104 const bool IsObjectFile =
105 O.Header.FileType == MachO::HeaderFileType::MH_OBJECT;
106 uint64_t Offset = IsObjectFile ? (HeaderSize + O.Header.SizeOfCmds) : 0;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000107 for (auto &LC : O.LoadCommands) {
Seiya Nuta552bcb82019-08-19 21:05:31 +0000108 auto &MLC = LC.MachOLoadCommand;
109 StringRef Segname;
Seiya Nuta12bd4902019-08-19 21:12:02 +0000110 uint64_t SegmentVmAddr;
111 uint64_t SegmentVmSize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000112 switch (MLC.load_command_data.cmd) {
113 case MachO::LC_SEGMENT:
Seiya Nuta12bd4902019-08-19 21:12:02 +0000114 SegmentVmAddr = MLC.segment_command_data.vmaddr;
115 SegmentVmSize = MLC.segment_command_data.vmsize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000116 Segname = StringRef(MLC.segment_command_data.segname,
117 strnlen(MLC.segment_command_data.segname,
118 sizeof(MLC.segment_command_data.segname)));
119 break;
120 case MachO::LC_SEGMENT_64:
Seiya Nuta12bd4902019-08-19 21:12:02 +0000121 SegmentVmAddr = MLC.segment_command_64_data.vmaddr;
122 SegmentVmSize = MLC.segment_command_64_data.vmsize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000123 Segname = StringRef(MLC.segment_command_64_data.segname,
124 strnlen(MLC.segment_command_64_data.segname,
125 sizeof(MLC.segment_command_64_data.segname)));
126 break;
127 default:
128 continue;
129 }
130
131 if (Segname == "__LINKEDIT") {
132 // We update the __LINKEDIT segment later (in layoutTail).
133 assert(LC.Sections.empty() && "__LINKEDIT segment has sections");
134 LinkEditLoadCommand = &MLC;
135 continue;
136 }
137
138 // Update file offsets and sizes of sections.
Seiya Nuta12bd4902019-08-19 21:12:02 +0000139 uint64_t SegOffset = Offset;
140 uint64_t SegFileSize = 0;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000141 uint64_t VMSize = 0;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000142 for (auto &Sec : LC.Sections) {
Seiya Nuta12bd4902019-08-19 21:12:02 +0000143 if (IsObjectFile) {
144 if (Sec.isVirtualSection()) {
145 Sec.Offset = 0;
146 } else {
147 uint64_t PaddingSize = OffsetToAlignment(SegFileSize, 1 << Sec.Align);
148 Sec.Offset = SegOffset + SegFileSize + PaddingSize;
149 Sec.Size = Sec.Content.size();
150 SegFileSize += PaddingSize + Sec.Size;
151 }
152 VMSize = std::max(VMSize, Sec.Addr + Sec.Size);
153 } else {
154 if (Sec.isVirtualSection()) {
155 Sec.Offset = 0;
156 VMSize += Sec.Size;
157 } else {
158 uint32_t SectOffset = Sec.Addr - SegmentVmAddr;
159 Sec.Offset = SegOffset + SectOffset;
160 Sec.Size = Sec.Content.size();
161 SegFileSize = std::max(SegFileSize, SectOffset + Sec.Size);
162 VMSize = std::max(VMSize, SegFileSize);
163 }
Seiya Nuta552bcb82019-08-19 21:05:31 +0000164 }
Seiya Nuta552bcb82019-08-19 21:05:31 +0000165 }
166
Seiya Nuta12bd4902019-08-19 21:12:02 +0000167 if (IsObjectFile) {
168 Offset += SegFileSize;
169 } else {
170 Offset = alignTo(Offset + SegFileSize, PageSize);
171 SegFileSize = alignTo(SegFileSize, PageSize);
172 // Use the original vmsize if the segment is __PAGEZERO.
173 VMSize =
174 Segname == "__PAGEZERO" ? SegmentVmSize : alignTo(VMSize, PageSize);
175 }
176
Seiya Nuta552bcb82019-08-19 21:05:31 +0000177 switch (MLC.load_command_data.cmd) {
178 case MachO::LC_SEGMENT:
179 MLC.segment_command_data.cmdsize =
180 sizeof(MachO::segment_command) +
181 sizeof(MachO::section) * LC.Sections.size();
182 MLC.segment_command_data.nsects = LC.Sections.size();
Seiya Nuta12bd4902019-08-19 21:12:02 +0000183 MLC.segment_command_data.fileoff = SegOffset;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000184 MLC.segment_command_data.vmsize = VMSize;
Seiya Nuta12bd4902019-08-19 21:12:02 +0000185 MLC.segment_command_data.filesize = SegFileSize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000186 break;
187 case MachO::LC_SEGMENT_64:
188 MLC.segment_command_64_data.cmdsize =
189 sizeof(MachO::segment_command_64) +
190 sizeof(MachO::section_64) * LC.Sections.size();
191 MLC.segment_command_64_data.nsects = LC.Sections.size();
Seiya Nuta12bd4902019-08-19 21:12:02 +0000192 MLC.segment_command_64_data.fileoff = SegOffset;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000193 MLC.segment_command_64_data.vmsize = VMSize;
Seiya Nuta12bd4902019-08-19 21:12:02 +0000194 MLC.segment_command_64_data.filesize = SegFileSize;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000195 break;
196 }
Seiya Nuta552bcb82019-08-19 21:05:31 +0000197 }
198
199 return Offset;
200}
201
202uint64_t MachOLayoutBuilder::layoutRelocations(uint64_t Offset) {
203 for (auto &LC : O.LoadCommands)
204 for (auto &Sec : LC.Sections) {
205 Sec.RelOff = Sec.Relocations.empty() ? 0 : Offset;
206 Sec.NReloc = Sec.Relocations.size();
207 Offset += sizeof(MachO::any_relocation_info) * Sec.NReloc;
208 }
209
210 return Offset;
211}
212
213Error MachOLayoutBuilder::layoutTail(uint64_t Offset) {
214 // The order of LINKEDIT elements is as follows:
215 // rebase info, binding info, weak binding info, lazy binding info, export
216 // trie, data-in-code, symbol table, indirect symbol table, symbol table
217 // strings.
218 uint64_t NListSize = Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist);
219 uint64_t StartOfLinkEdit = Offset;
220 uint64_t StartOfRebaseInfo = StartOfLinkEdit;
221 uint64_t StartOfBindingInfo = StartOfRebaseInfo + O.Rebases.Opcodes.size();
222 uint64_t StartOfWeakBindingInfo = StartOfBindingInfo + O.Binds.Opcodes.size();
223 uint64_t StartOfLazyBindingInfo =
224 StartOfWeakBindingInfo + O.WeakBinds.Opcodes.size();
225 uint64_t StartOfExportTrie =
226 StartOfLazyBindingInfo + O.LazyBinds.Opcodes.size();
227 uint64_t StartOfFunctionStarts = StartOfExportTrie + O.Exports.Trie.size();
228 uint64_t StartOfDataInCode =
229 StartOfFunctionStarts + O.FunctionStarts.Data.size();
230 uint64_t StartOfSymbols = StartOfDataInCode + O.DataInCode.Data.size();
231 uint64_t StartOfIndirectSymbols =
232 StartOfSymbols + NListSize * O.SymTable.Symbols.size();
233 uint64_t StartOfSymbolStrings =
234 StartOfIndirectSymbols +
235 sizeof(uint32_t) * O.IndirectSymTable.Symbols.size();
236 uint64_t LinkEditSize =
237 (StartOfSymbolStrings + StrTableBuilder.getSize()) - StartOfLinkEdit;
238
239 // Now we have determined the layout of the contents of the __LINKEDIT
240 // segment. Update its load command.
241 if (LinkEditLoadCommand) {
242 MachO::macho_load_command *MLC = LinkEditLoadCommand;
243 switch (LinkEditLoadCommand->load_command_data.cmd) {
244 case MachO::LC_SEGMENT:
245 MLC->segment_command_data.cmdsize = sizeof(MachO::segment_command);
246 MLC->segment_command_data.fileoff = StartOfLinkEdit;
247 MLC->segment_command_data.vmsize = alignTo(LinkEditSize, PageSize);
248 MLC->segment_command_data.filesize = LinkEditSize;
249 break;
250 case MachO::LC_SEGMENT_64:
251 MLC->segment_command_64_data.cmdsize = sizeof(MachO::segment_command_64);
252 MLC->segment_command_64_data.fileoff = StartOfLinkEdit;
253 MLC->segment_command_64_data.vmsize = alignTo(LinkEditSize, PageSize);
254 MLC->segment_command_64_data.filesize = LinkEditSize;
255 break;
256 }
257 }
258
259 for (auto &LC : O.LoadCommands) {
260 auto &MLC = LC.MachOLoadCommand;
261 auto cmd = MLC.load_command_data.cmd;
262 switch (cmd) {
263 case MachO::LC_SYMTAB:
264 MLC.symtab_command_data.symoff = StartOfSymbols;
265 MLC.symtab_command_data.nsyms = O.SymTable.Symbols.size();
266 MLC.symtab_command_data.stroff = StartOfSymbolStrings;
267 MLC.symtab_command_data.strsize = StrTableBuilder.getSize();
268 break;
269 case MachO::LC_DYSYMTAB: {
270 if (MLC.dysymtab_command_data.ntoc != 0 ||
271 MLC.dysymtab_command_data.nmodtab != 0 ||
272 MLC.dysymtab_command_data.nextrefsyms != 0 ||
273 MLC.dysymtab_command_data.nlocrel != 0 ||
274 MLC.dysymtab_command_data.nextrel != 0)
275 return createStringError(llvm::errc::not_supported,
276 "shared library is not yet supported");
277
278 if (!O.IndirectSymTable.Symbols.empty()) {
279 MLC.dysymtab_command_data.indirectsymoff = StartOfIndirectSymbols;
280 MLC.dysymtab_command_data.nindirectsyms =
281 O.IndirectSymTable.Symbols.size();
282 }
283
284 updateDySymTab(MLC);
285 break;
286 }
287 case MachO::LC_DATA_IN_CODE:
288 MLC.linkedit_data_command_data.dataoff = StartOfDataInCode;
289 MLC.linkedit_data_command_data.datasize = O.DataInCode.Data.size();
290 break;
291 case MachO::LC_FUNCTION_STARTS:
292 MLC.linkedit_data_command_data.dataoff = StartOfFunctionStarts;
293 MLC.linkedit_data_command_data.datasize = O.FunctionStarts.Data.size();
294 break;
295 case MachO::LC_DYLD_INFO:
296 case MachO::LC_DYLD_INFO_ONLY:
297 MLC.dyld_info_command_data.rebase_off =
298 O.Rebases.Opcodes.empty() ? 0 : StartOfRebaseInfo;
299 MLC.dyld_info_command_data.rebase_size = O.Rebases.Opcodes.size();
300 MLC.dyld_info_command_data.bind_off =
301 O.Binds.Opcodes.empty() ? 0 : StartOfBindingInfo;
302 MLC.dyld_info_command_data.bind_size = O.Binds.Opcodes.size();
303 MLC.dyld_info_command_data.weak_bind_off =
304 O.WeakBinds.Opcodes.empty() ? 0 : StartOfWeakBindingInfo;
305 MLC.dyld_info_command_data.weak_bind_size = O.WeakBinds.Opcodes.size();
306 MLC.dyld_info_command_data.lazy_bind_off =
307 O.LazyBinds.Opcodes.empty() ? 0 : StartOfLazyBindingInfo;
308 MLC.dyld_info_command_data.lazy_bind_size = O.LazyBinds.Opcodes.size();
309 MLC.dyld_info_command_data.export_off =
310 O.Exports.Trie.empty() ? 0 : StartOfExportTrie;
311 MLC.dyld_info_command_data.export_size = O.Exports.Trie.size();
312 break;
313 case MachO::LC_LOAD_DYLINKER:
314 case MachO::LC_MAIN:
315 case MachO::LC_RPATH:
316 case MachO::LC_SEGMENT:
317 case MachO::LC_SEGMENT_64:
318 case MachO::LC_VERSION_MIN_MACOSX:
319 case MachO::LC_BUILD_VERSION:
320 case MachO::LC_ID_DYLIB:
321 case MachO::LC_LOAD_DYLIB:
322 case MachO::LC_UUID:
323 case MachO::LC_SOURCE_VERSION:
324 // Nothing to update.
325 break;
326 default:
327 // Abort if it's unsupported in order to prevent corrupting the object.
328 return createStringError(llvm::errc::not_supported,
329 "unsupported load command (cmd=0x%x)", cmd);
330 }
331 }
332
333 return Error::success();
334}
335
336Error MachOLayoutBuilder::layout() {
337 O.Header.NCmds = O.LoadCommands.size();
338 O.Header.SizeOfCmds = computeSizeOfCmds();
339 constructStringTable();
340 updateSymbolIndexes();
341 uint64_t Offset = layoutSegments();
342 Offset = layoutRelocations(Offset);
343 return layoutTail(Offset);
344}
345
346} // end namespace macho
347} // end namespace objcopy
348} // end namespace llvm