diff options
author | Dan Albert <danalbert@google.com> | 2017-01-31 14:55:56 -0800 |
---|---|---|
committer | Dan Albert <danalbert@google.com> | 2017-02-01 14:05:15 -0800 |
commit | 173af83beaf3b4215fb718aeb9b6ec8e9d9cb857 (patch) | |
tree | 809c1c741a60cca7c64f5c876d96afcb895cece4 /parse_elfnote.py | |
parent | 9d2434d6db4df007185174f9931e5058392bf5b2 (diff) | |
download | ndk-173af83beaf3b4215fb718aeb9b6ec8e9d9cb857.tar.gz |
Fix parse_elfnote.py.
The first column of readelf -S output can be split into multiple items
by `split`:
[ 8] .note.android.ide NOTE 00000000 0000ec 000098 00 A 0 0 4
[ 9] .comment PROGBITS 00000000 000184 000028 01 MS 0 0 1
[10] .rel.text REL 00000000 0001ac 000048 08 I 13 1 4
[11] .note.GNU-stack PROGBITS 00000000 0001f4 000000 00 0 0 0
Skip until past the first closing bracket since we don't care about
the index anyway.
Also called readelf with -W to get wide output. Addresses for 64-bit
binaries were large enough to cause the data to be displayed across
two rows.
Bug: None
Test: ran parse_elfnote.py
Change-Id: Ica8cc5f232ebe333137f324c07979bfe7f6e1e1c
Diffstat (limited to 'parse_elfnote.py')
-rw-r--r-- | parse_elfnote.py | 58 |
1 files changed, 47 insertions, 11 deletions
diff --git a/parse_elfnote.py b/parse_elfnote.py index a5aae1a11..5f84b72a7 100644 --- a/parse_elfnote.py +++ b/parse_elfnote.py @@ -16,17 +16,23 @@ # import argparse +import logging import subprocess import sys from ctypes import c_char from ctypes import c_int from ctypes import Structure -SEC_NAME = '.note.android.ide' +SEC_NAME = '.note.android.ident' ABI_VENDOR = 'Android' NDK_RESERVED_SIZE = 64 +def logger(): + """Returns the module logger.""" + return logging.getLogger(__name__) + + class AbiTag(Structure): _fields_ = [('namesz', c_int), ('descsz', c_int), @@ -39,17 +45,32 @@ class AbiTag(Structure): # Get the offset to a section from the output of readelf def get_section_pos(sec_name, file_path): - cmd = ['readelf', '--sections', file_path] + cmd = ['readelf', '--sections', '-W', file_path] output = subprocess.check_output(cmd) lines = output.split('\n') for line in lines: - if sec_name in line: - sections = line.split() - if len(sections) < 6 or sec_name not in sections[1]: - sys.exit('Failed to get offset of {}'.format(sec_name)) - addr = int(sections[3], 16) - off = int(sections[4], 16) - return addr + off + logger().debug('Checking line for "%s": %s', sec_name, line) + # Looking for a line like the following (all whitespace of unknown + # width). + # + # [ 8] .note.android.ident NOTE 00000000 0000ec 000098 00 A 0 0 4 + # + # The only column that might have internal whitespace is the first one. + # Since we don't care about it, remove the head of the string until the + # closing bracket, then split. + if sec_name not in line: + continue + if ']' not in line: + continue + line = line[line.index(']') + 1:].strip() + + sections = line.split() + if len(sections) < 5 or sec_name not in sections[0]: + logger().debug('Did not find "%s" in %s', sec_name, sections[0]) + sys.exit('Failed to get offset of {}'.format(sec_name)) + addr = int(sections[2], 16) + off = int(sections[3], 16) + return addr + off sys.exit('Failed to find section: {}'.format(sec_name)) @@ -62,11 +83,26 @@ def print_info(tag): print 'ABI_NDK_BUILD_NUMBER: {}'.format(tag.ndk_build_number) -def main(): +def parse_args(): + """Parses command line arguments.""" parser = argparse.ArgumentParser() parser.add_argument('file_path', help="path of the ELF file with embedded ABI tags") - args = parser.parse_args() + parser.add_argument( + '-v', '--verbose', dest='verbosity', action='count', default=0, + help='Increase logging verbosity.') + return parser.parse_args() + + +def main(): + args = parse_args() + if args.verbosity == 1: + logging.basicConfig(level=logging.INFO) + elif args.verbosity >= 2: + logging.basicConfig(level=logging.DEBUG) + else: + logging.basicConfig() + file_path = args.file_path with open(file_path, "rb") as obj_file: |