-
Notifications
You must be signed in to change notification settings - Fork 4
Open
Description
Hello, and thank you to whoever is looking at this.
I am learning about static binary rewriting by attempting to add a print statement to some binaries. (It should just print hello, and I am using puts). However, I don't know how to add the hello. I tried calling puts and adding hello to the data section, but I got the error.
gtirb.serialization.EncodeError: String codec only supports strings
Anyway, how can I add data that can be printed?
import gtirb
import gtirb_rewriting.driver
from gtirb_rewriting import Pass, SingleBlockScope, BlockPosition
from gtirb_rewriting.patches import CallPatch
class PrintHelloPass(Pass):
def begin_module(self, module, functions, context):
func = next((f for f in functions if f.get_name() == "main"), None)
rodata_section = next((s for s in module.sections if s.name == ".rodata"), None)
if rodata_section is None:
rodata_section = gtirb.Section(name=".rodata", flags=gtirb.Section.Flag.Readable)
module.sections.add(rodata_section)
hello_bytes = b"hello\x00"
byte_interval = gtirb.ByteInterval(contents=hello_bytes)
rodata_section.byte_intervals.add(byte_interval)
data_block = gtirb.DataBlock(offset=0, size=len(hello_bytes))
byte_interval.blocks.add(data_block)
hello_symbol = gtirb.Symbol(name="hello_str", payload=data_block)
hello_symbol = context.get_or_insert_extern_symbol("hello_str", data_block)
puts_sym = context.get_or_insert_extern_symbol("puts", "libc.so.6")
for block in func.get_entry_blocks():
context.register_insert(
SingleBlockScope(block, BlockPosition.ENTRY),
CallPatch(puts_sym, [hello_symbol])
)
if __name__ == "__main__":
gtirb_rewriting.driver.main(PrintHelloPass)Metadata
Metadata
Assignees
Labels
No labels