Skip to content

Commit 3d19e7f

Browse files
committed
Change 'align' to align down and create 'align_up' instead
1 parent bace0ea commit 3d19e7f

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

qiling/os/memory.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,21 +195,45 @@ def show_mapinfo(self):
195195
def get_lib_base(self, filename: str) -> int:
196196
return next((s for s, _, _, info, _ in self.map_info if os.path.split(info)[1] == filename), -1)
197197

198-
def align(self, addr: int, alignment: int = None) -> int:
199-
"""Round up to nearest alignment.
198+
def align(self, value: int, alignment: int = None) -> int:
199+
"""Align a value down to the specified alignment boundary.
200200
201201
Args:
202-
addr: address to align
203-
alignment: alignment granularity, must be a power of 2
202+
value: a value to align
203+
alignment: alignment boundary; must be a power of 2. if not specified
204+
value will be aligned to page size
205+
206+
Returns: aligned value
207+
"""
208+
209+
if alignment is None:
210+
alignment = self.pagesize
211+
212+
# make sure alignment is a power of 2
213+
assert alignment & (alignment - 1) == 0
214+
215+
# round down to nearest alignment
216+
return value & ~(alignment - 1)
217+
218+
def align_up(self, value: int, alignment: int = None) -> int:
219+
"""Align a value up to the specified alignment boundary.
220+
221+
Args:
222+
value: value to align
223+
alignment: alignment boundary; must be a power of 2. if not specified
224+
value will be aligned to page size
225+
226+
Returns: aligned value
204227
"""
205228

206229
if alignment is None:
207230
alignment = self.pagesize
208231

209-
# rounds up to nearest alignment
210-
mask = self.max_mem_addr & -alignment
232+
# make sure alignment is a power of 2
233+
assert alignment & (alignment - 1) == 0
211234

212-
return (addr + (alignment - 1)) & mask
235+
# round up to nearest alignment
236+
return (value + alignment - 1) & ~(alignment - 1)
213237

214238
def save(self):
215239
"""Save entire memory content.

0 commit comments

Comments
 (0)