Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.16
      1  1.16   mycroft /*	$NetBSD: mdreloc.c,v 1.16 2002/09/25 07:27:52 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.14   mycroft _rtld_relocate_nonplt_objects(obj, self)
     48   1.7   mycroft 	const Obj_Entry *obj;
     49   1.9   mycroft 	bool self;
     50   1.2   mycroft {
     51   1.3   mycroft 	const Elf_Rel *rel;
     52   1.6   mycroft #define COMBRELOC
     53   1.4   mycroft #ifdef COMBRELOC
     54   1.4   mycroft 	unsigned long lastsym = -1;
     55   1.4   mycroft #endif
     56   1.4   mycroft 	Elf_Addr target;
     57   1.2   mycroft 
     58  1.12   mycroft 	if (self)
     59  1.12   mycroft 		return 0;
     60  1.12   mycroft 
     61   1.3   mycroft 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     62   1.3   mycroft 		Elf_Addr        *where;
     63   1.5   mycroft 		const Elf_Sym   *def;
     64   1.5   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 + rel->r_offset);
     69   1.4   mycroft 		symnum = ELF_R_SYM(rel->r_info);
     70   1.3   mycroft 
     71   1.3   mycroft 		switch (ELF_R_TYPE(rel->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 #ifdef COMBRELOC
     78   1.5   mycroft 			if (symnum != lastsym) {
     79   1.4   mycroft #endif
     80   1.4   mycroft 				def = _rtld_find_symdef(symnum, obj, &defobj,
     81   1.4   mycroft 				    false);
     82   1.4   mycroft 				if (def == NULL)
     83   1.4   mycroft 					return -1;
     84   1.4   mycroft 				target = (Elf_Addr)(defobj->relocbase +
     85   1.4   mycroft 				    def->st_value);
     86   1.4   mycroft #ifdef COMBRELOC
     87   1.4   mycroft 				lastsym = symnum;
     88   1.5   mycroft 			}
     89   1.4   mycroft #endif
     90   1.3   mycroft 
     91   1.4   mycroft 			*where += target - (Elf_Addr)where;
     92  1.14   mycroft 			rdbg(("PC32 %s in %s --> %p in %s",
     93   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
     94   1.5   mycroft 			    obj->path, (void *)*where, defobj->path));
     95   1.3   mycroft 			break;
     96   1.2   mycroft 
     97   1.3   mycroft 		case R_TYPE(GOT32):
     98   1.2   mycroft #endif
     99   1.3   mycroft 		case R_TYPE(32):
    100   1.3   mycroft 		case R_TYPE(GLOB_DAT):
    101   1.4   mycroft #ifdef COMBRELOC
    102   1.5   mycroft 			if (symnum != lastsym) {
    103   1.4   mycroft #endif
    104   1.4   mycroft 				def = _rtld_find_symdef(symnum, obj, &defobj,
    105   1.4   mycroft 				    false);
    106   1.4   mycroft 				if (def == NULL)
    107   1.4   mycroft 					return -1;
    108   1.4   mycroft 				target = (Elf_Addr)(defobj->relocbase +
    109   1.4   mycroft 				    def->st_value);
    110   1.4   mycroft #ifdef COMBRELOC
    111   1.4   mycroft 				lastsym = symnum;
    112   1.5   mycroft 			}
    113   1.4   mycroft #endif
    114   1.3   mycroft 
    115   1.4   mycroft 			tmp = target + *where;
    116   1.3   mycroft 			if (*where != tmp)
    117   1.3   mycroft 				*where = tmp;
    118  1.14   mycroft 			rdbg(("32/GLOB_DAT %s in %s --> %p in %s",
    119   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
    120   1.5   mycroft 			    obj->path, (void *)*where, defobj->path));
    121   1.3   mycroft 			break;
    122   1.3   mycroft 
    123   1.3   mycroft 		case R_TYPE(RELATIVE):
    124  1.12   mycroft 			*where += (Elf_Addr)obj->relocbase;
    125  1.14   mycroft 			rdbg(("RELATIVE in %s --> %p", obj->path,
    126  1.12   mycroft 			    (void *)*where));
    127   1.3   mycroft 			break;
    128   1.3   mycroft 
    129   1.3   mycroft 		case R_TYPE(COPY):
    130   1.3   mycroft 			/*
    131   1.3   mycroft 			 * These are deferred until all other relocations have
    132   1.3   mycroft 			 * been done.  All we do here is make sure that the
    133   1.3   mycroft 			 * COPY relocation is not in a shared library.  They
    134   1.3   mycroft 			 * are allowed only in executable files.
    135   1.3   mycroft 			 */
    136   1.8   mycroft 			if (obj->isdynamic) {
    137   1.3   mycroft 				_rtld_error(
    138   1.2   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    139   1.3   mycroft 				    obj->path);
    140   1.3   mycroft 				return -1;
    141   1.3   mycroft 			}
    142  1.14   mycroft 			rdbg(("COPY (avoid in main)"));
    143   1.3   mycroft 			break;
    144   1.3   mycroft 
    145   1.3   mycroft 		default:
    146  1.14   mycroft 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    147   1.3   mycroft 			    "contents = %p, symbol = %s",
    148   1.4   mycroft 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    149   1.3   mycroft 			    (void *)rel->r_offset, (void *)*where,
    150   1.4   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    151   1.3   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    152   1.3   mycroft 			    "in non-PLT relocations\n",
    153   1.3   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    154   1.2   mycroft 			return -1;
    155   1.2   mycroft 		}
    156   1.2   mycroft 	}
    157   1.6   mycroft 	return 0;
    158   1.6   mycroft }
    159   1.6   mycroft 
    160   1.6   mycroft int
    161  1.14   mycroft _rtld_relocate_plt_lazy(obj)
    162   1.7   mycroft 	const Obj_Entry *obj;
    163   1.6   mycroft {
    164   1.6   mycroft 	const Elf_Rel *rel;
    165   1.6   mycroft 
    166   1.8   mycroft 	if (!obj->isdynamic)
    167   1.6   mycroft 		return 0;
    168   1.6   mycroft 
    169   1.6   mycroft 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    170   1.6   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    171   1.6   mycroft 
    172   1.6   mycroft 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    173   1.6   mycroft 
    174   1.6   mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    175   1.6   mycroft 		*where += (Elf_Addr)obj->relocbase;
    176  1.14   mycroft 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    177   1.6   mycroft 	}
    178   1.6   mycroft 
    179   1.6   mycroft 	return 0;
    180   1.6   mycroft }
    181   1.6   mycroft 
    182  1.16   mycroft caddr_t
    183  1.16   mycroft _rtld_bind(obj, reloff)
    184   1.7   mycroft 	const Obj_Entry *obj;
    185  1.16   mycroft 	Elf_Word reloff;
    186   1.6   mycroft {
    187  1.16   mycroft 	const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
    188  1.16   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    189   1.6   mycroft 	Elf_Addr new_value;
    190   1.6   mycroft 	const Elf_Sym  *def;
    191   1.6   mycroft 	const Obj_Entry *defobj;
    192   1.6   mycroft 
    193  1.16   mycroft 	assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    194   1.6   mycroft 
    195  1.16   mycroft 	def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
    196   1.6   mycroft 	if (def == NULL)
    197  1.16   mycroft 		_rtld_die();
    198   1.6   mycroft 
    199   1.6   mycroft 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    200  1.14   mycroft 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    201   1.6   mycroft 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    202   1.6   mycroft 	if (*where != new_value)
    203   1.6   mycroft 		*where = new_value;
    204   1.6   mycroft 
    205  1.16   mycroft 	return (caddr_t)new_value;
    206  1.15  junyoung }
    207  1.15  junyoung 
    208  1.15  junyoung int
    209  1.15  junyoung _rtld_relocate_plt_objects(const Obj_Entry *obj)
    210  1.15  junyoung {
    211  1.15  junyoung 	const Elf_Rel *rel;
    212  1.15  junyoung 
    213  1.15  junyoung 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    214  1.15  junyoung 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    215  1.15  junyoung 		Elf_Addr target;
    216  1.15  junyoung 		const Elf_Sym *def;
    217  1.15  junyoung 		const Obj_Entry *defobj;
    218  1.15  junyoung 
    219  1.15  junyoung 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JMP_SLOT));
    220  1.15  junyoung 
    221  1.15  junyoung 		def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
    222  1.15  junyoung 		    true);
    223  1.15  junyoung 		if (def == NULL)
    224  1.15  junyoung 			return -1;
    225  1.15  junyoung 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
    226  1.15  junyoung 		rdbg(("bind now/fixup in %s --> old=%p new=%p",
    227  1.15  junyoung 		    defobj->strtab + def->st_name, (void *)*where,
    228  1.15  junyoung 		    (void *)target));
    229  1.15  junyoung 		if (*where != target)
    230  1.15  junyoung 			*where = target;
    231  1.15  junyoung 	}
    232   1.2   mycroft 	return 0;
    233   1.2   mycroft }
    234