2019-12-06 20:11:36 -05:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
2017-10-06 16:01:02 -04:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
// Package loadelf implements an ELF file reader.
|
|
|
|
|
package loadelf
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2016-04-06 21:45:29 -07:00
|
|
|
"cmd/internal/bio"
|
2017-04-18 12:53:25 -07:00
|
|
|
"cmd/internal/objabi"
|
2016-04-06 12:01:40 -07:00
|
|
|
"cmd/internal/sys"
|
2019-10-17 11:06:11 -04:00
|
|
|
"cmd/link/internal/loader"
|
2017-10-04 17:54:04 -04:00
|
|
|
"cmd/link/internal/sym"
|
2017-10-06 16:01:02 -04:00
|
|
|
"debug/elf"
|
2015-02-27 22:57:28 -05:00
|
|
|
"encoding/binary"
|
|
|
|
|
"fmt"
|
2015-09-23 15:46:00 +12:00
|
|
|
"io"
|
2015-02-27 22:57:28 -05:00
|
|
|
"log"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Derived from Plan 9 from User Space's src/libmach/elf.h, elf.c
|
2021-08-10 22:05:34 +08:00
|
|
|
https://github.com/9fans/plan9port/tree/master/src/libmach/
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
Copyright © 2004 Russ Cox.
|
|
|
|
|
Portions Copyright © 2008-2010 Google Inc.
|
|
|
|
|
Portions Copyright © 2010 The Go Authors.
|
|
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
|
THE SOFTWARE.
|
|
|
|
|
*/
|
2015-03-04 10:28:18 +13:00
|
|
|
|
|
|
|
|
const (
|
2017-10-06 16:01:02 -04:00
|
|
|
SHT_ARM_ATTRIBUTES = 0x70000003
|
|
|
|
|
)
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
type ElfSect struct {
|
2020-02-13 10:38:33 -05:00
|
|
|
name string
|
|
|
|
|
nameoff uint32
|
2020-04-26 15:47:18 +08:00
|
|
|
type_ elf.SectionType
|
|
|
|
|
flags elf.SectionFlag
|
2020-02-13 10:38:33 -05:00
|
|
|
addr uint64
|
|
|
|
|
off uint64
|
|
|
|
|
size uint64
|
|
|
|
|
link uint32
|
|
|
|
|
info uint32
|
|
|
|
|
align uint64
|
|
|
|
|
entsize uint64
|
|
|
|
|
base []byte
|
|
|
|
|
readOnlyMem bool // Is this section in readonly memory?
|
|
|
|
|
sym loader.Sym
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ElfObj struct {
|
2016-04-08 19:14:03 +10:00
|
|
|
f *bio.Reader
|
2015-03-05 13:57:36 -05:00
|
|
|
base int64 // offset in f where ELF begins
|
|
|
|
|
length int64 // length of ELF
|
2015-02-27 22:57:28 -05:00
|
|
|
is64 int
|
|
|
|
|
name string
|
|
|
|
|
e binary.ByteOrder
|
|
|
|
|
sect []ElfSect
|
|
|
|
|
nsect uint
|
|
|
|
|
nsymtab int
|
|
|
|
|
symtab *ElfSect
|
|
|
|
|
symstr *ElfSect
|
|
|
|
|
type_ uint32
|
|
|
|
|
machine uint32
|
|
|
|
|
version uint32
|
|
|
|
|
entry uint64
|
|
|
|
|
phoff uint64
|
|
|
|
|
shoff uint64
|
|
|
|
|
flags uint32
|
|
|
|
|
ehsize uint32
|
|
|
|
|
phentsize uint32
|
|
|
|
|
phnum uint32
|
|
|
|
|
shentsize uint32
|
|
|
|
|
shnum uint32
|
|
|
|
|
shstrndx uint32
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type ElfSym struct {
|
|
|
|
|
name string
|
|
|
|
|
value uint64
|
|
|
|
|
size uint64
|
2020-04-26 15:47:18 +08:00
|
|
|
bind elf.SymBind
|
|
|
|
|
type_ elf.SymType
|
2015-02-27 22:57:28 -05:00
|
|
|
other uint8
|
2020-04-26 15:47:18 +08:00
|
|
|
shndx elf.SectionIndex
|
2019-12-06 20:11:36 -05:00
|
|
|
sym loader.Sym
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-09-23 15:46:00 +12:00
|
|
|
const (
|
2016-08-22 10:33:13 -04:00
|
|
|
TagFile = 1
|
|
|
|
|
TagCPUName = 4
|
|
|
|
|
TagCPURawName = 5
|
|
|
|
|
TagCompatibility = 32
|
|
|
|
|
TagNoDefaults = 64
|
|
|
|
|
TagAlsoCompatibleWith = 65
|
|
|
|
|
TagABIVFPArgs = 28
|
2015-09-23 15:46:00 +12:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type elfAttribute struct {
|
|
|
|
|
tag uint64
|
|
|
|
|
sval string
|
|
|
|
|
ival uint64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type elfAttributeList struct {
|
|
|
|
|
data []byte
|
|
|
|
|
err error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *elfAttributeList) string() string {
|
|
|
|
|
if a.err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
nul := bytes.IndexByte(a.data, 0)
|
|
|
|
|
if nul < 0 {
|
|
|
|
|
a.err = io.EOF
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
s := string(a.data[:nul])
|
|
|
|
|
a.data = a.data[nul+1:]
|
|
|
|
|
return s
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *elfAttributeList) uleb128() uint64 {
|
|
|
|
|
if a.err != nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
v, size := binary.Uvarint(a.data)
|
|
|
|
|
a.data = a.data[size:]
|
|
|
|
|
return v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read an elfAttribute from the list following the rules used on ARM systems.
|
|
|
|
|
func (a *elfAttributeList) armAttr() elfAttribute {
|
|
|
|
|
attr := elfAttribute{tag: a.uleb128()}
|
|
|
|
|
switch {
|
2016-08-22 10:33:13 -04:00
|
|
|
case attr.tag == TagCompatibility:
|
2015-09-23 15:46:00 +12:00
|
|
|
attr.ival = a.uleb128()
|
|
|
|
|
attr.sval = a.string()
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
case attr.tag == TagNoDefaults: // Tag_nodefaults has no argument
|
2015-09-23 15:46:00 +12:00
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
case attr.tag == TagAlsoCompatibleWith:
|
2015-09-23 15:46:00 +12:00
|
|
|
// Not really, but we don't actually care about this tag.
|
|
|
|
|
attr.sval = a.string()
|
|
|
|
|
|
|
|
|
|
// Tag with string argument
|
2016-08-22 10:33:13 -04:00
|
|
|
case attr.tag == TagCPUName || attr.tag == TagCPURawName || (attr.tag >= 32 && attr.tag&1 != 0):
|
2015-09-23 15:46:00 +12:00
|
|
|
attr.sval = a.string()
|
|
|
|
|
|
|
|
|
|
default: // Tag with integer argument
|
|
|
|
|
attr.ival = a.uleb128()
|
|
|
|
|
}
|
|
|
|
|
return attr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *elfAttributeList) done() bool {
|
|
|
|
|
if a.err != nil || len(a.data) == 0 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Look for the attribute that indicates the object uses the hard-float ABI (a
|
|
|
|
|
// file-level attribute with tag Tag_VFP_arch and value 1). Unfortunately the
|
|
|
|
|
// format used means that we have to parse all of the file-level attributes to
|
|
|
|
|
// find the one we are looking for. This format is slightly documented in "ELF
|
|
|
|
|
// for the ARM Architecture" but mostly this is derived from reading the source
|
|
|
|
|
// to gold and readelf.
|
2018-02-07 15:46:26 +13:00
|
|
|
func parseArmAttributes(e binary.ByteOrder, data []byte) (found bool, ehdrFlags uint32, err error) {
|
|
|
|
|
found = false
|
2015-09-23 15:46:00 +12:00
|
|
|
if data[0] != 'A' {
|
2018-02-07 15:46:26 +13:00
|
|
|
return false, 0, fmt.Errorf(".ARM.attributes has unexpected format %c\n", data[0])
|
2015-09-23 15:46:00 +12:00
|
|
|
}
|
|
|
|
|
data = data[1:]
|
|
|
|
|
for len(data) != 0 {
|
|
|
|
|
sectionlength := e.Uint32(data)
|
|
|
|
|
sectiondata := data[4:sectionlength]
|
|
|
|
|
data = data[sectionlength:]
|
|
|
|
|
|
|
|
|
|
nulIndex := bytes.IndexByte(sectiondata, 0)
|
|
|
|
|
if nulIndex < 0 {
|
2018-02-07 15:46:26 +13:00
|
|
|
return false, 0, fmt.Errorf("corrupt .ARM.attributes (section name not NUL-terminated)\n")
|
2015-09-23 15:46:00 +12:00
|
|
|
}
|
|
|
|
|
name := string(sectiondata[:nulIndex])
|
|
|
|
|
sectiondata = sectiondata[nulIndex+1:]
|
|
|
|
|
|
|
|
|
|
if name != "aeabi" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
for len(sectiondata) != 0 {
|
|
|
|
|
subsectiontag, sz := binary.Uvarint(sectiondata)
|
|
|
|
|
subsectionsize := e.Uint32(sectiondata[sz:])
|
|
|
|
|
subsectiondata := sectiondata[sz+4 : subsectionsize]
|
|
|
|
|
sectiondata = sectiondata[subsectionsize:]
|
|
|
|
|
|
2017-10-09 10:11:00 +01:00
|
|
|
if subsectiontag != TagFile {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
attrList := elfAttributeList{data: subsectiondata}
|
|
|
|
|
for !attrList.done() {
|
|
|
|
|
attr := attrList.armAttr()
|
|
|
|
|
if attr.tag == TagABIVFPArgs && attr.ival == 1 {
|
2018-02-07 15:46:26 +13:00
|
|
|
found = true
|
2017-10-06 16:01:02 -04:00
|
|
|
ehdrFlags = 0x5000402 // has entry point, Version5 EABI, hard-float ABI
|
2015-09-23 15:46:00 +12:00
|
|
|
}
|
|
|
|
|
}
|
2017-10-09 10:11:00 +01:00
|
|
|
if attrList.err != nil {
|
2018-02-07 15:46:26 +13:00
|
|
|
return false, 0, fmt.Errorf("could not parse .ARM.attributes\n")
|
2017-10-09 10:11:00 +01:00
|
|
|
}
|
2015-09-23 15:46:00 +12:00
|
|
|
}
|
|
|
|
|
}
|
2018-02-07 15:46:26 +13:00
|
|
|
return found, ehdrFlags, nil
|
2015-09-23 15:46:00 +12:00
|
|
|
}
|
|
|
|
|
|
2019-11-11 14:32:30 -05:00
|
|
|
// Load loads the ELF file pn from f.
|
2019-12-06 20:11:36 -05:00
|
|
|
// Symbols are installed into the loader, and a slice of the text symbols is returned.
|
2017-10-06 16:01:02 -04:00
|
|
|
//
|
|
|
|
|
// On ARM systems, Load will attempt to determine what ELF header flags to
|
|
|
|
|
// emit by scanning the attributes in the ELF file being loaded. The
|
|
|
|
|
// parameter initEhdrFlags contains the current header flags for the output
|
2018-02-20 20:50:20 +00:00
|
|
|
// object, and the returned ehdrFlags contains what this Load function computes.
|
2017-10-06 16:01:02 -04:00
|
|
|
// TODO: find a better place for this logic.
|
2019-12-06 20:11:36 -05:00
|
|
|
func Load(l *loader.Loader, arch *sys.Arch, localSymVersion int, f *bio.Reader, pkg string, length int64, pn string, initEhdrFlags uint32) (textp []loader.Sym, ehdrFlags uint32, err error) {
|
|
|
|
|
newSym := func(name string, version int) loader.Sym {
|
2020-03-24 12:08:36 -04:00
|
|
|
return l.CreateStaticSym(name)
|
2019-12-03 10:38:43 -05:00
|
|
|
}
|
cmd/compile,cmd/link: resolve cgo symbols to the correct Go ABI
Currently, Go functions exported to cgo have some confusion around
ABIs that leads to crashes. The cmd/cgo-generated C code references an
exported Go wrapper function (which calls the underlying exported user
function). The linker resolves this reference to the ABI0 entry-point
to that Go wrapper function because all host object references are
currently assumed to be to version 0 of a symbol. This gets passed via
crosscall2 and winds its way to cgocallbackg1, which puts this ABI0
entry-point into a function value and calls it. Unfortunately,
function values always use the ABIInternal calling convention, so
calling this ABI0 entry-point goes poorly.
Fix this by threading definition ABIs through the cgo export mechanism
so the linker can resolve host object references (which have no
concept of multiple ABIs) to the correct Go symbol. This involves a
few pieces:
- The compiler extends the cgo_export_{static,dynamic} directives that
get passed on to the linker with symbol definition ABIs.
- The linker parses the ABIs in the cgo_export_{static,dynamic}
directives to look up the right symbol to apply export attributes to
and put in the dynexp list.
- For internal linking, the linker's Loader structure tracks the right
symbol (in particular the right ABI) to resolve host object
references to, and we use this in all of the host object loaders.
- For external linking, we mangle only the non-ABIInternal symbols
now, so the external linker is able to resolve the correct reference
from host objects to Go symbols.
Updates #40724.
Change-Id: I70a0b1610596768c3f473745fa1a3e630afbf1a8
Reviewed-on: https://go-review.googlesource.com/c/go/+/309341
Trust: Austin Clements <austin@google.com>
Run-TryBot: Austin Clements <austin@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
2021-04-11 15:44:25 -04:00
|
|
|
lookup := l.LookupOrCreateCgoExport
|
2019-12-06 20:11:36 -05:00
|
|
|
errorf := func(str string, args ...interface{}) ([]loader.Sym, uint32, error) {
|
2017-10-06 16:01:02 -04:00
|
|
|
return nil, 0, fmt.Errorf("loadelf: %s: %v", pn, fmt.Sprintf(str, args...))
|
2015-03-02 12:35:15 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:41:38 -06:00
|
|
|
ehdrFlags = initEhdrFlags
|
|
|
|
|
|
2016-04-08 19:14:03 +10:00
|
|
|
base := f.Offset()
|
2015-03-02 12:35:15 -05:00
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
var hdrbuf [64]byte
|
2016-04-08 18:19:10 +02:00
|
|
|
if _, err := io.ReadFull(f, hdrbuf[:]); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file: %v", err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2020-04-26 15:47:18 +08:00
|
|
|
|
2017-10-01 14:39:04 +11:00
|
|
|
var e binary.ByteOrder
|
2020-04-26 15:47:18 +08:00
|
|
|
switch elf.Data(hdrbuf[elf.EI_DATA]) {
|
|
|
|
|
case elf.ELFDATA2LSB:
|
2015-02-27 22:57:28 -05:00
|
|
|
e = binary.LittleEndian
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
case elf.ELFDATA2MSB:
|
2015-02-27 22:57:28 -05:00
|
|
|
e = binary.BigEndian
|
|
|
|
|
|
|
|
|
|
default:
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file, unknown header")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
hdr := new(elf.Header32)
|
|
|
|
|
binary.Read(bytes.NewReader(hdrbuf[:]), e, hdr)
|
|
|
|
|
|
|
|
|
|
if string(hdr.Ident[:elf.EI_CLASS]) != elf.ELFMAG {
|
|
|
|
|
return errorf("malformed elf file, bad header")
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
// read header
|
2017-10-01 14:39:04 +11:00
|
|
|
elfobj := new(ElfObj)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
elfobj.e = e
|
|
|
|
|
elfobj.f = f
|
2016-04-14 19:04:45 -07:00
|
|
|
elfobj.base = base
|
2015-02-27 22:57:28 -05:00
|
|
|
elfobj.length = length
|
|
|
|
|
elfobj.name = pn
|
|
|
|
|
|
2017-10-01 14:39:04 +11:00
|
|
|
is64 := 0
|
2020-04-26 15:47:18 +08:00
|
|
|
class := elf.Class(hdrbuf[elf.EI_CLASS])
|
|
|
|
|
if class == elf.ELFCLASS64 {
|
2015-02-27 22:57:28 -05:00
|
|
|
is64 = 1
|
2020-04-26 15:47:18 +08:00
|
|
|
hdr := new(elf.Header64)
|
|
|
|
|
binary.Read(bytes.NewReader(hdrbuf[:]), e, hdr)
|
|
|
|
|
elfobj.type_ = uint32(hdr.Type)
|
|
|
|
|
elfobj.machine = uint32(hdr.Machine)
|
|
|
|
|
elfobj.version = hdr.Version
|
|
|
|
|
elfobj.entry = hdr.Entry
|
|
|
|
|
elfobj.phoff = hdr.Phoff
|
|
|
|
|
elfobj.shoff = hdr.Shoff
|
|
|
|
|
elfobj.flags = hdr.Flags
|
|
|
|
|
elfobj.ehsize = uint32(hdr.Ehsize)
|
|
|
|
|
elfobj.phentsize = uint32(hdr.Phentsize)
|
|
|
|
|
elfobj.phnum = uint32(hdr.Phnum)
|
|
|
|
|
elfobj.shentsize = uint32(hdr.Shentsize)
|
|
|
|
|
elfobj.shnum = uint32(hdr.Shnum)
|
|
|
|
|
elfobj.shstrndx = uint32(hdr.Shstrndx)
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2020-04-26 15:47:18 +08:00
|
|
|
elfobj.type_ = uint32(hdr.Type)
|
|
|
|
|
elfobj.machine = uint32(hdr.Machine)
|
|
|
|
|
elfobj.version = hdr.Version
|
|
|
|
|
elfobj.entry = uint64(hdr.Entry)
|
|
|
|
|
elfobj.phoff = uint64(hdr.Phoff)
|
|
|
|
|
elfobj.shoff = uint64(hdr.Shoff)
|
|
|
|
|
elfobj.flags = hdr.Flags
|
|
|
|
|
elfobj.ehsize = uint32(hdr.Ehsize)
|
|
|
|
|
elfobj.phentsize = uint32(hdr.Phentsize)
|
|
|
|
|
elfobj.phnum = uint32(hdr.Phnum)
|
|
|
|
|
elfobj.shentsize = uint32(hdr.Shentsize)
|
|
|
|
|
elfobj.shnum = uint32(hdr.Shnum)
|
|
|
|
|
elfobj.shstrndx = uint32(hdr.Shstrndx)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elfobj.is64 = is64
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
if v := uint32(hdrbuf[elf.EI_VERSION]); v != elfobj.version {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf version: got %d, want %d", v, elfobj.version)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
if elf.Type(elfobj.type_) != elf.ET_REL {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf but not elf relocatable object")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
mach := elf.Machine(elfobj.machine)
|
2017-10-06 16:01:02 -04:00
|
|
|
switch arch.Family {
|
2015-02-27 22:57:28 -05:00
|
|
|
default:
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf %s unimplemented", arch.Name)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-10-18 23:50:45 +02:00
|
|
|
case sys.MIPS:
|
2020-04-26 15:47:18 +08:00
|
|
|
if mach != elf.EM_MIPS || class != elf.ELFCLASS32 {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf object but not mips")
|
2016-10-18 23:50:45 +02:00
|
|
|
}
|
|
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.MIPS64:
|
2020-04-26 15:47:18 +08:00
|
|
|
if mach != elf.EM_MIPS || class != elf.ELFCLASS64 {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf object but not mips64")
|
2015-09-10 11:32:49 -04:00
|
|
|
}
|
2021-08-15 16:25:46 +08:00
|
|
|
case sys.Loong64:
|
|
|
|
|
if mach != elf.EM_LOONGARCH || class != elf.ELFCLASS64 {
|
|
|
|
|
return errorf("elf object but not loong64")
|
|
|
|
|
}
|
2015-09-10 11:32:49 -04:00
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.ARM:
|
2020-04-26 15:47:18 +08:00
|
|
|
if e != binary.LittleEndian || mach != elf.EM_ARM || class != elf.ELFCLASS32 {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf object but not arm")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.AMD64:
|
2020-04-26 15:47:18 +08:00
|
|
|
if e != binary.LittleEndian || mach != elf.EM_X86_64 || class != elf.ELFCLASS64 {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf object but not amd64")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.ARM64:
|
2020-04-26 15:47:18 +08:00
|
|
|
if e != binary.LittleEndian || mach != elf.EM_AARCH64 || class != elf.ELFCLASS64 {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf object but not arm64")
|
2015-03-08 14:14:53 +01:00
|
|
|
}
|
|
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.I386:
|
2020-04-26 15:47:18 +08:00
|
|
|
if e != binary.LittleEndian || mach != elf.EM_386 || class != elf.ELFCLASS32 {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf object but not 386")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.PPC64:
|
2020-04-26 15:47:18 +08:00
|
|
|
if mach != elf.EM_PPC64 || class != elf.ELFCLASS64 {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf object but not ppc64")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-04-06 12:01:40 -07:00
|
|
|
|
2020-10-20 22:04:55 +11:00
|
|
|
case sys.RISCV64:
|
|
|
|
|
if mach != elf.EM_RISCV || class != elf.ELFCLASS64 {
|
|
|
|
|
return errorf("elf object but not riscv64")
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.S390X:
|
2020-04-26 15:47:18 +08:00
|
|
|
if mach != elf.EM_S390 || class != elf.ELFCLASS64 {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf object but not s390x")
|
2016-03-18 16:57:54 -04:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load section list into memory.
|
|
|
|
|
elfobj.sect = make([]ElfSect, elfobj.shnum)
|
|
|
|
|
|
|
|
|
|
elfobj.nsect = uint(elfobj.shnum)
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; uint(i) < elfobj.nsect; i++ {
|
2019-05-08 18:46:04 -04:00
|
|
|
f.MustSeek(int64(uint64(base)+elfobj.shoff+uint64(int64(i)*int64(elfobj.shentsize))), 0)
|
2017-10-01 14:39:04 +11:00
|
|
|
sect := &elfobj.sect[i]
|
2015-02-27 22:57:28 -05:00
|
|
|
if is64 != 0 {
|
2020-04-26 15:47:18 +08:00
|
|
|
var b elf.Section64
|
2017-04-20 10:45:01 +10:00
|
|
|
if err := binary.Read(f, e, &b); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file: %v", err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
sect.nameoff = b.Name
|
|
|
|
|
sect.type_ = elf.SectionType(b.Type)
|
|
|
|
|
sect.flags = elf.SectionFlag(b.Flags)
|
|
|
|
|
sect.addr = b.Addr
|
|
|
|
|
sect.off = b.Off
|
|
|
|
|
sect.size = b.Size
|
|
|
|
|
sect.link = b.Link
|
|
|
|
|
sect.info = b.Info
|
|
|
|
|
sect.align = b.Addralign
|
|
|
|
|
sect.entsize = b.Entsize
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2020-04-26 15:47:18 +08:00
|
|
|
var b elf.Section32
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-04-20 10:45:01 +10:00
|
|
|
if err := binary.Read(f, e, &b); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file: %v", err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2020-04-26 15:47:18 +08:00
|
|
|
sect.nameoff = b.Name
|
|
|
|
|
sect.type_ = elf.SectionType(b.Type)
|
|
|
|
|
sect.flags = elf.SectionFlag(b.Flags)
|
|
|
|
|
sect.addr = uint64(b.Addr)
|
|
|
|
|
sect.off = uint64(b.Off)
|
|
|
|
|
sect.size = uint64(b.Size)
|
|
|
|
|
sect.link = b.Link
|
|
|
|
|
sect.info = b.Info
|
|
|
|
|
sect.align = uint64(b.Addralign)
|
|
|
|
|
sect.entsize = uint64(b.Entsize)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// read section string table and translate names
|
|
|
|
|
if elfobj.shstrndx >= uint32(elfobj.nsect) {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file: shstrndx out of range %d >= %d", elfobj.shstrndx, elfobj.nsect)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 14:39:04 +11:00
|
|
|
sect := &elfobj.sect[elfobj.shstrndx]
|
2017-04-20 10:45:01 +10:00
|
|
|
if err := elfmap(elfobj, sect); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file: %v", err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; uint(i) < elfobj.nsect; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
if elfobj.sect[i].nameoff != 0 {
|
|
|
|
|
elfobj.sect[i].name = cstring(sect.base[elfobj.sect[i].nameoff:])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load string table for symbols into memory.
|
|
|
|
|
elfobj.symtab = section(elfobj, ".symtab")
|
|
|
|
|
|
|
|
|
|
if elfobj.symtab == nil {
|
|
|
|
|
// our work is done here - no symbols means nothing can refer to this file
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if elfobj.symtab.link <= 0 || elfobj.symtab.link >= uint32(elfobj.nsect) {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("elf object has symbol table with invalid string table link")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elfobj.symstr = &elfobj.sect[elfobj.symtab.link]
|
|
|
|
|
if is64 != 0 {
|
2020-04-26 15:47:18 +08:00
|
|
|
elfobj.nsymtab = int(elfobj.symtab.size / elf.Sym64Size)
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2020-04-26 15:47:18 +08:00
|
|
|
elfobj.nsymtab = int(elfobj.symtab.size / elf.Sym32Size)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-04-20 10:45:01 +10:00
|
|
|
if err := elfmap(elfobj, elfobj.symtab); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file: %v", err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-20 10:45:01 +10:00
|
|
|
if err := elfmap(elfobj, elfobj.symstr); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file: %v", err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load text and data segments into memory.
|
|
|
|
|
// they are not as small as the section lists, but we'll need
|
|
|
|
|
// the memory anyway for the symbol images, so we might
|
|
|
|
|
// as well use one large chunk.
|
|
|
|
|
|
|
|
|
|
// create symbols for elfmapped sections
|
2019-04-17 22:41:51 -07:00
|
|
|
sectsymNames := make(map[string]bool)
|
|
|
|
|
counter := 0
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; uint(i) < elfobj.nsect; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
sect = &elfobj.sect[i]
|
2015-09-23 15:46:00 +12:00
|
|
|
if sect.type_ == SHT_ARM_ATTRIBUTES && sect.name == ".ARM.attributes" {
|
2017-04-20 10:45:01 +10:00
|
|
|
if err := elfmap(elfobj, sect); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("%s: malformed elf file: %v", pn, err)
|
|
|
|
|
}
|
2018-02-07 15:46:26 +13:00
|
|
|
// We assume the soft-float ABI unless we see a tag indicating otherwise.
|
|
|
|
|
if initEhdrFlags == 0x5000002 {
|
|
|
|
|
ehdrFlags = 0x5000202
|
|
|
|
|
} else {
|
|
|
|
|
ehdrFlags = initEhdrFlags
|
|
|
|
|
}
|
|
|
|
|
found, newEhdrFlags, err := parseArmAttributes(e, sect.base[:sect.size])
|
2017-10-06 16:01:02 -04:00
|
|
|
if err != nil {
|
|
|
|
|
// TODO(dfc) should this return an error?
|
|
|
|
|
log.Printf("%s: %v", pn, err)
|
2015-09-23 15:46:00 +12:00
|
|
|
}
|
2018-02-07 15:46:26 +13:00
|
|
|
if found {
|
|
|
|
|
ehdrFlags = newEhdrFlags
|
|
|
|
|
}
|
2015-09-23 15:46:00 +12:00
|
|
|
}
|
2020-04-26 15:47:18 +08:00
|
|
|
if (sect.type_ != elf.SHT_PROGBITS && sect.type_ != elf.SHT_NOBITS) || sect.flags&elf.SHF_ALLOC == 0 {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
2020-04-26 15:47:18 +08:00
|
|
|
if sect.type_ != elf.SHT_NOBITS {
|
2017-04-20 10:45:01 +10:00
|
|
|
if err := elfmap(elfobj, sect); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("%s: malformed elf file: %v", pn, err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 14:39:04 +11:00
|
|
|
name := fmt.Sprintf("%s(%s)", pkg, sect.name)
|
2019-04-17 22:41:51 -07:00
|
|
|
for sectsymNames[name] {
|
|
|
|
|
counter++
|
|
|
|
|
name = fmt.Sprintf("%s(%s%d)", pkg, sect.name, counter)
|
|
|
|
|
}
|
|
|
|
|
sectsymNames[name] = true
|
|
|
|
|
|
2020-02-12 17:23:47 -05:00
|
|
|
sb := l.MakeSymbolUpdater(lookup(name, localSymVersion))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
switch sect.flags & (elf.SHF_ALLOC | elf.SHF_WRITE | elf.SHF_EXECINSTR) {
|
2015-02-27 22:57:28 -05:00
|
|
|
default:
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("%s: unexpected flags for ELF section %s", pn, sect.name)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
case elf.SHF_ALLOC:
|
2019-12-06 20:11:36 -05:00
|
|
|
sb.SetType(sym.SRODATA)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
case elf.SHF_ALLOC + elf.SHF_WRITE:
|
|
|
|
|
if sect.type_ == elf.SHT_NOBITS {
|
2019-12-06 20:11:36 -05:00
|
|
|
sb.SetType(sym.SNOPTRBSS)
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2019-12-06 20:11:36 -05:00
|
|
|
sb.SetType(sym.SNOPTRDATA)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
case elf.SHF_ALLOC + elf.SHF_EXECINSTR:
|
2019-12-06 20:11:36 -05:00
|
|
|
sb.SetType(sym.STEXT)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if sect.name == ".got" || sect.name == ".toc" {
|
2019-12-06 20:11:36 -05:00
|
|
|
sb.SetType(sym.SELFGOT)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2020-04-26 15:47:18 +08:00
|
|
|
if sect.type_ == elf.SHT_PROGBITS {
|
2019-12-06 20:11:36 -05:00
|
|
|
sb.SetData(sect.base[:sect.size])
|
2023-04-25 16:41:51 -05:00
|
|
|
sb.SetExternal(true)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-06 20:11:36 -05:00
|
|
|
sb.SetSize(int64(sect.size))
|
|
|
|
|
sb.SetAlign(int32(sect.align))
|
2020-02-13 10:38:33 -05:00
|
|
|
sb.SetReadOnly(sect.readOnlyMem)
|
2019-12-06 20:11:36 -05:00
|
|
|
|
|
|
|
|
sect.sym = sb.Sym()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// enter sub-symbols into symbol table.
|
|
|
|
|
// symbol 0 is the null symbol.
|
2019-12-06 20:11:36 -05:00
|
|
|
symbols := make([]loader.Sym, elfobj.nsymtab)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 1; i < elfobj.nsymtab; i++ {
|
2017-10-04 17:54:04 -04:00
|
|
|
var elfsym ElfSym
|
2019-12-06 20:11:36 -05:00
|
|
|
if err := readelfsym(newSym, lookup, l, arch, elfobj, i, &elfsym, 1, localSymVersion); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("%s: malformed elf file: %v", pn, err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-04 17:54:04 -04:00
|
|
|
symbols[i] = elfsym.sym
|
2020-04-26 15:47:18 +08:00
|
|
|
if elfsym.type_ != elf.STT_FUNC && elfsym.type_ != elf.STT_OBJECT && elfsym.type_ != elf.STT_NOTYPE && elfsym.type_ != elf.STT_COMMON {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
2020-04-26 15:47:18 +08:00
|
|
|
if elfsym.shndx == elf.SHN_COMMON || elfsym.type_ == elf.STT_COMMON {
|
2020-02-12 17:23:47 -05:00
|
|
|
sb := l.MakeSymbolUpdater(elfsym.sym)
|
2019-12-06 20:11:36 -05:00
|
|
|
if uint64(sb.Size()) < elfsym.size {
|
|
|
|
|
sb.SetSize(int64(elfsym.size))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2019-12-06 20:11:36 -05:00
|
|
|
if sb.Type() == 0 || sb.Type() == sym.SXREF {
|
|
|
|
|
sb.SetType(sym.SNOPTRBSS)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
if uint(elfsym.shndx) >= elfobj.nsect || elfsym.shndx == 0 {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// even when we pass needSym == 1 to readelfsym, it might still return nil to skip some unwanted symbols
|
2019-12-06 20:11:36 -05:00
|
|
|
if elfsym.sym == 0 {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
2017-10-04 17:54:04 -04:00
|
|
|
sect = &elfobj.sect[elfsym.shndx]
|
2019-12-06 20:11:36 -05:00
|
|
|
if sect.sym == 0 {
|
2017-10-04 17:54:04 -04:00
|
|
|
if strings.HasPrefix(elfsym.name, ".Linfo_string") { // clang does this
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
2015-11-04 18:20:57 -08:00
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
if elfsym.name == "" && elfsym.type_ == 0 && sect.name == ".debug_str" {
|
2015-11-04 18:20:57 -08:00
|
|
|
// This reportedly happens with clang 3.7 on ARM.
|
|
|
|
|
// See issue 13139.
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 10:12:18 +00:00
|
|
|
if strings.HasPrefix(elfsym.name, "$d") && elfsym.type_ == 0 && sect.name == ".debug_frame" {
|
|
|
|
|
// "$d" is a marker, not a real symbol.
|
|
|
|
|
// This happens with gcc on ARM64.
|
|
|
|
|
// See https://sourceware.org/bugzilla/show_bug.cgi?id=21809
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
if strings.HasPrefix(elfsym.name, ".LASF") { // gcc on s390x does this
|
2016-03-18 16:57:54 -04:00
|
|
|
continue
|
|
|
|
|
}
|
2021-11-10 19:51:34 +01:00
|
|
|
return errorf("%v: sym#%d (%s): ignoring symbol in section %d (type %d)", elfsym.sym, i, elfsym.name, elfsym.shndx, elfsym.type_)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
s := elfsym.sym
|
2019-12-06 20:11:36 -05:00
|
|
|
if l.OuterSym(s) != 0 {
|
|
|
|
|
if l.AttrDuplicateOK(s) {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
2019-12-06 20:11:36 -05:00
|
|
|
return errorf("duplicate symbol reference: %s in both %s and %s",
|
|
|
|
|
l.SymName(s), l.SymName(l.OuterSym(s)), l.SymName(sect.sym))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-02-12 17:23:47 -05:00
|
|
|
sectsb := l.MakeSymbolUpdater(sect.sym)
|
|
|
|
|
sb := l.MakeSymbolUpdater(s)
|
2019-12-06 20:11:36 -05:00
|
|
|
|
|
|
|
|
sb.SetType(sectsb.Type())
|
2020-06-30 11:30:28 -04:00
|
|
|
sectsb.AddInteriorSym(s)
|
2019-12-06 20:11:36 -05:00
|
|
|
if !l.AttrCgoExportDynamic(s) {
|
|
|
|
|
sb.SetDynimplib("") // satisfy dynimport
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2019-12-06 20:11:36 -05:00
|
|
|
sb.SetValue(int64(elfsym.value))
|
|
|
|
|
sb.SetSize(int64(elfsym.size))
|
|
|
|
|
if sectsb.Type() == sym.STEXT {
|
|
|
|
|
if l.AttrExternal(s) && !l.AttrDuplicateOK(s) {
|
|
|
|
|
return errorf("%s: duplicate symbol definition", sb.Name())
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2019-12-06 20:11:36 -05:00
|
|
|
l.SetAttrExternal(s, true)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
if elf.Machine(elfobj.machine) == elf.EM_PPC64 {
|
2017-10-04 17:54:04 -04:00
|
|
|
flag := int(elfsym.other) >> 5
|
2022-09-19 16:48:49 -05:00
|
|
|
switch flag {
|
|
|
|
|
case 0:
|
|
|
|
|
// No local entry. R2 is preserved.
|
|
|
|
|
case 1:
|
|
|
|
|
// These require R2 be saved and restored by the caller. This isn't supported today.
|
|
|
|
|
return errorf("%s: unable to handle local entry type 1", sb.Name())
|
|
|
|
|
case 7:
|
2019-12-06 20:11:36 -05:00
|
|
|
return errorf("%s: invalid sym.other 0x%x", sb.Name(), elfsym.other)
|
2022-09-19 16:48:49 -05:00
|
|
|
default:
|
|
|
|
|
l.SetSymLocalentry(s, 4<<uint(flag-2))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort outer lists by address, adding to textp.
|
|
|
|
|
// This keeps textp in increasing address order.
|
2017-10-01 14:39:04 +11:00
|
|
|
for i := uint(0); i < elfobj.nsect; i++ {
|
|
|
|
|
s := elfobj.sect[i].sym
|
2019-12-06 20:11:36 -05:00
|
|
|
if s == 0 {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
2020-02-12 17:23:47 -05:00
|
|
|
sb := l.MakeSymbolUpdater(s)
|
2019-12-06 20:11:36 -05:00
|
|
|
if l.SubSym(s) != 0 {
|
|
|
|
|
sb.SortSub()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2019-12-06 20:11:36 -05:00
|
|
|
if sb.Type() == sym.STEXT {
|
|
|
|
|
if l.AttrOnList(s) {
|
|
|
|
|
return errorf("symbol %s listed multiple times",
|
|
|
|
|
l.SymName(s))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2019-12-06 20:11:36 -05:00
|
|
|
l.SetAttrOnList(s, true)
|
2017-10-06 16:01:02 -04:00
|
|
|
textp = append(textp, s)
|
2019-12-06 20:11:36 -05:00
|
|
|
for ss := l.SubSym(s); ss != 0; ss = l.SubSym(ss) {
|
|
|
|
|
if l.AttrOnList(ss) {
|
|
|
|
|
return errorf("symbol %s listed multiple times",
|
|
|
|
|
l.SymName(ss))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2019-12-06 20:11:36 -05:00
|
|
|
l.SetAttrOnList(ss, true)
|
|
|
|
|
textp = append(textp, ss)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// load relocations
|
2017-10-01 14:39:04 +11:00
|
|
|
for i := uint(0); i < elfobj.nsect; i++ {
|
|
|
|
|
rsect := &elfobj.sect[i]
|
2020-04-26 15:47:18 +08:00
|
|
|
if rsect.type_ != elf.SHT_RELA && rsect.type_ != elf.SHT_REL {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if rsect.info >= uint32(elfobj.nsect) || elfobj.sect[rsect.info].base == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
sect = &elfobj.sect[rsect.info]
|
2017-04-20 10:45:01 +10:00
|
|
|
if err := elfmap(elfobj, rsect); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file: %v", err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-01 14:39:04 +11:00
|
|
|
rela := 0
|
2020-04-26 15:47:18 +08:00
|
|
|
if rsect.type_ == elf.SHT_RELA {
|
2015-02-27 22:57:28 -05:00
|
|
|
rela = 1
|
|
|
|
|
}
|
2017-10-01 14:39:04 +11:00
|
|
|
n := int(rsect.size / uint64(4+4*is64) / uint64(2+rela))
|
|
|
|
|
p := rsect.base
|
2020-04-09 19:45:48 -04:00
|
|
|
sb := l.MakeSymbolUpdater(sect.sym)
|
2017-10-01 14:39:04 +11:00
|
|
|
for j := 0; j < n; j++ {
|
|
|
|
|
var add uint64
|
2019-12-06 16:13:44 +08:00
|
|
|
var symIdx int
|
|
|
|
|
var relocType uint64
|
2020-04-09 19:45:48 -04:00
|
|
|
var rOff int32
|
|
|
|
|
var rAdd int64
|
|
|
|
|
var rSym loader.Sym
|
2019-12-06 16:13:44 +08:00
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
if is64 != 0 {
|
|
|
|
|
// 64-bit rel/rela
|
2020-04-09 19:45:48 -04:00
|
|
|
rOff = int32(e.Uint64(p))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
p = p[8:]
|
2019-12-06 16:13:44 +08:00
|
|
|
switch arch.Family {
|
|
|
|
|
case sys.MIPS64:
|
|
|
|
|
// https://www.linux-mips.org/pub/linux/mips/doc/ABI/elf64-2.4.pdf
|
|
|
|
|
// The doc shows it's different with general Linux ELF
|
|
|
|
|
symIdx = int(e.Uint32(p))
|
|
|
|
|
relocType = uint64(p[7])
|
|
|
|
|
default:
|
|
|
|
|
info := e.Uint64(p)
|
|
|
|
|
relocType = info & 0xffffffff
|
|
|
|
|
symIdx = int(info >> 32)
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
p = p[8:]
|
|
|
|
|
if rela != 0 {
|
|
|
|
|
add = e.Uint64(p)
|
|
|
|
|
p = p[8:]
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 32-bit rel/rela
|
2020-04-09 19:45:48 -04:00
|
|
|
rOff = int32(e.Uint32(p))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
p = p[4:]
|
2019-12-06 16:13:44 +08:00
|
|
|
info := e.Uint32(p)
|
|
|
|
|
relocType = uint64(info & 0xff)
|
|
|
|
|
symIdx = int(info >> 8)
|
2015-02-27 22:57:28 -05:00
|
|
|
p = p[4:]
|
|
|
|
|
if rela != 0 {
|
|
|
|
|
add = uint64(e.Uint32(p))
|
|
|
|
|
p = p[4:]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 16:13:44 +08:00
|
|
|
if relocType == 0 { // skip R_*_NONE relocation
|
2015-02-27 22:57:28 -05:00
|
|
|
j--
|
|
|
|
|
n--
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 16:13:44 +08:00
|
|
|
if symIdx == 0 { // absolute relocation, don't bother reading the null symbol
|
2020-04-09 19:45:48 -04:00
|
|
|
rSym = 0
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2017-10-04 17:54:04 -04:00
|
|
|
var elfsym ElfSym
|
2020-01-31 14:45:52 -05:00
|
|
|
if err := readelfsym(newSym, lookup, l, arch, elfobj, int(symIdx), &elfsym, 0, 0); err != nil {
|
2017-10-06 16:01:02 -04:00
|
|
|
return errorf("malformed elf file: %v", err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2019-12-06 16:13:44 +08:00
|
|
|
elfsym.sym = symbols[symIdx]
|
2019-12-06 20:11:36 -05:00
|
|
|
if elfsym.sym == 0 {
|
2020-01-31 14:45:52 -05:00
|
|
|
return errorf("malformed elf file: %s#%d: reloc of invalid sym #%d %s shndx=%d type=%d", l.SymName(sect.sym), j, int(symIdx), elfsym.name, elfsym.shndx, elfsym.type_)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 19:45:48 -04:00
|
|
|
rSym = elfsym.sym
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 19:45:48 -04:00
|
|
|
rType := objabi.ElfRelocOffset + objabi.RelocType(relocType)
|
2021-03-23 09:51:43 -05:00
|
|
|
rSize, addendSize, err := relSize(arch, pn, uint32(relocType))
|
2017-10-06 16:01:02 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
if rela != 0 {
|
2020-04-09 19:45:48 -04:00
|
|
|
rAdd = int64(add)
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
|
|
|
|
// load addend from image
|
2020-04-09 19:45:48 -04:00
|
|
|
if rSize == 4 {
|
|
|
|
|
rAdd = int64(e.Uint32(sect.base[rOff:]))
|
|
|
|
|
} else if rSize == 8 {
|
|
|
|
|
rAdd = int64(e.Uint64(sect.base[rOff:]))
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2020-04-09 19:45:48 -04:00
|
|
|
return errorf("invalid rela size %d", rSize)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 09:51:43 -05:00
|
|
|
if addendSize == 2 {
|
2020-04-09 19:45:48 -04:00
|
|
|
rAdd = int64(int16(rAdd))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2021-03-23 09:51:43 -05:00
|
|
|
if addendSize == 4 {
|
2020-04-09 19:45:48 -04:00
|
|
|
rAdd = int64(int32(rAdd))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-09 19:45:48 -04:00
|
|
|
r, _ := sb.AddRel(rType)
|
|
|
|
|
r.SetOff(rOff)
|
|
|
|
|
r.SetSiz(rSize)
|
|
|
|
|
r.SetSym(rSym)
|
|
|
|
|
r.SetAdd(rAdd)
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2020-04-09 19:45:48 -04:00
|
|
|
sb.SortRelocs() // just in case
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-06 16:01:02 -04:00
|
|
|
|
|
|
|
|
return textp, ehdrFlags, nil
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func section(elfobj *ElfObj, name string) *ElfSect {
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; uint(i) < elfobj.nsect; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
if elfobj.sect[i].name != "" && name != "" && elfobj.sect[i].name == name {
|
|
|
|
|
return &elfobj.sect[i]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func elfmap(elfobj *ElfObj, sect *ElfSect) (err error) {
|
|
|
|
|
if sect.base != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if sect.off+sect.size > uint64(elfobj.length) {
|
|
|
|
|
err = fmt.Errorf("elf section past end of file")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-08 18:46:04 -04:00
|
|
|
elfobj.f.MustSeek(int64(uint64(elfobj.base)+sect.off), 0)
|
2020-02-13 10:38:33 -05:00
|
|
|
sect.base, sect.readOnlyMem, err = elfobj.f.Slice(uint64(sect.size))
|
|
|
|
|
if err != nil {
|
2016-04-08 18:19:10 +02:00
|
|
|
return fmt.Errorf("short read: %v", err)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-06 20:11:36 -05:00
|
|
|
func readelfsym(newSym, lookup func(string, int) loader.Sym, l *loader.Loader, arch *sys.Arch, elfobj *ElfObj, i int, elfsym *ElfSym, needSym int, localSymVersion int) (err error) {
|
2015-02-27 22:57:28 -05:00
|
|
|
if i >= elfobj.nsymtab || i < 0 {
|
|
|
|
|
err = fmt.Errorf("invalid elf symbol index")
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if i == 0 {
|
2017-10-06 16:01:02 -04:00
|
|
|
return fmt.Errorf("readym: read null symbol!")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if elfobj.is64 != 0 {
|
2020-04-26 15:47:18 +08:00
|
|
|
b := new(elf.Sym64)
|
|
|
|
|
binary.Read(bytes.NewReader(elfobj.symtab.base[i*elf.Sym64Size:(i+1)*elf.Sym64Size]), elfobj.e, b)
|
|
|
|
|
elfsym.name = cstring(elfobj.symstr.base[b.Name:])
|
|
|
|
|
elfsym.value = b.Value
|
|
|
|
|
elfsym.size = b.Size
|
|
|
|
|
elfsym.shndx = elf.SectionIndex(b.Shndx)
|
|
|
|
|
elfsym.bind = elf.ST_BIND(b.Info)
|
|
|
|
|
elfsym.type_ = elf.ST_TYPE(b.Info)
|
2017-10-04 17:54:04 -04:00
|
|
|
elfsym.other = b.Other
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2020-04-26 15:47:18 +08:00
|
|
|
b := new(elf.Sym32)
|
|
|
|
|
binary.Read(bytes.NewReader(elfobj.symtab.base[i*elf.Sym32Size:(i+1)*elf.Sym32Size]), elfobj.e, b)
|
|
|
|
|
elfsym.name = cstring(elfobj.symstr.base[b.Name:])
|
|
|
|
|
elfsym.value = uint64(b.Value)
|
|
|
|
|
elfsym.size = uint64(b.Size)
|
|
|
|
|
elfsym.shndx = elf.SectionIndex(b.Shndx)
|
|
|
|
|
elfsym.bind = elf.ST_BIND(b.Info)
|
|
|
|
|
elfsym.type_ = elf.ST_TYPE(b.Info)
|
2017-10-04 17:54:04 -04:00
|
|
|
elfsym.other = b.Other
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2019-12-06 20:11:36 -05:00
|
|
|
var s loader.Sym
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
if elfsym.name == "_GLOBAL_OFFSET_TABLE_" {
|
|
|
|
|
elfsym.name = ".got"
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-04 17:54:04 -04:00
|
|
|
if elfsym.name == ".TOC." {
|
2015-02-27 22:57:28 -05:00
|
|
|
// Magic symbol on ppc64. Will be set to this object
|
|
|
|
|
// file's .got+0x8000.
|
2020-04-26 15:47:18 +08:00
|
|
|
elfsym.bind = elf.STB_LOCAL
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
switch elfsym.type_ {
|
2020-04-26 15:47:18 +08:00
|
|
|
case elf.STT_SECTION:
|
2017-10-04 17:54:04 -04:00
|
|
|
s = elfobj.sect[elfsym.shndx].sym
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
case elf.STT_OBJECT, elf.STT_FUNC, elf.STT_NOTYPE, elf.STT_COMMON:
|
2017-10-04 17:54:04 -04:00
|
|
|
switch elfsym.bind {
|
2020-04-26 15:47:18 +08:00
|
|
|
case elf.STB_GLOBAL:
|
2015-02-27 22:57:28 -05:00
|
|
|
if needSym != 0 {
|
2019-10-17 11:06:11 -04:00
|
|
|
s = lookup(elfsym.name, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// for global scoped hidden symbols we should insert it into
|
|
|
|
|
// symbol hash table, but mark them as hidden.
|
|
|
|
|
// __i686.get_pc_thunk.bx is allowed to be duplicated, to
|
|
|
|
|
// workaround that we set dupok.
|
|
|
|
|
// TODO(minux): correctly handle __i686.get_pc_thunk.bx without
|
2018-06-01 17:29:59 -03:00
|
|
|
// set dupok generally. See https://golang.org/cl/5823055
|
2015-02-27 22:57:28 -05:00
|
|
|
// comment #5 for details.
|
2019-12-06 20:11:36 -05:00
|
|
|
if s != 0 && elfsym.other == 2 {
|
|
|
|
|
if !l.IsExternal(s) {
|
2020-02-12 17:23:47 -05:00
|
|
|
l.MakeSymbolUpdater(s)
|
2019-12-06 20:11:36 -05:00
|
|
|
}
|
|
|
|
|
l.SetAttrDuplicateOK(s, true)
|
|
|
|
|
l.SetAttrVisibilityHidden(s, true)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
case elf.STB_LOCAL:
|
2018-09-26 10:12:18 +00:00
|
|
|
if (arch.Family == sys.ARM || arch.Family == sys.ARM64) && (strings.HasPrefix(elfsym.name, "$a") || strings.HasPrefix(elfsym.name, "$d") || strings.HasPrefix(elfsym.name, "$x")) {
|
|
|
|
|
// binutils for arm and arm64 generate these mapping
|
2015-02-27 22:57:28 -05:00
|
|
|
// symbols, ignore these
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
if elfsym.name == ".TOC." {
|
2015-02-27 22:57:28 -05:00
|
|
|
// We need to be able to look this up,
|
|
|
|
|
// so put it in the hash table.
|
|
|
|
|
if needSym != 0 {
|
2019-10-17 11:06:11 -04:00
|
|
|
s = lookup(elfsym.name, localSymVersion)
|
2019-12-06 20:11:36 -05:00
|
|
|
l.SetAttrVisibilityHidden(s, true)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if needSym != 0 {
|
2016-02-24 11:55:20 +01:00
|
|
|
// local names and hidden global names are unique
|
|
|
|
|
// and should only be referenced by their index, not name, so we
|
|
|
|
|
// don't bother to add them into the hash table
|
2019-11-22 15:27:01 -05:00
|
|
|
// FIXME: pass empty string here for name? This would
|
|
|
|
|
// reduce mem use, but also (possibly) make it harder
|
|
|
|
|
// to debug problems.
|
2019-10-17 11:06:11 -04:00
|
|
|
s = newSym(elfsym.name, localSymVersion)
|
2019-12-06 20:11:36 -05:00
|
|
|
l.SetAttrVisibilityHidden(s, true)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-04-26 15:47:18 +08:00
|
|
|
case elf.STB_WEAK:
|
2015-02-27 22:57:28 -05:00
|
|
|
if needSym != 0 {
|
2019-10-17 11:06:11 -04:00
|
|
|
s = lookup(elfsym.name, 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
if elfsym.other == 2 {
|
2019-12-06 20:11:36 -05:00
|
|
|
l.SetAttrVisibilityHidden(s, true)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2019-03-28 00:53:19 +11:00
|
|
|
|
|
|
|
|
// Allow weak symbols to be duplicated when already defined.
|
2019-12-06 20:11:36 -05:00
|
|
|
if l.OuterSym(s) != 0 {
|
|
|
|
|
l.SetAttrDuplicateOK(s, true)
|
2019-03-28 00:53:19 +11:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
2017-10-04 17:54:04 -04:00
|
|
|
err = fmt.Errorf("%s: invalid symbol binding %d", elfsym.name, elfsym.bind)
|
2015-02-27 22:57:28 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-05 13:46:15 -04:00
|
|
|
if s != 0 && l.SymType(s) == 0 && elfsym.type_ != elf.STT_SECTION {
|
2020-02-12 17:23:47 -05:00
|
|
|
sb := l.MakeSymbolUpdater(s)
|
2019-12-06 20:11:36 -05:00
|
|
|
sb.SetType(sym.SXREF)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-04 17:54:04 -04:00
|
|
|
elfsym.sym = s
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 09:51:43 -05:00
|
|
|
// Return the size of the relocated field, and the size of the addend as the first
|
|
|
|
|
// and second values. Note, the addend may be larger than the relocation field in
|
|
|
|
|
// some cases when a relocated value is split across multiple relocations.
|
|
|
|
|
func relSize(arch *sys.Arch, pn string, elftype uint32) (uint8, uint8, error) {
|
2016-04-06 20:06:12 -07:00
|
|
|
// TODO(mdempsky): Replace this with a struct-valued switch statement
|
|
|
|
|
// once golang.org/issue/15164 is fixed or found to not impair cmd/link
|
|
|
|
|
// performance.
|
2016-04-06 12:01:40 -07:00
|
|
|
|
2016-04-06 20:06:12 -07:00
|
|
|
const (
|
2020-10-20 22:04:55 +11:00
|
|
|
AMD64 = uint32(sys.AMD64)
|
|
|
|
|
ARM = uint32(sys.ARM)
|
|
|
|
|
ARM64 = uint32(sys.ARM64)
|
|
|
|
|
I386 = uint32(sys.I386)
|
2021-08-15 16:25:46 +08:00
|
|
|
LOONG64 = uint32(sys.Loong64)
|
2020-10-20 22:04:55 +11:00
|
|
|
MIPS = uint32(sys.MIPS)
|
|
|
|
|
MIPS64 = uint32(sys.MIPS64)
|
|
|
|
|
PPC64 = uint32(sys.PPC64)
|
|
|
|
|
RISCV64 = uint32(sys.RISCV64)
|
|
|
|
|
S390X = uint32(sys.S390X)
|
2016-04-06 20:06:12 -07:00
|
|
|
)
|
|
|
|
|
|
2018-09-26 10:12:18 +00:00
|
|
|
switch uint32(arch.Family) | elftype<<16 {
|
2015-02-27 22:57:28 -05:00
|
|
|
default:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 0, 0, fmt.Errorf("%s: unknown relocation type %d; compiled without -fpic?", pn, elftype)
|
2017-10-06 16:01:02 -04:00
|
|
|
|
2019-12-06 16:13:44 +08:00
|
|
|
case MIPS | uint32(elf.R_MIPS_HI16)<<16,
|
|
|
|
|
MIPS | uint32(elf.R_MIPS_LO16)<<16,
|
|
|
|
|
MIPS | uint32(elf.R_MIPS_GOT16)<<16,
|
2020-12-08 04:23:36 +11:00
|
|
|
MIPS | uint32(elf.R_MIPS_GOT_HI16)<<16,
|
|
|
|
|
MIPS | uint32(elf.R_MIPS_GOT_LO16)<<16,
|
2019-12-06 16:13:44 +08:00
|
|
|
MIPS | uint32(elf.R_MIPS_GPREL16)<<16,
|
|
|
|
|
MIPS | uint32(elf.R_MIPS_GOT_PAGE)<<16,
|
|
|
|
|
MIPS | uint32(elf.R_MIPS_JALR)<<16,
|
|
|
|
|
MIPS | uint32(elf.R_MIPS_GOT_OFST)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_HI16)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_LO16)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_GOT16)<<16,
|
2020-12-08 04:23:36 +11:00
|
|
|
MIPS64 | uint32(elf.R_MIPS_GOT_HI16)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_GOT_LO16)<<16,
|
2019-12-06 16:13:44 +08:00
|
|
|
MIPS64 | uint32(elf.R_MIPS_GPREL16)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_GOT_PAGE)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_JALR)<<16,
|
2020-11-16 17:50:01 +08:00
|
|
|
MIPS64 | uint32(elf.R_MIPS_GOT_OFST)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_CALL16)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_GPREL32)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_64)<<16,
|
|
|
|
|
MIPS64 | uint32(elf.R_MIPS_GOT_DISP)<<16:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 4, 4, nil
|
2021-08-15 16:25:46 +08:00
|
|
|
|
|
|
|
|
case LOONG64 | uint32(elf.R_LARCH_SOP_PUSH_PCREL)<<16,
|
|
|
|
|
LOONG64 | uint32(elf.R_LARCH_SOP_PUSH_GPREL)<<16,
|
|
|
|
|
LOONG64 | uint32(elf.R_LARCH_SOP_PUSH_ABSOLUTE)<<16,
|
|
|
|
|
LOONG64 | uint32(elf.R_LARCH_MARK_LA)<<16,
|
|
|
|
|
LOONG64 | uint32(elf.R_LARCH_SOP_POP_32_S_0_10_10_16_S2)<<16,
|
|
|
|
|
LOONG64 | uint32(elf.R_LARCH_64)<<16,
|
2022-08-03 19:43:49 +08:00
|
|
|
LOONG64 | uint32(elf.R_LARCH_MARK_PCREL)<<16,
|
|
|
|
|
LOONG64 | uint32(elf.R_LARCH_32_PCREL)<<16:
|
2021-08-15 16:25:46 +08:00
|
|
|
return 4, 4, nil
|
2019-12-06 16:13:44 +08:00
|
|
|
|
2018-09-26 10:12:18 +00:00
|
|
|
case S390X | uint32(elf.R_390_8)<<16:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 1, 1, nil
|
2017-10-06 16:01:02 -04:00
|
|
|
|
2018-09-26 10:12:18 +00:00
|
|
|
case PPC64 | uint32(elf.R_PPC64_TOC16)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_16)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_GOT16)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_PC16)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_PC16DBL)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_PLT16DBL)<<16:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 2, 2, nil
|
2017-10-06 16:01:02 -04:00
|
|
|
|
2018-09-26 10:12:18 +00:00
|
|
|
case ARM | uint32(elf.R_ARM_ABS32)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_GOT32)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_PLT32)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_GOTOFF)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_GOTPC)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_THM_PC22)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_REL32)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_CALL)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_V4BX)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_GOT_PREL)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_PC24)<<16,
|
|
|
|
|
ARM | uint32(elf.R_ARM_JUMP24)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_CALL26)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_ADR_GOT_PAGE)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_LD64_GOT_LO12_NC)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_ADR_PREL_PG_HI21)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_ADD_ABS_LO12_NC)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_LDST8_ABS_LO12_NC)<<16,
|
2020-11-18 04:00:57 +00:00
|
|
|
ARM64 | uint32(elf.R_AARCH64_LDST16_ABS_LO12_NC)<<16,
|
2018-09-26 10:12:18 +00:00
|
|
|
ARM64 | uint32(elf.R_AARCH64_LDST32_ABS_LO12_NC)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_LDST64_ABS_LO12_NC)<<16,
|
2019-05-11 02:21:22 +10:00
|
|
|
ARM64 | uint32(elf.R_AARCH64_LDST128_ABS_LO12_NC)<<16,
|
2018-09-26 10:12:18 +00:00
|
|
|
ARM64 | uint32(elf.R_AARCH64_PREL32)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_JUMP26)<<16,
|
|
|
|
|
AMD64 | uint32(elf.R_X86_64_PC32)<<16,
|
|
|
|
|
AMD64 | uint32(elf.R_X86_64_PLT32)<<16,
|
|
|
|
|
AMD64 | uint32(elf.R_X86_64_GOTPCREL)<<16,
|
|
|
|
|
AMD64 | uint32(elf.R_X86_64_GOTPCRELX)<<16,
|
|
|
|
|
AMD64 | uint32(elf.R_X86_64_REX_GOTPCRELX)<<16,
|
|
|
|
|
I386 | uint32(elf.R_386_32)<<16,
|
|
|
|
|
I386 | uint32(elf.R_386_PC32)<<16,
|
|
|
|
|
I386 | uint32(elf.R_386_GOT32)<<16,
|
|
|
|
|
I386 | uint32(elf.R_386_PLT32)<<16,
|
|
|
|
|
I386 | uint32(elf.R_386_GOTOFF)<<16,
|
|
|
|
|
I386 | uint32(elf.R_386_GOTPC)<<16,
|
|
|
|
|
I386 | uint32(elf.R_386_GOT32X)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_REL24)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC_REL32)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_32)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_PC32)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_GOT32)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_PLT32)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_PC32DBL)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_PLT32DBL)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_GOTPCDBL)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_GOTENT)<<16:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 4, 4, nil
|
2017-10-06 16:01:02 -04:00
|
|
|
|
2018-09-26 10:12:18 +00:00
|
|
|
case AMD64 | uint32(elf.R_X86_64_64)<<16,
|
|
|
|
|
AMD64 | uint32(elf.R_X86_64_PC64)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_ABS64)<<16,
|
|
|
|
|
ARM64 | uint32(elf.R_AARCH64_PREL64)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_ADDR64)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_GLOB_DAT)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_RELATIVE)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_GOTOFF)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_GOTPC)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_64)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_PC64)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_GOT64)<<16,
|
|
|
|
|
S390X | uint32(elf.R_390_PLT64)<<16:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 8, 8, nil
|
2020-10-20 22:04:55 +11:00
|
|
|
|
2022-09-18 02:32:42 +10:00
|
|
|
case RISCV64 | uint32(elf.R_RISCV_SET6)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_SUB6)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_SET8)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_SUB8)<<16:
|
|
|
|
|
return 1, 1, nil
|
|
|
|
|
|
2020-10-20 22:04:55 +11:00
|
|
|
case RISCV64 | uint32(elf.R_RISCV_RVC_BRANCH)<<16,
|
2022-09-18 02:32:42 +10:00
|
|
|
RISCV64 | uint32(elf.R_RISCV_RVC_JUMP)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_SET16)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_SUB16)<<16:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 2, 2, nil
|
2020-10-20 22:04:55 +11:00
|
|
|
|
|
|
|
|
case RISCV64 | uint32(elf.R_RISCV_32)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_BRANCH)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_HI20)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_LO12_I)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_LO12_S)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_GOT_HI20)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_PCREL_HI20)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_PCREL_LO12_I)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_PCREL_LO12_S)<<16,
|
2022-09-18 02:32:42 +10:00
|
|
|
RISCV64 | uint32(elf.R_RISCV_ADD32)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_SET32)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_SUB32)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_32_PCREL)<<16,
|
2020-10-20 22:04:55 +11:00
|
|
|
RISCV64 | uint32(elf.R_RISCV_RELAX)<<16:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 4, 4, nil
|
2020-10-20 22:04:55 +11:00
|
|
|
|
|
|
|
|
case RISCV64 | uint32(elf.R_RISCV_64)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_CALL)<<16,
|
|
|
|
|
RISCV64 | uint32(elf.R_RISCV_CALL_PLT)<<16:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 8, 8, nil
|
|
|
|
|
|
|
|
|
|
case PPC64 | uint32(elf.R_PPC64_TOC16_LO)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_TOC16_HI)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_TOC16_HA)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_TOC16_DS)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_TOC16_LO_DS)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_REL16_LO)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_REL16_HI)<<16,
|
2022-06-13 10:59:31 -05:00
|
|
|
PPC64 | uint32(elf.R_PPC64_REL16_HA)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_PLT16_HA)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_PLT16_LO_DS)<<16:
|
2021-03-23 09:51:43 -05:00
|
|
|
return 2, 4, nil
|
2022-06-13 10:59:31 -05:00
|
|
|
|
|
|
|
|
// PPC64 inline PLT sequence hint relocations (-fno-plt)
|
|
|
|
|
// These are informational annotations to assist linker optimizations.
|
|
|
|
|
case PPC64 | uint32(elf.R_PPC64_PLTSEQ)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_PLTCALL)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_PLTCALL_NOTOC)<<16,
|
|
|
|
|
PPC64 | uint32(elf.R_PPC64_PLTSEQ_NOTOC)<<16:
|
|
|
|
|
return 0, 0, nil
|
|
|
|
|
|
2017-10-06 16:01:02 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func cstring(x []byte) string {
|
|
|
|
|
i := bytes.IndexByte(x, '\x00')
|
|
|
|
|
if i >= 0 {
|
|
|
|
|
x = x[:i]
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-06 16:01:02 -04:00
|
|
|
return string(x)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|