2015-02-27 22:57:28 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package ld
|
|
|
|
|
|
|
|
|
|
import (
|
2017-04-18 12:53:25 -07:00
|
|
|
"cmd/internal/objabi"
|
2016-04-06 12:01:40 -07:00
|
|
|
"cmd/internal/sys"
|
2017-10-04 17:54:04 -04:00
|
|
|
"cmd/link/internal/sym"
|
2015-05-25 13:59:08 +12:00
|
|
|
"crypto/sha1"
|
2015-03-01 22:02:13 -05:00
|
|
|
"encoding/binary"
|
2016-03-03 22:37:14 -08:00
|
|
|
"encoding/hex"
|
2016-12-05 23:20:20 -05:00
|
|
|
"io"
|
2015-05-25 14:51:02 +12:00
|
|
|
"path/filepath"
|
2015-05-25 13:59:08 +12:00
|
|
|
"sort"
|
2015-05-25 14:51:02 +12:00
|
|
|
"strings"
|
2015-02-27 22:57:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Derived from:
|
|
|
|
|
* $FreeBSD: src/sys/sys/elf32.h,v 1.8.14.1 2005/12/30 22:13:58 marcel Exp $
|
|
|
|
|
* $FreeBSD: src/sys/sys/elf64.h,v 1.10.14.1 2005/12/30 22:13:58 marcel Exp $
|
|
|
|
|
* $FreeBSD: src/sys/sys/elf_common.h,v 1.15.8.1 2005/12/30 22:13:58 marcel Exp $
|
|
|
|
|
* $FreeBSD: src/sys/alpha/include/elf.h,v 1.14 2003/09/25 01:10:22 peter Exp $
|
|
|
|
|
* $FreeBSD: src/sys/amd64/include/elf.h,v 1.18 2004/08/03 08:21:48 dfr Exp $
|
|
|
|
|
* $FreeBSD: src/sys/arm/include/elf.h,v 1.5.2.1 2006/06/30 21:42:52 cognet Exp $
|
|
|
|
|
* $FreeBSD: src/sys/i386/include/elf.h,v 1.16 2004/08/02 19:12:17 dfr Exp $
|
|
|
|
|
* $FreeBSD: src/sys/powerpc/include/elf.h,v 1.7 2004/11/02 09:47:01 ssouhlal Exp $
|
|
|
|
|
* $FreeBSD: src/sys/sparc64/include/elf.h,v 1.12 2003/09/25 01:10:26 peter Exp $
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 1996-1998 John D. Polstra. All rights reserved.
|
|
|
|
|
* Copyright (c) 2001 David E. O'Brien
|
2016-04-10 14:32:26 -07:00
|
|
|
* Portions Copyright 2009 The Go Authors. All rights reserved.
|
2015-02-27 22:57:28 -05:00
|
|
|
*
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
|
* are met:
|
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* ELF definitions that are independent of architecture or word size.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Note header. The ".note" section contains an array of notes. Each
|
|
|
|
|
* begins with this header, aligned to a word boundary. Immediately
|
|
|
|
|
* following the note header is n_namesz bytes of name, padded to the
|
|
|
|
|
* next word boundary. Then comes n_descsz bytes of descriptor, again
|
|
|
|
|
* padded to a word boundary. The values of n_namesz and n_descsz do
|
|
|
|
|
* not include the padding.
|
|
|
|
|
*/
|
2016-08-22 10:33:13 -04:00
|
|
|
type elfNote struct {
|
|
|
|
|
nNamesz uint32
|
|
|
|
|
nDescsz uint32
|
|
|
|
|
nType uint32
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
EI_MAG0 = 0
|
|
|
|
|
EI_MAG1 = 1
|
|
|
|
|
EI_MAG2 = 2
|
|
|
|
|
EI_MAG3 = 3
|
|
|
|
|
EI_CLASS = 4
|
|
|
|
|
EI_DATA = 5
|
|
|
|
|
EI_VERSION = 6
|
|
|
|
|
EI_OSABI = 7
|
|
|
|
|
EI_ABIVERSION = 8
|
|
|
|
|
OLD_EI_BRAND = 8
|
|
|
|
|
EI_PAD = 9
|
|
|
|
|
EI_NIDENT = 16
|
|
|
|
|
ELFMAG0 = 0x7f
|
|
|
|
|
ELFMAG1 = 'E'
|
|
|
|
|
ELFMAG2 = 'L'
|
|
|
|
|
ELFMAG3 = 'F'
|
|
|
|
|
SELFMAG = 4
|
|
|
|
|
EV_NONE = 0
|
|
|
|
|
EV_CURRENT = 1
|
|
|
|
|
ELFCLASSNONE = 0
|
|
|
|
|
ELFCLASS32 = 1
|
|
|
|
|
ELFCLASS64 = 2
|
|
|
|
|
ELFDATANONE = 0
|
|
|
|
|
ELFDATA2LSB = 1
|
|
|
|
|
ELFDATA2MSB = 2
|
|
|
|
|
ELFOSABI_NONE = 0
|
|
|
|
|
ELFOSABI_HPUX = 1
|
|
|
|
|
ELFOSABI_NETBSD = 2
|
|
|
|
|
ELFOSABI_LINUX = 3
|
|
|
|
|
ELFOSABI_HURD = 4
|
|
|
|
|
ELFOSABI_86OPEN = 5
|
|
|
|
|
ELFOSABI_SOLARIS = 6
|
|
|
|
|
ELFOSABI_AIX = 7
|
|
|
|
|
ELFOSABI_IRIX = 8
|
|
|
|
|
ELFOSABI_FREEBSD = 9
|
|
|
|
|
ELFOSABI_TRU64 = 10
|
|
|
|
|
ELFOSABI_MODESTO = 11
|
|
|
|
|
ELFOSABI_OPENBSD = 12
|
|
|
|
|
ELFOSABI_OPENVMS = 13
|
|
|
|
|
ELFOSABI_NSK = 14
|
|
|
|
|
ELFOSABI_ARM = 97
|
|
|
|
|
ELFOSABI_STANDALONE = 255
|
|
|
|
|
ELFOSABI_SYSV = ELFOSABI_NONE
|
|
|
|
|
ELFOSABI_MONTEREY = ELFOSABI_AIX
|
|
|
|
|
ET_NONE = 0
|
|
|
|
|
ET_REL = 1
|
|
|
|
|
ET_EXEC = 2
|
|
|
|
|
ET_DYN = 3
|
|
|
|
|
ET_CORE = 4
|
|
|
|
|
ET_LOOS = 0xfe00
|
|
|
|
|
ET_HIOS = 0xfeff
|
|
|
|
|
ET_LOPROC = 0xff00
|
|
|
|
|
ET_HIPROC = 0xffff
|
|
|
|
|
EM_NONE = 0
|
|
|
|
|
EM_M32 = 1
|
|
|
|
|
EM_SPARC = 2
|
|
|
|
|
EM_386 = 3
|
|
|
|
|
EM_68K = 4
|
|
|
|
|
EM_88K = 5
|
|
|
|
|
EM_860 = 7
|
|
|
|
|
EM_MIPS = 8
|
|
|
|
|
EM_S370 = 9
|
|
|
|
|
EM_MIPS_RS3_LE = 10
|
|
|
|
|
EM_PARISC = 15
|
|
|
|
|
EM_VPP500 = 17
|
|
|
|
|
EM_SPARC32PLUS = 18
|
|
|
|
|
EM_960 = 19
|
|
|
|
|
EM_PPC = 20
|
|
|
|
|
EM_PPC64 = 21
|
|
|
|
|
EM_S390 = 22
|
|
|
|
|
EM_V800 = 36
|
|
|
|
|
EM_FR20 = 37
|
|
|
|
|
EM_RH32 = 38
|
|
|
|
|
EM_RCE = 39
|
|
|
|
|
EM_ARM = 40
|
|
|
|
|
EM_SH = 42
|
|
|
|
|
EM_SPARCV9 = 43
|
|
|
|
|
EM_TRICORE = 44
|
|
|
|
|
EM_ARC = 45
|
|
|
|
|
EM_H8_300 = 46
|
|
|
|
|
EM_H8_300H = 47
|
|
|
|
|
EM_H8S = 48
|
|
|
|
|
EM_H8_500 = 49
|
|
|
|
|
EM_IA_64 = 50
|
|
|
|
|
EM_MIPS_X = 51
|
|
|
|
|
EM_COLDFIRE = 52
|
|
|
|
|
EM_68HC12 = 53
|
|
|
|
|
EM_MMA = 54
|
|
|
|
|
EM_PCP = 55
|
|
|
|
|
EM_NCPU = 56
|
|
|
|
|
EM_NDR1 = 57
|
|
|
|
|
EM_STARCORE = 58
|
|
|
|
|
EM_ME16 = 59
|
|
|
|
|
EM_ST100 = 60
|
|
|
|
|
EM_TINYJ = 61
|
|
|
|
|
EM_X86_64 = 62
|
2015-03-08 14:14:53 +01:00
|
|
|
EM_AARCH64 = 183
|
2015-02-27 22:57:28 -05:00
|
|
|
EM_486 = 6
|
|
|
|
|
EM_MIPS_RS4_BE = 10
|
|
|
|
|
EM_ALPHA_STD = 41
|
|
|
|
|
EM_ALPHA = 0x9026
|
|
|
|
|
SHN_UNDEF = 0
|
|
|
|
|
SHN_LORESERVE = 0xff00
|
|
|
|
|
SHN_LOPROC = 0xff00
|
|
|
|
|
SHN_HIPROC = 0xff1f
|
|
|
|
|
SHN_LOOS = 0xff20
|
|
|
|
|
SHN_HIOS = 0xff3f
|
|
|
|
|
SHN_ABS = 0xfff1
|
|
|
|
|
SHN_COMMON = 0xfff2
|
|
|
|
|
SHN_XINDEX = 0xffff
|
|
|
|
|
SHN_HIRESERVE = 0xffff
|
|
|
|
|
SHT_NULL = 0
|
|
|
|
|
SHT_PROGBITS = 1
|
|
|
|
|
SHT_SYMTAB = 2
|
|
|
|
|
SHT_STRTAB = 3
|
|
|
|
|
SHT_RELA = 4
|
|
|
|
|
SHT_HASH = 5
|
|
|
|
|
SHT_DYNAMIC = 6
|
|
|
|
|
SHT_NOTE = 7
|
|
|
|
|
SHT_NOBITS = 8
|
|
|
|
|
SHT_REL = 9
|
|
|
|
|
SHT_SHLIB = 10
|
|
|
|
|
SHT_DYNSYM = 11
|
|
|
|
|
SHT_INIT_ARRAY = 14
|
|
|
|
|
SHT_FINI_ARRAY = 15
|
|
|
|
|
SHT_PREINIT_ARRAY = 16
|
|
|
|
|
SHT_GROUP = 17
|
|
|
|
|
SHT_SYMTAB_SHNDX = 18
|
|
|
|
|
SHT_LOOS = 0x60000000
|
|
|
|
|
SHT_HIOS = 0x6fffffff
|
|
|
|
|
SHT_GNU_VERDEF = 0x6ffffffd
|
|
|
|
|
SHT_GNU_VERNEED = 0x6ffffffe
|
|
|
|
|
SHT_GNU_VERSYM = 0x6fffffff
|
|
|
|
|
SHT_LOPROC = 0x70000000
|
2015-09-23 15:46:00 +12:00
|
|
|
SHT_ARM_ATTRIBUTES = 0x70000003
|
2015-02-27 22:57:28 -05:00
|
|
|
SHT_HIPROC = 0x7fffffff
|
|
|
|
|
SHT_LOUSER = 0x80000000
|
|
|
|
|
SHT_HIUSER = 0xffffffff
|
|
|
|
|
SHF_WRITE = 0x1
|
|
|
|
|
SHF_ALLOC = 0x2
|
|
|
|
|
SHF_EXECINSTR = 0x4
|
|
|
|
|
SHF_MERGE = 0x10
|
|
|
|
|
SHF_STRINGS = 0x20
|
|
|
|
|
SHF_INFO_LINK = 0x40
|
|
|
|
|
SHF_LINK_ORDER = 0x80
|
|
|
|
|
SHF_OS_NONCONFORMING = 0x100
|
|
|
|
|
SHF_GROUP = 0x200
|
|
|
|
|
SHF_TLS = 0x400
|
|
|
|
|
SHF_MASKOS = 0x0ff00000
|
|
|
|
|
SHF_MASKPROC = 0xf0000000
|
|
|
|
|
PT_NULL = 0
|
|
|
|
|
PT_LOAD = 1
|
|
|
|
|
PT_DYNAMIC = 2
|
|
|
|
|
PT_INTERP = 3
|
|
|
|
|
PT_NOTE = 4
|
|
|
|
|
PT_SHLIB = 5
|
|
|
|
|
PT_PHDR = 6
|
|
|
|
|
PT_TLS = 7
|
|
|
|
|
PT_LOOS = 0x60000000
|
|
|
|
|
PT_HIOS = 0x6fffffff
|
|
|
|
|
PT_LOPROC = 0x70000000
|
|
|
|
|
PT_HIPROC = 0x7fffffff
|
|
|
|
|
PT_GNU_STACK = 0x6474e551
|
2016-09-06 08:02:30 -04:00
|
|
|
PT_GNU_RELRO = 0x6474e552
|
2015-02-27 22:57:28 -05:00
|
|
|
PT_PAX_FLAGS = 0x65041580
|
2016-06-15 13:44:03 -07:00
|
|
|
PT_SUNWSTACK = 0x6ffffffb
|
2015-02-27 22:57:28 -05:00
|
|
|
PF_X = 0x1
|
|
|
|
|
PF_W = 0x2
|
|
|
|
|
PF_R = 0x4
|
|
|
|
|
PF_MASKOS = 0x0ff00000
|
|
|
|
|
PF_MASKPROC = 0xf0000000
|
|
|
|
|
DT_NULL = 0
|
|
|
|
|
DT_NEEDED = 1
|
|
|
|
|
DT_PLTRELSZ = 2
|
|
|
|
|
DT_PLTGOT = 3
|
|
|
|
|
DT_HASH = 4
|
|
|
|
|
DT_STRTAB = 5
|
|
|
|
|
DT_SYMTAB = 6
|
|
|
|
|
DT_RELA = 7
|
|
|
|
|
DT_RELASZ = 8
|
|
|
|
|
DT_RELAENT = 9
|
|
|
|
|
DT_STRSZ = 10
|
|
|
|
|
DT_SYMENT = 11
|
|
|
|
|
DT_INIT = 12
|
|
|
|
|
DT_FINI = 13
|
|
|
|
|
DT_SONAME = 14
|
|
|
|
|
DT_RPATH = 15
|
|
|
|
|
DT_SYMBOLIC = 16
|
|
|
|
|
DT_REL = 17
|
|
|
|
|
DT_RELSZ = 18
|
|
|
|
|
DT_RELENT = 19
|
|
|
|
|
DT_PLTREL = 20
|
|
|
|
|
DT_DEBUG = 21
|
|
|
|
|
DT_TEXTREL = 22
|
|
|
|
|
DT_JMPREL = 23
|
|
|
|
|
DT_BIND_NOW = 24
|
|
|
|
|
DT_INIT_ARRAY = 25
|
|
|
|
|
DT_FINI_ARRAY = 26
|
|
|
|
|
DT_INIT_ARRAYSZ = 27
|
|
|
|
|
DT_FINI_ARRAYSZ = 28
|
|
|
|
|
DT_RUNPATH = 29
|
|
|
|
|
DT_FLAGS = 30
|
|
|
|
|
DT_ENCODING = 32
|
|
|
|
|
DT_PREINIT_ARRAY = 32
|
|
|
|
|
DT_PREINIT_ARRAYSZ = 33
|
|
|
|
|
DT_LOOS = 0x6000000d
|
|
|
|
|
DT_HIOS = 0x6ffff000
|
|
|
|
|
DT_LOPROC = 0x70000000
|
|
|
|
|
DT_HIPROC = 0x7fffffff
|
|
|
|
|
DT_VERNEED = 0x6ffffffe
|
|
|
|
|
DT_VERNEEDNUM = 0x6fffffff
|
|
|
|
|
DT_VERSYM = 0x6ffffff0
|
|
|
|
|
DT_PPC64_GLINK = DT_LOPROC + 0
|
|
|
|
|
DT_PPC64_OPT = DT_LOPROC + 3
|
|
|
|
|
DF_ORIGIN = 0x0001
|
|
|
|
|
DF_SYMBOLIC = 0x0002
|
|
|
|
|
DF_TEXTREL = 0x0004
|
|
|
|
|
DF_BIND_NOW = 0x0008
|
|
|
|
|
DF_STATIC_TLS = 0x0010
|
|
|
|
|
NT_PRSTATUS = 1
|
|
|
|
|
NT_FPREGSET = 2
|
|
|
|
|
NT_PRPSINFO = 3
|
|
|
|
|
STB_LOCAL = 0
|
|
|
|
|
STB_GLOBAL = 1
|
|
|
|
|
STB_WEAK = 2
|
|
|
|
|
STB_LOOS = 10
|
|
|
|
|
STB_HIOS = 12
|
|
|
|
|
STB_LOPROC = 13
|
|
|
|
|
STB_HIPROC = 15
|
|
|
|
|
STT_NOTYPE = 0
|
|
|
|
|
STT_OBJECT = 1
|
|
|
|
|
STT_FUNC = 2
|
|
|
|
|
STT_SECTION = 3
|
|
|
|
|
STT_FILE = 4
|
|
|
|
|
STT_COMMON = 5
|
|
|
|
|
STT_TLS = 6
|
|
|
|
|
STT_LOOS = 10
|
|
|
|
|
STT_HIOS = 12
|
|
|
|
|
STT_LOPROC = 13
|
|
|
|
|
STT_HIPROC = 15
|
|
|
|
|
STV_DEFAULT = 0x0
|
|
|
|
|
STV_INTERNAL = 0x1
|
|
|
|
|
STV_HIDDEN = 0x2
|
|
|
|
|
STV_PROTECTED = 0x3
|
|
|
|
|
STN_UNDEF = 0
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/* For accessing the fields of r_info. */
|
|
|
|
|
|
|
|
|
|
/* For constructing r_info from field values. */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Relocation types.
|
|
|
|
|
*/
|
|
|
|
|
const (
|
2015-08-11 14:10:03 +12:00
|
|
|
ARM_MAGIC_TRAMP_NUMBER = 0x5c000003
|
2015-02-27 22:57:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Symbol table entries.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* For accessing the fields of st_info. */
|
|
|
|
|
|
|
|
|
|
/* For constructing st_info from field values. */
|
|
|
|
|
|
|
|
|
|
/* For accessing the fields of st_other. */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* ELF header.
|
|
|
|
|
*/
|
|
|
|
|
type ElfEhdr struct {
|
|
|
|
|
ident [EI_NIDENT]uint8
|
|
|
|
|
type_ uint16
|
|
|
|
|
machine uint16
|
|
|
|
|
version uint32
|
|
|
|
|
entry uint64
|
|
|
|
|
phoff uint64
|
|
|
|
|
shoff uint64
|
|
|
|
|
flags uint32
|
|
|
|
|
ehsize uint16
|
|
|
|
|
phentsize uint16
|
|
|
|
|
phnum uint16
|
|
|
|
|
shentsize uint16
|
|
|
|
|
shnum uint16
|
|
|
|
|
shstrndx uint16
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Section header.
|
|
|
|
|
*/
|
|
|
|
|
type ElfShdr struct {
|
|
|
|
|
name uint32
|
|
|
|
|
type_ uint32
|
|
|
|
|
flags uint64
|
|
|
|
|
addr uint64
|
|
|
|
|
off uint64
|
|
|
|
|
size uint64
|
|
|
|
|
link uint32
|
|
|
|
|
info uint32
|
|
|
|
|
addralign uint64
|
|
|
|
|
entsize uint64
|
|
|
|
|
shnum int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Program header.
|
|
|
|
|
*/
|
|
|
|
|
type ElfPhdr struct {
|
|
|
|
|
type_ uint32
|
|
|
|
|
flags uint32
|
|
|
|
|
off uint64
|
|
|
|
|
vaddr uint64
|
|
|
|
|
paddr uint64
|
|
|
|
|
filesz uint64
|
|
|
|
|
memsz uint64
|
|
|
|
|
align uint64
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* For accessing the fields of r_info. */
|
|
|
|
|
|
|
|
|
|
/* For constructing r_info from field values. */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Symbol table entries.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* For accessing the fields of st_info. */
|
|
|
|
|
|
|
|
|
|
/* For constructing st_info from field values. */
|
|
|
|
|
|
|
|
|
|
/* For accessing the fields of st_other. */
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Go linker interface
|
|
|
|
|
*/
|
|
|
|
|
const (
|
|
|
|
|
ELF64HDRSIZE = 64
|
|
|
|
|
ELF64PHDRSIZE = 56
|
|
|
|
|
ELF64SHDRSIZE = 64
|
|
|
|
|
ELF64RELSIZE = 16
|
|
|
|
|
ELF64RELASIZE = 24
|
|
|
|
|
ELF64SYMSIZE = 24
|
|
|
|
|
ELF32HDRSIZE = 52
|
|
|
|
|
ELF32PHDRSIZE = 32
|
|
|
|
|
ELF32SHDRSIZE = 40
|
|
|
|
|
ELF32SYMSIZE = 16
|
|
|
|
|
ELF32RELSIZE = 8
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The interface uses the 64-bit structures always,
|
|
|
|
|
* to avoid code duplication. The writers know how to
|
|
|
|
|
* marshal a 32-bit representation from the 64-bit structure.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
var Elfstrdat []byte
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Total amount of space to reserve at the start of the file
|
|
|
|
|
* for Header, PHeaders, SHeaders, and interp.
|
|
|
|
|
* May waste some.
|
|
|
|
|
* On FreeBSD, cannot be larger than a page.
|
|
|
|
|
*/
|
|
|
|
|
const (
|
2015-07-30 16:30:54 -04:00
|
|
|
ELFRESERVE = 4096
|
2015-02-27 22:57:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We use the 64-bit data structures on both 32- and 64-bit machines
|
|
|
|
|
* in order to write the code just once. The 64-bit data structure is
|
|
|
|
|
* written in the 32-bit format on the 32-bit machines.
|
|
|
|
|
*/
|
|
|
|
|
const (
|
2016-08-25 11:07:33 -05:00
|
|
|
NSECT = 400
|
2015-02-27 22:57:28 -05:00
|
|
|
)
|
|
|
|
|
|
2016-04-03 19:32:31 +12:00
|
|
|
var (
|
2017-10-09 10:11:00 +01:00
|
|
|
Nelfsym = 1
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-04-03 19:32:31 +12:00
|
|
|
elf64 bool
|
|
|
|
|
// Either ".rel" or ".rela" depending on which type of relocation the
|
|
|
|
|
// target platform uses.
|
|
|
|
|
elfRelType string
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-04-03 19:32:31 +12:00
|
|
|
ehdr ElfEhdr
|
|
|
|
|
phdr [NSECT]*ElfPhdr
|
|
|
|
|
shdr [NSECT]*ElfShdr
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-04-03 19:32:31 +12:00
|
|
|
interp string
|
|
|
|
|
)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
type Elfstring struct {
|
|
|
|
|
s string
|
|
|
|
|
off int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var elfstr [100]Elfstring
|
|
|
|
|
|
|
|
|
|
var nelfstr int
|
|
|
|
|
|
|
|
|
|
var buildinfo []byte
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Initialize the global variable that describes the ELF header. It will be updated as
|
|
|
|
|
we write section and prog headers.
|
|
|
|
|
*/
|
2016-08-19 22:40:38 -04:00
|
|
|
func Elfinit(ctxt *Link) {
|
2017-10-07 13:43:38 -04:00
|
|
|
ctxt.IsELF = true
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.InFamily(sys.AMD64, sys.ARM64, sys.MIPS64, sys.PPC64, sys.S390X) {
|
2016-04-03 19:32:31 +12:00
|
|
|
elfRelType = ".rela"
|
2016-04-06 12:01:40 -07:00
|
|
|
} else {
|
2016-04-03 19:32:31 +12:00
|
|
|
elfRelType = ".rel"
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-30 21:10:49 +00:00
|
|
|
switch ctxt.Arch.Family {
|
2015-02-27 22:57:28 -05:00
|
|
|
// 64-bit architectures
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.PPC64, sys.S390X:
|
2016-08-19 22:40:38 -04:00
|
|
|
if ctxt.Arch.ByteOrder == binary.BigEndian {
|
2015-02-27 22:57:28 -05:00
|
|
|
ehdr.flags = 1 /* Version 1 ABI */
|
|
|
|
|
} else {
|
|
|
|
|
ehdr.flags = 2 /* Version 2 ABI */
|
|
|
|
|
}
|
|
|
|
|
fallthrough
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.AMD64, sys.ARM64, sys.MIPS64:
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.Family == sys.MIPS64 {
|
2017-05-04 16:13:24 -04:00
|
|
|
ehdr.flags = 0x20000004 /* MIPS 3 CPIC */
|
2015-09-10 11:32:49 -04:00
|
|
|
}
|
2015-04-08 08:44:08 -07:00
|
|
|
elf64 = true
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2018-10-06 06:10:25 +00:00
|
|
|
ehdr.phoff = ELF64HDRSIZE /* Must be ELF64HDRSIZE: first PHdr must follow ELF header */
|
2015-02-27 22:57:28 -05:00
|
|
|
ehdr.shoff = ELF64HDRSIZE /* Will move as we add PHeaders */
|
|
|
|
|
ehdr.ehsize = ELF64HDRSIZE /* Must be ELF64HDRSIZE */
|
|
|
|
|
ehdr.phentsize = ELF64PHDRSIZE /* Must be ELF64PHDRSIZE */
|
|
|
|
|
ehdr.shentsize = ELF64SHDRSIZE /* Must be ELF64SHDRSIZE */
|
|
|
|
|
|
|
|
|
|
// 32-bit architectures
|
2016-10-18 23:50:45 +02:00
|
|
|
case sys.ARM, sys.MIPS:
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.Family == sys.ARM {
|
2016-10-18 23:50:45 +02:00
|
|
|
// we use EABI on linux/arm, freebsd/arm, netbsd/arm.
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hlinux || ctxt.HeadType == objabi.Hfreebsd || ctxt.HeadType == objabi.Hnetbsd {
|
2016-10-18 23:50:45 +02:00
|
|
|
// We set a value here that makes no indication of which
|
|
|
|
|
// float ABI the object uses, because this is information
|
|
|
|
|
// used by the dynamic linker to compare executables and
|
|
|
|
|
// shared libraries -- so it only matters for cgo calls, and
|
|
|
|
|
// the information properly comes from the object files
|
|
|
|
|
// produced by the host C compiler. parseArmAttributes in
|
|
|
|
|
// ldelf.go reads that information and updates this field as
|
|
|
|
|
// appropriate.
|
|
|
|
|
ehdr.flags = 0x5000002 // has entry point, Version5 EABI
|
|
|
|
|
}
|
2017-09-30 21:10:49 +00:00
|
|
|
} else if ctxt.Arch.Family == sys.MIPS {
|
2016-12-13 21:23:39 +01:00
|
|
|
ehdr.flags = 0x50001004 /* MIPS 32 CPIC O32*/
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
fallthrough
|
|
|
|
|
default:
|
|
|
|
|
ehdr.phoff = ELF32HDRSIZE
|
2018-10-06 06:10:25 +00:00
|
|
|
/* Must be ELF32HDRSIZE: first PHdr must follow ELF header */
|
2015-02-27 22:57:28 -05:00
|
|
|
ehdr.shoff = ELF32HDRSIZE /* Will move as we add PHeaders */
|
|
|
|
|
ehdr.ehsize = ELF32HDRSIZE /* Must be ELF32HDRSIZE */
|
|
|
|
|
ehdr.phentsize = ELF32PHDRSIZE /* Must be ELF32PHDRSIZE */
|
|
|
|
|
ehdr.shentsize = ELF32SHDRSIZE /* Must be ELF32SHDRSIZE */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-15 19:24:44 +02:00
|
|
|
// Make sure PT_LOAD is aligned properly and
|
|
|
|
|
// that there is no gap,
|
|
|
|
|
// correct ELF loaders will do this implicitly,
|
|
|
|
|
// but buggy ELF loaders like the one in some
|
|
|
|
|
// versions of QEMU and UPX won't.
|
|
|
|
|
func fixElfPhdr(e *ElfPhdr) {
|
|
|
|
|
frag := int(e.vaddr & (e.align - 1))
|
|
|
|
|
|
|
|
|
|
e.off -= uint64(frag)
|
|
|
|
|
e.vaddr -= uint64(frag)
|
|
|
|
|
e.paddr -= uint64(frag)
|
|
|
|
|
e.filesz += uint64(frag)
|
|
|
|
|
e.memsz += uint64(frag)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elf64phdr(out *OutBuf, e *ElfPhdr) {
|
2016-01-15 19:24:44 +02:00
|
|
|
if e.type_ == PT_LOAD {
|
|
|
|
|
fixElfPhdr(e)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write32(e.type_)
|
|
|
|
|
out.Write32(e.flags)
|
|
|
|
|
out.Write64(e.off)
|
|
|
|
|
out.Write64(e.vaddr)
|
|
|
|
|
out.Write64(e.paddr)
|
|
|
|
|
out.Write64(e.filesz)
|
|
|
|
|
out.Write64(e.memsz)
|
|
|
|
|
out.Write64(e.align)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elf32phdr(out *OutBuf, e *ElfPhdr) {
|
2015-02-27 22:57:28 -05:00
|
|
|
if e.type_ == PT_LOAD {
|
2016-01-15 19:24:44 +02:00
|
|
|
fixElfPhdr(e)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write32(e.type_)
|
|
|
|
|
out.Write32(uint32(e.off))
|
|
|
|
|
out.Write32(uint32(e.vaddr))
|
|
|
|
|
out.Write32(uint32(e.paddr))
|
|
|
|
|
out.Write32(uint32(e.filesz))
|
|
|
|
|
out.Write32(uint32(e.memsz))
|
|
|
|
|
out.Write32(e.flags)
|
|
|
|
|
out.Write32(uint32(e.align))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elf64shdr(out *OutBuf, e *ElfShdr) {
|
|
|
|
|
out.Write32(e.name)
|
|
|
|
|
out.Write32(e.type_)
|
|
|
|
|
out.Write64(e.flags)
|
|
|
|
|
out.Write64(e.addr)
|
|
|
|
|
out.Write64(e.off)
|
|
|
|
|
out.Write64(e.size)
|
|
|
|
|
out.Write32(e.link)
|
|
|
|
|
out.Write32(e.info)
|
|
|
|
|
out.Write64(e.addralign)
|
|
|
|
|
out.Write64(e.entsize)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elf32shdr(out *OutBuf, e *ElfShdr) {
|
|
|
|
|
out.Write32(e.name)
|
|
|
|
|
out.Write32(e.type_)
|
|
|
|
|
out.Write32(uint32(e.flags))
|
|
|
|
|
out.Write32(uint32(e.addr))
|
|
|
|
|
out.Write32(uint32(e.off))
|
|
|
|
|
out.Write32(uint32(e.size))
|
|
|
|
|
out.Write32(e.link)
|
|
|
|
|
out.Write32(e.info)
|
|
|
|
|
out.Write32(uint32(e.addralign))
|
|
|
|
|
out.Write32(uint32(e.entsize))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elfwriteshdrs(out *OutBuf) uint32 {
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < int(ehdr.shnum); i++ {
|
2017-10-01 02:37:20 +00:00
|
|
|
elf64shdr(out, shdr[i])
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return uint32(ehdr.shnum) * ELF64SHDRSIZE
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < int(ehdr.shnum); i++ {
|
2017-10-01 02:37:20 +00:00
|
|
|
elf32shdr(out, shdr[i])
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return uint32(ehdr.shnum) * ELF32SHDRSIZE
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func elfsetstring(s *sym.Symbol, str string, off int) {
|
2015-02-27 22:57:28 -05:00
|
|
|
if nelfstr >= len(elfstr) {
|
2016-09-17 09:39:33 -04:00
|
|
|
Errorf(s, "too many elf strings")
|
2015-04-09 07:37:17 -04:00
|
|
|
errorexit()
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
elfstr[nelfstr].s = str
|
2015-02-27 22:57:28 -05:00
|
|
|
elfstr[nelfstr].off = off
|
|
|
|
|
nelfstr++
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elfwritephdrs(out *OutBuf) uint32 {
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < int(ehdr.phnum); i++ {
|
2017-10-01 02:37:20 +00:00
|
|
|
elf64phdr(out, phdr[i])
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return uint32(ehdr.phnum) * ELF64PHDRSIZE
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < int(ehdr.phnum); i++ {
|
2017-10-01 02:37:20 +00:00
|
|
|
elf32phdr(out, phdr[i])
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
return uint32(ehdr.phnum) * ELF32PHDRSIZE
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
func newElfPhdr() *ElfPhdr {
|
2015-03-02 12:35:15 -05:00
|
|
|
e := new(ElfPhdr)
|
2015-02-27 22:57:28 -05:00
|
|
|
if ehdr.phnum >= NSECT {
|
2016-09-17 09:39:33 -04:00
|
|
|
Errorf(nil, "too many phdrs")
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
|
|
|
|
phdr[ehdr.phnum] = e
|
|
|
|
|
ehdr.phnum++
|
|
|
|
|
}
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2015-02-27 22:57:28 -05:00
|
|
|
ehdr.shoff += ELF64PHDRSIZE
|
|
|
|
|
} else {
|
|
|
|
|
ehdr.shoff += ELF32PHDRSIZE
|
|
|
|
|
}
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
func newElfShdr(name int64) *ElfShdr {
|
2015-03-02 12:35:15 -05:00
|
|
|
e := new(ElfShdr)
|
2015-02-27 22:57:28 -05:00
|
|
|
e.name = uint32(name)
|
|
|
|
|
e.shnum = int(ehdr.shnum)
|
|
|
|
|
if ehdr.shnum >= NSECT {
|
2016-09-17 09:39:33 -04:00
|
|
|
Errorf(nil, "too many shdrs")
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
|
|
|
|
shdr[ehdr.shnum] = e
|
|
|
|
|
ehdr.shnum++
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getElfEhdr() *ElfEhdr {
|
|
|
|
|
return &ehdr
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elf64writehdr(out *OutBuf) uint32 {
|
|
|
|
|
out.Write(ehdr.ident[:])
|
|
|
|
|
out.Write16(ehdr.type_)
|
|
|
|
|
out.Write16(ehdr.machine)
|
|
|
|
|
out.Write32(ehdr.version)
|
|
|
|
|
out.Write64(ehdr.entry)
|
|
|
|
|
out.Write64(ehdr.phoff)
|
|
|
|
|
out.Write64(ehdr.shoff)
|
|
|
|
|
out.Write32(ehdr.flags)
|
|
|
|
|
out.Write16(ehdr.ehsize)
|
|
|
|
|
out.Write16(ehdr.phentsize)
|
|
|
|
|
out.Write16(ehdr.phnum)
|
|
|
|
|
out.Write16(ehdr.shentsize)
|
|
|
|
|
out.Write16(ehdr.shnum)
|
|
|
|
|
out.Write16(ehdr.shstrndx)
|
2015-02-27 22:57:28 -05:00
|
|
|
return ELF64HDRSIZE
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elf32writehdr(out *OutBuf) uint32 {
|
|
|
|
|
out.Write(ehdr.ident[:])
|
|
|
|
|
out.Write16(ehdr.type_)
|
|
|
|
|
out.Write16(ehdr.machine)
|
|
|
|
|
out.Write32(ehdr.version)
|
|
|
|
|
out.Write32(uint32(ehdr.entry))
|
|
|
|
|
out.Write32(uint32(ehdr.phoff))
|
|
|
|
|
out.Write32(uint32(ehdr.shoff))
|
|
|
|
|
out.Write32(ehdr.flags)
|
|
|
|
|
out.Write16(ehdr.ehsize)
|
|
|
|
|
out.Write16(ehdr.phentsize)
|
|
|
|
|
out.Write16(ehdr.phnum)
|
|
|
|
|
out.Write16(ehdr.shentsize)
|
|
|
|
|
out.Write16(ehdr.shnum)
|
|
|
|
|
out.Write16(ehdr.shstrndx)
|
2015-02-27 22:57:28 -05:00
|
|
|
return ELF32HDRSIZE
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elfwritehdr(out *OutBuf) uint32 {
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2017-10-01 02:37:20 +00:00
|
|
|
return elf64writehdr(out)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-01 02:37:20 +00:00
|
|
|
return elf32writehdr(out)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Taken directly from the definition document for ELF64 */
|
2016-03-02 14:29:44 -08:00
|
|
|
func elfhash(name string) uint32 {
|
|
|
|
|
var h uint32
|
|
|
|
|
for i := 0; i < len(name); i++ {
|
|
|
|
|
h = (h << 4) + uint32(name[i])
|
|
|
|
|
if g := h & 0xf0000000; g != 0 {
|
2015-02-27 22:57:28 -05:00
|
|
|
h ^= g >> 24
|
|
|
|
|
}
|
|
|
|
|
h &= 0x0fffffff
|
|
|
|
|
}
|
|
|
|
|
return h
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func Elfwritedynent(ctxt *Link, s *sym.Symbol, tag int, val uint64) {
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint64(ctxt.Arch, uint64(tag))
|
|
|
|
|
s.AddUint64(ctxt.Arch, val)
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, uint32(tag))
|
|
|
|
|
s.AddUint32(ctxt.Arch, uint32(val))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func elfwritedynentsym(ctxt *Link, s *sym.Symbol, tag int, t *sym.Symbol) {
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynentsymplus(ctxt, s, tag, t, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func Elfwritedynentsymplus(ctxt *Link, s *sym.Symbol, tag int, t *sym.Symbol, add int64) {
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint64(ctxt.Arch, uint64(tag))
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, uint32(tag))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddAddrPlus(ctxt.Arch, t, add)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func elfwritedynentsymsize(ctxt *Link, s *sym.Symbol, tag int, t *sym.Symbol) {
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint64(ctxt.Arch, uint64(tag))
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, uint32(tag))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddSize(ctxt.Arch, t)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func elfinterp(sh *ElfShdr, startva uint64, resoff uint64, p string) int {
|
|
|
|
|
interp = p
|
2015-03-02 12:35:15 -05:00
|
|
|
n := len(interp) + 1
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.addr = startva + resoff - uint64(n)
|
|
|
|
|
sh.off = resoff - uint64(n)
|
|
|
|
|
sh.size = uint64(n)
|
|
|
|
|
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elfwriteinterp(out *OutBuf) int {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".interp")
|
2017-10-01 02:37:20 +00:00
|
|
|
out.SeekSet(int64(sh.off))
|
|
|
|
|
out.WriteString(interp)
|
|
|
|
|
out.Write8(0)
|
2015-02-27 22:57:28 -05:00
|
|
|
return int(sh.size)
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 15:52:30 +01:00
|
|
|
func elfnote(sh *ElfShdr, startva uint64, resoff uint64, sz int) int {
|
2015-03-02 12:35:15 -05:00
|
|
|
n := 3*4 + uint64(sz) + resoff%4
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
sh.type_ = SHT_NOTE
|
2018-04-03 15:52:30 +01:00
|
|
|
sh.flags = SHF_ALLOC
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.addralign = 4
|
|
|
|
|
sh.addr = startva + resoff - n
|
|
|
|
|
sh.off = resoff - n
|
|
|
|
|
sh.size = n - resoff%4
|
|
|
|
|
|
|
|
|
|
return int(n)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elfwritenotehdr(out *OutBuf, str string, namesz uint32, descsz uint32, tag uint32) *ElfShdr {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(str)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// Write Elf_Note header.
|
2017-10-01 02:37:20 +00:00
|
|
|
out.SeekSet(int64(sh.off))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write32(namesz)
|
|
|
|
|
out.Write32(descsz)
|
|
|
|
|
out.Write32(tag)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
return sh
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NetBSD Signature (as per sys/exec_elf.h)
|
|
|
|
|
const (
|
|
|
|
|
ELF_NOTE_NETBSD_NAMESZ = 7
|
|
|
|
|
ELF_NOTE_NETBSD_DESCSZ = 4
|
|
|
|
|
ELF_NOTE_NETBSD_TAG = 1
|
2015-03-05 13:57:36 -05:00
|
|
|
ELF_NOTE_NETBSD_VERSION = 599000000 /* NetBSD 5.99 */
|
2015-02-27 22:57:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var ELF_NOTE_NETBSD_NAME = []byte("NetBSD\x00")
|
|
|
|
|
|
|
|
|
|
func elfnetbsdsig(sh *ElfShdr, startva uint64, resoff uint64) int {
|
2015-03-02 12:35:15 -05:00
|
|
|
n := int(Rnd(ELF_NOTE_NETBSD_NAMESZ, 4) + Rnd(ELF_NOTE_NETBSD_DESCSZ, 4))
|
2018-04-03 15:52:30 +01:00
|
|
|
return elfnote(sh, startva, resoff, n)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elfwritenetbsdsig(out *OutBuf) int {
|
2015-02-27 22:57:28 -05:00
|
|
|
// Write Elf_Note header.
|
2017-10-01 02:37:20 +00:00
|
|
|
sh := elfwritenotehdr(out, ".note.netbsd.ident", ELF_NOTE_NETBSD_NAMESZ, ELF_NOTE_NETBSD_DESCSZ, ELF_NOTE_NETBSD_TAG)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if sh == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Followed by NetBSD string and version.
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write(ELF_NOTE_NETBSD_NAME)
|
|
|
|
|
out.Write8(0)
|
|
|
|
|
out.Write32(ELF_NOTE_NETBSD_VERSION)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
return int(sh.size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// OpenBSD Signature
|
|
|
|
|
const (
|
|
|
|
|
ELF_NOTE_OPENBSD_NAMESZ = 8
|
|
|
|
|
ELF_NOTE_OPENBSD_DESCSZ = 4
|
|
|
|
|
ELF_NOTE_OPENBSD_TAG = 1
|
|
|
|
|
ELF_NOTE_OPENBSD_VERSION = 0
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var ELF_NOTE_OPENBSD_NAME = []byte("OpenBSD\x00")
|
|
|
|
|
|
|
|
|
|
func elfopenbsdsig(sh *ElfShdr, startva uint64, resoff uint64) int {
|
2015-03-02 12:35:15 -05:00
|
|
|
n := ELF_NOTE_OPENBSD_NAMESZ + ELF_NOTE_OPENBSD_DESCSZ
|
2018-04-03 15:52:30 +01:00
|
|
|
return elfnote(sh, startva, resoff, n)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elfwriteopenbsdsig(out *OutBuf) int {
|
2015-02-27 22:57:28 -05:00
|
|
|
// Write Elf_Note header.
|
2017-10-01 02:37:20 +00:00
|
|
|
sh := elfwritenotehdr(out, ".note.openbsd.ident", ELF_NOTE_OPENBSD_NAMESZ, ELF_NOTE_OPENBSD_DESCSZ, ELF_NOTE_OPENBSD_TAG)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if sh == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Followed by OpenBSD string and version.
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write(ELF_NOTE_OPENBSD_NAME)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write32(ELF_NOTE_OPENBSD_VERSION)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
return int(sh.size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addbuildinfo(val string) {
|
2016-03-03 22:37:14 -08:00
|
|
|
if !strings.HasPrefix(val, "0x") {
|
2015-04-09 07:37:17 -04:00
|
|
|
Exitf("-B argument must start with 0x: %s", val)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
ov := val
|
2015-02-27 22:57:28 -05:00
|
|
|
val = val[2:]
|
|
|
|
|
|
2016-03-03 22:37:14 -08:00
|
|
|
const maxLen = 32
|
|
|
|
|
if hex.DecodedLen(len(val)) > maxLen {
|
|
|
|
|
Exitf("-B option too long (max %d digits): %s", maxLen, ov)
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-03-03 22:37:14 -08:00
|
|
|
b, err := hex.DecodeString(val)
|
|
|
|
|
if err != nil {
|
|
|
|
|
if err == hex.ErrLength {
|
|
|
|
|
Exitf("-B argument must have even number of digits: %s", ov)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-03-03 22:37:14 -08:00
|
|
|
if inv, ok := err.(hex.InvalidByteError); ok {
|
|
|
|
|
Exitf("-B argument contains invalid hex digit %c: %s", byte(inv), ov)
|
|
|
|
|
}
|
|
|
|
|
Exitf("-B argument contains invalid hex: %s", ov)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-03-03 22:37:14 -08:00
|
|
|
buildinfo = b
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build info note
|
|
|
|
|
const (
|
|
|
|
|
ELF_NOTE_BUILDINFO_NAMESZ = 4
|
|
|
|
|
ELF_NOTE_BUILDINFO_TAG = 3
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var ELF_NOTE_BUILDINFO_NAME = []byte("GNU\x00")
|
|
|
|
|
|
|
|
|
|
func elfbuildinfo(sh *ElfShdr, startva uint64, resoff uint64) int {
|
2015-03-02 12:35:15 -05:00
|
|
|
n := int(ELF_NOTE_BUILDINFO_NAMESZ + Rnd(int64(len(buildinfo)), 4))
|
2018-04-03 15:52:30 +01:00
|
|
|
return elfnote(sh, startva, resoff, n)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-04 14:31:05 -04:00
|
|
|
func elfgobuildid(sh *ElfShdr, startva uint64, resoff uint64) int {
|
2016-08-21 18:34:24 -04:00
|
|
|
n := len(ELF_NOTE_GO_NAME) + int(Rnd(int64(len(*flagBuildid)), 4))
|
2018-04-03 15:52:30 +01:00
|
|
|
return elfnote(sh, startva, resoff, n)
|
2015-06-04 14:31:05 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elfwritebuildinfo(out *OutBuf) int {
|
|
|
|
|
sh := elfwritenotehdr(out, ".note.gnu.build-id", ELF_NOTE_BUILDINFO_NAMESZ, uint32(len(buildinfo)), ELF_NOTE_BUILDINFO_TAG)
|
2015-02-27 22:57:28 -05:00
|
|
|
if sh == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write(ELF_NOTE_BUILDINFO_NAME)
|
|
|
|
|
out.Write(buildinfo)
|
2015-02-27 22:57:28 -05:00
|
|
|
var zero = make([]byte, 4)
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write(zero[:int(Rnd(int64(len(buildinfo)), 4)-int64(len(buildinfo)))])
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
return int(sh.size)
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
func elfwritegobuildid(out *OutBuf) int {
|
|
|
|
|
sh := elfwritenotehdr(out, ".note.go.buildid", uint32(len(ELF_NOTE_GO_NAME)), uint32(len(*flagBuildid)), ELF_NOTE_GOBUILDID_TAG)
|
2015-06-04 14:31:05 -04:00
|
|
|
if sh == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write(ELF_NOTE_GO_NAME)
|
|
|
|
|
out.Write([]byte(*flagBuildid))
|
2015-06-04 14:31:05 -04:00
|
|
|
var zero = make([]byte, 4)
|
2017-10-01 02:37:20 +00:00
|
|
|
out.Write(zero[:int(Rnd(int64(len(*flagBuildid)), 4)-int64(len(*flagBuildid)))])
|
2015-06-04 14:31:05 -04:00
|
|
|
|
|
|
|
|
return int(sh.size)
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-25 13:59:08 +12:00
|
|
|
// Go specific notes
|
2015-04-29 22:58:52 +12:00
|
|
|
const (
|
|
|
|
|
ELF_NOTE_GOPKGLIST_TAG = 1
|
2015-05-25 13:59:08 +12:00
|
|
|
ELF_NOTE_GOABIHASH_TAG = 2
|
2015-05-25 14:51:02 +12:00
|
|
|
ELF_NOTE_GODEPS_TAG = 3
|
2015-06-04 14:31:05 -04:00
|
|
|
ELF_NOTE_GOBUILDID_TAG = 4
|
2015-04-29 22:58:52 +12:00
|
|
|
)
|
|
|
|
|
|
2015-06-04 14:27:39 -04:00
|
|
|
var ELF_NOTE_GO_NAME = []byte("Go\x00\x00")
|
2015-04-29 22:58:52 +12:00
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
var elfverneed int
|
|
|
|
|
|
|
|
|
|
type Elfaux struct {
|
|
|
|
|
next *Elfaux
|
|
|
|
|
num int
|
|
|
|
|
vers string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Elflib struct {
|
|
|
|
|
next *Elflib
|
|
|
|
|
aux *Elfaux
|
|
|
|
|
file string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func addelflib(list **Elflib, file string, vers string) *Elfaux {
|
|
|
|
|
var lib *Elflib
|
|
|
|
|
|
|
|
|
|
for lib = *list; lib != nil; lib = lib.next {
|
|
|
|
|
if lib.file == file {
|
|
|
|
|
goto havelib
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lib = new(Elflib)
|
|
|
|
|
lib.next = *list
|
|
|
|
|
lib.file = file
|
|
|
|
|
*list = lib
|
|
|
|
|
|
|
|
|
|
havelib:
|
2015-03-02 12:35:15 -05:00
|
|
|
for aux := lib.aux; aux != nil; aux = aux.next {
|
2015-02-27 22:57:28 -05:00
|
|
|
if aux.vers == vers {
|
2015-03-02 12:35:15 -05:00
|
|
|
return aux
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
aux := new(Elfaux)
|
2015-02-27 22:57:28 -05:00
|
|
|
aux.next = lib.aux
|
|
|
|
|
aux.vers = vers
|
|
|
|
|
lib.aux = aux
|
|
|
|
|
|
|
|
|
|
return aux
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
func elfdynhash(ctxt *Link) {
|
2017-10-07 13:43:38 -04:00
|
|
|
if !ctxt.IsELF {
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
nsym := Nelfsym
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s := ctxt.Syms.Lookup(".hash", 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Type = sym.SELFROSECT
|
|
|
|
|
s.Attr |= sym.AttrReachable
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
i := nsym
|
|
|
|
|
nbucket := 1
|
2015-02-27 22:57:28 -05:00
|
|
|
for i > 0 {
|
|
|
|
|
nbucket++
|
|
|
|
|
i >>= 1
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 14:22:05 -05:00
|
|
|
var needlib *Elflib
|
2015-03-02 12:35:15 -05:00
|
|
|
need := make([]*Elfaux, nsym)
|
|
|
|
|
chain := make([]uint32, nsym)
|
|
|
|
|
buckets := make([]uint32, nbucket)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-20 14:59:39 +12:00
|
|
|
for _, sy := range ctxt.Syms.Allsym {
|
2015-02-27 22:57:28 -05:00
|
|
|
if sy.Dynid <= 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-28 16:10:19 -04:00
|
|
|
if sy.Dynimpvers() != "" {
|
|
|
|
|
need[sy.Dynid] = addelflib(&needlib, sy.Dynimplib(), sy.Dynimpvers())
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2018-07-17 11:02:57 -04:00
|
|
|
name := sy.Extname()
|
2016-03-02 14:29:44 -08:00
|
|
|
hc := elfhash(name)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-01 14:39:04 +11:00
|
|
|
b := hc % uint32(nbucket)
|
2015-02-27 22:57:28 -05:00
|
|
|
chain[sy.Dynid] = buckets[b]
|
|
|
|
|
buckets[b] = uint32(sy.Dynid)
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-18 16:57:54 -04:00
|
|
|
// s390x (ELF64) hash table entries are 8 bytes
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.Family == sys.S390X {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint64(ctxt.Arch, uint64(nbucket))
|
|
|
|
|
s.AddUint64(ctxt.Arch, uint64(nsym))
|
2016-03-18 16:57:54 -04:00
|
|
|
for i := 0; i < nbucket; i++ {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint64(ctxt.Arch, uint64(buckets[i]))
|
2016-03-18 16:57:54 -04:00
|
|
|
}
|
|
|
|
|
for i := 0; i < nsym; i++ {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint64(ctxt.Arch, uint64(chain[i]))
|
2016-03-18 16:57:54 -04:00
|
|
|
}
|
|
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, uint32(nbucket))
|
|
|
|
|
s.AddUint32(ctxt.Arch, uint32(nsym))
|
2016-03-18 16:57:54 -04:00
|
|
|
for i := 0; i < nbucket; i++ {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, buckets[i])
|
2016-03-18 16:57:54 -04:00
|
|
|
}
|
|
|
|
|
for i := 0; i < nsym; i++ {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, chain[i])
|
2016-03-18 16:57:54 -04:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// version symbols
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
dynstr := ctxt.Syms.Lookup(".dynstr", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".gnu.version_r", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
i = 2
|
2015-03-02 12:35:15 -05:00
|
|
|
nfile := 0
|
|
|
|
|
for l := needlib; l != nil; l = l.next {
|
2015-02-27 22:57:28 -05:00
|
|
|
nfile++
|
|
|
|
|
|
|
|
|
|
// header
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint16(ctxt.Arch, 1) // table version
|
2017-10-01 14:39:04 +11:00
|
|
|
j := 0
|
|
|
|
|
for x := l.aux; x != nil; x = x.next {
|
2015-02-27 22:57:28 -05:00
|
|
|
j++
|
|
|
|
|
}
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint16(ctxt.Arch, uint16(j)) // aux count
|
|
|
|
|
s.AddUint32(ctxt.Arch, uint32(Addstring(dynstr, l.file))) // file string offset
|
|
|
|
|
s.AddUint32(ctxt.Arch, 16) // offset from header to first aux
|
2015-02-27 22:57:28 -05:00
|
|
|
if l.next != nil {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, 16+uint32(j)*16) // offset from this header to next
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-01 14:39:04 +11:00
|
|
|
for x := l.aux; x != nil; x = x.next {
|
2015-02-27 22:57:28 -05:00
|
|
|
x.num = i
|
|
|
|
|
i++
|
|
|
|
|
|
|
|
|
|
// aux struct
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, elfhash(x.vers)) // hash
|
|
|
|
|
s.AddUint16(ctxt.Arch, 0) // flags
|
|
|
|
|
s.AddUint16(ctxt.Arch, uint16(x.num)) // other - index we refer to this by
|
|
|
|
|
s.AddUint32(ctxt.Arch, uint32(Addstring(dynstr, x.vers))) // version string offset
|
2015-02-27 22:57:28 -05:00
|
|
|
if x.next != nil {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, 16) // offset from this aux to next
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// version references
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".gnu.version", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < nsym; i++ {
|
2015-02-27 22:57:28 -05:00
|
|
|
if i == 0 {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint16(ctxt.Arch, 0) // first entry - no symbol
|
2015-02-27 22:57:28 -05:00
|
|
|
} else if need[i] == nil {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint16(ctxt.Arch, 1) // global
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint16(ctxt.Arch, uint16(need[i].num))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".dynamic", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
elfverneed = nfile
|
|
|
|
|
if elfverneed != 0 {
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_VERNEED, ctxt.Syms.Lookup(".gnu.version_r", 0))
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_VERNEEDNUM, uint64(nfile))
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_VERSYM, ctxt.Syms.Lookup(".gnu.version", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
sy := ctxt.Syms.Lookup(elfRelType+".plt", 0)
|
2016-04-03 19:32:31 +12:00
|
|
|
if sy.Size > 0 {
|
|
|
|
|
if elfRelType == ".rela" {
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_PLTREL, DT_RELA)
|
2016-04-03 19:32:31 +12:00
|
|
|
} else {
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_PLTREL, DT_REL)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-08-19 22:40:38 -04:00
|
|
|
elfwritedynentsymsize(ctxt, s, DT_PLTRELSZ, sy)
|
|
|
|
|
elfwritedynentsym(ctxt, s, DT_JMPREL, sy)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_NULL, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func elfphload(seg *sym.Segment) *ElfPhdr {
|
2016-09-17 09:39:33 -04:00
|
|
|
ph := newElfPhdr()
|
2015-02-27 22:57:28 -05:00
|
|
|
ph.type_ = PT_LOAD
|
|
|
|
|
if seg.Rwx&4 != 0 {
|
|
|
|
|
ph.flags |= PF_R
|
|
|
|
|
}
|
|
|
|
|
if seg.Rwx&2 != 0 {
|
|
|
|
|
ph.flags |= PF_W
|
|
|
|
|
}
|
|
|
|
|
if seg.Rwx&1 != 0 {
|
|
|
|
|
ph.flags |= PF_X
|
|
|
|
|
}
|
|
|
|
|
ph.vaddr = seg.Vaddr
|
|
|
|
|
ph.paddr = seg.Vaddr
|
|
|
|
|
ph.memsz = seg.Length
|
|
|
|
|
ph.off = seg.Fileoff
|
|
|
|
|
ph.filesz = seg.Filelen
|
2016-08-21 18:34:24 -04:00
|
|
|
ph.align = uint64(*FlagRound)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
return ph
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func elfphrelro(seg *sym.Segment) {
|
2016-09-17 09:39:33 -04:00
|
|
|
ph := newElfPhdr()
|
2016-09-06 08:02:30 -04:00
|
|
|
ph.type_ = PT_GNU_RELRO
|
|
|
|
|
ph.vaddr = seg.Vaddr
|
|
|
|
|
ph.paddr = seg.Vaddr
|
|
|
|
|
ph.memsz = seg.Length
|
|
|
|
|
ph.off = seg.Fileoff
|
|
|
|
|
ph.filesz = seg.Filelen
|
|
|
|
|
ph.align = uint64(*FlagRound)
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
func elfshname(name string) *ElfShdr {
|
2015-03-02 12:35:15 -05:00
|
|
|
for i := 0; i < nelfstr; i++ {
|
2017-10-09 10:11:00 +01:00
|
|
|
if name != elfstr[i].s {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
off := elfstr[i].off
|
|
|
|
|
for i = 0; i < int(ehdr.shnum); i++ {
|
|
|
|
|
sh := shdr[i]
|
|
|
|
|
if sh.name == uint32(off) {
|
|
|
|
|
return sh
|
2015-03-02 12:35:15 -05:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-09 10:11:00 +01:00
|
|
|
return newElfShdr(int64(off))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-09-17 09:39:33 -04:00
|
|
|
Exitf("cannot find elf name %s", name)
|
2015-02-27 22:57:28 -05:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-25 11:07:33 -05:00
|
|
|
// Create an ElfShdr for the section with name.
|
|
|
|
|
// Create a duplicate if one already exists with that name
|
|
|
|
|
func elfshnamedup(name string) *ElfShdr {
|
|
|
|
|
for i := 0; i < nelfstr; i++ {
|
|
|
|
|
if name == elfstr[i].s {
|
2017-10-01 14:39:04 +11:00
|
|
|
off := elfstr[i].off
|
|
|
|
|
return newElfShdr(int64(off))
|
2016-08-25 11:07:33 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Errorf(nil, "cannot find elf name %s", name)
|
|
|
|
|
errorexit()
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func elfshalloc(sect *sym.Section) *ElfShdr {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(sect.Name)
|
2015-02-27 22:57:28 -05:00
|
|
|
sect.Elfsect = sh
|
|
|
|
|
return sh
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
func elfshbits(linkmode LinkMode, sect *sym.Section) *ElfShdr {
|
2016-08-25 11:07:33 -05:00
|
|
|
var sh *ElfShdr
|
|
|
|
|
|
|
|
|
|
if sect.Name == ".text" {
|
|
|
|
|
if sect.Elfsect == nil {
|
|
|
|
|
sect.Elfsect = elfshnamedup(sect.Name)
|
|
|
|
|
}
|
2017-10-04 17:54:04 -04:00
|
|
|
sh = sect.Elfsect.(*ElfShdr)
|
2016-08-25 11:07:33 -05:00
|
|
|
} else {
|
|
|
|
|
sh = elfshalloc(sect)
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-25 13:59:08 +12:00
|
|
|
// If this section has already been set up as a note, we assume type_ and
|
|
|
|
|
// flags are already correct, but the other fields still need filling in.
|
|
|
|
|
if sh.type_ == SHT_NOTE {
|
2017-10-05 10:20:17 -04:00
|
|
|
if linkmode != LinkExternal {
|
2015-05-25 13:59:08 +12:00
|
|
|
// TODO(mwhudson): the approach here will work OK when
|
|
|
|
|
// linking internally for notes that we want to be included
|
|
|
|
|
// in a loadable segment (e.g. the abihash note) but not for
|
|
|
|
|
// notes that we do not want to be mapped (e.g. the package
|
|
|
|
|
// list note). The real fix is probably to define new values
|
2016-08-22 10:27:20 +12:00
|
|
|
// for Symbol.Type corresponding to mapped and unmapped notes
|
2015-05-25 13:59:08 +12:00
|
|
|
// and handle them in dodata().
|
2016-09-17 09:39:33 -04:00
|
|
|
Errorf(nil, "sh.type_ == SHT_NOTE in elfshbits when linking internally")
|
2015-05-25 13:59:08 +12:00
|
|
|
}
|
|
|
|
|
sh.addralign = uint64(sect.Align)
|
|
|
|
|
sh.size = sect.Length
|
|
|
|
|
sh.off = sect.Seg.Fileoff + sect.Vaddr - sect.Seg.Vaddr
|
|
|
|
|
return sh
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
if sh.type_ > 0 {
|
|
|
|
|
return sh
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if sect.Vaddr < sect.Seg.Vaddr+sect.Seg.Filelen {
|
|
|
|
|
sh.type_ = SHT_PROGBITS
|
|
|
|
|
} else {
|
|
|
|
|
sh.type_ = SHT_NOBITS
|
|
|
|
|
}
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
if sect.Rwx&1 != 0 {
|
|
|
|
|
sh.flags |= SHF_EXECINSTR
|
|
|
|
|
}
|
|
|
|
|
if sect.Rwx&2 != 0 {
|
|
|
|
|
sh.flags |= SHF_WRITE
|
|
|
|
|
}
|
|
|
|
|
if sect.Name == ".tbss" {
|
2015-09-02 11:23:15 +12:00
|
|
|
sh.flags |= SHF_TLS
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_NOBITS
|
|
|
|
|
}
|
cmd/link: compress DWARF sections in ELF binaries
Forked from CL 111895.
The trickiest part of this is that the binary layout code (blk,
elfshbits, and various other things) assumes a constant offset between
symbols' and sections' file locations and their virtual addresses.
Compression, of course, breaks this constant offset. But we need to
assign virtual addresses to everything before compression in order to
resolve relocations before compression. As a result, compression needs
to re-compute the "address" of the DWARF sections and symbols based on
their compressed size. Luckily, these are at the end of the file, so
this doesn't perturb any other sections or symbols. (And there is, of
course, a surprising amount of code that assumes the DWARF segment
comes last, so what's one more place?)
Relevant benchmarks:
name old time/op new time/op delta
StdCmd 10.3s ± 2% 10.8s ± 1% +5.43% (p=0.000 n=30+30)
name old text-bytes new text-bytes delta
HelloSize 746kB ± 0% 746kB ± 0% ~ (all equal)
CmdGoSize 8.41MB ± 0% 8.41MB ± 0% ~ (all equal)
[Geo mean] 2.50MB 2.50MB +0.00%
name old data-bytes new data-bytes delta
HelloSize 10.6kB ± 0% 10.6kB ± 0% ~ (all equal)
CmdGoSize 252kB ± 0% 252kB ± 0% ~ (all equal)
[Geo mean] 51.5kB 51.5kB +0.00%
name old bss-bytes new bss-bytes delta
HelloSize 125kB ± 0% 125kB ± 0% ~ (all equal)
CmdGoSize 145kB ± 0% 145kB ± 0% ~ (all equal)
[Geo mean] 135kB 135kB +0.00%
name old exe-bytes new exe-bytes delta
HelloSize 1.60MB ± 0% 1.05MB ± 0% -34.39% (p=0.000 n=30+30)
CmdGoSize 16.5MB ± 0% 11.3MB ± 0% -31.76% (p=0.000 n=30+30)
[Geo mean] 5.14MB 3.44MB -33.08%
Fixes #11799.
Updates #6853.
Change-Id: I64197afe4c01a237523a943088051ee056331c6f
Reviewed-on: https://go-review.googlesource.com/118276
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Austin Clements <austin@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2018-05-05 21:49:40 -04:00
|
|
|
if strings.HasPrefix(sect.Name, ".debug") || strings.HasPrefix(sect.Name, ".zdebug") {
|
2016-03-14 09:23:04 -07:00
|
|
|
sh.flags = 0
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if linkmode != LinkExternal {
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.addr = sect.Vaddr
|
|
|
|
|
}
|
|
|
|
|
sh.addralign = uint64(sect.Align)
|
|
|
|
|
sh.size = sect.Length
|
2015-09-02 11:23:15 +12:00
|
|
|
if sect.Name != ".tbss" {
|
2015-08-11 12:29:00 +12:00
|
|
|
sh.off = sect.Seg.Fileoff + sect.Vaddr - sect.Seg.Vaddr
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
return sh
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func elfshreloc(arch *sys.Arch, sect *sym.Section) *ElfShdr {
|
2015-02-27 22:57:28 -05:00
|
|
|
// If main section is SHT_NOBITS, nothing to relocate.
|
2015-05-25 13:59:08 +12:00
|
|
|
// Also nothing to relocate in .shstrtab or notes.
|
2015-02-27 22:57:28 -05:00
|
|
|
if sect.Vaddr >= sect.Seg.Vaddr+sect.Seg.Filelen {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
if sect.Name == ".shstrtab" || sect.Name == ".tbss" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-10-04 17:54:04 -04:00
|
|
|
if sect.Elfsect.(*ElfShdr).type_ == SHT_NOTE {
|
2015-05-25 13:59:08 +12:00
|
|
|
return nil
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2019-03-05 20:44:29 +00:00
|
|
|
typ := SHT_REL
|
2016-04-03 19:32:31 +12:00
|
|
|
if elfRelType == ".rela" {
|
2015-02-27 22:57:28 -05:00
|
|
|
typ = SHT_RELA
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(elfRelType + sect.Name)
|
2016-08-25 11:07:33 -05:00
|
|
|
// There could be multiple text sections but each needs
|
|
|
|
|
// its own .rela.text.
|
|
|
|
|
|
|
|
|
|
if sect.Name == ".text" {
|
2017-10-04 17:54:04 -04:00
|
|
|
if sh.info != 0 && sh.info != uint32(sect.Elfsect.(*ElfShdr).shnum) {
|
2016-08-25 11:07:33 -05:00
|
|
|
sh = elfshnamedup(elfRelType + sect.Name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = uint32(typ)
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.entsize = uint64(arch.RegSize) * 2
|
2015-02-27 22:57:28 -05:00
|
|
|
if typ == SHT_RELA {
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.entsize += uint64(arch.RegSize)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".symtab").shnum)
|
2017-10-04 17:54:04 -04:00
|
|
|
sh.info = uint32(sect.Elfsect.(*ElfShdr).shnum)
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.off = sect.Reloff
|
|
|
|
|
sh.size = sect.Rellen
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.addralign = uint64(arch.RegSize)
|
2015-02-27 22:57:28 -05:00
|
|
|
return sh
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func elfrelocsect(ctxt *Link, sect *sym.Section, syms []*sym.Symbol) {
|
2015-02-27 22:57:28 -05:00
|
|
|
// If main section is SHT_NOBITS, nothing to relocate.
|
|
|
|
|
// Also nothing to relocate in .shstrtab.
|
|
|
|
|
if sect.Vaddr >= sect.Seg.Vaddr+sect.Seg.Filelen {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if sect.Name == ".shstrtab" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
sect.Reloff = uint64(ctxt.Out.Offset())
|
2016-04-18 14:50:14 -04:00
|
|
|
for i, s := range syms {
|
|
|
|
|
if !s.Attr.Reachable() {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
2016-04-18 14:50:14 -04:00
|
|
|
if uint64(s.Value) >= sect.Vaddr {
|
|
|
|
|
syms = syms[i:]
|
2015-02-27 22:57:28 -05:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-02 12:35:15 -05:00
|
|
|
eaddr := int32(sect.Vaddr + sect.Length)
|
2017-10-04 17:54:04 -04:00
|
|
|
for _, s := range syms {
|
|
|
|
|
if !s.Attr.Reachable() {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
2017-10-04 17:54:04 -04:00
|
|
|
if s.Value >= int64(eaddr) {
|
2015-02-27 22:57:28 -05:00
|
|
|
break
|
|
|
|
|
}
|
2018-04-06 21:41:06 +01:00
|
|
|
for ri := range s.R {
|
2017-10-04 17:54:04 -04:00
|
|
|
r := &s.R[ri]
|
2017-08-27 22:00:00 +09:00
|
|
|
if r.Done {
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if r.Xsym == nil {
|
2017-10-04 17:54:04 -04:00
|
|
|
Errorf(s, "missing xsym in relocation %#v %#v", r.Sym.Name, s)
|
2015-02-27 22:57:28 -05:00
|
|
|
continue
|
|
|
|
|
}
|
2015-10-29 12:17:43 +13:00
|
|
|
if r.Xsym.ElfsymForReloc() == 0 {
|
2017-10-04 17:54:04 -04:00
|
|
|
Errorf(s, "reloc %d (%s) to non-elf symbol %s (outer=%s) %d (%s)", r.Type, sym.RelocName(ctxt.Arch, r.Type), r.Sym.Name, r.Xsym.Name, r.Sym.Type, r.Sym.Type)
|
2016-09-17 09:39:33 -04:00
|
|
|
}
|
|
|
|
|
if !r.Xsym.Attr.Reachable() {
|
2017-10-04 17:54:04 -04:00
|
|
|
Errorf(s, "unreachable reloc %d (%s) target %v", r.Type, sym.RelocName(ctxt.Arch, r.Type), r.Xsym.Name)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2018-04-01 00:58:48 +03:00
|
|
|
if !thearch.Elfreloc1(ctxt, r, int64(uint64(s.Value+int64(r.Off))-sect.Vaddr)) {
|
2017-10-04 17:54:04 -04:00
|
|
|
Errorf(s, "unsupported obj reloc %d (%s)/%d to %s", r.Type, sym.RelocName(ctxt.Arch, r.Type), r.Siz, r.Sym.Name)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
sect.Rellen = uint64(ctxt.Out.Offset()) - sect.Reloff
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
func Elfemitreloc(ctxt *Link) {
|
2017-10-01 02:37:20 +00:00
|
|
|
for ctxt.Out.Offset()&7 != 0 {
|
|
|
|
|
ctxt.Out.Write8(0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segtext.Sections {
|
2016-08-25 11:07:33 -05:00
|
|
|
if sect.Name == ".text" {
|
|
|
|
|
elfrelocsect(ctxt, sect, ctxt.Textp)
|
|
|
|
|
} else {
|
|
|
|
|
elfrelocsect(ctxt, sect, datap)
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-08-25 11:07:33 -05:00
|
|
|
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segrodata.Sections {
|
2016-08-19 22:40:38 -04:00
|
|
|
elfrelocsect(ctxt, sect, datap)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segrelrodata.Sections {
|
2016-09-05 23:29:16 -04:00
|
|
|
elfrelocsect(ctxt, sect, datap)
|
|
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segdata.Sections {
|
2016-08-19 22:40:38 -04:00
|
|
|
elfrelocsect(ctxt, sect, datap)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segdwarf.Sections {
|
2016-04-22 10:31:14 +12:00
|
|
|
elfrelocsect(ctxt, sect, dwarfp)
|
2016-03-14 09:23:04 -07:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
func addgonote(ctxt *Link, sectionName string, tag uint32, desc []byte) {
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s := ctxt.Syms.Lookup(sectionName, 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFROSECT
|
2015-05-25 13:59:08 +12:00
|
|
|
// namesz
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, uint32(len(ELF_NOTE_GO_NAME)))
|
2015-05-25 13:59:08 +12:00
|
|
|
// descsz
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, uint32(len(desc)))
|
2015-05-25 13:59:08 +12:00
|
|
|
// tag
|
2017-09-30 15:06:44 +00:00
|
|
|
s.AddUint32(ctxt.Arch, tag)
|
2015-05-25 13:59:08 +12:00
|
|
|
// name + padding
|
|
|
|
|
s.P = append(s.P, ELF_NOTE_GO_NAME...)
|
|
|
|
|
for len(s.P)%4 != 0 {
|
|
|
|
|
s.P = append(s.P, 0)
|
|
|
|
|
}
|
|
|
|
|
// desc + padding
|
|
|
|
|
s.P = append(s.P, desc...)
|
|
|
|
|
for len(s.P)%4 != 0 {
|
|
|
|
|
s.P = append(s.P, 0)
|
|
|
|
|
}
|
|
|
|
|
s.Size = int64(len(s.P))
|
2017-08-23 17:44:51 -07:00
|
|
|
s.Align = 4
|
2015-05-25 13:59:08 +12:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
func (ctxt *Link) doelf() {
|
2017-10-07 13:43:38 -04:00
|
|
|
if !ctxt.IsELF {
|
2015-02-27 22:57:28 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* predefine strings we need for section headers */
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
shstrtab := ctxt.Syms.Lookup(".shstrtab", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
shstrtab.Type = sym.SELFROSECT
|
|
|
|
|
shstrtab.Attr |= sym.AttrReachable
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, "")
|
|
|
|
|
Addstring(shstrtab, ".text")
|
|
|
|
|
Addstring(shstrtab, ".noptrdata")
|
|
|
|
|
Addstring(shstrtab, ".data")
|
|
|
|
|
Addstring(shstrtab, ".bss")
|
|
|
|
|
Addstring(shstrtab, ".noptrbss")
|
2019-10-18 17:39:39 -07:00
|
|
|
Addstring(shstrtab, "__libfuzzer_extra_counters")
|
2019-04-22 23:02:37 -04:00
|
|
|
Addstring(shstrtab, ".go.buildinfo")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-04-12 01:10:01 +10:00
|
|
|
// generate .tbss section for dynamic internal linker or external
|
|
|
|
|
// linking, so that various binutils could correctly calculate
|
|
|
|
|
// PT_TLS size. See https://golang.org/issue/5200.
|
2017-10-05 10:20:17 -04:00
|
|
|
if !*FlagD || ctxt.LinkMode == LinkExternal {
|
2017-04-12 01:10:01 +10:00
|
|
|
Addstring(shstrtab, ".tbss")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hnetbsd {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".note.netbsd.ident")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hopenbsd {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".note.openbsd.ident")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
if len(buildinfo) > 0 {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".note.gnu.build-id")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-08-21 18:34:24 -04:00
|
|
|
if *flagBuildid != "" {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".note.go.buildid")
|
2015-06-04 14:31:05 -04:00
|
|
|
}
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".elfdata")
|
|
|
|
|
Addstring(shstrtab, ".rodata")
|
2015-05-21 13:07:19 +12:00
|
|
|
// See the comment about data.rel.ro.FOO section names in data.go.
|
|
|
|
|
relro_prefix := ""
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.UseRelro() {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".data.rel.ro")
|
2015-05-21 13:07:19 +12:00
|
|
|
relro_prefix = ".data.rel.ro"
|
|
|
|
|
}
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, relro_prefix+".typelink")
|
|
|
|
|
Addstring(shstrtab, relro_prefix+".itablink")
|
|
|
|
|
Addstring(shstrtab, relro_prefix+".gosymtab")
|
|
|
|
|
Addstring(shstrtab, relro_prefix+".gopclntab")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.LinkMode == LinkExternal {
|
2016-08-21 18:34:24 -04:00
|
|
|
*FlagD = true
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, elfRelType+".text")
|
|
|
|
|
Addstring(shstrtab, elfRelType+".rodata")
|
|
|
|
|
Addstring(shstrtab, elfRelType+relro_prefix+".typelink")
|
|
|
|
|
Addstring(shstrtab, elfRelType+relro_prefix+".itablink")
|
|
|
|
|
Addstring(shstrtab, elfRelType+relro_prefix+".gosymtab")
|
|
|
|
|
Addstring(shstrtab, elfRelType+relro_prefix+".gopclntab")
|
|
|
|
|
Addstring(shstrtab, elfRelType+".noptrdata")
|
|
|
|
|
Addstring(shstrtab, elfRelType+".data")
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.UseRelro() {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, elfRelType+".data.rel.ro")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2019-04-22 23:02:37 -04:00
|
|
|
Addstring(shstrtab, elfRelType+".go.buildinfo")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// add a .note.GNU-stack section to mark the stack as non-executable
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".note.GNU-stack")
|
2015-05-25 13:59:08 +12:00
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.BuildMode == BuildModeShared {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".note.go.abihash")
|
|
|
|
|
Addstring(shstrtab, ".note.go.pkg-list")
|
|
|
|
|
Addstring(shstrtab, ".note.go.deps")
|
2015-05-25 13:59:08 +12:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-07 13:28:51 -04:00
|
|
|
hasinitarr := ctxt.linkShared
|
2015-04-01 14:17:43 +13:00
|
|
|
|
|
|
|
|
/* shared library initializer */
|
2017-10-05 10:20:17 -04:00
|
|
|
switch ctxt.BuildMode {
|
|
|
|
|
case BuildModeCArchive, BuildModeCShared, BuildModeShared, BuildModePlugin:
|
2015-04-01 14:17:43 +13:00
|
|
|
hasinitarr = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if hasinitarr {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".init_array")
|
|
|
|
|
Addstring(shstrtab, elfRelType+".init_array")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
if !*FlagS {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".symtab")
|
|
|
|
|
Addstring(shstrtab, ".strtab")
|
2016-08-19 22:40:38 -04:00
|
|
|
dwarfaddshstrings(ctxt, shstrtab)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".shstrtab")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
if !*FlagD { /* -d suppresses dynamic loader format */
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".interp")
|
|
|
|
|
Addstring(shstrtab, ".hash")
|
|
|
|
|
Addstring(shstrtab, ".got")
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.Family == sys.PPC64 {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".glink")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".got.plt")
|
|
|
|
|
Addstring(shstrtab, ".dynamic")
|
|
|
|
|
Addstring(shstrtab, ".dynsym")
|
|
|
|
|
Addstring(shstrtab, ".dynstr")
|
|
|
|
|
Addstring(shstrtab, elfRelType)
|
|
|
|
|
Addstring(shstrtab, elfRelType+".plt")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(shstrtab, ".plt")
|
|
|
|
|
Addstring(shstrtab, ".gnu.version")
|
|
|
|
|
Addstring(shstrtab, ".gnu.version_r")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/* dynamic symbol table - first entry all zeros */
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s := ctxt.Syms.Lookup(".dynsym", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Type = sym.SELFROSECT
|
|
|
|
|
s.Attr |= sym.AttrReachable
|
2016-04-05 23:01:10 -07:00
|
|
|
if elf64 {
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Size += ELF64SYMSIZE
|
2016-04-05 23:01:10 -07:00
|
|
|
} else {
|
2015-02-27 22:57:28 -05:00
|
|
|
s.Size += ELF32SYMSIZE
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* dynamic string table */
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".dynstr", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Type = sym.SELFROSECT
|
|
|
|
|
s.Attr |= sym.AttrReachable
|
2015-02-27 22:57:28 -05:00
|
|
|
if s.Size == 0 {
|
2016-09-20 15:31:26 +12:00
|
|
|
Addstring(s, "")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-03-02 12:35:15 -05:00
|
|
|
dynstr := s
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/* relocation table */
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(elfRelType, 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFROSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/* global offset table */
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".got", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFGOT // writable
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/* ppc64 glink resolver */
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.Family == sys.PPC64 {
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s := ctxt.Syms.Lookup(".glink", 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFRXSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* hash */
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".hash", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFROSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".got.plt", 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFSECT // writable
|
2015-02-27 22:57:28 -05:00
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".plt", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.Family == sys.PPC64 {
|
2015-02-27 22:57:28 -05:00
|
|
|
// In the ppc64 ABI, .plt is a data section
|
|
|
|
|
// written by the dynamic linker.
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Type = sym.SELFSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Type = sym.SELFRXSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2018-04-01 00:58:48 +03:00
|
|
|
thearch.Elfsetupplt(ctxt)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(elfRelType+".plt", 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFROSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".gnu.version", 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFROSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".gnu.version_r", 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFROSECT
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/* define dynamic elf table */
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s = ctxt.Syms.Lookup(".dynamic", 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrReachable
|
|
|
|
|
s.Type = sym.SELFSECT // writable
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* .dynamic table
|
|
|
|
|
*/
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_HASH, ctxt.Syms.Lookup(".hash", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_SYMTAB, ctxt.Syms.Lookup(".dynsym", 0))
|
2016-04-05 23:01:10 -07:00
|
|
|
if elf64 {
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_SYMENT, ELF64SYMSIZE)
|
2016-04-05 23:01:10 -07:00
|
|
|
} else {
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_SYMENT, ELF32SYMSIZE)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_STRTAB, ctxt.Syms.Lookup(".dynstr", 0))
|
|
|
|
|
elfwritedynentsymsize(ctxt, s, DT_STRSZ, ctxt.Syms.Lookup(".dynstr", 0))
|
2016-04-03 19:32:31 +12:00
|
|
|
if elfRelType == ".rela" {
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_RELA, ctxt.Syms.Lookup(".rela", 0))
|
|
|
|
|
elfwritedynentsymsize(ctxt, s, DT_RELASZ, ctxt.Syms.Lookup(".rela", 0))
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_RELAENT, ELF64RELASIZE)
|
2016-04-03 19:32:31 +12:00
|
|
|
} else {
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_REL, ctxt.Syms.Lookup(".rel", 0))
|
|
|
|
|
elfwritedynentsymsize(ctxt, s, DT_RELSZ, ctxt.Syms.Lookup(".rel", 0))
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_RELENT, ELF32RELSIZE)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-04-12 02:31:28 +02:00
|
|
|
if rpath.val != "" {
|
2016-09-20 15:31:26 +12:00
|
|
|
Elfwritedynent(ctxt, s, DT_RUNPATH, uint64(Addstring(dynstr, rpath.val)))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.Family == sys.PPC64 {
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_PLTGOT, ctxt.Syms.Lookup(".plt", 0))
|
2017-09-30 21:10:49 +00:00
|
|
|
} else if ctxt.Arch.Family == sys.S390X {
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_PLTGOT, ctxt.Syms.Lookup(".got", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
elfwritedynentsym(ctxt, s, DT_PLTGOT, ctxt.Syms.Lookup(".got.plt", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-30 21:10:49 +00:00
|
|
|
if ctxt.Arch.Family == sys.PPC64 {
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_PPC64_OPT, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Solaris dynamic linker can't handle an empty .rela.plt if
|
|
|
|
|
// DT_JMPREL is emitted so we have to defer generation of DT_PLTREL,
|
|
|
|
|
// DT_PLTRELSZ, and DT_JMPREL dynamic entries until after we know the
|
|
|
|
|
// size of .rel(a).plt section.
|
2016-08-19 22:40:38 -04:00
|
|
|
Elfwritedynent(ctxt, s, DT_DEBUG, 0)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2015-05-25 13:59:08 +12:00
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.BuildMode == BuildModeShared {
|
2015-05-25 13:59:08 +12:00
|
|
|
// The go.link.abihashbytes symbol will be pointed at the appropriate
|
|
|
|
|
// part of the .note.go.abihash section in data.go:func address().
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
s := ctxt.Syms.Lookup("go.link.abihashbytes", 0)
|
2017-10-04 17:54:04 -04:00
|
|
|
s.Attr |= sym.AttrLocal
|
|
|
|
|
s.Type = sym.SRODATA
|
|
|
|
|
s.Attr |= sym.AttrSpecial
|
|
|
|
|
s.Attr |= sym.AttrReachable
|
2015-05-25 13:59:08 +12:00
|
|
|
s.Size = int64(sha1.Size)
|
|
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
sort.Sort(byPkg(ctxt.Library))
|
2015-05-25 13:59:08 +12:00
|
|
|
h := sha1.New()
|
2016-08-19 22:40:38 -04:00
|
|
|
for _, l := range ctxt.Library {
|
2017-10-04 18:13:35 -04:00
|
|
|
io.WriteString(h, l.Hash)
|
2015-05-25 13:59:08 +12:00
|
|
|
}
|
2016-08-19 22:40:38 -04:00
|
|
|
addgonote(ctxt, ".note.go.abihash", ELF_NOTE_GOABIHASH_TAG, h.Sum([]byte{}))
|
|
|
|
|
addgonote(ctxt, ".note.go.pkg-list", ELF_NOTE_GOPKGLIST_TAG, pkglistfornote)
|
2015-05-25 14:51:02 +12:00
|
|
|
var deplist []string
|
2016-08-19 22:40:38 -04:00
|
|
|
for _, shlib := range ctxt.Shlibs {
|
2015-05-25 14:51:02 +12:00
|
|
|
deplist = append(deplist, filepath.Base(shlib.Path))
|
|
|
|
|
}
|
2016-08-19 22:40:38 -04:00
|
|
|
addgonote(ctxt, ".note.go.deps", ELF_NOTE_GODEPS_TAG, []byte(strings.Join(deplist, "\n")))
|
2015-05-25 13:59:08 +12:00
|
|
|
}
|
2015-06-04 14:31:05 -04:00
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.LinkMode == LinkExternal && *flagBuildid != "" {
|
2016-08-21 18:34:24 -04:00
|
|
|
addgonote(ctxt, ".note.go.buildid", ELF_NOTE_GOBUILDID_TAG, []byte(*flagBuildid))
|
2015-06-04 14:31:05 -04:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Do not write DT_NULL. elfdynhash will finish it.
|
2017-10-04 17:54:04 -04:00
|
|
|
func shsym(sh *ElfShdr, s *sym.Symbol) {
|
2016-09-17 09:39:33 -04:00
|
|
|
addr := Symaddr(s)
|
2015-02-27 22:57:28 -05:00
|
|
|
if sh.flags&SHF_ALLOC != 0 {
|
|
|
|
|
sh.addr = uint64(addr)
|
|
|
|
|
}
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.off = uint64(datoff(s, addr))
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.size = uint64(s.Size)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func phsh(ph *ElfPhdr, sh *ElfShdr) {
|
|
|
|
|
ph.vaddr = sh.addr
|
|
|
|
|
ph.paddr = ph.vaddr
|
|
|
|
|
ph.off = sh.off
|
|
|
|
|
ph.filesz = sh.size
|
|
|
|
|
ph.memsz = sh.size
|
|
|
|
|
ph.align = sh.addralign
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-20 15:57:53 +12:00
|
|
|
func Asmbelfsetup() {
|
2015-02-27 22:57:28 -05:00
|
|
|
/* This null SHdr must appear before all others */
|
2016-09-17 09:39:33 -04:00
|
|
|
elfshname("")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segtext.Sections {
|
2016-08-25 11:07:33 -05:00
|
|
|
// There could be multiple .text sections. Instead check the Elfsect
|
|
|
|
|
// field to determine if already has an ElfShdr and if not, create one.
|
|
|
|
|
if sect.Name == ".text" {
|
|
|
|
|
if sect.Elfsect == nil {
|
|
|
|
|
sect.Elfsect = elfshnamedup(sect.Name)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
elfshalloc(sect)
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segrodata.Sections {
|
2016-09-17 09:39:33 -04:00
|
|
|
elfshalloc(sect)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segrelrodata.Sections {
|
2016-09-17 09:39:33 -04:00
|
|
|
elfshalloc(sect)
|
2016-09-05 23:29:16 -04:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segdata.Sections {
|
2016-09-17 09:39:33 -04:00
|
|
|
elfshalloc(sect)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segdwarf.Sections {
|
2016-09-17 09:39:33 -04:00
|
|
|
elfshalloc(sect)
|
2016-03-14 09:23:04 -07:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-08-19 22:40:38 -04:00
|
|
|
func Asmbelf(ctxt *Link, symo int64) {
|
2015-03-02 12:35:15 -05:00
|
|
|
eh := getElfEhdr()
|
2017-09-30 21:10:49 +00:00
|
|
|
switch ctxt.Arch.Family {
|
2015-02-27 22:57:28 -05:00
|
|
|
default:
|
2017-09-30 21:10:49 +00:00
|
|
|
Exitf("unknown architecture in asmbelf: %v", ctxt.Arch.Family)
|
2016-10-18 23:50:47 +02:00
|
|
|
case sys.MIPS, sys.MIPS64:
|
2015-09-10 11:32:49 -04:00
|
|
|
eh.machine = EM_MIPS
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.ARM:
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.machine = EM_ARM
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.AMD64:
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.machine = EM_X86_64
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.ARM64:
|
2015-03-08 14:14:53 +01:00
|
|
|
eh.machine = EM_AARCH64
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.I386:
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.machine = EM_386
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.PPC64:
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.machine = EM_PPC64
|
2016-04-06 12:01:40 -07:00
|
|
|
case sys.S390X:
|
2016-03-18 16:57:54 -04:00
|
|
|
eh.machine = EM_S390
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-05-05 14:17:07 +12:00
|
|
|
elfreserve := int64(ELFRESERVE)
|
2016-08-25 11:07:33 -05:00
|
|
|
|
|
|
|
|
numtext := int64(0)
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segtext.Sections {
|
2016-08-25 11:07:33 -05:00
|
|
|
if sect.Name == ".text" {
|
|
|
|
|
numtext++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there are multiple text sections, extra space is needed
|
|
|
|
|
// in the elfreserve for the additional .text and .rela.text
|
|
|
|
|
// section headers. It can handle 4 extra now. Headers are
|
|
|
|
|
// 64 bytes.
|
|
|
|
|
|
|
|
|
|
if numtext > 4 {
|
|
|
|
|
elfreserve += elfreserve + numtext*64*2
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
startva := *FlagTextAddr - int64(HEADR)
|
2015-05-05 14:17:07 +12:00
|
|
|
resoff := elfreserve
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2015-03-02 14:22:05 -05:00
|
|
|
var pph *ElfPhdr
|
2015-03-02 12:35:15 -05:00
|
|
|
var pnote *ElfPhdr
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.LinkMode == LinkExternal {
|
2015-02-27 22:57:28 -05:00
|
|
|
/* skip program headers */
|
|
|
|
|
eh.phoff = 0
|
|
|
|
|
|
|
|
|
|
eh.phentsize = 0
|
2015-04-29 22:58:52 +12:00
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.BuildMode == BuildModeShared {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".note.go.pkg-list")
|
2015-05-25 13:59:08 +12:00
|
|
|
sh.type_ = SHT_NOTE
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".note.go.abihash")
|
2015-05-25 13:59:08 +12:00
|
|
|
sh.type_ = SHT_NOTE
|
|
|
|
|
sh.flags = SHF_ALLOC
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".note.go.deps")
|
2015-05-25 14:51:02 +12:00
|
|
|
sh.type_ = SHT_NOTE
|
2015-04-29 22:58:52 +12:00
|
|
|
}
|
2015-06-04 14:31:05 -04:00
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
if *flagBuildid != "" {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".note.go.buildid")
|
2015-06-04 14:31:05 -04:00
|
|
|
sh.type_ = SHT_NOTE
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
goto elfobj
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* program header info */
|
2016-09-17 09:39:33 -04:00
|
|
|
pph = newElfPhdr()
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
pph.type_ = PT_PHDR
|
|
|
|
|
pph.flags = PF_R
|
|
|
|
|
pph.off = uint64(eh.ehsize)
|
2016-08-21 18:34:24 -04:00
|
|
|
pph.vaddr = uint64(*FlagTextAddr) - uint64(HEADR) + pph.off
|
|
|
|
|
pph.paddr = uint64(*FlagTextAddr) - uint64(HEADR) + pph.off
|
|
|
|
|
pph.align = uint64(*FlagRound)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* PHDR must be in a loaded segment. Adjust the text
|
|
|
|
|
* segment boundaries downwards to include it.
|
|
|
|
|
*/
|
2019-10-09 16:22:47 +00:00
|
|
|
{
|
2015-03-02 12:35:15 -05:00
|
|
|
o := int64(Segtext.Vaddr - pph.vaddr)
|
2015-02-27 22:57:28 -05:00
|
|
|
Segtext.Vaddr -= uint64(o)
|
|
|
|
|
Segtext.Length += uint64(o)
|
|
|
|
|
o = int64(Segtext.Fileoff - pph.off)
|
|
|
|
|
Segtext.Fileoff -= uint64(o)
|
|
|
|
|
Segtext.Filelen += uint64(o)
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
if !*FlagD { /* -d suppresses dynamic loader format */
|
2015-02-27 22:57:28 -05:00
|
|
|
/* interpreter */
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".interp")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
sh.type_ = SHT_PROGBITS
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
sh.addralign = 1
|
2019-02-25 11:32:00 +01:00
|
|
|
|
|
|
|
|
if interpreter == "" && objabi.GO_LDSO != "" {
|
|
|
|
|
interpreter = objabi.GO_LDSO
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-22 22:29:24 -07:00
|
|
|
if interpreter == "" {
|
2017-10-07 13:49:44 -04:00
|
|
|
switch ctxt.HeadType {
|
2017-04-18 12:53:25 -07:00
|
|
|
case objabi.Hlinux:
|
2018-04-01 00:58:48 +03:00
|
|
|
interpreter = thearch.Linuxdynld
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-04-18 12:53:25 -07:00
|
|
|
case objabi.Hfreebsd:
|
2018-04-01 00:58:48 +03:00
|
|
|
interpreter = thearch.Freebsddynld
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-04-18 12:53:25 -07:00
|
|
|
case objabi.Hnetbsd:
|
2018-04-01 00:58:48 +03:00
|
|
|
interpreter = thearch.Netbsddynld
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-04-18 12:53:25 -07:00
|
|
|
case objabi.Hopenbsd:
|
2018-04-01 00:58:48 +03:00
|
|
|
interpreter = thearch.Openbsddynld
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-04-18 12:53:25 -07:00
|
|
|
case objabi.Hdragonfly:
|
2018-04-01 00:58:48 +03:00
|
|
|
interpreter = thearch.Dragonflydynld
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-04-18 12:53:25 -07:00
|
|
|
case objabi.Hsolaris:
|
2018-04-01 00:58:48 +03:00
|
|
|
interpreter = thearch.Solarisdynld
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-22 22:29:24 -07:00
|
|
|
resoff -= int64(elfinterp(sh, uint64(startva), uint64(resoff), interpreter))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
ph := newElfPhdr()
|
2015-02-27 22:57:28 -05:00
|
|
|
ph.type_ = PT_INTERP
|
|
|
|
|
ph.flags = PF_R
|
|
|
|
|
phsh(ph, sh)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pnote = nil
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hnetbsd || ctxt.HeadType == objabi.Hopenbsd {
|
2015-03-02 14:22:05 -05:00
|
|
|
var sh *ElfShdr
|
2017-10-07 13:49:44 -04:00
|
|
|
switch ctxt.HeadType {
|
2017-04-18 12:53:25 -07:00
|
|
|
case objabi.Hnetbsd:
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".note.netbsd.ident")
|
2015-02-27 22:57:28 -05:00
|
|
|
resoff -= int64(elfnetbsdsig(sh, uint64(startva), uint64(resoff)))
|
|
|
|
|
|
2017-04-18 12:53:25 -07:00
|
|
|
case objabi.Hopenbsd:
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".note.openbsd.ident")
|
2015-02-27 22:57:28 -05:00
|
|
|
resoff -= int64(elfopenbsdsig(sh, uint64(startva), uint64(resoff)))
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
pnote = newElfPhdr()
|
2015-02-27 22:57:28 -05:00
|
|
|
pnote.type_ = PT_NOTE
|
|
|
|
|
pnote.flags = PF_R
|
|
|
|
|
phsh(pnote, sh)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(buildinfo) > 0 {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".note.gnu.build-id")
|
2015-02-27 22:57:28 -05:00
|
|
|
resoff -= int64(elfbuildinfo(sh, uint64(startva), uint64(resoff)))
|
|
|
|
|
|
|
|
|
|
if pnote == nil {
|
2016-09-17 09:39:33 -04:00
|
|
|
pnote = newElfPhdr()
|
2015-02-27 22:57:28 -05:00
|
|
|
pnote.type_ = PT_NOTE
|
|
|
|
|
pnote.flags = PF_R
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
phsh(pnote, sh)
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
if *flagBuildid != "" {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".note.go.buildid")
|
2015-06-04 14:31:05 -04:00
|
|
|
resoff -= int64(elfgobuildid(sh, uint64(startva), uint64(resoff)))
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
pnote := newElfPhdr()
|
2015-06-04 14:31:05 -04:00
|
|
|
pnote.type_ = PT_NOTE
|
|
|
|
|
pnote.flags = PF_R
|
|
|
|
|
phsh(pnote, sh)
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
// Additions to the reserved area must be above this line.
|
|
|
|
|
|
2016-09-20 15:57:53 +12:00
|
|
|
elfphload(&Segtext)
|
2017-04-18 21:52:06 +12:00
|
|
|
if len(Segrodata.Sections) > 0 {
|
2016-09-20 15:57:53 +12:00
|
|
|
elfphload(&Segrodata)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
if len(Segrelrodata.Sections) > 0 {
|
2016-09-20 15:57:53 +12:00
|
|
|
elfphload(&Segrelrodata)
|
|
|
|
|
elfphrelro(&Segrelrodata)
|
2016-09-05 23:29:16 -04:00
|
|
|
}
|
2016-09-20 15:57:53 +12:00
|
|
|
elfphload(&Segdata)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/* Dynamic linking sections */
|
2016-08-21 18:34:24 -04:00
|
|
|
if !*FlagD {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".dynsym")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_DYNSYM
|
|
|
|
|
sh.flags = SHF_ALLOC
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.entsize = ELF64SYMSIZE
|
|
|
|
|
} else {
|
|
|
|
|
sh.entsize = ELF32SYMSIZE
|
|
|
|
|
}
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.addralign = uint64(ctxt.Arch.RegSize)
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".dynstr").shnum)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2019-07-31 11:45:32 +07:00
|
|
|
// sh.info is the index of first non-local symbol (number of local symbols)
|
|
|
|
|
s := ctxt.Syms.Lookup(".dynsym", 0)
|
|
|
|
|
i := uint32(0)
|
|
|
|
|
for sub := s; sub != nil; sub = sub.Sub {
|
|
|
|
|
i++
|
|
|
|
|
if !sub.Attr.Local() {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-01 12:47:49 +07:00
|
|
|
sh.info = i
|
2019-07-31 11:45:32 +07:00
|
|
|
shsym(sh, s)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".dynstr")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_STRTAB
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
sh.addralign = 1
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".dynstr", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
if elfverneed != 0 {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".gnu.version")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_GNU_VERSYM
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
sh.addralign = 2
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".dynsym").shnum)
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.entsize = 2
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".gnu.version", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".gnu.version_r")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_GNU_VERNEED
|
|
|
|
|
sh.flags = SHF_ALLOC
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.addralign = uint64(ctxt.Arch.RegSize)
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.info = uint32(elfverneed)
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".dynstr").shnum)
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".gnu.version_r", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-04-03 19:32:31 +12:00
|
|
|
if elfRelType == ".rela" {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".rela.plt")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_RELA
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
sh.entsize = ELF64RELASIZE
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.addralign = uint64(ctxt.Arch.RegSize)
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".dynsym").shnum)
|
|
|
|
|
sh.info = uint32(elfshname(".plt").shnum)
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".rela.plt", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".rela")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_RELA
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
sh.entsize = ELF64RELASIZE
|
|
|
|
|
sh.addralign = 8
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".dynsym").shnum)
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".rela", 0))
|
2016-04-03 19:32:31 +12:00
|
|
|
} else {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".rel.plt")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_REL
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
sh.entsize = ELF32RELSIZE
|
|
|
|
|
sh.addralign = 4
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".dynsym").shnum)
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".rel.plt", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".rel")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_REL
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
sh.entsize = ELF32RELSIZE
|
|
|
|
|
sh.addralign = 4
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".dynsym").shnum)
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".rel", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if eh.machine == EM_PPC64 {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".glink")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_PROGBITS
|
|
|
|
|
sh.flags = SHF_ALLOC + SHF_EXECINSTR
|
|
|
|
|
sh.addralign = 4
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".glink", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".plt")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_PROGBITS
|
|
|
|
|
sh.flags = SHF_ALLOC + SHF_EXECINSTR
|
|
|
|
|
if eh.machine == EM_X86_64 {
|
|
|
|
|
sh.entsize = 16
|
2016-03-18 16:57:54 -04:00
|
|
|
} else if eh.machine == EM_S390 {
|
|
|
|
|
sh.entsize = 32
|
2015-02-27 22:57:28 -05:00
|
|
|
} else if eh.machine == EM_PPC64 {
|
|
|
|
|
// On ppc64, this is just a table of addresses
|
|
|
|
|
// filled by the dynamic linker
|
|
|
|
|
sh.type_ = SHT_NOBITS
|
|
|
|
|
|
|
|
|
|
sh.flags = SHF_ALLOC + SHF_WRITE
|
|
|
|
|
sh.entsize = 8
|
|
|
|
|
} else {
|
|
|
|
|
sh.entsize = 4
|
|
|
|
|
}
|
|
|
|
|
sh.addralign = sh.entsize
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".plt", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
// On ppc64, .got comes from the input files, so don't
|
|
|
|
|
// create it here, and .got.plt is not used.
|
|
|
|
|
if eh.machine != EM_PPC64 {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".got")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_PROGBITS
|
|
|
|
|
sh.flags = SHF_ALLOC + SHF_WRITE
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.entsize = uint64(ctxt.Arch.RegSize)
|
|
|
|
|
sh.addralign = uint64(ctxt.Arch.RegSize)
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".got", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".got.plt")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_PROGBITS
|
|
|
|
|
sh.flags = SHF_ALLOC + SHF_WRITE
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.entsize = uint64(ctxt.Arch.RegSize)
|
|
|
|
|
sh.addralign = uint64(ctxt.Arch.RegSize)
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".got.plt", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".hash")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_HASH
|
|
|
|
|
sh.flags = SHF_ALLOC
|
|
|
|
|
sh.entsize = 4
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.addralign = uint64(ctxt.Arch.RegSize)
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".dynsym").shnum)
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".hash", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
/* sh and PT_DYNAMIC for .dynamic section */
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".dynamic")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
sh.type_ = SHT_DYNAMIC
|
|
|
|
|
sh.flags = SHF_ALLOC + SHF_WRITE
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.entsize = 2 * uint64(ctxt.Arch.RegSize)
|
|
|
|
|
sh.addralign = uint64(ctxt.Arch.RegSize)
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".dynstr").shnum)
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".dynamic", 0))
|
2016-09-17 09:39:33 -04:00
|
|
|
ph := newElfPhdr()
|
2015-02-27 22:57:28 -05:00
|
|
|
ph.type_ = PT_DYNAMIC
|
|
|
|
|
ph.flags = PF_R + PF_W
|
|
|
|
|
phsh(ph, sh)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Thread-local storage segment (really just size).
|
|
|
|
|
*/
|
2017-04-12 01:10:01 +10:00
|
|
|
tlssize := uint64(0)
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segdata.Sections {
|
2017-04-12 01:10:01 +10:00
|
|
|
if sect.Name == ".tbss" {
|
|
|
|
|
tlssize = sect.Length
|
2015-08-11 12:29:00 +12:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-12 01:10:01 +10:00
|
|
|
if tlssize != 0 {
|
|
|
|
|
ph := newElfPhdr()
|
|
|
|
|
ph.type_ = PT_TLS
|
|
|
|
|
ph.flags = PF_R
|
|
|
|
|
ph.memsz = tlssize
|
2017-09-30 21:10:49 +00:00
|
|
|
ph.align = uint64(ctxt.Arch.RegSize)
|
2017-04-12 01:10:01 +10:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hlinux {
|
2016-09-17 09:39:33 -04:00
|
|
|
ph := newElfPhdr()
|
2015-02-27 22:57:28 -05:00
|
|
|
ph.type_ = PT_GNU_STACK
|
|
|
|
|
ph.flags = PF_W + PF_R
|
2017-09-30 21:10:49 +00:00
|
|
|
ph.align = uint64(ctxt.Arch.RegSize)
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
ph = newElfPhdr()
|
2015-02-27 22:57:28 -05:00
|
|
|
ph.type_ = PT_PAX_FLAGS
|
|
|
|
|
ph.flags = 0x2a00 // mprotect, randexec, emutramp disabled
|
2017-09-30 21:10:49 +00:00
|
|
|
ph.align = uint64(ctxt.Arch.RegSize)
|
2017-10-07 13:49:44 -04:00
|
|
|
} else if ctxt.HeadType == objabi.Hsolaris {
|
2016-06-15 13:44:03 -07:00
|
|
|
ph := newElfPhdr()
|
|
|
|
|
ph.type_ = PT_SUNWSTACK
|
|
|
|
|
ph.flags = PF_W + PF_R
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elfobj:
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".shstrtab")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_STRTAB
|
|
|
|
|
sh.addralign = 1
|
2016-09-20 15:57:53 +12:00
|
|
|
shsym(sh, ctxt.Syms.Lookup(".shstrtab", 0))
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.shstrndx = uint16(sh.shnum)
|
|
|
|
|
|
|
|
|
|
// put these sections early in the list
|
2016-08-21 18:34:24 -04:00
|
|
|
if !*FlagS {
|
2016-09-17 09:39:33 -04:00
|
|
|
elfshname(".symtab")
|
|
|
|
|
elfshname(".strtab")
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segtext.Sections {
|
2017-10-05 10:20:17 -04:00
|
|
|
elfshbits(ctxt.LinkMode, sect)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segrodata.Sections {
|
2017-10-05 10:20:17 -04:00
|
|
|
elfshbits(ctxt.LinkMode, sect)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segrelrodata.Sections {
|
2017-10-05 10:20:17 -04:00
|
|
|
elfshbits(ctxt.LinkMode, sect)
|
2016-09-05 23:29:16 -04:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segdata.Sections {
|
2017-10-05 10:20:17 -04:00
|
|
|
elfshbits(ctxt.LinkMode, sect)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segdwarf.Sections {
|
2017-10-05 10:20:17 -04:00
|
|
|
elfshbits(ctxt.LinkMode, sect)
|
2016-03-14 09:23:04 -07:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.LinkMode == LinkExternal {
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segtext.Sections {
|
2017-09-30 21:10:49 +00:00
|
|
|
elfshreloc(ctxt.Arch, sect)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segrodata.Sections {
|
2017-09-30 21:10:49 +00:00
|
|
|
elfshreloc(ctxt.Arch, sect)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segrelrodata.Sections {
|
2017-09-30 21:10:49 +00:00
|
|
|
elfshreloc(ctxt.Arch, sect)
|
2016-09-05 23:29:16 -04:00
|
|
|
}
|
2017-04-18 21:52:06 +12:00
|
|
|
for _, sect := range Segdata.Sections {
|
2017-09-30 21:10:49 +00:00
|
|
|
elfshreloc(ctxt.Arch, sect)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-04-22 10:31:14 +12:00
|
|
|
for _, s := range dwarfp {
|
2017-10-04 17:54:04 -04:00
|
|
|
if len(s.R) > 0 || s.Type == sym.SDWARFINFO || s.Type == sym.SDWARFLOC {
|
2017-09-30 21:10:49 +00:00
|
|
|
elfshreloc(ctxt.Arch, s.Sect)
|
2016-03-14 09:23:04 -07:00
|
|
|
}
|
|
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
// add a .note.GNU-stack section to mark the stack as non-executable
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".note.GNU-stack")
|
2015-02-27 22:57:28 -05:00
|
|
|
|
|
|
|
|
sh.type_ = SHT_PROGBITS
|
|
|
|
|
sh.addralign = 1
|
|
|
|
|
sh.flags = 0
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-21 18:34:24 -04:00
|
|
|
if !*FlagS {
|
2016-09-17 09:39:33 -04:00
|
|
|
sh := elfshname(".symtab")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_SYMTAB
|
|
|
|
|
sh.off = uint64(symo)
|
|
|
|
|
sh.size = uint64(Symsize)
|
2017-09-30 21:10:49 +00:00
|
|
|
sh.addralign = uint64(ctxt.Arch.RegSize)
|
|
|
|
|
sh.entsize = 8 + 2*uint64(ctxt.Arch.RegSize)
|
2016-09-17 09:39:33 -04:00
|
|
|
sh.link = uint32(elfshname(".strtab").shnum)
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.info = uint32(elfglobalsymndx)
|
|
|
|
|
|
2016-09-17 09:39:33 -04:00
|
|
|
sh = elfshname(".strtab")
|
2015-02-27 22:57:28 -05:00
|
|
|
sh.type_ = SHT_STRTAB
|
|
|
|
|
sh.off = uint64(symo) + uint64(Symsize)
|
|
|
|
|
sh.size = uint64(len(Elfstrdat))
|
|
|
|
|
sh.addralign = 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Main header */
|
|
|
|
|
eh.ident[EI_MAG0] = '\177'
|
|
|
|
|
|
|
|
|
|
eh.ident[EI_MAG1] = 'E'
|
|
|
|
|
eh.ident[EI_MAG2] = 'L'
|
|
|
|
|
eh.ident[EI_MAG3] = 'F'
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hfreebsd {
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.ident[EI_OSABI] = ELFOSABI_FREEBSD
|
2017-10-07 13:49:44 -04:00
|
|
|
} else if ctxt.HeadType == objabi.Hnetbsd {
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.ident[EI_OSABI] = ELFOSABI_NETBSD
|
2017-10-07 13:49:44 -04:00
|
|
|
} else if ctxt.HeadType == objabi.Hopenbsd {
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.ident[EI_OSABI] = ELFOSABI_OPENBSD
|
2017-10-07 13:49:44 -04:00
|
|
|
} else if ctxt.HeadType == objabi.Hdragonfly {
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.ident[EI_OSABI] = ELFOSABI_NONE
|
|
|
|
|
}
|
2015-04-08 08:44:08 -07:00
|
|
|
if elf64 {
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.ident[EI_CLASS] = ELFCLASS64
|
|
|
|
|
} else {
|
|
|
|
|
eh.ident[EI_CLASS] = ELFCLASS32
|
|
|
|
|
}
|
2016-08-19 22:40:38 -04:00
|
|
|
if ctxt.Arch.ByteOrder == binary.BigEndian {
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.ident[EI_DATA] = ELFDATA2MSB
|
|
|
|
|
} else {
|
|
|
|
|
eh.ident[EI_DATA] = ELFDATA2LSB
|
|
|
|
|
}
|
|
|
|
|
eh.ident[EI_VERSION] = EV_CURRENT
|
|
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.LinkMode == LinkExternal {
|
2015-02-27 22:57:28 -05:00
|
|
|
eh.type_ = ET_REL
|
2017-10-05 10:20:17 -04:00
|
|
|
} else if ctxt.BuildMode == BuildModePIE {
|
2016-09-06 08:05:19 -04:00
|
|
|
eh.type_ = ET_DYN
|
2015-02-27 22:57:28 -05:00
|
|
|
} else {
|
|
|
|
|
eh.type_ = ET_EXEC
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.LinkMode != LinkExternal {
|
2016-08-19 22:40:38 -04:00
|
|
|
eh.entry = uint64(Entryvalue(ctxt))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
eh.version = EV_CURRENT
|
|
|
|
|
|
|
|
|
|
if pph != nil {
|
|
|
|
|
pph.filesz = uint64(eh.phnum) * uint64(eh.phentsize)
|
|
|
|
|
pph.memsz = pph.filesz
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-01 02:37:20 +00:00
|
|
|
ctxt.Out.SeekSet(0)
|
2015-03-02 12:35:15 -05:00
|
|
|
a := int64(0)
|
2017-10-01 02:37:20 +00:00
|
|
|
a += int64(elfwritehdr(ctxt.Out))
|
|
|
|
|
a += int64(elfwritephdrs(ctxt.Out))
|
|
|
|
|
a += int64(elfwriteshdrs(ctxt.Out))
|
2016-08-21 18:34:24 -04:00
|
|
|
if !*FlagD {
|
2017-10-01 02:37:20 +00:00
|
|
|
a += int64(elfwriteinterp(ctxt.Out))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-05 10:20:17 -04:00
|
|
|
if ctxt.LinkMode != LinkExternal {
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hnetbsd {
|
2017-10-01 02:37:20 +00:00
|
|
|
a += int64(elfwritenetbsdsig(ctxt.Out))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2017-10-07 13:49:44 -04:00
|
|
|
if ctxt.HeadType == objabi.Hopenbsd {
|
2017-10-01 02:37:20 +00:00
|
|
|
a += int64(elfwriteopenbsdsig(ctxt.Out))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
if len(buildinfo) > 0 {
|
2017-10-01 02:37:20 +00:00
|
|
|
a += int64(elfwritebuildinfo(ctxt.Out))
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
2016-08-21 18:34:24 -04:00
|
|
|
if *flagBuildid != "" {
|
2017-10-01 02:37:20 +00:00
|
|
|
a += int64(elfwritegobuildid(ctxt.Out))
|
2015-06-04 14:31:05 -04:00
|
|
|
}
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
|
2015-05-05 14:17:07 +12:00
|
|
|
if a > elfreserve {
|
2016-08-25 11:07:33 -05:00
|
|
|
Errorf(nil, "ELFRESERVE too small: %d > %d with %d text sections", a, elfreserve, numtext)
|
2015-02-27 22:57:28 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 17:54:04 -04:00
|
|
|
func elfadddynsym(ctxt *Link, s *sym.Symbol) {
|
2015-05-12 15:59:15 +12:00
|
|
|
if elf64 {
|
|
|
|
|
s.Dynid = int32(Nelfsym)
|
|
|
|
|
Nelfsym++
|
|
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
d := ctxt.Syms.Lookup(".dynsym", 0)
|
2015-05-12 15:59:15 +12:00
|
|
|
|
2018-07-17 11:02:57 -04:00
|
|
|
name := s.Extname()
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint32(ctxt.Arch, uint32(Addstring(ctxt.Syms.Lookup(".dynstr", 0), name)))
|
2015-05-12 15:59:15 +12:00
|
|
|
|
|
|
|
|
/* type */
|
|
|
|
|
t := STB_GLOBAL << 4
|
|
|
|
|
|
2017-04-28 13:01:03 +12:00
|
|
|
if s.Attr.CgoExport() && s.Type == sym.STEXT {
|
2015-05-12 15:59:15 +12:00
|
|
|
t |= STT_FUNC
|
|
|
|
|
} else {
|
|
|
|
|
t |= STT_OBJECT
|
|
|
|
|
}
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint8(uint8(t))
|
2015-05-12 15:59:15 +12:00
|
|
|
|
|
|
|
|
/* reserved */
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint8(0)
|
2015-05-12 15:59:15 +12:00
|
|
|
|
|
|
|
|
/* section where symbol is defined */
|
2017-10-04 17:54:04 -04:00
|
|
|
if s.Type == sym.SDYNIMPORT {
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint16(ctxt.Arch, SHN_UNDEF)
|
2015-05-12 15:59:15 +12:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint16(ctxt.Arch, 1)
|
2015-05-12 15:59:15 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* value */
|
2017-10-04 17:54:04 -04:00
|
|
|
if s.Type == sym.SDYNIMPORT {
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint64(ctxt.Arch, 0)
|
2015-05-12 15:59:15 +12:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddAddr(ctxt.Arch, s)
|
2015-05-12 15:59:15 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* size of object */
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint64(ctxt.Arch, uint64(s.Size))
|
2015-05-12 15:59:15 +12:00
|
|
|
|
2018-06-28 16:10:19 -04:00
|
|
|
if ctxt.Arch.Family == sys.AMD64 && !s.Attr.CgoExportDynamic() && s.Dynimplib() != "" && !seenlib[s.Dynimplib()] {
|
|
|
|
|
Elfwritedynent(ctxt, ctxt.Syms.Lookup(".dynamic", 0), DT_NEEDED, uint64(Addstring(ctxt.Syms.Lookup(".dynstr", 0), s.Dynimplib())))
|
2015-05-12 15:59:15 +12:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
s.Dynid = int32(Nelfsym)
|
|
|
|
|
Nelfsym++
|
|
|
|
|
|
cmd/link: use ctxt.{Lookup,ROLookup} in favour of function versions of same
Done with two eg templates:
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linklookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.Lookup(name, v)
}
package p
import (
"cmd/link/internal/ld"
)
func before(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ld.Linkrlookup(ctxt, name, v)
}
func after(ctxt *ld.Link, name string, v int) *ld.Symbol {
return ctxt.Syms.ROLookup(name, v)
}
Change-Id: I00647dbf62294557bd24c29ad1f108fc786335f1
Reviewed-on: https://go-review.googlesource.com/29343
Run-TryBot: Michael Hudson-Doyle <michael.hudson@canonical.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2016-09-20 15:06:08 +12:00
|
|
|
d := ctxt.Syms.Lookup(".dynsym", 0)
|
2015-05-12 15:59:15 +12:00
|
|
|
|
|
|
|
|
/* name */
|
2018-07-17 11:02:57 -04:00
|
|
|
name := s.Extname()
|
2015-05-12 15:59:15 +12:00
|
|
|
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint32(ctxt.Arch, uint32(Addstring(ctxt.Syms.Lookup(".dynstr", 0), name)))
|
2015-05-12 15:59:15 +12:00
|
|
|
|
|
|
|
|
/* value */
|
2017-10-04 17:54:04 -04:00
|
|
|
if s.Type == sym.SDYNIMPORT {
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint32(ctxt.Arch, 0)
|
2015-05-12 15:59:15 +12:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddAddr(ctxt.Arch, s)
|
2015-05-12 15:59:15 +12:00
|
|
|
}
|
|
|
|
|
|
2016-05-08 01:27:45 +10:00
|
|
|
/* size of object */
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint32(ctxt.Arch, uint32(s.Size))
|
2015-05-12 15:59:15 +12:00
|
|
|
|
|
|
|
|
/* type */
|
|
|
|
|
t := STB_GLOBAL << 4
|
|
|
|
|
|
2016-12-08 22:15:40 +00:00
|
|
|
// TODO(mwhudson): presumably the behavior should actually be the same on both arm and 386.
|
2017-04-28 13:01:03 +12:00
|
|
|
if ctxt.Arch.Family == sys.I386 && s.Attr.CgoExport() && s.Type == sym.STEXT {
|
2015-05-12 15:59:15 +12:00
|
|
|
t |= STT_FUNC
|
2017-04-28 13:01:03 +12:00
|
|
|
} else if ctxt.Arch.Family == sys.ARM && s.Attr.CgoExportDynamic() && s.Type == sym.STEXT {
|
2015-05-12 15:59:15 +12:00
|
|
|
t |= STT_FUNC
|
|
|
|
|
} else {
|
|
|
|
|
t |= STT_OBJECT
|
|
|
|
|
}
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint8(uint8(t))
|
|
|
|
|
d.AddUint8(0)
|
2015-05-12 15:59:15 +12:00
|
|
|
|
|
|
|
|
/* shndx */
|
2017-10-04 17:54:04 -04:00
|
|
|
if s.Type == sym.SDYNIMPORT {
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint16(ctxt.Arch, SHN_UNDEF)
|
2015-05-12 15:59:15 +12:00
|
|
|
} else {
|
2017-09-30 15:06:44 +00:00
|
|
|
d.AddUint16(ctxt.Arch, 1)
|
2015-05-12 15:59:15 +12:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-27 22:57:28 -05:00
|
|
|
func ELF32_R_SYM(info uint32) uint32 {
|
|
|
|
|
return info >> 8
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF32_R_TYPE(info uint32) uint32 {
|
|
|
|
|
return uint32(uint8(info))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF32_R_INFO(sym uint32, type_ uint32) uint32 {
|
|
|
|
|
return sym<<8 | type_
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF32_ST_BIND(info uint8) uint8 {
|
|
|
|
|
return info >> 4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF32_ST_TYPE(info uint8) uint8 {
|
|
|
|
|
return info & 0xf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF32_ST_INFO(bind uint8, type_ uint8) uint8 {
|
|
|
|
|
return bind<<4 | type_&0xf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF32_ST_VISIBILITY(oth uint8) uint8 {
|
|
|
|
|
return oth & 3
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF64_R_SYM(info uint64) uint32 {
|
|
|
|
|
return uint32(info >> 32)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF64_R_TYPE(info uint64) uint32 {
|
|
|
|
|
return uint32(info)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF64_R_INFO(sym uint32, type_ uint32) uint64 {
|
|
|
|
|
return uint64(sym)<<32 | uint64(type_)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF64_ST_BIND(info uint8) uint8 {
|
|
|
|
|
return info >> 4
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF64_ST_TYPE(info uint8) uint8 {
|
|
|
|
|
return info & 0xf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF64_ST_INFO(bind uint8, type_ uint8) uint8 {
|
|
|
|
|
return bind<<4 | type_&0xf
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ELF64_ST_VISIBILITY(oth uint8) uint8 {
|
|
|
|
|
return oth & 3
|
|
|
|
|
}
|