@@ -209,7 +209,24 @@ def pad(self, alignment: int) -> None:
209
209
self .disassembly .append (f"{ offset :x} : { ' ' .join (['00' ] * padding )} " )
210
210
self .body .extend ([0 ] * padding )
211
211
212
- def remove_jump (self , * , alignment : int = 1 ) -> None :
212
+ def add_nops (self , nop : bytes , alignment : int ) -> None :
213
+ """Add NOPs until there is alignment. Fail if it is not possible."""
214
+ offset = len (self .body )
215
+ nop_size = len (nop )
216
+
217
+ # Calculate the gap to the next multiple of alignment.
218
+ gap = - offset % alignment
219
+ if gap :
220
+ if gap % nop_size == 0 :
221
+ count = gap // nop_size
222
+ self .body .extend (nop * count )
223
+ else :
224
+ raise ValueError (
225
+ f"Cannot add nops of size '{ nop_size } ' to a body with "
226
+ f"offset '{ offset } ' to align with '{ alignment } '"
227
+ )
228
+
229
+ def remove_jump (self ) -> None :
213
230
"""Remove a zero-length continuation jump, if it exists."""
214
231
hole = max (self .holes , key = lambda hole : hole .offset )
215
232
match hole :
@@ -244,7 +261,7 @@ def remove_jump(self, *, alignment: int = 1) -> None:
244
261
jump = b"\x00 \x00 \x00 \x14 "
245
262
case _:
246
263
return
247
- if self .body [offset :] == jump and offset % alignment == 0 :
264
+ if self .body [offset :] == jump :
248
265
self .body = self .body [:offset ]
249
266
self .holes .remove (hole )
250
267
@@ -266,10 +283,7 @@ class StencilGroup:
266
283
_trampolines : set [int ] = dataclasses .field (default_factory = set , init = False )
267
284
268
285
def process_relocations (
269
- self ,
270
- known_symbols : dict [str , int ],
271
- * ,
272
- alignment : int = 1 ,
286
+ self , known_symbols : dict [str , int ], * , alignment : int = 1 , nop : bytes = b""
273
287
) -> None :
274
288
"""Fix up all GOT and internal relocations for this stencil group."""
275
289
for hole in self .code .holes .copy ():
@@ -289,8 +303,8 @@ def process_relocations(
289
303
self ._trampolines .add (ordinal )
290
304
hole .addend = ordinal
291
305
hole .symbol = None
292
- self .code .remove_jump (alignment = alignment )
293
- self .code .pad ( alignment )
306
+ self .code .remove_jump ()
307
+ self .code .add_nops ( nop = nop , alignment = alignment )
294
308
self .data .pad (8 )
295
309
for stencil in [self .code , self .data ]:
296
310
for hole in stencil .holes :
0 commit comments