Load and relocate an ELF file.
384 unsigned int shdrptr;
385 unsigned int nameptr;
389 unsigned short shdrnum, shdrsize;
394 ret =
b_seek_read( 0, (
char *)&ehdr,
sizeof(ehdr));
398 PRINTERR(stderr,
"ELF header problems\n");
403 printf (
"Grab section header\n");
406 shdrptr = ehdr.e_shoff;
407 ret =
b_seek_read( shdrptr, (
char *)&shdr,
sizeof(shdr));
410 shdrsize = ehdr.e_shentsize;
411 shdrnum = ehdr.e_shnum;
414 printf (
"Grab string table section\n");
417 ret =
b_seek_read( ehdr.e_shoff + shdrsize * ehdr.e_shstrndx,
418 (
char *)&strtable,
sizeof(strtable));
420 strs = strtable.sh_offset;
435 text.size =
text.relasize = data.size = data.relasize =
438 bss.number = data.number = rodata.number =
text.number = -1;
440 shdrptr = ehdr.e_shoff;
441 for(i = 0; i < shdrnum; ++i) {
443 ret =
b_seek_read( shdrptr, (
char *)&shdr,
sizeof(shdr));
444 DEBUGPRINTF(
"==shdrptr=0x%x, sizeof=%d; size=0x%x\n",shdrptr,
sizeof(shdr),shdrsize );
445 if (ret !=
sizeof(shdr)) {
PRINTERR(stderr,
"input error at %s:%d :loaded%d",__FILE__,__LINE__,ret);
return ELFFLT_INPUT_ERROR;}
448 nameptr = strs + shdr.sh_name;
449 DEBUGPRINTF(
"==nameptr=%x(%x+%x), size=%d\n",nameptr,strs,shdr.sh_name,
sizeof(name) );
453 DEBUGPRINTF(
"==shdrptr=0x%x, sizeof=%d; size=0x%x\n",shdrptr,
sizeof(shdr),shdrsize );
455 printf (
"Section #%d: %-15s [section header 0x%x, offset=0x%x, size %d, vma=0x%x]\n",i,name,shdrptr,
456 shdr.sh_offset,shdr.sh_size, shdr.sh_addr);
458 if(
strncmp(name,
".text", 5) == 0) {
460 text.offset = shdr.sh_offset;
461 text.size = shdr.sh_size;
462 text.base_addr = shdr.sh_addr;
463 }
else if(
strncmp(name,
".rel.text", 9) == 0) {
464 text.relaoff = shdr.sh_offset;
465 text.relasize = shdr.sh_size;
466 }
else if(
strncmp(name,
".data", 5) == 0) {
468 data.offset = shdr.sh_offset;
469 data.size = shdr.sh_size;
470 data.base_addr = shdr.sh_addr;
471 }
else if(
strncmp(name,
".rodata", 7) == 0) {
473 rodata.offset = shdr.sh_offset;
474 rodata.size = shdr.sh_size;
475 rodata.base_addr = shdr.sh_addr;
476 }
else if(
strncmp(name,
".rel.rodata", 11) == 0) {
477 rodata.relaoff = shdr.sh_offset;
478 rodata.relasize = shdr.sh_size;
479 }
else if(
strncmp(name,
".rel.data", 9) == 0) {
480 data.relaoff = shdr.sh_offset;
481 data.relasize = shdr.sh_size;
482 }
else if(
strncmp(name,
".rela.", 6) == 0) {
483 PRINTERR(stderr,
"RELA relocs are not supported.");
485 }
else if(
strncmp(name,
".symtab", 7) == 0) {
488 }
else if(
strncmp(name,
".strtab", 7) == 0) {
491 }
else if(
strncmp(name,
".bss", 4) == 0) {
492 bss.size = shdr.sh_size;
501 PRINTERR(stderr,
"No symbol table found.");
505 PRINTERR(stderr,
"No strings table found.");
509 PRINTERR(stderr,
"No .text segment found.");
513 if ( (
text.relasize + rodata.relasize+ data.relasize) <=0 ) {
514 PRINTERR(stderr,
"Found no reloc sections. Please link with -r -d options.\n");
519 bss.address = (
char *)
malloc(bss.size);
523 data.address = (
char *)
malloc(data.size);
531 rodata.address = (
char *)
malloc(rodata.size);
535 rodata.name=
".rodata";
542 b_seek_read(rodata.offset, rodata.address, rodata.size);
546 dump_section( data.name, (
unsigned char *)data.address, data.size );
547 dump_section( rodata.name, (
unsigned char *)rodata.address, rodata.size );
558 printf (
"Prepare flat\n");
560 int div0hack_size =
sizeof(
div0_arm);
562 int flatmainsize =
sizeof(
flat_hdr)+
text.size+div0hack_size+data.size+rodata.size;
563 int flatrelocsize =
text.relasize+rodata.relasize+data.relasize;
584 printf(
">>elf2flt: load segments\n");
591 rodata.flat_offset =
offset;
592 DEBUGPRINTF(
"load .rodata to %x (%x->%x)\n",offset,rodata.size,rodata.size+
align4(rodata.size));
594 offset+=rodata.size+
align4(rodata.size);
596 data.flat_offset =
offset;
597 DEBUGPRINTF(
"load .data to %x (%x->%x)\n",offset,data.size,data.size+
align4(data.size));
599 offset+=data.size+
align4(data.size);
647 printf(
">>elf2flt: lookup entry symbols\n");
651 PRINTERR(stderr,
"No or invalid section of _module_info. This symbol should be initialized as ModuleInfo structure.\n");
658 PRINTERR(stderr,
"Wrong _module_info->magicnum value. Please check correct filling of this structure\n");
663 PRINTERR(stderr,
"Wrong _module_info->sizeof_struct value. Please check correct filling of this structure\n");
685 new_import_buf[new_import_cnt++] = idx;
686 int pcnt = new_import_cnt;
697 new_import_buf[pcnt] = (cnt << 24) | new_import_buf[pcnt];
705 printf(
"\nFLT Headers:\n");
713 printf(
"\nModule info:\n");
717 char* branches_str[] = {
"any branch",
"CHDK",
"CHDK_DE",
"CHDK_SDM",
"PRIVATEBUILD"};
722 printf(
"Any platform.\n");
746 for (i = 0; i< new_import_cnt;)
749 int cnt = new_import_buf[i] >> 24;
751 for (j=0; j<cnt; j++)
763 printf(
"\n\nOutput file %s (size=%d bytes)\n",fltfile,filesize);
767 i =
write(output_fd, new_import_buf, new_import_cnt*
sizeof(
uint32_t));
769 free(new_import_buf);