blob: 0bfe6da6a780c1e010823448bd583ec5630bd5bd [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 Kledzik6bc04c62012-02-22 21:56:59 +000012#include "lld/Core/AbsoluteAtom.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000013#include "lld/Core/DefinedAtom.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000014#include "lld/Core/File.h"
15#include "lld/Core/InputFiles.h"
Michael J. Spencere6203a52012-04-03 18:39:40 +000016#include "lld/Core/LLVM.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000017#include "lld/Core/Resolver.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000018#include "lld/Core/SharedLibraryAtom.h"
19#include "lld/Core/UndefinedAtom.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000020
Nick Kledzikbfedfc12012-01-09 20:18:15 +000021#include "llvm/ADT/ArrayRef.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000022#include "llvm/ADT/DenseMapInfo.h"
23#include "llvm/Support/ErrorHandling.h"
Nick Kledzikbb963df2012-04-18 21:55:06 +000024#include "llvm/Support/raw_ostream.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000025
26#include <algorithm>
27#include <cassert>
Michael J. Spencercfd029f2012-03-28 19:04:02 +000028#include <cstdlib>
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000029#include <vector>
30
31namespace lld {
32
Nick Kledzikbb963df2012-04-18 21:55:06 +000033SymbolTable::SymbolTable(ResolverOptions &opts)
34 : _options(opts) {
Nick Kledzik38eec3d2011-12-22 02:38:01 +000035}
36
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000037void SymbolTable::add(const UndefinedAtom &atom) {
38 this->addByName(atom);
39}
40
Nick Kledzik6bc04c62012-02-22 21:56:59 +000041void SymbolTable::add(const SharedLibraryAtom &atom) {
42 this->addByName(atom);
43}
Michael J. Spencer765792d2012-04-03 18:40:27 +000044
Nick Kledzik6bc04c62012-02-22 21:56:59 +000045void SymbolTable::add(const AbsoluteAtom &atom) {
46 this->addByName(atom);
47}
48
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000049void SymbolTable::add(const DefinedAtom &atom) {
50 assert(atom.scope() != DefinedAtom::scopeTranslationUnit);
Nick Kledzik49d6cc82012-02-15 00:38:09 +000051 if ( !atom.name().empty() ) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000052 this->addByName(atom);
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000053 }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000054 else {
Nick Kledzikbfedfc12012-01-09 20:18:15 +000055 this->addByContent(atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000056 }
57}
58
59enum NameCollisionResolution {
60 NCR_First,
61 NCR_Second,
Nick Kledzik6bc04c62012-02-22 21:56:59 +000062 NCR_DupDef,
63 NCR_DupUndef,
64 NCR_DupShLib,
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000065 NCR_Error
66};
67
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000068static NameCollisionResolution cases[4][4] = {
69 //regular absolute undef sharedLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000070 {
71 // first is regular
Nick Kledzik6bc04c62012-02-22 21:56:59 +000072 NCR_DupDef, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000073 },
74 {
75 // first is absolute
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000076 NCR_Error, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000077 },
78 {
79 // first is undef
Nick Kledzik6bc04c62012-02-22 21:56:59 +000080 NCR_Second, NCR_Second, NCR_DupUndef, NCR_Second
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000081 },
82 {
83 // first is sharedLib
Nick Kledzik6bc04c62012-02-22 21:56:59 +000084 NCR_Second, NCR_Second, NCR_First, NCR_DupShLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000085 }
86};
87
88static NameCollisionResolution collide(Atom::Definition first,
89 Atom::Definition second) {
90 return cases[first][second];
91}
92
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000093
94enum MergeResolution {
95 MCR_First,
96 MCR_Second,
97 MCR_Largest,
98 MCR_Error
99};
100
101static MergeResolution mergeCases[4][4] = {
102 // no tentative weak weakAddressUsed
103 {
104 // first is no
105 MCR_Error, MCR_First, MCR_First, MCR_First
106 },
107 {
108 // first is tentative
109 MCR_Second, MCR_Largest, MCR_Second, MCR_Second
110 },
111 {
112 // first is weak
113 MCR_Second, MCR_First, MCR_First, MCR_Second
114 },
115 {
116 // first is weakAddressUsed
117 MCR_Second, MCR_First, MCR_First, MCR_First
118 }
119};
120
Michael J. Spencer765792d2012-04-03 18:40:27 +0000121static MergeResolution mergeSelect(DefinedAtom::Merge first,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000122 DefinedAtom::Merge second) {
123 return mergeCases[first][second];
124}
125
126
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000127void SymbolTable::addByName(const Atom & newAtom) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000128 StringRef name = newAtom.name();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000129 const Atom *existing = this->findByName(name);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000130 if (existing == nullptr) {
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000131 // Name is not in symbol table yet, add it associate with this atom.
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000132 _nameTable[name] = &newAtom;
Michael J. Spencer765792d2012-04-03 18:40:27 +0000133 }
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000134 else {
135 // Name is already in symbol table and associated with another atom.
136 bool useNew = true;
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000137 switch (collide(existing->definition(), newAtom.definition())) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000138 case NCR_First:
139 useNew = false;
140 break;
141 case NCR_Second:
142 useNew = true;
143 break;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000144 case NCR_DupDef:
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000145 assert(existing->definition() == Atom::definitionRegular);
146 assert(newAtom.definition() == Atom::definitionRegular);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000147 switch ( mergeSelect(((DefinedAtom*)existing)->merge(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000148 ((DefinedAtom*)(&newAtom))->merge()) ) {
149 case MCR_First:
150 useNew = false;
151 break;
152 case MCR_Second:
153 useNew = true;
154 break;
155 case MCR_Largest:
156 useNew = true;
157 break;
158 case MCR_Error:
159 llvm::report_fatal_error("duplicate symbol error");
160 break;
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000161 }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000162 break;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000163 case NCR_DupUndef: {
Michael J. Spencerb4955622012-04-02 23:56:36 +0000164 const UndefinedAtom* existingUndef =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000165 dyn_cast<UndefinedAtom>(existing);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000166 const UndefinedAtom* newUndef =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000167 dyn_cast<UndefinedAtom>(&newAtom);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000168 assert(existingUndef != nullptr);
169 assert(newUndef != nullptr);
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000170 if ( existingUndef->canBeNull() == newUndef->canBeNull() ) {
171 useNew = false;
172 }
173 else {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000174 if ( _options.warnIfCoalesableAtomsHaveDifferentCanBeNull() ) {
175 // FIXME: need diagonstics interface for writing warning messages
176 llvm::errs() << "lld warning: undefined symbol "
177 << existingUndef->name()
178 << " has different weakness in "
179 << existingUndef->file().path()
180 << " and in "
181 << newUndef->file().path();
182 }
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000183 useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000184 }
185 }
186 break;
187 case NCR_DupShLib: {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000188 const SharedLibraryAtom* curShLib =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000189 dyn_cast<SharedLibraryAtom>(existing);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000190 const SharedLibraryAtom* newShLib =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000191 dyn_cast<SharedLibraryAtom>(&newAtom);
Nick Kledzikbb963df2012-04-18 21:55:06 +0000192 assert(curShLib != nullptr);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000193 assert(newShLib != nullptr);
Nick Kledzikbb963df2012-04-18 21:55:06 +0000194 bool sameNullness = (curShLib->canBeNullAtRuntime()
195 == newShLib->canBeNullAtRuntime());
196 bool sameName = curShLib->loadName().equals(newShLib->loadName());
197 if ( !sameName ) {
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000198 useNew = false;
Nick Kledzikbb963df2012-04-18 21:55:06 +0000199 if ( _options.warnIfCoalesableAtomsHaveDifferentLoadName() ) {
200 // FIXME: need diagonstics interface for writing warning messages
201 llvm::errs() << "lld warning: shared library symbol "
202 << curShLib->name()
203 << " has different load path in "
204 << curShLib->file().path()
205 << " and in "
206 << newShLib->file().path();
207 }
208 }
209 else if ( ! sameNullness ) {
210 useNew = false;
211 if ( _options.warnIfCoalesableAtomsHaveDifferentCanBeNull() ) {
212 // FIXME: need diagonstics interface for writing warning messages
213 llvm::errs() << "lld warning: shared library symbol "
214 << curShLib->name()
215 << " has different weakness in "
216 << curShLib->file().path()
217 << " and in "
218 << newShLib->file().path();
219 }
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000220 }
221 else {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000222 // Both shlib atoms are identical and can be coalesced.
223 useNew = false;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000224 }
225 }
226 break;
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000227 default:
228 llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000229 }
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000230 if ( useNew ) {
231 // Update name table to use new atom.
232 _nameTable[name] = &newAtom;
233 // Add existing atom to replacement table.
234 _replacedAtoms[existing] = &newAtom;
235 }
236 else {
237 // New atom is not being used. Add it to replacement table.
238 _replacedAtoms[&newAtom] = existing;
239 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000240 }
241}
242
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000243
Nick Kledzikaf18a2b2012-02-15 00:50:07 +0000244unsigned SymbolTable::AtomMappingInfo::getHashValue(const DefinedAtom * const atom) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000245 unsigned hash = atom->size();
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000246 if ( atom->contentType() != DefinedAtom::typeZeroFill ) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000247 ArrayRef<uint8_t> content = atom->rawContent();
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000248 for (unsigned int i=0; i < content.size(); ++i) {
249 hash = hash * 33 + content[i];
250 }
251 }
252 hash &= 0x00FFFFFF;
253 hash |= ((unsigned)atom->contentType()) << 24;
254 //fprintf(stderr, "atom=%p, hash=0x%08X\n", atom, hash);
255 return hash;
256}
257
258
Michael J. Spencer765792d2012-04-03 18:40:27 +0000259bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000260 const DefinedAtom * const r) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000261 if ( l == r )
262 return true;
263 if ( l == getEmptyKey() )
264 return false;
265 if ( r == getEmptyKey() )
266 return false;
267 if ( l == getTombstoneKey() )
268 return false;
269 if ( r == getTombstoneKey() )
270 return false;
Michael J. Spencere6203a52012-04-03 18:39:40 +0000271
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000272 if ( l->contentType() != r->contentType() )
273 return false;
274 if ( l->size() != r->size() )
275 return false;
Michael J. Spencere6203a52012-04-03 18:39:40 +0000276 ArrayRef<uint8_t> lc = l->rawContent();
277 ArrayRef<uint8_t> rc = r->rawContent();
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000278 return lc.equals(rc);
279}
280
281
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000282void SymbolTable::addByContent(const DefinedAtom & newAtom) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000283 AtomContentSet::iterator pos = _contentTable.find(&newAtom);
284 if ( pos == _contentTable.end() ) {
285 _contentTable.insert(&newAtom);
286 return;
287 }
288 const Atom* existing = *pos;
289 // New atom is not being used. Add it to replacement table.
290 _replacedAtoms[&newAtom] = existing;
291}
292
293
294
Michael J. Spencere6203a52012-04-03 18:39:40 +0000295const Atom *SymbolTable::findByName(StringRef sym) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000296 NameToAtom::iterator pos = _nameTable.find(sym);
297 if (pos == _nameTable.end())
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000298 return nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000299 return pos->second;
300}
301
Michael J. Spencere6203a52012-04-03 18:39:40 +0000302bool SymbolTable::isDefined(StringRef sym) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000303 const Atom *atom = this->findByName(sym);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000304 if (atom == nullptr)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000305 return false;
306 if (atom->definition() == Atom::definitionUndefined)
307 return false;
308 return true;
309}
310
311const Atom *SymbolTable::replacement(const Atom *atom) {
312 AtomToAtom::iterator pos = _replacedAtoms.find(atom);
313 if (pos == _replacedAtoms.end())
314 return atom;
315 // might be chain, recurse to end
316 return this->replacement(pos->second);
317}
318
319unsigned int SymbolTable::size() {
320 return _nameTable.size();
321}
322
323void SymbolTable::undefines(std::vector<const Atom *> &undefs) {
324 for (NameToAtom::iterator it = _nameTable.begin(),
325 end = _nameTable.end(); it != end; ++it) {
326 const Atom *atom = it->second;
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000327 assert(atom != nullptr);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000328 if (atom->definition() == Atom::definitionUndefined)
329 undefs.push_back(atom);
330 }
331}
332
Nick Kledzik20e652d2012-04-20 01:24:37 +0000333void SymbolTable::tentativeDefinitions(std::vector<StringRef> &names) {
334 for (auto entry : _nameTable) {
335 const Atom *atom = entry.second;
336 StringRef name = entry.first;
337 assert(atom != nullptr);
338 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom) ) {
339 if ( defAtom->merge() == DefinedAtom::mergeAsTentative )
340 names.push_back(name);
341 }
342 }
343}
344
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000345} // namespace lld