Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop flat namespace option #1109

Merged
merged 6 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions bin/llvm-kompile-clang
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
flags=(
"-L@BREW_PREFIX@/opt/libffi/lib"
"-L@BREW_PREFIX@/lib"
"-L@LLVM_LIBRARY_DIR@"
"-Wl,-u,_table_getArgumentSortsForTag"
"-I" "@BREW_PREFIX@/include"
)
Expand All @@ -158,6 +157,19 @@ else
set_visibility_hidden="$LIBDIR/libSetVisibilityHidden.so"
fi

# On macOS, we get libunwind supplied as part of the developer tools in the OS,
# and so don't need to link it directly. If we instead try to explictly link
# against the libunwind that's part of Homebrew-supplied LLVM, it's easy to end
# up in a situation where exceptions thrown in a shared library are not
# compatible with the unwinding machinery in the main application binary. This
# then manifests as BAD_ACCESS errors (or similar) when an exception is thrown,
# _even if the exception should in principle be caught_.
if [[ "$OSTYPE" == "darwin"* ]]; then
libunwind=""
else
libunwind="-lunwind"
fi

# When building the Python AST module, there's no runtime and no main file, so
# we skip this entire step. The library code is just C++, so we can skip
# straight to invoking the C++ compiler.
Expand Down Expand Up @@ -207,7 +219,7 @@ if [[ "$OSTYPE" == "darwin"* ]]; then
start_whole_archive="-force_load"
end_whole_archive=""

flags+=("-Wl,-flat_namespace" "-Wl,-undefined" "-Wl,dynamic_lookup")
Robertorosmaninho marked this conversation as resolved.
Show resolved Hide resolved
flags+=("-Wl,-undefined" "-Wl,dynamic_lookup")
else
start_whole_archive="-Wl,--whole-archive"
end_whole_archive="-Wl,--no-whole-archive"
Expand All @@ -218,9 +230,13 @@ if [ "$main" = "static" ]; then
elif [[ "$main" =~ "python" ]]; then
# Don't link jemalloc when building a python library; it clashes with the
# pymalloc implementation that Python expects you to use.
all_libraries=("${libraries[@]}" "-lgmp" "-lgmpxx" "-lmpfr" "-lpthread" "-ldl" "-lffi" "-lunwind")
all_libraries=("${libraries[@]}" "-lgmp" "-lgmpxx" "-lmpfr" "-lpthread" "-ldl" "-lffi" "$libunwind")
flags+=("-fPIC" "-shared" "-I${INCDIR}" "-fvisibility=hidden")

if [[ "$OSTYPE" == "darwin"* ]]; then
flags+=("-Wl,-flat_namespace")
fi

Baltoli marked this conversation as resolved.
Show resolved Hide resolved
read -r -a python_include_flags <<< "$("${python_cmd}" -m pybind11 --includes)"
flags+=("${python_include_flags[@]}")

Expand All @@ -240,11 +256,11 @@ elif [ "$main" = "c" ]; then

# Avoid jemalloc for similar reasons as Python; we don't know who is loading
# this library so don't want to impose it.
all_libraries=("${libraries[@]}" "-lgmp" "-lgmpxx" "-lmpfr" "-lpthread" "-ldl" "-lffi" "-lunwind")
all_libraries=("${libraries[@]}" "-lgmp" "-lgmpxx" "-lmpfr" "-lpthread" "-ldl" "-lffi" "$libunwind")
flags+=("-fPIC" "-shared" "$start_whole_archive" "$LIBDIR/libkllvmcruntime.a" "$end_whole_archive")
clangpp_args+=("-o" "${output_file}")
else
all_libraries=("${libraries[@]}" "-lgmp" "-lgmpxx" "-lmpfr" "-lpthread" "-ldl" "-lffi" "-ljemalloc" "-lunwind")
all_libraries=("${libraries[@]}" "-lgmp" "-lgmpxx" "-lmpfr" "-lpthread" "-ldl" "-lffi" "-ljemalloc" "$libunwind")
fi

if $link; then
Expand Down
1 change: 1 addition & 0 deletions lib/codegen/ApplyPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ void generate_object_file(llvm::Module &mod, llvm::raw_ostream &os) {

auto features_string = features.getString();
auto options = TargetOptions{};
options.GuaranteedTailCallOpt = true;
Baltoli marked this conversation as resolved.
Show resolved Hide resolved

#if LLVM_VERSION_MAJOR >= 16
std::optional<CodeModel::Model> model = std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion nix/llvm-backend.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ stdenv.mkDerivation {
--replace '"-liconv"' '"-L${libiconv}/lib" "-liconv"' \
--replace '"-lncurses"' '"-L${ncurses}/lib" "-lncurses"' \
--replace '"-ltinfo"' '"-L${ncurses}/lib" "-ltinfo"' \
--replace '"-lunwind"' '"-L${libunwind}/lib" "-lunwind"' \
--replace '"$libunwind"' '"-L${libunwind}/lib" "-lunwind"' \
Robertorosmaninho marked this conversation as resolved.
Show resolved Hide resolved
--replace '"-L@BREW_PREFIX@/opt/libffi/lib"' ' ' \
--replace '"-L@LLVM_LIBRARY_DIR@"' ' ' \
--replace '-L@BREW_PREFIX@/lib' '-L${libcxx}/lib' \
Expand Down
40 changes: 40 additions & 0 deletions test/c/Inputs/flat-namespace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "api.h"

#include <stdio.h>
#include <stdlib.h>

/**
* The K program corresponding to this test will evaluate the `Int2Bytes` hooked
* function when it is run; this hook ends up allocating and freeing a temporary
* MPZ integer locally. If we're in a situation where `-flat_namespace` has been
* enabled on macOS, it's possible for the hook to end up resolving
* `__gmpz_clear` to a symbol defined in the host binary (rather than libgmp's
* dynamic library!).
*
* This originally manifested when the HB booster loaded a C bindings library
* (the booster statically links libgmp and so contains a symbol with this
* name); this test is a minimised reproduction of that issue.
*/
void __gmpz_clear(void *p) {
abort();
}

int main(int argc, char **argv) {
if (argc <= 1) {
return 1;
}

struct kllvm_c_api api = load_c_api(argv[1]);

api.kllvm_init();

kore_sort *sort_foo = api.kore_composite_sort_new("SortFoo");
kore_pattern *pat = api.kore_composite_pattern_new("Lblfoo");

kore_pattern *input = api.kore_pattern_make_interpreter_input(pat, sort_foo);

block *term = api.kore_pattern_construct(input);
block *after = api.take_steps(-1, term);

printf("%s", api.kore_block_dump(after));
}
Loading
Loading