• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/MC/MCSectionXCOFF.h"
17 #include "llvm/MC/MCStreamer.h"
18 #include "llvm/Target/TargetLoweringObjectFile.h"
19 #include "llvm/Target/TargetMachine.h"
20 
21 namespace llvm {
22 
AIXException(AsmPrinter * A)23 AIXException::AIXException(AsmPrinter *A) : DwarfCFIExceptionBase(A) {}
24 
emitExceptionInfoTable(const MCSymbol * LSDA,const MCSymbol * PerSym)25 void AIXException::emitExceptionInfoTable(const MCSymbol *LSDA,
26                                           const MCSymbol *PerSym) {
27   // Generate EH Info Table.
28   // The EH Info Table, aka, 'compat unwind section' on AIX, have the following
29   // format: struct eh_info_t {
30   //   unsigned version;           /* EH info verion 0 */
31   // #if defined(__64BIT__)
32   //   char _pad[4];               /* padding */
33   // #endif
34   //   unsigned long lsda;         /* Pointer to LSDA */
35   //   unsigned long personality;  /* Pointer to the personality routine */
36   //   }
37 
38   Asm->OutStreamer->SwitchSection(
39       Asm->getObjFileLowering().getCompactUnwindSection());
40   MCSymbol *EHInfoLabel = MMI->getContext().getOrCreateSymbol(
41       "__ehinfo." + Twine(Asm->getFunctionNumber()));
42   Asm->OutStreamer->emitLabel(EHInfoLabel);
43 
44   // Version number.
45   Asm->emitInt32(0);
46 
47   const DataLayout &DL = MMI->getModule()->getDataLayout();
48   const unsigned PointerSize = DL.getPointerSize();
49 
50   // Add necessary paddings in 64 bit mode.
51   Asm->OutStreamer->emitValueToAlignment(PointerSize);
52 
53   // LSDA location.
54   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(LSDA, Asm->OutContext),
55                               PointerSize);
56 
57   // Personality routine.
58   Asm->OutStreamer->emitValue(MCSymbolRefExpr::create(PerSym, Asm->OutContext),
59                               PointerSize);
60 }
61 
endFunction(const MachineFunction * MF)62 void AIXException::endFunction(const MachineFunction *MF) {
63   const Function &F = MF->getFunction();
64   bool HasLandingPads = !MF->getLandingPads().empty();
65   const Function *Per = nullptr;
66   if (F.hasPersonalityFn())
67     Per = dyn_cast<Function>(F.getPersonalityFn()->stripPointerCasts());
68   bool EmitEHBlock =
69       HasLandingPads || (F.hasPersonalityFn() &&
70                          !isNoOpWithoutInvoke(classifyEHPersonality(Per)) &&
71                          F.needsUnwindTableEntry());
72 
73   if (!EmitEHBlock)
74     return;
75 
76   const MCSymbol *LSDALabel = emitExceptionTable();
77   const MCSymbol *PerSym = Asm->TM.getSymbol(Per);
78 
79   emitExceptionInfoTable(LSDALabel, PerSym);
80 }
81 
82 } // End of namespace llvm
83