xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/AsmPrinter/AIXException.cpp (revision ccb59683b98360afaf5b5bb641a68fea22c68d0b)
1 //===-- CodeGen/AsmPrinter/AIXException.cpp - AIX Exception Impl ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing AIX exception info into asm files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "DwarfException.h"
14 #include "llvm/CodeGen/AsmPrinter.h"
15 #include "llvm/CodeGen/MachineModuleInfo.h"
16 #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
17 #include "llvm/MC/MCSectionXCOFF.h"
18 #include "llvm/MC/MCStreamer.h"
19 #include "llvm/Target/TargetLoweringObjectFile.h"
20 #include "llvm/Target/TargetMachine.h"
21 
22 namespace llvm {
23 
24 AIXException::AIXException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
25 
26 void AIXException::markFunctionEnd() { endFragment(); }
27 
28 void AIXException::emitExceptionInfoTable(const MCSymbol *LSDA,
29                                           const MCSymbol *PerSym) {
30   // Generate EH Info Table.
31   // The EH Info Table, aka, 'compat unwind section' on AIX, have the following
32   // format: struct eh_info_t {
33   //   unsigned version;           /* EH info verion 0 */
34   // #if defined(__64BIT__)
35   //   char _pad[4];               /* padding */
36   // #endif
37   //   unsigned long lsda;         /* Pointer to LSDA */
38   //   unsigned long personality;  /* Pointer to the personality routine */
39   //   }
40 
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);
54   MCSymbol *EHInfoLabel =
55       TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(Asm->MF);
56   Asm->OutStreamer->emitLabel(EHInfoLabel);
57 
58   // Version number.
59   Asm->emitInt32(0);
60 
61   const DataLayout &DL = MMI->getModule()->getDataLayout();
62   const unsigned PointerSize = DL.getPointerSize();
63 
64   // Add necessary paddings in 64 bit mode.
65   Asm->OutStreamer->emitValueToAlignment(PointerSize);
66 
67   // LSDA location.
68   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(LSDA, Asm->OutContext),
69                               PointerSize);
70 
71   // Personality routine.
72   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(PerSym, Asm->OutContext),
73                               PointerSize);
74 }
75 
76 void AIXException::endFunction(const MachineFunction *MF) {
77   // There is no easy way to access register information in `AIXException`
78   // class. when ShouldEmitEHBlock is false and VRs are saved, A dumy eh info
79   // table are emitted in PPCAIXAsmPrinter::emitFunctionBodyEnd.
80   if (!TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF))
81     return;
82 
83   const MCSymbol *LSDALabel = emitExceptionTable();
84 
85   const Function &F = MF->getFunction();
86   assert(F.hasPersonalityFn() &&
87          "Landingpads are presented, but no personality routine is found.");
88   const auto *Per =
89       cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
90   const MCSymbol *PerSym = Asm->TM.getSymbol(Per);
91 
92   emitExceptionInfoTable(LSDALabel, PerSym);
93 }
94 
95 } // End of namespace llvm
96