Home | History | Annotate | Line # | Download | only in m68k
mdreloc.c revision 1.35
      1 /*	$NetBSD: mdreloc.c,v 1.35 2024/11/30 01:04:05 christos Exp $	*/
      2 
      3 #include <sys/cdefs.h>
      4 #ifndef lint
      5 __RCSID("$NetBSD: mdreloc.c,v 1.35 2024/11/30 01:04:05 christos Exp $");
      6 #endif /* not lint */
      7 
      8 #include <sys/types.h>
      9 
     10 #include "debug.h"
     11 #include "rtld.h"
     12 
     13 #include <machine/lwp_private.h>
     14 
     15 void _rtld_bind_start(void);
     16 void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     17 caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     18 static inline int _rtld_relocate_plt_object(const Obj_Entry *,
     19     const Elf_Rela *, Elf_Addr *);
     20 
     21 
     22 void
     23 _rtld_setup_pltgot(const Obj_Entry *obj)
     24 {
     25 	obj->pltgot[1] = (Elf_Addr) obj;
     26 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     27 }
     28 
     29 void
     30 _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     31 {
     32 	const Elf_Rela *rela = 0, *relalim;
     33 	Elf_Addr relasz = 0;
     34 	Elf_Addr *where;
     35 
     36 	for (; dynp->d_tag != DT_NULL; dynp++) {
     37 		switch (dynp->d_tag) {
     38 		case DT_RELA:
     39 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
     40 			break;
     41 		case DT_RELASZ:
     42 			relasz = dynp->d_un.d_val;
     43 			break;
     44 		}
     45 	}
     46 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
     47 	for (; rela < relalim; rela++) {
     48 		where = (Elf_Addr *)(relocbase + rela->r_offset);
     49 		*where += (Elf_Addr)relocbase;
     50 	}
     51 }
     52 
     53 int
     54 _rtld_relocate_nonplt_objects(Obj_Entry *obj)
     55 {
     56 	const Elf_Rela *rela;
     57 	const Elf_Sym *def = NULL;
     58 	const Obj_Entry *defobj = NULL;
     59 	unsigned long last_symnum = ULONG_MAX;
     60 
     61 	for (rela = obj->rela; rela < obj->relalim; rela++) {
     62 		Elf_Addr        *where;
     63 		Elf_Addr         tmp;
     64 		unsigned long	 symnum;
     65 
     66 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
     67 
     68 		switch (ELF_R_TYPE(rela->r_info)) {
     69 		case R_TYPE(PC32):
     70 		case R_TYPE(GOT32):
     71 		case R_TYPE(32):
     72 		case R_TYPE(GLOB_DAT):
     73 		case R_TYPE(TLS_DTPMOD32):
     74 		case R_TYPE(TLS_DTPREL32):
     75 		case R_TYPE(TLS_TPREL32):
     76 			symnum = ELF_R_SYM(rela->r_info);
     77 			if (last_symnum != symnum) {
     78 				last_symnum = symnum;
     79 				def = _rtld_find_symdef(symnum, obj, &defobj,
     80 				    false);
     81 				if (def == NULL)
     82 					return -1;
     83 			}
     84 			break;
     85 		default:
     86 			break;
     87 		}
     88 
     89 		switch (ELF_R_TYPE(rela->r_info)) {
     90 		case R_TYPE(NONE):
     91 			break;
     92 
     93 #if 1 /* XXX should not occur */
     94 		case R_TYPE(PC32):
     95 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
     96 			    rela->r_addend) - (Elf_Addr)where;
     97 			if (*where != tmp)
     98 				*where = tmp;
     99 			rdbg(("PC32 %s in %s --> %p in %s",
    100 			    obj->strtab + obj->symtab[symnum].st_name,
    101 			    obj->path, (void *)*where, defobj->path));
    102 			break;
    103 
    104 		case R_TYPE(GOT32):
    105 #endif
    106 		case R_TYPE(32):
    107 		case R_TYPE(GLOB_DAT):
    108 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
    109 			    rela->r_addend);
    110 			if (*where != tmp)
    111 				*where = tmp;
    112 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    113 			    obj->strtab + obj->symtab[symnum].st_name,
    114 			    obj->path, (void *)*where, defobj->path));
    115 			break;
    116 
    117 		case R_TYPE(RELATIVE):
    118 			*where += (Elf_Addr)obj->relocbase;
    119 			rdbg(("RELATIVE in %s --> %p", obj->path,
    120 			    (void *)*where));
    121 			break;
    122 
    123 		case R_TYPE(COPY):
    124 			/*
    125 			 * These are deferred until all other relocations have
    126 			 * been done.  All we do here is make sure that the
    127 			 * COPY relocation is not in a shared library.  They
    128 			 * are allowed only in executable files.
    129 			 */
    130 			if (obj->isdynamic) {
    131 				_rtld_error(
    132 			"%s: Unexpected R_COPY relocation in shared library",
    133 				    obj->path);
    134 				return -1;
    135 			}
    136 			rdbg(("COPY (avoid in main)"));
    137 			break;
    138 
    139 		case R_TYPE(TLS_DTPMOD32):
    140 			*where = (Elf_Addr)defobj->tlsindex;
    141 			rdbg(("DTPMOD32 %s in %s --> %p in %s",
    142 			    obj->strtab + obj->symtab[symnum].st_name,
    143 			    obj->path, (void *)*where, defobj->path));
    144 			break;
    145 
    146 		case R_TYPE(TLS_DTPREL32):
    147 			*where = (Elf_Addr)(def->st_value + rela->r_addend
    148 			    - TLS_DTV_OFFSET);
    149 			rdbg(("DTPREL32 %s in %s --> %p in %s",
    150 			    obj->strtab + obj->symtab[symnum].st_name,
    151 			    obj->path, (void *)*where, defobj->path));
    152 			break;
    153 
    154 		case R_TYPE(TLS_TPREL32):
    155 			if (!defobj->tls_static &&
    156 			    _rtld_tls_offset_allocate(__UNCONST(defobj)))
    157 				return -1;
    158 
    159 			*where = (Elf_Addr)(def->st_value + rela->r_addend
    160 			    + defobj->tlsoffset - TLS_TP_OFFSET);
    161 			rdbg(("TPREL32 %s in %s --> %p in %s",
    162 			    obj->strtab + obj->symtab[symnum].st_name,
    163 			    obj->path, (void *)*where, defobj->path));
    164 			break;
    165 
    166 		default:
    167 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    168 			    "addend = %p, contents = %p, symbol = %s",
    169 			    (u_long)ELF_R_SYM(rela->r_info),
    170 			    (u_long)ELF_R_TYPE(rela->r_info),
    171 			    (void *)rela->r_offset, (void *)rela->r_addend,
    172 			    (void *)*where,
    173 			    obj->strtab + obj->symtab[symnum].st_name));
    174 			_rtld_error("%s: Unsupported relocation type %ld "
    175 			    "in non-PLT relocations",
    176 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    177 			return -1;
    178 		}
    179 	}
    180 	return 0;
    181 }
    182 
    183 int
    184 _rtld_relocate_plt_lazy(Obj_Entry *obj)
    185 {
    186 	const Elf_Rela *rela;
    187 
    188 	if (!obj->relocbase)
    189 		return 0;
    190 
    191 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    192 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    193 
    194 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    195 
    196 		/* Just relocate the GOT slots pointing into the PLT */
    197 		*where += (Elf_Addr)obj->relocbase;
    198 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    199 	}
    200 
    201 	return 0;
    202 }
    203 
    204 static inline int
    205 _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela,
    206     Elf_Addr *tp)
    207 {
    208 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    209 	Elf_Addr new_value;
    210 	const Elf_Sym  *def;
    211 	const Obj_Entry *defobj;
    212 	unsigned long info = rela->r_info;
    213 
    214 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    215 
    216 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    217 	if (__predict_false(def == NULL))
    218 		return -1;
    219 	if (__predict_false(def == &_rtld_sym_zero))
    220 		return 0;
    221 
    222 	assert(rela->r_addend == 0);
    223 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    224 		if (tp == NULL)
    225 			return 0;
    226 		new_value = _rtld_resolve_ifunc(defobj, def);
    227 	} else {
    228 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
    229 		    rela->r_addend);
    230 	}
    231 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    232 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    233 	if (*where != new_value)
    234 		*where = new_value;
    235 
    236 	if (tp)
    237 		*tp = new_value - rela->r_addend;
    238 
    239 	return 0;
    240 }
    241 
    242 caddr_t
    243 _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    244 {
    245 	const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
    246 	Elf_Addr result;
    247 	int err;
    248 
    249 	result = 0;	/* XXX gcc */
    250 
    251 	_rtld_shared_enter();
    252 	err = _rtld_relocate_plt_object(obj, rela, &result);
    253 	if (err)
    254 		_rtld_die();
    255 	_rtld_shared_exit();
    256 
    257 	return (caddr_t)result;
    258 }
    259 
    260 int
    261 _rtld_relocate_plt_objects(const Obj_Entry *obj)
    262 {
    263 	const Elf_Rela *rela;
    264 
    265 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
    266 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    267 			return -1;
    268 
    269 	return 0;
    270 }
    271