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