Blame scripts/localplt.awk

Packit 6c4009
# This awk script expects to get command-line files that are each
Packit 6c4009
# the output of 'readelf -WSdr' on a single shared object, and named
Packit 6c4009
# .../NAME.jmprel where NAME is the unadorned file name of the shared object.
Packit 6c4009
# It writes "NAME: SYMBOL" for each PLT entry in NAME that refers to a
Packit 6c4009
# symbol defined in the same object.
Packit 6c4009
Packit 6c4009
BEGIN { result = 0 }
Packit 6c4009
Packit 6c4009
FILENAME != lastfile {
Packit 6c4009
  if (lastfile && jmprel_offset == 0 && rela_offset == 0 && rel_offset == 0) {
Packit 6c4009
    print FILENAME ": *** failed to find expected output (readelf -WSdr)";
Packit 6c4009
    result = 2;
Packit 6c4009
  }
Packit 6c4009
  lastfile = FILENAME;
Packit 6c4009
  jmprel_offset = 0;
Packit 6c4009
  rela_offset = 0;
Packit 6c4009
  rel_offset = 0;
Packit 6c4009
  delete section_offset_by_address;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/^Section Headers:/ { in_shdrs = 1; next }
Packit 6c4009
in_shdrs && !/^ +\[/ { in_shdrs = 0 }
Packit 6c4009
Packit 6c4009
in_shdrs && /^ +\[/ { sub(/\[ +/, "[") }
Packit 6c4009
in_shdrs {
Packit 6c4009
  address = strtonum("0x" $4);
Packit 6c4009
  offset = strtonum("0x" $5);
Packit 6c4009
  section_offset_by_address[address] = offset;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
in_shdrs { next }
Packit 6c4009
Packit 6c4009
$1 == "Offset" && $2 == "Info" { in_relocs = 1; next }
Packit 6c4009
NF == 0 { in_relocs = 0 }
Packit 6c4009
Packit 6c4009
in_relocs && relocs_offset == jmprel_offset && NF >= 5 {
Packit 6c4009
  # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
Packit 6c4009
  # value, but rather as the resolver symbol followed by ().
Packit 6c4009
  if ($4 ~ /\(\)/) {
Packit 6c4009
    print whatfile, gensub(/@.*/, "", "g", $5)
Packit 6c4009
  } else {
Packit 6c4009
    symval = strtonum("0x" $4);
Packit 6c4009
    if (symval != 0)
Packit 6c4009
      print whatfile, gensub(/@.*/, "", "g", $5)
Packit 6c4009
  }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
in_relocs && relocs_offset == rela_offset && NF >= 5 {
Packit 6c4009
  # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
Packit 6c4009
  # value, but rather as the resolver symbol followed by ().
Packit 6c4009
  if ($4 ~ /\(\)/) {
Packit 6c4009
    print whatfile, gensub(/@.*/, "", "g", $5), "RELA", $3
Packit 6c4009
  } else {
Packit 6c4009
    symval = strtonum("0x" $4);
Packit 6c4009
    if (symval != 0)
Packit 6c4009
      print whatfile, gensub(/@.*/, "", "g", $5), "RELA", $3
Packit 6c4009
  }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
in_relocs && relocs_offset == rel_offset && NF >= 5 {
Packit 6c4009
  # Relocations against GNU_IFUNC symbols are not shown as an hexadecimal
Packit 6c4009
  # value, but rather as the resolver symbol followed by ().
Packit 6c4009
  if ($4 ~ /\(\)/) {
Packit 6c4009
    print whatfile, gensub(/@.*/, "", "g", $5), "REL", $3
Packit 6c4009
  } else {
Packit 6c4009
    symval = strtonum("0x" $4);
Packit 6c4009
    if (symval != 0)
Packit 6c4009
      print whatfile, gensub(/@.*/, "", "g", $5), "REL", $3
Packit 6c4009
  }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
in_relocs { next }
Packit 6c4009
Packit 6c4009
$1 == "Relocation" && $2 == "section" && $5 == "offset" {
Packit 6c4009
  relocs_offset = strtonum($6);
Packit 6c4009
  whatfile = gensub(/^.*\/([^/]+)\.jmprel$/, "\\1:", 1, FILENAME);
Packit 6c4009
  next
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
$2 == "(JMPREL)" {
Packit 6c4009
  jmprel_addr = strtonum($3);
Packit 6c4009
  if (jmprel_addr in section_offset_by_address) {
Packit 6c4009
    jmprel_offset = section_offset_by_address[jmprel_addr];
Packit 6c4009
  } else {
Packit 6c4009
    print FILENAME ": *** DT_JMPREL does not match any section's address";
Packit 6c4009
    result = 2;
Packit 6c4009
  }
Packit 6c4009
  next
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
$2 == "(RELA)" {
Packit 6c4009
  rela_addr = strtonum($3);
Packit 6c4009
  if (rela_addr in section_offset_by_address) {
Packit 6c4009
    rela_offset = section_offset_by_address[rela_addr];
Packit 6c4009
  } else {
Packit 6c4009
    print FILENAME ": *** DT_RELA does not match any section's address";
Packit 6c4009
    result = 2;
Packit 6c4009
  }
Packit 6c4009
  next
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
$2 == "(REL)" {
Packit 6c4009
  rel_addr = strtonum($3);
Packit 6c4009
  if (rel_addr in section_offset_by_address) {
Packit 6c4009
    rel_offset = section_offset_by_address[rel_addr];
Packit 6c4009
  } else {
Packit 6c4009
    print FILENAME ": *** DT_REL does not match any section's address";
Packit 6c4009
    result = 2;
Packit 6c4009
  }
Packit 6c4009
  next
Packit 6c4009
}
Packit 6c4009
END { exit(result) }