localplt.awk 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # This awk script expects to get command-line files that are each
  2. # the output of 'readelf -WSdr' on a single shared object, and named
  3. # .../NAME.jmprel where NAME is the unadorned file name of the shared object.
  4. # It writes "NAME: SYMBOL" for each PLT entry in NAME that refers to a
  5. # symbol defined in the same object.
  6. BEGIN {
  7. result = 0;
  8. pltrelsize = -1;
  9. }
  10. FILENAME != lastfile {
  11. if (lastfile && jmprel_offset == 0 && rela_offset == 0 && rel_offset == 0 \
  12. && relr_offset == 0) {
  13. print FILENAME ": *** failed to find expected output (readelf -WSdr)";
  14. result = 2;
  15. }
  16. if (pltrelsz > 0 && jmprel_offset == -1) {
  17. print FILENAME ": Could not find section for DT_JMPREL";
  18. result = 2;
  19. }
  20. lastfile = FILENAME;
  21. jmprel_offset = 0;
  22. rela_offset = 0;
  23. rel_offset = 0;
  24. relr_offset = 0;
  25. pltrelsz = -1;
  26. delete section_offset_by_address;
  27. }
  28. /^Section Headers:/ { in_shdrs = 1; next }
  29. in_shdrs && !/^ +\[/ { in_shdrs = 0 }
  30. in_shdrs && /^ +\[/ { sub(/\[ +/, "[") }
  31. in_shdrs {
  32. address = strtonum("0x" $4);
  33. offset = strtonum("0x" $5);
  34. section_offset_by_address[address] = offset;
  35. }
  36. in_shdrs { next }
  37. $1 == "Offset" && $2 == "Info" { in_relocs = 1; next }
  38. NF == 0 { in_relocs = 0 }
  39. in_relocs && relocs_offset == jmprel_offset && NF >= 5 {
  40. # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
  41. # value, but rather as the resolver symbol followed by ().
  42. if ($4 ~ /\(\)/) {
  43. print whatfile, gensub(/@.*/, "", "g", $5)
  44. } else {
  45. symval = strtonum("0x" $4);
  46. if (symval != 0)
  47. print whatfile, gensub(/@.*/, "", "g", $5)
  48. }
  49. }
  50. in_relocs && relocs_offset == rela_offset && NF >= 5 {
  51. # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
  52. # value, but rather as the resolver symbol followed by ().
  53. if ($4 ~ /\(\)/) {
  54. print whatfile, gensub(/@.*/, "", "g", $5), "RELA", $3
  55. } else {
  56. symval = strtonum("0x" $4);
  57. if (symval != 0)
  58. print whatfile, gensub(/@.*/, "", "g", $5), "RELA", $3
  59. }
  60. }
  61. in_relocs && relocs_offset == rel_offset && NF >= 5 {
  62. # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
  63. # value, but rather as the resolver symbol followed by ().
  64. if ($4 ~ /\(\)/) {
  65. print whatfile, gensub(/@.*/, "", "g", $5), "REL", $3
  66. } else {
  67. symval = strtonum("0x" $4);
  68. if (symval != 0)
  69. print whatfile, gensub(/@.*/, "", "g", $5), "REL", $3
  70. }
  71. }
  72. # No need to handle DT_RELR (all packed relocations are relative).
  73. in_relocs { next }
  74. $1 == "Relocation" && $2 == "section" && $5 == "offset" {
  75. relocs_offset = strtonum($6);
  76. whatfile = gensub(/^.*\/([^/]+)\.jmprel$/, "\\1:", 1, FILENAME);
  77. next
  78. }
  79. $2 == "(JMPREL)" {
  80. jmprel_addr = strtonum($3);
  81. if (jmprel_addr in section_offset_by_address) {
  82. jmprel_offset = section_offset_by_address[jmprel_addr];
  83. } else {
  84. jmprel_offset = -1
  85. }
  86. next
  87. }
  88. $2 == "(PLTRELSZ)" {
  89. pltrelsz = strtonum($3);
  90. next
  91. }
  92. $2 == "(RELA)" {
  93. rela_addr = strtonum($3);
  94. if (rela_addr in section_offset_by_address) {
  95. rela_offset = section_offset_by_address[rela_addr];
  96. } else {
  97. print FILENAME ": *** DT_RELA does not match any section's address";
  98. result = 2;
  99. }
  100. next
  101. }
  102. $2 == "(REL)" {
  103. rel_addr = strtonum($3);
  104. if (rel_addr in section_offset_by_address) {
  105. rel_offset = section_offset_by_address[rel_addr];
  106. } else {
  107. print FILENAME ": *** DT_REL does not match any section's address";
  108. result = 2;
  109. }
  110. next
  111. }
  112. $2 == "(RELR)" {
  113. relr_addr = strtonum($3);
  114. if (relr_addr in section_offset_by_address) {
  115. relr_offset = section_offset_by_address[relr_addr];
  116. } else {
  117. print FILENAME ": *** DT_RELR does not match any section's address";
  118. result = 2;
  119. }
  120. }
  121. END { exit(result) }