xref: /openbsd-src/libexec/ld.so/i386/rtld_machine.c (revision 388165f5fc29e344601c89e2ea0c4a725cc26eca)
1*388165f5Sgnezdo /*	$OpenBSD: rtld_machine.c,v 1.51 2023/01/29 20:30:21 gnezdo Exp $ */
23217e079Sdrahn 
33217e079Sdrahn /*
43217e079Sdrahn  * Copyright (c) 2002 Dale Rahn
53217e079Sdrahn  * Copyright (c) 2001 Niklas Hallqvist
63217e079Sdrahn  * Copyright (c) 2001 Artur Grabowski
73217e079Sdrahn  *
83217e079Sdrahn  * Redistribution and use in source and binary forms, with or without
93217e079Sdrahn  * modification, are permitted provided that the following conditions
103217e079Sdrahn  * are met:
113217e079Sdrahn  * 1. Redistributions of source code must retain the above copyright
123217e079Sdrahn  *    notice, this list of conditions and the following disclaimer.
133217e079Sdrahn  * 2. Redistributions in binary form must reproduce the above copyright
143217e079Sdrahn  *    notice, this list of conditions and the following disclaimer in the
153217e079Sdrahn  *    documentation and/or other materials provided with the distribution.
163217e079Sdrahn  *
173217e079Sdrahn  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
183217e079Sdrahn  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
193217e079Sdrahn  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203217e079Sdrahn  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
213217e079Sdrahn  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223217e079Sdrahn  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233217e079Sdrahn  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243217e079Sdrahn  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253217e079Sdrahn  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263217e079Sdrahn  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273217e079Sdrahn  * SUCH DAMAGE.
283217e079Sdrahn  */
293217e079Sdrahn /*-
303217e079Sdrahn  * Copyright (c) 2000 Eduardo Horvath.
313217e079Sdrahn  * Copyright (c) 1999 The NetBSD Foundation, Inc.
323217e079Sdrahn  * All rights reserved.
333217e079Sdrahn  *
343217e079Sdrahn  * This code is derived from software contributed to The NetBSD Foundation
353217e079Sdrahn  * by Paul Kranenburg.
363217e079Sdrahn  *
373217e079Sdrahn  * Redistribution and use in source and binary forms, with or without
383217e079Sdrahn  * modification, are permitted provided that the following conditions
393217e079Sdrahn  * are met:
403217e079Sdrahn  * 1. Redistributions of source code must retain the above copyright
413217e079Sdrahn  *    notice, this list of conditions and the following disclaimer.
423217e079Sdrahn  * 2. Redistributions in binary form must reproduce the above copyright
433217e079Sdrahn  *    notice, this list of conditions and the following disclaimer in the
443217e079Sdrahn  *    documentation and/or other materials provided with the distribution.
453217e079Sdrahn  * 3. All advertising materials mentioning features or use of this software
463217e079Sdrahn  *    must display the following acknowledgement:
473217e079Sdrahn  *	This product includes software developed by the NetBSD
483217e079Sdrahn  *	Foundation, Inc. and its contributors.
493217e079Sdrahn  * 4. Neither the name of The NetBSD Foundation nor the names of its
503217e079Sdrahn  *    contributors may be used to endorse or promote products derived
513217e079Sdrahn  *    from this software without specific prior written permission.
523217e079Sdrahn  *
533217e079Sdrahn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
543217e079Sdrahn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
553217e079Sdrahn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
563217e079Sdrahn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
573217e079Sdrahn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
583217e079Sdrahn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
593217e079Sdrahn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
603217e079Sdrahn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
613217e079Sdrahn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
623217e079Sdrahn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
633217e079Sdrahn  * POSSIBILITY OF SUCH DAMAGE.
643217e079Sdrahn  */
653217e079Sdrahn 
6603b27513Sdrahn #define _DYN_LOADER
6703b27513Sdrahn 
6803b27513Sdrahn #include <sys/types.h>
69b722ba42Sguenther #include <sys/exec_elf.h>
70325c51e2Sguenther #include <sys/syscall.h>
71325c51e2Sguenther #include <sys/unistd.h>
7203b27513Sdrahn 
73b722ba42Sguenther #include <machine/reloc.h>
7403b27513Sdrahn 
75b722ba42Sguenther #include "util.h"
7603b27513Sdrahn #include "resolve.h"
7703b27513Sdrahn 
78325c51e2Sguenther int64_t pcookie __attribute__((section(".openbsd.randomdata"))) __dso_hidden;
79325c51e2Sguenther 
80c8754c30Sguenther /*
81c8754c30Sguenther  * The following table holds for each relocation type:
82c8754c30Sguenther  *	- the width in bits of the memory location the relocation
83c8754c30Sguenther  *	  applies to (not currently used)
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 */
9992d6eedeSkettenis #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,							/* NONE */
10492d6eedeSkettenis 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* 32 */
105c8754c30Sguenther 	_RF_S|_RF_A|_RF_P|	_RF_SZ(32) | _RF_RS(0),		/* PC32 */
106c8754c30Sguenther 	_RF_G|			_RF_SZ(32) | _RF_RS(00),	/* GOT32 */
107c8754c30Sguenther 	      _RF_A|		_RF_SZ(32) | _RF_RS(0),		/* PLT32 */
108c8754c30Sguenther 	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/* COPY */
109c8754c30Sguenther 	_RF_S|_RF_A|		_RF_SZ(32) | _RF_RS(0),		/* GLOB_DAT */
110c8754c30Sguenther 	_RF_S|			_RF_SZ(32) | _RF_RS(0),		/* JUMP_SLOT */
111c8754c30Sguenther 	      _RF_A|	_RF_B|	_RF_SZ(32) | _RF_RS(0),		/* RELATIVE */
11292d6eedeSkettenis 	_RF_E,							/* GOTOFF */
11392d6eedeSkettenis 	_RF_E,							/* GOTPC */
11492d6eedeSkettenis 	_RF_E,							/* 32PLT */
11592d6eedeSkettenis 	_RF_E,							/* DUMMY 12 */
11692d6eedeSkettenis 	_RF_E,							/* DUMMY 13 */
11792d6eedeSkettenis 	_RF_E,							/* TLS_TPOFF */
11892d6eedeSkettenis 	_RF_E,							/* TLS_IE */
11992d6eedeSkettenis 	_RF_E,							/* TLS_GITIE */
12092d6eedeSkettenis 	_RF_E,							/* TLS_LE */
12192d6eedeSkettenis 	_RF_E,							/* TLS_GD */
12292d6eedeSkettenis 	_RF_E,							/* TLS_LDM */
12392d6eedeSkettenis 	_RF_S|_RF_A|		_RF_SZ(16) | _RF_RS(0),		/* 16 */
12492d6eedeSkettenis 	_RF_S|_RF_A|_RF_P|	_RF_SZ(16) | _RF_RS(0),		/* PC16 */
12592d6eedeSkettenis 	_RF_S|_RF_A|		_RF_SZ(8) | _RF_RS(0),		/* 8 */
12692d6eedeSkettenis 	_RF_S|_RF_A|_RF_P|	_RF_SZ(8) | _RF_RS(0),		/* PC8 */
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)
13592d6eedeSkettenis #define RELOC_ERROR(t) \
13692d6eedeSkettenis 	((t) >= nitems(reloc_target_flags) || (reloc_target_flags[t] & _RF_E))
137c8754c30Sguenther 
138c8754c30Sguenther static const long reloc_target_bitmask[] = {
139c8754c30Sguenther #define _BM(x)	(~(-(1ULL << (x))))
140c8754c30Sguenther 	0,		/* NONE */
141c8754c30Sguenther 	_BM(32),	/* RELOC_32*/
142c8754c30Sguenther 	_BM(32),	/* PC32 */
143c8754c30Sguenther 	_BM(32),	/* GOT32 */
144c8754c30Sguenther 	_BM(32),	/* PLT32 */
145c8754c30Sguenther 	0,		/* COPY */
146c8754c30Sguenther 	_BM(32),	/* GLOB_DAT */
147c8754c30Sguenther 	_BM(32),	/* JUMP_SLOT */
148c8754c30Sguenther 	_BM(32),	/* RELATIVE */
149c8754c30Sguenther 	0,		/* GOTOFF XXX */
150c8754c30Sguenther 	0,		/* GOTPC XXX */
151c8754c30Sguenther 	0,		/* DUMMY 11 */
152c8754c30Sguenther 	0,		/* DUMMY 12 */
153c8754c30Sguenther 	0,		/* DUMMY 13 */
154c8754c30Sguenther 	0,		/* DUMMY 14 */
155c8754c30Sguenther 	0,		/* DUMMY 15 */
156c8754c30Sguenther 	0,		/* DUMMY 16 */
157c8754c30Sguenther 	0,		/* DUMMY 17 */
158c8754c30Sguenther 	0,		/* DUMMY 18 */
159c8754c30Sguenther 	0,		/* DUMMY 19 */
160c8754c30Sguenther 	_BM(16),	/* RELOC_16 */
161c8754c30Sguenther 	_BM(8),		/* PC_16 */
162c8754c30Sguenther 	_BM(8),		/* RELOC_8 */
163c8754c30Sguenther 	_BM(8),		/* RELOC_PC8 */
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 
17003b27513Sdrahn int
_dl_md_reloc(elf_object_t * object,int rel,int relsz)17103b27513Sdrahn _dl_md_reloc(elf_object_t *object, int rel, int relsz)
17203b27513Sdrahn {
17303b27513Sdrahn 	long	i;
17403b27513Sdrahn 	long	numrel;
17588098a4dSguenther 	long	relrel;
176c8754c30Sguenther 	int	fails = 0;
17703b27513Sdrahn 	Elf_Addr loff;
17888098a4dSguenther 	Elf_Addr prev_value = 0;
17988098a4dSguenther 	const Elf_Sym *prev_sym = NULL;
18003b27513Sdrahn 	Elf_Rel *rels;
18103b27513Sdrahn 
182ce11e090Skurt 	loff = object->obj_base;
183e3b0f1d9Sguenther 	numrel = object->Dyn.info[relsz] / sizeof(Elf_Rel);
184c8754c30Sguenther 	relrel = rel == DT_REL ? object->relcount : 0;
185e3b0f1d9Sguenther 	rels = (Elf_Rel *)(object->Dyn.info[rel]);
18603b27513Sdrahn 	if (rels == NULL)
187e3b0f1d9Sguenther 		return 0;
18803b27513Sdrahn 
1893b50b772Sguenther 	if (relrel > numrel)
1903b50b772Sguenther 		_dl_die("relcount > 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 += loff;
19888098a4dSguenther 	}
19988098a4dSguenther 	for (; i < numrel; i++, rels++) {
200c8754c30Sguenther 		Elf_Addr *where, value, mask;
20103b27513Sdrahn 		Elf_Word type;
202143e5accSguenther 		const Elf_Sym *sym;
20303b27513Sdrahn 		const char *symn;
20403b27513Sdrahn 
205c8754c30Sguenther 		type = ELF_R_TYPE(rels->r_info);
206c8754c30Sguenther 
20792d6eedeSkettenis 		if (RELOC_ERROR(type))
20892d6eedeSkettenis 			_dl_die("relocation error %d idx %ld", type, i);
20992d6eedeSkettenis 
210c8754c30Sguenther 		if (type == R_TYPE(NONE))
211c8754c30Sguenther 			continue;
212c8754c30Sguenther 
213c8754c30Sguenther 		if (type == R_TYPE(JUMP_SLOT) && rel != DT_JMPREL)
214c8754c30Sguenther 			continue;
215c8754c30Sguenther 
21603b27513Sdrahn 		where = (Elf_Addr *)(rels->r_offset + loff);
21703b27513Sdrahn 
218c8754c30Sguenther 		if (RELOC_USE_ADDEND(type))
219c8754c30Sguenther 			value = *where & RELOC_VALUE_BITMASK(type);
220c8754c30Sguenther 		else
221c8754c30Sguenther 			value = 0;
222c8754c30Sguenther 
223c8754c30Sguenther 		sym = NULL;
224c8754c30Sguenther 		symn = NULL;
225c8754c30Sguenther 		if (RELOC_RESOLVE_SYMBOL(type)) {
22603b27513Sdrahn 			sym = object->dyn.symtab;
22703b27513Sdrahn 			sym += ELF_R_SYM(rels->r_info);
22803b27513Sdrahn 			symn = object->dyn.strtab + sym->st_name;
22903b27513Sdrahn 
23003b27513Sdrahn 			if (sym->st_shndx != SHN_UNDEF &&
23103b27513Sdrahn 			    ELF_ST_BIND(sym->st_info) == STB_LOCAL) {
23203b27513Sdrahn 				value += loff;
23388098a4dSguenther 			} else if (sym == prev_sym) {
23488098a4dSguenther 				value += prev_value;
23503b27513Sdrahn 			} 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++;
24703b27513Sdrahn 					continue;
24803b27513Sdrahn 				}
24988098a4dSguenther 				prev_sym = sym;
250c8754c30Sguenther 				prev_value = (Elf_Addr)(sr.obj->obj_base +
251c8754c30Sguenther 				    sr.sym->st_value);
25288098a4dSguenther 				value += prev_value;
25303b27513Sdrahn 			}
25403b27513Sdrahn 		}
25503b27513Sdrahn 
256c8754c30Sguenther 		if (type == R_TYPE(JUMP_SLOT)) {
257c8754c30Sguenther 			_dl_reloc_plt((Elf_Word *)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 		*where &= ~mask;
288c8754c30Sguenther 		*where |= value;
289c8754c30Sguenther 	}
290c8754c30Sguenther 
291c8754c30Sguenther 	return fails;
29203b27513Sdrahn }
29303b27513Sdrahn 
29491d8decdSdrahn #if 0
29503b27513Sdrahn struct jmpslot {
29603b27513Sdrahn 	u_short opcode;
29703b27513Sdrahn 	u_short addr[2];
29803b27513Sdrahn 	u_short reloc_index;
29903b27513Sdrahn #define JMPSLOT_RELOC_MASK	0xffff
30003b27513Sdrahn };
30103b27513Sdrahn #define JUMP			0xe990	/* NOP + JMP opcode */
30291d8decdSdrahn #endif
30303b27513Sdrahn 
304c8754c30Sguenther void
_dl_reloc_plt(Elf_Addr * where,Elf_Addr value)305c8754c30Sguenther _dl_reloc_plt(Elf_Addr *where, Elf_Addr value)
306c8754c30Sguenther {
307c8754c30Sguenther 	*where = value;
308c8754c30Sguenther }
309c8754c30Sguenther 
31003b27513Sdrahn /*
31103b27513Sdrahn  * Resolve a symbol at run-time.
31203b27513Sdrahn  */
31303b27513Sdrahn Elf_Addr
_dl_bind(elf_object_t * object,int index)31403b27513Sdrahn _dl_bind(elf_object_t *object, int index)
31503b27513Sdrahn {
31603b27513Sdrahn 	Elf_Rel *rel;
317143e5accSguenther 	const Elf_Sym *sym;
31803b27513Sdrahn 	const char *symn;
319143e5accSguenther 	struct sym_res sr;
320325c51e2Sguenther 	uint64_t cookie = pcookie;
321325c51e2Sguenther 	struct {
322325c51e2Sguenther 		struct __kbind param;
323325c51e2Sguenther 		Elf_Addr newval;
324325c51e2Sguenther 	} buf;
32503b27513Sdrahn 
32603b27513Sdrahn 	rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
32703b27513Sdrahn 
32803b27513Sdrahn 	rel += index/sizeof(Elf_Rel);
32903b27513Sdrahn 
33003b27513Sdrahn 	sym = object->dyn.symtab;
33103b27513Sdrahn 	sym += ELF_R_SYM(rel->r_info);
33203b27513Sdrahn 	symn = object->dyn.strtab + sym->st_name;
33303b27513Sdrahn 
334143e5accSguenther 	sr = _dl_find_symbol(symn, SYM_SEARCH_ALL|SYM_WARNNOTFOUND|SYM_PLT,
335143e5accSguenther 	    sym, object);
336143e5accSguenther 	if (sr.sym == NULL)
3373b50b772Sguenther 		_dl_die("lazy binding failed!");
33803b27513Sdrahn 
339143e5accSguenther 	buf.newval = sr.obj->obj_base + sr.sym->st_value;
340ae398163Smiod 
341143e5accSguenther 	if (__predict_false(sr.obj->traced) && _dl_trace_plt(sr.obj, symn))
342e3b0f1d9Sguenther 		return buf.newval;
343325c51e2Sguenther 
344325c51e2Sguenther 	buf.param.kb_addr = (Elf_Word *)(object->obj_base + rel->r_offset);
345325c51e2Sguenther 	buf.param.kb_size = sizeof(Elf_Addr);
346325c51e2Sguenther 
347325c51e2Sguenther 	/* directly code the syscall, so that it's actually inline here */
348325c51e2Sguenther 	{
349325c51e2Sguenther 		register long syscall_num __asm("eax") = SYS_kbind;
350325c51e2Sguenther 
351f60b60f5Snaddy 		__asm volatile("lea %3, %%edx; pushl 4(%%edx);"
352f60b60f5Snaddy 		    " pushl (%%edx); pushl %2; pushl %1;"
353325c51e2Sguenther 		    " push %%eax; int $0x80; addl $20, %%esp" :
354325c51e2Sguenther 		    "+a" (syscall_num) : "r" (&buf), "i" (sizeof(buf)),
355325c51e2Sguenther 		    "m" (cookie) : "edx", "cc", "memory");
35691d8decdSdrahn 	}
35791d8decdSdrahn 
358e3b0f1d9Sguenther 	return buf.newval;
35903b27513Sdrahn }
36003b27513Sdrahn 
361e9cfe40cSmiod int
_dl_md_reloc_got(elf_object_t * object,int lazy)36203b27513Sdrahn _dl_md_reloc_got(elf_object_t *object, int lazy)
36303b27513Sdrahn {
36403b27513Sdrahn 	extern void _dl_bind_start(void);	/* XXX */
365c8754c30Sguenther 	int	fails = 0;
36603b27513Sdrahn 	Elf_Addr *pltgot = (Elf_Addr *)object->Dyn.info[DT_PLTGOT];
367c8754c30Sguenther 	int i, num;
368c8754c30Sguenther 	Elf_Rel *rel;
36903b27513Sdrahn 
370b154c65fSdrahn 	if (pltgot == NULL)
371e3b0f1d9Sguenther 		return 0; /* it is possible to have no PLT/GOT relocations */
372b154c65fSdrahn 
37303b27513Sdrahn 	if (object->Dyn.info[DT_PLTREL] != DT_REL)
374e3b0f1d9Sguenther 		return 0;
37503b27513Sdrahn 
376c8754c30Sguenther 	if (!lazy) {
377c8754c30Sguenther 		fails = _dl_md_reloc(object, DT_JMPREL, DT_PLTRELSZ);
378c8754c30Sguenther 	} else {
379e23a26ffSguenther 		pltgot[1] = (Elf_Addr)object;
380e23a26ffSguenther 		pltgot[2] = (Elf_Addr)&_dl_bind_start;
381e23a26ffSguenther 
382c8754c30Sguenther 		rel = (Elf_Rel *)(object->Dyn.info[DT_JMPREL]);
383c8754c30Sguenther 		num = (object->Dyn.info[DT_PLTRELSZ]);
384c8754c30Sguenther 		for (i = 0; i < num/sizeof(Elf_Rel); i++, rel++) {
38503b27513Sdrahn 			Elf_Addr *where;
386c8754c30Sguenther 			where = (Elf_Addr *)(rel->r_offset + object->obj_base);
387ce11e090Skurt 			*where += object->obj_base;
38803b27513Sdrahn 		}
389c8754c30Sguenther 	}
390e23a26ffSguenther 
391c8754c30Sguenther 	return fails;
39203b27513Sdrahn }
393