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