Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 1 | //===- 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 Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 13 | /// 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 Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 16 | /// |
| 17 | /// The normalized view converts to and from binary mach-o object files using |
| 18 | /// the writeBinary() and readBinary() functions. |
| 19 | /// |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 20 | /// The normalized view converts to and from lld::Atoms using the |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 21 | /// normalizedToAtoms() and normalizedFromAtoms(). |
| 22 | /// |
| 23 | /// Overall, the conversion paths available look like: |
| 24 | /// |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 25 | /// +---------------+ |
| 26 | /// | binary mach-o | |
| 27 | /// +---------------+ |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 28 | /// ^ |
| 29 | /// | |
| 30 | /// v |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 31 | /// +------------+ +------+ |
| 32 | /// | normalized | <-> | yaml | |
| 33 | /// +------------+ +------+ |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 34 | /// ^ |
| 35 | /// | |
| 36 | /// v |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 37 | /// +-------+ |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 38 | /// | Atoms | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 39 | /// +-------+ |
| 40 | /// |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 41 | |
| 42 | #include "lld/Core/Error.h" |
| 43 | #include "lld/Core/LLVM.h" |
| 44 | #include "lld/ReaderWriter/MachOLinkingContext.h" |
| 45 | |
| 46 | #include "llvm/ADT/SmallString.h" |
| 47 | #include "llvm/ADT/StringRef.h" |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Allocator.h" |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 49 | #include "llvm/Support/ErrorOr.h" |
| 50 | #include "llvm/Support/MachO.h" |
| 51 | #include "llvm/Support/YAMLTraits.h" |
| 52 | |
Rui Ueyama | 014192db | 2013-11-15 03:09:26 +0000 | [diff] [blame] | 53 | #ifndef LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H |
| 54 | #define LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 55 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 56 | using llvm::BumpPtrAllocator; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 57 | using llvm::yaml::Hex64; |
| 58 | using llvm::yaml::Hex32; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 59 | using llvm::yaml::Hex16; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 60 | using llvm::yaml::Hex8; |
| 61 | using llvm::yaml::SequenceTraits; |
| 62 | using llvm::MachO::HeaderFileType; |
| 63 | using llvm::MachO::BindType; |
| 64 | using llvm::MachO::RebaseType; |
| 65 | using llvm::MachO::NListType; |
| 66 | using llvm::MachO::RelocationInfoType; |
| 67 | using llvm::MachO::SectionType; |
| 68 | using llvm::MachO::LoadCommandType; |
| 69 | using llvm::MachO::ExportSymbolKind; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 70 | using llvm::MachO::DataRegionType; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 71 | |
| 72 | namespace lld { |
| 73 | namespace mach_o { |
| 74 | namespace 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 Kledzik | 369ffd1 | 2013-10-08 02:07:19 +0000 | [diff] [blame] | 79 | /// normalized form has the union of all possible fields. |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 80 | struct Relocation { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 81 | Relocation() : offset(0), scattered(false), |
| 82 | type(llvm::MachO::GENERIC_RELOC_VANILLA), |
| 83 | length(0), pcRel(false), isExtern(false), value(0), |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 84 | 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. |
| 97 | typedef std::vector<Relocation> Relocations; |
| 98 | |
| 99 | /// A typedef so that YAML I/O can process the raw bytes in a section. |
| 100 | typedef std::vector<Hex8> ContentBytes; |
| 101 | |
| 102 | /// A typedef so that YAML I/O can treat indirect symbols as a flow sequence. |
| 103 | typedef std::vector<uint32_t> IndirectSymbols; |
| 104 | |
| 105 | /// A typedef so that YAML I/O can encode/decode section attributes. |
Alexey Samsonov | 8e6829e | 2014-03-19 09:38:31 +0000 | [diff] [blame] | 106 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionAttr) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 107 | |
| 108 | /// Mach-O has a 32-bit and 64-bit section record. This normalized form |
| 109 | /// can support either kind. |
| 110 | struct Section { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 111 | Section() : type(llvm::MachO::S_REGULAR), |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 112 | attributes(0), alignment(0), address(0) { } |
| 113 | |
| 114 | StringRef segmentName; |
| 115 | StringRef sectionName; |
| 116 | SectionType type; |
| 117 | SectionAttr attributes; |
| 118 | uint32_t alignment; |
| 119 | Hex64 address; |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 120 | ArrayRef<uint8_t> content; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 121 | Relocations relocations; |
| 122 | IndirectSymbols indirectSymbols; |
| 123 | }; |
| 124 | |
| 125 | |
| 126 | /// A typedef so that YAML I/O can encode/decode the scope bits of an nlist. |
Alexey Samsonov | 8e6829e | 2014-03-19 09:38:31 +0000 | [diff] [blame] | 127 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, SymbolScope) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 128 | |
| 129 | /// A typedef so that YAML I/O can encode/decode the desc bits of an nlist. |
Alexey Samsonov | 8e6829e | 2014-03-19 09:38:31 +0000 | [diff] [blame] | 130 | LLVM_YAML_STRONG_TYPEDEF(uint16_t, SymbolDesc) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 131 | |
| 132 | /// Mach-O has a 32-bit and 64-bit symbol table entry (nlist), and the symbol |
| 133 | /// type and scope and mixed in the same n_type field. This normalized form |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 134 | /// works for any pointer size and separates out the type and scope. |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 135 | struct Symbol { |
| 136 | Symbol() : type(llvm::MachO::N_UNDF), scope(0), sect(0), desc(0), value(0) { } |
| 137 | |
| 138 | StringRef name; |
| 139 | NListType type; |
| 140 | SymbolScope scope; |
| 141 | uint8_t sect; |
| 142 | SymbolDesc desc; |
| 143 | Hex64 value; |
| 144 | }; |
| 145 | |
| 146 | /// A typedef so that YAML I/O can (de/en)code the protection bits of a segment. |
Alexey Samsonov | 8e6829e | 2014-03-19 09:38:31 +0000 | [diff] [blame] | 147 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, VMProtect) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 148 | |
| 149 | /// Segments are only used in normalized final linked images (not in relocatable |
| 150 | /// object files). They specify how a range of the file is loaded. |
| 151 | struct Segment { |
| 152 | StringRef name; |
| 153 | Hex64 address; |
| 154 | Hex64 size; |
| 155 | VMProtect access; |
| 156 | }; |
| 157 | |
| 158 | /// Only used in normalized final linked images to specify on which dylibs |
| 159 | /// it depends. |
| 160 | struct DependentDylib { |
| 161 | StringRef path; |
| 162 | LoadCommandType kind; |
| 163 | }; |
| 164 | |
| 165 | /// A normalized rebasing entry. Only used in normalized final linked images. |
| 166 | struct RebaseLocation { |
| 167 | Hex32 segOffset; |
| 168 | uint8_t segIndex; |
| 169 | RebaseType kind; |
| 170 | }; |
| 171 | |
| 172 | /// A normalized binding entry. Only used in normalized final linked images. |
| 173 | struct BindLocation { |
| 174 | Hex32 segOffset; |
| 175 | uint8_t segIndex; |
| 176 | BindType kind; |
| 177 | bool canBeNull; |
| 178 | int ordinal; |
| 179 | StringRef symbolName; |
| 180 | Hex64 addend; |
| 181 | }; |
| 182 | |
| 183 | /// A typedef so that YAML I/O can encode/decode export flags. |
Alexey Samsonov | 8e6829e | 2014-03-19 09:38:31 +0000 | [diff] [blame] | 184 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportFlags) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 185 | |
| 186 | /// A normalized export entry. Only used in normalized final linked images. |
| 187 | struct Export { |
| 188 | StringRef name; |
| 189 | Hex64 offset; |
| 190 | ExportSymbolKind kind; |
| 191 | ExportFlags flags; |
| 192 | Hex32 otherOffset; |
| 193 | StringRef otherName; |
| 194 | }; |
| 195 | |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 196 | /// A normalized data-in-code entry. |
| 197 | struct DataInCode { |
| 198 | Hex32 offset; |
| 199 | Hex16 length; |
| 200 | DataRegionType kind; |
| 201 | }; |
| 202 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 203 | |
| 204 | /// A typedef so that YAML I/O can encode/decode mach_header.flags. |
Alexey Samsonov | 8e6829e | 2014-03-19 09:38:31 +0000 | [diff] [blame] | 205 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, FileFlags) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 206 | |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 207 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 208 | /// |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 209 | struct NormalizedFile { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 210 | NormalizedFile() : arch(MachOLinkingContext::arch_unknown), |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 211 | fileType(llvm::MachO::MH_OBJECT), |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 212 | flags(0), |
| 213 | hasUUID(false), |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 214 | os(MachOLinkingContext::OS::unknown) { } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 215 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 216 | MachOLinkingContext::Arch arch; |
| 217 | HeaderFileType fileType; |
| 218 | FileFlags flags; |
| 219 | std::vector<Segment> segments; // Not used in object files. |
| 220 | std::vector<Section> sections; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 221 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 222 | // Symbols sorted by kind. |
| 223 | std::vector<Symbol> localSymbols; |
| 224 | std::vector<Symbol> globalSymbols; |
| 225 | std::vector<Symbol> undefinedSymbols; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 226 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 227 | // Maps to load commands with no LINKEDIT content (final linked images only). |
| 228 | std::vector<DependentDylib> dependentDylibs; |
| 229 | StringRef installName; |
| 230 | bool hasUUID; |
| 231 | std::vector<StringRef> rpaths; |
| 232 | Hex64 entryAddress; |
| 233 | MachOLinkingContext::OS os; |
| 234 | Hex64 sourceVersion; |
| 235 | Hex32 minOSverson; |
| 236 | Hex32 sdkVersion; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 237 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 238 | // Maps to load commands with LINKEDIT content (final linked images only). |
Nick Kledzik | 1bebb28 | 2014-09-09 23:52:59 +0000 | [diff] [blame] | 239 | Hex32 pageSize; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 240 | std::vector<RebaseLocation> rebasingInfo; |
| 241 | std::vector<BindLocation> bindingInfo; |
| 242 | std::vector<BindLocation> weakBindingInfo; |
| 243 | std::vector<BindLocation> lazyBindingInfo; |
| 244 | std::vector<Export> exportInfo; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 245 | std::vector<DataInCode> dataInCode; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 246 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 247 | // TODO: |
| 248 | // code-signature |
| 249 | // split-seg-info |
| 250 | // function-starts |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 251 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 252 | // For any allocations in this struct which need to be owned by this struct. |
| 253 | BumpPtrAllocator ownedAllocations; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 256 | /// Tests if a file is a non-fat mach-o object file. |
| 257 | bool isThinObjectFile(StringRef path, MachOLinkingContext::Arch &arch); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 258 | |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame^] | 259 | /// If the buffer is a fat file with the request arch, then this function |
| 260 | /// returns true with 'offset' and 'size' set to location of the arch slice |
| 261 | /// within the buffer. Otherwise returns false; |
| 262 | bool sliceFromFatFile(const MemoryBuffer &mb, MachOLinkingContext::Arch arch, |
| 263 | uint32_t &offset, uint32_t &size); |
| 264 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 265 | /// Reads a mach-o file and produces an in-memory normalized view. |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 266 | ErrorOr<std::unique_ptr<NormalizedFile>> |
| 267 | readBinary(std::unique_ptr<MemoryBuffer> &mb, |
| 268 | const MachOLinkingContext::Arch arch); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 269 | |
| 270 | /// Takes in-memory normalized view and writes a mach-o object file. |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 271 | std::error_code writeBinary(const NormalizedFile &file, StringRef path); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 272 | |
| 273 | size_t headerAndLoadCommandsSize(const NormalizedFile &file); |
| 274 | |
| 275 | |
| 276 | /// Parses a yaml encoded mach-o file to produce an in-memory normalized view. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 277 | ErrorOr<std::unique_ptr<NormalizedFile>> |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 278 | readYaml(std::unique_ptr<MemoryBuffer> &mb); |
| 279 | |
| 280 | /// Writes a yaml encoded mach-o files given an in-memory normalized view. |
Rafael Espindola | b1a4d3a | 2014-06-12 14:53:47 +0000 | [diff] [blame] | 281 | std::error_code writeYaml(const NormalizedFile &file, raw_ostream &out); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 282 | |
| 283 | /// Takes in-memory normalized dylib or object and parses it into lld::File |
Rui Ueyama | 170a1a8 | 2013-12-20 07:48:29 +0000 | [diff] [blame] | 284 | ErrorOr<std::unique_ptr<lld::File>> |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 285 | normalizedToAtoms(const NormalizedFile &normalizedFile, StringRef path, |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 286 | bool copyRefs); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 287 | |
| 288 | /// Takes atoms and generates a normalized macho-o view. |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 289 | ErrorOr<std::unique_ptr<NormalizedFile>> |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 290 | normalizedFromAtoms(const lld::File &atomFile, const MachOLinkingContext &ctxt); |
| 291 | |
| 292 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 293 | } // namespace normalized |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 294 | |
| 295 | /// Class for interfacing mach-o yaml files into generic yaml parsing |
| 296 | class MachOYamlIOTaggedDocumentHandler : public YamlIOTaggedDocumentHandler { |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 297 | public: |
| 298 | MachOYamlIOTaggedDocumentHandler(MachOLinkingContext::Arch arch) |
| 299 | : _arch(arch) { } |
Rui Ueyama | bc69bce | 2014-03-28 21:36:33 +0000 | [diff] [blame] | 300 | bool handledDocTag(llvm::yaml::IO &io, const lld::File *&file) const override; |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 301 | private: |
| 302 | const MachOLinkingContext::Arch _arch; |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 303 | }; |
| 304 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 305 | } // namespace mach_o |
| 306 | } // namespace lld |
| 307 | |
Rui Ueyama | 014192db | 2013-11-15 03:09:26 +0000 | [diff] [blame] | 308 | #endif // LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H |