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" |
| 12 | #include "lld/Core/File.h" |
| 13 | #include "lld/Core/InputFiles.h" |
| 14 | #include "lld/Core/Resolver.h" |
| 15 | #include "lld/Core/UndefinedAtom.h" |
| 16 | #include "lld/Platform/Platform.h" |
| 17 | |
| 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cassert> |
| 22 | #include <stdlib.h> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace lld { |
| 26 | |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 27 | SymbolTable::SymbolTable(Platform& plat) |
| 28 | : _platform(plat) { |
| 29 | } |
| 30 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 31 | void SymbolTable::add(const Atom &atom) { |
| 32 | assert(atom.scope() != Atom::scopeTranslationUnit); |
Nick Kledzik | f96d0ad | 2011-12-20 02:18:44 +0000 | [diff] [blame] | 33 | if ( !atom.internalName() ) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 34 | this->addByName(atom); |
Nick Kledzik | f96d0ad | 2011-12-20 02:18:44 +0000 | [diff] [blame] | 35 | } |
| 36 | else if ( atom.mergeDuplicates() ) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 37 | // TO DO: support constants merging |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | enum NameCollisionResolution { |
| 42 | NCR_First, |
| 43 | NCR_Second, |
| 44 | NCR_Weak, |
| 45 | NCR_Larger, |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 46 | NCR_Dup, |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 47 | NCR_Error |
| 48 | }; |
| 49 | |
Nick Kledzik | f96d0ad | 2011-12-20 02:18:44 +0000 | [diff] [blame] | 50 | static NameCollisionResolution cases[6][6] = { |
| 51 | //regular weak tentative absolute undef sharedLib |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 52 | { |
| 53 | // first is regular |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 54 | NCR_Dup, NCR_First, NCR_First, NCR_Error, NCR_First, NCR_First |
Nick Kledzik | f96d0ad | 2011-12-20 02:18:44 +0000 | [diff] [blame] | 55 | }, |
| 56 | { |
| 57 | // first is weak |
| 58 | NCR_Second, NCR_Weak, NCR_Larger, NCR_Error, NCR_First, NCR_First |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 59 | }, |
| 60 | { |
| 61 | // first is tentative |
Nick Kledzik | f96d0ad | 2011-12-20 02:18:44 +0000 | [diff] [blame] | 62 | NCR_Second, NCR_Second, NCR_Larger, NCR_Error, NCR_First, NCR_First |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 63 | }, |
| 64 | { |
| 65 | // first is absolute |
Nick Kledzik | f96d0ad | 2011-12-20 02:18:44 +0000 | [diff] [blame] | 66 | NCR_Error, NCR_Error, NCR_Error, NCR_Error, NCR_First, NCR_First |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 67 | }, |
| 68 | { |
| 69 | // first is undef |
Nick Kledzik | f96d0ad | 2011-12-20 02:18:44 +0000 | [diff] [blame] | 70 | NCR_Second, NCR_Second, NCR_Second, NCR_Second, NCR_First, NCR_Second |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 71 | }, |
| 72 | { |
| 73 | // first is sharedLib |
Nick Kledzik | f96d0ad | 2011-12-20 02:18:44 +0000 | [diff] [blame] | 74 | NCR_Second, NCR_Second, NCR_Second, NCR_Second, NCR_First, NCR_First |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 75 | } |
| 76 | }; |
| 77 | |
| 78 | static NameCollisionResolution collide(Atom::Definition first, |
| 79 | Atom::Definition second) { |
| 80 | return cases[first][second]; |
| 81 | } |
| 82 | |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 83 | void SymbolTable::addByName(const Atom & newAtom) { |
| 84 | llvm::StringRef name = newAtom.name(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 85 | const Atom *existing = this->findByName(name); |
| 86 | if (existing == NULL) { |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 87 | // 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] | 88 | _nameTable[name] = &newAtom; |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 89 | } |
| 90 | else { |
| 91 | // Name is already in symbol table and associated with another atom. |
| 92 | bool useNew = true; |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 93 | switch (collide(existing->definition(), newAtom.definition())) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 94 | case NCR_First: |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 95 | useNew = false; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 96 | break; |
| 97 | case NCR_Second: |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 98 | useNew = true; |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 99 | break; |
| 100 | case NCR_Dup: |
| 101 | if ( existing->mergeDuplicates() && newAtom.mergeDuplicates() ) { |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 102 | // Both mergeable. Use auto-hide bit as tie breaker |
| 103 | if ( existing->autoHide() != newAtom.autoHide() ) { |
| 104 | // They have different autoHide values, keep non-autohide one |
| 105 | useNew = existing->autoHide(); |
| 106 | } |
| 107 | else { |
| 108 | // They have same autoHide, so just keep using existing |
| 109 | useNew = false; |
| 110 | } |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 111 | } |
| 112 | else { |
| 113 | const Atom& use = _platform.handleMultipleDefinitions(*existing, newAtom); |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 114 | useNew = ( &use != existing ); |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 115 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 116 | break; |
| 117 | default: |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame] | 118 | llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause"); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 119 | } |
Nick Kledzik | 7735a7d | 2012-01-04 23:58:17 +0000 | [diff] [blame^] | 120 | if ( useNew ) { |
| 121 | // Update name table to use new atom. |
| 122 | _nameTable[name] = &newAtom; |
| 123 | // Add existing atom to replacement table. |
| 124 | _replacedAtoms[existing] = &newAtom; |
| 125 | } |
| 126 | else { |
| 127 | // New atom is not being used. Add it to replacement table. |
| 128 | _replacedAtoms[&newAtom] = existing; |
| 129 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | const Atom *SymbolTable::findByName(llvm::StringRef sym) { |
| 134 | NameToAtom::iterator pos = _nameTable.find(sym); |
| 135 | if (pos == _nameTable.end()) |
| 136 | return NULL; |
| 137 | return pos->second; |
| 138 | } |
| 139 | |
| 140 | bool SymbolTable::isDefined(llvm::StringRef sym) { |
| 141 | const Atom *atom = this->findByName(sym); |
| 142 | if (atom == NULL) |
| 143 | return false; |
| 144 | if (atom->definition() == Atom::definitionUndefined) |
| 145 | return false; |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | const Atom *SymbolTable::replacement(const Atom *atom) { |
| 150 | AtomToAtom::iterator pos = _replacedAtoms.find(atom); |
| 151 | if (pos == _replacedAtoms.end()) |
| 152 | return atom; |
| 153 | // might be chain, recurse to end |
| 154 | return this->replacement(pos->second); |
| 155 | } |
| 156 | |
| 157 | unsigned int SymbolTable::size() { |
| 158 | return _nameTable.size(); |
| 159 | } |
| 160 | |
| 161 | void SymbolTable::undefines(std::vector<const Atom *> &undefs) { |
| 162 | for (NameToAtom::iterator it = _nameTable.begin(), |
| 163 | end = _nameTable.end(); it != end; ++it) { |
| 164 | const Atom *atom = it->second; |
| 165 | assert(atom != NULL); |
| 166 | if (atom->definition() == Atom::definitionUndefined) |
| 167 | undefs.push_back(atom); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | } // namespace lld |