11#include "srxfixup_internal.h"
18static int is_in_range(
unsigned int top,
unsigned int size,
unsigned int pos);
19static void read_symtab(
elf_file *elf,
int sctindex, FILE *fp);
20static void read_rel(
elf_file *elf,
int sctindex, FILE *fp);
23static void renumber_symtab(
elf_file *elf);
24static void write_symtab(
elf_file *elf,
int sctindex, FILE *fp);
25static void write_rel(
elf_file *elf,
int sctindex, FILE *fp);
28static size_t search_string_table(
const char *tbltop,
size_t endindex,
const char *str);
29static void rebuild_a_symbol_name_strings(
elf_section *scp);
30static int comp_Elf_file_slot(
const void *a1,
const void *a2);
32elf_file *read_elf(
const char *filename)
38 fp = fopen(filename,
"rb");
41 fprintf(stderr,
"\"%s\" can't open (errno=%d)\n", filename, errno);
46 if ( fread(elf->ehp,
sizeof(
Elf32_Ehdr), 1, fp) != 1 )
48 fprintf(stderr,
"%s: Could not read ELF header (errno=%d)\n", filename, errno);
51 swapmemory(elf->ehp,
"ccccccccccccccccsslllllssssss", 1);
52 ident = *(uint32_t *)elf->ehp->e_ident;
53 swapmemory(&ident,
"l", 1);
54 if ( ident != 0x464C457F )
56 fprintf(stderr,
"%s: not elf format\n", filename);
62 if ( elf->ehp->e_ident[4] != ELFCLASS32 || elf->ehp->e_ident[5] != ELFDATA2LSB || elf->ehp->e_ident[6] != EV_CURRENT )
64 fprintf(stderr,
"%s: Not 32-bit object or Not little endian or Invalid Elf version\n", filename);
70 if ( elf->ehp->e_machine != EM_MIPS )
72 fprintf(stderr,
"%s: not EM_MIPS\n", filename);
78 if ( elf->ehp->e_phentsize && elf->ehp->e_phentsize !=
sizeof(
Elf32_Phdr) )
80 fprintf(stderr,
"%s: Unknown program header size\n", filename);
86 if ( elf->ehp->e_shentsize && elf->ehp->e_shentsize !=
sizeof(
Elf32_Shdr) )
88 fprintf(stderr,
"%s: Unknown sectoin header size\n", filename);
94 if ( elf->ehp->e_phnum && elf->ehp->e_phentsize )
100 count_1 = elf->ehp->e_phnum;
101 pos_1 = elf->ehp->e_phoff;
103 fseek(fp, pos_1, SEEK_SET);
104 for ( i_1 = 0; count_1 > i_1; i_1 += 1 )
106 if ( fread(&elf->php[i_1],
sizeof(
Elf32_Phdr), 1, fp) != 1 )
108 fprintf(stderr,
"%s: Could not read ELF program header (errno=%d)\n", filename, errno);
111 swapmemory(&elf->php[i_1],
"llllllll", 1);
114 if ( elf->ehp->e_shnum && elf->ehp->e_shentsize )
122 pos_2 = elf->ehp->e_shoff;
123 count_2 = elf->ehp->e_shnum;
125 fseek(fp, pos_2, SEEK_SET);
126 for ( i_2 = 0; count_2 > i_2; i_2 += 1 )
129 if ( fread(elf->scp[i_2],
sizeof(
Elf32_Shdr), 1, fp) != 1 )
131 fprintf(stderr,
"%s: Could not read ELF section header (errno=%d)\n", filename, errno);
134 swapmemory(elf->scp[i_2],
"llllllllll", 1);
136 for ( i_3 = 0; count_2 > i_3; i_3 += 1 )
138 switch ( elf->scp[i_3]->shr.sh_type )
140#pragma GCC diagnostic push
141#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
144 elf->scp[i_3]->info = elf->scp[elf->scp[i_3]->shr.sh_info];
149 elf->scp[i_3]->link = elf->scp[elf->scp[i_3]->shr.sh_link];
151#pragma GCC diagnostic pop
155 if ( i_3 == elf->ehp->e_shstrndx )
157 elf->shstrptr = elf->scp[i_3];
160 for ( i_4 = 0;; i_4 += 1 )
165 if ( count_2 <= i_4 )
172 for ( i_5 = 0; count_2 > i_5; i_5 += 1 )
176 pos_4 = elf->scp[i_5]->shr.sh_offset;
177 switch ( elf->scp[i_5]->shr.sh_type )
181 if ( pos_4 != 0 && elf->scp[i_5]->shr.sh_size )
183 fseek(fp, pos_4, SEEK_SET);
184 read_symtab(elf, i_5, fp);
191 for ( i_6 = 0; count_2 > i_6; i_6 += 1 )
195 pos_5 = elf->scp[i_6]->shr.sh_offset;
196 if ( elf->scp[i_6]->shr.sh_type == SHT_REL && pos_5 != 0 && elf->scp[i_6]->shr.sh_size )
198 fseek(fp, pos_5, SEEK_SET);
199 read_rel(elf, i_6, fp);
202 for ( i_7 = 0; count_2 > i_7; i_7 += 1 )
204 elf->scp[i_7]->name =
205 strdup((elf->shstrptr != NULL) ? ((
char *)&elf->shstrptr->data[elf->scp[i_7]->shr.sh_name]) :
"");
207 for ( i_8 = 0; i_8 < elf->ehp->e_phnum; i_8 += 1 )
212 switch ( elf->php[i_8].phdr.p_type )
217 for ( s = 1; s < count_2; s += 1 )
219 unsigned int p_filesz;
220 unsigned int p_offset;
222 p_offset = elf->php[i_8].phdr.p_offset;
223 p_filesz = elf->php[i_8].phdr.p_filesz;
224 switch ( elf->scp[s]->shr.sh_type )
228 is_in_range(p_offset, p_filesz, elf->scp[s]->shr.sh_offset)
229 || (!elf->scp[s]->shr.sh_size && elf->scp[s]->shr.sh_offset == p_filesz + p_offset) )
231 elf->php[i_8].scp[d] = elf->scp[s];
237 is_in_range(elf->php[i_8].phdr.p_vaddr, elf->php[i_8].phdr.p_memsz, elf->scp[s]->shr.sh_addr)
238 || (!elf->scp[s]->shr.sh_size && elf->scp[s]->shr.sh_offset == p_filesz + p_offset) )
240 elf->php[i_8].scp[d] = elf->scp[s];
249 case PT_MIPS_REGINFO:
251 *elf->php[i_8].scp = search_section(elf, SHT_MIPS_REGINFO);
255 *elf->php[i_8].scp = search_section(elf, SHT_SCE_IOPMOD);
259 *elf->php[i_8].scp = search_section(elf, SHT_SCE_EEMOD);
266 switch ( elf->ehp->e_type )
268 case ET_SCE_IOPRELEXEC:
269 case ET_SCE_IOPRELEXEC2:
270 case ET_SCE_EERELEXEC:
271 case ET_SCE_EERELEXEC2:
272 for ( i_8 = 0; i_8 < elf->ehp->e_phnum; i_8 += 1 )
277 switch ( elf->php[i_8].phdr.p_type )
280 scp = elf->php[i_8].scp;
281 for ( s = 1; scp[s]; s += 1 )
283 if ( scp[s]->shr.sh_type != SHT_NOBITS )
285 unsigned int addrdiff;
286 unsigned int offsetdiff;
288 addrdiff = scp[s]->shr.sh_addr - scp[s - 1]->shr.sh_addr;
289 offsetdiff = scp[s]->shr.sh_offset - scp[s - 1]->shr.sh_offset;
290 if ( addrdiff != offsetdiff )
292 scp[s]->shr.sh_addr = scp[s - 1]->shr.sh_addr + offsetdiff;
307 pos_3 = elf->scp[i_4]->shr.sh_offset;
308 size = elf->scp[i_4]->shr.sh_size;
309 if ( pos_3 != 0 && size != 0 )
311 fseek(fp, pos_3, SEEK_SET);
312 switch ( elf->scp[i_4]->shr.sh_type )
318 case SHT_MIPS_REGINFO:
319 elf->scp[i_4]->data = (uint8_t *)malloc(size);
320 if ( fread(elf->scp[i_4]->data, size, 1, fp) != 1 )
322 fprintf(stderr,
"%s: Could not read ELF section contents, (errno=%d)\n", filename, errno);
325 swapmemory(elf->scp[i_4]->data,
"l", size >> 2);
332 elf->scp[i_4]->data = (uint8_t *)read_mips_symbolic(fp);
335 elf->scp[i_4]->data = (uint8_t *)malloc(size);
336 if ( fread(elf->scp[i_4]->data, size, 1, fp) != 1 )
338 fprintf(stderr,
"%s: Could not read ELF section contents (errno=%d)\n", filename, errno);
341 swapmemory(elf->scp[i_4]->data,
"lllllls", 1);
344 elf->scp[i_4]->data = (uint8_t *)malloc(size);
345 if ( fread(elf->scp[i_4]->data, size, 1, fp) != 1 )
347 fprintf(stderr,
"%s: Could not read ELF section contents (errno=%d)\n", filename, errno);
350 swapmemory(elf->scp[i_4]->data,
"lllllllllls", 1);
353 elf->scp[i_4]->data = (uint8_t *)malloc(size);
354 if ( fread(elf->scp[i_4]->data, size, 1, fp) != 1 )
356 fprintf(stderr,
"%s: Could not read ELF section contents (errno=%d)\n", filename, errno);
368static int is_in_range(
unsigned int top,
unsigned int size,
unsigned int pos)
370 if ( pos >= top && pos < size + top )
377static void read_symtab(
elf_file *elf,
int sctindex, FILE *fp)
381 unsigned int entrise;
384 sp_x = elf->scp[sctindex];
385 entrise = sp_x->shr.sh_size / sp_x->shr.sh_entsize;
387 sp_x->data = (uint8_t *)result;
388 for ( i = 0; entrise > i; i += 1 )
391 if ( fread(result[i], sp_x->shr.sh_entsize, 1, fp) != 1 )
393 fprintf(stderr,
"elflib: Could not read ELF symbol table (errno=%d)\n", errno);
396 swapmemory(result[i],
"lllccs", 1);
397 result[i]->bind = result[i]->sym.st_info >> 4;
398 result[i]->type = result[i]->sym.st_info & 0xF;
400 if ( result[i]->sym.st_name )
402 result[i]->name = strdup((
char *)&sp_x->link->data[result[i]->sym.st_name]);
404 if ( result[i]->sym.st_shndx && result[i]->sym.st_shndx <= 0xFEFF )
406 result[i]->shptr = elf->scp[result[i]->sym.st_shndx];
410 result[i]->shptr = 0;
415static void read_rel(
elf_file *elf,
int sctindex, FILE *fp)
420 unsigned int entrise;
423 sp_x = elf->scp[sctindex];
424 entrise = sp_x->shr.sh_size / sp_x->shr.sh_entsize;
426 sp_x->data = (uint8_t *)result;
428 for ( i = 0; entrise > i; i += 1 )
430 if ( fread(&result[i], sp_x->shr.sh_entsize, 1, fp) != 1 )
432 fprintf(stderr,
"elflib: Could not read ELF relocations (errno=%d)\n", errno);
435 swapmemory(&result[i],
"ll", 1);
436 result[i].type = result[i].rel.r_info & 0xFF;
440 result[i].symptr = symp[result[i].rel.r_info >> 8];
441 result[i].symptr->refcount += 1;
451 if ( fread(sycb,
sizeof(
hdrr), 1, fp) != 1 )
453 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
456 swapmemory(sycb,
"sslllllllllllllllllllllll", 1);
457 if ( sycb->head.cbLineOffset > 0 )
461 size_1 = sycb->head.cbLine;
462 sycb->cbLine_Ptr = (
char *)malloc(size_1);
463 fseek(fp, sycb->head.cbLineOffset, SEEK_SET);
464 if ( fread(sycb->cbLine_Ptr, size_1, 1, fp) != 1 )
466 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
470 if ( sycb->head.cbDnOffset > 0 )
474 size_2 = 8L * sycb->head.idnMax;
475 sycb->cbDn_Ptr = (
char *)malloc(size_2);
476 fseek(fp, sycb->head.cbDnOffset, SEEK_SET);
477 if ( fread(sycb->cbDn_Ptr, size_2, 1, fp) != 1 )
479 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
482 swapmemory(sycb->cbDn_Ptr,
"ll", sycb->head.idnMax);
484 if ( sycb->head.cbPdOffset > 0 )
488 size_3 = 52L * sycb->head.ipdMax;
489 sycb->cbPd_Ptr = (
char *)malloc(size_3);
490 fseek(fp, sycb->head.cbPdOffset, SEEK_SET);
491 if ( fread(sycb->cbPd_Ptr, size_3, 1, fp) != 1 )
493 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
496 swapmemory(sycb->cbPd_Ptr,
"lllllllllsslll", sycb->head.ipdMax);
498 if ( sycb->head.cbSymOffset > 0 )
502 size_4 = 12L * sycb->head.isymMax;
503 sycb->cbSym_Ptr = (
char *)malloc(size_4);
504 fseek(fp, sycb->head.cbSymOffset, SEEK_SET);
505 if ( fread(sycb->cbSym_Ptr, size_4, 1, fp) != 1 )
507 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
510 swapmemory(sycb->cbSym_Ptr,
"lll", sycb->head.isymMax);
512 if ( sycb->head.cbOptOffset > 0 )
516 size_5 = 12L * sycb->head.ioptMax;
517 sycb->cbOpt_Ptr = (
char *)malloc(size_5);
518 fseek(fp, sycb->head.cbOptOffset, SEEK_SET);
519 if ( fread(sycb->cbOpt_Ptr, size_5, 1, fp) != 1 )
521 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
524 swapmemory(sycb->cbOpt_Ptr,
"lll", sycb->head.ioptMax);
526 if ( sycb->head.cbAuxOffset > 0 )
530 size_6 = 4L * sycb->head.iauxMax;
531 sycb->cbAux_Ptr = (
char *)malloc(size_6);
532 fseek(fp, sycb->head.cbAuxOffset, SEEK_SET);
533 if ( fread(sycb->cbAux_Ptr, size_6, 1, fp) != 1 )
535 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
538 swapmemory(sycb->cbAux_Ptr,
"l", sycb->head.iauxMax);
540 if ( sycb->head.cbSsOffset > 0 )
544 size_7 = sycb->head.issMax;
545 sycb->cbSs_Ptr = (
char *)malloc(size_7);
546 fseek(fp, sycb->head.cbSsOffset, SEEK_SET);
547 if ( fread(sycb->cbSs_Ptr, size_7, 1, fp) != 1 )
549 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
553 if ( sycb->head.cbSsExtOffset > 0 )
557 size_8 = sycb->head.issExtMax;
558 sycb->cbSsExt_Ptr = (
char *)malloc(size_8);
559 fseek(fp, sycb->head.cbSsExtOffset, SEEK_SET);
560 if ( fread(sycb->cbSsExt_Ptr, size_8, 1, fp) != 1 )
562 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
566 if ( sycb->head.cbFdOffset > 0 )
570 size_9 = 72L * sycb->head.ifdMax;
571 sycb->cbFd_Ptr = (
char *)malloc(size_9);
572 fseek(fp, sycb->head.cbFdOffset, SEEK_SET);
573 if ( fread(sycb->cbFd_Ptr, size_9, 1, fp) != 1 )
575 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
578 swapmemory(sycb->cbFd_Ptr,
"llllllllllsslllllll", sycb->head.ifdMax);
580 if ( sycb->head.cbRfdOffset > 0 )
584 size_A = 4L * sycb->head.crfd;
585 sycb->cbRfd_Ptr = (
char *)malloc(size_A);
586 fseek(fp, sycb->head.cbRfdOffset, SEEK_SET);
587 if ( fread(sycb->cbRfd_Ptr, size_A, 1, fp) != 1 )
589 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
592 swapmemory(sycb->cbRfd_Ptr,
"l", sycb->head.crfd);
594 if ( sycb->head.cbExtOffset > 0 )
598 size_B = 16L * sycb->head.iextMax;
599 sycb->cbExt_Ptr = (
char *)malloc(size_B);
600 fseek(fp, sycb->head.cbExtOffset, SEEK_SET);
601 if ( fread(sycb->cbExt_Ptr, size_B, 1, fp) != 1 )
603 fprintf(stderr,
"elflib: Could not read ELF debug info contents (errno=%d)\n", errno);
606 swapmemory(sycb->cbExt_Ptr,
"sslll", sycb->head.iextMax);
616 rebuild_section_name_strings(elf);
617 rebuild_symbol_name_strings(elf);
618 order = build_file_order_list(elf);
619 shrink_file_order_list(order);
620 writeback_file_order_list(elf, order);
625int write_elf(
elf_file *elf,
const char *filename)
629 fp = fopen(filename,
"wb");
632 fprintf(stderr,
"\"%s\" can't open (errno=%d)\n", filename, errno);
635 renumber_symtab(elf);
636 if ( elf->ehp->e_shnum && elf->ehp->e_shentsize )
641 for ( i_1 = 0; i_1 < elf->ehp->e_shnum; i_1 += 1 )
643 elf->scp[i_1]->number = i_1;
645 for ( i_2 = 0; i_2 < elf->ehp->e_shnum; i_2 += 1 )
647 switch ( elf->scp[i_2]->shr.sh_type )
649#pragma GCC diagnostic push
650#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
653 elf->scp[i_2]->shr.sh_info = elf->scp[i_2]->info->number;
658 elf->scp[i_2]->shr.sh_link = elf->scp[i_2]->link->number;
660#pragma GCC diagnostic pop
664 if ( elf->scp[i_2] == elf->shstrptr )
666 elf->ehp->e_shstrndx = elf->scp[i_2]->number;
670 swapmemory(elf->ehp,
"ccccccccccccccccsslllllssssss", 1);
672 swapmemory(elf->ehp,
"ccccccccccccccccsslllllssssss", 1);
673 if ( elf->ehp->e_phnum && elf->ehp->e_phentsize )
678 count_1 = elf->ehp->e_phnum;
679 fseek(fp, elf->ehp->e_phoff, SEEK_SET);
680 for ( i_3 = 0; count_1 > i_3; i_3 += 1 )
682 swapmemory(&elf->php[i_3],
"llllllll", 1);
683 fwrite(&elf->php[i_3],
sizeof(
Elf32_Phdr), 1, fp);
684 swapmemory(&elf->php[i_3],
"llllllll", 1);
687 if ( elf->ehp->e_shnum && elf->ehp->e_shentsize )
693 count_2 = elf->ehp->e_shnum;
694 fseek(fp, elf->ehp->e_shoff, SEEK_SET);
695 for ( i_4 = 0; count_2 > i_4; i_4 += 1 )
697 swapmemory(elf->scp[i_4],
"llllllllll", 1);
698 fwrite(elf->scp[i_4],
sizeof(
Elf32_Shdr), 1, fp);
699 swapmemory(elf->scp[i_4],
"llllllllll", 1);
701 for ( i_5 = 0; count_2 > i_5; i_5 += 1 )
706 pos = elf->scp[i_5]->shr.sh_offset;
707 size = elf->scp[i_5]->shr.sh_size;
708 if ( pos != 0 && size != 0 )
710 fseek(fp, pos, SEEK_SET);
711 switch ( elf->scp[i_5]->shr.sh_type )
717 case SHT_MIPS_REGINFO:
718 swapmemory(elf->scp[i_5]->data,
"l", size >> 2);
719 fwrite(elf->scp[i_5]->data, size, 1, fp);
720 swapmemory(elf->scp[i_5]->data,
"l", size >> 2);
724 write_symtab(elf, i_5, fp);
729 write_rel(elf, i_5, fp);
735 swapmemory(elf->scp[i_5]->data,
"lllllls", 1);
736 fwrite(elf->scp[i_5]->data, size, 1, fp);
737 swapmemory(elf->scp[i_5]->data,
"lllllls", 1);
740 swapmemory(elf->scp[i_5]->data,
"lllllllllls", 1);
741 fwrite(elf->scp[i_5]->data, size, 1, fp);
742 swapmemory(elf->scp[i_5]->data,
"lllllllllls", 1);
745 fwrite(elf->scp[i_5]->data, size, 1, fp);
758 unsigned int entrise;
761 entrise = scp->shr.sh_size / scp->shr.sh_entsize;
763 for ( i = 0; entrise > i; i += 1 )
769static void renumber_symtab(
elf_file *elf)
773 for ( sc = 1; sc < elf->ehp->e_shnum; sc += 1 )
775 switch ( elf->scp[sc]->shr.sh_type )
779 renumber_a_symtab(elf->scp[sc]);
787static void write_symtab(
elf_file *elf,
int sctindex, FILE *fp)
792 unsigned int entrise;
795 sp_x = elf->scp[sctindex];
796 entrise = sp_x->shr.sh_size / sp_x->shr.sh_entsize;
798 fseek(fp, sp_x->shr.sh_offset, SEEK_SET);
799 for ( i = 0; entrise > i; i += 1 )
801 memcpy(&sym, syp[i],
sizeof(sym));
804 sym.st_shndx = syp[i]->shptr->number;
806 swapmemory(&sym,
"lllccs", 1);
811static void write_rel(
elf_file *elf,
int sctindex, FILE *fp)
816 unsigned int entrise;
819 sp_x = elf->scp[sctindex];
820 entrise = sp_x->shr.sh_size / sp_x->shr.sh_entsize;
822 fseek(fp, sp_x->shr.sh_offset, SEEK_SET);
823 for ( i = 0; entrise > i; i += 1 )
825 memcpy(&rel, &rp[i],
sizeof(rel));
826 if ( rp[i].symptr && rp[i].symptr->number == (
unsigned int)(-1) )
828 fprintf(stderr,
"Internal error !!\n");
829 fprintf(stderr,
" relocation entry have no symbol\nabort\n");
832 rel.r_info = ((rp[i].symptr ? rp[i].symptr->number : sp_x->info->number) << 8) + (rp[i].type & 0xFF);
833 swapmemory(&rel,
"ll", 1);
843 if ( sycb->head.cbLineOffset > 0 )
847 size_1 = sycb->head.cbLine;
848 sycb->head.cbLineOffset = pos;
850 fseek(fp, sycb->head.cbLineOffset, SEEK_SET);
851 fwrite(sycb->cbLine_Ptr, size_1, 1, fp);
853 if ( sycb->head.cbDnOffset > 0 )
857 size_2 = 8L * sycb->head.idnMax;
858 sycb->head.cbDnOffset = pos;
860 fseek(fp, sycb->head.cbDnOffset, SEEK_SET);
861 swapmemory(sycb->cbDn_Ptr,
"ll", sycb->head.idnMax);
862 fwrite(sycb->cbDn_Ptr, size_2, 1, fp);
863 swapmemory(sycb->cbDn_Ptr,
"ll", sycb->head.idnMax);
865 if ( sycb->head.cbPdOffset > 0 )
869 size_3 = 52L * sycb->head.ipdMax;
870 sycb->head.cbPdOffset = pos;
872 fseek(fp, sycb->head.cbPdOffset, SEEK_SET);
873 swapmemory(sycb->cbPd_Ptr,
"lllllllllsslll", sycb->head.ipdMax);
874 fwrite(sycb->cbPd_Ptr, size_3, 1, fp);
875 swapmemory(sycb->cbPd_Ptr,
"lllllllllsslll", sycb->head.ipdMax);
877 if ( sycb->head.cbSymOffset > 0 )
881 size_4 = 12L * sycb->head.isymMax;
882 sycb->head.cbSymOffset = pos;
884 fseek(fp, sycb->head.cbSymOffset, SEEK_SET);
885 swapmemory(sycb->cbSym_Ptr,
"lll", sycb->head.isymMax);
886 fwrite(sycb->cbSym_Ptr, size_4, 1, fp);
887 swapmemory(sycb->cbSym_Ptr,
"lll", sycb->head.isymMax);
889 if ( sycb->head.cbOptOffset > 0 )
893 size_5 = 12L * sycb->head.ioptMax;
895 fseek(fp, sycb->head.cbOptOffset, SEEK_SET);
896 swapmemory(sycb->cbOpt_Ptr,
"lll", sycb->head.ioptMax);
897 fwrite(sycb->cbOpt_Ptr, size_5, 1, fp);
898 swapmemory(sycb->cbOpt_Ptr,
"lll", sycb->head.ioptMax);
900 if ( sycb->head.cbAuxOffset > 0 )
904 size_6 = 4L * sycb->head.iauxMax;
905 sycb->head.cbAuxOffset = pos;
907 fseek(fp, sycb->head.cbAuxOffset, SEEK_SET);
908 swapmemory(sycb->cbAux_Ptr,
"l", sycb->head.iauxMax);
909 fwrite(sycb->cbAux_Ptr, size_6, 1, fp);
910 swapmemory(sycb->cbAux_Ptr,
"l", sycb->head.iauxMax);
912 if ( sycb->head.cbSsOffset > 0 )
916 size_7 = sycb->head.issMax;
917 sycb->head.cbSsOffset = pos;
919 fseek(fp, sycb->head.cbSsOffset, SEEK_SET);
920 fwrite(sycb->cbSs_Ptr, size_7, 1, fp);
922 if ( sycb->head.cbSsExtOffset > 0 )
926 size_8 = sycb->head.issExtMax;
927 sycb->head.cbSsExtOffset = pos;
929 fseek(fp, sycb->head.cbSsExtOffset, SEEK_SET);
930 fwrite(sycb->cbSsExt_Ptr, size_8, 1, fp);
932 if ( sycb->head.cbFdOffset > 0 )
936 size_9 = 72L * sycb->head.ifdMax;
937 sycb->head.cbFdOffset = pos;
939 fseek(fp, sycb->head.cbFdOffset, SEEK_SET);
940 swapmemory(sycb->cbFd_Ptr,
"llllllllllsslllllll", sycb->head.ifdMax);
941 fwrite(sycb->cbFd_Ptr, size_9, 1, fp);
942 swapmemory(sycb->cbFd_Ptr,
"llllllllllsslllllll", sycb->head.ifdMax);
944 if ( sycb->head.cbRfdOffset > 0 )
948 size_A = 4L * sycb->head.crfd;
949 sycb->head.cbRfdOffset = pos;
951 fseek(fp, sycb->head.cbRfdOffset, SEEK_SET);
952 swapmemory(sycb->cbRfd_Ptr,
"l", sycb->head.crfd);
953 fwrite(sycb->cbRfd_Ptr, size_A, 1, fp);
954 swapmemory(sycb->cbRfd_Ptr,
"l", sycb->head.crfd);
956 if ( sycb->head.cbExtOffset > 0 )
960 size_B = 16L * sycb->head.iextMax;
961 sycb->head.cbExtOffset = pos;
962 fseek(fp, sycb->head.cbExtOffset, SEEK_SET);
963 swapmemory(sycb->cbExt_Ptr,
"sslll", sycb->head.iextMax);
964 fwrite(sycb->cbExt_Ptr, size_B, 1, fp);
965 swapmemory(sycb->cbExt_Ptr,
"sslll", sycb->head.iextMax);
967 fseek(fp, basepos, SEEK_SET);
968 swapmemory(sycb,
"sslllllllllllllllllllllll", 1);
969 fwrite(sycb,
sizeof(
hdrr), 1, fp);
970 swapmemory(sycb,
"sslllllllllllllllllllllll", 1);
980 count = ehp->e_shnum;
982 elf->scp[
count - 1] = scp;
984 add_symbol(elf, 0, 0, 3, 0, scp, 0);
993 for ( s = 1; s < elf->ehp->e_shnum; s += 1 )
995 if ( shtype == elf->scp[s]->shr.sh_type )
997 elf->ehp->e_shnum -= 1;
1002 for ( ; s < elf->ehp->e_shnum; s += 1 )
1004 elf->scp[s] = elf->scp[s + 1];
1015 for ( s = 1; s < elf->ehp->e_shnum; s += 1 )
1017 if ( !strcmp(elf->scp[s]->name, secname) )
1019 elf->ehp->e_shnum -= 1;
1020 rmsec = elf->scp[s];
1024 for ( ; s < elf->ehp->e_shnum; s += 1 )
1026 elf->scp[s] = elf->scp[s + 1];
1035 for ( i = 1; i < elf->ehp->e_shnum; i += 1 )
1037 if ( stype == elf->scp[i]->shr.sh_type )
1049 for ( i = 1; i < elf->ehp->e_shnum; i += 1 )
1051 if ( !strcmp(elf->scp[i]->name, secname) )
1059unsigned int *get_section_data(
elf_file *elf,
unsigned int addr)
1063 for ( i = 1; i < elf->ehp->e_shnum; i += 1 )
1066 elf->scp[i]->shr.sh_type == SHT_PROGBITS && addr >= elf->scp[i]->shr.sh_addr
1067 && addr < elf->scp[i]->shr.sh_size + elf->scp[i]->shr.sh_addr )
1069 return (
unsigned int *)&elf->scp[i]->data[addr - elf->scp[i]->shr.sh_addr];
1077 unsigned int entrise;
1082 scp = search_section(elf, SHT_SYMTAB);
1087 entrise = scp->shr.sh_size / scp->shr.sh_entsize;
1089 for ( i = 1; entrise > i; i += 1 )
1091 if ( syp[i]->name && syp[i]->bind == STB_GLOBAL && !strcmp(syp[i]->name, name) )
1105 if ( !sym->sym.st_shndx )
1109 if ( sym->sym.st_shndx <= 0xFEFF )
1113 return sym->sym.st_shndx == SHN_ABS;
1118 unsigned int entrise;
1123 symtbl = search_section(elf, SHT_SYMTAB);
1128 entrise = symtbl->shr.sh_size / symtbl->shr.sh_entsize;
1131 newtab[entrise] = sym;
1132 symtbl->shr.sh_size += symtbl->shr.sh_entsize;
1133 symtbl->data = (uint8_t *)newtab;
1136 sym->name = strdup(name);
1140 sym->sym.st_info = (type & 0xF) + 16 * bind;
1141 sym->sym.st_value = value;
1145 sym->sym.st_shndx = 1;
1149 sym->sym.st_shndx = st_shndx;
1156 if ( !is_defined_symbol(sym) )
1160 if ( sym->sym.st_shndx != SHN_ABS && elf->ehp->e_type == ET_REL )
1162 return sym->shptr->shr.sh_addr + sym->sym.st_value;
1164 return sym->sym.st_value;
1172 unsigned int entrise;
1180 sections = elf->ehp->e_shnum;
1181 entrise = scp->shr.sh_size / scp->shr.sh_entsize;
1185 scp->data = (uint8_t *)newtab;
1188 for ( sc = 1; sections > sc; sc += 1 )
1190 for ( i = 1; entrise > i; i += 1 )
1193 oldtab[i] && oldtab[i]->type == STT_SECTION && !oldtab[i]->name && oldtab[i]->shptr
1194 && !strcmp(oldtab[i]->shptr->name, elf->scp[sc]->name) )
1196 newtab[d] = oldtab[i];
1203 for ( j = 1; entrise > j; j += 1 )
1205 if ( oldtab[j] && oldtab[j]->type == STT_SECTION && !oldtab[j]->name )
1210 for ( k = 1; entrise > k; k += 1 )
1212 if ( oldtab[k] && !oldtab[k]->bind )
1214 newtab[d] = oldtab[k];
1219 scp->shr.sh_info = d;
1220 for ( m = 1; entrise > m; m += 1 )
1224 newtab[d] = oldtab[m];
1229 scp->shr.sh_size = scp->shr.sh_entsize * d;
1237 for ( s = 1; s < elf->ehp->e_shnum; s += 1 )
1239 switch ( elf->scp[s]->shr.sh_type )
1243 reorder_an_symtab(elf, elf->scp[s]);
1251unsigned int adjust_align(
unsigned int value,
unsigned int align)
1253 return ~(align - 1) & (align + value - 1);
1256void rebuild_section_name_strings(
elf_file *elf)
1258 unsigned int offset;
1264 if ( elf->scp == NULL )
1268 for ( i_1 = 1; i_1 < elf->ehp->e_shnum; i_1 += 1 )
1270 namesize += strlen(elf->scp[i_1]->name) + 1;
1272 if ( elf->shstrptr->data )
1274 free(elf->shstrptr->data);
1276 elf->shstrptr->data = (uint8_t *)calloc(1, namesize);
1277 elf->shstrptr->shr.sh_size = namesize;
1279 for ( i_2 = 1; i_2 < elf->ehp->e_shnum; i_2 += 1 )
1281 strcpy((
char *)&elf->shstrptr->data[offset], elf->scp[i_2]->name);
1282 elf->scp[i_2]->shr.sh_name = offset;
1283 offset += strlen(elf->scp[i_2]->name) + 1;
1287static size_t search_string_table(
const char *tbltop,
size_t endindex,
const char *str)
1291 for ( idx = 1; idx < endindex; idx += strlen(&tbltop[idx]) + 1 )
1293 if ( !strcmp(str, &tbltop[idx]) )
1301static void rebuild_a_symbol_name_strings(
elf_section *scp)
1307 unsigned int entrise;
1311 entrise = scp->shr.sh_size / scp->shr.sh_entsize;
1315 for ( i_1 = 1; i_1 < entrise; i_1 += 1 )
1317 namesize = (syp[i_1] != NULL && syp[i_1]->name != NULL) ? (strlen(syp[i_1]->name) + namesize + 1) : namesize;
1323 strtab->data = (uint8_t *)calloc(1, namesize);
1325 for ( i_2 = 1; i_2 < entrise; i_2 += 1 )
1327 if ( syp[i_2] != NULL && syp[i_2]->name != NULL )
1329 syp[i_2]->sym.st_name = search_string_table((
char *)strtab->data, offset, syp[i_2]->name);
1330 if ( !syp[i_2]->sym.st_name )
1332 strcpy((
char *)&strtab->data[offset], syp[i_2]->name);
1333 syp[i_2]->sym.st_name = offset;
1334 offset += strlen(syp[i_2]->name) + 1;
1338 strtab->shr.sh_size = offset;
1341void rebuild_symbol_name_strings(
elf_file *elf)
1345 if ( elf->scp == NULL )
1349 for ( sc = 1; sc < elf->ehp->e_shnum; sc += 1 )
1351 switch ( elf->scp[sc]->shr.sh_type )
1355 rebuild_a_symbol_name_strings(elf->scp[sc]);
1363static int comp_Elf_file_slot(
const void *a1,
const void *a2)
1371 if ( p1->type == EFS_TYPE_ELF_HEADER && p2->type == EFS_TYPE_ELF_HEADER )
1375 if ( p1->type == EFS_TYPE_ELF_HEADER )
1379 if ( p2->type == EFS_TYPE_ELF_HEADER )
1383 if ( p1->type == EFS_TYPE_PROGRAM_HEADER_TABLE && p2->type == EFS_TYPE_PROGRAM_HEADER_TABLE )
1387 if ( p1->type == EFS_TYPE_PROGRAM_HEADER_TABLE )
1391 if ( p2->type == EFS_TYPE_PROGRAM_HEADER_TABLE )
1395 if ( !p1->type && !p2->type )
1407 if ( p1->type == EFS_TYPE_END && p2->type == EFS_TYPE_END )
1411 if ( p1->type == EFS_TYPE_END )
1415 if ( p2->type == EFS_TYPE_END )
1419 if ( p1->type == EFS_TYPE_PROGRAM_HEADER_ENTRY && p2->type == EFS_TYPE_SECTION_HEADER_TABLE )
1423 if ( p1->type == EFS_TYPE_SECTION_HEADER_TABLE && p2->type == EFS_TYPE_PROGRAM_HEADER_ENTRY )
1427 if ( p2->offset == p1->offset )
1431 if ( p2->offset >= p1->offset )
1448 sections = elf->ehp->e_shnum;
1450 memcpy(scp, elf->scp, sections *
sizeof(
elf_section *));
1451 maxent = elf->ehp->e_shnum + 2;
1452 if ( elf->ehp->e_phnum )
1454 maxent = elf->ehp->e_phnum + elf->ehp->e_shnum + 3;
1457 resolt->type = EFS_TYPE_ELF_HEADER;
1462 if ( elf->ehp->e_phnum )
1466 resolt[1].type = EFS_TYPE_PROGRAM_HEADER_TABLE;
1467 resolt[1].offset = resolt->size;
1468 resolt[1].size =
sizeof(
Elf32_Phdr) * elf->ehp->e_phnum;
1469 resolt[1].align = 4;
1470 for ( seg = 0, d_1 = 2; seg < elf->ehp->e_phnum; seg += 1, d_1 += 1 )
1474 resolt[d_1].type = EFS_TYPE_PROGRAM_HEADER_ENTRY;
1475 resolt[d_1].d.php = &elf->php[seg];
1476 resolt[d_1].offset = elf->php[seg].phdr.p_offset;
1477 resolt[d_1].size = elf->php[seg].phdr.p_filesz;
1478 resolt[d_1].align = elf->php[seg].phdr.p_align;
1479 for ( phdscp = elf->php[seg].scp; *phdscp; phdscp += 1 )
1483 for ( s_1 = 0; s_1 < sections; s_1 += 1 )
1485 if ( *phdscp == scp[s_1] )
1494 resolt[d_1].type = EFS_TYPE_SECTION_HEADER_TABLE;
1495 resolt[d_1].offset = elf->ehp->e_shoff ?: (Elf32_Off)(-256);
1496 resolt[d_1].size =
sizeof(
Elf32_Shdr) * elf->ehp->e_shnum;
1497 resolt[d_1].align = 4;
1499 for ( s_2 = 1; s_2 < sections; s_2 += 1 )
1503 resolt[d_2].type = EFS_TYPE_SECTION_DATA;
1504 resolt[d_2].d.scp = scp[s_2];
1505 resolt[d_2].offset = scp[s_2]->shr.sh_offset;
1506 resolt[d_2].size = scp[s_2]->shr.sh_size;
1507 resolt[d_2].align = scp[s_2]->shr.sh_addralign;
1512 resolt[d_2].type = EFS_TYPE_END;
1514 qsort(resolt, d_2,
sizeof(
Elf_file_slot), comp_Elf_file_slot);
1523 for ( ; efs->type != EFS_TYPE_END; efs += 1 )
1525 unsigned int foffset;
1527 foffset = adjust_align(slot, efs->align);
1528 efs->offset = foffset;
1529 slot = efs->size + foffset;
1536 unsigned int segoffset;
1539 for ( ; efs->type != EFS_TYPE_END; efs += 1 )
1541 switch ( efs->type )
1543 case EFS_TYPE_PROGRAM_HEADER_TABLE:
1544 elf->ehp->e_phoff = efs->offset;
1546 case EFS_TYPE_PROGRAM_HEADER_ENTRY:
1547 efs->d.php->phdr.p_offset = efs->offset;
1548 scp = efs->d.php->scp;
1549 segoffset = efs->offset;
1550 (*scp)->shr.sh_offset = efs->offset;
1551 for ( i = 1; scp[i]; i += 1 )
1553 if ( scp[i]->shr.sh_type != SHT_NOBITS )
1555 segoffset = scp[i]->shr.sh_addr + efs->offset - (*scp)->shr.sh_addr;
1557 scp[i]->shr.sh_offset = segoffset;
1558 if ( scp[i]->shr.sh_type != SHT_NOBITS )
1560 segoffset += scp[i]->shr.sh_size;
1564 case EFS_TYPE_SECTION_HEADER_TABLE:
1565 elf->ehp->e_shoff = efs->offset;
1567 case EFS_TYPE_SECTION_DATA:
1568 efs->d.php->phdr.p_filesz = efs->offset;
1578 unsigned int offset;
1579 unsigned int size_tmp;
1580 unsigned int offset_tmp;
1587 offset_tmp = efs->offset;
1589 for ( slot = efs; slot->type != EFS_TYPE_END; slot += 1 )
1591 unsigned int oldend_1;
1592 unsigned int size_1;
1593 unsigned int startpos_1;
1595 oldend_1 = slot->size;
1596 startpos_1 = slot->offset;
1597 if ( oldend_1 == 0 )
1599 offset = slot->offset;
1603 offset = oldend_1 + startpos_1 - 1;
1606 switch ( slot->type )
1608 case EFS_TYPE_ELF_HEADER:
1609 name =
"[Elf header]";
1611 case EFS_TYPE_PROGRAM_HEADER_TABLE:
1612 name =
"[Proram Header Table]";
1614 case EFS_TYPE_PROGRAM_HEADER_ENTRY:
1615 sprintf(tmp,
"[Proram Header entry %d]", (
int)(0xCCCCCCCD * ((
char *)slot->d.php - (
char *)elf->php)) >> 3);
1618 case EFS_TYPE_SECTION_HEADER_TABLE:
1619 name =
"[Section Header Table]";
1621 case EFS_TYPE_SECTION_DATA:
1622 sprintf(tmp,
"%s data", slot->d.scp->name);
1626 name =
"internal error Unknown EFS type !!!!";
1629 if ( startpos_1 > offset_tmp + 1 )
1631 printf(
"%36s = %08x-%08x (%06x)\n",
"**Blank**", offset_tmp + 1, startpos_1 - 1, startpos_1 - offset_tmp - 1);
1633 offset_tmp = size_1;
1634 printf(
"%36s = %08x-%08x (%06x)\n", name, startpos_1, size_1, oldend_1);
1635 if ( slot->type == EFS_TYPE_PROGRAM_HEADER_ENTRY )
1637 scp = slot->d.php->scp;
1638 (*scp)->shr.sh_offset = slot->offset;
1639 size_tmp = slot->offset;
1640 for ( i = 0; scp[i]; i += 1 )
1642 unsigned int oldend_2;
1643 unsigned int size_2;
1644 unsigned int startpos_2;
1646 if ( scp[i]->shr.sh_type == SHT_NOBITS )
1652 oldend_2 = scp[i]->shr.sh_size;
1654 startpos_2 = scp[i]->shr.sh_offset;
1655 size_2 = (oldend_2 == 0) ? (scp[i]->shr.sh_offset) : (Elf32_Off)(oldend_2 + startpos_2 - 1);
1656 sprintf(tmp,
"(%s)", scp[i]->name);
1658 if ( startpos_2 > size_tmp + 1 )
1660 printf(
"%36s | %08x-%08x (%06x)\n",
"**Blank**", size_tmp + 1, startpos_2 - 1, startpos_2 - size_tmp - 1);
1663 printf(
"%36s | %08x-%08x (%06x)\n", name, startpos_2, size_2, scp[i]->shr.sh_size);
u32 count
start sector of fragmented bd/file