[lld] [arm] Linker Cannot Set Custom Section Type to NOBITS

I am linking a program to be loaded in an ARM Cortex-M0+ based microcontroller. In the linker script, I have a section allocated for the stack which roughly looks like the following.
.stack : { . += stack_size; } > ram

Using the linker in the gcc arm toolchain, arm-none-eabi-ld, this section is automatically set to type NOBITS, however, when linking with version 10.0.0 of ld.lld, the section type is PROGBITS.
Here is the output when using readelf.

ld.lld
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al

[ 5] .stack PROGBITS 1ffff000 0014f0 000400 00 A 0 0 1

arm-none-eabi-ld
Section Headers:
[Nr] Name Type Addr Off Size ES Flg Lk Inf Al

[ 5] .stack NOBITS 1ffff000 01f000 000400 00 WA 0 0 1

Reading online, I found that I should add the (NOLOAD) directive to my linker script, so I tried the following.
.stack (NOLOAD) : { . += stack_size; } > ram

But this change to the linker script does not seem to affect the output ELF file. Interestingly, if I add the (NOLOAD) directive to the script’s .data section, the .data section will change to type NOBITS.
I’d like to know if I’m making any errors, either in the section definition itself or when I call the linker.

Thank you,

Hello Daniel,

From looking at the code I would expect LLD to give the .stack section type SHT_NOBITS if (NOLOAD) is used. If (NOLOAD) is not used then it is difficult to tell what the type of the section should be as it is likely to depend on the type of the previous Output Section. It is possible that ld.bfd is inheriting the type and LLD is not. I recommend being as explicit as possible in the linker script and using (NOLOAD) if you can.

A change since LLD 10.0 ⚙ D64930 [ELF][AArch64] Allow PT_LOAD to have overlapping p_offset ranges may have made LLD closer to ld.bfd in this area. If you are able to try out LLD 11.0 when it releases (or build it from source) that would be great.

If all else fails it would be great if you could raise a PR with an example so we can look into it. The lld --reproduce=<filename> can capture the dependencies.

Peter

Hello Peter,

Thank you for your help. I finally had some time to build LLD from source and confirmed that the (NOLOAD) directive does work properly in LLD 12.0.

I wanted to find out where this was fixed so I tried some different versions of LLD. I pulled versions 10.1 and 11.0 from llvm-project/releases and found that the issue is solved in both: the .stack section is shown as NOBITS in the elf file.

Best regards,

Daniel Way