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" |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 10 | #include "MachOLayoutBuilder.h" |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 11 | #include "Object.h" |
| 12 | #include "llvm/ADT/STLExtras.h" |
| 13 | #include "llvm/BinaryFormat/MachO.h" |
| 14 | #include "llvm/Object/MachO.h" |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Errc.h" |
| 16 | #include "llvm/Support/ErrorHandling.h" |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 17 | #include <memory> |
| 18 | |
| 19 | namespace llvm { |
| 20 | namespace objcopy { |
| 21 | namespace macho { |
| 22 | |
| 23 | size_t MachOWriter::headerSize() const { |
| 24 | return Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header); |
| 25 | } |
| 26 | |
| 27 | size_t MachOWriter::loadCommandsSize() const { return O.Header.SizeOfCmds; } |
| 28 | |
| 29 | size_t MachOWriter::symTableSize() const { |
Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 30 | return O.SymTable.Symbols.size() * |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 31 | (Is64Bit ? sizeof(MachO::nlist_64) : sizeof(MachO::nlist)); |
| 32 | } |
| 33 | |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 34 | size_t MachOWriter::totalSize() const { |
| 35 | // Going from tail to head and looking for an appropriate "anchor" to |
| 36 | // calculate the total size assuming that all the offsets are either valid |
| 37 | // ("true") or 0 (0 indicates that the corresponding part is missing). |
| 38 | |
| 39 | SmallVector<size_t, 7> Ends; |
| 40 | if (O.SymTabCommandIndex) { |
| 41 | const MachO::symtab_command &SymTabCommand = |
| 42 | O.LoadCommands[*O.SymTabCommandIndex] |
| 43 | .MachOLoadCommand.symtab_command_data; |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 44 | if (SymTabCommand.symoff) |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 45 | Ends.push_back(SymTabCommand.symoff + symTableSize()); |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 46 | if (SymTabCommand.stroff) |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 47 | Ends.push_back(SymTabCommand.stroff + SymTabCommand.strsize); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 48 | } |
| 49 | if (O.DyLdInfoCommandIndex) { |
| 50 | const MachO::dyld_info_command &DyLdInfoCommand = |
| 51 | O.LoadCommands[*O.DyLdInfoCommandIndex] |
| 52 | .MachOLoadCommand.dyld_info_command_data; |
| 53 | if (DyLdInfoCommand.rebase_off) { |
| 54 | assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) && |
| 55 | "Incorrect rebase opcodes size"); |
| 56 | Ends.push_back(DyLdInfoCommand.rebase_off + DyLdInfoCommand.rebase_size); |
| 57 | } |
| 58 | if (DyLdInfoCommand.bind_off) { |
| 59 | assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) && |
| 60 | "Incorrect bind opcodes size"); |
| 61 | Ends.push_back(DyLdInfoCommand.bind_off + DyLdInfoCommand.bind_size); |
| 62 | } |
| 63 | if (DyLdInfoCommand.weak_bind_off) { |
| 64 | assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) && |
| 65 | "Incorrect weak bind opcodes size"); |
| 66 | Ends.push_back(DyLdInfoCommand.weak_bind_off + |
| 67 | DyLdInfoCommand.weak_bind_size); |
| 68 | } |
| 69 | if (DyLdInfoCommand.lazy_bind_off) { |
| 70 | assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) && |
| 71 | "Incorrect lazy bind opcodes size"); |
| 72 | Ends.push_back(DyLdInfoCommand.lazy_bind_off + |
| 73 | DyLdInfoCommand.lazy_bind_size); |
| 74 | } |
| 75 | if (DyLdInfoCommand.export_off) { |
| 76 | assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) && |
| 77 | "Incorrect trie size"); |
| 78 | Ends.push_back(DyLdInfoCommand.export_off + DyLdInfoCommand.export_size); |
| 79 | } |
| 80 | } |
| 81 | |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 82 | if (O.DySymTabCommandIndex) { |
| 83 | const MachO::dysymtab_command &DySymTabCommand = |
| 84 | O.LoadCommands[*O.DySymTabCommandIndex] |
| 85 | .MachOLoadCommand.dysymtab_command_data; |
| 86 | |
| 87 | if (DySymTabCommand.indirectsymoff) |
| 88 | Ends.push_back(DySymTabCommand.indirectsymoff + |
| 89 | sizeof(uint32_t) * O.IndirectSymTable.Symbols.size()); |
| 90 | } |
| 91 | |
Alexander Shaposhnikov | 913bc31 | 2020-06-15 18:55:59 -0700 | [diff] [blame] | 92 | if (O.CodeSignatureCommandIndex) { |
| 93 | const MachO::linkedit_data_command &LinkEditDataCommand = |
| 94 | O.LoadCommands[*O.CodeSignatureCommandIndex] |
| 95 | .MachOLoadCommand.linkedit_data_command_data; |
| 96 | if (LinkEditDataCommand.dataoff) |
| 97 | Ends.push_back(LinkEditDataCommand.dataoff + |
| 98 | LinkEditDataCommand.datasize); |
| 99 | } |
| 100 | |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 101 | if (O.DataInCodeCommandIndex) { |
| 102 | const MachO::linkedit_data_command &LinkEditDataCommand = |
| 103 | O.LoadCommands[*O.DataInCodeCommandIndex] |
| 104 | .MachOLoadCommand.linkedit_data_command_data; |
| 105 | |
| 106 | if (LinkEditDataCommand.dataoff) |
| 107 | Ends.push_back(LinkEditDataCommand.dataoff + |
| 108 | LinkEditDataCommand.datasize); |
| 109 | } |
| 110 | |
| 111 | if (O.FunctionStartsCommandIndex) { |
| 112 | const MachO::linkedit_data_command &LinkEditDataCommand = |
| 113 | O.LoadCommands[*O.FunctionStartsCommandIndex] |
| 114 | .MachOLoadCommand.linkedit_data_command_data; |
| 115 | |
| 116 | if (LinkEditDataCommand.dataoff) |
| 117 | Ends.push_back(LinkEditDataCommand.dataoff + |
| 118 | LinkEditDataCommand.datasize); |
| 119 | } |
| 120 | |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 121 | // Otherwise, use the last section / reloction. |
Alexander Shaposhnikov | dc046c7 | 2020-02-21 13:18:36 -0800 | [diff] [blame] | 122 | for (const LoadCommand &LC : O.LoadCommands) |
| 123 | for (const std::unique_ptr<Section> &S : LC.Sections) { |
| 124 | Ends.push_back(S->Offset + S->Size); |
| 125 | if (S->RelOff) |
| 126 | Ends.push_back(S->RelOff + |
| 127 | S->NReloc * sizeof(MachO::any_relocation_info)); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (!Ends.empty()) |
| 131 | return *std::max_element(Ends.begin(), Ends.end()); |
| 132 | |
| 133 | // Otherwise, we have only Mach header and load commands. |
| 134 | return headerSize() + loadCommandsSize(); |
| 135 | } |
| 136 | |
| 137 | void MachOWriter::writeHeader() { |
| 138 | MachO::mach_header_64 Header; |
| 139 | |
| 140 | Header.magic = O.Header.Magic; |
| 141 | Header.cputype = O.Header.CPUType; |
| 142 | Header.cpusubtype = O.Header.CPUSubType; |
| 143 | Header.filetype = O.Header.FileType; |
| 144 | Header.ncmds = O.Header.NCmds; |
| 145 | Header.sizeofcmds = O.Header.SizeOfCmds; |
| 146 | Header.flags = O.Header.Flags; |
| 147 | Header.reserved = O.Header.Reserved; |
| 148 | |
| 149 | if (IsLittleEndian != sys::IsLittleEndianHost) |
| 150 | MachO::swapStruct(Header); |
| 151 | |
| 152 | auto HeaderSize = |
| 153 | Is64Bit ? sizeof(MachO::mach_header_64) : sizeof(MachO::mach_header); |
| 154 | memcpy(B.getBufferStart(), &Header, HeaderSize); |
| 155 | } |
| 156 | |
| 157 | void MachOWriter::writeLoadCommands() { |
| 158 | uint8_t *Begin = B.getBufferStart() + headerSize(); |
Alexander Shaposhnikov | dc046c7 | 2020-02-21 13:18:36 -0800 | [diff] [blame] | 159 | for (const LoadCommand &LC : O.LoadCommands) { |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 160 | // Construct a load command. |
| 161 | MachO::macho_load_command MLC = LC.MachOLoadCommand; |
| 162 | switch (MLC.load_command_data.cmd) { |
| 163 | case MachO::LC_SEGMENT: |
| 164 | if (IsLittleEndian != sys::IsLittleEndianHost) |
| 165 | MachO::swapStruct(MLC.segment_command_data); |
| 166 | memcpy(Begin, &MLC.segment_command_data, sizeof(MachO::segment_command)); |
| 167 | Begin += sizeof(MachO::segment_command); |
| 168 | |
Alexander Shaposhnikov | dc046c7 | 2020-02-21 13:18:36 -0800 | [diff] [blame] | 169 | for (const std::unique_ptr<Section> &Sec : LC.Sections) |
| 170 | writeSectionInLoadCommand<MachO::section>(*Sec, Begin); |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 171 | continue; |
| 172 | case MachO::LC_SEGMENT_64: |
| 173 | if (IsLittleEndian != sys::IsLittleEndianHost) |
| 174 | MachO::swapStruct(MLC.segment_command_64_data); |
| 175 | memcpy(Begin, &MLC.segment_command_64_data, |
| 176 | sizeof(MachO::segment_command_64)); |
| 177 | Begin += sizeof(MachO::segment_command_64); |
| 178 | |
Alexander Shaposhnikov | dc046c7 | 2020-02-21 13:18:36 -0800 | [diff] [blame] | 179 | for (const std::unique_ptr<Section> &Sec : LC.Sections) |
| 180 | writeSectionInLoadCommand<MachO::section_64>(*Sec, Begin); |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 181 | continue; |
| 182 | } |
| 183 | |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 184 | #define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \ |
| 185 | case MachO::LCName: \ |
| 186 | assert(sizeof(MachO::LCStruct) + LC.Payload.size() == \ |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 187 | MLC.load_command_data.cmdsize); \ |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 188 | if (IsLittleEndian != sys::IsLittleEndianHost) \ |
| 189 | MachO::swapStruct(MLC.LCStruct##_data); \ |
| 190 | memcpy(Begin, &MLC.LCStruct##_data, sizeof(MachO::LCStruct)); \ |
| 191 | Begin += sizeof(MachO::LCStruct); \ |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 192 | if (!LC.Payload.empty()) \ |
| 193 | memcpy(Begin, LC.Payload.data(), LC.Payload.size()); \ |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 194 | Begin += LC.Payload.size(); \ |
| 195 | break; |
| 196 | |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 197 | // Copy the load command as it is. |
| 198 | switch (MLC.load_command_data.cmd) { |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 199 | default: |
| 200 | assert(sizeof(MachO::load_command) + LC.Payload.size() == |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 201 | MLC.load_command_data.cmdsize); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 202 | if (IsLittleEndian != sys::IsLittleEndianHost) |
| 203 | MachO::swapStruct(MLC.load_command_data); |
| 204 | memcpy(Begin, &MLC.load_command_data, sizeof(MachO::load_command)); |
| 205 | Begin += sizeof(MachO::load_command); |
Alexander Shaposhnikov | c54959c | 2019-11-19 23:30:52 -0800 | [diff] [blame] | 206 | if (!LC.Payload.empty()) |
| 207 | memcpy(Begin, LC.Payload.data(), LC.Payload.size()); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 208 | Begin += LC.Payload.size(); |
| 209 | break; |
| 210 | #include "llvm/BinaryFormat/MachO.def" |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 215 | template <typename StructType> |
| 216 | void MachOWriter::writeSectionInLoadCommand(const Section &Sec, uint8_t *&Out) { |
| 217 | StructType Temp; |
| 218 | assert(Sec.Segname.size() <= sizeof(Temp.segname) && "too long segment name"); |
| 219 | assert(Sec.Sectname.size() <= sizeof(Temp.sectname) && |
| 220 | "too long section name"); |
| 221 | memset(&Temp, 0, sizeof(StructType)); |
| 222 | memcpy(Temp.segname, Sec.Segname.data(), Sec.Segname.size()); |
| 223 | memcpy(Temp.sectname, Sec.Sectname.data(), Sec.Sectname.size()); |
| 224 | Temp.addr = Sec.Addr; |
| 225 | Temp.size = Sec.Size; |
| 226 | Temp.offset = Sec.Offset; |
| 227 | Temp.align = Sec.Align; |
| 228 | Temp.reloff = Sec.RelOff; |
| 229 | Temp.nreloc = Sec.NReloc; |
| 230 | Temp.flags = Sec.Flags; |
| 231 | Temp.reserved1 = Sec.Reserved1; |
| 232 | Temp.reserved2 = Sec.Reserved2; |
| 233 | |
| 234 | if (IsLittleEndian != sys::IsLittleEndianHost) |
| 235 | MachO::swapStruct(Temp); |
| 236 | memcpy(Out, &Temp, sizeof(StructType)); |
| 237 | Out += sizeof(StructType); |
| 238 | } |
| 239 | |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 240 | void MachOWriter::writeSections() { |
Alexander Shaposhnikov | dc046c7 | 2020-02-21 13:18:36 -0800 | [diff] [blame] | 241 | for (const LoadCommand &LC : O.LoadCommands) |
| 242 | for (const std::unique_ptr<Section> &Sec : LC.Sections) { |
| 243 | if (Sec->isVirtualSection()) |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 244 | continue; |
| 245 | |
Alexander Shaposhnikov | dc046c7 | 2020-02-21 13:18:36 -0800 | [diff] [blame] | 246 | assert(Sec->Offset && "Section offset can not be zero"); |
| 247 | assert((Sec->Size == Sec->Content.size()) && "Incorrect section size"); |
| 248 | memcpy(B.getBufferStart() + Sec->Offset, Sec->Content.data(), |
| 249 | Sec->Content.size()); |
| 250 | for (size_t Index = 0; Index < Sec->Relocations.size(); ++Index) { |
Alexander Shaposhnikov | 0db3a5a | 2020-04-26 22:39:50 -0700 | [diff] [blame] | 251 | RelocationInfo RelocInfo = Sec->Relocations[Index]; |
| 252 | if (!RelocInfo.Scattered) { |
| 253 | const uint32_t SymbolNum = RelocInfo.Extern |
| 254 | ? (*RelocInfo.Symbol)->Index |
Alexander Shaposhnikov | 29c6f5c | 2020-04-27 18:20:01 -0700 | [diff] [blame] | 255 | : (*RelocInfo.Sec)->Index; |
Alexander Shaposhnikov | 0db3a5a | 2020-04-26 22:39:50 -0700 | [diff] [blame] | 256 | RelocInfo.setPlainRelocationSymbolNum(SymbolNum, IsLittleEndian); |
| 257 | } |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 258 | if (IsLittleEndian != sys::IsLittleEndianHost) |
Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 259 | MachO::swapStruct( |
| 260 | reinterpret_cast<MachO::any_relocation_info &>(RelocInfo.Info)); |
Alexander Shaposhnikov | dc046c7 | 2020-02-21 13:18:36 -0800 | [diff] [blame] | 261 | memcpy(B.getBufferStart() + Sec->RelOff + |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 262 | Index * sizeof(MachO::any_relocation_info), |
Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 263 | &RelocInfo.Info, sizeof(RelocInfo.Info)); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | template <typename NListType> |
Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 269 | void writeNListEntry(const SymbolEntry &SE, bool IsLittleEndian, char *&Out, |
| 270 | uint32_t Nstrx) { |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 271 | NListType ListEntry; |
Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 272 | ListEntry.n_strx = Nstrx; |
| 273 | ListEntry.n_type = SE.n_type; |
| 274 | ListEntry.n_sect = SE.n_sect; |
| 275 | ListEntry.n_desc = SE.n_desc; |
| 276 | ListEntry.n_value = SE.n_value; |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 277 | |
| 278 | if (IsLittleEndian != sys::IsLittleEndianHost) |
| 279 | MachO::swapStruct(ListEntry); |
| 280 | memcpy(Out, reinterpret_cast<const char *>(&ListEntry), sizeof(NListType)); |
| 281 | Out += sizeof(NListType); |
| 282 | } |
| 283 | |
Seiya Nuta | 36848ff | 2019-08-20 08:36:47 +0000 | [diff] [blame] | 284 | void MachOWriter::writeStringTable() { |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 285 | if (!O.SymTabCommandIndex) |
| 286 | return; |
| 287 | const MachO::symtab_command &SymTabCommand = |
| 288 | O.LoadCommands[*O.SymTabCommandIndex] |
| 289 | .MachOLoadCommand.symtab_command_data; |
Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 290 | |
| 291 | uint8_t *StrTable = (uint8_t *)B.getBufferStart() + SymTabCommand.stroff; |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 292 | LayoutBuilder.getStringTableBuilder().write(StrTable); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Seiya Nuta | 36848ff | 2019-08-20 08:36:47 +0000 | [diff] [blame] | 295 | void MachOWriter::writeSymbolTable() { |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 296 | if (!O.SymTabCommandIndex) |
| 297 | return; |
| 298 | const MachO::symtab_command &SymTabCommand = |
| 299 | O.LoadCommands[*O.SymTabCommandIndex] |
| 300 | .MachOLoadCommand.symtab_command_data; |
Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 301 | |
| 302 | char *SymTable = (char *)B.getBufferStart() + SymTabCommand.symoff; |
| 303 | for (auto Iter = O.SymTable.Symbols.begin(), End = O.SymTable.Symbols.end(); |
| 304 | Iter != End; Iter++) { |
| 305 | SymbolEntry *Sym = Iter->get(); |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 306 | uint32_t Nstrx = LayoutBuilder.getStringTableBuilder().getOffset(Sym->Name); |
Seiya Nuta | f923d9b | 2019-06-21 00:21:50 +0000 | [diff] [blame] | 307 | |
| 308 | if (Is64Bit) |
| 309 | writeNListEntry<MachO::nlist_64>(*Sym, IsLittleEndian, SymTable, Nstrx); |
| 310 | else |
| 311 | writeNListEntry<MachO::nlist>(*Sym, IsLittleEndian, SymTable, Nstrx); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | void MachOWriter::writeRebaseInfo() { |
| 316 | if (!O.DyLdInfoCommandIndex) |
| 317 | return; |
| 318 | const MachO::dyld_info_command &DyLdInfoCommand = |
| 319 | O.LoadCommands[*O.DyLdInfoCommandIndex] |
| 320 | .MachOLoadCommand.dyld_info_command_data; |
| 321 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.rebase_off; |
| 322 | assert((DyLdInfoCommand.rebase_size == O.Rebases.Opcodes.size()) && |
| 323 | "Incorrect rebase opcodes size"); |
| 324 | memcpy(Out, O.Rebases.Opcodes.data(), O.Rebases.Opcodes.size()); |
| 325 | } |
| 326 | |
| 327 | void MachOWriter::writeBindInfo() { |
| 328 | if (!O.DyLdInfoCommandIndex) |
| 329 | return; |
| 330 | const MachO::dyld_info_command &DyLdInfoCommand = |
| 331 | O.LoadCommands[*O.DyLdInfoCommandIndex] |
| 332 | .MachOLoadCommand.dyld_info_command_data; |
| 333 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.bind_off; |
| 334 | assert((DyLdInfoCommand.bind_size == O.Binds.Opcodes.size()) && |
| 335 | "Incorrect bind opcodes size"); |
| 336 | memcpy(Out, O.Binds.Opcodes.data(), O.Binds.Opcodes.size()); |
| 337 | } |
| 338 | |
| 339 | void MachOWriter::writeWeakBindInfo() { |
| 340 | if (!O.DyLdInfoCommandIndex) |
| 341 | return; |
| 342 | const MachO::dyld_info_command &DyLdInfoCommand = |
| 343 | O.LoadCommands[*O.DyLdInfoCommandIndex] |
| 344 | .MachOLoadCommand.dyld_info_command_data; |
| 345 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.weak_bind_off; |
| 346 | assert((DyLdInfoCommand.weak_bind_size == O.WeakBinds.Opcodes.size()) && |
| 347 | "Incorrect weak bind opcodes size"); |
| 348 | memcpy(Out, O.WeakBinds.Opcodes.data(), O.WeakBinds.Opcodes.size()); |
| 349 | } |
| 350 | |
| 351 | void MachOWriter::writeLazyBindInfo() { |
| 352 | if (!O.DyLdInfoCommandIndex) |
| 353 | return; |
| 354 | const MachO::dyld_info_command &DyLdInfoCommand = |
| 355 | O.LoadCommands[*O.DyLdInfoCommandIndex] |
| 356 | .MachOLoadCommand.dyld_info_command_data; |
| 357 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.lazy_bind_off; |
| 358 | assert((DyLdInfoCommand.lazy_bind_size == O.LazyBinds.Opcodes.size()) && |
| 359 | "Incorrect lazy bind opcodes size"); |
| 360 | memcpy(Out, O.LazyBinds.Opcodes.data(), O.LazyBinds.Opcodes.size()); |
| 361 | } |
| 362 | |
| 363 | void MachOWriter::writeExportInfo() { |
| 364 | if (!O.DyLdInfoCommandIndex) |
| 365 | return; |
| 366 | const MachO::dyld_info_command &DyLdInfoCommand = |
| 367 | O.LoadCommands[*O.DyLdInfoCommandIndex] |
| 368 | .MachOLoadCommand.dyld_info_command_data; |
| 369 | char *Out = (char *)B.getBufferStart() + DyLdInfoCommand.export_off; |
| 370 | assert((DyLdInfoCommand.export_size == O.Exports.Trie.size()) && |
| 371 | "Incorrect export trie size"); |
| 372 | memcpy(Out, O.Exports.Trie.data(), O.Exports.Trie.size()); |
| 373 | } |
| 374 | |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 375 | void MachOWriter::writeIndirectSymbolTable() { |
| 376 | if (!O.DySymTabCommandIndex) |
| 377 | return; |
| 378 | |
| 379 | const MachO::dysymtab_command &DySymTabCommand = |
| 380 | O.LoadCommands[*O.DySymTabCommandIndex] |
| 381 | .MachOLoadCommand.dysymtab_command_data; |
| 382 | |
Seiya Nuta | 1e589f6 | 2019-10-30 15:12:17 +0900 | [diff] [blame] | 383 | uint32_t *Out = |
| 384 | (uint32_t *)(B.getBufferStart() + DySymTabCommand.indirectsymoff); |
| 385 | for (const IndirectSymbolEntry &Sym : O.IndirectSymTable.Symbols) { |
| 386 | uint32_t Entry = (Sym.Symbol) ? (*Sym.Symbol)->Index : Sym.OriginalIndex; |
| 387 | if (IsLittleEndian != sys::IsLittleEndianHost) |
| 388 | sys::swapByteOrder(Entry); |
| 389 | *Out++ = Entry; |
| 390 | } |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Alexander Shaposhnikov | 913bc31 | 2020-06-15 18:55:59 -0700 | [diff] [blame] | 393 | void MachOWriter::writeLinkData(Optional<size_t> LCIndex, const LinkData &LD) { |
| 394 | if (!LCIndex) |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 395 | return; |
| 396 | const MachO::linkedit_data_command &LinkEditDataCommand = |
Alexander Shaposhnikov | 913bc31 | 2020-06-15 18:55:59 -0700 | [diff] [blame] | 397 | O.LoadCommands[*LCIndex].MachOLoadCommand.linkedit_data_command_data; |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 398 | char *Out = (char *)B.getBufferStart() + LinkEditDataCommand.dataoff; |
Alexander Shaposhnikov | 913bc31 | 2020-06-15 18:55:59 -0700 | [diff] [blame] | 399 | assert((LinkEditDataCommand.datasize == LD.Data.size()) && |
| 400 | "Incorrect data size"); |
| 401 | memcpy(Out, LD.Data.data(), LD.Data.size()); |
| 402 | } |
| 403 | |
| 404 | void MachOWriter::writeCodeSignatureData() { |
| 405 | return writeLinkData(O.CodeSignatureCommandIndex, O.CodeSignature); |
| 406 | } |
| 407 | |
| 408 | void MachOWriter::writeDataInCodeData() { |
| 409 | return writeLinkData(O.DataInCodeCommandIndex, O.DataInCode); |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | void MachOWriter::writeFunctionStartsData() { |
Alexander Shaposhnikov | 913bc31 | 2020-06-15 18:55:59 -0700 | [diff] [blame] | 413 | return writeLinkData(O.FunctionStartsCommandIndex, O.FunctionStarts); |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 416 | void MachOWriter::writeTail() { |
| 417 | typedef void (MachOWriter::*WriteHandlerType)(void); |
| 418 | typedef std::pair<uint64_t, WriteHandlerType> WriteOperation; |
| 419 | SmallVector<WriteOperation, 7> Queue; |
| 420 | |
| 421 | if (O.SymTabCommandIndex) { |
| 422 | const MachO::symtab_command &SymTabCommand = |
| 423 | O.LoadCommands[*O.SymTabCommandIndex] |
| 424 | .MachOLoadCommand.symtab_command_data; |
| 425 | if (SymTabCommand.symoff) |
| 426 | Queue.push_back({SymTabCommand.symoff, &MachOWriter::writeSymbolTable}); |
| 427 | if (SymTabCommand.stroff) |
| 428 | Queue.push_back({SymTabCommand.stroff, &MachOWriter::writeStringTable}); |
| 429 | } |
| 430 | |
| 431 | if (O.DyLdInfoCommandIndex) { |
| 432 | const MachO::dyld_info_command &DyLdInfoCommand = |
| 433 | O.LoadCommands[*O.DyLdInfoCommandIndex] |
| 434 | .MachOLoadCommand.dyld_info_command_data; |
| 435 | if (DyLdInfoCommand.rebase_off) |
| 436 | Queue.push_back( |
| 437 | {DyLdInfoCommand.rebase_off, &MachOWriter::writeRebaseInfo}); |
| 438 | if (DyLdInfoCommand.bind_off) |
| 439 | Queue.push_back({DyLdInfoCommand.bind_off, &MachOWriter::writeBindInfo}); |
| 440 | if (DyLdInfoCommand.weak_bind_off) |
| 441 | Queue.push_back( |
| 442 | {DyLdInfoCommand.weak_bind_off, &MachOWriter::writeWeakBindInfo}); |
| 443 | if (DyLdInfoCommand.lazy_bind_off) |
| 444 | Queue.push_back( |
| 445 | {DyLdInfoCommand.lazy_bind_off, &MachOWriter::writeLazyBindInfo}); |
| 446 | if (DyLdInfoCommand.export_off) |
| 447 | Queue.push_back( |
| 448 | {DyLdInfoCommand.export_off, &MachOWriter::writeExportInfo}); |
| 449 | } |
| 450 | |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 451 | if (O.DySymTabCommandIndex) { |
| 452 | const MachO::dysymtab_command &DySymTabCommand = |
| 453 | O.LoadCommands[*O.DySymTabCommandIndex] |
| 454 | .MachOLoadCommand.dysymtab_command_data; |
| 455 | |
| 456 | if (DySymTabCommand.indirectsymoff) |
| 457 | Queue.emplace_back(DySymTabCommand.indirectsymoff, |
| 458 | &MachOWriter::writeIndirectSymbolTable); |
| 459 | } |
| 460 | |
Alexander Shaposhnikov | 913bc31 | 2020-06-15 18:55:59 -0700 | [diff] [blame] | 461 | if (O.CodeSignatureCommandIndex) { |
| 462 | const MachO::linkedit_data_command &LinkEditDataCommand = |
| 463 | O.LoadCommands[*O.CodeSignatureCommandIndex] |
| 464 | .MachOLoadCommand.linkedit_data_command_data; |
| 465 | |
| 466 | if (LinkEditDataCommand.dataoff) |
| 467 | Queue.emplace_back(LinkEditDataCommand.dataoff, |
| 468 | &MachOWriter::writeCodeSignatureData); |
| 469 | } |
| 470 | |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 471 | if (O.DataInCodeCommandIndex) { |
| 472 | const MachO::linkedit_data_command &LinkEditDataCommand = |
| 473 | O.LoadCommands[*O.DataInCodeCommandIndex] |
| 474 | .MachOLoadCommand.linkedit_data_command_data; |
| 475 | |
| 476 | if (LinkEditDataCommand.dataoff) |
| 477 | Queue.emplace_back(LinkEditDataCommand.dataoff, |
| 478 | &MachOWriter::writeDataInCodeData); |
| 479 | } |
| 480 | |
| 481 | if (O.FunctionStartsCommandIndex) { |
| 482 | const MachO::linkedit_data_command &LinkEditDataCommand = |
| 483 | O.LoadCommands[*O.FunctionStartsCommandIndex] |
| 484 | .MachOLoadCommand.linkedit_data_command_data; |
| 485 | |
| 486 | if (LinkEditDataCommand.dataoff) |
| 487 | Queue.emplace_back(LinkEditDataCommand.dataoff, |
| 488 | &MachOWriter::writeFunctionStartsData); |
| 489 | } |
| 490 | |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 491 | llvm::sort(Queue, [](const WriteOperation &LHS, const WriteOperation &RHS) { |
| 492 | return LHS.first < RHS.first; |
| 493 | }); |
| 494 | |
| 495 | for (auto WriteOp : Queue) |
| 496 | (this->*WriteOp.second)(); |
| 497 | } |
| 498 | |
Seiya Nuta | 552bcb8 | 2019-08-19 21:05:31 +0000 | [diff] [blame] | 499 | Error MachOWriter::finalize() { return LayoutBuilder.layout(); } |
Seiya Nuta | b728e53 | 2019-06-08 01:22:54 +0000 | [diff] [blame] | 500 | |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 501 | Error MachOWriter::write() { |
| 502 | if (Error E = B.allocate(totalSize())) |
| 503 | return E; |
| 504 | memset(B.getBufferStart(), 0, totalSize()); |
| 505 | writeHeader(); |
| 506 | writeLoadCommands(); |
| 507 | writeSections(); |
| 508 | writeTail(); |
Jordan Rupprecht | 2e862c7 | 2019-02-04 19:09:20 +0000 | [diff] [blame] | 509 | return B.commit(); |
Alexander Shaposhnikov | d911ed1 | 2019-02-02 00:38:07 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | } // end namespace macho |
| 513 | } // end namespace objcopy |
| 514 | } // end namespace llvm |