libelf: Fix unaligned d_off offsets for input sections with large alignments
authorAndrei Homescu <ah@immunant.com>
Tue, 29 Jun 2021 01:26:53 +0000 (18:26 -0700)
committerMark Wielaard <mark@klomp.org>
Thu, 8 Jul 2021 09:31:22 +0000 (11:31 +0200)
commitb3601167d7a4c9f34eb65c3892c9ef25e3c1c66f
tree4d5651a699e00edfca95b5e6c9666071500f056b
parent6648d378bd94f50b455a8550db5f43ef3690b451
libelf: Fix unaligned d_off offsets for input sections with large alignments

The mkl_memory_patched.o object inside the libmkl_core.a library from
the Intel Math Kernel Library version 2018.2.199 has this section
with an alignment of 4096 and offset of 0xb68:
 [ 2] .data PROGBITS 0000000000000000 000b68 011000 00 WA 0 0 4096

Reading this file with libelf and trying to write it back to disk triggers
the following sequence of events:
1) code in elf_getdata.c clamps d_align for this section's data buffer
   to the section's offset
2) code in elf32_updatenull.c checks if the alignment is a power of two
   and incorrectly returns an error

This commit fixes this corner case by increasing the alignment to the
next power of two after the clamping, so the check passes.

A test that reproduces this bug using strip is also included.

Signed-off-by: Andrei Homescu <ah@immunant.com>
libelf/ChangeLog
libelf/elf_getdata.c
tests/ChangeLog
tests/Makefile.am
tests/run-strip-largealign.sh [new file with mode: 0755]
tests/testfile-largealign.o.bz2 [new file with mode: 0644]