1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031
|
head 1.3;
access;
symbols;
locks
geoffk:1.3; strict;
comment @ * @;
1.3
date 97.10.12.02.17.22; author geoffk; state Exp;
branches;
next 1.2;
1.2
date 97.08.30.10.51.08; author geoffk; state Exp;
branches;
next 1.1;
1.1
date 97.08.23.06.37.17; author geoffk; state Exp;
branches;
next ;
desc
@@
1.3
log
@Many changes.
@
text
@#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <elf.h>
#include <getopt.h>
#include <assert.h>
#include <sys/stat.h>
#include <errno.h>
#include "elfload.h"
static int verbose = 1;
static int force = 0;
static int preserve = 0;
const char *curfile;
/* Replace any DT_RELENT .dynamic entry with DT_RELAENT. */
static int
fixrelent(int fd, Elf32_Off dyn_offset)
{
int i;
if (lseek(fd, dyn_offset, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for dynamic: %m\n", curfile);
return -1;
}
for (i = 0; ; i++)
{
Elf32_Dyn dent;
if (read(fd, &dent, sizeof(dent)) != sizeof(dent))
{
fprintf (stderr, "%s: reading .dynamic entry %d: %m\n",
curfile, i);
return -1;
}
switch (dent.d_tag)
{
case DT_NULL:
fprintf(stderr, "%s: No DT_REL[A]ENT??\n", curfile);
return -1;
case DT_RELAENT:
if (verbose > 1)
printf("%s: Already has DT_RELAENT.\n", curfile);
return 0;
case DT_RELENT:
if (lseek(fd, dyn_offset + i*sizeof(Elf32_Dyn), SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for write: %m\n",
curfile);
return -1;
}
dent.d_tag = DT_RELAENT;
if (write(fd, &dent, sizeof(dent)) != sizeof(dent))
{
fprintf (stderr, "%s: writing DT_RELAENT: %m\n",
curfile);
return -1;
}
if (verbose)
printf("%s: DT_RELENT fixed.\n", curfile);
return 0;
default:
/* continue */
}
}
}
static int
startcmp(const Elf32_Word *broken_start, const Elf32_Word *start_data,
int length)
{
int i;
for (i = 0; i < length; i++)
if (((broken_start[i] & 0xffff) != 0x5555
|| (broken_start[i] & ~0xffff) != (start_data[i] & ~0xffff))
&& broken_start[i] != start_data[i])
return 0;
return 1;
}
/* Returns -1 on error, 0 otherwise. */
static int
fixstart(int fd, const Elf32_Ehdr *ehdr)
{
enum { start_length = 0xb8 };
static const Elf32_Word broken_start[] = {
0x3da05555, 0x61ad5555, 0x2c1f7654, 0x41820028,
0x7c3d0b78, 0x3821ffc0, 0x807d0000, 0x389d0004,
0x7c852378, 0x84050004, 0x2c000000, 0x4082fff8,
0x38a50004, 0x3fe05555, 0x90bf5555, 0x7c771b78,
0x7c982378, 0x7cb92b78, 0x2c1f7654, 0x41820008,
0x7fa3eb78
};
static const Elf32_Word new_start[] = {
0x3821FFF0, /* addi %r1,%r1,-16 */
0x38000000, /* li %r0,0 */
0x90010000, /* stw %r0,0(1) */
0x7C0803A6, /* mtlr %r0 */
0x7C771B78, /* mr %r23,%r3 */
0x7C982378, /* mr %r24,%r4 */
0x7CE33B79, /* mr. %r3,%r7 */
0x7CB92B78, /* mr %r25,%r5 */
0x41820008, /* beq 0f */
0x48000001, /* bl atexit */
0x7EE3BB78, /* 0: mr %r3,%r23 */
0x7F04C378, /* mr %r4,%r24 */
0x7F25CB78, /* mr %r5,%r25 */
0x48000018, /* b 1f */
0x60000000, /* nop */
0x60000000, /* nop */
0x60000000, /* nop */
0x60000000, /* nop */
0x60000000 /* nop */
/* 1: */
};
Elf32_Word *startp;
Elf32_Off start_offset;
int i;
if (ehdr->e_entry == 0)
{
if (verbose)
printf ("%s: No entry point!\n", curfile);
return 0;
}
startp = (Elf32_Word *)find_location(ehdr->e_entry, 0);
if (!startp)
{
fprintf(stderr,"%s: No section containing entry point (0x%08x).\n",
curfile, ehdr->e_entry);
return -1;
}
/* Check to see that it's the start routine we expect. */
if (find_location(ehdr->e_entry, 26*4)
&& startcmp(broken_start, startp,
sizeof(broken_start)/sizeof(broken_start[0])))
startp += 2;
else if (find_location(ehdr->e_entry, 24*4)
&& startcmp(broken_start+2, startp,
sizeof(broken_start)/sizeof(broken_start[0])-2))
{
if (verbose > 1)
printf("%s: Warning: Start routine does not set r13.\n",
curfile);
}
else
{
if (verbose > 1)
printf("%s: Start routine not known to fail, not changed.\n",
curfile);
return 0;
}
/* Fix it. */
for (i = 0; (size_t)i < sizeof(new_start)/sizeof(new_start[0]); i++)
startp[i] = new_start[i];
/* Get bl instruction for atexit. */
startp[9] |= 0x03fffffc & startp[23] + 56;
changed_segment();
if (verbose)
printf("%s: Start routine fixed.\n", curfile);
return 0;
}
static const char *const fixsyms[][2] = {
{ "sigisemptyset","s!,isemptyset" },
{ "sigemptyset", "s!,emptyset" },
{ "sigfillset", "s!,fillset" },
{ "sigprocmask", "s!,procmask" },
{ "sigpending", "s!,pending" },
{ "sigaction", "s!,action" },
{ "__sigsetjmp", "__s!,setjmp" },
{ "__sigjmp_save","__s!,jmp_save" },
{ "tcgetattr", "!,getattr" },
{ "tcsetattr", "!,setattr" },
{ "__fxstat", "__fx,!at" },
{ "__xstat", "__x,!at" },
{ "__lxstat", "__lx,!at" },
{ "readdir", "readd!," },
{ "readdir_r", "readd!,_r" },
{ "scandir", "scand!," },
{ "alphasort", "alphaso,!" },
{ "getdirentries","getd!,entries" }
};
/* Returns -1 on error, 0 otherwise. */
static int
checksyms(int fd, int dyn_offset)
{
enum { num_need = 32 };
Elf32_Addr needed[num_need];
int numneeded = 0;
int i;
Elf32_Addr sym_offset, string_offset, hash_offset;
if (lseek(fd, dyn_offset, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for dynamic: %m\n", curfile);
return -1;
}
sym_offset = string_offset = hash_offset = 0;
for (;;)
{
Elf32_Dyn dent;
if (read(fd, &dent, sizeof(dent)) != sizeof(dent))
{
fprintf (stderr, "%s: reading .dynamic entry: %m\n",
curfile);
return -1;
}
#ifndef DT_VERNEED
#define DT_VERNEED 0x6ffffffe
#endif
switch (dent.d_tag)
{
case DT_VERNEED:
if (verbose > 1)
printf("%s: Not looking for problem functions, has versioning.\n",
curfile);
return 0;
case DT_SYMTAB:
sym_offset = dent.d_un.d_ptr;
break;
case DT_STRTAB:
string_offset = dent.d_un.d_ptr;
break;
case DT_HASH:
hash_offset = dent.d_un.d_ptr;
break;
case DT_NEEDED:
if (numneeded == num_need)
{
fprintf(stderr, "%s: uses too many shared objects.\n",
curfile);
return -1;
}
needed[numneeded++] = dent.d_un.d_ptr;
break;
case DT_NULL:
goto endloop;
default:
break;
}
}
endloop:
if (sym_offset == 0 || string_offset == 0 || hash_offset == 0)
{
fprintf(stderr, "%s: No strings or no symbols!\n", curfile);
return -1;
}
#define findlocn(addr) ({ \
char *result = find_location(addr, 1); \
if (result == 0) \
{ \
fprintf (stderr, "%s: Unloaded address %08x!\n", curfile, addr); \
return -1; \
} \
result; \
})
{
Elf32_Word nchain, ichain, nbucket, c;
Elf32_Addr *bucket, *chain;
Elf32_Sym *syms;
int hit = 0;
char *name, *namep;
nchain = *(Elf32_Word *) findlocn(hash_offset + 4);
syms = (Elf32_Sym *) findlocn(sym_offset);
for (ichain = 0; ichain < nchain; ichain++)
if (syms[ichain].st_name != 0)
{
size_t k;
name = findlocn(string_offset + syms[ichain].st_name);
for (k = 0; k < sizeof(fixsyms)/sizeof(fixsyms[0]); k++)
if (strcmp(name, fixsyms[k][0]) == 0)
{
if (!hit && verbose == 1)
printf("%s: Fixing functions.\n", curfile);
else if (verbose > 1)
printf("%s: Changing function %s to %s.\n", curfile,
fixsyms[k][0], fixsyms[k][1]);
assert(strlen(fixsyms[k][0]) >= strlen(fixsyms[k][1]));
strcpy(name, fixsyms[k][1]);
hit = 1;
}
}
if (!hit && verbose > 1)
printf("%s: Found no functions that needed replacing.\n", curfile);
if (!hit)
return 0;
changed_segment();
/* Rebuild the hash table. */
nbucket = *(Elf32_Word *) findlocn(hash_offset);
bucket = (Elf32_Addr *) findlocn(hash_offset + 8);
chain = bucket + nbucket;
memset(bucket, 0, sizeof(Elf32_Addr)*nbucket);
memset(chain, 0, sizeof(Elf32_Addr)*nchain);
for (c = 1; c < nchain; c++)
if (syms[c].st_name != 0)
{
unsigned long h=0, g, j;
name = findlocn(string_offset + syms[c].st_name);
for (j = 0; name[j]; j++)
{
h = (h << 4) + name[j];
if (g = h & 0xf0000000)
h ^= g >> 24;
h &= ~g;
}
h %= nbucket;
if (bucket[h] == 0)
bucket[h] = c;
else
{
for (h = bucket[h]; chain[h] != 0; h = chain[h])
;
chain[h] = c;
}
}
}
{
int libcct = -1;
int ldsoct = -1;
static const char libname[] = "fix!.so";
for (i = 0; i < numneeded; i++)
{
char *name;
name = findlocn(string_offset + needed[i]);
if (strcmp(name, "libc.so.6") == 0)
libcct = i;
else if (strcmp (name, "ld.so.1") == 0)
{
if (verbose > 1)
printf("%s: Replacing %s with %s\n", curfile, name, libname);
strcpy(name, libname);
ldsoct = i;
}
}
if (libcct == -1)
{
fprintf (stderr, "%s: Does not use libc.so.6!\n", curfile);
return -1;
}
if (ldsoct == -1)
{
char *libcname;
libcname = findlocn(string_offset + needed[libcct]);
if (verbose > 1)
printf("%s: Replacing %s with %s\n", curfile, libcname, libname);
strcpy(libcname, libname);
}
}
return 0;
}
/* Returns -1 on error, 0 otherwise. */
static int
fixglobdat(int fd, int dyn_offset)
{
Elf32_Addr rela_offset;
Elf32_Word numrels, i, hit = 0;
Elf32_Rela *rels;
if (lseek(fd, dyn_offset, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for dynamic: %m\n", curfile);
return -1;
}
rela_offset = numrels = 0;
for (;;)
{
Elf32_Dyn dent;
if (read(fd, &dent, sizeof(dent)) != sizeof(dent))
{
fprintf (stderr, "%s: reading .dynamic entry: %m\n",
curfile);
return -1;
}
switch (dent.d_tag)
{
case DT_RELA:
rela_offset = dent.d_un.d_ptr;
break;
case DT_RELASZ:
numrels = dent.d_un.d_val / sizeof(Elf32_Rela);
break;
case DT_RELAENT:
if (dent.d_un.d_val != sizeof(Elf32_Rela))
{
fprintf (stderr, "%s: RELA entry size %d, expected %d!\n",
curfile, dent.d_un.d_val, sizeof(Elf32_Rela));
return -1;
}
break;
case DT_NULL:
goto endloop;
default:
break;
}
}
endloop:
if (rela_offset == 0 || numrels == 0)
{
if (verbose > 1)
printf("%s: No relocations.\n", curfile);
return 0;
}
rels = (Elf32_Rela *) find_location(rela_offset, sizeof(Elf32_Rela)*numrels);
if (rels == NULL)
{
fprintf (stderr, "%s: Relocations (0x%08x-0x%08x) not loaded.\n",
curfile, rela_offset, rela_offset+sizeof(Elf32_Rela)*numrels);
return -1;
}
for (i = 0; i < numrels; i++)
if (ELF32_R_SYM(rels[i].r_info) == SHN_UNDEF
&& ELF32_R_TYPE(rels[i].r_info) == R_PPC_GLOB_DAT)
{
hit++;
rels[i].r_info = ELF32_R_INFO(SHN_UNDEF, R_PPC_RELATIVE);
}
if (hit)
{
if (verbose)
printf("%s: %d R_PPC_GLOB_DAT relocs changed to R_PPC_RELATIVE.\n",
curfile, hit);
changed_segment();
}
else if (verbose > 1)
printf("%s: No broken R_PPC_GLOB_DAT relocs found.\n", curfile);
return 0;
}
static void
fixit(const char *whichfile)
{
Elf32_Ehdr ehdr;
Elf32_Off dyn_offset;
int fd;
struct stat oldperms;
int permschanged = 0;
curfile = whichfile;
if (preserve || force)
if (stat(curfile, &oldperms) != 0)
{
fprintf (stderr, "%s: couldn't stat: %m\n", curfile);
return;
}
fd = open(curfile, O_RDWR);
if (force && fd == -1 && errno == EACCES)
if (chmod(curfile, oldperms.st_mode | S_IRUSR | S_IWUSR) == -1)
{
fprintf (stderr, "%s: couldn't open and couldn't change modes: %m\n",
curfile);
return;
}
else
{
fd = open(curfile, O_RDWR);
permschanged = 1;
}
if (fd == -1)
{
fprintf (stderr, "%s: open failed: %m\n", curfile);
if (permschanged)
chmod(curfile, oldperms.st_mode);
return;
}
{ int ehdr_result = read_check_ehdr(fd, &ehdr);
if (ehdr_result == -2 && verbose > 1)
printf("%s: Not a PPC executable.\n", curfile);
if (ehdr_result != 0)
goto donefile;
}
{ int read_seg_result = read_segment_list(fd, &ehdr, &dyn_offset);
if (read_seg_result == 1 && verbose > 1)
printf("%s: No dynamic section.\n", curfile);
if (read_seg_result != 0)
goto donefile;
}
if (fixrelent(fd, dyn_offset) != 0)
goto donefile;
if (load_segments(fd) != 0)
goto donefile;
if (fixstart(fd, &ehdr) == -1)
goto donefile;
if (checksyms(fd, dyn_offset) == -1)
goto donefile;
if (ehdr.e_type == ET_DYN
&& fixglobdat(fd, dyn_offset) == -1)
goto donefile;
write_segments(fd);
donefile:
free_segments();
if (permschanged || preserve)
fchmod(fd, oldperms.st_mode);
close(fd);
}
int
main(int argc, char **argv)
{
int c;
size_t i, ct;
while (1)
{
static const struct option long_options[] =
{
/* These options set a flag. */
{"verbose", 0, &verbose, 2},
{"quiet", 0, &verbose, 0},
{"force", 0, &force, 1},
{"preserve", 0, &preserve, 1},
{"help", 0, 0, 'h'},
{"version", 0, 0, 'e'},
{0, 0, 0, 0}
};
/* `getopt_long' stores the option index here. */
int option_index = 0;
c = getopt_long (argc, argv, "svpf",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 0:
break;
case 's':
verbose = 0;
break;
case 'v':
verbose++;
break;
case 'p':
preserve = 1;
break;
case 'f':
force = 1;
break;
case 'h':
printf (
"Usage: %s [OPTION] FILE ...\n"
"Fix FILE to make it work with libc 2.1 on powerpc.\n\n"
" -s, --quiet don't print output indicating changes were made\n"
" -v, --verbose if a file was not changed, say why\n"
" -p, --preserve if the file was originally setuid, leave it that way\n"
" -f, --force ignore absence of read permission\n"
" --help display this help and exit\n"
" --version output version information and exit\n\n"
"The changes made are that:\n"
"1. A DT_RELENT program header entry is changed to the correct DT_RELAENT.\n"
"2. The startup procedure is modified to conform to the PPC version of the\n"
" SVR4 ELF ABI.\n"
"3. Routines which use data structures that changed size between the 971212\n"
" libc and the final libc 2.0, or which use constants that are different\n"
" between the two versions, are redirected to call a stub library that\n"
" emulates the old library behaviour. The affected routines are:\n"
" ", argv[0]);
ct = 9;
for (i = 0; i < sizeof(fixsyms)/sizeof(fixsyms[0]); i++)
ct += printf("%s%s",
(ct + sizeof(fixsyms[i][0]) > 70
? (ct=0), ",\n "
: (i==0 ? "" : ", ")),
fixsyms[i][0]);
printf(".\n"
"4. R_PPC_GLOB_DAT relocations in shared objects that point to an absolute\n"
" address are changed to be R_PPC_RELATIVE relocations.\n\n"
"If FILE is not a PowerPC ELF executable or shared object, or the changes\n"
"have already been made, or the changes don't need to be made, does nothing.\n"
);
exit(0);
case 'e':
printf("Version 1.0\n");
exit(0);
case '?':
/* `getopt_long' already printed an error message. */
fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
exit(1);
default:
abort ();
}
}
while (optind < argc)
fixit(argv[optind++]);
return 0;
}
@
1.2
log
@* Add a new kind of start routine;
* Rebuild hash table (this is probably not necessary, but can't hurt).
@
text
@d12 2
d17 1
a17 1
static const char *curfile;
d19 1
a19 1
/* Returns -1 on error, 1 if not dynamic executable, 0 otherwise. */
d21 1
a21 1
fixrelent(int fd, const Elf32_Ehdr *ehdr)
a22 1
Elf32_Off dyn_offset;
a24 34
if (lseek(fd, ehdr->e_phoff, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for sections: %m\n", curfile);
return -1;
}
dyn_offset = 0;
for (i = 0; i < ehdr->e_phnum; i++)
{
Elf32_Phdr phdr;
if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
{
fprintf (stderr, "%s: reading section header %d: %m\n",
curfile, i);
return -1;
}
if (phdr.p_type == PT_DYNAMIC)
if (dyn_offset == 0)
dyn_offset = phdr.p_offset;
else
{
fprintf(stderr, "%s: seems to have multiple dynamic sections.\n",
curfile);
return -1;
}
}
if (dyn_offset == 0)
{
if (verbose > 1)
printf("%s: No dynamic section.\n", curfile);
return 1;
}
a120 1
Elf32_Word start_data[start_length/4];
d132 1
a132 47
if (lseek(fd, ehdr->e_phoff, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for sections: %m\n", curfile);
return -1;
}
start_offset = 0;
for (i = 0; i < ehdr->e_phnum; i++)
{
Elf32_Phdr phdr;
if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
{
fprintf (stderr, "%s: reading section header %d: %m\n",
curfile, i);
return -1;
}
if (phdr.p_type == PT_LOAD
&& phdr.p_vaddr <= ehdr->e_entry
&& phdr.p_vaddr + phdr.p_memsz > ehdr->e_entry)
if (start_offset == 0)
{
Elf32_Word start_offset_in_seg;
start_offset_in_seg = ehdr->e_entry - phdr.p_vaddr;
start_offset = phdr.p_offset + start_offset_in_seg;
if (phdr.p_filesz < start_offset_in_seg+start_length)
{
if (verbose > 1)
printf("%s: Segment containing entry too small.\n", curfile);
return 0;
}
if ((phdr.p_flags & PF_X) == 0)
{
fprintf(stderr,
"%s: Segment containing entry (0x%08x) not "
"executable.\n",
curfile, ehdr->e_entry);
return -1;
}
}
else
{
fprintf(stderr,
"%s: Multiple sections corresponding to entry (0x%08x).\n",
curfile, ehdr->e_entry);
return -1;
}
}
d134 1
a134 1
if (start_offset == 0)
a139 12
if (lseek(fd, start_offset, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for start: %m\n", curfile);
return -1;
}
if (read(fd, start_data, start_length) != start_length)
{
fprintf (stderr, "%s: reading start routine: %m\n",
curfile);
return -1;
}
d142 7
a148 7
if (startcmp(broken_start, start_data,
sizeof(broken_start)/sizeof(broken_start[0])))
{
startp = start_data+2;
}
else if (startcmp(broken_start+2, start_data,
sizeof(broken_start)/sizeof(broken_start[0])-2))
a152 1
startp = start_data;
d168 1
a168 11
if (lseek(fd, start_offset, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for start: %m\n", curfile);
return -1;
}
if (write(fd, start_data, start_length) != start_length)
{
fprintf (stderr, "%s: writing start routine: %m\n",
curfile);
return -1;
}
d175 3
a177 1
static const char *fixsyms[][2] = {
d198 1
a198 2
/* Returns -1 on error, 1 if not dynamic executable,
2 if no problem, 0 otherwise. */
d200 1
a200 1
checksyms(int fd, const Elf32_Ehdr *ehdr)
d202 1
a202 9
Elf32_Off dyn_offset;
enum { num_load = 5, num_need = 32 };
struct {
char *data;
Elf32_Addr base;
Elf32_Word length, fsz;
Elf32_Off off;
} loaded[num_load];
int numloaded = 0;
a207 48
if (lseek(fd, ehdr->e_phoff, SEEK_SET) == -1)
{
fprintf (stderr, "%s: positioning for sections: %m\n", curfile);
return -1;
}
dyn_offset = 0;
for (i = 0; i < ehdr->e_phnum; i++)
{
Elf32_Phdr phdr;
if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
{
fprintf (stderr, "%s: reading section header %d: %m\n",
curfile, i);
return -1;
}
if (phdr.p_type == PT_DYNAMIC)
if (dyn_offset == 0)
dyn_offset = phdr.p_offset;
else
{
fprintf(stderr, "%s: Seems to have multiple dynamic sections.\n",
curfile);
return -1;
}
else if (phdr.p_type == PT_LOAD)
{
if (numloaded == num_load)
{
fprintf(stderr, "%s: Too many loadable sections.\n",
curfile);
return -1;
}
loaded[numloaded].base = phdr.p_vaddr;
loaded[numloaded].off = phdr.p_offset;
loaded[numloaded].length = phdr.p_memsz;
loaded[numloaded].fsz = phdr.p_filesz;
numloaded++;
}
}
if (dyn_offset == 0)
{
if (verbose > 1)
printf("%s: No dynamic section.\n", curfile);
return 1;
}
d232 1
a232 1
return 2;
a263 17
for (i = 0; i < numloaded; i++)
{
loaded[i].data = alloca(loaded[i].length);
memset(loaded[i].data, 0, loaded[i].length);
if (lseek(fd, loaded[i].off, SEEK_SET) == -1)
{
fprintf (stderr, "%s: seeking for segment %d: %m\n", curfile, i);
return -1;
}
if (read(fd, loaded[i].data, loaded[i].fsz) != (ssize_t)loaded[i].fsz)
{
fprintf (stderr, "%s: reading segment %d: %m\n", curfile, i);
return -1;
}
}
d265 1
a265 6
int t; \
char *result = 0; \
for (t = 0; t < numloaded; t++) \
if (loaded[t].base <= (addr) \
&& loaded[t].base + loaded[t].length > (addr)) \
result = loaded[t].data + ((addr) - loaded[t].base); \
d304 3
a306 1
return 2;
d373 12
a384 2
/* We now need to write out all the segments. */
for (i = 0; i < numloaded; i++)
d386 8
a393 1
if (lseek(fd, loaded[i].off, SEEK_SET) == -1)
d395 2
a396 1
fprintf (stderr, "%s: seeking for segment %d: %m\n", curfile, i);
d399 1
a399 1
if (write(fd, loaded[i].data, loaded[i].fsz) != (ssize_t)loaded[i].fsz)
d401 18
a418 2
fprintf (stderr, "%s: writing segment %d: %m\n", curfile, i);
return -1;
d421 34
a460 1
ssize_t numread;
d462 1
d496 4
a499 4
numread = read(fd, &ehdr, sizeof(ehdr));
if (numread == -1)
{
fprintf (stderr, "%s: header read failed: %m\n", curfile);
d501 1
a501 7
}
else if (numread < (ssize_t)sizeof(ehdr))
{
if (verbose > 1)
printf("%s: Too short.\n", curfile);
goto donefile;
}
d503 4
a506 9
if (*(unsigned *)ehdr.e_ident != *(const unsigned *)ELFMAG
|| ehdr.e_ident[EI_CLASS] != ELFCLASS32
|| ehdr.e_ident[EI_DATA] != ELFDATA2MSB
|| ehdr.e_ident[EI_VERSION] != EV_CURRENT
|| ehdr.e_type != ET_EXEC && ehdr.e_type != ET_DYN
|| ehdr.e_machine != EM_PPC)
{
if (verbose > 1)
printf("%s: Not a PPC executable.\n", curfile);
d508 1
a508 1
}
d510 1
a510 8
if (ehdr.e_phentsize != sizeof(Elf32_Phdr))
{
fprintf(stderr, "%s: Section size was read as %d, not %d!\n",
curfile, ehdr.e_phentsize, sizeof(Elf32_Phdr));
goto donefile;
}
if (fixrelent(fd, &ehdr) != 0)
d513 3
d519 8
a526 1
checksyms(fd, &ehdr);
d529 1
d585 1
a585 1
" -q, --quiet don't print output indicating changes were made\n"
d607 6
a612 3
printf(".\n\n"
"If FILE is not a PowerPC ELF executable, or the changes have already\n"
"been made, or the changes don't need to be made, does nothing.\n");
@
1.1
log
@Initial revision
@
text
@d104 14
d155 1
d234 20
a253 10
for (i = 0; (size_t)i < sizeof(broken_start)/sizeof(broken_start[0]); i++)
if (((broken_start[i] & 0xffff) != 0x5555
|| (broken_start[i] & ~0xffff) != (start_data[i] & ~0xffff))
&& broken_start[i] != start_data[i])
{
if (verbose > 1)
printf("%s: Start routine not known to fail, not changed.\n",
curfile);
return 0;
}
d257 1
a257 1
start_data[i+2] = new_start[i];
d259 1
a259 1
start_data[11] |= 0x03fffffc & start_data[25] + 56;
d455 2
a456 1
Elf32_Word nchain, ichain;
d459 1
a465 1
char *name;
d485 31
d538 1
a538 1
fprintf (stderr, "%s: Does not use libc.so.2!\n", curfile);
d619 6
a624 6
if (*(unsigned *)ehdr.e_ident != *(const unsigned *)ELFMAG ||
ehdr.e_ident[EI_CLASS] != ELFCLASS32 ||
ehdr.e_ident[EI_DATA] != ELFDATA2MSB ||
ehdr.e_ident[EI_VERSION] != EV_CURRENT ||
ehdr.e_type != ET_EXEC ||
ehdr.e_machine != EM_PPC)
@
|