GrammaTech / gtirb-rewriting

Python API for rewriting GTIRB files
GNU General Public License v3.0
16 stars 3 forks source link

How to rewrite/insert data in data/bss section. #2

Closed StarGazerM closed 2 years ago

StarGazerM commented 2 years ago

Hi: Does gtrirb-rewriting support modifying data other than .text section? If not, which part of GTIRB IR API maybe related to this?

jranieri-grammatech commented 2 years ago

It does not, though that's something that would be nice to add in the future. What sort of modifications are you looking to make?

StarGazerM commented 2 years ago

I want to add something like:

.comm foo

to a binary, and used foo in the asm code I want to insert. Can I create some Symbol objects and ByteInterval objects then directly add them to module before register the insertion?

jranieri-grammatech commented 2 years ago

Yeah, that would be my recommendation. As long as the symbols are added to the module before begin_module returns, the patches will be able to reference them.

StarGazerM commented 2 years ago

Ah thx, another question what if I need some thing like:

call read@PLT

Do I need manually construct an plt code block?

jranieri-grammatech commented 2 years ago

Nope, that'll get generated during reassembly.

You might need to use get_or_insert_extern_symbol to ensure that read is present in the module's symbols.

StarGazerM commented 2 years ago

okkk great thank you so much

jranieri-grammatech commented 2 years ago

Support for patches adding data has been added in gtirb-rewriting 0.24. It can't handle the .comm directive, but it can do things like this:

@patch_constraints(x86_syntax=X86Syntax.INTEL)
def assert_patch(insertion_context):
    return """
        lea rdi, [rip + .Lassertion]
        lea rsi, [rip + .Lunknown]
        xor rdx, rdx
        lea rcx, [rip + .Lunknown]
        call __assert_fail
        ud2

        .rodata
        .Lassertion:
            .string "something went wrong!"
        .Lunknown:
            .string "unknown"
    """
StarGazerM commented 2 years ago

thx!

jranieri-grammatech commented 2 years ago

gtirb-rewriting gained support for rewriting data blocks in main today, using insert_at, replace_at, and delete_at. It didn't add support for .comm, but it's still on my radar as something I'd like to add in the future.