1 //===- Object.h - Mach-O object file model ----------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_OBJCOPY_MACHO_OBJECT_H
10 #define LLVM_OBJCOPY_MACHO_OBJECT_H
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"
34 uint32_t Reserved
= 0;
37 struct RelocationInfo
;
53 std::vector
<RelocationInfo
> Relocations
;
55 MachO::SectionType
getType() const {
56 return static_cast<MachO::SectionType
>(Flags
& MachO::SECTION_TYPE
);
59 bool isVirtualSection() const {
60 return (getType() == MachO::S_ZEROFILL
||
61 getType() == MachO::S_GB_ZEROFILL
||
62 getType() == MachO::S_THREAD_LOCAL_ZEROFILL
);
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
70 MachO::macho_load_command MachOLoadCommand
;
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
;
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
;
84 // A symbol information. Fields which starts with "n_" are same as them in the
95 /// The location of the symbol table inside the binary is described by LC_SYMTAB
98 std::vector
<std::unique_ptr
<SymbolEntry
>> Symbols
;
100 const SymbolEntry
*getSymbolByIndex(uint32_t Index
) const;
103 /// The location of the string table inside the binary is described by LC_SYMTAB
106 std::vector
<std::string
> Strings
;
109 struct RelocationInfo
{
110 const SymbolEntry
*Symbol
;
111 // True if Info is a scattered_relocation_info.
113 MachO::any_relocation_info Info
;
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
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
;
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
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
;
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
;
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
;
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
206 ArrayRef
<uint8_t> Trie
;
211 std::vector
<LoadCommand
> LoadCommands
;
213 SymbolTable SymTable
;
214 StringTable StrTable
;
218 WeakBindInfo WeakBinds
;
219 LazyBindInfo LazyBinds
;
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
;
228 } // end namespace macho
229 } // end namespace objcopy
230 } // end namespace llvm
232 #endif // LLVM_OBJCOPY_MACHO_OBJECT_H