LLVM 20.0.0git
MachOObject.h
Go to the documentation of this file.
1//===- MachOObject.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_LIB_OBJCOPY_MACHO_MACHOOBJECT_H
10#define LLVM_LIB_OBJCOPY_MACHO_MACHOOBJECT_H
11
12#include "llvm/ADT/StringRef.h"
18#include <cstdint>
19#include <string>
20#include <vector>
21
22namespace llvm {
23namespace objcopy {
24namespace macho {
25
26struct MachHeader {
35};
36
37struct RelocationInfo;
38struct Section {
40 std::string Segname;
41 std::string Sectname;
42 // CanonicalName is a string formatted as “<Segname>,<Sectname>".
43 std::string CanonicalName;
46 // Offset in the input file.
47 std::optional<uint32_t> OriginalOffset;
57 std::vector<RelocationInfo> Relocations;
58
59 Section(StringRef SegName, StringRef SectName);
60
61 Section(StringRef SegName, StringRef SectName, StringRef Content);
62
64 return static_cast<MachO::SectionType>(Flags & MachO::SECTION_TYPE);
65 }
66
67 bool isVirtualSection() const {
68 return (getType() == MachO::S_ZEROFILL ||
71 }
72
73 bool hasValidOffset() const {
74 return !(isVirtualSection() || (OriginalOffset && *OriginalOffset == 0));
75 }
76};
77
79 // The type MachO::macho_load_command is defined in llvm/BinaryFormat/MachO.h
80 // and it is a union of all the structs corresponding to various load
81 // commands.
83
84 // The raw content of the payload of the load command (located right after the
85 // corresponding struct). In some cases it is either empty or can be
86 // copied-over without digging into its structure.
87 std::vector<uint8_t> Payload;
88
89 // Some load commands can contain (inside the payload) an array of sections,
90 // though the contents of the sections are stored separately. The struct
91 // Section describes only sections' metadata and where to find the
92 // corresponding content inside the binary.
93 std::vector<std::unique_ptr<Section>> Sections;
94
95 // Returns the segment name if the load command is a segment command.
96 std::optional<StringRef> getSegmentName() const;
97
98 // Returns the segment vm address if the load command is a segment command.
99 std::optional<uint64_t> getSegmentVMAddr() const;
100};
101
102// A symbol information. Fields which starts with "n_" are same as them in the
103// nlist.
105 std::string Name;
106 bool Referenced = false;
112
113 bool isExternalSymbol() const { return n_type & MachO::N_EXT; }
114
115 bool isLocalSymbol() const { return !isExternalSymbol(); }
116
117 bool isUndefinedSymbol() const {
118 return (n_type & MachO::N_TYPE) == MachO::N_UNDF;
119 }
120
121 bool isSwiftSymbol() const {
122 return StringRef(Name).starts_with("_$s") ||
123 StringRef(Name).starts_with("_$S");
124 }
125
126 std::optional<uint32_t> section() const {
127 return n_sect == MachO::NO_SECT ? std::nullopt
128 : std::optional<uint32_t>(n_sect);
129 }
130};
131
132/// The location of the symbol table inside the binary is described by LC_SYMTAB
133/// load command.
135 std::vector<std::unique_ptr<SymbolEntry>> Symbols;
136
138 std::vector<std::unique_ptr<SymbolEntry>>::const_iterator>;
139
140 iterator begin() const { return iterator(Symbols.begin()); }
141 iterator end() const { return iterator(Symbols.end()); }
142
145 void updateSymbols(function_ref<void(SymbolEntry &)> Callable);
146 void removeSymbols(
147 function_ref<bool(const std::unique_ptr<SymbolEntry> &)> ToRemove);
148};
149
151 // The original value in an indirect symbol table. Higher bits encode extra
152 // information (INDIRECT_SYMBOL_LOCAL and INDIRECT_SYMBOL_ABS).
154 /// The Symbol referenced by this entry. It's std::nullopt if the index is
155 /// INDIRECT_SYMBOL_LOCAL or INDIRECT_SYMBOL_ABS.
156 std::optional<SymbolEntry *> Symbol;
157
159 std::optional<SymbolEntry *> Symbol)
161};
162
164 std::vector<IndirectSymbolEntry> Symbols;
165};
166
167/// The location of the string table inside the binary is described by LC_SYMTAB
168/// load command.
170 std::vector<std::string> Strings;
171};
172
174 // The referenced symbol entry. Set if !Scattered && Extern.
175 std::optional<const SymbolEntry *> Symbol;
176 // The referenced section. Set if !Scattered && !Extern.
177 std::optional<const Section *> Sec;
178 // True if Info is a scattered_relocation_info.
180 // True if the type is an ADDEND. r_symbolnum holds the addend instead of a
181 // symbol index.
183 // True if the r_symbolnum points to a section number (i.e. r_extern=0).
184 bool Extern;
186
187 unsigned getPlainRelocationSymbolNum(bool IsLittleEndian) {
188 if (IsLittleEndian)
189 return Info.r_word1 & 0xffffff;
190 return Info.r_word1 >> 8;
191 }
192
193 void setPlainRelocationSymbolNum(unsigned SymbolNum, bool IsLittleEndian) {
194 assert(SymbolNum < (1 << 24) && "SymbolNum out of range");
195 if (IsLittleEndian)
196 Info.r_word1 = (Info.r_word1 & ~0x00ffffff) | SymbolNum;
197 else
198 Info.r_word1 = (Info.r_word1 & ~0xffffff00) | (SymbolNum << 8);
199 }
200};
201
202/// The location of the rebase info inside the binary is described by
203/// LC_DYLD_INFO load command. Dyld rebases an image whenever dyld loads it at
204/// an address different from its preferred address. The rebase information is
205/// a stream of byte sized opcodes whose symbolic names start with
206/// REBASE_OPCODE_. Conceptually the rebase information is a table of tuples:
207/// <seg-index, seg-offset, type>
208/// The opcodes are a compressed way to encode the table by only
209/// encoding when a column changes. In addition simple patterns
210/// like "every n'th offset for m times" can be encoded in a few
211/// bytes.
213 // At the moment we do not parse this info (and it is simply copied over),
214 // but the proper support will be added later.
216};
217
218/// The location of the bind info inside the binary is described by
219/// LC_DYLD_INFO load command. Dyld binds an image during the loading process,
220/// if the image requires any pointers to be initialized to symbols in other
221/// images. The bind information is a stream of byte sized opcodes whose
222/// symbolic names start with BIND_OPCODE_. Conceptually the bind information is
223/// a table of tuples: <seg-index, seg-offset, type, symbol-library-ordinal,
224/// symbol-name, addend> The opcodes are a compressed way to encode the table by
225/// only encoding when a column changes. In addition simple patterns like for
226/// runs of pointers initialized to the same value can be encoded in a few
227/// bytes.
228struct BindInfo {
229 // At the moment we do not parse this info (and it is simply copied over),
230 // but the proper support will be added later.
232};
233
234/// The location of the weak bind info inside the binary is described by
235/// LC_DYLD_INFO load command. Some C++ programs require dyld to unique symbols
236/// so that all images in the process use the same copy of some code/data. This
237/// step is done after binding. The content of the weak_bind info is an opcode
238/// stream like the bind_info. But it is sorted alphabetically by symbol name.
239/// This enable dyld to walk all images with weak binding information in order
240/// and look for collisions. If there are no collisions, dyld does no updating.
241/// That means that some fixups are also encoded in the bind_info. For
242/// instance, all calls to "operator new" are first bound to libstdc++.dylib
243/// using the information in bind_info. Then if some image overrides operator
244/// new that is detected when the weak_bind information is processed and the
245/// call to operator new is then rebound.
247 // At the moment we do not parse this info (and it is simply copied over),
248 // but the proper support will be added later.
250};
251
252/// The location of the lazy bind info inside the binary is described by
253/// LC_DYLD_INFO load command. Some uses of external symbols do not need to be
254/// bound immediately. Instead they can be lazily bound on first use. The
255/// lazy_bind contains a stream of BIND opcodes to bind all lazy symbols. Normal
256/// use is that dyld ignores the lazy_bind section when loading an image.
257/// Instead the static linker arranged for the lazy pointer to initially point
258/// to a helper function which pushes the offset into the lazy_bind area for the
259/// symbol needing to be bound, then jumps to dyld which simply adds the offset
260/// to lazy_bind_off to get the information on what to bind.
263};
264
265/// The location of the export info inside the binary is described by
266/// LC_DYLD_INFO load command. The symbols exported by a dylib are encoded in a
267/// trie. This is a compact representation that factors out common prefixes. It
268/// also reduces LINKEDIT pages in RAM because it encodes all information (name,
269/// address, flags) in one small, contiguous range. The export area is a stream
270/// of nodes. The first node sequentially is the start node for the trie. Nodes
271/// for a symbol start with a uleb128 that is the length of the exported symbol
272/// information for the string so far. If there is no exported symbol, the node
273/// starts with a zero byte. If there is exported info, it follows the length.
274/// First is a uleb128 containing flags. Normally, it is followed by
275/// a uleb128 encoded offset which is location of the content named
276/// by the symbol from the mach_header for the image. If the flags
277/// is EXPORT_SYMBOL_FLAGS_REEXPORT, then following the flags is
278/// a uleb128 encoded library ordinal, then a zero terminated
279/// UTF8 string. If the string is zero length, then the symbol
280/// is re-export from the specified dylib with the same name.
281/// If the flags is EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER, then following
282/// the flags is two uleb128s: the stub offset and the resolver offset.
283/// The stub is used by non-lazy pointers. The resolver is used
284/// by lazy pointers and must be called to get the actual address to use.
285/// After the optional exported symbol information is a byte of
286/// how many edges (0-255) that this node has leaving it,
287/// followed by each edge.
288/// Each edge is a zero terminated UTF8 of the addition chars
289/// in the symbol, followed by a uleb128 offset for the node that
290/// edge points to.
293};
294
295struct LinkData {
297};
298
299struct Object {
301 std::vector<LoadCommand> LoadCommands;
302
305
318
319 std::optional<uint32_t> SwiftVersion;
320
321 /// The index of LC_CODE_SIGNATURE load command if present.
322 std::optional<size_t> CodeSignatureCommandIndex;
323 /// The index of LC_DYLIB_CODE_SIGN_DRS load command if present.
324 std::optional<size_t> DylibCodeSignDRsIndex;
325 /// The index of LC_SYMTAB load command if present.
326 std::optional<size_t> SymTabCommandIndex;
327 /// The index of LC_DYLD_INFO or LC_DYLD_INFO_ONLY load command if present.
328 std::optional<size_t> DyLdInfoCommandIndex;
329 /// The index LC_DYSYMTAB load command if present.
330 std::optional<size_t> DySymTabCommandIndex;
331 /// The index LC_DATA_IN_CODE load command if present.
332 std::optional<size_t> DataInCodeCommandIndex;
333 /// The index of LC_LINKER_OPTIMIZATIN_HINT load command if present.
335 /// The index LC_FUNCTION_STARTS load command if present.
336 std::optional<size_t> FunctionStartsCommandIndex;
337 /// The index LC_DYLD_CHAINED_FIXUPS load command if present.
338 std::optional<size_t> ChainedFixupsCommandIndex;
339 /// The index LC_DYLD_EXPORTS_TRIE load command if present.
340 std::optional<size_t> ExportsTrieCommandIndex;
341 /// The index of the LC_SEGMENT or LC_SEGMENT_64 load command
342 /// corresponding to the __TEXT segment.
343 std::optional<size_t> TextSegmentCommandIndex;
344
347
349
350 Error
351 removeSections(function_ref<bool(const std::unique_ptr<Section> &)> ToRemove);
352
354
356
357 /// Creates a new segment load command in the object and returns a reference
358 /// to the newly created load command. The caller should verify that SegName
359 /// is not too long (SegName.size() should be less than or equal to 16).
360 LoadCommand &addSegment(StringRef SegName, uint64_t SegVMSize);
361
362 bool is64Bit() const {
363 return Header.Magic == MachO::MH_MAGIC_64 ||
365 }
366
368};
369
370} // end namespace macho
371} // end namespace objcopy
372} // end namespace llvm
373
374#endif // LLVM_LIB_OBJCOPY_MACHO_MACHOOBJECT_H
ReachingDefAnalysis InstSet & ToRemove
This file declares classes for handling the YAML representation of DWARF Debug Info.
uint32_t Index
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:66
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:265
Saves strings in the provided stable storage and returns a StringRef with a stable character pointer.
Definition: StringSaver.h:21
An efficient, type-erasing, non-owning reference to a callable.
@ MH_CIGAM_64
Definition: MachO.h:33
@ MH_MAGIC_64
Definition: MachO.h:32
@ SECTION_TYPE
Definition: MachO.h:114
@ NO_SECT
Definition: MachO.h:326
@ N_TYPE
Definition: MachO.h:309
SectionType
These are the section type and attributes fields.
Definition: MachO.h:122
@ S_GB_ZEROFILL
S_GB_ZEROFILL - Zero fill on demand section (that can be larger than 4 gigabytes).
Definition: MachO.h:155
@ S_THREAD_LOCAL_ZEROFILL
S_THREAD_LOCAL_ZEROFILL - Thread local zerofill section.
Definition: MachO.h:169
@ S_ZEROFILL
S_ZEROFILL - Zero fill on demand section.
Definition: MachO.h:129
@ N_UNDF
Definition: MachO.h:316
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
The location of the bind info inside the binary is described by LC_DYLD_INFO load command.
Definition: MachOObject.h:228
ArrayRef< uint8_t > Opcodes
Definition: MachOObject.h:231
The location of the export info inside the binary is described by LC_DYLD_INFO load command.
Definition: MachOObject.h:291
std::optional< SymbolEntry * > Symbol
The Symbol referenced by this entry.
Definition: MachOObject.h:156
IndirectSymbolEntry(uint32_t OriginalIndex, std::optional< SymbolEntry * > Symbol)
Definition: MachOObject.h:158
std::vector< IndirectSymbolEntry > Symbols
Definition: MachOObject.h:164
The location of the lazy bind info inside the binary is described by LC_DYLD_INFO load command.
Definition: MachOObject.h:261
ArrayRef< uint8_t > Data
Definition: MachOObject.h:296
MachO::macho_load_command MachOLoadCommand
Definition: MachOObject.h:82
std::optional< StringRef > getSegmentName() const
std::vector< std::unique_ptr< Section > > Sections
Definition: MachOObject.h:93
std::optional< uint64_t > getSegmentVMAddr() const
std::vector< uint8_t > Payload
Definition: MachOObject.h:87
std::optional< size_t > FunctionStartsCommandIndex
The index LC_FUNCTION_STARTS load command if present.
Definition: MachOObject.h:336
Error removeLoadCommands(function_ref< bool(const LoadCommand &)> ToRemove)
std::optional< size_t > ChainedFixupsCommandIndex
The index LC_DYLD_CHAINED_FIXUPS load command if present.
Definition: MachOObject.h:338
std::optional< size_t > ExportsTrieCommandIndex
The index LC_DYLD_EXPORTS_TRIE load command if present.
Definition: MachOObject.h:340
Error removeSections(function_ref< bool(const std::unique_ptr< Section > &)> ToRemove)
std::optional< size_t > DylibCodeSignDRsIndex
The index of LC_DYLIB_CODE_SIGN_DRS load command if present.
Definition: MachOObject.h:324
std::optional< size_t > SymTabCommandIndex
The index of LC_SYMTAB load command if present.
Definition: MachOObject.h:326
std::optional< size_t > DyLdInfoCommandIndex
The index of LC_DYLD_INFO or LC_DYLD_INFO_ONLY load command if present.
Definition: MachOObject.h:328
std::vector< LoadCommand > LoadCommands
Definition: MachOObject.h:301
std::optional< size_t > DataInCodeCommandIndex
The index LC_DATA_IN_CODE load command if present.
Definition: MachOObject.h:332
std::optional< uint32_t > SwiftVersion
Definition: MachOObject.h:319
std::optional< size_t > DySymTabCommandIndex
The index LC_DYSYMTAB load command if present.
Definition: MachOObject.h:330
std::optional< size_t > TextSegmentCommandIndex
The index of the LC_SEGMENT or LC_SEGMENT_64 load command corresponding to the __TEXT segment.
Definition: MachOObject.h:343
IndirectSymbolTable IndirectSymTable
Definition: MachOObject.h:311
uint64_t nextAvailableSegmentAddress() const
std::optional< size_t > CodeSignatureCommandIndex
The index of LC_CODE_SIGNATURE load command if present.
Definition: MachOObject.h:322
LoadCommand & addSegment(StringRef SegName, uint64_t SegVMSize)
Creates a new segment load command in the object and returns a reference to the newly created load co...
std::optional< size_t > LinkerOptimizationHintCommandIndex
The index of LC_LINKER_OPTIMIZATIN_HINT load command if present.
Definition: MachOObject.h:334
The location of the rebase info inside the binary is described by LC_DYLD_INFO load command.
Definition: MachOObject.h:212
ArrayRef< uint8_t > Opcodes
Definition: MachOObject.h:215
MachO::any_relocation_info Info
Definition: MachOObject.h:185
std::optional< const SymbolEntry * > Symbol
Definition: MachOObject.h:175
std::optional< const Section * > Sec
Definition: MachOObject.h:177
unsigned getPlainRelocationSymbolNum(bool IsLittleEndian)
Definition: MachOObject.h:187
void setPlainRelocationSymbolNum(unsigned SymbolNum, bool IsLittleEndian)
Definition: MachOObject.h:193
std::optional< uint32_t > OriginalOffset
Definition: MachOObject.h:47
std::vector< RelocationInfo > Relocations
Definition: MachOObject.h:57
MachO::SectionType getType() const
Definition: MachOObject.h:63
The location of the string table inside the binary is described by LC_SYMTAB load command.
Definition: MachOObject.h:169
std::vector< std::string > Strings
Definition: MachOObject.h:170
std::optional< uint32_t > section() const
Definition: MachOObject.h:126
The location of the symbol table inside the binary is described by LC_SYMTAB load command.
Definition: MachOObject.h:134
void updateSymbols(function_ref< void(SymbolEntry &)> Callable)
Definition: MachOObject.cpp:36
const SymbolEntry * getSymbolByIndex(uint32_t Index) const
Definition: MachOObject.cpp:26
std::vector< std::unique_ptr< SymbolEntry > > Symbols
Definition: MachOObject.h:135
pointee_iterator< std::vector< std::unique_ptr< SymbolEntry > >::const_iterator > iterator
Definition: MachOObject.h:138
void removeSymbols(function_ref< bool(const std::unique_ptr< SymbolEntry > &)> ToRemove)
Definition: MachOObject.cpp:49
The location of the weak bind info inside the binary is described by LC_DYLD_INFO load command.
Definition: MachOObject.h:246
An iterator type that allows iterating over the pointees via some other iterator.
Definition: iterator.h:324