blob: 1c7ea99ff0517dd67e3291e84b8debe53eed4b50 [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 {
Nick Kledzikbb963df2012-04-18 21:55:06 +000032SymbolTable::SymbolTable(ResolverOptions &opts)
33 : _options(opts) {
Nick Kledzik38eec3d2011-12-22 02:38:01 +000034}
35
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000036void SymbolTable::add(const UndefinedAtom &atom) {
37 this->addByName(atom);
38}
39
Nick Kledzik6bc04c62012-02-22 21:56:59 +000040void SymbolTable::add(const SharedLibraryAtom &atom) {
41 this->addByName(atom);
42}
Michael J. Spencer765792d2012-04-03 18:40:27 +000043
Nick Kledzik6bc04c62012-02-22 21:56:59 +000044void SymbolTable::add(const AbsoluteAtom &atom) {
45 this->addByName(atom);
46}
47
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000048void SymbolTable::add(const DefinedAtom &atom) {
Nick Kledzik233f5372013-01-15 00:17:57 +000049 if (!atom.name().empty() &&
50 (atom.scope() != DefinedAtom::scopeTranslationUnit)) {
51 // Named atoms cannot be merged by content.
52 assert(atom.merge() != DefinedAtom::mergeByContent);
53 // Track named atoms that are not scoped to file (static).
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000054 this->addByName(atom);
Nick Kledzikf96d0ad2011-12-20 02:18:44 +000055 }
Nick Kledzik233f5372013-01-15 00:17:57 +000056 else if ( atom.merge() == DefinedAtom::mergeByContent ) {
57 // Named atoms cannot be merged by content.
58 assert(atom.name().empty());
Nick Kledzikbfedfc12012-01-09 20:18:15 +000059 this->addByContent(atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000060 }
61}
62
63enum NameCollisionResolution {
64 NCR_First,
65 NCR_Second,
Nick Kledzik6bc04c62012-02-22 21:56:59 +000066 NCR_DupDef,
67 NCR_DupUndef,
68 NCR_DupShLib,
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000069 NCR_Error
70};
71
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000072static NameCollisionResolution cases[4][4] = {
73 //regular absolute undef sharedLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000074 {
75 // first is regular
Nick Kledzik6bc04c62012-02-22 21:56:59 +000076 NCR_DupDef, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000077 },
78 {
79 // first is absolute
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000080 NCR_Error, NCR_Error, NCR_First, NCR_First
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000081 },
82 {
83 // first is undef
Nick Kledzik6bc04c62012-02-22 21:56:59 +000084 NCR_Second, NCR_Second, NCR_DupUndef, NCR_Second
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000085 },
86 {
87 // first is sharedLib
Nick Kledzik6bc04c62012-02-22 21:56:59 +000088 NCR_Second, NCR_Second, NCR_First, NCR_DupShLib
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000089 }
90};
91
92static NameCollisionResolution collide(Atom::Definition first,
93 Atom::Definition second) {
94 return cases[first][second];
95}
96
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000097enum MergeResolution {
98 MCR_First,
99 MCR_Second,
100 MCR_Largest,
101 MCR_Error
102};
103
104static MergeResolution mergeCases[4][4] = {
105 // no tentative weak weakAddressUsed
106 {
107 // first is no
108 MCR_Error, MCR_First, MCR_First, MCR_First
109 },
110 {
111 // first is tentative
112 MCR_Second, MCR_Largest, MCR_Second, MCR_Second
113 },
114 {
115 // first is weak
116 MCR_Second, MCR_First, MCR_First, MCR_Second
117 },
118 {
119 // first is weakAddressUsed
120 MCR_Second, MCR_First, MCR_First, MCR_First
121 }
122};
123
Michael J. Spencer765792d2012-04-03 18:40:27 +0000124static MergeResolution mergeSelect(DefinedAtom::Merge first,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000125 DefinedAtom::Merge second) {
126 return mergeCases[first][second];
127}
128
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000129void SymbolTable::addByName(const Atom & newAtom) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000130 StringRef name = newAtom.name();
Nick Kledzik233f5372013-01-15 00:17:57 +0000131 assert(!name.empty());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000132 const Atom *existing = this->findByName(name);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000133 if (existing == nullptr) {
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000134 // Name is not in symbol table yet, add it associate with this atom.
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000135 _nameTable[name] = &newAtom;
Michael J. Spencer765792d2012-04-03 18:40:27 +0000136 }
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000137 else {
138 // Name is already in symbol table and associated with another atom.
139 bool useNew = true;
Nick Kledzik38eec3d2011-12-22 02:38:01 +0000140 switch (collide(existing->definition(), newAtom.definition())) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000141 case NCR_First:
142 useNew = false;
143 break;
144 case NCR_Second:
145 useNew = true;
146 break;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000147 case NCR_DupDef:
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000148 assert(existing->definition() == Atom::definitionRegular);
149 assert(newAtom.definition() == Atom::definitionRegular);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000150 switch ( mergeSelect(((DefinedAtom*)existing)->merge(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000151 ((DefinedAtom*)(&newAtom))->merge()) ) {
152 case MCR_First:
153 useNew = false;
154 break;
155 case MCR_Second:
156 useNew = true;
157 break;
158 case MCR_Largest:
159 useNew = true;
160 break;
161 case MCR_Error:
Michael J. Spencer7f693c52013-01-04 21:17:51 +0000162 llvm::errs() << "Duplicate symbols: "
163 << existing->name()
164 << ":"
165 << existing->file().path()
166 << " and "
167 << newAtom.name()
168 << ":"
169 << newAtom.file().path()
170 << "\n";
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000171 llvm::report_fatal_error("duplicate symbol error");
172 break;
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000173 }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000174 break;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000175 case NCR_DupUndef: {
Michael J. Spencerb4955622012-04-02 23:56:36 +0000176 const UndefinedAtom* existingUndef =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000177 dyn_cast<UndefinedAtom>(existing);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000178 const UndefinedAtom* newUndef =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000179 dyn_cast<UndefinedAtom>(&newAtom);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000180 assert(existingUndef != nullptr);
181 assert(newUndef != nullptr);
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000182 if ( existingUndef->canBeNull() == newUndef->canBeNull() ) {
183 useNew = false;
184 }
185 else {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000186 if ( _options.warnIfCoalesableAtomsHaveDifferentCanBeNull() ) {
187 // FIXME: need diagonstics interface for writing warning messages
Nick Kledzikabb69812012-05-31 22:34:00 +0000188 llvm::errs() << "lld warning: undefined symbol "
Nick Kledzikbb963df2012-04-18 21:55:06 +0000189 << existingUndef->name()
190 << " has different weakness in "
191 << existingUndef->file().path()
192 << " and in "
193 << newUndef->file().path();
194 }
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000195 useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000196 }
197 }
198 break;
199 case NCR_DupShLib: {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000200 const SharedLibraryAtom* curShLib =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000201 dyn_cast<SharedLibraryAtom>(existing);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000202 const SharedLibraryAtom* newShLib =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000203 dyn_cast<SharedLibraryAtom>(&newAtom);
Nick Kledzikbb963df2012-04-18 21:55:06 +0000204 assert(curShLib != nullptr);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000205 assert(newShLib != nullptr);
Nick Kledzikbb963df2012-04-18 21:55:06 +0000206 bool sameNullness = (curShLib->canBeNullAtRuntime()
207 == newShLib->canBeNullAtRuntime());
208 bool sameName = curShLib->loadName().equals(newShLib->loadName());
209 if ( !sameName ) {
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000210 useNew = false;
Nick Kledzikbb963df2012-04-18 21:55:06 +0000211 if ( _options.warnIfCoalesableAtomsHaveDifferentLoadName() ) {
212 // FIXME: need diagonstics interface for writing warning messages
Nick Kledzikabb69812012-05-31 22:34:00 +0000213 llvm::errs() << "lld warning: shared library symbol "
Nick Kledzikbb963df2012-04-18 21:55:06 +0000214 << curShLib->name()
215 << " has different load path in "
216 << curShLib->file().path()
217 << " and in "
218 << newShLib->file().path();
219 }
220 }
221 else if ( ! sameNullness ) {
222 useNew = false;
223 if ( _options.warnIfCoalesableAtomsHaveDifferentCanBeNull() ) {
224 // FIXME: need diagonstics interface for writing warning messages
Nick Kledzikabb69812012-05-31 22:34:00 +0000225 llvm::errs() << "lld warning: shared library symbol "
Nick Kledzikbb963df2012-04-18 21:55:06 +0000226 << curShLib->name()
227 << " has different weakness in "
228 << curShLib->file().path()
229 << " and in "
230 << newShLib->file().path();
231 }
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000232 }
233 else {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000234 // Both shlib atoms are identical and can be coalesced.
235 useNew = false;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000236 }
237 }
238 break;
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000239 default:
240 llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000241 }
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000242 if ( useNew ) {
243 // Update name table to use new atom.
244 _nameTable[name] = &newAtom;
245 // Add existing atom to replacement table.
246 _replacedAtoms[existing] = &newAtom;
247 }
248 else {
249 // New atom is not being used. Add it to replacement table.
250 _replacedAtoms[&newAtom] = existing;
251 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000252 }
253}
254
Nick Kledzikaf18a2b2012-02-15 00:50:07 +0000255unsigned SymbolTable::AtomMappingInfo::getHashValue(const DefinedAtom * const atom) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000256 unsigned hash = atom->size();
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000257 if ( atom->contentType() != DefinedAtom::typeZeroFill ) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000258 ArrayRef<uint8_t> content = atom->rawContent();
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000259 for (unsigned int i=0; i < content.size(); ++i) {
260 hash = hash * 33 + content[i];
261 }
262 }
263 hash &= 0x00FFFFFF;
264 hash |= ((unsigned)atom->contentType()) << 24;
265 //fprintf(stderr, "atom=%p, hash=0x%08X\n", atom, hash);
266 return hash;
267}
268
Michael J. Spencer765792d2012-04-03 18:40:27 +0000269bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000270 const DefinedAtom * const r) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000271 if ( l == r )
272 return true;
273 if ( l == getEmptyKey() )
274 return false;
275 if ( r == getEmptyKey() )
276 return false;
277 if ( l == getTombstoneKey() )
278 return false;
279 if ( r == getTombstoneKey() )
280 return false;
Michael J. Spencere6203a52012-04-03 18:39:40 +0000281
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000282 if ( l->contentType() != r->contentType() )
283 return false;
284 if ( l->size() != r->size() )
285 return false;
Michael J. Spencere6203a52012-04-03 18:39:40 +0000286 ArrayRef<uint8_t> lc = l->rawContent();
287 ArrayRef<uint8_t> rc = r->rawContent();
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000288 return lc.equals(rc);
289}
290
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000291void SymbolTable::addByContent(const DefinedAtom & newAtom) {
Nick Kledzik233f5372013-01-15 00:17:57 +0000292 // Currently only read-only constants can be merged.
293 assert(newAtom.permissions() == DefinedAtom::permR__);
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000294 AtomContentSet::iterator pos = _contentTable.find(&newAtom);
295 if ( pos == _contentTable.end() ) {
296 _contentTable.insert(&newAtom);
297 return;
298 }
299 const Atom* existing = *pos;
300 // New atom is not being used. Add it to replacement table.
301 _replacedAtoms[&newAtom] = existing;
302}
303
Michael J. Spencere6203a52012-04-03 18:39:40 +0000304const Atom *SymbolTable::findByName(StringRef sym) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000305 NameToAtom::iterator pos = _nameTable.find(sym);
306 if (pos == _nameTable.end())
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000307 return nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000308 return pos->second;
309}
310
Michael J. Spencere6203a52012-04-03 18:39:40 +0000311bool SymbolTable::isDefined(StringRef sym) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000312 const Atom *atom = this->findByName(sym);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000313 if (atom == nullptr)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000314 return false;
315 if (atom->definition() == Atom::definitionUndefined)
316 return false;
317 return true;
318}
319
320const Atom *SymbolTable::replacement(const Atom *atom) {
321 AtomToAtom::iterator pos = _replacedAtoms.find(atom);
322 if (pos == _replacedAtoms.end())
323 return atom;
324 // might be chain, recurse to end
325 return this->replacement(pos->second);
326}
327
328unsigned int SymbolTable::size() {
329 return _nameTable.size();
330}
331
332void SymbolTable::undefines(std::vector<const Atom *> &undefs) {
333 for (NameToAtom::iterator it = _nameTable.begin(),
334 end = _nameTable.end(); it != end; ++it) {
335 const Atom *atom = it->second;
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000336 assert(atom != nullptr);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000337 if (atom->definition() == Atom::definitionUndefined)
338 undefs.push_back(atom);
339 }
340}
341
Nick Kledzik20e652d2012-04-20 01:24:37 +0000342void SymbolTable::tentativeDefinitions(std::vector<StringRef> &names) {
343 for (auto entry : _nameTable) {
344 const Atom *atom = entry.second;
345 StringRef name = entry.first;
346 assert(atom != nullptr);
347 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom) ) {
348 if ( defAtom->merge() == DefinedAtom::mergeAsTentative )
349 names.push_back(name);
350 }
351 }
352}
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000353} // namespace lld