Skip to content

Commit

Permalink
[GR-29187] External LLVMFunction TLS init function for ... cannot be …
Browse files Browse the repository at this point in the history
…found.

PullRequest: graal/8264
  • Loading branch information
Palez committed Feb 23, 2021
2 parents 58ab7f6 + 2a2a73a commit 1ddc1c2
Show file tree
Hide file tree
Showing 14 changed files with 127 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ public LLVMExpressionNode createInlineAssemblerExpression(String asmExpression,
}
LLVMIRFunction function = new LLVMIRFunction(LLVMLanguage.createCallTarget(assemblyRoot), null);
LLVMFunction functionDetail = LLVMFunction.create("<asm>", function, new FunctionType(MetaType.UNKNOWN, 0, false), LLVMSymbol.INVALID_INDEX, LLVMSymbol.INVALID_INDEX,
false, assemblyRoot.getName());
false, assemblyRoot.getName(), false);
// The function descriptor for the inline assembly does not require a language.
LLVMFunctionDescriptor asm = new LLVMFunctionDescriptor(functionDetail, new LLVMFunctionCode(functionDetail));
LLVMManagedPointerLiteralNode asmFunction = LLVMManagedPointerLiteralNodeGen.create(LLVMManagedPointer.create(asm));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -110,7 +110,8 @@ private void defineAliases(List<GlobalAlias> aliases) {
private void defineGlobal(GlobalVariable global) {
assert !global.isExternal();
// handle the file scope
LLVMGlobal globalSymbol = LLVMGlobal.create(global.getName(), global.getType(), global.getSourceSymbol(), global.isReadOnly(), global.getIndex(), runtime.getBitcodeID(), global.isExported());
LLVMGlobal globalSymbol = LLVMGlobal.create(global.getName(), global.getType(), global.getSourceSymbol(), global.isReadOnly(), global.getIndex(), runtime.getBitcodeID(), global.isExported(),
global.isExternalWeak());
runtime.getFileScope().register(globalSymbol);
}

Expand All @@ -122,7 +123,7 @@ private void defineFunction(FunctionSymbol functionSymbol, ModelModule model, Da
model.getFunctionProcessor(), dataLayout);
Function function = new LazyLLVMIRFunction(lazyConverter);
LLVMFunction llvmFunction = LLVMFunction.create(functionSymbol.getName(), function, functionSymbol.getType(), runtime.getBitcodeID(), functionSymbol.getIndex(),
functionDefinition.isExported(), runtime.getFile().getPath());
functionDefinition.isExported(), runtime.getFile().getPath(), functionDefinition.isExternalWeak());
lazyConverter.setRootFunction(llvmFunction);
runtime.getFileScope().register(llvmFunction);
final boolean cxxInterop = LLVMLanguage.getContext().getEnv().getOptions().get(SulongEngineOption.CXX_INTEROP);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -94,4 +94,6 @@ public final int getIndex() {

public abstract boolean isExternal();

public abstract boolean isExternalWeak();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -84,6 +84,11 @@ public boolean isExternal() {
return true;
}

@Override
public boolean isExternalWeak() {
return getLinkage() == Linkage.EXTERN_WEAK;
}

@Override
public LLVMExpressionNode createNode(LLVMParserRuntime runtime, DataLayout dataLayout, GetStackSpaceFactory stackFactory) {
LLVMFunction value = runtime.lookupFunction(getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -224,6 +224,11 @@ public boolean isExternal() {
return Linkage.isExternal(getLinkage());
}

@Override
public boolean isExternalWeak() {
return getLinkage() == Linkage.EXTERN_WEAK;
}

@Override
public LLVMExpressionNode createNode(LLVMParserRuntime runtime, DataLayout dataLayout, GetStackSpaceFactory stackFactory) {
LLVMFunction value = runtime.lookupFunction(getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -133,4 +133,9 @@ public boolean isOverridable() {
public boolean isExternal() {
return getInitialiser() == 0 && isExported();
}

@Override
public boolean isExternalWeak() {
return getLinkage() == Linkage.EXTERN_WEAK;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates.
* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -40,7 +40,7 @@ public class LLVMAlias extends LLVMSymbol {
@CompilationFinal private LLVMSymbol target;

public LLVMAlias(String name, LLVMSymbol target, boolean exported) {
super(name, LLVMSymbol.INVALID_ID, LLVMSymbol.INVALID_ID, exported);
super(name, LLVMSymbol.INVALID_ID, LLVMSymbol.INVALID_ID, exported, false);
setTarget(target);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public final class LLVMFunction extends LLVMSymbol {
private final Assumption fixedCodeAssumption = Truffle.getRuntime().createAssumption();
@CompilationFinal private LLVMFunctionCode fixedCode;

public static LLVMFunction create(String name, Function function, FunctionType type, int bitcodeID, int symbolIndex, boolean exported, String path) {
return new LLVMFunction(name, function, type, bitcodeID, symbolIndex, exported, path);
public static LLVMFunction create(String name, Function function, FunctionType type, int bitcodeID, int symbolIndex, boolean exported, String path, boolean externalWeak) {
return new LLVMFunction(name, function, type, bitcodeID, symbolIndex, exported, path, externalWeak);
}

public LLVMFunction(String name, Function function, FunctionType type, int bitcodeID, int symbolIndex, boolean exported, String path) {
super(name, bitcodeID, symbolIndex, exported);
public LLVMFunction(String name, Function function, FunctionType type, int bitcodeID, int symbolIndex, boolean exported, String path, boolean externalWeak) {
super(name, bitcodeID, symbolIndex, exported, externalWeak);
this.type = type;
this.function = function;
this.path = path;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2020, Oracle and/or its affiliates.
* Copyright (c) 2018, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -42,18 +42,20 @@ public abstract class LLVMSymbol {
private final int moduleId;
private final int symbolIndex;
private final boolean exported;
private final boolean externalWeak;

// Index for non-parsed symbols, such as alias, and function symbol for inline assembly.
public static final int INVALID_INDEX = -1;

// ID for non-parsed symbols, such as alias, function symbol for inline assembly.
public static final int INVALID_ID = -1;

public LLVMSymbol(String name, int bitcodeID, int symbolIndex, boolean exported) {
public LLVMSymbol(String name, int bitcodeID, int symbolIndex, boolean exported, boolean externalWeak) {
this.name = name;
this.moduleId = bitcodeID;
this.symbolIndex = symbolIndex;
this.exported = exported;
this.externalWeak = externalWeak;
}

public final String getName() {
Expand All @@ -69,6 +71,10 @@ public final boolean isExported() {
return exported;
}

public final boolean isExternalWeak() {
return externalWeak;
}

/**
* Get the unique index of the symbol. The index is assigned during parsing. Symbols that are
* not created from parsing or that are alias have the value of -1.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -51,22 +51,22 @@ public final class LLVMGlobal extends LLVMSymbol {
@CompilationFinal private boolean interopTypeCached;
@CompilationFinal private LLVMInteropType interopType;

public static LLVMGlobal create(String name, PointerType type, LLVMSourceSymbol sourceSymbol, boolean readOnly, int index, int id, boolean exported) {
public static LLVMGlobal create(String name, PointerType type, LLVMSourceSymbol sourceSymbol, boolean readOnly, int index, int id, boolean exported, boolean externalWeak) {
if (index < 0) {
throw new AssertionError("Invalid index for LLVM global: " + index);
}
if (id < 0) {
throw new AssertionError("Invalid index for LLVM global: " + id);
}
return new LLVMGlobal(name, type, sourceSymbol, readOnly, index, id, exported);
return new LLVMGlobal(name, type, sourceSymbol, readOnly, index, id, exported, externalWeak);
}

public static LLVMGlobal createUnavailable(String name) {
return new LLVMGlobal(name + " (unavailable)", PointerType.VOID, null, true, -1, -1, false);
return new LLVMGlobal(name + " (unavailable)", PointerType.VOID, null, true, -1, -1, false, false);
}

private LLVMGlobal(String name, PointerType type, LLVMSourceSymbol sourceSymbol, boolean readOnly, int globalIndex, int moduleId, boolean exported) {
super(name, moduleId, globalIndex, exported);
private LLVMGlobal(String name, PointerType type, LLVMSourceSymbol sourceSymbol, boolean readOnly, int globalIndex, int moduleId, boolean exported, boolean externalWeak) {
super(name, moduleId, globalIndex, exported, externalWeak);
this.name = name;
this.type = type;
this.sourceSymbol = sourceSymbol;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020, Oracle and/or its affiliates.
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -215,7 +215,7 @@ private Object decorateForeign(Object function, LLVMFunctionDescriptor wrapperFu

private static Object registerRoot(String path, FunctionType newFunctionType, DecoratedRoot decoratedRoot) {
LLVMIRFunction function = new LLVMIRFunction(LLVMLanguage.createCallTarget(decoratedRoot), null);
LLVMFunction functionDetail = LLVMFunction.create("<wrapper>", function, newFunctionType, LLVMSymbol.INVALID_INDEX, LLVMSymbol.INVALID_INDEX, false, path);
LLVMFunction functionDetail = LLVMFunction.create("<wrapper>", function, newFunctionType, LLVMSymbol.INVALID_INDEX, LLVMSymbol.INVALID_INDEX, false, path, false);
LLVMFunctionDescriptor wrappedFunction = new LLVMFunctionDescriptor(functionDetail, new LLVMFunctionCode(functionDetail));
return LLVMManagedPointer.create(wrappedFunction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private static void createNewFunction(LLVMScope scope, String originalName, LLVM
String.format("The symbol %s could not be imported because the symbol %s was not found in library %s", external.getName(), originalName, lib));
}
LLVMFunction newFunction = LLVMFunction.create(name, originalSymbol.getFunction(), originalSymbol.getType(),
parserResult.getRuntime().getBitcodeID(), external.getIndex(), external.isExported(), parserResult.getRuntime().getFile().getPath());
parserResult.getRuntime().getBitcodeID(), external.getIndex(), external.isExported(), parserResult.getRuntime().getFile().getPath(), external.isExternalWeak());
LLVMScope fileScope = parserResult.getRuntime().getFileScope();
fileScope.register(newFunction);
it.remove();
Expand Down Expand Up @@ -485,14 +485,14 @@ private static void addExternalSymbolsToScopes(LLVMParserResult parserResult) {
for (FunctionSymbol function : parserResult.getExternalFunctions()) {
if (!fileScope.contains(function.getName())) {
fileScope.register(LLVMFunction.create(function.getName(), new LLVMFunctionCode.UnresolvedFunction(), function.getType(), parserResult.getRuntime().getBitcodeID(),
function.getIndex(), false, parserResult.getRuntime().getFile().getPath()));
function.getIndex(), false, parserResult.getRuntime().getFile().getPath(), function.isExternalWeak()));
}
}
for (GlobalVariable global : parserResult.getExternalGlobals()) {
if (!fileScope.contains(global.getName())) {
fileScope.register(
LLVMGlobal.create(global.getName(), global.getType(), global.getSourceSymbol(), global.isReadOnly(), global.getIndex(), parserResult.getRuntime().getBitcodeID(),
false));
false, global.isExternalWeak()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates.
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
Expand Down Expand Up @@ -197,6 +197,16 @@ abstract static class AllocExternalGlobalNode extends AllocExistingGlobalSymbols
super(symbol);
}

@Specialization(guards = {"localScope.get(symbol.getName()) == null", "globalScope.get(symbol.getName()) == null",
"symbol.isGlobalVariable()", "symbol.isExternalWeak()"})
LLVMPointer allocateExternalWeakGlobal(@SuppressWarnings("unused") LLVMLocalScope localScope,
@SuppressWarnings("unused") LLVMScope globalScope,
@SuppressWarnings("unused") LLVMIntrinsicProvider intrinsicProvider,
@SuppressWarnings("unused") NativeContextExtension nativeContextExtension,
@SuppressWarnings("unused") LLVMContext context) {
return LLVMNativePointer.createNull();
}

@TruffleBoundary
@Specialization(guards = {"localScope.get(symbol.getName()) == null", "globalScope.get(symbol.getName()) == null",
"!intrinsicProvider.isIntrinsified(symbol.getName())", "nativeContextExtension != null",
Expand Down Expand Up @@ -229,6 +239,16 @@ abstract static class AllocExternalFunctionNode extends AllocExistingGlobalSymbo
this.nodeFactory = nodeFactory;
}

@Specialization(guards = {"localScope.get(symbol.getName()) == null", "globalScope.get(symbol.getName()) == null",
"symbol.isFunction()", "symbol.isExternalWeak()"})
LLVMPointer allocateExternalWeakFunction(@SuppressWarnings("unused") LLVMLocalScope localScope,
@SuppressWarnings("unused") LLVMScope globalScope,
@SuppressWarnings("unused") LLVMIntrinsicProvider intrinsicProvider,
@SuppressWarnings("unused") NativeContextExtension nativeContextExtension,
@SuppressWarnings("unused") LLVMContext context) {
return LLVMNativePointer.createNull();
}

@TruffleBoundary
@Specialization(guards = {"intrinsicProvider != null", "localScope.get(symbol.getName()) == null", "globalScope.get(symbol.getName()) == null",
"intrinsicProvider.isIntrinsified(symbol.getName())", "symbol.isFunction()"})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to
* endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>

#ifdef __linux__
__attribute__((weak)) int foo();
__attribute__((weak)) extern int bar;
#define BAR_ADDR (&bar)
#else
void *foo = NULL; //No weak external support for Darwin. This will pretend to be the same.
#define BAR_ADDR NULL
#endif

int main() {
if (foo) {
printf("External weak function is defined!\n");
} else {
printf("External weak function is NOT defined!\n");
}

if (BAR_ADDR) {
printf("External weak global is defined!\n");
} else {
printf("External weak global is NOT defined!\n");
}
return 0;
}

0 comments on commit 1ddc1c2

Please sign in to comment.