github.com/Filosottile/go@v0.0.0-20170906193555-dbed9972d994/src/cmd/link/internal/mips/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 © 2016 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 mips
    32  
    33  import (
    34  	"cmd/internal/objabi"
    35  	"cmd/link/internal/ld"
    36  	"fmt"
    37  	"log"
    38  )
    39  
    40  func gentext(ctxt *ld.Link) {
    41  	return
    42  }
    43  
    44  func adddynrel(ctxt *ld.Link, s *ld.Symbol, r *ld.Reloc) bool {
    45  	log.Fatalf("adddynrel not implemented")
    46  	return false
    47  }
    48  
    49  func elfreloc1(ctxt *ld.Link, r *ld.Reloc, sectoff int64) bool {
    50  	ld.Thearch.Lput(uint32(sectoff))
    51  
    52  	elfsym := r.Xsym.ElfsymForReloc()
    53  	switch r.Type {
    54  	default:
    55  		return false
    56  	case objabi.R_ADDR:
    57  		if r.Siz != 4 {
    58  			return false
    59  		}
    60  		ld.Thearch.Lput(ld.R_MIPS_32 | uint32(elfsym)<<8)
    61  	case objabi.R_ADDRMIPS:
    62  		ld.Thearch.Lput(ld.R_MIPS_LO16 | uint32(elfsym)<<8)
    63  	case objabi.R_ADDRMIPSU:
    64  		ld.Thearch.Lput(ld.R_MIPS_HI16 | uint32(elfsym)<<8)
    65  	case objabi.R_ADDRMIPSTLS:
    66  		ld.Thearch.Lput(ld.R_MIPS_TLS_TPREL_LO16 | uint32(elfsym)<<8)
    67  	case objabi.R_CALLMIPS, objabi.R_JMPMIPS:
    68  		ld.Thearch.Lput(ld.R_MIPS_26 | uint32(elfsym)<<8)
    69  	}
    70  
    71  	return true
    72  }
    73  
    74  func elfsetupplt(ctxt *ld.Link) {
    75  	return
    76  }
    77  
    78  func machoreloc1(s *ld.Symbol, r *ld.Reloc, sectoff int64) bool {
    79  	return false
    80  }
    81  
    82  func applyrel(r *ld.Reloc, s *ld.Symbol, val *int64, t int64) {
    83  	o := ld.SysArch.ByteOrder.Uint32(s.P[r.Off:])
    84  	switch r.Type {
    85  	case objabi.R_ADDRMIPS, objabi.R_ADDRMIPSTLS:
    86  		*val = int64(o&0xffff0000 | uint32(t)&0xffff)
    87  	case objabi.R_ADDRMIPSU:
    88  		*val = int64(o&0xffff0000 | uint32((t+(1<<15))>>16)&0xffff)
    89  	case objabi.R_CALLMIPS, objabi.R_JMPMIPS:
    90  		*val = int64(o&0xfc000000 | uint32(t>>2)&^0xfc000000)
    91  	}
    92  }
    93  
    94  func archreloc(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, val *int64) bool {
    95  	if ld.Linkmode == ld.LinkExternal {
    96  		switch r.Type {
    97  		default:
    98  			return false
    99  		case objabi.R_ADDRMIPS, objabi.R_ADDRMIPSU:
   100  			r.Done = false
   101  
   102  			// set up addend for eventual relocation via outer symbol.
   103  			rs := r.Sym
   104  			r.Xadd = r.Add
   105  			for rs.Outer != nil {
   106  				r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer)
   107  				rs = rs.Outer
   108  			}
   109  
   110  			if rs.Type != ld.SHOSTOBJ && rs.Type != ld.SDYNIMPORT && rs.Sect == nil {
   111  				ld.Errorf(s, "missing section for %s", rs.Name)
   112  			}
   113  			r.Xsym = rs
   114  			applyrel(r, s, val, r.Xadd)
   115  			return true
   116  		case objabi.R_ADDRMIPSTLS, objabi.R_CALLMIPS, objabi.R_JMPMIPS:
   117  			r.Done = false
   118  			r.Xsym = r.Sym
   119  			r.Xadd = r.Add
   120  			applyrel(r, s, val, r.Add)
   121  			return true
   122  		}
   123  	}
   124  
   125  	switch r.Type {
   126  	case objabi.R_CONST:
   127  		*val = r.Add
   128  		return true
   129  	case objabi.R_GOTOFF:
   130  		*val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ctxt.Syms.Lookup(".got", 0))
   131  		return true
   132  	case objabi.R_ADDRMIPS, objabi.R_ADDRMIPSU:
   133  		t := ld.Symaddr(r.Sym) + r.Add
   134  		applyrel(r, s, val, t)
   135  		return true
   136  	case objabi.R_CALLMIPS, objabi.R_JMPMIPS:
   137  		t := ld.Symaddr(r.Sym) + r.Add
   138  
   139  		if t&3 != 0 {
   140  			ld.Errorf(s, "direct call is not aligned: %s %x", r.Sym.Name, t)
   141  		}
   142  
   143  		// check if target address is in the same 256 MB region as the next instruction
   144  		if (s.Value+int64(r.Off)+4)&0xf0000000 != (t & 0xf0000000) {
   145  			ld.Errorf(s, "direct call too far: %s %x", r.Sym.Name, t)
   146  		}
   147  
   148  		applyrel(r, s, val, t)
   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  		applyrel(r, s, val, t)
   157  		return true
   158  	}
   159  
   160  	return false
   161  }
   162  
   163  func archrelocvariant(ctxt *ld.Link, r *ld.Reloc, s *ld.Symbol, t int64) int64 {
   164  	return -1
   165  }
   166  
   167  func asmb(ctxt *ld.Link) {
   168  	if ctxt.Debugvlog != 0 {
   169  		ctxt.Logf("%5.2f asmb\n", ld.Cputime())
   170  	}
   171  
   172  	if ld.Iself {
   173  		ld.Asmbelfsetup()
   174  	}
   175  
   176  	sect := ld.Segtext.Sections[0]
   177  	ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   178  	ld.Codeblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   179  	for _, sect = range ld.Segtext.Sections[1:] {
   180  		ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
   181  		ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length))
   182  	}
   183  
   184  	if ld.Segrodata.Filelen > 0 {
   185  		if ctxt.Debugvlog != 0 {
   186  			ctxt.Logf("%5.2f rodatblk\n", ld.Cputime())
   187  		}
   188  
   189  		ld.Cseek(int64(ld.Segrodata.Fileoff))
   190  		ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
   191  	}
   192  
   193  	if ctxt.Debugvlog != 0 {
   194  		ctxt.Logf("%5.2f datblk\n", ld.Cputime())
   195  	}
   196  
   197  	ld.Cseek(int64(ld.Segdata.Fileoff))
   198  	ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
   199  
   200  	ld.Cseek(int64(ld.Segdwarf.Fileoff))
   201  	ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen))
   202  
   203  	/* output symbol table */
   204  	ld.Symsize = 0
   205  
   206  	ld.Lcsize = 0
   207  	symo := uint32(0)
   208  	if !*ld.FlagS {
   209  		if !ld.Iself {
   210  			ld.Errorf(nil, "unsupported executable format")
   211  		}
   212  		if ctxt.Debugvlog != 0 {
   213  			ctxt.Logf("%5.2f sym\n", ld.Cputime())
   214  		}
   215  		symo = uint32(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
   216  		symo = uint32(ld.Rnd(int64(symo), int64(*ld.FlagRound)))
   217  
   218  		ld.Cseek(int64(symo))
   219  		if ctxt.Debugvlog != 0 {
   220  			ctxt.Logf("%5.2f elfsym\n", ld.Cputime())
   221  		}
   222  		ld.Asmelfsym(ctxt)
   223  		ld.Cflush()
   224  		ld.Cwrite(ld.Elfstrdat)
   225  
   226  		if ctxt.Debugvlog != 0 {
   227  			ctxt.Logf("%5.2f dwarf\n", ld.Cputime())
   228  		}
   229  
   230  		if ld.Linkmode == ld.LinkExternal {
   231  			ld.Elfemitreloc(ctxt)
   232  		}
   233  	}
   234  
   235  	if ctxt.Debugvlog != 0 {
   236  		ctxt.Logf("%5.2f header\n", ld.Cputime())
   237  	}
   238  
   239  	ld.Cseek(0)
   240  	switch ld.Headtype {
   241  	default:
   242  		ld.Errorf(nil, "unsupported operating system")
   243  	case objabi.Hlinux:
   244  		ld.Asmbelf(ctxt, int64(symo))
   245  	}
   246  
   247  	ld.Cflush()
   248  	if *ld.FlagC {
   249  		fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
   250  		fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)
   251  		fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen)
   252  		fmt.Printf("symsize=%d\n", ld.Symsize)
   253  		fmt.Printf("lcsize=%d\n", ld.Lcsize)
   254  		fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize))
   255  	}
   256  }