From 190c04ba36d9c6c3dce41f12012aa97c6d7f22f5 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Mon, 5 Oct 2020 18:03:08 +0200 Subject: [PATCH] lto: fix LTO debug sections copying. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit readelf -S prints: There are 81999 section headers, starting at offset 0x1f488060: Section Headers: [Nr] Name Type Address Off Size ES Flg Lk Inf Al [ 0] NULL 0000000000000000 000000 01404f 00 81998 0 0 [ 1] .group GROUP 0000000000000000 000040 000008 04 81995 105027 4 ... [81995] .symtab SYMTAB 0000000000000000 d5d9298 2db310 18 81997 105026 8 [81996] .symtab_shndx SYMTAB SECTION INDICES 0000000000000000 d8b45a8 079dd8 04 81995 0 4 [81997] .strtab STRTAB 0000000000000000 d92e380 80460c 00 0 0 1 ... Looking at the documentation: Table 7–15 ELF sh_link and sh_info Interpretation sh_type - sh_link SHT_SYMTAB - The section header index of the associated string table. SHT_SYMTAB_SHNDX - The section header index of the associated symbol table. As seen, sh_link of a SHT_SYMTAB always points to a .strtab and readelf confirms that. So we need to use reverse mapping taken from [81996] .symtab_shndx SYMTAB SECTION INDICES 0000000000000000 d8b45a8 079dd8 04 81995 0 4 where sh_link points to 81995. libiberty/ChangeLog: PR lto/97290 * simple-object-elf.c (simple_object_elf_copy_lto_debug_sections): Use sh_link of a .symtab_shndx section. --- libiberty/simple-object-elf.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libiberty/simple-object-elf.c b/libiberty/simple-object-elf.c index 7c9d492..37e7334 100644 --- a/libiberty/simple-object-elf.c +++ b/libiberty/simple-object-elf.c @@ -1191,7 +1191,7 @@ simple_object_elf_copy_lto_debug_sections (simple_object_read *sobj, unsigned int sh_link; sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, shdr, sh_link, Elf_Word); - symtab_indices_shndx[sh_link - 1] = i; + symtab_indices_shndx[sh_link - 1] = i - 1; /* Always discard the extended index sections, after copying it will not be needed. This way we don't need to update it and deal with the ordering constraints of @@ -1372,19 +1372,22 @@ simple_object_elf_copy_lto_debug_sections (simple_object_read *sobj, { unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, shdr, sh_entsize, Elf_Addr); - unsigned strtab = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, - shdr, sh_link, Elf_Word); size_t prevailing_name_idx = 0; unsigned char *ent; unsigned *shndx_table = NULL; /* Read the section index table if present. */ if (symtab_indices_shndx[i - 1] != 0) { - unsigned char *sidxhdr = shdrs + (strtab - 1) * shdr_size; + unsigned char *sidxhdr = shdrs + symtab_indices_shndx[i - 1] * shdr_size; off_t sidxoff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, sidxhdr, sh_offset, Elf_Addr); size_t sidxsz = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, sidxhdr, sh_size, Elf_Addr); + unsigned int shndx_type + = ELF_FETCH_FIELD (type_functions, ei_class, Shdr, + sidxhdr, sh_type, Elf_Word); + if (shndx_type != SHT_SYMTAB_SHNDX) + return "Wrong section type of a SYMTAB SECTION INDICES section"; shndx_table = (unsigned *)XNEWVEC (char, sidxsz); simple_object_internal_read (sobj->descriptor, sobj->offset + sidxoff, -- 2.7.4