1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 11:13:28 +01:00

[COFF] Implement constructor priorities

The priorities in the section name suffixes are zero padded,
allowing the linker to just do a lexical sort.

Add zero padding for .ctors sections in ELF as well.

Differential Revision: https://reviews.llvm.org/D40407

llvm-svn: 319150
This commit is contained in:
Martin Storsjo 2017-11-28 08:07:18 +00:00
parent 06ca4c334a
commit cdcb5a026f
2 changed files with 55 additions and 9 deletions

View File

@ -52,6 +52,7 @@
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
@ -530,10 +531,8 @@ static MCSectionELF *getStaticStructorSection(MCContext &Ctx, bool UseInitArray,
Name = ".ctors";
else
Name = ".dtors";
if (Priority != 65535) {
Name += '.';
Name += utostr(65535 - Priority);
}
if (Priority != 65535)
raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
Type = ELF::SHT_PROGBITS;
}
@ -1212,16 +1211,38 @@ void TargetLoweringObjectFileCOFF::Initialize(MCContext &Ctx,
}
}
static MCSectionCOFF *getCOFFStaticStructorSection(MCContext &Ctx,
const Triple &T, bool IsCtor,
unsigned Priority,
const MCSymbol *KeySym,
MCSectionCOFF *Default) {
if (T.isKnownWindowsMSVCEnvironment() || T.isWindowsItaniumEnvironment())
return Ctx.getAssociativeCOFFSection(Default, KeySym, 0);
std::string Name = IsCtor ? ".ctors" : ".dtors";
if (Priority != 65535)
raw_string_ostream(Name) << format(".%05u", 65535 - Priority);
return Ctx.getAssociativeCOFFSection(
Ctx.getCOFFSection(Name, COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
COFF::IMAGE_SCN_MEM_READ |
COFF::IMAGE_SCN_MEM_WRITE,
SectionKind::getData()),
KeySym, 0);
}
MCSection *TargetLoweringObjectFileCOFF::getStaticCtorSection(
unsigned Priority, const MCSymbol *KeySym) const {
return getContext().getAssociativeCOFFSection(
cast<MCSectionCOFF>(StaticCtorSection), KeySym, 0);
return getCOFFStaticStructorSection(getContext(), getTargetTriple(), true,
Priority, KeySym,
cast<MCSectionCOFF>(StaticCtorSection));
}
MCSection *TargetLoweringObjectFileCOFF::getStaticDtorSection(
unsigned Priority, const MCSymbol *KeySym) const {
return getContext().getAssociativeCOFFSection(
cast<MCSectionCOFF>(StaticDtorSection), KeySym, 0);
return getCOFFStaticStructorSection(getContext(), getTargetTriple(), false,
Priority, KeySym,
cast<MCSectionCOFF>(StaticDtorSection));
}
void TargetLoweringObjectFileCOFF::emitLinkerFlagsForGlobal(

View File

@ -7,7 +7,8 @@
; RUN: llc -mtriple x86_64-unknown-nacl < %s | FileCheck --check-prefix=NACL %s
; RUN: llc -mtriple i586-intel-elfiamcu -use-ctors < %s | FileCheck %s --check-prefix=MCU-CTORS
; RUN: llc -mtriple i586-intel-elfiamcu < %s | FileCheck %s --check-prefix=MCU-INIT-ARRAY
@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 }]
; RUN: llc -mtriple x86_64-win32-gnu < %s | FileCheck --check-prefix=COFF-CTOR %s
@llvm.global_ctors = appending global [3 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @f, i8* null}, { i32, void ()*, i8* } { i32 15, void ()* @g, i8* @v }, { i32, void ()*, i8* } { i32 55555, void ()* @h, i8* @v }]
@v = weak_odr global i8 0
@ -21,9 +22,17 @@ entry:
ret void
}
define void @h() {
entry:
ret void
}
; CTOR: .section .ctors.65520,"aGw",@progbits,v,comdat
; CTOR-NEXT: .p2align 3
; CTOR-NEXT: .quad g
; CTOR-NEXT: .section .ctors.09980,"aGw",@progbits,v,comdat
; CTOR-NEXT: .p2align 3
; CTOR-NEXT: .quad h
; CTOR-NEXT: .section .ctors,"aw",@progbits
; CTOR-NEXT: .p2align 3
; CTOR-NEXT: .quad f
@ -31,6 +40,9 @@ entry:
; INIT-ARRAY: .section .init_array.15,"aGw",@init_array,v,comdat
; INIT-ARRAY-NEXT: .p2align 3
; INIT-ARRAY-NEXT: .quad g
; INIT-ARRAY-NEXT: .section .init_array.55555,"aGw",@init_array,v,comdat
; INIT-ARRAY-NEXT: .p2align 3
; INIT-ARRAY-NEXT: .quad h
; INIT-ARRAY-NEXT: .section .init_array,"aw",@init_array
; INIT-ARRAY-NEXT: .p2align 3
; INIT-ARRAY-NEXT: .quad f
@ -38,9 +50,22 @@ entry:
; NACL: .section .init_array.15,"aGw",@init_array,v,comdat
; NACL-NEXT: .p2align 2
; NACL-NEXT: .long g
; NACL-NEXT: .section .init_array.55555,"aGw",@init_array,v,comdat
; NACL-NEXT: .p2align 2
; NACL-NEXT: .long h
; NACL-NEXT: .section .init_array,"aw",@init_array
; NACL-NEXT: .p2align 2
; NACL-NEXT: .long f
; MCU-CTORS: .section .ctors,"aw",@progbits
; MCU-INIT-ARRAY: .section .init_array,"aw",@init_array
; COFF-CTOR: .section .ctors.65520,"dw",associative,v
; COFF-CTOR-NEXT: .p2align 3
; COFF-CTOR-NEXT: .quad g
; COFF-CTOR-NEXT: .section .ctors.09980,"dw",associative,v
; COFF-CTOR-NEXT: .p2align 3
; COFF-CTOR-NEXT: .quad h
; COFF-CTOR-NEXT: .section .ctors,"dw"
; COFF-CTOR-NEXT: .p2align 3
; COFF-CTOR-NEXT: .quad f