github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/cmd/link/internal/ld/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 ld
    33  
    34  import (
    35  	"cmd/internal/obj"
    36  	"log"
    37  	"os"
    38  	"path/filepath"
    39  	"strconv"
    40  )
    41  
    42  var headers = []struct {
    43  	name string
    44  	val  int
    45  }{
    46  	{"darwin", obj.Hdarwin},
    47  	{"dragonfly", obj.Hdragonfly},
    48  	{"elf", obj.Helf},
    49  	{"freebsd", obj.Hfreebsd},
    50  	{"linux", obj.Hlinux},
    51  	{"android", obj.Hlinux}, // must be after "linux" entry or else headstr(Hlinux) == "android"
    52  	{"nacl", obj.Hnacl},
    53  	{"netbsd", obj.Hnetbsd},
    54  	{"openbsd", obj.Hopenbsd},
    55  	{"plan9", obj.Hplan9},
    56  	{"solaris", obj.Hsolaris},
    57  	{"windows", obj.Hwindows},
    58  	{"windowsgui", obj.Hwindows},
    59  }
    60  
    61  func linknew(arch *LinkArch) *Link {
    62  	ctxt := new(Link)
    63  	ctxt.Hash = make(map[symVer]*LSym)
    64  	ctxt.Arch = arch
    65  	ctxt.Version = obj.HistVersion
    66  	ctxt.Goroot = obj.Getgoroot()
    67  
    68  	p := obj.Getgoarch()
    69  	if p != arch.Name {
    70  		log.Fatalf("invalid goarch %s (want %s)", p, arch.Name)
    71  	}
    72  
    73  	var buf string
    74  	buf, _ = os.Getwd()
    75  	if buf == "" {
    76  		buf = "/???"
    77  	}
    78  	buf = filepath.ToSlash(buf)
    79  
    80  	ctxt.Headtype = headtype(obj.Getgoos())
    81  	if ctxt.Headtype < 0 {
    82  		log.Fatalf("unknown goos %s", obj.Getgoos())
    83  	}
    84  
    85  	// Record thread-local storage offset.
    86  	// TODO(rsc): Move tlsoffset back into the linker.
    87  	switch ctxt.Headtype {
    88  	default:
    89  		log.Fatalf("unknown thread-local storage offset for %s", Headstr(ctxt.Headtype))
    90  
    91  	case obj.Hplan9, obj.Hwindows:
    92  		break
    93  
    94  		/*
    95  		 * ELF uses TLS offset negative from FS.
    96  		 * Translate 0(FS) and 8(FS) into -16(FS) and -8(FS).
    97  		 * Known to low-level assembly in package runtime and runtime/cgo.
    98  		 */
    99  	case obj.Hlinux,
   100  		obj.Hfreebsd,
   101  		obj.Hnetbsd,
   102  		obj.Hopenbsd,
   103  		obj.Hdragonfly,
   104  		obj.Hsolaris:
   105  		if obj.Getgoos() == "android" {
   106  			switch ctxt.Arch.Thechar {
   107  			case '6':
   108  				// Android/amd64 constant - offset from 0(FS) to our TLS slot.
   109  				// Explained in src/runtime/cgo/gcc_android_*.c
   110  				ctxt.Tlsoffset = 0x1d0
   111  			case '8':
   112  				// Android/386 constant - offset from 0(GS) to our TLS slot.
   113  				ctxt.Tlsoffset = 0xf8
   114  			default:
   115  				ctxt.Tlsoffset = -1 * ctxt.Arch.Ptrsize
   116  			}
   117  		} else {
   118  			ctxt.Tlsoffset = -1 * ctxt.Arch.Ptrsize
   119  		}
   120  
   121  	case obj.Hnacl:
   122  		switch ctxt.Arch.Thechar {
   123  		default:
   124  			log.Fatalf("unknown thread-local storage offset for nacl/%s", ctxt.Arch.Name)
   125  
   126  		case '5':
   127  			ctxt.Tlsoffset = 0
   128  
   129  		case '6':
   130  			ctxt.Tlsoffset = 0
   131  
   132  		case '8':
   133  			ctxt.Tlsoffset = -8
   134  		}
   135  
   136  		/*
   137  		 * OS X system constants - offset from 0(GS) to our TLS.
   138  		 * Explained in src/runtime/cgo/gcc_darwin_*.c.
   139  		 */
   140  	case obj.Hdarwin:
   141  		switch ctxt.Arch.Thechar {
   142  		default:
   143  			log.Fatalf("unknown thread-local storage offset for darwin/%s", ctxt.Arch.Name)
   144  
   145  		case '5':
   146  			ctxt.Tlsoffset = 0 // dummy value, not needed
   147  
   148  		case '6':
   149  			ctxt.Tlsoffset = 0x8a0
   150  
   151  		case '7':
   152  			ctxt.Tlsoffset = 0 // dummy value, not needed
   153  
   154  		case '8':
   155  			ctxt.Tlsoffset = 0x468
   156  		}
   157  	}
   158  
   159  	// On arm, record goarm.
   160  	if ctxt.Arch.Thechar == '5' {
   161  		ctxt.Goarm = obj.Getgoarm()
   162  	}
   163  
   164  	return ctxt
   165  }
   166  
   167  func linknewsym(ctxt *Link, symb string, v int) *LSym {
   168  	s := new(LSym)
   169  	*s = LSym{}
   170  
   171  	s.Dynid = -1
   172  	s.Plt = -1
   173  	s.Got = -1
   174  	s.Name = symb
   175  	s.Type = 0
   176  	s.Version = int16(v)
   177  	s.Value = 0
   178  	s.Size = 0
   179  	ctxt.Nsymbol++
   180  
   181  	s.Allsym = ctxt.Allsym
   182  	ctxt.Allsym = s
   183  
   184  	return s
   185  }
   186  
   187  type symVer struct {
   188  	sym string
   189  	ver int
   190  }
   191  
   192  func _lookup(ctxt *Link, symb string, v int, creat int) *LSym {
   193  	s := ctxt.Hash[symVer{symb, v}]
   194  	if s != nil {
   195  		return s
   196  	}
   197  	if creat == 0 {
   198  		return nil
   199  	}
   200  
   201  	s = linknewsym(ctxt, symb, v)
   202  	s.Extname = s.Name
   203  	ctxt.Hash[symVer{symb, v}] = s
   204  	return s
   205  }
   206  
   207  func Linklookup(ctxt *Link, name string, v int) *LSym {
   208  	return _lookup(ctxt, name, v, 1)
   209  }
   210  
   211  // read-only lookup
   212  func Linkrlookup(ctxt *Link, name string, v int) *LSym {
   213  	return _lookup(ctxt, name, v, 0)
   214  }
   215  
   216  func Headstr(v int) string {
   217  	for i := 0; i < len(headers); i++ {
   218  		if v == headers[i].val {
   219  			return headers[i].name
   220  		}
   221  	}
   222  	return strconv.Itoa(v)
   223  }
   224  
   225  func headtype(name string) int {
   226  	for i := 0; i < len(headers); i++ {
   227  		if name == headers[i].name {
   228  			return headers[i].val
   229  		}
   230  	}
   231  	return -1
   232  }