blob: 135418407361c9b4fc0c95baf85e5c12b62e207f [file] [log] [blame]
Michael J. Spencer773a8fb2011-12-18 08:27:59 +00001//===- Core/Resolver.cpp - Resolves Atom References -----------------------===//
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/Resolver.h"
11#include "lld/Core/Atom.h"
12#include "lld/Core/File.h"
13#include "lld/Core/InputFiles.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000014#include "lld/Core/Platform.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000015#include "lld/Core/SymbolTable.h"
16#include "lld/Core/UndefinedAtom.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000017
18#include "llvm/Support/raw_ostream.h"
19
20#include <algorithm>
21#include <cassert>
22#include <vector>
23
24namespace lld {
25
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000026/// This is used as a filter function to std::remove_if to dead strip atoms.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000027class NotLive {
28public:
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000029 NotLive(const llvm::DenseSet<const Atom*>& la) : _liveAtoms(la) { }
30
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000031 bool operator()(const Atom *atom) const {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000032 // don't remove if live
33 if ( _liveAtoms.count(atom) )
34 return false;
Nick Kledzik23384e82012-02-07 02:59:54 +000035 // don't remove if marked never-dead-strip
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000036 if ( const DefinedAtom* defAtom = atom->definedAtom() ) {
37 if ( defAtom->deadStrip() == DefinedAtom::deadStripNever )
38 return false;
39 }
40 // do remove this atom
41 return true;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000042 }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000043
44private:
45 const llvm::DenseSet<const Atom*> _liveAtoms;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000046};
47
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000048
49/// This is used as a filter function to std::remove_if to coalesced atoms.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000050class AtomCoalescedAway {
51public:
52 AtomCoalescedAway(SymbolTable &sym) : _symbolTable(sym) {}
53
54 bool operator()(const Atom *atom) const {
55 const Atom *rep = _symbolTable.replacement(atom);
56 return rep != atom;
57 }
58
59private:
60 SymbolTable &_symbolTable;
61};
62
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000063
64
65
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000066void Resolver::initializeState() {
67 _platform.initialize();
68}
69
70// add initial undefines from -u option
71void Resolver::addInitialUndefines() {
72
73}
74
75// add all atoms from all initial .o files
76void Resolver::buildInitialAtomList() {
77 // each input files contributes initial atoms
78 _atoms.reserve(1024);
79 _inputFiles.forEachInitialAtom(*this);
80
81 _completedInitialObjectFiles = true;
82}
83
84
85// called before the first atom in any file is added with doAtom()
86void Resolver::doFile(const File &file) {
87 // notify platform
88 _platform.fileAdded(file);
89}
90
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000091
92void Resolver::doUndefinedAtom(const class UndefinedAtom& atom) {
93 // add to list of known atoms
94 _atoms.push_back(&atom);
95
96 // tell symbol table
97 _symbolTable.add(atom);
98}
99
100
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000101// called on each atom when a file is added
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000102void Resolver::doDefinedAtom(const DefinedAtom &atom) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000103 // notify platform
104 _platform.atomAdded(atom);
105
106 // add to list of known atoms
107 _atoms.push_back(&atom);
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000108
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000109 // adjust scope (e.g. force some globals to be hidden)
110 _platform.adjustScope(atom);
111
112 // non-static atoms need extra handling
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000113 if (atom.scope() != DefinedAtom::scopeTranslationUnit) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000114 // tell symbol table about non-static atoms
115 _symbolTable.add(atom);
116
117 // platform can add aliases for any symbol
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000118 std::vector<const DefinedAtom *> aliases;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000119 if (_platform.getAliasAtoms(atom, aliases))
120 this->addAtoms(aliases);
121 }
122
123 if (_platform.deadCodeStripping()) {
124 // add to set of dead-strip-roots, all symbols that
125 // the compiler marks as don't strip
126 if (!atom.deadStrip())
127 _deadStripRoots.insert(&atom);
128
129 // add to set of dead-strip-roots, all symbols that
130 // the platform decided must remain
131 if (_platform.isDeadStripRoot(atom))
132 _deadStripRoots.insert(&atom);
133 }
134}
135
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000136void Resolver::doSharedLibraryAtom(const SharedLibraryAtom& atom) {
137 // add to list of known atoms
138 _atoms.push_back(&atom);
139
140 // tell symbol table
141 _symbolTable.add(atom);
142}
143
144void Resolver::doAbsoluteAtom(const AbsoluteAtom& atom) {
145 // add to list of known atoms
146 _atoms.push_back(&atom);
147
148 // tell symbol table
149 _symbolTable.add(atom);
150}
151
152
153
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000154// utility to add a vector of atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000155void Resolver::addAtoms(const std::vector<const DefinedAtom*>& newAtoms) {
156 for (std::vector<const DefinedAtom *>::const_iterator it = newAtoms.begin();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000157 it != newAtoms.end(); ++it) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000158 this->doDefinedAtom(**it);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000159 }
160}
161
162// ask symbol table if any definitionUndefined atoms still exist
163// if so, keep searching libraries until no more atoms being added
164void Resolver::resolveUndefines() {
165 const bool searchArchives =
166 _platform.searchArchivesToOverrideTentativeDefinitions();
167 const bool searchDylibs =
168 _platform.searchSharedLibrariesToOverrideTentativeDefinitions();
169
170 // keep looping until no more undefines were added in last loop
171 unsigned int undefineGenCount = 0xFFFFFFFF;
172 while (undefineGenCount != _symbolTable.size()) {
173 undefineGenCount = _symbolTable.size();
174 std::vector<const Atom *> undefines;
175 _symbolTable.undefines(undefines);
176 for (std::vector<const Atom *>::iterator it = undefines.begin();
177 it != undefines.end(); ++it) {
178 llvm::StringRef undefName = (*it)->name();
179 // load for previous undefine may also have loaded this undefine
180 if (!_symbolTable.isDefined(undefName)) {
181 _inputFiles.searchLibraries(undefName, true, true, false, *this);
182
183 // give platform a chance to instantiate platform
184 // specific atoms (e.g. section boundary)
185 if (!_symbolTable.isDefined(undefName)) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000186 std::vector<const DefinedAtom *> platAtoms;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000187 if (_platform.getPlatformAtoms(undefName, platAtoms))
188 this->addAtoms(platAtoms);
189 }
190 }
191 }
192 // search libraries for overrides of common symbols
193 if (searchArchives || searchDylibs) {
194 std::vector<const Atom *> tents;
195 for (std::vector<const Atom *>::iterator ait = _atoms.begin();
196 ait != _atoms.end(); ++ait) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000197 if ( const DefinedAtom* defAtom = (*ait)->definedAtom() ) {
198 if ( defAtom->merge() == DefinedAtom::mergeAsTentative )
199 tents.push_back(defAtom);
200 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000201 }
202 for (std::vector<const Atom *>::iterator dit = tents.begin();
203 dit != tents.end(); ++dit) {
204 // load for previous tentative may also have loaded
205 // this tentative, so check again
206 llvm::StringRef tentName = (*dit)->name();
207 const Atom *curAtom = _symbolTable.findByName(tentName);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000208 assert(curAtom != nullptr);
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000209 if ( const DefinedAtom* curDefAtom = curAtom->definedAtom() ) {
210 if (curDefAtom->merge() == DefinedAtom::mergeAsTentative )
211 _inputFiles.searchLibraries(tentName, searchDylibs,
212 true, true, *this);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000213 }
214 }
215 }
216 }
217}
218
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000219
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000220// switch all references to undefined or coalesced away atoms
221// to the new defined atom
222void Resolver::updateReferences() {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000223 for (auto ait = _atoms.begin(); ait != _atoms.end(); ++ait) {
224 if ( const DefinedAtom* defAtom = (*ait)->definedAtom() ) {
225 for (auto rit=defAtom->referencesBegin(), end=defAtom->referencesEnd();
226 rit != end; ++rit) {
227 const Reference* ref = *rit;
228 const Atom* newTarget = _symbolTable.replacement(ref->target());
229 (const_cast<Reference*>(ref))->setTarget(newTarget);
230 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000231 }
232 }
233}
234
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000235
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000236// for dead code stripping, recursively mark atom "live"
237void Resolver::markLive(const Atom &atom, WhyLiveBackChain *previous) {
238 // if -why_live cares about this symbol, then dump chain
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000239 if ((previous->referer != nullptr) && _platform.printWhyLive(atom.name())) {
Nick Kledzikf46669c2011-12-21 23:29:36 +0000240 llvm::errs() << atom.name() << " from " << atom.file().path() << "\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000241 int depth = 1;
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000242 for (WhyLiveBackChain *p = previous; p != nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000243 p = p->previous, ++depth) {
244 for (int i = depth; i > 0; --i)
245 llvm::errs() << " ";
246 llvm::errs() << p->referer->name() << " from "
Nick Kledzikf46669c2011-12-21 23:29:36 +0000247 << p->referer->file().path() << "\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000248 }
249 }
250
251 // if already marked live, then done (stop recursion)
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000252 if ( _liveAtoms.count(&atom) )
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000253 return;
254
255 // mark this atom is live
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000256 _liveAtoms.insert(&atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000257
258 // mark all atoms it references as live
259 WhyLiveBackChain thisChain;
260 thisChain.previous = previous;
261 thisChain.referer = &atom;
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000262 if ( const DefinedAtom* defAtom = atom.definedAtom() ) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000263 for (auto rit=defAtom->referencesBegin(), end=defAtom->referencesEnd();
264 rit != end; ++rit) {
265 const Reference* ref = *rit;
266 this->markLive(*ref->target(), &thisChain);
267 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000268 }
269}
270
271// remove all atoms not actually used
272void Resolver::deadStripOptimize() {
273 // only do this optimization with -dead_strip
274 if (!_platform.deadCodeStripping())
275 return;
276
277 // clear liveness on all atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000278 _liveAtoms.clear();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000279
280 // add entry point (main) to live roots
281 const Atom *entry = this->entryPoint();
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000282 if (entry != nullptr)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000283 _deadStripRoots.insert(entry);
284
285 // add -exported_symbols_list, -init, and -u entries to live roots
286 for (Platform::UndefinesIterator uit = _platform.initialUndefinesBegin();
287 uit != _platform.initialUndefinesEnd(); ++uit) {
288 llvm::StringRef sym = *uit;
289 const Atom *symAtom = _symbolTable.findByName(sym);
290 assert(symAtom->definition() != Atom::definitionUndefined);
291 _deadStripRoots.insert(symAtom);
292 }
293
294 // add platform specific helper atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000295 std::vector<const DefinedAtom *> platRootAtoms;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000296 if (_platform.getImplicitDeadStripRoots(platRootAtoms))
297 this->addAtoms(platRootAtoms);
298
299 // mark all roots as live, and recursively all atoms they reference
300 for (std::set<const Atom *>::iterator it = _deadStripRoots.begin();
301 it != _deadStripRoots.end(); ++it) {
302 WhyLiveBackChain rootChain;
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000303 rootChain.previous = nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000304 rootChain.referer = *it;
305 this->markLive(**it, &rootChain);
306 }
307
308 // now remove all non-live atoms from _atoms
309 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000310 NotLive(_liveAtoms)), _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000311}
312
313// error out if some undefines remain
314void Resolver::checkUndefines(bool final) {
315 // when using LTO, undefines are checked after bitcode is optimized
316 if (_haveLLVMObjs && !final)
317 return;
318
319 // build vector of remaining undefined symbols
320 std::vector<const Atom *> undefinedAtoms;
321 _symbolTable.undefines(undefinedAtoms);
322 if (_platform.deadCodeStripping()) {
323 // when dead code stripping we don't care if dead atoms are undefined
324 undefinedAtoms.erase(std::remove_if(
325 undefinedAtoms.begin(), undefinedAtoms.end(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000326 NotLive(_liveAtoms)), undefinedAtoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000327 }
328
329 // let platform make error message about missing symbols
330 if (undefinedAtoms.size() != 0)
331 _platform.errorWithUndefines(undefinedAtoms, _atoms);
332}
333
334// remove from _atoms all coaleseced away atoms
335void Resolver::removeCoalescedAwayAtoms() {
336 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
337 AtomCoalescedAway(_symbolTable)), _atoms.end());
338}
339
340// check for interactions between symbols defined in this linkage unit
341// and same symbol name in linked dynamic shared libraries
342void Resolver::checkDylibSymbolCollisions() {
343 for (std::vector<const Atom *>::const_iterator it = _atoms.begin();
344 it != _atoms.end(); ++it) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000345 const DefinedAtom* defAtom = (*it)->definedAtom();
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000346 if (defAtom == nullptr)
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000347 continue;
348 if ( defAtom->merge() != DefinedAtom::mergeAsTentative )
349 continue;
350 assert(defAtom->scope() != DefinedAtom::scopeTranslationUnit);
351 // See if any shared library also has symbol which
352 // collides with the tentative definition.
353 // SymbolTable will warn if needed.
354 _inputFiles.searchLibraries(defAtom->name(), true, false, false, *this);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000355 }
356}
357
358// get "main" atom for linkage unit
359const Atom *Resolver::entryPoint() {
360 llvm::StringRef symbolName = _platform.entryPointName();
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000361 if (symbolName != nullptr)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000362 return _symbolTable.findByName(symbolName);
363
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000364 return nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000365}
366
367// give platform a chance to tweak the set of atoms
368void Resolver::tweakAtoms() {
369 _platform.postResolveTweaks(_atoms);
370}
371
372void Resolver::linkTimeOptimize() {
373 // FIX ME
374}
375
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000376void Resolver::resolve() {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000377 this->initializeState();
378 this->addInitialUndefines();
379 this->buildInitialAtomList();
380 this->resolveUndefines();
381 this->updateReferences();
382 this->deadStripOptimize();
383 this->checkUndefines(false);
384 this->removeCoalescedAwayAtoms();
385 this->checkDylibSymbolCollisions();
386 this->linkTimeOptimize();
387 this->tweakAtoms();
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000388 this->_result.addAtoms(_atoms);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000389}
390
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000391void Resolver::MergedFile::addAtom(const Atom& atom) {
392 if ( const DefinedAtom* defAtom = atom.definedAtom() ) {
393 _definedAtoms._atoms.push_back(defAtom);
394 }
395 else if ( const UndefinedAtom* undefAtom = atom.undefinedAtom() ) {
396 _undefinedAtoms._atoms.push_back(undefAtom);
397 }
398 else if ( const SharedLibraryAtom* slAtom = atom.sharedLibraryAtom() ) {
399 _sharedLibraryAtoms._atoms.push_back(slAtom);
400 }
401 else if ( const AbsoluteAtom* abAtom = atom.absoluteAtom() ) {
402 _absoluteAtoms._atoms.push_back(abAtom);
403 }
404 else {
405 assert(0 && "atom has unknown definition kind");
406 }
407}
408
409void Resolver::MergedFile::addAtoms(std::vector<const Atom*>& all) {
410 for(std::vector<const Atom*>::iterator it=all.begin(); it != all.end(); ++it) {
411 this->addAtom(**it);
412 }
413}
414
415
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000416} // namespace lld