Skip to content

Commit abca4d2

Browse files
committed
[GR-40811] Ignore @llvm.used and @llvm.compiler.used globals during module init
PullRequest: graal/12630
2 parents 52fe667 + 1b5af3b commit abca4d2

File tree

5 files changed

+75
-3
lines changed

5 files changed

+75
-3
lines changed

sulong/projects/com.oracle.truffle.llvm.parser/src/com/oracle/truffle/llvm/parser/LLVMParser.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,24 @@ public LLVMParserResult parse(ModelModule module, DataLayout targetDataLayout) {
8585
defineAliases(module.getAliases(), targetDataLayout);
8686

8787
return new LLVMParserResult(runtime, definedFunctions, externalFunctions, definedGlobals, externalGlobals, threadLocalGlobals, threadLocalGlobalObjectCounter, targetDataLayout,
88-
module.getTargetInformation(TargetTriple.class));
88+
module.getTargetInformation(TargetTriple.class), module.getTotalSize());
89+
}
90+
91+
private static boolean ignoreGlobal(GlobalVariable g) {
92+
String name = g.getName();
93+
/*
94+
* Dummy array to keep other globals alive during compilation and linking. We do not
95+
* initialize it as it's not relevant for execution, and it can cause problems during module
96+
* initialization (e.g. if a TLS variable is referenced).
97+
*/
98+
return name.equals("llvm.used") || name.equals("llvm.compiler.used");
8999
}
90100

91101
private void defineGlobals(List<GlobalVariable> globals, List<GlobalVariable> definedGlobals, List<GlobalVariable> externalGlobals, List<GlobalVariable> threadLocalGlobals) {
92102
for (GlobalVariable global : globals) {
103+
if (ignoreGlobal(global)) {
104+
continue;
105+
}
93106
if (global.isExternal()) {
94107
externalGlobals.add(global);
95108
} else {

sulong/projects/com.oracle.truffle.llvm.parser/src/com/oracle/truffle/llvm/parser/LLVMParserResult.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public final class LLVMParserResult {
5757
List<GlobalVariable> threadLocalGlobals,
5858
int threadLocalGlobalObjectCounter,
5959
DataLayout dataLayout,
60-
TargetTriple targetTriple) {
60+
TargetTriple targetTriple,
61+
int totalSize) {
6162
this.runtime = runtime;
6263
this.definedFunctions = definedFunctions;
6364
this.externalFunctions = externalFunctions;
@@ -67,7 +68,8 @@ public final class LLVMParserResult {
6768
this.externalGlobals = externalGlobals;
6869
this.dataLayout = dataLayout;
6970
this.targetTriple = targetTriple;
70-
this.symbolTableSize = definedFunctions.size() + externalFunctions.size() + definedGlobals.size() + externalGlobals.size() + threadLocalGlobals.size();
71+
assert totalSize >= definedFunctions.size() + externalFunctions.size() + definedGlobals.size() + externalGlobals.size() + threadLocalGlobals.size();
72+
this.symbolTableSize = totalSize;
7173
}
7274

7375
public LLVMParserRuntime getRuntime() {

sulong/projects/com.oracle.truffle.llvm.parser/src/com/oracle/truffle/llvm/parser/listeners/Module.java

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public final class Module implements ParserListener {
8686
index = new AtomicInteger(0);
8787
}
8888

89+
@Override
90+
public void exit() {
91+
module.setTotalSize(index.get());
92+
}
93+
8994
// private static final int STRTAB_RECORD_OFFSET = 2;
9095
// private static final int STRTAB_RECORD_OFFSET_INDEX = 0;
9196
// private static final int STRTAB_RECORD_LENGTH_INDEX = 1;

sulong/projects/com.oracle.truffle.llvm.parser/src/com/oracle/truffle/llvm/parser/model/ModelModule.java

+9
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public final class ModelModule {
6565
private String targetDataLayout = defaultLayout;
6666
private DebugInfoFunctionProcessor functionProcessor = null;
6767
private final ArrayList<LLVMSourceFileReference> sourceFiles = new ArrayList<>();
68+
private int totalSize;
6869

6970
public ModelModule() {
7071
}
@@ -170,4 +171,12 @@ public <TI extends TargetInformation> TI getTargetInformation(Class<TI> targetIn
170171
}
171172
return null;
172173
}
174+
175+
public void setTotalSize(int index) {
176+
totalSize = index;
177+
}
178+
179+
public int getTotalSize() {
180+
return totalSize;
181+
}
173182
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates.
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without modification, are
7+
* permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice, this list of
10+
* conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13+
* conditions and the following disclaimer in the documentation and/or other materials provided
14+
* with the distribution.
15+
*
16+
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to
17+
* endorse or promote products derived from this software without specific prior written
18+
* permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
21+
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28+
* OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
#include <stdlib.h>
31+
32+
/* On some platforms this TLS variable will end up being referenced by the
33+
* global @llvm.used or @llvm.compiler.used array in the final bitcode. This
34+
* can cause problems during global variable initialization. */
35+
36+
__attribute__((used)) __thread int tls_val = 4;
37+
38+
int main(void) {
39+
if (tls_val != 4) {
40+
abort();
41+
}
42+
return 0;
43+
}

0 commit comments

Comments
 (0)