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