github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/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  	"fmt"
    36  	"log"
    37  	"math"
    38  	"os"
    39  	"path/filepath"
    40  )
    41  
    42  // WorkingDir returns the current working directory
    43  // (or "/???" if the directory cannot be identified),
    44  // with "/" as separator.
    45  func WorkingDir() string {
    46  	var path string
    47  	path, _ = os.Getwd()
    48  	if path == "" {
    49  		path = "/???"
    50  	}
    51  	return filepath.ToSlash(path)
    52  }
    53  
    54  func Linknew(arch *LinkArch) *Link {
    55  	ctxt := new(Link)
    56  	ctxt.Hash = make(map[SymVer]*LSym)
    57  	ctxt.Arch = arch
    58  	ctxt.Pathname = WorkingDir()
    59  
    60  	ctxt.Headtype.Set(GOOS)
    61  	if ctxt.Headtype < 0 {
    62  		log.Fatalf("unknown goos %s", GOOS)
    63  	}
    64  
    65  	ctxt.Flag_optimize = true
    66  	ctxt.Framepointer_enabled = Framepointer_enabled(GOOS, arch.Name)
    67  	return ctxt
    68  }
    69  
    70  // Lookup looks up the symbol with name name and version v.
    71  // If it does not exist, it creates it.
    72  func (ctxt *Link) Lookup(name string, v int) *LSym {
    73  	return ctxt.LookupInit(name, v, nil)
    74  }
    75  
    76  // LookupInit looks up the symbol with name name and version v.
    77  // If it does not exist, it creates it and passes it to initfn for one-time initialization.
    78  func (ctxt *Link) LookupInit(name string, v int, init func(s *LSym)) *LSym {
    79  	s := ctxt.Hash[SymVer{name, v}]
    80  	if s != nil {
    81  		return s
    82  	}
    83  
    84  	s = &LSym{Name: name, Version: int16(v)}
    85  	ctxt.Hash[SymVer{name, v}] = s
    86  	if init != nil {
    87  		init(s)
    88  	}
    89  	return s
    90  }
    91  
    92  func (ctxt *Link) Float32Sym(f float32) *LSym {
    93  	i := math.Float32bits(f)
    94  	name := fmt.Sprintf("$f32.%08x", i)
    95  	return ctxt.LookupInit(name, 0, func(s *LSym) {
    96  		s.Size = 4
    97  		s.Set(AttrLocal, true)
    98  	})
    99  }
   100  
   101  func (ctxt *Link) Float64Sym(f float64) *LSym {
   102  	i := math.Float64bits(f)
   103  	name := fmt.Sprintf("$f64.%016x", i)
   104  	return ctxt.LookupInit(name, 0, func(s *LSym) {
   105  		s.Size = 8
   106  		s.Set(AttrLocal, true)
   107  	})
   108  }
   109  
   110  func (ctxt *Link) Int64Sym(i int64) *LSym {
   111  	name := fmt.Sprintf("$i64.%016x", uint64(i))
   112  	return ctxt.LookupInit(name, 0, func(s *LSym) {
   113  		s.Size = 8
   114  		s.Set(AttrLocal, true)
   115  	})
   116  }
   117  
   118  func Linksymfmt(s *LSym) string {
   119  	if s == nil {
   120  		return "<nil>"
   121  	}
   122  	return s.Name
   123  }