Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp ---------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | /// |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 10 | /// \file For mach-o object files, this implementation converts normalized |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 11 | /// mach-o in memory to mach-o binary on disk. |
| 12 | /// |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 13 | /// +---------------+ |
| 14 | /// | binary mach-o | |
| 15 | /// +---------------+ |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 16 | /// ^ |
| 17 | /// | |
| 18 | /// | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 19 | /// +------------+ |
| 20 | /// | normalized | |
| 21 | /// +------------+ |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 22 | |
| 23 | #include "MachONormalizedFile.h" |
| 24 | #include "MachONormalizedFileBinaryUtils.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 +0000 | [diff] [blame] | 25 | #include "lld/Common/LLVM.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 26 | #include "lld/Core/Error.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallString.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
| 29 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/ilist.h" |
| 31 | #include "llvm/ADT/ilist_node.h" |
| 32 | #include "llvm/BinaryFormat/MachO.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Casting.h" |
| 34 | #include "llvm/Support/Debug.h" |
Shankar Easwaran | 2b67fca | 2014-10-18 05:33:55 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Errc.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
| 37 | #include "llvm/Support/FileOutputBuffer.h" |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 38 | #include "llvm/Support/Format.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 39 | #include "llvm/Support/Host.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 40 | #include "llvm/Support/MemoryBuffer.h" |
| 41 | #include "llvm/Support/raw_ostream.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 42 | #include <functional> |
Nick Kledzik | 07ba512 | 2014-12-02 01:50:44 +0000 | [diff] [blame] | 43 | #include <list> |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 44 | #include <map> |
Rafael Espindola | 54427cc | 2014-06-12 17:15:58 +0000 | [diff] [blame] | 45 | #include <system_error> |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 46 | |
| 47 | using namespace llvm::MachO; |
| 48 | |
| 49 | namespace lld { |
| 50 | namespace mach_o { |
| 51 | namespace normalized { |
| 52 | |
Pete Cooper | e420dd4 | 2016-01-25 21:50:54 +0000 | [diff] [blame] | 53 | struct TrieNode; // Forward declaration. |
| 54 | |
| 55 | struct 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 | |
| 67 | namespace llvm { |
Duncan P. N. Exon Smith | 9f71057 | 2016-09-03 01:29:36 +0000 | [diff] [blame] | 68 | using lld::mach_o::normalized::TrieEdge; |
| 69 | template <> |
| 70 | struct ilist_alloc_traits<TrieEdge> : ilist_noalloc_traits<TrieEdge> {}; |
Pete Cooper | e420dd4 | 2016-01-25 21:50:54 +0000 | [diff] [blame] | 71 | } // namespace llvm |
| 72 | |
| 73 | |
| 74 | namespace lld { |
| 75 | namespace mach_o { |
| 76 | namespace normalized { |
| 77 | |
| 78 | struct 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 Cooper | d0de368 | 2016-08-05 21:37:12 +0000 | [diff] [blame] | 88 | |
| 89 | void addOrderedNodes(const Export &entry, |
| 90 | std::vector<TrieNode *> &allNodes); |
Pete Cooper | e420dd4 | 2016-01-25 21:50:54 +0000 | [diff] [blame] | 91 | bool updateOffset(uint32_t &offset); |
| 92 | void appendToByteBuffer(ByteBuffer &out); |
| 93 | |
| 94 | private: |
| 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 Cooper | d0de368 | 2016-08-05 21:37:12 +0000 | [diff] [blame] | 103 | bool _ordered = false; |
Pete Cooper | e420dd4 | 2016-01-25 21:50:54 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 106 | /// Utility class for writing a mach-o binary file given an in-memory |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 107 | /// normalized file. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 108 | class MachOFileLayout { |
| 109 | public: |
Joey Gouly | b275d7f | 2013-12-23 23:29:50 +0000 | [diff] [blame] | 110 | /// All layout computation is done in the constructor. |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 111 | MachOFileLayout(const NormalizedFile &file, bool alwaysIncludeFunctionStarts); |
Joey Gouly | b275d7f | 2013-12-23 23:29:50 +0000 | [diff] [blame] | 112 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 113 | /// Returns the final file size as computed in the constructor. |
| 114 | size_t size() const; |
| 115 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 116 | // Returns size of the mach_header and load commands. |
| 117 | size_t headerAndLoadCommandsSize() const; |
| 118 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 119 | /// Writes the normalized file as a binary mach-o file to the specified |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 120 | /// path. This does not have a stream interface because the generated |
| 121 | /// file may need the 'x' bit set. |
Pete Cooper | fefbd22 | 2016-03-30 23:10:39 +0000 | [diff] [blame] | 122 | llvm::Error writeBinary(StringRef path); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 123 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 124 | private: |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 125 | uint32_t loadCommandsSize(uint32_t &count, |
| 126 | bool alwaysIncludeFunctionStarts); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 127 | void buildFileOffsets(); |
| 128 | void writeMachHeader(); |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 129 | llvm::Error writeLoadCommands(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 130 | void writeSectionContent(); |
| 131 | void writeRelocations(); |
| 132 | void writeSymbolTable(); |
| 133 | void writeRebaseInfo(); |
| 134 | void writeBindingInfo(); |
| 135 | void writeLazyBindingInfo(); |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 136 | void writeExportInfo(); |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 137 | void writeFunctionStartsInfo(); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 138 | void writeDataInCodeInfo(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 139 | void writeLinkEditContent(); |
| 140 | void buildLinkEditInfo(); |
| 141 | void buildRebaseInfo(); |
| 142 | void buildBindInfo(); |
| 143 | void buildLazyBindInfo(); |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 144 | void buildExportTrie(); |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 145 | void computeFunctionStartsSize(); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 146 | void computeDataInCodeSize(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 147 | 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 §, uint32_t &index); |
| 152 | uint32_t indirectSymbolElementSize(const Section §); |
| 153 | |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 154 | // 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 167 | |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 168 | template <typename T> |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 169 | llvm::Error writeSingleSegmentLoadCommand(uint8_t *&lc); |
| 170 | template <typename T> llvm::Error writeSegmentLoadCommands(uint8_t *&lc); |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 171 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 172 | uint32_t pointerAlign(uint32_t value); |
| 173 | static StringRef dyldPath(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 174 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 175 | struct SegExtraInfo { |
| 176 | uint32_t fileOffset; |
Tim Northover | 08d6a7b | 2014-06-30 09:49:30 +0000 | [diff] [blame] | 177 | uint32_t fileSize; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 178 | 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 185 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 186 | const NormalizedFile &_file; |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 187 | std::error_code _ec; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 188 | 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 Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 197 | uint32_t _startOfFunctionStarts; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 198 | uint32_t _startOfDataInCode; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 199 | 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 Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 208 | uint32_t _functionStartsSize; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 209 | uint32_t _dataInCodeSize; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 210 | 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 Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 222 | uint32_t _startOfExportTrie; |
| 223 | uint32_t _endOfExportTrie; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 224 | 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 Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 232 | ByteBuffer _exportTrie; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 233 | }; |
| 234 | |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 235 | size_t headerAndLoadCommandsSize(const NormalizedFile &file, |
| 236 | bool includeFunctionStarts) { |
| 237 | MachOFileLayout layout(file, includeFunctionStarts); |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 238 | return layout.headerAndLoadCommandsSize(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | StringRef MachOFileLayout::dyldPath() { |
| 242 | return "/usr/lib/dyld"; |
| 243 | } |
| 244 | |
| 245 | uint32_t MachOFileLayout::pointerAlign(uint32_t value) { |
Rui Ueyama | 489a806 | 2016-01-14 20:53:50 +0000 | [diff] [blame] | 246 | return llvm::alignTo(value, _is64 ? 8 : 4); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 247 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 248 | |
| 249 | |
Nick Kledzik | 2fcbe82 | 2014-07-30 00:58:06 +0000 | [diff] [blame] | 250 | size_t MachOFileLayout::headerAndLoadCommandsSize() const { |
| 251 | return _endOfLoadCommands; |
| 252 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 253 | |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 254 | MachOFileLayout::MachOFileLayout(const NormalizedFile &file, |
| 255 | bool alwaysIncludeFunctionStarts) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 256 | : _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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 262 | const size_t segCommandBaseSize = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 263 | (_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 Cooper | ceee5de | 2016-02-04 02:16:08 +0000 | [diff] [blame] | 272 | if (file.hasMinVersionLoadCommand) { |
| 273 | _endOfLoadCommands += sizeof(version_min_command); |
| 274 | _countOfLoadCommands++; |
| 275 | } |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 276 | if (!_file.functionStarts.empty() || alwaysIncludeFunctionStarts) { |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 277 | _endOfLoadCommands += sizeof(linkedit_data_command); |
| 278 | _countOfLoadCommands++; |
| 279 | } |
Pete Cooper | 9b28a45 | 2016-02-09 02:10:39 +0000 | [diff] [blame] | 280 | if (_file.generateDataInCodeLoadCommand) { |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 281 | _endOfLoadCommands += sizeof(linkedit_data_command); |
| 282 | _countOfLoadCommands++; |
| 283 | } |
Nick Kledzik | b072c36 | 2014-11-18 00:30:29 +0000 | [diff] [blame] | 284 | // Assign file offsets to each section. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 285 | _startOfSectionsContent = _endOfLoadCommands; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 286 | unsigned relocCount = 0; |
Nick Kledzik | b072c36 | 2014-11-18 00:30:29 +0000 | [diff] [blame] | 287 | uint64_t offset = _startOfSectionsContent; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 288 | for (const Section § : file.sections) { |
Lang Hames | ac2adce | 2015-12-11 23:25:09 +0000 | [diff] [blame] | 289 | if (isZeroFillSection(sect.type)) |
| 290 | _sectInfo[§].fileOffset = 0; |
| 291 | else { |
Rui Ueyama | 489a806 | 2016-01-14 20:53:50 +0000 | [diff] [blame] | 292 | offset = llvm::alignTo(offset, sect.alignment); |
Nick Kledzik | b072c36 | 2014-11-18 00:30:29 +0000 | [diff] [blame] | 293 | _sectInfo[§].fileOffset = offset; |
| 294 | offset += sect.content.size(); |
Nick Kledzik | b072c36 | 2014-11-18 00:30:29 +0000 | [diff] [blame] | 295 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 296 | relocCount += sect.relocations.size(); |
| 297 | } |
Nick Kledzik | b072c36 | 2014-11-18 00:30:29 +0000 | [diff] [blame] | 298 | _endOfSectionsContent = offset; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 299 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 300 | computeSymbolTableSizes(); |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 301 | computeFunctionStartsSize(); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 302 | computeDataInCodeSize(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 303 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 304 | // Align start of relocations. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 305 | _startOfRelocations = pointerAlign(_endOfSectionsContent); |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 306 | _startOfFunctionStarts = _startOfRelocations + relocCount * 8; |
| 307 | _startOfDataInCode = _startOfFunctionStarts + _functionStartsSize; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 308 | _startOfSymbols = _startOfDataInCode + _dataInCodeSize; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 309 | // Add Indirect symbol table. |
| 310 | _startOfIndirectSymbols = _startOfSymbols + _symbolTableSize; |
| 311 | // Align start of symbol table and symbol strings. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 312 | _startOfSymbolStrings = _startOfIndirectSymbols |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 313 | + pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 314 | _endOfSymbolStrings = _startOfSymbolStrings |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 315 | + pointerAlign(_symbolStringPoolSize); |
| 316 | _endOfLinkEdit = _endOfSymbolStrings; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 317 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 318 | 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 330 | _endOfLoadCommands = _startOfLoadCommands |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 331 | + loadCommandsSize(_countOfLoadCommands, |
| 332 | alwaysIncludeFunctionStarts); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 333 | |
| 334 | // Assign section file offsets. |
| 335 | buildFileOffsets(); |
| 336 | buildLinkEditInfo(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 337 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 338 | // LINKEDIT of final linked images has in order: |
| 339 | // rebase info, binding info, lazy binding info, weak binding info, |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 340 | // data-in-code, symbol table, indirect symbol table, symbol table strings. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 341 | _startOfRebaseInfo = _startOfLinkEdit; |
| 342 | _endOfRebaseInfo = _startOfRebaseInfo + _rebaseInfo.size(); |
| 343 | _startOfBindingInfo = _endOfRebaseInfo; |
| 344 | _endOfBindingInfo = _startOfBindingInfo + _bindingInfo.size(); |
| 345 | _startOfLazyBindingInfo = _endOfBindingInfo; |
| 346 | _endOfLazyBindingInfo = _startOfLazyBindingInfo + _lazyBindingInfo.size(); |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 347 | _startOfExportTrie = _endOfLazyBindingInfo; |
| 348 | _endOfExportTrie = _startOfExportTrie + _exportTrie.size(); |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 349 | _startOfFunctionStarts = _endOfExportTrie; |
| 350 | _startOfDataInCode = _startOfFunctionStarts + _functionStartsSize; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 351 | _startOfSymbols = _startOfDataInCode + _dataInCodeSize; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 352 | _startOfIndirectSymbols = _startOfSymbols + _symbolTableSize; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 353 | _startOfSymbolStrings = _startOfIndirectSymbols |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 354 | + pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 355 | _endOfSymbolStrings = _startOfSymbolStrings |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 356 | + pointerAlign(_symbolStringPoolSize); |
| 357 | _endOfLinkEdit = _endOfSymbolStrings; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 358 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 359 | 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 Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 370 | << " startOfExportTrie=" << _startOfExportTrie << "\n" |
| 371 | << " endOfExportTrie=" << _endOfExportTrie << "\n" |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 372 | << " startOfFunctionStarts=" << _startOfFunctionStarts << "\n" |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 373 | << " startOfDataInCode=" << _startOfDataInCode << "\n" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 374 | << " startOfSymbols=" << _startOfSymbols << "\n" |
| 375 | << " startOfSymbolStrings=" << _startOfSymbolStrings << "\n" |
| 376 | << " endOfSymbolStrings=" << _endOfSymbolStrings << "\n" |
| 377 | << " addressOfLinkEdit=" << _addressOfLinkEdit << "\n"); |
| 378 | } |
| 379 | } |
| 380 | |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 381 | uint32_t MachOFileLayout::loadCommandsSize(uint32_t &count, |
| 382 | bool alwaysIncludeFunctionStarts) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 383 | uint32_t size = 0; |
| 384 | count = 0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 385 | |
| 386 | const size_t segCommandSize = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 387 | (_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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 395 | |
Tim Northover | 301c4e6 | 2014-07-01 08:15:41 +0000 | [diff] [blame] | 396 | // 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 402 | // Add LC_DYLD_INFO |
| 403 | size += sizeof(dyld_info_command); |
| 404 | ++count; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 405 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 406 | // Add LC_SYMTAB |
| 407 | size += sizeof(symtab_command); |
| 408 | ++count; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 409 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 410 | // Add LC_DYSYMTAB |
| 411 | if (_file.fileType != llvm::MachO::MH_PRELOAD) { |
| 412 | size += sizeof(dysymtab_command); |
| 413 | ++count; |
| 414 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 415 | |
Pete Cooper | 354809e | 2016-02-03 22:28:29 +0000 | [diff] [blame] | 416 | // If main executable add LC_LOAD_DYLINKER |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 417 | if (_file.fileType == llvm::MachO::MH_EXECUTE) { |
| 418 | size += pointerAlign(sizeof(dylinker_command) + dyldPath().size()+1); |
| 419 | ++count; |
Pete Cooper | 354809e | 2016-02-03 22:28:29 +0000 | [diff] [blame] | 420 | } |
| 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 Cooper | 40576fa | 2016-02-04 02:45:23 +0000 | [diff] [blame] | 429 | // Add LC_SOURCE_VERSION |
| 430 | size += sizeof(source_version_command); |
| 431 | ++count; |
| 432 | |
Pete Cooper | 354809e | 2016-02-03 22:28:29 +0000 | [diff] [blame] | 433 | // If main executable add LC_MAIN |
| 434 | if (_file.fileType == llvm::MachO::MH_EXECUTE) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 435 | size += sizeof(entry_point_command); |
| 436 | ++count; |
| 437 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 438 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 439 | // 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 444 | |
Jean-Daniel Dupas | 23dd15e | 2014-12-18 21:33:38 +0000 | [diff] [blame] | 445 | // Add LC_RPATH |
| 446 | for (const StringRef &path : _file.rpaths) { |
Lang Hames | 2ed3bf9 | 2015-10-29 16:50:26 +0000 | [diff] [blame] | 447 | size += pointerAlign(sizeof(rpath_command) + path.size() + 1); |
Jean-Daniel Dupas | 23dd15e | 2014-12-18 21:33:38 +0000 | [diff] [blame] | 448 | ++count; |
| 449 | } |
| 450 | |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 451 | // Add LC_FUNCTION_STARTS if needed |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 452 | if (!_file.functionStarts.empty() || alwaysIncludeFunctionStarts) { |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 453 | size += sizeof(linkedit_data_command); |
| 454 | ++count; |
| 455 | } |
| 456 | |
Pete Cooper | 9b28a45 | 2016-02-09 02:10:39 +0000 | [diff] [blame] | 457 | // 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 Kledzik | 54ce2958 | 2014-10-28 22:21:10 +0000 | [diff] [blame] | 460 | size += sizeof(linkedit_data_command); |
| 461 | ++count; |
| 462 | } |
| 463 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 464 | return size; |
| 465 | } |
| 466 | |
| 467 | static 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 | |
| 475 | static 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 | |
| 483 | void 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 Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 490 | _ec = make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 491 | return; |
| 492 | } |
| 493 | } |
| 494 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 495 | |
| 496 | // Verify no sections overlap |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 497 | 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 Espindola | 372bc70 | 2014-06-13 17:20:48 +0000 | [diff] [blame] | 502 | _ec = make_error_code(llvm::errc::executable_format_error); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 503 | return; |
| 504 | } |
| 505 | } |
| 506 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 507 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 508 | // 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 Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 519 | bool foundSegment = false; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 520 | for (const Segment &sg : _file.segments) { |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 521 | if (sg.name.equals(s.segmentName)) { |
| 522 | if ((s.address >= sg.address) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 523 | && (s.address+s.content.size() <= sg.address+sg.size)) { |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 524 | _segInfo[&sg].sections.push_back(&s); |
| 525 | foundSegment = true; |
| 526 | break; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 527 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 528 | } |
| 529 | } |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 530 | if (!foundSegment) { |
| 531 | _ec = make_error_code(llvm::errc::executable_format_error); |
| 532 | return; |
| 533 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 534 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 535 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 536 | // Assign file offsets. |
| 537 | uint32_t fileOffset = 0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 538 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 539 | llvm::dbgs() << "buildFileOffsets()\n"); |
| 540 | for (const Segment &sg : _file.segments) { |
Tim Northover | 08d6a7b | 2014-06-30 09:49:30 +0000 | [diff] [blame] | 541 | _segInfo[&sg].fileOffset = fileOffset; |
Pete Cooper | b8fec3e | 2016-02-06 00:51:16 +0000 | [diff] [blame] | 542 | if ((_seg1addr == INT64_MAX) && sg.init_access) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 543 | _seg1addr = sg.address; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 544 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 545 | llvm::dbgs() << " segment=" << sg.name |
| 546 | << ", fileOffset=" << _segInfo[&sg].fileOffset << "\n"); |
Tim Northover | 08d6a7b | 2014-06-30 09:49:30 +0000 | [diff] [blame] | 547 | |
| 548 | uint32_t segFileSize = 0; |
Nick Kledzik | 761d654 | 2014-10-24 22:19:22 +0000 | [diff] [blame] | 549 | // A segment that is not zero-fill must use a least one page of disk space. |
Pete Cooper | b8fec3e | 2016-02-06 00:51:16 +0000 | [diff] [blame] | 550 | if (sg.init_access) |
Nick Kledzik | 761d654 | 2014-10-24 22:19:22 +0000 | [diff] [blame] | 551 | segFileSize = _file.pageSize; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 552 | for (const Section *s : _segInfo[&sg].sections) { |
Tim Northover | 08d6a7b | 2014-06-30 09:49:30 +0000 | [diff] [blame] | 553 | uint32_t sectOffset = s->address - sg.address; |
| 554 | uint32_t sectFileSize = |
Lang Hames | ac2adce | 2015-12-11 23:25:09 +0000 | [diff] [blame] | 555 | isZeroFillSection(s->type) ? 0 : s->content.size(); |
Tim Northover | 08d6a7b | 2014-06-30 09:49:30 +0000 | [diff] [blame] | 556 | segFileSize = std::max(segFileSize, sectOffset + sectFileSize); |
| 557 | |
| 558 | _sectInfo[s].fileOffset = _segInfo[&sg].fileOffset + sectOffset; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 559 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 560 | llvm::dbgs() << " section=" << s->sectionName |
| 561 | << ", fileOffset=" << fileOffset << "\n"); |
| 562 | } |
Tim Northover | 08d6a7b | 2014-06-30 09:49:30 +0000 | [diff] [blame] | 563 | |
Pete Cooper | 353652f | 2016-02-06 00:14:15 +0000 | [diff] [blame] | 564 | // 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 569 | _addressOfLinkEdit = sg.address + sg.size; |
| 570 | } |
Tim Northover | 08d6a7b | 2014-06-30 09:49:30 +0000 | [diff] [blame] | 571 | _startOfLinkEdit = fileOffset; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 574 | size_t MachOFileLayout::size() const { |
| 575 | return _endOfSymbolStrings; |
| 576 | } |
| 577 | |
| 578 | void MachOFileLayout::writeMachHeader() { |
Pete Cooper | 8563e5a | 2016-02-04 20:43:43 +0000 | [diff] [blame] | 579 | 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 592 | 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 Cooper | 8563e5a | 2016-02-04 20:43:43 +0000 | [diff] [blame] | 595 | mh->cpusubtype = cpusubtype; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 596 | 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 604 | uint32_t MachOFileLayout::indirectSymbolIndex(const Section §, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 605 | 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 | |
| 613 | uint32_t MachOFileLayout::indirectSymbolElementSize(const Section §) { |
| 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 Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 621 | template <typename T> |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 622 | llvm::Error MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) { |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 623 | typename T::command* seg = reinterpret_cast<typename T::command*>(lc); |
| 624 | seg->cmd = T::LC; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 625 | seg->cmdsize = sizeof(typename T::command) |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 626 | + _file.sections.size() * sizeof(typename T::section); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 627 | uint8_t *next = lc + seg->cmdsize; |
| 628 | memset(seg->segname, 0, 16); |
| 629 | seg->vmaddr = 0; |
Nick Kledzik | b072c36 | 2014-11-18 00:30:29 +0000 | [diff] [blame] | 630 | seg->vmsize = _file.sections.back().address |
| 631 | + _file.sections.back().content.size(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 632 | seg->fileoff = _endOfLoadCommands; |
Lang Hames | 8c2406b | 2016-08-10 22:15:09 +0000 | [diff] [blame] | 633 | seg->filesize = _sectInfo[&_file.sections.back()].fileOffset + |
| 634 | _file.sections.back().content.size() - |
| 635 | _sectInfo[&_file.sections.front()].fileOffset; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 636 | 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 Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 642 | typename T::section *sout = reinterpret_cast<typename T::section*> |
| 643 | (lc+sizeof(typename T::command)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 644 | uint32_t relOffset = _startOfRelocations; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 645 | 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 Kledzik | b072c36 | 2014-11-18 00:30:29 +0000 | [diff] [blame] | 651 | sout->offset = _sectInfo[&sin].fileOffset; |
Rui Ueyama | f217ef0 | 2015-03-26 02:03:44 +0000 | [diff] [blame] | 652 | sout->align = llvm::Log2_32(sin.alignment); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 653 | 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 659 | if (_swap) |
| 660 | swapStruct(*sout); |
| 661 | ++sout; |
| 662 | } |
| 663 | lc = next; |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 664 | return llvm::Error::success(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 667 | template <typename T> |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 668 | llvm::Error MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 669 | uint32_t indirectSymRunningIndex = 0; |
| 670 | for (const Segment &seg : _file.segments) { |
Pete Cooper | 353652f | 2016-02-06 00:14:15 +0000 | [diff] [blame] | 671 | // Link edit has no sections and a custom range of address, so handle it |
| 672 | // specially. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 673 | SegExtraInfo &segInfo = _segInfo[&seg]; |
Pete Cooper | 353652f | 2016-02-06 00:14:15 +0000 | [diff] [blame] | 674 | 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 Cooper | b8fec3e | 2016-02-06 00:51:16 +0000 | [diff] [blame] | 685 | cmd->initprot = seg.init_access; |
| 686 | cmd->maxprot = seg.max_access; |
Pete Cooper | 353652f | 2016-02-06 00:14:15 +0000 | [diff] [blame] | 687 | 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 Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 695 | typename T::command* cmd = reinterpret_cast<typename T::command*>(lc); |
| 696 | cmd->cmd = T::LC; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 697 | cmd->cmdsize = sizeof(typename T::command) |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 698 | + segInfo.sections.size() * sizeof(typename T::section); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 699 | 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 Northover | 08d6a7b | 2014-06-30 09:49:30 +0000 | [diff] [blame] | 704 | cmd->filesize = segInfo.fileSize; |
Pete Cooper | b8fec3e | 2016-02-06 00:51:16 +0000 | [diff] [blame] | 705 | cmd->initprot = seg.init_access; |
| 706 | cmd->maxprot = seg.max_access; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 707 | cmd->nsects = segInfo.sections.size(); |
| 708 | cmd->flags = 0; |
| 709 | if (_swap) |
| 710 | swapStruct(*cmd); |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 711 | typename T::section *sect = reinterpret_cast<typename T::section*> |
| 712 | (lc+sizeof(typename T::command)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 713 | 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 Hames | ac2adce | 2015-12-11 23:25:09 +0000 | [diff] [blame] | 718 | if (isZeroFillSection(section->type)) |
Nick Kledzik | b072c36 | 2014-11-18 00:30:29 +0000 | [diff] [blame] | 719 | sect->offset = 0; |
| 720 | else |
| 721 | sect->offset = section->address - seg.address + segInfo.fileOffset; |
Rui Ueyama | f217ef0 | 2015-03-26 02:03:44 +0000 | [diff] [blame] | 722 | sect->align = llvm::Log2_32(section->alignment); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 723 | 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 731 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 732 | lc = reinterpret_cast<uint8_t*>(next); |
| 733 | } |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 734 | return llvm::Error::success(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Pete Cooper | ceee5de | 2016-02-04 02:16:08 +0000 | [diff] [blame] | 737 | static 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 Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 769 | llvm::Error MachOFileLayout::writeLoadCommands() { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 770 | 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 Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 773 | if (_is64) { |
| 774 | if (auto ec = writeSingleSegmentLoadCommand<MachO64Trait>(lc)) |
Pete Cooper | e487da1 | 2016-03-31 00:35:50 +0000 | [diff] [blame] | 775 | return ec; |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 776 | } else { |
| 777 | if (auto ec = writeSingleSegmentLoadCommand<MachO32Trait>(lc)) |
Pete Cooper | dc59c79 | 2016-03-31 00:38:02 +0000 | [diff] [blame] | 778 | return ec; |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 779 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 780 | // 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 Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame] | 785 | st->nsyms = _file.stabsSymbols.size() + _file.localSymbols.size() + |
| 786 | _file.globalSymbols.size() + _file.undefinedSymbols.size(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 787 | st->stroff = _startOfSymbolStrings; |
| 788 | st->strsize = _endOfSymbolStrings - _startOfSymbolStrings; |
| 789 | if (_swap) |
| 790 | swapStruct(*st); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 791 | lc += sizeof(symtab_command); |
Pete Cooper | ceee5de | 2016-02-04 02:16:08 +0000 | [diff] [blame] | 792 | |
| 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 Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 797 | // 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 Cooper | 9b28a45 | 2016-02-09 02:10:39 +0000 | [diff] [blame] | 809 | // Add LC_DATA_IN_CODE if requested. |
| 810 | if (_file.generateDataInCodeLoadCommand) { |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 811 | 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 820 | } else { |
| 821 | // Final linked images have sections under segments. |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 822 | if (_is64) { |
| 823 | if (auto ec = writeSegmentLoadCommands<MachO64Trait>(lc)) |
Pete Cooper | dc59c79 | 2016-03-31 00:38:02 +0000 | [diff] [blame] | 824 | return ec; |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 825 | } else { |
| 826 | if (auto ec = writeSegmentLoadCommands<MachO32Trait>(lc)) |
Pete Cooper | dc59c79 | 2016-03-31 00:38:02 +0000 | [diff] [blame] | 827 | return ec; |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 828 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 829 | |
Tim Northover | 301c4e6 | 2014-07-01 08:15:41 +0000 | [diff] [blame] | 830 | // 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 Dupas | edefccc | 2014-12-20 09:22:56 +0000 | [diff] [blame] | 838 | // needs to be some constant value different than the one in LC_LOAD_DYLIB |
| 839 | dc->dylib.timestamp = 1; |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 840 | dc->dylib.current_version = _file.currentVersion; |
| 841 | dc->dylib.compatibility_version = _file.compatVersion; |
Tim Northover | 301c4e6 | 2014-07-01 08:15:41 +0000 | [diff] [blame] | 842 | 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 849 | // 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 Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 861 | di->export_off = _exportTrie.size() ? _startOfExportTrie : 0; |
| 862 | di->export_size = _exportTrie.size(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 863 | 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 Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame] | 872 | st->nsyms = _file.stabsSymbols.size() + _file.localSymbols.size() + |
| 873 | _file.globalSymbols.size() + _file.undefinedSymbols.size(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 874 | st->stroff = _startOfSymbolStrings; |
| 875 | st->strsize = _endOfSymbolStrings - _startOfSymbolStrings; |
| 876 | if (_swap) |
| 877 | swapStruct(*st); |
| 878 | lc += sizeof(symtab_command); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 879 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 880 | // 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 Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame] | 886 | dst->nlocalsym = _file.stabsSymbols.size() + |
| 887 | _file.localSymbols.size(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 888 | 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 898 | dst->indirectsymoff = _startOfIndirectSymbols; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 899 | 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 908 | |
Pete Cooper | 354809e | 2016-02-03 22:28:29 +0000 | [diff] [blame] | 909 | // If main executable, add LC_LOAD_DYLINKER |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 910 | 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 Cooper | 354809e | 2016-02-03 22:28:29 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | // Add LC_VERSION_MIN_MACOSX, LC_VERSION_MIN_IPHONEOS, LC_VERSION_MIN_WATCHOS, |
| 925 | // LC_VERSION_MIN_TVOS |
Pete Cooper | ceee5de | 2016-02-04 02:16:08 +0000 | [diff] [blame] | 926 | writeVersionMinLoadCommand(_file, _swap, lc); |
Pete Cooper | 354809e | 2016-02-03 22:28:29 +0000 | [diff] [blame] | 927 | |
Pete Cooper | 40576fa | 2016-02-04 02:45:23 +0000 | [diff] [blame] | 928 | // Add LC_SOURCE_VERSION |
| 929 | { |
Pete Cooper | b565bdf | 2016-03-23 22:00:09 +0000 | [diff] [blame] | 930 | // 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 Cooper | 40576fa | 2016-02-04 02:45:23 +0000 | [diff] [blame] | 936 | if (_swap) |
Pete Cooper | b565bdf | 2016-03-23 22:00:09 +0000 | [diff] [blame] | 937 | swapStruct(sv); |
| 938 | memcpy(lc, &sv, sizeof(source_version_command)); |
Pete Cooper | 40576fa | 2016-02-04 02:45:23 +0000 | [diff] [blame] | 939 | lc += sizeof(source_version_command); |
| 940 | } |
| 941 | |
Pete Cooper | 354809e | 2016-02-03 22:28:29 +0000 | [diff] [blame] | 942 | // If main executable, add LC_MAIN. |
| 943 | if (_file.fileType == llvm::MachO::MH_EXECUTE) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 944 | // Build LC_MAIN load command. |
Pete Cooper | 07601d3 | 2016-03-24 01:05:17 +0000 | [diff] [blame] | 945 | // 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 952 | if (_swap) |
Pete Cooper | 07601d3 | 2016-03-24 01:05:17 +0000 | [diff] [blame] | 953 | swapStruct(ep); |
| 954 | memcpy(lc, &ep, sizeof(entry_point_command)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 955 | lc += sizeof(entry_point_command); |
| 956 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 957 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 958 | // 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 Kledzik | 5172067 | 2014-10-16 19:31:28 +0000 | [diff] [blame] | 962 | dc->cmd = dep.kind; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 963 | dc->cmdsize = size; |
| 964 | dc->dylib.name = sizeof(dylib_command); // offset |
Jean-Daniel Dupas | edefccc | 2014-12-20 09:22:56 +0000 | [diff] [blame] | 965 | // needs to be some constant value different than the one in LC_ID_DYLIB |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 966 | dc->dylib.timestamp = 2; |
| 967 | dc->dylib.current_version = dep.currentVersion; |
| 968 | dc->dylib.compatibility_version = dep.compatVersion; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 969 | 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 Dupas | 23dd15e | 2014-12-18 21:33:38 +0000 | [diff] [blame] | 975 | |
| 976 | // Add LC_RPATH |
| 977 | for (const StringRef &path : _file.rpaths) { |
| 978 | rpath_command *rpc = reinterpret_cast<rpath_command *>(lc); |
Lang Hames | 2ed3bf9 | 2015-10-29 16:50:26 +0000 | [diff] [blame] | 979 | uint32_t size = pointerAlign(sizeof(rpath_command) + path.size() + 1); |
Jean-Daniel Dupas | 23dd15e | 2014-12-18 21:33:38 +0000 | [diff] [blame] | 980 | 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 Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 990 | // 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 Cooper | 9b28a45 | 2016-02-09 02:10:39 +0000 | [diff] [blame] | 1002 | // Add LC_DATA_IN_CODE if requested. |
| 1003 | if (_file.generateDataInCodeLoadCommand) { |
Nick Kledzik | 54ce2958 | 2014-10-28 22:21:10 +0000 | [diff] [blame] | 1004 | 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1013 | } |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 1014 | assert(lc == &_buffer[_endOfLoadCommands]); |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 1015 | return llvm::Error::success(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1016 | } |
| 1017 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1018 | void MachOFileLayout::writeSectionContent() { |
| 1019 | for (const Section &s : _file.sections) { |
| 1020 | // Copy all section content to output buffer. |
Lang Hames | ac2adce | 2015-12-11 23:25:09 +0000 | [diff] [blame] | 1021 | if (isZeroFillSection(s.type)) |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame] | 1022 | continue; |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 1023 | if (s.content.empty()) |
| 1024 | continue; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1025 | uint32_t offset = _sectInfo[&s].fileOffset; |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 1026 | assert(offset >= _endOfLoadCommands); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1027 | uint8_t *p = &_buffer[offset]; |
| 1028 | memcpy(p, &s.content[0], s.content.size()); |
| 1029 | p += s.content.size(); |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | void 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1045 | void 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 Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 1075 | void MachOFileLayout::writeFunctionStartsInfo() { |
Pete Cooper | 8e1b9a1 | 2016-03-22 22:51:03 +0000 | [diff] [blame] | 1076 | if (!_functionStartsSize) |
| 1077 | return; |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 1078 | memcpy(&_buffer[_startOfFunctionStarts], _file.functionStarts.data(), |
| 1079 | _functionStartsSize); |
| 1080 | } |
| 1081 | |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 1082 | void 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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1096 | void MachOFileLayout::writeSymbolTable() { |
| 1097 | // Write symbol table and symbol strings in parallel. |
| 1098 | uint32_t symOffset = _startOfSymbols; |
| 1099 | uint32_t strOffset = _startOfSymbolStrings; |
Pete Cooper | 5559b24 | 2016-08-08 23:20:04 +0000 | [diff] [blame] | 1100 | // Reserve n_strx offset of zero to mean no name. |
| 1101 | _buffer[strOffset++] = ' '; |
| 1102 | _buffer[strOffset++] = '\0'; |
Lang Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame] | 1103 | appendSymbols(_file.stabsSymbols, symOffset, strOffset); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1104 | 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 §ion : _file.sections) { |
| 1113 | for (uint32_t index : section.indirectSymbols) { |
| 1114 | if (_swap) |
Artyom Skrobov | 17587fb | 2014-06-14 12:40:04 +0000 | [diff] [blame] | 1115 | *indirects++ = llvm::sys::getSwappedBytes(index); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1116 | 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 Skrobov | 17587fb | 2014-06-14 12:40:04 +0000 | [diff] [blame] | 1127 | *indirects++ = llvm::sys::getSwappedBytes(index); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1128 | else |
| 1129 | *indirects++ = index; |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | void MachOFileLayout::writeRebaseInfo() { |
| 1137 | memcpy(&_buffer[_startOfRebaseInfo], _rebaseInfo.bytes(), _rebaseInfo.size()); |
| 1138 | } |
| 1139 | |
| 1140 | void MachOFileLayout::writeBindingInfo() { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1141 | memcpy(&_buffer[_startOfBindingInfo], |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1142 | _bindingInfo.bytes(), _bindingInfo.size()); |
| 1143 | } |
| 1144 | |
| 1145 | void MachOFileLayout::writeLazyBindingInfo() { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1146 | memcpy(&_buffer[_startOfLazyBindingInfo], |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1147 | _lazyBindingInfo.bytes(), _lazyBindingInfo.size()); |
| 1148 | } |
| 1149 | |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1150 | void MachOFileLayout::writeExportInfo() { |
| 1151 | memcpy(&_buffer[_startOfExportTrie], _exportTrie.bytes(), _exportTrie.size()); |
| 1152 | } |
| 1153 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1154 | void MachOFileLayout::buildLinkEditInfo() { |
| 1155 | buildRebaseInfo(); |
| 1156 | buildBindInfo(); |
| 1157 | buildLazyBindInfo(); |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1158 | buildExportTrie(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1159 | computeSymbolTableSizes(); |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 1160 | computeFunctionStartsSize(); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 1161 | computeDataInCodeSize(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
| 1164 | void MachOFileLayout::buildSectionRelocations() { |
| 1165 | |
| 1166 | } |
| 1167 | |
| 1168 | void 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1172 | _rebaseInfo.append_byte(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1173 | | entry.segIndex); |
| 1174 | _rebaseInfo.append_uleb128(entry.segOffset); |
| 1175 | _rebaseInfo.append_uleb128(REBASE_OPCODE_DO_REBASE_IMM_TIMES | 1); |
| 1176 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1177 | _rebaseInfo.append_byte(REBASE_OPCODE_DONE); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1178 | _rebaseInfo.align(_is64 ? 8 : 4); |
| 1179 | } |
| 1180 | |
| 1181 | void MachOFileLayout::buildBindInfo() { |
| 1182 | // TODO: compress bind info. |
Nick Kledzik | f373c77 | 2014-11-11 01:31:18 +0000 | [diff] [blame] | 1183 | uint64_t lastAddend = 0; |
Pete Cooper | 21f475e | 2016-08-11 20:37:02 +0000 | [diff] [blame] | 1184 | int lastOrdinal = 0x80000000; |
| 1185 | StringRef lastSymbolName; |
| 1186 | BindType lastType = (BindType)0; |
| 1187 | Hex32 lastSegOffset = ~0U; |
| 1188 | uint8_t lastSegIndex = (uint8_t)~0U; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1189 | for (const BindLocation& entry : _file.bindingInfo) { |
Pete Cooper | 21f475e | 2016-08-11 20:37:02 +0000 | [diff] [blame] | 1190 | 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 Kledzik | f373c77 | 2014-11-11 01:31:18 +0000 | [diff] [blame] | 1222 | if (entry.addend != lastAddend) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1223 | _bindingInfo.append_byte(BIND_OPCODE_SET_ADDEND_SLEB); |
| 1224 | _bindingInfo.append_sleb128(entry.addend); |
Nick Kledzik | f373c77 | 2014-11-11 01:31:18 +0000 | [diff] [blame] | 1225 | lastAddend = entry.addend; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1226 | } |
| 1227 | _bindingInfo.append_byte(BIND_OPCODE_DO_BIND); |
| 1228 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1229 | _bindingInfo.append_byte(BIND_OPCODE_DONE); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1230 | _bindingInfo.align(_is64 ? 8 : 4); |
| 1231 | } |
| 1232 | |
| 1233 | void MachOFileLayout::buildLazyBindInfo() { |
| 1234 | for (const BindLocation& entry : _file.lazyBindingInfo) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1235 | _lazyBindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1236 | | entry.segIndex); |
Pete Cooper | 1ed8f1f | 2016-08-11 20:59:27 +0000 | [diff] [blame] | 1237 | _lazyBindingInfo.append_uleb128(entry.segOffset); |
| 1238 | if (entry.ordinal <= 0) |
Lang Hames | 5c69200 | 2015-09-28 20:25:14 +0000 | [diff] [blame] | 1239 | _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | |
Pete Cooper | 1ed8f1f | 2016-08-11 20:59:27 +0000 | [diff] [blame] | 1240 | (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 Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1249 | _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 Kledzik | f373c77 | 2014-11-11 01:31:18 +0000 | [diff] [blame] | 1252 | _lazyBindingInfo.append_byte(BIND_OPCODE_DONE); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1253 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1254 | _lazyBindingInfo.align(_is64 ? 8 : 4); |
| 1255 | } |
| 1256 | |
Pete Cooper | e420dd4 | 2016-01-25 21:50:54 +0000 | [diff] [blame] | 1257 | void TrieNode::addSymbol(const Export& entry, |
| 1258 | BumpPtrAllocator &allocator, |
| 1259 | std::vector<TrieNode*> &allNodes) { |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1260 | 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 Zelenko | 4154794 | 2015-11-10 22:37:38 +0000 | [diff] [blame] | 1274 | auto *bNode = new (allocator) TrieNode(bNodeStr); |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1275 | 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 Zelenko | 4154794 | 2015-11-10 22:37:38 +0000 | [diff] [blame] | 1287 | auto *bcEdge = new (allocator) TrieEdge(bcEdgeStr, cNode); |
Pete Cooper | e420dd4 | 2016-01-25 21:50:54 +0000 | [diff] [blame] | 1288 | bNode->_children.insert(bNode->_children.end(), bcEdge); |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1289 | 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 Zelenko | 4154794 | 2015-11-10 22:37:38 +0000 | [diff] [blame] | 1301 | auto *newNode = new (allocator) TrieNode(entry.name.copy(allocator)); |
| 1302 | auto *newEdge = new (allocator) TrieEdge(partialStr, newNode); |
Pete Cooper | e420dd4 | 2016-01-25 21:50:54 +0000 | [diff] [blame] | 1303 | _children.insert(_children.end(), newEdge); |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1304 | 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 Cooper | d0de368 | 2016-08-05 21:37:12 +0000 | [diff] [blame] | 1317 | void 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 Cooper | e420dd4 | 2016-01-25 21:50:54 +0000 | [diff] [blame] | 1335 | bool TrieNode::updateOffset(uint32_t& offset) { |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1336 | 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 Cooper | e420dd4 | 2016-01-25 21:50:54 +0000 | [diff] [blame] | 1367 | void TrieNode::appendToByteBuffer(ByteBuffer &out) { |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1368 | 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 | |
| 1424 | void 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 Zelenko | 4154794 | 2015-11-10 22:37:38 +0000 | [diff] [blame] | 1432 | auto *rootNode = new (allocator) TrieNode(StringRef()); |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1433 | 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 Cooper | d0de368 | 2016-08-05 21:37:12 +0000 | [diff] [blame] | 1440 | std::vector<TrieNode*> orderedNodes; |
| 1441 | orderedNodes.reserve(allNodes.size()); |
| 1442 | |
| 1443 | for (const Export& entry : _file.exportInfo) |
| 1444 | rootNode->addOrderedNodes(entry, orderedNodes); |
| 1445 | |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1446 | // 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 Cooper | d0de368 | 2016-08-05 21:37:12 +0000 | [diff] [blame] | 1452 | for (TrieNode* node : orderedNodes) { |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1453 | if (node->updateOffset(offset)) |
| 1454 | more = true; |
| 1455 | } |
| 1456 | } while (more); |
| 1457 | |
| 1458 | // Serialize trie to ByteBuffer. |
Pete Cooper | d0de368 | 2016-08-05 21:37:12 +0000 | [diff] [blame] | 1459 | for (TrieNode* node : orderedNodes) { |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1460 | node->appendToByteBuffer(_exportTrie); |
| 1461 | } |
| 1462 | _exportTrie.align(_is64 ? 8 : 4); |
| 1463 | } |
| 1464 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1465 | void 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 Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame] | 1468 | _symbolTableSize = nlistSize * (_file.stabsSymbols.size() |
| 1469 | + _file.localSymbols.size() |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1470 | + _file.globalSymbols.size() |
| 1471 | + _file.undefinedSymbols.size()); |
Pete Cooper | 5559b24 | 2016-08-08 23:20:04 +0000 | [diff] [blame] | 1472 | // Always reserve 1-byte for the empty string and 1-byte for its terminator. |
| 1473 | _symbolStringPoolSize = 2; |
Lang Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame] | 1474 | for (const Symbol &sym : _file.stabsSymbols) { |
| 1475 | _symbolStringPoolSize += (sym.name.size()+1); |
| 1476 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1477 | 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 Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame] | 1487 | _symbolTableGlobalsStartIndex = _file.stabsSymbols.size() + |
| 1488 | _file.localSymbols.size(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 1489 | _symbolTableUndefinesStartIndex = _symbolTableGlobalsStartIndex |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1490 | + _file.globalSymbols.size(); |
| 1491 | |
| 1492 | _indirectSymbolTableCount = 0; |
| 1493 | for (const Section § : _file.sections) { |
| 1494 | _indirectSymbolTableCount += sect.indirectSymbols.size(); |
| 1495 | } |
| 1496 | } |
| 1497 | |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 1498 | void MachOFileLayout::computeFunctionStartsSize() { |
| 1499 | _functionStartsSize = _file.functionStarts.size(); |
| 1500 | } |
| 1501 | |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 1502 | void MachOFileLayout::computeDataInCodeSize() { |
| 1503 | _dataInCodeSize = _file.dataInCode.size() * sizeof(data_in_code_entry); |
| 1504 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1505 | |
| 1506 | void MachOFileLayout::writeLinkEditContent() { |
| 1507 | if (_file.fileType == llvm::MachO::MH_OBJECT) { |
| 1508 | writeRelocations(); |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 1509 | writeFunctionStartsInfo(); |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 1510 | writeDataInCodeInfo(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1511 | writeSymbolTable(); |
| 1512 | } else { |
| 1513 | writeRebaseInfo(); |
| 1514 | writeBindingInfo(); |
| 1515 | writeLazyBindingInfo(); |
| 1516 | // TODO: add weak binding info |
Nick Kledzik | 141330a | 2014-09-03 19:52:50 +0000 | [diff] [blame] | 1517 | writeExportInfo(); |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 1518 | writeFunctionStartsInfo(); |
Nick Kledzik | 54ce2958 | 2014-10-28 22:21:10 +0000 | [diff] [blame] | 1519 | writeDataInCodeInfo(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1520 | writeSymbolTable(); |
| 1521 | } |
| 1522 | } |
| 1523 | |
Pete Cooper | fefbd22 | 2016-03-30 23:10:39 +0000 | [diff] [blame] | 1524 | llvm::Error MachOFileLayout::writeBinary(StringRef path) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1525 | // Check for pending error from constructor. |
| 1526 | if (_ec) |
Pete Cooper | fefbd22 | 2016-03-30 23:10:39 +0000 | [diff] [blame] | 1527 | return llvm::errorCodeToError(_ec); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1528 | // Create FileOutputBuffer with calculated size. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1529 | unsigned flags = 0; |
| 1530 | if (_file.fileType != llvm::MachO::MH_OBJECT) |
| 1531 | flags = llvm::FileOutputBuffer::F_executable; |
Rafael Espindola | f7a5729 | 2017-11-08 01:05:52 +0000 | [diff] [blame] | 1532 | Expected<std::unique_ptr<llvm::FileOutputBuffer>> fobOrErr = |
Rafael Espindola | bdc8f2f | 2015-08-13 00:31:46 +0000 | [diff] [blame] | 1533 | llvm::FileOutputBuffer::create(path, size(), flags); |
Rafael Espindola | f7a5729 | 2017-11-08 01:05:52 +0000 | [diff] [blame] | 1534 | if (Error E = fobOrErr.takeError()) |
| 1535 | return E; |
Rafael Espindola | bdc8f2f | 2015-08-13 00:31:46 +0000 | [diff] [blame] | 1536 | std::unique_ptr<llvm::FileOutputBuffer> &fob = *fobOrErr; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1537 | // Write content. |
| 1538 | _buffer = fob->getBufferStart(); |
| 1539 | writeMachHeader(); |
Pete Cooper | 514594b | 2016-03-31 00:08:16 +0000 | [diff] [blame] | 1540 | if (auto ec = writeLoadCommands()) |
Pete Cooper | dc59c79 | 2016-03-31 00:38:02 +0000 | [diff] [blame] | 1541 | return ec; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1542 | writeSectionContent(); |
| 1543 | writeLinkEditContent(); |
Rafael Espindola | 5f903f3 | 2017-11-08 01:50:34 +0000 | [diff] [blame] | 1544 | if (Error E = fob->commit()) |
| 1545 | return E; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1546 | |
Mehdi Amini | c1edf56 | 2016-11-11 04:29:25 +0000 | [diff] [blame] | 1547 | return llvm::Error::success(); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1550 | /// Takes in-memory normalized view and writes a mach-o object file. |
Pete Cooper | fefbd22 | 2016-03-30 23:10:39 +0000 | [diff] [blame] | 1551 | llvm::Error writeBinary(const NormalizedFile &file, StringRef path) { |
Rui Ueyama | 7f8ca6e | 2019-04-17 01:47:16 +0000 | [diff] [blame^] | 1552 | MachOFileLayout layout(file, false); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1553 | return layout.writeBinary(path); |
| 1554 | } |
| 1555 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1556 | } // namespace normalized |
| 1557 | } // namespace mach_o |
| 1558 | } // namespace lld |