blob: d53d48fd46759a610c38a769ba3ca52805b40b72 [file]
# Copyright 2022 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("//build/zircon/c_utils.gni")
# Provide an ELF executable() or loadable_module() target with a PT_INTERP
# string based on the build ID of another linking target.
#
# This defines a link-input target (as in source_set() et al) that can
# be included in $deps of executable() and similar targets. Doing so
# gives that executable a PT_INTERP value of the build ID, in canonical
# hex textual form, of whatever executable-like target was reached by
# this target's $deps.
#
# Parameters
#
# * deps
# - Required: Must reach exactly one linking target (executable, etc.) as
# per link_output_rspfile(), which see. That executable's build ID
# rendered as hex text will become the PT_INTERP string used if an
# executable links in $target_name via its transitive $deps.
# - Type: list(label)
#
# * data_deps, metadata, visibility, testonly
# - Optional: As for source_set().
#
template("kernel_elf_interp") {
main_target = target_name
gen_target = "$target_name.elf-interp.S"
interp_file = "$target_gen_dir/$gen_target"
# Assemble a tiny file that defines an allocated `.interp` section.
source_set(main_target) {
forward_variables_from(invoker,
[
"data_deps",
"metadata",
"visibility",
"testonly",
])
sources = [ interp_file ]
deps = [ ":$gen_target" ]
}
# Generate an assembly file that embeds the text of the build ID taken from
# the .stamp file that buildidtool writes. This has a name that's the same
# as the link_ouptut_path plus ".build-id.stamp" and is an implicit output of
# the linking step. GN doesn't really care that this output file exists, but
# GN tells Ninja about it being an output of the link step. That's enough
# for Ninja to ingest a depfile that refers to it and ensure that the next
# incremental build will regenerate $interp_file when and only when the $deps
# linking target was actually relinked. Nonetheless, the script also avoids
# touching the output .S file if the build ID didn't change even when the
# timestamp of the binary did.
#
# **NOTE:** This generation has to be evaluated separately in each toolchain
# because there's no other way to be sure that the `deps` list is resolved to
# the intended toolchain. Even if it used explicit toolchain labels, then
# those would be generated using `$current_cpu` and the like, which gets
# re-evaluated in a toolchain redirect and e.g. in $default_toolchain might
# not be the same CPU that a particular kernel or phys toolchain is using.
rspfile_target = "$target_name.interp.rspfile"
interp_rspfile = "$target_gen_dir/$target_name.elf-interp.rspfile"
action(gen_target) {
visibility = [ ":$main_target" ]
forward_variables_from(invoker, [ "testonly" ])
script = "//zircon/kernel/phys/elf/kernel-elf-interp.sh"
sources = [ interp_rspfile ]
deps = [ ":$rspfile_target" ]
outputs = [ interp_file ]
depfile = "$interp_file.d"
args = rebase_path(sources + outputs + [ depfile ], root_build_dir)
metadata = {
# Don't propagate from zbi_input()-compatible or resource() targets.
zbi_input_barrier = []
distribution_entries_barrier = []
}
}
# Before all else, we need an rspfile that shows the file name that the
# link step wrote. The handy template generates an rspfile that contains
# the name of the main output file. The buildidtool file we need has a
# name that can be derived from this file name.
link_output_rspfile(rspfile_target) {
visibility = [ ":$gen_target" ]
forward_variables_from(invoker,
[
"deps",
"testonly",
])
outputs = [ interp_rspfile ]
}
}