Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- set -x
- GCC=gcc
- OBJDUMP=objdump
- # Create three source files: test1.c, test2.c, test3.c
- cat <<EOT > test1.c
- int func1();
- int func2();
- int func3();
- int func1() {
- return 1;
- }
- EOT
- cat <<EOT > test2.c
- int func1();
- int func2();
- int func3();
- int func2() {
- return 2 + func1();
- }
- EOT
- cat <<EOT > test3.c
- int func1();
- int func2();
- int func3();
- int func3() {
- return 3 + func2();
- }
- EOT
- OPTIM_LEVEL=2
- # Compile them with LTO
- $GCC -c test1.c -flto -O$OPTIM_LEVEL -o test1.o
- $GCC -c test2.c -flto -O$OPTIM_LEVEL -o test2.o
- $GCC -c test3.c -flto -O$OPTIM_LEVEL -o test3.o
- # Get list of sections (notice that .text is empty and there are many .gnu.lto_ sections that contain IL)
- $OBJDUMP -h test1.o test2.o test3.o
- # Link a relocatable object, get 1 .o file instead of 3 different ones, also compile LTO IL into x86 machine code
- # NOTE:
- # for GCC<9, -flinker-output does not support `nolto-rel`, `rel` should be used instead
- # for GCC>=9, -flinker-output=nolto-rel is REQUIRED to compile LTO IL into machine code
- $GCC -r -nostdlib -O$OPTIM_LEVEL -flinker-output=nolto-rel test1.o test2.o test3.o -o reloc.o
- # Get list of sections (notice that .gnu.lto_ sections are gone and we have normal .text filled with machine code)
- $OBJDUMP -h reloc.o
- # A small test program
- cat <<EOT > main.c
- #include <stdio.h>
- int func3();
- int main() {
- printf("%d\n", func3());
- return 0;
- }
- EOT
- # Compile and link app with relocatable object
- $GCC main.c reloc.o -o main
- # Run app
- ./main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement