bool isConstantUsed() const;
/// This method classifies the entry according to whether or not it may
- /// generate a relocation entry. This must be conservative, so if it might
- /// codegen to a relocatable entry, it should say so.
+ /// generate a relocation entry (either static or dynamic). This must be
+ /// conservative, so if it might codegen to a relocatable entry, it should say
+ /// so.
///
/// FIXME: This really should not be in IR.
bool needsRelocation() const;
+ bool needsDynamicRelocation() const;
/// For aggregates (struct/array/vector) return the constant that corresponds
/// to the specified element if possible, or null if not. This can return null
/// both must either be scalars or vectors with the same element count. If no
/// changes are made, the constant C is returned.
static Constant *mergeUndefsWith(Constant *C, Constant *Other);
+
+private:
+ enum PossibleRelocationsTy {
+ /// This constant requires no relocations. That is, it holds simple
+ /// constants (like integrals).
+ NoRelocation = 0,
+
+ /// This constant holds static relocations that can be resolved by the
+ /// static linker.
+ LocalRelocation = 1,
+
+ /// This constant holds dynamic relocations that the dynamic linker will
+ /// need to resolve.
+ GlobalRelocation = 2,
+ };
+
+ /// Determine what potential relocations may be needed by this constant.
+ PossibleRelocationsTy getRelocationInfo() const;
};
} // end namespace llvm
bool MachineConstantPoolEntry::needsRelocation() const {
if (isMachineConstantPoolEntry())
return true;
- return Val.ConstVal->needsRelocation();
+ return Val.ConstVal->needsDynamicRelocation();
}
SectionKind
return false;
}
+bool Constant::needsDynamicRelocation() const {
+ return getRelocationInfo() == GlobalRelocation;
+}
+
bool Constant::needsRelocation() const {
+ return getRelocationInfo() != NoRelocation;
+}
+
+Constant::PossibleRelocationsTy Constant::getRelocationInfo() const {
if (isa<GlobalValue>(this))
- return true; // Global reference.
+ return GlobalRelocation; // Global reference.
if (const BlockAddress *BA = dyn_cast<BlockAddress>(this))
- return BA->getFunction()->needsRelocation();
+ return BA->getFunction()->getRelocationInfo();
if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(this)) {
if (CE->getOpcode() == Instruction::Sub) {
if (isa<BlockAddress>(LHSOp0) && isa<BlockAddress>(RHSOp0) &&
cast<BlockAddress>(LHSOp0)->getFunction() ==
cast<BlockAddress>(RHSOp0)->getFunction())
- return false;
+ return NoRelocation;
// Relative pointers do not need to be dynamically relocated.
if (auto *RHSGV =
auto *LHS = LHSOp0->stripInBoundsConstantOffsets();
if (auto *LHSGV = dyn_cast<GlobalValue>(LHS)) {
if (LHSGV->isDSOLocal() && RHSGV->isDSOLocal())
- return false;
+ return LocalRelocation;
} else if (isa<DSOLocalEquivalent>(LHS)) {
if (RHSGV->isDSOLocal())
- return false;
+ return LocalRelocation;
}
}
}
}
}
- bool Result = false;
+ PossibleRelocationsTy Result = NoRelocation;
for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
- Result |= cast<Constant>(getOperand(i))->needsRelocation();
+ Result =
+ std::max(cast<Constant>(getOperand(i))->getRelocationInfo(), Result);
return Result;
}
// from .data to .text. This is not allowed in position-independent code.
auto *Init = GVar->getInitializer();
if ((TLI->isPositionIndependent() || TLI->getSubtarget()->isROPI()) &&
- Init->needsRelocation())
+ Init->needsDynamicRelocation())
return SDValue();
// The constant islands pass can only really deal with alignment requests
// variables or functions in constant data, so don't convert switches to
// lookup tables if any of the values would need relocation.
if (ST->isROPI() || ST->isRWPI())
- return !C->needsRelocation();
+ return !C->needsDynamicRelocation();
return true;
}
if (Kind.isReadOnly()) {
const auto *GVar = dyn_cast<GlobalVariable>(GO);
- if (GVar && GVar->isConstant() && GVar->getInitializer()->needsRelocation())
+ if (GVar && GVar->isConstant() &&
+ GVar->getInitializer()->needsDynamicRelocation())
Kind = SectionKind::getReadOnlyWithRel();
}
// consideration when it tries to merge entries in the section.
Reloc::Model ReloModel = TM.getRelocationModel();
if (ReloModel == Reloc::Static || ReloModel == Reloc::ROPI ||
- ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI)
+ ReloModel == Reloc::RWPI || ReloModel == Reloc::ROPI_RWPI ||
+ !C->needsDynamicRelocation())
return SectionKind::getReadOnly();
// Otherwise, the dynamic linker needs to fix it up, put it in the
; CHECK: .long hidden-relro2
@relro2 = constant i32 trunc (i64 sub (i64 ptrtoint (i8* @hidden to i64), i64 ptrtoint (i32* @relro2 to i64)) to i32)
-; CHECK: .section .rodata.cst8
+; CHECK: .section .rodata.obj
; CHECK-NEXT: .globl obj
; CHECK: obj:
; CHECK: .long 0