github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/cmd/internal/obj/sym.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  	"os"
    37  	"path/filepath"
    38  	"runtime"
    39  	"strconv"
    40  )
    41  
    42  var headers = []struct {
    43  	name string
    44  	val  int
    45  }{
    46  	{"darwin", Hdarwin},
    47  	{"dragonfly", Hdragonfly},
    48  	{"elf", Helf},
    49  	{"freebsd", Hfreebsd},
    50  	{"linux", Hlinux},
    51  	{"android", Hlinux}, // must be after "linux" entry or else headstr(Hlinux) == "android"
    52  	{"nacl", Hnacl},
    53  	{"netbsd", Hnetbsd},
    54  	{"openbsd", Hopenbsd},
    55  	{"plan9", Hplan9},
    56  	{"solaris", Hsolaris},
    57  	{"windows", Hwindows},
    58  	{"windowsgui", Hwindows},
    59  }
    60  
    61  func headtype(name string) int {
    62  	for i := 0; i < len(headers); i++ {
    63  		if name == headers[i].name {
    64  			return headers[i].val
    65  		}
    66  	}
    67  	return -1
    68  }
    69  
    70  func Headstr(v int) string {
    71  	for i := 0; i < len(headers); i++ {
    72  		if v == headers[i].val {
    73  			return headers[i].name
    74  		}
    75  	}
    76  	return strconv.Itoa(v)
    77  }
    78  
    79  func Linknew(arch *LinkArch) *Link {
    80  	ctxt := new(Link)
    81  	ctxt.Hash = make(map[SymVer]*LSym)
    82  	ctxt.Arch = arch
    83  	ctxt.Version = HistVersion
    84  	ctxt.Goroot = Getgoroot()
    85  	ctxt.Goroot_final = os.Getenv("GOROOT_FINAL")
    86  	if runtime.GOOS == "windows" {
    87  		// TODO(rsc): Remove ctxt.Windows and let callers use runtime.GOOS.
    88  		ctxt.Windows = 1
    89  	}
    90  
    91  	var buf string
    92  	buf, _ = os.Getwd()
    93  	if buf == "" {
    94  		buf = "/???"
    95  	}
    96  	buf = filepath.ToSlash(buf)
    97  	ctxt.Pathname = buf
    98  
    99  	ctxt.LineHist.GOROOT = ctxt.Goroot
   100  	ctxt.LineHist.GOROOT_FINAL = ctxt.Goroot_final
   101  	ctxt.LineHist.Dir = ctxt.Pathname
   102  
   103  	ctxt.Headtype = headtype(Getgoos())
   104  	if ctxt.Headtype < 0 {
   105  		log.Fatalf("unknown goos %s", Getgoos())
   106  	}
   107  
   108  	// On arm, record goarm.
   109  	if ctxt.Arch.Thechar == '5' {
   110  		ctxt.Goarm = Getgoarm()
   111  	}
   112  
   113  	return ctxt
   114  }
   115  
   116  func _lookup(ctxt *Link, symb string, v int, create bool) *LSym {
   117  	s := ctxt.Hash[SymVer{symb, v}]
   118  	if s != nil || !create {
   119  		return s
   120  	}
   121  
   122  	s = &LSym{
   123  		Name:    symb,
   124  		Type:    0,
   125  		Version: int16(v),
   126  		Value:   0,
   127  		Size:    0,
   128  	}
   129  	ctxt.Hash[SymVer{symb, v}] = s
   130  
   131  	return s
   132  }
   133  
   134  func Linklookup(ctxt *Link, name string, v int) *LSym {
   135  	return _lookup(ctxt, name, v, true)
   136  }
   137  
   138  // read-only lookup
   139  func linkrlookup(ctxt *Link, name string, v int) *LSym {
   140  	return _lookup(ctxt, name, v, false)
   141  }
   142  
   143  func Linksymfmt(s *LSym) string {
   144  	if s == nil {
   145  		return "<nil>"
   146  	}
   147  	return s.Name
   148  }