github.com/mattn/go@v0.0.0-20171011075504-07f7db3ea99f/src/cmd/link/internal/mips64/asm.go (about)

     1  // Inferno utils/5l/asm.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/default/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/sym"
    38  	"fmt"
    39  	"log"
    40  )
    41  
    42  func gentext(ctxt *ld.Link) {}
    43  
    44  func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool {
    45  	log.Fatalf("adddynrel not implemented")
    46  	return false
    47  }
    48  
    49  func elfreloc1(ctxt *ld.Link, r *sym.Reloc, sectoff int64) bool {
    50  	// mips64 ELF relocation (endian neutral)
    51  	//		offset	uint64
    52  	//		sym		uint32
    53  	//		ssym	uint8
    54  	//		type3	uint8
    55  	//		type2	uint8
    56  	//		type	uint8
    57  	//		addend	int64
    58  
    59  	ctxt.Out.Write64(uint64(sectoff))
    60  
    61  	elfsym := r.Xsym.ElfsymForReloc()
    62  	ctxt.Out.Write32(uint32(elfsym))
    63  	ctxt.Out.Write8(0)
    64  	ctxt.Out.Write8(0)
    65  	ctxt.Out.Write8(0)
    66  	switch r.Type {
    67  	default:
    68  		return false
    69  	case objabi.R_ADDR:
    70  		switch r.Siz {
    71  		case 4:
    72  			ctxt.Out.Write8(ld.R_MIPS_32)
    73  		case 8:
    74  			ctxt.Out.Write8(ld.R_MIPS_64)
    75  		default:
    76  			return false
    77  		}
    78  	case objabi.R_ADDRMIPS:
    79  		ctxt.Out.Write8(ld.R_MIPS_LO16)
    80  	case objabi.R_ADDRMIPSU:
    81  		ctxt.Out.Write8(ld.R_MIPS_HI16)
    82  	case objabi.R_ADDRMIPSTLS:
    83  		ctxt.Out.Write8(ld.R_MIPS_TLS_TPREL_LO16)
    84  	case objabi.R_CALLMIPS,
    85  		objabi.R_JMPMIPS:
    86  		ctxt.Out.Write8(ld.R_MIPS_26)
    87  	}
    88  	ctxt.Out.Write64(uint64(r.Xadd))
    89  
    90  	return true
    91  }
    92  
    93  func elfsetupplt(ctxt *ld.Link) {
    94  	return
    95  }
    96  
    97  func machoreloc1(arch *sys.Arch, out *ld.OutBuf, s *sym.Symbol, r *sym.Reloc, sectoff int64) bool {
    98  	return false
    99  }
   100  
   101  func archreloc(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, val *int64) bool {
   102  	if ctxt.LinkMode == ld.LinkExternal {
   103  		switch r.Type {
   104  		default:
   105  			return false
   106  		case objabi.R_ADDRMIPS,
   107  			objabi.R_ADDRMIPSU:
   108  			r.Done = false
   109  
   110  			// set up addend for eventual relocation via outer symbol.
   111  			rs := r.Sym
   112  			r.Xadd = r.Add
   113  			for rs.Outer != nil {
   114  				r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer)
   115  				rs = rs.Outer
   116  			}
   117  
   118  			if rs.Type != sym.SHOSTOBJ && rs.Type != sym.SDYNIMPORT && rs.Sect == nil {
   119  				ld.Errorf(s, "missing section for %s", rs.Name)
   120  			}
   121  			r.Xsym = rs
   122  
   123  			return true
   124  		case objabi.R_ADDRMIPSTLS,
   125  			objabi.R_CALLMIPS,
   126  			objabi.R_JMPMIPS:
   127  			r.Done = false
   128  			r.Xsym = r.Sym
   129  			r.Xadd = r.Add
   130  			return true
   131  		}
   132  	}
   133  
   134  	switch r.Type {
   135  	case objabi.R_CONST:
   136  		*val = r.Add
   137  		return true
   138  	case objabi.R_GOTOFF:
   139  		*val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ctxt.Syms.Lookup(".got", 0))
   140  		return true
   141  	case objabi.R_ADDRMIPS,
   142  		objabi.R_ADDRMIPSU:
   143  		t := ld.Symaddr(r.Sym) + r.Add
   144  		o1 := ctxt.Arch.ByteOrder.Uint32(s.P[r.Off:])
   145  		if r.Type == objabi.R_ADDRMIPS {
   146  			*val = int64(o1&0xffff0000 | uint32(t)&0xffff)
   147  		} else {
   148  			*val = int64(o1&0xffff0000 | uint32((t+1<<15)>>16)&0xffff)
   149  		}
   150  		return true
   151  	case objabi.R_ADDRMIPSTLS:
   152  		// thread pointer is at 0x7000 offset from the start of TLS data area
   153  		t := ld.Symaddr(r.Sym) + r.Add - 0x7000
   154  		if t < -32768 || t >= 32678 {
   155  			ld.Errorf(s, "TLS offset out of range %d", t)
   156  		}
   157  		o1 := ctxt.Arch.ByteOrder.Uint32(s.P[r.Off:])
   158  		*val = int64(o1&0xffff0000 | uint32(t)&0xffff)
   159  		return true
   160  	case objabi.R_CALLMIPS,
   161  		objabi.R_JMPMIPS:
   162  		// Low 26 bits = (S + A) >> 2
   163  		t := ld.Symaddr(r.Sym) + r.Add
   164  		o1 := ctxt.Arch.ByteOrder.Uint32(s.P[r.Off:])
   165  		*val = int64(o1&0xfc000000 | uint32(t>>2)&^0xfc000000)
   166  		return true
   167  	}
   168  
   169  	return false
   170  }
   171  
   172  func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64 {
   173  	return -1
   174  }
   175  
   176  func asmb(ctxt *ld.Link) {
   177  	if ctxt.Debugvlog != 0 {
   178  		ctxt.Logf("%5.2f asmb\n", ld.Cputime())
   179  	}
   180  
   181  	if ld.Iself {
   182  		ld.Asmbelfsetup()
   183  	}
   184  
   185  	sect := ld.Segtext.Sections[0]
   186  	ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   187  	ld.Codeblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   188  	for _, sect = range ld.Segtext.Sections[1:] {
   189  		ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   190  		ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   191  	}
   192  
   193  	if ld.Segrodata.Filelen > 0 {
   194  		if ctxt.Debugvlog != 0 {
   195  			ctxt.Logf("%5.2f rodatblk\n", ld.Cputime())
   196  		}
   197  		ctxt.Out.SeekSet(int64(ld.Segrodata.Fileoff))
   198  		ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
   199  	}
   200  	if ld.Segrelrodata.Filelen > 0 {
   201  		if ctxt.Debugvlog != 0 {
   202  			ctxt.Logf("%5.2f rodatblk\n", ld.Cputime())
   203  		}
   204  		ctxt.Out.SeekSet(int64(ld.Segrelrodata.Fileoff))
   205  		ld.Datblk(ctxt, int64(ld.Segrelrodata.Vaddr), int64(ld.Segrelrodata.Filelen))
   206  	}
   207  
   208  	if ctxt.Debugvlog != 0 {
   209  		ctxt.Logf("%5.2f datblk\n", ld.Cputime())
   210  	}
   211  
   212  	ctxt.Out.SeekSet(int64(ld.Segdata.Fileoff))
   213  	ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
   214  
   215  	ctxt.Out.SeekSet(int64(ld.Segdwarf.Fileoff))
   216  	ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen))
   217  
   218  	/* output symbol table */
   219  	ld.Symsize = 0
   220  
   221  	ld.Lcsize = 0
   222  	symo := uint32(0)
   223  	if !*ld.FlagS {
   224  		// TODO: rationalize
   225  		if ctxt.Debugvlog != 0 {
   226  			ctxt.Logf("%5.2f sym\n", ld.Cputime())
   227  		}
   228  		switch ld.Headtype {
   229  		default:
   230  			if ld.Iself {
   231  				symo = uint32(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
   232  				symo = uint32(ld.Rnd(int64(symo), int64(*ld.FlagRound)))
   233  			}
   234  
   235  		case objabi.Hplan9:
   236  			symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen)
   237  		}
   238  
   239  		ctxt.Out.SeekSet(int64(symo))
   240  		switch ld.Headtype {
   241  		default:
   242  			if ld.Iself {
   243  				if ctxt.Debugvlog != 0 {
   244  					ctxt.Logf("%5.2f elfsym\n", ld.Cputime())
   245  				}
   246  				ld.Asmelfsym(ctxt)
   247  				ctxt.Out.Flush()
   248  				ctxt.Out.Write(ld.Elfstrdat)
   249  
   250  				if ctxt.LinkMode == ld.LinkExternal {
   251  					ld.Elfemitreloc(ctxt)
   252  				}
   253  			}
   254  
   255  		case objabi.Hplan9:
   256  			ld.Asmplan9sym(ctxt)
   257  			ctxt.Out.Flush()
   258  
   259  			sym := ctxt.Syms.Lookup("pclntab", 0)
   260  			if sym != nil {
   261  				ld.Lcsize = int32(len(sym.P))
   262  				ctxt.Out.Write(sym.P)
   263  				ctxt.Out.Flush()
   264  			}
   265  		}
   266  	}
   267  
   268  	if ctxt.Debugvlog != 0 {
   269  		ctxt.Logf("%5.2f header\n", ld.Cputime())
   270  	}
   271  	ctxt.Out.SeekSet(0)
   272  	switch ld.Headtype {
   273  	default:
   274  	case objabi.Hplan9: /* plan 9 */
   275  		magic := uint32(4*18*18 + 7)
   276  		if ctxt.Arch == sys.ArchMIPS64LE {
   277  			magic = uint32(4*26*26 + 7)
   278  		}
   279  		ctxt.Out.Write32(magic)                      /* magic */
   280  		ctxt.Out.Write32(uint32(ld.Segtext.Filelen)) /* sizes */
   281  		ctxt.Out.Write32(uint32(ld.Segdata.Filelen))
   282  		ctxt.Out.Write32(uint32(ld.Segdata.Length - ld.Segdata.Filelen))
   283  		ctxt.Out.Write32(uint32(ld.Symsize))          /* nsyms */
   284  		ctxt.Out.Write32(uint32(ld.Entryvalue(ctxt))) /* va of entry */
   285  		ctxt.Out.Write32(0)
   286  		ctxt.Out.Write32(uint32(ld.Lcsize))
   287  
   288  	case objabi.Hlinux,
   289  		objabi.Hfreebsd,
   290  		objabi.Hnetbsd,
   291  		objabi.Hopenbsd,
   292  		objabi.Hnacl:
   293  		ld.Asmbelf(ctxt, int64(symo))
   294  	}
   295  
   296  	ctxt.Out.Flush()
   297  	if *ld.FlagC {
   298  		fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
   299  		fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)
   300  		fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen)
   301  		fmt.Printf("symsize=%d\n", ld.Symsize)
   302  		fmt.Printf("lcsize=%d\n", ld.Lcsize)
   303  		fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize))
   304  	}
   305  }