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 | cfd029f | 2012-03-28 19:04:02 +0000 | [diff] [blame] | 14 | #include "lld/Core/Platform.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 15 | #include "lld/Core/SymbolTable.h" |
| 16 | #include "lld/Core/UndefinedAtom.h" |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 17 | |
| 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | |
| 20 | #include <algorithm> |
| 21 | #include <cassert> |
| 22 | #include <vector> |
| 23 | |
| 24 | namespace lld { |
| 25 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 26 | /// 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] | 27 | class NotLive { |
| 28 | public: |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 29 | NotLive(const llvm::DenseSet<const Atom*>& la) : _liveAtoms(la) { } |
| 30 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 31 | bool operator()(const Atom *atom) const { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 32 | // don't remove if live |
| 33 | if ( _liveAtoms.count(atom) ) |
| 34 | return false; |
Nick Kledzik | 23384e8 | 2012-02-07 02:59:54 +0000 | [diff] [blame] | 35 | // don't remove if marked never-dead-strip |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 36 | if ( const DefinedAtom* defAtom = atom->definedAtom() ) { |
| 37 | if ( defAtom->deadStrip() == DefinedAtom::deadStripNever ) |
| 38 | return false; |
| 39 | } |
| 40 | // do remove this atom |
| 41 | return true; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 42 | } |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 43 | |
| 44 | private: |
| 45 | const llvm::DenseSet<const Atom*> _liveAtoms; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 48 | |
| 49 | /// 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] | 50 | class AtomCoalescedAway { |
| 51 | public: |
| 52 | AtomCoalescedAway(SymbolTable &sym) : _symbolTable(sym) {} |
| 53 | |
| 54 | bool operator()(const Atom *atom) const { |
| 55 | const Atom *rep = _symbolTable.replacement(atom); |
| 56 | return rep != atom; |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | SymbolTable &_symbolTable; |
| 61 | }; |
| 62 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 66 | void Resolver::initializeState() { |
| 67 | _platform.initialize(); |
| 68 | } |
| 69 | |
| 70 | // add initial undefines from -u option |
| 71 | void Resolver::addInitialUndefines() { |
| 72 | |
| 73 | } |
| 74 | |
| 75 | // add all atoms from all initial .o files |
| 76 | void Resolver::buildInitialAtomList() { |
| 77 | // each input files contributes initial atoms |
| 78 | _atoms.reserve(1024); |
| 79 | _inputFiles.forEachInitialAtom(*this); |
| 80 | |
| 81 | _completedInitialObjectFiles = true; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | // called before the first atom in any file is added with doAtom() |
| 86 | void Resolver::doFile(const File &file) { |
| 87 | // notify platform |
| 88 | _platform.fileAdded(file); |
| 89 | } |
| 90 | |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 91 | |
| 92 | void Resolver::doUndefinedAtom(const class UndefinedAtom& atom) { |
| 93 | // add to list of known atoms |
| 94 | _atoms.push_back(&atom); |
| 95 | |
| 96 | // tell symbol table |
| 97 | _symbolTable.add(atom); |
| 98 | } |
| 99 | |
| 100 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 101 | // called on each atom when a file is added |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 102 | void Resolver::doDefinedAtom(const DefinedAtom &atom) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 103 | // notify platform |
| 104 | _platform.atomAdded(atom); |
| 105 | |
| 106 | // add to list of known atoms |
| 107 | _atoms.push_back(&atom); |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 108 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 109 | // adjust scope (e.g. force some globals to be hidden) |
| 110 | _platform.adjustScope(atom); |
| 111 | |
| 112 | // non-static atoms need extra handling |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 113 | if (atom.scope() != DefinedAtom::scopeTranslationUnit) { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 114 | // tell symbol table about non-static atoms |
| 115 | _symbolTable.add(atom); |
| 116 | |
| 117 | // platform can add aliases for any symbol |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 118 | std::vector<const DefinedAtom *> aliases; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 119 | if (_platform.getAliasAtoms(atom, aliases)) |
| 120 | this->addAtoms(aliases); |
| 121 | } |
| 122 | |
| 123 | if (_platform.deadCodeStripping()) { |
| 124 | // add to set of dead-strip-roots, all symbols that |
| 125 | // the compiler marks as don't strip |
| 126 | if (!atom.deadStrip()) |
| 127 | _deadStripRoots.insert(&atom); |
| 128 | |
| 129 | // add to set of dead-strip-roots, all symbols that |
| 130 | // the platform decided must remain |
| 131 | if (_platform.isDeadStripRoot(atom)) |
| 132 | _deadStripRoots.insert(&atom); |
| 133 | } |
| 134 | } |
| 135 | |
Nick Kledzik | 6bc04c6 | 2012-02-22 21:56:59 +0000 | [diff] [blame] | 136 | void Resolver::doSharedLibraryAtom(const SharedLibraryAtom& atom) { |
| 137 | // add to list of known atoms |
| 138 | _atoms.push_back(&atom); |
| 139 | |
| 140 | // tell symbol table |
| 141 | _symbolTable.add(atom); |
| 142 | } |
| 143 | |
| 144 | void Resolver::doAbsoluteAtom(const AbsoluteAtom& atom) { |
| 145 | // add to list of known atoms |
| 146 | _atoms.push_back(&atom); |
| 147 | |
| 148 | // tell symbol table |
| 149 | _symbolTable.add(atom); |
| 150 | } |
| 151 | |
| 152 | |
| 153 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 154 | // utility to add a vector of atoms |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 155 | void Resolver::addAtoms(const std::vector<const DefinedAtom*>& newAtoms) { |
| 156 | for (std::vector<const DefinedAtom *>::const_iterator it = newAtoms.begin(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 157 | it != newAtoms.end(); ++it) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 158 | this->doDefinedAtom(**it); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | // ask symbol table if any definitionUndefined atoms still exist |
| 163 | // if so, keep searching libraries until no more atoms being added |
| 164 | void Resolver::resolveUndefines() { |
| 165 | const bool searchArchives = |
| 166 | _platform.searchArchivesToOverrideTentativeDefinitions(); |
| 167 | const bool searchDylibs = |
| 168 | _platform.searchSharedLibrariesToOverrideTentativeDefinitions(); |
| 169 | |
| 170 | // keep looping until no more undefines were added in last loop |
| 171 | unsigned int undefineGenCount = 0xFFFFFFFF; |
| 172 | while (undefineGenCount != _symbolTable.size()) { |
| 173 | undefineGenCount = _symbolTable.size(); |
| 174 | std::vector<const Atom *> undefines; |
| 175 | _symbolTable.undefines(undefines); |
| 176 | for (std::vector<const Atom *>::iterator it = undefines.begin(); |
| 177 | it != undefines.end(); ++it) { |
| 178 | llvm::StringRef undefName = (*it)->name(); |
| 179 | // load for previous undefine may also have loaded this undefine |
| 180 | if (!_symbolTable.isDefined(undefName)) { |
| 181 | _inputFiles.searchLibraries(undefName, true, true, false, *this); |
| 182 | |
| 183 | // give platform a chance to instantiate platform |
| 184 | // specific atoms (e.g. section boundary) |
| 185 | if (!_symbolTable.isDefined(undefName)) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 186 | std::vector<const DefinedAtom *> platAtoms; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 187 | if (_platform.getPlatformAtoms(undefName, platAtoms)) |
| 188 | this->addAtoms(platAtoms); |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | // search libraries for overrides of common symbols |
| 193 | if (searchArchives || searchDylibs) { |
| 194 | std::vector<const Atom *> tents; |
| 195 | for (std::vector<const Atom *>::iterator ait = _atoms.begin(); |
| 196 | ait != _atoms.end(); ++ait) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 197 | if ( const DefinedAtom* defAtom = (*ait)->definedAtom() ) { |
| 198 | if ( defAtom->merge() == DefinedAtom::mergeAsTentative ) |
| 199 | tents.push_back(defAtom); |
| 200 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 201 | } |
| 202 | for (std::vector<const Atom *>::iterator dit = tents.begin(); |
| 203 | dit != tents.end(); ++dit) { |
| 204 | // load for previous tentative may also have loaded |
| 205 | // this tentative, so check again |
| 206 | llvm::StringRef tentName = (*dit)->name(); |
| 207 | const Atom *curAtom = _symbolTable.findByName(tentName); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame^] | 208 | assert(curAtom != nullptr); |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 209 | if ( const DefinedAtom* curDefAtom = curAtom->definedAtom() ) { |
| 210 | if (curDefAtom->merge() == DefinedAtom::mergeAsTentative ) |
| 211 | _inputFiles.searchLibraries(tentName, searchDylibs, |
| 212 | true, true, *this); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 219 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 220 | // switch all references to undefined or coalesced away atoms |
| 221 | // to the new defined atom |
| 222 | void Resolver::updateReferences() { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 223 | for (auto ait = _atoms.begin(); ait != _atoms.end(); ++ait) { |
| 224 | if ( const DefinedAtom* defAtom = (*ait)->definedAtom() ) { |
| 225 | for (auto rit=defAtom->referencesBegin(), end=defAtom->referencesEnd(); |
| 226 | rit != end; ++rit) { |
| 227 | const Reference* ref = *rit; |
| 228 | const Atom* newTarget = _symbolTable.replacement(ref->target()); |
| 229 | (const_cast<Reference*>(ref))->setTarget(newTarget); |
| 230 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
Nick Kledzik | 49d6cc8 | 2012-02-15 00:38:09 +0000 | [diff] [blame] | 235 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 236 | // for dead code stripping, recursively mark atom "live" |
| 237 | void Resolver::markLive(const Atom &atom, WhyLiveBackChain *previous) { |
| 238 | // if -why_live cares about this symbol, then dump chain |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame^] | 239 | if ((previous->referer != nullptr) && _platform.printWhyLive(atom.name())) { |
Nick Kledzik | f46669c | 2011-12-21 23:29:36 +0000 | [diff] [blame] | 240 | llvm::errs() << atom.name() << " from " << atom.file().path() << "\n"; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 241 | int depth = 1; |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame^] | 242 | for (WhyLiveBackChain *p = previous; p != nullptr; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 243 | p = p->previous, ++depth) { |
| 244 | for (int i = depth; i > 0; --i) |
| 245 | llvm::errs() << " "; |
| 246 | llvm::errs() << p->referer->name() << " from " |
Nick Kledzik | f46669c | 2011-12-21 23:29:36 +0000 | [diff] [blame] | 247 | << p->referer->file().path() << "\n"; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
| 250 | |
| 251 | // if already marked live, then done (stop recursion) |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 252 | if ( _liveAtoms.count(&atom) ) |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 253 | return; |
| 254 | |
| 255 | // mark this atom is live |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 256 | _liveAtoms.insert(&atom); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 257 | |
| 258 | // mark all atoms it references as live |
| 259 | WhyLiveBackChain thisChain; |
| 260 | thisChain.previous = previous; |
| 261 | thisChain.referer = &atom; |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 262 | if ( const DefinedAtom* defAtom = atom.definedAtom() ) { |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 263 | for (auto rit=defAtom->referencesBegin(), end=defAtom->referencesEnd(); |
| 264 | rit != end; ++rit) { |
| 265 | const Reference* ref = *rit; |
| 266 | this->markLive(*ref->target(), &thisChain); |
| 267 | } |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | // remove all atoms not actually used |
| 272 | void Resolver::deadStripOptimize() { |
| 273 | // only do this optimization with -dead_strip |
| 274 | if (!_platform.deadCodeStripping()) |
| 275 | return; |
| 276 | |
| 277 | // clear liveness on all atoms |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 278 | _liveAtoms.clear(); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 279 | |
| 280 | // add entry point (main) to live roots |
| 281 | const Atom *entry = this->entryPoint(); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame^] | 282 | if (entry != nullptr) |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 283 | _deadStripRoots.insert(entry); |
| 284 | |
| 285 | // add -exported_symbols_list, -init, and -u entries to live roots |
| 286 | for (Platform::UndefinesIterator uit = _platform.initialUndefinesBegin(); |
| 287 | uit != _platform.initialUndefinesEnd(); ++uit) { |
| 288 | llvm::StringRef sym = *uit; |
| 289 | const Atom *symAtom = _symbolTable.findByName(sym); |
| 290 | assert(symAtom->definition() != Atom::definitionUndefined); |
| 291 | _deadStripRoots.insert(symAtom); |
| 292 | } |
| 293 | |
| 294 | // add platform specific helper atoms |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 295 | std::vector<const DefinedAtom *> platRootAtoms; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 296 | if (_platform.getImplicitDeadStripRoots(platRootAtoms)) |
| 297 | this->addAtoms(platRootAtoms); |
| 298 | |
| 299 | // mark all roots as live, and recursively all atoms they reference |
| 300 | for (std::set<const Atom *>::iterator it = _deadStripRoots.begin(); |
| 301 | it != _deadStripRoots.end(); ++it) { |
| 302 | WhyLiveBackChain rootChain; |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame^] | 303 | rootChain.previous = nullptr; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 304 | rootChain.referer = *it; |
| 305 | this->markLive(**it, &rootChain); |
| 306 | } |
| 307 | |
| 308 | // now remove all non-live atoms from _atoms |
| 309 | _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 310 | NotLive(_liveAtoms)), _atoms.end()); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | // error out if some undefines remain |
| 314 | void Resolver::checkUndefines(bool final) { |
| 315 | // when using LTO, undefines are checked after bitcode is optimized |
| 316 | if (_haveLLVMObjs && !final) |
| 317 | return; |
| 318 | |
| 319 | // build vector of remaining undefined symbols |
| 320 | std::vector<const Atom *> undefinedAtoms; |
| 321 | _symbolTable.undefines(undefinedAtoms); |
| 322 | if (_platform.deadCodeStripping()) { |
| 323 | // when dead code stripping we don't care if dead atoms are undefined |
| 324 | undefinedAtoms.erase(std::remove_if( |
| 325 | undefinedAtoms.begin(), undefinedAtoms.end(), |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 326 | NotLive(_liveAtoms)), undefinedAtoms.end()); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // let platform make error message about missing symbols |
| 330 | if (undefinedAtoms.size() != 0) |
| 331 | _platform.errorWithUndefines(undefinedAtoms, _atoms); |
| 332 | } |
| 333 | |
| 334 | // remove from _atoms all coaleseced away atoms |
| 335 | void Resolver::removeCoalescedAwayAtoms() { |
| 336 | _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), |
| 337 | AtomCoalescedAway(_symbolTable)), _atoms.end()); |
| 338 | } |
| 339 | |
| 340 | // check for interactions between symbols defined in this linkage unit |
| 341 | // and same symbol name in linked dynamic shared libraries |
| 342 | void Resolver::checkDylibSymbolCollisions() { |
| 343 | for (std::vector<const Atom *>::const_iterator it = _atoms.begin(); |
| 344 | it != _atoms.end(); ++it) { |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 345 | const DefinedAtom* defAtom = (*it)->definedAtom(); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame^] | 346 | if (defAtom == nullptr) |
Nick Kledzik | f4fb2c5 | 2012-01-11 01:06:19 +0000 | [diff] [blame] | 347 | continue; |
| 348 | if ( defAtom->merge() != DefinedAtom::mergeAsTentative ) |
| 349 | continue; |
| 350 | assert(defAtom->scope() != DefinedAtom::scopeTranslationUnit); |
| 351 | // See if any shared library also has symbol which |
| 352 | // collides with the tentative definition. |
| 353 | // SymbolTable will warn if needed. |
| 354 | _inputFiles.searchLibraries(defAtom->name(), true, false, false, *this); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | // get "main" atom for linkage unit |
| 359 | const Atom *Resolver::entryPoint() { |
| 360 | llvm::StringRef symbolName = _platform.entryPointName(); |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame^] | 361 | if (symbolName != nullptr) |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 362 | return _symbolTable.findByName(symbolName); |
| 363 | |
Michael J. Spencer | c9d2506 | 2012-03-29 19:39:14 +0000 | [diff] [blame^] | 364 | return nullptr; |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | // give platform a chance to tweak the set of atoms |
| 368 | void Resolver::tweakAtoms() { |
| 369 | _platform.postResolveTweaks(_atoms); |
| 370 | } |
| 371 | |
| 372 | void Resolver::linkTimeOptimize() { |
| 373 | // FIX ME |
| 374 | } |
| 375 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 376 | void Resolver::resolve() { |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 377 | this->initializeState(); |
| 378 | this->addInitialUndefines(); |
| 379 | this->buildInitialAtomList(); |
| 380 | this->resolveUndefines(); |
| 381 | this->updateReferences(); |
| 382 | this->deadStripOptimize(); |
| 383 | this->checkUndefines(false); |
| 384 | this->removeCoalescedAwayAtoms(); |
| 385 | this->checkDylibSymbolCollisions(); |
| 386 | this->linkTimeOptimize(); |
| 387 | this->tweakAtoms(); |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 388 | this->_result.addAtoms(_atoms); |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Nick Kledzik | 1a6615d | 2012-03-08 00:18:30 +0000 | [diff] [blame] | 391 | void Resolver::MergedFile::addAtom(const Atom& atom) { |
| 392 | if ( const DefinedAtom* defAtom = atom.definedAtom() ) { |
| 393 | _definedAtoms._atoms.push_back(defAtom); |
| 394 | } |
| 395 | else if ( const UndefinedAtom* undefAtom = atom.undefinedAtom() ) { |
| 396 | _undefinedAtoms._atoms.push_back(undefAtom); |
| 397 | } |
| 398 | else if ( const SharedLibraryAtom* slAtom = atom.sharedLibraryAtom() ) { |
| 399 | _sharedLibraryAtoms._atoms.push_back(slAtom); |
| 400 | } |
| 401 | else if ( const AbsoluteAtom* abAtom = atom.absoluteAtom() ) { |
| 402 | _absoluteAtoms._atoms.push_back(abAtom); |
| 403 | } |
| 404 | else { |
| 405 | assert(0 && "atom has unknown definition kind"); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | void Resolver::MergedFile::addAtoms(std::vector<const Atom*>& all) { |
| 410 | for(std::vector<const Atom*>::iterator it=all.begin(); it != all.end(); ++it) { |
| 411 | this->addAtom(**it); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | |
Michael J. Spencer | 773a8fb | 2011-12-18 08:27:59 +0000 | [diff] [blame] | 416 | } // namespace lld |