Source file src/cmd/link/internal/loong64/asm.go

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package loong64
     6  
     7  import (
     8  	. "cmd/internal/obj/loong64"
     9  	"cmd/internal/objabi"
    10  	"cmd/internal/sys"
    11  	"cmd/link/internal/ld"
    12  	"cmd/link/internal/loader"
    13  	"cmd/link/internal/sym"
    14  	"debug/elf"
    15  	"fmt"
    16  	"log"
    17  )
    18  
    19  func gentext(ctxt *ld.Link, ldr *loader.Loader) {
    20  	initfunc, addmoduledata := ld.PrepareAddmoduledata(ctxt)
    21  	if initfunc == nil {
    22  		return
    23  	}
    24  
    25  	o := func(op uint32) {
    26  		initfunc.AddUint32(ctxt.Arch, op)
    27  	}
    28  
    29  	// Emit the following function:
    30  	//
    31  	//	local.dso_init:
    32  	//		la.pcrel $a0, local.moduledata
    33  	//		b runtime.addmoduledata
    34  
    35  	//	0000000000000000 <local.dso_init>:
    36  	//	0:	1a000004	pcalau12i	$a0, 0
    37  	//				0: R_LARCH_PCALA_HI20	local.moduledata
    38  	o(OP_IR(OpCodeIR(APCALAU12I), 0, REG_R4))
    39  	rel, _ := initfunc.AddRel(objabi.R_LOONG64_ADDR_HI)
    40  	rel.SetOff(0)
    41  	rel.SetSiz(4)
    42  	rel.SetSym(ctxt.Moduledata)
    43  
    44  	//	4:	02c00084	addi.d	$a0, $a0, 0
    45  	//				4: R_LARCH_PCALA_LO12	local.moduledata
    46  	o(OP_12IRR(OpCodeIRR(AADDV), 0, REG_R4, REG_R4))
    47  	rel2, _ := initfunc.AddRel(objabi.R_LOONG64_ADDR_LO)
    48  	rel2.SetOff(4)
    49  	rel2.SetSiz(4)
    50  	rel2.SetSym(ctxt.Moduledata)
    51  
    52  	//	8:	50000000	b	0
    53  	//				8: R_LARCH_B26	runtime.addmoduledata
    54  	o(OP_B_BL(OpCodeI(AJMP), 0))
    55  	rel3, _ := initfunc.AddRel(objabi.R_CALLLOONG64)
    56  	rel3.SetOff(8)
    57  	rel3.SetSiz(4)
    58  	rel3.SetSym(addmoduledata)
    59  }
    60  
    61  func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym, r loader.Reloc, rIdx int) bool {
    62  	targ := r.Sym()
    63  	var targType sym.SymKind
    64  	if targ != 0 {
    65  		targType = ldr.SymType(targ)
    66  	}
    67  
    68  	switch r.Type() {
    69  	default:
    70  		if r.Type() >= objabi.ElfRelocOffset {
    71  			ldr.Errorf(s, "adddynrel: unexpected reloction type %d (%s)", r.Type(), sym.RelocName(target.Arch, r.Type()))
    72  			return false
    73  		}
    74  
    75  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_64):
    76  		if targType == sym.SDYNIMPORT {
    77  			ldr.Errorf(s, "unexpected R_LARCH_64 relocation for dynamic symbol %s", ldr.SymName(targ))
    78  		}
    79  		su := ldr.MakeSymbolUpdater(s)
    80  		su.SetRelocType(rIdx, objabi.R_ADDR)
    81  		if target.IsPIE() && target.IsInternal() {
    82  			// For internal linking PIE, this R_ADDR relocation cannot
    83  			// be resolved statically. We need to generate a dynamic
    84  			// relocation. Let the code below handle it.
    85  			break
    86  		}
    87  		return true
    88  
    89  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_B26),
    90  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_CALL36):
    91  		if targType == sym.SDYNIMPORT {
    92  			addpltsym(target, ldr, syms, targ)
    93  			su := ldr.MakeSymbolUpdater(s)
    94  			su.SetRelocSym(rIdx, syms.PLT)
    95  			su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymPlt(targ)))
    96  		}
    97  		if targType == 0 || targType == sym.SXREF {
    98  			ldr.Errorf(s, "unknown symbol %s in callloong64", ldr.SymName(targ))
    99  		}
   100  		relocType := objabi.R_CALLLOONG64
   101  		if r.Type() == objabi.ElfRelocOffset+objabi.RelocType(elf.R_LARCH_CALL36) {
   102  			relocType = objabi.R_LOONG64_CALL36
   103  		}
   104  		su := ldr.MakeSymbolUpdater(s)
   105  		su.SetRelocType(rIdx, relocType)
   106  		return true
   107  
   108  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_GOT_PC_HI20),
   109  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_GOT_PC_LO12),
   110  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_GOT64_PC_HI12),
   111  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_GOT64_PC_LO20):
   112  		if targType != sym.SDYNIMPORT {
   113  			// TODO: turn LDR of GOT entry into ADR of symbol itself
   114  		}
   115  
   116  		ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_LARCH_64))
   117  		su := ldr.MakeSymbolUpdater(s)
   118  
   119  		var relocType objabi.RelocType
   120  		switch r.Type() - objabi.ElfRelocOffset {
   121  		case objabi.RelocType(elf.R_LARCH_GOT_PC_HI20):
   122  			relocType = objabi.R_LOONG64_ADDR_HI
   123  		case objabi.RelocType(elf.R_LARCH_GOT_PC_LO12):
   124  			relocType = objabi.R_LOONG64_ADDR_LO
   125  		case objabi.RelocType(elf.R_LARCH_GOT64_PC_HI12):
   126  			relocType = objabi.R_LOONG64_ADDR64_HI
   127  		case objabi.RelocType(elf.R_LARCH_GOT64_PC_LO20):
   128  			relocType = objabi.R_LOONG64_ADDR64_LO
   129  		}
   130  
   131  		su.SetRelocType(rIdx, relocType)
   132  		su.SetRelocSym(rIdx, syms.GOT)
   133  		su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymGot(targ)))
   134  		return true
   135  
   136  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_PCALA_HI20),
   137  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_PCALA_LO12),
   138  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_PCALA64_HI12),
   139  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_PCALA64_LO20),
   140  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_PCREL20_S2):
   141  		if targType == sym.SDYNIMPORT {
   142  			ldr.Errorf(s, "unexpected relocation for dynamic symbol %s", ldr.SymName(targ))
   143  		}
   144  		if targType == 0 || targType == sym.SXREF {
   145  			ldr.Errorf(s, "unknown symbol %s", ldr.SymName(targ))
   146  		}
   147  
   148  		var relocType objabi.RelocType
   149  		switch r.Type() - objabi.ElfRelocOffset {
   150  		case objabi.RelocType(elf.R_LARCH_PCALA_HI20):
   151  			relocType = objabi.R_LOONG64_ADDR_HI
   152  		case objabi.RelocType(elf.R_LARCH_PCALA_LO12):
   153  			relocType = objabi.R_LOONG64_ADDR_LO
   154  		case objabi.RelocType(elf.R_LARCH_PCALA64_HI12):
   155  			relocType = objabi.R_LOONG64_ADDR64_HI
   156  		case objabi.RelocType(elf.R_LARCH_PCALA64_LO20):
   157  			relocType = objabi.R_LOONG64_ADDR64_LO
   158  		case objabi.RelocType(elf.R_LARCH_PCREL20_S2):
   159  			relocType = objabi.R_LOONG64_ADDR_PCREL20_S2
   160  		}
   161  
   162  		su := ldr.MakeSymbolUpdater(s)
   163  		su.SetRelocType(rIdx, relocType)
   164  		return true
   165  
   166  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_ADD64),
   167  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_SUB64):
   168  		su := ldr.MakeSymbolUpdater(s)
   169  		if r.Type() == objabi.ElfRelocOffset+objabi.RelocType(elf.R_LARCH_ADD64) {
   170  			su.SetRelocType(rIdx, objabi.R_LOONG64_ADD64)
   171  		} else {
   172  			su.SetRelocType(rIdx, objabi.R_LOONG64_SUB64)
   173  		}
   174  		return true
   175  
   176  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_B16),
   177  		objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_B21):
   178  		if targType == sym.SDYNIMPORT {
   179  			addpltsym(target, ldr, syms, targ)
   180  			su := ldr.MakeSymbolUpdater(s)
   181  			su.SetRelocSym(rIdx, syms.PLT)
   182  			su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymPlt(targ)))
   183  		}
   184  		if targType == 0 || targType == sym.SXREF {
   185  			ldr.Errorf(s, "unknown symbol %s in R_JMPxxLOONG64", ldr.SymName(targ))
   186  		}
   187  		su := ldr.MakeSymbolUpdater(s)
   188  		if r.Type() == objabi.ElfRelocOffset+objabi.RelocType(elf.R_LARCH_B16) {
   189  			su.SetRelocType(rIdx, objabi.R_JMP16LOONG64)
   190  		} else {
   191  			su.SetRelocType(rIdx, objabi.R_JMP21LOONG64)
   192  		}
   193  		return true
   194  	}
   195  
   196  	relocs := ldr.Relocs(s)
   197  	r = relocs.At(rIdx)
   198  
   199  	switch r.Type() {
   200  	case objabi.R_CALLLOONG64:
   201  		if targType != sym.SDYNIMPORT {
   202  			return true
   203  		}
   204  		if target.IsExternal() {
   205  			return true
   206  		}
   207  
   208  		// Internal linking.
   209  		if r.Add() != 0 {
   210  			ldr.Errorf(s, "PLT call with no-zero addend (%v)", r.Add())
   211  		}
   212  
   213  		// Build a PLT entry and change the relocation target to that entry.
   214  		addpltsym(target, ldr, syms, targ)
   215  		su := ldr.MakeSymbolUpdater(s)
   216  		su.SetRelocSym(rIdx, syms.PLT)
   217  		su.SetRelocAdd(rIdx, int64(ldr.SymPlt(targ)))
   218  		return true
   219  
   220  	case objabi.R_ADDR:
   221  		if ldr.SymType(s).IsText() && target.IsElf() {
   222  			// The code is asking for the address of an external
   223  			// function. We provide it with the address of the
   224  			// correspondent GOT symbol.
   225  			ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_LARCH_64))
   226  			su := ldr.MakeSymbolUpdater(s)
   227  			su.SetRelocSym(rIdx, syms.GOT)
   228  			su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymGot(targ)))
   229  			return true
   230  		}
   231  
   232  		// Process dynamic relocations for the data sections.
   233  		if target.IsPIE() && target.IsInternal() {
   234  			// When internally linking, generate dynamic relocations
   235  			// for all typical R_ADDR relocations. The exception
   236  			// are those R_ADDR that are created as part of generating
   237  			// the dynamic relocations and must be resolved statically.
   238  			//
   239  			// There are three phases relevant to understanding this:
   240  			//
   241  			//	dodata()  // we are here
   242  			//	address() // symbol address assignment
   243  			//	reloc()   // resolution of static R_ADDR relocs
   244  			//
   245  			// At this point symbol addresses have not been
   246  			// assigned yet (as the final size of the .rela section
   247  			// will affect the addresses), and so we cannot write
   248  			// the Elf64_Rela.r_offset now. Instead we delay it
   249  			// until after the 'address' phase of the linker is
   250  			// complete. We do this via Addaddrplus, which creates
   251  			// a new R_ADDR relocation which will be resolved in
   252  			// the 'reloc' phase.
   253  			//
   254  			// These synthetic static R_ADDR relocs must be skipped
   255  			// now, or else we will be caught in an infinite loop
   256  			// of generating synthetic relocs for our synthetic
   257  			// relocs.
   258  			//
   259  			// Furthermore, the rela sections contain dynamic
   260  			// relocations with R_ADDR relocations on
   261  			// Elf64_Rela.r_offset. This field should contain the
   262  			// symbol offset as determined by reloc(), not the
   263  			// final dynamically linked address as a dynamic
   264  			// relocation would provide.
   265  			switch ldr.SymName(s) {
   266  			case ".dynsym", ".rela", ".rela.plt", ".got.plt", ".dynamic":
   267  				return false
   268  			}
   269  		} else {
   270  			// Either internally linking a static executable,
   271  			// in which case we can resolve these relocations
   272  			// statically in the 'reloc' phase, or externally
   273  			// linking, in which case the relocation will be
   274  			// prepared in the 'reloc' phase and passed to the
   275  			// external linker in the 'asmb' phase.
   276  			if t := ldr.SymType(s); !t.IsDATA() && !t.IsNOPTRDATA() && !t.IsRODATA() {
   277  				break
   278  			}
   279  		}
   280  
   281  		if target.IsElf() {
   282  			ld.ElfAddDynAddrReloc(target, ldr, syms, s, r, rIdx, uint32(elf.R_LARCH_64), uint32(elf.R_LARCH_RELATIVE))
   283  			return true
   284  		}
   285  
   286  	case objabi.R_LOONG64_GOT_HI,
   287  		objabi.R_LOONG64_GOT_LO,
   288  		objabi.R_LOONG64_GOT64_HI,
   289  		objabi.R_LOONG64_GOT64_LO:
   290  		ld.AddGotSym(target, ldr, syms, targ, uint32(elf.R_LARCH_64))
   291  		su := ldr.MakeSymbolUpdater(s)
   292  
   293  		var relocType objabi.RelocType
   294  		switch r.Type() {
   295  		case objabi.R_LOONG64_GOT_HI:
   296  			relocType = objabi.R_LOONG64_ADDR_HI
   297  		case objabi.R_LOONG64_GOT_LO:
   298  			relocType = objabi.R_LOONG64_ADDR_LO
   299  		case objabi.R_LOONG64_GOT64_HI:
   300  			relocType = objabi.R_LOONG64_ADDR64_HI
   301  		case objabi.R_LOONG64_GOT64_LO:
   302  			relocType = objabi.R_LOONG64_ADDR64_LO
   303  		}
   304  
   305  		su.SetRelocType(rIdx, relocType)
   306  		su.SetRelocSym(rIdx, syms.GOT)
   307  		su.SetRelocAdd(rIdx, r.Add()+int64(ldr.SymGot(targ)))
   308  		return true
   309  	}
   310  	return false
   311  }
   312  
   313  func elfsetupplt(ctxt *ld.Link, ldr *loader.Loader, plt, gotplt *loader.SymbolBuilder, dynamic loader.Sym) {
   314  	if plt.Size() == 0 {
   315  		var t uint32
   316  
   317  		// pcalau12i $r14, imm
   318  		t = OP_IR(OpCodeIR(APCALAU12I), 0, REG_R14)
   319  		plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 0, objabi.R_LOONG64_ADDR_HI, 4)
   320  		plt.SetUint32(ctxt.Arch, plt.Size()-4, t)
   321  
   322  		// sub.d $r13, $r13, $r15
   323  		t = OP_RRR(OpCodeRRR(ASUBV), REG_R15, REG_R13, REG_R13)
   324  		plt.AddUint32(ctxt.Arch, t)
   325  
   326  		// ld.d $r15, $r14, imm
   327  		t = OP_12IRR(OpCodeIRR(-AMOVV), 0, REG_R14, REG_R15)
   328  		plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 0, objabi.R_LOONG64_ADDR_LO, 4)
   329  		plt.SetUint32(ctxt.Arch, plt.Size()-4, t)
   330  
   331  		// addi.d $r13, $r13, -40
   332  		t = OP_12IRR(OpCodeIRR(AADDV), uint32(0xfd8), REG_R13, REG_R13)
   333  		plt.AddUint32(ctxt.Arch, t)
   334  
   335  		// addi.d $r12, $r14, imm
   336  		t = OP_12IRR(OpCodeIRR(AADDV), 0, REG_R14, REG_R12)
   337  		plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 0, objabi.R_LOONG64_ADDR_LO, 4)
   338  		plt.SetUint32(ctxt.Arch, plt.Size()-4, t)
   339  
   340  		// srli.d $r13, $r13, 1
   341  		t = OP_6IRR(OpCodeIRR(ASRLV), 1, REG_R13, REG_R13)
   342  		plt.AddUint32(ctxt.Arch, t)
   343  
   344  		// ld.d $r12, $r12, 8
   345  		t = OP_12IRR(OpCodeIRR(-AMOVV), 8, REG_R12, REG_R12)
   346  		plt.AddUint32(ctxt.Arch, t)
   347  
   348  		// jirl $r0, $r15, 0
   349  		t = OP_16IRR(OpCodeIRR(AJAL), 0, REG_R15, REG_R0)
   350  		plt.AddUint32(ctxt.Arch, t)
   351  
   352  		// check gotplt.size == 0
   353  		if gotplt.Size() != 0 {
   354  			ctxt.Errorf(gotplt.Sym(), "got.plt is not empty at the very beginning")
   355  		}
   356  
   357  		gotplt.AddUint64(ctxt.Arch, 0)
   358  		gotplt.AddUint64(ctxt.Arch, 0)
   359  	}
   360  }
   361  
   362  func addpltsym(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym) {
   363  	if ldr.SymPlt(s) >= 0 {
   364  		return
   365  	}
   366  
   367  	ld.Adddynsym(ldr, target, syms, s)
   368  
   369  	if target.IsElf() {
   370  		var t uint32
   371  		plt := ldr.MakeSymbolUpdater(syms.PLT)
   372  		gotplt := ldr.MakeSymbolUpdater(syms.GOTPLT)
   373  		rela := ldr.MakeSymbolUpdater(syms.RelaPLT)
   374  		if plt.Size() == 0 {
   375  			panic("plt is not set up")
   376  		}
   377  
   378  		// pcalau12i $r15, imm
   379  		t = OP_IR(OpCodeIR(APCALAU12I), 0, REG_R15)
   380  		plt.AddAddrPlus4(target.Arch, gotplt.Sym(), gotplt.Size())
   381  		plt.SetUint32(target.Arch, plt.Size()-4, t)
   382  		relocs := plt.Relocs()
   383  		plt.SetRelocType(relocs.Count()-1, objabi.R_LOONG64_ADDR_HI)
   384  
   385  		// ld.d $r15, $r15, imm
   386  		t = OP_12IRR(OpCodeIRR(-AMOVV), 0, REG_R15, REG_R15)
   387  		plt.AddAddrPlus4(target.Arch, gotplt.Sym(), gotplt.Size())
   388  		plt.SetUint32(target.Arch, plt.Size()-4, t)
   389  		relocs = plt.Relocs()
   390  		plt.SetRelocType(relocs.Count()-1, objabi.R_LOONG64_ADDR_LO)
   391  
   392  		// pcaddu12i $r13, 0
   393  		t = OP_IR(OpCodeIR(APCADDU12I), 0, REG_R13)
   394  		plt.AddUint32(target.Arch, t)
   395  
   396  		// jirl r0, r15, 0
   397  		t = OP_16IRR(OpCodeIRR(AJAL), 0, REG_R15, REG_R0)
   398  		plt.AddUint32(target.Arch, t)
   399  
   400  		// add to got.plt: pointer to plt[0]
   401  		gotplt.AddAddrPlus(target.Arch, plt.Sym(), 0)
   402  
   403  		// rela
   404  		rela.AddAddrPlus(target.Arch, gotplt.Sym(), gotplt.Size()-8)
   405  		sDynid := ldr.SymDynid(s)
   406  		rela.AddUint64(target.Arch, elf.R_INFO(uint32(sDynid), uint32(elf.R_LARCH_JUMP_SLOT)))
   407  		rela.AddUint64(target.Arch, 0)
   408  
   409  		ldr.SetPlt(s, int32(plt.Size()-16))
   410  	} else {
   411  		ldr.Errorf(s, "addpltsym: unsupport binary format")
   412  	}
   413  }
   414  
   415  func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, ri int, sectoff int64) bool {
   416  	// loong64 ELF relocation (endian neutral)
   417  	//		offset     uint64
   418  	//		symreloc   uint64  // The high 32-bit is the symbol, the low 32-bit is the relocation type.
   419  	//		addend     int64
   420  
   421  	elfsym := ld.ElfSymForReloc(ctxt, r.Xsym)
   422  	switch r.Type {
   423  	default:
   424  		return false
   425  	case objabi.R_ADDR, objabi.R_DWARFSECREF:
   426  		switch r.Size {
   427  		case 4:
   428  			out.Write64(uint64(sectoff))
   429  			out.Write64(uint64(elf.R_LARCH_32) | uint64(elfsym)<<32)
   430  			out.Write64(uint64(r.Xadd))
   431  		case 8:
   432  			out.Write64(uint64(sectoff))
   433  			out.Write64(uint64(elf.R_LARCH_64) | uint64(elfsym)<<32)
   434  			out.Write64(uint64(r.Xadd))
   435  		default:
   436  			return false
   437  		}
   438  	case objabi.R_LOONG64_TLS_LE_LO:
   439  		out.Write64(uint64(sectoff))
   440  		out.Write64(uint64(elf.R_LARCH_TLS_LE_LO12) | uint64(elfsym)<<32)
   441  		out.Write64(uint64(r.Xadd))
   442  
   443  	case objabi.R_LOONG64_TLS_LE_HI:
   444  		out.Write64(uint64(sectoff))
   445  		out.Write64(uint64(elf.R_LARCH_TLS_LE_HI20) | uint64(elfsym)<<32)
   446  		out.Write64(uint64(r.Xadd))
   447  
   448  	case objabi.R_CALLLOONG64:
   449  		out.Write64(uint64(sectoff))
   450  		out.Write64(uint64(elf.R_LARCH_B26) | uint64(elfsym)<<32)
   451  		out.Write64(uint64(r.Xadd))
   452  
   453  	case objabi.R_LOONG64_CALL36:
   454  		out.Write64(uint64(sectoff))
   455  		out.Write64(uint64(elf.R_LARCH_CALL36) | uint64(elfsym)<<32)
   456  		out.Write64(uint64(r.Xadd))
   457  
   458  	case objabi.R_LOONG64_TLS_IE_HI:
   459  		out.Write64(uint64(sectoff))
   460  		out.Write64(uint64(elf.R_LARCH_TLS_IE_PC_HI20) | uint64(elfsym)<<32)
   461  		out.Write64(uint64(0x0))
   462  
   463  	case objabi.R_LOONG64_TLS_IE_LO:
   464  		out.Write64(uint64(sectoff))
   465  		out.Write64(uint64(elf.R_LARCH_TLS_IE_PC_LO12) | uint64(elfsym)<<32)
   466  		out.Write64(uint64(0x0))
   467  
   468  	case objabi.R_LOONG64_ADDR_LO:
   469  		out.Write64(uint64(sectoff))
   470  		out.Write64(uint64(elf.R_LARCH_PCALA_LO12) | uint64(elfsym)<<32)
   471  		out.Write64(uint64(r.Xadd))
   472  
   473  	case objabi.R_LOONG64_ADDR_HI:
   474  		out.Write64(uint64(sectoff))
   475  		out.Write64(uint64(elf.R_LARCH_PCALA_HI20) | uint64(elfsym)<<32)
   476  		out.Write64(uint64(r.Xadd))
   477  
   478  	case objabi.R_LOONG64_ADDR64_LO:
   479  		out.Write64(uint64(sectoff))
   480  		out.Write64(uint64(elf.R_LARCH_PCALA64_LO20) | uint64(elfsym)<<32)
   481  		out.Write64(uint64(r.Xadd))
   482  
   483  	case objabi.R_LOONG64_ADDR64_HI:
   484  		out.Write64(uint64(sectoff))
   485  		out.Write64(uint64(elf.R_LARCH_PCALA64_HI12) | uint64(elfsym)<<32)
   486  		out.Write64(uint64(r.Xadd))
   487  
   488  	case objabi.R_LOONG64_ADDR_PCREL20_S2:
   489  		out.Write64(uint64(sectoff))
   490  		out.Write64(uint64(elf.R_LARCH_PCREL20_S2) | uint64(elfsym)<<32)
   491  		out.Write64(uint64(r.Xadd))
   492  
   493  	case objabi.R_LOONG64_GOT_HI:
   494  		out.Write64(uint64(sectoff))
   495  		out.Write64(uint64(elf.R_LARCH_GOT_PC_HI20) | uint64(elfsym)<<32)
   496  		out.Write64(uint64(0x0))
   497  
   498  	case objabi.R_LOONG64_GOT_LO:
   499  		out.Write64(uint64(sectoff))
   500  		out.Write64(uint64(elf.R_LARCH_GOT_PC_LO12) | uint64(elfsym)<<32)
   501  		out.Write64(uint64(0x0))
   502  
   503  	case objabi.R_LOONG64_GOT64_HI:
   504  		out.Write64(uint64(sectoff))
   505  		out.Write64(uint64(elf.R_LARCH_GOT64_PC_HI12) | uint64(elfsym)<<32)
   506  		out.Write64(uint64(0x0))
   507  
   508  	case objabi.R_LOONG64_GOT64_LO:
   509  		out.Write64(uint64(sectoff))
   510  		out.Write64(uint64(elf.R_LARCH_GOT64_PC_LO20) | uint64(elfsym)<<32)
   511  		out.Write64(uint64(0x0))
   512  	}
   513  
   514  	return true
   515  }
   516  
   517  func machoreloc1(*sys.Arch, *ld.OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool {
   518  	return false
   519  }
   520  
   521  func archreloc(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, r loader.Reloc, s loader.Sym, val int64) (o int64, nExtReloc int, ok bool) {
   522  	rs := r.Sym()
   523  	if target.IsExternal() {
   524  		switch r.Type() {
   525  		default:
   526  			return val, 0, false
   527  		case objabi.R_LOONG64_ADDR_HI,
   528  			objabi.R_LOONG64_ADDR_LO,
   529  			objabi.R_LOONG64_ADDR64_HI,
   530  			objabi.R_LOONG64_ADDR64_LO,
   531  			objabi.R_LOONG64_ADDR_PCREL20_S2:
   532  			// set up addend for eventual relocation via outer symbol.
   533  			rs, _ := ld.FoldSubSymbolOffset(ldr, rs)
   534  			rst := ldr.SymType(rs)
   535  			if rst != sym.SHOSTOBJ && rst != sym.SDYNIMPORT && ldr.SymSect(rs) == nil {
   536  				ldr.Errorf(s, "missing section for %s", ldr.SymName(rs))
   537  			}
   538  			return val, 1, true
   539  
   540  		case objabi.R_LOONG64_TLS_LE_HI,
   541  			objabi.R_LOONG64_TLS_LE_LO,
   542  			objabi.R_CALLLOONG64,
   543  			objabi.R_LOONG64_CALL36,
   544  			objabi.R_LOONG64_TLS_IE_HI,
   545  			objabi.R_LOONG64_TLS_IE_LO,
   546  			objabi.R_LOONG64_GOT_HI,
   547  			objabi.R_LOONG64_GOT_LO,
   548  			objabi.R_LOONG64_GOT64_HI,
   549  			objabi.R_LOONG64_GOT64_LO:
   550  			return val, 1, true
   551  		}
   552  	}
   553  
   554  	const isOk = true
   555  	const noExtReloc = 0
   556  
   557  	switch r.Type() {
   558  	case objabi.R_LOONG64_ADDR_HI,
   559  		objabi.R_LOONG64_ADDR_LO,
   560  		objabi.R_LOONG64_ADDR64_HI,
   561  		objabi.R_LOONG64_ADDR64_LO:
   562  		pc := ldr.SymValue(s) + int64(r.Off())
   563  		var t int64
   564  
   565  		switch r.Type() {
   566  		case objabi.R_LOONG64_ADDR_LO:
   567  			// pcalau12i
   568  			t = pc32RelocBits(r.Type(), ldr.SymAddr(rs)+r.Add(), pc)
   569  			o = val&0xffc003ff | (t << 10)
   570  		case objabi.R_LOONG64_ADDR_HI:
   571  			// addi.w/addi.d
   572  			t = pc32RelocBits(r.Type(), ldr.SymAddr(rs)+r.Add(), pc)
   573  			o = val&0xfe00001f | (t << 5)
   574  		case objabi.R_LOONG64_ADDR64_LO:
   575  			// lu32i.d
   576  			t = pc64RelocBits(r.Type(), ldr.SymAddr(rs)+r.Add(), pc-8)
   577  			o = val&0xfe00001f | (t << 5)
   578  		case objabi.R_LOONG64_ADDR64_HI:
   579  			// lu52i.d
   580  			t = pc64RelocBits(r.Type(), ldr.SymAddr(rs)+r.Add(), pc-12)
   581  			o = val&0xffc003ff | (t << 10)
   582  		}
   583  
   584  		return o, noExtReloc, isOk
   585  
   586  	case objabi.R_LOONG64_ADDR_PCREL20_S2:
   587  		pc := ldr.SymValue(s) + int64(r.Off())
   588  		t := (ldr.SymAddr(rs) + r.Add() - pc) >> 2
   589  		if t < -1<<21 || t >= 1<<21 {
   590  			ldr.Errorf(s, "reloc: R_LOONG64_ADDR_PCREL20_S2, offset out of range %d", t)
   591  		}
   592  		// pcaddi
   593  		return val&0xfe00001f | ((t & 0xfffff) << 5), noExtReloc, isOk
   594  
   595  	case objabi.R_LOONG64_TLS_LE_HI,
   596  		objabi.R_LOONG64_TLS_LE_LO:
   597  		t := ldr.SymAddr(rs) + r.Add()
   598  		if t < -1<<31 || t >= 1<<31 {
   599  			ldr.Errorf(s, "reloc: R_LOONG64_TLS_LE_HI/LO, TLS offset out of range %d", t)
   600  		}
   601  		if r.Type() == objabi.R_LOONG64_TLS_LE_LO {
   602  			// ori
   603  			return val&0xffc003ff | ((t & 0xfff) << 10), noExtReloc, isOk
   604  		}
   605  		// lu12i.w
   606  		return val&0xfe00001f | (((t) >> 12 << 5) & 0x1ffffe0), noExtReloc, isOk
   607  
   608  	case objabi.R_CALLLOONG64:
   609  		pc := ldr.SymValue(s) + int64(r.Off())
   610  		t := ldr.SymAddr(rs) + r.Add() - pc
   611  		if t < -1<<27 || t >= 1<<27 {
   612  			ldr.Errorf(s, "reloc: R_CALLLOONG64, program too large, call relocation distance = %d", t)
   613  		}
   614  		// bl
   615  		return val&0xfc000000 | (((t >> 2) & 0xffff) << 10) | (((t >> 2) & 0x3ff0000) >> 16), noExtReloc, isOk
   616  
   617  	case objabi.R_LOONG64_CALL36:
   618  		pc := ldr.SymValue(s) + int64(r.Off())
   619  		t := (ldr.SymAddr(rs) + r.Add() - pc) >> 2
   620  		if t < -1<<37 || t >= 1<<37 {
   621  			ldr.Errorf(s, "reloc: R_LOONG64_CALL36, program too large, call relocation distance = %d", t)
   622  		}
   623  		// val is pcaddu18i (lower half) + jirl (upper half)
   624  		pcaddu18i := (val & 0xfe00001f) | (((t + 0x8000) >> 16) << 5)
   625  		jirl := ((val >> 32) & 0xfc0003ff) | ((t & 0xffff) << 10)
   626  		return pcaddu18i | (jirl << 32), noExtReloc, isOk
   627  
   628  	case objabi.R_JMP16LOONG64,
   629  		objabi.R_JMP21LOONG64:
   630  		pc := ldr.SymValue(s) + int64(r.Off())
   631  		t := ldr.SymAddr(rs) + r.Add() - pc
   632  		if r.Type() == objabi.R_JMP16LOONG64 {
   633  			if t < -1<<17 || t >= 1<<17 {
   634  				ldr.Errorf(s, "reloc: R_JMP16LOONG64, program too large, jmp relocation distance = %d", t)
   635  			}
   636  			// beq/bne/blt[u]/bge[u]
   637  			return val&0xfc0003ff | (((t >> 2) & 0xffff) << 10), noExtReloc, isOk
   638  		}
   639  		if t < -1<<22 || t >= 1<<22 {
   640  			ldr.Errorf(s, "reloc: R_JMP21LOONG64, program too large, call relocation distance = %d", t)
   641  		}
   642  		// beqz/bnez
   643  		return val&0xfc0003e0 | (((t >> 2) & 0xffff) << 10) | (((t >> 2) & 0x1f0000) >> 16), noExtReloc, isOk
   644  
   645  	case objabi.R_LOONG64_TLS_IE_HI,
   646  		objabi.R_LOONG64_TLS_IE_LO:
   647  		if target.IsPIE() && target.IsElf() {
   648  			if !target.IsLinux() {
   649  				ldr.Errorf(s, "TLS reloc on unsupported OS %v", target.HeadType)
   650  			}
   651  			t := ldr.SymAddr(rs) + r.Add()
   652  			if t < -1<<31 || t >= 1<<31 {
   653  				ldr.Errorf(s, "reloc: R_LOONG64_TLS_IE_HI/LO, TLS offset out of range %d", t)
   654  			}
   655  			if r.Type() == objabi.R_LOONG64_TLS_IE_HI {
   656  				// pcalau12i -> lu12i.w
   657  				return (0x14000000 | (val & 0x1f) | ((t >> 12) << 5)), noExtReloc, isOk
   658  			}
   659  			// ld.d -> ori
   660  			return (0x03800000 | (val & 0x3ff) | ((t & 0xfff) << 10)), noExtReloc, isOk
   661  		} else {
   662  			log.Fatalf("cannot handle R_LOONG64_TLS_IE_x (sym %s) when linking internally", ldr.SymName(rs))
   663  		}
   664  
   665  	case objabi.R_LOONG64_ADD64, objabi.R_LOONG64_SUB64:
   666  		if r.Type() == objabi.R_LOONG64_ADD64 {
   667  			return val + ldr.SymAddr(rs) + r.Add(), noExtReloc, isOk
   668  		}
   669  		return val - (ldr.SymAddr(rs) + r.Add()), noExtReloc, isOk
   670  	}
   671  
   672  	return val, 0, false
   673  }
   674  
   675  func archrelocvariant(*ld.Target, *loader.Loader, loader.Reloc, sym.RelocVariant, loader.Sym, int64, []byte) int64 {
   676  	return -1
   677  }
   678  
   679  func extreloc(target *ld.Target, ldr *loader.Loader, r loader.Reloc, s loader.Sym) (loader.ExtReloc, bool) {
   680  	switch r.Type() {
   681  	case objabi.R_LOONG64_ADDR_HI,
   682  		objabi.R_LOONG64_ADDR_LO,
   683  		objabi.R_LOONG64_ADDR64_HI,
   684  		objabi.R_LOONG64_ADDR64_LO,
   685  		objabi.R_LOONG64_GOT_HI,
   686  		objabi.R_LOONG64_GOT_LO,
   687  		objabi.R_LOONG64_GOT64_HI,
   688  		objabi.R_LOONG64_GOT64_LO:
   689  		return ld.ExtrelocViaOuterSym(ldr, r, s), true
   690  
   691  	case objabi.R_LOONG64_TLS_LE_HI,
   692  		objabi.R_LOONG64_TLS_LE_LO,
   693  		objabi.R_CALLLOONG64,
   694  		objabi.R_LOONG64_TLS_IE_HI,
   695  		objabi.R_LOONG64_TLS_IE_LO:
   696  		return ld.ExtrelocSimple(ldr, r), true
   697  	}
   698  	return loader.ExtReloc{}, false
   699  }
   700  
   701  // Comments from copying binutils/bfd/elfnn-loongarch.c
   702  //
   703  // For example: pc is 0x11000010000100, symbol is 0x1812348ffff812
   704  //
   705  // offset = (0x1812348ffff812 & ~0xfff) - (0x11000010000100 & ~0xfff)
   706  // offset = 0x712347ffff000
   707  //
   708  // lo12: 0x1812348ffff812 & 0xfff = 0x812
   709  // hi20: 0x7ffff + 0x1(lo12 > 0x7ff) = 0x80000
   710  // lo20: 0x71234 - 0x1(lo12 > 0x7ff) + 0x1(hi20 > 0x7ffff)
   711  // hi12: 0x0
   712  //
   713  // pcalau12i $t1, hi20 (0x80000)
   714  //
   715  //	$t1 = 0x11000010000100 + sign-extend(0x80000 << 12)
   716  //	    = 0x11000010000100 + 0xffffffff80000000
   717  //	    = 0x10ffff90000000
   718  //
   719  // addi.d $t0, $zero, lo12 (0x812)
   720  //
   721  //	$t0 = 0xfffffffffffff812 (if lo12 > 0x7ff, because sign-extend,
   722  //	lo20 need to sub 0x1)
   723  //
   724  // lu32i.d $t0, lo20 (0x71234)
   725  //
   726  //	$t0 = {0x71234, 0xfffff812}
   727  //	    = 0x71234fffff812
   728  //
   729  // lu52i.d $t0, hi12 (0x0)
   730  //
   731  //	$t0 = {0x0, 0x71234fffff812}
   732  //	    = 0x71234fffff812
   733  func pc32RelocBits(reloc objabi.RelocType, tgt int64, pc int64) int64 {
   734  	lo12 := tgt & 0xfff
   735  	if reloc == objabi.R_LOONG64_ADDR_LO {
   736  		return lo12
   737  	}
   738  
   739  	off := (tgt & ^0xfff) - (pc & ^0xfff)
   740  	if lo12 >= 0x800 {
   741  		off += 0x1000
   742  	}
   743  
   744  	// objabi.R_LOONG64_ADDR_HI
   745  	return (off >> 12) & 0xfffff
   746  }
   747  
   748  func pc64RelocBits(reloc objabi.RelocType, tgt int64, pc int64) int64 {
   749  	lo12 := tgt & 0xfff
   750  	off := (tgt & ^0xfff) - (pc & ^0xfff)
   751  
   752  	if lo12 >= 0x800 {
   753  		off += (0x1000 - 0x100000000)
   754  	}
   755  
   756  	if (off & 0x80000000) != 0 {
   757  		off += 0x100000000
   758  	}
   759  
   760  	if reloc == objabi.R_LOONG64_ADDR64_LO {
   761  		return (off >> 32) & 0xfffff
   762  	}
   763  
   764  	// objabi.R_LOONG64_ADDR64_HI
   765  	return (off >> 52) & 0xfff
   766  }
   767  
   768  // Convert the direct jump relocation r to refer to a trampoline if the target is too far.
   769  func trampoline(ctxt *ld.Link, ldr *loader.Loader, ri int, rs, s loader.Sym) {
   770  	relocs := ldr.Relocs(s)
   771  	r := relocs.At(ri)
   772  	switch r.Type() {
   773  	case objabi.ElfRelocOffset + objabi.RelocType(elf.R_LARCH_B26), objabi.R_CALLLOONG64:
   774  		var t int64
   775  		// ldr.SymValue(rs) == 0 indicates a cross-package jump to a function that is not yet
   776  		// laid out. Conservatively use a trampoline. This should be rare, as we lay out packages
   777  		// in dependency order.
   778  		if ldr.SymValue(rs) != 0 {
   779  			t = ldr.SymValue(rs) + r.Add() - (ldr.SymValue(s) + int64(r.Off()))
   780  		}
   781  		if t >= 1<<27 || t < -1<<27 || ldr.SymValue(rs) == 0 || (*ld.FlagDebugTramp > 1 && (ldr.SymPkg(s) == "" || ldr.SymPkg(s) != ldr.SymPkg(rs))) {
   782  			// direct call too far need to insert trampoline.
   783  			// look up existing trampolines first. if we found one within the range
   784  			// of direct call, we can reuse it. otherwise create a new one.
   785  			var tramp loader.Sym
   786  			for i := 0; ; i++ {
   787  				oName := ldr.SymName(rs)
   788  				name := oName + fmt.Sprintf("%+x-tramp%d", r.Add(), i)
   789  				tramp = ldr.LookupOrCreateSym(name, ldr.SymVersion(rs))
   790  				ldr.SetAttrReachable(tramp, true)
   791  				if ldr.SymType(tramp) == sym.SDYNIMPORT {
   792  					// don't reuse trampoline defined in other module
   793  					continue
   794  				}
   795  				if oName == "runtime.deferreturn" {
   796  					ldr.SetIsDeferReturnTramp(tramp, true)
   797  				}
   798  				if ldr.SymValue(tramp) == 0 {
   799  					// either the trampoline does not exist -- we need to create one,
   800  					// or found one the address which is not assigned -- this will be
   801  					// laid down immediately after the current function. use this one.
   802  					break
   803  				}
   804  
   805  				t = ldr.SymValue(tramp) - (ldr.SymValue(s) + int64(r.Off()))
   806  				if t >= -1<<27 && t < 1<<27 {
   807  					// found an existing trampoline that is not too far
   808  					// we can just use it.
   809  					break
   810  				}
   811  			}
   812  			if ldr.SymType(tramp) == 0 {
   813  				// trampoline does not exist, create one
   814  				trampb := ldr.MakeSymbolUpdater(tramp)
   815  				ctxt.AddTramp(trampb, ldr.SymType(s))
   816  				if ldr.SymType(rs) == sym.SDYNIMPORT {
   817  					if r.Add() != 0 {
   818  						ctxt.Errorf(s, "nonzero addend for DYNIMPORT call: %v+%d", ldr.SymName(rs), r.Add())
   819  					}
   820  					gentrampgot(ctxt, ldr, trampb, rs)
   821  				} else {
   822  					gentramp(ctxt, ldr, trampb, rs, r.Add())
   823  					ctxt.AddDwarfDirectTrampoline(tramp, rs, r.Add(), ldr.SymUnit(s))
   824  				}
   825  			}
   826  			// modify reloc to point to tramp, which will be resolved later
   827  			sb := ldr.MakeSymbolUpdater(s)
   828  			relocs := sb.Relocs()
   829  			r := relocs.At(ri)
   830  			r.SetSym(tramp)
   831  			r.SetAdd(0) // clear the offset embedded in the instruction
   832  		}
   833  	default:
   834  		ctxt.Errorf(s, "trampoline called with non-jump reloc: %d (%s)", r.Type(), sym.RelocName(ctxt.Arch, r.Type()))
   835  	}
   836  }
   837  
   838  // generate a trampoline to target+offset.
   839  func gentramp(ctxt *ld.Link, ldr *loader.Loader, tramp *loader.SymbolBuilder, target loader.Sym, offset int64) {
   840  	tramp.SetSize(12) // 3 instructions
   841  	P := make([]byte, tramp.Size())
   842  
   843  	o1 := OP_IR(OpCodeIR(APCALAU12I), 0, REGRT1) // pcalau12i $r20, 0
   844  	ctxt.Arch.ByteOrder.PutUint32(P, o1)
   845  	r1, _ := tramp.AddRel(objabi.R_LOONG64_ADDR_HI)
   846  	r1.SetOff(0)
   847  	r1.SetSiz(4)
   848  	r1.SetSym(target)
   849  	r1.SetAdd(offset)
   850  
   851  	o2 := OP_12IRR(OpCodeIRR(AADDV), 0, REGRT1, REGRT1) // addi.d $r20, $r20, 0
   852  	ctxt.Arch.ByteOrder.PutUint32(P[4:], o2)
   853  	r2, _ := tramp.AddRel(objabi.R_LOONG64_ADDR_LO)
   854  	r2.SetOff(4)
   855  	r2.SetSiz(4)
   856  	r2.SetSym(target)
   857  	r2.SetAdd(offset)
   858  
   859  	o3 := OP_16IRR(OpCodeIRR(AJAL), 0, REGRT1, REG_R0) // jirl $r0, $r20, 0
   860  	ctxt.Arch.ByteOrder.PutUint32(P[8:], o3)
   861  
   862  	tramp.SetData(P)
   863  }
   864  
   865  func gentrampgot(ctxt *ld.Link, ldr *loader.Loader, tramp *loader.SymbolBuilder, target loader.Sym) {
   866  	tramp.SetSize(12) // 3 instructions
   867  	P := make([]byte, tramp.Size())
   868  
   869  	o1 := OP_IR(OpCodeIR(APCALAU12I), 0, REGRT1) // pcalau12i $r20, 0
   870  	ctxt.Arch.ByteOrder.PutUint32(P, o1)
   871  	r1, _ := tramp.AddRel(objabi.R_LOONG64_GOT_HI)
   872  	r1.SetOff(0)
   873  	r1.SetSiz(4)
   874  	r1.SetSym(target)
   875  
   876  	o2 := OP_12IRR(OpCodeIRR(-AMOVV), 0, REGRT1, REGRT1) // ld.d $r20, $r20, 0
   877  	ctxt.Arch.ByteOrder.PutUint32(P[4:], o2)
   878  	r2, _ := tramp.AddRel(objabi.R_LOONG64_GOT_LO)
   879  	r2.SetOff(4)
   880  	r2.SetSiz(4)
   881  	r2.SetSym(target)
   882  
   883  	o3 := OP_16IRR(OpCodeIRR(AJAL), 0, REGRT1, REG_R0) // jirl $r0, $r20, 0
   884  	ctxt.Arch.ByteOrder.PutUint32(P[8:], o3)
   885  
   886  	tramp.SetData(P)
   887  }
   888  

View as plain text