Home | History | Annotate | Download | only in obj
      1 // Derived from Inferno utils/6l/obj.c and utils/6l/span.c
      2 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/obj.c
      3 // https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/span.c
      4 //
      5 //	Copyright  1994-1999 Lucent Technologies Inc.  All rights reserved.
      6 //	Portions Copyright  1995-1997 C H Forsyth (forsyth (a] 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 (a] 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 	"cmd/internal/objabi"
     36 	"fmt"
     37 	"log"
     38 	"math"
     39 )
     40 
     41 func Linknew(arch *LinkArch) *Link {
     42 	ctxt := new(Link)
     43 	ctxt.hash = make(map[string]*LSym)
     44 	ctxt.statichash = make(map[string]*LSym)
     45 	ctxt.Arch = arch
     46 	ctxt.Pathname = objabi.WorkingDir()
     47 
     48 	if err := ctxt.Headtype.Set(objabi.GOOS); err != nil {
     49 		log.Fatalf("unknown goos %s", objabi.GOOS)
     50 	}
     51 
     52 	ctxt.Flag_optimize = true
     53 	ctxt.Framepointer_enabled = objabi.Framepointer_enabled(objabi.GOOS, arch.Name)
     54 	return ctxt
     55 }
     56 
     57 // LookupDerived looks up or creates the symbol with name name derived from symbol s.
     58 // The resulting symbol will be static iff s is.
     59 func (ctxt *Link) LookupDerived(s *LSym, name string) *LSym {
     60 	if s.Static() {
     61 		return ctxt.LookupStatic(name)
     62 	}
     63 	return ctxt.Lookup(name)
     64 }
     65 
     66 // LookupStatic looks up the static symbol with name name.
     67 // If it does not exist, it creates it.
     68 func (ctxt *Link) LookupStatic(name string) *LSym {
     69 	s := ctxt.statichash[name]
     70 	if s == nil {
     71 		s = &LSym{Name: name, Attribute: AttrStatic}
     72 		ctxt.statichash[name] = s
     73 	}
     74 	return s
     75 }
     76 
     77 // Lookup looks up the symbol with name name.
     78 // If it does not exist, it creates it.
     79 func (ctxt *Link) Lookup(name string) *LSym {
     80 	return ctxt.LookupInit(name, nil)
     81 }
     82 
     83 // LookupInit looks up the symbol with name name.
     84 // If it does not exist, it creates it and
     85 // passes it to init for one-time initialization.
     86 func (ctxt *Link) LookupInit(name string, init func(s *LSym)) *LSym {
     87 	ctxt.hashmu.Lock()
     88 	s := ctxt.hash[name]
     89 	if s == nil {
     90 		s = &LSym{Name: name}
     91 		ctxt.hash[name] = s
     92 		if init != nil {
     93 			init(s)
     94 		}
     95 	}
     96 	ctxt.hashmu.Unlock()
     97 	return s
     98 }
     99 
    100 func (ctxt *Link) Float32Sym(f float32) *LSym {
    101 	i := math.Float32bits(f)
    102 	name := fmt.Sprintf("$f32.%08x", i)
    103 	return ctxt.LookupInit(name, func(s *LSym) {
    104 		s.Size = 4
    105 		s.Set(AttrLocal, true)
    106 	})
    107 }
    108 
    109 func (ctxt *Link) Float64Sym(f float64) *LSym {
    110 	i := math.Float64bits(f)
    111 	name := fmt.Sprintf("$f64.%016x", i)
    112 	return ctxt.LookupInit(name, func(s *LSym) {
    113 		s.Size = 8
    114 		s.Set(AttrLocal, true)
    115 	})
    116 }
    117 
    118 func (ctxt *Link) Int64Sym(i int64) *LSym {
    119 	name := fmt.Sprintf("$i64.%016x", uint64(i))
    120 	return ctxt.LookupInit(name, func(s *LSym) {
    121 		s.Size = 8
    122 		s.Set(AttrLocal, true)
    123 	})
    124 }
    125