Skip to content

Compiling with -fPIC on Linux #119

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

Closed
blaugold opened this issue Sep 1, 2023 · 2 comments · Fixed by #121
Closed

Compiling with -fPIC on Linux #119

blaugold opened this issue Sep 1, 2023 · 2 comments · Fixed by #121

Comments

@blaugold
Copy link
Contributor

blaugold commented Sep 1, 2023

While playing around with building a package that exposes an SQLite Dart API, using the new native assets feature, I found that it's currently not possible to build SQLite with native_toolchain_c on Linux:

Unhandled exception:
ProcessException: Full command string: '/usr/lib/llvm-14/bin/clang /home/runner/work/sqlite_dart_native/sqlite_dart_native/src/sqlite/sqlite3.c --shared -o /home/runner/work/sqlite_dart_native/sqlite_dart_native/.dart_tool/native_assets_builder/fb4a407d42b5[8](https://github.com/blaugold/sqlite_dart_native/actions/runs/5906585326/job/16022913631#step:6:9)5687ec9d3b5a6c0949b/out/libsqlite.so -DRELEASE'.
Exit code: '1'.
For the output of the process check the logger output.
  Command: /usr/lib/llvm-14/bin/clang /home/runner/work/sqlite_dart_native/sqlite_dart_native/src/sqlite/sqlite3.c --shared -o /home/runner/work/sqlite_dart_native/sqlite_dart_native/.dart_tool/native_assets_builder/fb4a407d42b585687ec9d3b5a6c0949b/out/libsqlite.so -DRELEASE
#0      runProcess (package:native_toolchain_c/src/utils/run_process.dart:92:5)
<asynchronous suspension>
#1      RunCBuilder.runClangLike (package:native_toolchain_c/src/cbuilder/run_cbuilder.dart:102:5)
<asynchronous suspension>
#2      RunCBuilder.run (package:native_toolchain_c/src/cbuilder/run_cbuilder.dart:83:7)
<asynchronous suspension>
#3      CBuilder.run (package:native_toolchain_c/src/cbuilder/cbuilder.dart:116:7)
<asynchronous suspension>
#4      main (file:///home/runner/work/sqlite_dart_native/sqlite_dart_native/build.dart:16:3)
<asynchronous suspension>
Building native assets for package:sqlite_dart_native failed.
build.dart returned with exit code: 255.
To reproduce run:
/opt/hostedtoolcache/dart/3.2.0-74.0.dev/x64/bin/dart --packages=/home/runner/work/sqlite_dart_native/sqlite_dart_native/.dart_tool/package_config.json /home/runner/work/sqlite_dart_native/sqlite_dart_native/build.dart --config=/home/runner/work/sqlite_dart_native/sqlite_dart_native/.dart_tool/native_assets_builder/fb4a407d42b585687ec9d3b5a6c0949b/config.yaml
stderr:
Unhandled exception:
ProcessException: Full command string: '/usr/lib/llvm-14/bin/clang /home/runner/work/sqlite_dart_native/sqlite_dart_native/src/sqlite/sqlite3.c --shared -o /home/runner/work/sqlite_dart_native/sqlite_dart_native/.dart_tool/native_assets_builder/fb4a407d42b585687ec9d3b5a6c0949b/out/libsqlite.so -DRELEASE'.
Exit code: '1'.
For the output of the process check the logger output.
  Command: /usr/lib/llvm-14/bin/clang /home/runner/work/sqlite_dart_native/sqlite_dart_native/src/sqlite/sqlite3.c --shared -o /home/runner/work/sqlite_dart_native/sqlite_dart_native/.dart_tool/native_assets_builder/fb4a407d42b585687ec[9](https://github.com/blaugold/sqlite_dart_native/actions/runs/5906585326/job/16022913631#step:6:10)d3b5a6c0949b/out/libsqlite.so -DRELEASE
#0      runProcess (package:native_toolchain_c/src/utils/run_process.dart:92:5)
<asynchronous suspension>
#1      RunCBuilder.runClangLike (package:native_toolchain_c/src/cbuilder/run_cbuilder.dart:[10](https://github.com/blaugold/sqlite_dart_native/actions/runs/5906585326/job/16022913631#step:6:11)2:5)
<asynchronous suspension>
#2      RunCBuilder.run (package:native_toolchain_c/src/cbuilder/run_cbuilder.dart:83:7)
<asynchronous suspension>
#3      CBuilder.run (package:native_toolchain_c/src/cbuilder/cbuilder.dart:[11](https://github.com/blaugold/sqlite_dart_native/actions/runs/5906585326/job/16022913631#step:6:12)6:7)
<asynchronous suspension>
#4      main (file:///home/runner/work/sqlite_dart_native/sqlite_dart_native/build.dart:16:3)
<asynchronous suspension>

stdout:
Running `which clang`.
Running `/usr/lib/llvm-[14](https://github.com/blaugold/sqlite_dart_native/actions/runs/5906585326/job/16022913631#step:6:15)/bin/clang --version`.
Running `/usr/lib/llvm-14/bin/clang --version`.
Running `/usr/lib/llvm-14/bin/clang /home/runner/work/sqlite_dart_native/sqlite_dart_native/src/sqlite/sqlite3.c --shared -o /home/runner/work/sqlite_dart_native/sqlite_dart_native/.dart_tool/native_assets_builder/fb4a407d42b585687ec9d3b5a6c0949b/out/libsqlite.so -DRELEASE`.
/usr/bin/ld: /tmp/sqlite3-547e69.o: warning: relocation against `sqlite3_temp_directory' in read-only section `.text'
/usr/bin/ld: /tmp/sqlite3-547e69.o: relocation R_X86_64_PC[32](https://github.com/blaugold/sqlite_dart_native/actions/runs/5906585326/job/16022913631#step:6:33) against symbol `sqlite3_version' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
clang: error: linker command failed with exit code 1 (use -v to see invocation)

        
Error: Compiling native assets failed.

As far as I understand it, any non-trivial shared library needs to be compiled with -fPIC, but at least the version of Clang that comes with Ubuntu 22.04 does not enable it by default on Linux. Most distributions of GCC seem to enable it by default.

Here is a discussion regarding this difference on the LLVM forum.

@dcharkes
Copy link
Collaborator

dcharkes commented Sep 1, 2023

  final cbuilder = CBuilder.library(
    name: packageName,
    assetId: 'package:$packageName/${packageName}.dart',
    sources: [
      'src/$packageName.c',
    ],
  );

This should take some argument for compiler flags. (And probably also include paths, defines #61, and possibly all kinds of other things.)

PRs (and design suggestions) are welcome!

Edit: we can take inspiration from https://docs.rs/cc/latest/cc/ which does exactly the same thing in the rust eco system. (The cc crate has a fluent API rather than one with named arguments, the latter is more dart-like.)

@blaugold
Copy link
Contributor Author

blaugold commented Sep 2, 2023

This should take some argument for compiler flags. (And probably also include paths, defines #61, and possibly all kinds of other things.)

I've started by taking a stab at macro defines (#120). Supporting compiler flags generally will definitely also be very useful. But I feel like it would be good to provide sensible defaults for PIC/PIE flags (#121 ). The cc crate does that, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants