load_elf_obj.c revision 329183
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/cdefs.h> 30__FBSDID("$FreeBSD: stable/11/stand/common/load_elf_obj.c 329183 2018-02-12 20:51:28Z kevans $"); 31 32#include <sys/param.h> 33#include <sys/exec.h> 34#include <sys/linker.h> 35#include <sys/module.h> 36#include <stdint.h> 37#include <string.h> 38#include <machine/elf.h> 39#include <stand.h> 40#define FREEBSD_ELF 41#include <sys/link_elf.h> 42 43#include "bootstrap.h" 44 45#define COPYOUT(s,d,l) archsw.arch_copyout((vm_offset_t)(s), d, l) 46 47#if defined(__i386__) && __ELF_WORD_SIZE == 64 48#undef ELF_TARG_CLASS 49#undef ELF_TARG_MACH 50#define ELF_TARG_CLASS ELFCLASS64 51#define ELF_TARG_MACH EM_X86_64 52#endif 53 54typedef struct elf_file { 55 Elf_Ehdr hdr; 56 Elf_Shdr *e_shdr; 57 58 int symtabindex; /* Index of symbol table */ 59 int shstrindex; /* Index of section name string table */ 60 61 int fd; 62 vm_offset_t off; 63} *elf_file_t; 64 65static int __elfN(obj_loadimage)(struct preloaded_file *mp, elf_file_t ef, 66 u_int64_t loadaddr); 67static int __elfN(obj_lookup_set)(struct preloaded_file *mp, elf_file_t ef, 68 const char *name, Elf_Addr *startp, Elf_Addr *stopp, int *countp); 69static int __elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, 70 Elf_Addr p, void *val, size_t len); 71static int __elfN(obj_parse_modmetadata)(struct preloaded_file *mp, 72 elf_file_t ef); 73static Elf_Addr __elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx); 74 75const char *__elfN(obj_kerneltype) = "elf kernel"; 76const char *__elfN(obj_moduletype) = "elf obj module"; 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 */ 83int 84__elfN(obj_loadfile)(char *filename, u_int64_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 104 hdr = &ef.hdr; 105 bytes_read = read(ef.fd, hdr, sizeof(*hdr)); 106 if (bytes_read != sizeof(*hdr)) { 107 err = EFTYPE; /* could be EIO, but may be small file */ 108 goto oerr; 109 } 110 111 /* Is it ELF? */ 112 if (!IS_ELF(*hdr)) { 113 err = EFTYPE; 114 goto oerr; 115 } 116 if (hdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || /* Layout ? */ 117 hdr->e_ident[EI_DATA] != ELF_TARG_DATA || 118 hdr->e_ident[EI_VERSION] != EV_CURRENT || /* Version ? */ 119 hdr->e_version != EV_CURRENT || 120 hdr->e_machine != ELF_TARG_MACH || /* Machine ? */ 121 hdr->e_type != ET_REL) { 122 err = EFTYPE; 123 goto oerr; 124 } 125 126 if (hdr->e_shnum * hdr->e_shentsize == 0 || hdr->e_shoff == 0 || 127 hdr->e_shentsize != sizeof(Elf_Shdr)) { 128 err = EFTYPE; 129 goto oerr; 130 } 131 132 kfp = file_findfile(NULL, __elfN(obj_kerneltype)); 133 if (kfp == NULL) { 134 printf("elf" __XSTRING(__ELF_WORD_SIZE) 135 "_obj_loadfile: can't load module before kernel\n"); 136 err = EPERM; 137 goto oerr; 138 } 139 140 if (archsw.arch_loadaddr != NULL) 141 dest = archsw.arch_loadaddr(LOAD_ELF, hdr, dest); 142 else 143 dest = roundup(dest, PAGE_SIZE); 144 145 /* 146 * Ok, we think we should handle this. 147 */ 148 fp = file_alloc(); 149 if (fp == NULL) { 150 printf("elf" __XSTRING(__ELF_WORD_SIZE) 151 "_obj_loadfile: cannot allocate module info\n"); 152 err = EPERM; 153 goto out; 154 } 155 fp->f_name = strdup(filename); 156 fp->f_type = strdup(__elfN(obj_moduletype)); 157 158 printf("%s ", filename); 159 160 fp->f_size = __elfN(obj_loadimage)(fp, &ef, dest); 161 if (fp->f_size == 0 || fp->f_addr == 0) 162 goto ioerr; 163 164 /* save exec header as metadata */ 165 file_addmetadata(fp, MODINFOMD_ELFHDR, sizeof(*hdr), hdr); 166 167 /* Load OK, return module pointer */ 168 *result = (struct preloaded_file *)fp; 169 err = 0; 170 goto out; 171 172ioerr: 173 err = EIO; 174oerr: 175 file_discard(fp); 176out: 177 close(ef.fd); 178 if (ef.e_shdr != NULL) 179 free(ef.e_shdr); 180 181 return(err); 182} 183 184/* 185 * With the file (fd) open on the image, and (ehdr) containing 186 * the Elf header, load the image at (off) 187 */ 188static int 189__elfN(obj_loadimage)(struct preloaded_file *fp, elf_file_t ef, u_int64_t off) 190{ 191 Elf_Ehdr *hdr; 192 Elf_Shdr *shdr, *cshdr, *lshdr; 193 vm_offset_t firstaddr, lastaddr; 194 int i, nsym, res, ret, shdrbytes, symstrindex; 195 196 ret = 0; 197 firstaddr = lastaddr = (vm_offset_t)off; 198 hdr = &ef->hdr; 199 ef->off = (vm_offset_t)off; 200 201 /* Read in the section headers. */ 202 shdrbytes = hdr->e_shnum * hdr->e_shentsize; 203 shdr = alloc_pread(ef->fd, (off_t)hdr->e_shoff, shdrbytes); 204 if (shdr == NULL) { 205 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 206 "_obj_loadimage: read section headers failed\n"); 207 goto out; 208 } 209 ef->e_shdr = shdr; 210 211 /* 212 * Decide where to load everything, but don't read it yet. 213 * We store the load address as a non-zero sh_addr value. 214 * Start with the code/data and bss. 215 */ 216 for (i = 0; i < hdr->e_shnum; i++) 217 shdr[i].sh_addr = 0; 218 for (i = 0; i < hdr->e_shnum; i++) { 219 if (shdr[i].sh_size == 0) 220 continue; 221 switch (shdr[i].sh_type) { 222 case SHT_PROGBITS: 223 case SHT_NOBITS: 224#if defined(__i386__) || defined(__amd64__) 225 case SHT_X86_64_UNWIND: 226#endif 227 lastaddr = roundup(lastaddr, shdr[i].sh_addralign); 228 shdr[i].sh_addr = (Elf_Addr)lastaddr; 229 lastaddr += shdr[i].sh_size; 230 break; 231 } 232 } 233 234 /* Symbols. */ 235 nsym = 0; 236 for (i = 0; i < hdr->e_shnum; i++) { 237 switch (shdr[i].sh_type) { 238 case SHT_SYMTAB: 239 nsym++; 240 ef->symtabindex = i; 241 shdr[i].sh_addr = (Elf_Addr)lastaddr; 242 lastaddr += shdr[i].sh_size; 243 break; 244 } 245 } 246 if (nsym != 1) { 247 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 248 "_obj_loadimage: file has no valid symbol table\n"); 249 goto out; 250 } 251 lastaddr = roundup(lastaddr, shdr[ef->symtabindex].sh_addralign); 252 shdr[ef->symtabindex].sh_addr = (Elf_Addr)lastaddr; 253 lastaddr += shdr[ef->symtabindex].sh_size; 254 255 symstrindex = shdr[ef->symtabindex].sh_link; 256 if (symstrindex < 0 || symstrindex >= hdr->e_shnum || 257 shdr[symstrindex].sh_type != SHT_STRTAB) { 258 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 259 "_obj_loadimage: file has invalid symbol strings\n"); 260 goto out; 261 } 262 lastaddr = roundup(lastaddr, shdr[symstrindex].sh_addralign); 263 shdr[symstrindex].sh_addr = (Elf_Addr)lastaddr; 264 lastaddr += shdr[symstrindex].sh_size; 265 266 /* Section names. */ 267 if (hdr->e_shstrndx == 0 || hdr->e_shstrndx >= hdr->e_shnum || 268 shdr[hdr->e_shstrndx].sh_type != SHT_STRTAB) { 269 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 270 "_obj_loadimage: file has no section names\n"); 271 goto out; 272 } 273 ef->shstrindex = hdr->e_shstrndx; 274 lastaddr = roundup(lastaddr, shdr[ef->shstrindex].sh_addralign); 275 shdr[ef->shstrindex].sh_addr = (Elf_Addr)lastaddr; 276 lastaddr += shdr[ef->shstrindex].sh_size; 277 278 /* Relocation tables. */ 279 for (i = 0; i < hdr->e_shnum; i++) { 280 switch (shdr[i].sh_type) { 281 case SHT_REL: 282 case SHT_RELA: 283 if ((shdr[shdr[i].sh_info].sh_flags & SHF_ALLOC) == 0) 284 break; 285 lastaddr = roundup(lastaddr, shdr[i].sh_addralign); 286 shdr[i].sh_addr = (Elf_Addr)lastaddr; 287 lastaddr += shdr[i].sh_size; 288 break; 289 } 290 } 291 292 /* Clear the whole area, including bss regions. */ 293 kern_bzero(firstaddr, lastaddr - firstaddr); 294 295 /* Figure section with the lowest file offset we haven't loaded yet. */ 296 for (cshdr = NULL; /* none */; /* none */) 297 { 298 /* 299 * Find next section to load. The complexity of this loop is 300 * O(n^2), but with the number of sections being typically 301 * small, we do not care. 302 */ 303 lshdr = cshdr; 304 305 for (i = 0; i < hdr->e_shnum; i++) { 306 if (shdr[i].sh_addr == 0 || 307 shdr[i].sh_type == SHT_NOBITS) 308 continue; 309 /* Skip sections that were loaded already. */ 310 if (lshdr != NULL && 311 lshdr->sh_offset >= shdr[i].sh_offset) 312 continue; 313 /* Find section with smallest offset. */ 314 if (cshdr == lshdr || 315 cshdr->sh_offset > shdr[i].sh_offset) 316 cshdr = &shdr[i]; 317 } 318 319 if (cshdr == lshdr) 320 break; 321 322 if (kern_pread(ef->fd, (vm_offset_t)cshdr->sh_addr, 323 cshdr->sh_size, (off_t)cshdr->sh_offset) != 0) { 324 printf("\nelf" __XSTRING(__ELF_WORD_SIZE) 325 "_obj_loadimage: read failed\n"); 326 goto out; 327 } 328 } 329 330 file_addmetadata(fp, MODINFOMD_SHDR, shdrbytes, shdr); 331 332 res = __elfN(obj_parse_modmetadata)(fp, ef); 333 if (res != 0) 334 goto out; 335 336 ret = lastaddr - firstaddr; 337 fp->f_addr = firstaddr; 338 339 printf("size 0x%lx at 0x%lx", (u_long)ret, (u_long)firstaddr); 340 341out: 342 printf("\n"); 343 return ret; 344} 345 346#if defined(__i386__) && __ELF_WORD_SIZE == 64 347struct mod_metadata64 { 348 int md_version; /* structure version MDTV_* */ 349 int md_type; /* type of entry MDT_* */ 350 u_int64_t md_data; /* specific data */ 351 u_int64_t md_cval; /* common string label */ 352}; 353#endif 354 355int 356__elfN(obj_parse_modmetadata)(struct preloaded_file *fp, elf_file_t ef) 357{ 358 struct mod_metadata md; 359#if defined(__i386__) && __ELF_WORD_SIZE == 64 360 struct mod_metadata64 md64; 361#endif 362 struct mod_depend *mdepend; 363 struct mod_version mver; 364 char *s; 365 int error, modcnt, minfolen; 366 Elf_Addr v, p, p_stop; 367 368 if (__elfN(obj_lookup_set)(fp, ef, "modmetadata_set", &p, &p_stop, 369 &modcnt) != 0) 370 return 0; 371 372 modcnt = 0; 373 while (p < p_stop) { 374 COPYOUT(p, &v, sizeof(v)); 375 error = __elfN(obj_reloc_ptr)(fp, ef, p, &v, sizeof(v)); 376 if (error != 0) 377 return (error); 378#if defined(__i386__) && __ELF_WORD_SIZE == 64 379 COPYOUT(v, &md64, sizeof(md64)); 380 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md64, sizeof(md64)); 381 if (error != 0) 382 return (error); 383 md.md_version = md64.md_version; 384 md.md_type = md64.md_type; 385 md.md_cval = (const char *)(uintptr_t)md64.md_cval; 386 md.md_data = (void *)(uintptr_t)md64.md_data; 387#else 388 COPYOUT(v, &md, sizeof(md)); 389 error = __elfN(obj_reloc_ptr)(fp, ef, v, &md, sizeof(md)); 390 if (error != 0) 391 return (error); 392#endif 393 p += sizeof(Elf_Addr); 394 switch(md.md_type) { 395 case MDT_DEPEND: 396 s = strdupout((vm_offset_t)md.md_cval); 397 minfolen = sizeof(*mdepend) + strlen(s) + 1; 398 mdepend = malloc(minfolen); 399 if (mdepend == NULL) 400 return ENOMEM; 401 COPYOUT((vm_offset_t)md.md_data, mdepend, 402 sizeof(*mdepend)); 403 strcpy((char*)(mdepend + 1), s); 404 free(s); 405 file_addmetadata(fp, MODINFOMD_DEPLIST, minfolen, 406 mdepend); 407 free(mdepend); 408 break; 409 case MDT_VERSION: 410 s = strdupout((vm_offset_t)md.md_cval); 411 COPYOUT((vm_offset_t)md.md_data, &mver, sizeof(mver)); 412 file_addmodule(fp, s, mver.mv_version, NULL); 413 free(s); 414 modcnt++; 415 break; 416 case MDT_MODULE: 417 case MDT_PNP_INFO: 418 break; 419 default: 420 printf("unknown type %d\n", md.md_type); 421 break; 422 } 423 } 424 return 0; 425} 426 427static int 428__elfN(obj_lookup_set)(struct preloaded_file *fp, elf_file_t ef, 429 const char* name, Elf_Addr *startp, Elf_Addr *stopp, int *countp) 430{ 431 Elf_Ehdr *hdr; 432 Elf_Shdr *shdr; 433 char *p; 434 vm_offset_t shstrtab; 435 int i; 436 437 hdr = &ef->hdr; 438 shdr = ef->e_shdr; 439 shstrtab = shdr[ef->shstrindex].sh_addr; 440 441 for (i = 0; i < hdr->e_shnum; i++) { 442 if (shdr[i].sh_type != SHT_PROGBITS) 443 continue; 444 if (shdr[i].sh_name == 0) 445 continue; 446 p = strdupout(shstrtab + shdr[i].sh_name); 447 if (strncmp(p, "set_", 4) == 0 && strcmp(p + 4, name) == 0) { 448 *startp = shdr[i].sh_addr; 449 *stopp = shdr[i].sh_addr + shdr[i].sh_size; 450 *countp = (*stopp - *startp) / sizeof(Elf_Addr); 451 free(p); 452 return (0); 453 } 454 free(p); 455 } 456 457 return (ESRCH); 458} 459 460/* 461 * Apply any intra-module relocations to the value. p is the load address 462 * of the value and val/len is the value to be modified. This does NOT modify 463 * the image in-place, because this is done by kern_linker later on. 464 */ 465static int 466__elfN(obj_reloc_ptr)(struct preloaded_file *mp, elf_file_t ef, Elf_Addr p, 467 void *val, size_t len) 468{ 469 Elf_Ehdr *hdr; 470 Elf_Shdr *shdr; 471 Elf_Addr off = p; 472 Elf_Addr base; 473 Elf_Rela a, *abase; 474 Elf_Rel r, *rbase; 475 int error, i, j, nrel, nrela; 476 477 hdr = &ef->hdr; 478 shdr = ef->e_shdr; 479 480 for (i = 0; i < hdr->e_shnum; i++) { 481 if (shdr[i].sh_type != SHT_RELA && shdr[i].sh_type != SHT_REL) 482 continue; 483 base = shdr[shdr[i].sh_info].sh_addr; 484 if (base == 0 || shdr[i].sh_addr == 0) 485 continue; 486 if (off < base || off + len > base + 487 shdr[shdr[i].sh_info].sh_size) 488 continue; 489 490 switch (shdr[i].sh_type) { 491 case SHT_RELA: 492 abase = (Elf_Rela *)(intptr_t)shdr[i].sh_addr; 493 494 nrela = shdr[i].sh_size / sizeof(Elf_Rela); 495 for (j = 0; j < nrela; j++) { 496 COPYOUT(abase + j, &a, sizeof(a)); 497 498 error = __elfN(reloc)(ef, __elfN(obj_symaddr), 499 &a, ELF_RELOC_RELA, base, off, val, len); 500 if (error != 0) 501 return (error); 502 } 503 break; 504 case SHT_REL: 505 rbase = (Elf_Rel *)(intptr_t)shdr[i].sh_addr; 506 507 nrel = shdr[i].sh_size / sizeof(Elf_Rel); 508 for (j = 0; j < nrel; j++) { 509 COPYOUT(rbase + j, &r, sizeof(r)); 510 511 error = __elfN(reloc)(ef, __elfN(obj_symaddr), 512 &r, ELF_RELOC_REL, base, off, val, len); 513 if (error != 0) 514 return (error); 515 } 516 break; 517 } 518 } 519 return (0); 520} 521 522/* Look up the address of a specified symbol. */ 523static Elf_Addr 524__elfN(obj_symaddr)(struct elf_file *ef, Elf_Size symidx) 525{ 526 Elf_Sym sym; 527 Elf_Addr base; 528 529 if (symidx >= ef->e_shdr[ef->symtabindex].sh_size / sizeof(Elf_Sym)) 530 return (0); 531 COPYOUT(ef->e_shdr[ef->symtabindex].sh_addr + symidx * sizeof(Elf_Sym), 532 &sym, sizeof(sym)); 533 if (sym.st_shndx == SHN_UNDEF || sym.st_shndx >= ef->hdr.e_shnum) 534 return (0); 535 base = ef->e_shdr[sym.st_shndx].sh_addr; 536 if (base == 0) 537 return (0); 538 return (base + sym.st_value); 539} 540