使用bazel制作共享对象时无法使用全局

时间:2019-03-31 12:05:08

标签: c gcc clang bazel

当我尝试使用bazel编译共享库时遇到编译错误,其中源之一是目标文件,并且使用clang编译。 具体来说,我会收到此错误

`usec_delay' can not be used when making a shared object; recompile with -fPIC
error adding symbols: Bad value

usec_delay是一个全局变量。

这是我要创建目标文件的步骤。

  1. 使用clang将源代码编译为llvm位代码

    clang -c -emit-llvm example_src.cc

  2. 将位码编译为汇编

    llc example_src.bc

  3. 将程序集编译为对象

    clang -c example_src.s -o example_src.o

example_src.o然后在bazel BUILD文件中用作源。

当我用gcc编译相同的示例源代码时,编译成功。即

g++ -S example_src.cc
g++ -c example_src.s

我尝试将-fPIC添加到bazel的copts中,但是问题仍然存在。对于g ++,我什至不需要此标志,没有它就可以工作。

BUILD文件中完成编译的部分如下:

sim_enclave(
    name = "example.so",
    srcs = [
        "example_enclave.cc",
        "example_src.o",
        "interface_selectors.h",
    ],
    copts = ASYLO_DEFAULT_COPTS,
    deps = [
        "//asylo/platform/primitives",
        "//asylo/util:status_macros",
    ],
)

sim_enclave是在asylo中定义的,并在下面使用sgx_enclave规则:

def sgx_enclave(
        name,
        config = "@linux_sgx//:enclave_debug_config",
        testonly = 0,
        **kwargs):
    """Build rule for creating SGX enclave shared object files signed for testing.

    The enclave is signed with test key stored in
    @linux_sgx//:enclave_test_private.pem.

    This macro creates two build targets:
      1) name_unsigned.so: cc_binary that builds the unsigned enclave.
      2) name: internal signing rule that (debug) signs name_unsigned.so.

    Args:
      name: The debug-signed enclave target name.
      config: An sgx_enclave_configuration rule.
      testonly: 0 or 1, set to 1 if the target is only used in tests.
      **kwargs: cc_binary arguments.
    Returns:
      string: The name of the signed enclave.
    """
    unsigned_name = _unsigned_enclave_name(name)

    _sgx_unsigned_enclave(
        name = unsigned_name,
        testonly = testonly,
        **kwargs
    )

    kwargs = {k: v for k, v in kwargs.items() if k in ["visibility"]}
    _sgx_debug_sign_enclave(
        name = name,
        unsigned = unsigned_name,
        config = config,
        testonly = testonly,
        **kwargs
    )

    return name

已定义_sgx_unsigned_enclave:

def _sgx_unsigned_enclave(
        name,
        **kwargs):
    """Build rule for creating an unsigned SGX enclave shared object file.

    Args:
      name: The enclave target name.
      **kwargs: cc_binary arguments.
    """

    # Append enclave specific arguments.
    kwargs["copts"] = kwargs.get("copts", []) + [
        "-Iexternal/linux_sgx/common/inc/",
        "-fstack-protector",
    ]

    lds_label = "@linux_sgx//:enclave_sim.lds"
    kwargs["features"] = kwargs.get("features", []) + [
        "fully_static_link",
    ]
    kwargs["linkopts"] = kwargs.get("linkopts", []) + [
        "-Wl,-Bstatic",
        "-Wl,-Bsymbolic",
        "-Wl,-pie,-eenclave_entry",
        "-Wl,--defsym,__ImageBase=0",
        "-Wl,--export-dynamic",
        "-Wl,--version-script",
        "$(location %s)" % lds_label,
    ]
    kwargs["linkshared"] = 1
    kwargs["linkstatic"] = 1
    kwargs["deps"] = kwargs.get("deps", []) + [
        lds_label,
        "@linux_sgx//:intel_runtime",
    ]

    # Create rule to build unsigned enclave.
    native.cc_binary(
        name = name,
        **kwargs
    )

和_sgx_debug_sign_enclave

_sgx_debug_sign_enclave = rule(
    implementation = _sgx_debug_sign_enclave_impl,
    executable = True,
    attrs = {
        "config": attr.label(
            mandatory = True,
            allow_single_file = True,
            providers = [SGXEnclaveConfigInfo],
        ),
        "unsigned": attr.label(allow_single_file = True),
        "_key": attr.label(
            default = Label(
                "@linux_sgx//:enclave_test_private.pem",
            ),
            allow_single_file = True,
        ),
        "_sign_tool": attr.label(
            default = Label("@linux_sgx//:sgx_sign_tool"),
            allow_single_file = True,
            executable = True,
            cfg = "host",
        ),
    },
)

构建bazel的详细输出是 enter image description here

0 个答案:

没有答案