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