* [PATCH v2 bpf-next] libbpf: support .text sub-calls relocations
@ 2020-01-15 19:08 Andrii Nakryiko
2020-01-15 19:29 ` Yonghong Song
2020-01-15 20:05 ` Alexei Starovoitov
0 siblings, 2 replies; 3+ messages in thread
From: Andrii Nakryiko @ 2020-01-15 19:08 UTC (permalink / raw)
To: bpf, netdev, ast, daniel, yhs
Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Alexei Starovoitov
The LLVM patch https://reviews.llvm.org/D72197 makes LLVM emit function call
relocations within the same section. This includes a default .text section,
which contains any BPF sub-programs. This wasn't the case before and so libbpf
was able to get a way with slightly simpler handling of subprogram call
relocations.
This patch adds support for .text section relocations. It needs to ensure
correct order of relocations, so does two passes:
- first, relocate .text instructions, if there are any relocations in it;
- then process all the other programs and copy over patched .text instructions
for all sub-program calls.
v1->v2:
- break early once .text program is processed.
Cc: Yonghong Song <yhs@fb.com>
Cc: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
tools/lib/bpf/libbpf.c | 29 ++++++++++++++++++++++-------
1 file changed, 22 insertions(+), 7 deletions(-)
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 0c229f00a67e..23868883477f 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -4692,13 +4692,7 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
size_t new_cnt;
int err;
- if (prog->idx == obj->efile.text_shndx) {
- pr_warn("relo in .text insn %d into off %d (insn #%d)\n",
- relo->insn_idx, relo->sym_off, relo->sym_off / 8);
- return -LIBBPF_ERRNO__RELOC;
- }
-
- if (prog->main_prog_cnt == 0) {
+ if (prog->idx != obj->efile.text_shndx && prog->main_prog_cnt == 0) {
text = bpf_object__find_prog_by_idx(obj, obj->efile.text_shndx);
if (!text) {
pr_warn("no .text section found yet relo into text exist\n");
@@ -4728,6 +4722,7 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
text->insns_cnt, text->section_name,
prog->section_name);
}
+
insn = &prog->insns[relo->insn_idx];
insn->imm += relo->sym_off / 8 + prog->main_prog_cnt - relo->insn_idx;
return 0;
@@ -4807,8 +4802,28 @@ bpf_object__relocate(struct bpf_object *obj, const char *targ_btf_path)
return err;
}
}
+ /* ensure .text is relocated first, as it's going to be copied as-is
+ * later for sub-program calls
+ */
+ for (i = 0; i < obj->nr_programs; i++) {
+ prog = &obj->programs[i];
+ if (prog->idx != obj->efile.text_shndx)
+ continue;
+
+ err = bpf_program__relocate(prog, obj);
+ if (err) {
+ pr_warn("failed to relocate '%s'\n", prog->section_name);
+ return err;
+ }
+ break;
+ }
+ /* now relocate everything but .text, which by now is relocated
+ * properly, so we can copy raw sub-program instructions as is safely
+ */
for (i = 0; i < obj->nr_programs; i++) {
prog = &obj->programs[i];
+ if (prog->idx == obj->efile.text_shndx)
+ continue;
err = bpf_program__relocate(prog, obj);
if (err) {
--
2.17.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2 bpf-next] libbpf: support .text sub-calls relocations
2020-01-15 19:08 [PATCH v2 bpf-next] libbpf: support .text sub-calls relocations Andrii Nakryiko
@ 2020-01-15 19:29 ` Yonghong Song
2020-01-15 20:05 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: Yonghong Song @ 2020-01-15 19:29 UTC (permalink / raw)
To: Andrii Nakryiko, bpf, netdev, Alexei Starovoitov, daniel
Cc: andrii.nakryiko, Kernel Team, Alexei Starovoitov
On 1/15/20 11:08 AM, Andrii Nakryiko wrote:
> The LLVM patch https://urldefense.proofpoint.com/v2/url?u=https-3A__reviews.llvm.org_D72197&d=DwIBAg&c=5VD0RTtNlTh3ycd41b3MUw&r=DA8e1B5r073vIqRrFz7MRA&m=Pw2Bljqg1D5zPVHNmG5W7MTL_jxP_myfOOC4O-YSp_M&s=SUm2Y_ugh5NW-P8Iek263sULvuebiPSzWwtJ9J1Z330&e= makes LLVM emit function call
> relocations within the same section. This includes a default .text section,
> which contains any BPF sub-programs. This wasn't the case before and so libbpf
> was able to get a way with slightly simpler handling of subprogram call
> relocations.
>
> This patch adds support for .text section relocations. It needs to ensure
> correct order of relocations, so does two passes:
> - first, relocate .text instructions, if there are any relocations in it;
> - then process all the other programs and copy over patched .text instructions
> for all sub-program calls.
>
> v1->v2:
> - break early once .text program is processed.
>
> Cc: Yonghong Song <yhs@fb.com>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Thanks for the quick fix!
Acked-by: Yonghong Song <yhs@fb.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2 bpf-next] libbpf: support .text sub-calls relocations
2020-01-15 19:08 [PATCH v2 bpf-next] libbpf: support .text sub-calls relocations Andrii Nakryiko
2020-01-15 19:29 ` Yonghong Song
@ 2020-01-15 20:05 ` Alexei Starovoitov
1 sibling, 0 replies; 3+ messages in thread
From: Alexei Starovoitov @ 2020-01-15 20:05 UTC (permalink / raw)
To: Andrii Nakryiko
Cc: bpf, netdev, ast, daniel, yhs, andrii.nakryiko, kernel-team,
Alexei Starovoitov
On Wed, Jan 15, 2020 at 11:08:56AM -0800, Andrii Nakryiko wrote:
> The LLVM patch https://reviews.llvm.org/D72197 makes LLVM emit function call
> relocations within the same section. This includes a default .text section,
> which contains any BPF sub-programs. This wasn't the case before and so libbpf
> was able to get a way with slightly simpler handling of subprogram call
> relocations.
>
> This patch adds support for .text section relocations. It needs to ensure
> correct order of relocations, so does two passes:
> - first, relocate .text instructions, if there are any relocations in it;
> - then process all the other programs and copy over patched .text instructions
> for all sub-program calls.
>
> v1->v2:
> - break early once .text program is processed.
>
> Cc: Yonghong Song <yhs@fb.com>
> Cc: Alexei Starovoitov <ast@kernel.org>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Tested with the latest llvm trunk (which points to version 11 already)
and all tests pass.
Applied. Thanks
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-01-15 20:05 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-15 19:08 [PATCH v2 bpf-next] libbpf: support .text sub-calls relocations Andrii Nakryiko
2020-01-15 19:29 ` Yonghong Song
2020-01-15 20:05 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).