buck icon indicating copy to clipboard operation
buck copied to clipboard

PIE/non-PIE mixups when integrating Rust and C

Open v-jizhang opened this issue 5 years ago • 0 comments

Repro project: $cat BUCK

linker_flags = []

# MacOS build fix
linker_flags += [
    "-lresolv",
]

# On Linux, Rust's standard library is built by default with threading support
# and with stacktrace symbolication support. Link libraries required for these
# features.
linker_flags += [
    "-ldl",
    "-lpthread",
]

cxx_binary(
    name = "hello",
    #link_style = "shared",
    srcs = ["main.c"],
    link_style = "static",
    linker_flags = linker_flags,
    deps = [":helloer"],
)

cxx_binary(
    name = "hello-shared",
    srcs = ["main.c"],
    link_style = "shared",
    linker_flags = linker_flags,
    deps = [":helloer"],
)

rust_library(
    name = "helloer",
    srcs = ["helloer.rs"],
    deps = [":morehello"],
)

rust_library(
    name = "morehello",
    srcs = ["morehello.rs"],
    deps = [":morehellodep"],
)

cxx_library(
    name = "morehellodep",
    srcs = ["morehellodep.c"],
)

$cat helloer.rs

extern crate morehello;

#[no_mangle]
pub extern "C" fn helloer() {
    println!("I'm printing hello!");
    morehello::helloer();
}

$cat main.c

#include <stdio.h>

extern void helloer(void);

int main() {
    printf("Calling helloer\n");
    helloer();
    printf("Helloer called\n");
    return 0;
}

$cat morehello.rs

extern "C" {
    fn evenmorehello();
}

pub fn helloer() {
    println!("I'm saying \"hello\" again!");
    unsafe { evenmorehello() };
}

$cat morehellodep.c

#include <stdio.h>

extern void evenmorehello(void) {
    printf("Hello back from C-land\n");
}

On Linux(Ubuntu 18.04.4 LTS), the shared target //:hello-shared builds but the static target //:hello won’t. The error reads “stderr: /usr/bin/ld: buck-out/gen/helloer#default,static/libhelloer-795ec4d5b6ad4640.a(helloer-795ec4d5b6ad4640.helloer.eqqwwzev-cgu.0.rcgu.o): relocation R_X86_64_32S against `.rodata..L__unnamed_1' can not be used when making a PIE object; recompile with -fPIC” From Jeremy Fitzhardinge: "I think the problem is that Buck thinks it's making a plain static non-PIE executable so it has build non-PIC libraries, whereas rustc thinks its generating PIE and so needs PIC libraries. The mismatch is likely a bug on Buck's side, and I seem to remember there were once issues with it (it = PIE/non-PIE mixups) I haven't seen any problems with this in fbcode, but its quite likely we're not hitting this path"

v-jizhang avatar Apr 03 '20 20:04 v-jizhang