blob: da27c7cadf96cee3c0ca88b2f8d72ae23c92073c [file] [log] [blame]
Nick Kledzike34182f2013-11-06 21:36:55 +00001//===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Nick Kledzike34182f2013-11-06 21:36:55 +00006//
7//===----------------------------------------------------------------------===//
8
9///
Shankar Easwaran3d8de472014-01-27 03:09:26 +000010/// \file For mach-o object files, this implementation converts normalized
Nick Kledzike34182f2013-11-06 21:36:55 +000011/// mach-o in memory to mach-o binary on disk.
12///
Shankar Easwaran3d8de472014-01-27 03:09:26 +000013/// +---------------+
14/// | binary mach-o |
15/// +---------------+
Nick Kledzike34182f2013-11-06 21:36:55 +000016/// ^
17/// |
18/// |
Shankar Easwaran3d8de472014-01-27 03:09:26 +000019/// +------------+
20/// | normalized |
21/// +------------+
Nick Kledzike34182f2013-11-06 21:36:55 +000022
23#include "MachONormalizedFile.h"
24#include "MachONormalizedFileBinaryUtils.h"
Rui Ueyama3f851702017-10-02 21:00:41 +000025#include "lld/Common/LLVM.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000026#include "lld/Core/Error.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000027#include "llvm/ADT/SmallString.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000030#include "llvm/ADT/ilist.h"
31#include "llvm/ADT/ilist_node.h"
32#include "llvm/BinaryFormat/MachO.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000033#include "llvm/Support/Casting.h"
34#include "llvm/Support/Debug.h"
Shankar Easwaran2b67fca2014-10-18 05:33:55 +000035#include "llvm/Support/Errc.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000036#include "llvm/Support/ErrorHandling.h"
37#include "llvm/Support/FileOutputBuffer.h"
Nick Kledzik141330a2014-09-03 19:52:50 +000038#include "llvm/Support/Format.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000039#include "llvm/Support/Host.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000040#include "llvm/Support/MemoryBuffer.h"
41#include "llvm/Support/raw_ostream.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000042#include <functional>
Nick Kledzik07ba5122014-12-02 01:50:44 +000043#include <list>
Nick Kledzike34182f2013-11-06 21:36:55 +000044#include <map>
Rafael Espindola54427cc2014-06-12 17:15:58 +000045#include <system_error>
Nick Kledzike34182f2013-11-06 21:36:55 +000046
47using namespace llvm::MachO;
48
49namespace lld {
50namespace mach_o {
51namespace normalized {
52
Pete Coopere420dd42016-01-25 21:50:54 +000053struct TrieNode; // Forward declaration.
54
55struct TrieEdge : public llvm::ilist_node<TrieEdge> {
56 TrieEdge(StringRef s, TrieNode *node) : _subString(s), _child(node) {}
57
58 StringRef _subString;
59 struct TrieNode *_child;
60};
61
62} // namespace normalized
63} // namespace mach_o
64} // namespace lld
65
66
67namespace llvm {
Duncan P. N. Exon Smith9f710572016-09-03 01:29:36 +000068using lld::mach_o::normalized::TrieEdge;
69template <>
70struct ilist_alloc_traits<TrieEdge> : ilist_noalloc_traits<TrieEdge> {};
Pete Coopere420dd42016-01-25 21:50:54 +000071} // namespace llvm
72
73
74namespace lld {
75namespace mach_o {
76namespace normalized {
77
78struct TrieNode {
79 typedef llvm::ilist<TrieEdge> TrieEdgeList;
80
81 TrieNode(StringRef s)
82 : _cummulativeString(s), _address(0), _flags(0), _other(0),
83 _trieOffset(0), _hasExportInfo(false) {}
84 ~TrieNode() = default;
85
86 void addSymbol(const Export &entry, BumpPtrAllocator &allocator,
87 std::vector<TrieNode *> &allNodes);
Pete Cooperd0de3682016-08-05 21:37:12 +000088
89 void addOrderedNodes(const Export &entry,
90 std::vector<TrieNode *> &allNodes);
Pete Coopere420dd42016-01-25 21:50:54 +000091 bool updateOffset(uint32_t &offset);
92 void appendToByteBuffer(ByteBuffer &out);
93
94private:
95 StringRef _cummulativeString;
96 TrieEdgeList _children;
97 uint64_t _address;
98 uint64_t _flags;
99 uint64_t _other;
100 StringRef _importedName;
101 uint32_t _trieOffset;
102 bool _hasExportInfo;
Pete Cooperd0de3682016-08-05 21:37:12 +0000103 bool _ordered = false;
Pete Coopere420dd42016-01-25 21:50:54 +0000104};
105
Nick Kledzike34182f2013-11-06 21:36:55 +0000106/// Utility class for writing a mach-o binary file given an in-memory
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000107/// normalized file.
Nick Kledzike34182f2013-11-06 21:36:55 +0000108class MachOFileLayout {
109public:
Joey Goulyb275d7f2013-12-23 23:29:50 +0000110 /// All layout computation is done in the constructor.
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +0000111 MachOFileLayout(const NormalizedFile &file, bool alwaysIncludeFunctionStarts);
Joey Goulyb275d7f2013-12-23 23:29:50 +0000112
Nick Kledzike34182f2013-11-06 21:36:55 +0000113 /// Returns the final file size as computed in the constructor.
114 size_t size() const;
115
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000116 // Returns size of the mach_header and load commands.
117 size_t headerAndLoadCommandsSize() const;
118
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000119 /// Writes the normalized file as a binary mach-o file to the specified
Nick Kledzike34182f2013-11-06 21:36:55 +0000120 /// path. This does not have a stream interface because the generated
121 /// file may need the 'x' bit set.
Pete Cooperfefbd222016-03-30 23:10:39 +0000122 llvm::Error writeBinary(StringRef path);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000123
Nick Kledzike34182f2013-11-06 21:36:55 +0000124private:
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +0000125 uint32_t loadCommandsSize(uint32_t &count,
126 bool alwaysIncludeFunctionStarts);
Nick Kledzike34182f2013-11-06 21:36:55 +0000127 void buildFileOffsets();
128 void writeMachHeader();
Pete Cooper514594b2016-03-31 00:08:16 +0000129 llvm::Error writeLoadCommands();
Nick Kledzike34182f2013-11-06 21:36:55 +0000130 void writeSectionContent();
131 void writeRelocations();
132 void writeSymbolTable();
133 void writeRebaseInfo();
134 void writeBindingInfo();
135 void writeLazyBindingInfo();
Nick Kledzik141330a2014-09-03 19:52:50 +0000136 void writeExportInfo();
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000137 void writeFunctionStartsInfo();
Nick Kledzik21921372014-07-24 23:06:56 +0000138 void writeDataInCodeInfo();
Nick Kledzike34182f2013-11-06 21:36:55 +0000139 void writeLinkEditContent();
140 void buildLinkEditInfo();
141 void buildRebaseInfo();
142 void buildBindInfo();
143 void buildLazyBindInfo();
Nick Kledzik141330a2014-09-03 19:52:50 +0000144 void buildExportTrie();
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000145 void computeFunctionStartsSize();
Nick Kledzik21921372014-07-24 23:06:56 +0000146 void computeDataInCodeSize();
Nick Kledzike34182f2013-11-06 21:36:55 +0000147 void computeSymbolTableSizes();
148 void buildSectionRelocations();
149 void appendSymbols(const std::vector<Symbol> &symbols,
150 uint32_t &symOffset, uint32_t &strOffset);
151 uint32_t indirectSymbolIndex(const Section &sect, uint32_t &index);
152 uint32_t indirectSymbolElementSize(const Section &sect);
153
Nick Kledzik29f749e2013-11-09 00:07:28 +0000154 // For use as template parameter to load command methods.
155 struct MachO64Trait {
156 typedef llvm::MachO::segment_command_64 command;
157 typedef llvm::MachO::section_64 section;
158 enum { LC = llvm::MachO::LC_SEGMENT_64 };
159 };
160
161 // For use as template parameter to load command methods.
162 struct MachO32Trait {
163 typedef llvm::MachO::segment_command command;
164 typedef llvm::MachO::section section;
165 enum { LC = llvm::MachO::LC_SEGMENT };
166 };
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000167
Nick Kledzik29f749e2013-11-09 00:07:28 +0000168 template <typename T>
Pete Cooper514594b2016-03-31 00:08:16 +0000169 llvm::Error writeSingleSegmentLoadCommand(uint8_t *&lc);
170 template <typename T> llvm::Error writeSegmentLoadCommands(uint8_t *&lc);
Nick Kledzik29f749e2013-11-09 00:07:28 +0000171
Nick Kledzike34182f2013-11-06 21:36:55 +0000172 uint32_t pointerAlign(uint32_t value);
173 static StringRef dyldPath();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000174
Nick Kledzike34182f2013-11-06 21:36:55 +0000175 struct SegExtraInfo {
176 uint32_t fileOffset;
Tim Northover08d6a7b2014-06-30 09:49:30 +0000177 uint32_t fileSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000178 std::vector<const Section*> sections;
179 };
180 typedef std::map<const Segment*, SegExtraInfo> SegMap;
181 struct SectionExtraInfo {
182 uint32_t fileOffset;
183 };
184 typedef std::map<const Section*, SectionExtraInfo> SectionMap;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000185
Nick Kledzike34182f2013-11-06 21:36:55 +0000186 const NormalizedFile &_file;
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000187 std::error_code _ec;
Nick Kledzike34182f2013-11-06 21:36:55 +0000188 uint8_t *_buffer;
189 const bool _is64;
190 const bool _swap;
191 const bool _bigEndianArch;
192 uint64_t _seg1addr;
193 uint32_t _startOfLoadCommands;
194 uint32_t _countOfLoadCommands;
195 uint32_t _endOfLoadCommands;
196 uint32_t _startOfRelocations;
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000197 uint32_t _startOfFunctionStarts;
Nick Kledzik21921372014-07-24 23:06:56 +0000198 uint32_t _startOfDataInCode;
Nick Kledzike34182f2013-11-06 21:36:55 +0000199 uint32_t _startOfSymbols;
200 uint32_t _startOfIndirectSymbols;
201 uint32_t _startOfSymbolStrings;
202 uint32_t _endOfSymbolStrings;
203 uint32_t _symbolTableLocalsStartIndex;
204 uint32_t _symbolTableGlobalsStartIndex;
205 uint32_t _symbolTableUndefinesStartIndex;
206 uint32_t _symbolStringPoolSize;
207 uint32_t _symbolTableSize;
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000208 uint32_t _functionStartsSize;
Nick Kledzik21921372014-07-24 23:06:56 +0000209 uint32_t _dataInCodeSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000210 uint32_t _indirectSymbolTableCount;
211 // Used in object file creation only
212 uint32_t _startOfSectionsContent;
213 uint32_t _endOfSectionsContent;
214 // Used in final linked image only
215 uint32_t _startOfLinkEdit;
216 uint32_t _startOfRebaseInfo;
217 uint32_t _endOfRebaseInfo;
218 uint32_t _startOfBindingInfo;
219 uint32_t _endOfBindingInfo;
220 uint32_t _startOfLazyBindingInfo;
221 uint32_t _endOfLazyBindingInfo;
Nick Kledzik141330a2014-09-03 19:52:50 +0000222 uint32_t _startOfExportTrie;
223 uint32_t _endOfExportTrie;
Nick Kledzike34182f2013-11-06 21:36:55 +0000224 uint32_t _endOfLinkEdit;
225 uint64_t _addressOfLinkEdit;
226 SegMap _segInfo;
227 SectionMap _sectInfo;
228 ByteBuffer _rebaseInfo;
229 ByteBuffer _bindingInfo;
230 ByteBuffer _lazyBindingInfo;
231 ByteBuffer _weakBindingInfo;
Nick Kledzik141330a2014-09-03 19:52:50 +0000232 ByteBuffer _exportTrie;
Nick Kledzike34182f2013-11-06 21:36:55 +0000233};
234
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +0000235size_t headerAndLoadCommandsSize(const NormalizedFile &file,
236 bool includeFunctionStarts) {
237 MachOFileLayout layout(file, includeFunctionStarts);
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000238 return layout.headerAndLoadCommandsSize();
Nick Kledzike34182f2013-11-06 21:36:55 +0000239}
240
241StringRef MachOFileLayout::dyldPath() {
242 return "/usr/lib/dyld";
243}
244
245uint32_t MachOFileLayout::pointerAlign(uint32_t value) {
Rui Ueyama489a8062016-01-14 20:53:50 +0000246 return llvm::alignTo(value, _is64 ? 8 : 4);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000247}
Nick Kledzike34182f2013-11-06 21:36:55 +0000248
249
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000250size_t MachOFileLayout::headerAndLoadCommandsSize() const {
251 return _endOfLoadCommands;
252}
Nick Kledzike34182f2013-11-06 21:36:55 +0000253
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +0000254MachOFileLayout::MachOFileLayout(const NormalizedFile &file,
255 bool alwaysIncludeFunctionStarts)
Nick Kledzike34182f2013-11-06 21:36:55 +0000256 : _file(file),
257 _is64(MachOLinkingContext::is64Bit(file.arch)),
258 _swap(!MachOLinkingContext::isHostEndian(file.arch)),
259 _bigEndianArch(MachOLinkingContext::isBigEndian(file.arch)),
260 _seg1addr(INT64_MAX) {
261 _startOfLoadCommands = _is64 ? sizeof(mach_header_64) : sizeof(mach_header);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000262 const size_t segCommandBaseSize =
Nick Kledzike34182f2013-11-06 21:36:55 +0000263 (_is64 ? sizeof(segment_command_64) : sizeof(segment_command));
264 const size_t sectsSize = (_is64 ? sizeof(section_64) : sizeof(section));
265 if (file.fileType == llvm::MachO::MH_OBJECT) {
266 // object files have just one segment load command containing all sections
267 _endOfLoadCommands = _startOfLoadCommands
268 + segCommandBaseSize
269 + file.sections.size() * sectsSize
270 + sizeof(symtab_command);
271 _countOfLoadCommands = 2;
Pete Cooperceee5de2016-02-04 02:16:08 +0000272 if (file.hasMinVersionLoadCommand) {
273 _endOfLoadCommands += sizeof(version_min_command);
274 _countOfLoadCommands++;
275 }
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +0000276 if (!_file.functionStarts.empty() || alwaysIncludeFunctionStarts) {
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000277 _endOfLoadCommands += sizeof(linkedit_data_command);
278 _countOfLoadCommands++;
279 }
Pete Cooper9b28a452016-02-09 02:10:39 +0000280 if (_file.generateDataInCodeLoadCommand) {
Nick Kledzik21921372014-07-24 23:06:56 +0000281 _endOfLoadCommands += sizeof(linkedit_data_command);
282 _countOfLoadCommands++;
283 }
Nick Kledzikb072c362014-11-18 00:30:29 +0000284 // Assign file offsets to each section.
Nick Kledzike34182f2013-11-06 21:36:55 +0000285 _startOfSectionsContent = _endOfLoadCommands;
Nick Kledzike34182f2013-11-06 21:36:55 +0000286 unsigned relocCount = 0;
Nick Kledzikb072c362014-11-18 00:30:29 +0000287 uint64_t offset = _startOfSectionsContent;
Nick Kledzike34182f2013-11-06 21:36:55 +0000288 for (const Section &sect : file.sections) {
Lang Hamesac2adce2015-12-11 23:25:09 +0000289 if (isZeroFillSection(sect.type))
290 _sectInfo[&sect].fileOffset = 0;
291 else {
Rui Ueyama489a8062016-01-14 20:53:50 +0000292 offset = llvm::alignTo(offset, sect.alignment);
Nick Kledzikb072c362014-11-18 00:30:29 +0000293 _sectInfo[&sect].fileOffset = offset;
294 offset += sect.content.size();
Nick Kledzikb072c362014-11-18 00:30:29 +0000295 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000296 relocCount += sect.relocations.size();
297 }
Nick Kledzikb072c362014-11-18 00:30:29 +0000298 _endOfSectionsContent = offset;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000299
Nick Kledzike34182f2013-11-06 21:36:55 +0000300 computeSymbolTableSizes();
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000301 computeFunctionStartsSize();
Nick Kledzik21921372014-07-24 23:06:56 +0000302 computeDataInCodeSize();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000303
Nick Kledzike34182f2013-11-06 21:36:55 +0000304 // Align start of relocations.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000305 _startOfRelocations = pointerAlign(_endOfSectionsContent);
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000306 _startOfFunctionStarts = _startOfRelocations + relocCount * 8;
307 _startOfDataInCode = _startOfFunctionStarts + _functionStartsSize;
Nick Kledzik21921372014-07-24 23:06:56 +0000308 _startOfSymbols = _startOfDataInCode + _dataInCodeSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000309 // Add Indirect symbol table.
310 _startOfIndirectSymbols = _startOfSymbols + _symbolTableSize;
311 // Align start of symbol table and symbol strings.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000312 _startOfSymbolStrings = _startOfIndirectSymbols
Nick Kledzike34182f2013-11-06 21:36:55 +0000313 + pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000314 _endOfSymbolStrings = _startOfSymbolStrings
Nick Kledzike34182f2013-11-06 21:36:55 +0000315 + pointerAlign(_symbolStringPoolSize);
316 _endOfLinkEdit = _endOfSymbolStrings;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000317 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000318 llvm::dbgs() << "MachOFileLayout()\n"
319 << " startOfLoadCommands=" << _startOfLoadCommands << "\n"
320 << " countOfLoadCommands=" << _countOfLoadCommands << "\n"
321 << " endOfLoadCommands=" << _endOfLoadCommands << "\n"
322 << " startOfRelocations=" << _startOfRelocations << "\n"
323 << " startOfSymbols=" << _startOfSymbols << "\n"
324 << " startOfSymbolStrings=" << _startOfSymbolStrings << "\n"
325 << " endOfSymbolStrings=" << _endOfSymbolStrings << "\n"
326 << " startOfSectionsContent=" << _startOfSectionsContent << "\n"
327 << " endOfSectionsContent=" << _endOfSectionsContent << "\n");
328 } else {
329 // Final linked images have one load command per segment.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000330 _endOfLoadCommands = _startOfLoadCommands
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +0000331 + loadCommandsSize(_countOfLoadCommands,
332 alwaysIncludeFunctionStarts);
Nick Kledzike34182f2013-11-06 21:36:55 +0000333
334 // Assign section file offsets.
335 buildFileOffsets();
336 buildLinkEditInfo();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000337
Nick Kledzike34182f2013-11-06 21:36:55 +0000338 // LINKEDIT of final linked images has in order:
339 // rebase info, binding info, lazy binding info, weak binding info,
Nick Kledzik21921372014-07-24 23:06:56 +0000340 // data-in-code, symbol table, indirect symbol table, symbol table strings.
Nick Kledzike34182f2013-11-06 21:36:55 +0000341 _startOfRebaseInfo = _startOfLinkEdit;
342 _endOfRebaseInfo = _startOfRebaseInfo + _rebaseInfo.size();
343 _startOfBindingInfo = _endOfRebaseInfo;
344 _endOfBindingInfo = _startOfBindingInfo + _bindingInfo.size();
345 _startOfLazyBindingInfo = _endOfBindingInfo;
346 _endOfLazyBindingInfo = _startOfLazyBindingInfo + _lazyBindingInfo.size();
Nick Kledzik141330a2014-09-03 19:52:50 +0000347 _startOfExportTrie = _endOfLazyBindingInfo;
348 _endOfExportTrie = _startOfExportTrie + _exportTrie.size();
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000349 _startOfFunctionStarts = _endOfExportTrie;
350 _startOfDataInCode = _startOfFunctionStarts + _functionStartsSize;
Nick Kledzik21921372014-07-24 23:06:56 +0000351 _startOfSymbols = _startOfDataInCode + _dataInCodeSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000352 _startOfIndirectSymbols = _startOfSymbols + _symbolTableSize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000353 _startOfSymbolStrings = _startOfIndirectSymbols
Nick Kledzike34182f2013-11-06 21:36:55 +0000354 + pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000355 _endOfSymbolStrings = _startOfSymbolStrings
Nick Kledzike34182f2013-11-06 21:36:55 +0000356 + pointerAlign(_symbolStringPoolSize);
357 _endOfLinkEdit = _endOfSymbolStrings;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000358 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000359 llvm::dbgs() << "MachOFileLayout()\n"
360 << " startOfLoadCommands=" << _startOfLoadCommands << "\n"
361 << " countOfLoadCommands=" << _countOfLoadCommands << "\n"
362 << " endOfLoadCommands=" << _endOfLoadCommands << "\n"
363 << " startOfLinkEdit=" << _startOfLinkEdit << "\n"
364 << " startOfRebaseInfo=" << _startOfRebaseInfo << "\n"
365 << " endOfRebaseInfo=" << _endOfRebaseInfo << "\n"
366 << " startOfBindingInfo=" << _startOfBindingInfo << "\n"
367 << " endOfBindingInfo=" << _endOfBindingInfo << "\n"
368 << " startOfLazyBindingInfo=" << _startOfLazyBindingInfo << "\n"
369 << " endOfLazyBindingInfo=" << _endOfLazyBindingInfo << "\n"
Nick Kledzik141330a2014-09-03 19:52:50 +0000370 << " startOfExportTrie=" << _startOfExportTrie << "\n"
371 << " endOfExportTrie=" << _endOfExportTrie << "\n"
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000372 << " startOfFunctionStarts=" << _startOfFunctionStarts << "\n"
Nick Kledzik21921372014-07-24 23:06:56 +0000373 << " startOfDataInCode=" << _startOfDataInCode << "\n"
Nick Kledzike34182f2013-11-06 21:36:55 +0000374 << " startOfSymbols=" << _startOfSymbols << "\n"
375 << " startOfSymbolStrings=" << _startOfSymbolStrings << "\n"
376 << " endOfSymbolStrings=" << _endOfSymbolStrings << "\n"
377 << " addressOfLinkEdit=" << _addressOfLinkEdit << "\n");
378 }
379}
380
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +0000381uint32_t MachOFileLayout::loadCommandsSize(uint32_t &count,
382 bool alwaysIncludeFunctionStarts) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000383 uint32_t size = 0;
384 count = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000385
386 const size_t segCommandSize =
Nick Kledzike34182f2013-11-06 21:36:55 +0000387 (_is64 ? sizeof(segment_command_64) : sizeof(segment_command));
388 const size_t sectionSize = (_is64 ? sizeof(section_64) : sizeof(section));
389
390 // Add LC_SEGMENT for each segment.
391 size += _file.segments.size() * segCommandSize;
392 count += _file.segments.size();
393 // Add section record for each section.
394 size += _file.sections.size() * sectionSize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000395
Tim Northover301c4e62014-07-01 08:15:41 +0000396 // If creating a dylib, add LC_ID_DYLIB.
397 if (_file.fileType == llvm::MachO::MH_DYLIB) {
398 size += sizeof(dylib_command) + pointerAlign(_file.installName.size() + 1);
399 ++count;
400 }
401
Nick Kledzike34182f2013-11-06 21:36:55 +0000402 // Add LC_DYLD_INFO
403 size += sizeof(dyld_info_command);
404 ++count;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000405
Nick Kledzike34182f2013-11-06 21:36:55 +0000406 // Add LC_SYMTAB
407 size += sizeof(symtab_command);
408 ++count;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000409
Nick Kledzike34182f2013-11-06 21:36:55 +0000410 // Add LC_DYSYMTAB
411 if (_file.fileType != llvm::MachO::MH_PRELOAD) {
412 size += sizeof(dysymtab_command);
413 ++count;
414 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000415
Pete Cooper354809e2016-02-03 22:28:29 +0000416 // If main executable add LC_LOAD_DYLINKER
Nick Kledzike34182f2013-11-06 21:36:55 +0000417 if (_file.fileType == llvm::MachO::MH_EXECUTE) {
418 size += pointerAlign(sizeof(dylinker_command) + dyldPath().size()+1);
419 ++count;
Pete Cooper354809e2016-02-03 22:28:29 +0000420 }
421
422 // Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_WATCHOS,
423 // LC_VERSION_MIN_TVOS
424 if (_file.hasMinVersionLoadCommand) {
425 size += sizeof(version_min_command);
426 ++count;
427 }
428
Pete Cooper40576fa2016-02-04 02:45:23 +0000429 // Add LC_SOURCE_VERSION
430 size += sizeof(source_version_command);
431 ++count;
432
Pete Cooper354809e2016-02-03 22:28:29 +0000433 // If main executable add LC_MAIN
434 if (_file.fileType == llvm::MachO::MH_EXECUTE) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000435 size += sizeof(entry_point_command);
436 ++count;
437 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000438
Nick Kledzike34182f2013-11-06 21:36:55 +0000439 // Add LC_LOAD_DYLIB for each dependent dylib.
440 for (const DependentDylib &dep : _file.dependentDylibs) {
441 size += sizeof(dylib_command) + pointerAlign(dep.path.size()+1);
442 ++count;
443 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000444
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000445 // Add LC_RPATH
446 for (const StringRef &path : _file.rpaths) {
Lang Hames2ed3bf92015-10-29 16:50:26 +0000447 size += pointerAlign(sizeof(rpath_command) + path.size() + 1);
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000448 ++count;
449 }
450
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000451 // Add LC_FUNCTION_STARTS if needed
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +0000452 if (!_file.functionStarts.empty() || alwaysIncludeFunctionStarts) {
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000453 size += sizeof(linkedit_data_command);
454 ++count;
455 }
456
Pete Cooper9b28a452016-02-09 02:10:39 +0000457 // Add LC_DATA_IN_CODE if requested. Note, we do encode zero length entries.
458 // FIXME: Zero length entries is only to match ld64. Should we change this?
459 if (_file.generateDataInCodeLoadCommand) {
Nick Kledzik54ce29582014-10-28 22:21:10 +0000460 size += sizeof(linkedit_data_command);
461 ++count;
462 }
463
Nick Kledzike34182f2013-11-06 21:36:55 +0000464 return size;
465}
466
467static bool overlaps(const Segment &s1, const Segment &s2) {
468 if (s2.address >= s1.address+s1.size)
469 return false;
470 if (s1.address >= s2.address+s2.size)
471 return false;
472 return true;
473}
474
475static bool overlaps(const Section &s1, const Section &s2) {
476 if (s2.address >= s1.address+s1.content.size())
477 return false;
478 if (s1.address >= s2.address+s2.content.size())
479 return false;
480 return true;
481}
482
483void MachOFileLayout::buildFileOffsets() {
484 // Verify no segments overlap
485 for (const Segment &sg1 : _file.segments) {
486 for (const Segment &sg2 : _file.segments) {
487 if (&sg1 == &sg2)
488 continue;
489 if (overlaps(sg1,sg2)) {
Rafael Espindola372bc702014-06-13 17:20:48 +0000490 _ec = make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000491 return;
492 }
493 }
494 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000495
496 // Verify no sections overlap
Nick Kledzike34182f2013-11-06 21:36:55 +0000497 for (const Section &s1 : _file.sections) {
498 for (const Section &s2 : _file.sections) {
499 if (&s1 == &s2)
500 continue;
501 if (overlaps(s1,s2)) {
Rafael Espindola372bc702014-06-13 17:20:48 +0000502 _ec = make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000503 return;
504 }
505 }
506 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000507
Nick Kledzike34182f2013-11-06 21:36:55 +0000508 // Build side table of extra info about segments and sections.
509 SegExtraInfo t;
510 t.fileOffset = 0;
511 for (const Segment &sg : _file.segments) {
512 _segInfo[&sg] = t;
513 }
514 SectionExtraInfo t2;
515 t2.fileOffset = 0;
516 // Assign sections to segments.
517 for (const Section &s : _file.sections) {
518 _sectInfo[&s] = t2;
Nick Kledzik1bebb282014-09-09 23:52:59 +0000519 bool foundSegment = false;
Nick Kledzike34182f2013-11-06 21:36:55 +0000520 for (const Segment &sg : _file.segments) {
Nick Kledzik1bebb282014-09-09 23:52:59 +0000521 if (sg.name.equals(s.segmentName)) {
522 if ((s.address >= sg.address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000523 && (s.address+s.content.size() <= sg.address+sg.size)) {
Nick Kledzik1bebb282014-09-09 23:52:59 +0000524 _segInfo[&sg].sections.push_back(&s);
525 foundSegment = true;
526 break;
Nick Kledzike34182f2013-11-06 21:36:55 +0000527 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000528 }
529 }
Nick Kledzik1bebb282014-09-09 23:52:59 +0000530 if (!foundSegment) {
531 _ec = make_error_code(llvm::errc::executable_format_error);
532 return;
533 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000534 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000535
Nick Kledzike34182f2013-11-06 21:36:55 +0000536 // Assign file offsets.
537 uint32_t fileOffset = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000538 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000539 llvm::dbgs() << "buildFileOffsets()\n");
540 for (const Segment &sg : _file.segments) {
Tim Northover08d6a7b2014-06-30 09:49:30 +0000541 _segInfo[&sg].fileOffset = fileOffset;
Pete Cooperb8fec3e2016-02-06 00:51:16 +0000542 if ((_seg1addr == INT64_MAX) && sg.init_access)
Nick Kledzike34182f2013-11-06 21:36:55 +0000543 _seg1addr = sg.address;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000544 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000545 llvm::dbgs() << " segment=" << sg.name
546 << ", fileOffset=" << _segInfo[&sg].fileOffset << "\n");
Tim Northover08d6a7b2014-06-30 09:49:30 +0000547
548 uint32_t segFileSize = 0;
Nick Kledzik761d6542014-10-24 22:19:22 +0000549 // A segment that is not zero-fill must use a least one page of disk space.
Pete Cooperb8fec3e2016-02-06 00:51:16 +0000550 if (sg.init_access)
Nick Kledzik761d6542014-10-24 22:19:22 +0000551 segFileSize = _file.pageSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000552 for (const Section *s : _segInfo[&sg].sections) {
Tim Northover08d6a7b2014-06-30 09:49:30 +0000553 uint32_t sectOffset = s->address - sg.address;
554 uint32_t sectFileSize =
Lang Hamesac2adce2015-12-11 23:25:09 +0000555 isZeroFillSection(s->type) ? 0 : s->content.size();
Tim Northover08d6a7b2014-06-30 09:49:30 +0000556 segFileSize = std::max(segFileSize, sectOffset + sectFileSize);
557
558 _sectInfo[s].fileOffset = _segInfo[&sg].fileOffset + sectOffset;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000559 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000560 llvm::dbgs() << " section=" << s->sectionName
561 << ", fileOffset=" << fileOffset << "\n");
562 }
Tim Northover08d6a7b2014-06-30 09:49:30 +0000563
Pete Cooper353652f2016-02-06 00:14:15 +0000564 // round up all segments to page aligned, except __LINKEDIT
565 if (!sg.name.equals("__LINKEDIT")) {
566 _segInfo[&sg].fileSize = llvm::alignTo(segFileSize, _file.pageSize);
567 fileOffset = llvm::alignTo(fileOffset + segFileSize, _file.pageSize);
568 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000569 _addressOfLinkEdit = sg.address + sg.size;
570 }
Tim Northover08d6a7b2014-06-30 09:49:30 +0000571 _startOfLinkEdit = fileOffset;
Nick Kledzike34182f2013-11-06 21:36:55 +0000572}
573
Nick Kledzike34182f2013-11-06 21:36:55 +0000574size_t MachOFileLayout::size() const {
575 return _endOfSymbolStrings;
576}
577
578void MachOFileLayout::writeMachHeader() {
Pete Cooper8563e5a2016-02-04 20:43:43 +0000579 auto cpusubtype = MachOLinkingContext::cpuSubtypeFromArch(_file.arch);
580 // dynamic x86 executables on newer OS version should also set the
581 // CPU_SUBTYPE_LIB64 mask in the CPU subtype.
582 // FIXME: Check that this is a dynamic executable, not a static one.
583 if (_file.fileType == llvm::MachO::MH_EXECUTE &&
584 cpusubtype == CPU_SUBTYPE_X86_64_ALL &&
585 _file.os == MachOLinkingContext::OS::macOSX) {
586 uint32_t version;
587 bool failed = MachOLinkingContext::parsePackedVersion("10.5", version);
588 if (!failed && _file.minOSverson >= version)
589 cpusubtype |= CPU_SUBTYPE_LIB64;
590 }
591
Nick Kledzike34182f2013-11-06 21:36:55 +0000592 mach_header *mh = reinterpret_cast<mach_header*>(_buffer);
593 mh->magic = _is64 ? llvm::MachO::MH_MAGIC_64 : llvm::MachO::MH_MAGIC;
594 mh->cputype = MachOLinkingContext::cpuTypeFromArch(_file.arch);
Pete Cooper8563e5a2016-02-04 20:43:43 +0000595 mh->cpusubtype = cpusubtype;
Nick Kledzike34182f2013-11-06 21:36:55 +0000596 mh->filetype = _file.fileType;
597 mh->ncmds = _countOfLoadCommands;
598 mh->sizeofcmds = _endOfLoadCommands - _startOfLoadCommands;
599 mh->flags = _file.flags;
600 if (_swap)
601 swapStruct(*mh);
602}
603
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000604uint32_t MachOFileLayout::indirectSymbolIndex(const Section &sect,
Nick Kledzike34182f2013-11-06 21:36:55 +0000605 uint32_t &index) {
606 if (sect.indirectSymbols.empty())
607 return 0;
608 uint32_t result = index;
609 index += sect.indirectSymbols.size();
610 return result;
611}
612
613uint32_t MachOFileLayout::indirectSymbolElementSize(const Section &sect) {
614 if (sect.indirectSymbols.empty())
615 return 0;
616 if (sect.type != S_SYMBOL_STUBS)
617 return 0;
618 return sect.content.size() / sect.indirectSymbols.size();
619}
620
Nick Kledzik29f749e2013-11-09 00:07:28 +0000621template <typename T>
Pete Cooper514594b2016-03-31 00:08:16 +0000622llvm::Error MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) {
Nick Kledzik29f749e2013-11-09 00:07:28 +0000623 typename T::command* seg = reinterpret_cast<typename T::command*>(lc);
624 seg->cmd = T::LC;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000625 seg->cmdsize = sizeof(typename T::command)
Nick Kledzik29f749e2013-11-09 00:07:28 +0000626 + _file.sections.size() * sizeof(typename T::section);
Nick Kledzike34182f2013-11-06 21:36:55 +0000627 uint8_t *next = lc + seg->cmdsize;
628 memset(seg->segname, 0, 16);
629 seg->vmaddr = 0;
Nick Kledzikb072c362014-11-18 00:30:29 +0000630 seg->vmsize = _file.sections.back().address
631 + _file.sections.back().content.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000632 seg->fileoff = _endOfLoadCommands;
Lang Hames8c2406b2016-08-10 22:15:09 +0000633 seg->filesize = _sectInfo[&_file.sections.back()].fileOffset +
634 _file.sections.back().content.size() -
635 _sectInfo[&_file.sections.front()].fileOffset;
Nick Kledzike34182f2013-11-06 21:36:55 +0000636 seg->maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
637 seg->initprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
638 seg->nsects = _file.sections.size();
639 seg->flags = 0;
640 if (_swap)
641 swapStruct(*seg);
Nick Kledzik29f749e2013-11-09 00:07:28 +0000642 typename T::section *sout = reinterpret_cast<typename T::section*>
643 (lc+sizeof(typename T::command));
Nick Kledzike34182f2013-11-06 21:36:55 +0000644 uint32_t relOffset = _startOfRelocations;
Nick Kledzike34182f2013-11-06 21:36:55 +0000645 uint32_t indirectSymRunningIndex = 0;
646 for (const Section &sin : _file.sections) {
647 setString16(sin.sectionName, sout->sectname);
648 setString16(sin.segmentName, sout->segname);
649 sout->addr = sin.address;
650 sout->size = sin.content.size();
Nick Kledzikb072c362014-11-18 00:30:29 +0000651 sout->offset = _sectInfo[&sin].fileOffset;
Rui Ueyamaf217ef02015-03-26 02:03:44 +0000652 sout->align = llvm::Log2_32(sin.alignment);
Nick Kledzike34182f2013-11-06 21:36:55 +0000653 sout->reloff = sin.relocations.empty() ? 0 : relOffset;
654 sout->nreloc = sin.relocations.size();
655 sout->flags = sin.type | sin.attributes;
656 sout->reserved1 = indirectSymbolIndex(sin, indirectSymRunningIndex);
657 sout->reserved2 = indirectSymbolElementSize(sin);
658 relOffset += sin.relocations.size() * sizeof(any_relocation_info);
Nick Kledzike34182f2013-11-06 21:36:55 +0000659 if (_swap)
660 swapStruct(*sout);
661 ++sout;
662 }
663 lc = next;
Mehdi Aminic1edf562016-11-11 04:29:25 +0000664 return llvm::Error::success();
Nick Kledzike34182f2013-11-06 21:36:55 +0000665}
666
Nick Kledzik29f749e2013-11-09 00:07:28 +0000667template <typename T>
Pete Cooper514594b2016-03-31 00:08:16 +0000668llvm::Error MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000669 uint32_t indirectSymRunningIndex = 0;
670 for (const Segment &seg : _file.segments) {
Pete Cooper353652f2016-02-06 00:14:15 +0000671 // Link edit has no sections and a custom range of address, so handle it
672 // specially.
Nick Kledzike34182f2013-11-06 21:36:55 +0000673 SegExtraInfo &segInfo = _segInfo[&seg];
Pete Cooper353652f2016-02-06 00:14:15 +0000674 if (seg.name.equals("__LINKEDIT")) {
675 size_t linkeditSize = _endOfLinkEdit - _startOfLinkEdit;
676 typename T::command* cmd = reinterpret_cast<typename T::command*>(lc);
677 cmd->cmd = T::LC;
678 cmd->cmdsize = sizeof(typename T::command);
679 uint8_t *next = lc + cmd->cmdsize;
680 setString16("__LINKEDIT", cmd->segname);
681 cmd->vmaddr = _addressOfLinkEdit;
682 cmd->vmsize = llvm::alignTo(linkeditSize, _file.pageSize);
683 cmd->fileoff = _startOfLinkEdit;
684 cmd->filesize = linkeditSize;
Pete Cooperb8fec3e2016-02-06 00:51:16 +0000685 cmd->initprot = seg.init_access;
686 cmd->maxprot = seg.max_access;
Pete Cooper353652f2016-02-06 00:14:15 +0000687 cmd->nsects = 0;
688 cmd->flags = 0;
689 if (_swap)
690 swapStruct(*cmd);
691 lc = next;
692 continue;
693 }
694 // Write segment command with trailing sections.
Nick Kledzik29f749e2013-11-09 00:07:28 +0000695 typename T::command* cmd = reinterpret_cast<typename T::command*>(lc);
696 cmd->cmd = T::LC;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000697 cmd->cmdsize = sizeof(typename T::command)
Nick Kledzik29f749e2013-11-09 00:07:28 +0000698 + segInfo.sections.size() * sizeof(typename T::section);
Nick Kledzike34182f2013-11-06 21:36:55 +0000699 uint8_t *next = lc + cmd->cmdsize;
700 setString16(seg.name, cmd->segname);
701 cmd->vmaddr = seg.address;
702 cmd->vmsize = seg.size;
703 cmd->fileoff = segInfo.fileOffset;
Tim Northover08d6a7b2014-06-30 09:49:30 +0000704 cmd->filesize = segInfo.fileSize;
Pete Cooperb8fec3e2016-02-06 00:51:16 +0000705 cmd->initprot = seg.init_access;
706 cmd->maxprot = seg.max_access;
Nick Kledzike34182f2013-11-06 21:36:55 +0000707 cmd->nsects = segInfo.sections.size();
708 cmd->flags = 0;
709 if (_swap)
710 swapStruct(*cmd);
Nick Kledzik29f749e2013-11-09 00:07:28 +0000711 typename T::section *sect = reinterpret_cast<typename T::section*>
712 (lc+sizeof(typename T::command));
Nick Kledzike34182f2013-11-06 21:36:55 +0000713 for (const Section *section : segInfo.sections) {
714 setString16(section->sectionName, sect->sectname);
715 setString16(section->segmentName, sect->segname);
716 sect->addr = section->address;
717 sect->size = section->content.size();
Lang Hamesac2adce2015-12-11 23:25:09 +0000718 if (isZeroFillSection(section->type))
Nick Kledzikb072c362014-11-18 00:30:29 +0000719 sect->offset = 0;
720 else
721 sect->offset = section->address - seg.address + segInfo.fileOffset;
Rui Ueyamaf217ef02015-03-26 02:03:44 +0000722 sect->align = llvm::Log2_32(section->alignment);
Nick Kledzike34182f2013-11-06 21:36:55 +0000723 sect->reloff = 0;
724 sect->nreloc = 0;
725 sect->flags = section->type | section->attributes;
726 sect->reserved1 = indirectSymbolIndex(*section, indirectSymRunningIndex);
727 sect->reserved2 = indirectSymbolElementSize(*section);
728 if (_swap)
729 swapStruct(*sect);
730 ++sect;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000731 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000732 lc = reinterpret_cast<uint8_t*>(next);
733 }
Mehdi Aminic1edf562016-11-11 04:29:25 +0000734 return llvm::Error::success();
Nick Kledzike34182f2013-11-06 21:36:55 +0000735}
736
Pete Cooperceee5de2016-02-04 02:16:08 +0000737static void writeVersionMinLoadCommand(const NormalizedFile &_file,
738 bool _swap,
739 uint8_t *&lc) {
740 if (!_file.hasMinVersionLoadCommand)
741 return;
742 version_min_command *vm = reinterpret_cast<version_min_command*>(lc);
743 switch (_file.os) {
744 case MachOLinkingContext::OS::unknown:
745 vm->cmd = _file.minOSVersionKind;
746 vm->cmdsize = sizeof(version_min_command);
747 vm->version = _file.minOSverson;
748 vm->sdk = 0;
749 break;
750 case MachOLinkingContext::OS::macOSX:
751 vm->cmd = LC_VERSION_MIN_MACOSX;
752 vm->cmdsize = sizeof(version_min_command);
753 vm->version = _file.minOSverson;
754 vm->sdk = _file.sdkVersion;
755 break;
756 case MachOLinkingContext::OS::iOS:
757 case MachOLinkingContext::OS::iOS_simulator:
758 vm->cmd = LC_VERSION_MIN_IPHONEOS;
759 vm->cmdsize = sizeof(version_min_command);
760 vm->version = _file.minOSverson;
761 vm->sdk = _file.sdkVersion;
762 break;
763 }
764 if (_swap)
765 swapStruct(*vm);
766 lc += sizeof(version_min_command);
767}
768
Pete Cooper514594b2016-03-31 00:08:16 +0000769llvm::Error MachOFileLayout::writeLoadCommands() {
Nick Kledzike34182f2013-11-06 21:36:55 +0000770 uint8_t *lc = &_buffer[_startOfLoadCommands];
771 if (_file.fileType == llvm::MachO::MH_OBJECT) {
772 // Object files have one unnamed segment which holds all sections.
Pete Cooper514594b2016-03-31 00:08:16 +0000773 if (_is64) {
774 if (auto ec = writeSingleSegmentLoadCommand<MachO64Trait>(lc))
Pete Coopere487da12016-03-31 00:35:50 +0000775 return ec;
Pete Cooper514594b2016-03-31 00:08:16 +0000776 } else {
777 if (auto ec = writeSingleSegmentLoadCommand<MachO32Trait>(lc))
Pete Cooperdc59c792016-03-31 00:38:02 +0000778 return ec;
Pete Cooper514594b2016-03-31 00:08:16 +0000779 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000780 // Add LC_SYMTAB with symbol table info
781 symtab_command* st = reinterpret_cast<symtab_command*>(lc);
782 st->cmd = LC_SYMTAB;
783 st->cmdsize = sizeof(symtab_command);
784 st->symoff = _startOfSymbols;
Lang Hames436f7d62016-07-27 22:55:30 +0000785 st->nsyms = _file.stabsSymbols.size() + _file.localSymbols.size() +
786 _file.globalSymbols.size() + _file.undefinedSymbols.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000787 st->stroff = _startOfSymbolStrings;
788 st->strsize = _endOfSymbolStrings - _startOfSymbolStrings;
789 if (_swap)
790 swapStruct(*st);
Nick Kledzik21921372014-07-24 23:06:56 +0000791 lc += sizeof(symtab_command);
Pete Cooperceee5de2016-02-04 02:16:08 +0000792
793 // Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
794 // LC_VERSION_MIN_WATCHOS, LC_VERSION_MIN_TVOS
795 writeVersionMinLoadCommand(_file, _swap, lc);
796
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000797 // Add LC_FUNCTION_STARTS if needed.
798 if (_functionStartsSize != 0) {
799 linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
800 dl->cmd = LC_FUNCTION_STARTS;
801 dl->cmdsize = sizeof(linkedit_data_command);
802 dl->dataoff = _startOfFunctionStarts;
803 dl->datasize = _functionStartsSize;
804 if (_swap)
805 swapStruct(*dl);
806 lc += sizeof(linkedit_data_command);
807 }
808
Pete Cooper9b28a452016-02-09 02:10:39 +0000809 // Add LC_DATA_IN_CODE if requested.
810 if (_file.generateDataInCodeLoadCommand) {
Nick Kledzik21921372014-07-24 23:06:56 +0000811 linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
812 dl->cmd = LC_DATA_IN_CODE;
813 dl->cmdsize = sizeof(linkedit_data_command);
814 dl->dataoff = _startOfDataInCode;
815 dl->datasize = _dataInCodeSize;
816 if (_swap)
817 swapStruct(*dl);
818 lc += sizeof(linkedit_data_command);
819 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000820 } else {
821 // Final linked images have sections under segments.
Pete Cooper514594b2016-03-31 00:08:16 +0000822 if (_is64) {
823 if (auto ec = writeSegmentLoadCommands<MachO64Trait>(lc))
Pete Cooperdc59c792016-03-31 00:38:02 +0000824 return ec;
Pete Cooper514594b2016-03-31 00:08:16 +0000825 } else {
826 if (auto ec = writeSegmentLoadCommands<MachO32Trait>(lc))
Pete Cooperdc59c792016-03-31 00:38:02 +0000827 return ec;
Pete Cooper514594b2016-03-31 00:08:16 +0000828 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000829
Tim Northover301c4e62014-07-01 08:15:41 +0000830 // Add LC_ID_DYLIB command for dynamic libraries.
831 if (_file.fileType == llvm::MachO::MH_DYLIB) {
832 dylib_command *dc = reinterpret_cast<dylib_command*>(lc);
833 StringRef path = _file.installName;
834 uint32_t size = sizeof(dylib_command) + pointerAlign(path.size() + 1);
835 dc->cmd = LC_ID_DYLIB;
836 dc->cmdsize = size;
837 dc->dylib.name = sizeof(dylib_command); // offset
Jean-Daniel Dupasedefccc2014-12-20 09:22:56 +0000838 // needs to be some constant value different than the one in LC_LOAD_DYLIB
839 dc->dylib.timestamp = 1;
Nick Kledzik5b9e48b2014-11-19 02:21:53 +0000840 dc->dylib.current_version = _file.currentVersion;
841 dc->dylib.compatibility_version = _file.compatVersion;
Tim Northover301c4e62014-07-01 08:15:41 +0000842 if (_swap)
843 swapStruct(*dc);
844 memcpy(lc + sizeof(dylib_command), path.begin(), path.size());
845 lc[sizeof(dylib_command) + path.size()] = '\0';
846 lc += size;
847 }
848
Nick Kledzike34182f2013-11-06 21:36:55 +0000849 // Add LC_DYLD_INFO_ONLY.
850 dyld_info_command* di = reinterpret_cast<dyld_info_command*>(lc);
851 di->cmd = LC_DYLD_INFO_ONLY;
852 di->cmdsize = sizeof(dyld_info_command);
853 di->rebase_off = _rebaseInfo.size() ? _startOfRebaseInfo : 0;
854 di->rebase_size = _rebaseInfo.size();
855 di->bind_off = _bindingInfo.size() ? _startOfBindingInfo : 0;
856 di->bind_size = _bindingInfo.size();
857 di->weak_bind_off = 0;
858 di->weak_bind_size = 0;
859 di->lazy_bind_off = _lazyBindingInfo.size() ? _startOfLazyBindingInfo : 0;
860 di->lazy_bind_size = _lazyBindingInfo.size();
Nick Kledzik141330a2014-09-03 19:52:50 +0000861 di->export_off = _exportTrie.size() ? _startOfExportTrie : 0;
862 di->export_size = _exportTrie.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000863 if (_swap)
864 swapStruct(*di);
865 lc += sizeof(dyld_info_command);
866
867 // Add LC_SYMTAB with symbol table info.
868 symtab_command* st = reinterpret_cast<symtab_command*>(lc);
869 st->cmd = LC_SYMTAB;
870 st->cmdsize = sizeof(symtab_command);
871 st->symoff = _startOfSymbols;
Lang Hames436f7d62016-07-27 22:55:30 +0000872 st->nsyms = _file.stabsSymbols.size() + _file.localSymbols.size() +
873 _file.globalSymbols.size() + _file.undefinedSymbols.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000874 st->stroff = _startOfSymbolStrings;
875 st->strsize = _endOfSymbolStrings - _startOfSymbolStrings;
876 if (_swap)
877 swapStruct(*st);
878 lc += sizeof(symtab_command);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000879
Nick Kledzike34182f2013-11-06 21:36:55 +0000880 // Add LC_DYSYMTAB
881 if (_file.fileType != llvm::MachO::MH_PRELOAD) {
882 dysymtab_command* dst = reinterpret_cast<dysymtab_command*>(lc);
883 dst->cmd = LC_DYSYMTAB;
884 dst->cmdsize = sizeof(dysymtab_command);
885 dst->ilocalsym = _symbolTableLocalsStartIndex;
Lang Hames436f7d62016-07-27 22:55:30 +0000886 dst->nlocalsym = _file.stabsSymbols.size() +
887 _file.localSymbols.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000888 dst->iextdefsym = _symbolTableGlobalsStartIndex;
889 dst->nextdefsym = _file.globalSymbols.size();
890 dst->iundefsym = _symbolTableUndefinesStartIndex;
891 dst->nundefsym = _file.undefinedSymbols.size();
892 dst->tocoff = 0;
893 dst->ntoc = 0;
894 dst->modtaboff = 0;
895 dst->nmodtab = 0;
896 dst->extrefsymoff = 0;
897 dst->nextrefsyms = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000898 dst->indirectsymoff = _startOfIndirectSymbols;
Nick Kledzike34182f2013-11-06 21:36:55 +0000899 dst->nindirectsyms = _indirectSymbolTableCount;
900 dst->extreloff = 0;
901 dst->nextrel = 0;
902 dst->locreloff = 0;
903 dst->nlocrel = 0;
904 if (_swap)
905 swapStruct(*dst);
906 lc += sizeof(dysymtab_command);
907 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000908
Pete Cooper354809e2016-02-03 22:28:29 +0000909 // If main executable, add LC_LOAD_DYLINKER
Nick Kledzike34182f2013-11-06 21:36:55 +0000910 if (_file.fileType == llvm::MachO::MH_EXECUTE) {
911 // Build LC_LOAD_DYLINKER load command.
912 uint32_t size=pointerAlign(sizeof(dylinker_command)+dyldPath().size()+1);
913 dylinker_command* dl = reinterpret_cast<dylinker_command*>(lc);
914 dl->cmd = LC_LOAD_DYLINKER;
915 dl->cmdsize = size;
916 dl->name = sizeof(dylinker_command); // offset
917 if (_swap)
918 swapStruct(*dl);
919 memcpy(lc+sizeof(dylinker_command), dyldPath().data(), dyldPath().size());
920 lc[sizeof(dylinker_command)+dyldPath().size()] = '\0';
921 lc += size;
Pete Cooper354809e2016-02-03 22:28:29 +0000922 }
923
924 // Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_WATCHOS,
925 // LC_VERSION_MIN_TVOS
Pete Cooperceee5de2016-02-04 02:16:08 +0000926 writeVersionMinLoadCommand(_file, _swap, lc);
Pete Cooper354809e2016-02-03 22:28:29 +0000927
Pete Cooper40576fa2016-02-04 02:45:23 +0000928 // Add LC_SOURCE_VERSION
929 {
Pete Cooperb565bdf2016-03-23 22:00:09 +0000930 // Note, using a temporary here to appease UB as we may not be aligned
931 // enough for a struct containing a uint64_t when emitting a 32-bit binary
932 source_version_command sv;
933 sv.cmd = LC_SOURCE_VERSION;
934 sv.cmdsize = sizeof(source_version_command);
935 sv.version = _file.sourceVersion;
Pete Cooper40576fa2016-02-04 02:45:23 +0000936 if (_swap)
Pete Cooperb565bdf2016-03-23 22:00:09 +0000937 swapStruct(sv);
938 memcpy(lc, &sv, sizeof(source_version_command));
Pete Cooper40576fa2016-02-04 02:45:23 +0000939 lc += sizeof(source_version_command);
940 }
941
Pete Cooper354809e2016-02-03 22:28:29 +0000942 // If main executable, add LC_MAIN.
943 if (_file.fileType == llvm::MachO::MH_EXECUTE) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000944 // Build LC_MAIN load command.
Pete Cooper07601d32016-03-24 01:05:17 +0000945 // Note, using a temporary here to appease UB as we may not be aligned
946 // enough for a struct containing a uint64_t when emitting a 32-bit binary
947 entry_point_command ep;
948 ep.cmd = LC_MAIN;
949 ep.cmdsize = sizeof(entry_point_command);
950 ep.entryoff = _file.entryAddress - _seg1addr;
951 ep.stacksize = _file.stackSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000952 if (_swap)
Pete Cooper07601d32016-03-24 01:05:17 +0000953 swapStruct(ep);
954 memcpy(lc, &ep, sizeof(entry_point_command));
Nick Kledzike34182f2013-11-06 21:36:55 +0000955 lc += sizeof(entry_point_command);
956 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000957
Nick Kledzike34182f2013-11-06 21:36:55 +0000958 // Add LC_LOAD_DYLIB commands
959 for (const DependentDylib &dep : _file.dependentDylibs) {
960 dylib_command* dc = reinterpret_cast<dylib_command*>(lc);
961 uint32_t size = sizeof(dylib_command) + pointerAlign(dep.path.size()+1);
Nick Kledzik51720672014-10-16 19:31:28 +0000962 dc->cmd = dep.kind;
Nick Kledzike34182f2013-11-06 21:36:55 +0000963 dc->cmdsize = size;
964 dc->dylib.name = sizeof(dylib_command); // offset
Jean-Daniel Dupasedefccc2014-12-20 09:22:56 +0000965 // needs to be some constant value different than the one in LC_ID_DYLIB
Nick Kledzik5b9e48b2014-11-19 02:21:53 +0000966 dc->dylib.timestamp = 2;
967 dc->dylib.current_version = dep.currentVersion;
968 dc->dylib.compatibility_version = dep.compatVersion;
Nick Kledzike34182f2013-11-06 21:36:55 +0000969 if (_swap)
970 swapStruct(*dc);
971 memcpy(lc+sizeof(dylib_command), dep.path.begin(), dep.path.size());
972 lc[sizeof(dylib_command)+dep.path.size()] = '\0';
973 lc += size;
974 }
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000975
976 // Add LC_RPATH
977 for (const StringRef &path : _file.rpaths) {
978 rpath_command *rpc = reinterpret_cast<rpath_command *>(lc);
Lang Hames2ed3bf92015-10-29 16:50:26 +0000979 uint32_t size = pointerAlign(sizeof(rpath_command) + path.size() + 1);
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000980 rpc->cmd = LC_RPATH;
981 rpc->cmdsize = size;
982 rpc->path = sizeof(rpath_command); // offset
983 if (_swap)
984 swapStruct(*rpc);
985 memcpy(lc+sizeof(rpath_command), path.begin(), path.size());
986 lc[sizeof(rpath_command)+path.size()] = '\0';
987 lc += size;
988 }
989
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000990 // Add LC_FUNCTION_STARTS if needed.
991 if (_functionStartsSize != 0) {
992 linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
993 dl->cmd = LC_FUNCTION_STARTS;
994 dl->cmdsize = sizeof(linkedit_data_command);
995 dl->dataoff = _startOfFunctionStarts;
996 dl->datasize = _functionStartsSize;
997 if (_swap)
998 swapStruct(*dl);
999 lc += sizeof(linkedit_data_command);
1000 }
1001
Pete Cooper9b28a452016-02-09 02:10:39 +00001002 // Add LC_DATA_IN_CODE if requested.
1003 if (_file.generateDataInCodeLoadCommand) {
Nick Kledzik54ce29582014-10-28 22:21:10 +00001004 linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
1005 dl->cmd = LC_DATA_IN_CODE;
1006 dl->cmdsize = sizeof(linkedit_data_command);
1007 dl->dataoff = _startOfDataInCode;
1008 dl->datasize = _dataInCodeSize;
1009 if (_swap)
1010 swapStruct(*dl);
1011 lc += sizeof(linkedit_data_command);
1012 }
Nick Kledzike34182f2013-11-06 21:36:55 +00001013 }
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +00001014 assert(lc == &_buffer[_endOfLoadCommands]);
Mehdi Aminic1edf562016-11-11 04:29:25 +00001015 return llvm::Error::success();
Nick Kledzike34182f2013-11-06 21:36:55 +00001016}
1017
Nick Kledzike34182f2013-11-06 21:36:55 +00001018void MachOFileLayout::writeSectionContent() {
1019 for (const Section &s : _file.sections) {
1020 // Copy all section content to output buffer.
Lang Hamesac2adce2015-12-11 23:25:09 +00001021 if (isZeroFillSection(s.type))
Nick Kledzik61fdef62014-05-15 20:59:23 +00001022 continue;
Nick Kledzik1bebb282014-09-09 23:52:59 +00001023 if (s.content.empty())
1024 continue;
Nick Kledzike34182f2013-11-06 21:36:55 +00001025 uint32_t offset = _sectInfo[&s].fileOffset;
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +00001026 assert(offset >= _endOfLoadCommands);
Nick Kledzike34182f2013-11-06 21:36:55 +00001027 uint8_t *p = &_buffer[offset];
1028 memcpy(p, &s.content[0], s.content.size());
1029 p += s.content.size();
1030 }
1031}
1032
1033void MachOFileLayout::writeRelocations() {
1034 uint32_t relOffset = _startOfRelocations;
1035 for (Section sect : _file.sections) {
1036 for (Relocation r : sect.relocations) {
1037 any_relocation_info* rb = reinterpret_cast<any_relocation_info*>(
1038 &_buffer[relOffset]);
1039 *rb = packRelocation(r, _swap, _bigEndianArch);
1040 relOffset += sizeof(any_relocation_info);
1041 }
1042 }
1043}
1044
Nick Kledzike34182f2013-11-06 21:36:55 +00001045void MachOFileLayout::appendSymbols(const std::vector<Symbol> &symbols,
1046 uint32_t &symOffset, uint32_t &strOffset) {
1047 for (const Symbol &sym : symbols) {
1048 if (_is64) {
1049 nlist_64* nb = reinterpret_cast<nlist_64*>(&_buffer[symOffset]);
1050 nb->n_strx = strOffset - _startOfSymbolStrings;
1051 nb->n_type = sym.type | sym.scope;
1052 nb->n_sect = sym.sect;
1053 nb->n_desc = sym.desc;
1054 nb->n_value = sym.value;
1055 if (_swap)
1056 swapStruct(*nb);
1057 symOffset += sizeof(nlist_64);
1058 } else {
1059 nlist* nb = reinterpret_cast<nlist*>(&_buffer[symOffset]);
1060 nb->n_strx = strOffset - _startOfSymbolStrings;
1061 nb->n_type = sym.type | sym.scope;
1062 nb->n_sect = sym.sect;
1063 nb->n_desc = sym.desc;
1064 nb->n_value = sym.value;
1065 if (_swap)
1066 swapStruct(*nb);
1067 symOffset += sizeof(nlist);
1068 }
1069 memcpy(&_buffer[strOffset], sym.name.begin(), sym.name.size());
1070 strOffset += sym.name.size();
1071 _buffer[strOffset++] ='\0'; // Strings in table have nul terminator.
1072 }
1073}
1074
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001075void MachOFileLayout::writeFunctionStartsInfo() {
Pete Cooper8e1b9a12016-03-22 22:51:03 +00001076 if (!_functionStartsSize)
1077 return;
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001078 memcpy(&_buffer[_startOfFunctionStarts], _file.functionStarts.data(),
1079 _functionStartsSize);
1080}
1081
Nick Kledzik21921372014-07-24 23:06:56 +00001082void MachOFileLayout::writeDataInCodeInfo() {
1083 uint32_t offset = _startOfDataInCode;
1084 for (const DataInCode &entry : _file.dataInCode) {
1085 data_in_code_entry *dst = reinterpret_cast<data_in_code_entry*>(
1086 &_buffer[offset]);
1087 dst->offset = entry.offset;
1088 dst->length = entry.length;
1089 dst->kind = entry.kind;
1090 if (_swap)
1091 swapStruct(*dst);
1092 offset += sizeof(data_in_code_entry);
1093 }
1094}
1095
Nick Kledzike34182f2013-11-06 21:36:55 +00001096void MachOFileLayout::writeSymbolTable() {
1097 // Write symbol table and symbol strings in parallel.
1098 uint32_t symOffset = _startOfSymbols;
1099 uint32_t strOffset = _startOfSymbolStrings;
Pete Cooper5559b242016-08-08 23:20:04 +00001100 // Reserve n_strx offset of zero to mean no name.
1101 _buffer[strOffset++] = ' ';
1102 _buffer[strOffset++] = '\0';
Lang Hames436f7d62016-07-27 22:55:30 +00001103 appendSymbols(_file.stabsSymbols, symOffset, strOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +00001104 appendSymbols(_file.localSymbols, symOffset, strOffset);
1105 appendSymbols(_file.globalSymbols, symOffset, strOffset);
1106 appendSymbols(_file.undefinedSymbols, symOffset, strOffset);
1107 // Write indirect symbol table array.
1108 uint32_t *indirects = reinterpret_cast<uint32_t*>
1109 (&_buffer[_startOfIndirectSymbols]);
1110 if (_file.fileType == llvm::MachO::MH_OBJECT) {
1111 // Object files have sections in same order as input normalized file.
1112 for (const Section &section : _file.sections) {
1113 for (uint32_t index : section.indirectSymbols) {
1114 if (_swap)
Artyom Skrobov17587fb2014-06-14 12:40:04 +00001115 *indirects++ = llvm::sys::getSwappedBytes(index);
Nick Kledzike34182f2013-11-06 21:36:55 +00001116 else
1117 *indirects++ = index;
1118 }
1119 }
1120 } else {
1121 // Final linked images must sort sections from normalized file.
1122 for (const Segment &seg : _file.segments) {
1123 SegExtraInfo &segInfo = _segInfo[&seg];
1124 for (const Section *section : segInfo.sections) {
1125 for (uint32_t index : section->indirectSymbols) {
1126 if (_swap)
Artyom Skrobov17587fb2014-06-14 12:40:04 +00001127 *indirects++ = llvm::sys::getSwappedBytes(index);
Nick Kledzike34182f2013-11-06 21:36:55 +00001128 else
1129 *indirects++ = index;
1130 }
1131 }
1132 }
1133 }
1134}
1135
1136void MachOFileLayout::writeRebaseInfo() {
1137 memcpy(&_buffer[_startOfRebaseInfo], _rebaseInfo.bytes(), _rebaseInfo.size());
1138}
1139
1140void MachOFileLayout::writeBindingInfo() {
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001141 memcpy(&_buffer[_startOfBindingInfo],
Nick Kledzike34182f2013-11-06 21:36:55 +00001142 _bindingInfo.bytes(), _bindingInfo.size());
1143}
1144
1145void MachOFileLayout::writeLazyBindingInfo() {
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001146 memcpy(&_buffer[_startOfLazyBindingInfo],
Nick Kledzike34182f2013-11-06 21:36:55 +00001147 _lazyBindingInfo.bytes(), _lazyBindingInfo.size());
1148}
1149
Nick Kledzik141330a2014-09-03 19:52:50 +00001150void MachOFileLayout::writeExportInfo() {
1151 memcpy(&_buffer[_startOfExportTrie], _exportTrie.bytes(), _exportTrie.size());
1152}
1153
Nick Kledzike34182f2013-11-06 21:36:55 +00001154void MachOFileLayout::buildLinkEditInfo() {
1155 buildRebaseInfo();
1156 buildBindInfo();
1157 buildLazyBindInfo();
Nick Kledzik141330a2014-09-03 19:52:50 +00001158 buildExportTrie();
Nick Kledzike34182f2013-11-06 21:36:55 +00001159 computeSymbolTableSizes();
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001160 computeFunctionStartsSize();
Nick Kledzik21921372014-07-24 23:06:56 +00001161 computeDataInCodeSize();
Nick Kledzike34182f2013-11-06 21:36:55 +00001162}
1163
1164void MachOFileLayout::buildSectionRelocations() {
1165
1166}
1167
1168void MachOFileLayout::buildRebaseInfo() {
1169 // TODO: compress rebasing info.
1170 for (const RebaseLocation& entry : _file.rebasingInfo) {
1171 _rebaseInfo.append_byte(REBASE_OPCODE_SET_TYPE_IMM | entry.kind);
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001172 _rebaseInfo.append_byte(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
Nick Kledzike34182f2013-11-06 21:36:55 +00001173 | entry.segIndex);
1174 _rebaseInfo.append_uleb128(entry.segOffset);
1175 _rebaseInfo.append_uleb128(REBASE_OPCODE_DO_REBASE_IMM_TIMES | 1);
1176 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001177 _rebaseInfo.append_byte(REBASE_OPCODE_DONE);
Nick Kledzike34182f2013-11-06 21:36:55 +00001178 _rebaseInfo.align(_is64 ? 8 : 4);
1179}
1180
1181void MachOFileLayout::buildBindInfo() {
1182 // TODO: compress bind info.
Nick Kledzikf373c772014-11-11 01:31:18 +00001183 uint64_t lastAddend = 0;
Pete Cooper21f475e2016-08-11 20:37:02 +00001184 int lastOrdinal = 0x80000000;
1185 StringRef lastSymbolName;
1186 BindType lastType = (BindType)0;
1187 Hex32 lastSegOffset = ~0U;
1188 uint8_t lastSegIndex = (uint8_t)~0U;
Nick Kledzike34182f2013-11-06 21:36:55 +00001189 for (const BindLocation& entry : _file.bindingInfo) {
Pete Cooper21f475e2016-08-11 20:37:02 +00001190 if (entry.ordinal != lastOrdinal) {
1191 if (entry.ordinal <= 0)
1192 _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
1193 (entry.ordinal & BIND_IMMEDIATE_MASK));
1194 else if (entry.ordinal <= BIND_IMMEDIATE_MASK)
1195 _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |
1196 entry.ordinal);
1197 else {
1198 _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
1199 _bindingInfo.append_uleb128(entry.ordinal);
1200 }
1201 lastOrdinal = entry.ordinal;
1202 }
1203
1204 if (lastSymbolName != entry.symbolName) {
1205 _bindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM);
1206 _bindingInfo.append_string(entry.symbolName);
1207 lastSymbolName = entry.symbolName;
1208 }
1209
1210 if (lastType != entry.kind) {
1211 _bindingInfo.append_byte(BIND_OPCODE_SET_TYPE_IMM | entry.kind);
1212 lastType = entry.kind;
1213 }
1214
1215 if (lastSegIndex != entry.segIndex || lastSegOffset != entry.segOffset) {
1216 _bindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
1217 | entry.segIndex);
1218 _bindingInfo.append_uleb128(entry.segOffset);
1219 lastSegIndex = entry.segIndex;
1220 lastSegOffset = entry.segOffset;
1221 }
Nick Kledzikf373c772014-11-11 01:31:18 +00001222 if (entry.addend != lastAddend) {
Nick Kledzike34182f2013-11-06 21:36:55 +00001223 _bindingInfo.append_byte(BIND_OPCODE_SET_ADDEND_SLEB);
1224 _bindingInfo.append_sleb128(entry.addend);
Nick Kledzikf373c772014-11-11 01:31:18 +00001225 lastAddend = entry.addend;
Nick Kledzike34182f2013-11-06 21:36:55 +00001226 }
1227 _bindingInfo.append_byte(BIND_OPCODE_DO_BIND);
1228 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001229 _bindingInfo.append_byte(BIND_OPCODE_DONE);
Nick Kledzike34182f2013-11-06 21:36:55 +00001230 _bindingInfo.align(_is64 ? 8 : 4);
1231}
1232
1233void MachOFileLayout::buildLazyBindInfo() {
1234 for (const BindLocation& entry : _file.lazyBindingInfo) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001235 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
Nick Kledzike34182f2013-11-06 21:36:55 +00001236 | entry.segIndex);
Pete Cooper1ed8f1f2016-08-11 20:59:27 +00001237 _lazyBindingInfo.append_uleb128(entry.segOffset);
1238 if (entry.ordinal <= 0)
Lang Hames5c692002015-09-28 20:25:14 +00001239 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
Pete Cooper1ed8f1f2016-08-11 20:59:27 +00001240 (entry.ordinal & BIND_IMMEDIATE_MASK));
1241 else if (entry.ordinal <= BIND_IMMEDIATE_MASK)
1242 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |
1243 entry.ordinal);
1244 else {
1245 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
1246 _lazyBindingInfo.append_uleb128(entry.ordinal);
1247 }
1248 // FIXME: We need to | the opcode here with flags.
Nick Kledzike34182f2013-11-06 21:36:55 +00001249 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM);
1250 _lazyBindingInfo.append_string(entry.symbolName);
1251 _lazyBindingInfo.append_byte(BIND_OPCODE_DO_BIND);
Nick Kledzikf373c772014-11-11 01:31:18 +00001252 _lazyBindingInfo.append_byte(BIND_OPCODE_DONE);
Nick Kledzike34182f2013-11-06 21:36:55 +00001253 }
Nick Kledzike34182f2013-11-06 21:36:55 +00001254 _lazyBindingInfo.align(_is64 ? 8 : 4);
1255}
1256
Pete Coopere420dd42016-01-25 21:50:54 +00001257void TrieNode::addSymbol(const Export& entry,
1258 BumpPtrAllocator &allocator,
1259 std::vector<TrieNode*> &allNodes) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001260 StringRef partialStr = entry.name.drop_front(_cummulativeString.size());
1261 for (TrieEdge &edge : _children) {
1262 StringRef edgeStr = edge._subString;
1263 if (partialStr.startswith(edgeStr)) {
1264 // Already have matching edge, go down that path.
1265 edge._child->addSymbol(entry, allocator, allNodes);
1266 return;
1267 }
1268 // See if string has commmon prefix with existing edge.
1269 for (int n=edgeStr.size()-1; n > 0; --n) {
1270 if (partialStr.substr(0, n).equals(edgeStr.substr(0, n))) {
1271 // Splice in new node: was A -> C, now A -> B -> C
1272 StringRef bNodeStr = edge._child->_cummulativeString;
1273 bNodeStr = bNodeStr.drop_back(edgeStr.size()-n).copy(allocator);
Eugene Zelenko41547942015-11-10 22:37:38 +00001274 auto *bNode = new (allocator) TrieNode(bNodeStr);
Nick Kledzik141330a2014-09-03 19:52:50 +00001275 allNodes.push_back(bNode);
1276 TrieNode* cNode = edge._child;
1277 StringRef abEdgeStr = edgeStr.substr(0,n).copy(allocator);
1278 StringRef bcEdgeStr = edgeStr.substr(n).copy(allocator);
1279 DEBUG_WITH_TYPE("trie-builder", llvm::dbgs()
1280 << "splice in TrieNode('" << bNodeStr
1281 << "') between edge '"
1282 << abEdgeStr << "' and edge='"
1283 << bcEdgeStr<< "'\n");
1284 TrieEdge& abEdge = edge;
1285 abEdge._subString = abEdgeStr;
1286 abEdge._child = bNode;
Eugene Zelenko41547942015-11-10 22:37:38 +00001287 auto *bcEdge = new (allocator) TrieEdge(bcEdgeStr, cNode);
Pete Coopere420dd42016-01-25 21:50:54 +00001288 bNode->_children.insert(bNode->_children.end(), bcEdge);
Nick Kledzik141330a2014-09-03 19:52:50 +00001289 bNode->addSymbol(entry, allocator, allNodes);
1290 return;
1291 }
1292 }
1293 }
1294 if (entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
1295 assert(entry.otherOffset != 0);
1296 }
1297 if (entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
1298 assert(entry.otherOffset != 0);
1299 }
1300 // No commonality with any existing child, make a new edge.
Eugene Zelenko41547942015-11-10 22:37:38 +00001301 auto *newNode = new (allocator) TrieNode(entry.name.copy(allocator));
1302 auto *newEdge = new (allocator) TrieEdge(partialStr, newNode);
Pete Coopere420dd42016-01-25 21:50:54 +00001303 _children.insert(_children.end(), newEdge);
Nick Kledzik141330a2014-09-03 19:52:50 +00001304 DEBUG_WITH_TYPE("trie-builder", llvm::dbgs()
1305 << "new TrieNode('" << entry.name << "') with edge '"
1306 << partialStr << "' from node='"
1307 << _cummulativeString << "'\n");
1308 newNode->_address = entry.offset;
1309 newNode->_flags = entry.flags | entry.kind;
1310 newNode->_other = entry.otherOffset;
1311 if ((entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT) && !entry.otherName.empty())
1312 newNode->_importedName = entry.otherName.copy(allocator);
1313 newNode->_hasExportInfo = true;
1314 allNodes.push_back(newNode);
1315}
1316
Pete Cooperd0de3682016-08-05 21:37:12 +00001317void TrieNode::addOrderedNodes(const Export& entry,
1318 std::vector<TrieNode*> &orderedNodes) {
1319 if (!_ordered) {
1320 orderedNodes.push_back(this);
1321 _ordered = true;
1322 }
1323
1324 StringRef partialStr = entry.name.drop_front(_cummulativeString.size());
1325 for (TrieEdge &edge : _children) {
1326 StringRef edgeStr = edge._subString;
1327 if (partialStr.startswith(edgeStr)) {
1328 // Already have matching edge, go down that path.
1329 edge._child->addOrderedNodes(entry, orderedNodes);
1330 return;
1331 }
1332 }
1333}
1334
Pete Coopere420dd42016-01-25 21:50:54 +00001335bool TrieNode::updateOffset(uint32_t& offset) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001336 uint32_t nodeSize = 1; // Length when no export info
1337 if (_hasExportInfo) {
1338 if (_flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
1339 nodeSize = llvm::getULEB128Size(_flags);
1340 nodeSize += llvm::getULEB128Size(_other); // Other contains ordinal.
1341 nodeSize += _importedName.size();
1342 ++nodeSize; // Trailing zero in imported name.
1343 } else {
1344 nodeSize = llvm::getULEB128Size(_flags) + llvm::getULEB128Size(_address);
1345 if (_flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
1346 nodeSize += llvm::getULEB128Size(_other);
1347 }
1348 // Overall node size so far is uleb128 of export info + actual export info.
1349 nodeSize += llvm::getULEB128Size(nodeSize);
1350 }
1351 // Compute size of all child edges.
1352 ++nodeSize; // Byte for number of chidren.
1353 for (TrieEdge &edge : _children) {
1354 nodeSize += edge._subString.size() + 1 // String length.
1355 + llvm::getULEB128Size(edge._child->_trieOffset); // Offset len.
1356 }
1357 // On input, 'offset' is new prefered location for this node.
1358 bool result = (_trieOffset != offset);
1359 // Store new location in node object for use by parents.
1360 _trieOffset = offset;
1361 // Update offset for next iteration.
1362 offset += nodeSize;
1363 // Return true if _trieOffset was changed.
1364 return result;
1365}
1366
Pete Coopere420dd42016-01-25 21:50:54 +00001367void TrieNode::appendToByteBuffer(ByteBuffer &out) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001368 if (_hasExportInfo) {
1369 if (_flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
1370 if (!_importedName.empty()) {
1371 // nodes with re-export info: size, flags, ordinal, import-name
1372 uint32_t nodeSize = llvm::getULEB128Size(_flags)
1373 + llvm::getULEB128Size(_other)
1374 + _importedName.size() + 1;
1375 assert(nodeSize < 256);
1376 out.append_byte(nodeSize);
1377 out.append_uleb128(_flags);
1378 out.append_uleb128(_other);
1379 out.append_string(_importedName);
1380 } else {
1381 // nodes without re-export info: size, flags, ordinal, empty-string
1382 uint32_t nodeSize = llvm::getULEB128Size(_flags)
1383 + llvm::getULEB128Size(_other) + 1;
1384 assert(nodeSize < 256);
1385 out.append_byte(nodeSize);
1386 out.append_uleb128(_flags);
1387 out.append_uleb128(_other);
1388 out.append_byte(0);
1389 }
1390 } else if ( _flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER ) {
1391 // Nodes with export info: size, flags, address, other
1392 uint32_t nodeSize = llvm::getULEB128Size(_flags)
1393 + llvm::getULEB128Size(_address)
1394 + llvm::getULEB128Size(_other);
1395 assert(nodeSize < 256);
1396 out.append_byte(nodeSize);
1397 out.append_uleb128(_flags);
1398 out.append_uleb128(_address);
1399 out.append_uleb128(_other);
1400 } else {
1401 // Nodes with export info: size, flags, address
1402 uint32_t nodeSize = llvm::getULEB128Size(_flags)
1403 + llvm::getULEB128Size(_address);
1404 assert(nodeSize < 256);
1405 out.append_byte(nodeSize);
1406 out.append_uleb128(_flags);
1407 out.append_uleb128(_address);
1408 }
1409 } else {
1410 // Node with no export info.
1411 uint32_t nodeSize = 0;
1412 out.append_byte(nodeSize);
1413 }
1414 // Add number of children.
1415 assert(_children.size() < 256);
1416 out.append_byte(_children.size());
1417 // Append each child edge substring and node offset.
1418 for (TrieEdge &edge : _children) {
1419 out.append_string(edge._subString);
1420 out.append_uleb128(edge._child->_trieOffset);
1421 }
1422}
1423
1424void MachOFileLayout::buildExportTrie() {
1425 if (_file.exportInfo.empty())
1426 return;
1427
1428 // For all temporary strings and objects used building trie.
1429 BumpPtrAllocator allocator;
1430
1431 // Build trie of all exported symbols.
Eugene Zelenko41547942015-11-10 22:37:38 +00001432 auto *rootNode = new (allocator) TrieNode(StringRef());
Nick Kledzik141330a2014-09-03 19:52:50 +00001433 std::vector<TrieNode*> allNodes;
1434 allNodes.reserve(_file.exportInfo.size()*2);
1435 allNodes.push_back(rootNode);
1436 for (const Export& entry : _file.exportInfo) {
1437 rootNode->addSymbol(entry, allocator, allNodes);
1438 }
1439
Pete Cooperd0de3682016-08-05 21:37:12 +00001440 std::vector<TrieNode*> orderedNodes;
1441 orderedNodes.reserve(allNodes.size());
1442
1443 for (const Export& entry : _file.exportInfo)
1444 rootNode->addOrderedNodes(entry, orderedNodes);
1445
Nick Kledzik141330a2014-09-03 19:52:50 +00001446 // Assign each node in the vector an offset in the trie stream, iterating
1447 // until all uleb128 sizes have stabilized.
1448 bool more;
1449 do {
1450 uint32_t offset = 0;
1451 more = false;
Pete Cooperd0de3682016-08-05 21:37:12 +00001452 for (TrieNode* node : orderedNodes) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001453 if (node->updateOffset(offset))
1454 more = true;
1455 }
1456 } while (more);
1457
1458 // Serialize trie to ByteBuffer.
Pete Cooperd0de3682016-08-05 21:37:12 +00001459 for (TrieNode* node : orderedNodes) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001460 node->appendToByteBuffer(_exportTrie);
1461 }
1462 _exportTrie.align(_is64 ? 8 : 4);
1463}
1464
Nick Kledzike34182f2013-11-06 21:36:55 +00001465void MachOFileLayout::computeSymbolTableSizes() {
1466 // MachO symbol tables have three ranges: locals, globals, and undefines
1467 const size_t nlistSize = (_is64 ? sizeof(nlist_64) : sizeof(nlist));
Lang Hames436f7d62016-07-27 22:55:30 +00001468 _symbolTableSize = nlistSize * (_file.stabsSymbols.size()
1469 + _file.localSymbols.size()
Nick Kledzike34182f2013-11-06 21:36:55 +00001470 + _file.globalSymbols.size()
1471 + _file.undefinedSymbols.size());
Pete Cooper5559b242016-08-08 23:20:04 +00001472 // Always reserve 1-byte for the empty string and 1-byte for its terminator.
1473 _symbolStringPoolSize = 2;
Lang Hames436f7d62016-07-27 22:55:30 +00001474 for (const Symbol &sym : _file.stabsSymbols) {
1475 _symbolStringPoolSize += (sym.name.size()+1);
1476 }
Nick Kledzike34182f2013-11-06 21:36:55 +00001477 for (const Symbol &sym : _file.localSymbols) {
1478 _symbolStringPoolSize += (sym.name.size()+1);
1479 }
1480 for (const Symbol &sym : _file.globalSymbols) {
1481 _symbolStringPoolSize += (sym.name.size()+1);
1482 }
1483 for (const Symbol &sym : _file.undefinedSymbols) {
1484 _symbolStringPoolSize += (sym.name.size()+1);
1485 }
1486 _symbolTableLocalsStartIndex = 0;
Lang Hames436f7d62016-07-27 22:55:30 +00001487 _symbolTableGlobalsStartIndex = _file.stabsSymbols.size() +
1488 _file.localSymbols.size();
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001489 _symbolTableUndefinesStartIndex = _symbolTableGlobalsStartIndex
Nick Kledzike34182f2013-11-06 21:36:55 +00001490 + _file.globalSymbols.size();
1491
1492 _indirectSymbolTableCount = 0;
1493 for (const Section &sect : _file.sections) {
1494 _indirectSymbolTableCount += sect.indirectSymbols.size();
1495 }
1496}
1497
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001498void MachOFileLayout::computeFunctionStartsSize() {
1499 _functionStartsSize = _file.functionStarts.size();
1500}
1501
Nick Kledzik21921372014-07-24 23:06:56 +00001502void MachOFileLayout::computeDataInCodeSize() {
1503 _dataInCodeSize = _file.dataInCode.size() * sizeof(data_in_code_entry);
1504}
Nick Kledzike34182f2013-11-06 21:36:55 +00001505
1506void MachOFileLayout::writeLinkEditContent() {
1507 if (_file.fileType == llvm::MachO::MH_OBJECT) {
1508 writeRelocations();
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001509 writeFunctionStartsInfo();
Nick Kledzik21921372014-07-24 23:06:56 +00001510 writeDataInCodeInfo();
Nick Kledzike34182f2013-11-06 21:36:55 +00001511 writeSymbolTable();
1512 } else {
1513 writeRebaseInfo();
1514 writeBindingInfo();
1515 writeLazyBindingInfo();
1516 // TODO: add weak binding info
Nick Kledzik141330a2014-09-03 19:52:50 +00001517 writeExportInfo();
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001518 writeFunctionStartsInfo();
Nick Kledzik54ce29582014-10-28 22:21:10 +00001519 writeDataInCodeInfo();
Nick Kledzike34182f2013-11-06 21:36:55 +00001520 writeSymbolTable();
1521 }
1522}
1523
Pete Cooperfefbd222016-03-30 23:10:39 +00001524llvm::Error MachOFileLayout::writeBinary(StringRef path) {
Nick Kledzike34182f2013-11-06 21:36:55 +00001525 // Check for pending error from constructor.
1526 if (_ec)
Pete Cooperfefbd222016-03-30 23:10:39 +00001527 return llvm::errorCodeToError(_ec);
Nick Kledzike34182f2013-11-06 21:36:55 +00001528 // Create FileOutputBuffer with calculated size.
Nick Kledzike34182f2013-11-06 21:36:55 +00001529 unsigned flags = 0;
1530 if (_file.fileType != llvm::MachO::MH_OBJECT)
1531 flags = llvm::FileOutputBuffer::F_executable;
Rafael Espindolaf7a57292017-11-08 01:05:52 +00001532 Expected<std::unique_ptr<llvm::FileOutputBuffer>> fobOrErr =
Rafael Espindolabdc8f2f2015-08-13 00:31:46 +00001533 llvm::FileOutputBuffer::create(path, size(), flags);
Rafael Espindolaf7a57292017-11-08 01:05:52 +00001534 if (Error E = fobOrErr.takeError())
1535 return E;
Rafael Espindolabdc8f2f2015-08-13 00:31:46 +00001536 std::unique_ptr<llvm::FileOutputBuffer> &fob = *fobOrErr;
Nick Kledzike34182f2013-11-06 21:36:55 +00001537 // Write content.
1538 _buffer = fob->getBufferStart();
1539 writeMachHeader();
Pete Cooper514594b2016-03-31 00:08:16 +00001540 if (auto ec = writeLoadCommands())
Pete Cooperdc59c792016-03-31 00:38:02 +00001541 return ec;
Nick Kledzike34182f2013-11-06 21:36:55 +00001542 writeSectionContent();
1543 writeLinkEditContent();
Rafael Espindola5f903f32017-11-08 01:50:34 +00001544 if (Error E = fob->commit())
1545 return E;
Nick Kledzike34182f2013-11-06 21:36:55 +00001546
Mehdi Aminic1edf562016-11-11 04:29:25 +00001547 return llvm::Error::success();
Nick Kledzike34182f2013-11-06 21:36:55 +00001548}
1549
Nick Kledzike34182f2013-11-06 21:36:55 +00001550/// Takes in-memory normalized view and writes a mach-o object file.
Pete Cooperfefbd222016-03-30 23:10:39 +00001551llvm::Error writeBinary(const NormalizedFile &file, StringRef path) {
Rui Ueyama7f8ca6e2019-04-17 01:47:16 +00001552 MachOFileLayout layout(file, false);
Nick Kledzike34182f2013-11-06 21:36:55 +00001553 return layout.writeBinary(path);
1554}
1555
Nick Kledzike34182f2013-11-06 21:36:55 +00001556} // namespace normalized
1557} // namespace mach_o
1558} // namespace lld