[ELF] - Fixed possible iterator overflow.

We can have Opt.Commands size greater then Sections.size().
For example if we have next script:

SECTIONS { 
.aaa : { *(.aaa) }           
.bbb : { *(.bbb) }   
.ccc : { *(.ccc) }   
}

and next code:

.global _start
_start:
 nop

.section .aaa,"a"
 .quad 0

Then amount of sections is less than amound of Opt.Commands
and if we for example have all commands NoConstraint,
that overflowed the iterator used.

llvm-svn: 276741
This commit is contained in:
George Rimar 2016-07-26 10:47:09 +00:00
parent 019e102426
commit bfc4a4b7a1
1 changed files with 7 additions and 6 deletions

View File

@ -150,19 +150,21 @@ LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
template <class ELFT>
std::vector<OutputSectionBase<ELFT> *>
LinkerScript<ELFT>::filter(std::vector<OutputSectionBase<ELFT> *> &Sections) {
// Sections and OutputSectionCommands are parallel arrays.
// In this loop, we remove output sections if they don't satisfy
// requested properties.
auto It = Sections.begin();
for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
if (!Cmd || Cmd->Name == "/DISCARD/")
continue;
if (Cmd->Constraint == ConstraintKind::NoConstraint) {
++It;
if (Cmd->Constraint == ConstraintKind::NoConstraint)
continue;
auto It = llvm::find_if(Sections, [&](OutputSectionBase<ELFT> *S) {
return S->getName() == Cmd->Name;
});
if (It == Sections.end())
continue;
}
OutputSectionBase<ELFT> *Sec = *It;
bool Writable = (Sec->getFlags() & SHF_WRITE);
@ -173,7 +175,6 @@ LinkerScript<ELFT>::filter(std::vector<OutputSectionBase<ELFT> *> &Sections) {
Sections.erase(It);
continue;
}
++It;
}
return Sections;
}