• Andrii Nakryiko's avatar
    libbpf: Add BPF static linker BTF and BTF.ext support · 8fd27bf6
    Andrii Nakryiko authored
    Add .BTF and .BTF.ext static linking logic.
    
    When multiple BPF object files are linked together, their respective .BTF and
    .BTF.ext sections are merged together. BTF types are not just concatenated,
    but also deduplicated. .BTF.ext data is grouped by type (func info, line info,
    core_relos) and target section names, and then all the records are
    concatenated together, preserving their relative order. All the BTF type ID
    references and string offsets are updated as necessary, to take into account
    possibly deduplicated strings and types.
    
    BTF DATASEC types are handled specially. Their respective var_secinfos are
    accumulated separately in special per-section data and then final DATASEC
    types are emitted at the very end during bpf_linker__finalize() operation,
    just before emitting final ELF output file.
    
    BTF data can also provide "section annotations" for some extern variables.
    Such concept is missing in ELF, but BTF will have DATASEC types for such
    special extern datasections (e.g., .kconfig, .ksyms). Such sections are called
    "ephemeral" internally. Internally linker will keep metadata for each such
    section, collecting variables information, but those sections won't be emitted
    into the final ELF file.
    
    Also, given LLVM/Clang during compilation emits BTF DATASECS that are
    incomplete, missing section size and variable offsets for static variables,
    BPF static linker will initially fix up such DATASECs, using ELF symbols data.
    The final DATASECs will preserve section sizes and all variable offsets. This
    is handled correctly by libbpf already, so won't cause any new issues. On the
    other hand, it's actually a nice property to have a complete BTF data without
    runtime adjustments done during bpf_object__open() by libbpf. In that sense,
    BPF static linker is also a BTF normalizer.
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
    Link: https://lore.kernel.org/bpf/20210318194036.3521577-8-andrii@kernel.org
    8fd27bf6
linker.c 47.7 KB