github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/obj/data.go (about)

     1  // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
     2  // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/6l/obj.c
     3  // https://bitbucket.org/inferno-os/inferno-os/src/master/utils/6l/span.c
     4  //
     5  //	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
     6  //	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
     7  //	Portions Copyright © 1997-1999 Vita Nuova Limited
     8  //	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
     9  //	Portions Copyright © 2004,2006 Bruce Ellis
    10  //	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
    11  //	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
    12  //	Portions Copyright © 2009 The Go Authors. All rights reserved.
    13  //
    14  // Permission is hereby granted, free of charge, to any person obtaining a copy
    15  // of this software and associated documentation files (the "Software"), to deal
    16  // in the Software without restriction, including without limitation the rights
    17  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    18  // copies of the Software, and to permit persons to whom the Software is
    19  // furnished to do so, subject to the following conditions:
    20  //
    21  // The above copyright notice and this permission notice shall be included in
    22  // all copies or substantial portions of the Software.
    23  //
    24  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    25  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    26  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    27  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    28  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    29  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    30  // THE SOFTWARE.
    31  
    32  package obj
    33  
    34  import (
    35  	"log"
    36  	"math"
    37  
    38  	"github.com/go-asm/go/cmd/objabi"
    39  )
    40  
    41  // Grow increases the length of s.P to lsiz.
    42  func (s *LSym) Grow(lsiz int64) {
    43  	siz := int(lsiz)
    44  	if int64(siz) != lsiz {
    45  		log.Fatalf("LSym.Grow size %d too long", lsiz)
    46  	}
    47  	if len(s.P) >= siz {
    48  		return
    49  	}
    50  	s.P = append(s.P, make([]byte, siz-len(s.P))...)
    51  }
    52  
    53  // GrowCap increases the capacity of s.P to c.
    54  func (s *LSym) GrowCap(c int64) {
    55  	if int64(cap(s.P)) >= c {
    56  		return
    57  	}
    58  	if s.P == nil {
    59  		s.P = make([]byte, 0, c)
    60  		return
    61  	}
    62  	b := make([]byte, len(s.P), c)
    63  	copy(b, s.P)
    64  	s.P = b
    65  }
    66  
    67  // prepwrite prepares to write data of size siz into s at offset off.
    68  func (s *LSym) prepwrite(ctxt *Link, off int64, siz int) {
    69  	if off < 0 || siz < 0 || off >= 1<<30 {
    70  		ctxt.Diag("prepwrite: bad off=%d siz=%d s=%v", off, siz, s)
    71  	}
    72  	switch s.Type {
    73  	case objabi.Sxxx, objabi.SBSS:
    74  		s.Type = objabi.SDATA
    75  	case objabi.SNOPTRBSS:
    76  		s.Type = objabi.SNOPTRDATA
    77  	case objabi.STLSBSS:
    78  		ctxt.Diag("cannot supply data for %v var %v", s.Type, s.Name)
    79  	}
    80  	l := off + int64(siz)
    81  	s.Grow(l)
    82  	if l > s.Size {
    83  		s.Size = l
    84  	}
    85  }
    86  
    87  // WriteFloat32 writes f into s at offset off.
    88  func (s *LSym) WriteFloat32(ctxt *Link, off int64, f float32) {
    89  	s.prepwrite(ctxt, off, 4)
    90  	ctxt.Arch.ByteOrder.PutUint32(s.P[off:], math.Float32bits(f))
    91  }
    92  
    93  // WriteFloat64 writes f into s at offset off.
    94  func (s *LSym) WriteFloat64(ctxt *Link, off int64, f float64) {
    95  	s.prepwrite(ctxt, off, 8)
    96  	ctxt.Arch.ByteOrder.PutUint64(s.P[off:], math.Float64bits(f))
    97  }
    98  
    99  // WriteInt writes an integer i of size siz into s at offset off.
   100  func (s *LSym) WriteInt(ctxt *Link, off int64, siz int, i int64) {
   101  	s.prepwrite(ctxt, off, siz)
   102  	switch siz {
   103  	default:
   104  		ctxt.Diag("WriteInt: bad integer size: %d", siz)
   105  	case 1:
   106  		s.P[off] = byte(i)
   107  	case 2:
   108  		ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(i))
   109  	case 4:
   110  		ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(i))
   111  	case 8:
   112  		ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(i))
   113  	}
   114  }
   115  
   116  func (s *LSym) writeAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64, rtype objabi.RelocType) {
   117  	// Allow 4-byte addresses for DWARF.
   118  	if siz != ctxt.Arch.PtrSize && siz != 4 {
   119  		ctxt.Diag("WriteAddr: bad address size %d in %s", siz, s.Name)
   120  	}
   121  	s.prepwrite(ctxt, off, siz)
   122  	r := Addrel(s)
   123  	r.Off = int32(off)
   124  	if int64(r.Off) != off {
   125  		ctxt.Diag("WriteAddr: off overflow %d in %s", off, s.Name)
   126  	}
   127  	r.Siz = uint8(siz)
   128  	r.Sym = rsym
   129  	r.Type = rtype
   130  	r.Add = roff
   131  }
   132  
   133  // WriteAddr writes an address of size siz into s at offset off.
   134  // rsym and roff specify the relocation for the address.
   135  func (s *LSym) WriteAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64) {
   136  	s.writeAddr(ctxt, off, siz, rsym, roff, objabi.R_ADDR)
   137  }
   138  
   139  // WriteWeakAddr writes an address of size siz into s at offset off.
   140  // rsym and roff specify the relocation for the address.
   141  // This is a weak reference.
   142  func (s *LSym) WriteWeakAddr(ctxt *Link, off int64, siz int, rsym *LSym, roff int64) {
   143  	s.writeAddr(ctxt, off, siz, rsym, roff, objabi.R_WEAKADDR)
   144  }
   145  
   146  // WriteCURelativeAddr writes a pointer-sized address into s at offset off.
   147  // rsym and roff specify the relocation for the address which will be
   148  // resolved by the linker to an offset from the DW_AT_low_pc attribute of
   149  // the DWARF Compile Unit of rsym.
   150  func (s *LSym) WriteCURelativeAddr(ctxt *Link, off int64, rsym *LSym, roff int64) {
   151  	s.writeAddr(ctxt, off, ctxt.Arch.PtrSize, rsym, roff, objabi.R_ADDRCUOFF)
   152  }
   153  
   154  // WriteOff writes a 4 byte offset to rsym+roff into s at offset off.
   155  // After linking the 4 bytes stored at s+off will be
   156  // rsym+roff-(start of section that s is in).
   157  func (s *LSym) WriteOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
   158  	s.prepwrite(ctxt, off, 4)
   159  	r := Addrel(s)
   160  	r.Off = int32(off)
   161  	if int64(r.Off) != off {
   162  		ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
   163  	}
   164  	r.Siz = 4
   165  	r.Sym = rsym
   166  	r.Type = objabi.R_ADDROFF
   167  	r.Add = roff
   168  }
   169  
   170  // WriteWeakOff writes a weak 4 byte offset to rsym+roff into s at offset off.
   171  // After linking the 4 bytes stored at s+off will be
   172  // rsym+roff-(start of section that s is in).
   173  func (s *LSym) WriteWeakOff(ctxt *Link, off int64, rsym *LSym, roff int64) {
   174  	s.prepwrite(ctxt, off, 4)
   175  	r := Addrel(s)
   176  	r.Off = int32(off)
   177  	if int64(r.Off) != off {
   178  		ctxt.Diag("WriteOff: off overflow %d in %s", off, s.Name)
   179  	}
   180  	r.Siz = 4
   181  	r.Sym = rsym
   182  	r.Type = objabi.R_WEAKADDROFF
   183  	r.Add = roff
   184  }
   185  
   186  // WriteString writes a string of size siz into s at offset off.
   187  func (s *LSym) WriteString(ctxt *Link, off int64, siz int, str string) {
   188  	if siz < len(str) {
   189  		ctxt.Diag("WriteString: bad string size: %d < %d", siz, len(str))
   190  	}
   191  	s.prepwrite(ctxt, off, siz)
   192  	copy(s.P[off:off+int64(siz)], str)
   193  }
   194  
   195  // WriteBytes writes a slice of bytes into s at offset off.
   196  func (s *LSym) WriteBytes(ctxt *Link, off int64, b []byte) int64 {
   197  	s.prepwrite(ctxt, off, len(b))
   198  	copy(s.P[off:], b)
   199  	return off + int64(len(b))
   200  }
   201  
   202  func Addrel(s *LSym) *Reloc {
   203  	if s.R == nil {
   204  		s.R = make([]Reloc, 0, 4)
   205  	}
   206  	s.R = append(s.R, Reloc{})
   207  	return &s.R[len(s.R)-1]
   208  }