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 | |
| 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. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 14 | #include "lld/Core/LLVM.h" |
Michael J. Spencer | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame] | 15 | #include "lld/Core/Platform.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" |
| 20 | |
| 21 | #include <algorithm> |
| 22 | #include <cassert> |
| 23 | #include <vector> |
| 24 | |
| 25 | namespace lld { |
| 26 | |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 27 | /// 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] | 28 | class NotLive { |
| 29 | public: |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 30 | NotLive(const llvm::DenseSet<const Atom*>& la) : _liveAtoms(la) { } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 31 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 32 | bool operator()(const Atom *atom) const { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 33 | // don't remove if live |
| 34 | if ( _liveAtoms.count(atom) ) |
| 35 | return false; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 36 | // don't remove if marked never-dead-strip |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 37 | if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(atom)) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 38 | if ( defAtom->deadStrip() == DefinedAtom::deadStripNever ) |
| 39 | return false; |
| 40 | } |
| 41 | // do remove this atom |
| 42 | return true; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 43 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 44 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 45 | private: |
| 46 | const llvm::DenseSet<const Atom*> _liveAtoms; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 49 | |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 50 | /// 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] | 51 | class AtomCoalescedAway { |
| 52 | public: |
| 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 | |
| 60 | private: |
| 61 | SymbolTable &_symbolTable; |
| 62 | }; |
| 63 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 64 | |
| 65 | |
| 66 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 67 | void Resolver::initializeState() { |
| 68 | _platform.initialize(); |
| 69 | } |
| 70 | |
| 71 | // add initial undefines from -u option |
| 72 | void Resolver::addInitialUndefines() { |
| 73 | |
| 74 | } |
| 75 | |
| 76 | // add all atoms from all initial .o files |
| 77 | void 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() |
| 87 | void Resolver::doFile(const File &file) { |
| 88 | // notify platform |
| 89 | _platform.fileAdded(file); |
| 90 | } |
| 91 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 92 | |
| 93 | void Resolver::doUndefinedAtom(const class UndefinedAtom& atom) { |
| 94 | // add to list of known atoms |
| 95 | _atoms.push_back(&atom); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 96 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 97 | // tell symbol table |
| 98 | _symbolTable.add(atom); |
| 99 | } |
| 100 | |
| 101 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 102 | // called on each atom when a file is added |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 103 | void Resolver::doDefinedAtom(const DefinedAtom &atom) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 104 | // notify platform |
| 105 | _platform.atomAdded(atom); |
| 106 | |
| 107 | // add to list of known atoms |
| 108 | _atoms.push_back(&atom); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 109 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 110 | // adjust scope (e.g. force some globals to be hidden) |
| 111 | _platform.adjustScope(atom); |
| 112 | |
| 113 | // non-static atoms need extra handling |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 114 | if (atom.scope() != DefinedAtom::scopeTranslationUnit) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 115 | // tell symbol table about non-static atoms |
| 116 | _symbolTable.add(atom); |
| 117 | |
| 118 | // platform can add aliases for any symbol |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 119 | std::vector<const DefinedAtom *> aliases; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 120 | 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 Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 137 | void Resolver::doSharedLibraryAtom(const SharedLibraryAtom& atom) { |
| 138 | // add to list of known atoms |
| 139 | _atoms.push_back(&atom); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 140 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 141 | // tell symbol table |
| 142 | _symbolTable.add(atom); |
| 143 | } |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 144 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 145 | void Resolver::doAbsoluteAtom(const AbsoluteAtom& atom) { |
| 146 | // add to list of known atoms |
| 147 | _atoms.push_back(&atom); |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 148 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 149 | // tell symbol table |
| 150 | _symbolTable.add(atom); |
| 151 | } |
| 152 | |
| 153 | |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 154 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 155 | // utility to add a vector of atoms |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 156 | void Resolver::addAtoms(const std::vector<const DefinedAtom*>& newAtoms) { |
| 157 | for (std::vector<const DefinedAtom *>::const_iterator it = newAtoms.begin(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 158 | it != newAtoms.end(); ++it) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 159 | this->doDefinedAtom(**it); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| 163 | // ask symbol table if any definitionUndefined atoms still exist |
| 164 | // if so, keep searching libraries until no more atoms being added |
| 165 | void 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. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 179 | StringRef undefName = (*it)->name(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 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 Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 187 | std::vector<const DefinedAtom *> platAtoms; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 188 | 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. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 198 | if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(*ait)) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 199 | if ( defAtom->merge() == DefinedAtom::mergeAsTentative ) |
| 200 | tents.push_back(defAtom); |
| 201 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 202 | } |
| 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. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 207 | StringRef tentName = (*dit)->name(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 208 | const Atom *curAtom = _symbolTable.findByName(tentName); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 209 | assert(curAtom != nullptr); |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 210 | if (const DefinedAtom* curDefAtom = dyn_cast<DefinedAtom>(curAtom)) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 211 | if (curDefAtom->merge() == DefinedAtom::mergeAsTentative ) |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 212 | _inputFiles.searchLibraries(tentName, searchDylibs, |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 213 | true, true, *this); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 220 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 221 | // switch all references to undefined or coalesced away atoms |
| 222 | // to the new defined atom |
| 223 | void Resolver::updateReferences() { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 224 | for (auto ait = _atoms.begin(); ait != _atoms.end(); ++ait) { |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 225 | if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(*ait)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 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. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 236 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 237 | // for dead code stripping, recursively mark atom "live" |
| 238 | void Resolver::markLive(const Atom &atom, WhyLiveBackChain *previous) { |
| 239 | // if -why_live cares about this symbol, then dump chain |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 240 | if ((previous->referer != nullptr) && _platform.printWhyLive(atom.name())) { |
Nick Kledzik | f46669c | 2011-12-21 23:29:36 +0000 | [diff] [blame] | 241 | llvm::errs() << atom.name() << " from " << atom.file().path() << "\n"; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 242 | int depth = 1; |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 243 | for (WhyLiveBackChain *p = previous; p != nullptr; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 244 | p = p->previous, ++depth) { |
| 245 | for (int i = depth; i > 0; --i) |
| 246 | llvm::errs() << " "; |
| 247 | llvm::errs() << p->referer->name() << " from " |
Nick Kledzik | f46669c | 2011-12-21 23:29:36 +0000 | [diff] [blame] | 248 | << p->referer->file().path() << "\n"; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
| 252 | // if already marked live, then done (stop recursion) |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 253 | if ( _liveAtoms.count(&atom) ) |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 254 | return; |
| 255 | |
| 256 | // mark this atom is live |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 257 | _liveAtoms.insert(&atom); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 258 | |
| 259 | // mark all atoms it references as live |
| 260 | WhyLiveBackChain thisChain; |
| 261 | thisChain.previous = previous; |
| 262 | thisChain.referer = &atom; |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 263 | if ( const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(&atom)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 264 | 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. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | // remove all atoms not actually used |
| 273 | void Resolver::deadStripOptimize() { |
| 274 | // only do this optimization with -dead_strip |
| 275 | if (!_platform.deadCodeStripping()) |
| 276 | return; |
| 277 | |
| 278 | // clear liveness on all atoms |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 279 | _liveAtoms.clear(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 280 | |
| 281 | // add entry point (main) to live roots |
| 282 | const Atom *entry = this->entryPoint(); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 283 | if (entry != nullptr) |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 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) { |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 289 | StringRef sym = *uit; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 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 Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 296 | std::vector<const DefinedAtom *> platRootAtoms; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 297 | 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. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 304 | rootChain.previous = nullptr; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 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 Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 311 | NotLive(_liveAtoms)), _atoms.end()); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | // error out if some undefines remain |
| 315 | void 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 Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 327 | NotLive(_liveAtoms)), undefinedAtoms.end()); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 328 | } |
| 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 |
| 336 | void 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 |
| 343 | void Resolver::checkDylibSymbolCollisions() { |
| 344 | for (std::vector<const Atom *>::const_iterator it = _atoms.begin(); |
| 345 | it != _atoms.end(); ++it) { |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 346 | const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(*it); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 347 | if (defAtom == nullptr) |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 348 | continue; |
Michael J. Spencer | 765792d | 2012-04-03 18:40:27 +0000 | [diff] [blame] | 349 | if ( defAtom->merge() != DefinedAtom::mergeAsTentative ) |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 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. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | // get "main" atom for linkage unit |
| 360 | const Atom *Resolver::entryPoint() { |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 361 | StringRef symbolName = _platform.entryPointName(); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 362 | if (symbolName != nullptr) |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 363 | return _symbolTable.findByName(symbolName); |
| 364 | |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame] | 365 | return nullptr; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | // give platform a chance to tweak the set of atoms |
| 369 | void Resolver::tweakAtoms() { |
| 370 | _platform.postResolveTweaks(_atoms); |
| 371 | } |
| 372 | |
| 373 | void Resolver::linkTimeOptimize() { |
| 374 | // FIX ME |
| 375 | } |
| 376 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 377 | void Resolver::resolve() { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 378 | 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 Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 389 | this->_result.addAtoms(_atoms); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 392 | void Resolver::MergedFile::addAtom(const Atom& atom) { |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 393 | if (const DefinedAtom* defAtom = dyn_cast<DefinedAtom>(&atom)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 394 | _definedAtoms._atoms.push_back(defAtom); |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 395 | } else if (const UndefinedAtom* undefAtom = dyn_cast<UndefinedAtom>(&atom)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 396 | _undefinedAtoms._atoms.push_back(undefAtom); |
Michael J. Spencer | b495562 | 2012-04-02 23:56:36 +0000 | [diff] [blame] | 397 | } else if (const SharedLibraryAtom* slAtom = |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 398 | dyn_cast<SharedLibraryAtom>(&atom)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 399 | _sharedLibraryAtoms._atoms.push_back(slAtom); |
Michael J. Spencer | e6203a5 | 2012-04-03 18:39:40 +0000 | [diff] [blame] | 400 | } else if (const AbsoluteAtom* abAtom = dyn_cast<AbsoluteAtom>(&atom)) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 401 | _absoluteAtoms._atoms.push_back(abAtom); |
Michael J. Spencer | b495562 | 2012-04-02 23:56:36 +0000 | [diff] [blame] | 402 | } else { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 403 | assert(0 && "atom has unknown definition kind"); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | void Resolver::MergedFile::addAtoms(std::vector<const Atom*>& all) { |
Michael J. Spencer | b495562 | 2012-04-02 23:56:36 +0000 | [diff] [blame] | 408 | for(std::vector<const Atom*>::iterator it=all.begin(); it != all.end(); ++it){ |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 409 | this->addAtom(**it); |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 414 | } // namespace lld |