Skip to content

Commit fd345e9

Browse files
committed
tmp fix for p_align issue
1 parent 8e037fc commit fd345e9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/patcherex2/components/allocation_managers/allocation_manager.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ def _find_in_mapped_blocks(
154154
def _create_new_mapped_block(
155155
self, size: int, flag=MemoryFlag.RWX, align=0x1
156156
) -> bool:
157-
# map 0x1000 bytes # TODO: currently we won't use available file/mem blocks, instead we create new one at the end of the file
157+
# TODO: currently we won't use available file/mem blocks, instead we create new one at the end of the file
158158
file_addr = None
159159
mem_addr = None
160160
for block in self.blocks[FileBlock]:
@@ -163,8 +163,19 @@ def _create_new_mapped_block(
163163
block.addr += 0x2000
164164
for block in self.blocks[MemoryBlock]:
165165
if block.size == -1:
166-
# mem_addr % 0x1000 should equal to file_addr % 0x1000 TODO
167-
mem_addr = block.addr + (file_addr % 0x1000)
166+
# NOTE: mem_addr % p_align should equal to file_addr % p_align
167+
# Check `man elf` and search for `p_align` for more information
168+
# FIXME: shouldn't do any assumption on component type, reimpl in a better way
169+
# FIXME: even worse, importing ELF will cause circular import
170+
# TODO: consider merge allocation_manager and binfmt_tool into one component
171+
if self.p.binfmt_tool.__class__.__name__ == "ELF":
172+
max_seg_align = max(
173+
[segment["p_align"] for segment in self.p.binfmt_tool._segments]
174+
+ [0]
175+
)
176+
mem_addr = block.addr + (file_addr % max_seg_align)
177+
else:
178+
mem_addr = block.addr + (file_addr % 0x1000)
168179
block.addr = mem_addr + 0x2000
169180
if file_addr and mem_addr:
170181
self.add_block(

0 commit comments

Comments
 (0)