Skip to content

cmd/link: support calls to SDYNIMPORT symbols on mips64 #46178

Open
@4a6f656c

Description

@4a6f656c

The mips64 port currently fails to handle calls to SDYNIMPORT symbols, even when external linking is in use, since it emits R_MIPS_26 relocations, resulting in "relocation truncated to fit" warnings. An equivalent call from assembly when compiled with LLVM results in the following:

   0:   df990000        ld      t9,0(gp)
   4:   0320f809        jalr    t9
   8:   00000000        nop

Furthermore, two relocations are produced for the external symbol - a R_MIPS_CALL16 relocation for the ld instruction and a R_MIPS_JALR relocation for the jalr instruction.

There are a couple of additional things to note here - firstly, PIC code requires that register 25 (t9) contains the address of the called function, which is why this register is used here. Secondly, when the external linker performs the R_MIPS_CALL16 relocation is is done with an expected gp register value, which is loaded by the dynamic linker. Unfortunately, Go currently uses this register and clobbers the value placed in it by the dynamic linker, breaking the relocation.

This means there are effectively two things to solve:

  1. Currently a CALL translates to a single JAL instruction with an R_CALLMIPS (aka R_MIPS_26) relocation - given that the assembler does not know if this is an internal function or an SDYNIMPORT symbol, the easiest option is to likely produce trampolines for the SDYNIMPORT calls, modelling the above assembly and generating the two matching relocations.

  2. In order for these relocations to work we likely have to avoid clobbering the gp register - one option may be to change Go's REGSB register (as was done for tp on riscv64). The other is to save the value of the gp register at start up and restore the gp value before calling an SDYNIMPORT, switching it back to Go's value after the call returns.

I've got a proof of concept working with external linking, trampoline generation and gp register save/restore on openbsd/mips64 (I'll make this available soon) - in the mean time, I'm interested in feedback and/or other suggestions on these issues/approach, before I proceed further.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsFixThe path to resolution is known, but the work has not been done.arch-mipscompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    Triage Backlog

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions