summaryrefslogtreecommitdiff
path: root/minix/llvm/passes/WeakAliasModuleOverride/WeakAliasModuleOverride.cpp
blob: 0d3a15120b3bf993958df8610eec394447c851e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"

#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Regex.h"

using namespace llvm;

//#define passLog(M) (errs() << "WeakAliasModuleOverride: " << M << "\n")
#define passLog(M) /* nothing */

namespace {
  class WeakAliasModuleOverride : public ModulePass {

  public:
    static char ID;
    WeakAliasModuleOverride() : ModulePass(ID) {
    }

    virtual bool runOnModule(Module &M) {
      const Module::FunctionListType &listFcts = M.getFunctionList();
      const Module::GlobalListType &listGlobalVars = M.getGlobalList();

      std::string Asm = M.getModuleInlineAsm();

      passLog("ASM START\n"
	  << Asm
	  << "ASM END\n");

      // Filter out Function symbols
      for(Module::const_iterator it = listFcts.begin(), end=listFcts.end(); it!= end; ++it)
      {
	if (it->isDeclaration())
	  continue;

	// Filter out the assembly weak symbol as well as its default value
        std::string symbolName = it->getName();
        std::string matchWeak = "(^.weak.* " + symbolName + "\n)";
        std::string matchAssignement = "(^" + symbolName + " .*=.*\n)";

        Regex filterWeak(matchWeak, Regex::Newline);
        Regex filterAssignement(matchAssignement, Regex::Newline);

        while(filterWeak.match(Asm))
          Asm = filterWeak.sub("", Asm);

        while(filterAssignement.match(Asm))
          Asm = filterAssignement.sub("", Asm);
      }

      // Filter out GlobalVariable symbols
      for(Module::const_global_iterator it = listGlobalVars.begin(), end=listGlobalVars.end(); it!= end; ++it)
      {
	if (! it->hasInitializer())
	  continue;

	// Filter out the assembly weak symbol as well as its default value
        std::string symbolName = it->getName();
        std::string matchWeak = "(^.weak.* " + symbolName + "\n)";
        std::string matchAssignement = "(^" + symbolName + " .*=.*\n)";

        Regex filterWeak(matchWeak, Regex::Newline);
        Regex filterAssignement(matchAssignement, Regex::Newline);

        while(filterWeak.match(Asm))
          Asm = filterWeak.sub("", Asm);

        while(filterAssignement.match(Asm))
          Asm = filterAssignement.sub("", Asm);
      }

      M.setModuleInlineAsm(Asm);

      passLog("ASM START - registered\n"
	  << M.getModuleInlineAsm()
	  << "ASM END\n");

      return true;
    }
  };
}

char WeakAliasModuleOverride::ID = 0;
RegisterPass<WeakAliasModuleOverride> WEAK_ALIAS_MODULE_OVERRIDE("weak-alias-module-override", "Fix Weak Alias overrides");