attributes that force symbols into named sections can create broken ELF output
| Bugzilla Link | 43457 |
| Resolution | FIXED |
| Resolved on | Apr 23, 2020 03:14 |
| Version | trunk |
| OS | Windows NT |
| CC | @slacka,@nickdesaulniers,@rjmccall,@yuanfang-chen |
Extended Description
Attributes that force the compiler to place symbols into the same section can create broken ELF output if those symbols are incompatible.
Consider the following test case:
@foo = unnamed_addr constant [4 x i32] [i32 1, i32 0, i32 0, i32 1], align 16 "rodata-section"=".explicit"
@bar = unnamed_addr constant [9 x i64] [i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 1], align 16 "rodata-section"=".explicit"
which if run through llc results in:
.type foo,@object # @​foo
.section .explicit,"aM",@progbits,16
.globl foo
.p2align 4
foo: .long 1 # 0x1 .long 0 # 0x0 .long 0 # 0x0 .long 1 # 0x1 .size foo, 16
.type bar,@object # @​bar
.globl bar
.p2align 4
bar: .quad 0 # 0x0 .quad 0 # 0x0 .quad 0 # 0x0 .quad 0 # 0x0 .quad 0 # 0x0 .quad 0 # 0x0 .quad 0 # 0x0 .quad 0 # 0x0 .quad 1 # 0x1 .size bar, 72
The entsize of the resulting .explicit section is less than the size of symbol bar. This is an incorrect ELF as the linker can merge entsize pieces of this section at link time which will result in incorrect output, given this input.
In this case the compiler has created an incorrect SHF_MERGE section as two constant symbols with different sizes were placed in the same section and, at the time of writing, the created section derives its properties from the first symbol to be placed in the section - which is why the entsize for the .explicit section is 16.
GCC does not produce a mergable section for this test case.
In terms of clang source code:
#pragma clang section ... - https://clang.llvm.org/docs/LanguageExtensions.html#specifying-section-names-for-global-objects-pragma-clang-section
and
attribute ((section ("section-name"))) - https://clang.llvm.org/docs/AttributeReference.html#section-declspec-allocate
map to IR attributes that can cause this bug.
Ideas for a fix:
-
Allow these attributes to create multiple sections, each with the same name, rather than a single section. This can be done by checking if the section properties required for a given symbol are incompatible with any existing section of the same name and creating a new section if they are.
-
Try to create sections that can safely have different kinds of symbols placed into them. Error if symbols are truly incompatible.
For the specific case of SHF_MERGE sections a simple fix is to not put symbols into SHF_MERGE sections if they have been explicitly assigned to a section. This misses out on the SHF_MERGE optimizations in the linker - but only for symbols that have been explicitly placed in a section (rare?). This change may be worth making first before tackling the larger problem.
mentioned in issue llvm/llvm-bugzilla-archive#44775
Mergeable issues fixed by: 86478d3de91a.
During review we agreed that we are happy with 86478d3de91a being the full fix for this bug.
Originally, I was of the opinion that we should diagnose/fix the case of any symbol being explicitly assigned to an incompatible section. For example putting a function in the data section (.data). However, you can easily do this at link time (with a linker script) and no diagnostic will be emitted. For most symbols the Lowering behaviour is also obvious from the source code. Mergeable symbols are the exception as it is not obvious that these will be optimized during Lowering.
This issue feels like a duplicate of: llvm/llvm-bugzilla-archive#43627 (I've also been following along on the review of https://reviews.llvm.org/D68101). I'm playing around with a few more test cases comparing what GCC generates before making further recommendations.
For the specific case of SHF_MERGE sections a simple fix is to not put symbols into SHF_MERGE sections if they have been explicitly assigned to a section. This misses out on the SHF_MERGE optimizations in the linker - but only for symbols that have been explicitly placed in a section (rare?). This change may be worth making first before tackling the larger problem.
I put up https://reviews.llvm.org/D68101 for this.