1 // Inferno utils/6l/asm.c 2 // http://code.google.com/p/inferno-os/source/browse/utils/6l/asm.c 3 // 4 // Copyright 1994-1999 Lucent Technologies Inc. All rights reserved. 5 // Portions Copyright 1995-1997 C H Forsyth (forsyth (a] terzarima.net) 6 // Portions Copyright 1997-1999 Vita Nuova Limited 7 // Portions Copyright 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com) 8 // Portions Copyright 2004,2006 Bruce Ellis 9 // Portions Copyright 2005-2007 C H Forsyth (forsyth (a] terzarima.net) 10 // Revisions Copyright 2000-2007 Lucent Technologies Inc. and others 11 // Portions Copyright 2009 The Go Authors. All rights reserved. 12 // 13 // Permission is hereby granted, free of charge, to any person obtaining a copy 14 // of this software and associated documentation files (the "Software"), to deal 15 // in the Software without restriction, including without limitation the rights 16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 // copies of the Software, and to permit persons to whom the Software is 18 // furnished to do so, subject to the following conditions: 19 // 20 // The above copyright notice and this permission notice shall be included in 21 // all copies or substantial portions of the Software. 22 // 23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 // THE SOFTWARE. 30 31 package amd64 32 33 import ( 34 "cmd/internal/obj" 35 "cmd/link/internal/ld" 36 "debug/elf" 37 "fmt" 38 "log" 39 ) 40 41 func PADDR(x uint32) uint32 { 42 return x &^ 0x80000000 43 } 44 45 var zeroes string 46 47 func Addcall(ctxt *ld.Link, s *ld.LSym, t *ld.LSym) int64 { 48 s.Reachable = true 49 i := s.Size 50 s.Size += 4 51 ld.Symgrow(ctxt, s, s.Size) 52 r := ld.Addrel(s) 53 r.Sym = t 54 r.Off = int32(i) 55 r.Type = obj.R_CALL 56 r.Siz = 4 57 return i + int64(r.Siz) 58 } 59 60 func gentext() { 61 if !ld.DynlinkingGo() { 62 return 63 } 64 addmoduledata := ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0) 65 if addmoduledata.Type == obj.STEXT { 66 // we're linking a module containing the runtime -> no need for 67 // an init function 68 return 69 } 70 addmoduledata.Reachable = true 71 initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0) 72 initfunc.Type = obj.STEXT 73 initfunc.Local = true 74 initfunc.Reachable = true 75 o := func(op ...uint8) { 76 for _, op1 := range op { 77 ld.Adduint8(ld.Ctxt, initfunc, op1) 78 } 79 } 80 // 0000000000000000 <local.dso_init>: 81 // 0: 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # 7 <local.dso_init+0x7> 82 // 3: R_X86_64_PC32 runtime.firstmoduledata-0x4 83 o(0x48, 0x8d, 0x3d) 84 ld.Addpcrelplus(ld.Ctxt, initfunc, ld.Linklookup(ld.Ctxt, "runtime.firstmoduledata", 0), 0) 85 // 7: e8 00 00 00 00 callq c <local.dso_init+0xc> 86 // 8: R_X86_64_PLT32 runtime.addmoduledata-0x4 87 o(0xe8) 88 Addcall(ld.Ctxt, initfunc, addmoduledata) 89 // c: c3 retq 90 o(0xc3) 91 if ld.Ctxt.Etextp != nil { 92 ld.Ctxt.Etextp.Next = initfunc 93 } else { 94 ld.Ctxt.Textp = initfunc 95 } 96 ld.Ctxt.Etextp = initfunc 97 initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0) 98 initarray_entry.Reachable = true 99 initarray_entry.Local = true 100 initarray_entry.Type = obj.SINITARR 101 ld.Addaddr(ld.Ctxt, initarray_entry, initfunc) 102 } 103 104 func adddynrela(rela *ld.LSym, s *ld.LSym, r *ld.Reloc) { 105 ld.Addaddrplus(ld.Ctxt, rela, s, int64(r.Off)) 106 ld.Adduint64(ld.Ctxt, rela, ld.R_X86_64_RELATIVE) 107 ld.Addaddrplus(ld.Ctxt, rela, r.Sym, r.Add) // Addend 108 } 109 110 func adddynrel(s *ld.LSym, r *ld.Reloc) { 111 targ := r.Sym 112 ld.Ctxt.Cursym = s 113 114 switch r.Type { 115 default: 116 if r.Type >= 256 { 117 ld.Diag("unexpected relocation type %d", r.Type) 118 return 119 } 120 121 // Handle relocations found in ELF object files. 122 case 256 + ld.R_X86_64_PC32: 123 if targ.Type == obj.SDYNIMPORT { 124 ld.Diag("unexpected R_X86_64_PC32 relocation for dynamic symbol %s", targ.Name) 125 } 126 if targ.Type == 0 || targ.Type == obj.SXREF { 127 ld.Diag("unknown symbol %s in pcrel", targ.Name) 128 } 129 r.Type = obj.R_PCREL 130 r.Add += 4 131 return 132 133 case 256 + ld.R_X86_64_PLT32: 134 r.Type = obj.R_PCREL 135 r.Add += 4 136 if targ.Type == obj.SDYNIMPORT { 137 addpltsym(targ) 138 r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) 139 r.Add += int64(targ.Plt) 140 } 141 142 return 143 144 case 256 + ld.R_X86_64_GOTPCREL: 145 if targ.Type != obj.SDYNIMPORT { 146 // have symbol 147 if r.Off >= 2 && s.P[r.Off-2] == 0x8b { 148 // turn MOVQ of GOT entry into LEAQ of symbol itself 149 s.P[r.Off-2] = 0x8d 150 151 r.Type = obj.R_PCREL 152 r.Add += 4 153 return 154 } 155 } 156 157 // fall back to using GOT and hope for the best (CMOV*) 158 // TODO: just needs relocation, no need to put in .dynsym 159 addgotsym(targ) 160 161 r.Type = obj.R_PCREL 162 r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) 163 r.Add += 4 164 r.Add += int64(targ.Got) 165 return 166 167 case 256 + ld.R_X86_64_64: 168 if targ.Type == obj.SDYNIMPORT { 169 ld.Diag("unexpected R_X86_64_64 relocation for dynamic symbol %s", targ.Name) 170 } 171 r.Type = obj.R_ADDR 172 return 173 174 // Handle relocations found in Mach-O object files. 175 case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 0, 176 512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 0, 177 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 0: 178 // TODO: What is the difference between all these? 179 r.Type = obj.R_ADDR 180 181 if targ.Type == obj.SDYNIMPORT { 182 ld.Diag("unexpected reloc for dynamic symbol %s", targ.Name) 183 } 184 return 185 186 case 512 + ld.MACHO_X86_64_RELOC_BRANCH*2 + 1: 187 if targ.Type == obj.SDYNIMPORT { 188 addpltsym(targ) 189 r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) 190 r.Add = int64(targ.Plt) 191 r.Type = obj.R_PCREL 192 return 193 } 194 fallthrough 195 196 // fall through 197 case 512 + ld.MACHO_X86_64_RELOC_UNSIGNED*2 + 1, 198 512 + ld.MACHO_X86_64_RELOC_SIGNED*2 + 1, 199 512 + ld.MACHO_X86_64_RELOC_SIGNED_1*2 + 1, 200 512 + ld.MACHO_X86_64_RELOC_SIGNED_2*2 + 1, 201 512 + ld.MACHO_X86_64_RELOC_SIGNED_4*2 + 1: 202 r.Type = obj.R_PCREL 203 204 if targ.Type == obj.SDYNIMPORT { 205 ld.Diag("unexpected pc-relative reloc for dynamic symbol %s", targ.Name) 206 } 207 return 208 209 case 512 + ld.MACHO_X86_64_RELOC_GOT_LOAD*2 + 1: 210 if targ.Type != obj.SDYNIMPORT { 211 // have symbol 212 // turn MOVQ of GOT entry into LEAQ of symbol itself 213 if r.Off < 2 || s.P[r.Off-2] != 0x8b { 214 ld.Diag("unexpected GOT_LOAD reloc for non-dynamic symbol %s", targ.Name) 215 return 216 } 217 218 s.P[r.Off-2] = 0x8d 219 r.Type = obj.R_PCREL 220 return 221 } 222 fallthrough 223 224 // fall through 225 case 512 + ld.MACHO_X86_64_RELOC_GOT*2 + 1: 226 if targ.Type != obj.SDYNIMPORT { 227 ld.Diag("unexpected GOT reloc for non-dynamic symbol %s", targ.Name) 228 } 229 addgotsym(targ) 230 r.Type = obj.R_PCREL 231 r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) 232 r.Add += int64(targ.Got) 233 return 234 } 235 236 // Handle references to ELF symbols from our own object files. 237 if targ.Type != obj.SDYNIMPORT { 238 return 239 } 240 241 switch r.Type { 242 case obj.R_CALL, 243 obj.R_PCREL: 244 if ld.HEADTYPE == obj.Hwindows { 245 // nothing to do, the relocation will be laid out in pereloc1 246 return 247 } else { 248 // for both ELF and Mach-O 249 addpltsym(targ) 250 r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) 251 r.Add = int64(targ.Plt) 252 return 253 } 254 255 case obj.R_ADDR: 256 if s.Type == obj.STEXT && ld.Iself { 257 if ld.HEADTYPE == obj.Hsolaris { 258 addpltsym(targ) 259 r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0) 260 r.Add += int64(targ.Plt) 261 return 262 } 263 // The code is asking for the address of an external 264 // function. We provide it with the address of the 265 // correspondent GOT symbol. 266 addgotsym(targ) 267 268 r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0) 269 r.Add += int64(targ.Got) 270 return 271 } 272 273 if s.Type != obj.SDATA { 274 break 275 } 276 if ld.Iself { 277 ld.Adddynsym(ld.Ctxt, targ) 278 rela := ld.Linklookup(ld.Ctxt, ".rela", 0) 279 ld.Addaddrplus(ld.Ctxt, rela, s, int64(r.Off)) 280 if r.Siz == 8 { 281 ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_64)) 282 } else { 283 ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_X86_64_32)) 284 } 285 ld.Adduint64(ld.Ctxt, rela, uint64(r.Add)) 286 r.Type = 256 // ignore during relocsym 287 return 288 } 289 290 if ld.HEADTYPE == obj.Hdarwin && s.Size == int64(ld.Thearch.Ptrsize) && r.Off == 0 { 291 // Mach-O relocations are a royal pain to lay out. 292 // They use a compact stateful bytecode representation 293 // that is too much bother to deal with. 294 // Instead, interpret the C declaration 295 // void *_Cvar_stderr = &stderr; 296 // as making _Cvar_stderr the name of a GOT entry 297 // for stderr. This is separate from the usual GOT entry, 298 // just in case the C code assigns to the variable, 299 // and of course it only works for single pointers, 300 // but we only need to support cgo and that's all it needs. 301 ld.Adddynsym(ld.Ctxt, targ) 302 303 got := ld.Linklookup(ld.Ctxt, ".got", 0) 304 s.Type = got.Type | obj.SSUB 305 s.Outer = got 306 s.Sub = got.Sub 307 got.Sub = s 308 s.Value = got.Size 309 ld.Adduint64(ld.Ctxt, got, 0) 310 ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.got", 0), uint32(targ.Dynid)) 311 r.Type = 256 // ignore during relocsym 312 return 313 } 314 315 if ld.HEADTYPE == obj.Hwindows { 316 // nothing to do, the relocation will be laid out in pereloc1 317 return 318 } 319 } 320 321 ld.Ctxt.Cursym = s 322 ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type) 323 } 324 325 func elfreloc1(r *ld.Reloc, sectoff int64) int { 326 ld.Thearch.Vput(uint64(sectoff)) 327 328 elfsym := r.Xsym.Elfsym 329 switch r.Type { 330 default: 331 return -1 332 333 case obj.R_ADDR: 334 if r.Siz == 4 { 335 ld.Thearch.Vput(ld.R_X86_64_32 | uint64(elfsym)<<32) 336 } else if r.Siz == 8 { 337 ld.Thearch.Vput(ld.R_X86_64_64 | uint64(elfsym)<<32) 338 } else { 339 return -1 340 } 341 342 case obj.R_TLS_LE: 343 if r.Siz == 4 { 344 ld.Thearch.Vput(ld.R_X86_64_TPOFF32 | uint64(elfsym)<<32) 345 } else { 346 return -1 347 } 348 349 case obj.R_TLS_IE: 350 if r.Siz == 4 { 351 ld.Thearch.Vput(ld.R_X86_64_GOTTPOFF | uint64(elfsym)<<32) 352 } else { 353 return -1 354 } 355 356 case obj.R_CALL: 357 if r.Siz == 4 { 358 if r.Xsym.Type == obj.SDYNIMPORT { 359 if ld.DynlinkingGo() { 360 ld.Thearch.Vput(ld.R_X86_64_PLT32 | uint64(elfsym)<<32) 361 } else { 362 ld.Thearch.Vput(ld.R_X86_64_GOTPCREL | uint64(elfsym)<<32) 363 } 364 } else { 365 ld.Thearch.Vput(ld.R_X86_64_PC32 | uint64(elfsym)<<32) 366 } 367 } else { 368 return -1 369 } 370 371 case obj.R_PCREL: 372 if r.Siz == 4 { 373 if r.Xsym.Type == obj.SDYNIMPORT && r.Xsym.ElfType == elf.STT_FUNC { 374 ld.Thearch.Vput(ld.R_X86_64_PLT32 | uint64(elfsym)<<32) 375 } else { 376 ld.Thearch.Vput(ld.R_X86_64_PC32 | uint64(elfsym)<<32) 377 } 378 } else { 379 return -1 380 } 381 382 case obj.R_GOTPCREL: 383 if r.Siz == 4 { 384 ld.Thearch.Vput(ld.R_X86_64_GOTPCREL | uint64(elfsym)<<32) 385 } else { 386 return -1 387 } 388 } 389 390 ld.Thearch.Vput(uint64(r.Xadd)) 391 return 0 392 } 393 394 func machoreloc1(r *ld.Reloc, sectoff int64) int { 395 var v uint32 396 397 rs := r.Xsym 398 399 if rs.Type == obj.SHOSTOBJ || r.Type == obj.R_PCREL { 400 if rs.Dynid < 0 { 401 ld.Diag("reloc %d to non-macho symbol %s type=%d", r.Type, rs.Name, rs.Type) 402 return -1 403 } 404 405 v = uint32(rs.Dynid) 406 v |= 1 << 27 // external relocation 407 } else { 408 v = uint32(rs.Sect.Extnum) 409 if v == 0 { 410 ld.Diag("reloc %d to symbol %s in non-macho section %s type=%d", r.Type, rs.Name, rs.Sect.Name, rs.Type) 411 return -1 412 } 413 } 414 415 switch r.Type { 416 default: 417 return -1 418 419 case obj.R_ADDR: 420 v |= ld.MACHO_X86_64_RELOC_UNSIGNED << 28 421 422 case obj.R_CALL: 423 v |= 1 << 24 // pc-relative bit 424 v |= ld.MACHO_X86_64_RELOC_BRANCH << 28 425 426 // NOTE: Only works with 'external' relocation. Forced above. 427 case obj.R_PCREL: 428 v |= 1 << 24 // pc-relative bit 429 v |= ld.MACHO_X86_64_RELOC_SIGNED << 28 430 } 431 432 switch r.Siz { 433 default: 434 return -1 435 436 case 1: 437 v |= 0 << 25 438 439 case 2: 440 v |= 1 << 25 441 442 case 4: 443 v |= 2 << 25 444 445 case 8: 446 v |= 3 << 25 447 } 448 449 ld.Thearch.Lput(uint32(sectoff)) 450 ld.Thearch.Lput(v) 451 return 0 452 } 453 454 func pereloc1(r *ld.Reloc, sectoff int64) bool { 455 var v uint32 456 457 rs := r.Xsym 458 459 if rs.Dynid < 0 { 460 ld.Diag("reloc %d to non-coff symbol %s type=%d", r.Type, rs.Name, rs.Type) 461 return false 462 } 463 464 ld.Thearch.Lput(uint32(sectoff)) 465 ld.Thearch.Lput(uint32(rs.Dynid)) 466 467 switch r.Type { 468 default: 469 return false 470 471 case obj.R_ADDR: 472 if r.Siz == 8 { 473 v = ld.IMAGE_REL_AMD64_ADDR64 474 } else { 475 v = ld.IMAGE_REL_AMD64_ADDR32 476 } 477 478 case obj.R_CALL, 479 obj.R_PCREL: 480 v = ld.IMAGE_REL_AMD64_REL32 481 } 482 483 ld.Thearch.Wput(uint16(v)) 484 485 return true 486 } 487 488 func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int { 489 return -1 490 } 491 492 func archrelocvariant(r *ld.Reloc, s *ld.LSym, t int64) int64 { 493 log.Fatalf("unexpected relocation variant") 494 return t 495 } 496 497 func elfsetupplt() { 498 plt := ld.Linklookup(ld.Ctxt, ".plt", 0) 499 got := ld.Linklookup(ld.Ctxt, ".got.plt", 0) 500 if plt.Size == 0 { 501 // pushq got+8(IP) 502 ld.Adduint8(ld.Ctxt, plt, 0xff) 503 504 ld.Adduint8(ld.Ctxt, plt, 0x35) 505 ld.Addpcrelplus(ld.Ctxt, plt, got, 8) 506 507 // jmpq got+16(IP) 508 ld.Adduint8(ld.Ctxt, plt, 0xff) 509 510 ld.Adduint8(ld.Ctxt, plt, 0x25) 511 ld.Addpcrelplus(ld.Ctxt, plt, got, 16) 512 513 // nopl 0(AX) 514 ld.Adduint32(ld.Ctxt, plt, 0x00401f0f) 515 516 // assume got->size == 0 too 517 ld.Addaddrplus(ld.Ctxt, got, ld.Linklookup(ld.Ctxt, ".dynamic", 0), 0) 518 519 ld.Adduint64(ld.Ctxt, got, 0) 520 ld.Adduint64(ld.Ctxt, got, 0) 521 } 522 } 523 524 func addpltsym(s *ld.LSym) { 525 if s.Plt >= 0 { 526 return 527 } 528 529 ld.Adddynsym(ld.Ctxt, s) 530 531 if ld.Iself { 532 plt := ld.Linklookup(ld.Ctxt, ".plt", 0) 533 got := ld.Linklookup(ld.Ctxt, ".got.plt", 0) 534 rela := ld.Linklookup(ld.Ctxt, ".rela.plt", 0) 535 if plt.Size == 0 { 536 elfsetupplt() 537 } 538 539 // jmpq *got+size(IP) 540 ld.Adduint8(ld.Ctxt, plt, 0xff) 541 542 ld.Adduint8(ld.Ctxt, plt, 0x25) 543 ld.Addpcrelplus(ld.Ctxt, plt, got, got.Size) 544 545 // add to got: pointer to current pos in plt 546 ld.Addaddrplus(ld.Ctxt, got, plt, plt.Size) 547 548 // pushq $x 549 ld.Adduint8(ld.Ctxt, plt, 0x68) 550 551 ld.Adduint32(ld.Ctxt, plt, uint32((got.Size-24-8)/8)) 552 553 // jmpq .plt 554 ld.Adduint8(ld.Ctxt, plt, 0xe9) 555 556 ld.Adduint32(ld.Ctxt, plt, uint32(-(plt.Size + 4))) 557 558 // rela 559 ld.Addaddrplus(ld.Ctxt, rela, got, got.Size-8) 560 561 ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_X86_64_JMP_SLOT)) 562 ld.Adduint64(ld.Ctxt, rela, 0) 563 564 s.Plt = int32(plt.Size - 16) 565 } else if ld.HEADTYPE == obj.Hdarwin { 566 // To do lazy symbol lookup right, we're supposed 567 // to tell the dynamic loader which library each 568 // symbol comes from and format the link info 569 // section just so. I'm too lazy (ha!) to do that 570 // so for now we'll just use non-lazy pointers, 571 // which don't need to be told which library to use. 572 // 573 // http://networkpx.blogspot.com/2009/09/about-lcdyldinfoonly-command.html 574 // has details about what we're avoiding. 575 576 addgotsym(s) 577 plt := ld.Linklookup(ld.Ctxt, ".plt", 0) 578 579 ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.plt", 0), uint32(s.Dynid)) 580 581 // jmpq *got+size(IP) 582 s.Plt = int32(plt.Size) 583 584 ld.Adduint8(ld.Ctxt, plt, 0xff) 585 ld.Adduint8(ld.Ctxt, plt, 0x25) 586 ld.Addpcrelplus(ld.Ctxt, plt, ld.Linklookup(ld.Ctxt, ".got", 0), int64(s.Got)) 587 } else { 588 ld.Diag("addpltsym: unsupported binary format") 589 } 590 } 591 592 func addgotsym(s *ld.LSym) { 593 if s.Got >= 0 { 594 return 595 } 596 597 ld.Adddynsym(ld.Ctxt, s) 598 got := ld.Linklookup(ld.Ctxt, ".got", 0) 599 s.Got = int32(got.Size) 600 ld.Adduint64(ld.Ctxt, got, 0) 601 602 if ld.Iself { 603 rela := ld.Linklookup(ld.Ctxt, ".rela", 0) 604 ld.Addaddrplus(ld.Ctxt, rela, got, int64(s.Got)) 605 ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_X86_64_GLOB_DAT)) 606 ld.Adduint64(ld.Ctxt, rela, 0) 607 } else if ld.HEADTYPE == obj.Hdarwin { 608 ld.Adduint32(ld.Ctxt, ld.Linklookup(ld.Ctxt, ".linkedit.got", 0), uint32(s.Dynid)) 609 } else { 610 ld.Diag("addgotsym: unsupported binary format") 611 } 612 } 613 614 func asmb() { 615 if ld.Debug['v'] != 0 { 616 fmt.Fprintf(&ld.Bso, "%5.2f asmb\n", obj.Cputime()) 617 } 618 ld.Bso.Flush() 619 620 if ld.Debug['v'] != 0 { 621 fmt.Fprintf(&ld.Bso, "%5.2f codeblk\n", obj.Cputime()) 622 } 623 ld.Bso.Flush() 624 625 if ld.Iself { 626 ld.Asmbelfsetup() 627 } 628 629 sect := ld.Segtext.Sect 630 ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff)) 631 ld.Codeblk(int64(sect.Vaddr), int64(sect.Length)) 632 for sect = sect.Next; sect != nil; sect = sect.Next { 633 ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff)) 634 ld.Datblk(int64(sect.Vaddr), int64(sect.Length)) 635 } 636 637 if ld.Segrodata.Filelen > 0 { 638 if ld.Debug['v'] != 0 { 639 fmt.Fprintf(&ld.Bso, "%5.2f rodatblk\n", obj.Cputime()) 640 } 641 ld.Bso.Flush() 642 643 ld.Cseek(int64(ld.Segrodata.Fileoff)) 644 ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen)) 645 } 646 647 if ld.Debug['v'] != 0 { 648 fmt.Fprintf(&ld.Bso, "%5.2f datblk\n", obj.Cputime()) 649 } 650 ld.Bso.Flush() 651 652 ld.Cseek(int64(ld.Segdata.Fileoff)) 653 ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen)) 654 655 machlink := int64(0) 656 if ld.HEADTYPE == obj.Hdarwin { 657 if ld.Debug['v'] != 0 { 658 fmt.Fprintf(&ld.Bso, "%5.2f dwarf\n", obj.Cputime()) 659 } 660 661 dwarfoff := ld.Rnd(int64(uint64(ld.HEADR)+ld.Segtext.Length), int64(ld.INITRND)) + ld.Rnd(int64(ld.Segdata.Filelen), int64(ld.INITRND)) 662 ld.Cseek(dwarfoff) 663 664 ld.Segdwarf.Fileoff = uint64(ld.Cpos()) 665 ld.Dwarfemitdebugsections() 666 ld.Segdwarf.Filelen = uint64(ld.Cpos()) - ld.Segdwarf.Fileoff 667 668 machlink = ld.Domacholink() 669 } 670 671 switch ld.HEADTYPE { 672 default: 673 ld.Diag("unknown header type %d", ld.HEADTYPE) 674 fallthrough 675 676 case obj.Hplan9, 677 obj.Helf: 678 break 679 680 case obj.Hdarwin: 681 ld.Debug['8'] = 1 /* 64-bit addresses */ 682 683 case obj.Hlinux, 684 obj.Hfreebsd, 685 obj.Hnetbsd, 686 obj.Hopenbsd, 687 obj.Hdragonfly, 688 obj.Hsolaris: 689 ld.Debug['8'] = 1 /* 64-bit addresses */ 690 691 case obj.Hnacl, 692 obj.Hwindows: 693 break 694 } 695 696 ld.Symsize = 0 697 ld.Spsize = 0 698 ld.Lcsize = 0 699 symo := int64(0) 700 if ld.Debug['s'] == 0 { 701 if ld.Debug['v'] != 0 { 702 fmt.Fprintf(&ld.Bso, "%5.2f sym\n", obj.Cputime()) 703 } 704 ld.Bso.Flush() 705 switch ld.HEADTYPE { 706 default: 707 case obj.Hplan9, 708 obj.Helf: 709 ld.Debug['s'] = 1 710 symo = int64(ld.Segdata.Fileoff + ld.Segdata.Filelen) 711 712 case obj.Hdarwin: 713 symo = int64(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(ld.INITRND))) + uint64(machlink)) 714 715 case obj.Hlinux, 716 obj.Hfreebsd, 717 obj.Hnetbsd, 718 obj.Hopenbsd, 719 obj.Hdragonfly, 720 obj.Hsolaris, 721 obj.Hnacl: 722 symo = int64(ld.Segdata.Fileoff + ld.Segdata.Filelen) 723 symo = ld.Rnd(symo, int64(ld.INITRND)) 724 725 case obj.Hwindows: 726 symo = int64(ld.Segdata.Fileoff + ld.Segdata.Filelen) 727 symo = ld.Rnd(symo, ld.PEFILEALIGN) 728 } 729 730 ld.Cseek(symo) 731 switch ld.HEADTYPE { 732 default: 733 if ld.Iself { 734 ld.Cseek(symo) 735 ld.Asmelfsym() 736 ld.Cflush() 737 ld.Cwrite(ld.Elfstrdat) 738 739 if ld.Debug['v'] != 0 { 740 fmt.Fprintf(&ld.Bso, "%5.2f dwarf\n", obj.Cputime()) 741 } 742 743 ld.Dwarfemitdebugsections() 744 745 if ld.Linkmode == ld.LinkExternal { 746 ld.Elfemitreloc() 747 } 748 } 749 750 case obj.Hplan9: 751 ld.Asmplan9sym() 752 ld.Cflush() 753 754 sym := ld.Linklookup(ld.Ctxt, "pclntab", 0) 755 if sym != nil { 756 ld.Lcsize = int32(len(sym.P)) 757 for i := 0; int32(i) < ld.Lcsize; i++ { 758 ld.Cput(uint8(sym.P[i])) 759 } 760 761 ld.Cflush() 762 } 763 764 case obj.Hwindows: 765 if ld.Debug['v'] != 0 { 766 fmt.Fprintf(&ld.Bso, "%5.2f dwarf\n", obj.Cputime()) 767 } 768 769 ld.Dwarfemitdebugsections() 770 771 case obj.Hdarwin: 772 if ld.Linkmode == ld.LinkExternal { 773 ld.Machoemitreloc() 774 } 775 } 776 } 777 778 if ld.Debug['v'] != 0 { 779 fmt.Fprintf(&ld.Bso, "%5.2f headr\n", obj.Cputime()) 780 } 781 ld.Bso.Flush() 782 ld.Cseek(0) 783 switch ld.HEADTYPE { 784 default: 785 case obj.Hplan9: /* plan9 */ 786 magic := int32(4*26*26 + 7) 787 788 magic |= 0x00008000 /* fat header */ 789 ld.Lputb(uint32(magic)) /* magic */ 790 ld.Lputb(uint32(ld.Segtext.Filelen)) /* sizes */ 791 ld.Lputb(uint32(ld.Segdata.Filelen)) 792 ld.Lputb(uint32(ld.Segdata.Length - ld.Segdata.Filelen)) 793 ld.Lputb(uint32(ld.Symsize)) /* nsyms */ 794 vl := ld.Entryvalue() 795 ld.Lputb(PADDR(uint32(vl))) /* va of entry */ 796 ld.Lputb(uint32(ld.Spsize)) /* sp offsets */ 797 ld.Lputb(uint32(ld.Lcsize)) /* line offsets */ 798 ld.Vputb(uint64(vl)) /* va of entry */ 799 800 case obj.Hdarwin: 801 ld.Asmbmacho() 802 803 case obj.Hlinux, 804 obj.Hfreebsd, 805 obj.Hnetbsd, 806 obj.Hopenbsd, 807 obj.Hdragonfly, 808 obj.Hsolaris, 809 obj.Hnacl: 810 ld.Asmbelf(symo) 811 812 case obj.Hwindows: 813 ld.Asmbpe() 814 } 815 816 ld.Cflush() 817 } 818