blob: 71de21e1c18e91b77235d10551b23ca88c90c983 [file] [log] [blame]
# Copyright 2019 The Fuchsia Authors
#
# Use of this source code is governed by a MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT
import("$zx/public/gn/config/levels.gni")
import("$zx/public/gn/config/standard.gni")
import("$zx/public/gn/test/zbi_test.gni")
import("$zx/public/gn/toolchain/c_utils.gni")
import("$zx/public/gn/toolchain/environment.gni")
import("$zx/public/gn/toolchain/environment_redirect.gni")
import("$zx/public/gn/zbi.gni")
# userboot gets its own toolchain for its special build requirements.
if (current_toolchain == default_toolchain) {
# Define the special toolchain itself only in the default toolchain.
foreach(cpu, standard_fuchsia_cpus) {
environment("userboot") {
cpu = cpu
configs += standard_fuchsia_configs + [
":userboot_config",
# TODO(48501): This is needed pervasively and not just
# in the linking target so its compile-time flags can
# work around fxb/48501. It's separate from
# userboot_config so it can be removed by other links
# reusing this environment, e.g. hermetic_module().
"$zx/public/gn/config:rodso",
]
# This doesn't get a separate shlib sub-toolchain, but the main one
# can do loadable_module().
solink = true
toolchain_args = {
if (optimize == "none" || optimize == "debug") {
# userboot doesn't stay sufficiently pure without optimization.
optimize = "regular"
}
# No runtime to print asserts, so can't compile them in.
assert_level = 0
}
# userboot can't use any instrumentation runtimes.
exclude_variant_tags = [ "instrumented" ]
tags = [ "hermetic" ]
}
}
} else if (toolchain.environment == "userboot") {
# Everything in userboot gets compiled this way.
config("userboot_config") {
configs = [
"$zx/public/gn/config:user",
"$zx/public/gn/config:static-libc++",
"$zx/public/gn/config:no_sanitizers",
"$zx/public/gn/config:no_undefined_symbols",
]
cflags = [
# -fPIE is the default in Clang, but not in GCC.
"-fpie",
# Everything is statically linked together with no PLT or GOT.
# No $inputs needed here since the depfile will list it.
"-include",
rebase_path("$zx/kernel/include/hidden.h", root_build_dir),
]
defines = [ "HIDDEN" ]
}
# This is the output of target ":gen-vdso-syms-ld", below.
vdso_syms_ld = "$target_gen_dir/vdso-syms.ld"
# userboot is a reentrant DSO (no writable segment) with an entry point.
loadable_module("userboot") {
sources = [
"bootdata.cc",
"bootfs.cc",
"loader-service.cc",
"option.cc",
"start.cc",
"userboot-elf.cc",
"util.cc",
"zx_panic.cc",
]
defines = [ "ZBI_TEST_SUCCESS_STRING=\"$zbi_test_success_string\"" ]
ldflags = [ "-Wl,-e,_start" ]
libs = [ vdso_syms_ld ]
deps = [
":gen-vdso-syms-ld",
"$zx/kernel/lib/userabi:headers",
"$zx/system/ulib/elf-psabi",
"$zx/system/ulib/elfload",
"$zx/system/ulib/fidl",
"$zx/system/ulib/hermetic-decompressor",
"$zx/system/ulib/ldmsg",
"$zx/system/ulib/processargs",
"$zx/system/ulib/zircon",
"$zx/system/ulib/zircon-internal",
"$zx/system/ulib/zx",
"$zx/third_party/ulib/musl/src/string:minimal_str",
"$zx/third_party/ulib/musl/src/string:stdmem",
]
if (zbi_compression_algorithm == "lz4f") {
defines += [ "ZBI_COMPRESSION_MAGIC=kLz4fMagic" ]
} else if (zbi_compression_algorithm == "zstd") {
defines += [ "ZBI_COMPRESSION_MAGIC=kZstdMagic" ]
} else {
assert(zbi_compression_algorithm == "none",
"bad zbi_compression_algorithm=\"$zbi_compression_algorithm\"")
}
metadata = {
# Suppress the data_deps from hermetic-decompressor.
# Those represent the default engine service, which userboot replaces.
manifest_barrier = []
}
}
# This generated header lists all the ABI symbols in the vDSO with their
# addresses. It's used to generate vdso-syms.ld, below.
toolchain_utils_action("gen-vdso-syms-header") {
visibility = [ ":gen-vdso-syms-ld" ]
# Use the same link_output_rspfile() that lib/userabi uses to embed it,
# so we're sure to be getting the exact same binary.
deps = [ "$zx/kernel/lib/userabi:vdso.rsp" ]
vdso_gen_dir = get_label_info(deps[0], "target_gen_dir")
sources = [ "$vdso_gen_dir/vdso.rsp" ]
outputs = [ "$target_gen_dir/vdso-syms.h" ]
depfile = "${outputs[0]}.d"
utils = [ "nm" ]
script = "$zx/scripts/shlib-symbols"
args = [
"-a",
"@" + rebase_path(sources[0], root_build_dir),
rebase_path(outputs[0], root_build_dir),
rebase_path(depfile, root_build_dir),
]
metadata = {
generated_sources = rebase_path(outputs, root_build_dir)
}
}
# This generated linker script defines symbols for each vDSO entry point
# giving the relative address where it will be found at runtime. With
# this hack, the userboot code doesn't need to do any special work to
# find the vDSO and its entry points, keeping the code far simpler.
toolchain_utils_action("gen-vdso-syms-ld") {
visibility = [ ":*" ]
outputs = [ vdso_syms_ld ]
deps = [ ":gen-vdso-syms-header" ]
sources = get_target_outputs(deps[0])
inputs = [ "vdso-syms.ld.h" ]
utils = [ "cc" ]
args = [
"-o",
rebase_path(outputs[0], root_build_dir),
"-E",
"-P",
"-include",
rebase_path(inputs[0], root_build_dir),
rebase_path(sources[0], root_build_dir),
]
}
} else {
# In any other toolchain, just redirect to the proper toolchain.
environment_redirect("userboot") {
environment_label = ":userboot"
direct = true
deps = [ ":userboot" ]
}
}