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