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