blob: 29d2fc8ea01b8a5a4baadba5c6c6b39d07f540cf [file] [log] [blame]
Nick Kledzik30332b12013-10-08 00:43:34 +00001//===- lib/ReaderWriter/MachO/NormalizedFile.h ----------------------===//
2//
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
42#include "lld/Core/Error.h"
43#include "lld/Core/LLVM.h"
44#include "lld/ReaderWriter/MachOLinkingContext.h"
Nick Kledzik30332b12013-10-08 00:43:34 +000045#include "llvm/ADT/SmallString.h"
46#include "llvm/ADT/StringRef.h"
Nick Kledzik6edd7222014-01-11 01:07:43 +000047#include "llvm/Support/Allocator.h"
Nick Kledzik30332b12013-10-08 00:43:34 +000048#include "llvm/Support/ErrorOr.h"
49#include "llvm/Support/MachO.h"
50#include "llvm/Support/YAMLTraits.h"
51
Rui Ueyama014192db2013-11-15 03:09:26 +000052#ifndef LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H
53#define LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H
Nick Kledzik30332b12013-10-08 00:43:34 +000054
Nick Kledzik6edd7222014-01-11 01:07:43 +000055using llvm::BumpPtrAllocator;
Nick Kledzik30332b12013-10-08 00:43:34 +000056using llvm::yaml::Hex64;
57using llvm::yaml::Hex32;
Nick Kledzik21921372014-07-24 23:06:56 +000058using llvm::yaml::Hex16;
Nick Kledzik30332b12013-10-08 00:43:34 +000059using llvm::yaml::Hex8;
60using llvm::yaml::SequenceTraits;
61using llvm::MachO::HeaderFileType;
62using llvm::MachO::BindType;
63using llvm::MachO::RebaseType;
64using llvm::MachO::NListType;
65using llvm::MachO::RelocationInfoType;
66using llvm::MachO::SectionType;
67using llvm::MachO::LoadCommandType;
68using llvm::MachO::ExportSymbolKind;
Nick Kledzik21921372014-07-24 23:06:56 +000069using llvm::MachO::DataRegionType;
Nick Kledzik30332b12013-10-08 00:43:34 +000070
71namespace lld {
72namespace mach_o {
73namespace normalized {
74
75
76/// The real mach-o relocation record is 8-bytes on disk and is
77/// encoded in one of two different bit-field patterns. This
Nick Kledzik369ffd12013-10-08 02:07:19 +000078/// normalized form has the union of all possible fields.
Nick Kledzik30332b12013-10-08 00:43:34 +000079struct Relocation {
Shankar Easwaran3d8de472014-01-27 03:09:26 +000080 Relocation() : offset(0), scattered(false),
81 type(llvm::MachO::GENERIC_RELOC_VANILLA),
82 length(0), pcRel(false), isExtern(false), value(0),
Nick Kledzik30332b12013-10-08 00:43:34 +000083 symbol(0) { }
84
85 Hex32 offset;
86 bool scattered;
87 RelocationInfoType type;
88 uint8_t length;
89 bool pcRel;
90 bool isExtern;
91 Hex32 value;
92 uint32_t symbol;
93};
94
95/// A typedef so that YAML I/O can treat this vector as a sequence.
96typedef std::vector<Relocation> Relocations;
97
98/// A typedef so that YAML I/O can process the raw bytes in a section.
99typedef std::vector<Hex8> ContentBytes;
100
101/// A typedef so that YAML I/O can treat indirect symbols as a flow sequence.
102typedef std::vector<uint32_t> IndirectSymbols;
103
104/// A typedef so that YAML I/O can encode/decode section attributes.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000105LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionAttr)
Nick Kledzik30332b12013-10-08 00:43:34 +0000106
107/// Mach-O has a 32-bit and 64-bit section record. This normalized form
108/// can support either kind.
109struct Section {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000110 Section() : type(llvm::MachO::S_REGULAR),
Nick Kledzik30332b12013-10-08 00:43:34 +0000111 attributes(0), alignment(0), address(0) { }
112
113 StringRef segmentName;
114 StringRef sectionName;
115 SectionType type;
116 SectionAttr attributes;
117 uint32_t alignment;
118 Hex64 address;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000119 ArrayRef<uint8_t> content;
Nick Kledzik30332b12013-10-08 00:43:34 +0000120 Relocations relocations;
121 IndirectSymbols indirectSymbols;
122};
123
124
125/// A typedef so that YAML I/O can encode/decode the scope bits of an nlist.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000126LLVM_YAML_STRONG_TYPEDEF(uint8_t, SymbolScope)
Nick Kledzik30332b12013-10-08 00:43:34 +0000127
128/// A typedef so that YAML I/O can encode/decode the desc bits of an nlist.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000129LLVM_YAML_STRONG_TYPEDEF(uint16_t, SymbolDesc)
Nick Kledzik30332b12013-10-08 00:43:34 +0000130
131/// Mach-O has a 32-bit and 64-bit symbol table entry (nlist), and the symbol
132/// type and scope and mixed in the same n_type field. This normalized form
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000133/// works for any pointer size and separates out the type and scope.
Nick Kledzik30332b12013-10-08 00:43:34 +0000134struct Symbol {
135 Symbol() : type(llvm::MachO::N_UNDF), scope(0), sect(0), desc(0), value(0) { }
136
137 StringRef name;
138 NListType type;
139 SymbolScope scope;
140 uint8_t sect;
141 SymbolDesc desc;
142 Hex64 value;
143};
144
145/// A typedef so that YAML I/O can (de/en)code the protection bits of a segment.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000146LLVM_YAML_STRONG_TYPEDEF(uint32_t, VMProtect)
Nick Kledzik30332b12013-10-08 00:43:34 +0000147
148/// Segments are only used in normalized final linked images (not in relocatable
149/// object files). They specify how a range of the file is loaded.
150struct Segment {
151 StringRef name;
152 Hex64 address;
153 Hex64 size;
154 VMProtect access;
155};
156
157/// Only used in normalized final linked images to specify on which dylibs
158/// it depends.
159struct DependentDylib {
160 StringRef path;
161 LoadCommandType kind;
162};
163
164/// A normalized rebasing entry. Only used in normalized final linked images.
165struct RebaseLocation {
166 Hex32 segOffset;
167 uint8_t segIndex;
168 RebaseType kind;
169};
170
171/// A normalized binding entry. Only used in normalized final linked images.
172struct BindLocation {
173 Hex32 segOffset;
174 uint8_t segIndex;
175 BindType kind;
176 bool canBeNull;
177 int ordinal;
178 StringRef symbolName;
179 Hex64 addend;
180};
181
182/// A typedef so that YAML I/O can encode/decode export flags.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000183LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportFlags)
Nick Kledzik30332b12013-10-08 00:43:34 +0000184
185/// A normalized export entry. Only used in normalized final linked images.
186struct Export {
187 StringRef name;
188 Hex64 offset;
189 ExportSymbolKind kind;
190 ExportFlags flags;
191 Hex32 otherOffset;
192 StringRef otherName;
193};
194
Nick Kledzik21921372014-07-24 23:06:56 +0000195/// A normalized data-in-code entry.
196struct DataInCode {
197 Hex32 offset;
198 Hex16 length;
199 DataRegionType kind;
200};
201
Nick Kledzik30332b12013-10-08 00:43:34 +0000202
203/// A typedef so that YAML I/O can encode/decode mach_header.flags.
Alexey Samsonov8e6829e2014-03-19 09:38:31 +0000204LLVM_YAML_STRONG_TYPEDEF(uint32_t, FileFlags)
Nick Kledzik30332b12013-10-08 00:43:34 +0000205
Nick Kledzik21921372014-07-24 23:06:56 +0000206
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000207///
Nick Kledzik30332b12013-10-08 00:43:34 +0000208struct NormalizedFile {
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000209 NormalizedFile() : arch(MachOLinkingContext::arch_unknown),
Nick Kledzik30332b12013-10-08 00:43:34 +0000210 fileType(llvm::MachO::MH_OBJECT),
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000211 flags(0),
212 hasUUID(false),
Nick Kledzik30332b12013-10-08 00:43:34 +0000213 os(MachOLinkingContext::OS::unknown) { }
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000214
Nick Kledzik30332b12013-10-08 00:43:34 +0000215 MachOLinkingContext::Arch arch;
216 HeaderFileType fileType;
217 FileFlags flags;
218 std::vector<Segment> segments; // Not used in object files.
219 std::vector<Section> sections;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000220
Nick Kledzik30332b12013-10-08 00:43:34 +0000221 // Symbols sorted by kind.
222 std::vector<Symbol> localSymbols;
223 std::vector<Symbol> globalSymbols;
224 std::vector<Symbol> undefinedSymbols;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000225
Nick Kledzik30332b12013-10-08 00:43:34 +0000226 // Maps to load commands with no LINKEDIT content (final linked images only).
227 std::vector<DependentDylib> dependentDylibs;
228 StringRef installName;
229 bool hasUUID;
230 std::vector<StringRef> rpaths;
231 Hex64 entryAddress;
232 MachOLinkingContext::OS os;
233 Hex64 sourceVersion;
234 Hex32 minOSverson;
235 Hex32 sdkVersion;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000236
Nick Kledzik30332b12013-10-08 00:43:34 +0000237 // Maps to load commands with LINKEDIT content (final linked images only).
Nick Kledzik1bebb282014-09-09 23:52:59 +0000238 Hex32 pageSize;
Nick Kledzik30332b12013-10-08 00:43:34 +0000239 std::vector<RebaseLocation> rebasingInfo;
240 std::vector<BindLocation> bindingInfo;
241 std::vector<BindLocation> weakBindingInfo;
242 std::vector<BindLocation> lazyBindingInfo;
243 std::vector<Export> exportInfo;
Nick Kledzik21921372014-07-24 23:06:56 +0000244 std::vector<DataInCode> dataInCode;
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000245
Nick Kledzik30332b12013-10-08 00:43:34 +0000246 // TODO:
247 // code-signature
248 // split-seg-info
249 // function-starts
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000250
Nick Kledzik6edd7222014-01-11 01:07:43 +0000251 // For any allocations in this struct which need to be owned by this struct.
252 BumpPtrAllocator ownedAllocations;
Nick Kledzik30332b12013-10-08 00:43:34 +0000253};
254
Nick Kledzik635f9c72014-09-04 20:08:30 +0000255/// Tests if a file is a non-fat mach-o object file.
256bool isThinObjectFile(StringRef path, MachOLinkingContext::Arch &arch);
Nick Kledzik30332b12013-10-08 00:43:34 +0000257
Nick Kledzik14b5d202014-10-08 01:48:10 +0000258/// If the buffer is a fat file with the request arch, then this function
259/// returns true with 'offset' and 'size' set to location of the arch slice
260/// within the buffer. Otherwise returns false;
261bool sliceFromFatFile(const MemoryBuffer &mb, MachOLinkingContext::Arch arch,
262 uint32_t &offset, uint32_t &size);
263
Nick Kledzik30332b12013-10-08 00:43:34 +0000264/// Reads a mach-o file and produces an in-memory normalized view.
Joey Gouly010b3762014-01-14 22:32:38 +0000265ErrorOr<std::unique_ptr<NormalizedFile>>
266readBinary(std::unique_ptr<MemoryBuffer> &mb,
267 const MachOLinkingContext::Arch arch);
Nick Kledzik30332b12013-10-08 00:43:34 +0000268
269/// Takes in-memory normalized view and writes a mach-o object file.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000270std::error_code writeBinary(const NormalizedFile &file, StringRef path);
Nick Kledzik30332b12013-10-08 00:43:34 +0000271
272size_t headerAndLoadCommandsSize(const NormalizedFile &file);
273
274
275/// Parses a yaml encoded mach-o file to produce an in-memory normalized view.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000276ErrorOr<std::unique_ptr<NormalizedFile>>
Nick Kledzik30332b12013-10-08 00:43:34 +0000277readYaml(std::unique_ptr<MemoryBuffer> &mb);
278
279/// Writes a yaml encoded mach-o files given an in-memory normalized view.
Rafael Espindolab1a4d3a2014-06-12 14:53:47 +0000280std::error_code writeYaml(const NormalizedFile &file, raw_ostream &out);
Nick Kledzik30332b12013-10-08 00:43:34 +0000281
282/// Takes in-memory normalized dylib or object and parses it into lld::File
Rui Ueyama170a1a82013-12-20 07:48:29 +0000283ErrorOr<std::unique_ptr<lld::File>>
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000284normalizedToAtoms(const NormalizedFile &normalizedFile, StringRef path,
Nick Kledzik6edd7222014-01-11 01:07:43 +0000285 bool copyRefs);
Nick Kledzik30332b12013-10-08 00:43:34 +0000286
287/// Takes atoms and generates a normalized macho-o view.
Shankar Easwaran3d8de472014-01-27 03:09:26 +0000288ErrorOr<std::unique_ptr<NormalizedFile>>
Nick Kledzik30332b12013-10-08 00:43:34 +0000289normalizedFromAtoms(const lld::File &atomFile, const MachOLinkingContext &ctxt);
290
291
Nick Kledzik30332b12013-10-08 00:43:34 +0000292} // namespace normalized
Nick Kledzik6edd7222014-01-11 01:07:43 +0000293
294/// Class for interfacing mach-o yaml files into generic yaml parsing
295class MachOYamlIOTaggedDocumentHandler : public YamlIOTaggedDocumentHandler {
Nick Kledzik378066c2014-06-30 22:57:33 +0000296public:
297 MachOYamlIOTaggedDocumentHandler(MachOLinkingContext::Arch arch)
298 : _arch(arch) { }
Rui Ueyamabc69bce2014-03-28 21:36:33 +0000299 bool handledDocTag(llvm::yaml::IO &io, const lld::File *&file) const override;
Nick Kledzik378066c2014-06-30 22:57:33 +0000300private:
301 const MachOLinkingContext::Arch _arch;
Nick Kledzik6edd7222014-01-11 01:07:43 +0000302};
303
Nick Kledzik30332b12013-10-08 00:43:34 +0000304} // namespace mach_o
305} // namespace lld
306
Rui Ueyama014192db2013-11-15 03:09:26 +0000307#endif // LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H