| Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 1 | //===- MachOWriter.cpp ------------------------------------------*- C++ -*-===// | 
|  | 2 | // | 
| Chandler Carruth | 127252b | 2019-02-11 08:25:19 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
|  | 4 | // See https://llvm.org/LICENSE.txt for license information. | 
|  | 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 6 | // | 
|  | 7 | //===----------------------------------------------------------------------===// | 
|  | 8 |  | 
|  | 9 | #include "MachOWriter.h" | 
| Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 10 | #include "Object.h" | 
|  | 11 | #include "llvm/ADT/STLExtras.h" | 
|  | 12 | #include "llvm/BinaryFormat/MachO.h" | 
|  | 13 | #include "llvm/Object/MachO.h" | 
|  | 14 | #include <memory> | 
|  | 15 |  | 
|  | 16 | namespace llvm { | 
|  | 17 | namespace objcopy { | 
|  | 18 | namespace macho { | 
|  | 19 |  | 
|  | 20 | size_t MachOWriter::headerSize() const { | 
|  | 21 | return Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header); | 
|  | 22 | } | 
|  | 23 |  | 
|  | 24 | size_t MachOWriter::loadCommandsSize() const { return O.Header.SizeOfCmds; } | 
|  | 25 |  | 
|  | 26 | size_t MachOWriter::symTableSize() const { | 
|  | 27 | return O.SymTable.NameList.size() * | 
|  | 28 | (Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist)); | 
|  | 29 | } | 
|  | 30 |  | 
|  | 31 | size_t MachOWriter::strTableSize() const { | 
|  | 32 | size_t S = 0; | 
|  | 33 | for (const auto &Str : O.StrTable.Strings) | 
|  | 34 | S += Str.size(); | 
|  | 35 | S += (O.StrTable.Strings.empty() ? 0 : O.StrTable.Strings.size() - 1); | 
|  | 36 | return S; | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | size_t MachOWriter::totalSize() const { | 
|  | 40 | // Going from tail to head and looking for an appropriate "anchor" to | 
|  | 41 | // calculate the total size assuming that all the offsets are either valid | 
|  | 42 | // ("true") or 0 (0 indicates that the corresponding part is missing). | 
|  | 43 |  | 
|  | 44 | SmallVector<size_t, 7> Ends; | 
|  | 45 | if (O.SymTabCommandIndex) { | 
|  | 46 | const MachO::symtab_command &SymTabCommand = | 
|  | 47 | O.LoadCommands[*O.SymTabCommandIndex] | 
|  | 48 | .MachOLoadCommand.symtab_command_data; | 
|  | 49 | if (SymTabCommand.symoff) { | 
|  | 50 | assert((SymTabCommand.nsyms == O.SymTable.NameList.size()) && | 
|  | 51 | "Incorrect number of symbols"); | 
|  | 52 | Ends.push_back(SymTabCommand.symoff + symTableSize()); | 
|  | 53 | } | 
|  | 54 | if (SymTabCommand.stroff) { | 
|  | 55 | assert((SymTabCommand.strsize == strTableSize()) && | 
|  | 56 | "Incorrect string table size"); | 
|  | 57 | Ends.push_back(SymTabCommand.stroff + SymTabCommand.strsize); | 
|  | 58 | } | 
|  | 59 | } | 
|  | 60 | if (O.DyLdInfoCommandIndex) { | 
|  | 61 | const MachO::dyld_info_command &DyLdInfoCommand = | 
|  | 62 | O.LoadCommands[*O.DyLdInfoCommandIndex] | 
|  | 63 | .MachOLoadCommand.dyld_info_command_data; | 
|  | 64 | if (DyLdInfoCommand.rebase_off) { | 
|  | 65 | assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) && | 
|  | 66 | "Incorrect rebase opcodes size"); | 
|  | 67 | Ends.push_back(DyLdInfoCommand.rebase_off + DyLdInfoCommand.rebase_size); | 
|  | 68 | } | 
|  | 69 | if (DyLdInfoCommand.bind_off) { | 
|  | 70 | assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) && | 
|  | 71 | "Incorrect bind opcodes size"); | 
|  | 72 | Ends.push_back(DyLdInfoCommand.bind_off + DyLdInfoCommand.bind_size); | 
|  | 73 | } | 
|  | 74 | if (DyLdInfoCommand.weak_bind_off) { | 
|  | 75 | assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) && | 
|  | 76 | "Incorrect weak bind opcodes size"); | 
|  | 77 | Ends.push_back(DyLdInfoCommand.weak_bind_off + | 
|  | 78 | DyLdInfoCommand.weak_bind_size); | 
|  | 79 | } | 
|  | 80 | if (DyLdInfoCommand.lazy_bind_off) { | 
|  | 81 | assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) && | 
|  | 82 | "Incorrect lazy bind opcodes size"); | 
|  | 83 | Ends.push_back(DyLdInfoCommand.lazy_bind_off + | 
|  | 84 | DyLdInfoCommand.lazy_bind_size); | 
|  | 85 | } | 
|  | 86 | if (DyLdInfoCommand.export_off) { | 
|  | 87 | assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) && | 
|  | 88 | "Incorrect trie size"); | 
|  | 89 | Ends.push_back(DyLdInfoCommand.export_off + DyLdInfoCommand.export_size); | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | // Otherwise, use the last section / reloction. | 
|  | 94 | for (const auto &LC : O.LoadCommands) | 
|  | 95 | for (const auto &S : LC.Sections) { | 
|  | 96 | Ends.push_back(S.Offset + S.Size); | 
|  | 97 | if (S.RelOff) | 
|  | 98 | Ends.push_back(S.RelOff + | 
|  | 99 | S.NReloc * sizeof(MachO::any_relocation_info)); | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | if (!Ends.empty()) | 
|  | 103 | return *std::max_element(Ends.begin(), Ends.end()); | 
|  | 104 |  | 
|  | 105 | // Otherwise, we have only Mach header and load commands. | 
|  | 106 | return headerSize() + loadCommandsSize(); | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | void MachOWriter::writeHeader() { | 
|  | 110 | MachO::mach_header_64 Header; | 
|  | 111 |  | 
|  | 112 | Header.magic = O.Header.Magic; | 
|  | 113 | Header.cputype = O.Header.CPUType; | 
|  | 114 | Header.cpusubtype = O.Header.CPUSubType; | 
|  | 115 | Header.filetype = O.Header.FileType; | 
|  | 116 | Header.ncmds = O.Header.NCmds; | 
|  | 117 | Header.sizeofcmds = O.Header.SizeOfCmds; | 
|  | 118 | Header.flags = O.Header.Flags; | 
|  | 119 | Header.reserved = O.Header.Reserved; | 
|  | 120 |  | 
|  | 121 | if (IsLittleEndian != sys::IsLittleEndianHost) | 
|  | 122 | MachO::swapStruct(Header); | 
|  | 123 |  | 
|  | 124 | auto HeaderSize = | 
|  | 125 | Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header); | 
|  | 126 | memcpy(B.getBufferStart(), &Header, HeaderSize); | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | void MachOWriter::writeLoadCommands() { | 
|  | 130 | uint8_t *Begin = B.getBufferStart() + headerSize(); | 
|  | 131 | MachO::macho_load_command MLC; | 
|  | 132 | for (const auto &LC : O.LoadCommands) { | 
|  | 133 | #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct)                         \ | 
|  | 134 | case MachO::LCName:                                                          \ | 
|  | 135 | assert(sizeof(MachO::LCStruct) + LC.Payload.size() ==                      \ | 
|  | 136 | LC.MachOLoadCommand.load_command_data.cmdsize);                     \ | 
|  | 137 | MLC = LC.MachOLoadCommand;                                                 \ | 
|  | 138 | if (IsLittleEndian != sys::IsLittleEndianHost)                             \ | 
|  | 139 | MachO::swapStruct(MLC.LCStruct##_data);                                  \ | 
|  | 140 | memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct));              \ | 
|  | 141 | Begin += sizeof(MachO::LCStruct);                                          \ | 
|  | 142 | memcpy(Begin, LC.Payload.data(), LC.Payload.size());                       \ | 
|  | 143 | Begin += LC.Payload.size();                                                \ | 
|  | 144 | break; | 
|  | 145 |  | 
|  | 146 | switch (LC.MachOLoadCommand.load_command_data.cmd) { | 
|  | 147 | default: | 
|  | 148 | assert(sizeof(MachO::load_command) + LC.Payload.size() == | 
|  | 149 | LC.MachOLoadCommand.load_command_data.cmdsize); | 
|  | 150 | MLC = LC.MachOLoadCommand; | 
|  | 151 | if (IsLittleEndian != sys::IsLittleEndianHost) | 
|  | 152 | MachO::swapStruct(MLC.load_command_data); | 
|  | 153 | memcpy(Begin, &MLC.load_command_data, sizeof(MachO::load_command)); | 
|  | 154 | Begin += sizeof(MachO::load_command); | 
|  | 155 | memcpy(Begin, LC.Payload.data(), LC.Payload.size()); | 
|  | 156 | Begin += LC.Payload.size(); | 
|  | 157 | break; | 
|  | 158 | #include "llvm/BinaryFormat/MachO.def" | 
|  | 159 | } | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | void MachOWriter::writeSections() { | 
|  | 164 | for (const auto &LC : O.LoadCommands) | 
|  | 165 | for (const auto &Sec : LC.Sections) { | 
|  | 166 | assert(Sec.Offset && "Section offset can not be zero"); | 
|  | 167 | assert((Sec.Size == Sec.Content.size()) && "Incorrect section size"); | 
|  | 168 | memcpy(B.getBufferStart() + Sec.Offset, Sec.Content.data(), | 
|  | 169 | Sec.Content.size()); | 
|  | 170 | for (size_t Index = 0; Index < Sec.Relocations.size(); ++Index) { | 
|  | 171 | MachO::any_relocation_info R = Sec.Relocations[Index]; | 
|  | 172 | if (IsLittleEndian != sys::IsLittleEndianHost) | 
|  | 173 | MachO::swapStruct(R); | 
|  | 174 | memcpy(B.getBufferStart() + Sec.RelOff + | 
|  | 175 | Index * sizeof(MachO::any_relocation_info), | 
|  | 176 | &R, sizeof(R)); | 
|  | 177 | } | 
|  | 178 | } | 
|  | 179 | } | 
|  | 180 |  | 
|  | 181 | template <typename NListType> | 
|  | 182 | void writeNListEntry(const NListEntry &NLE, bool IsLittleEndian, char *&Out) { | 
|  | 183 | NListType ListEntry; | 
|  | 184 | ListEntry.n_strx = NLE.n_strx; | 
|  | 185 | ListEntry.n_type = NLE.n_type; | 
|  | 186 | ListEntry.n_sect = NLE.n_sect; | 
|  | 187 | ListEntry.n_desc = NLE.n_desc; | 
|  | 188 | ListEntry.n_value = NLE.n_value; | 
|  | 189 |  | 
|  | 190 | if (IsLittleEndian != sys::IsLittleEndianHost) | 
|  | 191 | MachO::swapStruct(ListEntry); | 
|  | 192 | memcpy(Out, reinterpret_cast<const char *>(&ListEntry), sizeof(NListType)); | 
|  | 193 | Out += sizeof(NListType); | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | void MachOWriter::writeSymbolTable() { | 
|  | 197 | if (!O.SymTabCommandIndex) | 
|  | 198 | return; | 
|  | 199 | const MachO::symtab_command &SymTabCommand = | 
|  | 200 | O.LoadCommands[*O.SymTabCommandIndex] | 
|  | 201 | .MachOLoadCommand.symtab_command_data; | 
|  | 202 | assert((SymTabCommand.nsyms == O.SymTable.NameList.size()) && | 
|  | 203 | "Incorrect number of symbols"); | 
|  | 204 | char *Out = (char *)B.getBufferStart() + SymTabCommand.symoff; | 
|  | 205 | for (auto NLE : O.SymTable.NameList) { | 
|  | 206 | if (Is64Bit) | 
|  | 207 | writeNListEntry<MachO::nlist_64>(NLE, IsLittleEndian, Out); | 
|  | 208 | else | 
|  | 209 | writeNListEntry<MachO::nlist>(NLE, IsLittleEndian, Out); | 
|  | 210 | } | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | void MachOWriter::writeStringTable() { | 
|  | 214 | if (!O.SymTabCommandIndex) | 
|  | 215 | return; | 
|  | 216 | const MachO::symtab_command &SymTabCommand = | 
|  | 217 | O.LoadCommands[*O.SymTabCommandIndex] | 
|  | 218 | .MachOLoadCommand.symtab_command_data; | 
|  | 219 | char *Out = (char *)B.getBufferStart() + SymTabCommand.stroff; | 
|  | 220 | assert((SymTabCommand.strsize == strTableSize()) && | 
|  | 221 | "Incorrect string table size"); | 
|  | 222 | for (size_t Index = 0; Index < O.StrTable.Strings.size(); ++Index) { | 
|  | 223 | memcpy(Out, O.StrTable.Strings[Index].data(), | 
|  | 224 | O.StrTable.Strings[Index].size()); | 
|  | 225 | Out += O.StrTable.Strings[Index].size(); | 
|  | 226 | if (Index + 1 != O.StrTable.Strings.size()) { | 
|  | 227 | memcpy(Out, "\0", 1); | 
|  | 228 | Out += 1; | 
|  | 229 | } | 
|  | 230 | } | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | void MachOWriter::writeRebaseInfo() { | 
|  | 234 | if (!O.DyLdInfoCommandIndex) | 
|  | 235 | return; | 
|  | 236 | const MachO::dyld_info_command &DyLdInfoCommand = | 
|  | 237 | O.LoadCommands[*O.DyLdInfoCommandIndex] | 
|  | 238 | .MachOLoadCommand.dyld_info_command_data; | 
|  | 239 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.rebase_off; | 
|  | 240 | assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) && | 
|  | 241 | "Incorrect rebase opcodes size"); | 
|  | 242 | memcpy(Out, O.Rebases.Opcodes.data(), O.Rebases.Opcodes.size()); | 
|  | 243 | } | 
|  | 244 |  | 
|  | 245 | void MachOWriter::writeBindInfo() { | 
|  | 246 | if (!O.DyLdInfoCommandIndex) | 
|  | 247 | return; | 
|  | 248 | const MachO::dyld_info_command &DyLdInfoCommand = | 
|  | 249 | O.LoadCommands[*O.DyLdInfoCommandIndex] | 
|  | 250 | .MachOLoadCommand.dyld_info_command_data; | 
|  | 251 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.bind_off; | 
|  | 252 | assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) && | 
|  | 253 | "Incorrect bind opcodes size"); | 
|  | 254 | memcpy(Out, O.Binds.Opcodes.data(), O.Binds.Opcodes.size()); | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | void MachOWriter::writeWeakBindInfo() { | 
|  | 258 | if (!O.DyLdInfoCommandIndex) | 
|  | 259 | return; | 
|  | 260 | const MachO::dyld_info_command &DyLdInfoCommand = | 
|  | 261 | O.LoadCommands[*O.DyLdInfoCommandIndex] | 
|  | 262 | .MachOLoadCommand.dyld_info_command_data; | 
|  | 263 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.weak_bind_off; | 
|  | 264 | assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) && | 
|  | 265 | "Incorrect weak bind opcodes size"); | 
|  | 266 | memcpy(Out, O.WeakBinds.Opcodes.data(), O.WeakBinds.Opcodes.size()); | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | void MachOWriter::writeLazyBindInfo() { | 
|  | 270 | if (!O.DyLdInfoCommandIndex) | 
|  | 271 | return; | 
|  | 272 | const MachO::dyld_info_command &DyLdInfoCommand = | 
|  | 273 | O.LoadCommands[*O.DyLdInfoCommandIndex] | 
|  | 274 | .MachOLoadCommand.dyld_info_command_data; | 
|  | 275 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.lazy_bind_off; | 
|  | 276 | assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) && | 
|  | 277 | "Incorrect lazy bind opcodes size"); | 
|  | 278 | memcpy(Out, O.LazyBinds.Opcodes.data(), O.LazyBinds.Opcodes.size()); | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 | void MachOWriter::writeExportInfo() { | 
|  | 282 | if (!O.DyLdInfoCommandIndex) | 
|  | 283 | return; | 
|  | 284 | const MachO::dyld_info_command &DyLdInfoCommand = | 
|  | 285 | O.LoadCommands[*O.DyLdInfoCommandIndex] | 
|  | 286 | .MachOLoadCommand.dyld_info_command_data; | 
|  | 287 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.export_off; | 
|  | 288 | assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) && | 
|  | 289 | "Incorrect export trie size"); | 
|  | 290 | memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size()); | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | void MachOWriter::writeTail() { | 
|  | 294 | typedef void (MachOWriter::*WriteHandlerType)(void); | 
|  | 295 | typedef std::pair<uint64_t, WriteHandlerType> WriteOperation; | 
|  | 296 | SmallVector<WriteOperation, 7> Queue; | 
|  | 297 |  | 
|  | 298 | if (O.SymTabCommandIndex) { | 
|  | 299 | const MachO::symtab_command &SymTabCommand = | 
|  | 300 | O.LoadCommands[*O.SymTabCommandIndex] | 
|  | 301 | .MachOLoadCommand.symtab_command_data; | 
|  | 302 | if (SymTabCommand.symoff) | 
|  | 303 | Queue.push_back({SymTabCommand.symoff, &MachOWriter::writeSymbolTable}); | 
|  | 304 | if (SymTabCommand.stroff) | 
|  | 305 | Queue.push_back({SymTabCommand.stroff, &MachOWriter::writeStringTable}); | 
|  | 306 | } | 
|  | 307 |  | 
|  | 308 | if (O.DyLdInfoCommandIndex) { | 
|  | 309 | const MachO::dyld_info_command &DyLdInfoCommand = | 
|  | 310 | O.LoadCommands[*O.DyLdInfoCommandIndex] | 
|  | 311 | .MachOLoadCommand.dyld_info_command_data; | 
|  | 312 | if (DyLdInfoCommand.rebase_off) | 
|  | 313 | Queue.push_back( | 
|  | 314 | {DyLdInfoCommand.rebase_off, &MachOWriter::writeRebaseInfo}); | 
|  | 315 | if (DyLdInfoCommand.bind_off) | 
|  | 316 | Queue.push_back({DyLdInfoCommand.bind_off, &MachOWriter::writeBindInfo}); | 
|  | 317 | if (DyLdInfoCommand.weak_bind_off) | 
|  | 318 | Queue.push_back( | 
|  | 319 | {DyLdInfoCommand.weak_bind_off, &MachOWriter::writeWeakBindInfo}); | 
|  | 320 | if (DyLdInfoCommand.lazy_bind_off) | 
|  | 321 | Queue.push_back( | 
|  | 322 | {DyLdInfoCommand.lazy_bind_off, &MachOWriter::writeLazyBindInfo}); | 
|  | 323 | if (DyLdInfoCommand.export_off) | 
|  | 324 | Queue.push_back( | 
|  | 325 | {DyLdInfoCommand.export_off, &MachOWriter::writeExportInfo}); | 
|  | 326 | } | 
|  | 327 |  | 
|  | 328 | llvm::sort(Queue, [](const WriteOperation &LHS, const WriteOperation &RHS) { | 
|  | 329 | return LHS.first < RHS.first; | 
|  | 330 | }); | 
|  | 331 |  | 
|  | 332 | for (auto WriteOp : Queue) | 
|  | 333 | (this->*WriteOp.second)(); | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | Error MachOWriter::write() { | 
|  | 337 | if (Error E = B.allocate(totalSize())) | 
|  | 338 | return E; | 
|  | 339 | memset(B.getBufferStart(), 0, totalSize()); | 
|  | 340 | writeHeader(); | 
|  | 341 | writeLoadCommands(); | 
|  | 342 | writeSections(); | 
|  | 343 | writeTail(); | 
| Jordan Rupprecht | 2e862c7 | 2019-02-04 19:09:20 +0000 | [diff] [blame] | 344 | return B.commit(); | 
| Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 345 | } | 
|  | 346 |  | 
|  | 347 | } // end namespace macho | 
|  | 348 | } // end namespace objcopy | 
|  | 349 | } // end namespace llvm |