1*e60a0123Smiod /* $OpenBSD: rtld_machine.c,v 1.70 2024/03/30 08:44:20 miod Exp $ */
2b76408a9Sdrahn
3b76408a9Sdrahn /*
4b76408a9Sdrahn * Copyright (c) 1999 Dale Rahn
5b76408a9Sdrahn * Copyright (c) 2001 Niklas Hallqvist
6b76408a9Sdrahn * Copyright (c) 2001 Artur Grabowski
7b76408a9Sdrahn *
8b76408a9Sdrahn * Redistribution and use in source and binary forms, with or without
9b76408a9Sdrahn * modification, are permitted provided that the following conditions
10b76408a9Sdrahn * are met:
11b76408a9Sdrahn * 1. Redistributions of source code must retain the above copyright
12b76408a9Sdrahn * notice, this list of conditions and the following disclaimer.
13b76408a9Sdrahn * 2. Redistributions in binary form must reproduce the above copyright
14b76408a9Sdrahn * notice, this list of conditions and the following disclaimer in the
15b76408a9Sdrahn * documentation and/or other materials provided with the distribution.
16b76408a9Sdrahn *
17b76408a9Sdrahn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18b76408a9Sdrahn * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19b76408a9Sdrahn * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20b76408a9Sdrahn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21b76408a9Sdrahn * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22b76408a9Sdrahn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23b76408a9Sdrahn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24b76408a9Sdrahn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25b76408a9Sdrahn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26b76408a9Sdrahn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27b76408a9Sdrahn * SUCH DAMAGE.
288b3b42e8Sart */
298b3b42e8Sart /*-
308b3b42e8Sart * Copyright (c) 2000 Eduardo Horvath.
318b3b42e8Sart * Copyright (c) 1999 The NetBSD Foundation, Inc.
328b3b42e8Sart * All rights reserved.
33b76408a9Sdrahn *
348b3b42e8Sart * This code is derived from software contributed to The NetBSD Foundation
358b3b42e8Sart * by Paul Kranenburg.
368b3b42e8Sart *
378b3b42e8Sart * Redistribution and use in source and binary forms, with or without
388b3b42e8Sart * modification, are permitted provided that the following conditions
398b3b42e8Sart * are met:
408b3b42e8Sart * 1. Redistributions of source code must retain the above copyright
418b3b42e8Sart * notice, this list of conditions and the following disclaimer.
428b3b42e8Sart * 2. Redistributions in binary form must reproduce the above copyright
438b3b42e8Sart * notice, this list of conditions and the following disclaimer in the
448b3b42e8Sart * documentation and/or other materials provided with the distribution.
458b3b42e8Sart * 3. All advertising materials mentioning features or use of this software
468b3b42e8Sart * must display the following acknowledgement:
478b3b42e8Sart * This product includes software developed by the NetBSD
488b3b42e8Sart * Foundation, Inc. and its contributors.
498b3b42e8Sart * 4. Neither the name of The NetBSD Foundation nor the names of its
508b3b42e8Sart * contributors may be used to endorse or promote products derived
518b3b42e8Sart * from this software without specific prior written permission.
528b3b42e8Sart *
538b3b42e8Sart * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
548b3b42e8Sart * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
558b3b42e8Sart * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
568b3b42e8Sart * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
578b3b42e8Sart * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
588b3b42e8Sart * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
598b3b42e8Sart * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
608b3b42e8Sart * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
618b3b42e8Sart * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
628b3b42e8Sart * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
638b3b42e8Sart * POSSIBILITY OF SUCH DAMAGE.
64b76408a9Sdrahn */
65b76408a9Sdrahn
66b76408a9Sdrahn #define _DYN_LOADER
67b76408a9Sdrahn
68b76408a9Sdrahn #include <sys/types.h>
69b722ba42Sguenther #include <sys/exec_elf.h>
708465df4aSguenther #include <sys/syscall.h>
718465df4aSguenther #include <sys/unistd.h>
72b76408a9Sdrahn
73b722ba42Sguenther #include <machine/reloc.h>
74b722ba42Sguenther #include <machine/trap.h> /* for ST_SYSCALL */
75b76408a9Sdrahn
76b722ba42Sguenther #include "util.h"
77b76408a9Sdrahn #include "resolve.h"
78b76408a9Sdrahn
798465df4aSguenther int64_t pcookie __attribute__((section(".openbsd.randomdata"))) __dso_hidden;
808465df4aSguenther
818b3b42e8Sart /*
828b3b42e8Sart * The following table holds for each relocation type:
838b3b42e8Sart * - the width in bits of the memory location the relocation
848b3b42e8Sart * applies to (not currently used)
858b3b42e8Sart * - the number of bits the relocation value must be shifted to the
868b3b42e8Sart * right (i.e. discard least significant bits) to fit into
878b3b42e8Sart * the appropriate field in the instruction word.
888b3b42e8Sart * - flags indicating whether
898b3b42e8Sart * * the relocation involves a symbol
908b3b42e8Sart * * the relocation is relative to the current position
918b3b42e8Sart * * the relocation is for a GOT entry
928b3b42e8Sart * * the relocation is relative to the load address
938b3b42e8Sart *
948b3b42e8Sart */
958b3b42e8Sart #define _RF_S 0x80000000 /* Resolve symbol */
968b3b42e8Sart #define _RF_A 0x40000000 /* Use addend */
978b3b42e8Sart #define _RF_P 0x20000000 /* Location relative */
988b3b42e8Sart #define _RF_G 0x10000000 /* GOT offset */
998b3b42e8Sart #define _RF_B 0x08000000 /* Load address relative */
1008b3b42e8Sart #define _RF_U 0x04000000 /* Unaligned */
1018b3b42e8Sart #define _RF_SZ(s) (((s) & 0xff) << 8) /* memory target size */
1028b3b42e8Sart #define _RF_RS(s) ((s) & 0xff) /* right shift */
1032668a398Sguenther static const int reloc_target_flags[] = {
1048b3b42e8Sart 0, /* NONE */
1058b3b42e8Sart _RF_S|_RF_A| _RF_SZ(8) | _RF_RS(0), /* RELOC_8 */
1068b3b42e8Sart _RF_S|_RF_A| _RF_SZ(16) | _RF_RS(0), /* RELOC_16 */
1078b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* RELOC_32 */
1088b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(8) | _RF_RS(0), /* DISP_8 */
1098b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(16) | _RF_RS(0), /* DISP_16 */
1108b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* DISP_32 */
1118b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_30 */
1128b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP_22 */
1138b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HI22 */
1148b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 22 */
1158b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 13 */
1168b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LO10 */
1178b3b42e8Sart _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT10 */
1188b3b42e8Sart _RF_G| _RF_SZ(32) | _RF_RS(0), /* GOT13 */
1198b3b42e8Sart _RF_G| _RF_SZ(32) | _RF_RS(10), /* GOT22 */
1208b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PC10 */
1218b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC22 */
1228b3b42e8Sart _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WPLT30 */
12367aa3856Sart _RF_S| _RF_SZ(32) | _RF_RS(0), /* COPY */
1248b3b42e8Sart _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* GLOB_DAT */
125d8f81dbeSart _RF_S| _RF_SZ(32) | _RF_RS(0), /* JMP_SLOT */
1268b3b42e8Sart _RF_A| _RF_B| _RF_SZ(64) | _RF_RS(0), /* RELATIVE */
1278b3b42e8Sart _RF_S|_RF_A| _RF_U| _RF_SZ(32) | _RF_RS(0), /* UA_32 */
1288b3b42e8Sart
1298b3b42e8Sart _RF_A| _RF_SZ(32) | _RF_RS(0), /* PLT32 */
1308b3b42e8Sart _RF_A| _RF_SZ(32) | _RF_RS(10), /* HIPLT22 */
1318b3b42e8Sart _RF_A| _RF_SZ(32) | _RF_RS(0), /* LOPLT10 */
1328b3b42e8Sart _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT32 */
1338b3b42e8Sart _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PCPLT22 */
1348b3b42e8Sart _RF_A|_RF_P| _RF_SZ(32) | _RF_RS(0), /* PCPLT10 */
1358b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 10 */
1368b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 11 */
1378b3b42e8Sart _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* 64 */
1388b3b42e8Sart _RF_S|_RF_A|/*extra*/ _RF_SZ(32) | _RF_RS(0), /* OLO10 */
1398b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(42), /* HH22 */
1408b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(32), /* HM10 */
1418b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* LM22 */
1428b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(42), /* PC_HH22 */
1438b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(32), /* PC_HM10 */
1448b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(10), /* PC_LM22 */
1458b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP16 */
1468b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(32) | _RF_RS(2), /* WDISP19 */
1478b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* GLOB_JMP */
1488b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 7 */
1498b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 5 */
1508b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* 6 */
1518b3b42e8Sart _RF_S|_RF_A|_RF_P| _RF_SZ(64) | _RF_RS(0), /* DISP64 */
1528b3b42e8Sart _RF_A| _RF_SZ(64) | _RF_RS(0), /* PLT64 */
1538b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(10), /* HIX22 */
1548b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* LOX10 */
1558b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(22), /* H44 */
1568b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(12), /* M44 */
1578b3b42e8Sart _RF_S|_RF_A| _RF_SZ(32) | _RF_RS(0), /* L44 */
1588b3b42e8Sart _RF_S|_RF_A| _RF_SZ(64) | _RF_RS(0), /* REGISTER */
1598b3b42e8Sart _RF_S|_RF_A| _RF_U| _RF_SZ(64) | _RF_RS(0), /* UA64 */
1608b3b42e8Sart _RF_S|_RF_A| _RF_U| _RF_SZ(16) | _RF_RS(0), /* UA16 */
1618b3b42e8Sart };
1628b3b42e8Sart
1638b3b42e8Sart #define RELOC_RESOLVE_SYMBOL(t) ((reloc_target_flags[t] & _RF_S) != 0)
1648b3b42e8Sart #define RELOC_PC_RELATIVE(t) ((reloc_target_flags[t] & _RF_P) != 0)
1658b3b42e8Sart #define RELOC_BASE_RELATIVE(t) ((reloc_target_flags[t] & _RF_B) != 0)
1668b3b42e8Sart #define RELOC_UNALIGNED(t) ((reloc_target_flags[t] & _RF_U) != 0)
1678b3b42e8Sart #define RELOC_USE_ADDEND(t) ((reloc_target_flags[t] & _RF_A) != 0)
1688b3b42e8Sart #define RELOC_TARGET_SIZE(t) ((reloc_target_flags[t] >> 8) & 0xff)
1698b3b42e8Sart #define RELOC_VALUE_RIGHTSHIFT(t) (reloc_target_flags[t] & 0xff)
1708b3b42e8Sart
1712668a398Sguenther static const long reloc_target_bitmask[] = {
1728b3b42e8Sart #define _BM(x) (~(-(1ULL << (x))))
1738b3b42e8Sart 0, /* NONE */
1748b3b42e8Sart _BM(8), _BM(16), _BM(32), /* RELOC_8, _16, _32 */
1758b3b42e8Sart _BM(8), _BM(16), _BM(32), /* DISP8, DISP16, DISP32 */
1768b3b42e8Sart _BM(30), _BM(22), /* WDISP30, WDISP22 */
1778b3b42e8Sart _BM(22), _BM(22), /* HI22, _22 */
1788b3b42e8Sart _BM(13), _BM(10), /* RELOC_13, _LO10 */
1798b3b42e8Sart _BM(10), _BM(13), _BM(22), /* GOT10, GOT13, GOT22 */
1808b3b42e8Sart _BM(10), _BM(22), /* _PC10, _PC22 */
1818b3b42e8Sart _BM(30), 0, /* _WPLT30, _COPY */
1826afccae8Sdrahn -1, _BM(32), -1, /* _GLOB_DAT, JMP_SLOT, _RELATIVE */
1838b3b42e8Sart _BM(32), _BM(32), /* _UA32, PLT32 */
1848b3b42e8Sart _BM(22), _BM(10), /* _HIPLT22, LOPLT10 */
1858b3b42e8Sart _BM(32), _BM(22), _BM(10), /* _PCPLT32, _PCPLT22, _PCPLT10 */
1868b3b42e8Sart _BM(10), _BM(11), -1, /* _10, _11, _64 */
1878b3b42e8Sart _BM(10), _BM(22), /* _OLO10, _HH22 */
1888b3b42e8Sart _BM(10), _BM(22), /* _HM10, _LM22 */
1898b3b42e8Sart _BM(22), _BM(10), _BM(22), /* _PC_HH22, _PC_HM10, _PC_LM22 */
1908b3b42e8Sart _BM(16), _BM(19), /* _WDISP16, _WDISP19 */
1918b3b42e8Sart -1, /* GLOB_JMP */
1928b3b42e8Sart _BM(7), _BM(5), _BM(6) /* _7, _5, _6 */
1938b3b42e8Sart -1, -1, /* DISP64, PLT64 */
1948b3b42e8Sart _BM(22), _BM(13), /* HIX22, LOX10 */
1958b3b42e8Sart _BM(22), _BM(10), _BM(13), /* H44, M44, L44 */
1968b3b42e8Sart -1, -1, _BM(16), /* REGISTER, UA64, UA16 */
1978b3b42e8Sart #undef _BM
1988b3b42e8Sart };
1998b3b42e8Sart #define RELOC_VALUE_BITMASK(t) (reloc_target_bitmask[t])
200b76408a9Sdrahn
2018465df4aSguenther int _dl_reloc_plt(Elf_Word *where1, Elf_Word *where2, Elf_Word *pltaddr,
2028465df4aSguenther Elf_Addr value);
2035b36bcefSderaadt void _dl_install_plt(Elf_Word *pltgot, Elf_Addr proc);
204d8f81dbeSart
205b76408a9Sdrahn int
_dl_md_reloc(elf_object_t * object,int rel,int relasz)206b76408a9Sdrahn _dl_md_reloc(elf_object_t *object, int rel, int relasz)
207b76408a9Sdrahn {
208b76408a9Sdrahn long i;
209b76408a9Sdrahn long numrela;
21088098a4dSguenther long relrel;
211e9cfe40cSmiod int fails = 0;
212ab9fc4fcSart Elf_Addr loff;
21388098a4dSguenther Elf_Addr prev_value = 0;
21488098a4dSguenther const Elf_Sym *prev_sym = NULL;
2158b3b42e8Sart Elf_RelA *relas;
216b76408a9Sdrahn
217ce11e090Skurt loff = object->obj_base;
218e3b0f1d9Sguenther numrela = object->Dyn.info[relasz] / sizeof(Elf_RelA);
21988098a4dSguenther relrel = rel == DT_RELA ? object->relacount : 0;
220e3b0f1d9Sguenther relas = (Elf_RelA *)(object->Dyn.info[rel]);
221b76408a9Sdrahn
222bb87ac8eSart if (relas == NULL)
223e3b0f1d9Sguenther return 0;
224b76408a9Sdrahn
2253b50b772Sguenther if (relrel > numrela)
2263b50b772Sguenther _dl_die("relacount > numrel: %ld > %ld", relrel, numrela);
22788098a4dSguenther
22888098a4dSguenther /* tight loop for leading RELATIVE relocs */
22988098a4dSguenther for (i = 0; i < relrel; i++, relas++) {
23088098a4dSguenther Elf_Addr *where;
23188098a4dSguenther
23288098a4dSguenther where = (Elf_Addr *)(relas->r_offset + loff);
23388098a4dSguenther *where = relas->r_addend + loff;
23488098a4dSguenther }
23588098a4dSguenther for (; i < numrela; i++, relas++) {
236143e5accSguenther Elf_Addr *where, value, mask;
237ab9fc4fcSart Elf_Word type;
238143e5accSguenther const Elf_Sym *sym;
239b76408a9Sdrahn const char *symn;
240b76408a9Sdrahn
2418b3b42e8Sart type = ELF_R_TYPE(relas->r_info);
2428b3b42e8Sart
2438465df4aSguenther if (type == R_TYPE(NONE) || type == R_TYPE(JMP_SLOT))
244d8f81dbeSart continue;
245d8f81dbeSart
246ab9fc4fcSart where = (Elf_Addr *)(relas->r_offset + loff);
247ab9fc4fcSart
248ab9fc4fcSart if (RELOC_USE_ADDEND(type))
249ab9fc4fcSart value = relas->r_addend;
250ab9fc4fcSart else
251ab9fc4fcSart value = 0;
252ab9fc4fcSart
253bcbffc12Sdrahn sym = NULL;
254bcbffc12Sdrahn symn = NULL;
2558b3b42e8Sart if (RELOC_RESOLVE_SYMBOL(type)) {
256b76408a9Sdrahn sym = object->dyn.symtab;
2578b3b42e8Sart sym += ELF_R_SYM(relas->r_info);
258b76408a9Sdrahn symn = object->dyn.strtab + sym->st_name;
259b76408a9Sdrahn
2607d04d893Sart if (sym->st_shndx != SHN_UNDEF &&
2617d04d893Sart ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
2627d04d893Sart value += loff;
26388098a4dSguenther } else if (sym == prev_sym) {
26488098a4dSguenther value += prev_value;
2657d04d893Sart } else {
266143e5accSguenther struct sym_res sr;
267143e5accSguenther
268143e5accSguenther sr = _dl_find_symbol(symn,
269143e5accSguenther SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_NOTPLT,
270143e5accSguenther sym, object);
271143e5accSguenther if (sr.sym == NULL) {
27267aa3856Sart resolve_failed:
27306462af4Sdrahn if (ELF_ST_BIND(sym->st_info) !=
27406462af4Sdrahn STB_WEAK)
275b76408a9Sdrahn fails++;
2768b3b42e8Sart continue;
2778b3b42e8Sart }
27888098a4dSguenther prev_sym = sym;
279143e5accSguenther prev_value = (Elf_Addr)(sr.obj->obj_base +
280143e5accSguenther sr.sym->st_value);
28188098a4dSguenther value += prev_value;
2827d04d893Sart }
2838b3b42e8Sart }
2848b3b42e8Sart
28567aa3856Sart if (type == R_TYPE(COPY)) {
28667aa3856Sart void *dstaddr = where;
28767aa3856Sart const void *srcaddr;
288143e5accSguenther const Elf_Sym *dstsym = sym;
289143e5accSguenther struct sym_res sr;
29067aa3856Sart
291143e5accSguenther sr = _dl_find_symbol(symn,
2920acae5e5Sdrahn SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT,
293143e5accSguenther dstsym, object);
294143e5accSguenther if (sr.sym == NULL)
29567aa3856Sart goto resolve_failed;
29667aa3856Sart
297143e5accSguenther srcaddr = (void *)(sr.obj->obj_base + sr.sym->st_value);
298143e5accSguenther _dl_bcopy(srcaddr, dstaddr, dstsym->st_size);
299d8f81dbeSart continue;
300d8f81dbeSart }
301d8f81dbeSart
30239b7d201Sderaadt if (RELOC_PC_RELATIVE(type))
3038b3b42e8Sart value -= (Elf_Addr)where;
30439b7d201Sderaadt if (RELOC_BASE_RELATIVE(type))
3058b3b42e8Sart value += loff;
3068b3b42e8Sart
307ab9fc4fcSart mask = RELOC_VALUE_BITMASK(type);
308ab9fc4fcSart value >>= RELOC_VALUE_RIGHTSHIFT(type);
309ab9fc4fcSart value &= mask;
310ab9fc4fcSart
3118b3b42e8Sart if (RELOC_UNALIGNED(type)) {
3128b3b42e8Sart /* Handle unaligned relocations. */
3138b3b42e8Sart Elf_Addr tmp = 0;
3148b3b42e8Sart char *ptr = (char *)where;
3158b3b42e8Sart int i, size = RELOC_TARGET_SIZE(type)/8;
3168b3b42e8Sart
3178b3b42e8Sart /* Read it in one byte at a time. */
3188b3b42e8Sart for (i=0; i<size; i++)
3198b3b42e8Sart tmp = (tmp << 8) | ptr[i];
3208b3b42e8Sart
3218b3b42e8Sart tmp &= ~mask;
3228b3b42e8Sart tmp |= value;
3238b3b42e8Sart
3248b3b42e8Sart /* Write it back out. */
3258b3b42e8Sart for (i=0; i<size; i++)
3268b3b42e8Sart ptr[i] = ((tmp >> (8*i)) & 0xff);
3278b3b42e8Sart } else if (RELOC_TARGET_SIZE(type) > 32) {
3288b3b42e8Sart *where &= ~mask;
3298b3b42e8Sart *where |= value;
3308b3b42e8Sart } else {
3318b3b42e8Sart Elf32_Addr *where32 = (Elf32_Addr *)where;
3328b3b42e8Sart
3338b3b42e8Sart *where32 &= ~mask;
3348b3b42e8Sart *where32 |= value;
3358b3b42e8Sart }
336b76408a9Sdrahn }
337b76408a9Sdrahn
338e3b0f1d9Sguenther return fails;
339b76408a9Sdrahn }
340b76408a9Sdrahn
341b76408a9Sdrahn /*
342d8f81dbeSart * Instruction templates:
343d8f81dbeSart */
3445d8b74d1Sguenther
3455d8b74d1Sguenther #define BAA 0x30680000 /* ba,a %xcc, 0 */
346d8f81dbeSart #define SETHI 0x03000000 /* sethi %hi(0), %g1 */
3478465df4aSguenther #define JMP 0x81c06000 /* jmpl %g1+%lo(0), %g0 <-- simm13 */
348d8f81dbeSart #define NOP 0x01000000 /* sethi %hi(0), %g0 */
3495bc0636bSdrahn #define OR 0x82106000 /* or %g1, 0, %g1 */
3505bc0636bSdrahn #define ORG5 0x8a116000 /* or %g5, 0, %g5 */
3515bc0636bSdrahn #define XOR 0x82186000 /* xor %g1, 0, %g1 */
35206d820c1Sguenther #define MOV71 0x8210000f /* or %o7, 0, %g1 */
35306d820c1Sguenther #define MOV17 0x9e100001 /* or %g1, 0, %o7 */
3548465df4aSguenther #define CALL 0x40000000 /* call 0 <-- disp30 */
3555bc0636bSdrahn #define SLLX 0x83287000 /* sllx %g1, 0, %g1 */
3565bc0636bSdrahn #define SLLXG5 0x8b297000 /* sllx %g5, 0, %g5 */
3575bc0636bSdrahn #define SRAX 0x83387000 /* srax %g1, 0, %g1 */
358d8f81dbeSart #define SETHIG5 0x0b000000 /* sethi %hi(0), %g5 */
3595bc0636bSdrahn #define ORG15 0x82804005 /* or %g1, %g5, %g1 */
360d8f81dbeSart
361d8f81dbeSart
362d8f81dbeSart /* %hi(v) with variable shift */
363d8f81dbeSart #define HIVAL(v, s) (((v) >> (s)) & 0x003fffff)
364d8f81dbeSart #define LOVAL(v) ((v) & 0x000003ff)
365d8f81dbeSart
3668465df4aSguenther int
_dl_reloc_plt(Elf_Word * where1,Elf_Word * where2,Elf_Word * pltaddr,Elf_Addr value)3678465df4aSguenther _dl_reloc_plt(Elf_Word *where1, Elf_Word *where2, Elf_Word *pltaddr,
3688465df4aSguenther Elf_Addr value)
369d8f81dbeSart {
370d8f81dbeSart Elf_Addr offset;
371d8f81dbeSart
372d8f81dbeSart /*
373d8f81dbeSart * At the PLT entry pointed at by `where', we now construct
374d8f81dbeSart * a direct transfer to the now fully resolved function
375d8f81dbeSart * address.
376d8f81dbeSart *
377d8f81dbeSart * A PLT entry is supposed to start by looking like this:
378d8f81dbeSart *
379d8f81dbeSart * sethi %hi(. - .PLT0), %g1
3808465df4aSguenther * ba,a,pt %xcc, .PLT1
381d8f81dbeSart * nop
382d8f81dbeSart * nop
383d8f81dbeSart * nop
384d8f81dbeSart * nop
385d8f81dbeSart * nop
386d8f81dbeSart * nop
387d8f81dbeSart *
3888465df4aSguenther * When we replace these entries we either (a) only replace
3898465df4aSguenther * the second word (the ba,a,pt), or (b) replace multiple
3908465df4aSguenther * words: one or more nops, then finally the ba,a,pt. By
3918465df4aSguenther * replacing the ba,a,pt last, we guarantee that the PLT can
3928465df4aSguenther * be used by other threads even while it's being updated.
3938465df4aSguenther * This is made slightly more complicated by kbind, for which
3948465df4aSguenther * we need to pass them to the kernel in the order they get
3958465df4aSguenther * written. To that end, we store the word to overwrite the
3968465df4aSguenther * ba,a,pt at *where1, and the words to overwrite the nops at
3978465df4aSguenther * where2[0], where2[1], ...
398d8f81dbeSart *
399d8f81dbeSart * We now need to find out how far we need to jump. We
400d8f81dbeSart * have a choice of several different relocation techniques
401d8f81dbeSart * which are increasingly expensive.
402d8f81dbeSart */
403d8f81dbeSart
4048465df4aSguenther offset = value - ((Elf_Addr)pltaddr);
4058465df4aSguenther if ((int64_t)(offset-4) <= (1L<<20) &&
40606d820c1Sguenther (int64_t)(offset-4) >= -(1L<<20)) {
407d8f81dbeSart /*
408d8f81dbeSart * We're within 1MB -- we can use a direct branch insn.
409d8f81dbeSart *
410d8f81dbeSart * We can generate this pattern:
411d8f81dbeSart *
412d8f81dbeSart * sethi %hi(. - .PLT0), %g1
4138465df4aSguenther * ba,a,pt %xcc, addr
414d8f81dbeSart * nop
415d8f81dbeSart * nop
416d8f81dbeSart * nop
417d8f81dbeSart * nop
418d8f81dbeSart * nop
419d8f81dbeSart * nop
420d8f81dbeSart *
421d8f81dbeSart */
4228465df4aSguenther *where1 = BAA | (((offset-4) >> 2) &0x7ffff);
423e3b0f1d9Sguenther return 0;
4245bc0636bSdrahn } else if (value < (1UL<<32)) {
425d8f81dbeSart /*
4265bc0636bSdrahn * We're within 32-bits of address zero.
427d8f81dbeSart *
428d8f81dbeSart * The resulting code in the jump slot is:
429d8f81dbeSart *
430d8f81dbeSart * sethi %hi(. - .PLT0), %g1
431d8f81dbeSart * sethi %hi(addr), %g1
432d8f81dbeSart * jmp %g1+%lo(addr)
433d8f81dbeSart * nop
434d8f81dbeSart * nop
435d8f81dbeSart * nop
436d8f81dbeSart * nop
437d8f81dbeSart * nop
438d8f81dbeSart *
439d8f81dbeSart */
4408465df4aSguenther *where1 = SETHI | HIVAL(value, 10);
4418465df4aSguenther where2[0] = JMP | LOVAL(value);
442e3b0f1d9Sguenther return 1;
4435bc0636bSdrahn } else if (value > -(1UL<<32)) {
444d8f81dbeSart /*
4455bc0636bSdrahn * We're within 32-bits of address -1.
446d8f81dbeSart *
447d8f81dbeSart * The resulting code in the jump slot is:
448d8f81dbeSart *
449d8f81dbeSart * sethi %hi(. - .PLT0), %g1
45006d820c1Sguenther * sethi %hix(~addr), %g1
45106d820c1Sguenther * xor %g1, %lox(~addr), %g1
452d8f81dbeSart * jmp %g1
453d8f81dbeSart * nop
454d8f81dbeSart * nop
455d8f81dbeSart * nop
456d8f81dbeSart * nop
457d8f81dbeSart *
458d8f81dbeSart */
4598465df4aSguenther *where1 = SETHI | HIVAL(~value, 10);
4608465df4aSguenther where2[0] = XOR | ((~value) & 0x00001fff);
4618465df4aSguenther where2[1] = JMP;
462e3b0f1d9Sguenther return 2;
46306d820c1Sguenther } else if ((int64_t)(offset-8) <= (1L<<31) &&
46406d820c1Sguenther (int64_t)(offset-8) >= -((1L<<31) - 4)) {
465d8f81dbeSart /*
4665bc0636bSdrahn * We're within 32-bits -- we can use a direct call insn
467d8f81dbeSart *
468d8f81dbeSart * The resulting code in the jump slot is:
469d8f81dbeSart *
470d8f81dbeSart * sethi %hi(. - .PLT0), %g1
471d8f81dbeSart * mov %o7, %g1
472d8f81dbeSart * call (.+offset)
473d8f81dbeSart * mov %g1, %o7
474d8f81dbeSart * nop
475d8f81dbeSart * nop
476d8f81dbeSart * nop
477d8f81dbeSart * nop
478d8f81dbeSart *
479d8f81dbeSart */
4808465df4aSguenther *where1 = MOV71;
4818465df4aSguenther where2[0] = CALL | (((offset-8) >> 2) & 0x3fffffff);
4828465df4aSguenther where2[1] = MOV17;
483e3b0f1d9Sguenther return 2;
4845bc0636bSdrahn } else if (value < (1L<<42)) {
485d8f81dbeSart /*
4865bc0636bSdrahn * Target 42bits or smaller.
487d8f81dbeSart *
488d8f81dbeSart * The resulting code in the jump slot is:
489d8f81dbeSart *
490d8f81dbeSart * sethi %hi(. - .PLT0), %g1
4915bc0636bSdrahn * sethi %hi(addr >> 20), %g1
4925bc0636bSdrahn * or %g1, %lo(addr >> 10), %g1
4935bc0636bSdrahn * sllx %g1, 10, %g1
4945bc0636bSdrahn * jmp %g1+%lo(addr)
495d8f81dbeSart * nop
496d8f81dbeSart * nop
497d8f81dbeSart * nop
498d8f81dbeSart *
4995bc0636bSdrahn * this can handle addresses 0 - 0x3fffffffffc
500d8f81dbeSart */
5018465df4aSguenther *where1 = SETHI | HIVAL(value, 20);
5028465df4aSguenther where2[0] = OR | LOVAL(value >> 10);
5038465df4aSguenther where2[1] = SLLX | 10;
5048465df4aSguenther where2[2] = JMP | LOVAL(value);
505e3b0f1d9Sguenther return 3;
5065bc0636bSdrahn } else if (value > -(1UL<<41)) {
507d8f81dbeSart /*
5085bc0636bSdrahn * Large target >= 0xfffffe0000000000UL
509d8f81dbeSart *
510d8f81dbeSart * The resulting code in the jump slot is:
511d8f81dbeSart *
512d8f81dbeSart * sethi %hi(. - .PLT0), %g1
5135bc0636bSdrahn * sethi %hi(addr >> 20), %g1
5145bc0636bSdrahn * or %g1, %lo(addr >> 10), %g1
5155bc0636bSdrahn * sllx %g1, 32, %g1
5165bc0636bSdrahn * srax %g1, 22, %g1
5175bc0636bSdrahn * jmp %g1+%lo(addr)
518d8f81dbeSart * nop
519d8f81dbeSart * nop
520d8f81dbeSart * nop
521d8f81dbeSart *
522d8f81dbeSart */
5238465df4aSguenther *where1 = SETHI | HIVAL(value, 20);
5248465df4aSguenther where2[0] = OR | LOVAL(value >> 10);
5258465df4aSguenther where2[1] = SLLX | 32;
5268465df4aSguenther where2[2] = SRAX | 22;
5278465df4aSguenther where2[3] = JMP | LOVAL(value);
528e3b0f1d9Sguenther return 4;
529d8f81dbeSart } else {
530d8f81dbeSart /*
531d8f81dbeSart * We need to load all 64-bits
532d8f81dbeSart *
533d8f81dbeSart * The resulting code in the jump slot is:
534d8f81dbeSart *
535d8f81dbeSart * sethi %hi(. - .PLT0), %g1
5365bc0636bSdrahn * sethi %hi(addr >> 42), %g5
5375bc0636bSdrahn * sethi %hi(addr >> 10), %g1
5385bc0636bSdrahn * or %g1, %lo(addr >> 32), %g5
5395bc0636bSdrahn * sllx %g5, 32, %g5
540d8f81dbeSart * or %g1, %g5, %g1
541d8f81dbeSart * jmp %g1+%lo(addr)
542d8f81dbeSart * nop
543d8f81dbeSart *
544d8f81dbeSart */
5458465df4aSguenther *where1 = SETHIG5 | HIVAL(value, 42);
5468465df4aSguenther where2[0] = SETHI | HIVAL(value, 10);
5478465df4aSguenther where2[1] = ORG5 | LOVAL(value >> 32);
5488465df4aSguenther where2[2] = SLLXG5 | 32;
5498465df4aSguenther where2[3] = ORG15;
5508465df4aSguenther where2[4] = JMP | LOVAL(value);
551e3b0f1d9Sguenther return 5;
552d8f81dbeSart }
553d8f81dbeSart }
554d8f81dbeSart
555d8f81dbeSart /*
556b76408a9Sdrahn * Resolve a symbol at run-time.
557b76408a9Sdrahn */
5585b36bcefSderaadt Elf_Addr
_dl_bind(elf_object_t * object,int index)559ae680c51Sdrahn _dl_bind(elf_object_t *object, int index)
560b76408a9Sdrahn {
561b76408a9Sdrahn Elf_RelA *rela;
562710a39d1Sart Elf_Word *addr;
563143e5accSguenther Elf_Addr newvalue;
564143e5accSguenther struct sym_res sr;
565143e5accSguenther const Elf_Sym *sym;
566b76408a9Sdrahn const char *symn;
5678465df4aSguenther int64_t cookie = pcookie;
5688465df4aSguenther struct {
5698465df4aSguenther struct __kbind param[2];
5708465df4aSguenther Elf_Word newval[6];
5718465df4aSguenther } buf;
5728465df4aSguenther struct __kbind *param;
5738465df4aSguenther size_t psize;
5748465df4aSguenther int i;
575b76408a9Sdrahn
576ae680c51Sdrahn rela = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]);
577ae680c51Sdrahn if (ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)) {
578ae680c51Sdrahn /*
579ae680c51Sdrahn * XXXX
580ae680c51Sdrahn *
581ae680c51Sdrahn * The first four PLT entries are reserved. There
582ae680c51Sdrahn * is some disagreement whether they should have
583ae680c51Sdrahn * associated relocation entries. Both the SPARC
584ae680c51Sdrahn * 32-bit and 64-bit ELF specifications say that
585ae680c51Sdrahn * they should have relocation entries, but the
586ae680c51Sdrahn * 32-bit SPARC binutils do not generate them,
587ae680c51Sdrahn * and now the 64-bit SPARC binutils have stopped
588ae680c51Sdrahn * generating them too.
589ae680c51Sdrahn *
590ae680c51Sdrahn * So, to provide binary compatibility, we will
591ae680c51Sdrahn * check the first entry, if it is reserved it
592ae680c51Sdrahn * should not be of the type JMP_SLOT. If it
593ae680c51Sdrahn * is JMP_SLOT, then the 4 reserved entries were
594ae680c51Sdrahn * not generated and our index is 4 entries too far.
595ae680c51Sdrahn */
5968465df4aSguenther rela += index - 4;
5978465df4aSguenther } else
598ae680c51Sdrahn rela += index;
599b76408a9Sdrahn
600b76408a9Sdrahn sym = object->dyn.symtab;
601e3b0f1d9Sguenther sym += ELF_R_SYM(rela->r_info);
602b76408a9Sdrahn symn = object->dyn.strtab + sym->st_name;
603b76408a9Sdrahn
604143e5accSguenther sr = _dl_find_symbol(symn, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT,
605143e5accSguenther sym, object);
606143e5accSguenther if (sr.sym == NULL)
6073b50b772Sguenther _dl_die("lazy binding failed!");
608b76408a9Sdrahn
609143e5accSguenther newvalue = sr.obj->obj_base + sr.sym->st_value;
610ae398163Smiod
611143e5accSguenther if (__predict_false(sr.obj->traced) && _dl_trace_plt(sr.obj, symn))
612e3b0f1d9Sguenther return newvalue;
6138465df4aSguenther
6148465df4aSguenther /*
6158465df4aSguenther * While some relocations just need to write one word and
6168465df4aSguenther * can do that with kbind() with just one block, many
6178465df4aSguenther * require two blocks to be written: all but first word,
6188465df4aSguenther * then the first word. So, if we want to write 5 words
6198465df4aSguenther * in total, then the layout of the buffer we pass to
6208465df4aSguenther * kbind() needs to be one of these:
6218465df4aSguenther * +------------+
6228465df4aSguenther * | kbind.addr |
6238465df4aSguenther * | """ |
6248465df4aSguenther * | kbind.size |
6258465df4aSguenther * | """ | +------------+
6268465df4aSguenther * | kbind.addr | | kbind.addr |
6278465df4aSguenther * | """ | | """ |
6288465df4aSguenther * | kbind.size | | kbind.size |
6298465df4aSguenther * | """ | | """ |
6308465df4aSguenther * | word 2 | | word |
6318465df4aSguenther * | word 3 | +------------+
6328465df4aSguenther * | word 4 |
6338465df4aSguenther * | word 5 |
6348465df4aSguenther * | word 1 |
6358465df4aSguenther * +------------+
6368465df4aSguenther *
6378465df4aSguenther * We first handle the special case of relocations with a
6388465df4aSguenther * non-zero r_addend, which have one block to update whose
6398465df4aSguenther * address is the relocation address itself. This is only
6408465df4aSguenther * used for PLT entries after the 2^15th, i.e., truly monstrous
6418465df4aSguenther * programs, thus the __predict_false().
6428465df4aSguenther */
6438465df4aSguenther addr = (Elf_Word *)(object->obj_base + rela->r_offset);
6448465df4aSguenther _dl_memset(&buf, 0, sizeof(buf));
6458465df4aSguenther if (__predict_false(rela->r_addend)) {
6468465df4aSguenther /*
6478465df4aSguenther * This entry is >32768. The relocation points to a
6488465df4aSguenther * PC-relative pointer to the _dl_bind_start_0 stub at
6498465df4aSguenther * the top of the PLT section. Update it to point to
6508465df4aSguenther * the target function.
6518465df4aSguenther */
6528465df4aSguenther buf.newval[0] = rela->r_addend + newvalue
6538465df4aSguenther - object->Dyn.info[DT_PLTGOT];
6548465df4aSguenther buf.param[1].kb_addr = addr;
6558465df4aSguenther buf.param[1].kb_size = sizeof(buf.newval[0]);
6568465df4aSguenther param = &buf.param[1];
6578465df4aSguenther psize = sizeof(struct __kbind) + sizeof(buf.newval[0]);
6588465df4aSguenther } else {
6598465df4aSguenther Elf_Word first;
6608465df4aSguenther
6618465df4aSguenther /*
6628465df4aSguenther * For the other relocations, the word at the relocation
6638465df4aSguenther * address will be left unchanged. Assume _dl_reloc_plt()
6648465df4aSguenther * will tell us to update multiple words, so save the first
6658465df4aSguenther * word to the side.
6668465df4aSguenther */
6678465df4aSguenther i = _dl_reloc_plt(&first, &buf.newval[0], addr, newvalue);
6688465df4aSguenther
6698465df4aSguenther /*
6708465df4aSguenther * _dl_reloc_plt() returns the number of words that must be
6718465df4aSguenther * written after the first word in location, but before it
6728465df4aSguenther * in time. If it returns zero, then only a single block
6738465df4aSguenther * with one word is needed, so we just put it in place per
6748465df4aSguenther * the right-hand diagram and just use param[1] and newval[0]
6758465df4aSguenther */
6768465df4aSguenther if (i == 0) {
6778465df4aSguenther /* fill in the __kbind structure */
6788465df4aSguenther buf.param[1].kb_addr = &addr[1];
6798465df4aSguenther buf.param[1].kb_size = sizeof(Elf_Word);
6808465df4aSguenther buf.newval[0] = first;
6818465df4aSguenther param = &buf.param[1];
6828465df4aSguenther psize = sizeof(struct __kbind) + sizeof(buf.newval[0]);
6838465df4aSguenther } else {
6848465df4aSguenther /*
6858465df4aSguenther * Two blocks are necessary. Save the first word
6868465df4aSguenther * after the other words.
6878465df4aSguenther */
6888465df4aSguenther buf.param[0].kb_addr = &addr[2];
6898465df4aSguenther buf.param[0].kb_size = i * sizeof(Elf_Word);
6908465df4aSguenther buf.param[1].kb_addr = &addr[1];
6918465df4aSguenther buf.param[1].kb_size = sizeof(Elf_Word);
6928465df4aSguenther buf.newval[i] = first;
6938465df4aSguenther param = &buf.param[0];
6948465df4aSguenther psize = 2 * sizeof(struct __kbind) +
6958465df4aSguenther (i + 1) * sizeof(buf.newval[0]);
6968465df4aSguenther }
69791d8decdSdrahn }
69891d8decdSdrahn
6998465df4aSguenther /* directly code the syscall, so that it's actually inline here */
7008465df4aSguenther {
7018465df4aSguenther register long syscall_num __asm("g1") = SYS_kbind;
7028465df4aSguenther register void *arg1 __asm("o0") = param;
7038465df4aSguenther register long arg2 __asm("o1") = psize;
7048465df4aSguenther register long arg3 __asm("o2") = cookie;
705710a39d1Sart
7068465df4aSguenther __asm volatile("t %2" : "+r" (arg1), "+r" (arg2)
7078465df4aSguenther : "i" (ST_SYSCALL), "r" (syscall_num), "r" (arg3)
7088465df4aSguenther : "cc", "memory");
70991d8decdSdrahn }
71091d8decdSdrahn
711e3b0f1d9Sguenther return newvalue;
712b76408a9Sdrahn }
713b76408a9Sdrahn
714b76408a9Sdrahn /*
715710a39d1Sart * Install rtld function call into this PLT slot.
716710a39d1Sart */
717710a39d1Sart #define SAVE 0x9de3bf50
718710a39d1Sart #define SETHI_l0 0x21000000
719710a39d1Sart #define SETHI_l1 0x23000000
720710a39d1Sart #define OR_l0_l0 0xa0142000
721710a39d1Sart #define SLLX_l0_32_l0 0xa12c3020
722710a39d1Sart #define OR_l0_l1_l0 0xa0140011
723710a39d1Sart #define JMPL_l0_o1 0x93c42000
724710a39d1Sart #define MOV_g1_o0 0x90100001
725710a39d1Sart
726710a39d1Sart void
_dl_install_plt(Elf_Word * pltgot,Elf_Addr proc)727710a39d1Sart _dl_install_plt(Elf_Word *pltgot, Elf_Addr proc)
728710a39d1Sart {
729710a39d1Sart pltgot[0] = SAVE;
730710a39d1Sart pltgot[1] = SETHI_l0 | HIVAL(proc, 42);
731710a39d1Sart pltgot[2] = SETHI_l1 | HIVAL(proc, 10);
732710a39d1Sart pltgot[3] = OR_l0_l0 | LOVAL((proc) >> 32);
733710a39d1Sart pltgot[4] = SLLX_l0_32_l0;
734710a39d1Sart pltgot[5] = OR_l0_l1_l0;
735710a39d1Sart pltgot[6] = JMPL_l0_o1 | LOVAL(proc);
736710a39d1Sart pltgot[7] = MOV_g1_o0;
737710a39d1Sart }
738710a39d1Sart
739710a39d1Sart void _dl_bind_start_0(long, long);
740710a39d1Sart void _dl_bind_start_1(long, long);
741710a39d1Sart
7428465df4aSguenther static int
_dl_md_reloc_all_plt(elf_object_t * object)7438465df4aSguenther _dl_md_reloc_all_plt(elf_object_t *object)
7448465df4aSguenther {
7458465df4aSguenther long i;
7468465df4aSguenther long numrela;
7478465df4aSguenther int fails = 0;
7488465df4aSguenther Elf_Addr loff;
7498465df4aSguenther Elf_RelA *relas;
7508465df4aSguenther
7518465df4aSguenther loff = object->obj_base;
752e3b0f1d9Sguenther numrela = object->Dyn.info[DT_PLTRELSZ] / sizeof(Elf_RelA);
753e3b0f1d9Sguenther relas = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]);
7548465df4aSguenther
7558465df4aSguenther if (relas == NULL)
756e3b0f1d9Sguenther return 0;
7578465df4aSguenther
7588465df4aSguenther for (i = 0; i < numrela; i++, relas++) {
7598465df4aSguenther Elf_Addr value;
7608465df4aSguenther Elf_Word *where;
761143e5accSguenther struct sym_res sr;
762143e5accSguenther const Elf_Sym *sym;
7638465df4aSguenther
7648465df4aSguenther if (ELF_R_TYPE(relas->r_info) != R_TYPE(JMP_SLOT))
7658465df4aSguenther continue;
7668465df4aSguenther
7678465df4aSguenther sym = object->dyn.symtab + ELF_R_SYM(relas->r_info);
7688465df4aSguenther
769143e5accSguenther sr = _dl_find_symbol(object->dyn.strtab + sym->st_name,
770143e5accSguenther SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT,
771143e5accSguenther sym, object);
772143e5accSguenther if (sr.sym == NULL) {
7738465df4aSguenther if (ELF_ST_BIND(sym->st_info) != STB_WEAK)
7748465df4aSguenther fails++;
7758465df4aSguenther continue;
7768465df4aSguenther }
7778465df4aSguenther
7788465df4aSguenther where = (Elf_Word *)(relas->r_offset + loff);
779143e5accSguenther value = sr.obj->obj_base + sr.sym->st_value;
7808465df4aSguenther
7818465df4aSguenther if (__predict_false(relas->r_addend)) {
7828465df4aSguenther /*
7838465df4aSguenther * This entry is >32768. The relocation points to a
7848465df4aSguenther * PC-relative pointer to the _dl_bind_start_0 stub at
7858465df4aSguenther * the top of the PLT section. Update it to point to
7868465df4aSguenther * the target function.
7878465df4aSguenther */
7888465df4aSguenther *(Elf_Addr *)where = relas->r_addend + value -
7898465df4aSguenther object->Dyn.info[DT_PLTGOT];
7908465df4aSguenther } else
7918465df4aSguenther _dl_reloc_plt(&where[1], &where[2], where, value);
7928465df4aSguenther }
7938465df4aSguenther
794e3b0f1d9Sguenther return fails;
7958465df4aSguenther }
7968465df4aSguenther
797710a39d1Sart /*
798b76408a9Sdrahn * Relocate the Global Offset Table (GOT).
799b76408a9Sdrahn */
800e9cfe40cSmiod int
_dl_md_reloc_got(elf_object_t * object,int lazy)801b76408a9Sdrahn _dl_md_reloc_got(elf_object_t *object, int lazy)
802b76408a9Sdrahn {
803e9cfe40cSmiod int fails = 0;
804710a39d1Sart Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
805710a39d1Sart Elf_Word *entry = (Elf_Word *)pltgot;
806710a39d1Sart
807710a39d1Sart if (object->Dyn.info[DT_PLTREL] != DT_RELA)
808e3b0f1d9Sguenther return 0;
809710a39d1Sart
810710a39d1Sart if (!lazy) {
8118465df4aSguenther fails = _dl_md_reloc_all_plt(object);
812ab7fd747Sdrahn } else {
813710a39d1Sart _dl_install_plt(&entry[0], (Elf_Addr)&_dl_bind_start_0);
814710a39d1Sart _dl_install_plt(&entry[8], (Elf_Addr)&_dl_bind_start_1);
815710a39d1Sart
816710a39d1Sart pltgot[8] = (Elf_Addr)object;
817ab7fd747Sdrahn }
818e23a26ffSguenther
819e3b0f1d9Sguenther return fails;
820b76408a9Sdrahn }
821