Home | History | Annotate | Line # | Download | only in arm
mdreloc.c revision 1.11
      1  1.11  junyoung /*	$NetBSD: mdreloc.c,v 1.11 2002/09/11 14:19:30 junyoung 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.1   mycroft void
     10   1.1   mycroft _rtld_setup_pltgot(const Obj_Entry *obj)
     11   1.1   mycroft {
     12   1.1   mycroft 	obj->pltgot[1] = (Elf_Addr) obj;
     13   1.1   mycroft 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     14   1.1   mycroft }
     15   1.2   mycroft 
     16   1.2   mycroft int
     17   1.9   mycroft _rtld_relocate_nonplt_objects(obj, self, dodebug)
     18   1.7   mycroft 	const Obj_Entry *obj;
     19   1.9   mycroft 	bool self;
     20   1.2   mycroft 	bool dodebug;
     21   1.2   mycroft {
     22   1.3   mycroft 	const Elf_Rel *rel;
     23   1.2   mycroft 
     24   1.3   mycroft 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     25   1.3   mycroft 		Elf_Addr        *where;
     26   1.3   mycroft 		const Elf_Sym   *def;
     27   1.3   mycroft 		const Obj_Entry *defobj;
     28   1.3   mycroft 		Elf_Addr         tmp;
     29   1.4   mycroft 		unsigned long	 symnum;
     30   1.3   mycroft 
     31   1.3   mycroft 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     32   1.4   mycroft 		symnum = ELF_R_SYM(rel->r_info);
     33   1.3   mycroft 
     34   1.3   mycroft 		switch (ELF_R_TYPE(rel->r_info)) {
     35   1.3   mycroft 		case R_TYPE(NONE):
     36   1.3   mycroft 			break;
     37   1.2   mycroft 
     38   1.2   mycroft #if 1 /* XXX should not occur */
     39   1.3   mycroft 		case R_TYPE(PC24): {	/* word32 S - P + A */
     40   1.3   mycroft 			Elf32_Sword addend;
     41   1.2   mycroft 
     42   1.3   mycroft 			/*
     43   1.3   mycroft 			 * Extract addend and sign-extend if needed.
     44   1.3   mycroft 			 */
     45   1.3   mycroft 			addend = *where;
     46   1.3   mycroft 			if (addend & 0x00800000)
     47   1.3   mycroft 				addend |= 0xff000000;
     48   1.3   mycroft 
     49   1.4   mycroft 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     50   1.3   mycroft 			if (def == NULL)
     51   1.3   mycroft 				return -1;
     52   1.3   mycroft 			tmp = (Elf_Addr)obj->relocbase + def->st_value
     53   1.3   mycroft 			    - (Elf_Addr)where + (addend << 2);
     54   1.3   mycroft 			if ((tmp & 0xfe000000) != 0xfe000000 &&
     55   1.3   mycroft 			    (tmp & 0xfe000000) != 0) {
     56   1.3   mycroft 				_rtld_error(
     57   1.3   mycroft 				"%s: R_ARM_PC24 relocation @ %p to %s failed "
     58   1.3   mycroft 				"(displacement %ld (%#lx) out of range)",
     59   1.3   mycroft 				    obj->path, where,
     60   1.5   mycroft 				    obj->strtab + obj->symtab[symnum].st_name,
     61   1.5   mycroft 				    (long) tmp, (long) tmp);
     62   1.3   mycroft 				return -1;
     63   1.3   mycroft 			}
     64   1.3   mycroft 			tmp >>= 2;
     65   1.3   mycroft 			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
     66   1.3   mycroft 			rdbg(dodebug, ("PC24 %s in %s --> %p @ %p in %s",
     67   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
     68   1.5   mycroft 			    obj->path, (void *)*where, where, defobj->path));
     69   1.3   mycroft 			break;
     70   1.2   mycroft 		}
     71   1.2   mycroft #endif
     72   1.2   mycroft 
     73   1.3   mycroft 		case R_TYPE(ABS32):	/* word32 B + S + A */
     74   1.4   mycroft 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     75   1.3   mycroft 			if (def == NULL)
     76   1.3   mycroft 				return -1;
     77   1.3   mycroft 			*where += (Elf_Addr)defobj->relocbase + def->st_value;
     78   1.3   mycroft 			rdbg(dodebug, ("ABS32 %s in %s --> %p @ %p in %s",
     79   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
     80   1.5   mycroft 			    obj->path, (void *)*where, where, defobj->path));
     81   1.3   mycroft 			break;
     82   1.3   mycroft 
     83   1.3   mycroft 		case R_TYPE(GLOB_DAT):	/* word32 B + S */
     84   1.4   mycroft 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
     85   1.3   mycroft 			if (def == NULL)
     86   1.3   mycroft 				return -1;
     87   1.3   mycroft 			*where = (Elf_Addr)(defobj->relocbase + def->st_value);
     88   1.3   mycroft 			rdbg(dodebug, ("GLOB_DAT %s in %s --> %p @ %p in %s",
     89   1.5   mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
     90   1.5   mycroft 			    obj->path, (void *)*where, where, defobj->path));
     91   1.3   mycroft 			break;
     92   1.3   mycroft 
     93   1.3   mycroft 		case R_TYPE(RELATIVE):	/* word32 B + A */
     94  1.10   mycroft 		    {
     95  1.10   mycroft 			extern Elf_Addr	_GLOBAL_OFFSET_TABLE_[];
     96  1.10   mycroft 			extern Elf_Addr	_GOT_END_[];
     97  1.10   mycroft 
     98  1.10   mycroft 			/* This is the ...iffy hueristic. */
     99  1.10   mycroft 			if (!self ||
    100  1.10   mycroft 			    (caddr_t)where < (caddr_t)_GLOBAL_OFFSET_TABLE_ ||
    101  1.10   mycroft 			    (caddr_t)where >= (caddr_t)_GOT_END_) {
    102  1.10   mycroft 				*where += (Elf_Addr)obj->relocbase;
    103  1.10   mycroft 				rdbg(dodebug, ("RELATIVE in %s --> %p",
    104  1.10   mycroft 				    obj->path, (void *)*where));
    105  1.10   mycroft 			} else
    106  1.10   mycroft 				rdbg(dodebug, ("RELATIVE in %s stays at %p",
    107  1.10   mycroft 				    obj->path, (void *)*where));
    108   1.3   mycroft 			break;
    109  1.10   mycroft 		    }
    110   1.3   mycroft 
    111   1.3   mycroft 		case R_TYPE(COPY):
    112   1.3   mycroft 			/*
    113   1.3   mycroft 			 * These are deferred until all other relocations have
    114   1.3   mycroft 			 * been done.  All we do here is make sure that the
    115   1.3   mycroft 			 * COPY relocation is not in a shared library.  They
    116   1.3   mycroft 			 * are allowed only in executable files.
    117   1.3   mycroft 			 */
    118   1.8   mycroft 			if (obj->isdynamic) {
    119   1.3   mycroft 				_rtld_error(
    120   1.2   mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    121   1.3   mycroft 				    obj->path);
    122   1.3   mycroft 				return -1;
    123   1.3   mycroft 			}
    124   1.3   mycroft 			rdbg(dodebug, ("COPY (avoid in main)"));
    125   1.3   mycroft 			break;
    126   1.3   mycroft 
    127   1.3   mycroft 		default:
    128   1.3   mycroft 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    129   1.3   mycroft 			    "contents = %p, symbol = %s",
    130   1.4   mycroft 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    131   1.3   mycroft 			    (void *)rel->r_offset, (void *)*where,
    132   1.4   mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    133   1.3   mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    134   1.3   mycroft 			    "in non-PLT relocations\n",
    135   1.3   mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    136   1.2   mycroft 			return -1;
    137   1.2   mycroft 		}
    138   1.2   mycroft 	}
    139   1.2   mycroft 	return 0;
    140   1.2   mycroft }
    141   1.6   mycroft 
    142   1.6   mycroft int
    143   1.6   mycroft _rtld_relocate_plt_lazy(obj, dodebug)
    144   1.7   mycroft 	const Obj_Entry *obj;
    145   1.6   mycroft 	bool dodebug;
    146   1.6   mycroft {
    147   1.6   mycroft 	const Elf_Rel *rel;
    148   1.6   mycroft 
    149   1.8   mycroft 	if (!obj->isdynamic)
    150   1.6   mycroft 		return 0;
    151   1.6   mycroft 
    152   1.6   mycroft 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    153   1.6   mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    154   1.6   mycroft 
    155   1.6   mycroft 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    156   1.6   mycroft 
    157   1.6   mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    158   1.6   mycroft 		*where += (Elf_Addr)obj->relocbase;
    159   1.6   mycroft 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    160   1.6   mycroft 		    (void *)*where));
    161   1.6   mycroft 	}
    162   1.6   mycroft 
    163   1.6   mycroft 	return 0;
    164   1.6   mycroft }
    165   1.6   mycroft 
    166   1.6   mycroft int
    167   1.6   mycroft _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
    168   1.7   mycroft 	const Obj_Entry *obj;
    169   1.6   mycroft 	const Elf_Rela *rela;
    170   1.6   mycroft 	caddr_t *addrp;
    171   1.6   mycroft 	bool dodebug;
    172   1.6   mycroft {
    173   1.6   mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    174   1.6   mycroft 	Elf_Addr new_value;
    175   1.6   mycroft 	const Elf_Sym  *def;
    176   1.6   mycroft 	const Obj_Entry *defobj;
    177   1.6   mycroft 
    178   1.6   mycroft 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    179   1.6   mycroft 
    180   1.6   mycroft 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    181   1.6   mycroft 	if (def == NULL)
    182   1.6   mycroft 		return -1;
    183   1.6   mycroft 
    184   1.6   mycroft 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    185   1.6   mycroft 	rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
    186   1.6   mycroft 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    187   1.6   mycroft 	if (*where != new_value)
    188   1.6   mycroft 		*where = new_value;
    189   1.6   mycroft 
    190   1.6   mycroft 	*addrp = (caddr_t)new_value;
    191   1.6   mycroft 	return 0;
    192   1.6   mycroft }
    193