blob: a758a8628ab3a71e1e567a1e54f99649f7776268 [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
Nick Kledzikb334be12012-04-07 01:31:00 +000010#include "lld/Core/LLVM.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000011#include "lld/Core/Resolver.h"
12#include "lld/Core/Atom.h"
13#include "lld/Core/File.h"
14#include "lld/Core/InputFiles.h"
Michael J. Spencere6203a52012-04-03 18:39:40 +000015#include "lld/Core/LLVM.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"
Nick Kledzikbb963df2012-04-18 21:55:06 +000020#include "llvm/Support/ErrorHandling.h"
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000021
22#include <algorithm>
23#include <cassert>
24#include <vector>
25
26namespace lld {
27
Michael J. Spencer765792d2012-04-03 18:40:27 +000028/// This is used as a filter function to std::remove_if to dead strip atoms.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000029class NotLive {
30public:
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000031 NotLive(const llvm::DenseSet<const Atom*>& la) : _liveAtoms(la) { }
Michael J. Spencer765792d2012-04-03 18:40:27 +000032
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000033 bool operator()(const Atom *atom) const {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000034 // don't remove if live
35 if ( _liveAtoms.count(atom) )
36 return false;
Nick Kledzik23384e82012-02-07 02:59:54 +000037 // don't remove if marked never-dead-strip
Michael J. Spencere6203a52012-04-03 18:39:40 +000038 if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(atom)) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000039 if ( defAtom->deadStrip() == DefinedAtom::deadStripNever )
40 return false;
41 }
42 // do remove this atom
43 return true;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000044 }
Michael J. Spencer765792d2012-04-03 18:40:27 +000045
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000046private:
47 const llvm::DenseSet<const Atom*> _liveAtoms;
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000048};
49
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000050
Michael J. Spencer765792d2012-04-03 18:40:27 +000051/// This is used as a filter function to std::remove_if to coalesced atoms.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000052class AtomCoalescedAway {
53public:
54 AtomCoalescedAway(SymbolTable &sym) : _symbolTable(sym) {}
55
56 bool operator()(const Atom *atom) const {
57 const Atom *rep = _symbolTable.replacement(atom);
58 return rep != atom;
59 }
60
61private:
62 SymbolTable &_symbolTable;
63};
64
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000065
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000066// add all atoms from all initial .o files
67void Resolver::buildInitialAtomList() {
68 // each input files contributes initial atoms
69 _atoms.reserve(1024);
70 _inputFiles.forEachInitialAtom(*this);
71
72 _completedInitialObjectFiles = true;
73}
74
75
76// called before the first atom in any file is added with doAtom()
77void Resolver::doFile(const File &file) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000078}
79
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000080
81void Resolver::doUndefinedAtom(const class UndefinedAtom& atom) {
82 // add to list of known atoms
83 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +000084
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000085 // tell symbol table
86 _symbolTable.add(atom);
87}
88
89
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000090// called on each atom when a file is added
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000091void Resolver::doDefinedAtom(const DefinedAtom &atom) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000092 // add to list of known atoms
93 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +000094
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000095 // non-static atoms need extra handling
Nick Kledzikf4fb2c52012-01-11 01:06:19 +000096 if (atom.scope() != DefinedAtom::scopeTranslationUnit) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000097 // tell symbol table about non-static atoms
98 _symbolTable.add(atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +000099 }
Nick Kledzikbb963df2012-04-18 21:55:06 +0000100
101 if (_options.deadCodeStripping()) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000102 // add to set of dead-strip-roots, all symbols that
103 // the compiler marks as don't strip
Nick Kledzikbb963df2012-04-18 21:55:06 +0000104 if (atom.deadStrip() == DefinedAtom::deadStripNever)
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000105 _deadStripRoots.insert(&atom);
106 }
107}
108
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000109void Resolver::doSharedLibraryAtom(const SharedLibraryAtom& atom) {
110 // add to list of known atoms
111 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000112
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000113 // tell symbol table
114 _symbolTable.add(atom);
115}
Michael J. Spencer765792d2012-04-03 18:40:27 +0000116
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000117void Resolver::doAbsoluteAtom(const AbsoluteAtom& atom) {
118 // add to list of known atoms
119 _atoms.push_back(&atom);
Michael J. Spencer765792d2012-04-03 18:40:27 +0000120
Nick Kledzik6bc04c62012-02-22 21:56:59 +0000121 // tell symbol table
122 _symbolTable.add(atom);
123}
124
125
Michael J. Spencer765792d2012-04-03 18:40:27 +0000126
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000127// utility to add a vector of atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000128void Resolver::addAtoms(const std::vector<const DefinedAtom*>& newAtoms) {
129 for (std::vector<const DefinedAtom *>::const_iterator it = newAtoms.begin();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000130 it != newAtoms.end(); ++it) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000131 this->doDefinedAtom(**it);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000132 }
133}
134
135// ask symbol table if any definitionUndefined atoms still exist
136// if so, keep searching libraries until no more atoms being added
137void Resolver::resolveUndefines() {
138 const bool searchArchives =
Nick Kledzikbb963df2012-04-18 21:55:06 +0000139 _options.searchArchivesToOverrideTentativeDefinitions();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000140 const bool searchDylibs =
Nick Kledzikbb963df2012-04-18 21:55:06 +0000141 _options.searchSharedLibrariesToOverrideTentativeDefinitions();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000142
143 // keep looping until no more undefines were added in last loop
144 unsigned int undefineGenCount = 0xFFFFFFFF;
145 while (undefineGenCount != _symbolTable.size()) {
146 undefineGenCount = _symbolTable.size();
147 std::vector<const Atom *> undefines;
148 _symbolTable.undefines(undefines);
Nick Kledzik062a98c2012-04-08 23:52:13 +0000149 for ( const Atom *undefAtom : undefines ) {
150 StringRef undefName = undefAtom->name();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000151 // load for previous undefine may also have loaded this undefine
152 if (!_symbolTable.isDefined(undefName)) {
153 _inputFiles.searchLibraries(undefName, true, true, false, *this);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000154 }
155 }
156 // search libraries for overrides of common symbols
157 if (searchArchives || searchDylibs) {
158 std::vector<const Atom *> tents;
Nick Kledzik062a98c2012-04-08 23:52:13 +0000159 for ( const Atom *tent : tents ) {
160 if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(tent)) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000161 if ( defAtom->merge() == DefinedAtom::mergeAsTentative )
162 tents.push_back(defAtom);
163 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000164 }
Nick Kledzik062a98c2012-04-08 23:52:13 +0000165 for ( const Atom *tent : tents ) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000166 // load for previous tentative may also have loaded
167 // this tentative, so check again
Nick Kledzik062a98c2012-04-08 23:52:13 +0000168 StringRef tentName = tent->name();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000169 const Atom *curAtom = _symbolTable.findByName(tentName);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000170 assert(curAtom != nullptr);
Michael J. Spencere6203a52012-04-03 18:39:40 +0000171 if (const DefinedAtom* curDefAtom = dyn_cast<DefinedAtom>(curAtom)) {
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000172 if (curDefAtom->merge() == DefinedAtom::mergeAsTentative )
Michael J. Spencer765792d2012-04-03 18:40:27 +0000173 _inputFiles.searchLibraries(tentName, searchDylibs,
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000174 true, true, *this);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000175 }
176 }
177 }
178 }
179}
180
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000181
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000182// switch all references to undefined or coalesced away atoms
183// to the new defined atom
184void Resolver::updateReferences() {
Nick Kledzik062a98c2012-04-08 23:52:13 +0000185 for(const Atom *atom : _atoms) {
186 if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(atom)) {
187 for (const Reference *ref : *defAtom) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000188 const Atom* newTarget = _symbolTable.replacement(ref->target());
189 (const_cast<Reference*>(ref))->setTarget(newTarget);
190 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000191 }
192 }
193}
194
Nick Kledzik49d6cc82012-02-15 00:38:09 +0000195
Nick Kledzikbb963df2012-04-18 21:55:06 +0000196// for dead code stripping, recursively mark atoms "live"
197void Resolver::markLive(const Atom &atom) {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000198 // if already marked live, then done (stop recursion)
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000199 if ( _liveAtoms.count(&atom) )
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000200 return;
201
202 // mark this atom is live
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000203 _liveAtoms.insert(&atom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000204
205 // mark all atoms it references as live
Michael J. Spencere6203a52012-04-03 18:39:40 +0000206 if ( const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(&atom)) {
Nick Kledzik062a98c2012-04-08 23:52:13 +0000207 for (const Reference *ref : *defAtom) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000208 const Atom *target = ref->target();
209 if ( target != nullptr )
210 this->markLive(*target);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000211 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000212 }
213}
214
Nick Kledzikbb963df2012-04-18 21:55:06 +0000215
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000216// remove all atoms not actually used
217void Resolver::deadStripOptimize() {
218 // only do this optimization with -dead_strip
Nick Kledzikbb963df2012-04-18 21:55:06 +0000219 if (!_options.deadCodeStripping())
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000220 return;
221
222 // clear liveness on all atoms
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000223 _liveAtoms.clear();
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000224
Nick Kledzikbb963df2012-04-18 21:55:06 +0000225 // By default, shared libraries are built with all globals as dead strip roots
226 if ( _options.allGlobalsAreDeadStripRoots() ) {
227 for ( const Atom *atom : _atoms ) {
228 const DefinedAtom *defAtom = dyn_cast<DefinedAtom>(atom);
229 if (defAtom == nullptr)
230 continue;
231 if ( defAtom->scope() == DefinedAtom::scopeGlobal )
232 _deadStripRoots.insert(defAtom);
233 }
234 }
235
236 // Or, use list of names that are dead stip roots.
237 const std::vector<StringRef> &names = _options.deadStripRootNames();
238 for ( const StringRef &name : names ) {
239 const Atom *symAtom = _symbolTable.findByName(name);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000240 assert(symAtom->definition() != Atom::definitionUndefined);
241 _deadStripRoots.insert(symAtom);
242 }
243
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000244 // mark all roots as live, and recursively all atoms they reference
Nick Kledzik062a98c2012-04-08 23:52:13 +0000245 for ( const Atom *dsrAtom : _deadStripRoots) {
Nick Kledzikbb963df2012-04-18 21:55:06 +0000246 this->markLive(*dsrAtom);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000247 }
248
249 // now remove all non-live atoms from _atoms
250 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000251 NotLive(_liveAtoms)), _atoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000252}
253
Nick Kledzikbb963df2012-04-18 21:55:06 +0000254
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000255// error out if some undefines remain
256void Resolver::checkUndefines(bool final) {
257 // when using LTO, undefines are checked after bitcode is optimized
258 if (_haveLLVMObjs && !final)
259 return;
260
261 // build vector of remaining undefined symbols
262 std::vector<const Atom *> undefinedAtoms;
263 _symbolTable.undefines(undefinedAtoms);
Nick Kledzikbb963df2012-04-18 21:55:06 +0000264 if (_options.deadCodeStripping()) {
265 // When dead code stripping, we don't care if dead atoms are undefined.
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000266 undefinedAtoms.erase(std::remove_if(
267 undefinedAtoms.begin(), undefinedAtoms.end(),
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000268 NotLive(_liveAtoms)), undefinedAtoms.end());
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000269 }
270
Nick Kledzikbb963df2012-04-18 21:55:06 +0000271 // error message about missing symbols
272 if ( (undefinedAtoms.size() != 0) && _options.undefinesAreErrors() ) {
273 // FIXME: need diagonstics interface for writing error messages
274 llvm::errs() << "Undefined symbols:\n";
275 for ( const Atom *undefAtom : undefinedAtoms ) {
276 llvm::errs() << " " << undefAtom->name() << "\n";
277 }
278 llvm::report_fatal_error("symbol(s) not found");
279 }
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000280}
281
Nick Kledzikbb963df2012-04-18 21:55:06 +0000282
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000283// remove from _atoms all coaleseced away atoms
284void Resolver::removeCoalescedAwayAtoms() {
285 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(),
286 AtomCoalescedAway(_symbolTable)), _atoms.end());
287}
288
289// check for interactions between symbols defined in this linkage unit
290// and same symbol name in linked dynamic shared libraries
291void Resolver::checkDylibSymbolCollisions() {
Nick Kledzik062a98c2012-04-08 23:52:13 +0000292 for ( const Atom *atom : _atoms ) {
293 const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(atom);
Michael J. Spencerc9d25062012-03-29 19:39:14 +0000294 if (defAtom == nullptr)
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000295 continue;
Michael J. Spencer765792d2012-04-03 18:40:27 +0000296 if ( defAtom->merge() != DefinedAtom::mergeAsTentative )
Nick Kledzikf4fb2c52012-01-11 01:06:19 +0000297 continue;
298 assert(defAtom->scope() != DefinedAtom::scopeTranslationUnit);
299 // See if any shared library also has symbol which
300 // collides with the tentative definition.
301 // SymbolTable will warn if needed.
302 _inputFiles.searchLibraries(defAtom->name(), true, false, false, *this);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000303 }
304}
305
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000306
307void Resolver::linkTimeOptimize() {
308 // FIX ME
309}
310
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000311void Resolver::resolve() {
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000312 this->buildInitialAtomList();
313 this->resolveUndefines();
314 this->updateReferences();
315 this->deadStripOptimize();
316 this->checkUndefines(false);
317 this->removeCoalescedAwayAtoms();
318 this->checkDylibSymbolCollisions();
319 this->linkTimeOptimize();
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000320 this->_result.addAtoms(_atoms);
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000321}
322
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000323void Resolver::MergedFile::addAtom(const Atom& atom) {
Michael J. Spencere6203a52012-04-03 18:39:40 +0000324 if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(&atom)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000325 _definedAtoms._atoms.push_back(defAtom);
Michael J. Spencere6203a52012-04-03 18:39:40 +0000326 } else if (const UndefinedAtom* undefAtom = dyn_cast<UndefinedAtom>(&atom)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000327 _undefinedAtoms._atoms.push_back(undefAtom);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000328 } else if (const SharedLibraryAtom* slAtom =
Michael J. Spencere6203a52012-04-03 18:39:40 +0000329 dyn_cast<SharedLibraryAtom>(&atom)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000330 _sharedLibraryAtoms._atoms.push_back(slAtom);
Michael J. Spencere6203a52012-04-03 18:39:40 +0000331 } else if (const AbsoluteAtom* abAtom = dyn_cast<AbsoluteAtom>(&atom)) {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000332 _absoluteAtoms._atoms.push_back(abAtom);
Michael J. Spencerb4955622012-04-02 23:56:36 +0000333 } else {
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000334 assert(0 && "atom has unknown definition kind");
335 }
336}
337
338void Resolver::MergedFile::addAtoms(std::vector<const Atom*>& all) {
Nick Kledzik062a98c2012-04-08 23:52:13 +0000339 for ( const Atom *atom : all ) {
340 this->addAtom(*atom);
Nick Kledzik1a6615d2012-03-08 00:18:30 +0000341 }
342}
343
344
Michael J. Spencer773a8fb2011-12-18 08:27:59 +0000345} // namespace lld