Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp ---------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | /// |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 11 | /// \file For mach-o object files, this implementation converts normalized |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 12 | /// mach-o in memory to mach-o binary on disk. |
| 13 | /// |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 14 | /// +---------------+ |
| 15 | /// | binary mach-o | |
| 16 | /// +---------------+ |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 17 | /// ^ |
| 18 | /// | |
| 19 | /// | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 20 | /// +------------+ |
| 21 | /// | normalized | |
| 22 | /// +------------+ |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 23 | |
| 24 | #include "MachONormalizedFile.h" |
| 25 | #include "MachONormalizedFileBinaryUtils.h" |
| 26 | |
| 27 | #include "lld/Core/Error.h" |
| 28 | #include "lld/Core/LLVM.h" |
| 29 | |
| 30 | #include "llvm/ADT/SmallString.h" |
| 31 | #include "llvm/ADT/SmallVector.h" |
| 32 | #include "llvm/ADT/StringRef.h" |
| 33 | #include "llvm/Support/Casting.h" |
| 34 | #include "llvm/Support/Debug.h" |
| 35 | #include "llvm/Support/ErrorHandling.h" |
| 36 | #include "llvm/Support/FileOutputBuffer.h" |
| 37 | #include "llvm/Support/Host.h" |
Nick Kledzik | 00a15d9 | 2013-11-09 01:00:51 +0000 | [diff] [blame] | 38 | #include "llvm/Support/LEB128.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 39 | #include "llvm/Support/MachO.h" |
| 40 | #include "llvm/Support/MemoryBuffer.h" |
| 41 | #include "llvm/Support/raw_ostream.h" |
| 42 | #include "llvm/Support/system_error.h" |
| 43 | |
| 44 | #include <functional> |
| 45 | #include <map> |
| 46 | |
| 47 | using namespace llvm::MachO; |
| 48 | |
| 49 | namespace lld { |
| 50 | namespace mach_o { |
| 51 | namespace normalized { |
| 52 | |
| 53 | /// 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] | 54 | /// normalized file. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 55 | class MachOFileLayout { |
| 56 | public: |
Joey Gouly | b275d7f | 2013-12-23 23:29:50 +0000 | [diff] [blame] | 57 | /// All layout computation is done in the constructor. |
| 58 | MachOFileLayout(const NormalizedFile &file); |
| 59 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 60 | /// Returns the final file size as computed in the constructor. |
| 61 | size_t size() const; |
| 62 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 63 | /// 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] | 64 | /// path. This does not have a stream interface because the generated |
| 65 | /// file may need the 'x' bit set. |
| 66 | error_code writeBinary(StringRef path); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 67 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 68 | private: |
| 69 | uint32_t loadCommandsSize(uint32_t &count); |
| 70 | void buildFileOffsets(); |
| 71 | void writeMachHeader(); |
| 72 | error_code writeLoadCommands(); |
| 73 | void writeSectionContent(); |
| 74 | void writeRelocations(); |
| 75 | void writeSymbolTable(); |
| 76 | void writeRebaseInfo(); |
| 77 | void writeBindingInfo(); |
| 78 | void writeLazyBindingInfo(); |
| 79 | void writeLinkEditContent(); |
| 80 | void buildLinkEditInfo(); |
| 81 | void buildRebaseInfo(); |
| 82 | void buildBindInfo(); |
| 83 | void buildLazyBindInfo(); |
| 84 | void computeSymbolTableSizes(); |
| 85 | void buildSectionRelocations(); |
| 86 | void appendSymbols(const std::vector<Symbol> &symbols, |
| 87 | uint32_t &symOffset, uint32_t &strOffset); |
| 88 | uint32_t indirectSymbolIndex(const Section §, uint32_t &index); |
| 89 | uint32_t indirectSymbolElementSize(const Section §); |
| 90 | |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 91 | // For use as template parameter to load command methods. |
| 92 | struct MachO64Trait { |
| 93 | typedef llvm::MachO::segment_command_64 command; |
| 94 | typedef llvm::MachO::section_64 section; |
| 95 | enum { LC = llvm::MachO::LC_SEGMENT_64 }; |
| 96 | }; |
| 97 | |
| 98 | // For use as template parameter to load command methods. |
| 99 | struct MachO32Trait { |
| 100 | typedef llvm::MachO::segment_command command; |
| 101 | typedef llvm::MachO::section section; |
| 102 | enum { LC = llvm::MachO::LC_SEGMENT }; |
| 103 | }; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 104 | |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 105 | template <typename T> |
| 106 | error_code writeSingleSegmentLoadCommand(uint8_t *&lc); |
| 107 | template <typename T> |
| 108 | error_code writeSegmentLoadCommands(uint8_t *&lc); |
| 109 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 110 | uint32_t pointerAlign(uint32_t value); |
| 111 | static StringRef dyldPath(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 112 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 113 | class ByteBuffer { |
| 114 | public: |
Nick Kledzik | 00a15d9 | 2013-11-09 01:00:51 +0000 | [diff] [blame] | 115 | ByteBuffer() : _ostream(_bytes) { } |
| 116 | void append_byte(uint8_t b) { |
| 117 | _ostream << b; |
| 118 | } |
| 119 | void append_uleb128(uint64_t value) { |
| 120 | llvm::encodeULEB128(value, _ostream); |
| 121 | } |
| 122 | void append_sleb128(int64_t value) { |
| 123 | llvm::encodeSLEB128(value, _ostream); |
| 124 | } |
| 125 | void append_string(StringRef str) { |
| 126 | _ostream << str; |
| 127 | append_byte(0); |
| 128 | } |
| 129 | void align(unsigned alignment) { |
| 130 | while ( (_ostream.tell() % alignment) != 0 ) |
| 131 | append_byte(0); |
| 132 | } |
| 133 | size_t size() { |
| 134 | return _ostream.tell(); |
| 135 | } |
| 136 | const uint8_t *bytes() { |
| 137 | return reinterpret_cast<const uint8_t*>(_ostream.str().data()); |
| 138 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 139 | private: |
Nick Kledzik | 00a15d9 | 2013-11-09 01:00:51 +0000 | [diff] [blame] | 140 | SmallVector<char, 128> _bytes; |
| 141 | // Stream ivar must be after SmallVector ivar to construct properly. |
| 142 | llvm::raw_svector_ostream _ostream; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 143 | }; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 144 | |
Nick Kledzik | 00a15d9 | 2013-11-09 01:00:51 +0000 | [diff] [blame] | 145 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 146 | struct SegExtraInfo { |
| 147 | uint32_t fileOffset; |
| 148 | std::vector<const Section*> sections; |
| 149 | }; |
| 150 | typedef std::map<const Segment*, SegExtraInfo> SegMap; |
| 151 | struct SectionExtraInfo { |
| 152 | uint32_t fileOffset; |
| 153 | }; |
| 154 | typedef std::map<const Section*, SectionExtraInfo> SectionMap; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 155 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 156 | const NormalizedFile &_file; |
| 157 | error_code _ec; |
| 158 | uint8_t *_buffer; |
| 159 | const bool _is64; |
| 160 | const bool _swap; |
| 161 | const bool _bigEndianArch; |
| 162 | uint64_t _seg1addr; |
| 163 | uint32_t _startOfLoadCommands; |
| 164 | uint32_t _countOfLoadCommands; |
| 165 | uint32_t _endOfLoadCommands; |
| 166 | uint32_t _startOfRelocations; |
| 167 | uint32_t _startOfSymbols; |
| 168 | uint32_t _startOfIndirectSymbols; |
| 169 | uint32_t _startOfSymbolStrings; |
| 170 | uint32_t _endOfSymbolStrings; |
| 171 | uint32_t _symbolTableLocalsStartIndex; |
| 172 | uint32_t _symbolTableGlobalsStartIndex; |
| 173 | uint32_t _symbolTableUndefinesStartIndex; |
| 174 | uint32_t _symbolStringPoolSize; |
| 175 | uint32_t _symbolTableSize; |
| 176 | uint32_t _indirectSymbolTableCount; |
| 177 | // Used in object file creation only |
| 178 | uint32_t _startOfSectionsContent; |
| 179 | uint32_t _endOfSectionsContent; |
| 180 | // Used in final linked image only |
| 181 | uint32_t _startOfLinkEdit; |
| 182 | uint32_t _startOfRebaseInfo; |
| 183 | uint32_t _endOfRebaseInfo; |
| 184 | uint32_t _startOfBindingInfo; |
| 185 | uint32_t _endOfBindingInfo; |
| 186 | uint32_t _startOfLazyBindingInfo; |
| 187 | uint32_t _endOfLazyBindingInfo; |
| 188 | uint32_t _endOfLinkEdit; |
| 189 | uint64_t _addressOfLinkEdit; |
| 190 | SegMap _segInfo; |
| 191 | SectionMap _sectInfo; |
| 192 | ByteBuffer _rebaseInfo; |
| 193 | ByteBuffer _bindingInfo; |
| 194 | ByteBuffer _lazyBindingInfo; |
| 195 | ByteBuffer _weakBindingInfo; |
| 196 | ByteBuffer _exportInfo; |
| 197 | }; |
| 198 | |
| 199 | size_t headerAndLoadCommandsSize(const NormalizedFile &file) { |
| 200 | MachOFileLayout layout(file); |
| 201 | return layout.size(); |
| 202 | } |
| 203 | |
| 204 | StringRef MachOFileLayout::dyldPath() { |
| 205 | return "/usr/lib/dyld"; |
| 206 | } |
| 207 | |
| 208 | uint32_t MachOFileLayout::pointerAlign(uint32_t value) { |
| 209 | return llvm::RoundUpToAlignment(value, _is64 ? 8 : 4); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 210 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 211 | |
| 212 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 213 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 214 | |
| 215 | MachOFileLayout::MachOFileLayout(const NormalizedFile &file) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 216 | : _file(file), |
| 217 | _is64(MachOLinkingContext::is64Bit(file.arch)), |
| 218 | _swap(!MachOLinkingContext::isHostEndian(file.arch)), |
| 219 | _bigEndianArch(MachOLinkingContext::isBigEndian(file.arch)), |
| 220 | _seg1addr(INT64_MAX) { |
| 221 | _startOfLoadCommands = _is64 ? sizeof(mach_header_64) : sizeof(mach_header); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 222 | const size_t segCommandBaseSize = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 223 | (_is64 ? sizeof(segment_command_64) : sizeof(segment_command)); |
| 224 | const size_t sectsSize = (_is64 ? sizeof(section_64) : sizeof(section)); |
| 225 | if (file.fileType == llvm::MachO::MH_OBJECT) { |
| 226 | // object files have just one segment load command containing all sections |
| 227 | _endOfLoadCommands = _startOfLoadCommands |
| 228 | + segCommandBaseSize |
| 229 | + file.sections.size() * sectsSize |
| 230 | + sizeof(symtab_command); |
| 231 | _countOfLoadCommands = 2; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 232 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 233 | // Accumulate size of each section. |
| 234 | _startOfSectionsContent = _endOfLoadCommands; |
| 235 | _endOfSectionsContent = _startOfSectionsContent; |
| 236 | unsigned relocCount = 0; |
| 237 | for (const Section § : file.sections) { |
| 238 | _sectInfo[§].fileOffset = _endOfSectionsContent; |
| 239 | _endOfSectionsContent += sect.content.size(); |
| 240 | relocCount += sect.relocations.size(); |
| 241 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 242 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 243 | computeSymbolTableSizes(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 244 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 245 | // Align start of relocations. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 246 | _startOfRelocations = pointerAlign(_endOfSectionsContent); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 247 | _startOfSymbols = _startOfRelocations + relocCount * 8; |
| 248 | // Add Indirect symbol table. |
| 249 | _startOfIndirectSymbols = _startOfSymbols + _symbolTableSize; |
| 250 | // Align start of symbol table and symbol strings. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 251 | _startOfSymbolStrings = _startOfIndirectSymbols |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 252 | + pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 253 | _endOfSymbolStrings = _startOfSymbolStrings |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 254 | + pointerAlign(_symbolStringPoolSize); |
| 255 | _endOfLinkEdit = _endOfSymbolStrings; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 256 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 257 | llvm::dbgs() << "MachOFileLayout()\n" |
| 258 | << " startOfLoadCommands=" << _startOfLoadCommands << "\n" |
| 259 | << " countOfLoadCommands=" << _countOfLoadCommands << "\n" |
| 260 | << " endOfLoadCommands=" << _endOfLoadCommands << "\n" |
| 261 | << " startOfRelocations=" << _startOfRelocations << "\n" |
| 262 | << " startOfSymbols=" << _startOfSymbols << "\n" |
| 263 | << " startOfSymbolStrings=" << _startOfSymbolStrings << "\n" |
| 264 | << " endOfSymbolStrings=" << _endOfSymbolStrings << "\n" |
| 265 | << " startOfSectionsContent=" << _startOfSectionsContent << "\n" |
| 266 | << " endOfSectionsContent=" << _endOfSectionsContent << "\n"); |
| 267 | } else { |
| 268 | // Final linked images have one load command per segment. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 269 | _endOfLoadCommands = _startOfLoadCommands |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 270 | + loadCommandsSize(_countOfLoadCommands); |
| 271 | |
| 272 | // Assign section file offsets. |
| 273 | buildFileOffsets(); |
| 274 | buildLinkEditInfo(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 275 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 276 | // LINKEDIT of final linked images has in order: |
| 277 | // rebase info, binding info, lazy binding info, weak binding info, |
| 278 | // indirect symbol table, symbol table, symbol table strings. |
| 279 | _startOfRebaseInfo = _startOfLinkEdit; |
| 280 | _endOfRebaseInfo = _startOfRebaseInfo + _rebaseInfo.size(); |
| 281 | _startOfBindingInfo = _endOfRebaseInfo; |
| 282 | _endOfBindingInfo = _startOfBindingInfo + _bindingInfo.size(); |
| 283 | _startOfLazyBindingInfo = _endOfBindingInfo; |
| 284 | _endOfLazyBindingInfo = _startOfLazyBindingInfo + _lazyBindingInfo.size(); |
| 285 | |
| 286 | _startOfSymbols = _endOfLazyBindingInfo; |
| 287 | _startOfIndirectSymbols = _startOfSymbols + _symbolTableSize; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 288 | _startOfSymbolStrings = _startOfIndirectSymbols |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 289 | + pointerAlign(_indirectSymbolTableCount * sizeof(uint32_t)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 290 | _endOfSymbolStrings = _startOfSymbolStrings |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 291 | + pointerAlign(_symbolStringPoolSize); |
| 292 | _endOfLinkEdit = _endOfSymbolStrings; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 293 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 294 | llvm::dbgs() << "MachOFileLayout()\n" |
| 295 | << " startOfLoadCommands=" << _startOfLoadCommands << "\n" |
| 296 | << " countOfLoadCommands=" << _countOfLoadCommands << "\n" |
| 297 | << " endOfLoadCommands=" << _endOfLoadCommands << "\n" |
| 298 | << " startOfLinkEdit=" << _startOfLinkEdit << "\n" |
| 299 | << " startOfRebaseInfo=" << _startOfRebaseInfo << "\n" |
| 300 | << " endOfRebaseInfo=" << _endOfRebaseInfo << "\n" |
| 301 | << " startOfBindingInfo=" << _startOfBindingInfo << "\n" |
| 302 | << " endOfBindingInfo=" << _endOfBindingInfo << "\n" |
| 303 | << " startOfLazyBindingInfo=" << _startOfLazyBindingInfo << "\n" |
| 304 | << " endOfLazyBindingInfo=" << _endOfLazyBindingInfo << "\n" |
| 305 | << " startOfSymbols=" << _startOfSymbols << "\n" |
| 306 | << " startOfSymbolStrings=" << _startOfSymbolStrings << "\n" |
| 307 | << " endOfSymbolStrings=" << _endOfSymbolStrings << "\n" |
| 308 | << " addressOfLinkEdit=" << _addressOfLinkEdit << "\n"); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | uint32_t MachOFileLayout::loadCommandsSize(uint32_t &count) { |
| 313 | uint32_t size = 0; |
| 314 | count = 0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 315 | |
| 316 | const size_t segCommandSize = |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 317 | (_is64 ? sizeof(segment_command_64) : sizeof(segment_command)); |
| 318 | const size_t sectionSize = (_is64 ? sizeof(section_64) : sizeof(section)); |
| 319 | |
| 320 | // Add LC_SEGMENT for each segment. |
| 321 | size += _file.segments.size() * segCommandSize; |
| 322 | count += _file.segments.size(); |
| 323 | // Add section record for each section. |
| 324 | size += _file.sections.size() * sectionSize; |
| 325 | // Add one LC_SEGMENT for implicit __LINKEDIT segment |
| 326 | size += segCommandSize; |
| 327 | ++count; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 328 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 329 | // Add LC_DYLD_INFO |
| 330 | size += sizeof(dyld_info_command); |
| 331 | ++count; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 332 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 333 | // Add LC_SYMTAB |
| 334 | size += sizeof(symtab_command); |
| 335 | ++count; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 336 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 337 | // Add LC_DYSYMTAB |
| 338 | if (_file.fileType != llvm::MachO::MH_PRELOAD) { |
| 339 | size += sizeof(dysymtab_command); |
| 340 | ++count; |
| 341 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 342 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 343 | // If main executable add LC_LOAD_DYLINKER and LC_MAIN |
| 344 | if (_file.fileType == llvm::MachO::MH_EXECUTE) { |
| 345 | size += pointerAlign(sizeof(dylinker_command) + dyldPath().size()+1); |
| 346 | ++count; |
| 347 | size += sizeof(entry_point_command); |
| 348 | ++count; |
| 349 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 350 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 351 | // Add LC_LOAD_DYLIB for each dependent dylib. |
| 352 | for (const DependentDylib &dep : _file.dependentDylibs) { |
| 353 | size += sizeof(dylib_command) + pointerAlign(dep.path.size()+1); |
| 354 | ++count; |
| 355 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 356 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 357 | return size; |
| 358 | } |
| 359 | |
| 360 | static bool overlaps(const Segment &s1, const Segment &s2) { |
| 361 | if (s2.address >= s1.address+s1.size) |
| 362 | return false; |
| 363 | if (s1.address >= s2.address+s2.size) |
| 364 | return false; |
| 365 | return true; |
| 366 | } |
| 367 | |
| 368 | static bool overlaps(const Section &s1, const Section &s2) { |
| 369 | if (s2.address >= s1.address+s1.content.size()) |
| 370 | return false; |
| 371 | if (s1.address >= s2.address+s2.content.size()) |
| 372 | return false; |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | void MachOFileLayout::buildFileOffsets() { |
| 377 | // Verify no segments overlap |
| 378 | for (const Segment &sg1 : _file.segments) { |
| 379 | for (const Segment &sg2 : _file.segments) { |
| 380 | if (&sg1 == &sg2) |
| 381 | continue; |
| 382 | if (overlaps(sg1,sg2)) { |
| 383 | _ec = llvm::make_error_code(llvm::errc::executable_format_error); |
| 384 | return; |
| 385 | } |
| 386 | } |
| 387 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 388 | |
| 389 | // Verify no sections overlap |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 390 | for (const Section &s1 : _file.sections) { |
| 391 | for (const Section &s2 : _file.sections) { |
| 392 | if (&s1 == &s2) |
| 393 | continue; |
| 394 | if (overlaps(s1,s2)) { |
| 395 | _ec = llvm::make_error_code(llvm::errc::executable_format_error); |
| 396 | return; |
| 397 | } |
| 398 | } |
| 399 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 400 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 401 | // Build side table of extra info about segments and sections. |
| 402 | SegExtraInfo t; |
| 403 | t.fileOffset = 0; |
| 404 | for (const Segment &sg : _file.segments) { |
| 405 | _segInfo[&sg] = t; |
| 406 | } |
| 407 | SectionExtraInfo t2; |
| 408 | t2.fileOffset = 0; |
| 409 | // Assign sections to segments. |
| 410 | for (const Section &s : _file.sections) { |
| 411 | _sectInfo[&s] = t2; |
| 412 | for (const Segment &sg : _file.segments) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 413 | if ((s.address >= sg.address) |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 414 | && (s.address+s.content.size() <= sg.address+sg.size)) { |
| 415 | if (!sg.name.equals(s.segmentName)) { |
| 416 | _ec = llvm::make_error_code(llvm::errc::executable_format_error); |
| 417 | return; |
| 418 | } |
| 419 | _segInfo[&sg].sections.push_back(&s); |
| 420 | } |
| 421 | } |
| 422 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 423 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 424 | // Assign file offsets. |
| 425 | uint32_t fileOffset = 0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 426 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 427 | llvm::dbgs() << "buildFileOffsets()\n"); |
| 428 | for (const Segment &sg : _file.segments) { |
Alp Toker | 32e8bef | 2013-12-01 23:51:36 +0000 | [diff] [blame] | 429 | // FIXME: 4096 should be inferred from segments in normalized file. |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 430 | _segInfo[&sg].fileOffset = llvm::RoundUpToAlignment(fileOffset, 4096); |
| 431 | if ((_seg1addr == INT64_MAX) && sg.access) |
| 432 | _seg1addr = sg.address; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 433 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 434 | llvm::dbgs() << " segment=" << sg.name |
| 435 | << ", fileOffset=" << _segInfo[&sg].fileOffset << "\n"); |
| 436 | for (const Section *s : _segInfo[&sg].sections) { |
| 437 | fileOffset = s->address - sg.address + _segInfo[&sg].fileOffset; |
| 438 | _sectInfo[s].fileOffset = fileOffset; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 439 | DEBUG_WITH_TYPE("MachOFileLayout", |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 440 | llvm::dbgs() << " section=" << s->sectionName |
| 441 | << ", fileOffset=" << fileOffset << "\n"); |
| 442 | } |
| 443 | _addressOfLinkEdit = sg.address + sg.size; |
| 444 | } |
| 445 | _startOfLinkEdit = llvm::RoundUpToAlignment(fileOffset, 4096); |
| 446 | } |
| 447 | |
| 448 | |
| 449 | size_t MachOFileLayout::size() const { |
| 450 | return _endOfSymbolStrings; |
| 451 | } |
| 452 | |
| 453 | void MachOFileLayout::writeMachHeader() { |
| 454 | mach_header *mh = reinterpret_cast<mach_header*>(_buffer); |
| 455 | mh->magic = _is64 ? llvm::MachO::MH_MAGIC_64 : llvm::MachO::MH_MAGIC; |
| 456 | mh->cputype = MachOLinkingContext::cpuTypeFromArch(_file.arch); |
| 457 | mh->cpusubtype = MachOLinkingContext::cpuSubtypeFromArch(_file.arch); |
| 458 | mh->filetype = _file.fileType; |
| 459 | mh->ncmds = _countOfLoadCommands; |
| 460 | mh->sizeofcmds = _endOfLoadCommands - _startOfLoadCommands; |
| 461 | mh->flags = _file.flags; |
| 462 | if (_swap) |
| 463 | swapStruct(*mh); |
| 464 | } |
| 465 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 466 | uint32_t MachOFileLayout::indirectSymbolIndex(const Section §, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 467 | uint32_t &index) { |
| 468 | if (sect.indirectSymbols.empty()) |
| 469 | return 0; |
| 470 | uint32_t result = index; |
| 471 | index += sect.indirectSymbols.size(); |
| 472 | return result; |
| 473 | } |
| 474 | |
| 475 | uint32_t MachOFileLayout::indirectSymbolElementSize(const Section §) { |
| 476 | if (sect.indirectSymbols.empty()) |
| 477 | return 0; |
| 478 | if (sect.type != S_SYMBOL_STUBS) |
| 479 | return 0; |
| 480 | return sect.content.size() / sect.indirectSymbols.size(); |
| 481 | } |
| 482 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 483 | |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 484 | |
| 485 | template <typename T> |
| 486 | error_code MachOFileLayout::writeSingleSegmentLoadCommand(uint8_t *&lc) { |
| 487 | typename T::command* seg = reinterpret_cast<typename T::command*>(lc); |
| 488 | seg->cmd = T::LC; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 489 | seg->cmdsize = sizeof(typename T::command) |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 490 | + _file.sections.size() * sizeof(typename T::section); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 491 | uint8_t *next = lc + seg->cmdsize; |
| 492 | memset(seg->segname, 0, 16); |
| 493 | seg->vmaddr = 0; |
| 494 | seg->vmsize = _endOfSectionsContent - _endOfLoadCommands; |
| 495 | seg->fileoff = _endOfLoadCommands; |
| 496 | seg->filesize = seg->vmsize; |
| 497 | seg->maxprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE; |
| 498 | seg->initprot = VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE; |
| 499 | seg->nsects = _file.sections.size(); |
| 500 | seg->flags = 0; |
| 501 | if (_swap) |
| 502 | swapStruct(*seg); |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 503 | typename T::section *sout = reinterpret_cast<typename T::section*> |
| 504 | (lc+sizeof(typename T::command)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 505 | uint32_t relOffset = _startOfRelocations; |
| 506 | uint32_t contentOffset = _startOfSectionsContent; |
| 507 | uint32_t indirectSymRunningIndex = 0; |
| 508 | for (const Section &sin : _file.sections) { |
| 509 | setString16(sin.sectionName, sout->sectname); |
| 510 | setString16(sin.segmentName, sout->segname); |
| 511 | sout->addr = sin.address; |
| 512 | sout->size = sin.content.size(); |
| 513 | sout->offset = contentOffset; |
| 514 | sout->align = sin.alignment; |
| 515 | sout->reloff = sin.relocations.empty() ? 0 : relOffset; |
| 516 | sout->nreloc = sin.relocations.size(); |
| 517 | sout->flags = sin.type | sin.attributes; |
| 518 | sout->reserved1 = indirectSymbolIndex(sin, indirectSymRunningIndex); |
| 519 | sout->reserved2 = indirectSymbolElementSize(sin); |
| 520 | relOffset += sin.relocations.size() * sizeof(any_relocation_info); |
| 521 | contentOffset += sin.content.size(); |
| 522 | if (_swap) |
| 523 | swapStruct(*sout); |
| 524 | ++sout; |
| 525 | } |
| 526 | lc = next; |
| 527 | return error_code::success(); |
| 528 | } |
| 529 | |
| 530 | |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 531 | template <typename T> |
| 532 | error_code MachOFileLayout::writeSegmentLoadCommands(uint8_t *&lc) { |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 533 | uint32_t indirectSymRunningIndex = 0; |
| 534 | for (const Segment &seg : _file.segments) { |
| 535 | // Write segment command with trailing sections. |
| 536 | SegExtraInfo &segInfo = _segInfo[&seg]; |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 537 | typename T::command* cmd = reinterpret_cast<typename T::command*>(lc); |
| 538 | cmd->cmd = T::LC; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 539 | cmd->cmdsize = sizeof(typename T::command) |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 540 | + segInfo.sections.size() * sizeof(typename T::section); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 541 | uint8_t *next = lc + cmd->cmdsize; |
| 542 | setString16(seg.name, cmd->segname); |
| 543 | cmd->vmaddr = seg.address; |
| 544 | cmd->vmsize = seg.size; |
| 545 | cmd->fileoff = segInfo.fileOffset; |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 546 | cmd->filesize = seg.access ? seg.size : Hex64(0); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 547 | cmd->maxprot = seg.access; |
| 548 | cmd->initprot = seg.access; |
| 549 | cmd->nsects = segInfo.sections.size(); |
| 550 | cmd->flags = 0; |
| 551 | if (_swap) |
| 552 | swapStruct(*cmd); |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 553 | typename T::section *sect = reinterpret_cast<typename T::section*> |
| 554 | (lc+sizeof(typename T::command)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 555 | for (const Section *section : segInfo.sections) { |
| 556 | setString16(section->sectionName, sect->sectname); |
| 557 | setString16(section->segmentName, sect->segname); |
| 558 | sect->addr = section->address; |
| 559 | sect->size = section->content.size(); |
| 560 | sect->offset = section->address - seg.address + segInfo.fileOffset; |
| 561 | sect->align = section->alignment; |
| 562 | sect->reloff = 0; |
| 563 | sect->nreloc = 0; |
| 564 | sect->flags = section->type | section->attributes; |
| 565 | sect->reserved1 = indirectSymbolIndex(*section, indirectSymRunningIndex); |
| 566 | sect->reserved2 = indirectSymbolElementSize(*section); |
| 567 | if (_swap) |
| 568 | swapStruct(*sect); |
| 569 | ++sect; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 570 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 571 | lc = reinterpret_cast<uint8_t*>(next); |
| 572 | } |
| 573 | // Add implicit __LINKEDIT segment |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 574 | typename T::command* cmd = reinterpret_cast<typename T::command*>(lc); |
| 575 | cmd->cmd = T::LC; |
| 576 | cmd->cmdsize = sizeof(typename T::command); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 577 | uint8_t *next = lc + cmd->cmdsize; |
| 578 | setString16("__LINKEDIT", cmd->segname); |
| 579 | cmd->vmaddr = _addressOfLinkEdit; |
| 580 | cmd->vmsize = _endOfLinkEdit - _startOfLinkEdit; |
| 581 | cmd->fileoff = _startOfLinkEdit; |
| 582 | cmd->filesize = _endOfLinkEdit - _startOfLinkEdit; |
| 583 | cmd->maxprot = VM_PROT_READ; |
| 584 | cmd->initprot = VM_PROT_READ; |
| 585 | cmd->nsects = 0; |
| 586 | cmd->flags = 0; |
| 587 | if (_swap) |
| 588 | swapStruct(*cmd); |
| 589 | lc = next; |
| 590 | return error_code::success(); |
| 591 | } |
| 592 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 593 | |
| 594 | error_code MachOFileLayout::writeLoadCommands() { |
| 595 | error_code ec; |
| 596 | uint8_t *lc = &_buffer[_startOfLoadCommands]; |
| 597 | if (_file.fileType == llvm::MachO::MH_OBJECT) { |
| 598 | // Object files have one unnamed segment which holds all sections. |
| 599 | if (_is64) |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 600 | ec = writeSingleSegmentLoadCommand<MachO64Trait>(lc); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 601 | else |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 602 | ec = writeSingleSegmentLoadCommand<MachO32Trait>(lc); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 603 | // Add LC_SYMTAB with symbol table info |
| 604 | symtab_command* st = reinterpret_cast<symtab_command*>(lc); |
| 605 | st->cmd = LC_SYMTAB; |
| 606 | st->cmdsize = sizeof(symtab_command); |
| 607 | st->symoff = _startOfSymbols; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 608 | st->nsyms = _file.localSymbols.size() + _file.globalSymbols.size() |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 609 | + _file.undefinedSymbols.size(); |
| 610 | st->stroff = _startOfSymbolStrings; |
| 611 | st->strsize = _endOfSymbolStrings - _startOfSymbolStrings; |
| 612 | if (_swap) |
| 613 | swapStruct(*st); |
| 614 | } else { |
| 615 | // Final linked images have sections under segments. |
| 616 | if (_is64) |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 617 | ec = writeSegmentLoadCommands<MachO64Trait>(lc); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 618 | else |
Nick Kledzik | 29f749e | 2013-11-09 00:07:28 +0000 | [diff] [blame] | 619 | ec = writeSegmentLoadCommands<MachO32Trait>(lc); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 620 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 621 | // Add LC_DYLD_INFO_ONLY. |
| 622 | dyld_info_command* di = reinterpret_cast<dyld_info_command*>(lc); |
| 623 | di->cmd = LC_DYLD_INFO_ONLY; |
| 624 | di->cmdsize = sizeof(dyld_info_command); |
| 625 | di->rebase_off = _rebaseInfo.size() ? _startOfRebaseInfo : 0; |
| 626 | di->rebase_size = _rebaseInfo.size(); |
| 627 | di->bind_off = _bindingInfo.size() ? _startOfBindingInfo : 0; |
| 628 | di->bind_size = _bindingInfo.size(); |
| 629 | di->weak_bind_off = 0; |
| 630 | di->weak_bind_size = 0; |
| 631 | di->lazy_bind_off = _lazyBindingInfo.size() ? _startOfLazyBindingInfo : 0; |
| 632 | di->lazy_bind_size = _lazyBindingInfo.size(); |
| 633 | di->export_off = 0; |
| 634 | di->export_size = 0; |
| 635 | if (_swap) |
| 636 | swapStruct(*di); |
| 637 | lc += sizeof(dyld_info_command); |
| 638 | |
| 639 | // Add LC_SYMTAB with symbol table info. |
| 640 | symtab_command* st = reinterpret_cast<symtab_command*>(lc); |
| 641 | st->cmd = LC_SYMTAB; |
| 642 | st->cmdsize = sizeof(symtab_command); |
| 643 | st->symoff = _startOfSymbols; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 644 | st->nsyms = _file.localSymbols.size() + _file.globalSymbols.size() |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 645 | + _file.undefinedSymbols.size(); |
| 646 | st->stroff = _startOfSymbolStrings; |
| 647 | st->strsize = _endOfSymbolStrings - _startOfSymbolStrings; |
| 648 | if (_swap) |
| 649 | swapStruct(*st); |
| 650 | lc += sizeof(symtab_command); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 651 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 652 | // Add LC_DYSYMTAB |
| 653 | if (_file.fileType != llvm::MachO::MH_PRELOAD) { |
| 654 | dysymtab_command* dst = reinterpret_cast<dysymtab_command*>(lc); |
| 655 | dst->cmd = LC_DYSYMTAB; |
| 656 | dst->cmdsize = sizeof(dysymtab_command); |
| 657 | dst->ilocalsym = _symbolTableLocalsStartIndex; |
| 658 | dst->nlocalsym = _file.localSymbols.size(); |
| 659 | dst->iextdefsym = _symbolTableGlobalsStartIndex; |
| 660 | dst->nextdefsym = _file.globalSymbols.size(); |
| 661 | dst->iundefsym = _symbolTableUndefinesStartIndex; |
| 662 | dst->nundefsym = _file.undefinedSymbols.size(); |
| 663 | dst->tocoff = 0; |
| 664 | dst->ntoc = 0; |
| 665 | dst->modtaboff = 0; |
| 666 | dst->nmodtab = 0; |
| 667 | dst->extrefsymoff = 0; |
| 668 | dst->nextrefsyms = 0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 669 | dst->indirectsymoff = _startOfIndirectSymbols; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 670 | dst->nindirectsyms = _indirectSymbolTableCount; |
| 671 | dst->extreloff = 0; |
| 672 | dst->nextrel = 0; |
| 673 | dst->locreloff = 0; |
| 674 | dst->nlocrel = 0; |
| 675 | if (_swap) |
| 676 | swapStruct(*dst); |
| 677 | lc += sizeof(dysymtab_command); |
| 678 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 679 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 680 | // If main executable, add LC_LOAD_DYLINKER and LC_MAIN. |
| 681 | if (_file.fileType == llvm::MachO::MH_EXECUTE) { |
| 682 | // Build LC_LOAD_DYLINKER load command. |
| 683 | uint32_t size=pointerAlign(sizeof(dylinker_command)+dyldPath().size()+1); |
| 684 | dylinker_command* dl = reinterpret_cast<dylinker_command*>(lc); |
| 685 | dl->cmd = LC_LOAD_DYLINKER; |
| 686 | dl->cmdsize = size; |
| 687 | dl->name = sizeof(dylinker_command); // offset |
| 688 | if (_swap) |
| 689 | swapStruct(*dl); |
| 690 | memcpy(lc+sizeof(dylinker_command), dyldPath().data(), dyldPath().size()); |
| 691 | lc[sizeof(dylinker_command)+dyldPath().size()] = '\0'; |
| 692 | lc += size; |
| 693 | // Build LC_MAIN load command. |
| 694 | entry_point_command* ep = reinterpret_cast<entry_point_command*>(lc); |
| 695 | ep->cmd = LC_MAIN; |
| 696 | ep->cmdsize = sizeof(entry_point_command); |
| 697 | ep->entryoff = _file.entryAddress - _seg1addr; |
| 698 | ep->stacksize = 0; |
| 699 | if (_swap) |
| 700 | swapStruct(*ep); |
| 701 | lc += sizeof(entry_point_command); |
| 702 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 703 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 704 | // Add LC_LOAD_DYLIB commands |
| 705 | for (const DependentDylib &dep : _file.dependentDylibs) { |
| 706 | dylib_command* dc = reinterpret_cast<dylib_command*>(lc); |
| 707 | uint32_t size = sizeof(dylib_command) + pointerAlign(dep.path.size()+1); |
| 708 | dc->cmd = LC_LOAD_DYLIB; |
| 709 | dc->cmdsize = size; |
| 710 | dc->dylib.name = sizeof(dylib_command); // offset |
| 711 | dc->dylib.timestamp = 0; // FIXME |
| 712 | dc->dylib.current_version = 0; // FIXME |
| 713 | dc->dylib.compatibility_version = 0; // FIXME |
| 714 | if (_swap) |
| 715 | swapStruct(*dc); |
| 716 | memcpy(lc+sizeof(dylib_command), dep.path.begin(), dep.path.size()); |
| 717 | lc[sizeof(dylib_command)+dep.path.size()] = '\0'; |
| 718 | lc += size; |
| 719 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 720 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 721 | } |
| 722 | return ec; |
| 723 | } |
| 724 | |
| 725 | |
| 726 | void MachOFileLayout::writeSectionContent() { |
| 727 | for (const Section &s : _file.sections) { |
| 728 | // Copy all section content to output buffer. |
Nick Kledzik | 61fdef6 | 2014-05-15 20:59:23 +0000 | [diff] [blame^] | 729 | if (s.type == llvm::MachO::S_ZEROFILL) |
| 730 | continue; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 731 | uint32_t offset = _sectInfo[&s].fileOffset; |
| 732 | uint8_t *p = &_buffer[offset]; |
| 733 | memcpy(p, &s.content[0], s.content.size()); |
| 734 | p += s.content.size(); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | void MachOFileLayout::writeRelocations() { |
| 739 | uint32_t relOffset = _startOfRelocations; |
| 740 | for (Section sect : _file.sections) { |
| 741 | for (Relocation r : sect.relocations) { |
| 742 | any_relocation_info* rb = reinterpret_cast<any_relocation_info*>( |
| 743 | &_buffer[relOffset]); |
| 744 | *rb = packRelocation(r, _swap, _bigEndianArch); |
| 745 | relOffset += sizeof(any_relocation_info); |
| 746 | } |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | |
| 751 | void MachOFileLayout::appendSymbols(const std::vector<Symbol> &symbols, |
| 752 | uint32_t &symOffset, uint32_t &strOffset) { |
| 753 | for (const Symbol &sym : symbols) { |
| 754 | if (_is64) { |
| 755 | nlist_64* nb = reinterpret_cast<nlist_64*>(&_buffer[symOffset]); |
| 756 | nb->n_strx = strOffset - _startOfSymbolStrings; |
| 757 | nb->n_type = sym.type | sym.scope; |
| 758 | nb->n_sect = sym.sect; |
| 759 | nb->n_desc = sym.desc; |
| 760 | nb->n_value = sym.value; |
| 761 | if (_swap) |
| 762 | swapStruct(*nb); |
| 763 | symOffset += sizeof(nlist_64); |
| 764 | } else { |
| 765 | nlist* nb = reinterpret_cast<nlist*>(&_buffer[symOffset]); |
| 766 | nb->n_strx = strOffset - _startOfSymbolStrings; |
| 767 | nb->n_type = sym.type | sym.scope; |
| 768 | nb->n_sect = sym.sect; |
| 769 | nb->n_desc = sym.desc; |
| 770 | nb->n_value = sym.value; |
| 771 | if (_swap) |
| 772 | swapStruct(*nb); |
| 773 | symOffset += sizeof(nlist); |
| 774 | } |
| 775 | memcpy(&_buffer[strOffset], sym.name.begin(), sym.name.size()); |
| 776 | strOffset += sym.name.size(); |
| 777 | _buffer[strOffset++] ='\0'; // Strings in table have nul terminator. |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | void MachOFileLayout::writeSymbolTable() { |
| 782 | // Write symbol table and symbol strings in parallel. |
| 783 | uint32_t symOffset = _startOfSymbols; |
| 784 | uint32_t strOffset = _startOfSymbolStrings; |
| 785 | _buffer[strOffset++] = '\0'; // Reserve n_strx offset of zero to mean no name. |
| 786 | appendSymbols(_file.localSymbols, symOffset, strOffset); |
| 787 | appendSymbols(_file.globalSymbols, symOffset, strOffset); |
| 788 | appendSymbols(_file.undefinedSymbols, symOffset, strOffset); |
| 789 | // Write indirect symbol table array. |
| 790 | uint32_t *indirects = reinterpret_cast<uint32_t*> |
| 791 | (&_buffer[_startOfIndirectSymbols]); |
| 792 | if (_file.fileType == llvm::MachO::MH_OBJECT) { |
| 793 | // Object files have sections in same order as input normalized file. |
| 794 | for (const Section §ion : _file.sections) { |
| 795 | for (uint32_t index : section.indirectSymbols) { |
| 796 | if (_swap) |
| 797 | *indirects++ = SwapByteOrder(index); |
| 798 | else |
| 799 | *indirects++ = index; |
| 800 | } |
| 801 | } |
| 802 | } else { |
| 803 | // Final linked images must sort sections from normalized file. |
| 804 | for (const Segment &seg : _file.segments) { |
| 805 | SegExtraInfo &segInfo = _segInfo[&seg]; |
| 806 | for (const Section *section : segInfo.sections) { |
| 807 | for (uint32_t index : section->indirectSymbols) { |
| 808 | if (_swap) |
| 809 | *indirects++ = SwapByteOrder(index); |
| 810 | else |
| 811 | *indirects++ = index; |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | } |
| 817 | |
| 818 | void MachOFileLayout::writeRebaseInfo() { |
| 819 | memcpy(&_buffer[_startOfRebaseInfo], _rebaseInfo.bytes(), _rebaseInfo.size()); |
| 820 | } |
| 821 | |
| 822 | void MachOFileLayout::writeBindingInfo() { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 823 | memcpy(&_buffer[_startOfBindingInfo], |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 824 | _bindingInfo.bytes(), _bindingInfo.size()); |
| 825 | } |
| 826 | |
| 827 | void MachOFileLayout::writeLazyBindingInfo() { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 828 | memcpy(&_buffer[_startOfLazyBindingInfo], |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 829 | _lazyBindingInfo.bytes(), _lazyBindingInfo.size()); |
| 830 | } |
| 831 | |
| 832 | void MachOFileLayout::buildLinkEditInfo() { |
| 833 | buildRebaseInfo(); |
| 834 | buildBindInfo(); |
| 835 | buildLazyBindInfo(); |
| 836 | computeSymbolTableSizes(); |
| 837 | } |
| 838 | |
| 839 | void MachOFileLayout::buildSectionRelocations() { |
| 840 | |
| 841 | } |
| 842 | |
| 843 | void MachOFileLayout::buildRebaseInfo() { |
| 844 | // TODO: compress rebasing info. |
| 845 | for (const RebaseLocation& entry : _file.rebasingInfo) { |
| 846 | _rebaseInfo.append_byte(REBASE_OPCODE_SET_TYPE_IMM | entry.kind); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 847 | _rebaseInfo.append_byte(REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 848 | | entry.segIndex); |
| 849 | _rebaseInfo.append_uleb128(entry.segOffset); |
| 850 | _rebaseInfo.append_uleb128(REBASE_OPCODE_DO_REBASE_IMM_TIMES | 1); |
| 851 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 852 | _rebaseInfo.append_byte(REBASE_OPCODE_DONE); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 853 | _rebaseInfo.align(_is64 ? 8 : 4); |
| 854 | } |
| 855 | |
| 856 | void MachOFileLayout::buildBindInfo() { |
| 857 | // TODO: compress bind info. |
| 858 | for (const BindLocation& entry : _file.bindingInfo) { |
| 859 | _bindingInfo.append_byte(BIND_OPCODE_SET_TYPE_IMM | entry.kind); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 860 | _bindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 861 | | entry.segIndex); |
| 862 | _bindingInfo.append_uleb128(entry.segOffset); |
| 863 | _bindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | entry.ordinal); |
| 864 | _bindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM); |
| 865 | _bindingInfo.append_string(entry.symbolName); |
| 866 | if (entry.addend != 0) { |
| 867 | _bindingInfo.append_byte(BIND_OPCODE_SET_ADDEND_SLEB); |
| 868 | _bindingInfo.append_sleb128(entry.addend); |
| 869 | } |
| 870 | _bindingInfo.append_byte(BIND_OPCODE_DO_BIND); |
| 871 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 872 | _bindingInfo.append_byte(BIND_OPCODE_DONE); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 873 | _bindingInfo.align(_is64 ? 8 : 4); |
| 874 | } |
| 875 | |
| 876 | void MachOFileLayout::buildLazyBindInfo() { |
| 877 | for (const BindLocation& entry : _file.lazyBindingInfo) { |
| 878 | _lazyBindingInfo.append_byte(BIND_OPCODE_SET_TYPE_IMM | entry.kind); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 879 | _lazyBindingInfo.append_byte(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 880 | | entry.segIndex); |
| 881 | _lazyBindingInfo.append_uleb128(entry.segOffset); |
| 882 | _lazyBindingInfo.append_byte(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | entry.ordinal); |
| 883 | _lazyBindingInfo.append_byte(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM); |
| 884 | _lazyBindingInfo.append_string(entry.symbolName); |
| 885 | _lazyBindingInfo.append_byte(BIND_OPCODE_DO_BIND); |
| 886 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 887 | _lazyBindingInfo.append_byte(BIND_OPCODE_DONE); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 888 | _lazyBindingInfo.align(_is64 ? 8 : 4); |
| 889 | } |
| 890 | |
| 891 | void MachOFileLayout::computeSymbolTableSizes() { |
| 892 | // MachO symbol tables have three ranges: locals, globals, and undefines |
| 893 | const size_t nlistSize = (_is64 ? sizeof(nlist_64) : sizeof(nlist)); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 894 | _symbolTableSize = nlistSize * (_file.localSymbols.size() |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 895 | + _file.globalSymbols.size() |
| 896 | + _file.undefinedSymbols.size()); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 897 | _symbolStringPoolSize = 0; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 898 | for (const Symbol &sym : _file.localSymbols) { |
| 899 | _symbolStringPoolSize += (sym.name.size()+1); |
| 900 | } |
| 901 | for (const Symbol &sym : _file.globalSymbols) { |
| 902 | _symbolStringPoolSize += (sym.name.size()+1); |
| 903 | } |
| 904 | for (const Symbol &sym : _file.undefinedSymbols) { |
| 905 | _symbolStringPoolSize += (sym.name.size()+1); |
| 906 | } |
| 907 | _symbolTableLocalsStartIndex = 0; |
| 908 | _symbolTableGlobalsStartIndex = _file.localSymbols.size(); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 909 | _symbolTableUndefinesStartIndex = _symbolTableGlobalsStartIndex |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 910 | + _file.globalSymbols.size(); |
| 911 | |
| 912 | _indirectSymbolTableCount = 0; |
| 913 | for (const Section § : _file.sections) { |
| 914 | _indirectSymbolTableCount += sect.indirectSymbols.size(); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | |
| 919 | void MachOFileLayout::writeLinkEditContent() { |
| 920 | if (_file.fileType == llvm::MachO::MH_OBJECT) { |
| 921 | writeRelocations(); |
| 922 | writeSymbolTable(); |
| 923 | } else { |
| 924 | writeRebaseInfo(); |
| 925 | writeBindingInfo(); |
| 926 | writeLazyBindingInfo(); |
| 927 | // TODO: add weak binding info |
| 928 | writeSymbolTable(); |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | |
| 933 | error_code MachOFileLayout::writeBinary(StringRef path) { |
| 934 | // Check for pending error from constructor. |
| 935 | if (_ec) |
| 936 | return _ec; |
| 937 | // Create FileOutputBuffer with calculated size. |
Ahmed Charles | 13c70b6 | 2014-03-13 16:20:38 +0000 | [diff] [blame] | 938 | std::unique_ptr<llvm::FileOutputBuffer> fob; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 939 | unsigned flags = 0; |
| 940 | if (_file.fileType != llvm::MachO::MH_OBJECT) |
| 941 | flags = llvm::FileOutputBuffer::F_executable; |
| 942 | error_code ec; |
| 943 | ec = llvm::FileOutputBuffer::create(path, size(), fob, flags); |
| 944 | if (ec) |
| 945 | return ec; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 946 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 947 | // Write content. |
| 948 | _buffer = fob->getBufferStart(); |
| 949 | writeMachHeader(); |
| 950 | ec = writeLoadCommands(); |
| 951 | if (ec) |
| 952 | return ec; |
| 953 | writeSectionContent(); |
| 954 | writeLinkEditContent(); |
| 955 | fob->commit(); |
| 956 | |
| 957 | return error_code::success(); |
| 958 | } |
| 959 | |
| 960 | |
| 961 | |
| 962 | /// Takes in-memory normalized view and writes a mach-o object file. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 963 | error_code |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 964 | writeBinary(const NormalizedFile &file, StringRef path) { |
| 965 | MachOFileLayout layout(file); |
| 966 | return layout.writeBinary(path); |
| 967 | } |
| 968 | |
| 969 | |
| 970 | } // namespace normalized |
| 971 | } // namespace mach_o |
| 972 | } // namespace lld |
| 973 | |