1 /*- 2 * Copyright (c) 2004 Ian Dowse <iedowse@freebsd.org> 3 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 4 * Copyright (c) 1998 Peter Wemm <peter@freebsd.org> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/exec.h> 31 #include <sys/linker.h> 32 #include <sys/module.h> 33 #include <machine/elf.h> 34 #include <stand.h> 35 36 #include "bootstrap.h" 37 #include "modinfo.h" 38 39 #define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l) 40 41 #if defined(__i386__) && __ELF_WORD_SIZE == 64 42 #undef ELF_TARG_CLASS 43 #undef ELF_TARG_MACH 44 #define ELF_TARG_CLASS ELFCLASS64 45 #define ELF_TARG_MACH EM_X86_64 46 #endif 47 48 typedef struct elf_file { 49 Elf_Ehdr hdr; 50 Elf_Shdr *e_shdr; 51 52 int symtabindex; /* Index of symbol table */ 53 int shstrindex; /* Index of section name string table */ 54 55 int fd; 56 vm_offset_t off; 57 #ifdef LOADER_VERIEXEC_VECTX 58 struct vectx *vctx; 59 #endif 60 } *elf_file_t; 61 62 #ifdef LOADER_VERIEXEC_VECTX 63 #define VECTX_HANDLE(ef) (ef)->vctx 64 #else 65 #define VECTX_HANDLE(ef) (ef)->fd 66 #endif 67 68 static int __elfN(obj_loadimage)(struct preloaded_file *mp, elf_file_t ef, 69 uint64_t loadaddr); 70 static int __elfN(obj_lookup_set)(struct preloaded_file *mp, elf_file_t ef, 71 const char *name, Elf_Addr *startp, Elf_Addr *stopp, int *countp); 72 static int __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, 73 Elf_Addr p, void *val, size_t len); 74 static int __elfN(obj_parse_modmetadata)(struct preloaded_file *mp, 75 elf_file_t ef); 76 static Elf_Addr __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx); 77 78 /* 79 * Attempt to load the file (file) as an ELF module. It will be stored at 80 * (dest), and a pointer to a module structure describing the loaded object 81 * will be saved in (result). 82 */ 83 int 84 __elfN(obj_loadfile)(char *filename, uint64_t dest, 85 struct preloaded_file **result) 86 { 87 struct preloaded_file *fp, *kfp; 88 struct elf_file ef; 89 Elf_Ehdr *hdr; 90 int err; 91 ssize_t bytes_read; 92 93 fp = NULL; 94 bzero(&ef, sizeof(struct elf_file)); 95 96 /* 97 * Open the image, read and validate the ELF header 98 */ 99 if (filename == NULL) /* can't handle nameless */ 100 return(EFTYPE); 101 if ((ef.fd = open(filename, O_RDONLY)) == -1) 102 return(errno); 103 #ifdef LOADER_VERIEXEC_VECTX 104 { 105 int verror; 106 107 ef.vctx = vectx_open(ef.fd, filename, VE_MUST, 108 0L, NULL, &verror, __func__); 109 if (verror) { 110 printf("Unverified %s: %s\n", filename, ve_error_get()); 111 close(ef.fd); 112 free(ef.vctx); 113 return (EAUTH); 114 } 115 } 116 #endif 117 118 hdr = &ef.hdr; 119 bytes_read = VECTX_READ(VECTX_HANDLE(&ef), hdr, sizeof(*hdr)); 120 if (bytes_read != sizeof(*hdr)) { 121 err = EFTYPE; /* could be EIO, but may be small file */ 122 goto oerr; 123 } 124 125 /* Is it ELF? */ 126 if (!IS_ELF(*hdr)) { 127 err = EFTYPE; 128 goto oerr; 129 } 130 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */ 131 hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 132 hdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */ 133 hdr->e_version != EV_CURRENT || 134 hdr->e_machine != ELF_TARG_MACH || /* Machine ? */ 135 hdr->e_type != ET_REL) { 136 err = EFTYPE; 137 goto oerr; 138 } 139 140 if (hdr->e_shnum * hdr->e_shentsize == 0 || hdr->e_shoff == 0 || 141 hdr->e_shentsize != sizeof(Elf_Shdr)) { 142 err = EFTYPE; 143 goto oerr; 144 } 145 146 #if defined(LOADER_VERIEXEC) && !defined(LOADER_VERIEXEC_VECTX) 147 if (verify_file(ef.fd, filename, bytes_read, VE_MUST, __func__) < 0) { 148 err = EAUTH; 149 goto oerr; 150 } 151 #endif 152 153 kfp = file_findfile(NULL, md_kerntype); 154 if (kfp == NULL) { 155 printf("elf" __XSTRING(__ELF_WORD_SIZE) 156 "_obj_loadfile: can't load module before kernel\n"); 157 err = EPERM; 158 goto oerr; 159 } 160 161 dest = md_align(dest); 162 163 /* 164 * Ok, we think we should handle this. 165 */ 166 fp = file_alloc(); 167 if (fp == NULL) { 168 printf("elf" __XSTRING(__ELF_WORD_SIZE) 169 "_obj_loadfile: cannot allocate module info\n"); 170 err = EPERM; 171 goto out; 172 } 173 fp->f_name = strdup(filename); 174 fp->f_type = strdup(md_modtype_obj); 175 176 if (module_verbose > MODULE_VERBOSE_SILENT) 177 printf("%s ", filename); 178 179 fp->f_size = __elfN(obj_loadimage)(fp, &ef, dest); 180 if (fp->f_size == 0 || fp->f_addr == 0) 181 goto ioerr; 182 183 /* save exec header as metadata */ 184 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*hdr), hdr); 185 186 /* Load OK, return module pointer */ 187 *result = (struct preloaded_file *)fp; 188 err = 0; 189 goto out; 190 191 ioerr: 192 err = EIO; 193 oerr: 194 file_discard(fp); 195 out: 196 #ifdef LOADER_VERIEXEC_VECTX 197 if (!err && ef.vctx) { 198 int verror; 199 200 verror = vectx_close(ef.vctx, __func__); 201 if (verror) { 202 err = EAUTH; 203 file_discard(fp); 204 } 205 } 206 #endif 207 close(ef.fd); 208 if (ef.e_shdr != NULL) 209 free(ef.e_shdr); 210 211 return(err); 212 } 213 214 /* 215 * With the file (fd) open on the image, and (ehdr) containing 216 * the Elf header, load the image at (off) 217 */ 218 static int 219 __elfN(obj_loadimage)(struct preloaded_file *fp, elf_file_t ef, uint64_t off) 220 { 221 Elf_Ehdr *hdr; 222 Elf_Shdr *shdr, *cshdr, *lshdr; 223 vm_offset_t firstaddr, lastaddr; 224 int i, nsym, res, ret, shdrbytes, symstrindex; 225 226 ret = 0; 227 firstaddr = lastaddr = (vm_offset_t)off; 228 hdr = &ef->hdr; 229 ef->off = (vm_offset_t)off; 230 231 /* Read in the section headers. */ 232 shdrbytes = hdr->e_shnum * hdr->e_shentsize; 233 shdr = alloc_pread(VECTX_HANDLE(ef), (off_t)hdr->e_shoff, shdrbytes); 234 if (shdr == NULL) { 235 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 236 "_obj_loadimage: read section headers failed\n"); 237 goto out; 238 } 239 ef->e_shdr = shdr; 240 241 /* 242 * Decide where to load everything, but don't read it yet. 243 * We store the load address as a non-zero sh_addr value. 244 * Start with the code/data and bss. 245 */ 246 for (i = 0; i < hdr->e_shnum; i++) 247 shdr[i].sh_addr = 0; 248 for (i = 0; i < hdr->e_shnum; i++) { 249 if (shdr[i].sh_size == 0) 250 continue; 251 switch (shdr[i].sh_type) { 252 case SHT_PROGBITS: 253 case SHT_NOBITS: 254 #if defined(__i386__) || defined(__amd64__) 255 case SHT_X86_64_UNWIND: 256 #endif 257 case SHT_INIT_ARRAY: 258 case SHT_FINI_ARRAY: 259 if ((shdr[i].sh_flags & SHF_ALLOC) == 0) 260 break; 261 lastaddr = roundup(lastaddr, shdr[i].sh_addralign); 262 shdr[i].sh_addr = (Elf_Addr)lastaddr; 263 lastaddr += shdr[i].sh_size; 264 break; 265 } 266 } 267 268 /* Symbols. */ 269 nsym = 0; 270 for (i = 0; i < hdr->e_shnum; i++) { 271 switch (shdr[i].sh_type) { 272 case SHT_SYMTAB: 273 nsym++; 274 ef->symtabindex = i; 275 break; 276 } 277 } 278 if (nsym != 1) { 279 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 280 "_obj_loadimage: file has no valid symbol table\n"); 281 goto out; 282 } 283 lastaddr = roundup(lastaddr, shdr[ef->symtabindex].sh_addralign); 284 shdr[ef->symtabindex].sh_addr = (Elf_Addr)lastaddr; 285 lastaddr += shdr[ef->symtabindex].sh_size; 286 287 symstrindex = shdr[ef->symtabindex].sh_link; 288 if (symstrindex < 0 || symstrindex >= hdr->e_shnum || 289 shdr[symstrindex].sh_type != SHT_STRTAB) { 290 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 291 "_obj_loadimage: file has invalid symbol strings\n"); 292 goto out; 293 } 294 lastaddr = roundup(lastaddr, shdr[symstrindex].sh_addralign); 295 shdr[symstrindex].sh_addr = (Elf_Addr)lastaddr; 296 lastaddr += shdr[symstrindex].sh_size; 297 298 /* Section names. */ 299 if (hdr->e_shstrndx == 0 || hdr->e_shstrndx >= hdr->e_shnum || 300 shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) { 301 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 302 "_obj_loadimage: file has no section names\n"); 303 goto out; 304 } 305 ef->shstrindex = hdr->e_shstrndx; 306 lastaddr = roundup(lastaddr, shdr[ef->shstrindex].sh_addralign); 307 shdr[ef->shstrindex].sh_addr = (Elf_Addr)lastaddr; 308 lastaddr += shdr[ef->shstrindex].sh_size; 309 310 /* Relocation tables. */ 311 for (i = 0; i < hdr->e_shnum; i++) { 312 switch (shdr[i].sh_type) { 313 case SHT_REL: 314 case SHT_RELA: 315 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0) 316 break; 317 lastaddr = roundup(lastaddr, shdr[i].sh_addralign); 318 shdr[i].sh_addr = (Elf_Addr)lastaddr; 319 lastaddr += shdr[i].sh_size; 320 break; 321 } 322 } 323 324 /* Clear the whole area, including bss regions. */ 325 kern_bzero(firstaddr, lastaddr - firstaddr); 326 327 /* Figure section with the lowest file offset we haven't loaded yet. */ 328 for (cshdr = NULL; /* none */; /* none */) 329 { 330 /* 331 * Find next section to load. The complexity of this loop is 332 * O(n^2), but with the number of sections being typically 333 * small, we do not care. 334 */ 335 lshdr = cshdr; 336 337 for (i = 0; i < hdr->e_shnum; i++) { 338 if (shdr[i].sh_addr == 0 || 339 shdr[i].sh_type == SHT_NOBITS) 340 continue; 341 /* Skip sections that were loaded already. */ 342 if (lshdr != NULL && 343 lshdr->sh_offset >= shdr[i].sh_offset) 344 continue; 345 /* Find section with smallest offset. */ 346 if (cshdr == lshdr || 347 cshdr->sh_offset > shdr[i].sh_offset) 348 cshdr = &shdr[i]; 349 } 350 351 if (cshdr == lshdr) 352 break; 353 354 if (kern_pread(VECTX_HANDLE(ef), (vm_offset_t)cshdr->sh_addr, 355 cshdr->sh_size, (off_t)cshdr->sh_offset) != 0) { 356 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 357 "_obj_loadimage: read failed\n"); 358 goto out; 359 } 360 } 361 362 file_addmetadata(fp, MODINFOMD_SHDR, shdrbytes, shdr); 363 364 res = __elfN(obj_parse_modmetadata)(fp, ef); 365 if (res != 0) 366 goto out; 367 368 ret = lastaddr - firstaddr; 369 fp->f_addr = firstaddr; 370 371 if (module_verbose > MODULE_VERBOSE_SILENT) 372 printf("size 0x%lx at 0x%lx", (u_long)ret, (u_long)firstaddr); 373 374 out: 375 if (module_verbose > MODULE_VERBOSE_SILENT) 376 printf("\n"); 377 return ret; 378 } 379 380 #if defined(__i386__) && __ELF_WORD_SIZE == 64 381 struct mod_metadata64 { 382 int md_version; /* structure version MDTV_* */ 383 int md_type; /* type of entry MDT_* */ 384 uint64_t md_data; /* specific data */ 385 uint64_t md_cval; /* common string label */ 386 }; 387 #endif 388 389 int 390 __elfN(obj_parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef) 391 { 392 struct mod_metadata md; 393 #if defined(__i386__) && __ELF_WORD_SIZE == 64 394 struct mod_metadata64 md64; 395 #endif 396 struct mod_depend *mdepend; 397 struct mod_version mver; 398 char *s; 399 int error, modcnt, minfolen; 400 Elf_Addr v, p, p_stop; 401 402 if (__elfN(obj_lookup_set)(fp, ef, "modmetadata_set", &p, &p_stop, 403 &modcnt) != 0) 404 return 0; 405 406 modcnt = 0; 407 while (p < p_stop) { 408 COPYOUT(p, &v, sizeof(v)); 409 error = __elfN(obj_reloc_ptr)(fp, ef, p, &v, sizeof(v)); 410 if (error != 0) 411 return (error); 412 #if defined(__i386__) && __ELF_WORD_SIZE == 64 413 COPYOUT(v, &md64, sizeof(md64)); 414 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md64, sizeof(md64)); 415 if (error != 0) 416 return (error); 417 md.md_version = md64.md_version; 418 md.md_type = md64.md_type; 419 md.md_cval = (const char *)(uintptr_t)md64.md_cval; 420 md.md_data = (void *)(uintptr_t)md64.md_data; 421 #else 422 COPYOUT(v, &md, sizeof(md)); 423 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md, sizeof(md)); 424 if (error != 0) 425 return (error); 426 #endif 427 p += sizeof(Elf_Addr); 428 switch(md.md_type) { 429 case MDT_DEPEND: 430 s = strdupout((vm_offset_t)md.md_cval); 431 minfolen = sizeof(*mdepend) + strlen(s) + 1; 432 mdepend = malloc(minfolen); 433 if (mdepend == NULL) 434 return ENOMEM; 435 COPYOUT((vm_offset_t)md.md_data, mdepend, 436 sizeof(*mdepend)); 437 strcpy((char*)(mdepend + 1), s); 438 free(s); 439 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, 440 mdepend); 441 free(mdepend); 442 break; 443 case MDT_VERSION: 444 s = strdupout((vm_offset_t)md.md_cval); 445 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver)); 446 file_addmodule(fp, s, mver.mv_version, NULL); 447 free(s); 448 modcnt++; 449 break; 450 case MDT_MODULE: 451 case MDT_PNP_INFO: 452 break; 453 default: 454 printf("unknown type %d\n", md.md_type); 455 break; 456 } 457 } 458 return 0; 459 } 460 461 static int 462 __elfN(obj_lookup_set)(struct preloaded_file *fp, elf_file_t ef, 463 const char* name, Elf_Addr *startp, Elf_Addr *stopp, int *countp) 464 { 465 Elf_Ehdr *hdr; 466 Elf_Shdr *shdr; 467 char *p; 468 vm_offset_t shstrtab; 469 int i; 470 471 hdr = &ef->hdr; 472 shdr = ef->e_shdr; 473 shstrtab = shdr[ef->shstrindex].sh_addr; 474 475 for (i = 0; i < hdr->e_shnum; i++) { 476 if (shdr[i].sh_type != SHT_PROGBITS) 477 continue; 478 if (shdr[i].sh_name == 0) 479 continue; 480 p = strdupout(shstrtab + shdr[i].sh_name); 481 if (strncmp(p, "set_", 4) == 0 && strcmp(p + 4, name) == 0) { 482 *startp = shdr[i].sh_addr; 483 *stopp = shdr[i].sh_addr + shdr[i].sh_size; 484 *countp = (*stopp - *startp) / sizeof(Elf_Addr); 485 free(p); 486 return (0); 487 } 488 free(p); 489 } 490 491 return (ESRCH); 492 } 493 494 /* 495 * Apply any intra-module relocations to the value. p is the load address 496 * of the value and val/len is the value to be modified. This does NOT modify 497 * the image in-place, because this is done by kern_linker later on. 498 */ 499 static int 500 __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, Elf_Addr p, 501 void *val, size_t len) 502 { 503 Elf_Ehdr *hdr; 504 Elf_Shdr *shdr; 505 Elf_Addr off = p; 506 Elf_Addr base; 507 Elf_Rela a, *abase; 508 Elf_Rel r, *rbase; 509 int error, i, j, nrel, nrela; 510 511 hdr = &ef->hdr; 512 shdr = ef->e_shdr; 513 514 for (i = 0; i < hdr->e_shnum; i++) { 515 if (shdr[i].sh_type != SHT_RELA && shdr[i].sh_type != SHT_REL) 516 continue; 517 base = shdr[shdr[i].sh_info].sh_addr; 518 if (base == 0 || shdr[i].sh_addr == 0) 519 continue; 520 if (off < base || off + len > base + 521 shdr[shdr[i].sh_info].sh_size) 522 continue; 523 524 switch (shdr[i].sh_type) { 525 case SHT_RELA: 526 abase = (Elf_Rela *)(intptr_t)shdr[i].sh_addr; 527 528 nrela = shdr[i].sh_size / sizeof(Elf_Rela); 529 for (j = 0; j < nrela; j++) { 530 COPYOUT(abase + j, &a, sizeof(a)); 531 532 error = __elfN(reloc)(ef, __elfN(obj_symaddr), 533 &a, ELF_RELOC_RELA, base, off, val, len); 534 if (error != 0) 535 return (error); 536 } 537 break; 538 case SHT_REL: 539 rbase = (Elf_Rel *)(intptr_t)shdr[i].sh_addr; 540 541 nrel = shdr[i].sh_size / sizeof(Elf_Rel); 542 for (j = 0; j < nrel; j++) { 543 COPYOUT(rbase + j, &r, sizeof(r)); 544 545 error = __elfN(reloc)(ef, __elfN(obj_symaddr), 546 &r, ELF_RELOC_REL, base, off, val, len); 547 if (error != 0) 548 return (error); 549 } 550 break; 551 } 552 } 553 return (0); 554 } 555 556 /* Look up the address of a specified symbol. */ 557 static Elf_Addr 558 __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx) 559 { 560 Elf_Sym sym; 561 Elf_Addr base; 562 563 if (symidx >= ef->e_shdr[ef->symtabindex].sh_size / sizeof(Elf_Sym)) 564 return (0); 565 COPYOUT(ef->e_shdr[ef->symtabindex].sh_addr + symidx * sizeof(Elf_Sym), 566 &sym, sizeof(sym)); 567 if (sym.st_shndx == SHN_UNDEF || sym.st_shndx >= ef->hdr.e_shnum) 568 return (0); 569 base = ef->e_shdr[sym.st_shndx].sh_addr; 570 if (base == 0) 571 return (0); 572 return (base + sym.st_value); 573 } 574