1 /* $NetBSD: headers.c,v 1.17 2002/10/05 11:59:03 mycroft 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 <err.h> 42 #include <errno.h> 43 #include <fcntl.h> 44 #include <stdarg.h> 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <string.h> 48 #include <unistd.h> 49 #include <sys/types.h> 50 #include <sys/mman.h> 51 #include <dirent.h> 52 53 #include "debug.h" 54 #include "rtld.h" 55 56 /* 57 * Process a shared object's DYNAMIC section, and save the important 58 * information in its Obj_Entry structure. 59 */ 60 void 61 _rtld_digest_dynamic(obj) 62 Obj_Entry *obj; 63 { 64 Elf_Dyn *dynp; 65 Needed_Entry **needed_tail = &obj->needed; 66 const Elf_Dyn *dyn_rpath = NULL; 67 Elf_Sword plttype = DT_NULL; 68 Elf_Addr relsz = 0, relasz = 0; 69 Elf_Addr pltrel = 0, pltrelsz = 0; 70 Elf_Addr init = 0, fini = 0; 71 72 for (dynp = obj->dynamic; dynp->d_tag != DT_NULL; ++dynp) { 73 switch (dynp->d_tag) { 74 75 case DT_REL: 76 obj->rel = (const Elf_Rel *) 77 (obj->relocbase + dynp->d_un.d_ptr); 78 break; 79 80 case DT_RELSZ: 81 relsz = dynp->d_un.d_val; 82 break; 83 84 case DT_RELENT: 85 assert(dynp->d_un.d_val == sizeof(Elf_Rel)); 86 break; 87 88 case DT_JMPREL: 89 pltrel = dynp->d_un.d_ptr; 90 break; 91 92 case DT_PLTRELSZ: 93 pltrelsz = dynp->d_un.d_val; 94 break; 95 96 case DT_RELA: 97 obj->rela = (const Elf_Rela *) 98 (obj->relocbase + dynp->d_un.d_ptr); 99 break; 100 101 case DT_RELASZ: 102 relasz = dynp->d_un.d_val; 103 break; 104 105 case DT_RELAENT: 106 assert(dynp->d_un.d_val == sizeof(Elf_Rela)); 107 break; 108 109 case DT_PLTREL: 110 plttype = dynp->d_un.d_val; 111 assert(plttype == DT_REL || plttype == DT_RELA); 112 break; 113 114 case DT_SYMTAB: 115 obj->symtab = (const Elf_Sym *) 116 (obj->relocbase + dynp->d_un.d_ptr); 117 break; 118 119 case DT_SYMENT: 120 assert(dynp->d_un.d_val == sizeof(Elf_Sym)); 121 break; 122 123 case DT_STRTAB: 124 obj->strtab = (const char *) 125 (obj->relocbase + dynp->d_un.d_ptr); 126 break; 127 128 case DT_STRSZ: 129 obj->strsize = dynp->d_un.d_val; 130 break; 131 132 case DT_HASH: 133 { 134 const Elf_Word *hashtab = (const Elf_Word *) 135 (obj->relocbase + dynp->d_un.d_ptr); 136 137 obj->nbuckets = hashtab[0]; 138 obj->nchains = hashtab[1]; 139 obj->buckets = hashtab + 2; 140 obj->chains = obj->buckets + obj->nbuckets; 141 } 142 break; 143 144 case DT_NEEDED: 145 { 146 Needed_Entry *nep = NEW(Needed_Entry); 147 148 nep->name = dynp->d_un.d_val; 149 nep->obj = NULL; 150 nep->next = NULL; 151 152 *needed_tail = nep; 153 needed_tail = &nep->next; 154 } 155 break; 156 157 case DT_PLTGOT: 158 obj->pltgot = (Elf_Addr *) 159 (obj->relocbase + dynp->d_un.d_ptr); 160 break; 161 162 case DT_TEXTREL: 163 obj->textrel = true; 164 break; 165 166 case DT_SYMBOLIC: 167 obj->symbolic = true; 168 break; 169 170 case DT_RPATH: 171 /* 172 * We have to wait until later to process this, because 173 * we might not have gotten the address of the string 174 * table yet. 175 */ 176 dyn_rpath = dynp; 177 break; 178 179 case DT_SONAME: 180 /* Not used by the dynamic linker. */ 181 break; 182 183 case DT_INIT: 184 init = dynp->d_un.d_ptr; 185 break; 186 187 case DT_FINI: 188 fini = dynp->d_un.d_ptr; 189 break; 190 191 case DT_DEBUG: 192 #ifdef RTLD_LOADER 193 dynp->d_un.d_ptr = (Elf_Addr)&_rtld_debug; 194 #endif 195 break; 196 197 #ifdef __mips__ 198 case DT_MIPS_LOCAL_GOTNO: 199 obj->local_gotno = dynp->d_un.d_val; 200 break; 201 202 case DT_MIPS_SYMTABNO: 203 obj->symtabno = dynp->d_un.d_val; 204 break; 205 206 case DT_MIPS_GOTSYM: 207 obj->gotsym = dynp->d_un.d_val; 208 break; 209 210 case DT_MIPS_RLD_MAP: 211 #ifdef RTLD_LOADER 212 *((Elf_Addr *)(dynp->d_un.d_ptr)) = (Elf_Addr) 213 &_rtld_debug; 214 #endif 215 break; 216 #endif 217 } 218 } 219 220 obj->rellim = (const Elf_Rel *)((caddr_t)obj->rel + relsz); 221 obj->relalim = (const Elf_Rela *)((caddr_t)obj->rela + relasz); 222 if (plttype == DT_REL) { 223 obj->pltrel = (const Elf_Rel *)(obj->relocbase + pltrel); 224 obj->pltrellim = (const Elf_Rel *)(obj->relocbase + pltrel + pltrelsz); 225 obj->pltrelalim = 0; 226 /* On PPC and SPARC, at least, REL(A)SZ may include JMPREL. 227 Trim rel(a)lim to save time later. */ 228 if (obj->rellim && obj->pltrel && 229 obj->rellim > obj->pltrel && 230 obj->rellim <= obj->pltrellim) 231 obj->rellim = obj->pltrel; 232 } else if (plttype == DT_RELA) { 233 obj->pltrela = (const Elf_Rela *)(obj->relocbase + pltrel); 234 obj->pltrellim = 0; 235 obj->pltrelalim = (const Elf_Rela *)(obj->relocbase + pltrel + pltrelsz); 236 /* On PPC and SPARC, at least, REL(A)SZ may include JMPREL. 237 Trim rel(a)lim to save time later. */ 238 if (obj->relalim && obj->pltrela && 239 obj->relalim > obj->pltrela && 240 obj->relalim <= obj->pltrelalim) 241 obj->relalim = obj->pltrela; 242 } 243 244 #if defined(RTLD_LOADER) && defined(__HAVE_FUNCTION_DESCRIPTORS) 245 if (init != 0) 246 obj->init = (void (*) __P((void))) 247 _rtld_function_descriptor_alloc(obj, NULL, init); 248 if (fini != 0) 249 obj->fini = (void (*) __P((void))) 250 _rtld_function_descriptor_alloc(obj, NULL, fini); 251 #else 252 if (init != 0) 253 obj->init = (void (*) __P((void))) 254 (obj->relocbase + init); 255 if (fini != 0) 256 obj->fini = (void (*) __P((void))) 257 (obj->relocbase + fini); 258 #endif 259 260 if (dyn_rpath != NULL) { 261 _rtld_add_paths(&obj->rpaths, obj->strtab + 262 dyn_rpath->d_un.d_val); 263 } 264 } 265 266 /* 267 * Process a shared object's program header. This is used only for the 268 * main program, when the kernel has already loaded the main program 269 * into memory before calling the dynamic linker. It creates and 270 * returns an Obj_Entry structure. 271 */ 272 Obj_Entry * 273 _rtld_digest_phdr(phdr, phnum, entry) 274 const Elf_Phdr *phdr; 275 int phnum; 276 caddr_t entry; 277 { 278 Obj_Entry *obj; 279 const Elf_Phdr *phlimit = phdr + phnum; 280 const Elf_Phdr *ph; 281 int nsegs = 0; 282 283 obj = _rtld_obj_new(); 284 for (ph = phdr; ph < phlimit; ++ph) { 285 switch (ph->p_type) { 286 287 case PT_PHDR: 288 assert((const Elf_Phdr *) ph->p_vaddr == phdr); 289 break; 290 291 case PT_INTERP: 292 obj->interp = (const char *) ph->p_vaddr; 293 break; 294 295 case PT_LOAD: 296 assert(nsegs < 2); 297 if (nsegs == 0) { /* First load segment */ 298 obj->vaddrbase = round_down(ph->p_vaddr); 299 obj->mapbase = (caddr_t) obj->vaddrbase; 300 obj->relocbase = obj->mapbase - obj->vaddrbase; 301 obj->textsize = round_up(ph->p_vaddr + 302 ph->p_memsz) - obj->vaddrbase; 303 } else { /* Last load segment */ 304 obj->mapsize = round_up(ph->p_vaddr + 305 ph->p_memsz) - obj->vaddrbase; 306 } 307 ++nsegs; 308 break; 309 310 case PT_DYNAMIC: 311 obj->dynamic = (Elf_Dyn *) ph->p_vaddr; 312 break; 313 } 314 } 315 assert(nsegs == 2); 316 317 obj->entry = entry; 318 return obj; 319 } 320