Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.4
      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.3  mycroft _rtld_relocate_nonplt_objects(obj, dodebug)
     16  1.2  mycroft 	Obj_Entry *obj;
     17  1.2  mycroft 	bool dodebug;
     18  1.2  mycroft {
     19  1.3  mycroft 	const Elf_Rel *rel;
     20  1.4  mycroft #ifdef COMBRELOC
     21  1.4  mycroft 	unsigned long lastsym = -1;
     22  1.4  mycroft #endif
     23  1.4  mycroft 	Elf_Addr target;
     24  1.2  mycroft 
     25  1.3  mycroft 	for (rel = obj->rel; rel < obj->rellim; rel++) {
     26  1.3  mycroft 		Elf_Addr        *where;
     27  1.3  mycroft 		Elf_Addr         tmp;
     28  1.4  mycroft 		unsigned long	 symnum;
     29  1.3  mycroft 
     30  1.3  mycroft 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     31  1.4  mycroft 		symnum = ELF_R_SYM(rel->r_info);
     32  1.3  mycroft 
     33  1.3  mycroft 		switch (ELF_R_TYPE(rel->r_info)) {
     34  1.3  mycroft 		case R_TYPE(NONE):
     35  1.3  mycroft 			break;
     36  1.2  mycroft 
     37  1.2  mycroft #if 1 /* XXX should not occur */
     38  1.3  mycroft 		case R_TYPE(PC32):
     39  1.4  mycroft #ifdef COMBRELOC
     40  1.4  mycroft 			if (symnum != lastsym)
     41  1.4  mycroft #endif
     42  1.4  mycroft 			{
     43  1.4  mycroft 				const Elf_Sym   *def;
     44  1.4  mycroft 				const Obj_Entry *defobj;
     45  1.4  mycroft 				def = _rtld_find_symdef(symnum, obj, &defobj,
     46  1.4  mycroft 				    false);
     47  1.4  mycroft 				if (def == NULL)
     48  1.4  mycroft 					return -1;
     49  1.4  mycroft 				target = (Elf_Addr)(defobj->relocbase +
     50  1.4  mycroft 				    def->st_value);
     51  1.4  mycroft #ifdef COMBRELOC
     52  1.4  mycroft 				lastsym = symnum;
     53  1.4  mycroft #endif
     54  1.4  mycroft 			}
     55  1.3  mycroft 
     56  1.4  mycroft 			*where += target - (Elf_Addr)where;
     57  1.3  mycroft 			rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
     58  1.3  mycroft 			    defobj->strtab + def->st_name, obj->path,
     59  1.3  mycroft 			    (void *)*where, defobj->path));
     60  1.3  mycroft 			break;
     61  1.2  mycroft 
     62  1.3  mycroft 		case R_TYPE(GOT32):
     63  1.2  mycroft #endif
     64  1.3  mycroft 		case R_TYPE(32):
     65  1.3  mycroft 		case R_TYPE(GLOB_DAT):
     66  1.4  mycroft #ifdef COMBRELOC
     67  1.4  mycroft 			if (symnum != lastsym)
     68  1.4  mycroft #endif
     69  1.4  mycroft 			{
     70  1.4  mycroft 				const Elf_Sym   *def;
     71  1.4  mycroft 				const Obj_Entry *defobj;
     72  1.4  mycroft 				def = _rtld_find_symdef(symnum, obj, &defobj,
     73  1.4  mycroft 				    false);
     74  1.4  mycroft 				if (def == NULL)
     75  1.4  mycroft 					return -1;
     76  1.4  mycroft 				target = (Elf_Addr)(defobj->relocbase +
     77  1.4  mycroft 				    def->st_value);
     78  1.4  mycroft #ifdef COMBRELOC
     79  1.4  mycroft 				lastsym = symnum;
     80  1.4  mycroft #endif
     81  1.4  mycroft 			}
     82  1.3  mycroft 
     83  1.4  mycroft 			tmp = target + *where;
     84  1.3  mycroft 			if (*where != tmp)
     85  1.3  mycroft 				*where = tmp;
     86  1.3  mycroft 			rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
     87  1.3  mycroft 			    defobj->strtab + def->st_name, obj->path,
     88  1.3  mycroft 			    (void *)*where, defobj->path));
     89  1.3  mycroft 			break;
     90  1.3  mycroft 
     91  1.3  mycroft 		case R_TYPE(RELATIVE):
     92  1.3  mycroft 			*where += (Elf_Addr)obj->relocbase;
     93  1.3  mycroft 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
     94  1.3  mycroft 			    (void *)*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.3  mycroft 			if (!obj->mainprog) {
    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