blob: 68f4ba5b4f82a94e0f8aedd888877227d63b6db7 [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
137// utility to add a vector of atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000138void Resolver::addAtoms(const std::vector<const DefinedAtom*>& newAtoms) {
139 for (std::vector<const DefinedAtom *>::const_iterator it = newAtoms.begin();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000140 it != newAtoms.end(); ++it) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000141 this->doDefinedAtom(**it);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000142 }
143}
144
145// ask symbol table if any definitionUndefined atoms still exist
146// if so, keep searching libraries until no more atoms being added
147void Resolver::resolveUndefines() {
148 const bool searchArchives =
149 _platform.searchArchivesToOverrideTentativeDefinitions();
150 const bool searchDylibs =
151 _platform.searchSharedLibrariesToOverrideTentativeDefinitions();
152
153 // keep looping until no more undefines were added in last loop
154 unsigned int undefineGenCount = 0xFFFFFFFF;
155 while (undefineGenCount != _symbolTable.size()) {
156 undefineGenCount = _symbolTable.size();
157 std::vector<const Atom *> undefines;
158 _symbolTable.undefines(undefines);
159 for (std::vector<const Atom *>::iterator it = undefines.begin();
160 it != undefines.end(); ++it) {
161 llvm::StringRef undefName = (*it)->name();
162 // load for previous undefine may also have loaded this undefine
163 if (!_symbolTable.isDefined(undefName)) {
164 _inputFiles.searchLibraries(undefName, true, true, false, *this);
165
166 // give platform a chance to instantiate platform
167 // specific atoms (e.g. section boundary)
168 if (!_symbolTable.isDefined(undefName)) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000169 std::vector<const DefinedAtom *> platAtoms;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000170 if (_platform.getPlatformAtoms(undefName, platAtoms))
171 this->addAtoms(platAtoms);
172 }
173 }
174 }
175 // search libraries for overrides of common symbols
176 if (searchArchives || searchDylibs) {
177 std::vector<const Atom *> tents;
178 for (std::vector<const Atom *>::iterator ait = _atoms.begin();
179 ait != _atoms.end(); ++ait) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000180 if ( const DefinedAtom* defAtom = (*ait)->definedAtom() ) {
181 if ( defAtom->merge() == DefinedAtom::mergeAsTentative )
182 tents.push_back(defAtom);
183 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000184 }
185 for (std::vector<const Atom *>::iterator dit = tents.begin();
186 dit != tents.end(); ++dit) {
187 // load for previous tentative may also have loaded
188 // this tentative, so check again
189 llvm::StringRef tentName = (*dit)->name();
190 const Atom *curAtom = _symbolTable.findByName(tentName);
191 assert(curAtom != NULL);
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000192 if ( const DefinedAtom* curDefAtom = curAtom->definedAtom() ) {
193 if (curDefAtom->merge() == DefinedAtom::mergeAsTentative )
194 _inputFiles.searchLibraries(tentName, searchDylibs,
195 true, true, *this);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000196 }
197 }
198 }
199 }
200}
201
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000202// helper to update targets for use with forEachReference()
203class ReferenceUpdater : public DefinedAtom::ReferenceHandler {
204public:
205 ReferenceUpdater(SymbolTable& sym) : _symbolTable(sym) { }
206
207 virtual void doReference(const Reference& ref) {
208 const Atom* newTarget = _symbolTable.replacement(ref.target());
209 (const_cast<Reference*>(&ref))->setTarget(newTarget);
210 }
211
212private:
213 SymbolTable& _symbolTable;
214};
215
216
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000217// switch all references to undefined or coalesced away atoms
218// to the new defined atom
219void Resolver::updateReferences() {
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000220 ReferenceUpdater updater(_symbolTable);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000221 for (std::vector<const Atom *>::iterator it = _atoms.begin();
222 it != _atoms.end(); ++it) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000223 if ( const DefinedAtom* defAtom = (*it)->definedAtom() ) {
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000224 defAtom->forEachReference(updater);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000225 }
226 }
227}
228
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000229
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000230// for dead code stripping, recursively mark atom "live"
231void Resolver::markLive(const Atom &atom, WhyLiveBackChain *previous) {
232 // if -why_live cares about this symbol, then dump chain
233 if ((previous->referer != NULL) && _platform.printWhyLive(atom.name())) {
Nick Kledzikf46669c2011-12-21 23:29:36 +0000234 llvm::errs() << atom.name() << " from " << atom.file().path() << "\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000235 int depth = 1;
236 for (WhyLiveBackChain *p = previous; p != NULL;
237 p = p->previous, ++depth) {
238 for (int i = depth; i > 0; --i)
239 llvm::errs() << " ";
240 llvm::errs() << p->referer->name() << " from "
Nick Kledzikf46669c2011-12-21 23:29:36 +0000241 << p->referer->file().path() << "\n";
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000242 }
243 }
244
245 // if already marked live, then done (stop recursion)
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000246 if ( _liveAtoms.count(&atom) )
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000247 return;
248
249 // mark this atom is live
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000250 _liveAtoms.insert(&atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000251
252 // mark all atoms it references as live
253 WhyLiveBackChain thisChain;
254 thisChain.previous = previous;
255 thisChain.referer = &atom;
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000256 if ( const DefinedAtom* defAtom = atom.definedAtom() ) {
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000257 MarkLiveReferences markRefs(*this, &thisChain);
258 defAtom->forEachReference(markRefs);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000259 }
260}
261
262// remove all atoms not actually used
263void Resolver::deadStripOptimize() {
264 // only do this optimization with -dead_strip
265 if (!_platform.deadCodeStripping())
266 return;
267
268 // clear liveness on all atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000269 _liveAtoms.clear();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000270
271 // add entry point (main) to live roots
272 const Atom *entry = this->entryPoint();
273 if (entry != NULL)
274 _deadStripRoots.insert(entry);
275
276 // add -exported_symbols_list, -init, and -u entries to live roots
277 for (Platform::UndefinesIterator uit = _platform.initialUndefinesBegin();
278 uit != _platform.initialUndefinesEnd(); ++uit) {
279 llvm::StringRef sym = *uit;
280 const Atom *symAtom = _symbolTable.findByName(sym);
281 assert(symAtom->definition() != Atom::definitionUndefined);
282 _deadStripRoots.insert(symAtom);
283 }
284
285 // add platform specific helper atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000286 std::vector<const DefinedAtom *> platRootAtoms;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000287 if (_platform.getImplicitDeadStripRoots(platRootAtoms))
288 this->addAtoms(platRootAtoms);
289
290 // mark all roots as live, and recursively all atoms they reference
291 for (std::set<const Atom *>::iterator it = _deadStripRoots.begin();
292 it != _deadStripRoots.end(); ++it) {
293 WhyLiveBackChain rootChain;
294 rootChain.previous = NULL;
295 rootChain.referer = *it;
296 this->markLive(**it, &rootChain);
297 }
298
299 // now remove all non-live atoms from _atoms
300 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000301 NotLive(_liveAtoms)), _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000302}
303
304// error out if some undefines remain
305void Resolver::checkUndefines(bool final) {
306 // when using LTO, undefines are checked after bitcode is optimized
307 if (_haveLLVMObjs && !final)
308 return;
309
310 // build vector of remaining undefined symbols
311 std::vector<const Atom *> undefinedAtoms;
312 _symbolTable.undefines(undefinedAtoms);
313 if (_platform.deadCodeStripping()) {
314 // when dead code stripping we don't care if dead atoms are undefined
315 undefinedAtoms.erase(std::remove_if(
316 undefinedAtoms.begin(), undefinedAtoms.end(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000317 NotLive(_liveAtoms)), undefinedAtoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000318 }
319
320 // let platform make error message about missing symbols
321 if (undefinedAtoms.size() != 0)
322 _platform.errorWithUndefines(undefinedAtoms, _atoms);
323}
324
325// remove from _atoms all coaleseced away atoms
326void Resolver::removeCoalescedAwayAtoms() {
327 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
328 AtomCoalescedAway(_symbolTable)), _atoms.end());
329}
330
331// check for interactions between symbols defined in this linkage unit
332// and same symbol name in linked dynamic shared libraries
333void Resolver::checkDylibSymbolCollisions() {
334 for (std::vector<const Atom *>::const_iterator it = _atoms.begin();
335 it != _atoms.end(); ++it) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000336 const DefinedAtom* defAtom = (*it)->definedAtom();
337 if ( defAtom == NULL )
338 continue;
339 if ( defAtom->merge() != DefinedAtom::mergeAsTentative )
340 continue;
341 assert(defAtom->scope() != DefinedAtom::scopeTranslationUnit);
342 // See if any shared library also has symbol which
343 // collides with the tentative definition.
344 // SymbolTable will warn if needed.
345 _inputFiles.searchLibraries(defAtom->name(), true, false, false, *this);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000346 }
347}
348
349// get "main" atom for linkage unit
350const Atom *Resolver::entryPoint() {
351 llvm::StringRef symbolName = _platform.entryPointName();
352 if (symbolName != NULL)
353 return _symbolTable.findByName(symbolName);
354
355 return NULL;
356}
357
358// give platform a chance to tweak the set of atoms
359void Resolver::tweakAtoms() {
360 _platform.postResolveTweaks(_atoms);
361}
362
363void Resolver::linkTimeOptimize() {
364 // FIX ME
365}
366
367std::vector<const Atom *> &Resolver::resolve() {
368 this->initializeState();
369 this->addInitialUndefines();
370 this->buildInitialAtomList();
371 this->resolveUndefines();
372 this->updateReferences();
373 this->deadStripOptimize();
374 this->checkUndefines(false);
375 this->removeCoalescedAwayAtoms();
376 this->checkDylibSymbolCollisions();
377 this->linkTimeOptimize();
378 this->tweakAtoms();
379 return _atoms;
380}
381
382} // namespace lld