[llvm-dev] [ELF] String literals don't obey -fdata-sections
Gaƫl Jobin via llvm-dev
llvm-dev at lists.llvm.org
Tue Sep 15 13:32:59 PDT 2020
Hi there,
When I compile my code with -fdata-sections and -ffunction-sections, I
still see some unused string in my shared library (Android). Actually,
the strings appear together inside a .rodata.str1.1 section instead of
getting their own section. It seems that the C-string literal are
considered differently than other constant and the -fdata-sections is
not respected in
https://github.com/llvm/llvm-project/blob/master/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp#L799.
[1] I came across the following GCC bug
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=192 where they have fixed
the issue back in 2015. Any reason not to do so in LLVM?
My code example:
- static library 1 : expose functions api1() and api3()
> #include "lib1.h"
>
> static char *test = "Test";
> static char *unused = "Unused";
>
> void api1(){
> printf(test);
> }
>
> void api3(){
> printf(unused);
> }
- shared library : use only function api1() from static library 1
> #include "lib1.h"
>
> void test(){
> api1();
> }
Both compiled with "-fdata-sections -ffunction-sections
-fvisibility=hidden" and linked with "--gc-sections".
While the api3() function is correctly gone, the result for the C-string
is the following (in Hopper):
> ; Section .rodata.str1.1
>
> ; Range: [0x63; 0x6f[ (12 bytes)
>
> ; File offset : [151; 163[ (12 bytes)
>
> ; Flags: 0x32
>
> ; SHT_PROGBITS
>
> ; SHF_ALLOC
>
> .L.str:
>
> 00000063 db "Test", 0
>
> .L.str.1:
>
> 00000068 db "Unused", 0
Links:
------
[1]
https://github.com/llvm/llvm-project/blob/master/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp#L799
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200915/f44bd730/attachment.html>
More information about the llvm-dev
mailing list