@@ -13,6 +13,7 @@ pub use goblin_v023::elf::section_header::SHF_EXECINSTR;
1313
1414// GNU property note constants for RISC-V CFI features
1515// See: https://github.com/llvm/llvm-project/blob/c5aaee0bb07b221e5d3314bbdcf1abc4a604d6bd/llvm/include/llvm/BinaryFormat/ELF.h#L1809
16+ #[ allow( dead_code) ]
1617const NT_GNU_PROPERTY_TYPE_0 : u32 = 5 ;
1718// See: https://github.com/llvm/llvm-project/blob/c5aaee0bb07b221e5d3314bbdcf1abc4a604d6bd/llvm/include/llvm/BinaryFormat/ELF.h#L1845
1819const GNU_PROPERTY_RISCV_FEATURE_1_AND : u32 = 0xC000_0000 ;
@@ -371,37 +372,13 @@ pub fn parse_elf<R: Register>(program: &Bytes, version: u32) -> Result<ProgramMe
371372 ) ) ;
372373 }
373374 let note_data = & program[ note_start..note_end] ;
374- // Parse note header: namesz (4), descsz (4), type (4), name, desc
375- let mut offset = 0 ;
376- let mut buf = [ 0u8 ; 4 ] ;
377- while offset + 12 <= note_data. len ( ) {
378- buf. copy_from_slice ( & note_data[ offset..offset + 4 ] ) ;
379- let namesz = u32:: from_le_bytes ( buf) as usize ;
380- if namesz > note_data. len ( ) {
381- return Err ( Error :: ElfParseError ( "Invalid namesz" . into ( ) ) ) ;
375+ // Parse note header: namesz(4), descsz(4), type(4), name
376+ // 4u32 16u32 NT_GNU_PROPERTY_TYPE_0 GNU\0
377+ let expect_note_header: [ u8 ; 16 ] = [ 4 , 0 , 0 , 0 , 16 , 0 , 0 , 0 , 5 , 0 , 0 , 0 , 71 , 78 , 85 , 0 ] ;
378+ if note_data. len ( ) == 32 && note_data[ ..16 ] == expect_note_header {
379+ if let Ok ( icfi) = parse_gnu_property_note ( & note_data[ 16 ..] ) {
380+ cfi = icfi;
382381 }
383- buf. copy_from_slice ( & note_data[ offset + 4 ..offset + 8 ] ) ;
384- let descsz = u32:: from_le_bytes ( buf) as usize ;
385- if descsz > note_data. len ( ) {
386- return Err ( Error :: ElfParseError ( "Invalid descsz" . into ( ) ) ) ;
387- }
388- buf. copy_from_slice ( & note_data[ offset + 8 ..offset + 12 ] ) ;
389- let note_type = u32:: from_le_bytes ( buf) ;
390- offset += 12 ;
391- // Align namesz to 4 bytes.
392- let aligned_namesz = ( namesz + 3 ) & !3 ;
393- if note_type == NT_GNU_PROPERTY_TYPE_0 {
394- let desc_offset = offset + aligned_namesz;
395- if desc_offset + descsz <= note_data. len ( ) {
396- let desc_data = & note_data[ desc_offset..desc_offset + descsz] ;
397- if let Ok ( icfi) = parse_gnu_property_note ( desc_data) {
398- cfi = icfi;
399- }
400- }
401- }
402- // Move to next note (align descsz to 4 bytes)
403- let aligned_descsz = ( descsz + 3 ) & !3 ;
404- offset += aligned_namesz + aligned_descsz;
405382 }
406383 break ;
407384 }
0 commit comments