blob: bac41d2a52bfbd5e124eb9e720acc73e72759345 [file] [log] [blame]
Nick Kledzike34182f2013-11-06 21:36:55 +00001//===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp ---------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10///
Shankar Easwaran3d8de472014-01-27 03:09:26 +000011/// \file For mach-o object files, this implementation converts normalized
Nick Kledzike34182f2013-11-06 21:36:55 +000012/// mach-o in memory to mach-o binary on disk.
13///
Shankar Easwaran3d8de472014-01-27 03:09:26 +000014/// +---------------+
15/// | binary mach-o |
16/// +---------------+
Nick Kledzike34182f2013-11-06 21:36:55 +000017/// ^
18/// |
19/// |
Shankar Easwaran3d8de472014-01-27 03:09:26 +000020/// +------------+
21/// | normalized |
22/// +------------+
Nick Kledzike34182f2013-11-06 21:36:55 +000023
24#include "MachONormalizedFile.h"
25#include "MachONormalizedFileBinaryUtils.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000026#include "lld/Core/Error.h"
27#include "lld/Core/LLVM.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000028#include "llvm/ADT/SmallString.h"
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/StringRef.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000031#include "llvm/ADT/ilist.h"
32#include "llvm/ADT/ilist_node.h"
33#include "llvm/BinaryFormat/MachO.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000034#include "llvm/Support/Casting.h"
35#include "llvm/Support/Debug.h"
Shankar Easwaran2b67fca2014-10-18 05:33:55 +000036#include "llvm/Support/Errc.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000037#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/FileOutputBuffer.h"
Nick Kledzik141330a2014-09-03 19:52:50 +000039#include "llvm/Support/Format.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000040#include "llvm/Support/Host.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000041#include "llvm/Support/MemoryBuffer.h"
42#include "llvm/Support/raw_ostream.h"
Nick Kledzike34182f2013-11-06 21:36:55 +000043#include <functional>
Nick Kledzik07ba5122014-12-02 01:50:44 +000044#include <list>
Nick Kledzike34182f2013-11-06 21:36:55 +000045#include <map>
Rafael Espindola54427cc2014-06-12 17:15:58 +000046#include <system_error>
Nick Kledzike34182f2013-11-06 21:36:55 +000047
48using namespace llvm::MachO;
49
50namespace lld {
51namespace mach_o {
52namespace normalized {
53
Pete Coopere420dd42016-01-25 21:50:54 +000054struct TrieNode; // Forward declaration.
55
56struct TrieEdge : public llvm::ilist_node<TrieEdge> {
57 TrieEdge(StringRef s, TrieNode *node) : _subString(s), _child(node) {}
58
59 StringRef _subString;
60 struct TrieNode *_child;
61};
62
63} // namespace normalized
64} // namespace mach_o
65} // namespace lld
66
67
68namespace llvm {
Duncan P. N. Exon Smith9f710572016-09-03 01:29:36 +000069using lld::mach_o::normalized::TrieEdge;
70template <>
71struct ilist_alloc_traits<TrieEdge> : ilist_noalloc_traits<TrieEdge> {};
Pete Coopere420dd42016-01-25 21:50:54 +000072} // namespace llvm
73
74
75namespace lld {
76namespace mach_o {
77namespace normalized {
78
79struct TrieNode {
80 typedef llvm::ilist<TrieEdge> TrieEdgeList;
81
82 TrieNode(StringRef s)
83 : _cummulativeString(s), _address(0), _flags(0), _other(0),
84 _trieOffset(0), _hasExportInfo(false) {}
85 ~TrieNode() = default;
86
87 void addSymbol(const Export &entry, BumpPtrAllocator &allocator,
88 std::vector<TrieNode *> &allNodes);
Pete Cooperd0de3682016-08-05 21:37:12 +000089
90 void addOrderedNodes(const Export &entry,
91 std::vector<TrieNode *> &allNodes);
Pete Coopere420dd42016-01-25 21:50:54 +000092 bool updateOffset(uint32_t &offset);
93 void appendToByteBuffer(ByteBuffer &out);
94
95private:
96 StringRef _cummulativeString;
97 TrieEdgeList _children;
98 uint64_t _address;
99 uint64_t _flags;
100 uint64_t _other;
101 StringRef _importedName;
102 uint32_t _trieOffset;
103 bool _hasExportInfo;
Pete Cooperd0de3682016-08-05 21:37:12 +0000104 bool _ordered = false;
Pete Coopere420dd42016-01-25 21:50:54 +0000105};
106
Nick Kledzike34182f2013-11-06 21:36:55 +0000107/// Utility class for writing a mach-o binary file given an in-memory
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000108/// normalized file.
Nick Kledzike34182f2013-11-06 21:36:55 +0000109class MachOFileLayout {
110public:
Joey Goulyb275d7f2013-12-23 23:29:50 +0000111 /// All layout computation is done in the constructor.
112 MachOFileLayout(const NormalizedFile &file);
113
Nick Kledzike34182f2013-11-06 21:36:55 +0000114 /// Returns the final file size as computed in the constructor.
115 size_t size() const;
116
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000117 // Returns size of the mach_header and load commands.
118 size_t headerAndLoadCommandsSize() const;
119
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000120 /// Writes the normalized file as a binary mach-o file to the specified
Nick Kledzike34182f2013-11-06 21:36:55 +0000121 /// path. This does not have a stream interface because the generated
122 /// file may need the 'x' bit set.
Pete Cooperfefbd222016-03-30 23:10:39 +0000123 llvm::Error writeBinary(StringRef path);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000124
Nick Kledzike34182f2013-11-06 21:36:55 +0000125private:
126 uint32_t loadCommandsSize(uint32_t &count);
127 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
235size_t headerAndLoadCommandsSize(const NormalizedFile &file) {
236 MachOFileLayout layout(file);
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000237 return layout.headerAndLoadCommandsSize();
Nick Kledzike34182f2013-11-06 21:36:55 +0000238}
239
240StringRef MachOFileLayout::dyldPath() {
241 return "/usr/lib/dyld";
242}
243
244uint32_t MachOFileLayout::pointerAlign(uint32_t value) {
Rui Ueyama489a8062016-01-14 20:53:50 +0000245 return llvm::alignTo(value, _is64 ? 8 : 4);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000246}
Nick Kledzike34182f2013-11-06 21:36:55 +0000247
248
Nick Kledzik2fcbe822014-07-30 00:58:06 +0000249size_t MachOFileLayout::headerAndLoadCommandsSize() const {
250 return _endOfLoadCommands;
251}
Nick Kledzike34182f2013-11-06 21:36:55 +0000252
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000253MachOFileLayout::MachOFileLayout(const NormalizedFile &file)
Nick Kledzike34182f2013-11-06 21:36:55 +0000254 : _file(file),
255 _is64(MachOLinkingContext::is64Bit(file.arch)),
256 _swap(!MachOLinkingContext::isHostEndian(file.arch)),
257 _bigEndianArch(MachOLinkingContext::isBigEndian(file.arch)),
258 _seg1addr(INT64_MAX) {
259 _startOfLoadCommands = _is64 ? sizeof(mach_header_64) : sizeof(mach_header);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000260 const size_t segCommandBaseSize =
Nick Kledzike34182f2013-11-06 21:36:55 +0000261 (_is64 ? sizeof(segment_command_64) : sizeof(segment_command));
262 const size_t sectsSize = (_is64 ? sizeof(section_64) : sizeof(section));
263 if (file.fileType == llvm::MachO::MH_OBJECT) {
264 // object files have just one segment load command containing all sections
265 _endOfLoadCommands = _startOfLoadCommands
266 + segCommandBaseSize
267 + file.sections.size() * sectsSize
268 + sizeof(symtab_command);
269 _countOfLoadCommands = 2;
Pete Cooperceee5de2016-02-04 02:16:08 +0000270 if (file.hasMinVersionLoadCommand) {
271 _endOfLoadCommands += sizeof(version_min_command);
272 _countOfLoadCommands++;
273 }
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000274 if (!_file.functionStarts.empty()) {
275 _endOfLoadCommands += sizeof(linkedit_data_command);
276 _countOfLoadCommands++;
277 }
Pete Cooper9b28a452016-02-09 02:10:39 +0000278 if (_file.generateDataInCodeLoadCommand) {
Nick Kledzik21921372014-07-24 23:06:56 +0000279 _endOfLoadCommands += sizeof(linkedit_data_command);
280 _countOfLoadCommands++;
281 }
Nick Kledzikb072c362014-11-18 00:30:29 +0000282 // Assign file offsets to each section.
Nick Kledzike34182f2013-11-06 21:36:55 +0000283 _startOfSectionsContent = _endOfLoadCommands;
Nick Kledzike34182f2013-11-06 21:36:55 +0000284 unsigned relocCount = 0;
Nick Kledzikb072c362014-11-18 00:30:29 +0000285 uint64_t offset = _startOfSectionsContent;
Nick Kledzike34182f2013-11-06 21:36:55 +0000286 for (const Section &sect : file.sections) {
Lang Hamesac2adce2015-12-11 23:25:09 +0000287 if (isZeroFillSection(sect.type))
288 _sectInfo[&sect].fileOffset = 0;
289 else {
Rui Ueyama489a8062016-01-14 20:53:50 +0000290 offset = llvm::alignTo(offset, sect.alignment);
Nick Kledzikb072c362014-11-18 00:30:29 +0000291 _sectInfo[&sect].fileOffset = offset;
292 offset += sect.content.size();
Nick Kledzikb072c362014-11-18 00:30:29 +0000293 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000294 relocCount += sect.relocations.size();
295 }
Nick Kledzikb072c362014-11-18 00:30:29 +0000296 _endOfSectionsContent = offset;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000297
Nick Kledzike34182f2013-11-06 21:36:55 +0000298 computeSymbolTableSizes();
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000299 computeFunctionStartsSize();
Nick Kledzik21921372014-07-24 23:06:56 +0000300 computeDataInCodeSize();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000301
Nick Kledzike34182f2013-11-06 21:36:55 +0000302 // Align start of relocations.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000303 _startOfRelocations = pointerAlign(_endOfSectionsContent);
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000304 _startOfFunctionStarts = _startOfRelocations + relocCount * 8;
305 _startOfDataInCode = _startOfFunctionStarts + _functionStartsSize;
Nick Kledzik21921372014-07-24 23:06:56 +0000306 _startOfSymbols = _startOfDataInCode + _dataInCodeSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000307 // Add Indirect symbol table.
308 _startOfIndirectSymbols = _startOfSymbols + _symbolTableSize;
309 // Align start of symbol table and symbol strings.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000310 _startOfSymbolStrings = _startOfIndirectSymbols
Nick Kledzike34182f2013-11-06 21:36:55 +0000311 + pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000312 _endOfSymbolStrings = _startOfSymbolStrings
Nick Kledzike34182f2013-11-06 21:36:55 +0000313 + pointerAlign(_symbolStringPoolSize);
314 _endOfLinkEdit = _endOfSymbolStrings;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000315 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000316 llvm::dbgs() << "MachOFileLayout()\n"
317 << " startOfLoadCommands=" << _startOfLoadCommands << "\n"
318 << " countOfLoadCommands=" << _countOfLoadCommands << "\n"
319 << " endOfLoadCommands=" << _endOfLoadCommands << "\n"
320 << " startOfRelocations=" << _startOfRelocations << "\n"
321 << " startOfSymbols=" << _startOfSymbols << "\n"
322 << " startOfSymbolStrings=" << _startOfSymbolStrings << "\n"
323 << " endOfSymbolStrings=" << _endOfSymbolStrings << "\n"
324 << " startOfSectionsContent=" << _startOfSectionsContent << "\n"
325 << " endOfSectionsContent=" << _endOfSectionsContent << "\n");
326 } else {
327 // Final linked images have one load command per segment.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000328 _endOfLoadCommands = _startOfLoadCommands
Nick Kledzike34182f2013-11-06 21:36:55 +0000329 + loadCommandsSize(_countOfLoadCommands);
330
331 // Assign section file offsets.
332 buildFileOffsets();
333 buildLinkEditInfo();
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000334
Nick Kledzike34182f2013-11-06 21:36:55 +0000335 // LINKEDIT of final linked images has in order:
336 // rebase info, binding info, lazy binding info, weak binding info,
Nick Kledzik21921372014-07-24 23:06:56 +0000337 // data-in-code, symbol table, indirect symbol table, symbol table strings.
Nick Kledzike34182f2013-11-06 21:36:55 +0000338 _startOfRebaseInfo = _startOfLinkEdit;
339 _endOfRebaseInfo = _startOfRebaseInfo + _rebaseInfo.size();
340 _startOfBindingInfo = _endOfRebaseInfo;
341 _endOfBindingInfo = _startOfBindingInfo + _bindingInfo.size();
342 _startOfLazyBindingInfo = _endOfBindingInfo;
343 _endOfLazyBindingInfo = _startOfLazyBindingInfo + _lazyBindingInfo.size();
Nick Kledzik141330a2014-09-03 19:52:50 +0000344 _startOfExportTrie = _endOfLazyBindingInfo;
345 _endOfExportTrie = _startOfExportTrie + _exportTrie.size();
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000346 _startOfFunctionStarts = _endOfExportTrie;
347 _startOfDataInCode = _startOfFunctionStarts + _functionStartsSize;
Nick Kledzik21921372014-07-24 23:06:56 +0000348 _startOfSymbols = _startOfDataInCode + _dataInCodeSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000349 _startOfIndirectSymbols = _startOfSymbols + _symbolTableSize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000350 _startOfSymbolStrings = _startOfIndirectSymbols
Nick Kledzike34182f2013-11-06 21:36:55 +0000351 + pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t));
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000352 _endOfSymbolStrings = _startOfSymbolStrings
Nick Kledzike34182f2013-11-06 21:36:55 +0000353 + pointerAlign(_symbolStringPoolSize);
354 _endOfLinkEdit = _endOfSymbolStrings;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000355 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000356 llvm::dbgs() << "MachOFileLayout()\n"
357 << " startOfLoadCommands=" << _startOfLoadCommands << "\n"
358 << " countOfLoadCommands=" << _countOfLoadCommands << "\n"
359 << " endOfLoadCommands=" << _endOfLoadCommands << "\n"
360 << " startOfLinkEdit=" << _startOfLinkEdit << "\n"
361 << " startOfRebaseInfo=" << _startOfRebaseInfo << "\n"
362 << " endOfRebaseInfo=" << _endOfRebaseInfo << "\n"
363 << " startOfBindingInfo=" << _startOfBindingInfo << "\n"
364 << " endOfBindingInfo=" << _endOfBindingInfo << "\n"
365 << " startOfLazyBindingInfo=" << _startOfLazyBindingInfo << "\n"
366 << " endOfLazyBindingInfo=" << _endOfLazyBindingInfo << "\n"
Nick Kledzik141330a2014-09-03 19:52:50 +0000367 << " startOfExportTrie=" << _startOfExportTrie << "\n"
368 << " endOfExportTrie=" << _endOfExportTrie << "\n"
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000369 << " startOfFunctionStarts=" << _startOfFunctionStarts << "\n"
Nick Kledzik21921372014-07-24 23:06:56 +0000370 << " startOfDataInCode=" << _startOfDataInCode << "\n"
Nick Kledzike34182f2013-11-06 21:36:55 +0000371 << " startOfSymbols=" << _startOfSymbols << "\n"
372 << " startOfSymbolStrings=" << _startOfSymbolStrings << "\n"
373 << " endOfSymbolStrings=" << _endOfSymbolStrings << "\n"
374 << " addressOfLinkEdit=" << _addressOfLinkEdit << "\n");
375 }
376}
377
378uint32_t MachOFileLayout::loadCommandsSize(uint32_t &count) {
379 uint32_t size = 0;
380 count = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000381
382 const size_t segCommandSize =
Nick Kledzike34182f2013-11-06 21:36:55 +0000383 (_is64 ? sizeof(segment_command_64) : sizeof(segment_command));
384 const size_t sectionSize = (_is64 ? sizeof(section_64) : sizeof(section));
385
386 // Add LC_SEGMENT for each segment.
387 size += _file.segments.size() * segCommandSize;
388 count += _file.segments.size();
389 // Add section record for each section.
390 size += _file.sections.size() * sectionSize;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000391
Tim Northover301c4e62014-07-01 08:15:41 +0000392 // If creating a dylib, add LC_ID_DYLIB.
393 if (_file.fileType == llvm::MachO::MH_DYLIB) {
394 size += sizeof(dylib_command) + pointerAlign(_file.installName.size() + 1);
395 ++count;
396 }
397
Nick Kledzike34182f2013-11-06 21:36:55 +0000398 // Add LC_DYLD_INFO
399 size += sizeof(dyld_info_command);
400 ++count;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000401
Nick Kledzike34182f2013-11-06 21:36:55 +0000402 // Add LC_SYMTAB
403 size += sizeof(symtab_command);
404 ++count;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000405
Nick Kledzike34182f2013-11-06 21:36:55 +0000406 // Add LC_DYSYMTAB
407 if (_file.fileType != llvm::MachO::MH_PRELOAD) {
408 size += sizeof(dysymtab_command);
409 ++count;
410 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000411
Pete Cooper354809e2016-02-03 22:28:29 +0000412 // If main executable add LC_LOAD_DYLINKER
Nick Kledzike34182f2013-11-06 21:36:55 +0000413 if (_file.fileType == llvm::MachO::MH_EXECUTE) {
414 size += pointerAlign(sizeof(dylinker_command) + dyldPath().size()+1);
415 ++count;
Pete Cooper354809e2016-02-03 22:28:29 +0000416 }
417
418 // Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_WATCHOS,
419 // LC_VERSION_MIN_TVOS
420 if (_file.hasMinVersionLoadCommand) {
421 size += sizeof(version_min_command);
422 ++count;
423 }
424
Pete Cooper40576fa2016-02-04 02:45:23 +0000425 // Add LC_SOURCE_VERSION
426 size += sizeof(source_version_command);
427 ++count;
428
Pete Cooper354809e2016-02-03 22:28:29 +0000429 // If main executable add LC_MAIN
430 if (_file.fileType == llvm::MachO::MH_EXECUTE) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000431 size += sizeof(entry_point_command);
432 ++count;
433 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000434
Nick Kledzike34182f2013-11-06 21:36:55 +0000435 // Add LC_LOAD_DYLIB for each dependent dylib.
436 for (const DependentDylib &dep : _file.dependentDylibs) {
437 size += sizeof(dylib_command) + pointerAlign(dep.path.size()+1);
438 ++count;
439 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000440
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000441 // Add LC_RPATH
442 for (const StringRef &path : _file.rpaths) {
Lang Hames2ed3bf92015-10-29 16:50:26 +0000443 size += pointerAlign(sizeof(rpath_command) + path.size() + 1);
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000444 ++count;
445 }
446
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000447 // Add LC_FUNCTION_STARTS if needed
448 if (!_file.functionStarts.empty()) {
449 size += sizeof(linkedit_data_command);
450 ++count;
451 }
452
Pete Cooper9b28a452016-02-09 02:10:39 +0000453 // Add LC_DATA_IN_CODE if requested. Note, we do encode zero length entries.
454 // FIXME: Zero length entries is only to match ld64. Should we change this?
455 if (_file.generateDataInCodeLoadCommand) {
Nick Kledzik54ce29582014-10-28 22:21:10 +0000456 size += sizeof(linkedit_data_command);
457 ++count;
458 }
459
Nick Kledzike34182f2013-11-06 21:36:55 +0000460 return size;
461}
462
463static bool overlaps(const Segment &s1, const Segment &s2) {
464 if (s2.address >= s1.address+s1.size)
465 return false;
466 if (s1.address >= s2.address+s2.size)
467 return false;
468 return true;
469}
470
471static bool overlaps(const Section &s1, const Section &s2) {
472 if (s2.address >= s1.address+s1.content.size())
473 return false;
474 if (s1.address >= s2.address+s2.content.size())
475 return false;
476 return true;
477}
478
479void MachOFileLayout::buildFileOffsets() {
480 // Verify no segments overlap
481 for (const Segment &sg1 : _file.segments) {
482 for (const Segment &sg2 : _file.segments) {
483 if (&sg1 == &sg2)
484 continue;
485 if (overlaps(sg1,sg2)) {
Rafael Espindola372bc702014-06-13 17:20:48 +0000486 _ec = make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000487 return;
488 }
489 }
490 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000491
492 // Verify no sections overlap
Nick Kledzike34182f2013-11-06 21:36:55 +0000493 for (const Section &s1 : _file.sections) {
494 for (const Section &s2 : _file.sections) {
495 if (&s1 == &s2)
496 continue;
497 if (overlaps(s1,s2)) {
Rafael Espindola372bc702014-06-13 17:20:48 +0000498 _ec = make_error_code(llvm::errc::executable_format_error);
Nick Kledzike34182f2013-11-06 21:36:55 +0000499 return;
500 }
501 }
502 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000503
Nick Kledzike34182f2013-11-06 21:36:55 +0000504 // Build side table of extra info about segments and sections.
505 SegExtraInfo t;
506 t.fileOffset = 0;
507 for (const Segment &sg : _file.segments) {
508 _segInfo[&sg] = t;
509 }
510 SectionExtraInfo t2;
511 t2.fileOffset = 0;
512 // Assign sections to segments.
513 for (const Section &s : _file.sections) {
514 _sectInfo[&s] = t2;
Nick Kledzik1bebb282014-09-09 23:52:59 +0000515 bool foundSegment = false;
Nick Kledzike34182f2013-11-06 21:36:55 +0000516 for (const Segment &sg : _file.segments) {
Nick Kledzik1bebb282014-09-09 23:52:59 +0000517 if (sg.name.equals(s.segmentName)) {
518 if ((s.address >= sg.address)
Nick Kledzike34182f2013-11-06 21:36:55 +0000519 && (s.address+s.content.size() <= sg.address+sg.size)) {
Nick Kledzik1bebb282014-09-09 23:52:59 +0000520 _segInfo[&sg].sections.push_back(&s);
521 foundSegment = true;
522 break;
Nick Kledzike34182f2013-11-06 21:36:55 +0000523 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000524 }
525 }
Nick Kledzik1bebb282014-09-09 23:52:59 +0000526 if (!foundSegment) {
527 _ec = make_error_code(llvm::errc::executable_format_error);
528 return;
529 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000530 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000531
Nick Kledzike34182f2013-11-06 21:36:55 +0000532 // Assign file offsets.
533 uint32_t fileOffset = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000534 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000535 llvm::dbgs() << "buildFileOffsets()\n");
536 for (const Segment &sg : _file.segments) {
Tim Northover08d6a7b2014-06-30 09:49:30 +0000537 _segInfo[&sg].fileOffset = fileOffset;
Pete Cooperb8fec3e2016-02-06 00:51:16 +0000538 if ((_seg1addr == INT64_MAX) && sg.init_access)
Nick Kledzike34182f2013-11-06 21:36:55 +0000539 _seg1addr = sg.address;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000540 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000541 llvm::dbgs() << " segment=" << sg.name
542 << ", fileOffset=" << _segInfo[&sg].fileOffset << "\n");
Tim Northover08d6a7b2014-06-30 09:49:30 +0000543
544 uint32_t segFileSize = 0;
Nick Kledzik761d6542014-10-24 22:19:22 +0000545 // A segment that is not zero-fill must use a least one page of disk space.
Pete Cooperb8fec3e2016-02-06 00:51:16 +0000546 if (sg.init_access)
Nick Kledzik761d6542014-10-24 22:19:22 +0000547 segFileSize = _file.pageSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000548 for (const Section *s : _segInfo[&sg].sections) {
Tim Northover08d6a7b2014-06-30 09:49:30 +0000549 uint32_t sectOffset = s->address - sg.address;
550 uint32_t sectFileSize =
Lang Hamesac2adce2015-12-11 23:25:09 +0000551 isZeroFillSection(s->type) ? 0 : s->content.size();
Tim Northover08d6a7b2014-06-30 09:49:30 +0000552 segFileSize = std::max(segFileSize, sectOffset + sectFileSize);
553
554 _sectInfo[s].fileOffset = _segInfo[&sg].fileOffset + sectOffset;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000555 DEBUG_WITH_TYPE("MachOFileLayout",
Nick Kledzike34182f2013-11-06 21:36:55 +0000556 llvm::dbgs() << " section=" << s->sectionName
557 << ", fileOffset=" << fileOffset << "\n");
558 }
Tim Northover08d6a7b2014-06-30 09:49:30 +0000559
Pete Cooper353652f2016-02-06 00:14:15 +0000560 // round up all segments to page aligned, except __LINKEDIT
561 if (!sg.name.equals("__LINKEDIT")) {
562 _segInfo[&sg].fileSize = llvm::alignTo(segFileSize, _file.pageSize);
563 fileOffset = llvm::alignTo(fileOffset + segFileSize, _file.pageSize);
564 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000565 _addressOfLinkEdit = sg.address + sg.size;
566 }
Tim Northover08d6a7b2014-06-30 09:49:30 +0000567 _startOfLinkEdit = fileOffset;
Nick Kledzike34182f2013-11-06 21:36:55 +0000568}
569
Nick Kledzike34182f2013-11-06 21:36:55 +0000570size_t MachOFileLayout::size() const {
571 return _endOfSymbolStrings;
572}
573
574void MachOFileLayout::writeMachHeader() {
Pete Cooper8563e5a2016-02-04 20:43:43 +0000575 auto cpusubtype = MachOLinkingContext::cpuSubtypeFromArch(_file.arch);
576 // dynamic x86 executables on newer OS version should also set the
577 // CPU_SUBTYPE_LIB64 mask in the CPU subtype.
578 // FIXME: Check that this is a dynamic executable, not a static one.
579 if (_file.fileType == llvm::MachO::MH_EXECUTE &&
580 cpusubtype == CPU_SUBTYPE_X86_64_ALL &&
581 _file.os == MachOLinkingContext::OS::macOSX) {
582 uint32_t version;
583 bool failed = MachOLinkingContext::parsePackedVersion("10.5", version);
584 if (!failed && _file.minOSverson >= version)
585 cpusubtype |= CPU_SUBTYPE_LIB64;
586 }
587
Nick Kledzike34182f2013-11-06 21:36:55 +0000588 mach_header *mh = reinterpret_cast<mach_header*>(_buffer);
589 mh->magic = _is64 ? llvm::MachO::MH_MAGIC_64 : llvm::MachO::MH_MAGIC;
590 mh->cputype = MachOLinkingContext::cpuTypeFromArch(_file.arch);
Pete Cooper8563e5a2016-02-04 20:43:43 +0000591 mh->cpusubtype = cpusubtype;
Nick Kledzike34182f2013-11-06 21:36:55 +0000592 mh->filetype = _file.fileType;
593 mh->ncmds = _countOfLoadCommands;
594 mh->sizeofcmds = _endOfLoadCommands - _startOfLoadCommands;
595 mh->flags = _file.flags;
596 if (_swap)
597 swapStruct(*mh);
598}
599
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000600uint32_t MachOFileLayout::indirectSymbolIndex(const Section &sect,
Nick Kledzike34182f2013-11-06 21:36:55 +0000601 uint32_t &index) {
602 if (sect.indirectSymbols.empty())
603 return 0;
604 uint32_t result = index;
605 index += sect.indirectSymbols.size();
606 return result;
607}
608
609uint32_t MachOFileLayout::indirectSymbolElementSize(const Section &sect) {
610 if (sect.indirectSymbols.empty())
611 return 0;
612 if (sect.type != S_SYMBOL_STUBS)
613 return 0;
614 return sect.content.size() / sect.indirectSymbols.size();
615}
616
Nick Kledzik29f749e2013-11-09 00:07:28 +0000617template <typename T>
Pete Cooper514594b2016-03-31 00:08:16 +0000618llvm::Error MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) {
Nick Kledzik29f749e2013-11-09 00:07:28 +0000619 typename T::command* seg = reinterpret_cast<typename T::command*>(lc);
620 seg->cmd = T::LC;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000621 seg->cmdsize = sizeof(typename T::command)
Nick Kledzik29f749e2013-11-09 00:07:28 +0000622 + _file.sections.size() * sizeof(typename T::section);
Nick Kledzike34182f2013-11-06 21:36:55 +0000623 uint8_t *next = lc + seg->cmdsize;
624 memset(seg->segname, 0, 16);
625 seg->vmaddr = 0;
Nick Kledzikb072c362014-11-18 00:30:29 +0000626 seg->vmsize = _file.sections.back().address
627 + _file.sections.back().content.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000628 seg->fileoff = _endOfLoadCommands;
Lang Hames8c2406b2016-08-10 22:15:09 +0000629 seg->filesize = _sectInfo[&_file.sections.back()].fileOffset +
630 _file.sections.back().content.size() -
631 _sectInfo[&_file.sections.front()].fileOffset;
Nick Kledzike34182f2013-11-06 21:36:55 +0000632 seg->maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
633 seg->initprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE;
634 seg->nsects = _file.sections.size();
635 seg->flags = 0;
636 if (_swap)
637 swapStruct(*seg);
Nick Kledzik29f749e2013-11-09 00:07:28 +0000638 typename T::section *sout = reinterpret_cast<typename T::section*>
639 (lc+sizeof(typename T::command));
Nick Kledzike34182f2013-11-06 21:36:55 +0000640 uint32_t relOffset = _startOfRelocations;
Nick Kledzike34182f2013-11-06 21:36:55 +0000641 uint32_t indirectSymRunningIndex = 0;
642 for (const Section &sin : _file.sections) {
643 setString16(sin.sectionName, sout->sectname);
644 setString16(sin.segmentName, sout->segname);
645 sout->addr = sin.address;
646 sout->size = sin.content.size();
Nick Kledzikb072c362014-11-18 00:30:29 +0000647 sout->offset = _sectInfo[&sin].fileOffset;
Rui Ueyamaf217ef02015-03-26 02:03:44 +0000648 sout->align = llvm::Log2_32(sin.alignment);
Nick Kledzike34182f2013-11-06 21:36:55 +0000649 sout->reloff = sin.relocations.empty() ? 0 : relOffset;
650 sout->nreloc = sin.relocations.size();
651 sout->flags = sin.type | sin.attributes;
652 sout->reserved1 = indirectSymbolIndex(sin, indirectSymRunningIndex);
653 sout->reserved2 = indirectSymbolElementSize(sin);
654 relOffset += sin.relocations.size() * sizeof(any_relocation_info);
Nick Kledzike34182f2013-11-06 21:36:55 +0000655 if (_swap)
656 swapStruct(*sout);
657 ++sout;
658 }
659 lc = next;
Mehdi Aminic1edf562016-11-11 04:29:25 +0000660 return llvm::Error::success();
Nick Kledzike34182f2013-11-06 21:36:55 +0000661}
662
Nick Kledzik29f749e2013-11-09 00:07:28 +0000663template <typename T>
Pete Cooper514594b2016-03-31 00:08:16 +0000664llvm::Error MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000665 uint32_t indirectSymRunningIndex = 0;
666 for (const Segment &seg : _file.segments) {
Pete Cooper353652f2016-02-06 00:14:15 +0000667 // Link edit has no sections and a custom range of address, so handle it
668 // specially.
Nick Kledzike34182f2013-11-06 21:36:55 +0000669 SegExtraInfo &segInfo = _segInfo[&seg];
Pete Cooper353652f2016-02-06 00:14:15 +0000670 if (seg.name.equals("__LINKEDIT")) {
671 size_t linkeditSize = _endOfLinkEdit - _startOfLinkEdit;
672 typename T::command* cmd = reinterpret_cast<typename T::command*>(lc);
673 cmd->cmd = T::LC;
674 cmd->cmdsize = sizeof(typename T::command);
675 uint8_t *next = lc + cmd->cmdsize;
676 setString16("__LINKEDIT", cmd->segname);
677 cmd->vmaddr = _addressOfLinkEdit;
678 cmd->vmsize = llvm::alignTo(linkeditSize, _file.pageSize);
679 cmd->fileoff = _startOfLinkEdit;
680 cmd->filesize = linkeditSize;
Pete Cooperb8fec3e2016-02-06 00:51:16 +0000681 cmd->initprot = seg.init_access;
682 cmd->maxprot = seg.max_access;
Pete Cooper353652f2016-02-06 00:14:15 +0000683 cmd->nsects = 0;
684 cmd->flags = 0;
685 if (_swap)
686 swapStruct(*cmd);
687 lc = next;
688 continue;
689 }
690 // Write segment command with trailing sections.
Nick Kledzik29f749e2013-11-09 00:07:28 +0000691 typename T::command* cmd = reinterpret_cast<typename T::command*>(lc);
692 cmd->cmd = T::LC;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000693 cmd->cmdsize = sizeof(typename T::command)
Nick Kledzik29f749e2013-11-09 00:07:28 +0000694 + segInfo.sections.size() * sizeof(typename T::section);
Nick Kledzike34182f2013-11-06 21:36:55 +0000695 uint8_t *next = lc + cmd->cmdsize;
696 setString16(seg.name, cmd->segname);
697 cmd->vmaddr = seg.address;
698 cmd->vmsize = seg.size;
699 cmd->fileoff = segInfo.fileOffset;
Tim Northover08d6a7b2014-06-30 09:49:30 +0000700 cmd->filesize = segInfo.fileSize;
Pete Cooperb8fec3e2016-02-06 00:51:16 +0000701 cmd->initprot = seg.init_access;
702 cmd->maxprot = seg.max_access;
Nick Kledzike34182f2013-11-06 21:36:55 +0000703 cmd->nsects = segInfo.sections.size();
704 cmd->flags = 0;
705 if (_swap)
706 swapStruct(*cmd);
Nick Kledzik29f749e2013-11-09 00:07:28 +0000707 typename T::section *sect = reinterpret_cast<typename T::section*>
708 (lc+sizeof(typename T::command));
Nick Kledzike34182f2013-11-06 21:36:55 +0000709 for (const Section *section : segInfo.sections) {
710 setString16(section->sectionName, sect->sectname);
711 setString16(section->segmentName, sect->segname);
712 sect->addr = section->address;
713 sect->size = section->content.size();
Lang Hamesac2adce2015-12-11 23:25:09 +0000714 if (isZeroFillSection(section->type))
Nick Kledzikb072c362014-11-18 00:30:29 +0000715 sect->offset = 0;
716 else
717 sect->offset = section->address - seg.address + segInfo.fileOffset;
Rui Ueyamaf217ef02015-03-26 02:03:44 +0000718 sect->align = llvm::Log2_32(section->alignment);
Nick Kledzike34182f2013-11-06 21:36:55 +0000719 sect->reloff = 0;
720 sect->nreloc = 0;
721 sect->flags = section->type | section->attributes;
722 sect->reserved1 = indirectSymbolIndex(*section, indirectSymRunningIndex);
723 sect->reserved2 = indirectSymbolElementSize(*section);
724 if (_swap)
725 swapStruct(*sect);
726 ++sect;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000727 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000728 lc = reinterpret_cast<uint8_t*>(next);
729 }
Mehdi Aminic1edf562016-11-11 04:29:25 +0000730 return llvm::Error::success();
Nick Kledzike34182f2013-11-06 21:36:55 +0000731}
732
Pete Cooperceee5de2016-02-04 02:16:08 +0000733static void writeVersionMinLoadCommand(const NormalizedFile &_file,
734 bool _swap,
735 uint8_t *&lc) {
736 if (!_file.hasMinVersionLoadCommand)
737 return;
738 version_min_command *vm = reinterpret_cast<version_min_command*>(lc);
739 switch (_file.os) {
740 case MachOLinkingContext::OS::unknown:
741 vm->cmd = _file.minOSVersionKind;
742 vm->cmdsize = sizeof(version_min_command);
743 vm->version = _file.minOSverson;
744 vm->sdk = 0;
745 break;
746 case MachOLinkingContext::OS::macOSX:
747 vm->cmd = LC_VERSION_MIN_MACOSX;
748 vm->cmdsize = sizeof(version_min_command);
749 vm->version = _file.minOSverson;
750 vm->sdk = _file.sdkVersion;
751 break;
752 case MachOLinkingContext::OS::iOS:
753 case MachOLinkingContext::OS::iOS_simulator:
754 vm->cmd = LC_VERSION_MIN_IPHONEOS;
755 vm->cmdsize = sizeof(version_min_command);
756 vm->version = _file.minOSverson;
757 vm->sdk = _file.sdkVersion;
758 break;
759 }
760 if (_swap)
761 swapStruct(*vm);
762 lc += sizeof(version_min_command);
763}
764
Pete Cooper514594b2016-03-31 00:08:16 +0000765llvm::Error MachOFileLayout::writeLoadCommands() {
Nick Kledzike34182f2013-11-06 21:36:55 +0000766 uint8_t *lc = &_buffer[_startOfLoadCommands];
767 if (_file.fileType == llvm::MachO::MH_OBJECT) {
768 // Object files have one unnamed segment which holds all sections.
Pete Cooper514594b2016-03-31 00:08:16 +0000769 if (_is64) {
770 if (auto ec = writeSingleSegmentLoadCommand<MachO64Trait>(lc))
Pete Coopere487da12016-03-31 00:35:50 +0000771 return ec;
Pete Cooper514594b2016-03-31 00:08:16 +0000772 } else {
773 if (auto ec = writeSingleSegmentLoadCommand<MachO32Trait>(lc))
Pete Cooperdc59c792016-03-31 00:38:02 +0000774 return ec;
Pete Cooper514594b2016-03-31 00:08:16 +0000775 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000776 // Add LC_SYMTAB with symbol table info
777 symtab_command* st = reinterpret_cast<symtab_command*>(lc);
778 st->cmd = LC_SYMTAB;
779 st->cmdsize = sizeof(symtab_command);
780 st->symoff = _startOfSymbols;
Lang Hames436f7d62016-07-27 22:55:30 +0000781 st->nsyms = _file.stabsSymbols.size() + _file.localSymbols.size() +
782 _file.globalSymbols.size() + _file.undefinedSymbols.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000783 st->stroff = _startOfSymbolStrings;
784 st->strsize = _endOfSymbolStrings - _startOfSymbolStrings;
785 if (_swap)
786 swapStruct(*st);
Nick Kledzik21921372014-07-24 23:06:56 +0000787 lc += sizeof(symtab_command);
Pete Cooperceee5de2016-02-04 02:16:08 +0000788
789 // Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS,
790 // LC_VERSION_MIN_WATCHOS, LC_VERSION_MIN_TVOS
791 writeVersionMinLoadCommand(_file, _swap, lc);
792
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000793 // Add LC_FUNCTION_STARTS if needed.
794 if (_functionStartsSize != 0) {
795 linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
796 dl->cmd = LC_FUNCTION_STARTS;
797 dl->cmdsize = sizeof(linkedit_data_command);
798 dl->dataoff = _startOfFunctionStarts;
799 dl->datasize = _functionStartsSize;
800 if (_swap)
801 swapStruct(*dl);
802 lc += sizeof(linkedit_data_command);
803 }
804
Pete Cooper9b28a452016-02-09 02:10:39 +0000805 // Add LC_DATA_IN_CODE if requested.
806 if (_file.generateDataInCodeLoadCommand) {
Nick Kledzik21921372014-07-24 23:06:56 +0000807 linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
808 dl->cmd = LC_DATA_IN_CODE;
809 dl->cmdsize = sizeof(linkedit_data_command);
810 dl->dataoff = _startOfDataInCode;
811 dl->datasize = _dataInCodeSize;
812 if (_swap)
813 swapStruct(*dl);
814 lc += sizeof(linkedit_data_command);
815 }
Nick Kledzike34182f2013-11-06 21:36:55 +0000816 } else {
817 // Final linked images have sections under segments.
Pete Cooper514594b2016-03-31 00:08:16 +0000818 if (_is64) {
819 if (auto ec = writeSegmentLoadCommands<MachO64Trait>(lc))
Pete Cooperdc59c792016-03-31 00:38:02 +0000820 return ec;
Pete Cooper514594b2016-03-31 00:08:16 +0000821 } else {
822 if (auto ec = writeSegmentLoadCommands<MachO32Trait>(lc))
Pete Cooperdc59c792016-03-31 00:38:02 +0000823 return ec;
Pete Cooper514594b2016-03-31 00:08:16 +0000824 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000825
Tim Northover301c4e62014-07-01 08:15:41 +0000826 // Add LC_ID_DYLIB command for dynamic libraries.
827 if (_file.fileType == llvm::MachO::MH_DYLIB) {
828 dylib_command *dc = reinterpret_cast<dylib_command*>(lc);
829 StringRef path = _file.installName;
830 uint32_t size = sizeof(dylib_command) + pointerAlign(path.size() + 1);
831 dc->cmd = LC_ID_DYLIB;
832 dc->cmdsize = size;
833 dc->dylib.name = sizeof(dylib_command); // offset
Jean-Daniel Dupasedefccc2014-12-20 09:22:56 +0000834 // needs to be some constant value different than the one in LC_LOAD_DYLIB
835 dc->dylib.timestamp = 1;
Nick Kledzik5b9e48b2014-11-19 02:21:53 +0000836 dc->dylib.current_version = _file.currentVersion;
837 dc->dylib.compatibility_version = _file.compatVersion;
Tim Northover301c4e62014-07-01 08:15:41 +0000838 if (_swap)
839 swapStruct(*dc);
840 memcpy(lc + sizeof(dylib_command), path.begin(), path.size());
841 lc[sizeof(dylib_command) + path.size()] = '\0';
842 lc += size;
843 }
844
Nick Kledzike34182f2013-11-06 21:36:55 +0000845 // Add LC_DYLD_INFO_ONLY.
846 dyld_info_command* di = reinterpret_cast<dyld_info_command*>(lc);
847 di->cmd = LC_DYLD_INFO_ONLY;
848 di->cmdsize = sizeof(dyld_info_command);
849 di->rebase_off = _rebaseInfo.size() ? _startOfRebaseInfo : 0;
850 di->rebase_size = _rebaseInfo.size();
851 di->bind_off = _bindingInfo.size() ? _startOfBindingInfo : 0;
852 di->bind_size = _bindingInfo.size();
853 di->weak_bind_off = 0;
854 di->weak_bind_size = 0;
855 di->lazy_bind_off = _lazyBindingInfo.size() ? _startOfLazyBindingInfo : 0;
856 di->lazy_bind_size = _lazyBindingInfo.size();
Nick Kledzik141330a2014-09-03 19:52:50 +0000857 di->export_off = _exportTrie.size() ? _startOfExportTrie : 0;
858 di->export_size = _exportTrie.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000859 if (_swap)
860 swapStruct(*di);
861 lc += sizeof(dyld_info_command);
862
863 // Add LC_SYMTAB with symbol table info.
864 symtab_command* st = reinterpret_cast<symtab_command*>(lc);
865 st->cmd = LC_SYMTAB;
866 st->cmdsize = sizeof(symtab_command);
867 st->symoff = _startOfSymbols;
Lang Hames436f7d62016-07-27 22:55:30 +0000868 st->nsyms = _file.stabsSymbols.size() + _file.localSymbols.size() +
869 _file.globalSymbols.size() + _file.undefinedSymbols.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000870 st->stroff = _startOfSymbolStrings;
871 st->strsize = _endOfSymbolStrings - _startOfSymbolStrings;
872 if (_swap)
873 swapStruct(*st);
874 lc += sizeof(symtab_command);
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000875
Nick Kledzike34182f2013-11-06 21:36:55 +0000876 // Add LC_DYSYMTAB
877 if (_file.fileType != llvm::MachO::MH_PRELOAD) {
878 dysymtab_command* dst = reinterpret_cast<dysymtab_command*>(lc);
879 dst->cmd = LC_DYSYMTAB;
880 dst->cmdsize = sizeof(dysymtab_command);
881 dst->ilocalsym = _symbolTableLocalsStartIndex;
Lang Hames436f7d62016-07-27 22:55:30 +0000882 dst->nlocalsym = _file.stabsSymbols.size() +
883 _file.localSymbols.size();
Nick Kledzike34182f2013-11-06 21:36:55 +0000884 dst->iextdefsym = _symbolTableGlobalsStartIndex;
885 dst->nextdefsym = _file.globalSymbols.size();
886 dst->iundefsym = _symbolTableUndefinesStartIndex;
887 dst->nundefsym = _file.undefinedSymbols.size();
888 dst->tocoff = 0;
889 dst->ntoc = 0;
890 dst->modtaboff = 0;
891 dst->nmodtab = 0;
892 dst->extrefsymoff = 0;
893 dst->nextrefsyms = 0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000894 dst->indirectsymoff = _startOfIndirectSymbols;
Nick Kledzike34182f2013-11-06 21:36:55 +0000895 dst->nindirectsyms = _indirectSymbolTableCount;
896 dst->extreloff = 0;
897 dst->nextrel = 0;
898 dst->locreloff = 0;
899 dst->nlocrel = 0;
900 if (_swap)
901 swapStruct(*dst);
902 lc += sizeof(dysymtab_command);
903 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000904
Pete Cooper354809e2016-02-03 22:28:29 +0000905 // If main executable, add LC_LOAD_DYLINKER
Nick Kledzike34182f2013-11-06 21:36:55 +0000906 if (_file.fileType == llvm::MachO::MH_EXECUTE) {
907 // Build LC_LOAD_DYLINKER load command.
908 uint32_t size=pointerAlign(sizeof(dylinker_command)+dyldPath().size()+1);
909 dylinker_command* dl = reinterpret_cast<dylinker_command*>(lc);
910 dl->cmd = LC_LOAD_DYLINKER;
911 dl->cmdsize = size;
912 dl->name = sizeof(dylinker_command); // offset
913 if (_swap)
914 swapStruct(*dl);
915 memcpy(lc+sizeof(dylinker_command), dyldPath().data(), dyldPath().size());
916 lc[sizeof(dylinker_command)+dyldPath().size()] = '\0';
917 lc += size;
Pete Cooper354809e2016-02-03 22:28:29 +0000918 }
919
920 // Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_WATCHOS,
921 // LC_VERSION_MIN_TVOS
Pete Cooperceee5de2016-02-04 02:16:08 +0000922 writeVersionMinLoadCommand(_file, _swap, lc);
Pete Cooper354809e2016-02-03 22:28:29 +0000923
Pete Cooper40576fa2016-02-04 02:45:23 +0000924 // Add LC_SOURCE_VERSION
925 {
Pete Cooperb565bdf2016-03-23 22:00:09 +0000926 // Note, using a temporary here to appease UB as we may not be aligned
927 // enough for a struct containing a uint64_t when emitting a 32-bit binary
928 source_version_command sv;
929 sv.cmd = LC_SOURCE_VERSION;
930 sv.cmdsize = sizeof(source_version_command);
931 sv.version = _file.sourceVersion;
Pete Cooper40576fa2016-02-04 02:45:23 +0000932 if (_swap)
Pete Cooperb565bdf2016-03-23 22:00:09 +0000933 swapStruct(sv);
934 memcpy(lc, &sv, sizeof(source_version_command));
Pete Cooper40576fa2016-02-04 02:45:23 +0000935 lc += sizeof(source_version_command);
936 }
937
Pete Cooper354809e2016-02-03 22:28:29 +0000938 // If main executable, add LC_MAIN.
939 if (_file.fileType == llvm::MachO::MH_EXECUTE) {
Nick Kledzike34182f2013-11-06 21:36:55 +0000940 // Build LC_MAIN load command.
Pete Cooper07601d32016-03-24 01:05:17 +0000941 // Note, using a temporary here to appease UB as we may not be aligned
942 // enough for a struct containing a uint64_t when emitting a 32-bit binary
943 entry_point_command ep;
944 ep.cmd = LC_MAIN;
945 ep.cmdsize = sizeof(entry_point_command);
946 ep.entryoff = _file.entryAddress - _seg1addr;
947 ep.stacksize = _file.stackSize;
Nick Kledzike34182f2013-11-06 21:36:55 +0000948 if (_swap)
Pete Cooper07601d32016-03-24 01:05:17 +0000949 swapStruct(ep);
950 memcpy(lc, &ep, sizeof(entry_point_command));
Nick Kledzike34182f2013-11-06 21:36:55 +0000951 lc += sizeof(entry_point_command);
952 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000953
Nick Kledzike34182f2013-11-06 21:36:55 +0000954 // Add LC_LOAD_DYLIB commands
955 for (const DependentDylib &dep : _file.dependentDylibs) {
956 dylib_command* dc = reinterpret_cast<dylib_command*>(lc);
957 uint32_t size = sizeof(dylib_command) + pointerAlign(dep.path.size()+1);
Nick Kledzik51720672014-10-16 19:31:28 +0000958 dc->cmd = dep.kind;
Nick Kledzike34182f2013-11-06 21:36:55 +0000959 dc->cmdsize = size;
960 dc->dylib.name = sizeof(dylib_command); // offset
Jean-Daniel Dupasedefccc2014-12-20 09:22:56 +0000961 // needs to be some constant value different than the one in LC_ID_DYLIB
Nick Kledzik5b9e48b2014-11-19 02:21:53 +0000962 dc->dylib.timestamp = 2;
963 dc->dylib.current_version = dep.currentVersion;
964 dc->dylib.compatibility_version = dep.compatVersion;
Nick Kledzike34182f2013-11-06 21:36:55 +0000965 if (_swap)
966 swapStruct(*dc);
967 memcpy(lc+sizeof(dylib_command), dep.path.begin(), dep.path.size());
968 lc[sizeof(dylib_command)+dep.path.size()] = '\0';
969 lc += size;
970 }
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000971
972 // Add LC_RPATH
973 for (const StringRef &path : _file.rpaths) {
974 rpath_command *rpc = reinterpret_cast<rpath_command *>(lc);
Lang Hames2ed3bf92015-10-29 16:50:26 +0000975 uint32_t size = pointerAlign(sizeof(rpath_command) + path.size() + 1);
Jean-Daniel Dupas23dd15e2014-12-18 21:33:38 +0000976 rpc->cmd = LC_RPATH;
977 rpc->cmdsize = size;
978 rpc->path = sizeof(rpath_command); // offset
979 if (_swap)
980 swapStruct(*rpc);
981 memcpy(lc+sizeof(rpath_command), path.begin(), path.size());
982 lc[sizeof(rpath_command)+path.size()] = '\0';
983 lc += size;
984 }
985
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000986 // Add LC_FUNCTION_STARTS if needed.
987 if (_functionStartsSize != 0) {
988 linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
989 dl->cmd = LC_FUNCTION_STARTS;
990 dl->cmdsize = sizeof(linkedit_data_command);
991 dl->dataoff = _startOfFunctionStarts;
992 dl->datasize = _functionStartsSize;
993 if (_swap)
994 swapStruct(*dl);
995 lc += sizeof(linkedit_data_command);
996 }
997
Pete Cooper9b28a452016-02-09 02:10:39 +0000998 // Add LC_DATA_IN_CODE if requested.
999 if (_file.generateDataInCodeLoadCommand) {
Nick Kledzik54ce29582014-10-28 22:21:10 +00001000 linkedit_data_command* dl = reinterpret_cast<linkedit_data_command*>(lc);
1001 dl->cmd = LC_DATA_IN_CODE;
1002 dl->cmdsize = sizeof(linkedit_data_command);
1003 dl->dataoff = _startOfDataInCode;
1004 dl->datasize = _dataInCodeSize;
1005 if (_swap)
1006 swapStruct(*dl);
1007 lc += sizeof(linkedit_data_command);
1008 }
Nick Kledzike34182f2013-11-06 21:36:55 +00001009 }
Mehdi Aminic1edf562016-11-11 04:29:25 +00001010 return llvm::Error::success();
Nick Kledzike34182f2013-11-06 21:36:55 +00001011}
1012
Nick Kledzike34182f2013-11-06 21:36:55 +00001013void MachOFileLayout::writeSectionContent() {
1014 for (const Section &s : _file.sections) {
1015 // Copy all section content to output buffer.
Lang Hamesac2adce2015-12-11 23:25:09 +00001016 if (isZeroFillSection(s.type))
Nick Kledzik61fdef62014-05-15 20:59:23 +00001017 continue;
Nick Kledzik1bebb282014-09-09 23:52:59 +00001018 if (s.content.empty())
1019 continue;
Nick Kledzike34182f2013-11-06 21:36:55 +00001020 uint32_t offset = _sectInfo[&s].fileOffset;
1021 uint8_t *p = &_buffer[offset];
1022 memcpy(p, &s.content[0], s.content.size());
1023 p += s.content.size();
1024 }
1025}
1026
1027void MachOFileLayout::writeRelocations() {
1028 uint32_t relOffset = _startOfRelocations;
1029 for (Section sect : _file.sections) {
1030 for (Relocation r : sect.relocations) {
1031 any_relocation_info* rb = reinterpret_cast<any_relocation_info*>(
1032 &_buffer[relOffset]);
1033 *rb = packRelocation(r, _swap, _bigEndianArch);
1034 relOffset += sizeof(any_relocation_info);
1035 }
1036 }
1037}
1038
Nick Kledzike34182f2013-11-06 21:36:55 +00001039void MachOFileLayout::appendSymbols(const std::vector<Symbol> &symbols,
1040 uint32_t &symOffset, uint32_t &strOffset) {
1041 for (const Symbol &sym : symbols) {
1042 if (_is64) {
1043 nlist_64* nb = reinterpret_cast<nlist_64*>(&_buffer[symOffset]);
1044 nb->n_strx = strOffset - _startOfSymbolStrings;
1045 nb->n_type = sym.type | sym.scope;
1046 nb->n_sect = sym.sect;
1047 nb->n_desc = sym.desc;
1048 nb->n_value = sym.value;
1049 if (_swap)
1050 swapStruct(*nb);
1051 symOffset += sizeof(nlist_64);
1052 } else {
1053 nlist* nb = reinterpret_cast<nlist*>(&_buffer[symOffset]);
1054 nb->n_strx = strOffset - _startOfSymbolStrings;
1055 nb->n_type = sym.type | sym.scope;
1056 nb->n_sect = sym.sect;
1057 nb->n_desc = sym.desc;
1058 nb->n_value = sym.value;
1059 if (_swap)
1060 swapStruct(*nb);
1061 symOffset += sizeof(nlist);
1062 }
1063 memcpy(&_buffer[strOffset], sym.name.begin(), sym.name.size());
1064 strOffset += sym.name.size();
1065 _buffer[strOffset++] ='\0'; // Strings in table have nul terminator.
1066 }
1067}
1068
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001069void MachOFileLayout::writeFunctionStartsInfo() {
Pete Cooper8e1b9a12016-03-22 22:51:03 +00001070 if (!_functionStartsSize)
1071 return;
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001072 memcpy(&_buffer[_startOfFunctionStarts], _file.functionStarts.data(),
1073 _functionStartsSize);
1074}
1075
Nick Kledzik21921372014-07-24 23:06:56 +00001076void MachOFileLayout::writeDataInCodeInfo() {
1077 uint32_t offset = _startOfDataInCode;
1078 for (const DataInCode &entry : _file.dataInCode) {
1079 data_in_code_entry *dst = reinterpret_cast<data_in_code_entry*>(
1080 &_buffer[offset]);
1081 dst->offset = entry.offset;
1082 dst->length = entry.length;
1083 dst->kind = entry.kind;
1084 if (_swap)
1085 swapStruct(*dst);
1086 offset += sizeof(data_in_code_entry);
1087 }
1088}
1089
Nick Kledzike34182f2013-11-06 21:36:55 +00001090void MachOFileLayout::writeSymbolTable() {
1091 // Write symbol table and symbol strings in parallel.
1092 uint32_t symOffset = _startOfSymbols;
1093 uint32_t strOffset = _startOfSymbolStrings;
Pete Cooper5559b242016-08-08 23:20:04 +00001094 // Reserve n_strx offset of zero to mean no name.
1095 _buffer[strOffset++] = ' ';
1096 _buffer[strOffset++] = '\0';
Lang Hames436f7d62016-07-27 22:55:30 +00001097 appendSymbols(_file.stabsSymbols, symOffset, strOffset);
Nick Kledzike34182f2013-11-06 21:36:55 +00001098 appendSymbols(_file.localSymbols, symOffset, strOffset);
1099 appendSymbols(_file.globalSymbols, symOffset, strOffset);
1100 appendSymbols(_file.undefinedSymbols, symOffset, strOffset);
1101 // Write indirect symbol table array.
1102 uint32_t *indirects = reinterpret_cast<uint32_t*>
1103 (&_buffer[_startOfIndirectSymbols]);
1104 if (_file.fileType == llvm::MachO::MH_OBJECT) {
1105 // Object files have sections in same order as input normalized file.
1106 for (const Section &section : _file.sections) {
1107 for (uint32_t index : section.indirectSymbols) {
1108 if (_swap)
Artyom Skrobov17587fb2014-06-14 12:40:04 +00001109 *indirects++ = llvm::sys::getSwappedBytes(index);
Nick Kledzike34182f2013-11-06 21:36:55 +00001110 else
1111 *indirects++ = index;
1112 }
1113 }
1114 } else {
1115 // Final linked images must sort sections from normalized file.
1116 for (const Segment &seg : _file.segments) {
1117 SegExtraInfo &segInfo = _segInfo[&seg];
1118 for (const Section *section : segInfo.sections) {
1119 for (uint32_t index : section->indirectSymbols) {
1120 if (_swap)
Artyom Skrobov17587fb2014-06-14 12:40:04 +00001121 *indirects++ = llvm::sys::getSwappedBytes(index);
Nick Kledzike34182f2013-11-06 21:36:55 +00001122 else
1123 *indirects++ = index;
1124 }
1125 }
1126 }
1127 }
1128}
1129
1130void MachOFileLayout::writeRebaseInfo() {
1131 memcpy(&_buffer[_startOfRebaseInfo], _rebaseInfo.bytes(), _rebaseInfo.size());
1132}
1133
1134void MachOFileLayout::writeBindingInfo() {
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001135 memcpy(&_buffer[_startOfBindingInfo],
Nick Kledzike34182f2013-11-06 21:36:55 +00001136 _bindingInfo.bytes(), _bindingInfo.size());
1137}
1138
1139void MachOFileLayout::writeLazyBindingInfo() {
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001140 memcpy(&_buffer[_startOfLazyBindingInfo],
Nick Kledzike34182f2013-11-06 21:36:55 +00001141 _lazyBindingInfo.bytes(), _lazyBindingInfo.size());
1142}
1143
Nick Kledzik141330a2014-09-03 19:52:50 +00001144void MachOFileLayout::writeExportInfo() {
1145 memcpy(&_buffer[_startOfExportTrie], _exportTrie.bytes(), _exportTrie.size());
1146}
1147
Nick Kledzike34182f2013-11-06 21:36:55 +00001148void MachOFileLayout::buildLinkEditInfo() {
1149 buildRebaseInfo();
1150 buildBindInfo();
1151 buildLazyBindInfo();
Nick Kledzik141330a2014-09-03 19:52:50 +00001152 buildExportTrie();
Nick Kledzike34182f2013-11-06 21:36:55 +00001153 computeSymbolTableSizes();
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001154 computeFunctionStartsSize();
Nick Kledzik21921372014-07-24 23:06:56 +00001155 computeDataInCodeSize();
Nick Kledzike34182f2013-11-06 21:36:55 +00001156}
1157
1158void MachOFileLayout::buildSectionRelocations() {
1159
1160}
1161
1162void MachOFileLayout::buildRebaseInfo() {
1163 // TODO: compress rebasing info.
1164 for (const RebaseLocation& entry : _file.rebasingInfo) {
1165 _rebaseInfo.append_byte(REBASE_OPCODE_SET_TYPE_IMM | entry.kind);
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001166 _rebaseInfo.append_byte(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
Nick Kledzike34182f2013-11-06 21:36:55 +00001167 | entry.segIndex);
1168 _rebaseInfo.append_uleb128(entry.segOffset);
1169 _rebaseInfo.append_uleb128(REBASE_OPCODE_DO_REBASE_IMM_TIMES | 1);
1170 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001171 _rebaseInfo.append_byte(REBASE_OPCODE_DONE);
Nick Kledzike34182f2013-11-06 21:36:55 +00001172 _rebaseInfo.align(_is64 ? 8 : 4);
1173}
1174
1175void MachOFileLayout::buildBindInfo() {
1176 // TODO: compress bind info.
Nick Kledzikf373c772014-11-11 01:31:18 +00001177 uint64_t lastAddend = 0;
Pete Cooper21f475e2016-08-11 20:37:02 +00001178 int lastOrdinal = 0x80000000;
1179 StringRef lastSymbolName;
1180 BindType lastType = (BindType)0;
1181 Hex32 lastSegOffset = ~0U;
1182 uint8_t lastSegIndex = (uint8_t)~0U;
Nick Kledzike34182f2013-11-06 21:36:55 +00001183 for (const BindLocation& entry : _file.bindingInfo) {
Pete Cooper21f475e2016-08-11 20:37:02 +00001184 if (entry.ordinal != lastOrdinal) {
1185 if (entry.ordinal <= 0)
1186 _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
1187 (entry.ordinal & BIND_IMMEDIATE_MASK));
1188 else if (entry.ordinal <= BIND_IMMEDIATE_MASK)
1189 _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |
1190 entry.ordinal);
1191 else {
1192 _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
1193 _bindingInfo.append_uleb128(entry.ordinal);
1194 }
1195 lastOrdinal = entry.ordinal;
1196 }
1197
1198 if (lastSymbolName != entry.symbolName) {
1199 _bindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM);
1200 _bindingInfo.append_string(entry.symbolName);
1201 lastSymbolName = entry.symbolName;
1202 }
1203
1204 if (lastType != entry.kind) {
1205 _bindingInfo.append_byte(BIND_OPCODE_SET_TYPE_IMM | entry.kind);
1206 lastType = entry.kind;
1207 }
1208
1209 if (lastSegIndex != entry.segIndex || lastSegOffset != entry.segOffset) {
1210 _bindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
1211 | entry.segIndex);
1212 _bindingInfo.append_uleb128(entry.segOffset);
1213 lastSegIndex = entry.segIndex;
1214 lastSegOffset = entry.segOffset;
1215 }
Nick Kledzikf373c772014-11-11 01:31:18 +00001216 if (entry.addend != lastAddend) {
Nick Kledzike34182f2013-11-06 21:36:55 +00001217 _bindingInfo.append_byte(BIND_OPCODE_SET_ADDEND_SLEB);
1218 _bindingInfo.append_sleb128(entry.addend);
Nick Kledzikf373c772014-11-11 01:31:18 +00001219 lastAddend = entry.addend;
Nick Kledzike34182f2013-11-06 21:36:55 +00001220 }
1221 _bindingInfo.append_byte(BIND_OPCODE_DO_BIND);
1222 }
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001223 _bindingInfo.append_byte(BIND_OPCODE_DONE);
Nick Kledzike34182f2013-11-06 21:36:55 +00001224 _bindingInfo.align(_is64 ? 8 : 4);
1225}
1226
1227void MachOFileLayout::buildLazyBindInfo() {
1228 for (const BindLocation& entry : _file.lazyBindingInfo) {
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001229 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
Nick Kledzike34182f2013-11-06 21:36:55 +00001230 | entry.segIndex);
Pete Cooper1ed8f1f2016-08-11 20:59:27 +00001231 _lazyBindingInfo.append_uleb128(entry.segOffset);
1232 if (entry.ordinal <= 0)
Lang Hames5c692002015-09-28 20:25:14 +00001233 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
Pete Cooper1ed8f1f2016-08-11 20:59:27 +00001234 (entry.ordinal & BIND_IMMEDIATE_MASK));
1235 else if (entry.ordinal <= BIND_IMMEDIATE_MASK)
1236 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |
1237 entry.ordinal);
1238 else {
1239 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB);
1240 _lazyBindingInfo.append_uleb128(entry.ordinal);
1241 }
1242 // FIXME: We need to | the opcode here with flags.
Nick Kledzike34182f2013-11-06 21:36:55 +00001243 _lazyBindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM);
1244 _lazyBindingInfo.append_string(entry.symbolName);
1245 _lazyBindingInfo.append_byte(BIND_OPCODE_DO_BIND);
Nick Kledzikf373c772014-11-11 01:31:18 +00001246 _lazyBindingInfo.append_byte(BIND_OPCODE_DONE);
Nick Kledzike34182f2013-11-06 21:36:55 +00001247 }
Nick Kledzike34182f2013-11-06 21:36:55 +00001248 _lazyBindingInfo.align(_is64 ? 8 : 4);
1249}
1250
Pete Coopere420dd42016-01-25 21:50:54 +00001251void TrieNode::addSymbol(const Export& entry,
1252 BumpPtrAllocator &allocator,
1253 std::vector<TrieNode*> &allNodes) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001254 StringRef partialStr = entry.name.drop_front(_cummulativeString.size());
1255 for (TrieEdge &edge : _children) {
1256 StringRef edgeStr = edge._subString;
1257 if (partialStr.startswith(edgeStr)) {
1258 // Already have matching edge, go down that path.
1259 edge._child->addSymbol(entry, allocator, allNodes);
1260 return;
1261 }
1262 // See if string has commmon prefix with existing edge.
1263 for (int n=edgeStr.size()-1; n > 0; --n) {
1264 if (partialStr.substr(0, n).equals(edgeStr.substr(0, n))) {
1265 // Splice in new node: was A -> C, now A -> B -> C
1266 StringRef bNodeStr = edge._child->_cummulativeString;
1267 bNodeStr = bNodeStr.drop_back(edgeStr.size()-n).copy(allocator);
Eugene Zelenko41547942015-11-10 22:37:38 +00001268 auto *bNode = new (allocator) TrieNode(bNodeStr);
Nick Kledzik141330a2014-09-03 19:52:50 +00001269 allNodes.push_back(bNode);
1270 TrieNode* cNode = edge._child;
1271 StringRef abEdgeStr = edgeStr.substr(0,n).copy(allocator);
1272 StringRef bcEdgeStr = edgeStr.substr(n).copy(allocator);
1273 DEBUG_WITH_TYPE("trie-builder", llvm::dbgs()
1274 << "splice in TrieNode('" << bNodeStr
1275 << "') between edge '"
1276 << abEdgeStr << "' and edge='"
1277 << bcEdgeStr<< "'\n");
1278 TrieEdge& abEdge = edge;
1279 abEdge._subString = abEdgeStr;
1280 abEdge._child = bNode;
Eugene Zelenko41547942015-11-10 22:37:38 +00001281 auto *bcEdge = new (allocator) TrieEdge(bcEdgeStr, cNode);
Pete Coopere420dd42016-01-25 21:50:54 +00001282 bNode->_children.insert(bNode->_children.end(), bcEdge);
Nick Kledzik141330a2014-09-03 19:52:50 +00001283 bNode->addSymbol(entry, allocator, allNodes);
1284 return;
1285 }
1286 }
1287 }
1288 if (entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
1289 assert(entry.otherOffset != 0);
1290 }
1291 if (entry.flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER) {
1292 assert(entry.otherOffset != 0);
1293 }
1294 // No commonality with any existing child, make a new edge.
Eugene Zelenko41547942015-11-10 22:37:38 +00001295 auto *newNode = new (allocator) TrieNode(entry.name.copy(allocator));
1296 auto *newEdge = new (allocator) TrieEdge(partialStr, newNode);
Pete Coopere420dd42016-01-25 21:50:54 +00001297 _children.insert(_children.end(), newEdge);
Nick Kledzik141330a2014-09-03 19:52:50 +00001298 DEBUG_WITH_TYPE("trie-builder", llvm::dbgs()
1299 << "new TrieNode('" << entry.name << "') with edge '"
1300 << partialStr << "' from node='"
1301 << _cummulativeString << "'\n");
1302 newNode->_address = entry.offset;
1303 newNode->_flags = entry.flags | entry.kind;
1304 newNode->_other = entry.otherOffset;
1305 if ((entry.flags & EXPORT_SYMBOL_FLAGS_REEXPORT) && !entry.otherName.empty())
1306 newNode->_importedName = entry.otherName.copy(allocator);
1307 newNode->_hasExportInfo = true;
1308 allNodes.push_back(newNode);
1309}
1310
Pete Cooperd0de3682016-08-05 21:37:12 +00001311void TrieNode::addOrderedNodes(const Export& entry,
1312 std::vector<TrieNode*> &orderedNodes) {
1313 if (!_ordered) {
1314 orderedNodes.push_back(this);
1315 _ordered = true;
1316 }
1317
1318 StringRef partialStr = entry.name.drop_front(_cummulativeString.size());
1319 for (TrieEdge &edge : _children) {
1320 StringRef edgeStr = edge._subString;
1321 if (partialStr.startswith(edgeStr)) {
1322 // Already have matching edge, go down that path.
1323 edge._child->addOrderedNodes(entry, orderedNodes);
1324 return;
1325 }
1326 }
1327}
1328
Pete Coopere420dd42016-01-25 21:50:54 +00001329bool TrieNode::updateOffset(uint32_t& offset) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001330 uint32_t nodeSize = 1; // Length when no export info
1331 if (_hasExportInfo) {
1332 if (_flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
1333 nodeSize = llvm::getULEB128Size(_flags);
1334 nodeSize += llvm::getULEB128Size(_other); // Other contains ordinal.
1335 nodeSize += _importedName.size();
1336 ++nodeSize; // Trailing zero in imported name.
1337 } else {
1338 nodeSize = llvm::getULEB128Size(_flags) + llvm::getULEB128Size(_address);
1339 if (_flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER)
1340 nodeSize += llvm::getULEB128Size(_other);
1341 }
1342 // Overall node size so far is uleb128 of export info + actual export info.
1343 nodeSize += llvm::getULEB128Size(nodeSize);
1344 }
1345 // Compute size of all child edges.
1346 ++nodeSize; // Byte for number of chidren.
1347 for (TrieEdge &edge : _children) {
1348 nodeSize += edge._subString.size() + 1 // String length.
1349 + llvm::getULEB128Size(edge._child->_trieOffset); // Offset len.
1350 }
1351 // On input, 'offset' is new prefered location for this node.
1352 bool result = (_trieOffset != offset);
1353 // Store new location in node object for use by parents.
1354 _trieOffset = offset;
1355 // Update offset for next iteration.
1356 offset += nodeSize;
1357 // Return true if _trieOffset was changed.
1358 return result;
1359}
1360
Pete Coopere420dd42016-01-25 21:50:54 +00001361void TrieNode::appendToByteBuffer(ByteBuffer &out) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001362 if (_hasExportInfo) {
1363 if (_flags & EXPORT_SYMBOL_FLAGS_REEXPORT) {
1364 if (!_importedName.empty()) {
1365 // nodes with re-export info: size, flags, ordinal, import-name
1366 uint32_t nodeSize = llvm::getULEB128Size(_flags)
1367 + llvm::getULEB128Size(_other)
1368 + _importedName.size() + 1;
1369 assert(nodeSize < 256);
1370 out.append_byte(nodeSize);
1371 out.append_uleb128(_flags);
1372 out.append_uleb128(_other);
1373 out.append_string(_importedName);
1374 } else {
1375 // nodes without re-export info: size, flags, ordinal, empty-string
1376 uint32_t nodeSize = llvm::getULEB128Size(_flags)
1377 + llvm::getULEB128Size(_other) + 1;
1378 assert(nodeSize < 256);
1379 out.append_byte(nodeSize);
1380 out.append_uleb128(_flags);
1381 out.append_uleb128(_other);
1382 out.append_byte(0);
1383 }
1384 } else if ( _flags & EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER ) {
1385 // Nodes with export info: size, flags, address, other
1386 uint32_t nodeSize = llvm::getULEB128Size(_flags)
1387 + llvm::getULEB128Size(_address)
1388 + llvm::getULEB128Size(_other);
1389 assert(nodeSize < 256);
1390 out.append_byte(nodeSize);
1391 out.append_uleb128(_flags);
1392 out.append_uleb128(_address);
1393 out.append_uleb128(_other);
1394 } else {
1395 // Nodes with export info: size, flags, address
1396 uint32_t nodeSize = llvm::getULEB128Size(_flags)
1397 + llvm::getULEB128Size(_address);
1398 assert(nodeSize < 256);
1399 out.append_byte(nodeSize);
1400 out.append_uleb128(_flags);
1401 out.append_uleb128(_address);
1402 }
1403 } else {
1404 // Node with no export info.
1405 uint32_t nodeSize = 0;
1406 out.append_byte(nodeSize);
1407 }
1408 // Add number of children.
1409 assert(_children.size() < 256);
1410 out.append_byte(_children.size());
1411 // Append each child edge substring and node offset.
1412 for (TrieEdge &edge : _children) {
1413 out.append_string(edge._subString);
1414 out.append_uleb128(edge._child->_trieOffset);
1415 }
1416}
1417
1418void MachOFileLayout::buildExportTrie() {
1419 if (_file.exportInfo.empty())
1420 return;
1421
1422 // For all temporary strings and objects used building trie.
1423 BumpPtrAllocator allocator;
1424
1425 // Build trie of all exported symbols.
Eugene Zelenko41547942015-11-10 22:37:38 +00001426 auto *rootNode = new (allocator) TrieNode(StringRef());
Nick Kledzik141330a2014-09-03 19:52:50 +00001427 std::vector<TrieNode*> allNodes;
1428 allNodes.reserve(_file.exportInfo.size()*2);
1429 allNodes.push_back(rootNode);
1430 for (const Export& entry : _file.exportInfo) {
1431 rootNode->addSymbol(entry, allocator, allNodes);
1432 }
1433
Pete Cooperd0de3682016-08-05 21:37:12 +00001434 std::vector<TrieNode*> orderedNodes;
1435 orderedNodes.reserve(allNodes.size());
1436
1437 for (const Export& entry : _file.exportInfo)
1438 rootNode->addOrderedNodes(entry, orderedNodes);
1439
Nick Kledzik141330a2014-09-03 19:52:50 +00001440 // Assign each node in the vector an offset in the trie stream, iterating
1441 // until all uleb128 sizes have stabilized.
1442 bool more;
1443 do {
1444 uint32_t offset = 0;
1445 more = false;
Pete Cooperd0de3682016-08-05 21:37:12 +00001446 for (TrieNode* node : orderedNodes) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001447 if (node->updateOffset(offset))
1448 more = true;
1449 }
1450 } while (more);
1451
1452 // Serialize trie to ByteBuffer.
Pete Cooperd0de3682016-08-05 21:37:12 +00001453 for (TrieNode* node : orderedNodes) {
Nick Kledzik141330a2014-09-03 19:52:50 +00001454 node->appendToByteBuffer(_exportTrie);
1455 }
1456 _exportTrie.align(_is64 ? 8 : 4);
1457}
1458
Nick Kledzike34182f2013-11-06 21:36:55 +00001459void MachOFileLayout::computeSymbolTableSizes() {
1460 // MachO symbol tables have three ranges: locals, globals, and undefines
1461 const size_t nlistSize = (_is64 ? sizeof(nlist_64) : sizeof(nlist));
Lang Hames436f7d62016-07-27 22:55:30 +00001462 _symbolTableSize = nlistSize * (_file.stabsSymbols.size()
1463 + _file.localSymbols.size()
Nick Kledzike34182f2013-11-06 21:36:55 +00001464 + _file.globalSymbols.size()
1465 + _file.undefinedSymbols.size());
Pete Cooper5559b242016-08-08 23:20:04 +00001466 // Always reserve 1-byte for the empty string and 1-byte for its terminator.
1467 _symbolStringPoolSize = 2;
Lang Hames436f7d62016-07-27 22:55:30 +00001468 for (const Symbol &sym : _file.stabsSymbols) {
1469 _symbolStringPoolSize += (sym.name.size()+1);
1470 }
Nick Kledzike34182f2013-11-06 21:36:55 +00001471 for (const Symbol &sym : _file.localSymbols) {
1472 _symbolStringPoolSize += (sym.name.size()+1);
1473 }
1474 for (const Symbol &sym : _file.globalSymbols) {
1475 _symbolStringPoolSize += (sym.name.size()+1);
1476 }
1477 for (const Symbol &sym : _file.undefinedSymbols) {
1478 _symbolStringPoolSize += (sym.name.size()+1);
1479 }
1480 _symbolTableLocalsStartIndex = 0;
Lang Hames436f7d62016-07-27 22:55:30 +00001481 _symbolTableGlobalsStartIndex = _file.stabsSymbols.size() +
1482 _file.localSymbols.size();
Shankar Easwaran3d8de472014-01-27 03:09:26 +00001483 _symbolTableUndefinesStartIndex = _symbolTableGlobalsStartIndex
Nick Kledzike34182f2013-11-06 21:36:55 +00001484 + _file.globalSymbols.size();
1485
1486 _indirectSymbolTableCount = 0;
1487 for (const Section &sect : _file.sections) {
1488 _indirectSymbolTableCount += sect.indirectSymbols.size();
1489 }
1490}
1491
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001492void MachOFileLayout::computeFunctionStartsSize() {
1493 _functionStartsSize = _file.functionStarts.size();
1494}
1495
Nick Kledzik21921372014-07-24 23:06:56 +00001496void MachOFileLayout::computeDataInCodeSize() {
1497 _dataInCodeSize = _file.dataInCode.size() * sizeof(data_in_code_entry);
1498}
Nick Kledzike34182f2013-11-06 21:36:55 +00001499
1500void MachOFileLayout::writeLinkEditContent() {
1501 if (_file.fileType == llvm::MachO::MH_OBJECT) {
1502 writeRelocations();
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001503 writeFunctionStartsInfo();
Nick Kledzik21921372014-07-24 23:06:56 +00001504 writeDataInCodeInfo();
Nick Kledzike34182f2013-11-06 21:36:55 +00001505 writeSymbolTable();
1506 } else {
1507 writeRebaseInfo();
1508 writeBindingInfo();
1509 writeLazyBindingInfo();
1510 // TODO: add weak binding info
Nick Kledzik141330a2014-09-03 19:52:50 +00001511 writeExportInfo();
Pete Cooper41f3e8e2016-02-09 01:38:13 +00001512 writeFunctionStartsInfo();
Nick Kledzik54ce29582014-10-28 22:21:10 +00001513 writeDataInCodeInfo();
Nick Kledzike34182f2013-11-06 21:36:55 +00001514 writeSymbolTable();
1515 }
1516}
1517
Pete Cooperfefbd222016-03-30 23:10:39 +00001518llvm::Error MachOFileLayout::writeBinary(StringRef path) {
Nick Kledzike34182f2013-11-06 21:36:55 +00001519 // Check for pending error from constructor.
1520 if (_ec)
Pete Cooperfefbd222016-03-30 23:10:39 +00001521 return llvm::errorCodeToError(_ec);
Nick Kledzike34182f2013-11-06 21:36:55 +00001522 // Create FileOutputBuffer with calculated size.
Nick Kledzike34182f2013-11-06 21:36:55 +00001523 unsigned flags = 0;
1524 if (_file.fileType != llvm::MachO::MH_OBJECT)
1525 flags = llvm::FileOutputBuffer::F_executable;
Rafael Espindolabdc8f2f2015-08-13 00:31:46 +00001526 ErrorOr<std::unique_ptr<llvm::FileOutputBuffer>> fobOrErr =
1527 llvm::FileOutputBuffer::create(path, size(), flags);
1528 if (std::error_code ec = fobOrErr.getError())
Pete Cooperfefbd222016-03-30 23:10:39 +00001529 return llvm::errorCodeToError(ec);
Rafael Espindolabdc8f2f2015-08-13 00:31:46 +00001530 std::unique_ptr<llvm::FileOutputBuffer> &fob = *fobOrErr;
Nick Kledzike34182f2013-11-06 21:36:55 +00001531 // Write content.
1532 _buffer = fob->getBufferStart();
1533 writeMachHeader();
Pete Cooper514594b2016-03-31 00:08:16 +00001534 if (auto ec = writeLoadCommands())
Pete Cooperdc59c792016-03-31 00:38:02 +00001535 return ec;
Nick Kledzike34182f2013-11-06 21:36:55 +00001536 writeSectionContent();
1537 writeLinkEditContent();
1538 fob->commit();
1539
Mehdi Aminic1edf562016-11-11 04:29:25 +00001540 return llvm::Error::success();
Nick Kledzike34182f2013-11-06 21:36:55 +00001541}
1542
Nick Kledzike34182f2013-11-06 21:36:55 +00001543/// Takes in-memory normalized view and writes a mach-o object file.
Pete Cooperfefbd222016-03-30 23:10:39 +00001544llvm::Error writeBinary(const NormalizedFile &file, StringRef path) {
Nick Kledzike34182f2013-11-06 21:36:55 +00001545 MachOFileLayout layout(file);
1546 return layout.writeBinary(path);
1547}
1548
Nick Kledzike34182f2013-11-06 21:36:55 +00001549} // namespace normalized
1550} // namespace mach_o
1551} // namespace lld