[llvm-dev] [LLD] Linker Relaxation

Rafael Avila de Espindola via llvm-dev llvm-dev at lists.llvm.org
Thu Jul 13 16:39:00 PDT 2017


Bruce Hoult <bruce at hoult.org> writes:

> As a concrete suggestion, here is code for deciding how many bytes of
> padding to emit.
>
> For the case of .align 2 (4 byte alignment), it will emit a minimum of 3
> and a maximum of 6 bytes of padding. For .align 4 it emit a minimum of 15
> and a maximum of 30 bytes of padding.
>
> /*
>   RISC-V align.
>
>   Goals:
>   1) align to the desired amount in the generated object file.
>   2) provide enough extra padding that alignment can be maintained in the
> face of
>      code size changes, by only making the padding smaller, never bigger.
> */
>
> void emit_padding(uintptr_t sz);
>
> uintptr_t align(uintptr_t oldPos, int logAligment){
>     uintptr_t alignment = 1 << logAligment, mask = alignment - 1;
>     uintptr_t newPos = (oldPos + mask) & mask;
>     if ((newPos - oldPos) < mask) newPos += alignment;
>     emit_padding(newPos - oldPos);
>     return newPos;
> }

In the example provided this code will produce 6 bytes of padding, no?

It needs 2 to align to 4 bytes, and the if is taken, making it 4 + 2 =
6.

How did 3 come up?

BTW, that is a pretty cool trick for making sure a second pass is never
needed :-)

Cheers,
Rafael


More information about the llvm-dev mailing list