diff options
Diffstat (limited to 'bfd')
-rwxr-xr-x | bfd/codegen.py | 81 | ||||
-rwxr-xr-x | bfd/load.py | 35 | ||||
-rw-r--r-- | bfd/makefile | 2 | ||||
-rw-r--r-- | bfd/test/makefile | 6 |
4 files changed, 120 insertions, 4 deletions
diff --git a/bfd/codegen.py b/bfd/codegen.py new file mode 100755 index 0000000..fbac4a8 --- /dev/null +++ b/bfd/codegen.py @@ -0,0 +1,81 @@ +#!/bin/python3 + +import argparse +import code +import readline + +default_header="#include <stdint.h>\n" +main_sig="int main(int argc, char** argv)>" +def_kw="#define " +def_name = [] +separator="fff" +def_value = [] + +class Argparser(object): + def __init__(self): + parser = argparse.ArgumentParser() + parser.add_argument("--arglist", nargs="+", type=str, help="list of args") + parser.add_argument("--hex", action="store_true", help="generate hex(string) code, otherwise generate int", default=False) + self.args = parser.parse_args() + self.code = {} + +class CodeGen_Arg(object): + def __init__(self, arglist): + self.arglist = arglist + self.def_name = [] + self.def_value = [] + + def get_ret_type(self, type): + pass + + def gen_cast(self): + for argtype in self.arglist: + if argtype == "int8": self.def_name.append("i8") + elif argtype == "uint8":self.def_name.append("u8") + elif argtype == "uchar":self.def_name.append("c") + elif argtype == "char":self.def_name.append("c") + elif argtype == "lightuserdata":self.def_name.append("p") + elif argtype == "bool":self.def_name.append("b") + elif argtype == "int16":self.def_name.append("i16") + elif argtype == "uint16":self.def_name.append("u16") + elif argtype == "int32":self.def_name.append("i32") + elif argtype == "uint32":self.def_name.append("u32") + elif argtype == "int64":self.def_name.append("i64") + elif argtype == "uint64":self.def_name.append("u64") + elif argtype == "int128":self.def_name.append("i128") + elif argtype == "uint128":self.def_name.append("u128") + elif argtype == "float":self.def_name.append("f") + elif argtype == "double":self.def_name.append("d") + elif argtype == "long double":self.def_name.append("ld") + elif argtype == "string":self.def_name.append("s") + elif argtype == "custom":self.def_name.append("x") + else: + raise Exception("codegen : unknown type") + + def debugdump(self): + for argtype in self.arglist: + print(argtype) + + def genhex(): + pass + + def genint(): + pass + +# write code here +def premain(): + argparser = Argparser() + codegen = CodeGen_Arg(argparser.args.arglist) + codegen.debugdump() + +def main(): + try: + premain() + except: + variables = globals().copy() + variables.update(locals()) + shell = code.InteractiveConsole(variables) + shell.interact(banner="DEBUG REPL") + +if __name__ == "__main__": + main() diff --git a/bfd/load.py b/bfd/load.py index 08adfc5..ae984c8 100755 --- a/bfd/load.py +++ b/bfd/load.py @@ -24,6 +24,8 @@ class CLIArgParser(object): parser.add_argument("--objcode", action='store_true', help="dump objects", default=False) parser.add_argument("--test", action='store_true', help="test switch", default=False) parser.add_argument("--dynsym", action='store_true', help="dump dynamic symbol table", default=False) + parser.add_argument("--dlpath", action='store_true', help="dump dynamic linker path", default=False) + parser.add_argument("--section", type=str, help="dump a section") self.args = parser.parse_args() if self.args.obj is None: raise Exception("no object file provided. please specify an object with --obj.") @@ -296,6 +298,7 @@ class ELF(object): self.symbol_table_e = [] self.data_section = [] self.text_section = [] + self.dlpath = str() def init(self, size): self.size = size @@ -449,6 +452,36 @@ class ELF(object): print(name) return ret_list + def dump_section(self, section_name): + for section in self.shhdr: + name = self.read_section_name(byte2int(section.sh_name)) + if name == section_name: + self.so.seek(byte2int(section.sh_offset)) + obj = self.so.read(byte2int(section.sh_size)) + if section_name == ".interp": self.dlpath = repr(obj) + count = int() + strrep = [] + for byte in obj: + if count%16 == 0: + for ch in strrep: + if ord(ch) > 16: print(ch, end = '') + else: pass + print() + strrep = [] + print(format(count, "06x"), ': ', end='') + strrep.append(str(chr(byte))) + print(format(byte, '02x') + ' ', end='') + else: + strrep += str(chr(byte)) + print(format(byte, '02x') + ' ', end='') + count += 1 + for i in range(0, 16-count%16): print(" ", end="") + for ch in strrep: + if ord(ch) > 16: print(ch, end = '') + else: pass + print() + return self.dlpath + def dump_obj_size(self, stt_type, dump_b): ret_list = [] for entry in self.string_tb_e: @@ -706,6 +739,8 @@ def main(): elif argparser.args.test: elf.dump_symbol_string(ELF_ST_TYPE.STT_FUNC, True) elif argparser.args.test: elf.dump_symbol_string(ELF_ST_TYPE.STT_OBJECT, True) elif argparser.args.dynsym: elf.dump_st_entries_dyn() + elif argparser.args.dlpath: elf.dump_section(".interp") + elif argparser.args.section: elf.dump_section(argparser.args.section) except: variables = globals().copy() variables.update(locals()) diff --git a/bfd/makefile b/bfd/makefile index 2720678..0788260 100644 --- a/bfd/makefile +++ b/bfd/makefile @@ -7,7 +7,7 @@ TARGET=main ##################################RULES################################ .DEFAULT:all -.PHONY:all clean help $(TARGET) $(TARGET).so test +.PHONY:all clean help test all:$(TARGET) $(TARGET).so test diff --git a/bfd/test/makefile b/bfd/test/makefile index 8282536..7115100 100644 --- a/bfd/test/makefile +++ b/bfd/test/makefile @@ -7,9 +7,9 @@ TARGET=test ##################################RULES################################ .DEFAULT:all -.PHONY:all clean help $(TARGET) ASM +.PHONY:all clean help -all:$(TARGET) $(TARGET).so ASM +all:$(TARGET) $(TARGET).so $(TARGET).asm .c.o: $(CC) $(CC_FLAGS) -c $< -o $@ @@ -17,7 +17,7 @@ all:$(TARGET) $(TARGET).so ASM $(TARGET): $(TARGET).o $(CC) $^ $(LD_FLAGS) -o $@ -ASM: $(TARGET).o +$(TARGET).asm: $(TARGET).o objdump -d -M intel -S $(TARGET).o > $(TARGET).asm $(TARGET).so: $(TARGET).o |