Skip to content

BPF static linker: support externs

Andrii Nakryiko bpf netdev 3 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest18 patches 10 repliesIn review
  2. v217 patches 49 repliesSuperseded
  3. v117 patches 9 repliesSuperseded

Changes in v3 from the cover letter

- allow only STV_DEFAULT and STV_HIDDEN ELF symbol visibility (Yonghong);
- update selftests' README for required Clang 13 fix dependency (Alexei);
- comments, typos, slight code changes (Yonghong, Alexei);

Patches in v3 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/18bpftool: support dumping BTF VAR's "extern" linkagetools/bpf/bpftool/btf.c+3 -10 replies
2/18bpftool: dump more info about DATASEC memberstools/bpf/bpftool/btf.c+18 -80 replies
3/18libbpf: suppress compiler warning when using SEC() macro with externstools/lib/bpf/bpf_helpers.h+9 -20 replies
4/18libbpf: mark BPF subprogs with hidden visibility as static for BPF verifiertools/lib/bpf/bpf_helpers.h tools/lib/bpf/btf.c tools/lib/bpf/libbpf.c +1 more+58 -61 reply
5/18libbpf: allow gaps in BPF program sections to support overriden weak functionstools/lib/bpf/libbpf.c+22 -360 replies
6/18libbpf: refactor BTF map definition parsingtools/lib/bpf/libbpf.c tools/lib/bpf/libbpf_internal.h+178 -1111 reply
7/18libbpf: factor out symtab and relos sanity checkstools/lib/bpf/linker.c+127 -1060 replies
8/18libbpf: make few internal helpers available outside of libbpf.ctools/lib/bpf/libbpf.c tools/lib/bpf/libbpf_internal.h tools/lib/bpf/linker.c+16 -180 replies
9/18libbpf: extend sanity checking ELF symbols with externs validationtools/lib/bpf/linker.c+40 -91 reply
10/18libbpf: tighten BTF type ID rewriting with error checkingtools/lib/bpf/linker.c+7 -01 reply
11/18libbpf: add linker extern resolution support for functions and global variablestools/lib/bpf/linker.c+790 -591 reply
12/18libbpf: support extern resolution for BTF-defined maps in .maps sectiontools/lib/bpf/linker.c+132 -01 reply
13/18selftests/bpf: use -O0 instead of -Og in selftests buildstools/testing/selftests/bpf/Makefile+4 -40 replies
14/18selftests/bpf: omit skeleton generation for multi-linked BPF object filestools/testing/selftests/bpf/Makefile+3 -10 replies
15/18selftests/bpf: add function linking selftesttools/testing/selftests/bpf/Makefile tools/testing/selftests/bpf/prog_tests/linked_funcs.c tools/testing/selftests/bpf/progs/linked_funcs1.c +1 more+190 -10 replies
16/18selftests/bpf: add global variables linking selftesttools/testing/selftests/bpf/Makefile tools/testing/selftests/bpf/prog_tests/linked_vars.c tools/testing/selftests/bpf/progs/linked_vars1.c +1 more+154 -10 replies
17/18selftests/bpf: add map linking selftesttools/testing/selftests/bpf/Makefile tools/testing/selftests/bpf/prog_tests/linked_maps.c tools/testing/selftests/bpf/progs/linked_maps1.c +1 more+191 -13 replies
18/18selftests/bpf: document latest Clang fix expectations for linking teststools/testing/selftests/bpf/README.rst+9 -01 reply

Reviews trailers per patch

All 18 patches have a Reviewed-by or Acked-by.

PatchYonghong Song
1/18A-b
2/18A-b
3/18A-b
4/18A-b
5/18A-b
6/18A-b
7/18A-b
8/18A-b
9/18A-b
10/18A-b
11/18A-b
12/18A-b
13/18A-b
14/18A-b
15/18A-b
16/18A-b
17/18A-b
18/18A-b

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH v3 bpf-next 00/18] BPF static linker: support externs

Add BPF static linker support for extern resolution of global variables, functions, and BTF-defined maps.

This patch set consists of 4 parts:
- few patches are extending bpftool to simplify working with BTF dump;
- libbpf object loading logic is extended to support __hidden functions and overriden (unused) __weak functions; also BTF-defined map parsing logic is refactored to be re-used by linker;
- the crux of the patch set is BPF static linker logic extension to perform extern resolution for three categories: global variables, BPF sub-programs, BTF-defined maps;
- a set of selftests that validate that all the combinations of extern/weak/__hidden are working as expected.

See respective patches for more details.

One aspect hasn't been addressed yet and is going to be resolved in the next patch set, but is worth mentioning. With BPF static linking of multiple .o files, dealing with static everything becomes more problematic for BPF skeleton and in general for any by name look up APIs. This is due to static entities are allowed to have non-unique name. Historically this was never a problem due to BPF programs were always confined to a single C file. That changes now and needs to be addressed. The thinking so far is for BPF static linker to prepend filename to each static variable and static map (which is currently not supported by libbpf, btw), so that they can be unambiguously resolved by (mostly) unique name. Mostly, because even filenames can be duplicated, but that should be rare and easy to address by wiser choice of filenames by users. Fortunately, static BPF subprograms don't suffer from this issues, as they are not independent entities and are neither exposed in BPF skeleton, nor is lookup-able by any of libbpf APIs (and there is little reason to do that anyways).

This and few other things will be the topic of the next set of patches.

Some tests rely on Clang fix ([0]), so need latest Clang built from main.

[0] https://reviews.llvm.org/D100362

v2->v3:
- allow only STV_DEFAULT and STV_HIDDEN ELF symbol visibility (Yonghong);
- update selftests' README for required Clang 13 fix dependency (Alexei);
- comments, typos, slight code changes (Yonghong, Alexei);

v1->v2:
- make map externs support full attribute list, adjust linked_maps selftest to demonstrate that typedef works now (though no shared header file was added to simplicity sake) (Alexei);
- remove commented out parts from selftests and fix few minor code style

    issues;

- special __weak map definition semantics not yet implemented and will be addressed in a follow up.

Andrii Nakryiko (18):
bpftool: support dumping BTF VAR's "extern" linkage
bpftool: dump more info about DATASEC members
libbpf: suppress compiler warning when using SEC() macro with externs
libbpf: mark BPF subprogs with hidden visibility as static for BPF
verifier
libbpf: allow gaps in BPF program sections to support overriden weak
functions
libbpf: refactor BTF map definition parsing
libbpf: factor out symtab and relos sanity checks
libbpf: make few internal helpers available outside of libbpf.c
libbpf: extend sanity checking ELF symbols with externs validation
libbpf: tighten BTF type ID rewriting with error checking
libbpf: add linker extern resolution support for functions and global
variables
libbpf: support extern resolution for BTF-defined maps in .maps
section
selftests/bpf: use -O0 instead of -Og in selftests builds selftests/bpf: omit skeleton generation for multi-linked BPF object
files
selftests/bpf: add function linking selftest selftests/bpf: add global variables linking selftest selftests/bpf: add map linking selftest
selftests/bpf: document latest Clang fix expectations for linking
tests

 tools/bpf/bpftool/btf.c                       |   30 +-
 tools/lib/bpf/bpf_helpers.h                   |   19 +-
 tools/lib/bpf/btf.c                           |    5 -
 tools/lib/bpf/libbpf.c                        |  373 +++--
 tools/lib/bpf/libbpf_internal.h               |   45 +
 tools/lib/bpf/linker.c                        | 1270 ++++++++++++++---
 tools/testing/selftests/bpf/Makefile          |   18 +-
 tools/testing/selftests/bpf/README.rst        |    9 +
 .../selftests/bpf/prog_tests/linked_funcs.c   |   42 +
 .../selftests/bpf/prog_tests/linked_maps.c    |   30 +
 .../selftests/bpf/prog_tests/linked_vars.c    |   43 +
 .../selftests/bpf/progs/linked_funcs1.c       |   73 +
 .../selftests/bpf/progs/linked_funcs2.c       |   73 +
 .../selftests/bpf/progs/linked_maps1.c        |   82 ++
 .../selftests/bpf/progs/linked_maps2.c        |   76 +
 .../selftests/bpf/progs/linked_vars1.c        |   54 +
 .../selftests/bpf/progs/linked_vars2.c        |   55 +
 17 files changed, 1942 insertions(+), 355 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/linked_funcs.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/linked_maps.c
 create mode 100644 tools/testing/selftests/bpf/prog_tests/linked_vars.c
 create mode 100644 tools/testing/selftests/bpf/progs/linked_funcs1.c
 create mode 100644 tools/testing/selftests/bpf/progs/linked_funcs2.c
 create mode 100644 tools/testing/selftests/bpf/progs/linked_maps1.c
 create mode 100644 tools/testing/selftests/bpf/progs/linked_maps2.c
 create mode 100644 tools/testing/selftests/bpf/progs/linked_vars1.c
 create mode 100644 tools/testing/selftests/bpf/progs/linked_vars2.c
Signature · 1 line
2.30.2