Fix pr20793.

With this patch the third field of llvm.global_ctors is also used on ELF.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217202 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2014-09-04 23:03:58 +00:00
parent edee9e81ce
commit 295a0088db
2 changed files with 53 additions and 27 deletions

View File

@ -360,42 +360,66 @@ TargetLoweringObjectFileELF::getSectionForConstant(SectionKind Kind,
const MCSection *TargetLoweringObjectFileELF::getStaticCtorSection( const MCSection *TargetLoweringObjectFileELF::getStaticCtorSection(
unsigned Priority, const MCSymbol *KeySym) const { unsigned Priority, const MCSymbol *KeySym) const {
// The default scheme is .ctor / .dtor, so we have to invert the priority std::string Name;
// numbering. unsigned Type;
if (Priority == 65535) unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
return StaticCtorSection; SectionKind Kind = SectionKind::getDataRel();
StringRef COMDAT = KeySym ? KeySym->getName() : "";
if (KeySym)
Flags |= ELF::SHF_GROUP;
if (UseInitArray) { if (UseInitArray) {
std::string Name = std::string(".init_array.") + utostr(Priority); Name = ".init_array";
return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY, if (Priority != 65535) {
ELF::SHF_ALLOC | ELF::SHF_WRITE, Name += '.';
SectionKind::getDataRel()); Name += utostr(Priority);
}
Type = ELF::SHT_INIT_ARRAY;
} else { } else {
std::string Name = std::string(".ctors.") + utostr(65535 - Priority); // The default scheme is .ctor / .dtor, so we have to invert the priority
return getContext().getELFSection(Name, ELF::SHT_PROGBITS, // numbering.
ELF::SHF_ALLOC |ELF::SHF_WRITE, Name = std::string(".ctors");
SectionKind::getDataRel()); if (Priority != 65535) {
Name += '.';
Name += utostr(65535 - Priority);
}
Type = ELF::SHT_PROGBITS;
} }
return getContext().getELFSection(Name, Type, Flags, Kind, 0, COMDAT);
} }
const MCSection *TargetLoweringObjectFileELF::getStaticDtorSection( const MCSection *TargetLoweringObjectFileELF::getStaticDtorSection(
unsigned Priority, const MCSymbol *KeySym) const { unsigned Priority, const MCSymbol *KeySym) const {
// The default scheme is .ctor / .dtor, so we have to invert the priority std::string Name;
// numbering. unsigned Type;
if (Priority == 65535) unsigned Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
return StaticDtorSection; SectionKind Kind = SectionKind::getDataRel();
StringRef COMDAT = KeySym ? KeySym->getName() : "";
if (KeySym)
Flags |= ELF::SHF_GROUP;
if (UseInitArray) { if (UseInitArray) {
std::string Name = std::string(".fini_array.") + utostr(Priority); Name = ".fini_array";
return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY, if (Priority != 65535) {
ELF::SHF_ALLOC | ELF::SHF_WRITE, Name += '.';
SectionKind::getDataRel()); Name += utostr(Priority);
}
Type = ELF::SHT_FINI_ARRAY;
} else { } else {
std::string Name = std::string(".dtors.") + utostr(65535 - Priority); // The default scheme is .ctor / .dtor, so we have to invert the priority
return getContext().getELFSection(Name, ELF::SHT_PROGBITS, // numbering.
ELF::SHF_ALLOC |ELF::SHF_WRITE, Name = ".dtors";
SectionKind::getDataRel()); if (Priority != 65535) {
Name += '.';
Name += utostr(65535 - Priority);
}
Type = ELF::SHT_PROGBITS;
} }
return getContext().getELFSection(Name, Type, Flags, Kind, 0, COMDAT);
} }
void void

View File

@ -1,6 +1,8 @@
; RUN: llc -mtriple x86_64-pc-linux -use-ctors < %s | FileCheck --check-prefix=CTOR %s ; RUN: llc -mtriple x86_64-pc-linux -use-ctors < %s | FileCheck --check-prefix=CTOR %s
; RUN: llc -mtriple x86_64-pc-linux < %s | FileCheck --check-prefix=INIT-ARRAY %s ; RUN: llc -mtriple x86_64-pc-linux < %s | FileCheck --check-prefix=INIT-ARRAY %s
@llvm.global_ctors = appending global [2 x { i32, void ()* }] [{ i32, void ()* } { i32 65535, void ()* @f }, { i32, void ()* } { i32 15, void ()* @g }] @llvm.global_ctors = appending global [2 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @f, i8* null}, { i32, void ()*, i8* } { i32 15, void ()* @g, i8* @v }]
@v = weak_odr global i8 0
define void @f() { define void @f() {
entry: entry:
@ -12,14 +14,14 @@ entry:
ret void ret void
} }
; CTOR: .section .ctors.65520,"aw",@progbits ; CTOR: .section .ctors.65520,"aGw",@progbits,v,comdat
; CTOR-NEXT: .align 8 ; CTOR-NEXT: .align 8
; CTOR-NEXT: .quad g ; CTOR-NEXT: .quad g
; CTOR-NEXT: .section .ctors,"aw",@progbits ; CTOR-NEXT: .section .ctors,"aw",@progbits
; CTOR-NEXT: .align 8 ; CTOR-NEXT: .align 8
; CTOR-NEXT: .quad f ; CTOR-NEXT: .quad f
; INIT-ARRAY: .section .init_array.15,"aw",@init_array ; INIT-ARRAY: .section .init_array.15,"aGw",@init_array,v,comdat
; INIT-ARRAY-NEXT: .align 8 ; INIT-ARRAY-NEXT: .align 8
; INIT-ARRAY-NEXT: .quad g ; INIT-ARRAY-NEXT: .quad g
; INIT-ARRAY-NEXT: .section .init_array,"aw",@init_array ; INIT-ARRAY-NEXT: .section .init_array,"aw",@init_array