]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/7l: add the ARM64 linker
authorAram Hăvărneanu <aram@mgk.ro>
Sun, 8 Mar 2015 13:14:53 +0000 (14:14 +0100)
committerAram Hăvărneanu <aram@mgk.ro>
Mon, 16 Mar 2015 18:45:16 +0000 (18:45 +0000)
Only internal linking without cgo is supported for now.

Change-Id: I91eb1572c1ccc805db62fc4c29080df98797d51a
Reviewed-on: https://go-review.googlesource.com/7048
Reviewed-by: Minux Ma <minux@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/cmd/7l/asm.go
src/cmd/7l/l.go
src/cmd/7l/obj.go
src/cmd/internal/ld/arch.go
src/cmd/internal/ld/elf.go
src/cmd/internal/ld/ldelf.go
src/cmd/internal/ld/lib.go
src/cmd/internal/ld/link.go
src/cmd/internal/ld/symtab.go

index 2b6cdad2082e19ad008219089e709a9eb9e3d023..d597acecbe1ade5dd1042ec5b15f285c9f947857 100644 (file)
@@ -33,11 +33,12 @@ package main
 import (
        "cmd/internal/ld"
        "cmd/internal/obj"
-       "encoding/binary"
        "fmt"
        "log"
 )
 
+func gentext() {}
+
 func needlib(name string) int {
        if name[0] == '\x00' {
                return 0
@@ -56,258 +57,12 @@ func needlib(name string) int {
        return 0
 }
 
-func gentext() {
-       var s *ld.LSym
-       var stub *ld.LSym
-       var pprevtextp **ld.LSym
-       var r *ld.Reloc
-       var n string
-       var o1 uint32
-       var i int
-
-       // The ppc64 ABI PLT has similar concepts to other
-       // architectures, but is laid out quite differently.  When we
-       // see an R_PPC64_REL24 relocation to a dynamic symbol
-       // (indicating that the call needs to go through the PLT), we
-       // generate up to three stubs and reserve a PLT slot.
-       //
-       // 1) The call site will be bl x; nop (where the relocation
-       //    applies to the bl).  We rewrite this to bl x_stub; ld
-       //    r2,24(r1).  The ld is necessary because x_stub will save
-       //    r2 (the TOC pointer) at 24(r1) (the "TOC save slot").
-       //
-       // 2) We reserve space for a pointer in the .plt section (once
-       //    per referenced dynamic function).  .plt is a data
-       //    section filled solely by the dynamic linker (more like
-       //    .plt.got on other architectures).  Initially, the
-       //    dynamic linker will fill each slot with a pointer to the
-       //    corresponding x@plt entry point.
-       //
-       // 3) We generate the "call stub" x_stub (once per dynamic
-       //    function/object file pair).  This saves the TOC in the
-       //    TOC save slot, reads the function pointer from x's .plt
-       //    slot and calls it like any other global entry point
-       //    (including setting r12 to the function address).
-       //
-       // 4) We generate the "symbol resolver stub" x@plt (once per
-       //    dynamic function).  This is solely a branch to the glink
-       //    resolver stub.
-       //
-       // 5) We generate the glink resolver stub (only once).  This
-       //    computes which symbol resolver stub we came through and
-       //    invokes the dynamic resolver via a pointer provided by
-       //    the dynamic linker.  This will patch up the .plt slot to
-       //    point directly at the function so future calls go
-       //    straight from the call stub to the real function, and
-       //    then call the function.
-
-       // NOTE: It's possible we could make ppc64 closer to other
-       // architectures: ppc64's .plt is like .plt.got on other
-       // platforms and ppc64's .glink is like .plt on other
-       // platforms.
-
-       // Find all R_PPC64_REL24 relocations that reference dynamic
-       // imports.  Reserve PLT entries for these symbols and
-       // generate call stubs.  The call stubs need to live in .text,
-       // which is why we need to do this pass this early.
-       //
-       // This assumes "case 1" from the ABI, where the caller needs
-       // us to save and restore the TOC pointer.
-       pprevtextp = &ld.Ctxt.Textp
-
-       for s = *pprevtextp; s != nil; (func() { pprevtextp = &s.Next; s = *pprevtextp })() {
-               for i = range s.R {
-                       r = &s.R[i]
-                       if r.Type != 256+ld.R_PPC64_REL24 || r.Sym.Type != ld.SDYNIMPORT {
-                               continue
-                       }
-
-                       // Reserve PLT entry and generate symbol
-                       // resolver
-                       addpltsym(ld.Ctxt, r.Sym)
-
-                       // Generate call stub
-                       n = fmt.Sprintf("%s.%s", s.Name, r.Sym.Name)
-
-                       stub = ld.Linklookup(ld.Ctxt, n, 0)
-                       stub.Reachable = stub.Reachable || s.Reachable
-                       if stub.Size == 0 {
-                               // Need outer to resolve .TOC.
-                               stub.Outer = s
-
-                               // Link in to textp before s (we could
-                               // do it after, but would have to skip
-                               // the subsymbols)
-                               *pprevtextp = stub
-
-                               stub.Next = s
-                               pprevtextp = &stub.Next
-
-                               gencallstub(1, stub, r.Sym)
-                       }
-
-                       // Update the relocation to use the call stub
-                       r.Sym = stub
-
-                       // Restore TOC after bl.  The compiler put a
-                       // nop here for us to overwrite.
-                       o1 = 0xe8410018 // ld r2,24(r1)
-                       ld.Ctxt.Arch.ByteOrder.PutUint32(s.P[r.Off+4:], o1)
-               }
-       }
-}
-
-// Construct a call stub in stub that calls symbol targ via its PLT
-// entry.
-func gencallstub(abicase int, stub *ld.LSym, targ *ld.LSym) {
-       if abicase != 1 {
-               // If we see R_PPC64_TOCSAVE or R_PPC64_REL24_NOTOC
-               // relocations, we'll need to implement cases 2 and 3.
-               log.Fatalf("gencallstub only implements case 1 calls")
-       }
-
-       plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
-
-       stub.Type = ld.STEXT
-
-       // Save TOC pointer in TOC save slot
-       ld.Adduint32(ld.Ctxt, stub, 0xf8410018) // std r2,24(r1)
-
-       // Load the function pointer from the PLT.
-       r := ld.Addrel(stub)
-
-       r.Off = int32(stub.Size)
-       r.Sym = plt
-       r.Add = int64(targ.Plt)
-       r.Siz = 2
-       if ld.Ctxt.Arch.ByteOrder == binary.BigEndian {
-               r.Off += int32(r.Siz)
-       }
-       r.Type = ld.R_POWER_TOC
-       r.Variant = ld.RV_POWER_HA
-       ld.Adduint32(ld.Ctxt, stub, 0x3d820000) // addis r12,r2,targ@plt@toc@ha
-       r = ld.Addrel(stub)
-       r.Off = int32(stub.Size)
-       r.Sym = plt
-       r.Add = int64(targ.Plt)
-       r.Siz = 2
-       if ld.Ctxt.Arch.ByteOrder == binary.BigEndian {
-               r.Off += int32(r.Siz)
-       }
-       r.Type = ld.R_POWER_TOC
-       r.Variant = ld.RV_POWER_LO
-       ld.Adduint32(ld.Ctxt, stub, 0xe98c0000) // ld r12,targ@plt@toc@l(r12)
-
-       // Jump to the loaded pointer
-       ld.Adduint32(ld.Ctxt, stub, 0x7d8903a6) // mtctr r12
-       ld.Adduint32(ld.Ctxt, stub, 0x4e800420) // bctr
-}
-
 func adddynrela(rel *ld.LSym, s *ld.LSym, r *ld.Reloc) {
        log.Fatalf("adddynrela not implemented")
 }
 
 func adddynrel(s *ld.LSym, r *ld.Reloc) {
-       targ := r.Sym
-       ld.Ctxt.Cursym = s
-
-       switch r.Type {
-       default:
-               if r.Type >= 256 {
-                       ld.Diag("unexpected relocation type %d", r.Type)
-                       return
-               }
-
-               // Handle relocations found in ELF object files.
-       case 256 + ld.R_PPC64_REL24:
-               r.Type = ld.R_CALLPOWER
-
-               // This is a local call, so the caller isn't setting
-               // up r12 and r2 is the same for the caller and
-               // callee.  Hence, we need to go to the local entry
-               // point.  (If we don't do this, the callee will try
-               // to use r12 to compute r2.)
-               r.Add += int64(r.Sym.Localentry) * 4
-
-               if targ.Type == ld.SDYNIMPORT {
-                       // Should have been handled in elfsetupplt
-                       ld.Diag("unexpected R_PPC64_REL24 for dyn import")
-               }
-
-               return
-
-       case 256 + ld.R_PPC64_ADDR64:
-               r.Type = ld.R_ADDR
-               if targ.Type == ld.SDYNIMPORT {
-                       // These happen in .toc sections
-                       adddynsym(ld.Ctxt, targ)
-
-                       rela := ld.Linklookup(ld.Ctxt, ".rela", 0)
-                       ld.Addaddrplus(ld.Ctxt, rela, s, int64(r.Off))
-                       ld.Adduint64(ld.Ctxt, rela, ld.ELF64_R_INFO(uint32(targ.Dynid), ld.R_PPC64_ADDR64))
-                       ld.Adduint64(ld.Ctxt, rela, uint64(r.Add))
-                       r.Type = 256 // ignore during relocsym
-               }
-
-               return
-
-       case 256 + ld.R_PPC64_TOC16:
-               r.Type = ld.R_POWER_TOC
-               r.Variant = ld.RV_POWER_LO | ld.RV_CHECK_OVERFLOW
-               return
-
-       case 256 + ld.R_PPC64_TOC16_LO:
-               r.Type = ld.R_POWER_TOC
-               r.Variant = ld.RV_POWER_LO
-               return
-
-       case 256 + ld.R_PPC64_TOC16_HA:
-               r.Type = ld.R_POWER_TOC
-               r.Variant = ld.RV_POWER_HA | ld.RV_CHECK_OVERFLOW
-               return
-
-       case 256 + ld.R_PPC64_TOC16_HI:
-               r.Type = ld.R_POWER_TOC
-               r.Variant = ld.RV_POWER_HI | ld.RV_CHECK_OVERFLOW
-               return
-
-       case 256 + ld.R_PPC64_TOC16_DS:
-               r.Type = ld.R_POWER_TOC
-               r.Variant = ld.RV_POWER_DS | ld.RV_CHECK_OVERFLOW
-               return
-
-       case 256 + ld.R_PPC64_TOC16_LO_DS:
-               r.Type = ld.R_POWER_TOC
-               r.Variant = ld.RV_POWER_DS
-               return
-
-       case 256 + ld.R_PPC64_REL16_LO:
-               r.Type = ld.R_PCREL
-               r.Variant = ld.RV_POWER_LO
-               r.Add += 2 // Compensate for relocation size of 2
-               return
-
-       case 256 + ld.R_PPC64_REL16_HI:
-               r.Type = ld.R_PCREL
-               r.Variant = ld.RV_POWER_HI | ld.RV_CHECK_OVERFLOW
-               r.Add += 2
-               return
-
-       case 256 + ld.R_PPC64_REL16_HA:
-               r.Type = ld.R_PCREL
-               r.Variant = ld.RV_POWER_HA | ld.RV_CHECK_OVERFLOW
-               r.Add += 2
-               return
-       }
-
-       // Handle references to ELF symbols from our own object files.
-       if targ.Type != ld.SDYNIMPORT {
-               return
-       }
-
-       // TODO(austin): Translate our relocations to ELF
-
-       ld.Diag("unsupported relocation for dynamic symbol %s (type=%d stype=%d)", targ.Name, r.Type, targ.Type)
+       log.Fatalf("adddynrel not implemented")
 }
 
 func elfreloc1(r *ld.Reloc, sectoff int64) int {
@@ -316,43 +71,17 @@ func elfreloc1(r *ld.Reloc, sectoff int64) int {
 }
 
 func elfsetupplt() {
-       plt := ld.Linklookup(ld.Ctxt, ".plt", 0)
-       if plt.Size == 0 {
-               // The dynamic linker stores the address of the
-               // dynamic resolver and the DSO identifier in the two
-               // doublewords at the beginning of the .plt section
-               // before the PLT array.  Reserve space for these.
-               plt.Size = 16
-       }
+       // TODO(aram)
+       return
 }
 
 func machoreloc1(r *ld.Reloc, sectoff int64) int {
        return -1
 }
 
-// Return the value of .TOC. for symbol s
-func symtoc(s *ld.LSym) int64 {
-       var toc *ld.LSym
-
-       if s.Outer != nil {
-               toc = ld.Linkrlookup(ld.Ctxt, ".TOC.", int(s.Outer.Version))
-       } else {
-               toc = ld.Linkrlookup(ld.Ctxt, ".TOC.", int(s.Version))
-       }
-
-       if toc == nil {
-               ld.Diag("TOC-relative relocation in object without .TOC.")
-               return 0
-       }
-
-       return toc.Value
-}
-
 func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int {
        if ld.Linkmode == ld.LinkExternal {
-               // TODO(minux): translate R_ADDRPOWER and R_CALLPOWER into standard ELF relocations.
-               // R_ADDRPOWER corresponds to R_PPC_ADDR16_HA and R_PPC_ADDR16_LO.
-               // R_CALLPOWER corresponds to R_PPC_REL24.
+               // TODO(minux): translate R_CALLARM64 into standard ELF relocation.
                return -1
        }
 
@@ -365,59 +94,8 @@ func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int {
                *val = ld.Symaddr(r.Sym) + r.Add - ld.Symaddr(ld.Linklookup(ld.Ctxt, ".got", 0))
                return 0
 
-       case ld.R_ADDRPOWER:
-               // r->add is two ppc64 instructions holding an immediate 32-bit constant.
-               // We want to add r->sym's address to that constant.
-               // The encoding of the immediate x<<16 + y,
-               // where x is the low 16 bits of the first instruction and y is the low 16
-               // bits of the second. Both x and y are signed (int16, not uint16).
-               o1 := uint32(r.Add >> 32)
-               o2 := uint32(r.Add)
-               t := ld.Symaddr(r.Sym)
-               if t < 0 {
-                       ld.Ctxt.Diag("relocation for %s is too big (>=2G): %d", s.Name, ld.Symaddr(r.Sym))
-               }
-
-               t += int64((o1&0xffff)<<16 + uint32(int32(o2)<<16>>16))
-               if t&0x8000 != 0 {
-                       t += 0x10000
-               }
-               o1 = o1&0xffff0000 | (uint32(t)>>16)&0xffff
-               o2 = o2&0xffff0000 | uint32(t)&0xffff
-
-               // when laid out, the instruction order must always be o1, o2.
-               if ld.Ctxt.Arch.ByteOrder == binary.BigEndian {
-                       *val = int64(o1)<<32 | int64(o2)
-               } else {
-                       *val = int64(o2)<<32 | int64(o1)
-               }
-               return 0
-
-       case ld.R_CALLPOWER:
-               // Bits 6 through 29 = (S + A - P) >> 2
-               var o1 uint32
-               if ld.Ctxt.Arch.ByteOrder == binary.BigEndian {
-                       o1 = ld.Be32(s.P[r.Off:])
-               } else {
-                       o1 = ld.Le32(s.P[r.Off:])
-               }
-
-               t := ld.Symaddr(r.Sym) + r.Add - (s.Value + int64(r.Off))
-               if t&3 != 0 {
-                       ld.Ctxt.Diag("relocation for %s+%d is not aligned: %d", r.Sym.Name, r.Off, t)
-               }
-               if int64(int32(t<<6)>>6) != t {
-                       // TODO(austin) This can happen if text > 32M.
-                       // Add a call trampoline to .text in that case.
-                       ld.Ctxt.Diag("relocation for %s+%d is too big: %d", r.Sym.Name, r.Off, t)
-               }
-
-               *val = int64(o1&0xfc000003 | uint32(t)&^0xfc000003)
-               return 0
-
-       case ld.R_POWER_TOC: // S + A - .TOC.
-               *val = ld.Symaddr(r.Sym) + r.Add - symtoc(s)
-
+       case ld.R_CALLARM64:
+               *val = int64((0xfc000000 & uint32(r.Add)) | uint32((ld.Symaddr(r.Sym)+r.Add*4-(s.Value+int64(r.Off)))/4))
                return 0
        }
 
@@ -425,249 +103,12 @@ func archreloc(r *ld.Reloc, s *ld.LSym, val *int64) int {
 }
 
 func archrelocvariant(r *ld.Reloc, s *ld.LSym, t int64) int64 {
-       switch r.Variant & ld.RV_TYPE_MASK {
-       default:
-               ld.Diag("unexpected relocation variant %d", r.Variant)
-               fallthrough
-
-       case ld.RV_NONE:
-               return t
-
-       case ld.RV_POWER_LO:
-               if r.Variant&ld.RV_CHECK_OVERFLOW != 0 {
-                       // Whether to check for signed or unsigned
-                       // overflow depends on the instruction
-                       var o1 uint32
-                       if ld.Ctxt.Arch.ByteOrder == binary.BigEndian {
-                               o1 = ld.Be32(s.P[r.Off-2:])
-                       } else {
-                               o1 = ld.Le32(s.P[r.Off:])
-                       }
-                       switch o1 >> 26 {
-                       case 24, // ori
-                               26, // xori
-                               28: // andi
-                               if t>>16 != 0 {
-                                       goto overflow
-                               }
-
-                       default:
-                               if int64(int16(t)) != t {
-                                       goto overflow
-                               }
-                       }
-               }
-
-               return int64(int16(t))
-
-       case ld.RV_POWER_HA:
-               t += 0x8000
-               fallthrough
-
-               // Fallthrough
-       case ld.RV_POWER_HI:
-               t >>= 16
-
-               if r.Variant&ld.RV_CHECK_OVERFLOW != 0 {
-                       // Whether to check for signed or unsigned
-                       // overflow depends on the instruction
-                       var o1 uint32
-                       if ld.Ctxt.Arch.ByteOrder == binary.BigEndian {
-                               o1 = ld.Be32(s.P[r.Off-2:])
-                       } else {
-                               o1 = ld.Le32(s.P[r.Off:])
-                       }
-                       switch o1 >> 26 {
-                       case 25, // oris
-                               27, // xoris
-                               29: // andis
-                               if t>>16 != 0 {
-                                       goto overflow
-                               }
-
-                       default:
-                               if int64(int16(t)) != t {
-                                       goto overflow
-                               }
-                       }
-               }
-
-               return int64(int16(t))
-
-       case ld.RV_POWER_DS:
-               var o1 uint32
-               if ld.Ctxt.Arch.ByteOrder == binary.BigEndian {
-                       o1 = uint32(ld.Be16(s.P[r.Off:]))
-               } else {
-                       o1 = uint32(ld.Le16(s.P[r.Off:]))
-               }
-               if t&3 != 0 {
-                       ld.Diag("relocation for %s+%d is not aligned: %d", r.Sym.Name, r.Off, t)
-               }
-               if (r.Variant&ld.RV_CHECK_OVERFLOW != 0) && int64(int16(t)) != t {
-                       goto overflow
-               }
-               return int64(o1)&0x3 | int64(int16(t))
-       }
-
-overflow:
-       ld.Diag("relocation for %s+%d is too big: %d", r.Sym.Name, r.Off, t)
-       return t
-}
-
-func addpltsym(ctxt *ld.Link, s *ld.LSym) {
-       if s.Plt >= 0 {
-               return
-       }
-
-       adddynsym(ctxt, s)
-
-       if ld.Iself {
-               plt := ld.Linklookup(ctxt, ".plt", 0)
-               rela := ld.Linklookup(ctxt, ".rela.plt", 0)
-               if plt.Size == 0 {
-                       elfsetupplt()
-               }
-
-               // Create the glink resolver if necessary
-               glink := ensureglinkresolver()
-
-               // Write symbol resolver stub (just a branch to the
-               // glink resolver stub)
-               r := ld.Addrel(glink)
-
-               r.Sym = glink
-               r.Off = int32(glink.Size)
-               r.Siz = 4
-               r.Type = ld.R_CALLPOWER
-               ld.Adduint32(ctxt, glink, 0x48000000) // b .glink
-
-               // In the ppc64 ABI, the dynamic linker is responsible
-               // for writing the entire PLT.  We just need to
-               // reserve 8 bytes for each PLT entry and generate a
-               // JMP_SLOT dynamic relocation for it.
-               //
-               // TODO(austin): ABI v1 is different
-               s.Plt = int32(plt.Size)
-
-               plt.Size += 8
-
-               ld.Addaddrplus(ctxt, rela, plt, int64(s.Plt))
-               ld.Adduint64(ctxt, rela, ld.ELF64_R_INFO(uint32(s.Dynid), ld.R_PPC64_JMP_SLOT))
-               ld.Adduint64(ctxt, rela, 0)
-       } else {
-               ld.Diag("addpltsym: unsupported binary format")
-       }
-}
-
-// Generate the glink resolver stub if necessary and return the .glink section
-func ensureglinkresolver() *ld.LSym {
-       glink := ld.Linklookup(ld.Ctxt, ".glink", 0)
-       if glink.Size != 0 {
-               return glink
-       }
-
-       // This is essentially the resolver from the ppc64 ELF ABI.
-       // At entry, r12 holds the address of the symbol resolver stub
-       // for the target routine and the argument registers hold the
-       // arguments for the target routine.
-       //
-       // This stub is PIC, so first get the PC of label 1 into r11.
-       // Other things will be relative to this.
-       ld.Adduint32(ld.Ctxt, glink, 0x7c0802a6) // mflr r0
-       ld.Adduint32(ld.Ctxt, glink, 0x429f0005) // bcl 20,31,1f
-       ld.Adduint32(ld.Ctxt, glink, 0x7d6802a6) // 1: mflr r11
-       ld.Adduint32(ld.Ctxt, glink, 0x7c0803a6) // mtlf r0
-
-       // Compute the .plt array index from the entry point address.
-       // Because this is PIC, everything is relative to label 1b (in
-       // r11):
-       //   r0 = ((r12 - r11) - (res_0 - r11)) / 4 = (r12 - res_0) / 4
-       ld.Adduint32(ld.Ctxt, glink, 0x3800ffd0) // li r0,-(res_0-1b)=-48
-       ld.Adduint32(ld.Ctxt, glink, 0x7c006214) // add r0,r0,r12
-       ld.Adduint32(ld.Ctxt, glink, 0x7c0b0050) // sub r0,r0,r11
-       ld.Adduint32(ld.Ctxt, glink, 0x7800f082) // srdi r0,r0,2
-
-       // r11 = address of the first byte of the PLT
-       r := ld.Addrel(glink)
-
-       r.Off = int32(glink.Size)
-       r.Sym = ld.Linklookup(ld.Ctxt, ".plt", 0)
-       r.Siz = 8
-       r.Type = ld.R_ADDRPOWER
-
-       // addis r11,0,.plt@ha; addi r11,r11,.plt@l
-       r.Add = 0x3d600000<<32 | 0x396b0000
-
-       glink.Size += 8
-
-       // Load r12 = dynamic resolver address and r11 = DSO
-       // identifier from the first two doublewords of the PLT.
-       ld.Adduint32(ld.Ctxt, glink, 0xe98b0000) // ld r12,0(r11)
-       ld.Adduint32(ld.Ctxt, glink, 0xe96b0008) // ld r11,8(r11)
-
-       // Jump to the dynamic resolver
-       ld.Adduint32(ld.Ctxt, glink, 0x7d8903a6) // mtctr r12
-       ld.Adduint32(ld.Ctxt, glink, 0x4e800420) // bctr
-
-       // The symbol resolvers must immediately follow.
-       //   res_0:
-
-       // Add DT_PPC64_GLINK .dynamic entry, which points to 32 bytes
-       // before the first symbol resolver stub.
-       s := ld.Linklookup(ld.Ctxt, ".dynamic", 0)
-
-       ld.Elfwritedynentsymplus(s, ld.DT_PPC64_GLINK, glink, glink.Size-32)
-
-       return glink
+       log.Fatalf("unexpected relocation variant")
+       return -1
 }
 
 func adddynsym(ctxt *ld.Link, s *ld.LSym) {
-       if s.Dynid >= 0 {
-               return
-       }
-
-       if ld.Iself {
-               s.Dynid = int32(ld.Nelfsym)
-               ld.Nelfsym++
-
-               d := ld.Linklookup(ctxt, ".dynsym", 0)
-
-               name := s.Extname
-               ld.Adduint32(ctxt, d, uint32(ld.Addstring(ld.Linklookup(ctxt, ".dynstr", 0), name)))
-
-               /* type */
-               t := ld.STB_GLOBAL << 4
-
-               if s.Cgoexport != 0 && s.Type&ld.SMASK == ld.STEXT {
-                       t |= ld.STT_FUNC
-               } else {
-                       t |= ld.STT_OBJECT
-               }
-               ld.Adduint8(ctxt, d, uint8(t))
-
-               /* reserved */
-               ld.Adduint8(ctxt, d, 0)
-
-               /* section where symbol is defined */
-               if s.Type == ld.SDYNIMPORT {
-                       ld.Adduint16(ctxt, d, ld.SHN_UNDEF)
-               } else {
-                       ld.Adduint16(ctxt, d, 1)
-               }
-
-               /* value */
-               if s.Type == ld.SDYNIMPORT {
-                       ld.Adduint64(ctxt, d, 0)
-               } else {
-                       ld.Addaddr(ctxt, d, s)
-               }
-
-               /* size of object */
-               ld.Adduint64(ctxt, d, uint64(s.Size))
-       } else {
-               ld.Diag("adddynsym: unsupported binary format")
-       }
+       log.Fatalf("adddynsym not implemented")
 }
 
 func adddynlib(lib string) {
index e7dc102af23e3173891785bd861907a63bda77f8..6f90acb1076543066b0a1e800150557af6d70091 100644 (file)
@@ -62,7 +62,7 @@ package main
 // THE SOFTWARE.
 
 const (
-       thechar   = '9'
+       thechar   = '7'
        PtrSize   = 8
        IntSize   = 8
        RegSize   = 8
@@ -73,5 +73,5 @@ const (
 
 /* Used by ../ld/dwarf.c */
 const (
-       DWARFREGSP = 1
+       DWARFREGSP = 31
 )
index 29b384a1a6d446e475b82aadf2a2872c486798df..3fa70f2019d02e717a990068f43d9ae4d427ee91 100644 (file)
@@ -46,11 +46,7 @@ func main() {
 
 func linkarchinit() {
        ld.Thestring = obj.Getgoarch()
-       if ld.Thestring == "ppc64le" {
-               ld.Thelinkarch = &ld.Linkppc64le
-       } else {
-               ld.Thelinkarch = &ld.Linkppc64
-       }
+       ld.Thelinkarch = &ld.Linkarm64
 
        ld.Thearch.Thechar = thechar
        ld.Thearch.Ptrsize = ld.Thelinkarch.Ptrsize
@@ -72,18 +68,11 @@ func linkarchinit() {
        ld.Thearch.Elfsetupplt = elfsetupplt
        ld.Thearch.Gentext = gentext
        ld.Thearch.Machoreloc1 = machoreloc1
-       if ld.Thelinkarch == &ld.Linkppc64le {
-               ld.Thearch.Lput = ld.Lputl
-               ld.Thearch.Wput = ld.Wputl
-               ld.Thearch.Vput = ld.Vputl
-       } else {
-               ld.Thearch.Lput = ld.Lputb
-               ld.Thearch.Wput = ld.Wputb
-               ld.Thearch.Vput = ld.Vputb
-       }
+       ld.Thearch.Lput = ld.Lputl
+       ld.Thearch.Wput = ld.Wputl
+       ld.Thearch.Vput = ld.Vputl
 
-       // TODO(austin): ABI v1 uses /usr/lib/ld.so.1
-       ld.Thearch.Linuxdynld = "/lib64/ld64.so.1"
+       ld.Thearch.Linuxdynld = "/lib/ld-linux-aarch64.so.1"
 
        ld.Thearch.Freebsddynld = "XXX"
        ld.Thearch.Openbsddynld = "XXX"
@@ -129,9 +118,7 @@ func archinit() {
                }
 
        case ld.Hlinux: /* ppc64 elf */
-               if ld.Thestring == "ppc64" {
-                       ld.Debug['d'] = 1 // TODO(austin): ELF ABI v1 not supported yet
-               }
+               ld.Debug['d'] = 1 // TODO(aram): dynamic linking is not supported yet.
                ld.Elfinit()
                ld.HEADR = ld.ELFRESERVE
                if ld.INITTEXT == -1 {
index aa84a957801504b07a61640e527329d7df50cb32..1b8e1b19bae3131007fe3656c9107bbafeaaa3e4 100644 (file)
@@ -15,6 +15,15 @@ var Linkarm = LinkArch{
        Regsize:   4,
 }
 
+var Linkarm64 = LinkArch{
+       ByteOrder: binary.LittleEndian,
+       Name:      "arm64",
+       Thechar:   '7',
+       Minlc:     4,
+       Ptrsize:   8,
+       Regsize:   8,
+}
+
 var Linkamd64 = LinkArch{
        ByteOrder: binary.LittleEndian,
        Name:      "amd64",
index 3fc6907d76fbedea93ad84fe76b055dbc3c39d5a..ab79cf322ed0e5127abdb1153aecf716b8c0b856 100644 (file)
@@ -164,6 +164,7 @@ const (
        EM_ST100             = 60
        EM_TINYJ             = 61
        EM_X86_64            = 62
+       EM_AARCH64           = 183
        EM_486               = 6
        EM_MIPS_RS4_BE       = 10
        EM_ALPHA_STD         = 41
@@ -344,6 +345,9 @@ const (
        R_X86_64_GOTTPOFF      = 22
        R_X86_64_TPOFF32       = 23
        R_X86_64_COUNT         = 24
+       R_AARCH64_ABS64        = 257
+       R_AARCH64_ABS32        = 258
+       R_AARCH64_CALL26       = 283
        R_ALPHA_NONE           = 0
        R_ALPHA_REFLONG        = 1
        R_ALPHA_REFQUAD        = 2
@@ -757,8 +761,7 @@ func Elfinit() {
                }
                fallthrough
 
-               // fallthrough
-       case '6':
+       case '6', '7':
                elf64 = 1
 
                ehdr.phoff = ELF64HDRSIZE      /* Must be be ELF64HDRSIZE: first PHdr must follow ELF header */
@@ -1367,14 +1370,15 @@ func elfdynhash() {
                elfwritedynentsym(s, DT_VERSYM, Linklookup(Ctxt, ".gnu.version", 0))
        }
 
-       if Thearch.Thechar == '6' || Thearch.Thechar == '9' {
+       switch Thearch.Thechar {
+       case '6', '7', '9':
                sy := Linklookup(Ctxt, ".rela.plt", 0)
                if sy.Size > 0 {
                        Elfwritedynent(s, DT_PLTREL, DT_RELA)
                        elfwritedynentsymsize(s, DT_PLTRELSZ, sy)
                        elfwritedynentsym(s, DT_JMPREL, sy)
                }
-       } else {
+       default:
                sy := Linklookup(Ctxt, ".rel.plt", 0)
                if sy.Size > 0 {
                        Elfwritedynent(s, DT_PLTREL, DT_REL)
@@ -1627,7 +1631,8 @@ func doelf() {
                Debug['s'] = 0
                Debug['d'] = 1
 
-               if Thearch.Thechar == '6' || Thearch.Thechar == '9' {
+               switch Thearch.Thechar {
+               case '6', '7', '9':
                        Addstring(shstrtab, ".rela.text")
                        Addstring(shstrtab, ".rela.rodata")
                        Addstring(shstrtab, ".rela.typelink")
@@ -1635,7 +1640,8 @@ func doelf() {
                        Addstring(shstrtab, ".rela.gopclntab")
                        Addstring(shstrtab, ".rela.noptrdata")
                        Addstring(shstrtab, ".rela.data")
-               } else {
+
+               default:
                        Addstring(shstrtab, ".rel.text")
                        Addstring(shstrtab, ".rel.rodata")
                        Addstring(shstrtab, ".rel.typelink")
@@ -1711,9 +1717,10 @@ func doelf() {
                dynstr := s
 
                /* relocation table */
-               if Thearch.Thechar == '6' || Thearch.Thechar == '9' {
+               switch Thearch.Thechar {
+               case '6', '7', '9':
                        s = Linklookup(Ctxt, ".rela", 0)
-               } else {
+               default:
                        s = Linklookup(Ctxt, ".rel", 0)
                }
                s.Reachable = true
@@ -1755,9 +1762,10 @@ func doelf() {
 
                Thearch.Elfsetupplt()
 
-               if Thearch.Thechar == '6' || Thearch.Thechar == '9' {
+               switch Thearch.Thechar {
+               case '6', '7', '9':
                        s = Linklookup(Ctxt, ".rela.plt", 0)
-               } else {
+               default:
                        s = Linklookup(Ctxt, ".rel.plt", 0)
                }
                s.Reachable = true
@@ -1790,11 +1798,12 @@ func doelf() {
                }
                elfwritedynentsym(s, DT_STRTAB, Linklookup(Ctxt, ".dynstr", 0))
                elfwritedynentsymsize(s, DT_STRSZ, Linklookup(Ctxt, ".dynstr", 0))
-               if Thearch.Thechar == '6' || Thearch.Thechar == '9' {
+               switch Thearch.Thechar {
+               case '6', '7', '9':
                        elfwritedynentsym(s, DT_RELA, Linklookup(Ctxt, ".rela", 0))
                        elfwritedynentsymsize(s, DT_RELASZ, Linklookup(Ctxt, ".rela", 0))
                        Elfwritedynent(s, DT_RELAENT, ELF64RELASIZE)
-               } else {
+               default:
                        elfwritedynentsym(s, DT_REL, Linklookup(Ctxt, ".rel", 0))
                        elfwritedynentsymsize(s, DT_RELSZ, Linklookup(Ctxt, ".rel", 0))
                        Elfwritedynent(s, DT_RELENT, ELF32RELSIZE)
@@ -1870,6 +1879,9 @@ func Asmbelf(symo int64) {
        case '6':
                eh.machine = EM_X86_64
 
+       case '7':
+               eh.machine = EM_AARCH64
+
        case '8':
                eh.machine = EM_386
 
@@ -2033,7 +2045,8 @@ func Asmbelf(symo int64) {
 
                switch eh.machine {
                case EM_X86_64,
-                       EM_PPC64:
+                       EM_PPC64,
+                       EM_AARCH64:
                        sh := elfshname(".rela.plt")
                        sh.type_ = SHT_RELA
                        sh.flags = SHF_ALLOC
index a02ec60f25cdab2d92b78ef81bb9e3097971977d..97944d73753336fd19d6cefc81edf47291bb694b 100644 (file)
@@ -84,6 +84,7 @@ const (
        ElfMachSH          = 42
        ElfMachSparc9      = 43
        ElfMachAmd64       = 62
+       ElfMachArm64       = 183
 )
 
 const (
@@ -432,6 +433,12 @@ func ldelf(f *Biobuf, pkg string, length int64, pn string) {
                        return
                }
 
+       case '7':
+               if e != binary.LittleEndian || elfobj.machine != ElfMachArm64 || hdr.Ident[4] != ElfClass64 {
+                       Diag("%s: elf object but not arm64", pn)
+                       return
+               }
+
        case '8':
                if e != binary.LittleEndian || elfobj.machine != ElfMach386 || hdr.Ident[4] != ElfClass32 {
                        Diag("%s: elf object but not 386", pn)
index 324a8d4d02eb5c7dfb0394891a30b37869ee3e4e..b4f683fe9e9159659d50d58ef456aee58162a936 100644 (file)
@@ -1064,7 +1064,7 @@ var (
 // allow stack checks here.
 
 func haslinkregister() bool {
-       return Thearch.Thechar == '5' || Thearch.Thechar == '9'
+       return Thearch.Thechar == '5' || Thearch.Thechar == '9' || Thearch.Thechar == '7'
 }
 
 func callsize() int {
@@ -1183,6 +1183,7 @@ func stkcheck(up *Chain, depth int) int {
                        // Direct call.
                        case R_CALL,
                                R_CALLARM,
+                               R_CALLARM64,
                                R_CALLPOWER:
                                ch.limit = int(int32(limit) - pcsp.value - int32(callsize()))
 
index 923bf52aeb54bd707af5570e7baed6db86c61236..a5624ed3c5af9466d205d0930cb24ce397f2f986 100644 (file)
@@ -219,6 +219,7 @@ const (
        R_SIZE
        R_CALL
        R_CALLARM
+       R_CALLARM64
        R_CALLIND
        R_CALLPOWER
        R_CONST
index 94ae6a55d27bb9be7724d46a4eb6f4a2e20083fb..e1ac864426c258bfac2e404a62035fda7310e8f4 100644 (file)
@@ -58,6 +58,7 @@ func putelfstr(s string) int {
 func putelfsyment(off int, addr int64, size int64, info int, shndx int, other int) {
        switch Thearch.Thechar {
        case '6',
+               '7',
                '9':
                Thearch.Lput(uint32(off))
                Cput(uint8(info))