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