blob: 532831212b3ad078c4bf362d861404eb135a6f70 [file] [log] [blame]
Nick Kledzik5b9e48b2014-11-19 02:21:53 +00001//===- lib/ReaderWriter/MachO/MachONormalizedFile.h -----------------------===//
Nick Kledzik30332b12013-10-08 00:43:34 +00002//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10///
11/// \file These data structures comprise the "normalized" view of
12/// mach-o object files. The normalized view is an in-memory only data structure
Shankar Easwaran3d8de472014-01-27 03:09:26 +000013/// which is always in native endianness and pointer size.
14///
15/// The normalized view easily converts to and from YAML using YAML I/O.
Nick Kledzik30332b12013-10-08 00:43:34 +000016///
17/// The normalized view converts to and from binary mach-o object files using
18/// the writeBinary() and readBinary() functions.
19///
Shankar Easwaran3d8de472014-01-27 03:09:26 +000020/// The normalized view converts to and from lld::Atoms using the
Nick Kledzik30332b12013-10-08 00:43:34 +000021/// normalizedToAtoms() and normalizedFromAtoms().
22///
23/// Overall, the conversion paths available look like:
24///
Shankar Easwaran3d8de472014-01-27 03:09:26 +000025/// +---------------+
26/// | binary mach-o |
27/// +---------------+
Nick Kledzik30332b12013-10-08 00:43:34 +000028/// ^
29/// |
30/// v
Shankar Easwaran3d8de472014-01-27 03:09:26 +000031/// +------------+ +------+
32/// | normalized | <-> | yaml |
33/// +------------+ +------+
Nick Kledzik30332b12013-10-08 00:43:34 +000034/// ^
35/// |
36/// v
Shankar Easwaran3d8de472014-01-27 03:09:26 +000037/// +-------+
Nick Kledzik30332b12013-10-08 00:43:34 +000038/// | Atoms |
Shankar Easwaran3d8de472014-01-27 03:09:26 +000039/// +-------+
40///
Nick Kledzik30332b12013-10-08 00:43:34 +000041
Pete Cooperd75b7182016-02-08 21:50:45 +000042#ifndef LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H
43#define LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H
44
Nick Kledzik30332b12013-10-08 00:43:34 +000045#include "lld/Core/Error.h"
46#include "lld/Core/LLVM.h"
47#include "lld/ReaderWriter/MachOLinkingContext.h"
Nick Kledzik30332b12013-10-08 00:43:34 +000048#include "llvm/ADT/SmallString.h"
49#include "llvm/ADT/StringRef.h"
Nick Kledzik6edd7222014-01-11 01:07:43 +000050#include "llvm/Support/Allocator.h"
Pete Coopere5fa5a32015-12-16 22:03:21 +000051#include "llvm/Support/Debug.h"
Nick Kledzik30332b12013-10-08 00:43:34 +000052#include "llvm/Support/ErrorOr.h"
53#include "llvm/Support/MachO.h"
54#include "llvm/Support/YAMLTraits.h"
55
Nick Kledzik6edd7222014-01-11 01:07:43 +000056using llvm::BumpPtrAllocator;
Nick Kledzik30332b12013-10-08 00:43:34 +000057using llvm::yaml::Hex64;
58using llvm::yaml::Hex32;
Nick Kledzik21921372014-07-24 23:06:56 +000059using llvm::yaml::Hex16;
Nick Kledzik30332b12013-10-08 00:43:34 +000060using llvm::yaml::Hex8;
61using llvm::yaml::SequenceTraits;
62using llvm::MachO::HeaderFileType;
63using llvm::MachO::BindType;
64using llvm::MachO::RebaseType;
65using llvm::MachO::NListType;
66using llvm::MachO::RelocationInfoType;
67using llvm::MachO::SectionType;
68using llvm::MachO::LoadCommandType;
69using llvm::MachO::ExportSymbolKind;
Nick Kledzik21921372014-07-24 23:06:56 +000070using llvm::MachO::DataRegionType;
Nick Kledzik30332b12013-10-08 00:43:34 +000071
72namespace lld {
73namespace mach_o {
74namespace normalized {
75
76
77/// The real mach-o relocation record is 8-bytes on disk and is
78/// encoded in one of two different bit-field patterns. This
Nick Kledzik369ffd12013-10-08 02:07:19 +000079/// normalized form has the union of all possible fields.
Nick Kledzik30332b12013-10-08 00:43:34 +000080struct Relocation {
Shankar Easwaran3d8de472014-01-27 03:09:26 +000081 Relocation() : offset(0), scattered(false),
82 type(llvm::MachO::GENERIC_RELOC_VANILLA),
83 length(0), pcRel(false), isExtern(false), value(0),
Nick Kledzik30332b12013-10-08 00:43:34 +000084 symbol(0) { }
85
86 Hex32 offset;
87 bool scattered;
88 RelocationInfoType type;
89 uint8_t length;
90 bool pcRel;
91 bool isExtern;
92 Hex32 value;
93 uint32_t symbol;
94};
95
96/// A typedef so that YAML I/O can treat this vector as a sequence.
97typedef std::vector<Relocation> Relocations;
98
99/// A typedef so that YAML I/O can process the raw bytes in a section.
100typedef std::vector<Hex8> ContentBytes;
101
102/// A typedef so that YAML I/O can treat indirect symbols as a flow sequence.
103typedef std::vector<uint32_t> IndirectSymbols;
104
105/// A typedef so that YAML I/O can encode/decode section attributes.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000106LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionAttr)
Nick Kledzik30332b12013-10-08 00:43:34 +0000107
108/// Mach-O has a 32-bit and 64-bit section record. This normalized form
109/// can support either kind.
110struct Section {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000111 Section() : type(llvm::MachO::S_REGULAR),
Rui Ueyamaf006f4d2015-03-26 01:44:01 +0000112 attributes(0), alignment(1), address(0) { }
Nick Kledzik30332b12013-10-08 00:43:34 +0000113
114 StringRef segmentName;
115 StringRef sectionName;
116 SectionType type;
117 SectionAttr attributes;
Rui Ueyama629f9642015-03-26 02:20:25 +0000118 uint16_t alignment;
Nick Kledzik30332b12013-10-08 00:43:34 +0000119 Hex64 address;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000120 ArrayRef<uint8_t> content;
Nick Kledzik30332b12013-10-08 00:43:34 +0000121 Relocations relocations;
122 IndirectSymbols indirectSymbols;
Pete Coopere5fa5a32015-12-16 22:03:21 +0000123
124#ifndef NDEBUG
125 raw_ostream& operator<<(raw_ostream &OS) const {
126 dump(OS);
127 return OS;
128 }
129
130 void dump(raw_ostream &OS = llvm::dbgs()) const;
131#endif
Nick Kledzik30332b12013-10-08 00:43:34 +0000132};
133
134
135/// A typedef so that YAML I/O can encode/decode the scope bits of an nlist.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000136LLVM_YAML_STRONG_TYPEDEF(uint8_t, SymbolScope)
Nick Kledzik30332b12013-10-08 00:43:34 +0000137
138/// A typedef so that YAML I/O can encode/decode the desc bits of an nlist.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000139LLVM_YAML_STRONG_TYPEDEF(uint16_t, SymbolDesc)
Nick Kledzik30332b12013-10-08 00:43:34 +0000140
141/// Mach-O has a 32-bit and 64-bit symbol table entry (nlist), and the symbol
142/// type and scope and mixed in the same n_type field. This normalized form
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000143/// works for any pointer size and separates out the type and scope.
Nick Kledzik30332b12013-10-08 00:43:34 +0000144struct Symbol {
145 Symbol() : type(llvm::MachO::N_UNDF), scope(0), sect(0), desc(0), value(0) { }
146
147 StringRef name;
148 NListType type;
149 SymbolScope scope;
150 uint8_t sect;
151 SymbolDesc desc;
152 Hex64 value;
153};
154
Lang Hamesac2adce2015-12-11 23:25:09 +0000155/// Check whether the given section type indicates a zero-filled section.
156// FIXME: Utility functions of this kind should probably be moved into
157// llvm/Support.
158inline bool isZeroFillSection(SectionType T) {
159 return (T == llvm::MachO::S_ZEROFILL ||
160 T == llvm::MachO::S_THREAD_LOCAL_ZEROFILL);
161}
162
Nick Kledzik30332b12013-10-08 00:43:34 +0000163/// A typedef so that YAML I/O can (de/en)code the protection bits of a segment.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000164LLVM_YAML_STRONG_TYPEDEF(uint32_t, VMProtect)
Nick Kledzik30332b12013-10-08 00:43:34 +0000165
Nick Kledzik5b9e48b2014-11-19 02:21:53 +0000166/// A typedef to hold verions X.Y.X packed into 32-bit xxxx.yy.zz
167LLVM_YAML_STRONG_TYPEDEF(uint32_t, PackedVersion)
168
Nick Kledzik30332b12013-10-08 00:43:34 +0000169/// Segments are only used in normalized final linked images (not in relocatable
170/// object files). They specify how a range of the file is loaded.
171struct Segment {
172 StringRef name;
173 Hex64 address;
174 Hex64 size;
Pete Cooperb8fec3e2016-02-06 00:51:16 +0000175 VMProtect init_access;
176 VMProtect max_access;
Nick Kledzik30332b12013-10-08 00:43:34 +0000177};
178
179/// Only used in normalized final linked images to specify on which dylibs
180/// it depends.
181struct DependentDylib {
182 StringRef path;
183 LoadCommandType kind;
Nick Kledzik5b9e48b2014-11-19 02:21:53 +0000184 PackedVersion compatVersion;
185 PackedVersion currentVersion;
Nick Kledzik30332b12013-10-08 00:43:34 +0000186};
187
188/// A normalized rebasing entry. Only used in normalized final linked images.
189struct RebaseLocation {
190 Hex32 segOffset;
191 uint8_t segIndex;
192 RebaseType kind;
193};
194
195/// A normalized binding entry. Only used in normalized final linked images.
196struct BindLocation {
197 Hex32 segOffset;
198 uint8_t segIndex;
199 BindType kind;
200 bool canBeNull;
201 int ordinal;
202 StringRef symbolName;
203 Hex64 addend;
204};
205
206/// A typedef so that YAML I/O can encode/decode export flags.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000207LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportFlags)
Nick Kledzik30332b12013-10-08 00:43:34 +0000208
209/// A normalized export entry. Only used in normalized final linked images.
210struct Export {
211 StringRef name;
212 Hex64 offset;
213 ExportSymbolKind kind;
214 ExportFlags flags;
215 Hex32 otherOffset;
216 StringRef otherName;
217};
218
Nick Kledzik21921372014-07-24 23:06:56 +0000219/// A normalized data-in-code entry.
220struct DataInCode {
221 Hex32 offset;
222 Hex16 length;
223 DataRegionType kind;
224};
225
Nick Kledzik30332b12013-10-08 00:43:34 +0000226
227/// A typedef so that YAML I/O can encode/decode mach_header.flags.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000228LLVM_YAML_STRONG_TYPEDEF(uint32_t, FileFlags)
Nick Kledzik30332b12013-10-08 00:43:34 +0000229
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000230///
Nick Kledzik30332b12013-10-08 00:43:34 +0000231struct NormalizedFile {
Benjamin Kramercfacc9d2015-06-23 19:55:04 +0000232 MachOLinkingContext::Arch arch = MachOLinkingContext::arch_unknown;
233 HeaderFileType fileType = llvm::MachO::MH_OBJECT;
234 FileFlags flags = 0;
Nick Kledzik30332b12013-10-08 00:43:34 +0000235 std::vector<Segment> segments; // Not used in object files.
236 std::vector<Section> sections;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000237
Nick Kledzik30332b12013-10-08 00:43:34 +0000238 // Symbols sorted by kind.
239 std::vector<Symbol> localSymbols;
240 std::vector<Symbol> globalSymbols;
241 std::vector<Symbol> undefinedSymbols;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000242
Nick Kledzik30332b12013-10-08 00:43:34 +0000243 // Maps to load commands with no LINKEDIT content (final linked images only).
244 std::vector<DependentDylib> dependentDylibs;
Benjamin Kramercfacc9d2015-06-23 19:55:04 +0000245 StringRef installName; // dylibs only
246 PackedVersion compatVersion = 0; // dylibs only
247 PackedVersion currentVersion = 0; // dylibs only
248 bool hasUUID = false;
Pete Cooper354809e2016-02-03 22:28:29 +0000249 bool hasMinVersionLoadCommand = false;
Nick Kledzik30332b12013-10-08 00:43:34 +0000250 std::vector<StringRef> rpaths;
Benjamin Kramercfacc9d2015-06-23 19:55:04 +0000251 Hex64 entryAddress = 0;
252 Hex64 stackSize = 0;
253 MachOLinkingContext::OS os = MachOLinkingContext::OS::unknown;
254 Hex64 sourceVersion = 0;
255 PackedVersion minOSverson = 0;
256 PackedVersion sdkVersion = 0;
Pete Cooperceee5de2016-02-04 02:16:08 +0000257 LoadCommandType minOSVersionKind = (LoadCommandType)0;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000258
Nick Kledzik30332b12013-10-08 00:43:34 +0000259 // Maps to load commands with LINKEDIT content (final linked images only).
Benjamin Kramercfacc9d2015-06-23 19:55:04 +0000260 Hex32 pageSize = 0;
Nick Kledzik30332b12013-10-08 00:43:34 +0000261 std::vector<RebaseLocation> rebasingInfo;
262 std::vector<BindLocation> bindingInfo;
263 std::vector<BindLocation> weakBindingInfo;
264 std::vector<BindLocation> lazyBindingInfo;
265 std::vector<Export> exportInfo;
Pete Cooper41f3e8e2016-02-09 01:38:13 +0000266 std::vector<uint8_t> functionStarts;
Nick Kledzik21921372014-07-24 23:06:56 +0000267 std::vector<DataInCode> dataInCode;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000268
Nick Kledzik30332b12013-10-08 00:43:34 +0000269 // TODO:
270 // code-signature
271 // split-seg-info
272 // function-starts
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000273
Nick Kledzik6edd7222014-01-11 01:07:43 +0000274 // For any allocations in this struct which need to be owned by this struct.
275 BumpPtrAllocator ownedAllocations;
Nick Kledzik30332b12013-10-08 00:43:34 +0000276};
277
Nick Kledzik635f9c72014-09-04 20:08:30 +0000278/// Tests if a file is a non-fat mach-o object file.
279bool isThinObjectFile(StringRef path, MachOLinkingContext::Arch &arch);
Nick Kledzik30332b12013-10-08 00:43:34 +0000280
Nick Kledzik14b5d202014-10-08 01:48:10 +0000281/// If the buffer is a fat file with the request arch, then this function
282/// returns true with 'offset' and 'size' set to location of the arch slice
283/// within the buffer. Otherwise returns false;
Rafael Espindolaed48e532015-04-27 22:48:51 +0000284bool sliceFromFatFile(MemoryBufferRef mb, MachOLinkingContext::Arch arch,
285 uint32_t &offset, uint32_t &size);
Nick Kledzik14b5d202014-10-08 01:48:10 +0000286
Nick Kledzik30332b12013-10-08 00:43:34 +0000287/// Reads a mach-o file and produces an in-memory normalized view.
Joey Gouly010b3762014-01-14 22:32:38 +0000288ErrorOr<std::unique_ptr<NormalizedFile>>
289readBinary(std::unique_ptr<MemoryBuffer> &mb,
290 const MachOLinkingContext::Arch arch);
Nick Kledzik30332b12013-10-08 00:43:34 +0000291
292/// Takes in-memory normalized view and writes a mach-o object file.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000293std::error_code writeBinary(const NormalizedFile &file, StringRef path);
Nick Kledzik30332b12013-10-08 00:43:34 +0000294
295size_t headerAndLoadCommandsSize(const NormalizedFile &file);
296
297
298/// Parses a yaml encoded mach-o file to produce an in-memory normalized view.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000299ErrorOr<std::unique_ptr<NormalizedFile>>
Nick Kledzik30332b12013-10-08 00:43:34 +0000300readYaml(std::unique_ptr<MemoryBuffer> &mb);
301
302/// Writes a yaml encoded mach-o files given an in-memory normalized view.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000303std::error_code writeYaml(const NormalizedFile &file, raw_ostream &out);
Nick Kledzik30332b12013-10-08 00:43:34 +0000304
Rui Ueyama1d510422014-12-12 07:31:09 +0000305std::error_code
306normalizedObjectToAtoms(MachOFile *file,
307 const NormalizedFile &normalizedFile,
308 bool copyRefs);
309
310std::error_code
311normalizedDylibToAtoms(MachODylibFile *file,
312 const NormalizedFile &normalizedFile,
313 bool copyRefs);
314
Nick Kledzik30332b12013-10-08 00:43:34 +0000315/// Takes in-memory normalized dylib or object and parses it into lld::File
Rui Ueyama170a1a82013-12-20 07:48:29 +0000316ErrorOr<std::unique_ptr<lld::File>>
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000317normalizedToAtoms(const NormalizedFile &normalizedFile, StringRef path,
Nick Kledzik6edd7222014-01-11 01:07:43 +0000318 bool copyRefs);
Nick Kledzik30332b12013-10-08 00:43:34 +0000319
320/// Takes atoms and generates a normalized macho-o view.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000321ErrorOr<std::unique_ptr<NormalizedFile>>
Nick Kledzik30332b12013-10-08 00:43:34 +0000322normalizedFromAtoms(const lld::File &atomFile, const MachOLinkingContext &ctxt);
323
324
Nick Kledzik30332b12013-10-08 00:43:34 +0000325} // namespace normalized
Nick Kledzik6edd7222014-01-11 01:07:43 +0000326
327/// Class for interfacing mach-o yaml files into generic yaml parsing
328class MachOYamlIOTaggedDocumentHandler : public YamlIOTaggedDocumentHandler {
Nick Kledzik378066c2014-06-30 22:57:33 +0000329public:
330 MachOYamlIOTaggedDocumentHandler(MachOLinkingContext::Arch arch)
331 : _arch(arch) { }
Rui Ueyamabc69bce2014-03-28 21:36:33 +0000332 bool handledDocTag(llvm::yaml::IO &io, const lld::File *&file) const override;
Nick Kledzik378066c2014-06-30 22:57:33 +0000333private:
334 const MachOLinkingContext::Arch _arch;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000335};
336
Nick Kledzik30332b12013-10-08 00:43:34 +0000337} // namespace mach_o
338} // namespace lld
339
Rui Ueyama014192db2013-11-15 03:09:26 +0000340#endif // LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H