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