Skip to content

Commit ed20366

Browse files
author
Dale Johannesen
committed
Do not mark EH tables no-dead-strip unless the
associated function is so marked. llvm-svn: 46088
1 parent 88d5909 commit ed20366

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "llvm/Support/DataTypes.h"
3636
#include "llvm/ADT/SmallVector.h"
3737
#include "llvm/ADT/UniqueVector.h"
38+
#include "llvm/ADT/SmallPtrSet.h"
3839
#include "llvm/GlobalValue.h"
3940
#include "llvm/Pass.h"
4041

@@ -1037,6 +1038,10 @@ class MachineModuleInfo : public ImmutablePass {
10371038
// common EH frames.
10381039
std::vector<Function *> Personalities;
10391040

1041+
// UsedFunctions - the functions in the llvm.used list in a more easily
1042+
// searchable format.
1043+
SmallPtrSet<const Function *, 32> UsedFunctions;
1044+
10401045
bool CallsEHReturn;
10411046
bool CallsUnwindInit;
10421047
public:
@@ -1235,6 +1240,11 @@ class MachineModuleInfo : public ImmutablePass {
12351240
return Personalities;
12361241
}
12371242

1243+
// UsedFunctions - Return set of the functions in the llvm.used list.
1244+
const SmallPtrSet<const Function *, 32>& getUsedFunctions() const {
1245+
return UsedFunctions;
1246+
}
1247+
12381248
/// addCatchTypeInfo - Provide the catch typeinfo for a landing pad.
12391249
///
12401250
void addCatchTypeInfo(MachineBasicBlock *LandingPad,

llvm/lib/CodeGen/DwarfWriter.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2764,15 +2764,14 @@ class DwarfException : public Dwarf {
27642764
bool hasCalls;
27652765
bool hasLandingPads;
27662766
std::vector<MachineMove> Moves;
2767-
Function::LinkageTypes linkage;
2767+
const Function * function;
27682768

27692769
FunctionEHFrameInfo(const std::string &FN, unsigned Num, unsigned P,
27702770
bool hC, bool hL,
27712771
const std::vector<MachineMove> &M,
2772-
Function::LinkageTypes l):
2772+
const Function *f):
27732773
FnName(FN), Number(Num), PersonalityIndex(P),
2774-
hasCalls(hC), hasLandingPads(hL), Moves(M),
2775-
linkage(l) { }
2774+
hasCalls(hC), hasLandingPads(hL), Moves(M), function (f) { }
27762775
};
27772776

27782777
std::vector<FunctionEHFrameInfo> EHFrames;
@@ -2869,32 +2868,39 @@ class DwarfException : public Dwarf {
28692868
/// EmitEHFrame - Emit function exception frame information.
28702869
///
28712870
void EmitEHFrame(const FunctionEHFrameInfo &EHFrameInfo) {
2871+
Function::LinkageTypes linkage = EHFrameInfo.function->getLinkage();
2872+
28722873
Asm->SwitchToTextSection(TAI->getDwarfEHFrameSection());
28732874

28742875
// Externally visible entry into the functions eh frame info.
28752876
// If the corresponding function is static, this should not be
28762877
// externally visible.
2877-
if (EHFrameInfo.linkage != Function::InternalLinkage) {
2878+
if (linkage != Function::InternalLinkage) {
28782879
if (const char *GlobalEHDirective = TAI->getGlobalEHDirective())
28792880
O << GlobalEHDirective << EHFrameInfo.FnName << "\n";
28802881
}
28812882

28822883
// If corresponding function is weak definition, this should be too.
2883-
if ((EHFrameInfo.linkage == Function::WeakLinkage ||
2884-
EHFrameInfo.linkage == Function::LinkOnceLinkage) &&
2884+
if ((linkage == Function::WeakLinkage ||
2885+
linkage == Function::LinkOnceLinkage) &&
28852886
TAI->getWeakDefDirective())
28862887
O << TAI->getWeakDefDirective() << EHFrameInfo.FnName << "\n";
28872888

28882889
// If there are no calls then you can't unwind. This may mean we can
28892890
// omit the EH Frame, but some environments do not handle weak absolute
28902891
// symbols.
28912892
if (!EHFrameInfo.hasCalls &&
2892-
((EHFrameInfo.linkage != Function::WeakLinkage &&
2893-
EHFrameInfo.linkage != Function::LinkOnceLinkage) ||
2893+
((linkage != Function::WeakLinkage &&
2894+
linkage != Function::LinkOnceLinkage) ||
28942895
!TAI->getWeakDefDirective() ||
28952896
TAI->getSupportsWeakOmittedEHFrame()))
28962897
{
28972898
O << EHFrameInfo.FnName << " = 0\n";
2899+
// This name has no connection to the function, so it might get
2900+
// dead-stripped when the function is not, erroneously. Prohibit
2901+
// dead-stripping unconditionally.
2902+
if (const char *UsedDirective = TAI->getUsedDirective())
2903+
O << UsedDirective << EHFrameInfo.FnName << "\n\n";
28982904
} else {
28992905
O << EHFrameInfo.FnName << ":\n";
29002906

@@ -2941,10 +2947,16 @@ class DwarfException : public Dwarf {
29412947

29422948
Asm->EmitAlignment(2);
29432949
EmitLabel("eh_frame_end", EHFrameInfo.Number);
2944-
}
29452950

2946-
if (const char *UsedDirective = TAI->getUsedDirective())
2947-
O << UsedDirective << EHFrameInfo.FnName << "\n\n";
2951+
// If the function is marked used, this table should be also. We cannot
2952+
// make the mark unconditional in this case, since retaining the table
2953+
// also retains the function in this case, and there is code around
2954+
// that depends on unused functions (calling undefined externals) being
2955+
// dead-stripped to link correctly. Yes, there really is.
2956+
if (MMI->getUsedFunctions().count(EHFrameInfo.function))
2957+
if (const char *UsedDirective = TAI->getUsedDirective())
2958+
O << UsedDirective << EHFrameInfo.FnName << "\n\n";
2959+
}
29482960
}
29492961

29502962
/// EmitExceptionTable - Emit landing pads and actions.
@@ -3414,7 +3426,7 @@ class DwarfException : public Dwarf {
34143426
MF->getFrameInfo()->hasCalls(),
34153427
!MMI->getLandingPads().empty(),
34163428
MMI->getFrameMoves(),
3417-
MF->getFunction()->getLinkage()));
3429+
MF->getFunction()));
34183430
}
34193431
};
34203432

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,21 @@ bool MachineModuleInfo::Verify(Value *V) {
15521552
///
15531553
void MachineModuleInfo::AnalyzeModule(Module &M) {
15541554
SetupCompileUnits(M);
1555+
1556+
// Insert functions in the llvm.used array into UsedFunctions.
1557+
GlobalVariable *GV = M.getGlobalVariable("llvm.used");
1558+
if (!GV || !GV->hasInitializer()) return;
1559+
1560+
// Should be an array of 'i8*'.
1561+
ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
1562+
if (InitList == 0) return;
1563+
1564+
for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
1565+
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(InitList->getOperand(i)))
1566+
if (CE->getOpcode() == Instruction::BitCast)
1567+
if (Function *F = dyn_cast<Function>(CE->getOperand(0)))
1568+
UsedFunctions.insert(F);
1569+
}
15551570
}
15561571

15571572
/// needsFrameInfo - Returns true if we need to gather callee-saved register

0 commit comments

Comments
 (0)