1 /*- 2 * Copyright (c) 1998 Doug Rabson 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $Id: link_elf.c,v 1.1 1998/08/24 08:25:26 dfr Exp $ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/kernel.h> 31 #include <sys/systm.h> 32 #include <sys/malloc.h> 33 #include <sys/proc.h> 34 #include <sys/namei.h> 35 #include <sys/fcntl.h> 36 #include <sys/vnode.h> 37 #include <sys/linker.h> 38 #include <machine/elf.h> 39 40 #include <vm/vm.h> 41 #include <vm/vm_prot.h> 42 #include <vm/vm_param.h> 43 #include <sys/lock.h> 44 #include <vm/vm_object.h> 45 #include <vm/vm_kern.h> 46 #include <vm/vm_extern.h> 47 #include <vm/pmap.h> 48 #include <vm/vm_map.h> 49 50 extern int elf_reloc(linker_file_t lf, const Elf_Rela *rela, 51 const char *sym); 52 53 static int link_elf_load_file(const char*, linker_file_t*); 54 static int link_elf_lookup_symbol(linker_file_t, const char*, 55 linker_sym_t*); 56 static void link_elf_symbol_values(linker_file_t, linker_sym_t, linker_symval_t*); 57 static int link_elf_search_symbol(linker_file_t, caddr_t value, 58 linker_sym_t* sym, long* diffp); 59 60 static void link_elf_unload(linker_file_t); 61 62 /* 63 * The file representing the currently running kernel. This contains 64 * the global symbol table. 65 */ 66 67 linker_file_t linker_kernel_file; 68 69 static struct linker_class_ops link_elf_class_ops = { 70 link_elf_load_file, 71 }; 72 73 static struct linker_file_ops link_elf_file_ops = { 74 link_elf_lookup_symbol, 75 link_elf_symbol_values, 76 link_elf_search_symbol, 77 link_elf_unload, 78 }; 79 80 typedef struct elf_file { 81 caddr_t address; /* Relocation address */ 82 #ifdef SPARSE_MAPPING 83 vm_object_t object; /* VM object to hold file pages */ 84 #endif 85 const Elf_Dyn* dynamic; /* Symbol table etc. */ 86 Elf_Off nbuckets; /* DT_HASH info */ 87 Elf_Off nchains; 88 const Elf_Off* buckets; 89 const Elf_Off* chains; 90 caddr_t hash; 91 caddr_t strtab; /* DT_STRTAB */ 92 const Elf_Sym* symtab; /* DT_SYMTAB */ 93 Elf_Addr* got; /* DT_PLTGOT */ 94 const Elf_Rel* pltrel; /* DT_JMPREL */ 95 int pltrelsize; /* DT_PLTRELSZ */ 96 const Elf_Rela* pltrela; /* DT_JMPREL */ 97 int pltrelasize; /* DT_PLTRELSZ */ 98 const Elf_Rel* rel; /* DT_REL */ 99 int relsize; /* DT_RELSZ */ 100 const Elf_Rela* rela; /* DT_RELA */ 101 int relasize; /* DT_RELASZ */ 102 } *elf_file_t; 103 104 static int parse_dynamic(linker_file_t lf); 105 static int load_dependancies(linker_file_t lf); 106 static int relocate_file(linker_file_t lf); 107 108 /* 109 * The kernel symbol table starts here. 110 */ 111 extern struct _dynamic _DYNAMIC; 112 113 static void 114 link_elf_init(void* arg) 115 { 116 Elf_Dyn* dp = (Elf_Dyn*) &_DYNAMIC; 117 118 #if ELF_TARG_CLASS == ELFCLASS32 119 linker_add_class("elf32", NULL, &link_elf_class_ops); 120 #else 121 linker_add_class("elf64", NULL, &link_elf_class_ops); 122 #endif 123 124 if (dp) { 125 elf_file_t ef; 126 127 ef = malloc(sizeof(struct elf_file), M_LINKER, M_NOWAIT); 128 if (ef == NULL) 129 panic("link_elf_init: Can't create linker structures for kernel"); 130 131 ef->address = 0; 132 #ifdef SPARSE_MAPPING 133 ef->object = 0; 134 #endif 135 ef->dynamic = dp; 136 linker_kernel_file = 137 linker_make_file(kernelname, ef, &link_elf_file_ops); 138 if (linker_kernel_file == NULL) 139 panic("link_elf_init: Can't create linker structures for kernel"); 140 parse_dynamic(linker_kernel_file); 141 /* 142 * XXX there must be a better way of getting these constants. 143 */ 144 #ifdef __alpha__ 145 linker_kernel_file->address = (caddr_t) 0xfffffc0000300000; 146 #else 147 linker_kernel_file->address = (caddr_t) 0xf0100000; 148 #endif 149 linker_kernel_file->size = -(long)linker_kernel_file->address; 150 linker_current_file = linker_kernel_file; 151 } 152 } 153 154 SYSINIT(link_elf, SI_SUB_KMEM, SI_ORDER_THIRD, link_elf_init, 0); 155 156 static int 157 parse_dynamic(linker_file_t lf) 158 { 159 elf_file_t ef = lf->priv; 160 const Elf_Dyn *dp; 161 int plttype = DT_REL; 162 163 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) { 164 switch (dp->d_tag) { 165 case DT_HASH: 166 { 167 /* From src/libexec/rtld-elf/rtld.c */ 168 const Elf_Off *hashtab = (const Elf_Off *) 169 (ef->address + dp->d_un.d_ptr); 170 ef->nbuckets = hashtab[0]; 171 ef->nchains = hashtab[1]; 172 ef->buckets = hashtab + 2; 173 ef->chains = ef->buckets + ef->nbuckets; 174 break; 175 } 176 case DT_STRTAB: 177 ef->strtab = (caddr_t) (ef->address + dp->d_un.d_ptr); 178 break; 179 case DT_SYMTAB: 180 ef->symtab = (Elf_Sym*) (ef->address + dp->d_un.d_ptr); 181 break; 182 case DT_SYMENT: 183 if (dp->d_un.d_val != sizeof(Elf_Sym)) 184 return ENOEXEC; 185 break; 186 case DT_PLTGOT: 187 ef->got = (Elf_Addr *) (ef->address + dp->d_un.d_ptr); 188 break; 189 case DT_REL: 190 ef->rel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr); 191 break; 192 case DT_RELSZ: 193 ef->relsize = dp->d_un.d_val; 194 break; 195 case DT_RELENT: 196 if (dp->d_un.d_val != sizeof(Elf_Rel)) 197 return ENOEXEC; 198 break; 199 case DT_JMPREL: 200 ef->pltrel = (const Elf_Rel *) (ef->address + dp->d_un.d_ptr); 201 break; 202 case DT_PLTRELSZ: 203 ef->pltrelsize = dp->d_un.d_val; 204 break; 205 case DT_RELA: 206 ef->rela = (const Elf_Rela *) (ef->address + dp->d_un.d_ptr); 207 break; 208 case DT_RELASZ: 209 ef->relasize = dp->d_un.d_val; 210 break; 211 case DT_RELAENT: 212 if (dp->d_un.d_val != sizeof(Elf_Rela)) 213 return ENOEXEC; 214 break; 215 case DT_PLTREL: 216 plttype = dp->d_un.d_val; 217 if (plttype != DT_REL && plttype != DT_RELA) 218 return ENOEXEC; 219 break; 220 } 221 } 222 223 if (plttype == DT_RELA) { 224 ef->pltrela = (const Elf_Rela *) ef->pltrel; 225 ef->pltrel = NULL; 226 ef->pltrelasize = ef->pltrelsize; 227 ef->pltrelsize = 0; 228 } 229 230 return 0; 231 } 232 233 static void 234 link_elf_error(const char *s) 235 { 236 printf("kldload: %s\n", s); 237 } 238 239 static int 240 link_elf_load_file(const char* filename, linker_file_t* result) 241 { 242 struct nameidata nd; 243 struct proc* p = curproc; /* XXX */ 244 union { 245 Elf_Ehdr hdr; 246 char buf[PAGE_SIZE]; 247 } u; 248 int nbytes, i; 249 Elf_Phdr *phdr; 250 Elf_Phdr *phlimit; 251 Elf_Phdr *segs[2]; 252 int nsegs; 253 Elf_Phdr *phdyn; 254 Elf_Phdr *phphdr; 255 caddr_t mapbase; 256 size_t mapsize; 257 Elf_Off base_offset; 258 Elf_Addr base_vaddr; 259 Elf_Addr base_vlimit; 260 caddr_t base_addr; 261 Elf_Off data_offset; 262 Elf_Addr data_vaddr; 263 Elf_Addr data_vlimit; 264 caddr_t data_addr; 265 Elf_Addr clear_vaddr; 266 caddr_t clear_addr; 267 size_t nclear; 268 Elf_Addr bss_vaddr; 269 Elf_Addr bss_vlimit; 270 caddr_t bss_addr; 271 int error = 0; 272 int resid; 273 elf_file_t ef; 274 linker_file_t lf; 275 276 NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, filename, p); 277 error = vn_open(&nd, FREAD, 0); 278 if (error) 279 return error; 280 281 /* 282 * Read the elf header from the file. 283 */ 284 error = vn_rdwr(UIO_READ, nd.ni_vp, (void*) &u, sizeof u, 0, 285 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p); 286 nbytes = sizeof u - resid; 287 if (error) 288 goto out; 289 290 if (!IS_ELF(u.hdr)) { 291 error = ENOEXEC; 292 goto out; 293 } 294 295 if (u.hdr.e_ident[EI_CLASS] != ELF_TARG_CLASS 296 || u.hdr.e_ident[EI_DATA] != ELF_TARG_DATA) { 297 link_elf_error("Unsupported file layout"); 298 error = ENOEXEC; 299 goto out; 300 } 301 if (u.hdr.e_ident[EI_VERSION] != EV_CURRENT 302 || u.hdr.e_version != EV_CURRENT) { 303 link_elf_error("Unsupported file version"); 304 error = ENOEXEC; 305 goto out; 306 } 307 if (u.hdr.e_type != ET_EXEC && u.hdr.e_type != ET_DYN) { 308 link_elf_error("Unsupported file type"); 309 error = ENOEXEC; 310 goto out; 311 } 312 if (u.hdr.e_machine != ELF_TARG_MACH) { 313 link_elf_error("Unsupported machine"); 314 error = ENOEXEC; 315 goto out; 316 } 317 if (!ELF_MACHINE_OK(u.hdr.e_machine)) { 318 link_elf_error("Incompatibile elf machine type"); 319 error = ENOEXEC; 320 goto out; 321 } 322 323 /* 324 * We rely on the program header being in the first page. This is 325 * not strictly required by the ABI specification, but it seems to 326 * always true in practice. And, it simplifies things considerably. 327 */ 328 if (!((u.hdr.e_phentsize == sizeof(Elf_Phdr)) 329 || (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) <= PAGE_SIZE) 330 || (u.hdr.e_phoff + u.hdr.e_phnum*sizeof(Elf_Phdr) <= nbytes))) 331 link_elf_error("Unreadable program headers"); 332 333 /* 334 * Scan the program header entries, and save key information. 335 * 336 * We rely on there being exactly two load segments, text and data, 337 * in that order. 338 */ 339 phdr = (Elf_Phdr *) (u.buf + u.hdr.e_phoff); 340 phlimit = phdr + u.hdr.e_phnum; 341 nsegs = 0; 342 phdyn = NULL; 343 phphdr = NULL; 344 while (phdr < phlimit) { 345 switch (phdr->p_type) { 346 347 case PT_LOAD: 348 if (nsegs == 2) { 349 link_elf_error("Too many sections"); 350 error = ENOEXEC; 351 goto out; 352 } 353 segs[nsegs] = phdr; 354 ++nsegs; 355 break; 356 357 case PT_PHDR: 358 phphdr = phdr; 359 break; 360 361 case PT_DYNAMIC: 362 phdyn = phdr; 363 break; 364 } 365 366 ++phdr; 367 } 368 if (phdyn == NULL) { 369 link_elf_error("Object is not dynamically-linked"); 370 error = ENOEXEC; 371 goto out; 372 } 373 374 /* 375 * Allocate the entire address space of the object, to stake out our 376 * contiguous region, and to establish the base address for relocation. 377 */ 378 base_offset = trunc_page(segs[0]->p_offset); 379 base_vaddr = trunc_page(segs[0]->p_vaddr); 380 base_vlimit = round_page(segs[1]->p_vaddr + segs[1]->p_memsz); 381 mapsize = base_vlimit - base_vaddr; 382 383 ef = malloc(sizeof(struct elf_file), M_LINKER, M_WAITOK); 384 #ifdef SPARSE_MAPPING 385 ef->object = vm_object_allocate(OBJT_DEFAULT, mapsize >> PAGE_SHIFT); 386 if (ef->object == NULL) { 387 free(ef, M_LINKER); 388 error = ENOMEM; 389 goto out; 390 } 391 vm_object_reference(ef->object); 392 ef->address = (caddr_t) vm_map_min(kernel_map); 393 error = vm_map_find(kernel_map, ef->object, 0, 394 (vm_offset_t *) &ef->address, 395 mapsize, 1, 396 VM_PROT_ALL, VM_PROT_ALL, 0); 397 if (error) { 398 vm_object_deallocate(ef->object); 399 free(ef, M_LINKER); 400 goto out; 401 } 402 #else 403 ef->address = malloc(mapsize, M_LINKER, M_WAITOK); 404 #endif 405 mapbase = ef->address; 406 407 /* 408 * Read the text and data sections and zero the bss. 409 */ 410 for (i = 0; i < 2; i++) { 411 caddr_t segbase = mapbase + segs[i]->p_vaddr - base_vaddr; 412 error = vn_rdwr(UIO_READ, nd.ni_vp, 413 segbase, segs[i]->p_filesz, segs[i]->p_offset, 414 UIO_SYSSPACE, IO_NODELOCKED, p->p_ucred, &resid, p); 415 if (error) { 416 #ifdef SPARSE_MAPPING 417 vm_map_remove(kernel_map, (vm_offset_t) ef->address, 418 (vm_offset_t) ef->address 419 + (ef->object->size << PAGE_SHIFT)); 420 vm_object_deallocate(ef->object); 421 #else 422 free(ef->address, M_LINKER); 423 #endif 424 free(ef, M_LINKER); 425 goto out; 426 } 427 bzero(segbase + segs[i]->p_filesz, 428 segs[i]->p_memsz - segs[i]->p_filesz); 429 430 #ifdef SPARSE_MAPPING 431 /* 432 * Wire down the pages 433 */ 434 vm_map_pageable(kernel_map, 435 (vm_offset_t) segbase, 436 (vm_offset_t) segbase + segs[i]->p_memsz, 437 FALSE); 438 #endif 439 } 440 441 ef->dynamic = (const Elf_Dyn *) (mapbase + phdyn->p_vaddr - base_vaddr); 442 443 lf = linker_make_file(filename, ef, &link_elf_file_ops); 444 if (lf == NULL) { 445 #ifdef SPARSE_MAPPING 446 vm_map_remove(kernel_map, (vm_offset_t) ef->address, 447 (vm_offset_t) ef->address 448 + (ef->object->size << PAGE_SHIFT)); 449 vm_object_deallocate(ef->object); 450 #else 451 free(ef->address, M_LINKER); 452 #endif 453 free(ef, M_LINKER); 454 error = ENOMEM; 455 goto out; 456 } 457 lf->address = ef->address; 458 lf->size = mapsize; 459 460 if ((error = parse_dynamic(lf)) != 0 461 || (error = load_dependancies(lf)) != 0 462 || (error = relocate_file(lf)) != 0) { 463 linker_file_unload(lf); 464 goto out; 465 } 466 467 *result = lf; 468 469 out: 470 VOP_UNLOCK(nd.ni_vp, 0, p); 471 vn_close(nd.ni_vp, FREAD, p->p_ucred, p); 472 473 return error; 474 } 475 476 static void 477 link_elf_unload(linker_file_t file) 478 { 479 elf_file_t ef = file->priv; 480 481 if (ef) { 482 #ifdef SPARSE_MAPPING 483 if (ef->object) { 484 vm_map_remove(kernel_map, (vm_offset_t) ef->address, 485 (vm_offset_t) ef->address 486 + (ef->object->size << PAGE_SHIFT)); 487 vm_object_deallocate(ef->object); 488 } 489 #else 490 free(ef->address, M_LINKER); 491 #endif 492 free(ef, M_LINKER); 493 } 494 } 495 496 static int 497 load_dependancies(linker_file_t lf) 498 { 499 elf_file_t ef = lf->priv; 500 linker_file_t lfdep; 501 char* name; 502 char* filename = 0; 503 const Elf_Dyn *dp; 504 int error = 0; 505 506 /* 507 * All files are dependant on /kernel. 508 */ 509 linker_kernel_file->refs++; 510 linker_file_add_dependancy(lf, linker_kernel_file); 511 512 513 for (dp = ef->dynamic; dp->d_tag != DT_NULL; dp++) { 514 if (dp->d_tag == DT_NEEDED) { 515 name = ef->strtab + dp->d_un.d_val; 516 517 /* 518 * Prepend pathname if dep is not an absolute filename. 519 */ 520 if (name[0] != '/') { 521 char* p; 522 if (!filename) { 523 filename = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 524 if (!filename) { 525 error = ENOMEM; 526 goto out; 527 } 528 } 529 p = lf->filename + strlen(lf->filename) - 1; 530 while (p >= lf->filename && *p != '/') 531 p--; 532 if (p >= lf->filename) { 533 strncpy(filename, lf->filename, p - lf->filename); 534 filename[p - lf->filename] = '\0'; 535 strcat(filename, "/"); 536 strcat(filename, name); 537 name = filename; 538 } 539 } 540 error = linker_load_file(name, &lfdep); 541 if (error) 542 goto out; 543 error = linker_file_add_dependancy(lf, lfdep); 544 if (error) 545 goto out; 546 } 547 } 548 549 out: 550 if (filename) 551 free(filename, M_TEMP); 552 return error; 553 } 554 555 static const char * 556 symbol_name(elf_file_t ef, const Elf_Rela *rela) 557 { 558 const Elf_Sym *ref; 559 560 if (ELF_R_SYM(rela->r_info)) { 561 ref = ef->symtab + ELF_R_SYM(rela->r_info); 562 return ef->strtab + ref->st_name; 563 } else 564 return NULL; 565 } 566 567 static int 568 relocate_file(linker_file_t lf) 569 { 570 elf_file_t ef = lf->priv; 571 const Elf_Rel *rellim; 572 const Elf_Rel *rel; 573 const Elf_Rela *relalim; 574 const Elf_Rela *rela; 575 576 /* Perform relocations without addend if there are any: */ 577 rellim = (const Elf_Rel *) ((caddr_t) ef->rel + ef->relsize); 578 for (rel = ef->rel; ef->rel != NULL && rel < rellim; rel++) { 579 Elf_Rela locrela; 580 581 locrela.r_info = rel->r_info; 582 locrela.r_offset = rel->r_offset; 583 locrela.r_addend = 0; 584 if (elf_reloc(lf, &locrela, symbol_name(ef, &locrela))) 585 return ENOENT; 586 } 587 588 /* Perform relocations with addend if there are any: */ 589 relalim = (const Elf_Rela *) ((caddr_t) ef->rela + ef->relasize); 590 for (rela = ef->rela; ef->rela != NULL && rela < relalim; rela++) { 591 if (elf_reloc(lf, rela, symbol_name(ef, rela))) 592 return ENOENT; 593 } 594 595 /* Perform PLT relocations without addend if there are any: */ 596 rellim = (const Elf_Rel *) ((caddr_t) ef->pltrel + ef->pltrelsize); 597 for (rel = ef->pltrel; ef->pltrel != NULL && rel < rellim; rel++) { 598 Elf_Rela locrela; 599 600 locrela.r_info = rel->r_info; 601 locrela.r_offset = rel->r_offset; 602 locrela.r_addend = 0; 603 if (elf_reloc(lf, &locrela, symbol_name(ef, &locrela))) 604 return ENOENT; 605 } 606 607 /* Perform relocations with addend if there are any: */ 608 relalim = (const Elf_Rela *) ((caddr_t) ef->pltrela + ef->pltrelasize); 609 for (rela = ef->pltrela; ef->pltrela != NULL && rela < relalim; rela++) { 610 if (elf_reloc(lf, rela, symbol_name(ef, rela))) 611 return ENOENT; 612 } 613 614 return 0; 615 } 616 617 /* 618 * Hash function for symbol table lookup. Don't even think about changing 619 * this. It is specified by the System V ABI. 620 */ 621 static unsigned long 622 elf_hash(const char *name) 623 { 624 const unsigned char *p = (const unsigned char *) name; 625 unsigned long h = 0; 626 unsigned long g; 627 628 while (*p != '\0') { 629 h = (h << 4) + *p++; 630 if ((g = h & 0xf0000000) != 0) 631 h ^= g >> 24; 632 h &= ~g; 633 } 634 return h; 635 } 636 637 int 638 link_elf_lookup_symbol(linker_file_t lf, const char* name, linker_sym_t* sym) 639 { 640 elf_file_t ef = lf->priv; 641 unsigned long symnum; 642 const Elf_Sym* es; 643 unsigned long hash; 644 int i; 645 646 hash = elf_hash(name); 647 symnum = ef->buckets[hash % ef->nbuckets]; 648 649 while (symnum != STN_UNDEF) { 650 const Elf_Sym *symp; 651 const char *strp; 652 653 if (symnum >= ef->nchains) { 654 printf("link_elf_lookup_symbol: corrupt symbol table\n"); 655 return ENOENT; 656 } 657 658 symp = ef->symtab + symnum; 659 if (symp->st_name == 0) { 660 printf("link_elf_lookup_symbol: corrupt symbol table\n"); 661 return ENOENT; 662 } 663 664 strp = ef->strtab + symp->st_name; 665 666 if (strcmp(name, strp) == 0) { 667 if (symp->st_shndx != SHN_UNDEF || 668 (symp->st_value != 0 && 669 ELF_ST_TYPE(symp->st_info) == STT_FUNC)) { 670 *sym = (linker_sym_t) symp; 671 return 0; 672 } else 673 return ENOENT; 674 } 675 676 symnum = ef->chains[symnum]; 677 } 678 679 return ENOENT; 680 } 681 682 static void 683 link_elf_symbol_values(linker_file_t lf, linker_sym_t sym, linker_symval_t* symval) 684 { 685 elf_file_t ef = lf->priv; 686 Elf_Sym* es = (Elf_Sym*) sym; 687 688 symval->name = ef->strtab + es->st_name; 689 symval->value = (caddr_t) ef->address + es->st_value; 690 symval->size = es->st_size; 691 } 692 693 static int 694 link_elf_search_symbol(linker_file_t lf, caddr_t value, 695 linker_sym_t* sym, long* diffp) 696 { 697 elf_file_t ef = lf->priv; 698 u_long off = (u_long) value; 699 u_long diff = off; 700 int symcount = ef->nchains; 701 const Elf_Sym* es; 702 const Elf_Sym* best = 0; 703 int i; 704 705 for (i = 0, es = ef->symtab; i < ef->nchains; i++, es++) { 706 if (es->st_name == 0) 707 continue; 708 if (off >= es->st_value) { 709 if (off - es->st_value < diff) { 710 diff = off - es->st_value; 711 best = es; 712 if (diff == 0) 713 break; 714 } else if (off - es->st_value == diff) { 715 best = es; 716 } 717 } 718 } 719 if (best == 0) 720 *diffp = off; 721 else 722 *diffp = diff; 723 *sym = (linker_sym_t) best; 724 725 return 0; 726 } 727 728