Andrew Geissler | c926e17 | 2021-05-07 16:11:35 -0500 | [diff] [blame] | 1 | If a binary has multiple SHT_NOTE sections and corresponding PT_NOTE |
| 2 | headers, we can see the error: |
| 3 | |
| 4 | patchelf: cannot normalize PT_NOTE segment: non-contiguous SHT_NOTE sections |
| 5 | |
| 6 | if the SHT_NOTE sections aren't sized to end on aligned boundaries. An example |
| 7 | would be a binary with: |
| 8 | |
| 9 | [ 2] .note.ABI-tag NOTE 00000000000002f4 000002f4 |
| 10 | 0000000000000020 0000000000000000 A 0 0 4 |
| 11 | [ 3] .note.gnu.propert NOTE 0000000000000318 00000318 |
| 12 | 0000000000000030 0000000000000000 A 0 0 8 |
| 13 | [ 4] .note.gnu.build-i NOTE 0000000000000348 00000348 |
| 14 | 0000000000000024 0000000000000000 A 0 0 4 |
| 15 | |
| 16 | NOTE 0x0000000000000318 0x0000000000000318 0x0000000000000318 |
| 17 | 0x0000000000000030 0x0000000000000030 R 0x8 |
| 18 | NOTE 0x00000000000002f4 0x00000000000002f4 0x00000000000002f4 |
| 19 | 0x0000000000000078 0x0000000000000074 R 0x4 |
| 20 | |
| 21 | since the PT_NOTE section at 2f4 covers [2] and [3] but the code |
| 22 | calclates curr_off should be 314, not the 318 in the binary. This |
| 23 | is an alignment issue. |
| 24 | |
| 25 | To fix this, we need to round curr_off to the next section alignment. |
| 26 | |
| 27 | Upstream-Status: Submitted [https://github.com/NixOS/patchelf/pull/274] |
| 28 | Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org> |
| 29 | |
| 30 | Index: git/src/patchelf.cc |
| 31 | =================================================================== |
| 32 | --- git.orig/src/patchelf.cc |
| 33 | +++ git/src/patchelf.cc |
| 34 | @@ -1010,8 +1010,9 @@ void ElfFile<ElfFileParamNames>::normali |
| 35 | size_t size = 0; |
| 36 | for (const auto & shdr : shdrs) { |
| 37 | if (rdi(shdr.sh_type) != SHT_NOTE) continue; |
| 38 | - if (rdi(shdr.sh_offset) != curr_off) continue; |
| 39 | + if (rdi(shdr.sh_offset) != roundUp(curr_off, rdi(shdr.sh_addralign))) continue; |
| 40 | size = rdi(shdr.sh_size); |
| 41 | + curr_off = roundUp(curr_off, rdi(shdr.sh_addralign)); |
| 42 | break; |
| 43 | } |
| 44 | if (size == 0) |