github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/cmd/internal/obj/data.go (about)

     1  // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
     2  // http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c
     3  // http://code.google.com/p/inferno-os/source/browse/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  
    39  func Symgrow(ctxt *Link, s *LSym, lsiz int64) {
    40  	siz := int(lsiz)
    41  	if int64(siz) != lsiz {
    42  		log.Fatalf("Symgrow size %d too long", lsiz)
    43  	}
    44  	if len(s.P) >= siz {
    45  		return
    46  	}
    47  	// TODO(dfc) append cap-len at once, rather than
    48  	// one byte at a time.
    49  	for cap(s.P) < siz {
    50  		s.P = append(s.P[:cap(s.P)], 0)
    51  	}
    52  	s.P = s.P[:siz]
    53  }
    54  
    55  func savedata(ctxt *Link, s *LSym, p *Prog, file string) {
    56  	off := int32(p.From.Offset)
    57  	siz := int32(p.From3.Offset)
    58  	if off < 0 || siz < 0 || off >= 1<<30 || siz >= 100 {
    59  		log.Fatalf("%s: mangled input file", file)
    60  	}
    61  	if ctxt.Enforce_data_order != 0 && off < int32(len(s.P)) {
    62  		ctxt.Diag("data out of order (already have %d)\n%v", len(s.P), p)
    63  	}
    64  	if s.Type == SBSS || s.Type == STLSBSS {
    65  		ctxt.Diag("cannot supply data for BSS var")
    66  	}
    67  	Symgrow(ctxt, s, int64(off+siz))
    68  
    69  	switch int(p.To.Type) {
    70  	default:
    71  		ctxt.Diag("bad data: %v", p)
    72  
    73  	case TYPE_FCONST:
    74  		switch siz {
    75  		default:
    76  			ctxt.Diag("unexpected %d-byte floating point constant", siz)
    77  
    78  		case 4:
    79  			flt := math.Float32bits(float32(p.To.Val.(float64)))
    80  			ctxt.Arch.ByteOrder.PutUint32(s.P[off:], flt)
    81  
    82  		case 8:
    83  			flt := math.Float64bits(p.To.Val.(float64))
    84  			ctxt.Arch.ByteOrder.PutUint64(s.P[off:], flt)
    85  		}
    86  
    87  	case TYPE_SCONST:
    88  		copy(s.P[off:off+siz], p.To.Val.(string))
    89  
    90  	case TYPE_CONST, TYPE_ADDR:
    91  		if p.To.Sym != nil || int(p.To.Type) == TYPE_ADDR {
    92  			r := Addrel(s)
    93  			r.Off = off
    94  			r.Siz = uint8(siz)
    95  			r.Sym = p.To.Sym
    96  			r.Type = R_ADDR
    97  			r.Add = p.To.Offset
    98  			break
    99  		}
   100  		o := p.To.Offset
   101  		switch siz {
   102  		default:
   103  			ctxt.Diag("unexpected %d-byte integer constant", siz)
   104  		case 1:
   105  			s.P[off] = byte(o)
   106  		case 2:
   107  			ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(o))
   108  		case 4:
   109  			ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(o))
   110  		case 8:
   111  			ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(o))
   112  		}
   113  	}
   114  }
   115  
   116  func Addrel(s *LSym) *Reloc {
   117  	s.R = append(s.R, Reloc{})
   118  	return &s.R[len(s.R)-1]
   119  }
   120  
   121  func Setuintxx(ctxt *Link, s *LSym, off int64, v uint64, wid int64) int64 {
   122  	if s.Type == 0 {
   123  		s.Type = SDATA
   124  	}
   125  	if s.Size < off+wid {
   126  		s.Size = off + wid
   127  		Symgrow(ctxt, s, s.Size)
   128  	}
   129  
   130  	switch wid {
   131  	case 1:
   132  		s.P[off] = uint8(v)
   133  	case 2:
   134  		ctxt.Arch.ByteOrder.PutUint16(s.P[off:], uint16(v))
   135  	case 4:
   136  		ctxt.Arch.ByteOrder.PutUint32(s.P[off:], uint32(v))
   137  	case 8:
   138  		ctxt.Arch.ByteOrder.PutUint64(s.P[off:], uint64(v))
   139  	}
   140  
   141  	return off + wid
   142  }