Skip to content

Instantly share code, notes, and snippets.

@alonstern
Last active April 14, 2020 11:39
Show Gist options
  • Select an option

  • Save alonstern/2abdc27ce584207149add3ddc1df444b to your computer and use it in GitHub Desktop.

Select an option

Save alonstern/2abdc27ce584207149add3ddc1df444b to your computer and use it in GitHub Desktop.
extract the tags from an elf file
def _generate_tags(self, binary_elf: ELFFile):
text_section = binary_elf.get_section_by_name(".text")
# text_section["sh_addr"] is the address of the .text section.
# We need the addresses of the symbols to be relative to the .text section so we subtract sh_addr from them.
function_addresses = [function_address - text_section["sh_addr"] for function_address in self._get_function_addresses(binary_elf)]
tags = numpy.zeros(text_section.data_size, dtype=int)
tags[function_addresses] = 1
return tags
@staticmethod
def _get_function_addresses(binary_elf):
symbol_table = binary_elf.get_section_by_name(".symtab")
# st_value is the address of the symbol in the binary.
# There are more types of symbol than function so we make sure we only get the function symbols
return [symbol["st_value"] for symbol in symbol_table.iter_symbols()
if symbol["st_info"]["type"] == "STT_FUNC" and symbol["st_size"] != 0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment