[ELF] - Fix use of freed memory.
authorGeorge Rimar <grimar@accesssoftek.com>
Wed, 21 Dec 2016 08:11:49 +0000 (08:11 +0000)
committerGeorge Rimar <grimar@accesssoftek.com>
Wed, 21 Dec 2016 08:11:49 +0000 (08:11 +0000)
commit4fb6e79c65527b5617fe859c4c4a7d232281d2dd
tree0c7ef2bc0ba115c846271e352aa55453fd796cb7
parent86dc60d8d4c7cc720f578e4a18fa2664483bc186
[ELF] - Fix use of freed memory.

It was revealed by D27831.

If we have linkerscript that includes another one that sets OUTPUT for example:

RUN: echo "INCLUDE \"foo.script\"" > %t.script
RUN: echo "OUTPUT(\"%t.out\")" > %T/foo.script
then we do:

void ScriptParser::readInclude() {
...
  std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
  tokenize(MB->getMemBufferRef());
  OwningMBs.push_back(std::move(MB));
}

void ScriptParser::readOutput() {
...
    Config->OutputFile = unquote(Tok);
...
}
Problem is that OwningMBs are destroyed after script parser do its job.
So all Toks are dead and Config->OutputFile points to destroyed data.

Patch suggests to save all included scripts into using string Saver.

Differential revision: https://reviews.llvm.org/D27987

llvm-svn: 290238
lld/ELF/LinkerScript.cpp