blob: 223f15e6cdaf23c3b2e05ddaea76f3178be858cb [file] [log] [blame]
Michael J. Spencer773a8fb2011-12-18 08:27:59 +00001//===- 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
25namespace lld {
26
Nick Kledzik38eec3d2011-12-22 02:38:01 +000027SymbolTable::SymbolTable(Platform& plat)
28 : _platform(plat) {
29}
30
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000031void SymbolTable::add(const Atom &atom) {
32 assert(atom.scope() != Atom::scopeTranslationUnit);
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000033 if ( !atom.internalName() ) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000034 this->addByName(atom);
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000035 }
36 else if ( atom.mergeDuplicates() ) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000037 // TO DO: support constants merging
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000038 }
39}
40
41enum NameCollisionResolution {
42 NCR_First,
43 NCR_Second,
44 NCR_Weak,
45 NCR_Larger,
Nick Kledzik38eec3d2011-12-22 02:38:01 +000046 NCR_Dup,
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000047 NCR_Error
48};
49
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000050static NameCollisionResolution cases[6][6] = {
51 //regular weak tentative absolute undef sharedLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000052 {
53 // first is regular
Nick Kledzik38eec3d2011-12-22 02:38:01 +000054 NCR_Dup, NCR_First, NCR_First, NCR_Error, NCR_First, NCR_First
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000055 },
56 {
57 // first is weak
58 NCR_Second, NCR_Weak, NCR_Larger, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000059 },
60 {
61 // first is tentative
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000062 NCR_Second, NCR_Second, NCR_Larger, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000063 },
64 {
65 // first is absolute
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000066 NCR_Error, NCR_Error, NCR_Error, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000067 },
68 {
69 // first is undef
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000070 NCR_Second, NCR_Second, NCR_Second, NCR_Second, NCR_First, NCR_Second
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000071 },
72 {
73 // first is sharedLib
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000074 NCR_Second, NCR_Second, NCR_Second, NCR_Second, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000075 }
76};
77
78static NameCollisionResolution collide(Atom::Definition first,
79 Atom::Definition second) {
80 return cases[first][second];
81}
82
Nick Kledzik38eec3d2011-12-22 02:38:01 +000083void SymbolTable::addByName(const Atom & newAtom) {
84 llvm::StringRef name = newAtom.name();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000085 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 Kledzik38eec3d2011-12-22 02:38:01 +000088 _nameTable[name] = &newAtom;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000089 } else {
90 // name is already in symbol table and associated with another atom
Nick Kledzik38eec3d2011-12-22 02:38:01 +000091 switch (collide(existing->definition(), newAtom.definition())) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000092 case NCR_First:
93 // using first, just add new to _replacedAtoms
Nick Kledzik38eec3d2011-12-22 02:38:01 +000094 _replacedAtoms[&newAtom] = existing;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000095 break;
96 case NCR_Second:
97 // using second, update tables
Nick Kledzik38eec3d2011-12-22 02:38:01 +000098 _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. Spencer773a8fb2011-12-18 08:27:59 +0000118 break;
119 default:
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000120 llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000121 }
122 }
123}
124
125const 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
132bool 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
141const 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
149unsigned int SymbolTable::size() {
150 return _nameTable.size();
151}
152
153void 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