[PATCH] D36138: [ELF] - Don't segfault when accessing location counter inside MEMORY command.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 1 10:24:34 PDT 2017


This LGTM, but a nicer error message would be a good followup.

We might be able to diagnose this in the parser and fuzz a bit more to
find if anything else gets here too soon.

Cheers,
Rafael

George Rimar via Phabricator <reviews at reviews.llvm.org> writes:

> grimar created this revision.
> Herald added a subscriber: emaste.
>
> We would previously crash on next script:
> `MEMORY { name : ORIGIN = .; }`
>
> Patch fixes that.
>
>
> https://reviews.llvm.org/D36138
>
> Files:
>   ELF/LinkerScript.cpp
>   test/ELF/linkerscript/memory-err.s
>
>
> Index: test/ELF/linkerscript/memory-err.s
> ===================================================================
> --- test/ELF/linkerscript/memory-err.s
> +++ test/ELF/linkerscript/memory-err.s
> @@ -0,0 +1,5 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
> +# RUN: echo "MEMORY { name : ORIGIN = .; }" > %t.script
> +# RUN: not ld.lld -shared -o %t2 --script %t.script %t 2>&1 | FileCheck %s
> +# CHECK: error: {{.*}}.script:1: unable to get location counter value
> Index: ELF/LinkerScript.cpp
> ===================================================================
> --- ELF/LinkerScript.cpp
> +++ ELF/LinkerScript.cpp
> @@ -812,8 +812,13 @@
>  }
>  
>  ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
> -  if (S == ".")
> -    return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr, Loc};
> +  if (S == ".") {
> +    if (CurAddressState)
> +      return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr,
> +              Loc};
> +    error(Loc + ": unable to get location counter value");
> +    return 0;
> +  }
>    if (SymbolBody *B = Symtab->find(S)) {
>      if (auto *D = dyn_cast<DefinedRegular>(B))
>        return {D->Section, D->Value, Loc};
>
>
> Index: test/ELF/linkerscript/memory-err.s
> ===================================================================
> --- test/ELF/linkerscript/memory-err.s
> +++ test/ELF/linkerscript/memory-err.s
> @@ -0,0 +1,5 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
> +# RUN: echo "MEMORY { name : ORIGIN = .; }" > %t.script
> +# RUN: not ld.lld -shared -o %t2 --script %t.script %t 2>&1 | FileCheck %s
> +# CHECK: error: {{.*}}.script:1: unable to get location counter value
> Index: ELF/LinkerScript.cpp
> ===================================================================
> --- ELF/LinkerScript.cpp
> +++ ELF/LinkerScript.cpp
> @@ -812,8 +812,13 @@
>  }
>  
>  ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
> -  if (S == ".")
> -    return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr, Loc};
> +  if (S == ".") {
> +    if (CurAddressState)
> +      return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr,
> +              Loc};
> +    error(Loc + ": unable to get location counter value");
> +    return 0;
> +  }
>    if (SymbolBody *B = Symtab->find(S)) {
>      if (auto *D = dyn_cast<DefinedRegular>(B))
>        return {D->Section, D->Value, Loc};


More information about the llvm-commits mailing list