69 lines
1.9 KiB
C
69 lines
1.9 KiB
C
////////////////////////////////
|
|
// PE Get Section Function
|
|
|
|
static RangePtr
|
|
pe_img_get_section(void *image_raw, String8 name){
|
|
U8 *image = (U8*)image_raw;
|
|
|
|
// dos header
|
|
PE_DosHeader *dos_header = (PE_DosHeader*)(image);
|
|
if (dos_header->magic != PE_MSDOS_MAGIC){
|
|
dos_header = 0;
|
|
}
|
|
|
|
// coff header
|
|
PE_CoffHeader *coff_header = 0;
|
|
U64 after_coff_header_offset = 0;
|
|
if (dos_header != 0){
|
|
U64 pe_signature_offset = dos_header->coff_file_offset;
|
|
U64 coff_header_offset = pe_signature_offset + sizeof(U32);
|
|
U32 pe_signature = *(U32*)(image + pe_signature_offset);
|
|
if (pe_signature == PE_SIGNATURE){
|
|
coff_header = (PE_CoffHeader*)(image + coff_header_offset);
|
|
after_coff_header_offset = coff_header_offset + sizeof(*coff_header);
|
|
}
|
|
}
|
|
|
|
// section table offset & count
|
|
U32 section_table_offset = 0;
|
|
U32 section_count = 0;
|
|
if (coff_header != 0){
|
|
section_table_offset = ( after_coff_header_offset
|
|
+ coff_header->optional_header_size);
|
|
section_count = coff_header->section_count;
|
|
}
|
|
|
|
// scan section table
|
|
PE_SectionHeader *match = 0;
|
|
{
|
|
PE_SectionHeader *sec = (PE_SectionHeader*)(image + section_table_offset);
|
|
for (U32 i = 0; i < section_count; i += 1, sec += 1){
|
|
U8 *secptr = sec->name;
|
|
U8 *secopl = sec->name + sizeof(sec->name);
|
|
U8 *keyptr = name.str;
|
|
U8 *keyopl = name.str + name.size;
|
|
B32 string_match = 1;
|
|
for (;*secptr != 0 && secptr < secopl && keyptr < keyopl;
|
|
secptr += 1, keyptr += 1){
|
|
if (*secptr != *keyptr){
|
|
string_match = 0;
|
|
break;
|
|
}
|
|
}
|
|
if (string_match){
|
|
match = sec;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// fill result
|
|
RangePtr result = {0};
|
|
if (match != 0){
|
|
result.first = image + match->voff;
|
|
result.opl = result.first + match->vsize;
|
|
}
|
|
|
|
return(result);
|
|
}
|