2 #include "llvm/IR/GlobalVariable.h"
3 #include "llvm/IR/Module.h"
6 #include "llvm/Support/raw_ostream.h"
7 #include "llvm/Support/Regex.h"
11 //#define passLog(M) (errs() << "WeakAliasModuleOverride: " << M << "\n")
12 #define passLog(M) /* nothing */
15 class WeakAliasModuleOverride
: public ModulePass
{
19 WeakAliasModuleOverride() : ModulePass(ID
) {
22 virtual bool runOnModule(Module
&M
) {
23 const Module::FunctionListType
&listFcts
= M
.getFunctionList();
24 const Module::GlobalListType
&listGlobalVars
= M
.getGlobalList();
26 std::string Asm
= M
.getModuleInlineAsm();
32 // Filter out Function symbols
33 for(Module::const_iterator it
= listFcts
.begin(), end
=listFcts
.end(); it
!= end
; ++it
)
35 if (it
->isDeclaration())
38 // Filter out the assembly weak symbol as well as its default value
39 std::string symbolName
= it
->getName();
40 std::string matchWeak
= "(^.weak.* " + symbolName
+ "\n)";
41 std::string matchAssignement
= "(^" + symbolName
+ " .*=.*\n)";
43 Regex
filterWeak(matchWeak
, Regex::Newline
);
44 Regex
filterAssignement(matchAssignement
, Regex::Newline
);
46 while(filterWeak
.match(Asm
))
47 Asm
= filterWeak
.sub("", Asm
);
49 while(filterAssignement
.match(Asm
))
50 Asm
= filterAssignement
.sub("", Asm
);
53 // Filter out GlobalVariable symbols
54 for(Module::const_global_iterator it
= listGlobalVars
.begin(), end
=listGlobalVars
.end(); it
!= end
; ++it
)
56 if (! it
->hasInitializer())
59 // Filter out the assembly weak symbol as well as its default value
60 std::string symbolName
= it
->getName();
61 std::string matchWeak
= "(^.weak.* " + symbolName
+ "\n)";
62 std::string matchAssignement
= "(^" + symbolName
+ " .*=.*\n)";
64 Regex
filterWeak(matchWeak
, Regex::Newline
);
65 Regex
filterAssignement(matchAssignement
, Regex::Newline
);
67 while(filterWeak
.match(Asm
))
68 Asm
= filterWeak
.sub("", Asm
);
70 while(filterAssignement
.match(Asm
))
71 Asm
= filterAssignement
.sub("", Asm
);
74 M
.setModuleInlineAsm(Asm
);
76 passLog("ASM START - registered\n"
77 << M
.getModuleInlineAsm()
85 char WeakAliasModuleOverride::ID
= 0;
86 RegisterPass
<WeakAliasModuleOverride
> WEAK_ALIAS_MODULE_OVERRIDE("weak-alias-module-override", "Fix Weak Alias overrides");