xref: /llvm-project/llvm/lib/ExecutionEngine/JITLink/SEHFrameSupport.h (revision 34313eb1f0c2f5c6b5303b26d6babdad985e16e9)
1cd953e4fSSunho Kim //===------- SEHFrameSupport.h - JITLink seh-frame utils --------*- C++ -*-===//
2cd953e4fSSunho Kim //
3cd953e4fSSunho Kim // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4cd953e4fSSunho Kim // See https://llvm.org/LICENSE.txt for license information.
5cd953e4fSSunho Kim // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6cd953e4fSSunho Kim //
7cd953e4fSSunho Kim //===----------------------------------------------------------------------===//
8cd953e4fSSunho Kim //
9cd953e4fSSunho Kim // SEHFrame utils for JITLink.
10cd953e4fSSunho Kim //
11cd953e4fSSunho Kim //===----------------------------------------------------------------------===//
12cd953e4fSSunho Kim 
13cd953e4fSSunho Kim #ifndef LLVM_EXECUTIONENGINE_JITLINK_SEHFRAMESUPPORT_H
14cd953e4fSSunho Kim #define LLVM_EXECUTIONENGINE_JITLINK_SEHFRAMESUPPORT_H
15cd953e4fSSunho Kim 
16*34313eb1SFangrui Song #include "llvm/ADT/SetVector.h"
17cd953e4fSSunho Kim #include "llvm/ExecutionEngine/JITLink/JITLink.h"
18cd953e4fSSunho Kim #include "llvm/ExecutionEngine/JITSymbol.h"
19cd953e4fSSunho Kim #include "llvm/Support/Error.h"
2062c7f035SArchibald Elliott #include "llvm/TargetParser/Triple.h"
21cd953e4fSSunho Kim 
22cd953e4fSSunho Kim namespace llvm {
23cd953e4fSSunho Kim namespace jitlink {
24cd953e4fSSunho Kim /// This pass adds keep-alive edge from SEH frame sections
25cd953e4fSSunho Kim /// to the parent function content block.
26cd953e4fSSunho Kim class SEHFrameKeepAlivePass {
27cd953e4fSSunho Kim public:
SEHFrameKeepAlivePass(StringRef SEHFrameSectionName)28cd953e4fSSunho Kim   SEHFrameKeepAlivePass(StringRef SEHFrameSectionName)
29cd953e4fSSunho Kim       : SEHFrameSectionName(SEHFrameSectionName) {}
30cd953e4fSSunho Kim 
operator()31cd953e4fSSunho Kim   Error operator()(LinkGraph &G) {
32cd953e4fSSunho Kim     auto *S = G.findSectionByName(SEHFrameSectionName);
33cd953e4fSSunho Kim     if (!S)
34cd953e4fSSunho Kim       return Error::success();
35cd953e4fSSunho Kim 
36cd953e4fSSunho Kim     // Simply consider every block pointed by seh frame block as parants.
37cd953e4fSSunho Kim     // This adds some unnecessary keep-alive edges to unwind info blocks,
38cd953e4fSSunho Kim     // (xdata) but these blocks are usually dead by default, so they wouldn't
39cd953e4fSSunho Kim     // count for the fate of seh frame block.
40cd953e4fSSunho Kim     for (auto *B : S->blocks()) {
41cd953e4fSSunho Kim       auto &DummySymbol = G.addAnonymousSymbol(*B, 0, 0, false, false);
42*34313eb1SFangrui Song       SetVector<Block *> Children;
43cd953e4fSSunho Kim       for (auto &E : B->edges()) {
44cd953e4fSSunho Kim         auto &Sym = E.getTarget();
45cd953e4fSSunho Kim         if (!Sym.isDefined())
46cd953e4fSSunho Kim           continue;
47cd953e4fSSunho Kim         Children.insert(&Sym.getBlock());
48cd953e4fSSunho Kim       }
49cd953e4fSSunho Kim       for (auto *Child : Children)
50cd953e4fSSunho Kim         Child->addEdge(Edge(Edge::KeepAlive, 0, DummySymbol, 0));
51cd953e4fSSunho Kim     }
52cd953e4fSSunho Kim     return Error::success();
53cd953e4fSSunho Kim   }
54cd953e4fSSunho Kim 
55cd953e4fSSunho Kim private:
56cd953e4fSSunho Kim   StringRef SEHFrameSectionName;
57cd953e4fSSunho Kim };
58cd953e4fSSunho Kim 
59cd953e4fSSunho Kim } // end namespace jitlink
60cd953e4fSSunho Kim } // end namespace llvm
61cd953e4fSSunho Kim 
62cd953e4fSSunho Kim #endif // LLVM_EXECUTIONENGINE_JITLINK_SEHFRAMESUPPORT_H
63