Author Topic: Wrong nested structure offset in x64  (Read 3587 times)

Yuri

  • Member
  • **
  • Posts: 177
Wrong nested structure offset in x64
« on: December 05, 2013, 07:45:51 PM »
It probably has something to do with x64 structure alignment.

Code: [Select]
ONE STRUCT
    a DQ
    b DD
ENDS

TWO STRUCT
    c ONE <>
ENDS

CODE SECTION

Start:
    invoke msvcrt:printf, "%d %d", ONE.b, TWO.c.b
    ret

Code: [Select]
8 12

The second offset will be correct ( 8 ) if you change b's type to DQ or put one more structure member after it.

wjr

  • Member
  • **
  • Posts: 223
    • WJR's website
Re: Wrong nested structure offset in x64
« Reply #1 on: December 08, 2013, 03:33:50 AM »
Nice find. Definite signs of a bug nesting in a structure, but I haven’t managed to catch it yet (while also trying to maintain momentum with progress on a remaining challenging complexity with a GoLink update)...

wjr

  • Member
  • **
  • Posts: 223
    • WJR's website
Re: Wrong nested structure offset in x64
« Reply #2 on: December 22, 2013, 04:31:13 AM »
Yes, this was an issue with x64 padding for the last member of a nested structure. Now fixed in GoASM v0.58.0.4.

However, while looking into this I found another one that occurs if padding is needed for the first member, but this is less likely and easier to work around. Using the above, for example:

Code: [Select]
DATA SECTION
First DD 0
Second TWO <>

TWO gets QWORD aligned, but the address for Second will be in error, being before the padding. The more specific labels Second.c or Second.c.a are correct taking into account the padding. Manually place an ALIGN 8 before Second would correct that address.

The fix for this one is in a different area of coding that is more complex, so this will take a bit longer...