1 /* $NetBSD: headers.c,v 1.29 2010/04/05 14:01:26 joerg Exp $ */ 2 3 /* 4 * Copyright 1996 John D. Polstra. 5 * Copyright 1996 Matt Thomas <matt@3am-software.com> 6 * Copyright 2002 Charles M. Hannum <root@ihack.net> 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by John Polstra. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Dynamic linker for ELF. 37 * 38 * John Polstra <jdp@polstra.com>. 39 */ 40 41 #include <sys/cdefs.h> 42 #ifndef lint 43 __RCSID("$NetBSD: headers.c,v 1.29 2010/04/05 14:01:26 joerg Exp $"); 44 #endif /* not lint */ 45 46 #include <err.h> 47 #include <errno.h> 48 #include <fcntl.h> 49 #include <stdarg.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include <sys/types.h> 55 #include <sys/mman.h> 56 #include <sys/bitops.h> 57 #include <dirent.h> 58 59 #include "debug.h" 60 #include "rtld.h" 61 62 /* 63 * Process a shared object's DYNAMIC section, and save the important 64 * information in its Obj_Entry structure. 65 */ 66 void 67 _rtld_digest_dynamic(const char *execname, Obj_Entry *obj) 68 { 69 Elf_Dyn *dynp; 70 Needed_Entry **needed_tail = &obj->needed; 71 const Elf_Dyn *dyn_rpath = NULL; 72 Elf_Sword plttype = DT_NULL; 73 Elf_Addr relsz = 0, relasz = 0; 74 Elf_Addr pltrel = 0, pltrelsz = 0; 75 Elf_Addr init = 0, fini = 0; 76 77 for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; ++dynp) { 78 switch (dynp->d_tag) { 79 80 case DT_REL: 81 obj->rel = (const Elf_Rel *) 82 (obj->relocbase + dynp->d_un.d_ptr); 83 break; 84 85 case DT_RELSZ: 86 relsz = dynp->d_un.d_val; 87 break; 88 89 case DT_RELENT: 90 assert(dynp->d_un.d_val == sizeof(Elf_Rel)); 91 break; 92 93 case DT_JMPREL: 94 pltrel = dynp->d_un.d_ptr; 95 break; 96 97 case DT_PLTRELSZ: 98 pltrelsz = dynp->d_un.d_val; 99 break; 100 101 case DT_RELA: 102 obj->rela = (const Elf_Rela *) 103 (obj->relocbase + dynp->d_un.d_ptr); 104 break; 105 106 case DT_RELASZ: 107 relasz = dynp->d_un.d_val; 108 break; 109 110 case DT_RELAENT: 111 assert(dynp->d_un.d_val == sizeof(Elf_Rela)); 112 break; 113 114 case DT_PLTREL: 115 plttype = dynp->d_un.d_val; 116 assert(plttype == DT_REL || plttype == DT_RELA); 117 break; 118 119 case DT_SYMTAB: 120 obj->symtab = (const Elf_Sym *) 121 (obj->relocbase + dynp->d_un.d_ptr); 122 break; 123 124 case DT_SYMENT: 125 assert(dynp->d_un.d_val == sizeof(Elf_Sym)); 126 break; 127 128 case DT_STRTAB: 129 obj->strtab = (const char *) 130 (obj->relocbase + dynp->d_un.d_ptr); 131 break; 132 133 case DT_STRSZ: 134 obj->strsize = dynp->d_un.d_val; 135 break; 136 137 case DT_HASH: 138 { 139 const Elf_Word *hashtab = (const Elf_Word *) 140 (obj->relocbase + dynp->d_un.d_ptr); 141 142 if (hashtab[0] > UINT32_MAX) 143 obj->nbuckets = UINT32_MAX; 144 else 145 obj->nbuckets = hashtab[0]; 146 obj->nchains = hashtab[1]; 147 obj->buckets = hashtab + 2; 148 obj->chains = obj->buckets + obj->nbuckets; 149 /* 150 * Should really be in _rtld_relocate_objects, 151 * but _rtld_symlook_obj might be used before. 152 */ 153 if (obj->nbuckets) { 154 fast_divide32_prepare(obj->nbuckets, 155 &obj->nbuckets_m, 156 &obj->nbuckets_s1, 157 &obj->nbuckets_s2); 158 } 159 } 160 break; 161 162 case DT_NEEDED: 163 { 164 Needed_Entry *nep = NEW(Needed_Entry); 165 166 nep->name = dynp->d_un.d_val; 167 nep->obj = NULL; 168 nep->next = NULL; 169 170 *needed_tail = nep; 171 needed_tail = &nep->next; 172 } 173 break; 174 175 case DT_PLTGOT: 176 obj->pltgot = (Elf_Addr *) 177 (obj->relocbase + dynp->d_un.d_ptr); 178 break; 179 180 case DT_TEXTREL: 181 obj->textrel = true; 182 break; 183 184 case DT_SYMBOLIC: 185 obj->symbolic = true; 186 break; 187 188 case DT_RPATH: 189 /* 190 * We have to wait until later to process this, because 191 * we might not have gotten the address of the string 192 * table yet. 193 */ 194 dyn_rpath = dynp; 195 break; 196 197 case DT_SONAME: 198 /* Not used by the dynamic linker. */ 199 break; 200 201 case DT_INIT: 202 init = dynp->d_un.d_ptr; 203 break; 204 205 case DT_FINI: 206 fini = dynp->d_un.d_ptr; 207 break; 208 209 /* 210 * Don't process DT_DEBUG on MIPS as the dynamic section 211 * is mapped read-only. DT_MIPS_RLD_MAP is used instead. 212 * XXX: n32/n64 may use DT_DEBUG, not sure yet. 213 */ 214 #ifndef __mips__ 215 case DT_DEBUG: 216 #ifdef RTLD_LOADER 217 dynp->d_un.d_ptr = (Elf_Addr)&_rtld_debug; 218 #endif 219 break; 220 #endif 221 222 #ifdef __mips__ 223 case DT_MIPS_LOCAL_GOTNO: 224 obj->local_gotno = dynp->d_un.d_val; 225 break; 226 227 case DT_MIPS_SYMTABNO: 228 obj->symtabno = dynp->d_un.d_val; 229 break; 230 231 case DT_MIPS_GOTSYM: 232 obj->gotsym = dynp->d_un.d_val; 233 break; 234 235 case DT_MIPS_RLD_MAP: 236 #ifdef RTLD_LOADER 237 *((Elf_Addr *)(dynp->d_un.d_ptr)) = (Elf_Addr) 238 &_rtld_debug; 239 #endif 240 break; 241 #endif 242 case DT_FLAGS_1: 243 obj->initfirst = 244 ((dynp->d_un.d_val & DF_1_INITFIRST) != 0); 245 break; 246 } 247 } 248 249 obj->rellim = (const Elf_Rel *)((const uint8_t *)obj->rel + relsz); 250 obj->relalim = (const Elf_Rela *)((const uint8_t *)obj->rela + relasz); 251 if (plttype == DT_REL) { 252 obj->pltrel = (const Elf_Rel *)(obj->relocbase + pltrel); 253 obj->pltrellim = (const Elf_Rel *)(obj->relocbase + pltrel + pltrelsz); 254 obj->pltrelalim = 0; 255 /* On PPC and SPARC, at least, REL(A)SZ may include JMPREL. 256 Trim rel(a)lim to save time later. */ 257 if (obj->rellim && obj->pltrel && 258 obj->rellim > obj->pltrel && 259 obj->rellim <= obj->pltrellim) 260 obj->rellim = obj->pltrel; 261 } else if (plttype == DT_RELA) { 262 obj->pltrela = (const Elf_Rela *)(obj->relocbase + pltrel); 263 obj->pltrellim = 0; 264 obj->pltrelalim = (const Elf_Rela *)(obj->relocbase + pltrel + pltrelsz); 265 /* On PPC and SPARC, at least, REL(A)SZ may include JMPREL. 266 Trim rel(a)lim to save time later. */ 267 if (obj->relalim && obj->pltrela && 268 obj->relalim > obj->pltrela && 269 obj->relalim <= obj->pltrelalim) 270 obj->relalim = obj->pltrela; 271 } 272 273 #if defined(RTLD_LOADER) && defined(__HAVE_FUNCTION_DESCRIPTORS) 274 if (init != 0) 275 obj->init = (void (*)(void)) 276 _rtld_function_descriptor_alloc(obj, NULL, init); 277 if (fini != 0) 278 obj->fini = (void (*)(void)) 279 _rtld_function_descriptor_alloc(obj, NULL, fini); 280 #else 281 if (init != 0) 282 obj->init = (void (*)(void)) 283 (obj->relocbase + init); 284 if (fini != 0) 285 obj->fini = (void (*)(void)) 286 (obj->relocbase + fini); 287 #endif 288 289 if (dyn_rpath != NULL) { 290 _rtld_add_paths(execname, &obj->rpaths, obj->strtab + 291 dyn_rpath->d_un.d_val); 292 } 293 } 294 295 /* 296 * Process a shared object's program header. This is used only for the 297 * main program, when the kernel has already loaded the main program 298 * into memory before calling the dynamic linker. It creates and 299 * returns an Obj_Entry structure. 300 */ 301 Obj_Entry * 302 _rtld_digest_phdr(const Elf_Phdr *phdr, int phnum, caddr_t entry) 303 { 304 Obj_Entry *obj; 305 const Elf_Phdr *phlimit = phdr + phnum; 306 const Elf_Phdr *ph; 307 int nsegs = 0; 308 ptrdiff_t relocoffs = 0; 309 Elf_Addr vaddr; 310 311 obj = _rtld_obj_new(); 312 for (ph = phdr; ph < phlimit; ++ph) { 313 vaddr = ph->p_vaddr + relocoffs; 314 dbg(("headers: relocoffs = %lx\n", (long)relocoffs)); 315 switch (ph->p_type) { 316 317 case PT_PHDR: 318 relocoffs = (uintptr_t)phdr - (uintptr_t)ph->p_vaddr; 319 break; 320 321 case PT_INTERP: 322 obj->interp = (const char *)(uintptr_t)vaddr; 323 break; 324 325 case PT_LOAD: 326 assert(nsegs < 2); 327 if (nsegs == 0) { /* First load segment */ 328 obj->vaddrbase = round_down(vaddr); 329 obj->mapbase = (caddr_t)(uintptr_t)obj->vaddrbase; 330 obj->relocbase = (void *)relocoffs; 331 obj->textsize = round_up(vaddr + ph->p_memsz) - 332 obj->vaddrbase; 333 } else { /* Last load segment */ 334 obj->mapsize = round_up(vaddr + ph->p_memsz) - 335 obj->vaddrbase; 336 } 337 ++nsegs; 338 break; 339 340 case PT_DYNAMIC: 341 obj->dynamic = (Elf_Dyn *)(uintptr_t)vaddr; 342 break; 343 } 344 } 345 assert(nsegs == 2); 346 347 obj->entry = entry; 348 return obj; 349 } 350