Commit 1934a77f authored by Michael Hudson-Doyle's avatar Michael Hudson-Doyle

cmd/link: define a variable for the target platform's elf relocation type

Rather than having half a dozen switch statements. Also remove some c2go dregs.

Change-Id: I19af5b64f73369126020e15421c34cad5bbcfbf8
Reviewed-on: https://go-review.googlesource.com/21442Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent af74dca4
...@@ -9,7 +9,6 @@ import ( ...@@ -9,7 +9,6 @@ import (
"crypto/sha1" "crypto/sha1"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"fmt"
"path/filepath" "path/filepath"
"sort" "sort"
"strings" "strings"
...@@ -832,19 +831,22 @@ const ( ...@@ -832,19 +831,22 @@ const (
NSECT = 48 NSECT = 48
) )
var Iself bool var (
Iself bool
var Nelfsym int = 1 Nelfsym int = 1
var elf64 bool elf64 bool
// Either ".rel" or ".rela" depending on which type of relocation the
// target platform uses.
elfRelType string
var ehdr ElfEhdr ehdr ElfEhdr
phdr [NSECT]*ElfPhdr
shdr [NSECT]*ElfShdr
var phdr [NSECT]*ElfPhdr interp string
)
var shdr [NSECT]*ElfShdr
var interp string
type Elfstring struct { type Elfstring struct {
s string s string
...@@ -864,6 +866,13 @@ var buildinfo []byte ...@@ -864,6 +866,13 @@ var buildinfo []byte
func Elfinit() { func Elfinit() {
Iself = true Iself = true
switch Thearch.Thechar {
case '0', '6', '7', '9', 'z':
elfRelType = ".rela"
default:
elfRelType = ".rel"
}
switch Thearch.Thechar { switch Thearch.Thechar {
// 64-bit architectures // 64-bit architectures
case '9', 'z': case '9', 'z':
...@@ -1507,22 +1516,16 @@ func elfdynhash() { ...@@ -1507,22 +1516,16 @@ func elfdynhash() {
elfwritedynentsym(s, DT_VERSYM, Linklookup(Ctxt, ".gnu.version", 0)) elfwritedynentsym(s, DT_VERSYM, Linklookup(Ctxt, ".gnu.version", 0))
} }
switch Thearch.Thechar { sy := Linklookup(Ctxt, elfRelType+".plt", 0)
case '0', '6', '7', '9', 'z':
sy := Linklookup(Ctxt, ".rela.plt", 0)
if sy.Size > 0 { if sy.Size > 0 {
if elfRelType == ".rela" {
Elfwritedynent(s, DT_PLTREL, DT_RELA) Elfwritedynent(s, DT_PLTREL, DT_RELA)
elfwritedynentsymsize(s, DT_PLTRELSZ, sy) } else {
elfwritedynentsym(s, DT_JMPREL, sy)
}
default:
sy := Linklookup(Ctxt, ".rel.plt", 0)
if sy.Size > 0 {
Elfwritedynent(s, DT_PLTREL, DT_REL) Elfwritedynent(s, DT_PLTREL, DT_REL)
}
elfwritedynentsymsize(s, DT_PLTRELSZ, sy) elfwritedynentsymsize(s, DT_PLTRELSZ, sy)
elfwritedynentsym(s, DT_JMPREL, sy) elfwritedynentsym(s, DT_JMPREL, sy)
} }
}
Elfwritedynent(s, DT_NULL, 0) Elfwritedynent(s, DT_NULL, 0)
} }
...@@ -1645,19 +1648,14 @@ func elfshreloc(sect *Section) *ElfShdr { ...@@ -1645,19 +1648,14 @@ func elfshreloc(sect *Section) *ElfShdr {
return nil return nil
} }
var prefix string
var typ int var typ int
switch Thearch.Thechar { if elfRelType == ".rela" {
case '0', '6', '7', '9', 'z':
prefix = ".rela"
typ = SHT_RELA typ = SHT_RELA
default: } else {
prefix = ".rel"
typ = SHT_REL typ = SHT_REL
} }
buf := fmt.Sprintf("%s%s", prefix, sect.Name) sh := elfshname(elfRelType + sect.Name)
sh := elfshname(buf)
sh.type_ = uint32(typ) sh.type_ = uint32(typ)
sh.entsize = uint64(Thearch.Regsize) * 2 sh.entsize = uint64(Thearch.Regsize) * 2
if typ == SHT_RELA { if typ == SHT_RELA {
...@@ -1821,32 +1819,16 @@ func doelf() { ...@@ -1821,32 +1819,16 @@ func doelf() {
if Linkmode == LinkExternal { if Linkmode == LinkExternal {
Debug['d'] = 1 Debug['d'] = 1
switch Thearch.Thechar { Addstring(shstrtab, elfRelType+".text")
case '0', '6', '7', '9', 'z': Addstring(shstrtab, elfRelType+".rodata")
Addstring(shstrtab, ".rela.text") Addstring(shstrtab, elfRelType+relro_prefix+".typelink")
Addstring(shstrtab, ".rela.rodata") Addstring(shstrtab, elfRelType+relro_prefix+".itablink")
Addstring(shstrtab, ".rela"+relro_prefix+".typelink") Addstring(shstrtab, elfRelType+relro_prefix+".gosymtab")
Addstring(shstrtab, ".rela"+relro_prefix+".itablink") Addstring(shstrtab, elfRelType+relro_prefix+".gopclntab")
Addstring(shstrtab, ".rela"+relro_prefix+".gosymtab") Addstring(shstrtab, elfRelType+".noptrdata")
Addstring(shstrtab, ".rela"+relro_prefix+".gopclntab") Addstring(shstrtab, elfRelType+".data")
Addstring(shstrtab, ".rela.noptrdata")
Addstring(shstrtab, ".rela.data")
if UseRelro() {
Addstring(shstrtab, ".rela.data.rel.ro")
}
default:
Addstring(shstrtab, ".rel.text")
Addstring(shstrtab, ".rel.rodata")
Addstring(shstrtab, ".rel"+relro_prefix+".typelink")
Addstring(shstrtab, ".rel"+relro_prefix+".itablink")
Addstring(shstrtab, ".rel"+relro_prefix+".gosymtab")
Addstring(shstrtab, ".rel"+relro_prefix+".gopclntab")
Addstring(shstrtab, ".rel.noptrdata")
Addstring(shstrtab, ".rel.data")
if UseRelro() { if UseRelro() {
Addstring(shstrtab, ".rel.data.rel.ro") Addstring(shstrtab, elfRelType+".data.rel.ro")
}
} }
// add a .note.GNU-stack section to mark the stack as non-executable // add a .note.GNU-stack section to mark the stack as non-executable
...@@ -1869,12 +1851,7 @@ func doelf() { ...@@ -1869,12 +1851,7 @@ func doelf() {
if hasinitarr { if hasinitarr {
Addstring(shstrtab, ".init_array") Addstring(shstrtab, ".init_array")
switch Thearch.Thechar { Addstring(shstrtab, elfRelType+".init_array")
case '0', '6', '7', '9', 'z':
Addstring(shstrtab, ".rela.init_array")
default:
Addstring(shstrtab, ".rel.init_array")
}
} }
if Debug['s'] == 0 { if Debug['s'] == 0 {
...@@ -1896,14 +1873,8 @@ func doelf() { ...@@ -1896,14 +1873,8 @@ func doelf() {
Addstring(shstrtab, ".dynamic") Addstring(shstrtab, ".dynamic")
Addstring(shstrtab, ".dynsym") Addstring(shstrtab, ".dynsym")
Addstring(shstrtab, ".dynstr") Addstring(shstrtab, ".dynstr")
switch Thearch.Thechar { Addstring(shstrtab, elfRelType)
case '0', '6', '7', '9', 'z': Addstring(shstrtab, elfRelType+".plt")
Addstring(shstrtab, ".rela")
Addstring(shstrtab, ".rela.plt")
default:
Addstring(shstrtab, ".rel")
Addstring(shstrtab, ".rel.plt")
}
Addstring(shstrtab, ".plt") Addstring(shstrtab, ".plt")
Addstring(shstrtab, ".gnu.version") Addstring(shstrtab, ".gnu.version")
...@@ -1932,12 +1903,7 @@ func doelf() { ...@@ -1932,12 +1903,7 @@ func doelf() {
dynstr := s dynstr := s
/* relocation table */ /* relocation table */
switch Thearch.Thechar { s = Linklookup(Ctxt, elfRelType, 0)
case '0', '6', '7', '9', 'z':
s = Linklookup(Ctxt, ".rela", 0)
default:
s = Linklookup(Ctxt, ".rel", 0)
}
s.Attr |= AttrReachable s.Attr |= AttrReachable
s.Type = obj.SELFROSECT s.Type = obj.SELFROSECT
...@@ -1977,12 +1943,7 @@ func doelf() { ...@@ -1977,12 +1943,7 @@ func doelf() {
Thearch.Elfsetupplt() Thearch.Elfsetupplt()
switch Thearch.Thechar { s = Linklookup(Ctxt, elfRelType+".plt", 0)
case '0', '6', '7', '9', 'z':
s = Linklookup(Ctxt, ".rela.plt", 0)
default:
s = Linklookup(Ctxt, ".rel.plt", 0)
}
s.Attr |= AttrReachable s.Attr |= AttrReachable
s.Type = obj.SELFROSECT s.Type = obj.SELFROSECT
...@@ -2014,12 +1975,11 @@ func doelf() { ...@@ -2014,12 +1975,11 @@ func doelf() {
} }
elfwritedynentsym(s, DT_STRTAB, Linklookup(Ctxt, ".dynstr", 0)) elfwritedynentsym(s, DT_STRTAB, Linklookup(Ctxt, ".dynstr", 0))
elfwritedynentsymsize(s, DT_STRSZ, Linklookup(Ctxt, ".dynstr", 0)) elfwritedynentsymsize(s, DT_STRSZ, Linklookup(Ctxt, ".dynstr", 0))
switch Thearch.Thechar { if elfRelType == ".rela" {
case '0', '6', '7', '9', 'z':
elfwritedynentsym(s, DT_RELA, Linklookup(Ctxt, ".rela", 0)) elfwritedynentsym(s, DT_RELA, Linklookup(Ctxt, ".rela", 0))
elfwritedynentsymsize(s, DT_RELASZ, Linklookup(Ctxt, ".rela", 0)) elfwritedynentsymsize(s, DT_RELASZ, Linklookup(Ctxt, ".rela", 0))
Elfwritedynent(s, DT_RELAENT, ELF64RELASIZE) Elfwritedynent(s, DT_RELAENT, ELF64RELASIZE)
default: } else {
elfwritedynentsym(s, DT_REL, Linklookup(Ctxt, ".rel", 0)) elfwritedynentsym(s, DT_REL, Linklookup(Ctxt, ".rel", 0))
elfwritedynentsymsize(s, DT_RELSZ, Linklookup(Ctxt, ".rel", 0)) elfwritedynentsymsize(s, DT_RELSZ, Linklookup(Ctxt, ".rel", 0))
Elfwritedynent(s, DT_RELENT, ELF32RELSIZE) Elfwritedynent(s, DT_RELENT, ELF32RELSIZE)
...@@ -2314,8 +2274,7 @@ func Asmbelf(symo int64) { ...@@ -2314,8 +2274,7 @@ func Asmbelf(symo int64) {
shsym(sh, Linklookup(Ctxt, ".gnu.version_r", 0)) shsym(sh, Linklookup(Ctxt, ".gnu.version_r", 0))
} }
switch eh.machine { if elfRelType == ".rela" {
case EM_X86_64, EM_PPC64, EM_AARCH64, EM_S390:
sh := elfshname(".rela.plt") sh := elfshname(".rela.plt")
sh.type_ = SHT_RELA sh.type_ = SHT_RELA
sh.flags = SHF_ALLOC sh.flags = SHF_ALLOC
...@@ -2332,8 +2291,7 @@ func Asmbelf(symo int64) { ...@@ -2332,8 +2291,7 @@ func Asmbelf(symo int64) {
sh.addralign = 8 sh.addralign = 8
sh.link = uint32(elfshname(".dynsym").shnum) sh.link = uint32(elfshname(".dynsym").shnum)
shsym(sh, Linklookup(Ctxt, ".rela", 0)) shsym(sh, Linklookup(Ctxt, ".rela", 0))
} else {
default:
sh := elfshname(".rel.plt") sh := elfshname(".rel.plt")
sh.type_ = SHT_REL sh.type_ = SHT_REL
sh.flags = SHF_ALLOC sh.flags = SHF_ALLOC
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment