blob: 9fd4eb26354c83f569dbf6e6633bc202795d5d6d [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"
Nick Kledzik6bc04c62012-02-22 21:56:59 +000011#include "lld/Core/AbsoluteAtom.h"
Michael J. Spencer4586fbc2013-01-22 20:49:42 +000012#include "lld/Core/Atom.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. Spencer4586fbc2013-01-22 20:49:42 +000016#include "lld/Core/LinkerOptions.h"
Michael J. Spencere6203a52012-04-03 18:39:40 +000017#include "lld/Core/LLVM.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000018#include "lld/Core/Resolver.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000019#include "lld/Core/SharedLibraryAtom.h"
Michael J. Spencer4586fbc2013-01-22 20:49:42 +000020#include "lld/Core/TargetInfo.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000021#include "lld/Core/UndefinedAtom.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000022
Nick Kledzikbfedfc12012-01-09 20:18:15 +000023#include "llvm/ADT/ArrayRef.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000024#include "llvm/ADT/DenseMapInfo.h"
25#include "llvm/Support/ErrorHandling.h"
Nick Kledzikbb963df2012-04-18 21:55:06 +000026#include "llvm/Support/raw_ostream.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000027
28#include <algorithm>
29#include <cassert>
Michael J. Spencercfd029f2012-03-28 19:04:02 +000030#include <cstdlib>
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000031#include <vector>
32
33namespace lld {
Michael J. Spencer4586fbc2013-01-22 20:49:42 +000034SymbolTable::SymbolTable(const TargetInfo &ti) : _targetInfo(ti) {}
Nick Kledzik38eec3d2011-12-22 02:38:01 +000035
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) {
Shankar Easwaran8962feb2013-03-14 16:09:49 +000049 if (!atom.name().empty() &&
Nick Kledzik233f5372013-01-15 00:17:57 +000050 (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 {
Michael J. Spencer4586fbc2013-01-22 20:49:42 +0000186 if (_targetInfo.getLinkerOptions().
187 _warnIfCoalesableAtomsHaveDifferentCanBeNull) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000188 // FIXME: need diagonstics interface for writing warning messages
Nick Kledzikabb69812012-05-31 22:34:00 +0000189 llvm::errs() << "lld warning: undefined symbol "
Nick Kledzikbb963df2012-04-18 21:55:06 +0000190 << existingUndef->name()
191 << " has different weakness in "
192 << existingUndef->file().path()
193 << " and in "
194 << newUndef->file().path();
195 }
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000196 useNew = (newUndef->canBeNull() < existingUndef->canBeNull());
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000197 }
198 }
199 break;
200 case NCR_DupShLib: {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000201 const SharedLibraryAtom* curShLib =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000202 dyn_cast<SharedLibraryAtom>(existing);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000203 const SharedLibraryAtom* newShLib =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000204 dyn_cast<SharedLibraryAtom>(&newAtom);
Nick Kledzikbb963df2012-04-18 21:55:06 +0000205 assert(curShLib != nullptr);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000206 assert(newShLib != nullptr);
Nick Kledzikbb963df2012-04-18 21:55:06 +0000207 bool sameNullness = (curShLib->canBeNullAtRuntime()
208 == newShLib->canBeNullAtRuntime());
209 bool sameName = curShLib->loadName().equals(newShLib->loadName());
210 if ( !sameName ) {
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000211 useNew = false;
Michael J. Spencer4586fbc2013-01-22 20:49:42 +0000212 if (_targetInfo.getLinkerOptions().
213 _warnIfCoalesableAtomsHaveDifferentLoadName) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000214 // FIXME: need diagonstics interface for writing warning messages
Nick Kledzikabb69812012-05-31 22:34:00 +0000215 llvm::errs() << "lld warning: shared library symbol "
Nick Kledzikbb963df2012-04-18 21:55:06 +0000216 << curShLib->name()
217 << " has different load path in "
218 << curShLib->file().path()
219 << " and in "
220 << newShLib->file().path();
221 }
222 }
223 else if ( ! sameNullness ) {
224 useNew = false;
Michael J. Spencer4586fbc2013-01-22 20:49:42 +0000225 if (_targetInfo.getLinkerOptions().
226 _warnIfCoalesableAtomsHaveDifferentCanBeNull) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000227 // FIXME: need diagonstics interface for writing warning messages
Nick Kledzikabb69812012-05-31 22:34:00 +0000228 llvm::errs() << "lld warning: shared library symbol "
Nick Kledzikbb963df2012-04-18 21:55:06 +0000229 << curShLib->name()
230 << " has different weakness in "
231 << curShLib->file().path()
232 << " and in "
233 << newShLib->file().path();
234 }
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000235 }
236 else {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000237 // Both shlib atoms are identical and can be coalesced.
238 useNew = false;
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000239 }
240 }
241 break;
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000242 default:
243 llvm::report_fatal_error("SymbolTable::addByName(): unhandled switch clause");
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000244 }
Nick Kledzik7735a7d2012-01-04 23:58:17 +0000245 if ( useNew ) {
246 // Update name table to use new atom.
247 _nameTable[name] = &newAtom;
248 // Add existing atom to replacement table.
249 _replacedAtoms[existing] = &newAtom;
250 }
251 else {
252 // New atom is not being used. Add it to replacement table.
253 _replacedAtoms[&newAtom] = existing;
254 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000255 }
256}
257
Nick Kledzikaf18a2b2012-02-15 00:50:07 +0000258unsigned SymbolTable::AtomMappingInfo::getHashValue(const DefinedAtom * const atom) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000259 unsigned hash = atom->size();
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000260 if ( atom->contentType() != DefinedAtom::typeZeroFill ) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000261 ArrayRef<uint8_t> content = atom->rawContent();
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000262 for (unsigned int i=0; i < content.size(); ++i) {
263 hash = hash * 33 + content[i];
264 }
265 }
266 hash &= 0x00FFFFFF;
267 hash |= ((unsigned)atom->contentType()) << 24;
268 //fprintf(stderr, "atom=%p, hash=0x%08X\n", atom, hash);
269 return hash;
270}
271
Michael J. Spencer765792d2012-04-03 18:40:27 +0000272bool SymbolTable::AtomMappingInfo::isEqual(const DefinedAtom * const l,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000273 const DefinedAtom * const r) {
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000274 if ( l == r )
275 return true;
276 if ( l == getEmptyKey() )
277 return false;
278 if ( r == getEmptyKey() )
279 return false;
280 if ( l == getTombstoneKey() )
281 return false;
282 if ( r == getTombstoneKey() )
283 return false;
Michael J. Spencere6203a52012-04-03 18:39:40 +0000284
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000285 if ( l->contentType() != r->contentType() )
286 return false;
287 if ( l->size() != r->size() )
288 return false;
Michael J. Spencere6203a52012-04-03 18:39:40 +0000289 ArrayRef<uint8_t> lc = l->rawContent();
290 ArrayRef<uint8_t> rc = r->rawContent();
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000291 return lc.equals(rc);
292}
293
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000294void SymbolTable::addByContent(const DefinedAtom & newAtom) {
Nick Kledzik233f5372013-01-15 00:17:57 +0000295 // Currently only read-only constants can be merged.
296 assert(newAtom.permissions() == DefinedAtom::permR__);
Nick Kledzikbfedfc12012-01-09 20:18:15 +0000297 AtomContentSet::iterator pos = _contentTable.find(&newAtom);
298 if ( pos == _contentTable.end() ) {
299 _contentTable.insert(&newAtom);
300 return;
301 }
302 const Atom* existing = *pos;
303 // New atom is not being used. Add it to replacement table.
304 _replacedAtoms[&newAtom] = existing;
305}
306
Michael J. Spencere6203a52012-04-03 18:39:40 +0000307const Atom *SymbolTable::findByName(StringRef sym) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000308 NameToAtom::iterator pos = _nameTable.find(sym);
309 if (pos == _nameTable.end())
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000310 return nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000311 return pos->second;
312}
313
Michael J. Spencere6203a52012-04-03 18:39:40 +0000314bool SymbolTable::isDefined(StringRef sym) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000315 const Atom *atom = this->findByName(sym);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000316 if (atom == nullptr)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000317 return false;
318 if (atom->definition() == Atom::definitionUndefined)
319 return false;
320 return true;
321}
322
323const Atom *SymbolTable::replacement(const Atom *atom) {
324 AtomToAtom::iterator pos = _replacedAtoms.find(atom);
325 if (pos == _replacedAtoms.end())
326 return atom;
327 // might be chain, recurse to end
328 return this->replacement(pos->second);
329}
330
331unsigned int SymbolTable::size() {
332 return _nameTable.size();
333}
334
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000335void SymbolTable::undefines(std::vector<const UndefinedAtom *> &undefs) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000336 for (NameToAtom::iterator it = _nameTable.begin(),
337 end = _nameTable.end(); it != end; ++it) {
338 const Atom *atom = it->second;
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000339 assert(atom != nullptr);
Michael J. Spencer280dadb2013-01-31 22:56:13 +0000340 if (const auto undef = dyn_cast<const UndefinedAtom>(atom))
341 undefs.push_back(undef);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000342 }
343}
344
Nick Kledzik20e652d2012-04-20 01:24:37 +0000345void SymbolTable::tentativeDefinitions(std::vector<StringRef> &names) {
346 for (auto entry : _nameTable) {
347 const Atom *atom = entry.second;
348 StringRef name = entry.first;
349 assert(atom != nullptr);
350 if (const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom) ) {
351 if ( defAtom->merge() == DefinedAtom::mergeAsTentative )
352 names.push_back(name);
353 }
354 }
355}
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000356} // namespace lld