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

     1  // Inferno utils/5l/asm.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/5l/asm.c
     3  //
     4  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     5  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     6  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     7  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     8  //	Portions Copyright © 2004,2006 Bruce Ellis
     9  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    10  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    11  //	Portions Copyright © 2009 The Go Authors. All rights reserved.
    12  //
    13  // Permission is hereby granted, free of charge, to any person obtaining a copy
    14  // of this software and associated documentation files (the "Software"), to deal
    15  // in the Software without restriction, including without limitation the rights
    16  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    17  // copies of the Software, and to permit persons to whom the Software is
    18  // furnished to do so, subject to the following conditions:
    19  //
    20  // The above copyright notice and this permission notice shall be included in
    21  // all copies or substantial portions of the Software.
    22  //
    23  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    24  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    25  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    26  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    27  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    28  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    29  // THE SOFTWARE.
    30  
    31  package mips64
    32  
    33  import (
    34  	"cmd/internal/objabi"
    35  	"cmd/internal/sys"
    36  	"cmd/link/internal/ld"
    37  	"cmd/link/internal/loader"
    38  	"cmd/link/internal/sym"
    39  	"debug/elf"
    40  )
    41  
    42  var (
    43  	// dtOffsets contains offsets for entries within the .dynamic section.
    44  	// These are used to fix up symbol values once they are known.
    45  	dtOffsets map[elf.DynTag]int64
    46  
    47  	// dynSymCount contains the number of entries in the .dynsym section.
    48  	// This is used to populate the DT_MIPS_SYMTABNO entry in the .dynamic
    49  	// section.
    50  	dynSymCount uint64
    51  
    52  	// gotLocalCount contains the number of local global offset table
    53  	// entries. This is used to populate the DT_MIPS_LOCAL_GOTNO entry in
    54  	// the .dynamic section.
    55  	gotLocalCount uint64
    56  
    57  	// gotSymIndex contains the index of the first dynamic symbol table
    58  	// entry that corresponds to an entry in the global offset table.
    59  	// This is used to populate the DT_MIPS_GOTSYM entry in the .dynamic
    60  	// section.
    61  	gotSymIndex uint64
    62  )
    63  
    64  func gentext(ctxt *ld.Link, ldr *loader.Loader) {
    65  	if *ld.FlagD || ctxt.Target.IsExternal() {
    66  		return
    67  	}
    68  
    69  	dynamic := ldr.MakeSymbolUpdater(ctxt.ArchSyms.Dynamic)
    70  
    71  	ld.Elfwritedynent(ctxt.Arch, dynamic, elf.DT_MIPS_RLD_VERSION, 1)
    72  	ld.Elfwritedynent(ctxt.Arch, dynamic, elf.DT_MIPS_BASE_ADDRESS, 0)
    73  
    74  	// elfsetupplt should have been called and gotLocalCount should now
    75  	// have its correct value.
    76  	if gotLocalCount == 0 {
    77  		ctxt.Errorf(0, "internal error: elfsetupplt has not been called")
    78  	}
    79  	ld.Elfwritedynent(ctxt.Arch, dynamic, elf.DT_MIPS_LOCAL_GOTNO, gotLocalCount)
    80  
    81  	// DT_* entries have to exist prior to elfdynhash(), which finalises the
    82  	// table by adding DT_NULL. However, the values for the following entries
    83  	// are not know until after dynreloc() has completed. Add the symbols now,
    84  	// then update their values prior to code generation.
    85  	dts := []elf.DynTag{
    86  		elf.DT_MIPS_SYMTABNO,
    87  		elf.DT_MIPS_GOTSYM,
    88  	}
    89  	dtOffsets = make(map[elf.DynTag]int64)
    90  	for _, dt := range dts {
    91  		ld.Elfwritedynent(ctxt.Arch, dynamic, dt, 0)
    92  		dtOffsets[dt] = dynamic.Size() - 8
    93  	}
    94  }
    95  
    96  func adddynrel(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym, r loader.Reloc, rIdx int) bool {
    97  	targ := r.Sym()
    98  	var targType sym.SymKind
    99  	if targ != 0 {
   100  		targType = ldr.SymType(targ)
   101  	}
   102  
   103  	if r.Type() >= objabi.ElfRelocOffset {
   104  		ldr.Errorf(s, "unexpected relocation type %d (%s)", r.Type(), sym.RelocName(target.Arch, r.Type()))
   105  		return false
   106  	}
   107  
   108  	switch r.Type() {
   109  	case objabi.R_CALLMIPS, objabi.R_JMPMIPS:
   110  		if targType != sym.SDYNIMPORT {
   111  			// Nothing to do, the relocation will be laid out in reloc
   112  			return true
   113  		}
   114  		if target.IsExternal() {
   115  			// External linker will do this relocation.
   116  			return true
   117  		}
   118  
   119  		// Internal linking, build a PLT entry and change the relocation
   120  		// target to that entry.
   121  		if r.Add() != 0 {
   122  			ldr.Errorf(s, "PLT call with non-zero addend (%v)", r.Add())
   123  		}
   124  		addpltsym(target, ldr, syms, targ)
   125  		su := ldr.MakeSymbolUpdater(s)
   126  		su.SetRelocSym(rIdx, syms.PLT)
   127  		su.SetRelocAdd(rIdx, int64(ldr.SymPlt(targ)))
   128  		return true
   129  	}
   130  
   131  	return false
   132  }
   133  
   134  func elfreloc1(ctxt *ld.Link, out *ld.OutBuf, ldr *loader.Loader, s loader.Sym, r loader.ExtReloc, ri int, sectoff int64) bool {
   135  
   136  	// mips64 ELF relocation (endian neutral)
   137  	//		offset	uint64
   138  	//		sym		uint32
   139  	//		ssym	uint8
   140  	//		type3	uint8
   141  	//		type2	uint8
   142  	//		type	uint8
   143  	//		addend	int64
   144  
   145  	out.Write64(uint64(sectoff))
   146  
   147  	elfsym := ld.ElfSymForReloc(ctxt, r.Xsym)
   148  	out.Write32(uint32(elfsym))
   149  	out.Write8(0)
   150  	out.Write8(0)
   151  	out.Write8(0)
   152  	switch r.Type {
   153  	default:
   154  		return false
   155  	case objabi.R_ADDR, objabi.R_DWARFSECREF:
   156  		switch r.Size {
   157  		case 4:
   158  			out.Write8(uint8(elf.R_MIPS_32))
   159  		case 8:
   160  			out.Write8(uint8(elf.R_MIPS_64))
   161  		default:
   162  			return false
   163  		}
   164  	case objabi.R_ADDRMIPS:
   165  		out.Write8(uint8(elf.R_MIPS_LO16))
   166  	case objabi.R_ADDRMIPSU:
   167  		out.Write8(uint8(elf.R_MIPS_HI16))
   168  	case objabi.R_ADDRMIPSTLS:
   169  		out.Write8(uint8(elf.R_MIPS_TLS_TPREL_LO16))
   170  	case objabi.R_CALLMIPS,
   171  		objabi.R_JMPMIPS:
   172  		out.Write8(uint8(elf.R_MIPS_26))
   173  	}
   174  	out.Write64(uint64(r.Xadd))
   175  
   176  	return true
   177  }
   178  
   179  func elfsetupplt(ctxt *ld.Link, ldr *loader.Loader, plt, gotplt *loader.SymbolBuilder, dynamic loader.Sym) {
   180  	if plt.Size() != 0 {
   181  		return
   182  	}
   183  
   184  	// Load resolver address from got[0] into r25.
   185  	plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 0, objabi.R_ADDRMIPSU, 4)
   186  	plt.SetUint32(ctxt.Arch, plt.Size()-4, 0x3c0e0000) // lui   $14, %hi(&GOTPLT[0])
   187  	plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 0, objabi.R_ADDRMIPS, 4)
   188  	plt.SetUint32(ctxt.Arch, plt.Size()-4, 0xddd90000) // ld    $25, %lo(&GOTPLT[0])($14)
   189  
   190  	// Load return address into r15, the index of the got.plt entry into r24, then
   191  	// JALR to the resolver. The address of the got.plt entry is currently in r24,
   192  	// which we have to turn into an index.
   193  	plt.AddSymRef(ctxt.Arch, gotplt.Sym(), 0, objabi.R_ADDRMIPS, 4)
   194  	plt.SetUint32(ctxt.Arch, plt.Size()-4, 0x25ce0000) // addiu $14, $14, %lo(&GOTPLT[0])
   195  	plt.AddUint32(ctxt.Arch, 0x030ec023)               // subu  $24, $24, $14
   196  	plt.AddUint32(ctxt.Arch, 0x03e07825)               // move  $15, $31
   197  	plt.AddUint32(ctxt.Arch, 0x0018c0c2)               // srl   $24, $24, 3
   198  	plt.AddUint32(ctxt.Arch, 0x0320f809)               // jalr  $25
   199  	plt.AddUint32(ctxt.Arch, 0x2718fffe)               // subu  $24, $24, 2
   200  
   201  	if gotplt.Size() != 0 {
   202  		ctxt.Errorf(gotplt.Sym(), "got.plt is not empty")
   203  	}
   204  
   205  	// Reserve got[0] for resolver address (populated by dynamic loader).
   206  	gotplt.AddUint32(ctxt.Arch, 0)
   207  	gotplt.AddUint32(ctxt.Arch, 0)
   208  	gotLocalCount++
   209  
   210  	// Reserve got[1] for ELF object pointer (populated by dynamic loader).
   211  	gotplt.AddUint32(ctxt.Arch, 0)
   212  	gotplt.AddUint32(ctxt.Arch, 0)
   213  	gotLocalCount++
   214  }
   215  
   216  func addpltsym(target *ld.Target, ldr *loader.Loader, syms *ld.ArchSyms, s loader.Sym) {
   217  	if ldr.SymPlt(s) >= 0 {
   218  		return
   219  	}
   220  
   221  	dynamic := ldr.MakeSymbolUpdater(syms.Dynamic)
   222  
   223  	const dynSymEntrySize = 20
   224  	if gotSymIndex == 0 {
   225  		// Compute and update GOT symbol index.
   226  		gotSymIndex = uint64(ldr.SymSize(syms.DynSym) / dynSymEntrySize)
   227  		dynamic.SetUint(target.Arch, dtOffsets[elf.DT_MIPS_GOTSYM], gotSymIndex)
   228  	}
   229  	if dynSymCount == 0 {
   230  		dynSymCount = uint64(ldr.SymSize(syms.DynSym) / dynSymEntrySize)
   231  	}
   232  
   233  	ld.Adddynsym(ldr, target, syms, s)
   234  	dynSymCount++
   235  
   236  	if !target.IsElf() {
   237  		ldr.Errorf(s, "addpltsym: unsupported binary format")
   238  	}
   239  
   240  	plt := ldr.MakeSymbolUpdater(syms.PLT)
   241  	gotplt := ldr.MakeSymbolUpdater(syms.GOTPLT)
   242  	if plt.Size() == 0 {
   243  		panic("plt is not set up")
   244  	}
   245  
   246  	// Load got.plt entry into r25.
   247  	plt.AddSymRef(target.Arch, gotplt.Sym(), gotplt.Size(), objabi.R_ADDRMIPSU, 4)
   248  	plt.SetUint32(target.Arch, plt.Size()-4, 0x3c0f0000) // lui   $15, %hi(.got.plt entry)
   249  	plt.AddSymRef(target.Arch, gotplt.Sym(), gotplt.Size(), objabi.R_ADDRMIPS, 4)
   250  	plt.SetUint32(target.Arch, plt.Size()-4, 0xddf90000) // ld    $25, %lo(.got.plt entry)($15)
   251  
   252  	// Load address of got.plt entry into r24 and JALR to address in r25.
   253  	plt.AddUint32(target.Arch, 0x03200008) // jr  $25
   254  	plt.AddSymRef(target.Arch, gotplt.Sym(), gotplt.Size(), objabi.R_ADDRMIPS, 4)
   255  	plt.SetUint32(target.Arch, plt.Size()-4, 0x65f80000) // daddiu $24, $15, %lo(.got.plt entry)
   256  
   257  	// Add pointer to plt[0] to got.plt
   258  	gotplt.AddAddrPlus(target.Arch, plt.Sym(), 0)
   259  
   260  	ldr.SetPlt(s, int32(plt.Size()-16))
   261  
   262  	// Update dynamic symbol count.
   263  	dynamic.SetUint(target.Arch, dtOffsets[elf.DT_MIPS_SYMTABNO], dynSymCount)
   264  }
   265  
   266  func machoreloc1(*sys.Arch, *ld.OutBuf, *loader.Loader, loader.Sym, loader.ExtReloc, int64) bool {
   267  	return false
   268  }
   269  
   270  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) {
   271  	if target.IsExternal() {
   272  		switch r.Type() {
   273  		default:
   274  			return val, 0, false
   275  
   276  		case objabi.R_ADDRMIPS,
   277  			objabi.R_ADDRMIPSU,
   278  			objabi.R_ADDRMIPSTLS,
   279  			objabi.R_CALLMIPS,
   280  			objabi.R_JMPMIPS:
   281  			return val, 1, true
   282  		}
   283  	}
   284  
   285  	const isOk = true
   286  	const noExtReloc = 0
   287  	rs := r.Sym()
   288  	switch r.Type() {
   289  	case objabi.R_ADDRMIPS,
   290  		objabi.R_ADDRMIPSU:
   291  		t := ldr.SymValue(rs) + r.Add()
   292  		if r.Type() == objabi.R_ADDRMIPS {
   293  			return val&0xffff0000 | t&0xffff, noExtReloc, isOk
   294  		}
   295  		return val&0xffff0000 | ((t+1<<15)>>16)&0xffff, noExtReloc, isOk
   296  	case objabi.R_ADDRMIPSTLS:
   297  		// thread pointer is at 0x7000 offset from the start of TLS data area
   298  		t := ldr.SymValue(rs) + r.Add() - 0x7000
   299  		if t < -32768 || t >= 32678 {
   300  			ldr.Errorf(s, "TLS offset out of range %d", t)
   301  		}
   302  		return val&0xffff0000 | t&0xffff, noExtReloc, isOk
   303  	case objabi.R_CALLMIPS,
   304  		objabi.R_JMPMIPS:
   305  		// Low 26 bits = (S + A) >> 2
   306  		t := ldr.SymValue(rs) + r.Add()
   307  		return val&0xfc000000 | (t>>2)&^0xfc000000, noExtReloc, isOk
   308  	}
   309  
   310  	return val, 0, false
   311  }
   312  
   313  func archrelocvariant(*ld.Target, *loader.Loader, loader.Reloc, sym.RelocVariant, loader.Sym, int64, []byte) int64 {
   314  	return -1
   315  }
   316  
   317  func extreloc(target *ld.Target, ldr *loader.Loader, r loader.Reloc, s loader.Sym) (loader.ExtReloc, bool) {
   318  	switch r.Type() {
   319  	case objabi.R_ADDRMIPS,
   320  		objabi.R_ADDRMIPSU:
   321  		return ld.ExtrelocViaOuterSym(ldr, r, s), true
   322  
   323  	case objabi.R_ADDRMIPSTLS,
   324  		objabi.R_CALLMIPS,
   325  		objabi.R_JMPMIPS:
   326  		return ld.ExtrelocSimple(ldr, r), true
   327  	}
   328  	return loader.ExtReloc{}, false
   329  }
   330  

View as plain text