Skip to content

Commit 34e3d50

Browse files
committedMar 23, 2017
Force @{init,fini}_array if section name starts with ".{init,fini}_array.".
Fixes https://bugs.llvm.org/show_bug.cgi?id=32307. Differential Revision: https://reviews.llvm.org/D31255 llvm-svn: 298569
1 parent b57412e commit 34e3d50

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed
 

‎lld/ELF/InputSection.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,32 @@ InputSectionBase::InputSectionBase(InputFile *File, uint64_t Flags,
7171
this->Alignment = V;
7272
}
7373

74+
// GNU assembler 2.24 and LLVM 4.0.0's MC (the newest release as of
75+
// March 2017) fail to infer section types for sections starting with
76+
// ".init_array." or ".fini_array.". They set SHT_PROGBITS instead of
77+
// SHF_INIT_ARRAY. As a result, the following assembler directive
78+
// creates ".init_array.100" with SHT_PROGBITS, for example.
79+
//
80+
// .section .init_array.100, "aw"
81+
//
82+
// This function forces SHT_{INIT,FINI}_ARRAY so that we can handle
83+
// incorrect inputs as if they were correct from the beginning.
84+
static uint64_t getType(uint64_t Type, StringRef Name) {
85+
if (Type == SHT_PROGBITS && Name.startswith(".init_array."))
86+
return SHT_INIT_ARRAY;
87+
if (Type == SHT_PROGBITS && Name.startswith(".fini_array."))
88+
return SHT_FINI_ARRAY;
89+
return Type;
90+
}
91+
7492
template <class ELFT>
7593
InputSectionBase::InputSectionBase(elf::ObjectFile<ELFT> *File,
7694
const typename ELFT::Shdr *Hdr,
7795
StringRef Name, Kind SectionKind)
78-
: InputSectionBase(File, Hdr->sh_flags & ~SHF_INFO_LINK, Hdr->sh_type,
79-
Hdr->sh_entsize, Hdr->sh_link, Hdr->sh_info,
80-
Hdr->sh_addralign, getSectionContents(File, Hdr), Name,
81-
SectionKind) {
96+
: InputSectionBase(File, Hdr->sh_flags & ~SHF_INFO_LINK,
97+
getType(Hdr->sh_type, Name), Hdr->sh_entsize,
98+
Hdr->sh_link, Hdr->sh_info, Hdr->sh_addralign,
99+
getSectionContents(File, Hdr), Name, SectionKind) {
82100
// We reject object files having insanely large alignments even though
83101
// they are allowed by the spec. I think 4GB is a reasonable limitation.
84102
// We might want to relax this in the future.

‎lld/test/ELF/init-fini-progbits.s

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// REQUIRES: x86
2+
3+
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
4+
// RUN: ld.lld %t -o %t.exe
5+
// RUN: llvm-readobj -sections %t.exe | FileCheck %s
6+
7+
// CHECK: Name: .init_array
8+
// CHECK-NEXT: Type: SHT_INIT_ARRAY
9+
// CHECK: Name: .fini_array
10+
// CHECK-NEXT: Type: SHT_FINI_ARRAY
11+
12+
.globl _start
13+
_start:
14+
nop
15+
16+
.section .init_array.100, "aw", @progbits
17+
.byte 0
18+
.section .fini_array.100, "aw", @progbits
19+
.byte 0

0 commit comments

Comments
 (0)
Please sign in to comment.