1 /* $NetBSD: loadfile_elf32.c,v 1.25 2010/03/12 21:43:11 darran Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center and by Christos Zoulas. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* If not included by exec_elf64.c, ELFSIZE won't be defined. */ 34 #ifndef ELFSIZE 35 #define ELFSIZE 32 36 #endif 37 38 #ifdef _STANDALONE 39 #include <lib/libsa/stand.h> 40 #include <lib/libkern/libkern.h> 41 #else 42 #include <stdio.h> 43 #include <string.h> 44 #include <errno.h> 45 #include <stdlib.h> 46 #include <unistd.h> 47 #include <fcntl.h> 48 #include <err.h> 49 #endif 50 51 #include <sys/param.h> 52 #include <sys/exec.h> 53 54 #include "loadfile.h" 55 56 #if ((ELFSIZE == 32) && defined(BOOT_ELF32)) || \ 57 ((ELFSIZE == 64) && defined(BOOT_ELF64)) 58 59 #define ELFROUND (ELFSIZE / 8) 60 61 #ifndef _STANDALONE 62 #include "byteorder.h" 63 64 /* 65 * Byte swapping may be necessary in the non-_STANDLONE case because 66 * we may be built with a host compiler. 67 */ 68 #define E16(f) \ 69 f = (bo == ELFDATA2LSB) ? sa_htole16(f) : sa_htobe16(f) 70 #define E32(f) \ 71 f = (bo == ELFDATA2LSB) ? sa_htole32(f) : sa_htobe32(f) 72 #define E64(f) \ 73 f = (bo == ELFDATA2LSB) ? sa_htole64(f) : sa_htobe64(f) 74 75 #define I16(f) \ 76 f = (bo == ELFDATA2LSB) ? sa_le16toh(f) : sa_be16toh(f) 77 #define I32(f) \ 78 f = (bo == ELFDATA2LSB) ? sa_le32toh(f) : sa_be32toh(f) 79 #define I64(f) \ 80 f = (bo == ELFDATA2LSB) ? sa_le64toh(f) : sa_be64toh(f) 81 82 static void 83 internalize_ehdr(Elf_Byte bo, Elf_Ehdr *ehdr) 84 { 85 86 #if ELFSIZE == 32 87 I16(ehdr->e_type); 88 I16(ehdr->e_machine); 89 I32(ehdr->e_version); 90 I32(ehdr->e_entry); 91 I32(ehdr->e_phoff); 92 I32(ehdr->e_shoff); 93 I32(ehdr->e_flags); 94 I16(ehdr->e_ehsize); 95 I16(ehdr->e_phentsize); 96 I16(ehdr->e_phnum); 97 I16(ehdr->e_shentsize); 98 I16(ehdr->e_shnum); 99 I16(ehdr->e_shstrndx); 100 #elif ELFSIZE == 64 101 I16(ehdr->e_type); 102 I16(ehdr->e_machine); 103 I32(ehdr->e_version); 104 I64(ehdr->e_entry); 105 I64(ehdr->e_phoff); 106 I64(ehdr->e_shoff); 107 I32(ehdr->e_flags); 108 I16(ehdr->e_ehsize); 109 I16(ehdr->e_phentsize); 110 I16(ehdr->e_phnum); 111 I16(ehdr->e_shentsize); 112 I16(ehdr->e_shnum); 113 I16(ehdr->e_shstrndx); 114 #else 115 #error ELFSIZE is not 32 or 64 116 #endif 117 } 118 119 static void 120 externalize_ehdr(Elf_Byte bo, Elf_Ehdr *ehdr) 121 { 122 123 #if ELFSIZE == 32 124 E16(ehdr->e_type); 125 E16(ehdr->e_machine); 126 E32(ehdr->e_version); 127 E32(ehdr->e_entry); 128 E32(ehdr->e_phoff); 129 E32(ehdr->e_shoff); 130 E32(ehdr->e_flags); 131 E16(ehdr->e_ehsize); 132 E16(ehdr->e_phentsize); 133 E16(ehdr->e_phnum); 134 E16(ehdr->e_shentsize); 135 E16(ehdr->e_shnum); 136 E16(ehdr->e_shstrndx); 137 #elif ELFSIZE == 64 138 E16(ehdr->e_type); 139 E16(ehdr->e_machine); 140 E32(ehdr->e_version); 141 E64(ehdr->e_entry); 142 E64(ehdr->e_phoff); 143 E64(ehdr->e_shoff); 144 E32(ehdr->e_flags); 145 E16(ehdr->e_ehsize); 146 E16(ehdr->e_phentsize); 147 E16(ehdr->e_phnum); 148 E16(ehdr->e_shentsize); 149 E16(ehdr->e_shnum); 150 E16(ehdr->e_shstrndx); 151 #else 152 #error ELFSIZE is not 32 or 64 153 #endif 154 } 155 156 static void 157 internalize_phdr(Elf_Byte bo, Elf_Phdr *phdr) 158 { 159 160 #if ELFSIZE == 32 161 I32(phdr->p_type); 162 I32(phdr->p_offset); 163 I32(phdr->p_vaddr); 164 I32(phdr->p_paddr); 165 I32(phdr->p_filesz); 166 I32(phdr->p_memsz); 167 I32(phdr->p_flags); 168 I32(phdr->p_align); 169 #elif ELFSIZE == 64 170 I32(phdr->p_type); 171 I32(phdr->p_offset); 172 I64(phdr->p_vaddr); 173 I64(phdr->p_paddr); 174 I64(phdr->p_filesz); 175 I64(phdr->p_memsz); 176 I64(phdr->p_flags); 177 I64(phdr->p_align); 178 #else 179 #error ELFSIZE is not 32 or 64 180 #endif 181 } 182 183 static void 184 internalize_shdr(Elf_Byte bo, Elf_Shdr *shdr) 185 { 186 187 #if ELFSIZE == 32 188 I32(shdr->sh_name); 189 I32(shdr->sh_type); 190 I32(shdr->sh_flags); 191 I32(shdr->sh_addr); 192 I32(shdr->sh_offset); 193 I32(shdr->sh_size); 194 I32(shdr->sh_link); 195 I32(shdr->sh_info); 196 I32(shdr->sh_addralign); 197 I32(shdr->sh_entsize); 198 #elif ELFSIZE == 64 199 I32(shdr->sh_name); 200 I32(shdr->sh_type); 201 I64(shdr->sh_flags); 202 I64(shdr->sh_addr); 203 I64(shdr->sh_offset); 204 I64(shdr->sh_size); 205 I32(shdr->sh_link); 206 I32(shdr->sh_info); 207 I64(shdr->sh_addralign); 208 I64(shdr->sh_entsize); 209 #else 210 #error ELFSIZE is not 32 or 64 211 #endif 212 } 213 214 static void 215 externalize_shdr(Elf_Byte bo, Elf_Shdr *shdr) 216 { 217 218 #if ELFSIZE == 32 219 E32(shdr->sh_name); 220 E32(shdr->sh_type); 221 E32(shdr->sh_flags); 222 E32(shdr->sh_addr); 223 E32(shdr->sh_offset); 224 E32(shdr->sh_size); 225 E32(shdr->sh_link); 226 E32(shdr->sh_info); 227 E32(shdr->sh_addralign); 228 E32(shdr->sh_entsize); 229 #elif ELFSIZE == 64 230 E32(shdr->sh_name); 231 E32(shdr->sh_type); 232 E64(shdr->sh_flags); 233 E64(shdr->sh_addr); 234 E64(shdr->sh_offset); 235 E64(shdr->sh_size); 236 E32(shdr->sh_link); 237 E32(shdr->sh_info); 238 E64(shdr->sh_addralign); 239 E64(shdr->sh_entsize); 240 #else 241 #error ELFSIZE is not 32 or 64 242 #endif 243 } 244 #else /* _STANDALONE */ 245 /* 246 * Byte swapping is never necessary in the _STANDALONE case because 247 * we are being built with the target compiler. 248 */ 249 #define internalize_ehdr(bo, ehdr) /* nothing */ 250 #define externalize_ehdr(bo, ehdr) /* nothing */ 251 252 #define internalize_phdr(bo, phdr) /* nothing */ 253 254 #define internalize_shdr(bo, shdr) /* nothing */ 255 #define externalize_shdr(bo, shdr) /* nothing */ 256 #endif /* _STANDALONE */ 257 258 int 259 ELFNAMEEND(loadfile)(int fd, Elf_Ehdr *elf, u_long *marks, int flags) 260 { 261 Elf_Shdr *shp; 262 Elf_Phdr *phdr; 263 int i, j; 264 ssize_t sz; 265 int first; 266 paddr_t minp = ~0, maxp = 0, pos = 0; 267 paddr_t offset = marks[MARK_START], shpp, elfp = 0; 268 ssize_t nr; 269 struct __packed { 270 Elf_Nhdr nh; 271 uint8_t name[ELF_NOTE_NETBSD_NAMESZ + 1]; 272 uint8_t desc[ELF_NOTE_NETBSD_DESCSZ]; 273 } note; 274 char *shstr = NULL; 275 int boot_load_ctf=1; 276 277 /* some ports dont use the offset */ 278 offset = offset; 279 280 internalize_ehdr(elf->e_ident[EI_DATA], elf); 281 282 sz = elf->e_phnum * sizeof(Elf_Phdr); 283 phdr = ALLOC(sz); 284 285 if (lseek(fd, elf->e_phoff, SEEK_SET) == -1) { 286 WARN(("lseek phdr")); 287 goto freephdr; 288 } 289 nr = read(fd, phdr, sz); 290 if (nr == -1) { 291 WARN(("read program headers")); 292 goto freephdr; 293 } 294 if (nr != sz) { 295 errno = EIO; 296 WARN(("read program headers")); 297 goto freephdr; 298 } 299 300 for (first = 1, i = 0; i < elf->e_phnum; i++) { 301 internalize_phdr(elf->e_ident[EI_DATA], &phdr[i]); 302 303 #ifndef MD_LOADSEG /* Allow processor ABI specific segment loads */ 304 #define MD_LOADSEG(a) /*CONSTCOND*/0 305 #endif 306 if (MD_LOADSEG(&phdr[i])) 307 goto loadseg; 308 309 if (phdr[i].p_type != PT_LOAD || 310 (phdr[i].p_flags & (PF_W|PF_X)) == 0) 311 continue; 312 313 #define IS_TEXT(p) (p.p_flags & PF_X) 314 #define IS_DATA(p) (p.p_flags & PF_W) 315 #define IS_BSS(p) (p.p_filesz < p.p_memsz) 316 /* 317 * XXX: Assume first address is lowest 318 */ 319 if ((IS_TEXT(phdr[i]) && (flags & LOAD_TEXT)) || 320 (IS_DATA(phdr[i]) && (flags & LOAD_DATA))) { 321 322 loadseg: 323 if (marks[MARK_DATA] == 0 && IS_DATA(phdr[i])) 324 marks[MARK_DATA] = LOADADDR(phdr[i].p_vaddr); 325 326 /* Read in segment. */ 327 PROGRESS(("%s%lu", first ? "" : "+", 328 (u_long)phdr[i].p_filesz)); 329 330 if (lseek(fd, phdr[i].p_offset, SEEK_SET) == -1) { 331 WARN(("lseek text")); 332 goto freephdr; 333 } 334 nr = READ(fd, phdr[i].p_vaddr, phdr[i].p_filesz); 335 if (nr == -1) { 336 WARN(("read text error")); 337 goto freephdr; 338 } 339 if (nr != (ssize_t)phdr[i].p_filesz) { 340 errno = EIO; 341 WARN(("read text")); 342 goto freephdr; 343 } 344 first = 0; 345 346 } 347 if ((IS_TEXT(phdr[i]) && (flags & (LOAD_TEXT|COUNT_TEXT))) || 348 (IS_DATA(phdr[i]) && (flags & (LOAD_DATA|COUNT_TEXT)))) { 349 pos = phdr[i].p_vaddr; 350 if (minp > pos) 351 minp = pos; 352 pos += phdr[i].p_filesz; 353 if (maxp < pos) 354 maxp = pos; 355 } 356 357 /* Zero out bss. */ 358 if (IS_BSS(phdr[i]) && (flags & LOAD_BSS)) { 359 PROGRESS(("+%lu", 360 (u_long)(phdr[i].p_memsz - phdr[i].p_filesz))); 361 BZERO((phdr[i].p_vaddr + phdr[i].p_filesz), 362 phdr[i].p_memsz - phdr[i].p_filesz); 363 } 364 if (IS_BSS(phdr[i]) && (flags & (LOAD_BSS|COUNT_BSS))) { 365 pos += phdr[i].p_memsz - phdr[i].p_filesz; 366 if (maxp < pos) 367 maxp = pos; 368 } 369 } 370 DEALLOC(phdr, sz); 371 372 /* 373 * Copy the ELF and section headers. 374 */ 375 maxp = roundup(maxp, ELFROUND); 376 if (flags & (LOAD_HDR|COUNT_HDR)) { 377 elfp = maxp; 378 maxp += sizeof(Elf_Ehdr); 379 } 380 381 if (flags & (LOAD_SYM|COUNT_SYM)) { 382 if (lseek(fd, elf->e_shoff, SEEK_SET) == -1) { 383 WARN(("lseek section headers")); 384 return 1; 385 } 386 sz = elf->e_shnum * sizeof(Elf_Shdr); 387 388 shp = ALLOC(sz); 389 390 nr = read(fd, shp, sz); 391 if (nr == -1) { 392 WARN(("read section headers")); 393 goto freeshp; 394 } 395 if (nr != sz) { 396 errno = EIO; 397 WARN(("read section headers")); 398 goto freeshp; 399 } 400 401 shpp = maxp; 402 maxp += roundup(sz, ELFROUND); 403 404 #ifndef _STANDALONE 405 /* Internalize the section headers. */ 406 for (i = 0; i < elf->e_shnum; i++) 407 internalize_shdr(elf->e_ident[EI_DATA], &shp[i]); 408 #endif /* ! _STANDALONE */ 409 410 /* 411 * First load the section names section. 412 */ 413 if (boot_load_ctf && (elf->e_shstrndx != 0)) { 414 if (lseek(fd, shp[elf->e_shstrndx].sh_offset, 415 SEEK_SET) == -1) { 416 WARN(("lseek symbols")); 417 goto freeshp; 418 } 419 nr = READ(fd, maxp, shp[elf->e_shstrndx].sh_size); 420 if (nr == -1) { 421 WARN(("read symbols")); 422 goto freeshp; 423 } 424 if (nr != (ssize_t)shp[elf->e_shstrndx].sh_size) { 425 errno = EIO; 426 WARN(("read symbols")); 427 goto freeshp; 428 } 429 430 shstr = ALLOC(shp[elf->e_shstrndx].sh_size); 431 if (lseek(fd, shp[elf->e_shstrndx].sh_offset, 432 SEEK_SET) == -1) { 433 WARN(("lseek symbols")); 434 goto freeshp; 435 } 436 nr = read(fd, shstr, shp[elf->e_shstrndx].sh_size); 437 if (nr == -1) { 438 WARN(("read symbols")); 439 goto freeshp; 440 } 441 442 shp[elf->e_shstrndx].sh_offset = maxp - elfp; 443 maxp += roundup(shp[elf->e_shstrndx].sh_size, ELFROUND); 444 } 445 446 /* 447 * Now load the symbol sections themselves. Make sure 448 * the sections are aligned. Don't bother with any 449 * string table that isn't referenced by a symbol 450 * table. 451 */ 452 for (first = 1, i = 0; i < elf->e_shnum; i++) { 453 if (i == elf->e_shstrndx) { 454 /* already loaded this section */ 455 continue; 456 } 457 switch (shp[i].sh_type) { 458 case SHT_PROGBITS: 459 if (boot_load_ctf && shstr) { 460 /* got a CTF section? */ 461 if (strncmp(".SUNW_ctf", 462 &shstr[shp[i].sh_name], 463 10) == 0) { 464 goto havesym; 465 } 466 } 467 468 /* Not loading this, so zero out the offset. */ 469 shp[i].sh_offset = 0; 470 break; 471 case SHT_STRTAB: 472 for (j = 0; j < elf->e_shnum; j++) 473 if (shp[j].sh_type == SHT_SYMTAB && 474 shp[j].sh_link == (unsigned int)i) 475 goto havesym; 476 /* FALLTHROUGH */ 477 default: 478 /* Not loading this, so zero out the offset. */ 479 shp[i].sh_offset = 0; 480 break; 481 havesym: 482 case SHT_SYMTAB: 483 if (flags & LOAD_SYM) { 484 PROGRESS(("%s%ld", first ? " [" : "+", 485 (u_long)shp[i].sh_size)); 486 if (lseek(fd, shp[i].sh_offset, 487 SEEK_SET) == -1) { 488 WARN(("lseek symbols")); 489 goto freeshp; 490 } 491 nr = READ(fd, maxp, shp[i].sh_size); 492 if (nr == -1) { 493 WARN(("read symbols")); 494 goto freeshp; 495 } 496 if (nr != (ssize_t)shp[i].sh_size) { 497 errno = EIO; 498 WARN(("read symbols")); 499 goto freeshp; 500 } 501 } 502 shp[i].sh_offset = maxp - elfp; 503 maxp += roundup(shp[i].sh_size, ELFROUND); 504 first = 0; 505 break; 506 case SHT_NOTE: 507 if ((flags & LOAD_NOTE) == 0) 508 break; 509 if (shp[i].sh_size < sizeof(note)) { 510 shp[i].sh_offset = 0; 511 break; 512 } 513 if (lseek(fd, shp[i].sh_offset, SEEK_SET) 514 == -1) { 515 WARN(("lseek note")); 516 goto freeshp; 517 } 518 nr = read(fd, ¬e, sizeof(note)); 519 if (nr == -1) { 520 WARN(("read note")); 521 goto freeshp; 522 } 523 if (note.nh.n_namesz == 524 ELF_NOTE_NETBSD_NAMESZ && 525 note.nh.n_descsz == 526 ELF_NOTE_NETBSD_DESCSZ && 527 note.nh.n_type == 528 ELF_NOTE_TYPE_NETBSD_TAG && 529 memcmp(note.name, ELF_NOTE_NETBSD_NAME, 530 sizeof(note.name)) == 0) { 531 memcpy(&netbsd_version, ¬e.desc, 532 sizeof(netbsd_version)); 533 } 534 shp[i].sh_offset = 0; 535 break; 536 } 537 } 538 if (flags & LOAD_SYM) { 539 #ifndef _STANDALONE 540 /* Externalize the section headers. */ 541 for (i = 0; i < elf->e_shnum; i++) 542 externalize_shdr(elf->e_ident[EI_DATA], 543 &shp[i]); 544 #endif /* ! _STANDALONE */ 545 BCOPY(shp, shpp, sz); 546 547 if (first == 0) 548 PROGRESS(("]")); 549 } 550 DEALLOC(shp, sz); 551 } 552 553 if (shstr) { 554 DEALLOC(shstr, shp[elf->e_shstrndx].sh_size); 555 } 556 557 /* 558 * Frob the copied ELF header to give information relative 559 * to elfp. 560 */ 561 if (flags & LOAD_HDR) { 562 elf->e_phoff = 0; 563 elf->e_shoff = sizeof(Elf_Ehdr); 564 elf->e_phentsize = 0; 565 elf->e_phnum = 0; 566 externalize_ehdr(elf->e_ident[EI_DATA], elf); 567 BCOPY(elf, elfp, sizeof(*elf)); 568 internalize_ehdr(elf->e_ident[EI_DATA], elf); 569 } 570 571 marks[MARK_START] = LOADADDR(minp); 572 marks[MARK_ENTRY] = LOADADDR(elf->e_entry); 573 /* 574 * Since there can be more than one symbol section in the code 575 * and we need to find strtab too in order to do anything 576 * useful with the symbols, we just pass the whole elf 577 * header back and we let the kernel debugger find the 578 * location and number of symbols by itself. 579 */ 580 marks[MARK_NSYM] = 1; /* XXX: Kernel needs >= 0 */ 581 marks[MARK_SYM] = LOADADDR(elfp); 582 marks[MARK_END] = LOADADDR(maxp); 583 return 0; 584 freephdr: 585 DEALLOC(phdr, sz); 586 return 1; 587 freeshp: 588 DEALLOC(shp, sz); 589 return 1; 590 } 591 592 #ifdef TEST 593 #include <stdlib.h> 594 #include <fcntl.h> 595 #include <err.h> 596 #include <stdio.h> 597 u_int32_t netbsd_version; 598 int 599 main(int argc, char *argv[]) 600 { 601 int fd; 602 u_long marks[MARK_MAX]; 603 Elf_Ehdr elf; 604 if (argc != 2) { 605 (void)fprintf(stderr, "Usage: %s <file>\n", getprogname()); 606 return 1; 607 } 608 if ((fd = open(argv[1], O_RDONLY)) == -1) 609 err(1, "Can't open `%s'", argv[1]); 610 if (read(fd, &elf, sizeof(elf)) != sizeof(elf)) 611 err(1, "Can't read `%s'", argv[1]); 612 memset(marks, 0, sizeof(marks)); 613 marks[MARK_START] = (u_long)malloc(2LL * 1024 * 2024 * 1024); 614 ELFNAMEEND(loadfile)(fd, &elf, marks, LOAD_ALL); 615 printf("%d\n", netbsd_version); 616 return 0; 617 } 618 #endif 619 620 #endif /* (ELFSIZE == 32 && BOOT_ELF32) || (ELFSIZE == 64 && BOOT_ELF64) */ 621