Home | History | Annotate | Line # | Download | only in m68k
      1  1.37  riastrad /*	$NetBSD: mdreloc.c,v 1.37 2025/04/18 12:56:47 riastradh Exp $	*/
      2  1.36  riastrad 
      3  1.36  riastrad /*
      4  1.36  riastrad  * Copyright 1996 John D. Polstra.
      5  1.36  riastrad  * Copyright 1996 Matt Thomas <matt (at) 3am-software.com>
      6  1.36  riastrad  * All rights reserved.
      7  1.36  riastrad  *
      8  1.36  riastrad  * Redistribution and use in source and binary forms, with or without
      9  1.36  riastrad  * modification, are permitted provided that the following conditions
     10  1.36  riastrad  * are met:
     11  1.36  riastrad  * 1. Redistributions of source code must retain the above copyright
     12  1.36  riastrad  *    notice, this list of conditions and the following disclaimer.
     13  1.36  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.36  riastrad  *    notice, this list of conditions and the following disclaimer in the
     15  1.36  riastrad  *    documentation and/or other materials provided with the distribution.
     16  1.36  riastrad  * 3. All advertising materials mentioning features or use of this software
     17  1.36  riastrad  *    must display the following acknowledgement:
     18  1.36  riastrad  *      This product includes software developed by John Polstra.
     19  1.36  riastrad  * 4. The name of the author may not be used to endorse or promote products
     20  1.36  riastrad  *    derived from this software without specific prior written permission.
     21  1.36  riastrad  *
     22  1.36  riastrad  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.36  riastrad  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.36  riastrad  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.36  riastrad  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.36  riastrad  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.36  riastrad  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.36  riastrad  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.36  riastrad  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.36  riastrad  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.36  riastrad  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.36  riastrad  */
     33  1.19     skrll 
     34  1.37  riastrad /*
     35  1.37  riastrad  * m68k ELF relocations.
     36  1.37  riastrad  *
     37  1.37  riastrad  * References:
     38  1.37  riastrad  *
     39  1.37  riastrad  *	[M68KSYSVABI] System V Application Binary Interface: Motorola
     40  1.37  riastrad  *	68000 Processor Family Supplement, 1990, AT&T.
     41  1.37  riastrad  *	https://people.debian.org/~glaubitz/m68k-sysv-abi.pdf
     42  1.37  riastrad  *	https://web.archive.org/web/20250317195959/https://people.debian.org/~glaubitz/m68k-sysv-abi.pdf
     43  1.37  riastrad  */
     44  1.37  riastrad 
     45  1.19     skrll #include <sys/cdefs.h>
     46  1.19     skrll #ifndef lint
     47  1.37  riastrad __RCSID("$NetBSD: mdreloc.c,v 1.37 2025/04/18 12:56:47 riastradh Exp $");
     48  1.19     skrll #endif /* not lint */
     49  1.10  junyoung 
     50   1.1   mycroft #include <sys/types.h>
     51   1.1   mycroft 
     52   1.1   mycroft #include "debug.h"
     53   1.1   mycroft #include "rtld.h"
     54   1.1   mycroft 
     55  1.35  christos #include <machine/lwp_private.h>
     56  1.35  christos 
     57  1.12   mycroft void _rtld_bind_start(void);
     58  1.11   mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     59  1.17     skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     60  1.18     skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
     61  1.18     skrll     const Elf_Rela *, Elf_Addr *);
     62  1.18     skrll 
     63  1.11   mycroft 
     64   1.1   mycroft void
     65   1.1   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
     66   1.1   mycroft {
     67   1.1   mycroft 	obj->pltgot[1] = (Elf_Addr) obj;
     68   1.1   mycroft 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     69   1.1   mycroft }
     70   1.2   mycroft 
     71  1.11   mycroft void
     72  1.17     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     73  1.11   mycroft {
     74  1.11   mycroft 	const Elf_Rela *rela = 0, *relalim;
     75  1.11   mycroft 	Elf_Addr relasz = 0;
     76  1.11   mycroft 	Elf_Addr *where;
     77  1.11   mycroft 
     78  1.11   mycroft 	for (; dynp->d_tag != DT_NULL; dynp++) {
     79  1.11   mycroft 		switch (dynp->d_tag) {
     80  1.11   mycroft 		case DT_RELA:
     81  1.11   mycroft 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
     82  1.11   mycroft 			break;
     83  1.11   mycroft 		case DT_RELASZ:
     84  1.11   mycroft 			relasz = dynp->d_un.d_val;
     85  1.11   mycroft 			break;
     86  1.11   mycroft 		}
     87  1.11   mycroft 	}
     88  1.23     lukem 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
     89  1.11   mycroft 	for (; rela < relalim; rela++) {
     90  1.11   mycroft 		where = (Elf_Addr *)(relocbase + rela->r_offset);
     91  1.11   mycroft 		*where += (Elf_Addr)relocbase;
     92  1.11   mycroft 	}
     93  1.11   mycroft }
     94  1.11   mycroft 
     95   1.2   mycroft int
     96  1.27     joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
     97   1.2   mycroft {
     98   1.3   mycroft 	const Elf_Rela *rela;
     99  1.32     joerg 	const Elf_Sym *def = NULL;
    100  1.32     joerg 	const Obj_Entry *defobj = NULL;
    101  1.32     joerg 	unsigned long last_symnum = ULONG_MAX;
    102   1.2   mycroft 
    103   1.3   mycroft 	for (rela = obj->rela; rela < obj->relalim; rela++) {
    104   1.3   mycroft 		Elf_Addr        *where;
    105   1.3   mycroft 		Elf_Addr         tmp;
    106   1.4   mycroft 		unsigned long	 symnum;
    107   1.3   mycroft 
    108   1.3   mycroft 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    109  1.32     joerg 
    110  1.32     joerg 		switch (ELF_R_TYPE(rela->r_info)) {
    111  1.32     joerg 		case R_TYPE(PC32):
    112  1.32     joerg 		case R_TYPE(GOT32):
    113  1.32     joerg 		case R_TYPE(32):
    114  1.32     joerg 		case R_TYPE(GLOB_DAT):
    115  1.32     joerg 		case R_TYPE(TLS_DTPMOD32):
    116  1.32     joerg 		case R_TYPE(TLS_DTPREL32):
    117  1.32     joerg 		case R_TYPE(TLS_TPREL32):
    118  1.32     joerg 			symnum = ELF_R_SYM(rela->r_info);
    119  1.32     joerg 			if (last_symnum != symnum) {
    120  1.32     joerg 				last_symnum = symnum;
    121  1.32     joerg 				def = _rtld_find_symdef(symnum, obj, &defobj,
    122  1.32     joerg 				    false);
    123  1.32     joerg 				if (def == NULL)
    124  1.32     joerg 					return -1;
    125  1.32     joerg 			}
    126  1.32     joerg 			break;
    127  1.32     joerg 		default:
    128  1.32     joerg 			break;
    129  1.32     joerg 		}
    130   1.3   mycroft 
    131   1.3   mycroft 		switch (ELF_R_TYPE(rela->r_info)) {
    132   1.3   mycroft 		case R_TYPE(NONE):
    133   1.3   mycroft 			break;
    134   1.2   mycroft 
    135   1.2   mycroft #if 1 /* XXX should not occur */
    136   1.3   mycroft 		case R_TYPE(PC32):
    137   1.3   mycroft 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    138   1.3   mycroft 			    rela->r_addend) - (Elf_Addr)where;
    139   1.3   mycroft 			if (*where != tmp)
    140   1.3   mycroft 				*where = tmp;
    141  1.13   mycroft 			rdbg(("PC32 %s in %s --> %p in %s",
    142   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    143   1.5   mycroft 			    obj->path, (void *)*where, defobj->path));
    144   1.3   mycroft 			break;
    145   1.2   mycroft 
    146   1.3   mycroft 		case R_TYPE(GOT32):
    147   1.2   mycroft #endif
    148   1.3   mycroft 		case R_TYPE(32):
    149   1.3   mycroft 		case R_TYPE(GLOB_DAT):
    150   1.3   mycroft 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    151   1.3   mycroft 			    rela->r_addend);
    152   1.3   mycroft 			if (*where != tmp)
    153   1.3   mycroft 				*where = tmp;
    154  1.13   mycroft 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    155   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    156   1.5   mycroft 			    obj->path, (void *)*where, defobj->path));
    157   1.3   mycroft 			break;
    158   1.3   mycroft 
    159   1.3   mycroft 		case R_TYPE(RELATIVE):
    160   1.3   mycroft 			*where += (Elf_Addr)obj->relocbase;
    161  1.13   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    162   1.3   mycroft 			    (void *)*where));
    163   1.3   mycroft 			break;
    164   1.3   mycroft 
    165   1.3   mycroft 		case R_TYPE(COPY):
    166   1.3   mycroft 			/*
    167   1.3   mycroft 			 * These are deferred until all other relocations have
    168   1.3   mycroft 			 * been done.  All we do here is make sure that the
    169   1.3   mycroft 			 * COPY relocation is not in a shared library.  They
    170   1.3   mycroft 			 * are allowed only in executable files.
    171   1.3   mycroft 			 */
    172   1.8   mycroft 			if (obj->isdynamic) {
    173   1.3   mycroft 				_rtld_error(
    174   1.2   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    175   1.3   mycroft 				    obj->path);
    176   1.3   mycroft 				return -1;
    177   1.3   mycroft 			}
    178  1.13   mycroft 			rdbg(("COPY (avoid in main)"));
    179   1.3   mycroft 			break;
    180   1.3   mycroft 
    181  1.29     joerg 		case R_TYPE(TLS_DTPMOD32):
    182  1.29     joerg 			*where = (Elf_Addr)defobj->tlsindex;
    183  1.29     joerg 			rdbg(("DTPMOD32 %s in %s --> %p in %s",
    184  1.29     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    185  1.29     joerg 			    obj->path, (void *)*where, defobj->path));
    186  1.29     joerg 			break;
    187  1.29     joerg 
    188  1.29     joerg 		case R_TYPE(TLS_DTPREL32):
    189  1.29     joerg 			*where = (Elf_Addr)(def->st_value + rela->r_addend
    190  1.29     joerg 			    - TLS_DTV_OFFSET);
    191  1.29     joerg 			rdbg(("DTPREL32 %s in %s --> %p in %s",
    192  1.29     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    193  1.29     joerg 			    obj->path, (void *)*where, defobj->path));
    194  1.29     joerg 			break;
    195  1.29     joerg 
    196  1.29     joerg 		case R_TYPE(TLS_TPREL32):
    197  1.34     joerg 			if (!defobj->tls_static &&
    198  1.34     joerg 			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
    199  1.29     joerg 				return -1;
    200  1.29     joerg 
    201  1.29     joerg 			*where = (Elf_Addr)(def->st_value + rela->r_addend
    202  1.29     joerg 			    + defobj->tlsoffset - TLS_TP_OFFSET);
    203  1.29     joerg 			rdbg(("TPREL32 %s in %s --> %p in %s",
    204  1.29     joerg 			    obj->strtab + obj->symtab[symnum].st_name,
    205  1.29     joerg 			    obj->path, (void *)*where, defobj->path));
    206  1.29     joerg 			break;
    207  1.29     joerg 
    208   1.3   mycroft 		default:
    209  1.13   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    210   1.3   mycroft 			    "addend = %p, contents = %p, symbol = %s",
    211  1.32     joerg 			    (u_long)ELF_R_SYM(rela->r_info),
    212  1.32     joerg 			    (u_long)ELF_R_TYPE(rela->r_info),
    213   1.3   mycroft 			    (void *)rela->r_offset, (void *)rela->r_addend,
    214   1.3   mycroft 			    (void *)*where,
    215   1.4   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    216   1.3   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    217  1.24      jmmv 			    "in non-PLT relocations",
    218   1.3   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    219   1.2   mycroft 			return -1;
    220   1.2   mycroft 		}
    221   1.2   mycroft 	}
    222   1.2   mycroft 	return 0;
    223   1.2   mycroft }
    224   1.6   mycroft 
    225   1.6   mycroft int
    226  1.33     joerg _rtld_relocate_plt_lazy(Obj_Entry *obj)
    227   1.6   mycroft {
    228   1.6   mycroft 	const Elf_Rela *rela;
    229   1.6   mycroft 
    230  1.16   mycroft 	if (!obj->relocbase)
    231   1.6   mycroft 		return 0;
    232   1.6   mycroft 
    233   1.6   mycroft 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    234   1.6   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    235   1.6   mycroft 
    236   1.6   mycroft 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    237   1.6   mycroft 
    238   1.6   mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    239   1.6   mycroft 		*where += (Elf_Addr)obj->relocbase;
    240  1.13   mycroft 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    241   1.6   mycroft 	}
    242   1.6   mycroft 
    243   1.6   mycroft 	return 0;
    244   1.6   mycroft }
    245   1.6   mycroft 
    246  1.18     skrll static inline int
    247  1.26     skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
    248  1.26     skrll     Elf_Addr *tp)
    249   1.6   mycroft {
    250   1.6   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    251   1.6   mycroft 	Elf_Addr new_value;
    252   1.6   mycroft 	const Elf_Sym  *def;
    253   1.6   mycroft 	const Obj_Entry *defobj;
    254  1.25  christos 	unsigned long info = rela->r_info;
    255   1.6   mycroft 
    256  1.25  christos 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    257   1.6   mycroft 
    258  1.25  christos 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    259  1.25  christos 	if (__predict_false(def == NULL))
    260  1.18     skrll 		return -1;
    261  1.25  christos 	if (__predict_false(def == &_rtld_sym_zero))
    262  1.25  christos 		return 0;
    263   1.6   mycroft 
    264  1.18     skrll 	assert(rela->r_addend == 0);
    265  1.30     joerg 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    266  1.30     joerg 		if (tp == NULL)
    267  1.30     joerg 			return 0;
    268  1.30     joerg 		new_value = _rtld_resolve_ifunc(defobj, def);
    269  1.30     joerg 	} else {
    270  1.30     joerg 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
    271  1.30     joerg 		    rela->r_addend);
    272  1.30     joerg 	}
    273  1.13   mycroft 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    274   1.6   mycroft 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    275   1.6   mycroft 	if (*where != new_value)
    276   1.6   mycroft 		*where = new_value;
    277   1.6   mycroft 
    278  1.18     skrll 	if (tp)
    279  1.18     skrll 		*tp = new_value - rela->r_addend;
    280  1.18     skrll 
    281  1.18     skrll 	return 0;
    282  1.18     skrll }
    283  1.18     skrll 
    284  1.18     skrll caddr_t
    285  1.18     skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    286  1.18     skrll {
    287  1.23     lukem 	const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
    288  1.18     skrll 	Elf_Addr result;
    289  1.18     skrll 	int err;
    290  1.18     skrll 
    291  1.20       mrg 	result = 0;	/* XXX gcc */
    292  1.20       mrg 
    293  1.28     joerg 	_rtld_shared_enter();
    294  1.18     skrll 	err = _rtld_relocate_plt_object(obj, rela, &result);
    295  1.25  christos 	if (err)
    296  1.18     skrll 		_rtld_die();
    297  1.28     joerg 	_rtld_shared_exit();
    298  1.18     skrll 
    299  1.18     skrll 	return (caddr_t)result;
    300  1.18     skrll }
    301  1.18     skrll 
    302  1.18     skrll int
    303  1.18     skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
    304  1.18     skrll {
    305  1.18     skrll 	const Elf_Rela *rela;
    306  1.18     skrll 
    307  1.18     skrll 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
    308  1.18     skrll 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    309  1.18     skrll 			return -1;
    310  1.18     skrll 
    311  1.18     skrll 	return 0;
    312   1.6   mycroft }
    313