| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/Native/WriterNative.cpp ---------------------------===// |
| 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 | |
| Michael J. Spencer | 64afcb4 | 2013-01-23 01:18:43 +0000 | [diff] [blame] | 10 | #include "lld/ReaderWriter/Writer.h" |
| Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame] | 11 | #include "lld/Core/File.h" |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 12 | |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/ArrayRef.h" |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/DenseMap.h" |
| Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringRef.h" |
| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include "llvm/Support/system_error.h" |
| 18 | |
| 19 | #include "NativeFileFormat.h" |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 20 | |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 21 | #include <cstdint> |
| 22 | #include <set> |
| Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame] | 23 | #include <vector> |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 24 | |
| 25 | namespace lld { |
| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 26 | namespace native { |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 27 | |
| 28 | /// |
| 29 | /// Class for writing native object files. |
| 30 | /// |
| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 31 | class Writer : public lld::Writer { |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 32 | public: |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 33 | Writer(const LinkingContext &context) {} |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 34 | |
| Rui Ueyama | 16e543b | 2014-03-06 21:14:04 +0000 | [diff] [blame^] | 35 | error_code writeFile(const lld::File &file, StringRef outPath) override { |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 36 | // reserve first byte for unnamed atoms |
| 37 | _stringPool.push_back('\0'); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 38 | // visit all atoms |
| Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 39 | for ( const DefinedAtom *defAtom : file.defined() ) { |
| 40 | this->addIVarsForDefinedAtom(*defAtom); |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 41 | } |
| Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 42 | for ( const UndefinedAtom *undefAtom : file.undefined() ) { |
| 43 | this->addIVarsForUndefinedAtom(*undefAtom); |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 44 | } |
| Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 45 | for ( const SharedLibraryAtom *shlibAtom : file.sharedLibrary() ) { |
| 46 | this->addIVarsForSharedLibraryAtom(*shlibAtom); |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 47 | } |
| Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 48 | for ( const AbsoluteAtom *absAtom : file.absolute() ) { |
| 49 | this->addIVarsForAbsoluteAtom(*absAtom); |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 52 | maybeConvertReferencesToV1(); |
| 53 | |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 54 | // construct file header based on atom information accumulated |
| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 55 | this->makeHeader(); |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 56 | |
| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 57 | std::string errorInfo; |
| 58 | llvm::raw_fd_ostream out(outPath.data(), errorInfo, |
| Rafael Espindola | f27f9fa | 2014-02-24 18:20:36 +0000 | [diff] [blame] | 59 | llvm::sys::fs::F_None); |
| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 60 | if (!errorInfo.empty()) |
| 61 | return error_code::success(); // FIXME |
| 62 | |
| 63 | this->write(out); |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 64 | |
| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 65 | return error_code::success(); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 68 | virtual ~Writer() { |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 73 | // write the lld::File in native format to the specified stream |
| Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 74 | void write(raw_ostream &out) { |
| Rui Ueyama | 3f823e3 | 2013-11-15 22:37:34 +0000 | [diff] [blame] | 75 | assert(out.tell() == 0); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 76 | out.write((char*)_headerBuffer, _headerBufferSize); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 77 | |
| Rui Ueyama | 3f823e3 | 2013-11-15 22:37:34 +0000 | [diff] [blame] | 78 | writeChunk(out, _definedAtomIvars, NCS_DefinedAtomsV1); |
| 79 | writeChunk(out, _attributes, NCS_AttributesArrayV1); |
| 80 | writeChunk(out, _undefinedAtomIvars, NCS_UndefinedAtomsV1); |
| 81 | writeChunk(out, _sharedLibraryAtomIvars, NCS_SharedLibraryAtomsV1); |
| 82 | writeChunk(out, _absoluteAtomIvars, NCS_AbsoluteAtomsV1); |
| 83 | writeChunk(out, _absAttributes, NCS_AbsoluteAttributesV1); |
| 84 | writeChunk(out, _stringPool, NCS_Strings); |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 85 | writeChunk(out, _referencesV1, NCS_ReferencesArrayV1); |
| 86 | writeChunk(out, _referencesV2, NCS_ReferencesArrayV2); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 87 | |
| Rui Ueyama | 3f823e3 | 2013-11-15 22:37:34 +0000 | [diff] [blame] | 88 | if (!_targetsTableIndex.empty()) { |
| 89 | assert(out.tell() == findChunk(NCS_TargetsTable).fileOffset); |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 90 | writeTargetTable(out); |
| 91 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 92 | |
| Rui Ueyama | 3f823e3 | 2013-11-15 22:37:34 +0000 | [diff] [blame] | 93 | if (!_addendsTableIndex.empty()) { |
| 94 | assert(out.tell() == findChunk(NCS_AddendsTable).fileOffset); |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 95 | writeAddendTable(out); |
| 96 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 97 | |
| Rui Ueyama | 3f823e3 | 2013-11-15 22:37:34 +0000 | [diff] [blame] | 98 | writeChunk(out, _contentPool, NCS_Content); |
| 99 | } |
| 100 | |
| 101 | template<class T> |
| 102 | void writeChunk(raw_ostream &out, std::vector<T> &vector, uint32_t signature) { |
| 103 | if (vector.empty()) |
| 104 | return; |
| 105 | assert(out.tell() == findChunk(signature).fileOffset); |
| 106 | out.write((char*)&vector[0], vector.size() * sizeof(T)); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 109 | void addIVarsForDefinedAtom(const DefinedAtom& atom) { |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 110 | _definedAtomIndex[&atom] = _definedAtomIvars.size(); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 111 | NativeDefinedAtomIvarsV1 ivar; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 112 | unsigned refsCount; |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 113 | ivar.nameOffset = getNameOffset(atom); |
| 114 | ivar.attributesOffset = getAttributeOffset(atom); |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 115 | ivar.referencesStartIndex = getReferencesIndex(atom, refsCount); |
| 116 | ivar.referencesCount = refsCount; |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 117 | ivar.contentOffset = getContentOffset(atom); |
| 118 | ivar.contentSize = atom.size(); |
| 119 | _definedAtomIvars.push_back(ivar); |
| 120 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 121 | |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 122 | void addIVarsForUndefinedAtom(const UndefinedAtom& atom) { |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 123 | _undefinedAtomIndex[&atom] = _undefinedAtomIvars.size(); |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 124 | NativeUndefinedAtomIvarsV1 ivar; |
| 125 | ivar.nameOffset = getNameOffset(atom); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 126 | ivar.flags = (atom.canBeNull() & 0x03); |
| Shankar Easwaran | 0879c1e | 2013-10-18 03:23:24 +0000 | [diff] [blame] | 127 | ivar.fallbackNameOffset = 0; |
| 128 | if (atom.fallback()) |
| 129 | ivar.fallbackNameOffset = getNameOffset(*atom.fallback()); |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 130 | _undefinedAtomIvars.push_back(ivar); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 131 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 132 | |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 133 | void addIVarsForSharedLibraryAtom(const SharedLibraryAtom& atom) { |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 134 | _sharedLibraryAtomIndex[&atom] = _sharedLibraryAtomIvars.size(); |
| 135 | NativeSharedLibraryAtomIvarsV1 ivar; |
| Michael J. Spencer | 4355bb9 | 2013-09-26 22:08:43 +0000 | [diff] [blame] | 136 | ivar.size = atom.size(); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 137 | ivar.nameOffset = getNameOffset(atom); |
| 138 | ivar.loadNameOffset = getSharedLibraryNameOffset(atom.loadName()); |
| Michael J. Spencer | 4355bb9 | 2013-09-26 22:08:43 +0000 | [diff] [blame] | 139 | ivar.type = (uint32_t)atom.type(); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 140 | ivar.flags = atom.canBeNullAtRuntime(); |
| 141 | _sharedLibraryAtomIvars.push_back(ivar); |
| 142 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 143 | |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 144 | void addIVarsForAbsoluteAtom(const AbsoluteAtom& atom) { |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 145 | _absoluteAtomIndex[&atom] = _absoluteAtomIvars.size(); |
| 146 | NativeAbsoluteAtomIvarsV1 ivar; |
| 147 | ivar.nameOffset = getNameOffset(atom); |
| Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 148 | ivar.attributesOffset = getAttributeOffset(atom); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 149 | ivar.reserved = 0; |
| 150 | ivar.value = atom.value(); |
| 151 | _absoluteAtomIvars.push_back(ivar); |
| 152 | } |
| 153 | |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 154 | void convertReferencesToV1() { |
| 155 | for (const NativeReferenceIvarsV2 &v2 : _referencesV2) { |
| 156 | NativeReferenceIvarsV1 v1; |
| 157 | v1.offsetInAtom = v2.offsetInAtom; |
| Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 158 | v1.kindNamespace = v2.kindNamespace; |
| 159 | v1.kindArch = v2.kindArch; |
| 160 | v1.kindValue = v2.kindValue; |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 161 | v1.targetIndex = (v2.targetIndex == NativeReferenceIvarsV2::noTarget) ? |
| 162 | NativeReferenceIvarsV1::noTarget : v2.targetIndex; |
| 163 | v1.addendIndex = this->getAddendIndex(v2.addend); |
| 164 | _referencesV1.push_back(v1); |
| 165 | } |
| 166 | _referencesV2.clear(); |
| 167 | } |
| 168 | |
| 169 | bool canConvertReferenceToV1(const NativeReferenceIvarsV2 &ref) { |
| 170 | bool validOffset = (ref.offsetInAtom == NativeReferenceIvarsV2::noTarget) || |
| 171 | ref.offsetInAtom < NativeReferenceIvarsV1::noTarget; |
| 172 | return validOffset && ref.targetIndex < UINT16_MAX; |
| 173 | } |
| 174 | |
| 175 | // Convert vector of NativeReferenceIvarsV2 to NativeReferenceIvarsV1 if |
| 176 | // possible. |
| 177 | void maybeConvertReferencesToV1() { |
| 178 | std::set<int64_t> addends; |
| 179 | for (const NativeReferenceIvarsV2 &ref : _referencesV2) { |
| 180 | if (!canConvertReferenceToV1(ref)) |
| 181 | return; |
| 182 | addends.insert(ref.addend); |
| 183 | if (addends.size() >= UINT16_MAX) |
| 184 | return; |
| 185 | } |
| 186 | convertReferencesToV1(); |
| 187 | } |
| 188 | |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 189 | // fill out native file header and chunk directory |
| 190 | void makeHeader() { |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 191 | const bool hasDefines = !_definedAtomIvars.empty(); |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 192 | const bool hasUndefines = !_undefinedAtomIvars.empty(); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 193 | const bool hasSharedLibraries = !_sharedLibraryAtomIvars.empty(); |
| 194 | const bool hasAbsolutes = !_absoluteAtomIvars.empty(); |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 195 | const bool hasReferencesV1 = !_referencesV1.empty(); |
| 196 | const bool hasReferencesV2 = !_referencesV2.empty(); |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 197 | const bool hasTargetsTable = !_targetsTableIndex.empty(); |
| 198 | const bool hasAddendTable = !_addendsTableIndex.empty(); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 199 | const bool hasContent = !_contentPool.empty(); |
| 200 | |
| 201 | int chunkCount = 1; // always have string pool chunk |
| 202 | if ( hasDefines ) chunkCount += 2; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 203 | if ( hasUndefines ) ++chunkCount; |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 204 | if ( hasSharedLibraries ) ++chunkCount; |
| Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 205 | if ( hasAbsolutes ) chunkCount += 2; |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 206 | if ( hasReferencesV1 ) ++chunkCount; |
| 207 | if ( hasReferencesV2 ) ++chunkCount; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 208 | if ( hasTargetsTable ) ++chunkCount; |
| 209 | if ( hasAddendTable ) ++chunkCount; |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 210 | if ( hasContent ) ++chunkCount; |
| 211 | |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 212 | _headerBufferSize = sizeof(NativeFileHeader) |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 213 | + chunkCount*sizeof(NativeChunk); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 214 | _headerBuffer = reinterpret_cast<NativeFileHeader*> |
| 215 | (operator new(_headerBufferSize, std::nothrow)); |
| Michael J. Spencer | b2bd733 | 2012-01-31 21:45:53 +0000 | [diff] [blame] | 216 | NativeChunk *chunks = |
| 217 | reinterpret_cast<NativeChunk*>(reinterpret_cast<char*>(_headerBuffer) |
| 218 | + sizeof(NativeFileHeader)); |
| Rui Ueyama | 0858864 | 2013-11-15 23:11:00 +0000 | [diff] [blame] | 219 | memcpy(_headerBuffer->magic, NATIVE_FILE_HEADER_MAGIC, |
| 220 | sizeof(_headerBuffer->magic)); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 221 | _headerBuffer->endian = NFH_LittleEndian; |
| 222 | _headerBuffer->architecture = 0; |
| 223 | _headerBuffer->fileSize = 0; |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 224 | _headerBuffer->chunkCount = chunkCount; |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 225 | |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 226 | // create chunk for defined atom ivar array |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 227 | int nextIndex = 0; |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 228 | uint32_t nextFileOffset = _headerBufferSize; |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 229 | if (hasDefines) { |
| 230 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, _definedAtomIvars, |
| 231 | NCS_DefinedAtomsV1); |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 232 | |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 233 | // create chunk for attributes |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 234 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, _attributes, |
| 235 | NCS_AttributesArrayV1); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 236 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 237 | |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 238 | // create chunk for undefined atom array |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 239 | if (hasUndefines) |
| 240 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, _undefinedAtomIvars, |
| 241 | NCS_UndefinedAtomsV1); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 242 | |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 243 | // create chunk for shared library atom array |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 244 | if (hasSharedLibraries) |
| 245 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, |
| 246 | _sharedLibraryAtomIvars, NCS_SharedLibraryAtomsV1); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 247 | |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 248 | // create chunk for shared library atom array |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 249 | if (hasAbsolutes) { |
| 250 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, _absoluteAtomIvars, |
| 251 | NCS_AbsoluteAtomsV1); |
| Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 252 | |
| 253 | // create chunk for attributes |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 254 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, _absAttributes, |
| 255 | NCS_AbsoluteAttributesV1); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 256 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 257 | |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 258 | // create chunk for symbol strings |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 259 | // pad end of string pool to 4-bytes |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 260 | while ((_stringPool.size() % 4) != 0) |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 261 | _stringPool.push_back('\0'); |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 262 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, _stringPool, |
| 263 | NCS_Strings); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 264 | |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 265 | // create chunk for referencesV2 |
| 266 | if (hasReferencesV1) |
| 267 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, _referencesV1, |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 268 | NCS_ReferencesArrayV1); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 269 | |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 270 | // create chunk for referencesV2 |
| 271 | if (hasReferencesV2) |
| 272 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, _referencesV2, |
| 273 | NCS_ReferencesArrayV2); |
| 274 | |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 275 | // create chunk for target table |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 276 | if (hasTargetsTable) { |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 277 | NativeChunk& cht = chunks[nextIndex++]; |
| 278 | cht.signature = NCS_TargetsTable; |
| 279 | cht.fileOffset = nextFileOffset; |
| 280 | cht.fileSize = _targetsTableIndex.size() * sizeof(uint32_t); |
| 281 | cht.elementCount = _targetsTableIndex.size(); |
| 282 | nextFileOffset = cht.fileOffset + cht.fileSize; |
| 283 | } |
| 284 | |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 285 | // create chunk for addend table |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 286 | if (hasAddendTable) { |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 287 | NativeChunk& chad = chunks[nextIndex++]; |
| 288 | chad.signature = NCS_AddendsTable; |
| 289 | chad.fileOffset = nextFileOffset; |
| 290 | chad.fileSize = _addendsTableIndex.size() * sizeof(Reference::Addend); |
| 291 | chad.elementCount = _addendsTableIndex.size(); |
| 292 | nextFileOffset = chad.fileOffset + chad.fileSize; |
| 293 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 294 | |
| 295 | // create chunk for content |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 296 | if (hasContent) |
| 297 | fillChunkHeader(chunks[nextIndex++], nextFileOffset, _contentPool, |
| 298 | NCS_Content); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 299 | |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 300 | _headerBuffer->fileSize = nextFileOffset; |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 303 | template<class T> |
| 304 | void fillChunkHeader(NativeChunk &chunk, uint32_t &nextFileOffset, |
| Rui Ueyama | 11d1f18 | 2013-11-18 04:05:28 +0000 | [diff] [blame] | 305 | const std::vector<T> &data, uint32_t signature) { |
| Rui Ueyama | 559b0aa | 2013-11-15 23:28:58 +0000 | [diff] [blame] | 306 | chunk.signature = signature; |
| 307 | chunk.fileOffset = nextFileOffset; |
| 308 | chunk.fileSize = data.size() * sizeof(T); |
| 309 | chunk.elementCount = data.size(); |
| 310 | nextFileOffset = chunk.fileOffset + chunk.fileSize; |
| 311 | } |
| 312 | |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 313 | // scan header to find particular chunk |
| 314 | NativeChunk& findChunk(uint32_t signature) { |
| 315 | const uint32_t chunkCount = _headerBuffer->chunkCount; |
| 316 | NativeChunk* chunks = |
| 317 | reinterpret_cast<NativeChunk*>(reinterpret_cast<char*>(_headerBuffer) |
| 318 | + sizeof(NativeFileHeader)); |
| 319 | for (uint32_t i=0; i < chunkCount; ++i) { |
| 320 | if ( chunks[i].signature == signature ) |
| 321 | return chunks[i]; |
| 322 | } |
| Rui Ueyama | 249becb | 2013-11-15 23:36:48 +0000 | [diff] [blame] | 323 | llvm_unreachable("findChunk() signature not found"); |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 324 | } |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 325 | |
| 326 | // append atom name to string pool and return offset |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 327 | uint32_t getNameOffset(const Atom& atom) { |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 328 | return this->getNameOffset(atom.name()); |
| 329 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 330 | |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 331 | // check if name is already in pool or append and return offset |
| Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 332 | uint32_t getSharedLibraryNameOffset(StringRef name) { |
| Rui Ueyama | 12027e5 | 2013-11-15 23:53:32 +0000 | [diff] [blame] | 333 | assert(!name.empty()); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 334 | // look to see if this library name was used by another atom |
| Rui Ueyama | 12027e5 | 2013-11-15 23:53:32 +0000 | [diff] [blame] | 335 | for (auto &it : _sharedLibraryNames) |
| 336 | if (name.equals(it.first)) |
| 337 | return it.second; |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 338 | // first use of this library name |
| 339 | uint32_t result = this->getNameOffset(name); |
| Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 340 | _sharedLibraryNames.push_back(std::make_pair(name, result)); |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 341 | return result; |
| 342 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 343 | |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 344 | // append atom name to string pool and return offset |
| Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 345 | uint32_t getNameOffset(StringRef name) { |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 346 | if ( name.empty() ) |
| 347 | return 0; |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 348 | uint32_t result = _stringPool.size(); |
| Michael J. Spencer | b5ef4df | 2012-03-09 05:27:20 +0000 | [diff] [blame] | 349 | _stringPool.insert(_stringPool.end(), name.begin(), name.end()); |
| 350 | _stringPool.push_back(0); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 351 | return result; |
| 352 | } |
| 353 | |
| 354 | // append atom cotent to content pool and return offset |
| Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 355 | uint32_t getContentOffset(const DefinedAtom& atom) { |
| Shankar Easwaran | d17ba4b | 2013-08-23 20:03:21 +0000 | [diff] [blame] | 356 | if (!atom.occupiesDiskSpace()) |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 357 | return 0; |
| 358 | uint32_t result = _contentPool.size(); |
| Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 359 | ArrayRef<uint8_t> cont = atom.rawContent(); |
| Michael J. Spencer | 846fe66 | 2012-01-31 21:46:05 +0000 | [diff] [blame] | 360 | _contentPool.insert(_contentPool.end(), cont.begin(), cont.end()); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 361 | return result; |
| 362 | } |
| 363 | |
| 364 | // reuse existing attributes entry or create a new one and return offet |
| Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 365 | uint32_t getAttributeOffset(const DefinedAtom& atom) { |
| Rui Ueyama | 4072d91 | 2013-11-16 00:55:08 +0000 | [diff] [blame] | 366 | NativeAtomAttributesV1 attrs = computeAttributesV1(atom); |
| 367 | return getOrPushAttribute(_attributes, attrs); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 368 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 369 | |
| Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 370 | uint32_t getAttributeOffset(const AbsoluteAtom& atom) { |
| Rui Ueyama | 4072d91 | 2013-11-16 00:55:08 +0000 | [diff] [blame] | 371 | NativeAtomAttributesV1 attrs = computeAbsoluteAttributes(atom); |
| 372 | return getOrPushAttribute(_absAttributes, attrs); |
| 373 | } |
| 374 | |
| 375 | uint32_t getOrPushAttribute(std::vector<NativeAtomAttributesV1> &dest, |
| 376 | const NativeAtomAttributesV1 &attrs) { |
| 377 | for (size_t i = 0, e = dest.size(); i < e; ++i) { |
| 378 | if (!memcmp(&dest[i], &attrs, sizeof(attrs))) { |
| Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 379 | // found that this set of attributes already used, so re-use |
| Rui Ueyama | 4072d91 | 2013-11-16 00:55:08 +0000 | [diff] [blame] | 380 | return i * sizeof(attrs); |
| Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | // append new attribute set to end |
| Rui Ueyama | 4072d91 | 2013-11-16 00:55:08 +0000 | [diff] [blame] | 384 | uint32_t result = dest.size() * sizeof(attrs); |
| 385 | dest.push_back(attrs); |
| Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 386 | return result; |
| 387 | } |
| 388 | |
| Rui Ueyama | 7b7b0b9 | 2013-06-21 19:59:15 +0000 | [diff] [blame] | 389 | uint32_t sectionNameOffset(const DefinedAtom& atom) { |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 390 | // if section based on content, then no custom section name available |
| Rui Ueyama | 12027e5 | 2013-11-15 23:53:32 +0000 | [diff] [blame] | 391 | if (atom.sectionChoice() == DefinedAtom::sectionBasedOnContent) |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 392 | return 0; |
| Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 393 | StringRef name = atom.customSectionName(); |
| Rui Ueyama | 12027e5 | 2013-11-15 23:53:32 +0000 | [diff] [blame] | 394 | assert(!name.empty()); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 395 | // look to see if this section name was used by another atom |
| Rui Ueyama | 12027e5 | 2013-11-15 23:53:32 +0000 | [diff] [blame] | 396 | for (auto &it : _sectionNames) |
| 397 | if (name.equals(it.first)) |
| 398 | return it.second; |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 399 | // first use of this section name |
| 400 | uint32_t result = this->getNameOffset(name); |
| Michael J. Spencer | e753cbc | 2012-03-09 05:27:43 +0000 | [diff] [blame] | 401 | _sectionNames.push_back(std::make_pair(name, result)); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 402 | return result; |
| 403 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 404 | |
| Rui Ueyama | 4072d91 | 2013-11-16 00:55:08 +0000 | [diff] [blame] | 405 | NativeAtomAttributesV1 computeAttributesV1(const DefinedAtom& atom) { |
| 406 | NativeAtomAttributesV1 attrs; |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 407 | attrs.sectionNameOffset = sectionNameOffset(atom); |
| 408 | attrs.align2 = atom.alignment().powerOf2; |
| 409 | attrs.alignModulus = atom.alignment().modulus; |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 410 | attrs.scope = atom.scope(); |
| 411 | attrs.interposable = atom.interposable(); |
| 412 | attrs.merge = atom.merge(); |
| 413 | attrs.contentType = atom.contentType(); |
| Shankar Easwaran | 8962feb | 2013-03-14 16:09:49 +0000 | [diff] [blame] | 414 | attrs.sectionChoiceAndPosition |
| Nick Kledzik | 36293f6 | 2013-01-23 22:32:56 +0000 | [diff] [blame] | 415 | = atom.sectionChoice() << 4 | atom.sectionPosition(); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 416 | attrs.deadStrip = atom.deadStrip(); |
| Michael J. Spencer | b8ab9f5 | 2013-11-08 21:04:20 +0000 | [diff] [blame] | 417 | attrs.dynamicExport = atom.dynamicExport(); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 418 | attrs.permissions = atom.permissions(); |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 419 | attrs.alias = atom.isAlias(); |
| Rui Ueyama | 4072d91 | 2013-11-16 00:55:08 +0000 | [diff] [blame] | 420 | return attrs; |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| Rui Ueyama | 4072d91 | 2013-11-16 00:55:08 +0000 | [diff] [blame] | 423 | NativeAtomAttributesV1 computeAbsoluteAttributes(const AbsoluteAtom& atom) { |
| 424 | NativeAtomAttributesV1 attrs; |
| 425 | attrs.scope = atom.scope(); |
| 426 | return attrs; |
| Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 429 | // add references for this atom in a contiguous block in NCS_ReferencesArrayV2 |
| Rui Ueyama | e4d20ab | 2013-11-16 01:01:35 +0000 | [diff] [blame] | 430 | uint32_t getReferencesIndex(const DefinedAtom& atom, unsigned& refsCount) { |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 431 | size_t startRefSize = _referencesV2.size(); |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 432 | uint32_t result = startRefSize; |
| Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 433 | for (const Reference *ref : atom) { |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 434 | NativeReferenceIvarsV2 nref; |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 435 | nref.offsetInAtom = ref->offsetInAtom(); |
| Nick Kledzik | e555277 | 2013-12-19 21:58:00 +0000 | [diff] [blame] | 436 | nref.kindNamespace = (uint8_t)ref->kindNamespace(); |
| 437 | nref.kindArch = (uint8_t)ref->kindArch(); |
| 438 | nref.kindValue = ref->kindValue(); |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 439 | nref.targetIndex = this->getTargetIndex(ref->target()); |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 440 | nref.addend = ref->addend(); |
| 441 | _referencesV2.push_back(nref); |
| Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 442 | } |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 443 | refsCount = _referencesV2.size() - startRefSize; |
| Rui Ueyama | e4d20ab | 2013-11-16 01:01:35 +0000 | [diff] [blame] | 444 | return (refsCount == 0) ? 0 : result; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 445 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 446 | |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 447 | uint32_t getTargetIndex(const Atom* target) { |
| Nick Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 448 | if ( target == nullptr ) |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 449 | return NativeReferenceIvarsV2::noTarget; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 450 | TargetToIndex::const_iterator pos = _targetsTableIndex.find(target); |
| 451 | if ( pos != _targetsTableIndex.end() ) { |
| 452 | return pos->second; |
| 453 | } |
| Nick Kledzik | abb6981 | 2012-05-31 22:34:00 +0000 | [diff] [blame] | 454 | uint32_t result = _targetsTableIndex.size(); |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 455 | _targetsTableIndex[target] = result; |
| 456 | return result; |
| 457 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 458 | |
| 459 | void writeTargetTable(raw_ostream &out) { |
| 460 | // Build table of target indexes |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 461 | uint32_t maxTargetIndex = _targetsTableIndex.size(); |
| Michael J. Spencer | 4ff3c79 | 2012-03-09 05:26:55 +0000 | [diff] [blame] | 462 | assert(maxTargetIndex > 0); |
| 463 | std::vector<uint32_t> targetIndexes(maxTargetIndex); |
| Rui Ueyama | 12027e5 | 2013-11-15 23:53:32 +0000 | [diff] [blame] | 464 | for (auto &it : _targetsTableIndex) { |
| 465 | const Atom* atom = it.first; |
| 466 | uint32_t targetIndex = it.second; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 467 | assert(targetIndex < maxTargetIndex); |
| Rui Ueyama | 5dcabbc | 2013-11-16 01:14:37 +0000 | [diff] [blame] | 468 | |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 469 | TargetToIndex::iterator pos = _definedAtomIndex.find(atom); |
| Rui Ueyama | 5dcabbc | 2013-11-16 01:14:37 +0000 | [diff] [blame] | 470 | if (pos != _definedAtomIndex.end()) { |
| 471 | targetIndexes[targetIndex] = pos->second; |
| 472 | continue; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 473 | } |
| Rui Ueyama | 5dcabbc | 2013-11-16 01:14:37 +0000 | [diff] [blame] | 474 | uint32_t base = _definedAtomIvars.size(); |
| 475 | |
| 476 | pos = _undefinedAtomIndex.find(atom); |
| 477 | if (pos != _undefinedAtomIndex.end()) { |
| 478 | targetIndexes[targetIndex] = pos->second + base; |
| 479 | continue; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 480 | } |
| Rui Ueyama | 5dcabbc | 2013-11-16 01:14:37 +0000 | [diff] [blame] | 481 | base += _undefinedAtomIndex.size(); |
| 482 | |
| 483 | pos = _sharedLibraryAtomIndex.find(atom); |
| 484 | if (pos != _sharedLibraryAtomIndex.end()) { |
| 485 | targetIndexes[targetIndex] = pos->second + base; |
| 486 | continue; |
| 487 | } |
| 488 | base += _sharedLibraryAtomIndex.size(); |
| 489 | |
| 490 | pos = _absoluteAtomIndex.find(atom); |
| 491 | assert(pos != _absoluteAtomIndex.end()); |
| 492 | targetIndexes[targetIndex] = pos->second + base; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 493 | } |
| 494 | // write table |
| Rui Ueyama | 5dcabbc | 2013-11-16 01:14:37 +0000 | [diff] [blame] | 495 | out.write((char*)&targetIndexes[0], maxTargetIndex * sizeof(uint32_t)); |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 496 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 497 | |
| 498 | uint32_t getAddendIndex(Reference::Addend addend) { |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 499 | if ( addend == 0 ) |
| 500 | return 0; // addend index zero is used to mean "no addend" |
| 501 | AddendToIndex::const_iterator pos = _addendsTableIndex.find(addend); |
| 502 | if ( pos != _addendsTableIndex.end() ) { |
| 503 | return pos->second; |
| 504 | } |
| 505 | uint32_t result = _addendsTableIndex.size() + 1; // one-based index |
| 506 | _addendsTableIndex[addend] = result; |
| 507 | return result; |
| 508 | } |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 509 | |
| Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 510 | void writeAddendTable(raw_ostream &out) { |
| Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 511 | // Build table of addends |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 512 | uint32_t maxAddendIndex = _addendsTableIndex.size(); |
| Michael J. Spencer | 4ff3c79 | 2012-03-09 05:26:55 +0000 | [diff] [blame] | 513 | std::vector<Reference::Addend> addends(maxAddendIndex); |
| Rui Ueyama | 12027e5 | 2013-11-15 23:53:32 +0000 | [diff] [blame] | 514 | for (auto &it : _addendsTableIndex) { |
| 515 | Reference::Addend addend = it.first; |
| 516 | uint32_t index = it.second; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 517 | assert(index <= maxAddendIndex); |
| 518 | addends[index-1] = addend; |
| 519 | } |
| 520 | // write table |
| 521 | out.write((char*)&addends[0], maxAddendIndex*sizeof(Reference::Addend)); |
| 522 | } |
| 523 | |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 524 | typedef std::vector<std::pair<StringRef, uint32_t>> NameToOffsetVector; |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 525 | |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 526 | typedef llvm::DenseMap<const Atom*, uint32_t> TargetToIndex; |
| 527 | typedef llvm::DenseMap<Reference::Addend, uint32_t> AddendToIndex; |
| 528 | |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 529 | NativeFileHeader* _headerBuffer; |
| 530 | size_t _headerBufferSize; |
| 531 | std::vector<char> _stringPool; |
| 532 | std::vector<uint8_t> _contentPool; |
| 533 | std::vector<NativeDefinedAtomIvarsV1> _definedAtomIvars; |
| 534 | std::vector<NativeAtomAttributesV1> _attributes; |
| Sid Manning | 2a59024 | 2012-10-18 17:16:19 +0000 | [diff] [blame] | 535 | std::vector<NativeAtomAttributesV1> _absAttributes; |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 536 | std::vector<NativeUndefinedAtomIvarsV1> _undefinedAtomIvars; |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 537 | std::vector<NativeSharedLibraryAtomIvarsV1> _sharedLibraryAtomIvars; |
| 538 | std::vector<NativeAbsoluteAtomIvarsV1> _absoluteAtomIvars; |
| Rui Ueyama | e05b629 | 2013-11-20 20:54:18 +0000 | [diff] [blame] | 539 | std::vector<NativeReferenceIvarsV1> _referencesV1; |
| 540 | std::vector<NativeReferenceIvarsV2> _referencesV2; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 541 | TargetToIndex _targetsTableIndex; |
| 542 | TargetToIndex _definedAtomIndex; |
| 543 | TargetToIndex _undefinedAtomIndex; |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 544 | TargetToIndex _sharedLibraryAtomIndex; |
| 545 | TargetToIndex _absoluteAtomIndex; |
| Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 546 | AddendToIndex _addendsTableIndex; |
| Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 547 | NameToOffsetVector _sectionNames; |
| Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 548 | NameToOffsetVector _sharedLibraryNames; |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 549 | }; |
| Michael J. Spencer | 64afcb4 | 2013-01-23 01:18:43 +0000 | [diff] [blame] | 550 | } // end namespace native |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 551 | |
| Rui Ueyama | 0ca149f | 2013-08-06 22:31:59 +0000 | [diff] [blame] | 552 | std::unique_ptr<Writer> createWriterNative(const LinkingContext &context) { |
| 553 | return std::unique_ptr<Writer>(new native::Writer(context)); |
| Nick Kledzik | 55fd6be | 2012-01-16 22:03:44 +0000 | [diff] [blame] | 554 | } |
| Michael J. Spencer | 64afcb4 | 2013-01-23 01:18:43 +0000 | [diff] [blame] | 555 | } // end namespace lld |