blob: 1b940cc88ef562fd744aaf0cd65d63fe921a104a [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"
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000012#include "lld/Core/DefinedAtom.h"
13#include "lld/Core/UndefinedAtom.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000014#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 Kledzikbfedfc12012-01-09 20:18:15 +000021#include "llvm/ADT/DenseMapInfo.h"
22#include "llvm/ADT/ArrayRef.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000023
24#include <algorithm>
25#include <cassert>
26#include <stdlib.h>
27#include <vector>
28
29namespace lld {
30
Nick Kledzik38eec3d2011-12-22 02:38:01 +000031SymbolTable::SymbolTable(Platform& plat)
32 : _platform(plat) {
33}
34
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000035void SymbolTable::add(const UndefinedAtom &atom) {
36 this->addByName(atom);
37}
38
39void SymbolTable::add(const DefinedAtom &atom) {
40 assert(atom.scope() != DefinedAtom::scopeTranslationUnit);
Nick Kledzik49d6cc82012-02-15 00:38:09 +000041 if ( !atom.name().empty() ) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000042 this->addByName(atom);
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000043 }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000044 else {
Nick Kledzikbfedfc12012-01-09 20:18:15 +000045 this->addByContent(atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000046 }
47}
48
49enum NameCollisionResolution {
50 NCR_First,
51 NCR_Second,
Nick Kledzik38eec3d2011-12-22 02:38:01 +000052 NCR_Dup,
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000053 NCR_Error
54};
55
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000056static NameCollisionResolution cases[4][4] = {
57 //regular absolute undef sharedLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000058 {
59 // first is regular
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000060 NCR_Dup, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000061 },
62 {
63 // first is absolute
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000064 NCR_Error, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000065 },
66 {
67 // first is undef
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000068 NCR_Second, NCR_Second, NCR_First, NCR_Second
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000069 },
70 {
71 // first is sharedLib
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000072 NCR_Second, NCR_Second, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000073 }
74};
75
76static NameCollisionResolution collide(Atom::Definition first,
77 Atom::Definition second) {
78 return cases[first][second];
79}
80
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000081
82enum MergeResolution {
83 MCR_First,
84 MCR_Second,
85 MCR_Largest,
86 MCR_Error
87};
88
89static 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
109static MergeResolution mergeSelect(DefinedAtom::Merge first,
110 DefinedAtom::Merge second) {
111 return mergeCases[first][second];
112}
113
114
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000115void SymbolTable::addByName(const Atom & newAtom) {
116 llvm::StringRef name = newAtom.name();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000117 const Atom *existing = this->findByName(name);
118 if (existing == NULL) {
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000119 // Name is not in symbol table yet, add it associate with this atom.
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000120 _nameTable[name] = &newAtom;
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000121 }
122 else {
123 // Name is already in symbol table and associated with another atom.
124 bool useNew = true;
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000125 switch (collide(existing->definition(), newAtom.definition())) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000126 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 Kledzik7735a7d2012-01-04 23:58:17 +0000149 }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000150 break;
151 default:
152 llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000153 }
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000154 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. Spencer773a8fb2011-12-18 08:27:59 +0000164 }
165}
166
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000167
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000168unsigned SymbolTable::MyMappingInfo::getHashValue(const DefinedAtom * const atom) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000169 unsigned hash = atom->size();
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000170 if ( atom->contentType() != DefinedAtom::typeZeroFill ) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000171 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 Kledzikf4fb2c52012-01-11 01:06:19 +0000183bool SymbolTable::MyMappingInfo::isEqual(const DefinedAtom * const l,
184 const DefinedAtom * const r) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000185 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 Kledzikf4fb2c52012-01-11 01:06:19 +0000206void SymbolTable::addByContent(const DefinedAtom & newAtom) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000207 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. Spencer773a8fb2011-12-18 08:27:59 +0000219const 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
226bool 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
235const 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
243unsigned int SymbolTable::size() {
244 return _nameTable.size();
245}
246
247void 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