github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/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 "github.com/gagliardetto/golang-go/cmd/internal/objabi" 35 "github.com/gagliardetto/golang-go/cmd/internal/sys" 36 "github.com/gagliardetto/golang-go/cmd/link/internal/ld" 37 "github.com/gagliardetto/golang-go/cmd/link/internal/sym" 38 "debug/elf" 39 "fmt" 40 "log" 41 ) 42 43 func gentext(ctxt *ld.Link) { 44 return 45 } 46 47 func adddynrel(ctxt *ld.Link, s *sym.Symbol, r *sym.Reloc) bool { 48 log.Fatalf("adddynrel not implemented") 49 return false 50 } 51 52 func elfreloc1(ctxt *ld.Link, r *sym.Reloc, sectoff int64) bool { 53 ctxt.Out.Write32(uint32(sectoff)) 54 55 elfsym := r.Xsym.ElfsymForReloc() 56 switch r.Type { 57 default: 58 return false 59 case objabi.R_ADDR: 60 if r.Siz != 4 { 61 return false 62 } 63 ctxt.Out.Write32(uint32(elf.R_MIPS_32) | uint32(elfsym)<<8) 64 case objabi.R_ADDRMIPS: 65 ctxt.Out.Write32(uint32(elf.R_MIPS_LO16) | uint32(elfsym)<<8) 66 case objabi.R_ADDRMIPSU: 67 ctxt.Out.Write32(uint32(elf.R_MIPS_HI16) | uint32(elfsym)<<8) 68 case objabi.R_ADDRMIPSTLS: 69 ctxt.Out.Write32(uint32(elf.R_MIPS_TLS_TPREL_LO16) | uint32(elfsym)<<8) 70 case objabi.R_CALLMIPS, objabi.R_JMPMIPS: 71 ctxt.Out.Write32(uint32(elf.R_MIPS_26) | uint32(elfsym)<<8) 72 } 73 74 return true 75 } 76 77 func elfsetupplt(ctxt *ld.Link) { 78 return 79 } 80 81 func machoreloc1(arch *sys.Arch, out *ld.OutBuf, s *sym.Symbol, r *sym.Reloc, sectoff int64) bool { 82 return false 83 } 84 85 func applyrel(arch *sys.Arch, r *sym.Reloc, s *sym.Symbol, val int64, t int64) int64 { 86 o := arch.ByteOrder.Uint32(s.P[r.Off:]) 87 switch r.Type { 88 case objabi.R_ADDRMIPS, objabi.R_ADDRMIPSTLS: 89 return int64(o&0xffff0000 | uint32(t)&0xffff) 90 case objabi.R_ADDRMIPSU: 91 return int64(o&0xffff0000 | uint32((t+(1<<15))>>16)&0xffff) 92 case objabi.R_CALLMIPS, objabi.R_JMPMIPS: 93 return int64(o&0xfc000000 | uint32(t>>2)&^0xfc000000) 94 default: 95 return val 96 } 97 } 98 99 func archreloc(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, val int64) (int64, bool) { 100 if ctxt.LinkMode == ld.LinkExternal { 101 switch r.Type { 102 default: 103 return val, false 104 case objabi.R_ADDRMIPS, objabi.R_ADDRMIPSU: 105 r.Done = false 106 107 // set up addend for eventual relocation via outer symbol. 108 rs := r.Sym 109 r.Xadd = r.Add 110 for rs.Outer != nil { 111 r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer) 112 rs = rs.Outer 113 } 114 115 if rs.Type != sym.SHOSTOBJ && rs.Type != sym.SDYNIMPORT && rs.Sect == nil { 116 ld.Errorf(s, "missing section for %s", rs.Name) 117 } 118 r.Xsym = rs 119 return applyrel(ctxt.Arch, r, s, val, r.Xadd), true 120 case objabi.R_ADDRMIPSTLS, objabi.R_CALLMIPS, objabi.R_JMPMIPS: 121 r.Done = false 122 r.Xsym = r.Sym 123 r.Xadd = r.Add 124 return applyrel(ctxt.Arch, r, s, val, r.Add), true 125 } 126 } 127 128 switch r.Type { 129 case objabi.R_CONST: 130 return r.Add, true 131 case objabi.R_GOTOFF: 132 return ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ctxt.Syms.Lookup(".got", 0)), true 133 case objabi.R_ADDRMIPS, objabi.R_ADDRMIPSU: 134 t := ld.Symaddr(r.Sym) + r.Add 135 return applyrel(ctxt.Arch, r, s, val, t), 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 return applyrel(ctxt.Arch, r, s, val, t), true 149 case objabi.R_ADDRMIPSTLS: 150 // thread pointer is at 0x7000 offset from the start of TLS data area 151 t := ld.Symaddr(r.Sym) + r.Add - 0x7000 152 if t < -32768 || t >= 32678 { 153 ld.Errorf(s, "TLS offset out of range %d", t) 154 } 155 return applyrel(ctxt.Arch, r, s, val, t), true 156 } 157 158 return val, false 159 } 160 161 func archrelocvariant(ctxt *ld.Link, r *sym.Reloc, s *sym.Symbol, t int64) int64 { 162 return -1 163 } 164 165 func asmb(ctxt *ld.Link) { 166 if ctxt.IsELF { 167 ld.Asmbelfsetup() 168 } 169 170 sect := ld.Segtext.Sections[0] 171 ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff)) 172 ld.Codeblk(ctxt, int64(sect.Vaddr), int64(sect.Length)) 173 for _, sect = range ld.Segtext.Sections[1:] { 174 ctxt.Out.SeekSet(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff)) 175 ld.Datblk(ctxt, int64(sect.Vaddr), int64(sect.Length)) 176 } 177 178 if ld.Segrodata.Filelen > 0 { 179 ctxt.Out.SeekSet(int64(ld.Segrodata.Fileoff)) 180 ld.Datblk(ctxt, int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen)) 181 } 182 183 ctxt.Out.SeekSet(int64(ld.Segdata.Fileoff)) 184 ld.Datblk(ctxt, int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen)) 185 186 ctxt.Out.SeekSet(int64(ld.Segdwarf.Fileoff)) 187 ld.Dwarfblk(ctxt, int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen)) 188 } 189 190 func asmb2(ctxt *ld.Link) { 191 /* output symbol table */ 192 ld.Symsize = 0 193 194 ld.Lcsize = 0 195 symo := uint32(0) 196 if !*ld.FlagS { 197 if !ctxt.IsELF { 198 ld.Errorf(nil, "unsupported executable format") 199 } 200 symo = uint32(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen) 201 symo = uint32(ld.Rnd(int64(symo), int64(*ld.FlagRound))) 202 203 ctxt.Out.SeekSet(int64(symo)) 204 ld.Asmelfsym(ctxt) 205 ctxt.Out.Flush() 206 ctxt.Out.Write(ld.Elfstrdat) 207 208 if ctxt.LinkMode == ld.LinkExternal { 209 ld.Elfemitreloc(ctxt) 210 } 211 } 212 213 ctxt.Out.SeekSet(0) 214 switch ctxt.HeadType { 215 default: 216 ld.Errorf(nil, "unsupported operating system") 217 case objabi.Hlinux: 218 ld.Asmbelf(ctxt, int64(symo)) 219 } 220 221 ctxt.Out.Flush() 222 if *ld.FlagC { 223 fmt.Printf("textsize=%d\n", ld.Segtext.Filelen) 224 fmt.Printf("datsize=%d\n", ld.Segdata.Filelen) 225 fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen) 226 fmt.Printf("symsize=%d\n", ld.Symsize) 227 fmt.Printf("lcsize=%d\n", ld.Lcsize) 228 fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize)) 229 } 230 }