Home | History | Annotate | Line # | Download | only in vax
mdreloc.c revision 1.32
      1  1.32     joerg /*	$NetBSD: mdreloc.c,v 1.32 2017/06/19 11:57:02 joerg Exp $	*/
      2  1.20     skrll 
      3  1.20     skrll #include <sys/cdefs.h>
      4  1.20     skrll #ifndef lint
      5  1.32     joerg __RCSID("$NetBSD: mdreloc.c,v 1.32 2017/06/19 11:57:02 joerg Exp $");
      6  1.20     skrll #endif /* not lint */
      7  1.10  junyoung 
      8   1.1   mycroft #include <sys/types.h>
      9   1.1   mycroft 
     10   1.1   mycroft #include "debug.h"
     11   1.1   mycroft #include "rtld.h"
     12  1.11   mycroft 
     13  1.11   mycroft void _rtld_bind_start(void);
     14  1.15   mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     15  1.17     skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     16  1.19     skrll static inline int _rtld_relocate_plt_object(const Obj_Entry *,
     17  1.19     skrll     const Elf_Rela *, Elf_Addr *);
     18   1.1   mycroft 
     19   1.1   mycroft void
     20   1.1   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
     21   1.1   mycroft {
     22   1.1   mycroft 	obj->pltgot[1] = (Elf_Addr) obj;
     23   1.1   mycroft 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     24  1.15   mycroft }
     25  1.15   mycroft 
     26  1.15   mycroft void
     27  1.17     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     28  1.15   mycroft {
     29  1.15   mycroft 	const Elf_Rela *rela = 0, *relalim;
     30  1.15   mycroft 	Elf_Addr relasz = 0;
     31  1.15   mycroft 	Elf_Addr *where;
     32  1.15   mycroft 
     33  1.15   mycroft 	for (; dynp->d_tag != DT_NULL; dynp++) {
     34  1.15   mycroft 		switch (dynp->d_tag) {
     35  1.15   mycroft 		case DT_RELA:
     36  1.15   mycroft 			rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr);
     37  1.15   mycroft 			break;
     38  1.15   mycroft 		case DT_RELASZ:
     39  1.15   mycroft 			relasz = dynp->d_un.d_val;
     40  1.15   mycroft 			break;
     41  1.15   mycroft 		}
     42  1.15   mycroft 	}
     43  1.24     lukem 	relalim = (const Elf_Rela *)((const uint8_t *)rela + relasz);
     44  1.15   mycroft 	for (; rela < relalim; rela++) {
     45  1.15   mycroft 		where = (Elf_Addr *)(relocbase + rela->r_offset);
     46  1.15   mycroft 		*where = (Elf_Addr)(relocbase + rela->r_addend);
     47  1.15   mycroft 	}
     48   1.1   mycroft }
     49   1.2   mycroft 
     50   1.2   mycroft int
     51  1.27     joerg _rtld_relocate_nonplt_objects(Obj_Entry *obj)
     52   1.2   mycroft {
     53   1.3   mycroft 	const Elf_Rela *rela;
     54  1.32     joerg 	const Elf_Sym *def = NULL;
     55  1.32     joerg 	const Obj_Entry *defobj = NULL;
     56  1.32     joerg 	unsigned long last_symnum = ULONG_MAX;
     57   1.2   mycroft 
     58   1.3   mycroft 	for (rela = obj->rela; rela < obj->relalim; rela++) {
     59   1.3   mycroft 		Elf_Addr        *where;
     60   1.3   mycroft 		Elf_Addr         tmp;
     61   1.3   mycroft 
     62   1.3   mycroft 		where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
     63  1.32     joerg 
     64  1.32     joerg 		switch (ELF_R_TYPE(rela->r_info)) {
     65  1.32     joerg 		case R_TYPE(32):	/* word32 S + A */
     66  1.32     joerg 		case R_TYPE(GLOB_DAT):	/* word32 S + A */
     67  1.32     joerg 			symnum = ELF_R_SYM(rela->r_info);
     68  1.32     joerg 			if (last_symnum != symnum) {
     69  1.32     joerg 				last_symnum = symnum;
     70  1.32     joerg 				def = _rtld_find_symdef(symnum, obj, &defobj,
     71  1.32     joerg 				    false);
     72  1.32     joerg 				if (def == NULL)
     73  1.32     joerg 					return -1;
     74  1.32     joerg 			}
     75  1.32     joerg 			break;
     76  1.32     joerg 
     77  1.32     joerg 		default:
     78  1.32     joerg 			break;
     79  1.32     joerg 		}
     80   1.3   mycroft 
     81   1.3   mycroft 		switch (ELF_R_TYPE(rela->r_info)) {
     82   1.3   mycroft 		case R_TYPE(NONE):
     83   1.3   mycroft 			break;
     84   1.3   mycroft 
     85   1.3   mycroft 		case R_TYPE(32):	/* word32 S + A */
     86   1.3   mycroft 		case R_TYPE(GLOB_DAT):	/* word32 S + A */
     87   1.3   mycroft 			tmp = (Elf_Addr)(defobj->relocbase + def->st_value +
     88   1.3   mycroft 			    rela->r_addend);
     89   1.3   mycroft 
     90   1.3   mycroft 			if (*where != tmp)
     91   1.3   mycroft 				*where = tmp;
     92  1.12   mycroft 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
     93   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
     94   1.5   mycroft 			    obj->path, (void *)*where, defobj->path));
     95   1.3   mycroft 			break;
     96   1.3   mycroft 
     97   1.3   mycroft 		case R_TYPE(RELATIVE):	/* word32 B + A */
     98   1.3   mycroft 			tmp = (Elf_Addr)(obj->relocbase + rela->r_addend);
     99   1.3   mycroft 			if (*where != tmp)
    100   1.3   mycroft 				*where = tmp;
    101  1.12   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    102   1.3   mycroft 			    (void *)*where));
    103   1.3   mycroft 			break;
    104   1.3   mycroft 
    105   1.3   mycroft 		case R_TYPE(COPY):
    106   1.3   mycroft 			/*
    107   1.3   mycroft 			 * These are deferred until all other relocations have
    108   1.3   mycroft 			 * been done.  All we do here is make sure that the
    109   1.3   mycroft 			 * COPY relocation is not in a shared library.  They
    110   1.3   mycroft 			 * are allowed only in executable files.
    111   1.3   mycroft 			 */
    112   1.8   mycroft 			if (obj->isdynamic) {
    113   1.3   mycroft 				_rtld_error(
    114   1.2   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    115   1.3   mycroft 				    obj->path);
    116   1.3   mycroft 				return -1;
    117   1.3   mycroft 			}
    118  1.12   mycroft 			rdbg(("COPY (avoid in main)"));
    119   1.3   mycroft 			break;
    120   1.3   mycroft 
    121   1.3   mycroft 		default:
    122  1.12   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    123   1.3   mycroft 			    "addend = %p, contents = %p, symbol = %s",
    124  1.32     joerg 			    (u_long)ELF_R_SYM(rela->r_info),
    125  1.32     joerg 			    (u_long)ELF_R_TYPE(rela->r_info),
    126   1.3   mycroft 			    (void *)rela->r_offset, (void *)rela->r_addend,
    127   1.3   mycroft 			    (void *)*where,
    128   1.4   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    129   1.3   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    130  1.25      jmmv 			    "in non-PLT relocations",
    131   1.3   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rela->r_info));
    132   1.2   mycroft 			return -1;
    133   1.2   mycroft 		}
    134   1.2   mycroft 	}
    135   1.2   mycroft 	return 0;
    136   1.2   mycroft }
    137   1.6   mycroft 
    138   1.6   mycroft int
    139  1.17     skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    140   1.6   mycroft {
    141   1.6   mycroft 	const Elf_Rela *rela;
    142   1.6   mycroft 
    143  1.16   mycroft 	if (!obj->relocbase)
    144   1.6   mycroft 		return 0;
    145   1.6   mycroft 
    146   1.6   mycroft 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) {
    147   1.6   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    148   1.6   mycroft 
    149   1.6   mycroft 		assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT));
    150   1.6   mycroft 
    151   1.6   mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    152   1.6   mycroft 		*where += (Elf_Addr)obj->relocbase;
    153  1.29      matt 		rdbg(("lazy fixup pltgot %p in %s --> %p", where, obj->path,
    154  1.29      matt 		    (void *)*where));
    155   1.6   mycroft 	}
    156   1.6   mycroft 
    157   1.6   mycroft 	return 0;
    158   1.6   mycroft }
    159   1.6   mycroft 
    160  1.18     skrll static inline int
    161  1.18     skrll _rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp)
    162   1.6   mycroft {
    163   1.6   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    164   1.6   mycroft 	Elf_Addr new_value;
    165   1.6   mycroft 	const Elf_Sym  *def;
    166   1.6   mycroft 	const Obj_Entry *defobj;
    167  1.26  christos 	unsigned long info = rela->r_info;
    168   1.6   mycroft 
    169  1.26  christos 	assert(ELF_R_TYPE(info) == R_TYPE(JMP_SLOT));
    170   1.6   mycroft 
    171  1.26  christos 	def = _rtld_find_plt_symdef(ELF_R_SYM(info), obj, &defobj, tp != NULL);
    172  1.26  christos 	if (__predict_false(def == NULL))
    173  1.18     skrll 		return -1;
    174  1.26  christos 	if (__predict_false(def == &_rtld_sym_zero))
    175  1.26  christos 		return 0;
    176   1.6   mycroft 
    177  1.30     joerg 	if (ELF_ST_TYPE(def->st_info) == STT_GNU_IFUNC) {
    178  1.30     joerg 		if (tp == NULL)
    179  1.30     joerg 			return 0;
    180  1.30     joerg 		new_value = _rtld_resolve_ifunc(defobj, def);
    181  1.30     joerg 	} else {
    182  1.30     joerg 		new_value = (Elf_Addr)(defobj->relocbase + def->st_value +
    183  1.30     joerg 		    rela->r_addend);
    184  1.30     joerg 	}
    185  1.29      matt 	rdbg(("bind now/fixup pltgot %p in %s --> old=%p new=%p", where,
    186   1.6   mycroft 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    187   1.6   mycroft 	if (*where != new_value)
    188   1.6   mycroft 		*where = new_value;
    189   1.6   mycroft 
    190  1.18     skrll 	if (tp)
    191  1.18     skrll 		*tp = new_value - rela->r_addend;
    192  1.18     skrll 
    193  1.18     skrll 	return 0;
    194  1.18     skrll }
    195  1.18     skrll 
    196  1.18     skrll caddr_t
    197  1.18     skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    198  1.18     skrll {
    199  1.24     lukem 	const Elf_Rela *rela = (const Elf_Rela *)((const uint8_t *)obj->pltrela + reloff);
    200  1.18     skrll 	Elf_Addr result;
    201  1.18     skrll 	int err;
    202  1.18     skrll 
    203  1.21       mrg 	result = 0;	/* XXX gcc */
    204  1.21       mrg 
    205  1.28     joerg 	_rtld_shared_enter();
    206  1.18     skrll 	err = _rtld_relocate_plt_object(obj, rela, &result);
    207  1.26  christos 	if (err)
    208  1.18     skrll 		_rtld_die();
    209  1.28     joerg 	_rtld_shared_exit();
    210  1.18     skrll 
    211  1.18     skrll 	return (caddr_t)result;
    212  1.18     skrll }
    213  1.18     skrll 
    214  1.18     skrll int
    215  1.18     skrll _rtld_relocate_plt_objects(const Obj_Entry *obj)
    216  1.18     skrll {
    217  1.18     skrll 	const Elf_Rela *rela;
    218  1.18     skrll 
    219  1.18     skrll 	for (rela = obj->pltrela; rela < obj->pltrelalim; rela++)
    220  1.18     skrll 		if (_rtld_relocate_plt_object(obj, rela, NULL) < 0)
    221  1.18     skrll 			return -1;
    222  1.18     skrll 
    223  1.18     skrll 	return 0;
    224   1.6   mycroft }
    225