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