Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 1 | //===- Core/SymbolTable.cpp - Main Symbol Table ---------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lld/Core/SymbolTable.h" |
| 11 | #include "lld/Core/Atom.h" |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 12 | #include "lld/Core/DefinedAtom.h" |
| 13 | #include "lld/Core/UndefinedAtom.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 14 | #include "lld/Core/File.h" |
| 15 | #include "lld/Core/InputFiles.h" |
| 16 | #include "lld/Core/Resolver.h" |
| 17 | #include "lld/Core/UndefinedAtom.h" |
| 18 | #include "lld/Platform/Platform.h" |
| 19 | |
| 20 | #include "llvm/Support/ErrorHandling.h" |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/DenseMapInfo.h" |
| 22 | #include "llvm/ADT/ArrayRef.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 23 | |
| 24 | #include <algorithm> |
| 25 | #include <cassert> |
| 26 | #include <stdlib.h> |
| 27 | #include <vector> |
| 28 | |
| 29 | namespace lld { |
| 30 | |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 31 | SymbolTable::SymbolTable(Platform& plat) |
| 32 | : _platform(plat) { |
| 33 | } |
| 34 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 35 | void SymbolTable::add(const UndefinedAtom &atom) { |
| 36 | this->addByName(atom); |
| 37 | } |
| 38 | |
| 39 | void SymbolTable::add(const DefinedAtom &atom) { |
| 40 | assert(atom.scope() != DefinedAtom::scopeTranslationUnit); |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame^] | 41 | if ( !atom.name().empty() ) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 42 | this->addByName(atom); |
Nick Kledzik | f96d0ad | 2011-12-20 02:18:44 +0000 | [diff] [blame] | 43 | } |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 44 | else { |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 45 | this->addByContent(atom); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | enum NameCollisionResolution { |
| 50 | NCR_First, |
| 51 | NCR_Second, |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 52 | NCR_Dup, |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 53 | NCR_Error |
| 54 | }; |
| 55 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 56 | static NameCollisionResolution cases[4][4] = { |
| 57 | //regular absolute undef sharedLib |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 58 | { |
| 59 | // first is regular |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 60 | NCR_Dup, NCR_Error, NCR_First, NCR_First |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 61 | }, |
| 62 | { |
| 63 | // first is absolute |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 64 | NCR_Error, NCR_Error, NCR_First, NCR_First |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 65 | }, |
| 66 | { |
| 67 | // first is undef |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 68 | NCR_Second, NCR_Second, NCR_First, NCR_Second |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 69 | }, |
| 70 | { |
| 71 | // first is sharedLib |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 72 | NCR_Second, NCR_Second, NCR_First, NCR_First |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 73 | } |
| 74 | }; |
| 75 | |
| 76 | static NameCollisionResolution collide(Atom::Definition first, |
| 77 | Atom::Definition second) { |
| 78 | return cases[first][second]; |
| 79 | } |
| 80 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 81 | |
| 82 | enum MergeResolution { |
| 83 | MCR_First, |
| 84 | MCR_Second, |
| 85 | MCR_Largest, |
| 86 | MCR_Error |
| 87 | }; |
| 88 | |
| 89 | static MergeResolution mergeCases[4][4] = { |
| 90 | // no tentative weak weakAddressUsed |
| 91 | { |
| 92 | // first is no |
| 93 | MCR_Error, MCR_First, MCR_First, MCR_First |
| 94 | }, |
| 95 | { |
| 96 | // first is tentative |
| 97 | MCR_Second, MCR_Largest, MCR_Second, MCR_Second |
| 98 | }, |
| 99 | { |
| 100 | // first is weak |
| 101 | MCR_Second, MCR_First, MCR_First, MCR_Second |
| 102 | }, |
| 103 | { |
| 104 | // first is weakAddressUsed |
| 105 | MCR_Second, MCR_First, MCR_First, MCR_First |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | static MergeResolution mergeSelect(DefinedAtom::Merge first, |
| 110 | DefinedAtom::Merge second) { |
| 111 | return mergeCases[first][second]; |
| 112 | } |
| 113 | |
| 114 | |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 115 | void SymbolTable::addByName(const Atom & newAtom) { |
| 116 | llvm::StringRef name = newAtom.name(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 117 | const Atom *existing = this->findByName(name); |
| 118 | if (existing == NULL) { |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 119 | // Name is not in symbol table yet, add it associate with this atom. |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 120 | _nameTable[name] = &newAtom; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 121 | } |
| 122 | else { |
| 123 | // Name is already in symbol table and associated with another atom. |
| 124 | bool useNew = true; |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 125 | switch (collide(existing->definition(), newAtom.definition())) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 126 | case NCR_First: |
| 127 | useNew = false; |
| 128 | break; |
| 129 | case NCR_Second: |
| 130 | useNew = true; |
| 131 | break; |
| 132 | case NCR_Dup: |
| 133 | assert(existing->definition() == Atom::definitionRegular); |
| 134 | assert(newAtom.definition() == Atom::definitionRegular); |
| 135 | switch ( mergeSelect(((DefinedAtom*)existing)->merge(), |
| 136 | ((DefinedAtom*)(&newAtom))->merge()) ) { |
| 137 | case MCR_First: |
| 138 | useNew = false; |
| 139 | break; |
| 140 | case MCR_Second: |
| 141 | useNew = true; |
| 142 | break; |
| 143 | case MCR_Largest: |
| 144 | useNew = true; |
| 145 | break; |
| 146 | case MCR_Error: |
| 147 | llvm::report_fatal_error("duplicate symbol error"); |
| 148 | break; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 149 | } |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 150 | break; |
| 151 | default: |
| 152 | llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause"); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 153 | } |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame] | 154 | if ( useNew ) { |
| 155 | // Update name table to use new atom. |
| 156 | _nameTable[name] = &newAtom; |
| 157 | // Add existing atom to replacement table. |
| 158 | _replacedAtoms[existing] = &newAtom; |
| 159 | } |
| 160 | else { |
| 161 | // New atom is not being used. Add it to replacement table. |
| 162 | _replacedAtoms[&newAtom] = existing; |
| 163 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 167 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 168 | unsigned SymbolTable::MyMappingInfo::getHashValue(const DefinedAtom * const atom) { |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 169 | unsigned hash = atom->size(); |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 170 | if ( atom->contentType() != DefinedAtom::typeZeroFill ) { |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 171 | llvm::ArrayRef<uint8_t> content = atom->rawContent(); |
| 172 | for (unsigned int i=0; i < content.size(); ++i) { |
| 173 | hash = hash * 33 + content[i]; |
| 174 | } |
| 175 | } |
| 176 | hash &= 0x00FFFFFF; |
| 177 | hash |= ((unsigned)atom->contentType()) << 24; |
| 178 | //fprintf(stderr, "atom=%p, hash=0x%08X\n", atom, hash); |
| 179 | return hash; |
| 180 | } |
| 181 | |
| 182 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 183 | bool SymbolTable::MyMappingInfo::isEqual(const DefinedAtom * const l, |
| 184 | const DefinedAtom * const r) { |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 185 | if ( l == r ) |
| 186 | return true; |
| 187 | if ( l == getEmptyKey() ) |
| 188 | return false; |
| 189 | if ( r == getEmptyKey() ) |
| 190 | return false; |
| 191 | if ( l == getTombstoneKey() ) |
| 192 | return false; |
| 193 | if ( r == getTombstoneKey() ) |
| 194 | return false; |
| 195 | |
| 196 | if ( l->contentType() != r->contentType() ) |
| 197 | return false; |
| 198 | if ( l->size() != r->size() ) |
| 199 | return false; |
| 200 | llvm::ArrayRef<uint8_t> lc = l->rawContent(); |
| 201 | llvm::ArrayRef<uint8_t> rc = r->rawContent(); |
| 202 | return lc.equals(rc); |
| 203 | } |
| 204 | |
| 205 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 206 | void SymbolTable::addByContent(const DefinedAtom & newAtom) { |
Nick Kledzik | bfedfc1 | 2012-01-09 20:18:15 +0000 | [diff] [blame] | 207 | AtomContentSet::iterator pos = _contentTable.find(&newAtom); |
| 208 | if ( pos == _contentTable.end() ) { |
| 209 | _contentTable.insert(&newAtom); |
| 210 | return; |
| 211 | } |
| 212 | const Atom* existing = *pos; |
| 213 | // New atom is not being used. Add it to replacement table. |
| 214 | _replacedAtoms[&newAtom] = existing; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 219 | const Atom *SymbolTable::findByName(llvm::StringRef sym) { |
| 220 | NameToAtom::iterator pos = _nameTable.find(sym); |
| 221 | if (pos == _nameTable.end()) |
| 222 | return NULL; |
| 223 | return pos->second; |
| 224 | } |
| 225 | |
| 226 | bool SymbolTable::isDefined(llvm::StringRef sym) { |
| 227 | const Atom *atom = this->findByName(sym); |
| 228 | if (atom == NULL) |
| 229 | return false; |
| 230 | if (atom->definition() == Atom::definitionUndefined) |
| 231 | return false; |
| 232 | return true; |
| 233 | } |
| 234 | |
| 235 | const Atom *SymbolTable::replacement(const Atom *atom) { |
| 236 | AtomToAtom::iterator pos = _replacedAtoms.find(atom); |
| 237 | if (pos == _replacedAtoms.end()) |
| 238 | return atom; |
| 239 | // might be chain, recurse to end |
| 240 | return this->replacement(pos->second); |
| 241 | } |
| 242 | |
| 243 | unsigned int SymbolTable::size() { |
| 244 | return _nameTable.size(); |
| 245 | } |
| 246 | |
| 247 | void SymbolTable::undefines(std::vector<const Atom *> &undefs) { |
| 248 | for (NameToAtom::iterator it = _nameTable.begin(), |
| 249 | end = _nameTable.end(); it != end; ++it) { |
| 250 | const Atom *atom = it->second; |
| 251 | assert(atom != NULL); |
| 252 | if (atom->definition() == Atom::definitionUndefined) |
| 253 | undefs.push_back(atom); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | } // namespace lld |