|
| 1 | +"""Utilities for binary file manipulation""" |
| 2 | +import subprocess |
| 3 | +from os.path import join, exists |
| 4 | +from . import util |
| 5 | +from .._ffi.base import py_str |
| 6 | +from ..api import register_func, convert |
| 7 | + |
| 8 | + |
| 9 | +@register_func("tvm_get_section_size") |
| 10 | +def tvm_get_section_size(binary_name, section): |
| 11 | + """Finds size of the section in the binary. |
| 12 | + Assumes "size" shell command exists (typically works only on Linux machines) |
| 13 | +
|
| 14 | + Parameters |
| 15 | + ---------- |
| 16 | + binary_name : string |
| 17 | + name of the binary file |
| 18 | +
|
| 19 | + section : string |
| 20 | + type of section |
| 21 | +
|
| 22 | + Return |
| 23 | + ------ |
| 24 | + size : integer |
| 25 | + size of the section in bytes |
| 26 | + """ |
| 27 | + section_map = {"text": "1", "data": "2", "bss": "3"} |
| 28 | + p1 = subprocess.Popen(["size", binary_name], stdout=subprocess.PIPE) |
| 29 | + p2 = subprocess.Popen(["awk", "{print $" + section_map[section] + "}"], |
| 30 | + stdin=p1.stdout, stdout=subprocess.PIPE) |
| 31 | + p3 = subprocess.Popen(["tail", "-1"], stdin=p2.stdout, stdout=subprocess.PIPE) |
| 32 | + p1.stdout.close() |
| 33 | + p2.stdout.close() |
| 34 | + (out, _) = p3.communicate() |
| 35 | + if p3.returncode != 0: |
| 36 | + msg = "Error in finding section size:\n" |
| 37 | + msg += py_str(out) |
| 38 | + raise RuntimeError(msg) |
| 39 | + return int(out) |
| 40 | + |
| 41 | + |
| 42 | +@register_func("tvm_relocate_binary") |
| 43 | +def tvm_relocate_binary(binary_name, text, data, bss): |
| 44 | + """Relocates sections in the binary to new addresses |
| 45 | +
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + binary_name : string |
| 49 | + name of the binary file |
| 50 | +
|
| 51 | + text : string |
| 52 | + text section address |
| 53 | +
|
| 54 | + data : string |
| 55 | + data section address |
| 56 | +
|
| 57 | + bss : string |
| 58 | + bss section address |
| 59 | +
|
| 60 | + Return |
| 61 | + ------ |
| 62 | + rel_bin : bytearray |
| 63 | + the relocated binary |
| 64 | + """ |
| 65 | + tmp_dir = util.tempdir() |
| 66 | + rel_obj = tmp_dir.relpath("relocated.o") |
| 67 | + p1 = subprocess.Popen(["ld", binary_name, |
| 68 | + "-Ttext", text, |
| 69 | + "-Tdata", data, |
| 70 | + "-Tbss", bss, |
| 71 | + "-o", rel_obj], |
| 72 | + stdout=subprocess.PIPE, |
| 73 | + stderr=subprocess.STDOUT) |
| 74 | + (out, _) = p1.communicate() |
| 75 | + if p1.returncode != 0: |
| 76 | + msg = "Linking error using ld:\n" |
| 77 | + msg += py_str(out) |
| 78 | + raise RuntimeError(msg) |
| 79 | + rel_bin = bytearray(open(rel_obj, "rb").read()) |
| 80 | + return rel_bin |
| 81 | + |
| 82 | + |
| 83 | +@register_func("tvm_read_binary_section") |
| 84 | +def tvm_read_binary_section(binary_name, section): |
| 85 | + """Returns the contents of the specified section in the binary file |
| 86 | +
|
| 87 | + Parameters |
| 88 | + ---------- |
| 89 | + binary_name : string |
| 90 | + name of the binary file |
| 91 | +
|
| 92 | + section : string |
| 93 | + type of section |
| 94 | +
|
| 95 | + Return |
| 96 | + ------ |
| 97 | + section_bin : bytearray |
| 98 | + contents of the read section |
| 99 | + """ |
| 100 | + tmp_dir = util.tempdir() |
| 101 | + tmp_section = tmp_dir.relpath("tmp_section.bin") |
| 102 | + p1 = subprocess.Popen(["objcopy", "--dump-section", |
| 103 | + "." + section + "=" + tmp_section, |
| 104 | + binary_name], |
| 105 | + stdout=subprocess.PIPE, |
| 106 | + stderr=subprocess.STDOUT) |
| 107 | + (out, _) = p1.communicate() |
| 108 | + if p1.returncode != 0: |
| 109 | + msg = "Error in using objcopy:\n" |
| 110 | + msg += py_str(out) |
| 111 | + raise RuntimeError(msg) |
| 112 | + try: |
| 113 | + # get section content if it exits |
| 114 | + section_bin = bytearray(open(tmp_section, "rb").read()) |
| 115 | + except IOError: |
| 116 | + # return empty bytearray if the section does not exist |
| 117 | + section_bin = bytearray("") |
| 118 | + return section_bin |
| 119 | + |
| 120 | + |
| 121 | +@register_func("tvm_get_symbol_map") |
| 122 | +def tvm_get_symbol_map(binary): |
| 123 | + """Obtains a map of symbols to addresses in the passed binary |
| 124 | +
|
| 125 | + Parameters |
| 126 | + ---------- |
| 127 | + binary : bytearray |
| 128 | + the object file |
| 129 | +
|
| 130 | + Return |
| 131 | + ------ |
| 132 | + symbol_map : dictionary |
| 133 | + map of defined symbols to addresses |
| 134 | + """ |
| 135 | + tmp_dir = util.tempdir() |
| 136 | + tmp_obj = tmp_dir.relpath("tmp_obj.bin") |
| 137 | + with open(tmp_obj, "wb") as out_file: |
| 138 | + out_file.write(bytes(binary)) |
| 139 | + p1 = subprocess.Popen(["nm", "-C", "--defined-only", tmp_obj], |
| 140 | + stdout=subprocess.PIPE, |
| 141 | + stderr=subprocess.STDOUT) |
| 142 | + (out, _) = p1.communicate() |
| 143 | + if p1.returncode != 0: |
| 144 | + msg = "Error in using nm:\n" |
| 145 | + msg += py_str(out) |
| 146 | + raise RuntimeError(msg) |
| 147 | + out = out.splitlines() |
| 148 | + map_str = "" |
| 149 | + for line in out: |
| 150 | + line = line.split() |
| 151 | + map_str += line[2] + "\n" |
| 152 | + map_str += line[0] + "\n" |
| 153 | + return map_str |
0 commit comments