objfile.go 7.21 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2013 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package ld

import (
	"bytes"
	"fmt"
	"log"
	"strconv"
	"strings"
)

15 16 17 18
const (
	startmagic = "\x00\x00go13ld"
	endmagic   = "\xff\xffgo13ld"
)
19 20

func ldobjfile(ctxt *Link, f *Biobuf, pkg string, length int64, pn string) {
Russ Cox's avatar
Russ Cox committed
21
	start := Boffset(f)
22
	ctxt.Version++
Russ Cox's avatar
Russ Cox committed
23
	var buf [8]uint8
24 25 26 27
	Bread(f, buf[:])
	if string(buf[:]) != startmagic {
		log.Fatalf("%s: invalid file start %x %x %x %x %x %x %x %x", pn, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7])
	}
Russ Cox's avatar
Russ Cox committed
28
	c := Bgetc(f)
29 30 31 32
	if c != 1 {
		log.Fatalf("%s: invalid file version number %d", pn, c)
	}

Russ Cox's avatar
Russ Cox committed
33
	var lib string
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	for {
		lib = rdstring(f)
		if lib == "" {
			break
		}
		addlib(ctxt, pkg, pn, lib)
	}

	for {
		c = Bgetc(f)
		Bungetc(f)
		if c == 0xff {
			break
		}
		readsym(ctxt, f, pkg, pn)
	}

	buf = [8]uint8{}
	Bread(f, buf[:])
	if string(buf[:]) != endmagic {
		log.Fatalf("%s: invalid file end", pn)
	}

	if Boffset(f) != start+length {
		log.Fatalf("%s: unexpected end at %d, want %d", pn, int64(Boffset(f)), int64(start+length))
	}
}

var readsym_ndup int

func readsym(ctxt *Link, f *Biobuf, pkg string, pn string) {
	if Bgetc(f) != 0xfe {
		log.Fatalf("readsym out of sync")
	}
Russ Cox's avatar
Russ Cox committed
68 69 70
	t := int(rdint(f))
	name := expandpkg(rdstring(f), pkg)
	v := int(rdint(f))
71 72 73
	if v != 0 && v != 1 {
		log.Fatalf("invalid symbol version %d", v)
	}
Russ Cox's avatar
Russ Cox committed
74
	dupok := int(rdint(f))
75
	dupok &= 1
Russ Cox's avatar
Russ Cox committed
76 77 78
	size := int(rdint(f))
	typ := rdsym(ctxt, f, pkg)
	var data []byte
79
	rddata(f, &data)
Russ Cox's avatar
Russ Cox committed
80
	nreloc := int(rdint(f))
81 82 83 84

	if v != 0 {
		v = ctxt.Version
	}
Russ Cox's avatar
Russ Cox committed
85
	s := Linklookup(ctxt, name, v)
Russ Cox's avatar
Russ Cox committed
86
	var dup *LSym
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	if s.Type != 0 && s.Type != SXREF {
		if (t == SDATA || t == SBSS || t == SNOPTRBSS) && len(data) == 0 && nreloc == 0 {
			if s.Size < int64(size) {
				s.Size = int64(size)
			}
			if typ != nil && s.Gotype == nil {
				s.Gotype = typ
			}
			return
		}

		if (s.Type == SDATA || s.Type == SBSS || s.Type == SNOPTRBSS) && len(s.P) == 0 && len(s.R) == 0 {
			goto overwrite
		}
		if s.Type != SBSS && s.Type != SNOPTRBSS && dupok == 0 && s.Dupok == 0 {
			log.Fatalf("duplicate symbol %s (types %d and %d) in %s and %s", s.Name, s.Type, t, s.File, pn)
		}
		if len(s.P) > 0 {
			dup = s
			s = linknewsym(ctxt, ".dup", readsym_ndup)
			readsym_ndup++ // scratch
		}
	}

overwrite:
	s.File = pkg
	s.Dupok = uint8(dupok)
	if t == SXREF {
		log.Fatalf("bad sxref")
	}
	if t == 0 {
		log.Fatalf("missing type for %s in %s", name, pn)
	}
	if t == SBSS && (s.Type == SRODATA || s.Type == SNOPTRBSS) {
		t = int(s.Type)
	}
	s.Type = int16(t)
	if s.Size < int64(size) {
		s.Size = int64(size)
	}
	if typ != nil { // if bss sym defined multiple times, take type from any one def
		s.Gotype = typ
	}
	if dup != nil && typ != nil {
		dup.Gotype = typ
	}
	s.P = data
	s.P = s.P[:len(data)]
	if nreloc > 0 {
		s.R = make([]Reloc, nreloc)
		s.R = s.R[:nreloc]
Russ Cox's avatar
Russ Cox committed
138 139
		var r *Reloc
		for i := 0; i < nreloc; i++ {
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
			r = &s.R[i]
			r.Off = int32(rdint(f))
			r.Siz = uint8(rdint(f))
			r.Type = int32(rdint(f))
			r.Add = rdint(f)
			r.Xadd = rdint(f)
			r.Sym = rdsym(ctxt, f, pkg)
			r.Xsym = rdsym(ctxt, f, pkg)
		}
	}

	if len(s.P) > 0 && dup != nil && len(dup.P) > 0 && strings.HasPrefix(s.Name, "gclocals·") {
		// content-addressed garbage collection liveness bitmap symbol.
		// double check for hash collisions.
		if !bytes.Equal(s.P, dup.P) {
			log.Fatalf("dupok hash collision for %s in %s and %s", s.Name, s.File, pn)
		}
	}

	if s.Type == STEXT {
		s.Args = int32(rdint(f))
		s.Locals = int32(rdint(f))
		s.Nosplit = uint8(rdint(f))
Russ Cox's avatar
Russ Cox committed
163
		v := int(rdint(f))
164 165
		s.Leaf = uint8(v & 1)
		s.Cfunc = uint8(v & 2)
Russ Cox's avatar
Russ Cox committed
166 167 168
		n := int(rdint(f))
		var a *Auto
		for i := 0; i < n; i++ {
169 170 171 172 173 174 175 176 177 178
			a = new(Auto)
			a.Asym = rdsym(ctxt, f, pkg)
			a.Aoffset = int32(rdint(f))
			a.Name = int16(rdint(f))
			a.Gotype = rdsym(ctxt, f, pkg)
			a.Link = s.Autom
			s.Autom = a
		}

		s.Pcln = new(Pcln)
Russ Cox's avatar
Russ Cox committed
179
		pc := s.Pcln
180 181 182 183 184 185
		rddata(f, &pc.Pcsp.P)
		rddata(f, &pc.Pcfile.P)
		rddata(f, &pc.Pcline.P)
		n = int(rdint(f))
		pc.Pcdata = make([]Pcdata, n)
		pc.Npcdata = n
Russ Cox's avatar
Russ Cox committed
186
		for i := 0; i < n; i++ {
187 188 189 190 191 192
			rddata(f, &pc.Pcdata[i].P)
		}
		n = int(rdint(f))
		pc.Funcdata = make([]*LSym, n)
		pc.Funcdataoff = make([]int64, n)
		pc.Nfuncdata = n
Russ Cox's avatar
Russ Cox committed
193
		for i := 0; i < n; i++ {
194 195
			pc.Funcdata[i] = rdsym(ctxt, f, pkg)
		}
Russ Cox's avatar
Russ Cox committed
196
		for i := 0; i < n; i++ {
197 198 199 200 201
			pc.Funcdataoff[i] = rdint(f)
		}
		n = int(rdint(f))
		pc.File = make([]*LSym, n)
		pc.Nfile = n
Russ Cox's avatar
Russ Cox committed
202
		for i := 0; i < n; i++ {
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
			pc.File[i] = rdsym(ctxt, f, pkg)
		}

		if dup == nil {
			if s.Onlist != 0 {
				log.Fatalf("symbol %s listed multiple times", s.Name)
			}
			s.Onlist = 1
			if ctxt.Etextp != nil {
				ctxt.Etextp.Next = s
			} else {
				ctxt.Textp = s
			}
			ctxt.Etextp = s
		}
	}

	if ctxt.Debugasm != 0 {
		fmt.Fprintf(ctxt.Bso, "%s ", s.Name)
		if s.Version != 0 {
			fmt.Fprintf(ctxt.Bso, "v=%d ", s.Version)
		}
		if s.Type != 0 {
			fmt.Fprintf(ctxt.Bso, "t=%d ", s.Type)
		}
		if s.Dupok != 0 {
			fmt.Fprintf(ctxt.Bso, "dupok ")
		}
		if s.Cfunc != 0 {
			fmt.Fprintf(ctxt.Bso, "cfunc ")
		}
		if s.Nosplit != 0 {
			fmt.Fprintf(ctxt.Bso, "nosplit ")
		}
		fmt.Fprintf(ctxt.Bso, "size=%d value=%d", int64(s.Size), int64(s.Value))
		if s.Type == STEXT {
			fmt.Fprintf(ctxt.Bso, " args=%#x locals=%#x", uint64(s.Args), uint64(s.Locals))
		}
		fmt.Fprintf(ctxt.Bso, "\n")
Russ Cox's avatar
Russ Cox committed
242 243 244
		var c int
		var j int
		for i := 0; i < len(s.P); {
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
			fmt.Fprintf(ctxt.Bso, "\t%#04x", uint(i))
			for j = i; j < i+16 && j < len(s.P); j++ {
				fmt.Fprintf(ctxt.Bso, " %02x", s.P[j])
			}
			for ; j < i+16; j++ {
				fmt.Fprintf(ctxt.Bso, "   ")
			}
			fmt.Fprintf(ctxt.Bso, "  ")
			for j = i; j < i+16 && j < len(s.P); j++ {
				c = int(s.P[j])
				if ' ' <= c && c <= 0x7e {
					fmt.Fprintf(ctxt.Bso, "%c", c)
				} else {
					fmt.Fprintf(ctxt.Bso, ".")
				}
			}

			fmt.Fprintf(ctxt.Bso, "\n")
			i += 16
		}

Russ Cox's avatar
Russ Cox committed
266 267
		var r *Reloc
		for i := 0; i < len(s.R); i++ {
268 269 270 271 272 273 274 275 276
			r = &s.R[i]
			fmt.Fprintf(ctxt.Bso, "\trel %d+%d t=%d %s+%d\n", int(r.Off), r.Siz, r.Type, r.Sym.Name, int64(r.Add))
		}
	}
}

func rdint(f *Biobuf) int64 {
	var c int

Russ Cox's avatar
Russ Cox committed
277 278
	uv := uint64(0)
	for shift := 0; ; shift += 7 {
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
		if shift >= 64 {
			log.Fatalf("corrupt input")
		}
		c = Bgetc(f)
		uv |= uint64(c&0x7F) << uint(shift)
		if c&0x80 == 0 {
			break
		}
	}

	return int64(uv>>1) ^ (int64(uint64(uv)<<63) >> 63)
}

func rdstring(f *Biobuf) string {
	n := rdint(f)
	p := make([]byte, n)
	Bread(f, p)
	return string(p)
}

func rddata(f *Biobuf, pp *[]byte) {
	n := rdint(f)
	*pp = make([]byte, n)
	Bread(f, *pp)
}

var symbuf []byte

func rdsym(ctxt *Link, f *Biobuf, pkg string) *LSym {
Russ Cox's avatar
Russ Cox committed
308
	n := int(rdint(f))
309 310 311 312 313 314 315 316 317
	if n == 0 {
		rdint(f)
		return nil
	}

	if len(symbuf) < n {
		symbuf = make([]byte, n)
	}
	Bread(f, symbuf[:n])
Russ Cox's avatar
Russ Cox committed
318 319
	p := string(symbuf[:n])
	v := int(rdint(f))
320 321 322
	if v != 0 {
		v = ctxt.Version
	}
Russ Cox's avatar
Russ Cox committed
323
	s := Linklookup(ctxt, expandpkg(p, pkg), v)
324 325 326 327

	if v == 0 && s.Name[0] == '$' && s.Type == 0 {
		if strings.HasPrefix(s.Name, "$f32.") {
			x, _ := strconv.ParseUint(s.Name[5:], 16, 32)
Russ Cox's avatar
Russ Cox committed
328
			i32 := int32(x)
329 330 331 332 333
			s.Type = SRODATA
			Adduint32(ctxt, s, uint32(i32))
			s.Reachable = false
		} else if strings.HasPrefix(s.Name, "$f64.") || strings.HasPrefix(s.Name, "$i64.") {
			x, _ := strconv.ParseUint(s.Name[5:], 16, 64)
Russ Cox's avatar
Russ Cox committed
334
			i64 := int64(x)
335 336 337 338 339 340 341 342
			s.Type = SRODATA
			Adduint64(ctxt, s, uint64(i64))
			s.Reachable = false
		}
	}

	return s
}