blob: 4afbe4b075d989545160040117a0031c183a8f4d [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. Spencere6203a52012-04-03 18:39:40 +000014#include "lld/Core/LLVM.h"
Michael J. Spencercfd029f2012-03-28 19:04:02 +000015#include "lld/Core/Platform.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000016#include "lld/Core/SymbolTable.h"
17#include "lld/Core/UndefinedAtom.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000018
19#include "llvm/Support/raw_ostream.h"
20
21#include <algorithm>
22#include <cassert>
23#include <vector>
24
25namespace lld {
26
Michael J. Spencer765792d2012-04-03 18:40:27 +000027/// This is used as a filter function to std::remove_if to dead strip atoms.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000028class NotLive {
29public:
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000030 NotLive(const llvm::DenseSet<const Atom*>& la) : _liveAtoms(la) { }
Michael J. Spencer765792d2012-04-03 18:40:27 +000031
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000032 bool operator()(const Atom *atom) const {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000033 // don't remove if live
34 if ( _liveAtoms.count(atom) )
35 return false;
Nick Kledzik23384e82012-02-07 02:59:54 +000036 // don't remove if marked never-dead-strip
Michael J. Spencere6203a52012-04-03 18:39:40 +000037 if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(atom)) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000038 if ( defAtom->deadStrip() == DefinedAtom::deadStripNever )
39 return false;
40 }
41 // do remove this atom
42 return true;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000043 }
Michael J. Spencer765792d2012-04-03 18:40:27 +000044
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000045private:
46 const llvm::DenseSet<const Atom*> _liveAtoms;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000047};
48
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000049
Michael J. Spencer765792d2012-04-03 18:40:27 +000050/// This is used as a filter function to std::remove_if to coalesced atoms.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000051class AtomCoalescedAway {
52public:
53 AtomCoalescedAway(SymbolTable &sym) : _symbolTable(sym) {}
54
55 bool operator()(const Atom *atom) const {
56 const Atom *rep = _symbolTable.replacement(atom);
57 return rep != atom;
58 }
59
60private:
61 SymbolTable &_symbolTable;
62};
63
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000064
65
66
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000067void Resolver::initializeState() {
68 _platform.initialize();
69}
70
71// add initial undefines from -u option
72void Resolver::addInitialUndefines() {
73
74}
75
76// add all atoms from all initial .o files
77void Resolver::buildInitialAtomList() {
78 // each input files contributes initial atoms
79 _atoms.reserve(1024);
80 _inputFiles.forEachInitialAtom(*this);
81
82 _completedInitialObjectFiles = true;
83}
84
85
86// called before the first atom in any file is added with doAtom()
87void Resolver::doFile(const File &file) {
88 // notify platform
89 _platform.fileAdded(file);
90}
91
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000092
93void Resolver::doUndefinedAtom(const class UndefinedAtom& atom) {
94 // add to list of known atoms
95 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +000096
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000097 // tell symbol table
98 _symbolTable.add(atom);
99}
100
101
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000102// called on each atom when a file is added
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000103void Resolver::doDefinedAtom(const DefinedAtom &atom) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000104 // notify platform
105 _platform.atomAdded(atom);
106
107 // add to list of known atoms
108 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000109
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000110 // adjust scope (e.g. force some globals to be hidden)
111 _platform.adjustScope(atom);
112
113 // non-static atoms need extra handling
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000114 if (atom.scope() != DefinedAtom::scopeTranslationUnit) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000115 // tell symbol table about non-static atoms
116 _symbolTable.add(atom);
117
118 // platform can add aliases for any symbol
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000119 std::vector<const DefinedAtom *> aliases;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000120 if (_platform.getAliasAtoms(atom, aliases))
121 this->addAtoms(aliases);
122 }
123
124 if (_platform.deadCodeStripping()) {
125 // add to set of dead-strip-roots, all symbols that
126 // the compiler marks as don't strip
127 if (!atom.deadStrip())
128 _deadStripRoots.insert(&atom);
129
130 // add to set of dead-strip-roots, all symbols that
131 // the platform decided must remain
132 if (_platform.isDeadStripRoot(atom))
133 _deadStripRoots.insert(&atom);
134 }
135}
136
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000137void Resolver::doSharedLibraryAtom(const SharedLibraryAtom& atom) {
138 // add to list of known atoms
139 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000140
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000141 // tell symbol table
142 _symbolTable.add(atom);
143}
Michael J. Spencer765792d2012-04-03 18:40:27 +0000144
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000145void Resolver::doAbsoluteAtom(const AbsoluteAtom& atom) {
146 // add to list of known atoms
147 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000148
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000149 // tell symbol table
150 _symbolTable.add(atom);
151}
152
153
Michael J. Spencer765792d2012-04-03 18:40:27 +0000154
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000155// utility to add a vector of atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000156void Resolver::addAtoms(const std::vector<const DefinedAtom*>& newAtoms) {
157 for (std::vector<const DefinedAtom *>::const_iterator it = newAtoms.begin();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000158 it != newAtoms.end(); ++it) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000159 this->doDefinedAtom(**it);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000160 }
161}
162
163// ask symbol table if any definitionUndefined atoms still exist
164// if so, keep searching libraries until no more atoms being added
165void Resolver::resolveUndefines() {
166 const bool searchArchives =
167 _platform.searchArchivesToOverrideTentativeDefinitions();
168 const bool searchDylibs =
169 _platform.searchSharedLibrariesToOverrideTentativeDefinitions();
170
171 // keep looping until no more undefines were added in last loop
172 unsigned int undefineGenCount = 0xFFFFFFFF;
173 while (undefineGenCount != _symbolTable.size()) {
174 undefineGenCount = _symbolTable.size();
175 std::vector<const Atom *> undefines;
176 _symbolTable.undefines(undefines);
177 for (std::vector<const Atom *>::iterator it = undefines.begin();
178 it != undefines.end(); ++it) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000179 StringRef undefName = (*it)->name();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000180 // load for previous undefine may also have loaded this undefine
181 if (!_symbolTable.isDefined(undefName)) {
182 _inputFiles.searchLibraries(undefName, true, true, false, *this);
183
184 // give platform a chance to instantiate platform
185 // specific atoms (e.g. section boundary)
186 if (!_symbolTable.isDefined(undefName)) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000187 std::vector<const DefinedAtom *> platAtoms;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000188 if (_platform.getPlatformAtoms(undefName, platAtoms))
189 this->addAtoms(platAtoms);
190 }
191 }
192 }
193 // search libraries for overrides of common symbols
194 if (searchArchives || searchDylibs) {
195 std::vector<const Atom *> tents;
196 for (std::vector<const Atom *>::iterator ait = _atoms.begin();
197 ait != _atoms.end(); ++ait) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000198 if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(*ait)) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000199 if ( defAtom->merge() == DefinedAtom::mergeAsTentative )
200 tents.push_back(defAtom);
201 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000202 }
203 for (std::vector<const Atom *>::iterator dit = tents.begin();
204 dit != tents.end(); ++dit) {
205 // load for previous tentative may also have loaded
206 // this tentative, so check again
Michael J. Spencere6203a52012-04-03 18:39:40 +0000207 StringRef tentName = (*dit)->name();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000208 const Atom *curAtom = _symbolTable.findByName(tentName);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000209 assert(curAtom != nullptr);
Michael J. Spencere6203a52012-04-03 18:39:40 +0000210 if (const DefinedAtom* curDefAtom = dyn_cast<DefinedAtom>(curAtom)) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000211 if (curDefAtom->merge() == DefinedAtom::mergeAsTentative )
Michael J. Spencer765792d2012-04-03 18:40:27 +0000212 _inputFiles.searchLibraries(tentName, searchDylibs,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000213 true, true, *this);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000214 }
215 }
216 }
217 }
218}
219
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000220
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000221// switch all references to undefined or coalesced away atoms
222// to the new defined atom
223void Resolver::updateReferences() {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000224 for (auto ait = _atoms.begin(); ait != _atoms.end(); ++ait) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000225 if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(*ait)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000226 for (auto rit=defAtom->referencesBegin(), end=defAtom->referencesEnd();
227 rit != end; ++rit) {
228 const Reference* ref = *rit;
229 const Atom* newTarget = _symbolTable.replacement(ref->target());
230 (const_cast<Reference*>(ref))->setTarget(newTarget);
231 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000232 }
233 }
234}
235
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000236
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000237// for dead code stripping, recursively mark atom "live"
238void Resolver::markLive(const Atom &atom, WhyLiveBackChain *previous) {
239 // if -why_live cares about this symbol, then dump chain
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000240 if ((previous->referer != nullptr) && _platform.printWhyLive(atom.name())) {
Nick Kledzikf46669c2011-12-21 23:29:36 +0000241 llvm::errs() << atom.name() << " from " << atom.file().path() << "\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000242 int depth = 1;
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000243 for (WhyLiveBackChain *p = previous; p != nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000244 p = p->previous, ++depth) {
245 for (int i = depth; i > 0; --i)
246 llvm::errs() << " ";
247 llvm::errs() << p->referer->name() << " from "
Nick Kledzikf46669c2011-12-21 23:29:36 +0000248 << p->referer->file().path() << "\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000249 }
250 }
251
252 // if already marked live, then done (stop recursion)
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000253 if ( _liveAtoms.count(&atom) )
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000254 return;
255
256 // mark this atom is live
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000257 _liveAtoms.insert(&atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000258
259 // mark all atoms it references as live
260 WhyLiveBackChain thisChain;
261 thisChain.previous = previous;
262 thisChain.referer = &atom;
Michael J. Spencere6203a52012-04-03 18:39:40 +0000263 if ( const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(&atom)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000264 for (auto rit=defAtom->referencesBegin(), end=defAtom->referencesEnd();
265 rit != end; ++rit) {
266 const Reference* ref = *rit;
267 this->markLive(*ref->target(), &thisChain);
268 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000269 }
270}
271
272// remove all atoms not actually used
273void Resolver::deadStripOptimize() {
274 // only do this optimization with -dead_strip
275 if (!_platform.deadCodeStripping())
276 return;
277
278 // clear liveness on all atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000279 _liveAtoms.clear();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000280
281 // add entry point (main) to live roots
282 const Atom *entry = this->entryPoint();
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000283 if (entry != nullptr)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000284 _deadStripRoots.insert(entry);
285
286 // add -exported_symbols_list, -init, and -u entries to live roots
287 for (Platform::UndefinesIterator uit = _platform.initialUndefinesBegin();
288 uit != _platform.initialUndefinesEnd(); ++uit) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000289 StringRef sym = *uit;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000290 const Atom *symAtom = _symbolTable.findByName(sym);
291 assert(symAtom->definition() != Atom::definitionUndefined);
292 _deadStripRoots.insert(symAtom);
293 }
294
295 // add platform specific helper atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000296 std::vector<const DefinedAtom *> platRootAtoms;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000297 if (_platform.getImplicitDeadStripRoots(platRootAtoms))
298 this->addAtoms(platRootAtoms);
299
300 // mark all roots as live, and recursively all atoms they reference
301 for (std::set<const Atom *>::iterator it = _deadStripRoots.begin();
302 it != _deadStripRoots.end(); ++it) {
303 WhyLiveBackChain rootChain;
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000304 rootChain.previous = nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000305 rootChain.referer = *it;
306 this->markLive(**it, &rootChain);
307 }
308
309 // now remove all non-live atoms from _atoms
310 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000311 NotLive(_liveAtoms)), _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000312}
313
314// error out if some undefines remain
315void Resolver::checkUndefines(bool final) {
316 // when using LTO, undefines are checked after bitcode is optimized
317 if (_haveLLVMObjs && !final)
318 return;
319
320 // build vector of remaining undefined symbols
321 std::vector<const Atom *> undefinedAtoms;
322 _symbolTable.undefines(undefinedAtoms);
323 if (_platform.deadCodeStripping()) {
324 // when dead code stripping we don't care if dead atoms are undefined
325 undefinedAtoms.erase(std::remove_if(
326 undefinedAtoms.begin(), undefinedAtoms.end(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000327 NotLive(_liveAtoms)), undefinedAtoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000328 }
329
330 // let platform make error message about missing symbols
331 if (undefinedAtoms.size() != 0)
332 _platform.errorWithUndefines(undefinedAtoms, _atoms);
333}
334
335// remove from _atoms all coaleseced away atoms
336void Resolver::removeCoalescedAwayAtoms() {
337 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
338 AtomCoalescedAway(_symbolTable)), _atoms.end());
339}
340
341// check for interactions between symbols defined in this linkage unit
342// and same symbol name in linked dynamic shared libraries
343void Resolver::checkDylibSymbolCollisions() {
344 for (std::vector<const Atom *>::const_iterator it = _atoms.begin();
345 it != _atoms.end(); ++it) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000346 const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(*it);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000347 if (defAtom == nullptr)
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000348 continue;
Michael J. Spencer765792d2012-04-03 18:40:27 +0000349 if ( defAtom->merge() != DefinedAtom::mergeAsTentative )
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000350 continue;
351 assert(defAtom->scope() != DefinedAtom::scopeTranslationUnit);
352 // See if any shared library also has symbol which
353 // collides with the tentative definition.
354 // SymbolTable will warn if needed.
355 _inputFiles.searchLibraries(defAtom->name(), true, false, false, *this);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000356 }
357}
358
359// get "main" atom for linkage unit
360const Atom *Resolver::entryPoint() {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000361 StringRef symbolName = _platform.entryPointName();
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000362 if (symbolName != nullptr)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000363 return _symbolTable.findByName(symbolName);
364
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000365 return nullptr;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000366}
367
368// give platform a chance to tweak the set of atoms
369void Resolver::tweakAtoms() {
370 _platform.postResolveTweaks(_atoms);
371}
372
373void Resolver::linkTimeOptimize() {
374 // FIX ME
375}
376
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000377void Resolver::resolve() {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000378 this->initializeState();
379 this->addInitialUndefines();
380 this->buildInitialAtomList();
381 this->resolveUndefines();
382 this->updateReferences();
383 this->deadStripOptimize();
384 this->checkUndefines(false);
385 this->removeCoalescedAwayAtoms();
386 this->checkDylibSymbolCollisions();
387 this->linkTimeOptimize();
388 this->tweakAtoms();
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000389 this->_result.addAtoms(_atoms);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000390}
391
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000392void Resolver::MergedFile::addAtom(const Atom& atom) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000393 if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(&atom)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000394 _definedAtoms._atoms.push_back(defAtom);
Michael J. Spencere6203a52012-04-03 18:39:40 +0000395 } else if (const UndefinedAtom* undefAtom = dyn_cast<UndefinedAtom>(&atom)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000396 _undefinedAtoms._atoms.push_back(undefAtom);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000397 } else if (const SharedLibraryAtom* slAtom =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000398 dyn_cast<SharedLibraryAtom>(&atom)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000399 _sharedLibraryAtoms._atoms.push_back(slAtom);
Michael J. Spencere6203a52012-04-03 18:39:40 +0000400 } else if (const AbsoluteAtom* abAtom = dyn_cast<AbsoluteAtom>(&atom)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000401 _absoluteAtoms._atoms.push_back(abAtom);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000402 } else {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000403 assert(0 && "atom has unknown definition kind");
404 }
405}
406
407void Resolver::MergedFile::addAtoms(std::vector<const Atom*>& all) {
Michael J. Spencerb4955622012-04-02 23:56:36 +0000408 for(std::vector<const Atom*>::iterator it=all.begin(); it != all.end(); ++it){
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000409 this->addAtom(**it);
410 }
411}
412
413
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000414} // namespace lld