diff --git a/lld/include/lld/Core/File.h b/lld/include/lld/Core/File.h index 6a555c62c884..9010978eeaeb 100644 --- a/lld/include/lld/Core/File.h +++ b/lld/include/lld/Core/File.h @@ -73,12 +73,11 @@ public: return _ordinal; } - /// Sets the command line order of the file. The parameter must - /// also be incremented to the next available ordinal number. - virtual void setOrdinalAndIncrement(uint64_t &ordinal) const { - _ordinal = ordinal; - ++ordinal; - } + /// Returns true/false depending on whether an ordinal has been set. + bool hasOrdinal() const { return (_ordinal != UINT64_MAX); } + + /// Sets the command line order of the file. + void setOrdinal(uint64_t ordinal) const { _ordinal = ordinal; } public: template class atom_iterator; // forward reference diff --git a/lld/include/lld/Core/LinkingContext.h b/lld/include/lld/Core/LinkingContext.h index 2f410998963c..220d4c01cb2e 100644 --- a/lld/include/lld/Core/LinkingContext.h +++ b/lld/include/lld/Core/LinkingContext.h @@ -304,7 +304,8 @@ public: /// The LinkingContext's can override the default behavior to change the way /// the resolver operates. This uses the currentInputElement. When there are /// no more files to be processed an appropriate InputGraphError is - /// returned. + /// returned. Ordinals are assigned to files returned by nextFile, which means + /// ordinals would be assigned in the way files are resolved. virtual ErrorOr nextFile(); /// Set the resolver state for the current Input element This is used by the @@ -314,6 +315,9 @@ public: /// atoms. virtual void setResolverState(uint32_t resolverState); + /// Return the next ordinal and Increment it. + virtual uint64_t getNextOrdinalAndIncrement() { return _nextOrdinal++; } + /// @} /// \name Methods needed by YAML I/O and error messages to convert Kind values @@ -363,6 +367,7 @@ protected: std::unique_ptr _inputGraph; mutable llvm::BumpPtrAllocator _allocator; InputElement *_currentInputElement; + uint64_t _nextOrdinal; private: /// Validate the subclass bits. Only called by validate. diff --git a/lld/include/lld/Driver/InputGraph.h b/lld/include/lld/Driver/InputGraph.h index a0c4657cd346..6d4bcb3e57d9 100644 --- a/lld/include/lld/Driver/InputGraph.h +++ b/lld/include/lld/Driver/InputGraph.h @@ -67,9 +67,6 @@ public: /// \brief Set Ordinals for all the InputElements that form the InputGraph virtual bool assignOrdinals(); - /// \brief Set ordinals for all the Files that are part of the InputElements - virtual bool assignFileOrdinals(uint64_t &ordinal); - /// Destructor virtual ~InputGraph() {} @@ -149,10 +146,6 @@ public: _ordinal = ordinal; } - /// \brief Assign File ordinals for files contained - /// in the InputElement - virtual void assignFileOrdinals(uint64_t &fileOrdinal) = 0; - virtual int64_t getOrdinal() const { return _ordinal; } virtual int64_t weight() const { return _weight; } @@ -228,10 +221,6 @@ public: return make_range(_elements.begin(), _elements.end()); } - /// \brief Assign File ordinals for files contained - /// in the InputElement - virtual void assignFileOrdinals(uint64_t &startOrdinal); - virtual void resetNextIndex() { _currentElementIndex = _nextElementIndex = 0; for (auto &elem : _elements) @@ -306,10 +295,6 @@ public: _files.push_back(std::move(ai)); } - /// \brief Assign File ordinals for files contained - /// in the InputElement - virtual void assignFileOrdinals(uint64_t &startOrdinal); - /// \brief Reset the file index if the resolver needs to process /// the node again. virtual void resetNextIndex(); @@ -426,10 +411,6 @@ public: // Do nothing here. virtual void resetNextIndex() {} - /// \brief Assign File ordinals for files contained - /// in the InputElement - virtual void assignFileOrdinals(uint64_t &startOrdinal); - protected: StringRef _path; // A string associated with this file. InputGraph::FileVectorT _files; // Vector of lld::File objects diff --git a/lld/include/lld/ReaderWriter/FileArchive.h b/lld/include/lld/ReaderWriter/FileArchive.h index d11a0a7c156a..695d01bae188 100644 --- a/lld/include/lld/ReaderWriter/FileArchive.h +++ b/lld/include/lld/ReaderWriter/FileArchive.h @@ -56,8 +56,6 @@ public: assert(result.size() == 1); - result[0]->setOrdinalAndIncrement(_curChildOrd); - // give up the pointer so that this object no longer manages it return result[0].release(); } @@ -79,23 +77,9 @@ public: if ((ec = _context.getDefaultReader().parseFile(mbc, result))) return ec; } - for (auto &file : result) - file->setOrdinalAndIncrement(_curChildOrd); return error_code::success(); } - /// \brief members - - virtual void setOrdinalAndIncrement(uint64_t &ordinal) const { - _ordinal = ordinal++; - _curChildOrd = _ordinal; - // Leave space in ordinal range for all children - for (auto mf = _archive->begin_children(), - me = _archive->end_children(); mf != me; ++mf) { - ordinal++; - } - } - virtual const atom_collection &defined() const { return _definedAtoms; } diff --git a/lld/lib/Core/LinkingContext.cpp b/lld/lib/Core/LinkingContext.cpp index 779d9b81be13..9ae4f51da004 100644 --- a/lld/lib/Core/LinkingContext.cpp +++ b/lld/lib/Core/LinkingContext.cpp @@ -24,7 +24,8 @@ LinkingContext::LinkingContext() _warnIfCoalesableAtomsHaveDifferentLoadName(false), _printRemainingUndefines(true), _allowRemainingUndefines(false), _logInputFiles(false), _allowShlibUndefines(false), - _outputFileType(OutputFileType::Default), _currentInputElement(nullptr) {} + _outputFileType(OutputFileType::Default), _currentInputElement(nullptr), + _nextOrdinal(0) {} LinkingContext::~LinkingContext() {} diff --git a/lld/lib/Core/Resolver.cpp b/lld/lib/Core/Resolver.cpp index c8d1549c64bf..1147bc55236e 100644 --- a/lld/lib/Core/Resolver.cpp +++ b/lld/lib/Core/Resolver.cpp @@ -108,8 +108,10 @@ void Resolver::handleArchiveFile(const File &file) { StringRef undefName = undefAtom->name(); // load for previous undefine may also have loaded this undefine if (!_symbolTable.isDefined(undefName)) { - if (const File *member = archiveFile->find(undefName, false)) + if (const File *member = archiveFile->find(undefName, false)) { + member->setOrdinal(_context.getNextOrdinalAndIncrement()); handleFile(*member); + } } // If the undefined symbol has an alternative name, try to resolve the // symbol with the name to give it a second chance. This feature is used @@ -132,8 +134,10 @@ void Resolver::handleArchiveFile(const File &file) { assert(curAtom != nullptr); if (const DefinedAtom *curDefAtom = dyn_cast(curAtom)) { if (curDefAtom->merge() == DefinedAtom::mergeAsTentative) { - if (const File *member = archiveFile->find(tentDefName, true)) + if (const File *member = archiveFile->find(tentDefName, true)) { + member->setOrdinal(_context.getNextOrdinalAndIncrement()); handleFile(*member); + } } } } @@ -147,7 +151,6 @@ void Resolver::handleSharedLibrary(const File &file) { // Add all the atoms from the shared library handleFile(*sharedLibrary); - do { undefineGenCount = _symbolTable.size(); std::vector undefines; @@ -295,12 +298,20 @@ void Resolver::resolveUndefines() { _context.setResolverState(Resolver::StateNoChange); if (error_code(nextFile) == InputGraphError::no_more_files) break; - if (nextFile->kind() == File::kindObject) + if (nextFile->kind() == File::kindObject) { + nextFile->setOrdinal(_context.getNextOrdinalAndIncrement()); handleFile(*nextFile); - if (nextFile->kind() == File::kindArchiveLibrary) + } + if (nextFile->kind() == File::kindArchiveLibrary) { + if (!nextFile->hasOrdinal()) + nextFile->setOrdinal(_context.getNextOrdinalAndIncrement()); handleArchiveFile(*nextFile); - if (nextFile->kind() == File::kindSharedLibrary) + } + if (nextFile->kind() == File::kindSharedLibrary) { + if (!nextFile->hasOrdinal()) + nextFile->setOrdinal(_context.getNextOrdinalAndIncrement()); handleSharedLibrary(*nextFile); + } } } diff --git a/lld/lib/Driver/Driver.cpp b/lld/lib/Driver/Driver.cpp index 87822305f66b..dd744e133660 100644 --- a/lld/lib/Driver/Driver.cpp +++ b/lld/lib/Driver/Driver.cpp @@ -104,9 +104,6 @@ bool Driver::link(LinkingContext &context, raw_ostream &diagnostics) { context.inputGraph().doPostProcess(); - uint64_t ordinal = 0; - context.inputGraph().assignFileOrdinals(ordinal); - // Do core linking. ScopedTask resolveTask(getDefaultDomain(), "Resolve"); Resolver resolver(context); diff --git a/lld/lib/Driver/InputGraph.cpp b/lld/lib/Driver/InputGraph.cpp index e7b4f1b6e1ef..7b43c1d3b3ca 100644 --- a/lld/lib/Driver/InputGraph.cpp +++ b/lld/lib/Driver/InputGraph.cpp @@ -29,12 +29,6 @@ bool InputGraph::assignOrdinals() { return true; } -bool InputGraph::assignFileOrdinals(uint64_t &startOrdinal) { - for (auto &ie : _inputArgs) - ie->assignFileOrdinals(startOrdinal); - return true; -} - void InputGraph::doPostProcess() { std::stable_sort(_inputArgs.begin(), _inputArgs.end(), sortInputElements); } @@ -103,13 +97,6 @@ FileNode::FileNode(StringRef path, int64_t ordinal) : InputElement(InputElement::Kind::File, ordinal), _path(path), _resolveState(Resolver::StateNoChange), _nextFileIndex(0) {} -/// \brief Assign File ordinals for files contained -/// in the InputElement -void FileNode::assignFileOrdinals(uint64_t &startOrdinal) { - for (auto &file : _files) - file->setOrdinalAndIncrement(startOrdinal); -} - /// \brief Read the file into _buffer. error_code FileNode::readFile(const LinkingContext &ctx, raw_ostream &diagnostics) { @@ -147,13 +134,6 @@ void FileNode::resetNextIndex() { /// ControlNode -/// \brief Assign File ordinals for files contained -/// in the InputElement -void ControlNode::assignFileOrdinals(uint64_t &startOrdinal) { - for (auto &elem : _elements) - elem->assignFileOrdinals(startOrdinal); -} - /// \brief Get the resolver State. The return value of the resolve /// state for a control node is the or'ed value of the resolve states /// contained in it. @@ -178,13 +158,6 @@ SimpleFileNode::SimpleFileNode(StringRef path, int64_t ordinal) : InputElement(InputElement::Kind::SimpleFile, ordinal), _path(path), _nextFileIndex(0), _resolveState(Resolver::StateNoChange) {} -/// \brief Assign File ordinals for files contained -/// in the InputElement -void SimpleFileNode::assignFileOrdinals(uint64_t &startOrdinal) { - for (auto &file : _files) - file->setOrdinalAndIncrement(startOrdinal); -} - /// Group /// \brief Return the next file that need to be processed by the resolver. diff --git a/lld/lib/ReaderWriter/ReaderLinkerScript.cpp b/lld/lib/ReaderWriter/ReaderLinkerScript.cpp index 902cc5f5ef2a..aab0d8574223 100644 --- a/lld/lib/ReaderWriter/ReaderLinkerScript.cpp +++ b/lld/lib/ReaderWriter/ReaderLinkerScript.cpp @@ -34,10 +34,6 @@ public: return f->kind() == kindLinkerScript; } - virtual void setOrdinalAndIncrement(uint64_t &ordinal) const { - _ordinal = ordinal++; - } - virtual const LinkingContext &getLinkingContext() const { return _context; } virtual const atom_collection &defined() const { diff --git a/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp b/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp index 142cbfe888e5..133540788e7e 100644 --- a/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp +++ b/lld/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp @@ -600,14 +600,6 @@ template <> struct MappingTraits { const lld::File *denormalize(IO &io) { return this; } - virtual void setOrdinalAndIncrement(uint64_t &ordinal) const { - _ordinal = ordinal++; - // Assign sequential ordinals to member files - for (const ArchMember &member : _members) { - member._content->setOrdinalAndIncrement(ordinal); - } - } - virtual const atom_collection &defined() const { return _noDefinedAtoms; }