Commit f8c7a4d4 authored by Joe Stringer's avatar Joe Stringer Committed by Alexei Starovoitov

bpf, libbpf: refactor relocation handling

Adjust the code for relocations slightly with no functional changes,
so that upcoming patches that will introduce support for relocations
into the .data, .rodata and .bss sections can be added independent
of these changes.
Signed-off-by: default avatarJoe Stringer <joe@wand.net.nz>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Acked-by: default avatarAndrii Nakryiko <andriin@fb.com>
Acked-by: default avatarMartin KaFai Lau <kafai@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent c83fef6b
...@@ -871,9 +871,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) ...@@ -871,9 +871,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
obj->efile.symbols = data; obj->efile.symbols = data;
obj->efile.strtabidx = sh.sh_link; obj->efile.strtabidx = sh.sh_link;
} }
} else if ((sh.sh_type == SHT_PROGBITS) && } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) {
(sh.sh_flags & SHF_EXECINSTR) && if (sh.sh_flags & SHF_EXECINSTR) {
(data->d_size > 0)) {
if (strcmp(name, ".text") == 0) if (strcmp(name, ".text") == 0)
obj->efile.text_shndx = idx; obj->efile.text_shndx = idx;
err = bpf_object__add_program(obj, data->d_buf, err = bpf_object__add_program(obj, data->d_buf,
...@@ -886,6 +885,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) ...@@ -886,6 +885,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags)
pr_warning("failed to alloc program %s (%s): %s", pr_warning("failed to alloc program %s (%s): %s",
name, obj->path, cp); name, obj->path, cp);
} }
}
} else if (sh.sh_type == SHT_REL) { } else if (sh.sh_type == SHT_REL) {
void *reloc = obj->efile.reloc; void *reloc = obj->efile.reloc;
int nr_reloc = obj->efile.nr_reloc + 1; int nr_reloc = obj->efile.nr_reloc + 1;
...@@ -1046,6 +1046,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, ...@@ -1046,6 +1046,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
return -LIBBPF_ERRNO__RELOC; return -LIBBPF_ERRNO__RELOC;
} }
if (sym.st_shndx == maps_shndx) {
/* TODO: 'maps' is sorted. We can use bsearch to make it faster. */ /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */
for (map_idx = 0; map_idx < nr_maps; map_idx++) { for (map_idx = 0; map_idx < nr_maps; map_idx++) {
if (maps[map_idx].offset == sym.st_value) { if (maps[map_idx].offset == sym.st_value) {
...@@ -1065,6 +1066,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, ...@@ -1065,6 +1066,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr,
prog->reloc_desc[i].insn_idx = insn_idx; prog->reloc_desc[i].insn_idx = insn_idx;
prog->reloc_desc[i].map_idx = map_idx; prog->reloc_desc[i].map_idx = map_idx;
} }
}
return 0; return 0;
} }
...@@ -1425,7 +1427,7 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) ...@@ -1425,7 +1427,7 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj)
} }
insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD;
insns[insn_idx].imm = obj->maps[map_idx].fd; insns[insn_idx].imm = obj->maps[map_idx].fd;
} else { } else if (prog->reloc_desc[i].type == RELO_CALL) {
err = bpf_program__reloc_text(prog, obj, err = bpf_program__reloc_text(prog, obj,
&prog->reloc_desc[i]); &prog->reloc_desc[i]);
if (err) if (err)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment