sym.go 6.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
// Derived from Inferno utils/6l/obj.c and utils/6l/span.c
// http://code.google.com/p/inferno-os/source/browse/utils/6l/obj.c
// http://code.google.com/p/inferno-os/source/browse/utils/6l/span.c
//
//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
//	Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
//	Portions Copyright © 1997-1999 Vita Nuova Limited
//	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
//	Portions Copyright © 2004,2006 Bruce Ellis
//	Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
//	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
//	Portions Copyright © 2009 The Go Authors.  All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package ld

import (
	"cmd/internal/obj"
	"log"
37
	"strconv"
38 39 40 41 42 43
)

var headers = []struct {
	name string
	val  int
}{
44 45 46 47 48 49 50 51 52 53 54 55 56
	{"darwin", obj.Hdarwin},
	{"dragonfly", obj.Hdragonfly},
	{"elf", obj.Helf},
	{"freebsd", obj.Hfreebsd},
	{"linux", obj.Hlinux},
	{"android", obj.Hlinux}, // must be after "linux" entry or else headstr(Hlinux) == "android"
	{"nacl", obj.Hnacl},
	{"netbsd", obj.Hnetbsd},
	{"openbsd", obj.Hopenbsd},
	{"plan9", obj.Hplan9},
	{"solaris", obj.Hsolaris},
	{"windows", obj.Hwindows},
	{"windowsgui", obj.Hwindows},
57 58 59
}

func linknew(arch *LinkArch) *Link {
60
	ctxt := &Link{
61 62 63 64 65 66
		HashName:    make(map[string]*LSym, 100000), // preallocate about 2mb for hash
		HashVersion: make(map[symVer]*LSym),
		Allsym:      make([]*LSym, 0, 100000),
		Arch:        arch,
		Version:     obj.HistVersion,
		Goroot:      obj.Getgoroot(),
67
	}
68

Russ Cox's avatar
Russ Cox committed
69
	p := obj.Getgoarch()
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
	if p != arch.Name {
		log.Fatalf("invalid goarch %s (want %s)", p, arch.Name)
	}

	ctxt.Headtype = headtype(obj.Getgoos())
	if ctxt.Headtype < 0 {
		log.Fatalf("unknown goos %s", obj.Getgoos())
	}

	// Record thread-local storage offset.
	// TODO(rsc): Move tlsoffset back into the linker.
	switch ctxt.Headtype {
	default:
		log.Fatalf("unknown thread-local storage offset for %s", Headstr(ctxt.Headtype))

85
	case obj.Hplan9, obj.Hwindows:
86 87 88 89 90 91 92
		break

		/*
		 * ELF uses TLS offset negative from FS.
		 * Translate 0(FS) and 8(FS) into -16(FS) and -8(FS).
		 * Known to low-level assembly in package runtime and runtime/cgo.
		 */
93 94 95 96 97 98
	case obj.Hlinux,
		obj.Hfreebsd,
		obj.Hnetbsd,
		obj.Hopenbsd,
		obj.Hdragonfly,
		obj.Hsolaris:
99 100 101 102 103 104 105 106 107 108 109 110
		if obj.Getgoos() == "android" {
			switch ctxt.Arch.Thechar {
			case '6':
				// Android/amd64 constant - offset from 0(FS) to our TLS slot.
				// Explained in src/runtime/cgo/gcc_android_*.c
				ctxt.Tlsoffset = 0x1d0
			case '8':
				// Android/386 constant - offset from 0(GS) to our TLS slot.
				ctxt.Tlsoffset = 0xf8
			default:
				ctxt.Tlsoffset = -1 * ctxt.Arch.Ptrsize
			}
111 112 113
		} else {
			ctxt.Tlsoffset = -1 * ctxt.Arch.Ptrsize
		}
114

115
	case obj.Hnacl:
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
		switch ctxt.Arch.Thechar {
		default:
			log.Fatalf("unknown thread-local storage offset for nacl/%s", ctxt.Arch.Name)

		case '5':
			ctxt.Tlsoffset = 0

		case '6':
			ctxt.Tlsoffset = 0

		case '8':
			ctxt.Tlsoffset = -8
		}

		/*
		 * OS X system constants - offset from 0(GS) to our TLS.
132
		 * Explained in src/runtime/cgo/gcc_darwin_*.c.
133
		 */
134
	case obj.Hdarwin:
135 136 137 138
		switch ctxt.Arch.Thechar {
		default:
			log.Fatalf("unknown thread-local storage offset for darwin/%s", ctxt.Arch.Name)

139 140 141
		case '5':
			ctxt.Tlsoffset = 0 // dummy value, not needed

142 143 144
		case '6':
			ctxt.Tlsoffset = 0x8a0

145 146 147
		case '7':
			ctxt.Tlsoffset = 0 // dummy value, not needed

148 149 150 151 152 153 154
		case '8':
			ctxt.Tlsoffset = 0x468
		}
	}

	// On arm, record goarm.
	if ctxt.Arch.Thechar == '5' {
155
		ctxt.Goarm = obj.Getgoarm()
156 157 158 159 160 161
	}

	return ctxt
}

func linknewsym(ctxt *Link, symb string, v int) *LSym {
162 163 164 165 166 167
	batch := ctxt.LSymBatch
	if len(batch) == 0 {
		batch = make([]LSym, 1000)
	}
	s := &batch[0]
	ctxt.LSymBatch = batch[1:]
168 169 170 171 172 173 174 175

	s.Dynid = -1
	s.Plt = -1
	s.Got = -1
	s.Name = symb
	s.Version = int16(v)
	ctxt.Nsymbol++

176 177 178 179 180
	if v != -1 {
		ctxt.Allsym = append(ctxt.Allsym, s)
	} else if v < -1 {
		ctxt.Diag("invalid version %d in linknewsym", v)
	}
181 182 183 184 185 186 187 188 189
	return s
}

type symVer struct {
	sym string
	ver int
}

func _lookup(ctxt *Link, symb string, v int, creat int) *LSym {
190 191 192 193 194 195
	// Most symbols have only a single version, and a string key
	// is faster to search for. So we store the first symbol in HashName,
	// keyed only by symbol name. If there are name collisions, the
	// alternate versions are stored in the spill over map
	// HashVersion.
	s, exist := ctxt.HashName[symb]
196
	if s != nil {
197 198 199 200 201 202 203
		if int(s.Version) == v {
			return s
		}
		s = ctxt.HashVersion[symVer{symb, v}]
		if s != nil {
			return s
		}
204 205 206 207 208 209 210
	}
	if creat == 0 {
		return nil
	}

	s = linknewsym(ctxt, symb, v)
	s.Extname = s.Name
211 212 213 214 215
	if exist {
		ctxt.HashVersion[symVer{symb, v}] = s
	} else {
		ctxt.HashName[symb] = s
	}
216 217 218 219 220 221 222 223 224 225 226 227 228
	return s
}

func Linklookup(ctxt *Link, name string, v int) *LSym {
	return _lookup(ctxt, name, v, 1)
}

// read-only lookup
func Linkrlookup(ctxt *Link, name string, v int) *LSym {
	return _lookup(ctxt, name, v, 0)
}

func Headstr(v int) string {
Russ Cox's avatar
Russ Cox committed
229
	for i := 0; i < len(headers); i++ {
230 231 232 233
		if v == headers[i].val {
			return headers[i].name
		}
	}
234
	return strconv.Itoa(v)
235 236 237
}

func headtype(name string) int {
Russ Cox's avatar
Russ Cox committed
238
	for i := 0; i < len(headers); i++ {
239 240 241 242 243 244
		if name == headers[i].name {
			return headers[i].val
		}
	}
	return -1
}