File elfutils-fix-large-alignment.diff of Package elfutils.41427

Fixes a kernel compile error within objtool.

commit b3601167d7a4c9f34eb65c3892c9ef25e3c1c66f
Author: Andrei Homescu <ah@immunant.com>
Date:   Mon Jun 28 18:26:53 2021 -0700

    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>

2021-06-09  Andrei Homescu  <ah@immunant.com>

	* elf_getdata.c: Fix d_align for sections where alignment is larger
	than offset.

2021-06-09  Andrei Homescu  <ah@immunant.com>

	* testfile-largealign.o.bz2: New test file.
	* run-strip-largealign.sh: New test.
	* Makefile.am (TESTS): Add run-strip-largealign.sh.
	(EXTRA_DIST): Add run-strip-largealign.sh and
	testfile-largealign.o.bz2

diff --git a/libelf/elf_getdata.c b/libelf/elf_getdata.c
index 6ed44504..3cad29de 100644
--- a/libelf/elf_getdata.c
+++ b/libelf/elf_getdata.c
@@ -384,7 +384,18 @@ __libelf_set_rawdata_wrlock (Elf_Scn *scn)
      which should be uncommon.  */
   align = align ?: 1;
   if (type != SHT_NOBITS && align > offset)
-    align = offset;
+    {
+      /* Align the offset to the next power of two. Uses algorithm from
+         https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */
+      align = offset - 1;
+      align |= align >> 1;
+      align |= align >> 2;
+      align |= align >> 4;
+      align |= align >> 8;
+      align |= align >> 16;
+      align |= align >> 32;
+      align++;
+    }
   scn->rawdata.d.d_align = align;
   if (elf->class == ELFCLASS32
       || (offsetof (struct Elf, state.elf32.ehdr)
openSUSE Build Service is sponsored by