github.com/tidwall/go@v0.0.0-20170415222209-6694a6888b7d/src/cmd/internal/obj/objfile.go (about) 1 // Copyright 2013 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Writing of Go object files. 6 // 7 // Originally, Go object files were Plan 9 object files, but no longer. 8 // Now they are more like standard object files, in that each symbol is defined 9 // by an associated memory image (bytes) and a list of relocations to apply 10 // during linking. We do not (yet?) use a standard file format, however. 11 // For now, the format is chosen to be as simple as possible to read and write. 12 // It may change for reasons of efficiency, or we may even switch to a 13 // standard file format if there are compelling benefits to doing so. 14 // See golang.org/s/go13linker for more background. 15 // 16 // The file format is: 17 // 18 // - magic header: "\x00\x00go19ld" 19 // - byte 1 - version number 20 // - sequence of strings giving dependencies (imported packages) 21 // - empty string (marks end of sequence) 22 // - sequence of symbol references used by the defined symbols 23 // - byte 0xff (marks end of sequence) 24 // - sequence of integer lengths: 25 // - total data length 26 // - total number of relocations 27 // - total number of pcdata 28 // - total number of automatics 29 // - total number of funcdata 30 // - total number of files 31 // - data, the content of the defined symbols 32 // - sequence of defined symbols 33 // - byte 0xff (marks end of sequence) 34 // - magic footer: "\xff\xffgo19ld" 35 // 36 // All integers are stored in a zigzag varint format. 37 // See golang.org/s/go12symtab for a definition. 38 // 39 // Data blocks and strings are both stored as an integer 40 // followed by that many bytes. 41 // 42 // A symbol reference is a string name followed by a version. 43 // 44 // A symbol points to other symbols using an index into the symbol 45 // reference sequence. Index 0 corresponds to a nil LSym* pointer. 46 // In the symbol layout described below "symref index" stands for this 47 // index. 48 // 49 // Each symbol is laid out as the following fields (taken from LSym*): 50 // 51 // - byte 0xfe (sanity check for synchronization) 52 // - type [int] 53 // - name & version [symref index] 54 // - flags [int] 55 // 1<<0 dupok 56 // 1<<1 local 57 // 1<<2 add to typelink table 58 // - size [int] 59 // - gotype [symref index] 60 // - p [data block] 61 // - nr [int] 62 // - r [nr relocations, sorted by off] 63 // 64 // If type == STEXT, there are a few more fields: 65 // 66 // - args [int] 67 // - locals [int] 68 // - nosplit [int] 69 // - flags [int] 70 // 1<<0 leaf 71 // 1<<1 C function 72 // 1<<2 function may call reflect.Type.Method 73 // - nlocal [int] 74 // - local [nlocal automatics] 75 // - pcln [pcln table] 76 // 77 // Each relocation has the encoding: 78 // 79 // - off [int] 80 // - siz [int] 81 // - type [int] 82 // - add [int] 83 // - sym [symref index] 84 // 85 // Each local has the encoding: 86 // 87 // - asym [symref index] 88 // - offset [int] 89 // - type [int] 90 // - gotype [symref index] 91 // 92 // The pcln table has the encoding: 93 // 94 // - pcsp [data block] 95 // - pcfile [data block] 96 // - pcline [data block] 97 // - pcinline [data block] 98 // - npcdata [int] 99 // - pcdata [npcdata data blocks] 100 // - nfuncdata [int] 101 // - funcdata [nfuncdata symref index] 102 // - funcdatasym [nfuncdata ints] 103 // - nfile [int] 104 // - file [nfile symref index] 105 // - ninlinedcall [int] 106 // - inlinedcall [ninlinedcall int symref int symref] 107 // 108 // The file layout and meaning of type integers are architecture-independent. 109 // 110 // TODO(rsc): The file format is good for a first pass but needs work. 111 // - There are SymID in the object file that should really just be strings. 112 113 package obj 114 115 import ( 116 "bufio" 117 "cmd/internal/dwarf" 118 "cmd/internal/sys" 119 "fmt" 120 "log" 121 "path/filepath" 122 "sort" 123 ) 124 125 // objWriter writes Go object files. 126 type objWriter struct { 127 wr *bufio.Writer 128 ctxt *Link 129 // Temporary buffer for zigzag int writing. 130 varintbuf [10]uint8 131 132 // Provide the index of a symbol reference by symbol name. 133 // One map for versioned symbols and one for unversioned symbols. 134 // Used for deduplicating the symbol reference list. 135 refIdx map[string]int 136 vrefIdx map[string]int 137 138 // Number of objects written of each type. 139 nRefs int 140 nData int 141 nReloc int 142 nPcdata int 143 nAutom int 144 nFuncdata int 145 nFile int 146 } 147 148 func (w *objWriter) addLengths(s *LSym) { 149 w.nData += len(s.P) 150 w.nReloc += len(s.R) 151 152 if s.Type != STEXT { 153 return 154 } 155 156 pc := &s.Pcln 157 158 data := 0 159 data += len(pc.Pcsp.P) 160 data += len(pc.Pcfile.P) 161 data += len(pc.Pcline.P) 162 data += len(pc.Pcinline.P) 163 for i := 0; i < len(pc.Pcdata); i++ { 164 data += len(pc.Pcdata[i].P) 165 } 166 167 w.nData += data 168 w.nPcdata += len(pc.Pcdata) 169 170 w.nAutom += len(s.Autom) 171 w.nFuncdata += len(pc.Funcdataoff) 172 w.nFile += len(pc.File) 173 } 174 175 func (w *objWriter) writeLengths() { 176 w.writeInt(int64(w.nData)) 177 w.writeInt(int64(w.nReloc)) 178 w.writeInt(int64(w.nPcdata)) 179 w.writeInt(int64(w.nAutom)) 180 w.writeInt(int64(w.nFuncdata)) 181 w.writeInt(int64(w.nFile)) 182 } 183 184 func newObjWriter(ctxt *Link, b *bufio.Writer) *objWriter { 185 return &objWriter{ 186 ctxt: ctxt, 187 wr: b, 188 vrefIdx: make(map[string]int), 189 refIdx: make(map[string]int), 190 } 191 } 192 193 func WriteObjFile(ctxt *Link, b *bufio.Writer) { 194 w := newObjWriter(ctxt, b) 195 196 // Magic header 197 w.wr.WriteString("\x00\x00go19ld") 198 199 // Version 200 w.wr.WriteByte(1) 201 202 // Autolib 203 for _, pkg := range ctxt.Imports { 204 w.writeString(pkg) 205 } 206 w.writeString("") 207 208 // Symbol references 209 for _, s := range ctxt.Text { 210 w.writeRefs(s) 211 w.addLengths(s) 212 } 213 for _, s := range ctxt.Data { 214 w.writeRefs(s) 215 w.addLengths(s) 216 } 217 // End symbol references 218 w.wr.WriteByte(0xff) 219 220 // Lengths 221 w.writeLengths() 222 223 // Data block 224 for _, s := range ctxt.Text { 225 w.wr.Write(s.P) 226 pc := &s.Pcln 227 w.wr.Write(pc.Pcsp.P) 228 w.wr.Write(pc.Pcfile.P) 229 w.wr.Write(pc.Pcline.P) 230 w.wr.Write(pc.Pcinline.P) 231 for i := 0; i < len(pc.Pcdata); i++ { 232 w.wr.Write(pc.Pcdata[i].P) 233 } 234 } 235 for _, s := range ctxt.Data { 236 w.wr.Write(s.P) 237 } 238 239 // Symbols 240 for _, s := range ctxt.Text { 241 w.writeSym(s) 242 } 243 for _, s := range ctxt.Data { 244 w.writeSym(s) 245 } 246 247 // Magic footer 248 w.wr.WriteString("\xff\xffgo19ld") 249 } 250 251 // Symbols are prefixed so their content doesn't get confused with the magic footer. 252 const symPrefix = 0xfe 253 254 func (w *objWriter) writeRef(s *LSym, isPath bool) { 255 if s == nil || s.RefIdx != 0 { 256 return 257 } 258 var m map[string]int 259 switch s.Version { 260 case 0: 261 m = w.refIdx 262 case 1: 263 m = w.vrefIdx 264 default: 265 log.Fatalf("%s: invalid version number %d", s.Name, s.Version) 266 } 267 268 idx := m[s.Name] 269 if idx != 0 { 270 s.RefIdx = idx 271 return 272 } 273 w.wr.WriteByte(symPrefix) 274 if isPath { 275 w.writeString(filepath.ToSlash(s.Name)) 276 } else { 277 w.writeString(s.Name) 278 } 279 w.writeInt(int64(s.Version)) 280 w.nRefs++ 281 s.RefIdx = w.nRefs 282 m[s.Name] = w.nRefs 283 } 284 285 func (w *objWriter) writeRefs(s *LSym) { 286 w.writeRef(s, false) 287 w.writeRef(s.Gotype, false) 288 for i := range s.R { 289 w.writeRef(s.R[i].Sym, false) 290 } 291 292 if s.Type == STEXT { 293 for _, a := range s.Autom { 294 w.writeRef(a.Asym, false) 295 w.writeRef(a.Gotype, false) 296 } 297 pc := &s.Pcln 298 for _, d := range pc.Funcdata { 299 w.writeRef(d, false) 300 } 301 for _, f := range pc.File { 302 fsym := w.ctxt.Lookup(f, 0) 303 w.writeRef(fsym, true) 304 } 305 for _, call := range pc.InlTree.nodes { 306 w.writeRef(call.Func, false) 307 f, _ := linkgetlineFromPos(w.ctxt, call.Pos) 308 fsym := w.ctxt.Lookup(f, 0) 309 w.writeRef(fsym, true) 310 } 311 } 312 } 313 314 func (w *objWriter) writeSymDebug(s *LSym) { 315 ctxt := w.ctxt 316 fmt.Fprintf(ctxt.Bso, "%s ", s.Name) 317 if s.Version != 0 { 318 fmt.Fprintf(ctxt.Bso, "v=%d ", s.Version) 319 } 320 if s.Type != 0 { 321 fmt.Fprintf(ctxt.Bso, "t=%d ", s.Type) 322 } 323 if s.DuplicateOK() { 324 fmt.Fprintf(ctxt.Bso, "dupok ") 325 } 326 if s.CFunc() { 327 fmt.Fprintf(ctxt.Bso, "cfunc ") 328 } 329 if s.NoSplit() { 330 fmt.Fprintf(ctxt.Bso, "nosplit ") 331 } 332 fmt.Fprintf(ctxt.Bso, "size=%d", s.Size) 333 if s.Type == STEXT { 334 fmt.Fprintf(ctxt.Bso, " args=%#x locals=%#x", uint64(s.Args), uint64(s.Locals)) 335 if s.Leaf() { 336 fmt.Fprintf(ctxt.Bso, " leaf") 337 } 338 } 339 fmt.Fprintf(ctxt.Bso, "\n") 340 if s.Type == STEXT { 341 for p := s.Text; p != nil; p = p.Link { 342 fmt.Fprintf(ctxt.Bso, "\t%#04x %v\n", uint(int(p.Pc)), p) 343 } 344 } 345 for i := 0; i < len(s.P); i += 16 { 346 fmt.Fprintf(ctxt.Bso, "\t%#04x", uint(i)) 347 j := i 348 for j = i; j < i+16 && j < len(s.P); j++ { 349 fmt.Fprintf(ctxt.Bso, " %02x", s.P[j]) 350 } 351 for ; j < i+16; j++ { 352 fmt.Fprintf(ctxt.Bso, " ") 353 } 354 fmt.Fprintf(ctxt.Bso, " ") 355 for j = i; j < i+16 && j < len(s.P); j++ { 356 c := int(s.P[j]) 357 if ' ' <= c && c <= 0x7e { 358 fmt.Fprintf(ctxt.Bso, "%c", c) 359 } else { 360 fmt.Fprintf(ctxt.Bso, ".") 361 } 362 } 363 364 fmt.Fprintf(ctxt.Bso, "\n") 365 } 366 367 sort.Sort(relocByOff(s.R)) // generate stable output 368 for _, r := range s.R { 369 name := "" 370 if r.Sym != nil { 371 name = r.Sym.Name 372 } else if r.Type == R_TLS_LE { 373 name = "TLS" 374 } 375 if ctxt.Arch.InFamily(sys.ARM, sys.PPC64) { 376 fmt.Fprintf(ctxt.Bso, "\trel %d+%d t=%d %s+%x\n", int(r.Off), r.Siz, r.Type, name, uint64(r.Add)) 377 } else { 378 fmt.Fprintf(ctxt.Bso, "\trel %d+%d t=%d %s+%d\n", int(r.Off), r.Siz, r.Type, name, r.Add) 379 } 380 } 381 } 382 383 func (w *objWriter) writeSym(s *LSym) { 384 ctxt := w.ctxt 385 if ctxt.Debugasm { 386 w.writeSymDebug(s) 387 } 388 389 w.wr.WriteByte(symPrefix) 390 w.writeInt(int64(s.Type)) 391 w.writeRefIndex(s) 392 flags := int64(0) 393 if s.DuplicateOK() { 394 flags |= 1 395 } 396 if s.Local() { 397 flags |= 1 << 1 398 } 399 if s.MakeTypelink() { 400 flags |= 1 << 2 401 } 402 w.writeInt(flags) 403 w.writeInt(s.Size) 404 w.writeRefIndex(s.Gotype) 405 w.writeInt(int64(len(s.P))) 406 407 w.writeInt(int64(len(s.R))) 408 var r *Reloc 409 for i := 0; i < len(s.R); i++ { 410 r = &s.R[i] 411 w.writeInt(int64(r.Off)) 412 w.writeInt(int64(r.Siz)) 413 w.writeInt(int64(r.Type)) 414 w.writeInt(r.Add) 415 w.writeRefIndex(r.Sym) 416 } 417 418 if s.Type != STEXT { 419 return 420 } 421 422 w.writeInt(int64(s.Args)) 423 w.writeInt(int64(s.Locals)) 424 if s.NoSplit() { 425 w.writeInt(1) 426 } else { 427 w.writeInt(0) 428 } 429 flags = int64(0) 430 if s.Leaf() { 431 flags |= 1 432 } 433 if s.CFunc() { 434 flags |= 1 << 1 435 } 436 if s.ReflectMethod() { 437 flags |= 1 << 2 438 } 439 w.writeInt(flags) 440 w.writeInt(int64(len(s.Autom))) 441 for _, a := range s.Autom { 442 w.writeRefIndex(a.Asym) 443 w.writeInt(int64(a.Aoffset)) 444 if a.Name == NAME_AUTO { 445 w.writeInt(A_AUTO) 446 } else if a.Name == NAME_PARAM { 447 w.writeInt(A_PARAM) 448 } else { 449 log.Fatalf("%s: invalid local variable type %d", s.Name, a.Name) 450 } 451 w.writeRefIndex(a.Gotype) 452 } 453 454 pc := &s.Pcln 455 w.writeInt(int64(len(pc.Pcsp.P))) 456 w.writeInt(int64(len(pc.Pcfile.P))) 457 w.writeInt(int64(len(pc.Pcline.P))) 458 w.writeInt(int64(len(pc.Pcinline.P))) 459 w.writeInt(int64(len(pc.Pcdata))) 460 for i := 0; i < len(pc.Pcdata); i++ { 461 w.writeInt(int64(len(pc.Pcdata[i].P))) 462 } 463 w.writeInt(int64(len(pc.Funcdataoff))) 464 for i := 0; i < len(pc.Funcdataoff); i++ { 465 w.writeRefIndex(pc.Funcdata[i]) 466 } 467 for i := 0; i < len(pc.Funcdataoff); i++ { 468 w.writeInt(pc.Funcdataoff[i]) 469 } 470 w.writeInt(int64(len(pc.File))) 471 for _, f := range pc.File { 472 fsym := ctxt.Lookup(f, 0) 473 w.writeRefIndex(fsym) 474 } 475 w.writeInt(int64(len(pc.InlTree.nodes))) 476 for _, call := range pc.InlTree.nodes { 477 w.writeInt(int64(call.Parent)) 478 f, l := linkgetlineFromPos(w.ctxt, call.Pos) 479 fsym := ctxt.Lookup(f, 0) 480 w.writeRefIndex(fsym) 481 w.writeInt(int64(l)) 482 w.writeRefIndex(call.Func) 483 } 484 } 485 486 func (w *objWriter) writeInt(sval int64) { 487 var v uint64 488 uv := (uint64(sval) << 1) ^ uint64(sval>>63) 489 p := w.varintbuf[:] 490 for v = uv; v >= 0x80; v >>= 7 { 491 p[0] = uint8(v | 0x80) 492 p = p[1:] 493 } 494 p[0] = uint8(v) 495 p = p[1:] 496 w.wr.Write(w.varintbuf[:len(w.varintbuf)-len(p)]) 497 } 498 499 func (w *objWriter) writeString(s string) { 500 w.writeInt(int64(len(s))) 501 w.wr.WriteString(s) 502 } 503 504 func (w *objWriter) writeRefIndex(s *LSym) { 505 if s == nil { 506 w.writeInt(0) 507 return 508 } 509 if s.RefIdx == 0 { 510 log.Fatalln("writing an unreferenced symbol", s.Name) 511 } 512 w.writeInt(int64(s.RefIdx)) 513 } 514 515 // relocByOff sorts relocations by their offsets. 516 type relocByOff []Reloc 517 518 func (x relocByOff) Len() int { return len(x) } 519 func (x relocByOff) Less(i, j int) bool { return x[i].Off < x[j].Off } 520 func (x relocByOff) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 521 522 // implement dwarf.Context 523 type dwCtxt struct{ *Link } 524 525 func (c dwCtxt) PtrSize() int { 526 return c.Arch.PtrSize 527 } 528 func (c dwCtxt) AddInt(s dwarf.Sym, size int, i int64) { 529 ls := s.(*LSym) 530 ls.WriteInt(c.Link, ls.Size, size, i) 531 } 532 func (c dwCtxt) AddBytes(s dwarf.Sym, b []byte) { 533 ls := s.(*LSym) 534 ls.WriteBytes(c.Link, ls.Size, b) 535 } 536 func (c dwCtxt) AddString(s dwarf.Sym, v string) { 537 ls := s.(*LSym) 538 ls.WriteString(c.Link, ls.Size, len(v), v) 539 ls.WriteInt(c.Link, ls.Size, 1, 0) 540 } 541 func (c dwCtxt) SymValue(s dwarf.Sym) int64 { 542 return 0 543 } 544 func (c dwCtxt) AddAddress(s dwarf.Sym, data interface{}, value int64) { 545 rsym := data.(*LSym) 546 ls := s.(*LSym) 547 size := c.PtrSize() 548 ls.WriteAddr(c.Link, ls.Size, size, rsym, value) 549 } 550 func (c dwCtxt) AddSectionOffset(s dwarf.Sym, size int, t interface{}, ofs int64) { 551 ls := s.(*LSym) 552 rsym := t.(*LSym) 553 ls.WriteAddr(c.Link, ls.Size, size, rsym, ofs) 554 r := &ls.R[len(ls.R)-1] 555 r.Type = R_DWARFREF 556 } 557 558 // dwarfSym returns the DWARF symbol for TEXT symbol. 559 func (ctxt *Link) dwarfSym(s *LSym) *LSym { 560 if s.Type != STEXT { 561 ctxt.Diag("dwarfSym of non-TEXT %v", s) 562 } 563 if s.FuncInfo.dwarfSym == nil { 564 s.FuncInfo.dwarfSym = ctxt.Lookup(dwarf.InfoPrefix+s.Name, int(s.Version)) 565 } 566 return s.FuncInfo.dwarfSym 567 } 568 569 // populateDWARF fills in the DWARF Debugging Information Entry for TEXT symbol s. 570 // The DWARF symbol must already have been initialized in InitTextSym. 571 func (ctxt *Link) populateDWARF(curfn interface{}, s *LSym) { 572 dsym := ctxt.dwarfSym(s) 573 if dsym.Size != 0 { 574 ctxt.Diag("makeFuncDebugEntry double process %v", s) 575 } 576 var vars []*dwarf.Var 577 if ctxt.DebugInfo != nil { 578 vars = ctxt.DebugInfo(s, curfn) 579 } 580 dwarf.PutFunc(dwCtxt{ctxt}, dsym, s.Name, s.Version == 0, s, s.Size, vars) 581 }