Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align data added by patch #7

Open
avncharlie opened this issue Jul 29, 2023 · 6 comments
Open

Align data added by patch #7

avncharlie opened this issue Jul 29, 2023 · 6 comments

Comments

@avncharlie
Copy link

avncharlie commented Jul 29, 2023

Hello,
I'm trying to align data added by my patch using the .align and .balign directives. I'm having no luck with this and I'm wondering if it is because I'm using the directives incorrectly or if it isn't implemented to be used within patches. I'm currently manually adding bytes to the data added to the patch to make sure it is aligned.

Thanks,
Alvin

@jranieri-grammatech
Copy link
Collaborator

At a minimum, the .align directive is supported (implemented here). Manually adding bytes is going to be more problematic, since you don't necessarily know the alignment of the code once it's been re-assembled without .align directives (e.g. you're inserting your patch into a block that's 4-byte aligned, you add manual padding based off of the block addresses, and then the pretty-printer does relayout and it now falls on a different 4-byte aligned address).

Could you attach an example?

@avncharlie
Copy link
Author

In the example below, I'm trying to align the string to 16-byte alignment:

context.register_insert_function(
    'test_alignment_func',
    Patch.from_function(lambda _: '''
    nop

    .rodata
    .TEST_STRING:
        .string "TEST STRING"
        .align 4
    ''', Constraints(x86_syntax=X86Syntax.INTEL))
)

This doesn't seem to work. I've also tried moving the .align to before the added data and that doesn't do anything differently either.

@jranieri-grammatech
Copy link
Collaborator

jranieri-grammatech commented Aug 2, 2023

In the example below, I'm trying to align the string to 16-byte alignment:

context.register_insert_function(
    'test_alignment_func',
    Patch.from_function(lambda _: '''
    nop

    .rodata
    .TEST_STRING:
        .string "TEST STRING"
        .align 4
    ''', Constraints(x86_syntax=X86Syntax.INTEL))
)

This doesn't seem to work. I've also tried moving the .align to before the added data and that doesn't do anything differently either.

Try this:

context.register_insert_function(
    'test_alignment_func',
    Patch.from_function(lambda _: '''
    nop

    .rodata
    .p2align 4
    .TEST_STRING:
        .string "TEST STRING"
    ''', Constraints(x86_syntax=X86Syntax.INTEL))
)

It's untested and if it doesn't work, I can dig a bit deeper to see what's going on.

@avncharlie
Copy link
Author

avncharlie commented Aug 3, 2023

That doesn't seem to work either unfortunately

@jranieri-grammatech
Copy link
Collaborator

I tested out the patch I mentioned, with the full program being:

import gtirb_rewriting.driver
from gtirb_rewriting import *

class MyPass(Pass):
  def begin_module(self, module, functions, rewriting_ctx) -> None:
    rewriting_ctx.register_insert_function(
      'test_alignment_func',
      Patch.from_function(lambda _: '''
        nop

        .rodata
        .p2align 4
        .TEST_STRING:
            .string "TEST STRING"
        ''', Constraints(x86_syntax=X86Syntax.INTEL))
    )

if __name__ == "__main__":
    gtirb_rewriting.driver.main(MyPass)

In the output assembly from gtirb-pprinter, I see this:

#===================================
.section .rodata ,"a",@progbits
#===================================

.align 16
.TEST_STRING:
          .string "TEST STRING"

and further down in the file I see this:

#===================================
.text
#===================================

.align 16
#-----------------------------------
.globl test_alignment_func
.type test_alignment_func, @function
#-----------------------------------
test_alignment_func:

            nop

Are you seeing some other behavior?

@avncharlie
Copy link
Author

Apologies, I realise my issue is that I would like to align my added data as a whole, not within itself or at the beginning.

I tested this program below:

import gtirb_rewriting.driver
from gtirb_rewriting import *

class MyPass(Pass):
  def begin_module(self, module, functions, rewriting_ctx) -> None:
    rewriting_ctx.register_insert_function(
      'test_alignment_func',
      Patch.from_function(lambda _: '''
        nop

        .rodata
        .TEST_STRING:
            .string "TEST STRING"
            .align 16
        ''', Constraints(x86_syntax=X86Syntax.INTEL))
    )

if __name__ == "__main__":
    gtirb_rewriting.driver.main(MyPass)

The generated assembly contains this:

#===================================
.section .rodata ,"a",@progbits
#===================================

.align 16
.TEST_STRING:
          .string "TEST STRING"

I would like it to output this:

#===================================
.section .rodata ,"a",@progbits
#===================================


.TEST_STRING:
          .string "TEST STRING"
.align 16

Is there any way to do this? Thanks for the help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants