Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.23
      1  1.23      matt /*	$NetBSD: mdreloc.c,v 1.23 2007/02/23 01:17:11 matt Exp $	*/
      2  1.21     skrll 
      3  1.21     skrll #include <sys/cdefs.h>
      4  1.21     skrll #ifndef lint
      5  1.23      matt __RCSID("$NetBSD: mdreloc.c,v 1.23 2007/02/23 01:17:11 matt Exp $");
      6  1.21     skrll #endif /* not lint */
      7  1.11  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.13   mycroft void _rtld_bind_start(void);
     15  1.12   mycroft void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     16  1.19     skrll caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     17  1.12   mycroft 
     18   1.1   mycroft void
     19   1.1   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
     20   1.1   mycroft {
     21   1.1   mycroft 	obj->pltgot[1] = (Elf_Addr) obj;
     22   1.1   mycroft 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     23   1.1   mycroft }
     24   1.2   mycroft 
     25  1.12   mycroft void
     26  1.19     skrll _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     27  1.12   mycroft {
     28  1.12   mycroft 	const Elf_Rel *rel = 0, *rellim;
     29  1.12   mycroft 	Elf_Addr relsz = 0;
     30  1.12   mycroft 	Elf_Addr *where;
     31  1.12   mycroft 
     32  1.12   mycroft 	for (; dynp->d_tag != DT_NULL; dynp++) {
     33  1.12   mycroft 		switch (dynp->d_tag) {
     34  1.12   mycroft 		case DT_REL:
     35  1.12   mycroft 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     36  1.12   mycroft 			break;
     37  1.12   mycroft 		case DT_RELSZ:
     38  1.12   mycroft 			relsz = dynp->d_un.d_val;
     39  1.12   mycroft 			break;
     40  1.12   mycroft 		}
     41  1.12   mycroft 	}
     42  1.22  christos 	if (rel == 0 || relsz == 0)
     43  1.22  christos 		return;
     44  1.12   mycroft 	rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
     45  1.12   mycroft 	for (; rel < rellim; rel++) {
     46  1.12   mycroft 		where = (Elf_Addr *)(relocbase + rel->r_offset);
     47  1.12   mycroft 		*where += (Elf_Addr)relocbase;
     48  1.12   mycroft 	}
     49  1.12   mycroft }
     50  1.12   mycroft 
     51   1.2   mycroft int
     52  1.19     skrll _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
     53   1.2   mycroft {
     54   1.3   mycroft 	const Elf_Rel *rel;
     55  1.20     lukem 	Elf_Addr target = 0;
     56  1.12   mycroft 
     57   1.3   mycroft 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     58   1.3   mycroft 		Elf_Addr        *where;
     59   1.5   mycroft 		const Elf_Sym   *def;
     60   1.5   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 + rel->r_offset);
     65   1.4   mycroft 		symnum = ELF_R_SYM(rel->r_info);
     66   1.3   mycroft 
     67   1.3   mycroft 		switch (ELF_R_TYPE(rel->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.23      matt 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     74  1.23      matt 			if (def == NULL)
     75  1.23      matt 				return -1;
     76  1.23      matt 			target = (Elf_Addr)(defobj->relocbase + def->st_value);
     77   1.3   mycroft 
     78   1.4   mycroft 			*where += target - (Elf_Addr)where;
     79  1.14   mycroft 			rdbg(("PC32 %s in %s --> %p in %s",
     80   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
     81   1.5   mycroft 			    obj->path, (void *)*where, defobj->path));
     82   1.3   mycroft 			break;
     83   1.2   mycroft 
     84   1.3   mycroft 		case R_TYPE(GOT32):
     85   1.2   mycroft #endif
     86   1.3   mycroft 		case R_TYPE(32):
     87   1.3   mycroft 		case R_TYPE(GLOB_DAT):
     88  1.23      matt 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     89  1.23      matt 			if (def == NULL)
     90  1.23      matt 				return -1;
     91  1.23      matt 			target = (Elf_Addr)(defobj->relocbase + def->st_value);
     92   1.3   mycroft 
     93   1.4   mycroft 			tmp = target + *where;
     94   1.3   mycroft 			if (*where != tmp)
     95   1.3   mycroft 				*where = tmp;
     96  1.14   mycroft 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
     97   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
     98   1.5   mycroft 			    obj->path, (void *)*where, defobj->path));
     99   1.3   mycroft 			break;
    100   1.3   mycroft 
    101   1.3   mycroft 		case R_TYPE(RELATIVE):
    102  1.12   mycroft 			*where += (Elf_Addr)obj->relocbase;
    103  1.14   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    104  1.12   mycroft 			    (void *)*where));
    105   1.3   mycroft 			break;
    106   1.3   mycroft 
    107   1.3   mycroft 		case R_TYPE(COPY):
    108   1.3   mycroft 			/*
    109   1.3   mycroft 			 * These are deferred until all other relocations have
    110   1.3   mycroft 			 * been done.  All we do here is make sure that the
    111   1.3   mycroft 			 * COPY relocation is not in a shared library.  They
    112   1.3   mycroft 			 * are allowed only in executable files.
    113   1.3   mycroft 			 */
    114   1.8   mycroft 			if (obj->isdynamic) {
    115   1.3   mycroft 				_rtld_error(
    116   1.2   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    117   1.3   mycroft 				    obj->path);
    118   1.3   mycroft 				return -1;
    119   1.3   mycroft 			}
    120  1.14   mycroft 			rdbg(("COPY (avoid in main)"));
    121   1.3   mycroft 			break;
    122   1.3   mycroft 
    123   1.3   mycroft 		default:
    124  1.14   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    125   1.3   mycroft 			    "contents = %p, symbol = %s",
    126   1.4   mycroft 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    127   1.3   mycroft 			    (void *)rel->r_offset, (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.3   mycroft 			    "in non-PLT relocations\n",
    131   1.3   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    132   1.2   mycroft 			return -1;
    133   1.2   mycroft 		}
    134   1.2   mycroft 	}
    135   1.6   mycroft 	return 0;
    136   1.6   mycroft }
    137   1.6   mycroft 
    138   1.6   mycroft int
    139  1.19     skrll _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    140   1.6   mycroft {
    141   1.6   mycroft 	const Elf_Rel *rel;
    142   1.6   mycroft 
    143  1.18   mycroft 	if (!obj->relocbase)
    144   1.6   mycroft 		return 0;
    145   1.6   mycroft 
    146   1.6   mycroft 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    147   1.6   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    148   1.6   mycroft 
    149   1.6   mycroft 		assert(ELF_R_TYPE(rel->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.14   mycroft 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    154   1.6   mycroft 	}
    155   1.6   mycroft 
    156   1.6   mycroft 	return 0;
    157   1.6   mycroft }
    158   1.6   mycroft 
    159  1.16   mycroft caddr_t
    160  1.19     skrll _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    161   1.6   mycroft {
    162  1.16   mycroft 	const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
    163  1.16   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->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.6   mycroft 
    168  1.16   mycroft 	assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    169   1.6   mycroft 
    170  1.16   mycroft 	def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
    171   1.6   mycroft 	if (def == NULL)
    172  1.16   mycroft 		_rtld_die();
    173   1.6   mycroft 
    174   1.6   mycroft 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    175  1.14   mycroft 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    176   1.6   mycroft 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    177   1.6   mycroft 	if (*where != new_value)
    178   1.6   mycroft 		*where = new_value;
    179   1.6   mycroft 
    180  1.16   mycroft 	return (caddr_t)new_value;
    181  1.15  junyoung }
    182  1.15  junyoung 
    183  1.15  junyoung int
    184  1.15  junyoung _rtld_relocate_plt_objects(const Obj_Entry *obj)
    185  1.15  junyoung {
    186  1.15  junyoung 	const Elf_Rel *rel;
    187  1.15  junyoung 
    188  1.15  junyoung 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    189  1.15  junyoung 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    190  1.15  junyoung 		Elf_Addr target;
    191  1.15  junyoung 		const Elf_Sym *def;
    192  1.15  junyoung 		const Obj_Entry *defobj;
    193  1.15  junyoung 
    194  1.15  junyoung 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    195  1.15  junyoung 
    196  1.15  junyoung 		def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
    197  1.15  junyoung 		    true);
    198  1.15  junyoung 		if (def == NULL)
    199  1.15  junyoung 			return -1;
    200  1.15  junyoung 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
    201  1.15  junyoung 		rdbg(("bind now/fixup in %s --> old=%p new=%p",
    202  1.15  junyoung 		    defobj->strtab + def->st_name, (void *)*where,
    203  1.15  junyoung 		    (void *)target));
    204  1.15  junyoung 		if (*where != target)
    205  1.15  junyoung 			*where = target;
    206  1.15  junyoung 	}
    207   1.2   mycroft 	return 0;
    208   1.2   mycroft }
    209