mdreloc.c revision 1.18
1/* $NetBSD: mdreloc.c,v 1.18 2005/07/26 15:43:34 skrll Exp $ */ 2 3#include <sys/types.h> 4#include <sys/stat.h> 5 6#include "debug.h" 7#include "rtld.h" 8 9void _rtld_bind_start(void); 10void _rtld_relocate_nonplt_self(Elf_Dyn *, Elf_Addr); 11caddr_t _rtld_bind(const Obj_Entry *, Elf_Word); 12static inline int _rtld_relocate_plt_object(const Obj_Entry *, 13 const Elf_Rela *, Elf_Addr *); 14 15 16void 17_rtld_setup_pltgot(const Obj_Entry *obj) 18{ 19 obj->pltgot[1] = (Elf_Addr) obj; 20 obj->pltgot[2] = (Elf_Addr) &_rtld_bind_start; 21} 22 23void 24_rtld_relocate_nonplt_self(Elf_Dyn *dynp, Elf_Addr relocbase) 25{ 26 const Elf_Rela *rela = 0, *relalim; 27 Elf_Addr relasz = 0; 28 Elf_Addr *where; 29 30 for (; dynp->d_tag != DT_NULL; dynp++) { 31 switch (dynp->d_tag) { 32 case DT_RELA: 33 rela = (const Elf_Rela *)(relocbase + dynp->d_un.d_ptr); 34 break; 35 case DT_RELASZ: 36 relasz = dynp->d_un.d_val; 37 break; 38 } 39 } 40 relalim = (const Elf_Rela *)((caddr_t)rela + relasz); 41 for (; rela < relalim; rela++) { 42 where = (Elf_Addr *)(relocbase + rela->r_offset); 43 *where += (Elf_Addr)relocbase; 44 } 45} 46 47int 48_rtld_relocate_nonplt_objects(const Obj_Entry *obj) 49{ 50 const Elf_Rela *rela; 51 52 for (rela = obj->rela; rela < obj->relalim; rela++) { 53 Elf_Addr *where; 54 const Elf_Sym *def; 55 const Obj_Entry *defobj; 56 Elf_Addr tmp; 57 unsigned long symnum; 58 59 where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 60 symnum = ELF_R_SYM(rela->r_info); 61 62 switch (ELF_R_TYPE(rela->r_info)) { 63 case R_TYPE(NONE): 64 break; 65 66#if 1 /* XXX should not occur */ 67 case R_TYPE(PC32): 68 def = _rtld_find_symdef(symnum, obj, &defobj, false); 69 if (def == NULL) 70 return -1; 71 72 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 73 rela->r_addend) - (Elf_Addr)where; 74 if (*where != tmp) 75 *where = tmp; 76 rdbg(("PC32 %s in %s --> %p in %s", 77 obj->strtab + obj->symtab[symnum].st_name, 78 obj->path, (void *)*where, defobj->path)); 79 break; 80 81 case R_TYPE(GOT32): 82#endif 83 case R_TYPE(32): 84 case R_TYPE(GLOB_DAT): 85 def = _rtld_find_symdef(symnum, obj, &defobj, false); 86 if (def == NULL) 87 return -1; 88 89 tmp = (Elf_Addr)(defobj->relocbase + def->st_value + 90 rela->r_addend); 91 if (*where != tmp) 92 *where = tmp; 93 rdbg(("32/GLOB_DAT %s in %s --> %p in %s", 94 obj->strtab + obj->symtab[symnum].st_name, 95 obj->path, (void *)*where, defobj->path)); 96 break; 97 98 case R_TYPE(RELATIVE): 99 *where += (Elf_Addr)obj->relocbase; 100 rdbg(("RELATIVE in %s --> %p", obj->path, 101 (void *)*where)); 102 break; 103 104 case R_TYPE(COPY): 105 /* 106 * These are deferred until all other relocations have 107 * been done. All we do here is make sure that the 108 * COPY relocation is not in a shared library. They 109 * are allowed only in executable files. 110 */ 111 if (obj->isdynamic) { 112 _rtld_error( 113 "%s: Unexpected R_COPY relocation in shared library", 114 obj->path); 115 return -1; 116 } 117 rdbg(("COPY (avoid in main)")); 118 break; 119 120 default: 121 rdbg(("sym = %lu, type = %lu, offset = %p, " 122 "addend = %p, contents = %p, symbol = %s", 123 symnum, (u_long)ELF_R_TYPE(rela->r_info), 124 (void *)rela->r_offset, (void *)rela->r_addend, 125 (void *)*where, 126 obj->strtab + obj->symtab[symnum].st_name)); 127 _rtld_error("%s: Unsupported relocation type %ld " 128 "in non-PLT relocations\n", 129 obj->path, (u_long) ELF_R_TYPE(rela->r_info)); 130 return -1; 131 } 132 } 133 return 0; 134} 135 136int 137_rtld_relocate_plt_lazy(const Obj_Entry *obj) 138{ 139 const Elf_Rela *rela; 140 141 if (!obj->relocbase) 142 return 0; 143 144 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) { 145 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 146 147 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)); 148 149 /* Just relocate the GOT slots pointing into the PLT */ 150 *where += (Elf_Addr)obj->relocbase; 151 rdbg(("fixup !main in %s --> %p", obj->path, (void *)*where)); 152 } 153 154 return 0; 155} 156 157static inline int 158_rtld_relocate_plt_object(const Obj_Entry *obj, const Elf_Rela *rela, Elf_Addr *tp) 159{ 160 Elf_Addr *where = (Elf_Addr *)(obj->relocbase + rela->r_offset); 161 Elf_Addr new_value; 162 const Elf_Sym *def; 163 const Obj_Entry *defobj; 164 165 assert(ELF_R_TYPE(rela->r_info) == R_TYPE(JMP_SLOT)); 166 167 def = _rtld_find_symdef(ELF_R_SYM(rela->r_info), obj, &defobj, true); 168 if (def == NULL) 169 return -1; 170 171 assert(rela->r_addend == 0); 172 new_value = (Elf_Addr)(defobj->relocbase + def->st_value + 173 rela->r_addend); 174 rdbg(("bind now/fixup in %s --> old=%p new=%p", 175 defobj->strtab + def->st_name, (void *)*where, (void *)new_value)); 176 if (*where != new_value) 177 *where = new_value; 178 179 if (tp) 180 *tp = new_value - rela->r_addend; 181 182 return 0; 183} 184 185caddr_t 186_rtld_bind(const Obj_Entry *obj, Elf_Word reloff) 187{ 188 const Elf_Rela *rela = (const Elf_Rela *)((caddr_t)obj->pltrela + reloff); 189 Elf_Addr result; 190 int err; 191 192 err = _rtld_relocate_plt_object(obj, rela, &result); 193 if (err) 194 _rtld_die(); 195 196 return (caddr_t)result; 197} 198 199int 200_rtld_relocate_plt_objects(const Obj_Entry *obj) 201{ 202 const Elf_Rela *rela; 203 204 for (rela = obj->pltrela; rela < obj->pltrelalim; rela++) 205 if (_rtld_relocate_plt_object(obj, rela, NULL) < 0) 206 return -1; 207 208 return 0; 209} 210