aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm12/tools/llvm-objcopy/MachO/Object.h
blob: 8642a7973deb8fdb9b985f130f7fe3a3f743b8a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//===- Object.h - Mach-O object file model ----------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_OBJCOPY_MACHO_OBJECT_H
#define LLVM_OBJCOPY_MACHO_OBJECT_H

#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/ObjectYAML/DWARFYAML.h"
#include "llvm/Support/StringSaver.h"
#include "llvm/Support/YAMLTraits.h"
#include <cstdint>
#include <string>
#include <vector>

namespace llvm {
namespace objcopy {
namespace macho {

struct MachHeader {
  uint32_t Magic;
  uint32_t CPUType;
  uint32_t CPUSubType;
  uint32_t FileType;
  uint32_t NCmds;
  uint32_t SizeOfCmds;
  uint32_t Flags;
  uint32_t Reserved = 0;
};

struct RelocationInfo;
struct Section {
  uint32_t Index;
  std::string Segname;
  std::string Sectname;
  // CanonicalName is a string formatted as “<Segname>,<Sectname>".
  std::string CanonicalName;
  uint64_t Addr = 0;
  uint64_t Size = 0;
  // Offset in the input file.
  Optional<uint32_t> OriginalOffset;
  uint32_t Offset = 0;
  uint32_t Align = 0;
  uint32_t RelOff = 0;
  uint32_t NReloc = 0;
  uint32_t Flags = 0;
  uint32_t Reserved1 = 0;
  uint32_t Reserved2 = 0;
  uint32_t Reserved3 = 0;
  StringRef Content;
  std::vector<RelocationInfo> Relocations;

  Section(StringRef SegName, StringRef SectName);

  Section(StringRef SegName, StringRef SectName, StringRef Content);

  MachO::SectionType getType() const {
    return static_cast<MachO::SectionType>(Flags & MachO::SECTION_TYPE);
  }

  bool isVirtualSection() const {
    return (getType() == MachO::S_ZEROFILL ||
            getType() == MachO::S_GB_ZEROFILL ||
            getType() == MachO::S_THREAD_LOCAL_ZEROFILL);
  }

  bool hasValidOffset() const {
    return !(isVirtualSection() || (OriginalOffset && *OriginalOffset == 0));
  }
};

struct LoadCommand {
  // The type MachO::macho_load_command is defined in llvm/BinaryFormat/MachO.h
  // and it is a union of all the structs corresponding to various load
  // commands.
  MachO::macho_load_command MachOLoadCommand;

  // The raw content of the payload of the load command (located right after the
  // corresponding struct). In some cases it is either empty or can be
  // copied-over without digging into its structure.
  std::vector<uint8_t> Payload;

  // Some load commands can contain (inside the payload) an array of sections,
  // though the contents of the sections are stored separately. The struct
  // Section describes only sections' metadata and where to find the
  // corresponding content inside the binary.
  std::vector<std::unique_ptr<Section>> Sections;

  // Returns the segment name if the load command is a segment command.
  Optional<StringRef> getSegmentName() const;

  // Returns the segment vm address if the load command is a segment command.
  Optional<uint64_t> getSegmentVMAddr() const;
};

// A symbol information. Fields which starts with "n_" are same as them in the
// nlist.
struct SymbolEntry {
  std::string Name;
  bool Referenced = false;
  uint32_t Index;
  uint8_t n_type;
  uint8_t n_sect;
  uint16_t n_desc;
  uint64_t n_value;

  bool isExternalSymbol() const { return n_type & MachO::N_EXT; }

  bool isLocalSymbol() const { return !isExternalSymbol(); }

  bool isUndefinedSymbol() const {
    return (n_type & MachO::N_TYPE) == MachO::N_UNDF;
  }

  bool isSwiftSymbol() const {
    return StringRef(Name).startswith("_$s") ||
           StringRef(Name).startswith("_$S");
  }

  Optional<uint32_t> section() const {
    return n_sect == MachO::NO_SECT ? None : Optional<uint32_t>(n_sect);
  }
};

/// The location of the symbol table inside the binary is described by LC_SYMTAB
/// load command.
struct SymbolTable {
  std::vector<std::unique_ptr<SymbolEntry>> Symbols;

  using iterator = pointee_iterator<
      std::vector<std::unique_ptr<SymbolEntry>>::const_iterator>;

  iterator begin() const { return iterator(Symbols.begin()); }
  iterator end() const { return iterator(Symbols.end()); }

  const SymbolEntry *getSymbolByIndex(uint32_t Index) const;
  SymbolEntry *getSymbolByIndex(uint32_t Index);
  void removeSymbols(
      function_ref<bool(const std::unique_ptr<SymbolEntry> &)> ToRemove);
};

struct IndirectSymbolEntry {
  // The original value in an indirect symbol table. Higher bits encode extra
  // information (INDIRECT_SYMBOL_LOCAL and INDIRECT_SYMBOL_ABS).
  uint32_t OriginalIndex;
  /// The Symbol referenced by this entry. It's None if the index is
  /// INDIRECT_SYMBOL_LOCAL or INDIRECT_SYMBOL_ABS.
  Optional<SymbolEntry *> Symbol;

  IndirectSymbolEntry(uint32_t OriginalIndex, Optional<SymbolEntry *> Symbol)
      : OriginalIndex(OriginalIndex), Symbol(Symbol) {}
};

struct IndirectSymbolTable {
  std::vector<IndirectSymbolEntry> Symbols;
};

/// The location of the string table inside the binary is described by LC_SYMTAB
/// load command.
struct StringTable {
  std::vector<std::string> Strings;
};

struct RelocationInfo {
  // The referenced symbol entry. Set if !Scattered && Extern.
  Optional<const SymbolEntry *> Symbol;
  // The referenced section. Set if !Scattered && !Extern.
  Optional<const Section *> Sec;
  // True if Info is a scattered_relocation_info.
  bool Scattered;
  // True if the r_symbolnum points to a section number (i.e. r_extern=0).
  bool Extern;
  MachO::any_relocation_info Info;

  unsigned getPlainRelocationSymbolNum(bool IsLittleEndian) {
    if (IsLittleEndian)
      return Info.r_word1 & 0xffffff;
    return Info.r_word1 >> 8;
  }

  void setPlainRelocationSymbolNum(unsigned SymbolNum, bool IsLittleEndian) {
    assert(SymbolNum < (1 << 24) && "SymbolNum out of range");
    if (IsLittleEndian)
      Info.r_word1 = (Info.r_word1 & ~0x00ffffff) | SymbolNum;
    else
      Info.r_word1 = (Info.r_word1 & ~0xffffff00) | (SymbolNum << 8);
  }
};

/// The location of the rebase info inside the binary is described by
/// LC_DYLD_INFO load command. Dyld rebases an image whenever dyld loads it at
/// an address different from its preferred address.  The rebase information is
/// a stream of byte sized opcodes whose symbolic names start with
/// REBASE_OPCODE_. Conceptually the rebase information is a table of tuples:
///   <seg-index, seg-offset, type>
/// The opcodes are a compressed way to encode the table by only
/// encoding when a column changes.  In addition simple patterns
/// like "every n'th offset for m times" can be encoded in a few
/// bytes.
struct RebaseInfo {
  // At the moment we do not parse this info (and it is simply copied over),
  // but the proper support will be added later.
  ArrayRef<uint8_t> Opcodes;
};

/// The location of the bind info inside the binary is described by
/// LC_DYLD_INFO load command. Dyld binds an image during the loading process,
/// if the image requires any pointers to be initialized to symbols in other
/// images. The bind information is a stream of byte sized opcodes whose
/// symbolic names start with BIND_OPCODE_. Conceptually the bind information is
/// a table of tuples: <seg-index, seg-offset, type, symbol-library-ordinal,
/// symbol-name, addend> The opcodes are a compressed way to encode the table by
/// only encoding when a column changes.  In addition simple patterns like for
/// runs of pointers initialized to the same value can be encoded in a few
/// bytes.
struct BindInfo {
  // At the moment we do not parse this info (and it is simply copied over),
  // but the proper support will be added later.
  ArrayRef<uint8_t> Opcodes;
};

/// The location of the weak bind info inside the binary is described by
/// LC_DYLD_INFO load command. Some C++ programs require dyld to unique symbols
/// so that all images in the process use the same copy of some code/data. This
/// step is done after binding. The content of the weak_bind info is an opcode
/// stream like the bind_info.  But it is sorted alphabetically by symbol name.
/// This enable dyld to walk all images with weak binding information in order
/// and look for collisions.  If there are no collisions, dyld does no updating.
/// That means that some fixups are also encoded in the bind_info.  For
/// instance, all calls to "operator new" are first bound to libstdc++.dylib
/// using the information in bind_info.  Then if some image overrides operator
/// new that is detected when the weak_bind information is processed and the
/// call to operator new is then rebound.
struct WeakBindInfo {
  // At the moment we do not parse this info (and it is simply copied over),
  // but the proper support will be added later.
  ArrayRef<uint8_t> Opcodes;
};

/// The location of the lazy bind info inside the binary is described by
/// LC_DYLD_INFO load command. Some uses of external symbols do not need to be
/// bound immediately. Instead they can be lazily bound on first use.  The
/// lazy_bind contains a stream of BIND opcodes to bind all lazy symbols. Normal
/// use is that dyld ignores the lazy_bind section when loading an image.
/// Instead the static linker arranged for the lazy pointer to initially point
/// to a helper function which pushes the offset into the lazy_bind area for the
/// symbol needing to be bound, then jumps to dyld which simply adds the offset
/// to lazy_bind_off to get the information on what to bind.
struct LazyBindInfo {
  ArrayRef<uint8_t> Opcodes;
};

/// The location of the export info inside the binary is described by
/// LC_DYLD_INFO load command. The symbols exported by a dylib are encoded in a
/// trie.  This is a compact representation that factors out common prefixes. It
/// also reduces LINKEDIT pages in RAM because it encodes all information (name,
/// address, flags) in one small, contiguous range. The export area is a stream
/// of nodes.  The first node sequentially is the start node for the trie. Nodes
/// for a symbol start with a uleb128 that is the length of the exported symbol
/// information for the string so far. If there is no exported symbol, the node
/// starts with a zero byte. If there is exported info, it follows the length.
/// First is a uleb128 containing flags. Normally, it is followed by
/// a uleb128 encoded offset which is location of the content named
/// by the symbol from the mach_header for the image.  If the flags
/// is EXPORT_SYMBOL_FLAGS_REEXPORT, then following the flags is
/// a uleb128 encoded library ordinal, then a zero terminated
/// UTF8 string.  If the string is zero length, then the symbol
/// is re-export from the specified dylib with the same name.
/// If the flags is EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER, then following
/// the flags is two uleb128s: the stub offset and the resolver offset.
/// The stub is used by non-lazy pointers.  The resolver is used
/// by lazy pointers and must be called to get the actual address to use.
/// After the optional exported symbol information is a byte of
/// how many edges (0-255) that this node has leaving it,
/// followed by each edge.
/// Each edge is a zero terminated UTF8 of the addition chars
/// in the symbol, followed by a uleb128 offset for the node that
/// edge points to.
struct ExportInfo {
  ArrayRef<uint8_t> Trie;
};

struct LinkData {
  ArrayRef<uint8_t> Data;
};

struct Object {
  MachHeader Header;
  std::vector<LoadCommand> LoadCommands;

  SymbolTable SymTable;
  StringTable StrTable;

  RebaseInfo Rebases;
  BindInfo Binds;
  WeakBindInfo WeakBinds;
  LazyBindInfo LazyBinds;
  ExportInfo Exports;
  IndirectSymbolTable IndirectSymTable;
  LinkData DataInCode;
  LinkData FunctionStarts;
  LinkData CodeSignature;

  Optional<uint32_t> SwiftVersion;

  /// The index of LC_CODE_SIGNATURE load command if present.
  Optional<size_t> CodeSignatureCommandIndex;
  /// The index of LC_SYMTAB load command if present.
  Optional<size_t> SymTabCommandIndex;
  /// The index of LC_DYLD_INFO or LC_DYLD_INFO_ONLY load command if present.
  Optional<size_t> DyLdInfoCommandIndex;
  /// The index LC_DYSYMTAB load comamnd if present.
  Optional<size_t> DySymTabCommandIndex;
  /// The index LC_DATA_IN_CODE load comamnd if present.
  Optional<size_t> DataInCodeCommandIndex;
  /// The index LC_FUNCTION_STARTS load comamnd if present.
  Optional<size_t> FunctionStartsCommandIndex;

  BumpPtrAllocator Alloc;
  StringSaver NewSectionsContents;

  Object() : NewSectionsContents(Alloc) {}

  Error
  removeSections(function_ref<bool(const std::unique_ptr<Section> &)> ToRemove);

  Error removeLoadCommands(function_ref<bool(const LoadCommand &)> ToRemove);

  void updateLoadCommandIndexes();

  /// Creates a new segment load command in the object and returns a reference
  /// to the newly created load command. The caller should verify that SegName
  /// is not too long (SegName.size() should be less than or equal to 16).
  LoadCommand &addSegment(StringRef SegName, uint64_t SegVMSize);

  bool is64Bit() const {
    return Header.Magic == MachO::MH_MAGIC_64 ||
           Header.Magic == MachO::MH_CIGAM_64;
  }

  uint64_t nextAvailableSegmentAddress() const;
};

} // end namespace macho
} // end namespace objcopy
} // end namespace llvm

#endif // LLVM_OBJCOPY_MACHO_OBJECT_H