7 #define __attribute__(x)
16 const char *full=
"", *abbr=
"";
18 case PAIR_EXPORTS: full=
"Export Table"; abbr=
"Exports";
break;
19 case PAIR_IMPORTS: full=
"Import Table"; abbr=
"Imports";
break;
23 case PAIR_BASERELOCS: full=
"Base Relocation Table"; abbr=
"BaseReloc";
break;
24 case PAIR_DEBUG: full=
"Debug"; abbr=
"Debug";
break;
27 case PAIR_TLS: full=
"TLS Table"; abbr=
"TLS";
break;
30 case PAIR_IAT: full=
"Import Address Table"; abbr=
"IAT";
break;
94 unsigned char dos_magic[2];
96 if (
'M'!=dos_magic[0] ||
'Z'!=dos_magic[1])
100 uint32_t lfanew_disk;
101 file->
read_content(0x3c, &lfanew_disk,
sizeof lfanew_disk);
105 unsigned char pe_magic[4];
106 file->
read_content(pe_offset, pe_magic,
sizeof pe_magic);
107 if (
'P'!=pe_magic[0] ||
'E'!=pe_magic[1] ||
'\0'!=pe_magic[2] ||
'\0'!=pe_magic[3])
130 fprintf(stderr,
"SgAsmPEFileHeader::parse: warning: short read of PE header at byte 0x%08"PRIx64
"\n",
get_offset());
155 fprintf(stderr,
"SgAsmPEFileHeader::parse: warning: short read of PE Optional Header at byte 0x%08"PRIx64
"\n",
202 fprintf(stderr,
"SgAsmPEFileHeader::parse: warning: short read of PE Optional Header at byte 0x%08"PRIx64
"\n",
234 throw FormatError(
"unrecognized Windows PE optional header magic number");
239 for (
size_t i = 0; i <
sizeof(fh.
e_magic); ++i)
308 fprintf(stderr,
"SgAsmPEFileHeader::parse: warning: unrecognized e_cputype = 0x%x (%u)\n",
p_e_cpu_type,
p_e_cpu_type);
329 fprintf(stderr,
"warning: PE File Header contains an unreasonable number of Rva/Size pairs. Limiting to 1000.\n");
462 fprintf(stderr,
"SgAsmPEFileHeader::set_rvasize_pair: warning: index %u exceeds specification limit\n", (
unsigned)idx);
467 for (
size_t i=0; i<=(size_t)idx; i++) {
476 ROSE_ASSERT(pair!=NULL);
484 const char *short_name;
518 if (
sizeof(pairs_disk)!=
read_content_local(pairs_offset, &pairs_disk,
sizeof pairs_disk,
false))
519 fprintf(stderr,
"SgAsmPEFileHeader::add_rvasize_pairs: warning: RVA/Size pair %zu at file offset 0x%08"PRIx64
520 " extends beyond the end of file (assuming 0/0)\n", i,
get_offset()+pairs_offset);
538 const char *tabname_short;
548 ROSE_ASSERT(map!=NULL);
550 fprintf(stderr,
"SgAsmPEFileHeader::create_table_sections: warning: pair-%zu, rva=0x%08"PRIx64
", size=%"PRIu64
551 " bytes \"%s\": unable to find a mapping for the virtual address (skipping)\n",
555 std::pair<Extent, MemoryMap::Segment> me = map->
at(pair_va);
556 rose_addr_t file_offset = me.second.get_buffer_offset(me.first, pair_va);
566 bool seen_exports =
false;
567 for (SgAsmGenericSectionPtrList::iterator si=sections.begin(); !seen_exports && si!=sections.end(); ++si)
581 bool seen_imports =
false;
582 for (SgAsmGenericSectionPtrList::iterator si=sections.begin(); !seen_imports && si!=sections.end(); ++si)
663 for (
size_t i=0; i<all->get_sections().size(); i++) {
678 for (
size_t i=0, nfound=0; i<all->get_sections().size(); i++) {
684 min_offset = std::min(min_offset, pesec->
get_offset() );
691 header_size = header_size2;
732 size_t rvasize_offset;
740 unsigned char *oh =
new unsigned char[oh_size];
752 if (0!=oh[oh_size-1])
break;
769 for (SgAsmGenericSectionPtrList::const_iterator si=sections.begin(); si!=sections.end(); ++si) {
773 for (SgAsmGenericSectionPtrList::const_iterator si=sections.begin(); si!=sections.end(); ++si) {
777 for (SgAsmGenericSectionPtrList::const_iterator si=sections.begin(); si!=sections.end(); ++si) {
787 size_t rvasize_offset;
795 unsigned char *oh =
new unsigned char[oh_size];
810 if (0!=oh[oh_size-1])
break;
821 spos =
write(f, spos, oh_size, oh);
830 sprintf(p,
"%sPEFileHeader[%zd].", prefix, idx);
832 sprintf(p,
"%sPEFileHeader.", prefix);
838 struct tm *tm = localtime(&t);
840 strftime(time_str,
sizeof time_str,
"%c", tm);
842 strcpy(time_str,
"INVALID");
847 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_nsections",
p_e_nsections);
848 fprintf(f,
"%s%-*s = %u (%s)\n", p, w,
"e_time",
p_e_time, time_str);
850 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_coff_nsyms",
p_e_coff_nsyms);
852 fprintf(f,
"%s%-*s = [%d] \"%s\"\n", p, w,
"coff_symtab",
855 fprintf(f,
"%s%-*s = none\n", p, w,
"coff_symtab");
859 fprintf(f,
"%s%-*s = 0x%04x %s\n", p, w,
"e_opt_magic",
p_e_opt_magic,
872 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_reserved9",
p_e_reserved9);
876 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_subsystem",
p_e_subsystem);
878 fprintf(f,
"%s%-*s = 0x%08x (%u)\n", p, w,
"e_stack_reserve_size",
888 assert((
size_t)nprint<
sizeof p2);
890 fprintf(f,
"%s%-*s = rva %s,\tsize 0x%08"PRIx64
" (%"PRIu64
")\n", p2, w,
"..",
895 fprintf(f,
"%s%-*s = [%d] \"%s\"\n", p, w,
"section_table",
898 fprintf(f,
"%s%-*s = none\n", p, w,
"section_table");
907 map->
dump(f, (std::string(p)+
"loader_map: ").c_str());
909 fprintf(f,
"%s%-*s = not defined\n", p, w,
"loader_map");