fix ELF compressed header and skip duplicate sections

This commit is contained in:
Nikita Smith
2025-08-12 14:10:13 -07:00
parent b50f309422
commit 086d774b8b
2 changed files with 47 additions and 49 deletions
+45 -48
View File
@@ -32,7 +32,6 @@ dw_input_from_elf_bin(Arena *arena, String8 data, ELF_Bin *bin)
{ {
DW_Input result = {0}; DW_Input result = {0};
B32 is_section_present[ArrayCount(result.sec)] = {0}; B32 is_section_present[ArrayCount(result.sec)] = {0};
Temp scratch = scratch_begin(&arena, 1);
for(U64 section_idx = 1; section_idx < bin->shdrs.count; section_idx += 1) for(U64 section_idx = 1; section_idx < bin->shdrs.count; section_idx += 1)
{ {
ELF_Shdr64 *shdr = &bin->shdrs.v[section_idx]; ELF_Shdr64 *shdr = &bin->shdrs.v[section_idx];
@@ -49,69 +48,67 @@ dw_input_from_elf_bin(Arena *arena, String8 data, ELF_Bin *bin)
is_dwo = (section_kind != DW_Section_Null); is_dwo = (section_kind != DW_Section_Null);
} }
if(section_kind == DW_Section_Null) { continue; } // skip unknown sections
if(is_section_present[section_kind]) { continue; } // skip duplicate sections
//- rjf: decompress section data if needed //- rjf: decompress section data if needed
String8 section_data__uncompressed = {0}; String8 section_data__uncompressed = {0};
if(section_kind != DW_Section_Null) if(!(shdr->sh_flags & ELF_Shf_Compressed))
{ {
if(!(shdr->sh_flags & ELF_Shf_Compressed)) section_data__uncompressed = section_data__maybe_compressed;
}
else
{
// rjf: read compressed-section header
ELF_Chdr64 chdr64 = {0};
U64 chdr_size = 0;
if(ELF_HdrIs64Bit(bin->hdr.e_ident))
{ {
section_data__uncompressed = section_data__maybe_compressed; chdr_size = str8_deserial_read_struct(section_data__maybe_compressed, 0, &chdr64);
} }
else else if(ELF_HdrIs32Bit(bin->hdr.e_ident))
{ {
// rjf: read compressed-section header ELF_Chdr32 chdr32 = {0};
ELF_Chdr64 chdr64 = {0}; chdr_size = str8_deserial_read_struct(section_data__maybe_compressed, 0, &chdr32);
U64 chdr_size = 0; if(chdr_size == sizeof(chdr32))
if(ELF_HdrIs64Bit(bin->hdr.e_ident))
{ {
chdr_size = str8_deserial_read_struct(section_data__maybe_compressed, 0, &chdr64); chdr64 = elf_chdr64_from_chdr32(chdr32);
} }
else if(ELF_HdrIs32Bit(bin->hdr.e_ident)) }
// rjf: decompress
{
String8 section_data__compressed_contents = str8_skip(section_data__maybe_compressed, chdr_size);
switch(chdr64.ch_type)
{ {
ELF_Chdr32 chdr32 = {0}; case ELF_CompressType_None:
chdr_size = str8_deserial_read_struct(section_data__maybe_compressed, 0, &chdr32);
if(chdr_size == sizeof(chdr32))
{ {
chdr64 = elf_chdr64_from_chdr32(chdr32); section_data__uncompressed = section_data__compressed_contents;
} }break;
} case ELF_CompressType_ZLib:
// rjf: decompress
{
String8 section_data__compressed_contents = str8_skip(section_data__maybe_compressed, chdr_size);
switch(chdr64.ch_type)
{ {
default: U8 *section_data_uncompressed_buffer = push_array_no_zero_aligned(arena, U8, chdr64.ch_size, chdr64.ch_addr_align);
case ELF_CompressType_None: U64 section_data_uncompressed_size = zsinflate(section_data_uncompressed_buffer, chdr64.ch_size, section_data__compressed_contents.str, section_data__compressed_contents.size);
{ section_data__uncompressed = str8(section_data_uncompressed_buffer, section_data_uncompressed_size);
section_data__uncompressed = section_data__compressed_contents; }break;
}break; case ELF_CompressType_ZStd:
case ELF_CompressType_ZLib: {
{ NotImplemented;
U8 *section_data_uncompressed_buffer = push_array_no_zero_aligned(arena, U8, chdr64.ch_size, chdr64.ch_addr_align); }break;
U64 section_data_uncompressed_size = 0; default:
section_data_uncompressed_size = zsinflate(section_data_uncompressed_buffer, chdr64.ch_size, section_data__compressed_contents.str, section_data__compressed_contents.size); {
section_data__uncompressed = str8(section_data_uncompressed_buffer, section_data_uncompressed_size); NotImplemented;
}break; }break;
case ELF_CompressType_ZStd:
{
NotImplemented;
}break;
}
} }
} }
} }
//- rjf: store //- rjf: store
if(section_kind != DW_Section_Null) is_section_present[section_kind] = 1;
{ DW_Section *d = &result.sec[section_kind];
is_section_present[section_kind] = 1; d->name = push_str8_copy(arena, section_name);
DW_Section *d = &result.sec[section_kind]; d->data = section_data__uncompressed;
d->name = push_str8_copy(arena, section_name); d->is_dwo = is_dwo;
d->data = section_data__uncompressed;
d->is_dwo = is_dwo;
}
} }
scratch_end(scratch);
return result; return result;
} }
+2 -1
View File
@@ -971,7 +971,8 @@ typedef struct ELF_Chdr32
typedef struct ELF_Chdr64 typedef struct ELF_Chdr64
{ {
U64 ch_type; U32 ch_type;
U32 ch_reserved;
U64 ch_size; U64 ch_size;
U64 ch_addr_align; U64 ch_addr_align;
} ELF_Chdr64; } ELF_Chdr64;