OLD | NEW |
(Empty) | |
| 1 //===- RenameEntryPoint.cpp - Rename _start to avoid linking collisions ---===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // MinSFI compiles PNaCl bitcode into a native object file and links it into |
| 11 // a standard C program. However, both C and PNaCl name their entry points |
| 12 // '_start' which causes a linking collision. This pass therefore renames the |
| 13 // entry function of the MinSFI module to '_start_minsfi'. By changing the name |
| 14 // in the bitcode, we also avoid relying on objcopy. |
| 15 // |
| 16 //===----------------------------------------------------------------------===// |
| 17 |
| 18 #include "llvm/Pass.h" |
| 19 #include "llvm/IR/Function.h" |
| 20 #include "llvm/IR/Module.h" |
| 21 #include "llvm/Transforms/MinSFI.h" |
| 22 |
| 23 using namespace llvm; |
| 24 |
| 25 static const char PNaClEntryPointName[] = "_start"; |
| 26 const char minsfi::EntryFunctionName[] = "_start_minsfi"; |
| 27 |
| 28 namespace { |
| 29 class RenameEntryPoint : public ModulePass { |
| 30 public: |
| 31 static char ID; |
| 32 RenameEntryPoint() : ModulePass(ID) { |
| 33 initializeRenameEntryPointPass(*PassRegistry::getPassRegistry()); |
| 34 } |
| 35 |
| 36 virtual bool runOnModule(Module &M); |
| 37 }; |
| 38 } // namespace |
| 39 |
| 40 bool RenameEntryPoint::runOnModule(Module &M) { |
| 41 if (M.getNamedValue(minsfi::EntryFunctionName)) { |
| 42 report_fatal_error(std::string("RenameEntryPoint: The module already " |
| 43 "contains a value named '") + |
| 44 minsfi::EntryFunctionName + "'"); |
| 45 } |
| 46 |
| 47 Function *EntryFunc = M.getFunction(PNaClEntryPointName); |
| 48 if (!EntryFunc) { |
| 49 report_fatal_error(std::string("RenameEntryPoint: The module does not " |
| 50 "contain a function named '") + |
| 51 PNaClEntryPointName + "'"); |
| 52 } |
| 53 |
| 54 EntryFunc->setName(minsfi::EntryFunctionName); |
| 55 return true; |
| 56 } |
| 57 |
| 58 char RenameEntryPoint::ID = 0; |
| 59 INITIALIZE_PASS(RenameEntryPoint, "minsfi-rename-entry-point", |
| 60 "Rename _start to avoid linking collisions", false, false) |
| 61 |
| 62 ModulePass *llvm::createRenameEntryPointPass() { |
| 63 return new RenameEntryPoint(); |
| 64 } |
OLD | NEW |