Move the support for using .init_array from ARM to the generic

TargetLoweringObjectFileELF. Use this to support it on X86. Unlike ARM,
on X86 it is not easy to find out if .init_array should be used or not, so
the decision is made via TargetOptions and defaults to off.

Add a command line option to llc that enables it.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@158692 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2012-06-19 00:48:28 +00:00
parent f6fc855a00
commit d6b43a317e
10 changed files with 99 additions and 54 deletions

View File

@ -33,6 +33,8 @@ namespace llvm {
class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
bool UseInitArray;
public:
virtual ~TargetLoweringObjectFileELF() {}
@ -66,6 +68,7 @@ public:
getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
MachineModuleInfo *MMI) const;
void InitializeELF(bool UseInitArray_);
virtual const MCSection *
getStaticCtorSection(unsigned Priority = 65535) const;
virtual const MCSection *

View File

@ -43,7 +43,7 @@ namespace llvm {
StackAlignmentOverride(0), RealignStack(true),
DisableJumpTables(false), EnableFastISel(false),
PositionIndependentExecutable(false), EnableSegmentedStacks(false),
TrapFuncName(""), FloatABIType(FloatABI::Default)
UseInitArray(false), TrapFuncName(""), FloatABIType(FloatABI::Default)
{}
/// PrintMachineCode - This flag is enabled when the -print-machineinstrs
@ -172,6 +172,10 @@ namespace llvm {
unsigned EnableSegmentedStacks : 1;
/// UseInitArray - Use .init_array instead of .ctors for static
/// constructors.
unsigned UseInitArray : 1;
/// getTrapFunctionName - If this returns a non-empty string, this means
/// isel should lower Intrinsic::trap to a call to the specified function
/// name instead of an ISD::TRAP node.

View File

@ -349,10 +349,17 @@ TargetLoweringObjectFileELF::getStaticCtorSection(unsigned Priority) const {
if (Priority == 65535)
return StaticCtorSection;
if (UseInitArray) {
std::string Name = std::string(".init_array.") + utostr(Priority);
return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY,
ELF::SHF_ALLOC | ELF::SHF_WRITE,
SectionKind::getDataRel());
} else {
std::string Name = std::string(".ctors.") + utostr(65535 - Priority);
return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
ELF::SHF_ALLOC |ELF::SHF_WRITE,
SectionKind::getDataRel());
}
}
const MCSection *
@ -362,10 +369,35 @@ TargetLoweringObjectFileELF::getStaticDtorSection(unsigned Priority) const {
if (Priority == 65535)
return StaticDtorSection;
if (UseInitArray) {
std::string Name = std::string(".fini_array.") + utostr(Priority);
return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY,
ELF::SHF_ALLOC | ELF::SHF_WRITE,
SectionKind::getDataRel());
} else {
std::string Name = std::string(".dtors.") + utostr(65535 - Priority);
return getContext().getELFSection(Name, ELF::SHT_PROGBITS,
ELF::SHF_ALLOC |ELF::SHF_WRITE,
SectionKind::getDataRel());
}
}
void
TargetLoweringObjectFileELF::InitializeELF(bool UseInitArray_) {
UseInitArray = UseInitArray_;
if (!UseInitArray)
return;
StaticCtorSection =
getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
ELF::SHF_WRITE |
ELF::SHF_ALLOC,
SectionKind::getDataRel());
StaticDtorSection =
getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
ELF::SHF_WRITE |
ELF::SHF_ALLOC,
SectionKind::getDataRel());
}
//===----------------------------------------------------------------------===//

View File

@ -24,20 +24,11 @@ using namespace dwarf;
void ARMElfTargetObjectFile::Initialize(MCContext &Ctx,
const TargetMachine &TM) {
bool isAAPCS_ABI = TM.getSubtarget<ARMSubtarget>().isAAPCS_ABI();
TargetLoweringObjectFileELF::Initialize(Ctx, TM);
isAAPCS_ABI = TM.getSubtarget<ARMSubtarget>().isAAPCS_ABI();
InitializeELF(isAAPCS_ABI);
if (isAAPCS_ABI) {
StaticCtorSection =
getContext().getELFSection(".init_array", ELF::SHT_INIT_ARRAY,
ELF::SHF_WRITE |
ELF::SHF_ALLOC,
SectionKind::getDataRel());
StaticDtorSection =
getContext().getELFSection(".fini_array", ELF::SHT_FINI_ARRAY,
ELF::SHF_WRITE |
ELF::SHF_ALLOC,
SectionKind::getDataRel());
LSDASection = NULL;
}
@ -47,33 +38,3 @@ void ARMElfTargetObjectFile::Initialize(MCContext &Ctx,
0,
SectionKind::getMetadata());
}
const MCSection *
ARMElfTargetObjectFile::getStaticCtorSection(unsigned Priority) const {
if (!isAAPCS_ABI)
return TargetLoweringObjectFileELF::getStaticCtorSection(Priority);
if (Priority == 65535)
return StaticCtorSection;
// Emit ctors in priority order.
std::string Name = std::string(".init_array.") + utostr(Priority);
return getContext().getELFSection(Name, ELF::SHT_INIT_ARRAY,
ELF::SHF_ALLOC | ELF::SHF_WRITE,
SectionKind::getDataRel());
}
const MCSection *
ARMElfTargetObjectFile::getStaticDtorSection(unsigned Priority) const {
if (!isAAPCS_ABI)
return TargetLoweringObjectFileELF::getStaticDtorSection(Priority);
if (Priority == 65535)
return StaticDtorSection;
// Emit dtors in priority order.
std::string Name = std::string(".fini_array.") + utostr(Priority);
return getContext().getELFSection(Name, ELF::SHT_FINI_ARRAY,
ELF::SHF_ALLOC | ELF::SHF_WRITE,
SectionKind::getDataRel());
}

View File

@ -20,7 +20,6 @@ class TargetMachine;
class ARMElfTargetObjectFile : public TargetLoweringObjectFileELF {
protected:
const MCSection *AttributesSection;
bool isAAPCS_ABI;
public:
ARMElfTargetObjectFile() :
TargetLoweringObjectFileELF(),
@ -32,9 +31,6 @@ public:
virtual const MCSection *getAttributesSection() const {
return AttributesSection;
}
const MCSection * getStaticCtorSection(unsigned Priority) const;
const MCSection * getStaticDtorSection(unsigned Priority) const;
};
} // end namespace llvm

View File

@ -140,6 +140,8 @@ static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
return new TargetLoweringObjectFileMachO();
}
if (Subtarget->isTargetLinux())
return new X86LinuxTargetObjectFile();
if (Subtarget->isTargetELF())
return new TargetLoweringObjectFileELF();
if (Subtarget->isTargetCOFF() && !Subtarget->isTargetEnvMacho())

View File

@ -9,12 +9,15 @@
#include "X86TargetObjectFile.h"
#include "X86TargetMachine.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/CodeGen/MachineModuleInfoImpls.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/Target/Mangler.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/ELF.h"
using namespace llvm;
using namespace dwarf;
@ -42,3 +45,9 @@ getCFIPersonalitySymbol(const GlobalValue *GV, Mangler *Mang,
MachineModuleInfo *MMI) const {
return Mang->getSymbol(GV);
}
void
X86LinuxTargetObjectFile::Initialize(MCContext &Ctx, const TargetMachine &TM) {
TargetLoweringObjectFileELF::Initialize(Ctx, TM);
InitializeELF(TM.Options.UseInitArray);
}

View File

@ -32,6 +32,12 @@ namespace llvm {
MachineModuleInfo *MMI) const;
};
/// X86LinuxTargetObjectFile - This implementation is used for linux x86
/// and x86-64.
class X86LinuxTargetObjectFile : public TargetLoweringObjectFileELF {
virtual void Initialize(MCContext &Ctx, const TargetMachine &TM);
};
} // end namespace llvm
#endif

View File

@ -0,0 +1,27 @@
; RUN: llc < %s | FileCheck --check-prefix=CTOR %s
; RUN: llc -use-init-array < %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 }]
define void @f() {
entry:
ret void
}
define void @g() {
entry:
ret void
}
; CTOR: .section .ctors.65520,"aw",@progbits
; CTOR-NEXT: .align 8
; CTOR-NEXT: .quad g
; CTOR-NEXT: .section .ctors,"aw",@progbits
; CTOR-NEXT: .align 8
; CTOR-NEXT: .quad f
; INIT-ARRAY: .section .init_array.15,"aw",@init_array
; INIT-ARRAY-NEXT: .align 8
; INIT-ARRAY-NEXT: .quad g
; INIT-ARRAY-NEXT: .section .init_array,"aw",@init_array
; INIT-ARRAY-NEXT: .align 8
; INIT-ARRAY-NEXT: .quad f

View File

@ -244,6 +244,10 @@ SegmentedStacks("segmented-stacks",
cl::desc("Use segmented stacks if possible."),
cl::init(false));
static cl::opt<bool>
UseInitArray("use-init-array",
cl::desc("Use .init_array instead of .ctors."),
cl::init(false));
// GetFileNameRoot - Helper function to get the basename of a filename.
static inline std::string
@ -418,6 +422,7 @@ int main(int argc, char **argv) {
Options.TrapFuncName = TrapFuncName;
Options.PositionIndependentExecutable = EnablePIE;
Options.EnableSegmentedStacks = SegmentedStacks;
Options.UseInitArray = UseInitArray;
std::auto_ptr<TargetMachine>
target(TheTarget->createTargetMachine(TheTriple.getTriple(),