Skip to content

Commit e592690

Browse files
committed
[XCOFF][AIX] Use unique section names for LSDA and EH info sections with -ffunction-sections
Summary: When -ffunction-sections is on, this patch makes the compiler to generate unique LSDA and EH info sections for functions on AIX by appending the function name to the section name as a suffix. This will allow the AIX linker to garbage-collect unused function. Reviewed by: MaskRay, hubert.reinterpretcast Differential Revision: https://reviews.llvm.org/D124855
1 parent 75f9e83 commit e592690

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ class TargetLoweringObjectFileXCOFF : public TargetLoweringObjectFile {
286286

287287
MCSymbol *getFunctionEntryPointSymbol(const GlobalValue *Func,
288288
const TargetMachine &TM) const override;
289+
290+
/// For functions, this will return the LSDA section. If option
291+
/// -ffunction-sections is on, this will return a unique csect with the
292+
/// function name appended to .gcc_except_table as a suffix of the LSDA
293+
/// section name.
294+
MCSection *getSectionForLSDA(const Function &F, const MCSymbol &FnSym,
295+
const TargetMachine &TM) const override;
289296
};
290297

291298
class TargetLoweringObjectFileGOFF : public TargetLoweringObjectFile {

llvm/include/llvm/MC/MCSectionXCOFF.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class MCSectionXCOFF final : public MCSection {
116116
Optional<XCOFF::DwarfSectionSubtypeFlags> getDwarfSubtypeFlags() const {
117117
return DwarfSubtypeFlags;
118118
}
119+
Optional<XCOFF::CsectProperties> getCsectProp() const { return CsectProp; }
119120
};
120121

121122
} // end namespace llvm

llvm/lib/CodeGen/AsmPrinter/AIXException.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/CodeGen/AsmPrinter.h"
1515
#include "llvm/CodeGen/MachineModuleInfo.h"
1616
#include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
17+
#include "llvm/MC/MCSectionXCOFF.h"
1718
#include "llvm/MC/MCStreamer.h"
1819
#include "llvm/Target/TargetLoweringObjectFile.h"
1920
#include "llvm/Target/TargetMachine.h"
@@ -37,8 +38,19 @@ void AIXException::emitExceptionInfoTable(const MCSymbol *LSDA,
3738
// unsigned long personality; /* Pointer to the personality routine */
3839
// }
3940

40-
Asm->OutStreamer->SwitchSection(
41-
Asm->getObjFileLowering().getCompactUnwindSection());
41+
auto *EHInfo =
42+
cast<MCSectionXCOFF>(Asm->getObjFileLowering().getCompactUnwindSection());
43+
if (Asm->TM.getFunctionSections()) {
44+
// If option -ffunction-sections is on, append the function name to the
45+
// name of EH Info Table csect so that each function has its own EH Info
46+
// Table csect. This helps the linker to garbage-collect EH info of unused
47+
// functions.
48+
SmallString<128> NameStr = EHInfo->getName();
49+
raw_svector_ostream(NameStr) << '.' << Asm->MF->getFunction().getName();
50+
EHInfo = Asm->OutContext.getXCOFFSection(NameStr, EHInfo->getKind(),
51+
EHInfo->getCsectProp());
52+
}
53+
Asm->OutStreamer->SwitchSection(EHInfo);
4254
MCSymbol *EHInfoLabel =
4355
TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(Asm->MF);
4456
Asm->OutStreamer->emitLabel(EHInfoLabel);

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,20 @@ MCSection *TargetLoweringObjectFileXCOFF::getSectionForTOCEntry(
25652565
XCOFF::XTY_SD));
25662566
}
25672567

2568+
MCSection *TargetLoweringObjectFileXCOFF::getSectionForLSDA(
2569+
const Function &F, const MCSymbol &FnSym, const TargetMachine &TM) const {
2570+
auto *LSDA = cast<MCSectionXCOFF>(LSDASection);
2571+
if (TM.getFunctionSections()) {
2572+
// If option -ffunction-sections is on, append the function name to the
2573+
// name of the LSDA csect so that each function has its own LSDA csect.
2574+
// This helps the linker to garbage-collect EH info of unused functions.
2575+
SmallString<128> NameStr = LSDA->getName();
2576+
raw_svector_ostream(NameStr) << '.' << F.getName();
2577+
LSDA = getContext().getXCOFFSection(NameStr, LSDA->getKind(),
2578+
LSDA->getCsectProp());
2579+
}
2580+
return LSDA;
2581+
}
25682582
//===----------------------------------------------------------------------===//
25692583
// GOFF
25702584
//===----------------------------------------------------------------------===//

llvm/test/CodeGen/PowerPC/aix-exception.ll

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
; RUN: llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff -mcpu=pwr4 \
22
; RUN: -mattr=-altivec -simplifycfg-require-and-preserve-domtree=1 < %s | \
3-
; RUN: FileCheck --check-prefixes=ASM,ASM32 %s
3+
; RUN: FileCheck --check-prefixes=ASM,ASMNFS,ASM32 %s
44

55
; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff -mcpu=pwr4 \
66
; RUN: -mattr=-altivec -simplifycfg-require-and-preserve-domtree=1 < %s | \
7-
; RUN: FileCheck --check-prefixes=ASM,ASM64 %s
7+
; RUN: FileCheck --check-prefixes=ASM,ASMNFS,ASM64 %s
8+
9+
; RUN: llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff -mcpu=pwr4 \
10+
; RUN: -mattr=-altivec -simplifycfg-require-and-preserve-domtree=1 \
11+
; RUN: -function-sections < %s | \
12+
; RUN: FileCheck --check-prefixes=ASM,ASMFS,ASM32 %s
13+
14+
; RUN: llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff -mcpu=pwr4 \
15+
; RUN: -mattr=-altivec -simplifycfg-require-and-preserve-domtree=1 \
16+
; RUN: -function-sections < %s | \
17+
; RUN: FileCheck --check-prefixes=ASM,ASMFS,ASM64 %s
818

919
@_ZTIi = external constant i8*
1020

@@ -17,7 +27,8 @@ entry:
1727
unreachable
1828
}
1929

20-
; ASM: ._Z9throwFuncv:
30+
; ASMNFS: ._Z9throwFuncv:
31+
; ASMFS: .csect ._Z9throwFuncv[PR],2
2132
; ASM: bl .__cxa_allocate_exception[PR]
2233
; ASM: nop
2334
; ASM32: lwz 4, L..C0(2)
@@ -78,7 +89,8 @@ eh.resume: ; preds = %catch.dispatch
7889
resume { i8*, i32 } %lpad.val3
7990
}
8091

81-
; ASM: ._Z9catchFuncv:
92+
; ASMNFS: ._Z9catchFuncv:
93+
; ASMFS: .csect ._Z9catchFuncv[PR],2
8294
; ASM: L..func_begin0:
8395
; ASM: # %bb.0: # %entry
8496
; ASM: mflr 0
@@ -114,7 +126,8 @@ eh.resume: ; preds = %catch.dispatch
114126
; ASM: .byte 0x80 # +HasExtensionTable, -HasVectorInfo, NumOfGPRsSaved = 0
115127
; ASM: .byte 0x00 # NumberOfFixedParms = 0
116128
; ASM: .byte 0x01 # NumberOfFPParms = 0, +HasParmsOnStack
117-
; ASM: .vbyte 4, L.._Z9catchFuncv0-._Z9catchFuncv # Function size
129+
; ASMNFS: .vbyte 4, L.._Z9catchFuncv0-._Z9catchFuncv # Function size
130+
; ASMFS: .vbyte 4, L.._Z9catchFuncv0-._Z9catchFuncv[PR] # Function size
118131
; ASM: .vbyte 2, 0x000d # Function name len = 13
119132
; ASM: .byte "_Z9catchFuncv" # Function Name
120133
; ASM: .byte 0x08 # ExtensionTableFlag = TB_EH_INFO
@@ -123,7 +136,8 @@ eh.resume: ; preds = %catch.dispatch
123136
; ASM64: .vbyte 8, L..C1-TOC[TC0] # EHInfo Table
124137
; ASM: L..func_end0:
125138

126-
; ASM: .csect .gcc_except_table[RO],2
139+
; ASMNFS: .csect .gcc_except_table[RO],2
140+
; ASMFS: .csect .gcc_except_table._Z9catchFuncv[RO],2
127141
; ASM: .align 2
128142
; ASM: GCC_except_table1:
129143
; ASM: L..exception0:
@@ -153,7 +167,8 @@ eh.resume: ; preds = %catch.dispatch
153167
; ASM: L..ttbase0:
154168
; ASM: .align 2
155169

156-
; ASM: .csect .eh_info_table[RW],2
170+
; ASMNFS: .csect .eh_info_table[RW],2
171+
; ASMFS: .csect .eh_info_table._Z9catchFuncv[RW],2
157172
; ASM: __ehinfo.1:
158173
; ASM: .vbyte 4, 0
159174
; ASM32: .align 2

0 commit comments

Comments
 (0)