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