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