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