[PATCH] D36145: [ELF] - Do not segfault when doing logical and/or operations on symbols that have no output sections.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 2 15:48:05 PDT 2017


LGTM

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

> grimar updated this revision to Diff 109291.
> grimar added a comment.
>
> - Addressed review comment.
>
>
> https://reviews.llvm.org/D36145
>
> Files:
>   ELF/LinkerScript.cpp
>   test/ELF/linkerscript/early-assign-symbol.s
>
>
> Index: test/ELF/linkerscript/early-assign-symbol.s
> ===================================================================
> --- test/ELF/linkerscript/early-assign-symbol.s
> +++ test/ELF/linkerscript/early-assign-symbol.s
> @@ -7,6 +7,12 @@
>  # RUN: echo "SECTIONS { aaa = ABSOLUTE(foo - 1) + 1; .text  : { *(.text*) } }" > %t2.script
>  # RUN: not ld.lld -o %t --script %t2.script %t.o 2>&1 | FileCheck %s
>  
> +# RUN: echo "SECTIONS { aaa = foo | 1; .text  : { *(.text*) } }" > %t3.script
> +# RUN: not ld.lld -o %t --script %t3.script %t.o 2>&1 | FileCheck %s
> +
> +# RUN: echo "SECTIONS { aaa = foo & 1; .text  : { *(.text*) } }" > %t4.script
> +# RUN: not ld.lld -o %t --script %t4.script %t.o 2>&1 | FileCheck %s
> +
>  # CHECK: error: {{.*}}.script:1: unable to evaluate expression: input section .text has no output section assigned
>  
>  .section .text
> Index: ELF/LinkerScript.cpp
> ===================================================================
> --- ELF/LinkerScript.cpp
> +++ ELF/LinkerScript.cpp
> @@ -49,19 +49,24 @@
>  
>  LinkerScript *elf::Script;
>  
> +static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) {
> +  if (OutputSection *OS = InputSec->getOutputSection())
> +    return OS->Addr;
> +  error(Loc + ": unable to evaluate expression: input section " +
> +        InputSec->Name + " has no output section assigned");
> +  return 0;
> +}
> +
>  uint64_t ExprValue::getValue() const {
> -  if (Sec) {
> -    if (OutputSection *OS = Sec->getOutputSection())
> -      return alignTo(Sec->getOffset(Val) + OS->Addr, Alignment);
> -    error(Loc + ": unable to evaluate expression: input section " + Sec->Name +
> -          " has no output section assigned");
> -  }
> +  if (Sec)
> +    return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc),
> +                   Alignment);
>    return alignTo(Val, Alignment);
>  }
>  
>  uint64_t ExprValue::getSecAddr() const {
>    if (Sec)
> -    return Sec->getOffset(0) + Sec->getOutputSection()->Addr;
> +    return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc);
>    return 0;
>  }
>  
>
>
> Index: test/ELF/linkerscript/early-assign-symbol.s
> ===================================================================
> --- test/ELF/linkerscript/early-assign-symbol.s
> +++ test/ELF/linkerscript/early-assign-symbol.s
> @@ -7,6 +7,12 @@
>  # RUN: echo "SECTIONS { aaa = ABSOLUTE(foo - 1) + 1; .text  : { *(.text*) } }" > %t2.script
>  # RUN: not ld.lld -o %t --script %t2.script %t.o 2>&1 | FileCheck %s
>  
> +# RUN: echo "SECTIONS { aaa = foo | 1; .text  : { *(.text*) } }" > %t3.script
> +# RUN: not ld.lld -o %t --script %t3.script %t.o 2>&1 | FileCheck %s
> +
> +# RUN: echo "SECTIONS { aaa = foo & 1; .text  : { *(.text*) } }" > %t4.script
> +# RUN: not ld.lld -o %t --script %t4.script %t.o 2>&1 | FileCheck %s
> +
>  # CHECK: error: {{.*}}.script:1: unable to evaluate expression: input section .text has no output section assigned
>  
>  .section .text
> Index: ELF/LinkerScript.cpp
> ===================================================================
> --- ELF/LinkerScript.cpp
> +++ ELF/LinkerScript.cpp
> @@ -49,19 +49,24 @@
>  
>  LinkerScript *elf::Script;
>  
> +static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) {
> +  if (OutputSection *OS = InputSec->getOutputSection())
> +    return OS->Addr;
> +  error(Loc + ": unable to evaluate expression: input section " +
> +        InputSec->Name + " has no output section assigned");
> +  return 0;
> +}
> +
>  uint64_t ExprValue::getValue() const {
> -  if (Sec) {
> -    if (OutputSection *OS = Sec->getOutputSection())
> -      return alignTo(Sec->getOffset(Val) + OS->Addr, Alignment);
> -    error(Loc + ": unable to evaluate expression: input section " + Sec->Name +
> -          " has no output section assigned");
> -  }
> +  if (Sec)
> +    return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc),
> +                   Alignment);
>    return alignTo(Val, Alignment);
>  }
>  
>  uint64_t ExprValue::getSecAddr() const {
>    if (Sec)
> -    return Sec->getOffset(0) + Sec->getOutputSection()->Addr;
> +    return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc);
>    return 0;
>  }
>  


More information about the llvm-commits mailing list