@@ -154,7 +154,7 @@ def _find_in_mapped_blocks(
154
154
def _create_new_mapped_block (
155
155
self , size : int , flag = MemoryFlag .RWX , align = 0x1
156
156
) -> 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
158
158
file_addr = None
159
159
mem_addr = None
160
160
for block in self .blocks [FileBlock ]:
@@ -163,8 +163,19 @@ def _create_new_mapped_block(
163
163
block .addr += 0x2000
164
164
for block in self .blocks [MemoryBlock ]:
165
165
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 )
168
179
block .addr = mem_addr + 0x2000
169
180
if file_addr and mem_addr :
170
181
self .add_block (
0 commit comments