blob: 62361e002eb2cce7729ceac908759ee78616b5e7 [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"
14#include "lld/Core/SymbolTable.h"
15#include "lld/Core/UndefinedAtom.h"
16#include "lld/Platform/Platform.h"
17
18#include "llvm/Support/raw_ostream.h"
19
20#include <algorithm>
21#include <cassert>
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000022
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000023#include <vector>
24
25namespace lld {
26
Nick Kledzikf4fb2c52012-01-11 01:06:19 +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) { }
31
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
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000037 if ( const DefinedAtom* defAtom = atom->definedAtom() ) {
38 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 }
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000044
45private:
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
50/// 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);
96
97 // 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);
Nick Kledzikf4fb2c52012-01-11 01:06:19 +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);
140
141 // tell symbol table
142 _symbolTable.add(atom);
143}
144
145void Resolver::doAbsoluteAtom(const AbsoluteAtom& atom) {
146 // add to list of known atoms
147 _atoms.push_back(&atom);
148
149 // tell symbol table
150 _symbolTable.add(atom);
151}
152
153
154
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) {
179 llvm::StringRef undefName = (*it)->name();
180 // 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) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000198 if ( const DefinedAtom* defAtom = (*ait)->definedAtom() ) {
199 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
207 llvm::StringRef tentName = (*dit)->name();
208 const Atom *curAtom = _symbolTable.findByName(tentName);
209 assert(curAtom != NULL);
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000210 if ( const DefinedAtom* curDefAtom = curAtom->definedAtom() ) {
211 if (curDefAtom->merge() == DefinedAtom::mergeAsTentative )
212 _inputFiles.searchLibraries(tentName, searchDylibs,
213 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) {
225 if ( const DefinedAtom* defAtom = (*ait)->definedAtom() ) {
226 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
240 if ((previous->referer != NULL) && _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;
243 for (WhyLiveBackChain *p = previous; p != NULL;
244 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;
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000263 if ( const DefinedAtom* defAtom = atom.definedAtom() ) {
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();
283 if (entry != NULL)
284 _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) {
289 llvm::StringRef sym = *uit;
290 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;
304 rootChain.previous = NULL;
305 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) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000346 const DefinedAtom* defAtom = (*it)->definedAtom();
347 if ( defAtom == NULL )
348 continue;
349 if ( defAtom->merge() != DefinedAtom::mergeAsTentative )
350 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() {
361 llvm::StringRef symbolName = _platform.entryPointName();
362 if (symbolName != NULL)
363 return _symbolTable.findByName(symbolName);
364
365 return NULL;
366}
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) {
393 if ( const DefinedAtom* defAtom = atom.definedAtom() ) {
394 _definedAtoms._atoms.push_back(defAtom);
395 }
396 else if ( const UndefinedAtom* undefAtom = atom.undefinedAtom() ) {
397 _undefinedAtoms._atoms.push_back(undefAtom);
398 }
399 else if ( const SharedLibraryAtom* slAtom = atom.sharedLibraryAtom() ) {
400 _sharedLibraryAtoms._atoms.push_back(slAtom);
401 }
402 else if ( const AbsoluteAtom* abAtom = atom.absoluteAtom() ) {
403 _absoluteAtoms._atoms.push_back(abAtom);
404 }
405 else {
406 assert(0 && "atom has unknown definition kind");
407 }
408}
409
410void Resolver::MergedFile::addAtoms(std::vector<const Atom*>& all) {
411 for(std::vector<const Atom*>::iterator it=all.begin(); it != all.end(); ++it) {
412 this->addAtom(**it);
413 }
414}
415
416
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000417} // namespace lld