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

Support linking with LLVM shared libraries #1593

Merged
merged 1 commit into from
Feb 16, 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
1 change: 1 addition & 0 deletions bap-llvm.opam.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ build: [
[
"ocaml" "tools/configure.ml"
"--with-llvm-config=%{conf-bap-llvm:config}%"
"--%{llvm-shared?disable:enable}%-llvm-static"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, didn't know about this syntax, is it new? (I mean will it work with opam 2.0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was added in opam 1.2.1.

]
[
"dune"
Expand Down
8 changes: 6 additions & 2 deletions lib/bap_llvm/config/llvm_configurator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ let args = [


let () = C.main ~args ~name:"bap-llvm" @@ fun self ->
let linkmode =
"--link-" ^
(String.strip @@
C.Process.run_capture_exn self llvm_config ["--shared-mode"]) in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the --shared-mode command-line option is present on all llvm versions that we support? Maybe, it is better to be more defensive here (both against previous and future versions) and anticipate possible failures by sticking with the static mode by default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It exists at least as far back as llvm 4.0. I don't know if there's a great solution here in general, since the installed version of llvm could change since bap was built, and then we might have a mismatch of the linking mode anyway.

C.Flags.write_sexp "link.flags" @@ List.concat [
llvm self ["--link-static"; "--ldflags"];
llvm self (["--link-static"; "--libs"] @ llvm_components);
llvm self [linkmode; "--ldflags"];
llvm self ([linkmode; "--libs"] @ llvm_components);
["-lstdc++"; "-lcurses"; "-lzstd"];
];
C.Flags.write_sexp "cxx.flags" @@ List.concat [
Expand Down
3 changes: 3 additions & 0 deletions lib/bap_llvm/llvm_disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#if LLVM_VERSION_MAJOR >= 12
#include <llvm/Support/Process.h>
#include <llvm/Support/StringSaver.h>
#if LLVM_VERSION_MAJOR <= 16
#include <llvm/ADT/Optional.h>
#endif
#endif
#include <llvm-c/Target.h>

Expand Down
Loading