[clang] Differential behavior in rodata between clang and gcc
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
example:
```c
#include
void fun01(void)
{
printf("###fun01###\n");
}
void fun02_1(void)
{
printf("###fun02###\n");
}
void fun02_2(void)
{
printf("###fun02###\n");
}
__attribute__((section(".sec03"),noinline))
void fun03(void)
{
printf("###fun03###\n");
}
__attribute__((section(".text"),noinline))
void fun04(void)
{
printf("###fun04###\n");
}
__attribute__((section(".text.sec05"),noinline))
void fun05(void)
{
printf("###fun05###\n");
}
__attribute__((section(".sec06_1"),noinline))
void fun06_1(void)
{
printf("###fun06###\n");
}
__attribute__((section(".sec06_1"),noinline))
void fun06_2(void)
{
printf("###fun06###\n");
}
__attribute__((section(".text.sec07_1"),noinline))
void fun07_1(void)
{
printf("###fun07###\n");
}
__attribute__((section(".text.sec07_2"),noinline))
void fun07_2(void)
{
printf("###fun07###\n");
}
```
version of clang : llvm-20.1.3
version of gcc: gcc-14.3.0
compiler and read section headers:
$clang demo01.c -ffunction-sections -fdata-sections -O2 -c -o demo01-clang.o
$gcc demo01.c -ffunction-sections -fdata-sections -O2 -c -o demo01-gcc.o
$readelf -SW demo01-gcc.o demo01-clang.o
To compare these section headers,we'll see gcc has generated additional sections :
.rodata.fun01.str1
.rodata.fun02_1.str1
.rodata.sec05.str1
.rodata.sec07_1.str1
Strings in functions were placed in separate segments.
But clang do not.
godbolt :
https://c.godbolt.org/z/Ka4TdWxGG
Impact of the difference:
when using u-boottools, only specified segments are loaded into memory, for example __attribute__((__section__(".text.efi_runtime"))) . Some functions in the section have strings. gcc places these strings to section named ".rodata.efi_runtime", but clang places these in defualt section ".rodata.str".
When Using gcc, ".text.efi_runtime" and ".rodata.efi_runtime" can be loaded into memory , functions and strings can be used both. When using clang, only functions in ".text.efi_runtime" be load, without strings.
Despite u-boot having an adaptation solution of this bug:
https://github.com/u-boot/u-boot/commit/530e869ff89d9575103637af201fe97864d4f577
I think this differece should be valued.
Contributor guide
Assessment
This issue has not been assessed yet.