2 #include <featureTests.h>
3 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
4 #include "sage3basic.h"
8 #pragma GCC diagnostic ignored "-Waddress-of-packed-member"
34 if (fh.e_magic[0]!=
'L' ||
35 (fh.e_magic[1]!=
'E' && fh.e_magic[1]!=
'X'))
36 throw FormatError(
"Bad LE/LX magic number");
40 const char *section_name =
FAMILY_LE == p_exec_format->get_family() ?
"LE File Header" :
"LX File Header";
44 p_e_byte_order = ByteOrder::le_to_host(fh.e_byte_order);
45 p_e_word_order = ByteOrder::le_to_host(fh.e_word_order);
46 ROSE_ASSERT(p_e_byte_order == p_e_word_order);
47 ByteOrder::Endianness sex = 0 == p_e_byte_order ? ByteOrder::ORDER_LSB : ByteOrder::ORDER_MSB;
49 p_e_format_level = ByteOrder::disk_to_host(sex, fh.e_format_level);
50 p_e_cpu_type = ByteOrder::disk_to_host(sex, fh.e_cpu_type);
51 p_e_os_type = ByteOrder::disk_to_host(sex, fh.e_os_type);
52 p_e_module_version = ByteOrder::disk_to_host(sex, fh.e_module_version);
53 p_e_flags = ByteOrder::disk_to_host(sex, fh.e_flags);
54 p_e_npages = ByteOrder::disk_to_host(sex, fh.e_npages);
55 p_e_eip_section = ByteOrder::disk_to_host(sex, fh.e_eip_section);
56 p_e_eip = ByteOrder::disk_to_host(sex, fh.e_eip);
57 p_e_esp_section = ByteOrder::disk_to_host(sex, fh.e_esp_section);
58 p_e_esp = ByteOrder::disk_to_host(sex, fh.e_esp);
59 p_e_page_size = ByteOrder::disk_to_host(sex, fh.e_page_size);
60 if (
FAMILY_LE == p_exec_format->get_family()) {
61 p_e_last_page_size = ByteOrder::disk_to_host(sex, fh.e_lps_or_shift);
62 p_e_page_offset_shift = 0;
64 ROSE_ASSERT(FAMILY_LX == p_exec_format->get_family());
65 p_e_last_page_size = 0;
66 p_e_page_offset_shift = ByteOrder::disk_to_host(sex, fh.e_lps_or_shift);
68 p_e_fixup_sect_size = ByteOrder::disk_to_host(sex, fh.e_fixup_sect_size);
69 p_e_fixup_sect_cksum = ByteOrder::disk_to_host(sex, fh.e_fixup_sect_cksum);
70 p_e_loader_sect_size = ByteOrder::disk_to_host(sex, fh.e_loader_sect_size);
71 p_e_loader_sect_cksum = ByteOrder::disk_to_host(sex, fh.e_loader_sect_cksum);
72 p_e_secttab_rfo = ByteOrder::disk_to_host(sex, fh.e_secttab_rfo);
73 p_e_secttab_nentries = ByteOrder::disk_to_host(sex, fh.e_secttab_nentries);
74 p_e_pagetab_rfo = ByteOrder::disk_to_host(sex, fh.e_pagetab_rfo);
75 p_e_iterpages_offset = ByteOrder::disk_to_host(sex, fh.e_iterpages_offset);
76 p_e_rsrctab_rfo = ByteOrder::disk_to_host(sex, fh.e_rsrctab_rfo);
77 p_e_rsrctab_nentries = ByteOrder::disk_to_host(sex, fh.e_rsrctab_nentries);
78 p_e_resnametab_rfo = ByteOrder::disk_to_host(sex, fh.e_resnametab_rfo);
79 p_e_entrytab_rfo = ByteOrder::disk_to_host(sex, fh.e_entrytab_rfo);
80 p_e_fmtdirtab_rfo = ByteOrder::disk_to_host(sex, fh.e_fmtdirtab_rfo);
81 p_e_fmtdirtab_nentries = ByteOrder::disk_to_host(sex, fh.e_fmtdirtab_nentries);
82 p_e_fixup_pagetab_rfo = ByteOrder::disk_to_host(sex, fh.e_fixup_pagetab_rfo);
83 p_e_fixup_rectab_rfo = ByteOrder::disk_to_host(sex, fh.e_fixup_rectab_rfo);
84 p_e_import_modtab_rfo = ByteOrder::disk_to_host(sex, fh.e_import_modtab_rfo);
85 p_e_import_modtab_nentries = ByteOrder::disk_to_host(sex, fh.e_import_modtab_nentries);
86 p_e_import_proctab_rfo = ByteOrder::disk_to_host(sex, fh.e_import_proctab_rfo);
87 p_e_ppcksumtab_rfo = ByteOrder::disk_to_host(sex, fh.e_ppcksumtab_rfo);
88 p_e_data_pages_offset = ByteOrder::disk_to_host(sex, fh.e_data_pages_offset);
89 p_e_preload_npages = ByteOrder::disk_to_host(sex, fh.e_preload_npages);
90 p_e_nonresnametab_offset = ByteOrder::disk_to_host(sex, fh.e_nonresnametab_offset);
91 p_e_nonresnametab_size = ByteOrder::disk_to_host(sex, fh.e_nonresnametab_size);
92 p_e_nonresnametab_cksum = ByteOrder::disk_to_host(sex, fh.e_nonresnametab_cksum);
93 p_e_auto_ds_section = ByteOrder::disk_to_host(sex, fh.e_auto_ds_section);
94 p_e_debug_info_rfo = ByteOrder::disk_to_host(sex, fh.e_debug_info_rfo);
95 p_e_debug_info_size = ByteOrder::disk_to_host(sex, fh.e_debug_info_size);
96 p_e_num_instance_preload = ByteOrder::disk_to_host(sex, fh.e_num_instance_preload);
97 p_e_num_instance_demand = ByteOrder::disk_to_host(sex, fh.e_num_instance_demand);
98 p_e_heap_size = ByteOrder::disk_to_host(sex, fh.e_heap_size);
101 for (
size_t i = 0; i <
sizeof(fh.e_magic); ++i)
102 p_magic.push_back(fh.e_magic[i]);
107 p_exec_format->set_sex( sex );
108 switch (p_e_os_type) {
110 case 1: p_exec_format->set_abi(
ABI_OS2 );
break;
111 case 2: p_exec_format->set_abi(
ABI_NT );
break;
112 case 3: p_exec_format->set_abi(
ABI_MSDOS );
break;
113 case 4: p_exec_format->set_abi(
ABI_WIN386 );
break;
114 default: p_exec_format->set_abi(
ABI_OTHER );
break;
116 p_exec_format->set_abi_version( 0 );
117 p_exec_format->set_word_size( 4 );
118 p_exec_format->set_version( p_e_format_level );
119 p_exec_format->set_is_current_version( 0 == p_e_format_level );
122 switch (p_e_cpu_type) {
147 bool was_tracking = file->get_tracking_references();
148 file->set_tracking_references(
false);
152 unsigned char dos_magic[2];
154 if (
'M'!=dos_magic[0] ||
'Z'!=dos_magic[1])
158 uint32_t lfanew_disk;
159 file->
read_content(0x3c, &lfanew_disk,
sizeof lfanew_disk);
160 rose_addr_t le_offset = ByteOrder::le_to_host(lfanew_disk);
163 unsigned char le_magic[4];
164 file->
read_content(le_offset, le_magic,
sizeof le_magic);
165 if (
'L'!=le_magic[0] || (
'E'!=le_magic[1] &&
'X'!=le_magic[1]))
168 file->set_tracking_references(was_tracking);
172 file->set_tracking_references(was_tracking);
178 SgAsmLEFileHeader::encode(ByteOrder::Endianness sex, LEFileHeader_disk *disk)
const
180 for (
size_t i=0; i<NELMTS(disk->e_magic); i++)
182 host_to_disk(sex, p_e_byte_order, &(disk->e_byte_order));
183 host_to_disk(sex, p_e_word_order, &(disk->e_word_order));
184 host_to_disk(sex, p_e_format_level, &(disk->e_format_level));
185 host_to_disk(sex, p_e_cpu_type, &(disk->e_cpu_type));
186 host_to_disk(sex, p_e_os_type, &(disk->e_os_type));
187 host_to_disk(sex, p_e_module_version, &(disk->e_module_version));
188 host_to_disk(sex, p_e_flags, &(disk->e_flags));
189 host_to_disk(sex, p_e_npages, &(disk->e_npages));
190 host_to_disk(sex, p_e_eip_section, &(disk->e_eip_section));
191 host_to_disk(sex, p_e_eip, &(disk->e_eip));
192 host_to_disk(sex, p_e_esp_section, &(disk->e_esp_section));
193 host_to_disk(sex, p_e_esp, &(disk->e_esp));
194 host_to_disk(sex, p_e_page_size, &(disk->e_page_size));
195 if (
FAMILY_LE == p_exec_format->get_family()) {
196 host_to_disk(sex, p_e_last_page_size, &(disk->e_lps_or_shift));
198 ROSE_ASSERT(FAMILY_LX == p_exec_format->get_family());
199 host_to_disk(sex, p_e_page_offset_shift, &(disk->e_lps_or_shift));
201 host_to_disk(sex, p_e_fixup_sect_size, &(disk->e_fixup_sect_size));
202 host_to_disk(sex, p_e_fixup_sect_cksum, &(disk->e_fixup_sect_cksum));
203 host_to_disk(sex, p_e_loader_sect_size, &(disk->e_loader_sect_size));
204 host_to_disk(sex, p_e_loader_sect_cksum, &(disk->e_loader_sect_cksum));
205 host_to_disk(sex, p_e_secttab_rfo, &(disk->e_secttab_rfo));
206 host_to_disk(sex, p_e_secttab_nentries, &(disk->e_secttab_nentries));
207 host_to_disk(sex, p_e_pagetab_rfo, &(disk->e_pagetab_rfo));
208 host_to_disk(sex, p_e_iterpages_offset, &(disk->e_iterpages_offset));
209 host_to_disk(sex, p_e_rsrctab_rfo, &(disk->e_rsrctab_rfo));
210 host_to_disk(sex, p_e_rsrctab_nentries, &(disk->e_rsrctab_nentries));
211 host_to_disk(sex, p_e_resnametab_rfo, &(disk->e_resnametab_rfo));
212 host_to_disk(sex, p_e_entrytab_rfo, &(disk->e_entrytab_rfo));
213 host_to_disk(sex, p_e_fmtdirtab_rfo, &(disk->e_fmtdirtab_rfo));
214 host_to_disk(sex, p_e_fmtdirtab_nentries, &(disk->e_fmtdirtab_nentries));
215 host_to_disk(sex, p_e_fixup_pagetab_rfo, &(disk->e_fixup_pagetab_rfo));
216 host_to_disk(sex, p_e_fixup_rectab_rfo, &(disk->e_fixup_rectab_rfo));
217 host_to_disk(sex, p_e_import_modtab_rfo, &(disk->e_import_modtab_rfo));
218 host_to_disk(sex, p_e_import_modtab_nentries, &(disk->e_import_modtab_nentries));
219 host_to_disk(sex, p_e_import_proctab_rfo, &(disk->e_import_proctab_rfo));
220 host_to_disk(sex, p_e_ppcksumtab_rfo, &(disk->e_ppcksumtab_rfo));
221 host_to_disk(sex, p_e_data_pages_offset, &(disk->e_data_pages_offset));
222 host_to_disk(sex, p_e_preload_npages, &(disk->e_preload_npages));
223 host_to_disk(sex, p_e_nonresnametab_offset, &(disk->e_nonresnametab_offset));
224 host_to_disk(sex, p_e_nonresnametab_size, &(disk->e_nonresnametab_size));
225 host_to_disk(sex, p_e_nonresnametab_cksum, &(disk->e_nonresnametab_cksum));
226 host_to_disk(sex, p_e_auto_ds_section, &(disk->e_auto_ds_section));
227 host_to_disk(sex, p_e_debug_info_rfo, &(disk->e_debug_info_rfo));
228 host_to_disk(sex, p_e_debug_info_size, &(disk->e_debug_info_size));
229 host_to_disk(sex, p_e_num_instance_preload, &(disk->e_num_instance_preload));
230 host_to_disk(sex, p_e_num_instance_demand, &(disk->e_num_instance_demand));
231 host_to_disk(sex, p_e_heap_size, &(disk->e_heap_size));
240 encode(get_sex(), &fh);
241 write(f, 0,
sizeof fh, &fh);
256 if (p_nonresname_table)
257 p_nonresname_table->
unparse(f);
266 SgAsmLEFileHeader::format_name()
const
268 if (
FAMILY_LE == p_exec_format->get_family()) {
271 ROSE_ASSERT(FAMILY_LX == p_exec_format->get_family());
282 sprintf(p,
"%s%sFileHeader[%zd].", prefix, format_name(), idx);
284 sprintf(p,
"%s%sFileHeader.", prefix, format_name());
287 int w = std::max(1, DUMP_FIELD_WIDTH-(
int)strlen(p));
290 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_byte_order", p_e_byte_order);
291 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_word_order", p_e_word_order);
292 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_format_level", p_e_format_level);
293 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_cpu_type", p_e_cpu_type);
294 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_os_type", p_e_os_type);
295 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_module_version", p_e_module_version);
296 fprintf(f,
"%s%-*s = 0x%08x\n", p, w,
"e_flags", p_e_flags);
297 fprintf(f,
"%s%-*s = %" PRIu64
"\n", p, w,
"e_npages", p_e_npages);
298 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_eip_section", p_e_eip_section);
299 fprintf(f,
"%s%-*s = 0x%08" PRIx64
"\n", p, w,
"e_eip", p_e_eip);
300 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_esp_section", p_e_esp_section);
301 fprintf(f,
"%s%-*s = 0x%08" PRIx64
"\n", p, w,
"e_esp", p_e_esp);
302 fprintf(f,
"%s%-*s = %" PRIu64
"\n", p, w,
"e_page_size", p_e_page_size);
303 if (
FAMILY_LE == p_exec_format->get_family()) {
304 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_last_page_size", p_e_last_page_size);
306 ROSE_ASSERT(FAMILY_LX == p_exec_format->get_family());
307 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_page_offset_shift", p_e_page_offset_shift);
309 fprintf(f,
"%s%-*s = %" PRIu64
"\n", p, w,
"e_fixup_sect_size", p_e_fixup_sect_size);
310 fprintf(f,
"%s%-*s = 0x%08x\n", p, w,
"e_fixup_sect_cksum", p_e_fixup_sect_cksum);
311 fprintf(f,
"%s%-*s = %" PRIu64
"\n", p, w,
"e_loader_sect_size", p_e_loader_sect_size);
312 fprintf(f,
"%s%-*s = 0x%08x\n", p, w,
"e_loader_sect_cksum", p_e_loader_sect_cksum);
313 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_secttab_rfo",
314 p_e_secttab_rfo, p_e_secttab_rfo+p_offset);
315 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_secttab_nentries", p_e_secttab_nentries);
316 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_pagetab_rfo",
317 p_e_pagetab_rfo, p_e_pagetab_rfo+p_offset);
318 fprintf(f,
"%s%-*s = %" PRIu64
"\n", p, w,
"e_iterpages_offset", p_e_iterpages_offset);
319 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_rsrctab_rfo",
320 p_e_rsrctab_rfo, p_e_rsrctab_rfo+p_offset);
321 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_rsrctab_nentries", p_e_rsrctab_nentries);
322 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_resnametab_rfo",
323 p_e_resnametab_rfo, p_e_resnametab_rfo+p_offset);
324 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_entrytab_rfo",
325 p_e_entrytab_rfo, p_e_entrytab_rfo+p_offset);
326 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_fmtdirtab_rfo",
327 p_e_fmtdirtab_rfo, p_e_fmtdirtab_rfo+p_offset);
328 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_fmtdirtab_nentries", p_e_fmtdirtab_nentries);
329 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_fixup_pagetab_rfo",
330 p_e_fixup_pagetab_rfo, p_e_fixup_pagetab_rfo+p_offset);
331 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_fixup_rectab_rfo",
332 p_e_fixup_rectab_rfo, p_e_fixup_rectab_rfo+p_offset);
333 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_import_modtab_rfo",
334 p_e_import_modtab_rfo, p_e_import_modtab_rfo+p_offset);
335 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_import_modtab_nentries", p_e_import_modtab_nentries);
336 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_import_proctab_rfo",
337 p_e_import_proctab_rfo, p_e_import_proctab_rfo+p_offset);
338 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_ppcksumtab_rfo",
339 p_e_ppcksumtab_rfo, p_e_ppcksumtab_rfo+p_offset);
340 fprintf(f,
"%s%-*s = %" PRIu64
"\n", p, w,
"e_data_pages_offset", p_e_data_pages_offset);
341 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_preload_npages", p_e_preload_npages);
342 fprintf(f,
"%s%-*s = %" PRIu64
"\n", p, w,
"e_nonresnametab_offset", p_e_nonresnametab_offset);
343 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_nonresnametab_size", p_e_nonresnametab_size);
344 fprintf(f,
"%s%-*s = 0x%08x\n", p, w,
"e_nonresnametab_cksum", p_e_nonresnametab_cksum);
345 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_auto_ds_section", p_e_auto_ds_section);
346 fprintf(f,
"%s%-*s = %" PRIu64
" (%" PRIu64
" abs)\n", p, w,
"e_debug_info_rfo",
347 p_e_debug_info_rfo, p_e_debug_info_rfo+p_offset);
348 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_debug_info_size", p_e_debug_info_size);
349 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_num_instance_preload", p_e_num_instance_preload);
350 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_num_instance_demand", p_e_num_instance_demand);
351 fprintf(f,
"%s%-*s = %u\n", p, w,
"e_heap_size", p_e_heap_size);
354 fprintf(f,
"%s%-*s = [%d] \"%s\"\n", p, w,
"dos2_header",
357 fprintf(f,
"%s%-*s = none\n", p, w,
"dos2_header");
359 if (p_section_table) {
360 fprintf(f,
"%s%-*s = [%d] \"%s\"\n", p, w,
"section_table",
363 fprintf(f,
"%s%-*s = none\n", p, w,
"section_table");
366 fprintf(f,
"%s%-*s = [%d] \"%s\"\n", p, w,
"page_table",
369 fprintf(f,
"%s%-*s = none\n", p, w,
"page_table");
371 if (p_resname_table) {
372 fprintf(f,
"%s%-*s = [%d] \"%s\"\n", p, w,
"resname_table",
375 fprintf(f,
"%s%-*s = none\n", p, w,
"resname_table");
377 if (p_nonresname_table) {
378 fprintf(f,
"%s%-*s = [%d] \"%s\"\n", p, w,
"nonresname_table",
381 fprintf(f,
"%s%-*s = none\n", p, w,
"nonresname_table");
384 fprintf(f,
"%s%-*s = [%d] \"%s\"\n", p, w,
"entry_table",
387 fprintf(f,
"%s%-*s = none\n", p, w,
"entry_table");
390 fprintf(f,
"%s%-*s = [%d] \"%s\"\n", p, w,
"reloc_table",
393 fprintf(f,
"%s%-*s = none\n", p, w,
"reloc_table");
405 unsigned pageno_lo = ByteOrder::disk_to_host(sex, disk->pageno_lo);
406 unsigned pageno_hi = ByteOrder::disk_to_host(sex, disk->pageno_hi);
407 p_pageno = (pageno_hi << 8) | pageno_lo;
408 p_flags = ByteOrder::disk_to_host(sex, disk->flags);
415 host_to_disk(sex, (p_pageno & 0xff), &(disk->pageno_lo));
416 host_to_disk(sex, (p_pageno>>8)&0xffff, &(disk->pageno_hi));
417 host_to_disk(sex, p_flags, &(disk->flags));
423 SgAsmLEPageTableEntry::dump(FILE *f,
const char *prefix, ssize_t idx)
const
427 sprintf(p,
"%sPageTableEntry[%zd].", prefix, idx);
429 sprintf(p,
"%sPageTableEntry.", prefix);
432 int w = std::max(1, DUMP_FIELD_WIDTH-(
int)strlen(p));
434 fprintf(f,
"%s%-*s = 0x%04x\n", p, w,
"flags", p_flags);
435 fprintf(f,
"%s%-*s = %u\n", p, w,
"pageno", p_pageno);
440 SgAsmLEPageTable::ctor(rose_addr_t offset, rose_addr_t size)
447 ROSE_ASSERT(fhdr!=NULL);
449 char section_name[64];
450 sprintf(section_name,
"%s Page Table", fhdr->format_name());
456 for (rose_addr_t entry_offset=0; entry_offset+entry_size <=
get_size(); entry_offset+=entry_size) {
465 SgAsmLEPageTable::get_page(
size_t idx)
467 ROSE_ASSERT(idx > 0);
468 ROSE_ASSERT(idx <= p_entries.size());
469 return p_entries[idx-1];
477 for (
size_t i=0; i < p_entries.size(); i++) {
479 p_entries[i]->encode(
get_header()->get_sex(), &disk);
480 spos =
write(f, spos,
sizeof disk, &disk);
490 sprintf(p,
"%s%sPageTable[%zd].", prefix,
get_header()->format_name(), idx);
492 sprintf(p,
"%s%sPageTable.", prefix,
get_header()->format_name());
496 for (
size_t i = 0; i < p_entries.size(); i++) {
497 p_entries[i]->dump(f, p, i);
507 SgAsmLESectionTableEntry::ctor(ByteOrder::Endianness sex,
const LESectionTableEntry_disk *disk)
509 p_mapped_size = ByteOrder::disk_to_host(sex, disk->mapped_size);
510 p_base_addr = ByteOrder::disk_to_host(sex, disk->base_addr);
511 p_flags = ByteOrder::disk_to_host(sex, disk->flags);
512 p_pagemap_index = ByteOrder::disk_to_host(sex, disk->pagemap_index);
513 p_pagemap_nentries = ByteOrder::disk_to_host(sex, disk->pagemap_nentries);
514 p_res1 = ByteOrder::disk_to_host(sex, disk->res1);
519 SgAsmLESectionTableEntry::encode(ByteOrder::Endianness sex, LESectionTableEntry_disk *disk)
const
521 ByteOrder::host_to_disk(sex, p_mapped_size, &(disk->mapped_size));
522 ByteOrder::host_to_disk(sex, p_base_addr, &(disk->base_addr));
523 ByteOrder::host_to_disk(sex, p_flags, &(disk->flags));
524 ByteOrder::host_to_disk(sex, p_pagemap_index, &(disk->pagemap_index));
525 ByteOrder::host_to_disk(sex, p_pagemap_nentries, &(disk->pagemap_nentries));
526 ByteOrder::host_to_disk(sex, p_res1, &(disk->res1));
532 SgAsmLESectionTableEntry::dump(FILE *f,
const char *prefix, ssize_t idx)
const
536 sprintf(p,
"%sLESectionTableEntry[%zd].", prefix, idx);
538 sprintf(p,
"%sLESectionTableEntry.", prefix);
541 const int w = std::max(1, DUMP_FIELD_WIDTH-(
int)strlen(p));
543 fprintf(f,
"%s%-*s = %" PRIu64
" bytes\n", p, w,
"mapped_size", p_mapped_size);
544 fprintf(f,
"%s%-*s = 0x%08" PRIx64
"\n", p, w,
"base_addr", p_base_addr);
546 fprintf(f,
"%s%-*s = 0x%08x", p, w,
"flags", p_flags);
547 switch (p_flags & SF_TYPE_MASK) {
548 case SF_TYPE_NORMAL: fputs(
" normal", f);
break;
552 default: fprintf(f,
"type=%u", p_flags & SF_TYPE_MASK);
break;
560 if (p_flags &
SF_SHARED) fputs(
" shared", f);
565 if (p_flags &
SF_BIG_BIT) fputs(
" big-bit", f);
567 if (p_flags &
SF_IO_PRIV) fputs(
" io-priv", f);
570 fprintf(f,
"%s%-*s = %u\n", p, w,
"pagemap_index", p_pagemap_index);
571 fprintf(f,
"%s%-*s = %u entries\n", p, w,
"pagemap_nentries", p_pagemap_nentries);
572 fprintf(f,
"%s%-*s = 0x%08x\n", p, w,
"res1", p_res1);
583 sprintf(p,
"%s%sSection[%zd].", prefix, fhdr->format_name(), idx);
585 sprintf(p,
"%s%sSection.", prefix, fhdr->format_name());
589 p_st_entry->dump(f, p, -1);
594 SgAsmLESectionTable::ctor(rose_addr_t offset, rose_addr_t size)
601 ROSE_ASSERT(fhdr!=NULL);
604 char section_name[64];
605 sprintf(section_name,
"%s Section Table", fhdr->format_name());
612 for (
size_t i = 0; i < fhdr->get_e_secttab_nentries(); i++) {
621 rose_addr_t section_offset, section_size;
624 for (
size_t j = 1; j < entry->get_pagemap_nentries(); j++) {
626 ROSE_ASSERT(page->get_pageno()+j == p2->get_pageno());
629 rose_addr_t pageno = page->get_pageno();
630 ROSE_ASSERT(pageno>0);
632 section_offset = fhdr->get_e_data_pages_offset() + (pageno-1) * fhdr->get_e_page_size();
634 section_size = std::min(entry->get_mapped_size(), entry->get_pagemap_nentries() * fhdr->get_e_page_size());
638 section_offset = fhdr->get_e_data_pages_offset() + ((pageno-1) << fhdr->get_e_page_offset_shift());
640 section_size = std::min(entry->get_mapped_size(),
641 (rose_addr_t)(entry->get_pagemap_nentries() * (1<<fhdr->get_e_page_offset_shift())));
652 section->set_st_entry(entry);
665 unsigned section_type = entry->get_flags() & SgAsmLESectionTableEntry::SF_TYPE_MASK;
679 ROSE_ASSERT(fhdr!=NULL);
682 for (
size_t i = 0; i < sections.size(); i++) {
683 if (sections[i]->
get_id() >= 0) {
687 ROSE_ASSERT(section->
get_id()>0);
688 size_t slot = section->
get_id()-1;
692 write(f, slot*
sizeof(disk),
sizeof disk, &disk);
706 sprintf(p,
"%s%sSectionTable[%zd].", prefix,
get_header()->format_name(), idx);
708 sprintf(p,
"%s%sSectionTable.", prefix,
get_header()->format_name());
719 SgAsmLENameTable::ctor(rose_addr_t offset)
726 ROSE_ASSERT(fhdr!=NULL);
729 char section_name[64];
730 sprintf(section_name,
"%s Name Table", fhdr->format_name());
741 size_t length = byte;
742 if (0==length)
break;
745 char *buf =
new char[length];
747 p_names.push_back(std::string(buf, length));
754 p_ordinals.push_back(ByteOrder::le_to_host(u16_disk));
764 ROSE_ASSERT(p_names.size() == p_ordinals.size());
765 for (
size_t i = 0; i < p_names.size(); i++) {
767 ROSE_ASSERT(p_names[i].size() <= 0xff);
768 unsigned char len = p_names[i].size();
769 spos =
write(f, spos, len);
772 spos =
write(f, spos, p_names[i]);
775 ROSE_ASSERT(p_ordinals[i] <= 0xffff);
777 ByteOrder::host_to_le(p_ordinals[i], &ordinal_le);
778 spos =
write(f, spos,
sizeof ordinal_le, &ordinal_le);
782 write(f, spos,
'\0');
791 sprintf(p,
"%sLENameTable[%zd].", prefix, idx);
793 sprintf(p,
"%sLENameTable.", prefix);
796 const int w = std::max(1, DUMP_FIELD_WIDTH-(
int)strlen(p));
799 ROSE_ASSERT(p_names.size() == p_ordinals.size());
800 for (
size_t i = 0; i < p_names.size(); i++) {
801 fprintf(f,
"%s%-*s = [%zd] \"%s\"\n", p, w,
"names", i, escapeString(p_names[i]).c_str());
802 fprintf(f,
"%s%-*s = [%zd] %u\n", p, w,
"ordinals", i, p_ordinals[i]);
814 p_flags = ByteOrder::disk_to_host(sex, disk->flags);
815 p_objnum = ByteOrder::disk_to_host(sex, disk->objnum);
816 p_entry_type = ByteOrder::disk_to_host(sex, disk->entry_type);
817 p_entry_offset = ByteOrder::disk_to_host(sex, disk->entry_offset);
818 p_res1 = ByteOrder::disk_to_host(sex, disk->res1);
823 SgAsmLEEntryPoint::unparse(std::ostream &f, ByteOrder::Endianness sex,
const SgAsmGenericSection *section,
824 rose_addr_t spos)
const
826 if (0==(p_flags & 0x01)) {
829 ByteOrder::host_to_disk(sex, p_flags, &byte);
830 spos = section->
write(f, spos, byte);
833 LEEntryPoint_disk disk;
834 ByteOrder::host_to_disk(sex, p_flags, &(disk.flags));
835 ByteOrder::host_to_disk(sex, p_objnum, &(disk.objnum));
836 ByteOrder::host_to_disk(sex, p_entry_type, &(disk.entry_type));
837 ByteOrder::host_to_disk(sex, p_entry_offset, &(disk.entry_offset));
838 ByteOrder::host_to_disk(sex, p_res1, &(disk.res1));
839 spos = section->
write(f, spos,
sizeof disk, &disk);
846 SgAsmLEEntryPoint::dump(FILE *f,
const char *prefix, ssize_t idx)
const
850 sprintf(p,
"%sEntryPoint[%zd].", prefix, idx);
852 sprintf(p,
"%sEntryPoint.", prefix);
855 const int w = std::max(1, DUMP_FIELD_WIDTH-(
int)strlen(p));
857 fprintf(f,
"%s%-*s = 0x%02x", p, w,
"flags", p_flags);
859 fprintf(f,
" 32-bit");
860 if (p_flags & 0x01) {
861 fprintf(f,
" non-empty\n");
862 fprintf(f,
"%s%-*s = %u\n", p, w,
"objnum", p_objnum);
863 fprintf(f,
"%s%-*s = 0x%02x", p, w,
"entry_type", p_entry_type);
864 if (p_entry_type & 0x01) fputs(
" exported", f);
865 if (p_entry_type & 0x02) fputs(
" shared-data", f);
866 fprintf(f,
" stack-params=%u\n", (p_entry_type >> 3) & 0x1f);
867 fprintf(f,
"%s%-*s = %" PRIu64
"\n", p, w,
"entry_offset", p_entry_offset);
868 fprintf(f,
"%s%-*s = 0x%04x\n", p, w,
"res1", p_res1);
870 fprintf(f,
" empty\n");
877 SgAsmLEEntryTable::ctor(rose_addr_t offset)
884 ROSE_ASSERT(fhdr!=NULL);
887 char section_name[64];
888 sprintf(section_name,
"%s Entry Table", fhdr->format_name());
906 size_t nentries = byte;
907 for (
size_t i = 0; i < nentries; i++) {
927 ROSE_ASSERT(p_entries.size()<=0xff);
928 uint8_t byte = p_entries.size();
929 spos =
write(f, spos, byte);
931 ByteOrder::Endianness sex =
get_header()->get_sex();
932 for (
size_t i = 0; i < p_entries.size(); i++) {
933 spos = p_entries[i]->unparse(f, sex,
this, spos);
943 sprintf(p,
"%s%sEntryTable[%zd].", prefix,
get_header()->format_name(), idx);
945 sprintf(p,
"%s%sEntryTable.", prefix,
get_header()->format_name());
948 const int w = std::max(1, DUMP_FIELD_WIDTH-(
int)strlen(p));
951 fprintf(f,
"%s%-*s = %" PRIuPTR
" entry points\n", p, w,
"size", p_entries.size());
952 for (
size_t i = 0; i < p_entries.size(); i++) {
953 p_entries[i]->dump(f, p, i);
963 SgAsmLERelocTable::ctor(rose_addr_t offset)
970 ROSE_ASSERT(fhdr!=NULL);
973 sprintf(name,
"%s Relocation Table", fhdr->format_name());
984 rose_addr_t at = 0, reloc_size = 0;
985 for (
size_t i = 0; i < nrelocs; i++, at+=reloc_size) {
996 ROSE_ASSERT(0==reallocate(
false));
1006 sprintf(p,
"%s%sRelocTable[%zd].", prefix,
get_header()->format_name(), idx);
1008 sprintf(p,
"%s%sRelocTable.", prefix,
get_header()->format_name());
1011 const int w = std::max(1, DUMP_FIELD_WIDTH-(
int)strlen(p));
1014 fprintf(f,
"%s%-*s = %" PRIuPTR
" entries\n", p, w,
"size", p_entries.size());
1015 for (
size_t i = 0; i < p_entries.size(); i++) {
1016 p_entries[i]->dump(f, p, i);
1026 ROSE_ASSERT(dos_header);
1033 dos2_header->parse();
1037 le_header->set_dos2_header(dos2_header);
1040 if (le_header->get_e_pagetab_rfo() > 0 && le_header->get_e_npages() > 0) {
1041 rose_addr_t table_offset = le_header->
get_offset() + le_header->get_e_pagetab_rfo();
1044 le_header->set_page_table(table);
1048 if (le_header->get_e_secttab_rfo() > 0 && le_header->get_e_secttab_nentries() > 0) {
1049 rose_addr_t table_offset = le_header->
get_offset() + le_header->get_e_secttab_rfo();
1052 le_header->set_section_table(table);
1056 if (le_header->get_e_rsrctab_rfo() > 0 && le_header->get_e_rsrctab_nentries() > 0) {
1061 if (le_header->get_e_resnametab_rfo() > 0) {
1062 rose_addr_t table_offset = le_header->
get_offset() + le_header->get_e_resnametab_rfo();
1064 char section_name[64];
1065 sprintf(section_name,
"%s Resident Name Table", le_header->format_name());
1067 le_header->set_resname_table(table);
1071 if (le_header->get_e_nonresnametab_offset() > 0) {
1072 rose_addr_t table_offset = le_header->get_e_nonresnametab_offset();
1074 char section_name[64];
1075 sprintf(section_name,
"%s Non-resident Name Table", le_header->format_name());
1077 le_header->set_nonresname_table(table);
1081 if (le_header->get_e_entrytab_rfo() > 0) {
1082 rose_addr_t table_offset = le_header->
get_offset() + le_header->get_e_entrytab_rfo();
1084 le_header->set_entry_table(table);
1088 if (le_header->get_e_fixup_rectab_rfo() > 0) {
1089 rose_addr_t table_offset = le_header->
get_offset() + le_header->get_e_fixup_rectab_rfo();
1091 le_header->set_reloc_table(table);
Section has preload pages.
SgAsmGenericFile * get_file() const
Property: File to which this section belongs.
16:16 alias required (80x86 specific)
String associated with a binary file.
size_t read_content(rose_addr_t offset, void *dst_buf, rose_addr_t size, bool strict=true)
Reads data from a file.
Contiguous region of a file.
void set_mapped_xperm(bool)
Property: Whether mapped with execute permission.
virtual void unparse(std::ostream &) const
Write a section back to the file.
Section is conforming for code (80x86 specific)
virtual void dump(FILE *, const char *prefix, ssize_t idx) const ROSE_OVERRIDE
Print some debugging info.
size_t read_content_local(rose_addr_t rel_offset, void *dst_buf, rose_addr_t size, bool strict=true)
Reads data from a file.
void set_parent(SgNode *parent)
All nodes in the AST contain a reference to a parent node.
Section has zero-filled pages.
const SgAsmGenericSectionPtrList & get_sections() const
Property: List of section pointers.
void set_mapped_actual_va(rose_addr_t)
Property: Virtual address where ROSE maps this section.
rose_addr_t get_offset() const
Property: Offset to start of section in file.
virtual void set_mapped_preferred_rva(rose_addr_t)
Property: Relative virtual address where section prefers to be mapped.
Big/default bit setting (80x86 specific); see note above.
SgAsmGenericHeader * get_header() const
Property: File header that owns this section.
rose_addr_t write(std::ostream &f, rose_addr_t offset, size_t bufsize, const void *buf) const
Write data to a file section.
void set_mapped_rperm(bool)
Property: Whether mapped with read permission.
virtual void unparse(std::ostream &) const ROSE_OVERRIDE
Write a section back to the file.
Section contains resource objects.
Section is resident and contiguous.
Program-supplied data, code, etc.
Section I/O privilege level (80x86 specific; used only for 16:16 alias objects)
virtual void unparse(std::ostream &) const ROSE_OVERRIDE
Write a section back to the file.
virtual void set_offset(rose_addr_t)
Property: Offset to start of section in file.
virtual void dump(FILE *, const char *prefix, ssize_t idx) const
Print some debugging info.
void set_synthesized(bool)
Property: Whether section really exists.
virtual void dump(FILE *, const char *prefix, ssize_t idx) const ROSE_OVERRIDE
Print some debugging info.
Section is resident and "long-lockable" (VDDs and PDDs only)
rose_addr_t get_size() const
Property: Size of section in file in bytes.
Write permission granted when mapped.
void grab_content()
Saves a reference to the original file data for a section based on the section's current offset and s...
void set_mapped_wperm(bool)
Property: Whether mapped with write permission.
virtual void set_size(rose_addr_t)
Property: Size of section in file in bytes.
Execute permission granted when mapped.
virtual void dump(FILE *, const char *prefix, ssize_t idx) const ROSE_OVERRIDE
Print some debugging info.
void set_purpose(SectionPurpose)
Property: General contents of the section.
void set_id(int)
Property: Non-unique section ID or negative.
virtual void dump(FILE *, const char *prefix, ssize_t idx) const ROSE_OVERRIDE
Print some debugging info.
Section contains a header for the executable file format.
Read permission granted when mapped.
virtual void set_mapped_size(rose_addr_t)
Property: Mapped size.
virtual std::string get_string(bool escape=false) const
Property: String value.
SgAsmGenericString * get_name() const
Property: Non-unique name of section.
void extend(rose_addr_t nbytes)
Extend a section by some number of bytes during the construction and/or parsing phase.
Section is resident (valid for VDDs and PDDs only)
virtual void dump(FILE *, const char *prefix, ssize_t idx) const ROSE_OVERRIDE
Print some debugging info.
int get_id() const
Property: Non-unique section ID or negative.
void set_name(SgAsmGenericString *s)
Property: Non-unique name of section.
Base class for binary files.
Section has invalid pages.
virtual void unparse(std::ostream &) const ROSE_OVERRIDE
Write a section back to the file.