Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 70 additions & 73 deletions lib/Object/ObjectBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ void ObjectBuilder::doPluginIterateSections(eld::InputFile *Obj,
}

void ObjectBuilder::assignInputFromOutput(eld::InputFile *Obj) {
std::unordered_map<Section *, bool> RetrySections;
bool IsPartialLink = (ThisConfig.codeGenType() == LinkerConfig::Object);
bool IsGnuCompatible =
(ThisConfig.options().getScriptOption() == GeneralOptions::MatchGNU);
Expand All @@ -273,82 +272,78 @@ void ObjectBuilder::assignInputFromOutput(eld::InputFile *Obj) {
SectionMap &SectionMap = ThisModule.getScript().sectionMap();
ObjectFile *ObjFile = llvm::dyn_cast<ObjectFile>(Obj);
std::vector<Section *> Sections = getInputSectionsForRuleMatching(ObjFile);
// For all output sections.
for (auto *Out : SectionMap) {
// For each input rule.
for (auto *In : *Out) {
auto Start = std::chrono::system_clock::now();
// For each input section.
for (Section *Section : Sections) {
ELFSection *ELFSect = llvm::dyn_cast<eld::ELFSection>(Section);
// For each input section.
for (Section *Sect : Sections) {
ELFSection *ELFSect = llvm::dyn_cast<eld::ELFSection>(Sect);

bool IsRetrySect = false;
bool IsRetrySect = false;

// Skip sections with merge strings and if there is no linker scripts
// provided.
if (IsPartialLink && (ELFSect && ELFSect->isMergeStr()) &&
!LinkerScriptHasSectionsCommand)
continue;
InputFile *Input = Obj;
bool IsCommonSection = false;
if (CommonELFSection *CommonSection =
llvm::dyn_cast<CommonELFSection>(Sect)) {
Input = CommonSection->getOrigin();
IsCommonSection = true;
}
if (Sect->getOldInputFile())
Input = Sect->getOldInputFile();
std::string const &PInputFile =
Input->getInput()->getResolvedPath().native();
std::string const &Name = Input->getInput()->getName();
bool IsArchive = Input->isArchive() ||
llvm::dyn_cast<eld::ArchiveMemberInput>(Input->getInput());
// Hash of all the required things for Match.
uint64_t InputFileHash = Input->getInput()->getResolvedPathHash();
uint64_t NameHash = Input->getInput()->getArchiveMemberNameHash();
std::string SectName = Sect->name().str();
uint64_t InputSectionHash = Sect->sectionNameHash();
if (ELFSection *ELFSect = llvm::dyn_cast<ELFSection>(Sect)) {
if (auto OptRThisSectionName =
ObjFile->getRuleMatchingSectName(ELFSect->getIndex())) {
SectName = OptRThisSectionName.value();
InputSectionHash = llvm::hash_combine(SectName);
}
}
// For all output sections.
for (auto *Out : SectionMap) {
if (ELFSect) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (ELFSect) {
if (Section->getOutputSection() && !IsRetrySect)
break;
if (ELFSect) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (Section->getOutputSection() && !IsRetrySect) check needs to stay within the input-section-description loop (for (auto *In : *Out)). Otherwise, we will keep traversing input section descriptions even though the section has already matched an input section description.

// If the rule needs to match on permissions, skip if the rule doesnot
// satisfy.
switch (Out->prolog().constraint()) {
case OutputSectDesc::NO_CONSTRAINT:
break;
case OutputSectDesc::ONLY_IF_RO:
if (ELFSect->isWritable())
continue;
break;
case OutputSectDesc::ONLY_IF_RW:
if (!ELFSect->isWritable())
continue;
break;
}
}
bool shouldSkipMatch = false;
// For each input rule.
for (auto *In : *Out) {
// If the section has an already assigned output section, skip.
// If the section needs to be retried, we may need to revisit the
// section to match the best rule.
if (Section->getOutputSection()) {
if (!RetrySections.size() ||
(RetrySections.find(Section) == RetrySections.end())) {
continue;
}
IsRetrySect = true;
}

if (ELFSect) {
// If the rule needs to match on permissions, skip if the rule doesnot
// satisfy.
switch (Out->prolog().constraint()) {
case OutputSectDesc::NO_CONSTRAINT:
break;
case OutputSectDesc::ONLY_IF_RO:
if (ELFSect->isWritable())
continue;
break;
case OutputSectDesc::ONLY_IF_RW:
if (!ELFSect->isWritable())
continue;
break;
}
}
// Skip sections with merge strings and if there is no linker scripts
// provided.
if (IsPartialLink && (ELFSect && ELFSect->isMergeStr()) &&
!LinkerScriptHasSectionsCommand)
continue;
InputFile *Input = Obj;
bool IsCommonSection = false;
if (CommonELFSection *CommonSection =
llvm::dyn_cast<CommonELFSection>(Section)) {
Input = CommonSection->getOrigin();
IsCommonSection = true;
}
if (Section->getOldInputFile())
Input = Section->getOldInputFile();
std::string const &PInputFile =
Input->getInput()->getResolvedPath().native();
std::string const &Name = Input->getInput()->getName();
bool IsArchive =
Input->isArchive() ||
llvm::dyn_cast<eld::ArchiveMemberInput>(Input->getInput());
// Hash of all the required things for Match.
uint64_t InputFileHash = Input->getInput()->getResolvedPathHash();
uint64_t NameHash = Input->getInput()->getArchiveMemberNameHash();
std::string SectName = Section->name().str();
uint64_t InputSectionHash = Section->sectionNameHash();
if (ELFSection *ELFSect = llvm::dyn_cast<ELFSection>(Section)) {
if (auto OptRThisSectionName =
ObjFile->getRuleMatchingSectName(ELFSect->getIndex())) {
SectName = OptRThisSectionName.value();
InputSectionHash = llvm::hash_combine(SectName);
}
if (Sect->getOutputSection() && !IsRetrySect) {
shouldSkipMatch = true;
break;
}
// auto Start = std::chrono::system_clock::now();
if (SectionMap.matched(*In, Input, PInputFile, SectName, IsArchive,
Name, InputSectionHash, InputFileHash, NameHash,
IsGnuCompatible, IsCommonSection)) {
In->incMatchCount();
Section->setOutputSection(Out);
Section->setMatchedLinkerScriptRule(In);
Sect->setOutputSection(Out);
Sect->setMatchedLinkerScriptRule(In);
// FIXME: Shouldn't we set ELFSect to LDFileFormat::Discard?
if (ELFSect && Out->isDiscard()) {
ELFSect->setKind(LDFileFormat::Ignore);
Expand All @@ -359,15 +354,17 @@ void ObjectBuilder::assignInputFromOutput(eld::InputFile *Obj) {
<< ObjFile->getInput()->decoratedPath();
}
if (IsRetrySect && !In->isSpecial())
RetrySections.erase(Section);
IsRetrySect = false;
// Retry the match until the closest match is found.
if (In->isSpecial())
RetrySections.insert(std::make_pair(Section, true));
IsRetrySect = true;
} // end match
} // end each input section
In->addMatchTime(std::chrono::system_clock::now() - Start);
} // end each rule
} // end each output section
// RuleMatchTimes[In] += std::chrono::system_clock::now() - Start;
} // end each rule
if (shouldSkipMatch)
break;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
break;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need shouldBreak here to signal that more output sections do not need to be traversed for this input section.

} // end each output section
} // end each input section
}

bool ObjectBuilder::initializePluginsAndProcess(
Expand Down
Loading