Home | History | Annotate | Line # | Download | only in arm
mdreloc.c revision 1.9
      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.3  mycroft 			*where += (Elf_Addr)obj->relocbase;
     93  1.3  mycroft 			rdbg(dodebug, ("RELATIVE in %s --> %p @ %p", obj->path,
     94  1.3  mycroft 			    (void *)*where, where));
     95  1.3  mycroft 			break;
     96  1.3  mycroft 
     97  1.3  mycroft 		case R_TYPE(COPY):
     98  1.3  mycroft 			/*
     99  1.3  mycroft 			 * These are deferred until all other relocations have
    100  1.3  mycroft 			 * been done.  All we do here is make sure that the
    101  1.3  mycroft 			 * COPY relocation is not in a shared library.  They
    102  1.3  mycroft 			 * are allowed only in executable files.
    103  1.3  mycroft 			 */
    104  1.8  mycroft 			if (obj->isdynamic) {
    105  1.3  mycroft 				_rtld_error(
    106  1.2  mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    107  1.3  mycroft 				    obj->path);
    108  1.3  mycroft 				return -1;
    109  1.3  mycroft 			}
    110  1.3  mycroft 			rdbg(dodebug, ("COPY (avoid in main)"));
    111  1.3  mycroft 			break;
    112  1.3  mycroft 
    113  1.3  mycroft 		default:
    114  1.3  mycroft 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    115  1.3  mycroft 			    "contents = %p, symbol = %s",
    116  1.4  mycroft 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    117  1.3  mycroft 			    (void *)rel->r_offset, (void *)*where,
    118  1.4  mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    119  1.3  mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    120  1.3  mycroft 			    "in non-PLT relocations\n",
    121  1.3  mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    122  1.2  mycroft 			return -1;
    123  1.2  mycroft 		}
    124  1.2  mycroft 	}
    125  1.2  mycroft 	return 0;
    126  1.2  mycroft }
    127  1.6  mycroft 
    128  1.6  mycroft int
    129  1.6  mycroft _rtld_relocate_plt_lazy(obj, dodebug)
    130  1.7  mycroft 	const Obj_Entry *obj;
    131  1.6  mycroft 	bool dodebug;
    132  1.6  mycroft {
    133  1.6  mycroft 	const Elf_Rel *rel;
    134  1.6  mycroft 
    135  1.8  mycroft 	if (!obj->isdynamic)
    136  1.6  mycroft 		return 0;
    137  1.6  mycroft 
    138  1.6  mycroft 	for (rel = obj->pltrel; rel < obj->pltrellim; rel++) {
    139  1.6  mycroft 		Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
    140  1.6  mycroft 
    141  1.6  mycroft 		assert(ELF_R_TYPE(rel->r_info) == R_TYPE(JUMP_SLOT));
    142  1.6  mycroft 
    143  1.6  mycroft 		/* Just relocate the GOT slots pointing into the PLT */
    144  1.6  mycroft 		*where += (Elf_Addr)obj->relocbase;
    145  1.6  mycroft 		rdbg(dodebug, ("fixup !main in %s --> %p", obj->path,
    146  1.6  mycroft 		    (void *)*where));
    147  1.6  mycroft 	}
    148  1.6  mycroft 
    149  1.6  mycroft 	return 0;
    150  1.6  mycroft }
    151  1.6  mycroft 
    152  1.6  mycroft int
    153  1.6  mycroft _rtld_relocate_plt_object(obj, rela, addrp, dodebug)
    154  1.7  mycroft 	const Obj_Entry *obj;
    155  1.6  mycroft 	const Elf_Rela *rela;
    156  1.6  mycroft 	caddr_t *addrp;
    157  1.6  mycroft 	bool dodebug;
    158  1.6  mycroft {
    159  1.6  mycroft 	Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset);
    160  1.6  mycroft 	Elf_Addr new_value;
    161  1.6  mycroft 	const Elf_Sym  *def;
    162  1.6  mycroft 	const Obj_Entry *defobj;
    163  1.6  mycroft 
    164  1.6  mycroft 	assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JUMP_SLOT));
    165  1.6  mycroft 
    166  1.6  mycroft 	def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true);
    167  1.6  mycroft 	if (def == NULL)
    168  1.6  mycroft 		return -1;
    169  1.6  mycroft 
    170  1.6  mycroft 	new_value = (Elf_Addr)(defobj->relocbase + def->st_value);
    171  1.6  mycroft 	rdbg(dodebug, ("bind now/fixup in %s --> old=%p new=%p",
    172  1.6  mycroft 	    defobj->strtab + def->st_name, (void *)*where, (void *)new_value));
    173  1.6  mycroft 	if (*where != new_value)
    174  1.6  mycroft 		*where = new_value;
    175  1.6  mycroft 
    176  1.6  mycroft 	*addrp = (caddr_t)new_value;
    177  1.6  mycroft 	return 0;
    178  1.6  mycroft }
    179