Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/MachONormalizedFile.h -----------------------===// |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 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 | |
Pete Cooper | d75b718 | 2016-02-08 21:50:45 +0000 | [diff] [blame] | 42 | #ifndef LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H |
| 43 | #define LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H |
| 44 | |
Lang Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame^] | 45 | #include "DebugInfo.h" |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 46 | #include "lld/Core/Error.h" |
| 47 | #include "lld/Core/LLVM.h" |
| 48 | #include "lld/ReaderWriter/MachOLinkingContext.h" |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 49 | #include "llvm/ADT/SmallString.h" |
| 50 | #include "llvm/ADT/StringRef.h" |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Allocator.h" |
Pete Cooper | e5fa5a3 | 2015-12-16 22:03:21 +0000 | [diff] [blame] | 52 | #include "llvm/Support/Debug.h" |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 53 | #include "llvm/Support/ErrorOr.h" |
| 54 | #include "llvm/Support/MachO.h" |
| 55 | #include "llvm/Support/YAMLTraits.h" |
| 56 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 57 | using llvm::BumpPtrAllocator; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 58 | using llvm::yaml::Hex64; |
| 59 | using llvm::yaml::Hex32; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 60 | using llvm::yaml::Hex16; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 61 | using llvm::yaml::Hex8; |
| 62 | using llvm::yaml::SequenceTraits; |
| 63 | using llvm::MachO::HeaderFileType; |
| 64 | using llvm::MachO::BindType; |
| 65 | using llvm::MachO::RebaseType; |
| 66 | using llvm::MachO::NListType; |
| 67 | using llvm::MachO::RelocationInfoType; |
| 68 | using llvm::MachO::SectionType; |
| 69 | using llvm::MachO::LoadCommandType; |
| 70 | using llvm::MachO::ExportSymbolKind; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 71 | using llvm::MachO::DataRegionType; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 72 | |
| 73 | namespace lld { |
| 74 | namespace mach_o { |
| 75 | namespace normalized { |
| 76 | |
| 77 | |
| 78 | /// The real mach-o relocation record is 8-bytes on disk and is |
| 79 | /// encoded in one of two different bit-field patterns. This |
Nick Kledzik | 369ffd1 | 2013-10-08 02:07:19 +0000 | [diff] [blame] | 80 | /// normalized form has the union of all possible fields. |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 81 | struct Relocation { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 82 | Relocation() : offset(0), scattered(false), |
| 83 | type(llvm::MachO::GENERIC_RELOC_VANILLA), |
| 84 | length(0), pcRel(false), isExtern(false), value(0), |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 85 | symbol(0) { } |
| 86 | |
| 87 | Hex32 offset; |
| 88 | bool scattered; |
| 89 | RelocationInfoType type; |
| 90 | uint8_t length; |
| 91 | bool pcRel; |
| 92 | bool isExtern; |
| 93 | Hex32 value; |
| 94 | uint32_t symbol; |
Lang Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame^] | 95 | |
| 96 | #ifndef NDEBUG |
| 97 | raw_ostream& operator<<(raw_ostream &OS) const { |
| 98 | dump(OS); |
| 99 | return OS; |
| 100 | } |
| 101 | |
| 102 | void dump(raw_ostream &OS = llvm::dbgs()) const; |
| 103 | #endif |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Lang Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame^] | 106 | inline raw_ostream& operator<<(raw_ostream &OS, const Relocation &R) { |
| 107 | R.dump(OS); |
| 108 | return OS; |
| 109 | } |
| 110 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 111 | /// A typedef so that YAML I/O can treat this vector as a sequence. |
| 112 | typedef std::vector<Relocation> Relocations; |
| 113 | |
| 114 | /// A typedef so that YAML I/O can process the raw bytes in a section. |
| 115 | typedef std::vector<Hex8> ContentBytes; |
| 116 | |
| 117 | /// A typedef so that YAML I/O can treat indirect symbols as a flow sequence. |
| 118 | typedef std::vector<uint32_t> IndirectSymbols; |
| 119 | |
| 120 | /// A typedef so that YAML I/O can encode/decode section attributes. |
Alexey Samsonov | 8e6829e | 2014-03-19 09:38:31 +0000 | [diff] [blame] | 121 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, SectionAttr) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 122 | |
Pete Cooper | 3f564a5 | 2016-03-24 00:36:37 +0000 | [diff] [blame] | 123 | /// A typedef so that YAML I/O can encode/decode section alignment. |
| 124 | LLVM_YAML_STRONG_TYPEDEF(uint16_t, SectionAlignment) |
| 125 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 126 | /// Mach-O has a 32-bit and 64-bit section record. This normalized form |
| 127 | /// can support either kind. |
| 128 | struct Section { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 129 | Section() : type(llvm::MachO::S_REGULAR), |
Rui Ueyama | f006f4d | 2015-03-26 01:44:01 +0000 | [diff] [blame] | 130 | attributes(0), alignment(1), address(0) { } |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 131 | |
| 132 | StringRef segmentName; |
| 133 | StringRef sectionName; |
| 134 | SectionType type; |
| 135 | SectionAttr attributes; |
Pete Cooper | 3f564a5 | 2016-03-24 00:36:37 +0000 | [diff] [blame] | 136 | SectionAlignment alignment; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 137 | Hex64 address; |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 138 | ArrayRef<uint8_t> content; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 139 | Relocations relocations; |
| 140 | IndirectSymbols indirectSymbols; |
Pete Cooper | e5fa5a3 | 2015-12-16 22:03:21 +0000 | [diff] [blame] | 141 | |
| 142 | #ifndef NDEBUG |
| 143 | raw_ostream& operator<<(raw_ostream &OS) const { |
| 144 | dump(OS); |
| 145 | return OS; |
| 146 | } |
| 147 | |
| 148 | void dump(raw_ostream &OS = llvm::dbgs()) const; |
| 149 | #endif |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | |
| 153 | /// 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] | 154 | LLVM_YAML_STRONG_TYPEDEF(uint8_t, SymbolScope) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 155 | |
| 156 | /// 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] | 157 | LLVM_YAML_STRONG_TYPEDEF(uint16_t, SymbolDesc) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 158 | |
| 159 | /// Mach-O has a 32-bit and 64-bit symbol table entry (nlist), and the symbol |
| 160 | /// 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] | 161 | /// works for any pointer size and separates out the type and scope. |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 162 | struct Symbol { |
| 163 | Symbol() : type(llvm::MachO::N_UNDF), scope(0), sect(0), desc(0), value(0) { } |
| 164 | |
| 165 | StringRef name; |
| 166 | NListType type; |
| 167 | SymbolScope scope; |
| 168 | uint8_t sect; |
| 169 | SymbolDesc desc; |
| 170 | Hex64 value; |
| 171 | }; |
| 172 | |
Lang Hames | ac2adce | 2015-12-11 23:25:09 +0000 | [diff] [blame] | 173 | /// Check whether the given section type indicates a zero-filled section. |
| 174 | // FIXME: Utility functions of this kind should probably be moved into |
| 175 | // llvm/Support. |
| 176 | inline bool isZeroFillSection(SectionType T) { |
| 177 | return (T == llvm::MachO::S_ZEROFILL || |
| 178 | T == llvm::MachO::S_THREAD_LOCAL_ZEROFILL); |
| 179 | } |
| 180 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 181 | /// 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] | 182 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, VMProtect) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 183 | |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 184 | /// A typedef to hold verions X.Y.X packed into 32-bit xxxx.yy.zz |
| 185 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, PackedVersion) |
| 186 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 187 | /// Segments are only used in normalized final linked images (not in relocatable |
| 188 | /// object files). They specify how a range of the file is loaded. |
| 189 | struct Segment { |
| 190 | StringRef name; |
| 191 | Hex64 address; |
| 192 | Hex64 size; |
Pete Cooper | b8fec3e | 2016-02-06 00:51:16 +0000 | [diff] [blame] | 193 | VMProtect init_access; |
| 194 | VMProtect max_access; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | /// Only used in normalized final linked images to specify on which dylibs |
| 198 | /// it depends. |
| 199 | struct DependentDylib { |
| 200 | StringRef path; |
| 201 | LoadCommandType kind; |
Nick Kledzik | 5b9e48b | 2014-11-19 02:21:53 +0000 | [diff] [blame] | 202 | PackedVersion compatVersion; |
| 203 | PackedVersion currentVersion; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | /// A normalized rebasing entry. Only used in normalized final linked images. |
| 207 | struct RebaseLocation { |
| 208 | Hex32 segOffset; |
| 209 | uint8_t segIndex; |
| 210 | RebaseType kind; |
| 211 | }; |
| 212 | |
| 213 | /// A normalized binding entry. Only used in normalized final linked images. |
| 214 | struct BindLocation { |
| 215 | Hex32 segOffset; |
| 216 | uint8_t segIndex; |
| 217 | BindType kind; |
| 218 | bool canBeNull; |
| 219 | int ordinal; |
| 220 | StringRef symbolName; |
| 221 | Hex64 addend; |
| 222 | }; |
| 223 | |
| 224 | /// A typedef so that YAML I/O can encode/decode export flags. |
Alexey Samsonov | 8e6829e | 2014-03-19 09:38:31 +0000 | [diff] [blame] | 225 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, ExportFlags) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 226 | |
| 227 | /// A normalized export entry. Only used in normalized final linked images. |
| 228 | struct Export { |
| 229 | StringRef name; |
| 230 | Hex64 offset; |
| 231 | ExportSymbolKind kind; |
| 232 | ExportFlags flags; |
| 233 | Hex32 otherOffset; |
| 234 | StringRef otherName; |
| 235 | }; |
| 236 | |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 237 | /// A normalized data-in-code entry. |
| 238 | struct DataInCode { |
| 239 | Hex32 offset; |
| 240 | Hex16 length; |
| 241 | DataRegionType kind; |
| 242 | }; |
| 243 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 244 | /// 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] | 245 | LLVM_YAML_STRONG_TYPEDEF(uint32_t, FileFlags) |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 246 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 247 | /// |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 248 | struct NormalizedFile { |
Benjamin Kramer | cfacc9d | 2015-06-23 19:55:04 +0000 | [diff] [blame] | 249 | MachOLinkingContext::Arch arch = MachOLinkingContext::arch_unknown; |
| 250 | HeaderFileType fileType = llvm::MachO::MH_OBJECT; |
| 251 | FileFlags flags = 0; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 252 | std::vector<Segment> segments; // Not used in object files. |
| 253 | std::vector<Section> sections; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 254 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 255 | // Symbols sorted by kind. |
| 256 | std::vector<Symbol> localSymbols; |
| 257 | std::vector<Symbol> globalSymbols; |
| 258 | std::vector<Symbol> undefinedSymbols; |
Lang Hames | 436f7d6 | 2016-07-27 22:55:30 +0000 | [diff] [blame^] | 259 | std::vector<Symbol> stabsSymbols; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 260 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 261 | // Maps to load commands with no LINKEDIT content (final linked images only). |
| 262 | std::vector<DependentDylib> dependentDylibs; |
Benjamin Kramer | cfacc9d | 2015-06-23 19:55:04 +0000 | [diff] [blame] | 263 | StringRef installName; // dylibs only |
| 264 | PackedVersion compatVersion = 0; // dylibs only |
| 265 | PackedVersion currentVersion = 0; // dylibs only |
| 266 | bool hasUUID = false; |
Pete Cooper | 354809e | 2016-02-03 22:28:29 +0000 | [diff] [blame] | 267 | bool hasMinVersionLoadCommand = false; |
Pete Cooper | 9b28a45 | 2016-02-09 02:10:39 +0000 | [diff] [blame] | 268 | bool generateDataInCodeLoadCommand = false; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 269 | std::vector<StringRef> rpaths; |
Benjamin Kramer | cfacc9d | 2015-06-23 19:55:04 +0000 | [diff] [blame] | 270 | Hex64 entryAddress = 0; |
| 271 | Hex64 stackSize = 0; |
| 272 | MachOLinkingContext::OS os = MachOLinkingContext::OS::unknown; |
| 273 | Hex64 sourceVersion = 0; |
| 274 | PackedVersion minOSverson = 0; |
| 275 | PackedVersion sdkVersion = 0; |
Pete Cooper | ceee5de | 2016-02-04 02:16:08 +0000 | [diff] [blame] | 276 | LoadCommandType minOSVersionKind = (LoadCommandType)0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 277 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 278 | // Maps to load commands with LINKEDIT content (final linked images only). |
Benjamin Kramer | cfacc9d | 2015-06-23 19:55:04 +0000 | [diff] [blame] | 279 | Hex32 pageSize = 0; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 280 | std::vector<RebaseLocation> rebasingInfo; |
| 281 | std::vector<BindLocation> bindingInfo; |
| 282 | std::vector<BindLocation> weakBindingInfo; |
| 283 | std::vector<BindLocation> lazyBindingInfo; |
| 284 | std::vector<Export> exportInfo; |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 285 | std::vector<uint8_t> functionStarts; |
Nick Kledzik | 2192137 | 2014-07-24 23:06:56 +0000 | [diff] [blame] | 286 | std::vector<DataInCode> dataInCode; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 287 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 288 | // TODO: |
| 289 | // code-signature |
| 290 | // split-seg-info |
| 291 | // function-starts |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 292 | |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 293 | // For any allocations in this struct which need to be owned by this struct. |
| 294 | BumpPtrAllocator ownedAllocations; |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
Nick Kledzik | 635f9c7 | 2014-09-04 20:08:30 +0000 | [diff] [blame] | 297 | /// Tests if a file is a non-fat mach-o object file. |
| 298 | bool isThinObjectFile(StringRef path, MachOLinkingContext::Arch &arch); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 299 | |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 300 | /// If the buffer is a fat file with the request arch, then this function |
| 301 | /// returns true with 'offset' and 'size' set to location of the arch slice |
| 302 | /// within the buffer. Otherwise returns false; |
Rafael Espindola | ed48e53 | 2015-04-27 22:48:51 +0000 | [diff] [blame] | 303 | bool sliceFromFatFile(MemoryBufferRef mb, MachOLinkingContext::Arch arch, |
| 304 | uint32_t &offset, uint32_t &size); |
Nick Kledzik | 14b5d20 | 2014-10-08 01:48:10 +0000 | [diff] [blame] | 305 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 306 | /// Reads a mach-o file and produces an in-memory normalized view. |
Pete Cooper | c6e7b81 | 2016-03-30 23:58:24 +0000 | [diff] [blame] | 307 | llvm::Expected<std::unique_ptr<NormalizedFile>> |
Joey Gouly | 010b376 | 2014-01-14 22:32:38 +0000 | [diff] [blame] | 308 | readBinary(std::unique_ptr<MemoryBuffer> &mb, |
| 309 | const MachOLinkingContext::Arch arch); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 310 | |
| 311 | /// Takes in-memory normalized view and writes a mach-o object file. |
Pete Cooper | fefbd22 | 2016-03-30 23:10:39 +0000 | [diff] [blame] | 312 | llvm::Error writeBinary(const NormalizedFile &file, StringRef path); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 313 | |
| 314 | size_t headerAndLoadCommandsSize(const NormalizedFile &file); |
| 315 | |
| 316 | |
| 317 | /// Parses a yaml encoded mach-o file to produce an in-memory normalized view. |
Pete Cooper | 2f6216c | 2016-03-31 01:13:04 +0000 | [diff] [blame] | 318 | llvm::Expected<std::unique_ptr<NormalizedFile>> |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 319 | readYaml(std::unique_ptr<MemoryBuffer> &mb); |
| 320 | |
| 321 | /// 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] | 322 | std::error_code writeYaml(const NormalizedFile &file, raw_ostream &out); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 323 | |
Pete Cooper | ec4e166 | 2016-03-30 23:43:27 +0000 | [diff] [blame] | 324 | llvm::Error |
Rui Ueyama | 1d51042 | 2014-12-12 07:31:09 +0000 | [diff] [blame] | 325 | normalizedObjectToAtoms(MachOFile *file, |
| 326 | const NormalizedFile &normalizedFile, |
| 327 | bool copyRefs); |
| 328 | |
Pete Cooper | ec4e166 | 2016-03-30 23:43:27 +0000 | [diff] [blame] | 329 | llvm::Error |
Rui Ueyama | 1d51042 | 2014-12-12 07:31:09 +0000 | [diff] [blame] | 330 | normalizedDylibToAtoms(MachODylibFile *file, |
| 331 | const NormalizedFile &normalizedFile, |
| 332 | bool copyRefs); |
| 333 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 334 | /// Takes in-memory normalized dylib or object and parses it into lld::File |
Pete Cooper | ec4e166 | 2016-03-30 23:43:27 +0000 | [diff] [blame] | 335 | llvm::Expected<std::unique_ptr<lld::File>> |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 336 | normalizedToAtoms(const NormalizedFile &normalizedFile, StringRef path, |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 337 | bool copyRefs); |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 338 | |
| 339 | /// Takes atoms and generates a normalized macho-o view. |
Pete Cooper | fefbd22 | 2016-03-30 23:10:39 +0000 | [diff] [blame] | 340 | llvm::Expected<std::unique_ptr<NormalizedFile>> |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 341 | normalizedFromAtoms(const lld::File &atomFile, const MachOLinkingContext &ctxt); |
| 342 | |
| 343 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 344 | } // namespace normalized |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 345 | |
| 346 | /// Class for interfacing mach-o yaml files into generic yaml parsing |
| 347 | class MachOYamlIOTaggedDocumentHandler : public YamlIOTaggedDocumentHandler { |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 348 | public: |
| 349 | MachOYamlIOTaggedDocumentHandler(MachOLinkingContext::Arch arch) |
| 350 | : _arch(arch) { } |
Rui Ueyama | bc69bce | 2014-03-28 21:36:33 +0000 | [diff] [blame] | 351 | bool handledDocTag(llvm::yaml::IO &io, const lld::File *&file) const override; |
Nick Kledzik | 378066c | 2014-06-30 22:57:33 +0000 | [diff] [blame] | 352 | private: |
| 353 | const MachOLinkingContext::Arch _arch; |
Nick Kledzik | 6edd722 | 2014-01-11 01:07:43 +0000 | [diff] [blame] | 354 | }; |
| 355 | |
Nick Kledzik | 30332b1 | 2013-10-08 00:43:34 +0000 | [diff] [blame] | 356 | } // namespace mach_o |
| 357 | } // namespace lld |
| 358 | |
Rui Ueyama | 014192db | 2013-11-15 03:09:26 +0000 | [diff] [blame] | 359 | #endif // LLD_READER_WRITER_MACHO_NORMALIZE_FILE_H |