GCC with -flto detects some (very) unlikely issues in error paths.
In theory getting the Ehdr from an Elf handle might fail. But in
most cases where __libelf_data_type is used, we already have the
Ehdr anyway. So simply pass that to __libelf_data_type. In the one
place where we don't have it yet, get it and check for failure
before calling __libelf_data_type.
Signed-off-by: Mark Wielaard <mark@klomp.org>
+2020-11-01 Mark Wielaard <mark@klomp.org>
+
+ * libelfP.h (__libelf_data_type): Take an GElf_Ehdr instead of an
+ Elf handle.
+ * elf_getdata.c (__libelf_data_type): Likewise. And check ehdr
+ directly instead of fetching a new one.
+ (__libelf_set_rawdata_wrlock): Fetch Ehdr, report an error when that
+ fails, otherwise call __libelf_data_type.
+
2020-10-28 Mark Wielaard <mark@klomp.org>
* elf.h: Update from glibc.
__libelf_reset_rawdata (scn, scn->zdata_base,
scn->zdata_size, scn->zdata_align,
- __libelf_data_type (elf, sh_type,
+ __libelf_data_type (&ehdr, sh_type,
scn->zdata_align));
return 1;
}
__libelf_reset_rawdata (scn, buf_out, size, sh_addralign,
- __libelf_data_type (elf, sh_type, sh_addralign));
+ __libelf_data_type (&ehdr, sh_type,
+ sh_addralign));
scn->zdata_base = buf_out;
Elf_Type
internal_function
-__libelf_data_type (Elf *elf, int sh_type, GElf_Xword align)
+__libelf_data_type (GElf_Ehdr *ehdr, int sh_type, GElf_Xword align)
{
/* Some broken ELF ABI for 64-bit machines use the wrong hash table
entry size. See elf-knowledge.h for more information. */
- if (sh_type == SHT_HASH && elf->class == ELFCLASS64)
+ if (sh_type == SHT_HASH && ehdr->e_ident[EI_CLASS] == ELFCLASS64)
{
- GElf_Ehdr ehdr_mem;
- GElf_Ehdr *ehdr = __gelf_getehdr_rdlock (elf, &ehdr_mem);
return (SH_ENTSIZE_HASH (ehdr) == 4 ? ELF_T_WORD : ELF_T_XWORD);
}
else
if ((flags & SHF_COMPRESSED) != 0)
scn->rawdata.d.d_type = ELF_T_CHDR;
else
- scn->rawdata.d.d_type = __libelf_data_type (elf, type, align);
+ {
+ GElf_Ehdr ehdr_mem;
+ GElf_Ehdr *ehdr = __gelf_getehdr_rdlock (elf, &ehdr_mem);
+ if (unlikely (ehdr == NULL))
+ return 1;
+ scn->rawdata.d.d_type = __libelf_data_type (ehdr, type, align);
+ }
scn->rawdata.d.d_off = 0;
/* Make sure the alignment makes sense. d_align should be aligned both
# define __libelf_type_align(class, type) \
(__libelf_type_aligns[class - 1][type] ?: 1)
-/* Given an Elf handle and a section type returns the Elf_Data d_type.
+/* Given an GElf_Ehdr handle and a section type returns the Elf_Data d_type.
Should not be called when SHF_COMPRESSED is set, the d_type should
be ELF_T_BYTE. */
-extern Elf_Type __libelf_data_type (Elf *elf, int sh_type, GElf_Xword align)
+extern Elf_Type __libelf_data_type (GElf_Ehdr *ehdr,
+ int sh_type, GElf_Xword align)
internal_function;