Generate_eh_elf: apply black

This commit is contained in:
Théophile Bastian 2019-07-16 11:18:06 +02:00
parent b9c6f748ce
commit 382914d193

View file

@ -13,53 +13,52 @@ import tempfile
import argparse import argparse
from enum import Enum from enum import Enum
from shared_python import \ from shared_python import (
elf_so_deps, \ elf_so_deps,
do_remote, \ do_remote,
is_newer, \ is_newer,
to_eh_elf_path, \ to_eh_elf_path,
find_eh_elf_dir, \ find_eh_elf_dir,
DEFAULT_AUX_DIRS DEFAULT_AUX_DIRS,
)
from extract_pc import generate_pc_list from extract_pc import generate_pc_list
DWARF_ASSEMBLY_BIN = os.path.join( DWARF_ASSEMBLY_BIN = os.path.join(
os.path.dirname(os.path.abspath(sys.argv[0])), os.path.dirname(os.path.abspath(sys.argv[0])), "dwarf-assembly"
'dwarf-assembly') )
C_BIN = ( C_BIN = "gcc" if "C" not in os.environ else os.environ["C"]
'gcc' if 'C' not in os.environ
else os.environ['C'])
class SwitchGenPolicy(Enum): class SwitchGenPolicy(Enum):
''' The various switch generation policies possible ''' """ The various switch generation policies possible """
SWITCH_PER_FUNC = '--switch-per-func'
GLOBAL_SWITCH = '--global-switch' SWITCH_PER_FUNC = "--switch-per-func"
GLOBAL_SWITCH = "--global-switch"
class Config: class Config:
''' Holds the run's settings ''' """ Holds the run's settings """
default_aux = DEFAULT_AUX_DIRS default_aux = DEFAULT_AUX_DIRS
def __init__(self, def __init__(
output, self,
aux, output,
no_dft_aux, aux,
objects, no_dft_aux,
sw_gen_policy=SwitchGenPolicy.GLOBAL_SWITCH, objects,
force=False, sw_gen_policy=SwitchGenPolicy.GLOBAL_SWITCH,
use_pc_list=False, force=False,
c_opt_level='3', use_pc_list=False,
enable_deref_arg=False, c_opt_level="3",
keep_holes=False, enable_deref_arg=False,
cc_debug=False, keep_holes=False,
remote=None): cc_debug=False,
self.output = '.' if output is None else output remote=None,
self.aux = ( ):
aux self.output = "." if output is None else output
+ ([] if no_dft_aux else self.default_aux) self.aux = aux + ([] if no_dft_aux else self.default_aux)
)
self.objects = objects self.objects = objects
self.sw_gen_policy = sw_gen_policy self.sw_gen_policy = sw_gen_policy
self.force = force self.force = force
@ -72,65 +71,64 @@ class Config:
@staticmethod @staticmethod
def default_aux_str(): def default_aux_str():
return ', '.join(Config.default_aux) return ", ".join(Config.default_aux)
def dwarf_assembly_args(self): def dwarf_assembly_args(self):
''' Arguments to `dwarf_assembly` ''' """ Arguments to `dwarf_assembly` """
out = [] out = []
out.append(self.sw_gen_policy.value) out.append(self.sw_gen_policy.value)
if self.enable_deref_arg: if self.enable_deref_arg:
out.append('--enable-deref-arg') out.append("--enable-deref-arg")
if self.keep_holes: if self.keep_holes:
out.append('--keep-holes') out.append("--keep-holes")
return out return out
def cc_opts(self): def cc_opts(self):
''' Options to pass to the C compiler ''' """ Options to pass to the C compiler """
out = ['-fPIC'] out = ["-fPIC"]
if self.cc_debug: if self.cc_debug:
out.append('-g') out.append("-g")
out.append(self.opt_level()) out.append(self.opt_level())
return out return out
def opt_level(self): def opt_level(self):
''' The optimization level to pass to gcc ''' """ The optimization level to pass to gcc """
return '-O{}'.format(self.c_opt_level) return "-O{}".format(self.c_opt_level)
def aux_dirs(self): def aux_dirs(self):
''' Get the list of auxiliary directories ''' """ Get the list of auxiliary directories """
return self.aux return self.aux
def gen_dw_asm_c(obj_path, out_path, config, pc_list_path=None): def gen_dw_asm_c(obj_path, out_path, config, pc_list_path=None):
''' Generate the C code produced by dwarf-assembly from `obj_path`, saving """ Generate the C code produced by dwarf-assembly from `obj_path`, saving
it as `out_path` ''' it as `out_path` """
dw_assembly_args = config.dwarf_assembly_args() dw_assembly_args = config.dwarf_assembly_args()
if pc_list_path is not None: if pc_list_path is not None:
dw_assembly_args += ['--pc-list', pc_list_path] dw_assembly_args += ["--pc-list", pc_list_path]
try: try:
with open(out_path, 'w') as out_handle: with open(out_path, "w") as out_handle:
# TODO enhance error handling # TODO enhance error handling
dw_asm_output = subprocess.check_output( command_args = [DWARF_ASSEMBLY_BIN, obj_path] + dw_assembly_args
[DWARF_ASSEMBLY_BIN, obj_path] + dw_assembly_args) \ dw_asm_output = subprocess.check_output(command_args).decode("utf-8")
.decode('utf-8')
out_handle.write(dw_asm_output) out_handle.write(dw_asm_output)
except subprocess.CalledProcessError as exn: except subprocess.CalledProcessError as exn:
raise Exception( raise Exception(
("Cannot generate C code from object file {} using {}: process " (
"terminated with exit code {}.").format( "Cannot generate C code from object file {} using {}: process "
obj_path, "terminated with exit code {}."
DWARF_ASSEMBLY_BIN, ).format(obj_path, DWARF_ASSEMBLY_BIN, exn.returncode)
exn.returncode)) )
def resolve_symlink_chain(objpath): def resolve_symlink_chain(objpath):
''' Resolves a symlink chain. This returns a pair `(new_obj, chain)`, """ Resolves a symlink chain. This returns a pair `(new_obj, chain)`,
`new_obj` being the canonical path for `objpath`, and `chain` being a list `new_obj` being the canonical path for `objpath`, and `chain` being a list
representing the path followed, eg. `[(objpath, a), (a, b), (b, new_obj)]`. representing the path followed, eg. `[(objpath, a), (a, b), (b, new_obj)]`.
The goal of this function is to allow reproducing symlink architectures at The goal of this function is to allow reproducing symlink architectures at
the eh_elf level. ''' the eh_elf level. """
chain = [] chain = []
out_path = objpath out_path = objpath
@ -146,16 +144,16 @@ def resolve_symlink_chain(objpath):
def find_out_dir(obj_path, config): def find_out_dir(obj_path, config):
''' Find the directory in which the eh_elf corresponding to `obj_path` will """ Find the directory in which the eh_elf corresponding to `obj_path` will
be outputted, among the output directory and the aux directories ''' be outputted, among the output directory and the aux directories """
return find_eh_elf_dir(obj_path, config.aux_dirs(), config.output) return find_eh_elf_dir(obj_path, config.aux_dirs(), config.output)
def gen_eh_elf(obj_path, config): def gen_eh_elf(obj_path, config):
''' Generate the eh_elf corresponding to `obj_path`, saving it as """ Generate the eh_elf corresponding to `obj_path`, saving it as
`out_dir/$(basename obj_path).eh_elf.so` (or in the current working `out_dir/$(basename obj_path).eh_elf.so` (or in the current working
directory if out_dir is None) ''' directory if out_dir is None) """
out_dir = find_out_dir(obj_path, config) out_dir = find_out_dir(obj_path, config)
obj_path, link_chain = resolve_symlink_chain(obj_path) obj_path, link_chain = resolve_symlink_chain(obj_path)
@ -165,19 +163,20 @@ def gen_eh_elf(obj_path, config):
link_chain = map( link_chain = map(
lambda elt: ( lambda elt: (
to_eh_elf_path(elt[0], out_dir), to_eh_elf_path(elt[0], out_dir),
os.path.basename(to_eh_elf_path(elt[1], out_dir))), os.path.basename(to_eh_elf_path(elt[1], out_dir)),
link_chain) ),
link_chain,
)
out_base_name = to_eh_elf_path(obj_path, out_dir, base=True) out_base_name = to_eh_elf_path(obj_path, out_dir, base=True)
out_so_path = to_eh_elf_path(obj_path, out_dir, base=False) out_so_path = to_eh_elf_path(obj_path, out_dir, base=False)
pc_list_dir = os.path.join(out_dir, 'pc_list') pc_list_dir = os.path.join(out_dir, "pc_list")
if is_newer(out_so_path, obj_path) and not config.force: if is_newer(out_so_path, obj_path) and not config.force:
return # The object is recent enough, no need to recreate it return # The object is recent enough, no need to recreate it
if os.path.exists(out_dir) and not os.path.isdir(out_dir): if os.path.exists(out_dir) and not os.path.isdir(out_dir):
raise Exception("The output path {} is not a directory.".format( raise Exception("The output path {} is not a directory.".format(out_dir))
out_dir))
if not os.path.exists(out_dir): if not os.path.exists(out_dir):
os.makedirs(out_dir, exist_ok=True) os.makedirs(out_dir, exist_ok=True)
@ -185,42 +184,38 @@ def gen_eh_elf(obj_path, config):
# Generate PC list # Generate PC list
pc_list_path = None pc_list_path = None
if config.use_pc_list: if config.use_pc_list:
pc_list_path = \ pc_list_path = os.path.join(pc_list_dir, out_base_name + ".pc_list")
os.path.join(pc_list_dir, out_base_name + '.pc_list')
os.makedirs(pc_list_dir, exist_ok=True) os.makedirs(pc_list_dir, exist_ok=True)
print('\tGenerating PC list…') print("\tGenerating PC list…")
generate_pc_list(obj_path, pc_list_path) generate_pc_list(obj_path, pc_list_path)
# Generate the C source file # Generate the C source file
print("\tGenerating C…") print("\tGenerating C…")
c_path = os.path.join(compile_dir, (out_base_name + '.c')) c_path = os.path.join(compile_dir, (out_base_name + ".c"))
gen_dw_asm_c(obj_path, c_path, config, pc_list_path) gen_dw_asm_c(obj_path, c_path, config, pc_list_path)
# Compile it into a .o # Compile it into a .o
print("\tCompiling into .o…") print("\tCompiling into .o…")
o_path = os.path.join(compile_dir, (out_base_name + '.o')) o_path = os.path.join(compile_dir, (out_base_name + ".o"))
if config.remote: if config.remote:
remote_out = do_remote( remote_out = do_remote(
config.remote, config.remote,
[ [C_BIN, "-o", out_base_name + ".o", "-c", out_base_name + ".c"]
C_BIN, + config.cc_opts(),
'-o', out_base_name + '.o',
'-c', out_base_name + '.c'
] + config.cc_opts(),
send_files=[c_path], send_files=[c_path],
retr_files=[(out_base_name + '.o', o_path)]) retr_files=[(out_base_name + ".o", o_path)],
)
call_rc = 1 if remote_out is None else 0 call_rc = 1 if remote_out is None else 0
else: else:
call_rc = subprocess.call( call_rc = subprocess.call(
[C_BIN, '-o', o_path, '-c', c_path, [C_BIN, "-o", o_path, "-c", c_path, config.opt_level(), "-fPIC"]
config.opt_level(), '-fPIC']) )
if call_rc != 0: if call_rc != 0:
raise Exception("Failed to compile to a .o file") raise Exception("Failed to compile to a .o file")
# Compile it into a .so # Compile it into a .so
print("\tCompiling into .so…") print("\tCompiling into .so…")
call_rc = subprocess.call( call_rc = subprocess.call([C_BIN, "-o", out_so_path, "-shared", o_path])
[C_BIN, '-o', out_so_path, '-shared', o_path])
if call_rc != 0: if call_rc != 0:
raise Exception("Failed to compile to a .so file") raise Exception("Failed to compile to a .so file")
@ -229,40 +224,32 @@ def gen_eh_elf(obj_path, config):
if os.path.exists(elt[0]): if os.path.exists(elt[0]):
if not os.path.islink(elt[0]): if not os.path.islink(elt[0]):
raise Exception( raise Exception(
"{}: file already exists and is not a symlink.".format( "{}: file already exists and is not a symlink.".format(elt[0])
elt[0])) )
os.remove(elt[0]) os.remove(elt[0])
os.symlink(elt[1], elt[0]) os.symlink(elt[1], elt[0])
def gen_all_eh_elf(obj_path, config): def gen_all_eh_elf(obj_path, config):
''' Call `gen_eh_elf` on obj_path and all its dependencies ''' """ Call `gen_eh_elf` on obj_path and all its dependencies """
deps = elf_so_deps(obj_path) deps = elf_so_deps(obj_path)
deps.append(obj_path) deps.append(obj_path)
for dep in deps: for dep in deps:
gen_eh_elf(dep, config) gen_eh_elf(dep, config)
def gen_eh_elfs(obj_path, def gen_eh_elfs(obj_path, out_dir, global_switch=True, deps=True, remote=None):
out_dir, """ Call gen{_all,}_eh_elf with args setup accordingly with the given
global_switch=True, options """
deps=True,
remote=None):
''' Call gen{_all,}_eh_elf with args setup accordingly with the given
options '''
switch_gen_policy = ( switch_gen_policy = (
SwitchGenPolicy.GLOBAL_SWITCH if global_switch SwitchGenPolicy.GLOBAL_SWITCH
if global_switch
else SwitchGenPolicy.SWITCH_PER_FUNC else SwitchGenPolicy.SWITCH_PER_FUNC
) )
config = Config( config = Config(
out_dir, out_dir, [], False, [obj_path], sw_gen_policy=switch_gen_policy, remote=remote
[],
False,
[obj_path],
sw_gen_policy=switch_gen_policy,
remote=remote,
) )
if deps: if deps:
@ -271,92 +258,158 @@ def gen_eh_elfs(obj_path,
def process_args(): def process_args():
''' Process `sys.argv` arguments ''' """ Process `sys.argv` arguments """
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
description="Compile ELFs into their related eh_elfs", description="Compile ELFs into their related eh_elfs"
) )
parser.add_argument('--deps', action='store_const', parser.add_argument(
const=gen_all_eh_elf, default=gen_eh_elf, "--deps",
dest='gen_func', action="store_const",
help=("Also generate eh_elfs for the shared objects " const=gen_all_eh_elf,
"this object depends on")) default=gen_eh_elf,
parser.add_argument('-o', '--output', metavar="path", dest="gen_func",
help=("Save the generated objects at the given path " help=("Also generate eh_elfs for the shared objects " "this object depends on"),
"instead of the current working directory")) )
parser.add_argument('-a', '--aux', action='append', default=[], parser.add_argument(
help=("Alternative output directories. These " "-o",
"directories are searched for existing matching " "--output",
"eh_elfs, and if found, these files are updated " metavar="path",
"instead of creating new files in the --output " help=(
"directory. By default, some aux directories " "Save the generated objects at the given path "
"are always considered, unless -A is passed: " "instead of the current working directory"
"{}.").format(Config.default_aux_str())) ),
parser.add_argument('-A', '--no-dft-aux', action='store_true', )
help=("Do not use the default auxiliary output " parser.add_argument(
"directories: {}.").format( "-a",
Config.default_aux_str())) "--aux",
parser.add_argument('--remote', metavar='ssh_args', action="append",
help=("Execute the heavyweight commands on the remote " default=[],
"machine, using `ssh ssh_args`.")) help=(
parser.add_argument('--use-pc-list', action='store_true', "Alternative output directories. These "
help=("Generate a PC list using `extract_pc.py` for " "directories are searched for existing matching "
"each processed ELF file, and call " "eh_elfs, and if found, these files are updated "
"dwarf-assembly accordingly.")) "instead of creating new files in the --output "
parser.add_argument('--force', '-f', action='store_true', "directory. By default, some aux directories "
help=("Force re-generation of the output files, even " "are always considered, unless -A is passed: "
"when those files are newer than the target " "{}."
"ELF.")) ).format(Config.default_aux_str()),
parser.add_argument('--enable-deref-arg', action='store_true', )
help=("Pass the `--enable-deref-arg` to " parser.add_argument(
"dwarf-assembly, enabling an extra `deref` " "-A",
"argument for each lookup function, allowing " "--no-dft-aux",
"to work on remote address spaces.")) action="store_true",
parser.add_argument('--keep-holes', action='store_true', help=("Do not use the default auxiliary output " "directories: {}.").format(
help=("Keep holes between FDEs instead of filling " Config.default_aux_str()
"them with junk. More accurate, less compact.")) ),
parser.add_argument("-g", "--cc-debug", action='store_true', )
help=("Compile the source file with -g for easy " parser.add_argument(
"debugging")) "--remote",
metavar="ssh_args",
help=(
"Execute the heavyweight commands on the remote "
"machine, using `ssh ssh_args`."
),
)
parser.add_argument(
"--use-pc-list",
action="store_true",
help=(
"Generate a PC list using `extract_pc.py` for "
"each processed ELF file, and call "
"dwarf-assembly accordingly."
),
)
parser.add_argument(
"--force",
"-f",
action="store_true",
help=(
"Force re-generation of the output files, even "
"when those files are newer than the target "
"ELF."
),
)
parser.add_argument(
"--enable-deref-arg",
action="store_true",
help=(
"Pass the `--enable-deref-arg` to "
"dwarf-assembly, enabling an extra `deref` "
"argument for each lookup function, allowing "
"to work on remote address spaces."
),
)
parser.add_argument(
"--keep-holes",
action="store_true",
help=(
"Keep holes between FDEs instead of filling "
"them with junk. More accurate, less compact."
),
)
parser.add_argument(
"-g",
"--cc-debug",
action="store_true",
help=("Compile the source file with -g for easy " "debugging"),
)
# c_opt_level # c_opt_level
opt_level_grp = parser.add_mutually_exclusive_group() opt_level_grp = parser.add_mutually_exclusive_group()
opt_level_grp.add_argument('-O0', action='store_const', const='0', opt_level_grp.add_argument(
dest='c_opt_level', "-O0",
help=("Compile C file with this optimization " action="store_const",
"level.")) const="0",
opt_level_grp.add_argument('-O1', action='store_const', const='1', dest="c_opt_level",
dest='c_opt_level', help=("Compile C file with this optimization " "level."),
help=("Compile C file with this optimization " )
"level.")) opt_level_grp.add_argument(
opt_level_grp.add_argument('-O2', action='store_const', const='2', "-O1",
dest='c_opt_level', action="store_const",
help=("Compile C file with this optimization " const="1",
"level.")) dest="c_opt_level",
opt_level_grp.add_argument('-O3', action='store_const', const='3', help=("Compile C file with this optimization " "level."),
dest='c_opt_level', )
help=("Compile C file with this optimization " opt_level_grp.add_argument(
"level.")) "-O2",
opt_level_grp.add_argument('-Os', action='store_const', const='s', action="store_const",
dest='c_opt_level', const="2",
help=("Compile C file with this optimization " dest="c_opt_level",
"level.")) help=("Compile C file with this optimization " "level."),
opt_level_grp.set_defaults(c_opt_level='3') )
opt_level_grp.add_argument(
"-O3",
action="store_const",
const="3",
dest="c_opt_level",
help=("Compile C file with this optimization " "level."),
)
opt_level_grp.add_argument(
"-Os",
action="store_const",
const="s",
dest="c_opt_level",
help=("Compile C file with this optimization " "level."),
)
opt_level_grp.set_defaults(c_opt_level="3")
switch_gen_policy = \ switch_gen_policy = parser.add_mutually_exclusive_group(required=True)
parser.add_mutually_exclusive_group(required=True) switch_gen_policy.add_argument(
switch_gen_policy.add_argument('--switch-per-func', "--switch-per-func",
dest='sw_gen_policy', dest="sw_gen_policy",
action='store_const', action="store_const",
const=SwitchGenPolicy.SWITCH_PER_FUNC, const=SwitchGenPolicy.SWITCH_PER_FUNC,
help=("Passed to dwarf-assembly.")) help=("Passed to dwarf-assembly."),
switch_gen_policy.add_argument('--global-switch', )
dest='sw_gen_policy', switch_gen_policy.add_argument(
action='store_const', "--global-switch",
const=SwitchGenPolicy.GLOBAL_SWITCH, dest="sw_gen_policy",
help=("Passed to dwarf-assembly.")) action="store_const",
parser.add_argument('object', nargs='+', const=SwitchGenPolicy.GLOBAL_SWITCH,
help="The ELF object(s) to process") help=("Passed to dwarf-assembly."),
)
parser.add_argument("object", nargs="+", help="The ELF object(s) to process")
return parser.parse_args() return parser.parse_args()