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