xref: /openbsd-src/gnu/llvm/llvm/lib/CodeGen/AsmPrinter/AIXException.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
173471bf0Spatrick //===-- CodeGen/AsmPrinter/AIXException.cpp - AIX Exception Impl ----------===//
273471bf0Spatrick //
373471bf0Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
473471bf0Spatrick // See https://llvm.org/LICENSE.txt for license information.
573471bf0Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
673471bf0Spatrick //
773471bf0Spatrick //===----------------------------------------------------------------------===//
873471bf0Spatrick //
973471bf0Spatrick // This file contains support for writing AIX exception info into asm files.
1073471bf0Spatrick //
1173471bf0Spatrick //===----------------------------------------------------------------------===//
1273471bf0Spatrick 
1373471bf0Spatrick #include "DwarfException.h"
1473471bf0Spatrick #include "llvm/CodeGen/AsmPrinter.h"
1573471bf0Spatrick #include "llvm/CodeGen/MachineModuleInfo.h"
1673471bf0Spatrick #include "llvm/CodeGen/TargetLoweringObjectFileImpl.h"
1773471bf0Spatrick #include "llvm/MC/MCSectionXCOFF.h"
1873471bf0Spatrick #include "llvm/MC/MCStreamer.h"
1973471bf0Spatrick #include "llvm/Target/TargetLoweringObjectFile.h"
2073471bf0Spatrick #include "llvm/Target/TargetMachine.h"
2173471bf0Spatrick 
2273471bf0Spatrick namespace llvm {
2373471bf0Spatrick 
AIXException(AsmPrinter * A)24*d415bd75Srobert AIXException::AIXException(AsmPrinter *A) : EHStreamer(A) {}
2573471bf0Spatrick 
emitExceptionInfoTable(const MCSymbol * LSDA,const MCSymbol * PerSym)2673471bf0Spatrick void AIXException::emitExceptionInfoTable(const MCSymbol *LSDA,
2773471bf0Spatrick                                           const MCSymbol *PerSym) {
2873471bf0Spatrick   // Generate EH Info Table.
2973471bf0Spatrick   // The EH Info Table, aka, 'compat unwind section' on AIX, have the following
3073471bf0Spatrick   // format: struct eh_info_t {
3173471bf0Spatrick   //   unsigned version;           /* EH info verion 0 */
3273471bf0Spatrick   // #if defined(__64BIT__)
3373471bf0Spatrick   //   char _pad[4];               /* padding */
3473471bf0Spatrick   // #endif
3573471bf0Spatrick   //   unsigned long lsda;         /* Pointer to LSDA */
3673471bf0Spatrick   //   unsigned long personality;  /* Pointer to the personality routine */
3773471bf0Spatrick   //   }
3873471bf0Spatrick 
39*d415bd75Srobert   auto *EHInfo =
40*d415bd75Srobert       cast<MCSectionXCOFF>(Asm->getObjFileLowering().getCompactUnwindSection());
41*d415bd75Srobert   if (Asm->TM.getFunctionSections()) {
42*d415bd75Srobert     // If option -ffunction-sections is on, append the function name to the
43*d415bd75Srobert     // name of EH Info Table csect so that each function has its own EH Info
44*d415bd75Srobert     // Table csect. This helps the linker to garbage-collect EH info of unused
45*d415bd75Srobert     // functions.
46*d415bd75Srobert     SmallString<128> NameStr = EHInfo->getName();
47*d415bd75Srobert     raw_svector_ostream(NameStr) << '.' << Asm->MF->getFunction().getName();
48*d415bd75Srobert     EHInfo = Asm->OutContext.getXCOFFSection(NameStr, EHInfo->getKind(),
49*d415bd75Srobert                                              EHInfo->getCsectProp());
50*d415bd75Srobert   }
51*d415bd75Srobert   Asm->OutStreamer->switchSection(EHInfo);
5273471bf0Spatrick   MCSymbol *EHInfoLabel =
5373471bf0Spatrick       TargetLoweringObjectFileXCOFF::getEHInfoTableSymbol(Asm->MF);
5473471bf0Spatrick   Asm->OutStreamer->emitLabel(EHInfoLabel);
5573471bf0Spatrick 
5673471bf0Spatrick   // Version number.
5773471bf0Spatrick   Asm->emitInt32(0);
5873471bf0Spatrick 
5973471bf0Spatrick   const DataLayout &DL = MMI->getModule()->getDataLayout();
6073471bf0Spatrick   const unsigned PointerSize = DL.getPointerSize();
6173471bf0Spatrick 
6273471bf0Spatrick   // Add necessary paddings in 64 bit mode.
63*d415bd75Srobert   Asm->OutStreamer->emitValueToAlignment(Align(PointerSize));
6473471bf0Spatrick 
6573471bf0Spatrick   // LSDA location.
6673471bf0Spatrick   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(LSDA, Asm->OutContext),
6773471bf0Spatrick                               PointerSize);
6873471bf0Spatrick 
6973471bf0Spatrick   // Personality routine.
7073471bf0Spatrick   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(PerSym, Asm->OutContext),
7173471bf0Spatrick                               PointerSize);
7273471bf0Spatrick }
7373471bf0Spatrick 
endFunction(const MachineFunction * MF)7473471bf0Spatrick void AIXException::endFunction(const MachineFunction *MF) {
7573471bf0Spatrick   // There is no easy way to access register information in `AIXException`
7673471bf0Spatrick   // class. when ShouldEmitEHBlock is false and VRs are saved, A dumy eh info
7773471bf0Spatrick   // table are emitted in PPCAIXAsmPrinter::emitFunctionBodyEnd.
7873471bf0Spatrick   if (!TargetLoweringObjectFileXCOFF::ShouldEmitEHBlock(MF))
7973471bf0Spatrick     return;
8073471bf0Spatrick 
8173471bf0Spatrick   const MCSymbol *LSDALabel = emitExceptionTable();
8273471bf0Spatrick 
8373471bf0Spatrick   const Function &F = MF->getFunction();
8473471bf0Spatrick   assert(F.hasPersonalityFn() &&
8573471bf0Spatrick          "Landingpads are presented, but no personality routine is found.");
86*d415bd75Srobert   const auto *Per =
87*d415bd75Srobert       cast<GlobalValue>(F.getPersonalityFn()->stripPointerCasts());
8873471bf0Spatrick   const MCSymbol *PerSym = Asm->TM.getSymbol(Per);
8973471bf0Spatrick 
9073471bf0Spatrick   emitExceptionInfoTable(LSDALabel, PerSym);
9173471bf0Spatrick }
9273471bf0Spatrick 
9373471bf0Spatrick } // End of namespace llvm
94