Use instructions size information for a Target to insert new instructions


I want to check, when generating object code (for the X86 infrastructure), where does the code cross between pages (let’s say 4KiB). Ideally, I would want to add new instructions at this boundary, and moving the existing code around. Is even possible to have this detection? And if so, how does the LLVM infrastructure deal with inserting new instructions? I say because under my understanding, this feature should exist at the MC level, and the IR shenanigans would not be useful for this problem.

Thanks in advance

Usually, anything that needs to be aware of page boundaries would need to be implemented in the linker: you don’t know the layout of your code in advance. Usually, code sections in intermediate object files are only aligned to 16 bytes. You can look in lld to see how we handle this sort of thing; I think one of the AArch64 erratum fixes is page-aware.

Maybe as a hack, you could force code sections to be 4KB-aligned. Impractical in general, but maybe okay for narrow applications or experimentation. If you want to go this route, look at X86AsmBackend::finishLayout.

1 Like