I am a compiler engineer currently working on a project to run Grub2 with Clang + LLVM instead of GCC. I would like to discuss a specific expectation regarding the behavior of LLVM when using both the mcmodel=large
flag and the Position-Independent Code (PIC) option (-fPIE
/-fPIC
).
Currently, I have observed that the generated code remains the same whether -fPIE
is present or not when using mcmodel=large
. Here are the examples:
test.c:
const char *test(void){
return "xx";
}
execution with -fPIE
:
$ clang -fPIE -o test.o -c test.c -mcmodel=large
$ readelf -r test.o
Relocation section '.rela.text' at offset 0x1c0 contains 4 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000000 000700000108 R_AARCH64_MOVW_UA 0000000000000000 .rodata.str1.1 + 0
000000000004 00070000010a R_AARCH64_MOVW_UA 0000000000000000 .rodata.str1.1 + 0
000000000008 00070000010c R_AARCH64_MOVW_UA 0000000000000000 .rodata.str1.1 + 0
00000000000c 00070000010d R_AARCH64_MOVW_UA 0000000000000000 .rodata.str1.1 + 0
Relocation section '.rela.eh_frame' at offset 0x220 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
00000000001c 000600000104 R_AARCH64_PREL64 0000000000000000 .text + 0
execution without -fPIE
:
$ clang -o test.o -c test.c -mcmodel=large
$ readelf -r test.o
Relocation section '.rela.text' at offset 0x1c0 contains 4 entries:
Offset Info Type Sym. Value Sym. Name + Addend
000000000000 000700000108 R_AARCH64_MOVW_UA 0000000000000000 .rodata.str1.1 + 0
000000000004 00070000010a R_AARCH64_MOVW_UA 0000000000000000 .rodata.str1.1 + 0
000000000008 00070000010c R_AARCH64_MOVW_UA 0000000000000000 .rodata.str1.1 + 0
00000000000c 00070000010d R_AARCH64_MOVW_UA 0000000000000000 .rodata.str1.1 + 0
Relocation section '.rela.eh_frame' at offset 0x220 contains 1 entry:
Offset Info Type Sym. Value Sym. Name + Addend
00000000001c 000600000104 R_AARCH64_PREL64 0000000000000000 .text + 0
I believe it would be beneficial to have a distinct generated code when both options are used together. Hence, I would like to suggest that LLVM generates group relocations, such as MOVW_PREL_G0
, when both the -fPIC
option and mcmodel=large
flag are used during compilation.
Thank you for your attention, and I look forward to your response.