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