@@ -185,7 +185,9 @@ def _add_end_of_file_block(self) -> None:
185
185
self .p .allocation_manager .add_block (block )
186
186
187
187
addr = load_segments [- 1 ]["p_vaddr" ] + load_segments [- 1 ]["p_memsz" ]
188
- addr = (addr + 0xFFF ) & ~ 0xFFF # round up to 0x1000
188
+ # TODO: should we use the max_align of the segments?
189
+ max_align = max ([segment ["p_align" ] for segment in self ._segments ] + [0 ])
190
+ addr = (addr + (max_align - 1 )) & ~ (max_align - 1 ) # round up to max_align
189
191
block = MemoryBlock (addr , - 1 )
190
192
self .p .allocation_manager .add_block (block )
191
193
@@ -342,6 +344,7 @@ def finalize(self) -> None:
342
344
self ._segments .append (phdr_load_segment )
343
345
344
346
# magic
347
+ # TODO: should we use the max_align of the segments?
345
348
load_segments_rounded = []
346
349
first_load_segment = None
347
350
for segment in self ._segments :
@@ -350,24 +353,24 @@ def finalize(self) -> None:
350
353
first_load_segment = segment
351
354
load_segments_rounded .append (
352
355
(
353
- # start of the segment, round down to multiple of 0x1000
356
+ # start of the segment, round down to multiple of max_align
354
357
(segment ["p_vaddr" ] - first_load_segment ["p_vaddr" ])
355
358
- (
356
359
(segment ["p_vaddr" ] - first_load_segment ["p_vaddr" ])
357
- % 0x1000
360
+ % max_align
358
361
),
359
- # end of the segment, round up to multiple of 0x1000
362
+ # end of the segment, round up to multiple of max_align
360
363
int (
361
364
(
362
365
segment ["p_vaddr" ]
363
366
+ segment ["p_memsz" ]
364
367
- first_load_segment ["p_vaddr" ]
365
- + 0x1000
368
+ + max_align
366
369
- 1
367
370
)
368
- / 0x1000
371
+ / max_align
369
372
)
370
- * 0x1000 ,
373
+ * max_align ,
371
374
)
372
375
)
373
376
load_segments_rounded = sorted (load_segments_rounded , key = lambda x : x [0 ])
@@ -402,9 +405,10 @@ def finalize(self) -> None:
402
405
for prev_seg , next_seg in zip (
403
406
load_segments_rounded [:- 1 ], load_segments_rounded [1 :]
404
407
):
408
+ # TODO: should we use the max_align of the segments?
405
409
potential_base = (
406
- max (prev_seg [1 ], self .p .binfmt_tool .file_size ) + 0xFFF
407
- ) & ~ 0xFFF # round up to 0x1000
410
+ max (prev_seg [1 ], self .p .binfmt_tool .file_size ) + ( max_align - 1 )
411
+ ) & ~ ( max_align - 1 ) # round up to max_align
408
412
if next_seg [0 ] - potential_base > self ._elf .header ["e_phentsize" ] * len (
409
413
self ._segments
410
414
): # if there is space between segments, put phdr here
@@ -417,10 +421,11 @@ def finalize(self) -> None:
417
421
# this is to workaround a weird issue in the dynamic linker of glibc
418
422
# we want to make sure p_vaddr (phdr_start) == p_offset (len(ncontent))
419
423
if phdr_start <= self .p .binfmt_tool .file_size :
424
+ # TODO: should we use the max_align of the segments?
420
425
# p_vaddr <= p_offset: pad the file (p_offset) to page size, and let p_vaddr = p_offset
421
426
self .p .binfmt_tool .file_size = (
422
- self .p .binfmt_tool .file_size + 0xFFF
423
- ) & ~ 0xFFF # round up to 0x1000
427
+ self .p .binfmt_tool .file_size + ( max_align - 1 )
428
+ ) & ~ ( max_align - 1 ) # round up to max_align
424
429
phdr_start = self .p .binfmt_tool .file_size
425
430
426
431
# update phdr segment and its corresponding load segment
0 commit comments