Linker doesn't replace weak functions in assembly with global function in C when LTO is turned on

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-

- Hi,
- As title, my compiler version is RISC-V GCC 9.2.0. This situation didn't
happen in RISC-V GCC 7.1.1 20170509. I don't know whether other
architectures have the same issue. I also post the issue to repo of RISC-V
GCC (https://github.com/riscv/riscv-gcc/issues/172)

If we have a weak function in *weak.S*.

.weak func
.type func, @function

.text
func:
    ret
.size func, .-func

The corresponding global function is in *strong.c*.

#include <stdio.h>
void func() {
    printf("Hello World!\n");
}

Main will call the function in *main.c*.

extern void func();
int main() {
    func();
}

If we compile without *-flto* option, linker will replace the weak function
with global function.
The sequence of linking doesn’t matter.

riscv64-unknown-elf-gcc -c weak.S -o weak.o
riscv64-unknown-elf-gcc -c strong.c -o strong.o
riscv64-unknown-elf-gcc -c main.c -o main.o

riscv64-unknown-elf-gcc strong.o weak.o main.o -o main
riscv64-unknown-elf-readelf -a main | grep "func"# 148:
000000000001015c    28 FUNC    GLOBAL DEFAULT    1 func

riscv64-unknown-elf-gcc weak.o strong.o main.o -o main
riscv64-unknown-elf-readelf -a main | grep "func"# 148:
000000000001015e    28 FUNC    GLOBAL DEFAULT    1 func

If we compile with *-flto* option, linker will use the function in the
later file.

riscv64-unknown-elf-gcc -flto -c weak.S -o weak.o
riscv64-unknown-elf-gcc -flto -c strong.c -o strong.o
riscv64-unknown-elf-gcc -flto -c main.c -o main.o

riscv64-unknown-elf-gcc -flto strong.o weak.o main.o -o main
riscv64-unknown-elf-readelf -a main | grep "func"# 54:
000000000001016e     2 FUNC    WEAK   DEFAULT    1 func

riscv64-unknown-elf-gcc -flto weak.o strong.o main.o -o main
riscv64-unknown-elf-readelf -a main | grep "func"# 23:
000000000001015c    26 FUNC    LOCAL  DEFAULT    1 func

Main will call the function in *main.c*.


- Best Regards,
- Henry Hsieh




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux