blob: 21c3fe8b8e74ed57d546c6ec7f9aed709c7caae9 (
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
//===- GlobalCleanup.cpp - Cleanup global symbols post-bitcode-link -------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
// ===---------------------------------------------------------------------===//
//
// PNaCl executables should have no external symbols or aliases. These passes
// internalize (or otherwise remove/resolve) GlobalValues and resolve all
// GlobalAliases.
//
//===----------------------------------------------------------------------===//
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/NaCl.h"
using namespace llvm;
namespace {
class GlobalCleanup : public ModulePass {
public:
static char ID;
GlobalCleanup() : ModulePass(ID) {
initializeGlobalCleanupPass(*PassRegistry::getPassRegistry());
}
virtual bool runOnModule(Module &M);
};
class ResolveAliases : public ModulePass {
public:
static char ID;
ResolveAliases() : ModulePass(ID) {
initializeResolveAliasesPass(*PassRegistry::getPassRegistry());
}
virtual bool runOnModule(Module &M);
};
}
char GlobalCleanup::ID = 0;
INITIALIZE_PASS(GlobalCleanup, "nacl-global-cleanup",
"GlobalValue cleanup for PNaCl "
"(assumes all of the binary is linked statically)",
false, false)
static bool CleanUpLinkage(GlobalValue *GV) {
// TODO(dschuff): handle the rest of the linkage types as necessary without
// running afoul of the IR verifier or breaking the native link
switch (GV->getLinkage()) {
case GlobalValue::ExternalWeakLinkage: {
Constant *NullRef = Constant::getNullValue(GV->getType());
GV->replaceAllUsesWith(NullRef);
GV->eraseFromParent();
return true;
}
case GlobalValue::WeakAnyLinkage: {
GV->setLinkage(GlobalValue::InternalLinkage);
return true;
}
default:
// default with fall through to avoid compiler warning
return false;
}
return false;
}
bool GlobalCleanup::runOnModule(Module &M) {
bool Modified = false;
if (GlobalVariable *GV = M.getNamedGlobal("llvm.compiler.used")) {
GV->eraseFromParent();
Modified = true;
}
#if 0 // XXX EMSCRIPTEN: we need dis
if (GlobalVariable *GV = M.getNamedGlobal("llvm.used")) {
GV->eraseFromParent();
Modified = true;
}
#endif
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ) {
GlobalVariable *GV = I++;
Modified |= CleanUpLinkage(GV);
}
for (Module::iterator I = M.begin(), E = M.end(); I != E; ) {
Function *F = I++;
Modified |= CleanUpLinkage(F);
}
return Modified;
}
ModulePass *llvm::createGlobalCleanupPass() {
return new GlobalCleanup();
}
char ResolveAliases::ID = 0;
INITIALIZE_PASS(ResolveAliases, "resolve-aliases",
"resolve global variable and function aliases", false, false)
bool ResolveAliases::runOnModule(Module &M) {
bool Modified = false;
for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
I != E; ) {
GlobalAlias *Alias = I++;
Alias->replaceAllUsesWith(Alias->getAliasee());
Alias->eraseFromParent();
Modified = true;
}
return Modified;
}
ModulePass *llvm::createResolveAliasesPass() {
return new ResolveAliases();
}
|