Home | History | Annotate | Line # | Download | only in i386
mdreloc.c revision 1.5
      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.5  mycroft 		const Elf_Sym   *def;
     28  1.5  mycroft 		const Obj_Entry *defobj;
     29  1.3  mycroft 		Elf_Addr         tmp;
     30  1.4  mycroft 		unsigned long	 symnum;
     31  1.3  mycroft 
     32  1.3  mycroft 		where = (Elf_Addr *)(obj->relocbase + rel->r_offset);
     33  1.4  mycroft 		symnum = ELF_R_SYM(rel->r_info);
     34  1.3  mycroft 
     35  1.3  mycroft 		switch (ELF_R_TYPE(rel->r_info)) {
     36  1.3  mycroft 		case R_TYPE(NONE):
     37  1.3  mycroft 			break;
     38  1.2  mycroft 
     39  1.2  mycroft #if 1 /* XXX should not occur */
     40  1.3  mycroft 		case R_TYPE(PC32):
     41  1.4  mycroft #ifdef COMBRELOC
     42  1.5  mycroft 			if (symnum != lastsym) {
     43  1.4  mycroft #endif
     44  1.4  mycroft 				def = _rtld_find_symdef(symnum, obj, &defobj,
     45  1.4  mycroft 				    false);
     46  1.4  mycroft 				if (def == NULL)
     47  1.4  mycroft 					return -1;
     48  1.4  mycroft 				target = (Elf_Addr)(defobj->relocbase +
     49  1.4  mycroft 				    def->st_value);
     50  1.4  mycroft #ifdef COMBRELOC
     51  1.4  mycroft 				lastsym = symnum;
     52  1.5  mycroft 			}
     53  1.4  mycroft #endif
     54  1.3  mycroft 
     55  1.4  mycroft 			*where += target - (Elf_Addr)where;
     56  1.3  mycroft 			rdbg(dodebug, ("PC32 %s in %s --> %p in %s",
     57  1.5  mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
     58  1.5  mycroft 			    obj->path, (void *)*where, defobj->path));
     59  1.3  mycroft 			break;
     60  1.2  mycroft 
     61  1.3  mycroft 		case R_TYPE(GOT32):
     62  1.2  mycroft #endif
     63  1.3  mycroft 		case R_TYPE(32):
     64  1.3  mycroft 		case R_TYPE(GLOB_DAT):
     65  1.4  mycroft #ifdef COMBRELOC
     66  1.5  mycroft 			if (symnum != lastsym) {
     67  1.4  mycroft #endif
     68  1.4  mycroft 				def = _rtld_find_symdef(symnum, obj, &defobj,
     69  1.4  mycroft 				    false);
     70  1.4  mycroft 				if (def == NULL)
     71  1.4  mycroft 					return -1;
     72  1.4  mycroft 				target = (Elf_Addr)(defobj->relocbase +
     73  1.4  mycroft 				    def->st_value);
     74  1.4  mycroft #ifdef COMBRELOC
     75  1.4  mycroft 				lastsym = symnum;
     76  1.5  mycroft 			}
     77  1.4  mycroft #endif
     78  1.3  mycroft 
     79  1.4  mycroft 			tmp = target + *where;
     80  1.3  mycroft 			if (*where != tmp)
     81  1.3  mycroft 				*where = tmp;
     82  1.3  mycroft 			rdbg(dodebug, ("32/GLOB_DAT %s in %s --> %p in %s",
     83  1.5  mycroft 			    obj->strtab + obj->symtab[symnum].st_name,
     84  1.5  mycroft 			    obj->path, (void *)*where, defobj->path));
     85  1.3  mycroft 			break;
     86  1.3  mycroft 
     87  1.3  mycroft 		case R_TYPE(RELATIVE):
     88  1.3  mycroft 			*where += (Elf_Addr)obj->relocbase;
     89  1.3  mycroft 			rdbg(dodebug, ("RELATIVE in %s --> %p", obj->path,
     90  1.3  mycroft 			    (void *)*where));
     91  1.3  mycroft 			break;
     92  1.3  mycroft 
     93  1.3  mycroft 		case R_TYPE(COPY):
     94  1.3  mycroft 			/*
     95  1.3  mycroft 			 * These are deferred until all other relocations have
     96  1.3  mycroft 			 * been done.  All we do here is make sure that the
     97  1.3  mycroft 			 * COPY relocation is not in a shared library.  They
     98  1.3  mycroft 			 * are allowed only in executable files.
     99  1.3  mycroft 			 */
    100  1.3  mycroft 			if (!obj->mainprog) {
    101  1.3  mycroft 				_rtld_error(
    102  1.2  mycroft 			"%s: Unexpected R_COPY relocation in shared library",
    103  1.3  mycroft 				    obj->path);
    104  1.3  mycroft 				return -1;
    105  1.3  mycroft 			}
    106  1.3  mycroft 			rdbg(dodebug, ("COPY (avoid in main)"));
    107  1.3  mycroft 			break;
    108  1.3  mycroft 
    109  1.3  mycroft 		default:
    110  1.3  mycroft 			rdbg(dodebug, ("sym = %lu, type = %lu, offset = %p, "
    111  1.3  mycroft 			    "contents = %p, symbol = %s",
    112  1.4  mycroft 			    symnum, (u_long)ELF_R_TYPE(rel->r_info),
    113  1.3  mycroft 			    (void *)rel->r_offset, (void *)*where,
    114  1.4  mycroft 			    obj->strtab + obj->symtab[symnum].st_name));
    115  1.3  mycroft 			_rtld_error("%s: Unsupported relocation type %ld "
    116  1.3  mycroft 			    "in non-PLT relocations\n",
    117  1.3  mycroft 			    obj->path, (u_long) ELF_R_TYPE(rel->r_info));
    118  1.2  mycroft 			return -1;
    119  1.2  mycroft 		}
    120  1.2  mycroft 	}
    121  1.2  mycroft 	return 0;
    122  1.2  mycroft }
    123