[llvm-dev] [LLD] Writing thunks before the corresponding section

Peter Smith via llvm-dev llvm-dev at lists.llvm.org
Wed Sep 7 09:55:56 PDT 2016


Hello Simon,

Yes it is okay to write ARM thunks before an InputSection. There is a
similar "inline state change" thunk in ARM that does BX PC, NOP to
change state and fall through. The ARM Thunks that are implemented now
just need to be in range of the source branch. I have previously
worked on an ARM Linker that has thunks in separate sections in the
same way that you describe for bfd/gold.

I can't tell if you are planning to implement Thunks as separate
InputSections or assigning them to existing InputSections as they are
now but writing them at the front and not the end.

If you are considering putting the thunks as data to be written prior
to the InputSection contents I think you'll need some extra book
keeping.
- Padding might be needed between the last thunk and the InputSection
contents if the alignment of the InputSection is higher than the usual
2 or 4.
- If the Thunk is conceptually part of the InputSection (starts at
offset 0) then all the relocations and symbols will need displacing.

It is worth mentioning that disassembly of ARM and Thumb Thunks may
look a bit strange if they are moved from after the InputSection. This
is because they lack a mapping symbol ($a or $t) that tells the
disassembler what instruction set to disassemble. I've got adding
mapping symbol for linker generated InputSections on my list of things
to do.

Hope this helps

Peter



On 7 September 2016 at 14:58, Simon Atanasyan <simon at atanasyan.com> wrote:
> Hi,
>
> MIPS LA25 thunk is used to call PIC function from non-PIC code.
> Usually it contains three instructions:
>
> lui   $25, %hi(func)
> addiu $25, $25, %lo(func)
> j     func
>
> We can write such thunk in an arbitrary place of the generated file.
> But if a PIC function requires the thunk is the first routine in a
> section, we can optimize the code and escape jump instruction. To do
> so we just write the following thunk right before the PIC routine.
>
> lui   $25, %hi(func)
> addiu $25, $25, %lo(func)
>
> In fact GNU bfd/gold linkers write all MIPS LA25 thunks required for
> the section "A" into a separate input section "S" and put section "S"
> before "A". The last thunk in the section "S" might have an optimized
> two-instructions form.
>
> I would like to implement such optimization in LLD. My question is
> about ARM thunks - is it okay to write them before corresponding input
> section not after like LLD does now?
>
> --
> Simon Atanasyan


More information about the llvm-dev mailing list