blob: 61cfbf18bdd9b5311216e0227d5de9c5d289657a [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"
15#include "llvm/ObjectYAML/DWARFYAML.h"
16#include "llvm/Support/YAMLTraits.h"
17#include <cstdint>
18#include <string>
19#include <vector>
20
21namespace llvm {
22namespace objcopy {
23namespace macho {
24
25struct MachHeader {
26 uint32_t Magic;
27 uint32_t CPUType;
28 uint32_t CPUSubType;
29 uint32_t FileType;
30 uint32_t NCmds;
31 uint32_t SizeOfCmds;
32 uint32_t Flags;
33 uint32_t Reserved = 0;
34};
35
36struct Section {
37 char Sectname[16];
38 char Segname[16];
39 uint64_t Addr;
40 uint64_t Size;
41 uint32_t Offset;
42 uint32_t Align;
43 uint32_t RelOff;
44 uint32_t NReloc;
45 uint32_t Flags;
46 uint32_t Reserved1;
47 uint32_t Reserved2;
48 uint32_t Reserved3;
49
50 StringRef Content;
51 std::vector<MachO::any_relocation_info> Relocations;
52};
53
54struct LoadCommand {
55 // The type MachO::macho_load_command is defined in llvm/BinaryFormat/MachO.h
56 // and it is a union of all the structs corresponding to various load
57 // commands.
58 MachO::macho_load_command MachOLoadCommand;
59
60 // The raw content of the payload of the load command (located right after the
61 // corresponding struct). In some cases it is either empty or can be
62 // copied-over without digging into its structure.
63 ArrayRef<uint8_t> Payload;
64
65 // Some load commands can contain (inside the payload) an array of sections,
66 // though the contents of the sections are stored separately. The struct
67 // Section describes only sections' metadata and where to find the
68 // corresponding content inside the binary.
69 std::vector<Section> Sections;
70};
71
72struct NListEntry {
73 uint32_t n_strx;
74 uint8_t n_type;
75 uint8_t n_sect;
76 uint16_t n_desc;
77 uint64_t n_value;
78};
79
80/// The location of the symbol table inside the binary is described by LC_SYMTAB
81/// load command.
82struct SymbolTable {
83 std::vector<NListEntry> NameList;
84};
85
86/// The location of the string table inside the binary is described by LC_SYMTAB
87/// load command.
88struct StringTable {
89 std::vector<std::string> Strings;
90};
91
92/// The location of the rebase info inside the binary is described by
93/// LC_DYLD_INFO load command. Dyld rebases an image whenever dyld loads it at
94/// an address different from its preferred address. The rebase information is
95/// a stream of byte sized opcodes whose symbolic names start with
96/// REBASE_OPCODE_. Conceptually the rebase information is a table of tuples:
97/// <seg-index, seg-offset, type>
98/// The opcodes are a compressed way to encode the table by only
99/// encoding when a column changes. In addition simple patterns
100/// like "every n'th offset for m times" can be encoded in a few
101/// bytes.
102struct RebaseInfo {
103 // At the moment we do not parse this info (and it is simply copied over),
104 // but the proper support will be added later.
105 ArrayRef<uint8_t> Opcodes;
106};
107
108/// The location of the bind info inside the binary is described by
109/// LC_DYLD_INFO load command. Dyld binds an image during the loading process,
110/// if the image requires any pointers to be initialized to symbols in other
111/// images. The bind information is a stream of byte sized opcodes whose
112/// symbolic names start with BIND_OPCODE_. Conceptually the bind information is
113/// a table of tuples: <seg-index, seg-offset, type, symbol-library-ordinal,
114/// symbol-name, addend> The opcodes are a compressed way to encode the table by
115/// only encoding when a column changes. In addition simple patterns like for
116/// runs of pointers initialized to the same value can be encoded in a few
117/// bytes.
118struct BindInfo {
119 // At the moment we do not parse this info (and it is simply copied over),
120 // but the proper support will be added later.
121 ArrayRef<uint8_t> Opcodes;
122};
123
124/// The location of the weak bind info inside the binary is described by
125/// LC_DYLD_INFO load command. Some C++ programs require dyld to unique symbols
126/// so that all images in the process use the same copy of some code/data. This
127/// step is done after binding. The content of the weak_bind info is an opcode
128/// stream like the bind_info. But it is sorted alphabetically by symbol name.
129/// This enable dyld to walk all images with weak binding information in order
130/// and look for collisions. If there are no collisions, dyld does no updating.
131/// That means that some fixups are also encoded in the bind_info. For
132/// instance, all calls to "operator new" are first bound to libstdc++.dylib
133/// using the information in bind_info. Then if some image overrides operator
134/// new that is detected when the weak_bind information is processed and the
135/// call to operator new is then rebound.
136struct WeakBindInfo {
137 // At the moment we do not parse this info (and it is simply copied over),
138 // but the proper support will be added later.
139 ArrayRef<uint8_t> Opcodes;
140};
141
142/// The location of the lazy bind info inside the binary is described by
143/// LC_DYLD_INFO load command. Some uses of external symbols do not need to be
144/// bound immediately. Instead they can be lazily bound on first use. The
145/// lazy_bind contains a stream of BIND opcodes to bind all lazy symbols. Normal
146/// use is that dyld ignores the lazy_bind section when loading an image.
147/// Instead the static linker arranged for the lazy pointer to initially point
148/// to a helper function which pushes the offset into the lazy_bind area for the
149/// symbol needing to be bound, then jumps to dyld which simply adds the offset
150/// to lazy_bind_off to get the information on what to bind.
151struct LazyBindInfo {
152 ArrayRef<uint8_t> Opcodes;
153};
154
155/// The location of the export info inside the binary is described by
156/// LC_DYLD_INFO load command. The symbols exported by a dylib are encoded in a
157/// trie. This is a compact representation that factors out common prefixes. It
158/// also reduces LINKEDIT pages in RAM because it encodes all information (name,
159/// address, flags) in one small, contiguous range. The export area is a stream
160/// of nodes. The first node sequentially is the start node for the trie. Nodes
161/// for a symbol start with a uleb128 that is the length of the exported symbol
162/// information for the string so far. If there is no exported symbol, the node
163/// starts with a zero byte. If there is exported info, it follows the length.
164/// First is a uleb128 containing flags. Normally, it is followed by
165/// a uleb128 encoded offset which is location of the content named
166/// by the symbol from the mach_header for the image. If the flags
167/// is EXPORT_SYMBOL_FLAGS_REEXPORT, then following the flags is
168/// a uleb128 encoded library ordinal, then a zero terminated
169/// UTF8 string. If the string is zero length, then the symbol
170/// is re-export from the specified dylib with the same name.
171/// If the flags is EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER, then following
172/// the flags is two uleb128s: the stub offset and the resolver offset.
173/// The stub is used by non-lazy pointers. The resolver is used
174/// by lazy pointers and must be called to get the actual address to use.
175/// After the optional exported symbol information is a byte of
176/// how many edges (0-255) that this node has leaving it,
177/// followed by each edge.
178/// Each edge is a zero terminated UTF8 of the addition chars
179/// in the symbol, followed by a uleb128 offset for the node that
180/// edge points to.
181struct ExportInfo {
182 ArrayRef<uint8_t> Trie;
183};
184
185struct Object {
186 MachHeader Header;
187 std::vector<LoadCommand> LoadCommands;
188
189 SymbolTable SymTable;
190 StringTable StrTable;
191
192 RebaseInfo Rebases;
193 BindInfo Binds;
194 WeakBindInfo WeakBinds;
195 LazyBindInfo LazyBinds;
196 ExportInfo Exports;
197
198 /// The index of LC_SYMTAB load command if present.
199 Optional<size_t> SymTabCommandIndex;
200 /// The index of LC_DYLD_INFO or LC_DYLD_INFO_ONLY load command if present.
201 Optional<size_t> DyLdInfoCommandIndex;
202};
203
204} // end namespace macho
205} // end namespace objcopy
206} // end namespace llvm
207
208#endif // LLVM_OBJCOPY_MACHO_OBJECT_H