Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/Native/NativeFileFormat.h -------------------------===// |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +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 | |
Rui Ueyama | 014192db | 2013-11-15 03:09:26 +0000 | [diff] [blame] | 10 | #ifndef LLD_READER_WRITER_NATIVE_NATIVE_FILE_FORMAT_H |
| 11 | #define LLD_READER_WRITER_NATIVE_NATIVE_FILE_FORMAT_H |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 12 | |
Rui Ueyama | 86f32f4 | 2013-11-19 03:48:23 +0000 | [diff] [blame] | 13 | #include <cstdint> |
| 14 | |
Michael J. Spencer | 9f91f95 | 2012-03-09 05:27:11 +0000 | [diff] [blame] | 15 | #include "llvm/Support/DataTypes.h" |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 16 | |
| 17 | namespace lld { |
| 18 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 19 | // |
| 20 | // Overview: |
| 21 | // |
| 22 | // The number one design goal of this file format is enable the linker to |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 23 | // read object files into in-memory Atom objects extremely quickly. |
| 24 | // The second design goal is to enable future modifications to the |
| 25 | // Atom attribute model. |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 26 | // |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 27 | // The llvm native object file format is not like traditional object file |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 28 | // formats (e.g. ELF, COFF, mach-o). There is no symbol table and no |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 29 | // sections. Instead the file is essentially an array of archived Atoms. |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 30 | // It is *not* serialized Atoms which would require deserialization into |
| 31 | // in memory objects. Instead it is an array of read-only info about each |
| 32 | // Atom. The NativeReader bulk creates in-memory Atoms which just have |
| 33 | // an ivar which points to the read-only info for that Atom. No additional |
| 34 | // processing is done to construct the in-memory Atoms. All Atom attribute |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 35 | // getter methods are virtual calls which dig up the info they need from the |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 36 | // ivar data. |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 37 | // |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 38 | // To support the gradual evolution of Atom attributes, the Atom read-only |
| 39 | // data is versioned. The NativeReader chooses which in-memory Atom class |
| 40 | // to use based on the version. What this means is that if new attributes |
| 41 | // are added (or changed) in the Atom model, a new native atom class and |
| 42 | // read-only atom info struct needs to be defined. Then, all the existing |
| 43 | // native reader atom classes need to be modified to do their best effort |
| 44 | // to map their old style read-only data to the new Atom model. At some point |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 45 | // some classes to support old versions may be dropped. |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 46 | // |
| 47 | // |
| 48 | // Details: |
| 49 | // |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 50 | // The native object file format consists of a header that specifies the |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 51 | // endianness of the file and the architecture along with a list of "chunks" |
| 52 | // in the file. A Chunk is simply a tagged range of the file. There is |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 53 | // one chunk for the array of atom infos. There is another chunk for the |
| 54 | // string pool, and another for the content pool. |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 55 | // |
| 56 | // It turns out there most atoms have very similar sets of attributes, only |
| 57 | // the name and content attribute vary. To exploit this fact to reduce the file |
| 58 | // size, the atom read-only info contains just the name and content info plus |
| 59 | // a reference to which attribute set it uses. The attribute sets are stored |
| 60 | // in another chunk. |
| 61 | // |
| 62 | |
| 63 | |
| 64 | // |
| 65 | // An entry in the NativeFileHeader that describes one chunk of the file. |
| 66 | // |
| 67 | struct NativeChunk { |
| 68 | uint32_t signature; |
| 69 | uint32_t fileOffset; |
| 70 | uint32_t fileSize; |
| 71 | uint32_t elementCount; |
| 72 | }; |
| 73 | |
| 74 | |
| 75 | // |
| 76 | // The header in a native object file |
| 77 | // |
| 78 | struct NativeFileHeader { |
| 79 | uint8_t magic[16]; |
| 80 | uint32_t endian; |
| 81 | uint32_t architecture; |
| 82 | uint32_t fileSize; |
| 83 | uint32_t chunkCount; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 84 | // NativeChunk chunks[] |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | // |
| 88 | // Possible values for NativeChunk.signature field |
| 89 | // |
| 90 | enum NativeChunkSignatures { |
| 91 | NCS_DefinedAtomsV1 = 1, |
| 92 | NCS_AttributesArrayV1 = 2, |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 93 | NCS_AbsoluteAttributesV1 = 12, |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 94 | NCS_UndefinedAtomsV1 = 3, |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 95 | NCS_SharedLibraryAtomsV1 = 4, |
| 96 | NCS_AbsoluteAtomsV1 = 5, |
| 97 | NCS_Strings = 6, |
| 98 | NCS_ReferencesArrayV1 = 7, |
| 99 | NCS_ReferencesArrayV2 = 8, |
| 100 | NCS_TargetsTable = 9, |
| 101 | NCS_AddendsTable = 10, |
| 102 | NCS_Content = 11, |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 103 | }; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 104 | |
| 105 | // |
| 106 | // The 16-bytes at the start of a native object file |
| 107 | // |
| 108 | #define NATIVE_FILE_HEADER_MAGIC "llvm nat obj v1 " |
| 109 | |
| 110 | // |
| 111 | // Possible values for the NativeFileHeader.endian field |
| 112 | // |
| 113 | enum { |
| 114 | NFH_BigEndian = 0x42696745, |
| 115 | NFH_LittleEndian = 0x4574696c |
| 116 | }; |
| 117 | |
| 118 | |
| 119 | // |
| 120 | // Possible values for the NativeFileHeader.architecture field |
| 121 | // |
| 122 | enum { |
| 123 | NFA_x86 = 1, |
| 124 | NFA_x86_64 = 2, |
| 125 | NFA_armv6 = 3, |
| 126 | NFA_armv7 = 4, |
| 127 | }; |
| 128 | |
| 129 | |
| 130 | // |
| 131 | // The NCS_DefinedAtomsV1 chunk contains an array of these structs |
| 132 | // |
| 133 | struct NativeDefinedAtomIvarsV1 { |
| 134 | uint32_t nameOffset; |
| 135 | uint32_t attributesOffset; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 136 | uint32_t referencesStartIndex; |
| 137 | uint32_t referencesCount; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 138 | uint32_t contentOffset; |
| 139 | uint32_t contentSize; |
| 140 | }; |
| 141 | |
| 142 | |
| 143 | // |
| 144 | // The NCS_AttributesArrayV1 chunk contains an array of these structs |
| 145 | // |
| 146 | struct NativeAtomAttributesV1 { |
| 147 | uint32_t sectionNameOffset; |
| 148 | uint16_t align2; |
| 149 | uint16_t alignModulus; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 150 | uint8_t scope; |
| 151 | uint8_t interposable; |
| 152 | uint8_t merge; |
| 153 | uint8_t contentType; |
Nick Kledzik | 36293f6 | 2013-01-23 22:32:56 +0000 | [diff] [blame] | 154 | uint8_t sectionChoiceAndPosition; // high nibble is choice, low is position |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 155 | uint8_t deadStrip; |
Michael J. Spencer | b8ab9f5 | 2013-11-08 21:04:20 +0000 | [diff] [blame] | 156 | uint8_t dynamicExport; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 157 | uint8_t permissions; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 158 | uint8_t alias; |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | |
| 162 | |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 163 | // |
| 164 | // The NCS_UndefinedAtomsV1 chunk contains an array of these structs |
| 165 | // |
| 166 | struct NativeUndefinedAtomIvarsV1 { |
| 167 | uint32_t nameOffset; |
| 168 | uint32_t flags; |
Rui Ueyama | e5416ec | 2013-09-12 19:14:05 +0000 | [diff] [blame] | 169 | uint32_t fallbackNameOffset; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 173 | // |
| 174 | // The NCS_SharedLibraryAtomsV1 chunk contains an array of these structs |
| 175 | // |
| 176 | struct NativeSharedLibraryAtomIvarsV1 { |
Michael J. Spencer | 4355bb9 | 2013-09-26 22:08:43 +0000 | [diff] [blame] | 177 | uint64_t size; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 178 | uint32_t nameOffset; |
| 179 | uint32_t loadNameOffset; |
Michael J. Spencer | 4355bb9 | 2013-09-26 22:08:43 +0000 | [diff] [blame] | 180 | uint32_t type; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 181 | uint32_t flags; |
| 182 | }; |
| 183 | |
| 184 | |
| 185 | |
| 186 | // |
| 187 | // The NCS_AbsoluteAtomsV1 chunk contains an array of these structs |
| 188 | // |
| 189 | struct NativeAbsoluteAtomIvarsV1 { |
| 190 | uint32_t nameOffset; |
Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 191 | uint32_t attributesOffset; |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 192 | uint32_t reserved; |
| 193 | uint64_t value; |
| 194 | }; |
| 195 | |
| 196 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 197 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 198 | // |
| 199 | // The NCS_ReferencesArrayV1 chunk contains an array of these structs |
| 200 | // |
| 201 | struct NativeReferenceIvarsV1 { |
Nick Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 202 | enum { |
Rui Ueyama | 86f32f4 | 2013-11-19 03:48:23 +0000 | [diff] [blame] | 203 | noTarget = UINT16_MAX |
Nick Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 204 | }; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 205 | uint16_t offsetInAtom; |
| 206 | int16_t kind; |
| 207 | uint16_t targetIndex; |
| 208 | uint16_t addendIndex; |
| 209 | }; |
| 210 | |
| 211 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 212 | // |
| 213 | // The NCS_ReferencesArrayV2 chunk contains an array of these structs |
| 214 | // |
| 215 | struct NativeReferenceIvarsV2 { |
Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame^] | 216 | enum { |
| 217 | noTarget = UINT32_MAX |
| 218 | }; |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 219 | uint64_t offsetInAtom; |
| 220 | int64_t addend; |
| 221 | int32_t kind; |
| 222 | uint32_t targetIndex; |
| 223 | }; |
| 224 | |
| 225 | |
| 226 | // |
| 227 | // The NCS_TargetsTable chunk contains an array of uint32_t entries. |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 228 | // The C++ class Reference has a target() method that returns a |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 229 | // pointer to another Atom. We can't have pointers in object files, |
| 230 | // so instead NativeReferenceIvarsV1 contains an index to the target. |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 231 | // The index is into this NCS_TargetsTable of uint32_t entries. |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 232 | // The values in this table are the index of the (target) atom in this file. |
| 233 | // For DefinedAtoms the value is from 0 to NCS_DefinedAtomsV1.elementCount. |
| 234 | // For UndefinedAtoms the value is from NCS_DefinedAtomsV1.elementCount to |
| 235 | // NCS_DefinedAtomsV1.elementCount+NCS_UndefinedAtomsV1.elementCount. |
| 236 | // |
| 237 | |
| 238 | |
| 239 | // |
| 240 | // The NCS_AddendsTable chunk contains an array of int64_t entries. |
| 241 | // If we allocated space for addends directly in NativeReferenceIvarsV1 |
| 242 | // it would double the size of that struct. But since addends are rare, |
| 243 | // we instead just keep a pool of addends and have NativeReferenceIvarsV1 |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 244 | // (if it needs an addend) just store the index (into the pool) of the |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 245 | // addend it needs. |
| 246 | // |
| 247 | |
Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 248 | |
| 249 | |
| 250 | } // namespace lld |
| 251 | |
Rui Ueyama | 014192db | 2013-11-15 03:09:26 +0000 | [diff] [blame] | 252 | #endif // LLD_READER_WRITER_NATIVE_NATIVE_FILE_FORMAT_H |