@@ -348,13 +348,29 @@ static void generate_copy_with_dbg_info(std::shared_ptr<DwarfGenInfo> info,
348348
349349 add_debug_section_data (info);
350350
351- // Fix the section header start location
351+ // Get the current last section (to fix section header and string table loc)
352352 auto last_scn = get_last_section (elf_out);
353353 GElf_Shdr last_shdr;
354354 if (gelf_getshdr (last_scn, &last_shdr) != &last_shdr)
355355 dwarfexport_error (" gelf_getshdr() failed: " , elf_errmsg (-1 ));
356356
357- ehdr_out.e_shoff = last_shdr.sh_offset + last_shdr.sh_size ;
357+ // Fix the section header string table start location
358+ std::size_t shstrndx = 0 ;
359+ if (elf_getshdrstrndx (elf_out, &shstrndx) == -1 )
360+ dwarfexport_error (" elf_getshdrstrndx() failed: " , elf_errmsg (-1 ));
361+
362+ Elf_Scn *shstr_scn = elf_getscn (elf_out, shstrndx);
363+ GElf_Shdr shstr_shdr;
364+ if (!gelf_getshdr (shstr_scn, &shstr_shdr))
365+ dwarfexport_error (" elf_getshdr() failed: " , elf_errmsg (-1 ));
366+
367+ shstr_shdr.sh_offset = last_shdr.sh_offset + last_shdr.sh_size ;
368+
369+ if (!gelf_update_shdr (shstr_scn, &shstr_shdr))
370+ dwarfexport_error (" gelf_update_shdr() failed: " , elf_errmsg (-1 ));
371+
372+ // Fix the section header start location
373+ ehdr_out.e_shoff = shstr_shdr.sh_offset + shstr_shdr.sh_size ;
358374 if (gelf_update_ehdr (elf_out, &ehdr_out) == 0 )
359375 dwarfexport_error (" gelf_update_ehdr() failed: " , elf_errmsg (-1 ));
360376
0 commit comments