xref: /openbsd/libexec/ld.so/amd64/rtld_machine.c (revision 388165f5)
1*388165f5Sgnezdo /*	$OpenBSD: rtld_machine.c,v 1.42 2023/01/29 20:30:21 gnezdo Exp $ */
2672d3582Sdrahn 
3672d3582Sdrahn /*
4672d3582Sdrahn  * Copyright (c) 2002,2004 Dale Rahn
5672d3582Sdrahn  * Copyright (c) 2001 Niklas Hallqvist
6672d3582Sdrahn  * Copyright (c) 2001 Artur Grabowski
7672d3582Sdrahn  *
8672d3582Sdrahn  * Redistribution and use in source and binary forms, with or without
9672d3582Sdrahn  * modification, are permitted provided that the following conditions
10672d3582Sdrahn  * are met:
11672d3582Sdrahn  * 1. Redistributions of source code must retain the above copyright
12672d3582Sdrahn  *    notice, this list of conditions and the following disclaimer.
13672d3582Sdrahn  * 2. Redistributions in binary form must reproduce the above copyright
14672d3582Sdrahn  *    notice, this list of conditions and the following disclaimer in the
15672d3582Sdrahn  *    documentation and/or other materials provided with the distribution.
16672d3582Sdrahn  *
17672d3582Sdrahn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18672d3582Sdrahn  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19672d3582Sdrahn  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20672d3582Sdrahn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21672d3582Sdrahn  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22672d3582Sdrahn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23672d3582Sdrahn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24672d3582Sdrahn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25672d3582Sdrahn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26672d3582Sdrahn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27672d3582Sdrahn  * SUCH DAMAGE.
28672d3582Sdrahn  */
29672d3582Sdrahn /*-
30672d3582Sdrahn  * Copyright (c) 2000 Eduardo Horvath.
31672d3582Sdrahn  * Copyright (c) 1999 The NetBSD Foundation, Inc.
32672d3582Sdrahn  * All rights reserved.
33672d3582Sdrahn  *
34672d3582Sdrahn  * This code is derived from software contributed to The NetBSD Foundation
35672d3582Sdrahn  * by Paul Kranenburg.
36672d3582Sdrahn  *
37672d3582Sdrahn  * Redistribution and use in source and binary forms, with or without
38672d3582Sdrahn  * modification, are permitted provided that the following conditions
39672d3582Sdrahn  * are met:
40672d3582Sdrahn  * 1. Redistributions of source code must retain the above copyright
41672d3582Sdrahn  *    notice, this list of conditions and the following disclaimer.
42672d3582Sdrahn  * 2. Redistributions in binary form must reproduce the above copyright
43672d3582Sdrahn  *    notice, this list of conditions and the following disclaimer in the
44672d3582Sdrahn  *    documentation and/or other materials provided with the distribution.
45672d3582Sdrahn  * 3. All advertising materials mentioning features or use of this software
46672d3582Sdrahn  *    must display the following acknowledgement:
47672d3582Sdrahn  *	This product includes software developed by the NetBSD
48672d3582Sdrahn  *	Foundation, Inc. and its contributors.
49672d3582Sdrahn  * 4. Neither the name of The NetBSD Foundation nor the names of its
50672d3582Sdrahn  *    contributors may be used to endorse or promote products derived
51672d3582Sdrahn  *    from this software without specific prior written permission.
52672d3582Sdrahn  *
53672d3582Sdrahn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54672d3582Sdrahn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55672d3582Sdrahn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56672d3582Sdrahn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57672d3582Sdrahn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58672d3582Sdrahn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59672d3582Sdrahn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60672d3582Sdrahn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61672d3582Sdrahn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62672d3582Sdrahn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63672d3582Sdrahn  * POSSIBILITY OF SUCH DAMAGE.
64672d3582Sdrahn  */
65672d3582Sdrahn 
66672d3582Sdrahn #define _DYN_LOADER
67672d3582Sdrahn 
68672d3582Sdrahn #include <sys/types.h>
69b722ba42Sguenther #include <sys/exec_elf.h>
708465df4aSguenther #include <sys/syscall.h>
718465df4aSguenther #include <sys/unistd.h>
72672d3582Sdrahn 
73b722ba42Sguenther #include <machine/reloc.h>
74672d3582Sdrahn 
75b722ba42Sguenther #include "util.h"
76672d3582Sdrahn #include "resolve.h"
77672d3582Sdrahn 
788465df4aSguenther int64_t pcookie __attribute__((section(".openbsd.randomdata"))) __dso_hidden;
798465df4aSguenther 
80c8754c30Sguenther /*
81c8754c30Sguenther  * The following table holds for each relocation type:
82c8754c30Sguenther  *	- the width in bits of the memory location the relocation
83c8754c30Sguenther  *	  applies to
84c8754c30Sguenther  *	- the number of bits the relocation value must be shifted to the
85c8754c30Sguenther  *	  right (i.e. discard least significant bits) to fit into
86c8754c30Sguenther  *	  the appropriate field in the instruction word.
87c8754c30Sguenther  *	- flags indicating whether
88c8754c30Sguenther  *		* the relocation involves a symbol
89c8754c30Sguenther  *		* the relocation is relative to the current position
90c8754c30Sguenther  *		* the relocation is for a GOT entry
91c8754c30Sguenther  *		* the relocation is relative to the load address
92c8754c30Sguenther  *
93c8754c30Sguenther  */
94c8754c30Sguenther #define _RF_S		0x80000000		/* Resolve symbol */
95c8754c30Sguenther #define _RF_A		0x40000000		/* Use addend */
96c8754c30Sguenther #define _RF_P		0x20000000		/* Location relative */
97c8754c30Sguenther #define _RF_G		0x10000000		/* GOT offset */
98c8754c30Sguenther #define _RF_B		0x08000000		/* Load address relative */
99c8754c30Sguenther #define _RF_E		0x02000000		/* ERROR */
100c8754c30Sguenther #define _RF_SZ(s)	(((s) & 0xff) << 8)	/* memory target size */
101c8754c30Sguenther #define _RF_RS(s)	((s) & 0xff)		/* right shift */
102c8754c30Sguenther static const int reloc_target_flags[] = {
103c8754c30Sguenther 	0,							/*  0 NONE */
104c8754c30Sguenther 	_RF_S|_RF_A|		_RF_SZ(64) | _RF_RS(0),		/*  1 _64*/
105c8754c30Sguenther 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/*  2 PC32 */
106c8754c30Sguenther 	_RF_G|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/*  3 GOT32 */
107c8754c30Sguenther 	_RF_E|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/*  4 PLT32 */
108c8754c30Sguenther 	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/*  5 COPY */
109c8754c30Sguenther 	_RF_S|			_RF_SZ(64) | _RF_RS(0),		/*  6 GLOB_DAT*/
110c8754c30Sguenther 	_RF_S|			_RF_SZ(64) | _RF_RS(0),		/* 7 JUMP_SLOT*/
111c8754c30Sguenther 	      _RF_A|	_RF_B|	_RF_SZ(64) | _RF_RS(0),		/*  8 RELATIVE*/
112c8754c30Sguenther 	_RF_E,							/*  9 GOTPCREL*/
113c8754c30Sguenther 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 10 32 */
114c8754c30Sguenther 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 11 32S */
115c8754c30Sguenther 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* 12 16 */
116c8754c30Sguenther 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* 13 PC16 */
117c8754c30Sguenther 	_RF_S|_RF_A|		_RF_SZ(8) | _RF_RS(0),		/* 14 8 */
118c8754c30Sguenther 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8) | _RF_RS(0),		/* 15 PC8 */
119c8754c30Sguenther 	_RF_E,							/* 16 DTPMOD64*/
120c8754c30Sguenther 	_RF_E,							/* 17 DTPOFF64*/
121c8754c30Sguenther 	_RF_E,							/* 18 TPOFF64 */
122c8754c30Sguenther 	_RF_E,							/* 19 TLSGD */
123c8754c30Sguenther 	_RF_E,							/* 20 TLSLD */
124c8754c30Sguenther 	_RF_E,							/* 21 DTPOFF32*/
125c8754c30Sguenther 	_RF_E,							/* 22 GOTTPOFF*/
126c8754c30Sguenther 	_RF_E							/* 23 TPOFF32*/
127c8754c30Sguenther };
128c8754c30Sguenther 
129c8754c30Sguenther #define RELOC_RESOLVE_SYMBOL(t)		((reloc_target_flags[t] & _RF_S) != 0)
130c8754c30Sguenther #define RELOC_PC_RELATIVE(t)		((reloc_target_flags[t] & _RF_P) != 0)
131c8754c30Sguenther #define RELOC_BASE_RELATIVE(t)		((reloc_target_flags[t] & _RF_B) != 0)
132c8754c30Sguenther #define RELOC_USE_ADDEND(t)		((reloc_target_flags[t] & _RF_A) != 0)
133c8754c30Sguenther #define RELOC_TARGET_SIZE(t)		((reloc_target_flags[t] >> 8) & 0xff)
134c8754c30Sguenther #define RELOC_VALUE_RIGHTSHIFT(t)	(reloc_target_flags[t] & 0xff)
135eabe4e05Skettenis #define RELOC_ERROR(t) \
136eabe4e05Skettenis 	((t) >= nitems(reloc_target_flags) || (reloc_target_flags[t] & _RF_E))
137c8754c30Sguenther 
138c8754c30Sguenther static const Elf_Addr reloc_target_bitmask[] = {
139c8754c30Sguenther #define _BM(x)  (~(Elf_Addr)0 >> ((8*sizeof(reloc_target_bitmask[0])) - (x)))
140c8754c30Sguenther 	0,			/*  0 NONE */
141c8754c30Sguenther 	_BM(64),		/*  1 _64*/
142c8754c30Sguenther 	_BM(32),		/*  2 PC32 */
143c8754c30Sguenther 	_BM(32),		/*  3 GOT32 */
144c8754c30Sguenther 	_BM(32),		/*  4 PLT32 */
145c8754c30Sguenther 	0,			/*  5 COPY */
146c8754c30Sguenther 	_BM(64),		/*  6 GLOB_DAT*/
147c8754c30Sguenther 	_BM(64),		/*  7 JUMP_SLOT*/
148c8754c30Sguenther 	_BM(64),		/*  8 RELATIVE*/
149c8754c30Sguenther 	_BM(32),		/*  9 GOTPCREL*/
150c8754c30Sguenther 	_BM(32),		/* 10 32 */
151c8754c30Sguenther 	_BM(32),		/* 11 32S */
152c8754c30Sguenther 	_BM(16),		/* 12 16 */
153c8754c30Sguenther 	_BM(16),		/* 13 PC16 */
154c8754c30Sguenther 	_BM(8),			/* 14 8 */
155c8754c30Sguenther 	_BM(8),			/* 15 PC8 */
156c8754c30Sguenther 	0,			/* 16 DTPMOD64*/
157c8754c30Sguenther 	0,			/* 17 DTPOFF64*/
158c8754c30Sguenther 	0,			/* 18 TPOFF64 */
159c8754c30Sguenther 	0,			/* 19 TLSGD */
160c8754c30Sguenther 	0,			/* 20 TLSLD */
161c8754c30Sguenther 	0,			/* 21 DTPOFF32*/
162c8754c30Sguenther 	0,			/* 22 GOTTPOFF*/
163c8754c30Sguenther 	0			/* 23 TPOFF32*/
164c8754c30Sguenther #undef _BM
165c8754c30Sguenther };
166c8754c30Sguenther #define RELOC_VALUE_BITMASK(t)	(reloc_target_bitmask[t])
167c8754c30Sguenther 
168c8754c30Sguenther void _dl_reloc_plt(Elf_Addr *where, Elf_Addr value);
169c8754c30Sguenther 
170672d3582Sdrahn int
_dl_md_reloc(elf_object_t * object,int rel,int relsz)171672d3582Sdrahn _dl_md_reloc(elf_object_t *object, int rel, int relsz)
172672d3582Sdrahn {
173672d3582Sdrahn 	long	i;
174672d3582Sdrahn 	long	numrel;
17588098a4dSguenther 	long	relrel;
176c8754c30Sguenther 	int	fails = 0;
177672d3582Sdrahn 	Elf_Addr loff;
17888098a4dSguenther 	Elf_Addr prev_value = 0;
17988098a4dSguenther 	const Elf_Sym *prev_sym = NULL;
180672d3582Sdrahn 	Elf_RelA *rels;
181672d3582Sdrahn 
182ce11e090Skurt 	loff = object->obj_base;
183672d3582Sdrahn 	numrel = object->Dyn.info[relsz] / sizeof(Elf_RelA);
184c8754c30Sguenther 	relrel = rel == DT_RELA ? object->relacount : 0;
185672d3582Sdrahn 	rels = (Elf_RelA *)(object->Dyn.info[rel]);
186672d3582Sdrahn 	if (rels == NULL)
187e3b0f1d9Sguenther 		return 0;
188672d3582Sdrahn 
1893b50b772Sguenther 	if (relrel > numrel)
1903b50b772Sguenther 		_dl_die("relacount > numrel: %ld > %ld", relrel, numrel);
19188098a4dSguenther 
19288098a4dSguenther 	/* tight loop for leading RELATIVE relocs */
19388098a4dSguenther 	for (i = 0; i < relrel; i++, rels++) {
19488098a4dSguenther 		Elf_Addr *where;
19588098a4dSguenther 
19688098a4dSguenther 		where = (Elf_Addr *)(rels->r_offset + loff);
19788098a4dSguenther 		*where = rels->r_addend + loff;
19888098a4dSguenther 	}
19988098a4dSguenther 	for (; i < numrel; i++, rels++) {
200c8754c30Sguenther 		Elf_Addr *where, value, mask;
201672d3582Sdrahn 		Elf_Word type;
202143e5accSguenther 		const Elf_Sym *sym;
203672d3582Sdrahn 		const char *symn;
204672d3582Sdrahn 
205c8754c30Sguenther 		type = ELF_R_TYPE(rels->r_info);
206c8754c30Sguenther 
207c8754c30Sguenther 		if (RELOC_ERROR(type))
208c8754c30Sguenther 			_dl_die("relocation error %d idx %ld", type, i);
209c8754c30Sguenther 
210c8754c30Sguenther 		if (type == R_TYPE(NONE))
211c8754c30Sguenther 			continue;
212c8754c30Sguenther 
213c8754c30Sguenther 		if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL)
214c8754c30Sguenther 			continue;
215c8754c30Sguenther 
216672d3582Sdrahn 		where = (Elf_Addr *)(rels->r_offset + loff);
217672d3582Sdrahn 
218c8754c30Sguenther 		if (RELOC_USE_ADDEND(type))
219c8754c30Sguenther 			value = rels->r_addend;
220c8754c30Sguenther 		else
221c8754c30Sguenther 			value = 0;
222c8754c30Sguenther 
223c8754c30Sguenther 		sym = NULL;
224c8754c30Sguenther 		symn = NULL;
225c8754c30Sguenther 		if (RELOC_RESOLVE_SYMBOL(type)) {
226672d3582Sdrahn 			sym = object->dyn.symtab;
227672d3582Sdrahn 			sym += ELF_R_SYM(rels->r_info);
228672d3582Sdrahn 			symn = object->dyn.strtab + sym->st_name;
229672d3582Sdrahn 
230672d3582Sdrahn 			if (sym->st_shndx != SHN_UNDEF &&
231672d3582Sdrahn 			    ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
232672d3582Sdrahn 				value += loff;
23388098a4dSguenther 			} else if (sym == prev_sym) {
23488098a4dSguenther 				value += prev_value;
235672d3582Sdrahn 			} else {
236143e5accSguenther 				struct sym_res sr;
237143e5accSguenther 
238143e5accSguenther 				sr = _dl_find_symbol(symn,
239c8754c30Sguenther 				    SYM_SEARCH_ALL|SYM_WARNNOTFOUND|
240c8754c30Sguenther 				    ((type == R_TYPE(JUMP_SLOT))?
241c8754c30Sguenther 					SYM_PLT:SYM_NOTPLT), sym, object);
242143e5accSguenther 				if (sr.sym == NULL) {
243c8754c30Sguenther resolve_failed:
244c8754c30Sguenther 					if (ELF_ST_BIND(sym->st_info) !=
245c8754c30Sguenther 					    STB_WEAK)
246c8754c30Sguenther 						fails++;
247672d3582Sdrahn 					continue;
248672d3582Sdrahn 				}
24988098a4dSguenther 				prev_sym = sym;
250c8754c30Sguenther 				prev_value = (Elf_Addr)(sr.obj->obj_base +
251c8754c30Sguenther 				    sr.sym->st_value);
25288098a4dSguenther 				value += prev_value;
253672d3582Sdrahn 			}
254672d3582Sdrahn 		}
255672d3582Sdrahn 
256c8754c30Sguenther 		if (type == R_TYPE(JUMP_SLOT)) {
257c8754c30Sguenther 			_dl_reloc_plt(where, value);
258c8754c30Sguenther 			continue;
259c8754c30Sguenther 		}
260c8754c30Sguenther 
261c8754c30Sguenther 		if (type == R_TYPE(COPY)) {
262c8754c30Sguenther 			void *dstaddr = where;
263c8754c30Sguenther 			const void *srcaddr;
264c8754c30Sguenther 			const Elf_Sym *dstsym = sym;
265c8754c30Sguenther 			struct sym_res sr;
266c8754c30Sguenther 
267c8754c30Sguenther 			sr = _dl_find_symbol(symn,
268c8754c30Sguenther 			    SYM_SEARCH_OTHER|SYM_WARNNOTFOUND|SYM_NOTPLT,
269c8754c30Sguenther 			    dstsym, object);
270c8754c30Sguenther 			if (sr.sym == NULL)
271c8754c30Sguenther 				goto resolve_failed;
272c8754c30Sguenther 
273c8754c30Sguenther 			srcaddr = (void *)(sr.obj->obj_base + sr.sym->st_value);
274c8754c30Sguenther 			_dl_bcopy(srcaddr, dstaddr, dstsym->st_size);
275c8754c30Sguenther 			continue;
276c8754c30Sguenther 		}
277c8754c30Sguenther 
278c8754c30Sguenther 		if (RELOC_PC_RELATIVE(type))
279c8754c30Sguenther 			value -= (Elf_Addr)where;
280c8754c30Sguenther 		if (RELOC_BASE_RELATIVE(type))
281c8754c30Sguenther 			value += loff;
282c8754c30Sguenther 
283c8754c30Sguenther 		mask = RELOC_VALUE_BITMASK(type);
284c8754c30Sguenther 		value >>= RELOC_VALUE_RIGHTSHIFT(type);
285c8754c30Sguenther 		value &= mask;
286c8754c30Sguenther 
287c8754c30Sguenther 		if (RELOC_TARGET_SIZE(type) > 32) {
288c8754c30Sguenther 			*where &= ~mask;
289c8754c30Sguenther 			*where |= value;
290c8754c30Sguenther 		} else {
291c8754c30Sguenther 			Elf32_Addr *where32 = (Elf32_Addr *)where;
292c8754c30Sguenther 
293c8754c30Sguenther 			*where32 &= ~mask;
294c8754c30Sguenther 			*where32 |= value;
295c8754c30Sguenther 		}
296c8754c30Sguenther 	}
297c8754c30Sguenther 
298c8754c30Sguenther 	return fails;
299c8754c30Sguenther }
300c8754c30Sguenther 
301c8754c30Sguenther void
_dl_reloc_plt(Elf_Addr * where,Elf_Addr value)302c8754c30Sguenther _dl_reloc_plt(Elf_Addr *where, Elf_Addr value)
303c8754c30Sguenther {
304c8754c30Sguenther 	*where = value;
305672d3582Sdrahn }
306672d3582Sdrahn 
307672d3582Sdrahn /*
308672d3582Sdrahn  * Resolve a symbol at run-time.
309672d3582Sdrahn  */
310672d3582Sdrahn Elf_Addr
_dl_bind(elf_object_t * object,int index)311672d3582Sdrahn _dl_bind(elf_object_t *object, int index)
312672d3582Sdrahn {
3130cab1769Sdrahn 	Elf_RelA *rel;
314143e5accSguenther 	const Elf_Sym *sym;
315672d3582Sdrahn 	const char *symn;
316143e5accSguenther 	struct sym_res sr;
3178465df4aSguenther 	int64_t cookie = pcookie;
3188465df4aSguenther 	struct {
3198465df4aSguenther 		struct __kbind param;
3208465df4aSguenther 		Elf_Addr newval;
3218465df4aSguenther 	} buf;
322672d3582Sdrahn 
3238465df4aSguenther 	rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]) + index;
324672d3582Sdrahn 
325672d3582Sdrahn 	sym = object->dyn.symtab;
326672d3582Sdrahn 	sym += ELF_R_SYM(rel->r_info);
327672d3582Sdrahn 	symn = object->dyn.strtab + sym->st_name;
328672d3582Sdrahn 
329143e5accSguenther 	sr = _dl_find_symbol(symn, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT,
330143e5accSguenther 	    sym, object);
331143e5accSguenther 	if (sr.sym == NULL)
3323b50b772Sguenther 		_dl_die("lazy binding failed!");
333672d3582Sdrahn 
334143e5accSguenther 	buf.newval = sr.obj->obj_base + sr.sym->st_value;
3350cab1769Sdrahn 
336143e5accSguenther 	if (__predict_false(sr.obj->traced) && _dl_trace_plt(sr.obj, symn))
337e3b0f1d9Sguenther 		return buf.newval;
338ae398163Smiod 
3398465df4aSguenther 	buf.param.kb_addr = (Elf_Word *)(object->obj_base + rel->r_offset);
3408465df4aSguenther 	buf.param.kb_size = sizeof(Elf_Addr);
3418465df4aSguenther 
3428465df4aSguenther 	/* directly code the syscall, so that it's actually inline here */
3438465df4aSguenther 	{
3448465df4aSguenther 		register long syscall_num __asm("rax") = SYS_kbind;
3458465df4aSguenther 		register void *arg1 __asm("rdi") = &buf;
3468465df4aSguenther 		register long  arg2 __asm("rsi") = sizeof(buf);
3478465df4aSguenther 		register long  arg3 __asm("rdx") = cookie;
3488465df4aSguenther 
3498465df4aSguenther 		__asm volatile("syscall" : "+r" (syscall_num), "+r" (arg3) :
3508465df4aSguenther 		    "r" (arg1), "r" (arg2) : "cc", "rcx", "r11", "memory");
351672d3582Sdrahn 	}
352e3b0f1d9Sguenther 	return buf.newval;
353672d3582Sdrahn }
354672d3582Sdrahn 
355e9cfe40cSmiod int
_dl_md_reloc_got(elf_object_t * object,int lazy)356672d3582Sdrahn _dl_md_reloc_got(elf_object_t *object, int lazy)
357672d3582Sdrahn {
358672d3582Sdrahn 	extern void _dl_bind_start(void);	/* XXX */
359c8754c30Sguenther 	int	fails = 0;
360672d3582Sdrahn 	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
361c8754c30Sguenther 	int i, num;
362c8754c30Sguenther 	Elf_RelA *rel;
363672d3582Sdrahn 
364672d3582Sdrahn 	if (pltgot == NULL)
365e3b0f1d9Sguenther 		return 0; /* it is possible to have no PLT/GOT relocations */
366672d3582Sdrahn 
367672d3582Sdrahn 	if (object->Dyn.info[DT_PLTREL] != DT_RELA)
368e3b0f1d9Sguenther 		return 0;
369672d3582Sdrahn 
370c8754c30Sguenther 	if (__predict_false(!lazy)) {
371c8754c30Sguenther 		fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
372c8754c30Sguenther 	} else {
373e23a26ffSguenther 		pltgot[1] = (Elf_Addr)object;
374e23a26ffSguenther 		pltgot[2] = (Elf_Addr)&_dl_bind_start;
375e23a26ffSguenther 
376c8754c30Sguenther 		rel = (Elf_RelA *)(object->Dyn.info[DT_JMPREL]);
377c8754c30Sguenther 		num = (object->Dyn.info[DT_PLTRELSZ]);
378c8754c30Sguenther 		for (i = 0; i < num/sizeof(Elf_RelA); i++, rel++) {
379672d3582Sdrahn 			Elf_Addr *where;
380c8754c30Sguenther 			where = (Elf_Addr *)(rel->r_offset + object->obj_base);
381ce11e090Skurt 			*where += object->obj_base;
382672d3582Sdrahn 		}
383c8754c30Sguenther 	}
3840cab1769Sdrahn 
385c8754c30Sguenther 	return fails;
386672d3582Sdrahn }
387