localplt.awk 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 { result = 0 }
  7. FILENAME != lastfile {
  8. if (lastfile && jmprel_offset == 0 && rela_offset == 0 && rel_offset == 0) {
  9. print FILENAME ": *** failed to find expected output (readelf -WSdr)";
  10. result = 2;
  11. }
  12. lastfile = FILENAME;
  13. jmprel_offset = 0;
  14. rela_offset = 0;
  15. rel_offset = 0;
  16. delete section_offset_by_address;
  17. }
  18. /^Section Headers:/ { in_shdrs = 1; next }
  19. in_shdrs && !/^ +\[/ { in_shdrs = 0 }
  20. in_shdrs && /^ +\[/ { sub(/\[ +/, "[") }
  21. in_shdrs {
  22. address = strtonum("0x" $4);
  23. offset = strtonum("0x" $5);
  24. section_offset_by_address[address] = offset;
  25. }
  26. in_shdrs { next }
  27. $1 == "Offset" && $2 == "Info" { in_relocs = 1; next }
  28. NF == 0 { in_relocs = 0 }
  29. in_relocs && relocs_offset == jmprel_offset && NF >= 5 {
  30. # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
  31. # value, but rather as the resolver symbol followed by ().
  32. if ($4 ~ /\(\)/) {
  33. print whatfile, gensub(/@.*/, "", "g", $5)
  34. } else {
  35. symval = strtonum("0x" $4);
  36. if (symval != 0)
  37. print whatfile, gensub(/@.*/, "", "g", $5)
  38. }
  39. }
  40. in_relocs && relocs_offset == rela_offset && NF >= 5 {
  41. # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
  42. # value, but rather as the resolver symbol followed by ().
  43. if ($4 ~ /\(\)/) {
  44. print whatfile, gensub(/@.*/, "", "g", $5), "RELA", $3
  45. } else {
  46. symval = strtonum("0x" $4);
  47. if (symval != 0)
  48. print whatfile, gensub(/@.*/, "", "g", $5), "RELA", $3
  49. }
  50. }
  51. in_relocs && relocs_offset == rel_offset && NF >= 5 {
  52. # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
  53. # value, but rather as the resolver symbol followed by ().
  54. if ($4 ~ /\(\)/) {
  55. print whatfile, gensub(/@.*/, "", "g", $5), "REL", $3
  56. } else {
  57. symval = strtonum("0x" $4);
  58. if (symval != 0)
  59. print whatfile, gensub(/@.*/, "", "g", $5), "REL", $3
  60. }
  61. }
  62. in_relocs { next }
  63. $1 == "Relocation" && $2 == "section" && $5 == "offset" {
  64. relocs_offset = strtonum($6);
  65. whatfile = gensub(/^.*\/([^/]+)\.jmprel$/, "\\1:", 1, FILENAME);
  66. next
  67. }
  68. $2 == "(JMPREL)" {
  69. jmprel_addr = strtonum($3);
  70. if (jmprel_addr in section_offset_by_address) {
  71. jmprel_offset = section_offset_by_address[jmprel_addr];
  72. } else {
  73. print FILENAME ": *** DT_JMPREL does not match any section's address";
  74. result = 2;
  75. }
  76. next
  77. }
  78. $2 == "(RELA)" {
  79. rela_addr = strtonum($3);
  80. if (rela_addr in section_offset_by_address) {
  81. rela_offset = section_offset_by_address[rela_addr];
  82. } else {
  83. print FILENAME ": *** DT_RELA does not match any section's address";
  84. result = 2;
  85. }
  86. next
  87. }
  88. $2 == "(REL)" {
  89. rel_addr = strtonum($3);
  90. if (rel_addr in section_offset_by_address) {
  91. rel_offset = section_offset_by_address[rel_addr];
  92. } else {
  93. print FILENAME ": *** DT_REL does not match any section's address";
  94. result = 2;
  95. }
  96. next
  97. }
  98. END { exit(result) }