[lld] linker script error while use "-=" on arm target

Hello.

I got link error when I try build code on arm target.
It seems that It could’n use “-=” operation when using arm.

The following is what we tried.

$ cat sample.c
void __init_array_start(void){};
void __init_array_end(void){};
int main()
{
return 0;
}

$ cat sample.ld
SECTIONS
{
sample = 0;
sample += 1;
sample -= 1;
. = 0x08048000 + SIZEOF_HEADERS;
.text : {(.text)}
.rodata : {
(.rodata)}
.data : {(.data)}
.bss : {
(.bss)}
}

There is no error that build x86_64 target.

$ clang -fPIE -target x86_64-unknown-none-elf -nodefaultlibs sample.c -T sample.ld

But I get a error when build arm target.

$ clang -target arm-none-eabi -march=armv7-m -mcpu=cortex-m4 -nodefaultlibs sample.c -T sample.ld
ld.lld: error: sample.ld:5: malformed number: =

sample -= 1;
^
clang-12: error: ld.lld command failed with exit code 1 (use -v to see invocation)

The version of clang I use using is 12.

$ clang --version
clang version 12.0.0 (https://github.com/llvm/llvm-project/ b978a93635b584db380274d7c8963c73989944a1)
Target: x86_64-unknown-linux-gnu
Thread model: posix

Is there some linker script difference depend on target?

Thanks.
Saito Yutaka

Hello.

I got link error when I try build code on arm target.
It seems that It could'n use "-=" operation when using arm.
The following is what we tried.

$ cat sample.c
void __init_array_start(void){};
void __init_array_end(void){};
int main()
{
    return 0;
}

$ cat sample.ld
SECTIONS
{
   sample = 0;
   sample += 1;
   sample -= 1;
   . = 0x08048000 + SIZEOF_HEADERS;
   .text : {*(.text)}
   .rodata : {*(.rodata)}
   .data : {*(.data)}
   .bss : {*(.bss)}
}

# There is no error that build x86_64 target.
$ clang -fPIE -target x86_64-unknown-none-elf -nodefaultlibs sample.c -T
sample.ld

# But I get a error when build arm target.
$ clang -target arm-none-eabi -march=armv7-m -mcpu=cortex-m4
-nodefaultlibs sample.c -T sample.ld
ld.lld: error: sample.ld:5: malformed number: =

    sample -= 1;
            ^

clang-12: error: ld.lld command failed with exit code 1 (use -v to see
invocation)

-= is not supported. ld.lld only supports +=.
-= is probably too rare in the wild.

Actually changing the value of a symbol is hardly a good idea.
The evaluation order of expressions is not well defined.
Using -= or += may cause difficult-to-explain portability issues across
GNU ld and ld.lld.

# The version of clang I use using is 12.
$ clang --version
clang version 12.0.0 (GitHub - llvm/llvm-project: The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Note: the repository does not accept github pull requests at this moment. Please submit your patches at http://reviews.llvm.org.
b978a93635b584db380274d7c8963c73989944a1)
Target: x86_64-unknown-linux-gnu
Thread model: posix

Is there some linker script difference depend on target?

No.

Hi

Thanks for your reply.

The evaluation order of expressions is not well defined.
Using -= or += may cause difficult-to-explain portability issues across
GNU ld and ld.lld.

I didn’t know that.
Thank you for the information.

Saito Yutaka

2021年4月25日(日) 6:16 Fangrui Song <maskray@google.com>: