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