Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 1 | //===- 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 Kledzik | b334be1 | 2012-04-07 01:31:00 +0000 | [diff] [blame] | 10 | #include "lld/Core/LLVM.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 11 | #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. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 15 | #include "lld/Core/LLVM.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 16 | #include "lld/Core/SymbolTable.h" |
| 17 | #include "lld/Core/UndefinedAtom.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 18 | |
| 19 | #include "llvm/Support/raw_ostream.h" |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 20 | #include "llvm/Support/ErrorHandling.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 21 | |
| 22 | #include <algorithm> |
| 23 | #include <cassert> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace lld { |
| 27 | |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 28 | /// This is used as a filter function to std::remove_if to dead strip atoms. |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 29 | class NotLive { |
| 30 | public: |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 31 | NotLive(const llvm::DenseSet<const Atom*>& la) : _liveAtoms(la) { } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 32 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 33 | bool operator()(const Atom *atom) const { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 34 | // don't remove if live |
| 35 | if ( _liveAtoms.count(atom) ) |
| 36 | return false; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 37 | // don't remove if marked never-dead-strip |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 38 | if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(atom)) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 39 | if ( defAtom->deadStrip() == DefinedAtom::deadStripNever ) |
| 40 | return false; |
| 41 | } |
| 42 | // do remove this atom |
| 43 | return true; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 44 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 45 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 46 | private: |
| 47 | const llvm::DenseSet<const Atom*> _liveAtoms; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 48 | }; |
| 49 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 50 | |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 51 | /// This is used as a filter function to std::remove_if to coalesced atoms. |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 52 | class AtomCoalescedAway { |
| 53 | public: |
| 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 | |
| 61 | private: |
| 62 | SymbolTable &_symbolTable; |
| 63 | }; |
| 64 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 65 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 66 | // add all atoms from all initial .o files |
| 67 | void 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() |
| 77 | void Resolver::doFile(const File &file) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 80 | |
| 81 | void Resolver::doUndefinedAtom(const class UndefinedAtom& atom) { |
| 82 | // add to list of known atoms |
| 83 | _atoms.push_back(&atom); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 84 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 85 | // tell symbol table |
| 86 | _symbolTable.add(atom); |
| 87 | } |
| 88 | |
| 89 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 90 | // called on each atom when a file is added |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 91 | void Resolver::doDefinedAtom(const DefinedAtom &atom) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 92 | // add to list of known atoms |
| 93 | _atoms.push_back(&atom); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 94 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 95 | // non-static atoms need extra handling |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 96 | if (atom.scope() != DefinedAtom::scopeTranslationUnit) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 97 | // tell symbol table about non-static atoms |
| 98 | _symbolTable.add(atom); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 99 | } |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 100 | |
| 101 | if (_options.deadCodeStripping()) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 102 | // add to set of dead-strip-roots, all symbols that |
| 103 | // the compiler marks as don't strip |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 104 | if (atom.deadStrip() == DefinedAtom::deadStripNever) |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 105 | _deadStripRoots.insert(&atom); |
| 106 | } |
| 107 | } |
| 108 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 109 | void Resolver::doSharedLibraryAtom(const SharedLibraryAtom& atom) { |
| 110 | // add to list of known atoms |
| 111 | _atoms.push_back(&atom); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 112 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 113 | // tell symbol table |
| 114 | _symbolTable.add(atom); |
| 115 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 116 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 117 | void Resolver::doAbsoluteAtom(const AbsoluteAtom& atom) { |
| 118 | // add to list of known atoms |
| 119 | _atoms.push_back(&atom); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 120 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 121 | // tell symbol table |
| 122 | _symbolTable.add(atom); |
| 123 | } |
| 124 | |
| 125 | |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 126 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 127 | // utility to add a vector of atoms |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 128 | void Resolver::addAtoms(const std::vector<const DefinedAtom*>& newAtoms) { |
| 129 | for (std::vector<const DefinedAtom *>::const_iterator it = newAtoms.begin(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 130 | it != newAtoms.end(); ++it) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 131 | this->doDefinedAtom(**it); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | |
| 135 | // ask symbol table if any definitionUndefined atoms still exist |
| 136 | // if so, keep searching libraries until no more atoms being added |
| 137 | void Resolver::resolveUndefines() { |
| 138 | const bool searchArchives = |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 139 | _options.searchArchivesToOverrideTentativeDefinitions(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 140 | const bool searchDylibs = |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 141 | _options.searchSharedLibrariesToOverrideTentativeDefinitions(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 142 | |
| 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 Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 149 | for ( const Atom *undefAtom : undefines ) { |
| 150 | StringRef undefName = undefAtom->name(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 151 | // 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. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | // search libraries for overrides of common symbols |
| 157 | if (searchArchives || searchDylibs) { |
| 158 | std::vector<const Atom *> tents; |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 159 | for ( const Atom *tent : tents ) { |
| 160 | if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(tent)) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 161 | if ( defAtom->merge() == DefinedAtom::mergeAsTentative ) |
| 162 | tents.push_back(defAtom); |
| 163 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 164 | } |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 165 | for ( const Atom *tent : tents ) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 166 | // load for previous tentative may also have loaded |
| 167 | // this tentative, so check again |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 168 | StringRef tentName = tent->name(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 169 | const Atom *curAtom = _symbolTable.findByName(tentName); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 170 | assert(curAtom != nullptr); |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 171 | if (const DefinedAtom* curDefAtom = dyn_cast<DefinedAtom>(curAtom)) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 172 | if (curDefAtom->merge() == DefinedAtom::mergeAsTentative ) |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 173 | _inputFiles.searchLibraries(tentName, searchDylibs, |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 174 | true, true, *this); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 181 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 182 | // switch all references to undefined or coalesced away atoms |
| 183 | // to the new defined atom |
| 184 | void Resolver::updateReferences() { |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 185 | for(const Atom *atom : _atoms) { |
| 186 | if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(atom)) { |
| 187 | for (const Reference *ref : *defAtom) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 188 | const Atom* newTarget = _symbolTable.replacement(ref->target()); |
| 189 | (const_cast<Reference*>(ref))->setTarget(newTarget); |
| 190 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 195 | |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 196 | // for dead code stripping, recursively mark atoms "live" |
| 197 | void Resolver::markLive(const Atom &atom) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 198 | // if already marked live, then done (stop recursion) |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 199 | if ( _liveAtoms.count(&atom) ) |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 200 | return; |
| 201 | |
| 202 | // mark this atom is live |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 203 | _liveAtoms.insert(&atom); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 204 | |
| 205 | // mark all atoms it references as live |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 206 | if ( const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(&atom)) { |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 207 | for (const Reference *ref : *defAtom) { |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 208 | const Atom *target = ref->target(); |
| 209 | if ( target != nullptr ) |
| 210 | this->markLive(*target); |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 211 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 215 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 216 | // remove all atoms not actually used |
| 217 | void Resolver::deadStripOptimize() { |
| 218 | // only do this optimization with -dead_strip |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 219 | if (!_options.deadCodeStripping()) |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 220 | return; |
| 221 | |
| 222 | // clear liveness on all atoms |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 223 | _liveAtoms.clear(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 224 | |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 225 | // 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. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 240 | assert(symAtom->definition() != Atom::definitionUndefined); |
| 241 | _deadStripRoots.insert(symAtom); |
| 242 | } |
| 243 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 244 | // mark all roots as live, and recursively all atoms they reference |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 245 | for ( const Atom *dsrAtom : _deadStripRoots) { |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 246 | this->markLive(*dsrAtom); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // now remove all non-live atoms from _atoms |
| 250 | _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 251 | NotLive(_liveAtoms)), _atoms.end()); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 254 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 255 | // error out if some undefines remain |
| 256 | void 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 Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 264 | if (_options.deadCodeStripping()) { |
| 265 | // When dead code stripping, we don't care if dead atoms are undefined. |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 266 | undefinedAtoms.erase(std::remove_if( |
| 267 | undefinedAtoms.begin(), undefinedAtoms.end(), |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 268 | NotLive(_liveAtoms)), undefinedAtoms.end()); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 271 | // 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. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Nick Kledzik | bb963df | 2012-04-18 21:55:06 +0000 | [diff] [blame^] | 282 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 283 | // remove from _atoms all coaleseced away atoms |
| 284 | void 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 |
| 291 | void Resolver::checkDylibSymbolCollisions() { |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 292 | for ( const Atom *atom : _atoms ) { |
| 293 | const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(atom); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 294 | if (defAtom == nullptr) |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 295 | continue; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 296 | if ( defAtom->merge() != DefinedAtom::mergeAsTentative ) |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 297 | 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. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 306 | |
| 307 | void Resolver::linkTimeOptimize() { |
| 308 | // FIX ME |
| 309 | } |
| 310 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 311 | void Resolver::resolve() { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 312 | 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 Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 320 | this->_result.addAtoms(_atoms); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 323 | void Resolver::MergedFile::addAtom(const Atom& atom) { |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 324 | if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(&atom)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 325 | _definedAtoms._atoms.push_back(defAtom); |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 326 | } else if (const UndefinedAtom* undefAtom = dyn_cast<UndefinedAtom>(&atom)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 327 | _undefinedAtoms._atoms.push_back(undefAtom); |
Michael J. Spencer | b495562 | 2012-04-02 23:56:36 +0000 | [diff] [blame] | 328 | } else if (const SharedLibraryAtom* slAtom = |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 329 | dyn_cast<SharedLibraryAtom>(&atom)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 330 | _sharedLibraryAtoms._atoms.push_back(slAtom); |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 331 | } else if (const AbsoluteAtom* abAtom = dyn_cast<AbsoluteAtom>(&atom)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 332 | _absoluteAtoms._atoms.push_back(abAtom); |
Michael J. Spencer | b495562 | 2012-04-02 23:56:36 +0000 | [diff] [blame] | 333 | } else { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 334 | assert(0 && "atom has unknown definition kind"); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | void Resolver::MergedFile::addAtoms(std::vector<const Atom*>& all) { |
Nick Kledzik | 062a98c | 2012-04-08 23:52:13 +0000 | [diff] [blame] | 339 | for ( const Atom *atom : all ) { |
| 340 | this->addAtom(*atom); |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 345 | } // namespace lld |