summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Wielaard <mark@klomp.org>2018-11-13 21:18:09 +0100
committerMark Wielaard <mark@klomp.org>2018-11-13 21:30:17 +0100
commitcf10453f8252df81225796d98548ba6eac113df3 (patch)
tree13cea388f62135b702fe1db133dd842e1bf2873a
parentd3e6266754b95244063aa1e40c531fdd57259332 (diff)
libelf: Correctly setup alignment of SHF_COMPRESSED section data.
We didn't set the alignment of SHF_COMPRESSED sections correctly. Those sections start with an Elf(32|64)_Chdr. Make sure sh_addralign is setup to be able to read such a struct directly. Likewise don't trust the alignment set on any SHF_COMPRESSED section, but always make the (raw) compressed data aligned correctly for the reading the Elf(32|64)_Chdr directly. Signed-off-by: Mark Wielaard <mark@klomp.org>
-rw-r--r--libelf/ChangeLog7
-rw-r--r--libelf/elf_compress.c4
-rw-r--r--libelf/elf_getdata.c10
3 files changed, 17 insertions, 4 deletions
diff --git a/libelf/ChangeLog b/libelf/ChangeLog
index ab078cb5..93820d19 100644
--- a/libelf/ChangeLog
+++ b/libelf/ChangeLog
@@ -1,3 +1,10 @@
+2018-11-13 Mark Wielaard <mark@klomp.org>
+
+ * elf_getdata.c (__libelf_set_rawdata_wrlock): Explicitly set the
+ alignment of SHF_COMPRESSED data to the alignment of ELF_T_CHDR.
+ * elf_compress.c (elf_compress): After compression set sh_addralign
+ to the alignment of ELF_T_CHDR.
+
2018-11-09 Mark Wielaard <mark@klomp.org>
* elf_compress_gnu.c (elf_compress_gnu): Use elf_getdata.
diff --git a/libelf/elf_compress.c b/libelf/elf_compress.c
index d96245df..be9eeaba 100644
--- a/libelf/elf_compress.c
+++ b/libelf/elf_compress.c
@@ -455,14 +455,14 @@ elf_compress (Elf_Scn *scn, int type, unsigned int flags)
{
Elf32_Shdr *shdr = elf32_getshdr (scn);
shdr->sh_size = new_size;
- shdr->sh_addralign = 1;
+ shdr->sh_addralign = __libelf_type_align (ELFCLASS32, ELF_T_CHDR);
shdr->sh_flags |= SHF_COMPRESSED;
}
else
{
Elf64_Shdr *shdr = elf64_getshdr (scn);
shdr->sh_size = new_size;
- shdr->sh_addralign = 1;
+ shdr->sh_addralign = __libelf_type_align (ELFCLASS64, ELF_T_CHDR);
shdr->sh_flags |= SHF_COMPRESSED;
}
diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c
index 2043bba2..639a798e 100644
--- a/libelf/elf_getdata.c
+++ b/libelf/elf_getdata.c
@@ -268,9 +268,15 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
/* First a test whether the section is valid at all. */
size_t entsize;
- /* Compressed data has a header, but then compressed data. */
+ /* Compressed data has a header, but then compressed data.
+ Make sure to set the alignment of the header explicitly,
+ don't trust the file alignment for the section, it is
+ often wrong. */
if ((flags & SHF_COMPRESSED) != 0)
- entsize = 1;
+ {
+ entsize = 1;
+ align = __libelf_type_align (elf->class, ELF_T_CHDR);
+ }
else if (type == SHT_HASH)
{
GElf_Ehdr ehdr_mem;