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