@@ -307,11 +307,12 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport]
307307 stub. reserve_section_headers ( ) ;
308308 stub. reserve_dynsym ( ) ;
309309 stub. reserve_dynstr ( ) ;
310+ let verdef_count = 1 + vers. len ( ) ;
310311 if !vers. is_empty ( ) {
311312 stub. reserve_gnu_versym ( ) ;
312- stub. reserve_gnu_verdef ( 1 + vers . len ( ) , 1 + vers . len ( ) ) ;
313+ stub. reserve_gnu_verdef ( verdef_count , verdef_count ) ;
313314 }
314- stub. reserve_dynamic ( 2 ) ; // DT_SONAME, DT_NULL
315+ stub. reserve_dynamic ( 3 ) ; // DT_SONAME, DT_VERDEFNUM , DT_NULL
315316
316317 // First write the ELF header with the arch information.
317318 let e_machine = match ( arch, sub_arch) {
@@ -443,9 +444,13 @@ fn create_elf_raw_dylib_stub(sess: &Session, soname: &str, symbols: &[DllImport]
443444 // .dynamic
444445 // the DT_SONAME will be used by the linker to populate DT_NEEDED
445446 // which the loader uses to find the library.
446- // DT_NULL terminates the .dynamic table.
447447 stub. write_align_dynamic ( ) ;
448448 stub. write_dynamic_string ( elf:: DT_SONAME , soname) ;
449+ // LSB section "2.7. Symbol Versioning" requires `DT_VERDEFNUM` to be reliable.
450+ if verdef_count > 1 {
451+ stub. write_dynamic ( elf:: DT_VERDEFNUM , verdef_count as u64 ) ;
452+ }
453+ // DT_NULL terminates the .dynamic table.
449454 stub. write_dynamic ( elf:: DT_NULL , 0 ) ;
450455
451456 stub_buf
0 commit comments