From aae158d530cf6ac4b5adf365f8c87d9384ef38a3 Mon Sep 17 00:00:00 2001 From: Goomelo <1034570130@qq.com> Date: Thu, 12 May 2022 10:13:51 +0800 Subject: [PATCH] add edit.py to edit prebuilt elf files automatically --- bionic/prebuilt-elf-files/edit.py | 47 +++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 bionic/prebuilt-elf-files/edit.py diff --git a/bionic/prebuilt-elf-files/edit.py b/bionic/prebuilt-elf-files/edit.py new file mode 100644 index 0000000..3eeaceb --- /dev/null +++ b/bionic/prebuilt-elf-files/edit.py @@ -0,0 +1,47 @@ +from __future__ import print_function +import sys + +# If pyelftools is not installed, the example can also run from the root or +# examples/ dir of the source distribution. +sys.path[0:0] = ['.', '..'] + +from elftools.elf.elffile import ELFFile +from elftools.elf.sections import SymbolTableSection + + +def process_file(filename): + + #use map as "switch case" to decide which file is being edited + files = { + "libtest_invalid-zero_shdr_table_content.so": invalid_zero_shdr_table_content_FileEdit + } + + print('Processing file:', filename) + method = files.get(filename) + with open(filename, 'rb+') as f: + if method: + method(f) + +def invalid_zero_shdr_table_content_FileEdit(stream): + elfFile = ELFFile(stream) + elfSection = elfFile.get_section_by_name('.dynamic') + + if not elfSection: + print('.dynamic') + return + + section_type = elfSection['sh_type'] + if section_type != 'SHT_DYNAMIC': + print('wrong type') + return + + #section header's offset is 0xb04 + #sh_size in the section header is the size of section area instead of that of section header's + stream.seek(0xb04) + stream.write(b'\x00') + +if __name__ == '__main__': + if sys.argv[1] == '--test': + for filename in sys.argv[2:]: + process_file(filename) + -- Gitee