github.com/Filosottile/go@v0.0.0-20170906193555-dbed9972d994/src/cmd/internal/obj/sym.go (about)

     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@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  	"cmd/internal/objabi"
    36  	"fmt"
    37  	"math"
    38  )
    39  
    40  func Linknew(arch *LinkArch) *Link {
    41  	ctxt := new(Link)
    42  	ctxt.hash = make(map[string]*LSym)
    43  	ctxt.statichash = make(map[string]*LSym)
    44  	ctxt.Arch = arch
    45  	ctxt.Pathname = objabi.WorkingDir()
    46  
    47  	ctxt.Headtype.Set(objabi.GOOS)
    48  
    49  	ctxt.Flag_optimize = true
    50  	ctxt.Framepointer_enabled = objabi.Framepointer_enabled(objabi.GOOS, arch.Name)
    51  	return ctxt
    52  }
    53  
    54  // LookupDerived looks up or creates the symbol with name name derived from symbol s.
    55  // The resulting symbol will be static iff s is.
    56  func (ctxt *Link) LookupDerived(s *LSym, name string) *LSym {
    57  	if s.Static() {
    58  		return ctxt.LookupStatic(name)
    59  	}
    60  	return ctxt.Lookup(name)
    61  }
    62  
    63  // LookupStatic looks up the static symbol with name name.
    64  // If it does not exist, it creates it.
    65  func (ctxt *Link) LookupStatic(name string) *LSym {
    66  	s := ctxt.statichash[name]
    67  	if s == nil {
    68  		s = &LSym{Name: name, Attribute: AttrStatic}
    69  		ctxt.statichash[name] = s
    70  	}
    71  	return s
    72  }
    73  
    74  // Lookup looks up the symbol with name name.
    75  // If it does not exist, it creates it.
    76  func (ctxt *Link) Lookup(name string) *LSym {
    77  	return ctxt.LookupInit(name, nil)
    78  }
    79  
    80  // LookupInit looks up the symbol with name name.
    81  // If it does not exist, it creates it and
    82  // passes it to init for one-time initialization.
    83  func (ctxt *Link) LookupInit(name string, init func(s *LSym)) *LSym {
    84  	ctxt.hashmu.Lock()
    85  	s := ctxt.hash[name]
    86  	if s == nil {
    87  		s = &LSym{Name: name}
    88  		ctxt.hash[name] = s
    89  		if init != nil {
    90  			init(s)
    91  		}
    92  	}
    93  	ctxt.hashmu.Unlock()
    94  	return s
    95  }
    96  
    97  func (ctxt *Link) Float32Sym(f float32) *LSym {
    98  	i := math.Float32bits(f)
    99  	name := fmt.Sprintf("$f32.%08x", i)
   100  	return ctxt.LookupInit(name, func(s *LSym) {
   101  		s.Size = 4
   102  		s.Set(AttrLocal, true)
   103  	})
   104  }
   105  
   106  func (ctxt *Link) Float64Sym(f float64) *LSym {
   107  	i := math.Float64bits(f)
   108  	name := fmt.Sprintf("$f64.%016x", i)
   109  	return ctxt.LookupInit(name, func(s *LSym) {
   110  		s.Size = 8
   111  		s.Set(AttrLocal, true)
   112  	})
   113  }
   114  
   115  func (ctxt *Link) Int64Sym(i int64) *LSym {
   116  	name := fmt.Sprintf("$i64.%016x", uint64(i))
   117  	return ctxt.LookupInit(name, func(s *LSym) {
   118  		s.Size = 8
   119  		s.Set(AttrLocal, true)
   120  	})
   121  }