[PATCH] D27712: [ELF] - Do not crash when move location counter backward.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 13 08:37:17 PST 2016


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

> grimar created this revision.
> grimar added reviewers: ruiu, rafael.
> grimar added subscribers: llvm-commits, evgeny777, grimar.
>
> PR31335 shows that we do that in next case:
> SECTIONS { .text 0x2000 : {. = 0x100 ; *(.text) } }
>
> though documentations says that "If . is used inside a section description however, it refers to the byte offset from the start of that section, not an absolute address. " looks does not work as documented in bfd (as mentioned in comments for PR31335).
> Until we find out the expected behavior was suggested at least not to 'crash', what we do after trying to generate huge file.
>
>
> https://reviews.llvm.org/D27712
>
> Files:
>   ELF/LinkerScript.cpp
>   test/ELF/linkerscript/locationcountererr.s
>
>
> Index: test/ELF/linkerscript/locationcountererr.s
> ===================================================================
> --- test/ELF/linkerscript/locationcountererr.s
> +++ test/ELF/linkerscript/locationcountererr.s
> @@ -0,0 +1,9 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
> +# RUN: echo "SECTIONS { .text 0x2000 : {. = 0x10 ; *(.text) } }" > %t.script
> +# RUN: not ld.lld %t --script %t.script -o %t1 2>&1 | FileCheck %s
> +# CHECK: unable to move location counter backward for: .text
> +
> +.globl _start
> +_start:
> +nop
> Index: ELF/LinkerScript.cpp
> ===================================================================
> --- ELF/LinkerScript.cpp
> +++ ELF/LinkerScript.cpp
> @@ -464,7 +464,10 @@
>    if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
>      if (AssignCmd->Name == ".") {
>        // Update to location counter means update to section size.
> -      Dot = AssignCmd->Expression(Dot);
> +      uintX_t Val = AssignCmd->Expression(Dot);
> +      if (Val < Dot)
> +        error("unable to move location counter backward for: " + CurOutSec->Name);
> +      Dot = Val;
>        CurOutSec->Size = Dot - CurOutSec->Addr;
>        return;
>      }
>
>
> Index: test/ELF/linkerscript/locationcountererr.s
> ===================================================================
> --- test/ELF/linkerscript/locationcountererr.s
> +++ test/ELF/linkerscript/locationcountererr.s
> @@ -0,0 +1,9 @@
> +# REQUIRES: x86
> +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
> +# RUN: echo "SECTIONS { .text 0x2000 : {. = 0x10 ; *(.text) } }" > %t.script
> +# RUN: not ld.lld %t --script %t.script -o %t1 2>&1 | FileCheck %s
> +# CHECK: unable to move location counter backward for: .text
> +
> +.globl _start
> +_start:
> +nop
> Index: ELF/LinkerScript.cpp
> ===================================================================
> --- ELF/LinkerScript.cpp
> +++ ELF/LinkerScript.cpp
> @@ -464,7 +464,10 @@
>    if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
>      if (AssignCmd->Name == ".") {
>        // Update to location counter means update to section size.
> -      Dot = AssignCmd->Expression(Dot);
> +      uintX_t Val = AssignCmd->Expression(Dot);
> +      if (Val < Dot)
> +        error("unable to move location counter backward for: " + CurOutSec->Name);
> +      Dot = Val;
>        CurOutSec->Size = Dot - CurOutSec->Addr;
>        return;
>      }


More information about the llvm-commits mailing list