Home | History | Annotate | Line # | Download | only in arm
mdreloc.c revision 1.23.4.2
      1  1.23.4.2  tron /*	$NetBSD: mdreloc.c,v 1.23.4.2 2004/05/28 08:31:22 tron Exp $	*/
      2  1.23.4.2  tron 
      3  1.23.4.2  tron #include <sys/types.h>
      4  1.23.4.2  tron #include <sys/stat.h>
      5  1.23.4.2  tron 
      6  1.23.4.2  tron #include <string.h>
      7  1.23.4.2  tron 
      8  1.23.4.2  tron #include "debug.h"
      9  1.23.4.2  tron #include "rtld.h"
     10  1.23.4.2  tron 
     11  1.23.4.2  tron void _rtld_bind_start(void);
     12  1.23.4.2  tron void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr);
     13  1.23.4.2  tron caddr_t _rtld_bind(const Obj_Entry *, Elf_Word);
     14  1.23.4.2  tron 
     15  1.23.4.2  tron void
     16  1.23.4.2  tron _rtld_setup_pltgot(const Obj_Entry *obj)
     17  1.23.4.2  tron {
     18  1.23.4.2  tron 	obj->pltgot[1] = (Elf_Addr) obj;
     19  1.23.4.2  tron 	obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start;
     20  1.23.4.2  tron }
     21  1.23.4.2  tron 
     22  1.23.4.2  tron void
     23  1.23.4.2  tron _rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase)
     24  1.23.4.2  tron {
     25  1.23.4.2  tron 	const Elf_Rel *rel = 0, *rellim;
     26  1.23.4.2  tron 	Elf_Addr relsz = 0;
     27  1.23.4.2  tron 	Elf_Addr *where;
     28  1.23.4.2  tron 
     29  1.23.4.2  tron 	for (; dynp->d_tag != DT_NULL; dynp++) {
     30  1.23.4.2  tron 		switch (dynp->d_tag) {
     31  1.23.4.2  tron 		case DT_REL:
     32  1.23.4.2  tron 			rel = (const Elf_Rel *)(relocbase + dynp->d_un.d_ptr);
     33  1.23.4.2  tron 			break;
     34  1.23.4.2  tron 		case DT_RELSZ:
     35  1.23.4.2  tron 			relsz = dynp->d_un.d_val;
     36  1.23.4.2  tron 			break;
     37  1.23.4.2  tron 		}
     38  1.23.4.2  tron 	}
     39  1.23.4.2  tron 	rellim = (const Elf_Rel *)((caddr_t)rel + relsz);
     40  1.23.4.2  tron 	for (; rel < rellim; rel++) {
     41  1.23.4.2  tron 		where = (Elf_Addr *)(relocbase + rel->r_offset);
     42  1.23.4.2  tron 		*where += (Elf_Addr)relocbase;
     43  1.23.4.2  tron 	}
     44  1.23.4.2  tron }
     45  1.23.4.2  tron 
     46  1.23.4.2  tron /*
     47  1.23.4.2  tron  * It is possible for the compiler to emit relocations for unaligned data.
     48  1.23.4.2  tron  * We handle this situation with these inlines.
     49  1.23.4.2  tron  */
     50  1.23.4.2  tron #define	RELOC_ALIGNED_P(x) \
     51  1.23.4.2  tron 	(((uintptr_t)(x) & (sizeof(void *) - 1)) == 0)
     52  1.23.4.2  tron 
     53  1.23.4.2  tron static __inline Elf_Addr
     54  1.23.4.2  tron load_ptr(void *where)
     55  1.23.4.2  tron {
     56  1.23.4.2  tron 	Elf_Addr res;
     57  1.23.4.2  tron 
     58  1.23.4.2  tron 	memcpy(&res, where, sizeof(res));
     59  1.23.4.2  tron 
     60  1.23.4.2  tron 	return (res);
     61  1.23.4.2  tron }
     62  1.23.4.2  tron 
     63  1.23.4.2  tron static __inline void
     64  1.23.4.2  tron store_ptr(void *where, Elf_Addr val)
     65  1.23.4.2  tron {
     66  1.23.4.2  tron 
     67  1.23.4.2  tron 	memcpy(where, &val, sizeof(val));
     68  1.23.4.2  tron }
     69  1.23.4.2  tron 
     70  1.23.4.2  tron int
     71  1.23.4.2  tron _rtld_relocate_nonplt_objects(const Obj_Entry *obj)
     72  1.23.4.2  tron {
     73  1.23.4.2  tron 	const Elf_Rel *rel;
     74  1.23.4.2  tron 
     75  1.23.4.2  tron 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     76  1.23.4.2  tron 		Elf_Addr        *where;
     77  1.23.4.2  tron 		const Elf_Sym   *def;
     78  1.23.4.2  tron 		const Obj_Entry *defobj;
     79  1.23.4.2  tron 		Elf_Addr         tmp;
     80  1.23.4.2  tron 		unsigned long	 symnum;
     81  1.23.4.2  tron 
     82  1.23.4.2  tron 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     83  1.23.4.2  tron 		symnum = ELF_R_SYM(rel->r_info);
     84  1.23.4.2  tron 
     85  1.23.4.2  tron 		switch (ELF_R_TYPE(rel->r_info)) {
     86  1.23.4.2  tron 		case R_TYPE(NONE):
     87  1.23.4.2  tron 			break;
     88  1.23.4.2  tron 
     89  1.23.4.2  tron #if 1 /* XXX should not occur */
     90  1.23.4.2  tron 		case R_TYPE(PC24): {	/* word32 S - P + A */
     91  1.23.4.2  tron 			Elf32_Sword addend;
     92  1.23.4.2  tron 
     93  1.23.4.2  tron 			/*
     94  1.23.4.2  tron 			 * Extract addend and sign-extend if needed.
     95  1.23.4.2  tron 			 */
     96  1.23.4.2  tron 			addend = *where;
     97  1.23.4.2  tron 			if (addend & 0x00800000)
     98  1.23.4.2  tron 				addend |= 0xff000000;
     99  1.23.4.2  tron 
    100  1.23.4.2  tron 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    101  1.23.4.2  tron 			if (def == NULL)
    102  1.23.4.2  tron 				return -1;
    103  1.23.4.2  tron 			tmp = (Elf_Addr)obj->relocbase + def->st_value
    104  1.23.4.2  tron 			    - (Elf_Addr)where + (addend << 2);
    105  1.23.4.2  tron 			if ((tmp & 0xfe000000) != 0xfe000000 &&
    106  1.23.4.2  tron 			    (tmp & 0xfe000000) != 0) {
    107  1.23.4.2  tron 				_rtld_error(
    108  1.23.4.2  tron 				"%s: R_ARM_PC24 relocation @ %p to %s failed "
    109  1.23.4.2  tron 				"(displacement %ld (%#lx) out of range)",
    110  1.23.4.2  tron 				    obj->path, where,
    111  1.23.4.2  tron 				    obj->strtab + obj->symtab[symnum].st_name,
    112  1.23.4.2  tron 				    (long) tmp, (long) tmp);
    113  1.23.4.2  tron 				return -1;
    114  1.23.4.2  tron 			}
    115  1.23.4.2  tron 			tmp >>= 2;
    116  1.23.4.2  tron 			*where = (*where & 0xff000000) | (tmp & 0x00ffffff);
    117  1.23.4.2  tron 			rdbg(("PC24 %s in %s --> %p @ %p in %s",
    118  1.23.4.2  tron 			    obj->strtab + obj->symtab[symnum].st_name,
    119  1.23.4.2  tron 			    obj->path, (void *)*where, where, defobj->path));
    120  1.23.4.2  tron 			break;
    121  1.23.4.2  tron 		}
    122  1.23.4.2  tron #endif
    123  1.23.4.2  tron 
    124  1.23.4.2  tron 		case R_TYPE(ABS32):	/* word32 B + S + A */
    125  1.23.4.2  tron 		case R_TYPE(GLOB_DAT):	/* word32 B + S */
    126  1.23.4.2  tron 			def = _rtld_find_symdef(symnum, obj, &defobj, false);
    127  1.23.4.2  tron 			if (def == NULL)
    128  1.23.4.2  tron 				return -1;
    129  1.23.4.2  tron 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    130  1.23.4.2  tron 				tmp = *where + (Elf_Addr)defobj->relocbase +
    131  1.23.4.2  tron 				    def->st_value;
    132  1.23.4.2  tron 				*where = tmp;
    133  1.23.4.2  tron 			} else {
    134  1.23.4.2  tron 				tmp = load_ptr(where) +
    135  1.23.4.2  tron 				    (Elf_Addr)defobj->relocbase +
    136  1.23.4.2  tron 				    def->st_value;
    137  1.23.4.2  tron 				store_ptr(where, tmp);
    138  1.23.4.2  tron 			}
    139  1.23.4.2  tron 			rdbg(("ABS32/GLOB_DAT %s in %s --> %p @ %p in %s",
    140  1.23.4.2  tron 			    obj->strtab + obj->symtab[symnum].st_name,
    141  1.23.4.2  tron 			    obj->path, (void *)tmp, where, defobj->path));
    142  1.23.4.2  tron 			break;
    143  1.23.4.2  tron 
    144  1.23.4.2  tron 		case R_TYPE(RELATIVE):	/* word32 B + A */
    145  1.23.4.2  tron 			if (__predict_true(RELOC_ALIGNED_P(where))) {
    146  1.23.4.2  tron 				tmp = *where + (Elf_Addr)obj->relocbase;
    147  1.23.4.2  tron 				*where = tmp;
    148  1.23.4.2  tron 			} else {
    149  1.23.4.2  tron 				tmp = load_ptr(where) +
    150  1.23.4.2  tron 				    (Elf_Addr)obj->relocbase;
    151  1.23.4.2  tron 				store_ptr(where, tmp);
    152  1.23.4.2  tron 			}
    153  1.23.4.2  tron 			rdbg(("RELATIVE in %s --> %p", obj->path,
    154  1.23.4.2  tron 			    (void *)tmp));
    155  1.23.4.2  tron 			break;
    156  1.23.4.2  tron 
    157  1.23.4.2  tron 		case R_TYPE(COPY):
    158  1.23.4.2  tron 			/*
    159  1.23.4.2  tron 			 * These are deferred until all other relocations have
    160  1.23.4.2  tron 			 * been done.  All we do here is make sure that the
    161  1.23.4.2  tron 			 * COPY relocation is not in a shared library.  They
    162  1.23.4.2  tron 			 * are allowed only in executable files.
    163  1.23.4.2  tron 			 */
    164  1.23.4.2  tron 			if (obj->isdynamic) {
    165  1.23.4.2  tron 				_rtld_error(
    166  1.23.4.2  tron 			"%s: Unexpected R_COPY relocation in shared library",
    167  1.23.4.2  tron 				    obj->path);
    168  1.23.4.2  tron 				return -1;
    169  1.23.4.2  tron 			}
    170  1.23.4.2  tron 			rdbg(("COPY (avoid in main)"));
    171  1.23.4.2  tron 			break;
    172  1.23.4.2  tron 
    173  1.23.4.2  tron 		default:
    174  1.23.4.2  tron 			rdbg(("sym = %lu, type = %lu, offset = %p, "
    175  1.23.4.2  tron 			    "contents = %p, symbol = %s",
    176  1.23.4.2  tron 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    177  1.23.4.2  tron 			    (void *)rel->r_offset, (void *)load_ptr(where),
    178  1.23.4.2  tron 			    obj->strtab + obj->symtab[symnum].st_name));
    179  1.23.4.2  tron 			_rtld_error("%s: Unsupported relocation type %ld "
    180  1.23.4.2  tron 			    "in non-PLT relocations\n",
    181  1.23.4.2  tron 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    182  1.23.4.2  tron 			return -1;
    183  1.23.4.2  tron 		}
    184  1.23.4.2  tron 	}
    185  1.23.4.2  tron 	return 0;
    186  1.23.4.2  tron }
    187  1.23.4.2  tron 
    188  1.23.4.2  tron int
    189  1.23.4.2  tron _rtld_relocate_plt_lazy(const Obj_Entry *obj)
    190  1.23.4.2  tron {
    191  1.23.4.2  tron 	const Elf_Rel *rel;
    192  1.23.4.2  tron 
    193  1.23.4.2  tron 	if (!obj->relocbase)
    194  1.23.4.2  tron 		return 0;
    195  1.23.4.2  tron 
    196  1.23.4.2  tron 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    197  1.23.4.2  tron 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    198  1.23.4.2  tron 
    199  1.23.4.2  tron 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    200  1.23.4.2  tron 
    201  1.23.4.2  tron 		/* Just relocate the GOT slots pointing into the PLT */
    202  1.23.4.2  tron 		*where += (Elf_Addr)obj->relocbase;
    203  1.23.4.2  tron 		rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where));
    204  1.23.4.2  tron 	}
    205  1.23.4.2  tron 
    206  1.23.4.2  tron 	return 0;
    207  1.23.4.2  tron }
    208  1.23.4.2  tron 
    209  1.23.4.2  tron caddr_t
    210  1.23.4.2  tron _rtld_bind(const Obj_Entry *obj, Elf_Word reloff)
    211  1.23.4.2  tron {
    212  1.23.4.2  tron 	const Elf_Rel *rel = (const Elf_Rel *)((caddr_t)obj->pltrel + reloff);
    213  1.23.4.2  tron 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    214  1.23.4.2  tron 	Elf_Addr new_value;
    215  1.23.4.2  tron 	const Elf_Sym  *def;
    216  1.23.4.2  tron 	const Obj_Entry *defobj;
    217  1.23.4.2  tron 
    218  1.23.4.2  tron 	assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    219  1.23.4.2  tron 
    220  1.23.4.2  tron 	def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj, true);
    221  1.23.4.2  tron 	if (def == NULL)
    222  1.23.4.2  tron 		_rtld_die();
    223  1.23.4.2  tron 
    224  1.23.4.2  tron 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    225  1.23.4.2  tron 	rdbg(("bind now/fixup in %s --> old=%p new=%p",
    226  1.23.4.2  tron 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    227  1.23.4.2  tron 	if (*where != new_value)
    228  1.23.4.2  tron 		*where = new_value;
    229  1.23.4.2  tron 
    230  1.23.4.2  tron 	return (caddr_t)new_value;
    231  1.23.4.2  tron }
    232  1.23.4.2  tron 
    233  1.23.4.2  tron int
    234  1.23.4.2  tron _rtld_relocate_plt_objects(const Obj_Entry *obj)
    235  1.23.4.2  tron {
    236  1.23.4.2  tron 	const Elf_Rel *rel;
    237  1.23.4.2  tron 
    238  1.23.4.2  tron 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    239  1.23.4.2  tron 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    240  1.23.4.2  tron 		Elf_Addr target;
    241  1.23.4.2  tron 		const Elf_Sym *def;
    242  1.23.4.2  tron 		const Obj_Entry *defobj;
    243  1.23.4.2  tron 
    244  1.23.4.2  tron 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    245  1.23.4.2  tron 
    246  1.23.4.2  tron 		def = _rtld_find_symdef(ELF_R_SYM(rel->r_info), obj, &defobj,
    247  1.23.4.2  tron 		    true);
    248  1.23.4.2  tron 		if (def == NULL)
    249  1.23.4.2  tron 			return -1;
    250  1.23.4.2  tron 		target = (Elf_Addr)(defobj->relocbase + def->st_value);
    251  1.23.4.2  tron 		rdbg(("bind now/fixup in %s --> old=%p new=%p",
    252  1.23.4.2  tron 		    defobj->strtab + def->st_name, (void *)*where,
    253  1.23.4.2  tron 		    (void *)target));
    254  1.23.4.2  tron 		if (*where != target)
    255  1.23.4.2  tron 			*where = target;
    256  1.23.4.2  tron 	}
    257  1.23.4.2  tron 	return 0;
    258  1.23.4.2  tron }
    259