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