llvm-nm yields no useful information for files compiled with clang+lto
| Bugzilla Link | 34395 |
| Version | 5.0 |
| OS | Linux |
| CC | @rupprecht |
Extended Description
llvm-nm doesn't returns nothing useful from files compiled with LTO.
If we have the file test.c containing ''' char a[10]; char b[20]; char c[30]; '''
and run $CC -flto -c test.c -o test.o the result of llvm-nm -P test.o turns out to be the following
'''
a C ---------------- 0
b C ---------------- 0
c C ---------------- 0
'''
I do understand address/offset information being unavailable, but shouldn't this output encode at least something concerning each symbol's value/size?
(retitling to make this clang specific -- see llvm/llvm-bugzilla-archive#41437 for gcc)
FWIW, gcc+nm seems to also not provide useful data for these fields when using lto. So I'm not sure if this is really a bug? Unless printing zeroes instead of dashes is important?
$ gcc -c /tmp/test.c -o /tmp/test.o && nm -P /tmp/test.o a C 000000000000000a 000000000000000a b C 0000000000000014 0000000000000014 c C 000000000000001e 000000000000001e
$ gcc -flto -c /tmp/test.c -o /tmp/test.o && nm -P /tmp/test.o a C 00000000 b C 00000000 c C 00000000
Just hit that today on llvm 17 and llvm 18:
$ file object.o
object.o: LLVM IR bitcode
$ llvm-nm-18 -S object.o
---------------- 0000000000000000 d LISA_C_CONST_VALUE
$ llvm-dis-18 object.o
$ cat object.o.ll
[...]
@LISA_C_CONST_VALUE = internal global [8 x i8] zeroinitializer, align 1, !dbg !0
[...]
So the weird thing is that the IR clearly knows LISA_C_CONST_VALUE is an array of char of size 8 ([8 x i8]), so the info does exist, it's just that llvm-nm does not extract it.
This is quite unfortunate as I based a hack to print the value of C compile-time constants using the array size, later displayed as the symbol size by nm. It probably will require disabling LTO (which is not possible in combination with -fsanitize=cfi), or at least convince clang to emit a regular object file with the bitcode in a section rather than just a bitcode file.
EDIT: as a workaround, the -ffat-lto-objects option forces clang to create an ELF object file rather than just raw LLVM bitcode, but this option is only available starting from clang 18.