|
| 1 | +//===- Linkage.cpp - Ajdust linkage for the Yk JIT -----------------===// |
| 2 | +// |
| 3 | +// The JIT relies upon the use of `dlsym()` at runtime in order to lookup any |
| 4 | +// given function from its virtual address. For this to work the symbols for |
| 5 | +// all functions must be in the dynamic symbol table. |
| 6 | +// |
| 7 | +// `yk-config` already provides the `--export-dynamic` flag in order to ensure |
| 8 | +// that all *externally visible* symbols make it in to the dynamic symbol table, |
| 9 | +// but that's not enough: functions marked for internal linkage (e.g. `static` |
| 10 | +// functions in C) will be missed. |
| 11 | +// |
| 12 | +// This pass walks the functions of a module and flips any with internal linkage |
| 13 | +// to external linkage. |
| 14 | +// |
| 15 | +// Note that whilst symbols with internal linkage can have the same name and be |
| 16 | +// distinct, this is not so for symbols with external linkage. That's OK for |
| 17 | +// us because Yk requires the use of LTO, and the LTO module merger will have |
| 18 | +// already mangled the names for us so that symbol clashes can't occur. |
| 19 | + |
| 20 | +#include "llvm/Transforms/Yk/Linkage.h" |
| 21 | +#include "llvm/IR/Function.h" |
| 22 | +#include "llvm/IR/Module.h" |
| 23 | +#include "llvm/InitializePasses.h" |
| 24 | +#include "llvm/Pass.h" |
| 25 | + |
| 26 | +#define DEBUG_TYPE "yk-linkage" |
| 27 | + |
| 28 | +using namespace llvm; |
| 29 | + |
| 30 | +namespace llvm { |
| 31 | +void initializeYkLinkagePass(PassRegistry &); |
| 32 | +} // namespace llvm |
| 33 | + |
| 34 | +namespace { |
| 35 | +class YkLinkage : public ModulePass { |
| 36 | +public: |
| 37 | + static char ID; |
| 38 | + YkLinkage() : ModulePass(ID) { |
| 39 | + initializeYkLinkagePass(*PassRegistry::getPassRegistry()); |
| 40 | + } |
| 41 | + |
| 42 | + bool runOnModule(Module &M) override { |
| 43 | + for (Function &F : M) { |
| 44 | + if (F.hasInternalLinkage()) { |
| 45 | + F.setLinkage(GlobalVariable::ExternalLinkage); |
| 46 | + } |
| 47 | + } |
| 48 | + return true; |
| 49 | + } |
| 50 | +}; |
| 51 | +} // namespace |
| 52 | + |
| 53 | +char YkLinkage::ID = 0; |
| 54 | +INITIALIZE_PASS(YkLinkage, DEBUG_TYPE, "yk-linkage", false, false) |
| 55 | + |
| 56 | +ModulePass *llvm::createYkLinkagePass() { return new YkLinkage(); } |
0 commit comments