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) { |
| 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; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 89 | } else { |
| 90 | // name is already in symbol table and associated with another atom |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame^] | 91 | switch (collide(existing->definition(), newAtom.definition())) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 92 | case NCR_First: |
| 93 | // using first, just add new to _replacedAtoms |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame^] | 94 | _replacedAtoms[&newAtom] = existing; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 95 | break; |
| 96 | case NCR_Second: |
| 97 | // using second, update tables |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame^] | 98 | _nameTable[name] = &newAtom; |
| 99 | _replacedAtoms[existing] = &newAtom; |
| 100 | break; |
| 101 | case NCR_Dup: |
| 102 | if ( existing->mergeDuplicates() && newAtom.mergeDuplicates() ) { |
| 103 | // using existing atom, add new atom to _replacedAtoms |
| 104 | _replacedAtoms[&newAtom] = existing; |
| 105 | } |
| 106 | else { |
| 107 | const Atom& use = _platform.handleMultipleDefinitions(*existing, newAtom); |
| 108 | if ( &use == existing ) { |
| 109 | // using existing atom, add new atom to _replacedAtoms |
| 110 | _replacedAtoms[&newAtom] = existing; |
| 111 | } |
| 112 | else { |
| 113 | // using new atom, update tables |
| 114 | _nameTable[name] = &newAtom; |
| 115 | _replacedAtoms[existing] = &newAtom; |
| 116 | } |
| 117 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 118 | break; |
| 119 | default: |
Nick Kledzik | 38eec3d | 2011-12-22 02:38:01 +0000 | [diff] [blame^] | 120 | llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause"); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | const Atom *SymbolTable::findByName(llvm::StringRef sym) { |
| 126 | NameToAtom::iterator pos = _nameTable.find(sym); |
| 127 | if (pos == _nameTable.end()) |
| 128 | return NULL; |
| 129 | return pos->second; |
| 130 | } |
| 131 | |
| 132 | bool SymbolTable::isDefined(llvm::StringRef sym) { |
| 133 | const Atom *atom = this->findByName(sym); |
| 134 | if (atom == NULL) |
| 135 | return false; |
| 136 | if (atom->definition() == Atom::definitionUndefined) |
| 137 | return false; |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | const Atom *SymbolTable::replacement(const Atom *atom) { |
| 142 | AtomToAtom::iterator pos = _replacedAtoms.find(atom); |
| 143 | if (pos == _replacedAtoms.end()) |
| 144 | return atom; |
| 145 | // might be chain, recurse to end |
| 146 | return this->replacement(pos->second); |
| 147 | } |
| 148 | |
| 149 | unsigned int SymbolTable::size() { |
| 150 | return _nameTable.size(); |
| 151 | } |
| 152 | |
| 153 | void SymbolTable::undefines(std::vector<const Atom *> &undefs) { |
| 154 | for (NameToAtom::iterator it = _nameTable.begin(), |
| 155 | end = _nameTable.end(); it != end; ++it) { |
| 156 | const Atom *atom = it->second; |
| 157 | assert(atom != NULL); |
| 158 | if (atom->definition() == Atom::definitionUndefined) |
| 159 | undefs.push_back(atom); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | } // namespace lld |