blob: 36b0f7eb96dda424e6a525d9c34d6c4607005a34 [file] [log] [blame]
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +00001//===- Object.h - Mach-O object file model ----------------------*- 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#ifndef LLVM_OBJCOPY_MACHO_OBJECT_H
10#define LLVM_OBJCOPY_MACHO_OBJECT_H
11
12#include "llvm/ADT/Optional.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/BinaryFormat/MachO.h"
Seiya Nutaf923d9b2019-06-21 00:21:50 +000015#include "llvm/MC/StringTableBuilder.h"
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000016#include "llvm/ObjectYAML/DWARFYAML.h"
17#include "llvm/Support/YAMLTraits.h"
18#include <cstdint>
19#include <string>
20#include <vector>
21
22namespace llvm {
23namespace objcopy {
24namespace macho {
25
26struct MachHeader {
27 uint32_t Magic;
28 uint32_t CPUType;
29 uint32_t CPUSubType;
30 uint32_t FileType;
31 uint32_t NCmds;
32 uint32_t SizeOfCmds;
33 uint32_t Flags;
34 uint32_t Reserved = 0;
35};
36
Seiya Nutaf923d9b2019-06-21 00:21:50 +000037struct RelocationInfo;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000038struct Section {
Seiya Nutab728e532019-06-08 01:22:54 +000039 std::string Sectname;
40 std::string Segname;
Seiya Nuta7f19dd12019-10-28 15:40:37 +090041 // CanonicalName is a string formatted as “<Segname>,<Sectname>".
42 std::string CanonicalName;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000043 uint64_t Addr;
44 uint64_t Size;
45 uint32_t Offset;
46 uint32_t Align;
47 uint32_t RelOff;
48 uint32_t NReloc;
49 uint32_t Flags;
50 uint32_t Reserved1;
51 uint32_t Reserved2;
52 uint32_t Reserved3;
53
54 StringRef Content;
Seiya Nutaf923d9b2019-06-21 00:21:50 +000055 std::vector<RelocationInfo> Relocations;
Seiya Nutab728e532019-06-08 01:22:54 +000056
57 MachO::SectionType getType() const {
58 return static_cast<MachO::SectionType>(Flags & MachO::SECTION_TYPE);
59 }
60
61 bool isVirtualSection() const {
62 return (getType() == MachO::S_ZEROFILL ||
63 getType() == MachO::S_GB_ZEROFILL ||
64 getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
65 }
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000066};
67
68struct LoadCommand {
69 // The type MachO::macho_load_command is defined in llvm/BinaryFormat/MachO.h
70 // and it is a union of all the structs corresponding to various load
71 // commands.
72 MachO::macho_load_command MachOLoadCommand;
73
74 // The raw content of the payload of the load command (located right after the
75 // corresponding struct). In some cases it is either empty or can be
76 // copied-over without digging into its structure.
77 ArrayRef<uint8_t> Payload;
78
79 // Some load commands can contain (inside the payload) an array of sections,
80 // though the contents of the sections are stored separately. The struct
81 // Section describes only sections' metadata and where to find the
82 // corresponding content inside the binary.
83 std::vector<Section> Sections;
84};
85
Seiya Nutaf923d9b2019-06-21 00:21:50 +000086// A symbol information. Fields which starts with "n_" are same as them in the
87// nlist.
88struct SymbolEntry {
89 std::string Name;
90 uint32_t Index;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +000091 uint8_t n_type;
92 uint8_t n_sect;
93 uint16_t n_desc;
94 uint64_t n_value;
Seiya Nuta552bcb82019-08-19 21:05:31 +000095
96 bool isExternalSymbol() const {
97 return n_type & ((MachO::N_EXT | MachO::N_PEXT));
98 }
99
100 bool isLocalSymbol() const { return !isExternalSymbol(); }
101
102 bool isUndefinedSymbol() const {
103 return (n_type & MachO::N_TYPE) == MachO::N_UNDF;
104 }
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000105};
106
107/// The location of the symbol table inside the binary is described by LC_SYMTAB
108/// load command.
109struct SymbolTable {
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000110 std::vector<std::unique_ptr<SymbolEntry>> Symbols;
111
112 const SymbolEntry *getSymbolByIndex(uint32_t Index) const;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000113};
114
Seiya Nuta552bcb82019-08-19 21:05:31 +0000115struct IndirectSymbolTable {
116 std::vector<uint32_t> Symbols;
117};
118
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000119/// The location of the string table inside the binary is described by LC_SYMTAB
120/// load command.
121struct StringTable {
122 std::vector<std::string> Strings;
123};
124
Seiya Nutaf923d9b2019-06-21 00:21:50 +0000125struct RelocationInfo {
126 const SymbolEntry *Symbol;
127 // True if Info is a scattered_relocation_info.
128 bool Scattered;
129 MachO::any_relocation_info Info;
130};
131
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000132/// The location of the rebase info inside the binary is described by
133/// LC_DYLD_INFO load command. Dyld rebases an image whenever dyld loads it at
134/// an address different from its preferred address. The rebase information is
135/// a stream of byte sized opcodes whose symbolic names start with
136/// REBASE_OPCODE_. Conceptually the rebase information is a table of tuples:
137/// <seg-index, seg-offset, type>
138/// The opcodes are a compressed way to encode the table by only
139/// encoding when a column changes. In addition simple patterns
140/// like "every n'th offset for m times" can be encoded in a few
141/// bytes.
142struct RebaseInfo {
143 // At the moment we do not parse this info (and it is simply copied over),
144 // but the proper support will be added later.
145 ArrayRef<uint8_t> Opcodes;
146};
147
148/// The location of the bind info inside the binary is described by
149/// LC_DYLD_INFO load command. Dyld binds an image during the loading process,
150/// if the image requires any pointers to be initialized to symbols in other
151/// images. The bind information is a stream of byte sized opcodes whose
152/// symbolic names start with BIND_OPCODE_. Conceptually the bind information is
153/// a table of tuples: <seg-index, seg-offset, type, symbol-library-ordinal,
154/// symbol-name, addend> The opcodes are a compressed way to encode the table by
155/// only encoding when a column changes. In addition simple patterns like for
156/// runs of pointers initialized to the same value can be encoded in a few
157/// bytes.
158struct BindInfo {
159 // At the moment we do not parse this info (and it is simply copied over),
160 // but the proper support will be added later.
161 ArrayRef<uint8_t> Opcodes;
162};
163
164/// The location of the weak bind info inside the binary is described by
165/// LC_DYLD_INFO load command. Some C++ programs require dyld to unique symbols
166/// so that all images in the process use the same copy of some code/data. This
167/// step is done after binding. The content of the weak_bind info is an opcode
168/// stream like the bind_info. But it is sorted alphabetically by symbol name.
169/// This enable dyld to walk all images with weak binding information in order
170/// and look for collisions. If there are no collisions, dyld does no updating.
171/// That means that some fixups are also encoded in the bind_info. For
172/// instance, all calls to "operator new" are first bound to libstdc++.dylib
173/// using the information in bind_info. Then if some image overrides operator
174/// new that is detected when the weak_bind information is processed and the
175/// call to operator new is then rebound.
176struct WeakBindInfo {
177 // At the moment we do not parse this info (and it is simply copied over),
178 // but the proper support will be added later.
179 ArrayRef<uint8_t> Opcodes;
180};
181
182/// The location of the lazy bind info inside the binary is described by
183/// LC_DYLD_INFO load command. Some uses of external symbols do not need to be
184/// bound immediately. Instead they can be lazily bound on first use. The
185/// lazy_bind contains a stream of BIND opcodes to bind all lazy symbols. Normal
186/// use is that dyld ignores the lazy_bind section when loading an image.
187/// Instead the static linker arranged for the lazy pointer to initially point
188/// to a helper function which pushes the offset into the lazy_bind area for the
189/// symbol needing to be bound, then jumps to dyld which simply adds the offset
190/// to lazy_bind_off to get the information on what to bind.
191struct LazyBindInfo {
192 ArrayRef<uint8_t> Opcodes;
193};
194
195/// The location of the export info inside the binary is described by
196/// LC_DYLD_INFO load command. The symbols exported by a dylib are encoded in a
197/// trie. This is a compact representation that factors out common prefixes. It
198/// also reduces LINKEDIT pages in RAM because it encodes all information (name,
199/// address, flags) in one small, contiguous range. The export area is a stream
200/// of nodes. The first node sequentially is the start node for the trie. Nodes
201/// for a symbol start with a uleb128 that is the length of the exported symbol
202/// information for the string so far. If there is no exported symbol, the node
203/// starts with a zero byte. If there is exported info, it follows the length.
204/// First is a uleb128 containing flags. Normally, it is followed by
205/// a uleb128 encoded offset which is location of the content named
206/// by the symbol from the mach_header for the image. If the flags
207/// is EXPORT_SYMBOL_FLAGS_REEXPORT, then following the flags is
208/// a uleb128 encoded library ordinal, then a zero terminated
209/// UTF8 string. If the string is zero length, then the symbol
210/// is re-export from the specified dylib with the same name.
211/// If the flags is EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER, then following
212/// the flags is two uleb128s: the stub offset and the resolver offset.
213/// The stub is used by non-lazy pointers. The resolver is used
214/// by lazy pointers and must be called to get the actual address to use.
215/// After the optional exported symbol information is a byte of
216/// how many edges (0-255) that this node has leaving it,
217/// followed by each edge.
218/// Each edge is a zero terminated UTF8 of the addition chars
219/// in the symbol, followed by a uleb128 offset for the node that
220/// edge points to.
221struct ExportInfo {
222 ArrayRef<uint8_t> Trie;
223};
224
Seiya Nuta552bcb82019-08-19 21:05:31 +0000225struct LinkData {
226 ArrayRef<uint8_t> Data;
227};
228
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000229struct Object {
230 MachHeader Header;
231 std::vector<LoadCommand> LoadCommands;
232
233 SymbolTable SymTable;
234 StringTable StrTable;
235
236 RebaseInfo Rebases;
237 BindInfo Binds;
238 WeakBindInfo WeakBinds;
239 LazyBindInfo LazyBinds;
240 ExportInfo Exports;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000241 IndirectSymbolTable IndirectSymTable;
242 LinkData DataInCode;
243 LinkData FunctionStarts;
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000244
245 /// The index of LC_SYMTAB load command if present.
246 Optional<size_t> SymTabCommandIndex;
247 /// The index of LC_DYLD_INFO or LC_DYLD_INFO_ONLY load command if present.
248 Optional<size_t> DyLdInfoCommandIndex;
Seiya Nuta552bcb82019-08-19 21:05:31 +0000249 /// The index LC_DYSYMTAB load comamnd if present.
250 Optional<size_t> DySymTabCommandIndex;
251 /// The index LC_DATA_IN_CODE load comamnd if present.
252 Optional<size_t> DataInCodeCommandIndex;
253 /// The index LC_FUNCTION_STARTS load comamnd if present.
254 Optional<size_t> FunctionStartsCommandIndex;
Seiya Nuta7f19dd12019-10-28 15:40:37 +0900255
256 void removeSections(function_ref<bool(const Section &)> ToRemove);
Alexander Shaposhnikovd911ed12019-02-02 00:38:07 +0000257};
258
259} // end namespace macho
260} // end namespace objcopy
261} // end namespace llvm
262
263#endif // LLVM_OBJCOPY_MACHO_OBJECT_H