Commit 04d69ad
authored
utils: Remove alignment in rva2offset() (#491)
rva2offset() currently aligns PointerToRawData down to a hardcoded
0x200 boundary. Unless malformed, the PointerToRawData is expected to
be a multiple of the file alignment and the miniumum file alignment
per the PE/COFF Specification is 512:
https://learn.microsoft.com/windows/win32/debug/pe-format#optional-header-windows-specific-fields-image-only
So, this alignment choice is understandable.
UEFI images are based on the PE/COFF Specification and since they are
stored in flash and flash space is limited, file alignments less than
512 are used and supported by linkers today.
This proposes updating the logic to follow the formula:
file_offset = PointerToRawData + (RVA - VirtualAddress)
This allows both cases to be supported. While this does lose aligning
an incorrectly reported pointer to raw data, that seems somewhat
problematic anyway when tools, such as in this case, may report a
value that is converted to an incorrect result.
---
An example of where this was an issue was with the following values:
- RVA (rva): 0xF6C8
- VirtualAddress (va): 0xD000
- PointerToRawData (ptrd): 0xBDA0
- File Alignment: 0x20
Previous Result:
(rva - va) + aligned_pointer_to_raw_data(ptrd)
(0xF6C8 - 0xD000) + aligned_pointer_to_raw_data(0xBDA0)
0x26C8 + 0xBC00 = 0xE2C8 (incorrect)
Current Result:
ptrd + (rva - va)
0xBDA0 + (0xF6C8 - 0xD000)
0xBDA0 + 0x26C8 = 0xE468 (correct)
---
An example of where this was not previously an issue was with the
following values:
- RVA (rva): 0x13AB8
- VirtualAddress (va): 0x10000
- PointerToRawData (ptrd): 0xEC00
- File Alignment: 0x200
Previous Result:
(rva - va) + aligned_pointer_to_raw_data(ptrd)
(0x13AB8 - 0x10000) + aligned_pointer_to_raw_data(0xEC00)
0x3AB8 + 0xEC00 = 0x126B8 (correct)
Current Result:
ptrd + (rva - va)
0xEC00 + (0x13AB8 - 0x10000)
0xEC00 + 0x3AB8 = 0x126B8 (same outcome, also correct)1 parent 090a1d4 commit 04d69ad
1 file changed
+1
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
76 | | - | |
| 75 | + | |
77 | 76 | | |
78 | 77 | | |
79 | 78 | | |
| |||
0 commit comments