blob: ce4bc2e553c18c9b1059045a24946f263256d7b8 [file] [log] [blame]
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00001//===- MachOReader.cpp ------------------------------------------*- C++ -*-===//
2//
Chandler Carruth127252b2019-02-11 08:25:19 +00003// 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 Shaposhnikovd911ed12019-02-02 00:38:07 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "MachOReader.h"
10#include "../llvm-objcopy.h"
11#include "Object.h"
12#include "llvm/BinaryFormat/MachO.h"
13#include "llvm/Object/MachO.h"
14#include <memory>
15
16namespace llvm {
17namespace objcopy {
18namespace macho {
19
20void MachOReader::readHeader(Object &O) const {
21 O.Header.Magic = MachOObj.getHeader().magic;
22 O.Header.CPUType = MachOObj.getHeader().cputype;
23 O.Header.CPUSubType = MachOObj.getHeader().cpusubtype;
24 O.Header.FileType = MachOObj.getHeader().filetype;
25 O.Header.NCmds = MachOObj.getHeader().ncmds;
26 O.Header.SizeOfCmds = MachOObj.getHeader().sizeofcmds;
27 O.Header.Flags = MachOObj.getHeader().flags;
28}
29
30template <typename SectionType>
31Section constructSectionCommon(SectionType Sec) {
32 Section S;
Seiya Nutab728e532019-06-08 01:22:54 +000033 S.Sectname =
34 StringRef(Sec.sectname, strnlen(Sec.sectname, sizeof(Sec.sectname)))
35 .str();
36 S.Segname =
37 StringRef(Sec.segname, strnlen(Sec.segname, sizeof(Sec.sectname))).str();
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000038 S.Addr = Sec.addr;
39 S.Size = Sec.size;
40 S.Offset = Sec.offset;
41 S.Align = Sec.align;
42 S.RelOff = Sec.reloff;
43 S.NReloc = Sec.nreloc;
44 S.Flags = Sec.flags;
45 S.Reserved1 = Sec.reserved1;
46 S.Reserved2 = Sec.reserved2;
47 S.Reserved3 = 0;
48 return S;
49}
50
51template <typename SectionType> Section constructSection(SectionType Sec);
52
53template <> Section constructSection(MachO::section Sec) {
54 return constructSectionCommon(Sec);
55}
56
57template <> Section constructSection(MachO::section_64 Sec) {
58 Section S = constructSectionCommon(Sec);
59 S.Reserved3 = Sec.reserved3;
60 return S;
61}
62
63// TODO: get rid of reportError and make MachOReader return Expected<> instead.
64template <typename SectionType, typename SegmentType>
65std::vector<Section>
66extractSections(const object::MachOObjectFile::LoadCommandInfo &LoadCmd,
67 const object::MachOObjectFile &MachOObj,
68 size_t &NextSectionIndex) {
69 auto End = LoadCmd.Ptr + LoadCmd.C.cmdsize;
70 const SectionType *Curr =
71 reinterpret_cast<const SectionType *>(LoadCmd.Ptr + sizeof(SegmentType));
72 std::vector<Section> Sections;
73 for (; reinterpret_cast<const void *>(Curr) < End; Curr++) {
74 if (MachOObj.isLittleEndian() != sys::IsLittleEndianHost) {
75 SectionType Sec;
76 memcpy((void *)&Sec, Curr, sizeof(SectionType));
77 MachO::swapStruct(Sec);
78 Sections.push_back(constructSection(Sec));
79 } else {
80 Sections.push_back(constructSection(*Curr));
81 }
82
83 Section &S = Sections.back();
84
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000085 Expected<object::SectionRef> SecRef =
86 MachOObj.getSection(NextSectionIndex++);
87 if (!SecRef)
88 reportError(MachOObj.getFileName(), SecRef.takeError());
89
Fangrui Songe1cb2c02019-05-14 04:22:51 +000090 if (Expected<ArrayRef<uint8_t>> E =
91 MachOObj.getSectionContents(SecRef->getRawDataRefImpl()))
92 S.Content =
93 StringRef(reinterpret_cast<const char *>(E->data()), E->size());
94 else
95 reportError(MachOObj.getFileName(), E.takeError());
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000096
97 S.Relocations.reserve(S.NReloc);
98 for (auto RI = MachOObj.section_rel_begin(SecRef->getRawDataRefImpl()),
99 RE = MachOObj.section_rel_end(SecRef->getRawDataRefImpl());
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000100 RI != RE; ++RI) {
101 RelocationInfo R;
102 R.Symbol = nullptr; // We'll fill this field later.
103 R.Info = MachOObj.getRelocation(RI->getRawDataRefImpl());
104 R.Scattered =
105 reinterpret_cast<MachO::scattered_relocation_info *>(&R.Info)
106 ->r_scattered;
107 S.Relocations.push_back(R);
108 }
109
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000110 assert(S.NReloc == S.Relocations.size() &&
111 "Incorrect number of relocations");
112 }
113 return Sections;
114}
115
116void MachOReader::readLoadCommands(Object &O) const {
117 // For MachO sections indices start from 1.
118 size_t NextSectionIndex = 1;
119 for (auto LoadCmd : MachOObj.load_commands()) {
120 LoadCommand LC;
121 switch (LoadCmd.C.cmd) {
122 case MachO::LC_SEGMENT:
123 LC.Sections = extractSections<MachO::section, MachO::segment_command>(
124 LoadCmd, MachOObj, NextSectionIndex);
125 break;
126 case MachO::LC_SEGMENT_64:
127 LC.Sections =
128 extractSections<MachO::section_64, MachO::segment_command_64>(
129 LoadCmd, MachOObj, NextSectionIndex);
130 break;
131 case MachO::LC_SYMTAB:
132 O.SymTabCommandIndex = O.LoadCommands.size();
133 break;
134 case MachO::LC_DYLD_INFO:
135 case MachO::LC_DYLD_INFO_ONLY:
136 O.DyLdInfoCommandIndex = O.LoadCommands.size();
137 break;
138 }
139#define HANDLE_LOAD_COMMAND(LCName, LCValue, LCStruct) \
140 case MachO::LCName: \
141 memcpy((void *)&(LC.MachOLoadCommand.LCStruct##_data), LoadCmd.Ptr, \
142 sizeof(MachO::LCStruct)); \
143 if (MachOObj.isLittleEndian() != sys::IsLittleEndianHost) \
144 MachO::swapStruct(LC.MachOLoadCommand.LCStruct##_data); \
145 LC.Payload = ArrayRef<uint8_t>( \
146 reinterpret_cast<uint8_t *>(const_cast<char *>(LoadCmd.Ptr)) + \
147 sizeof(MachO::LCStruct), \
148 LoadCmd.C.cmdsize - sizeof(MachO::LCStruct)); \
149 break;
150
151 switch (LoadCmd.C.cmd) {
152 default:
153 memcpy((void *)&(LC.MachOLoadCommand.load_command_data), LoadCmd.Ptr,
154 sizeof(MachO::load_command));
155 if (MachOObj.isLittleEndian() != sys::IsLittleEndianHost)
156 MachO::swapStruct(LC.MachOLoadCommand.load_command_data);
157 LC.Payload = ArrayRef<uint8_t>(
158 reinterpret_cast<uint8_t *>(const_cast<char *>(LoadCmd.Ptr)) +
159 sizeof(MachO::load_command),
160 LoadCmd.C.cmdsize - sizeof(MachO::load_command));
161 break;
162#include "llvm/BinaryFormat/MachO.def"
163 }
164 O.LoadCommands.push_back(std::move(LC));
165 }
166}
167
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000168template <typename nlist_t>
169SymbolEntry constructSymbolEntry(StringRef StrTable, const nlist_t &nlist) {
170 assert(nlist.n_strx < StrTable.size() &&
171 "n_strx exceeds the size of the string table");
172 SymbolEntry SE;
173 SE.Name = StringRef(StrTable.data() + nlist.n_strx).str();
174 SE.n_type = nlist.n_type;
175 SE.n_sect = nlist.n_sect;
176 SE.n_desc = nlist.n_desc;
177 SE.n_value = nlist.n_value;
178 return SE;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000179}
180
181void MachOReader::readSymbolTable(Object &O) const {
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000182 StringRef StrTable = MachOObj.getStringTableData();
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000183 for (auto Symbol : MachOObj.symbols()) {
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000184 SymbolEntry SE =
185 (MachOObj.is64Bit()
186 ? constructSymbolEntry(
187 StrTable,
188 MachOObj.getSymbol64TableEntry(Symbol.getRawDataRefImpl()))
189 : constructSymbolEntry(
190 StrTable,
191 MachOObj.getSymbolTableEntry(Symbol.getRawDataRefImpl())));
192
193 O.SymTable.Symbols.push_back(llvm::make_unique<SymbolEntry>(SE));
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000194 }
Clement Courbetbe16b802019-02-04 10:24:42 +0000195}
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000196
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000197void MachOReader::setSymbolInRelocationInfo(Object &O) const {
198 for (auto &LC : O.LoadCommands)
199 for (auto &Sec : LC.Sections)
200 for (auto &Reloc : Sec.Relocations)
201 if (!Reloc.Scattered) {
202 auto *Info = reinterpret_cast<MachO::relocation_info *>(&Reloc.Info);
203 Reloc.Symbol = O.SymTable.getSymbolByIndex(Info->r_symbolnum);
204 }
Clement Courbetbe16b802019-02-04 10:24:42 +0000205}
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000206
207void MachOReader::readRebaseInfo(Object &O) const {
208 O.Rebases.Opcodes = MachOObj.getDyldInfoRebaseOpcodes();
209}
210
211void MachOReader::readBindInfo(Object &O) const {
212 O.Binds.Opcodes = MachOObj.getDyldInfoBindOpcodes();
213}
214
215void MachOReader::readWeakBindInfo(Object &O) const {
216 O.WeakBinds.Opcodes = MachOObj.getDyldInfoWeakBindOpcodes();
217}
218
219void MachOReader::readLazyBindInfo(Object &O) const {
220 O.LazyBinds.Opcodes = MachOObj.getDyldInfoLazyBindOpcodes();
221}
222
223void MachOReader::readExportInfo(Object &O) const {
224 O.Exports.Trie = MachOObj.getDyldInfoExportsTrie();
225}
226
227std::unique_ptr<Object> MachOReader::create() const {
228 auto Obj = llvm::make_unique<Object>();
229 readHeader(*Obj);
230 readLoadCommands(*Obj);
231 readSymbolTable(*Obj);
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000232 setSymbolInRelocationInfo(*Obj);
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000233 readRebaseInfo(*Obj);
234 readBindInfo(*Obj);
235 readWeakBindInfo(*Obj);
236 readLazyBindInfo(*Obj);
237 readExportInfo(*Obj);
238 return Obj;
239}
240
241} // end namespace macho
242} // end namespace objcopy
243} // end namespace llvm