Can't add the allocated section to elf file using inline assembly
Fangrui Song
i at maskray.me
Sat Nov 30 08:50:59 UTC 2019
On 2019-11-08, drug wrote:
>On 11/7/19 6:20 PM, kinke wrote:
>>On Thursday, 7 November 2019 at 14:58:24 UTC, drug wrote:
>>>What can be the reason of this?
>>
>>--gc-sections applied by LDC by default - use
>>-disable-linker-strip-dead to keep unreferenced sections.
>
>Thank you very much! It works now.
>
>What is the best way to create a reference to this section to prevent
>its stripping?
Use the .reloc directive and an R_*_NONE (no-op) relocation to reference
the section from a known-live section (e.g. main). As an example:
import ldc.llvmasm;
void foo() {
__asm!void(
`.pushsection .stapsdt.base,"aG",%progbits,.stapsdt.base,comdat
.weak _.stapsdt.base
.hidden _.stapsdt.base
_.stapsdt.base: .space 1
.size _.stapsdt.base,1
.popsection`, ""
);
}
void main() {
__asm!void(".reloc 0, R_X86_64_NONE, .stapsdt.base", "");
}
This works with GNU ld, gold and lld, and it does not require
-disable-linker-strip-dead. Parsing R_*_NONE requires llvm 9 or newer.
I added .reloc for x86/ARM/AArch64/PowerPC in May 2019. R_MIPS_NONE
worked even before that time.
More information about the digitalmars-d-ldc
mailing list