Advertisement
Guest User

test_objects_lto.sh

a guest
Nov 10th, 2020
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.53 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. set -x
  4.  
  5. GCC=gcc
  6. OBJDUMP=objdump
  7.  
  8. # Create three source files: test1.c, test2.c, test3.c
  9. cat <<EOT > test1.c
  10. int func1();
  11. int func2();
  12. int func3();
  13.  
  14. int func1() {
  15.   return 1;
  16. }
  17. EOT
  18.  
  19. cat <<EOT > test2.c
  20. int func1();
  21. int func2();
  22. int func3();
  23.  
  24. int func2() {
  25.   return 2 + func1();
  26. }
  27. EOT
  28.  
  29. cat <<EOT > test3.c
  30. int func1();
  31. int func2();
  32. int func3();
  33.  
  34. int func3() {
  35.   return 3 + func2();
  36. }
  37. EOT
  38.  
  39. OPTIM_LEVEL=2
  40.  
  41. # Compile them with LTO
  42. $GCC -c test1.c -flto -O$OPTIM_LEVEL -o test1.o
  43. $GCC -c test2.c -flto -O$OPTIM_LEVEL -o test2.o
  44. $GCC -c test3.c -flto -O$OPTIM_LEVEL -o test3.o
  45.  
  46. # Get list of sections (notice that .text is empty and there are many .gnu.lto_ sections that contain IL)
  47. $OBJDUMP -h test1.o test2.o test3.o
  48.  
  49. # Link a relocatable object, get 1 .o file instead of 3 different ones, also compile LTO IL into x86 machine code
  50. # NOTE:
  51. #   for GCC<9, -flinker-output does not support `nolto-rel`, `rel` should be used instead
  52. #   for GCC>=9, -flinker-output=nolto-rel is REQUIRED to compile LTO IL into machine code
  53. $GCC -r -nostdlib -O$OPTIM_LEVEL -flinker-output=nolto-rel test1.o test2.o test3.o -o reloc.o
  54.  
  55. # Get list of sections (notice that .gnu.lto_ sections are gone and we have normal .text filled with machine code)
  56. $OBJDUMP -h reloc.o
  57.  
  58. # A small test program
  59. cat <<EOT > main.c
  60. #include <stdio.h>
  61.  
  62. int func3();
  63.  
  64. int main() {
  65.   printf("%d\n", func3());
  66.   return 0;
  67. }
  68. EOT
  69.  
  70. # Compile and link app with relocatable object
  71. $GCC main.c reloc.o -o main
  72.  
  73. # Run app
  74. ./main
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement