1 // Inferno utils/5l/asm.c
2 // http://code.google.com/p/inferno-os/source/browse/utils/5l/asm.c
4 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@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@terzarima.net)
10 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11 // Portions Copyright © 2009 The Go Authors. All rights reserved.
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:
20 // The above copyright notice and this permission notice shall be included in
21 // all copies or substantial portions of the Software.
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
35 "cmd/link/internal/ld"
44 // ldr r0, .Lmoduledata
47 // b runtime.addmoduledata@plt
50 // .word local.moduledata(GOT_PREL) + (. - (.Lloadfrom + 4))
53 // 00000000 <local.dso_init>:
54 // 0: e59f0004 ldr r0, [pc, #4] ; c <local.dso_init+0xc>
55 // 4: e5900000 ldr r0, [r0]
56 // 8: eafffffe b 0 <runtime.addmoduledata>
57 // 8: R_ARM_JUMP24 runtime.addmoduledata
58 // c: 00000004 .word 0x00000004
59 // c: R_ARM_GOT_PREL local.moduledata
62 if !ld.DynlinkingGo() {
65 addmoduledata := ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0)
66 if addmoduledata.Type == obj.STEXT {
67 // we're linking a module containing the runtime -> no need for
71 addmoduledata.Attr |= ld.AttrReachable
72 initfunc := ld.Linklookup(ld.Ctxt, "go.link.addmoduledata", 0)
73 initfunc.Type = obj.STEXT
74 initfunc.Attr |= ld.AttrLocal
75 initfunc.Attr |= ld.AttrReachable
76 o := func(op uint32) {
77 ld.Adduint32(ld.Ctxt, initfunc, op)
83 rel := ld.Addrel(initfunc)
86 rel.Sym = ld.Linklookup(ld.Ctxt, "runtime.addmoduledata", 0)
87 rel.Type = obj.R_CALLARM
88 rel.Add = 0xeafffffe // vomit
91 rel = ld.Addrel(initfunc)
94 rel.Sym = ld.Ctxt.Moduledata
95 rel.Type = obj.R_PCREL
98 ld.Ctxt.Textp = append(ld.Ctxt.Textp, initfunc)
99 initarray_entry := ld.Linklookup(ld.Ctxt, "go.link.addmoduledatainit", 0)
100 initarray_entry.Attr |= ld.AttrReachable
101 initarray_entry.Attr |= ld.AttrLocal
102 initarray_entry.Type = obj.SINITARR
103 ld.Addaddr(ld.Ctxt, initarray_entry, initfunc)
106 // Preserve highest 8 bits of a, and do addition to lower 24-bit
107 // of a and b; used to adjust ARM branch instruction's target
108 func braddoff(a int32, b int32) int32 {
109 return int32((uint32(a))&0xff000000 | 0x00ffffff&uint32(a+b))
112 func adddynrel(s *ld.LSym, r *ld.Reloc) {
119 ld.Diag("unexpected relocation type %d", r.Type)
123 // Handle relocations found in ELF object files.
124 case 256 + ld.R_ARM_PLT32:
125 r.Type = obj.R_CALLARM
127 if targ.Type == obj.SDYNIMPORT {
128 addpltsym(ld.Ctxt, targ)
129 r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
130 r.Add = int64(braddoff(int32(r.Add), targ.Plt/4))
135 case 256 + ld.R_ARM_THM_PC22: // R_ARM_THM_CALL
136 ld.Exitf("R_ARM_THM_CALL, are you using -marm?")
139 case 256 + ld.R_ARM_GOT32: // R_ARM_GOT_BREL
140 if targ.Type != obj.SDYNIMPORT {
141 addgotsyminternal(ld.Ctxt, targ)
143 addgotsym(ld.Ctxt, targ)
146 r.Type = obj.R_CONST // write r->add during relocsym
148 r.Add += int64(targ.Got)
151 case 256 + ld.R_ARM_GOT_PREL: // GOT(nil) + A - nil
152 if targ.Type != obj.SDYNIMPORT {
153 addgotsyminternal(ld.Ctxt, targ)
155 addgotsym(ld.Ctxt, targ)
159 r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0)
160 r.Add += int64(targ.Got) + 4
163 case 256 + ld.R_ARM_GOTOFF: // R_ARM_GOTOFF32
164 r.Type = obj.R_GOTOFF
168 case 256 + ld.R_ARM_GOTPC: // R_ARM_BASE_PREL
171 r.Sym = ld.Linklookup(ld.Ctxt, ".got", 0)
175 case 256 + ld.R_ARM_CALL:
176 r.Type = obj.R_CALLARM
177 if targ.Type == obj.SDYNIMPORT {
178 addpltsym(ld.Ctxt, targ)
179 r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
180 r.Add = int64(braddoff(int32(r.Add), targ.Plt/4))
185 case 256 + ld.R_ARM_REL32: // R_ARM_REL32
191 case 256 + ld.R_ARM_ABS32:
192 if targ.Type == obj.SDYNIMPORT {
193 ld.Diag("unexpected R_ARM_ABS32 relocation for dynamic symbol %s", targ.Name)
198 // we can just ignore this, because we are targeting ARM V5+ anyway
199 case 256 + ld.R_ARM_V4BX:
201 // R_ARM_V4BX is ABS relocation, so this symbol is a dummy symbol, ignore it
208 case 256 + ld.R_ARM_PC24,
209 256 + ld.R_ARM_JUMP24:
210 r.Type = obj.R_CALLARM
211 if targ.Type == obj.SDYNIMPORT {
212 addpltsym(ld.Ctxt, targ)
213 r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
214 r.Add = int64(braddoff(int32(r.Add), targ.Plt/4))
220 // Handle references to ELF symbols from our own object files.
221 if targ.Type != obj.SDYNIMPORT {
227 addpltsym(ld.Ctxt, targ)
228 r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
229 r.Add = int64(targ.Plt)
233 if s.Type != obj.SDATA {
237 ld.Adddynsym(ld.Ctxt, targ)
238 rel := ld.Linklookup(ld.Ctxt, ".rel", 0)
239 ld.Addaddrplus(ld.Ctxt, rel, s, int64(r.Off))
240 ld.Adduint32(ld.Ctxt, rel, ld.ELF32_R_INFO(uint32(targ.Dynid), ld.R_ARM_GLOB_DAT)) // we need a nil + A dynamic reloc
241 r.Type = obj.R_CONST // write r->add during relocsym
248 ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type)
251 func elfreloc1(r *ld.Reloc, sectoff int64) int {
252 ld.Thearch.Lput(uint32(sectoff))
254 elfsym := r.Xsym.ElfsymForReloc()
261 ld.Thearch.Lput(ld.R_ARM_ABS32 | uint32(elfsym)<<8)
268 ld.Thearch.Lput(ld.R_ARM_REL32 | uint32(elfsym)<<8)
275 if r.Add&0xff000000 == 0xeb000000 { // BL
276 ld.Thearch.Lput(ld.R_ARM_CALL | uint32(elfsym)<<8)
278 ld.Thearch.Lput(ld.R_ARM_JUMP24 | uint32(elfsym)<<8)
285 ld.Thearch.Lput(ld.R_ARM_TLS_LE32 | uint32(elfsym)<<8)
288 ld.Thearch.Lput(ld.R_ARM_TLS_IE32 | uint32(elfsym)<<8)
292 ld.Thearch.Lput(ld.R_ARM_GOT_PREL | uint32(elfsym)<<8)
302 plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
303 got := ld.Linklookup(ld.Ctxt, ".got.plt", 0)
305 // str lr, [sp, #-4]!
306 ld.Adduint32(ld.Ctxt, plt, 0xe52de004)
309 ld.Adduint32(ld.Ctxt, plt, 0xe59fe004)
312 ld.Adduint32(ld.Ctxt, plt, 0xe08fe00e)
315 ld.Adduint32(ld.Ctxt, plt, 0xe5bef008)
317 // .word &GLOBAL_OFFSET_TABLE[0] - .
318 ld.Addpcrelplus(ld.Ctxt, plt, got, 4)
320 // the first .plt entry requires 3 .plt.got entries
321 ld.Adduint32(ld.Ctxt, got, 0)
323 ld.Adduint32(ld.Ctxt, got, 0)
324 ld.Adduint32(ld.Ctxt, got, 0)
328 func machoreloc1(r *ld.Reloc, sectoff int64) int {
333 if r.Type == obj.R_PCREL {
334 if rs.Type == obj.SHOSTOBJ {
335 ld.Diag("pc-relative relocation of external symbol is not supported")
342 // emit a pair of "scattered" relocations that
343 // resolve to the difference of section addresses of
344 // the symbol and the instruction
345 // this value is added to the field being relocated
346 o1 := uint32(sectoff)
347 o1 |= 1 << 31 // scattered bit
348 o1 |= ld.MACHO_ARM_RELOC_SECTDIFF << 24
349 o1 |= 2 << 28 // size = 4
352 o2 |= 1 << 31 // scattered bit
353 o2 |= ld.MACHO_ARM_RELOC_PAIR << 24
354 o2 |= 2 << 28 // size = 4
357 ld.Thearch.Lput(uint32(ld.Symaddr(rs)))
359 ld.Thearch.Lput(uint32(ld.Ctxt.Cursym.Value + int64(r.Off)))
363 if rs.Type == obj.SHOSTOBJ || r.Type == obj.R_CALLARM {
365 ld.Diag("reloc %d to non-macho symbol %s type=%d", r.Type, rs.Name, rs.Type)
370 v |= 1 << 27 // external relocation
372 v = uint32(rs.Sect.Extnum)
374 ld.Diag("reloc %d to symbol %s in non-macho section %s type=%d", r.Type, rs.Name, rs.Sect.Name, rs.Type)
384 v |= ld.MACHO_GENERIC_RELOC_VANILLA << 28
387 v |= 1 << 24 // pc-relative bit
388 v |= ld.MACHO_ARM_RELOC_BR24 << 28
408 ld.Thearch.Lput(uint32(sectoff))
413 func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int {
414 if ld.Linkmode == ld.LinkExternal {
419 // set up addend for eventual relocation via outer symbol.
423 if r.Xadd&0x800000 != 0 {
427 for rs.Outer != nil {
428 r.Xadd += ld.Symaddr(rs) - ld.Symaddr(rs.Outer)
432 if rs.Type != obj.SHOSTOBJ && rs.Type != obj.SDYNIMPORT && rs.Sect == nil {
433 ld.Diag("missing section for %s", rs.Name)
437 // ld64 for arm seems to want the symbol table to contain offset
438 // into the section rather than pseudo virtual address that contains
439 // the section load address.
440 // we need to compensate that by removing the instruction's address
442 if ld.HEADTYPE == obj.Hdarwin {
443 r.Xadd -= ld.Symaddr(s) + int64(r.Off)
446 *val = int64(braddoff(int32(0xff000000&uint32(r.Add)), int32(0xffffff&uint32(r.Xadd/4))))
459 *val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ld.Linklookup(ld.Ctxt, ".got", 0))
462 // The following three arch specific relocations are only for generation of
463 // Linux/ARM ELF's PLT entry (3 assembler instruction)
464 case obj.R_PLT0: // add ip, pc, #0xXX00000
465 if ld.Symaddr(ld.Linklookup(ld.Ctxt, ".got.plt", 0)) < ld.Symaddr(ld.Linklookup(ld.Ctxt, ".plt", 0)) {
466 ld.Diag(".got.plt should be placed after .plt section.")
468 *val = 0xe28fc600 + (0xff & (int64(uint32(ld.Symaddr(r.Sym)-(ld.Symaddr(ld.Linklookup(ld.Ctxt, ".plt", 0))+int64(r.Off))+r.Add)) >> 20))
471 case obj.R_PLT1: // add ip, ip, #0xYY000
472 *val = 0xe28cca00 + (0xff & (int64(uint32(ld.Symaddr(r.Sym)-(ld.Symaddr(ld.Linklookup(ld.Ctxt, ".plt", 0))+int64(r.Off))+r.Add+4)) >> 12))
476 case obj.R_PLT2: // ldr pc, [ip, #0xZZZ]!
477 *val = 0xe5bcf000 + (0xfff & int64(uint32(ld.Symaddr(r.Sym)-(ld.Symaddr(ld.Linklookup(ld.Ctxt, ".plt", 0))+int64(r.Off))+r.Add+8)))
481 case obj.R_CALLARM: // bl XXXXXX or b YYYYYY
482 *val = int64(braddoff(int32(0xff000000&uint32(r.Add)), int32(0xffffff&uint32((ld.Symaddr(r.Sym)+int64((uint32(r.Add))*4)-(s.Value+int64(r.Off)))/4))))
490 func archrelocvariant(r *ld.Reloc, s *ld.LSym, t int64) int64 {
491 log.Fatalf("unexpected relocation variant")
495 func addpltreloc(ctxt *ld.Link, plt *ld.LSym, got *ld.LSym, sym *ld.LSym, typ int) *ld.Reloc {
498 r.Off = int32(plt.Size)
501 r.Add = int64(sym.Got) - 8
503 plt.Attr |= ld.AttrReachable
505 ld.Symgrow(ctxt, plt, plt.Size)
510 func addpltsym(ctxt *ld.Link, s *ld.LSym) {
515 ld.Adddynsym(ctxt, s)
518 plt := ld.Linklookup(ctxt, ".plt", 0)
519 got := ld.Linklookup(ctxt, ".got.plt", 0)
520 rel := ld.Linklookup(ctxt, ".rel.plt", 0)
526 s.Got = int32(got.Size)
528 // In theory, all GOT should point to the first PLT entry,
529 // Linux/ARM's dynamic linker will do that for us, but FreeBSD/ARM's
530 // dynamic linker won't, so we'd better do it ourselves.
531 ld.Addaddrplus(ctxt, got, plt, 0)
533 // .plt entry, this depends on the .got entry
534 s.Plt = int32(plt.Size)
536 addpltreloc(ctxt, plt, got, s, obj.R_PLT0) // add lr, pc, #0xXX00000
537 addpltreloc(ctxt, plt, got, s, obj.R_PLT1) // add lr, lr, #0xYY000
538 addpltreloc(ctxt, plt, got, s, obj.R_PLT2) // ldr pc, [lr, #0xZZZ]!
541 ld.Addaddrplus(ctxt, rel, got, int64(s.Got))
543 ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(s.Dynid), ld.R_ARM_JUMP_SLOT))
545 ld.Diag("addpltsym: unsupported binary format")
549 func addgotsyminternal(ctxt *ld.Link, s *ld.LSym) {
554 got := ld.Linklookup(ctxt, ".got", 0)
555 s.Got = int32(got.Size)
557 ld.Addaddrplus(ctxt, got, s, 0)
561 ld.Diag("addgotsyminternal: unsupported binary format")
565 func addgotsym(ctxt *ld.Link, s *ld.LSym) {
570 ld.Adddynsym(ctxt, s)
571 got := ld.Linklookup(ctxt, ".got", 0)
572 s.Got = int32(got.Size)
573 ld.Adduint32(ctxt, got, 0)
576 rel := ld.Linklookup(ctxt, ".rel", 0)
577 ld.Addaddrplus(ctxt, rel, got, int64(s.Got))
578 ld.Adduint32(ctxt, rel, ld.ELF32_R_INFO(uint32(s.Dynid), ld.R_ARM_GLOB_DAT))
580 ld.Diag("addgotsym: unsupported binary format")
585 if ld.Debug['v'] != 0 {
586 fmt.Fprintf(ld.Bso, "%5.2f asmb\n", obj.Cputime())
594 sect := ld.Segtext.Sect
595 ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
596 ld.Codeblk(int64(sect.Vaddr), int64(sect.Length))
597 for sect = sect.Next; sect != nil; sect = sect.Next {
598 ld.Cseek(int64(sect.Vaddr - ld.Segtext.Vaddr + ld.Segtext.Fileoff))
599 ld.Datblk(int64(sect.Vaddr), int64(sect.Length))
602 if ld.Segrodata.Filelen > 0 {
603 if ld.Debug['v'] != 0 {
604 fmt.Fprintf(ld.Bso, "%5.2f rodatblk\n", obj.Cputime())
608 ld.Cseek(int64(ld.Segrodata.Fileoff))
609 ld.Datblk(int64(ld.Segrodata.Vaddr), int64(ld.Segrodata.Filelen))
612 if ld.Debug['v'] != 0 {
613 fmt.Fprintf(ld.Bso, "%5.2f datblk\n", obj.Cputime())
617 ld.Cseek(int64(ld.Segdata.Fileoff))
618 ld.Datblk(int64(ld.Segdata.Vaddr), int64(ld.Segdata.Filelen))
620 ld.Cseek(int64(ld.Segdwarf.Fileoff))
621 ld.Dwarfblk(int64(ld.Segdwarf.Vaddr), int64(ld.Segdwarf.Filelen))
623 machlink := uint32(0)
624 if ld.HEADTYPE == obj.Hdarwin {
625 machlink = uint32(ld.Domacholink())
628 /* output symbol table */
633 if ld.Debug['s'] == 0 {
635 if ld.Debug['v'] != 0 {
636 fmt.Fprintf(ld.Bso, "%5.2f sym\n", obj.Cputime())
642 symo = uint32(ld.Segdwarf.Fileoff + ld.Segdwarf.Filelen)
643 symo = uint32(ld.Rnd(int64(symo), int64(ld.INITRND)))
647 symo = uint32(ld.Segdata.Fileoff + ld.Segdata.Filelen)
650 symo = uint32(ld.Segdwarf.Fileoff + uint64(ld.Rnd(int64(ld.Segdwarf.Filelen), int64(ld.INITRND))) + uint64(machlink))
653 ld.Cseek(int64(symo))
657 if ld.Debug['v'] != 0 {
658 fmt.Fprintf(ld.Bso, "%5.2f elfsym\n", obj.Cputime())
662 ld.Cwrite(ld.Elfstrdat)
664 if ld.Linkmode == ld.LinkExternal {
673 sym := ld.Linklookup(ld.Ctxt, "pclntab", 0)
675 ld.Lcsize = int32(len(sym.P))
676 for i := 0; int32(i) < ld.Lcsize; i++ {
684 if ld.Linkmode == ld.LinkExternal {
691 if ld.Debug['v'] != 0 {
692 fmt.Fprintf(ld.Bso, "%5.2f header\n", obj.Cputime())
698 case obj.Hplan9: /* plan 9 */
699 ld.Lputb(0x647) /* magic */
700 ld.Lputb(uint32(ld.Segtext.Filelen)) /* sizes */
701 ld.Lputb(uint32(ld.Segdata.Filelen))
702 ld.Lputb(uint32(ld.Segdata.Length - ld.Segdata.Filelen))
703 ld.Lputb(uint32(ld.Symsize)) /* nsyms */
704 ld.Lputb(uint32(ld.Entryvalue())) /* va of entry */
706 ld.Lputb(uint32(ld.Lcsize))
713 ld.Asmbelf(int64(symo))
720 if ld.Debug['c'] != 0 {
721 fmt.Printf("textsize=%d\n", ld.Segtext.Filelen)
722 fmt.Printf("datsize=%d\n", ld.Segdata.Filelen)
723 fmt.Printf("bsssize=%d\n", ld.Segdata.Length-ld.Segdata.Filelen)
724 fmt.Printf("symsize=%d\n", ld.Symsize)
725 fmt.Printf("lcsize=%d\n", ld.Lcsize)
726 fmt.Printf("total=%d\n", ld.Segtext.Filelen+ld.Segdata.Length+uint64(ld.Symsize)+uint64(ld.Lcsize))