Commit 28b93c64 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Alexei Starovoitov

libbpf: Clean up and improve CO-RE reloc logging

Add logging of local/target type kind (struct/union/typedef/etc). Preserve
unresolved root type ID (for cases of typedef). Improve the format of CO-RE
reloc spec output format to contain only relevant and succinct info.
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20200818223921.2911963-3-andriin@fb.com
parent 81ba0889
...@@ -1131,14 +1131,14 @@ static int btf_ext_setup_line_info(struct btf_ext *btf_ext) ...@@ -1131,14 +1131,14 @@ static int btf_ext_setup_line_info(struct btf_ext *btf_ext)
return btf_ext_setup_info(btf_ext, &param); return btf_ext_setup_info(btf_ext, &param);
} }
static int btf_ext_setup_field_reloc(struct btf_ext *btf_ext) static int btf_ext_setup_core_relos(struct btf_ext *btf_ext)
{ {
struct btf_ext_sec_setup_param param = { struct btf_ext_sec_setup_param param = {
.off = btf_ext->hdr->field_reloc_off, .off = btf_ext->hdr->core_relo_off,
.len = btf_ext->hdr->field_reloc_len, .len = btf_ext->hdr->core_relo_len,
.min_rec_size = sizeof(struct bpf_field_reloc), .min_rec_size = sizeof(struct bpf_core_relo),
.ext_info = &btf_ext->field_reloc_info, .ext_info = &btf_ext->core_relo_info,
.desc = "field_reloc", .desc = "core_relo",
}; };
return btf_ext_setup_info(btf_ext, &param); return btf_ext_setup_info(btf_ext, &param);
...@@ -1217,10 +1217,9 @@ struct btf_ext *btf_ext__new(__u8 *data, __u32 size) ...@@ -1217,10 +1217,9 @@ struct btf_ext *btf_ext__new(__u8 *data, __u32 size)
if (err) if (err)
goto done; goto done;
if (btf_ext->hdr->hdr_len < if (btf_ext->hdr->hdr_len < offsetofend(struct btf_ext_header, core_relo_len))
offsetofend(struct btf_ext_header, field_reloc_len))
goto done; goto done;
err = btf_ext_setup_field_reloc(btf_ext); err = btf_ext_setup_core_relos(btf_ext);
if (err) if (err)
goto done; goto done;
......
...@@ -24,44 +24,6 @@ struct btf_type; ...@@ -24,44 +24,6 @@ struct btf_type;
struct bpf_object; struct bpf_object;
/*
* The .BTF.ext ELF section layout defined as
* struct btf_ext_header
* func_info subsection
*
* The func_info subsection layout:
* record size for struct bpf_func_info in the func_info subsection
* struct btf_sec_func_info for section #1
* a list of bpf_func_info records for section #1
* where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
* but may not be identical
* struct btf_sec_func_info for section #2
* a list of bpf_func_info records for section #2
* ......
*
* Note that the bpf_func_info record size in .BTF.ext may not
* be the same as the one defined in include/uapi/linux/bpf.h.
* The loader should ensure that record_size meets minimum
* requirement and pass the record as is to the kernel. The
* kernel will handle the func_info properly based on its contents.
*/
struct btf_ext_header {
__u16 magic;
__u8 version;
__u8 flags;
__u32 hdr_len;
/* All offsets are in bytes relative to the end of this header */
__u32 func_info_off;
__u32 func_info_len;
__u32 line_info_off;
__u32 line_info_len;
/* optional part of .BTF.ext header */
__u32 field_reloc_off;
__u32 field_reloc_len;
};
LIBBPF_API void btf__free(struct btf *btf); LIBBPF_API void btf__free(struct btf *btf);
LIBBPF_API struct btf *btf__new(const void *data, __u32 size); LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext); LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
......
This diff is collapsed.
...@@ -138,6 +138,44 @@ struct btf_ext_info { ...@@ -138,6 +138,44 @@ struct btf_ext_info {
i < (sec)->num_info; \ i < (sec)->num_info; \
i++, rec = (void *)rec + (seg)->rec_size) i++, rec = (void *)rec + (seg)->rec_size)
/*
* The .BTF.ext ELF section layout defined as
* struct btf_ext_header
* func_info subsection
*
* The func_info subsection layout:
* record size for struct bpf_func_info in the func_info subsection
* struct btf_sec_func_info for section #1
* a list of bpf_func_info records for section #1
* where struct bpf_func_info mimics one in include/uapi/linux/bpf.h
* but may not be identical
* struct btf_sec_func_info for section #2
* a list of bpf_func_info records for section #2
* ......
*
* Note that the bpf_func_info record size in .BTF.ext may not
* be the same as the one defined in include/uapi/linux/bpf.h.
* The loader should ensure that record_size meets minimum
* requirement and pass the record as is to the kernel. The
* kernel will handle the func_info properly based on its contents.
*/
struct btf_ext_header {
__u16 magic;
__u8 version;
__u8 flags;
__u32 hdr_len;
/* All offsets are in bytes relative to the end of this header */
__u32 func_info_off;
__u32 func_info_len;
__u32 line_info_off;
__u32 line_info_len;
/* optional part of .BTF.ext header */
__u32 core_relo_off;
__u32 core_relo_len;
};
struct btf_ext { struct btf_ext {
union { union {
struct btf_ext_header *hdr; struct btf_ext_header *hdr;
...@@ -145,7 +183,7 @@ struct btf_ext { ...@@ -145,7 +183,7 @@ struct btf_ext {
}; };
struct btf_ext_info func_info; struct btf_ext_info func_info;
struct btf_ext_info line_info; struct btf_ext_info line_info;
struct btf_ext_info field_reloc_info; struct btf_ext_info core_relo_info;
__u32 data_size; __u32 data_size;
}; };
...@@ -170,32 +208,34 @@ struct bpf_line_info_min { ...@@ -170,32 +208,34 @@ struct bpf_line_info_min {
__u32 line_col; __u32 line_col;
}; };
/* bpf_field_info_kind encodes which aspect of captured field has to be /* bpf_core_relo_kind encodes which aspect of captured field/type/enum value
* adjusted by relocations. Currently supported values are: * has to be adjusted by relocations.
* - BPF_FIELD_BYTE_OFFSET: field offset (in bytes);
* - BPF_FIELD_EXISTS: field existence (1, if field exists; 0, otherwise);
*/ */
enum bpf_field_info_kind { enum bpf_core_relo_kind {
BPF_FIELD_BYTE_OFFSET = 0, /* field byte offset */ BPF_FIELD_BYTE_OFFSET = 0, /* field byte offset */
BPF_FIELD_BYTE_SIZE = 1, BPF_FIELD_BYTE_SIZE = 1, /* field size in bytes */
BPF_FIELD_EXISTS = 2, /* field existence in target kernel */ BPF_FIELD_EXISTS = 2, /* field existence in target kernel */
BPF_FIELD_SIGNED = 3, BPF_FIELD_SIGNED = 3, /* field signedness (0 - unsigned, 1 - signed) */
BPF_FIELD_LSHIFT_U64 = 4, BPF_FIELD_LSHIFT_U64 = 4, /* bitfield-specific left bitshift */
BPF_FIELD_RSHIFT_U64 = 5, BPF_FIELD_RSHIFT_U64 = 5, /* bitfield-specific right bitshift */
}; };
/* The minimum bpf_field_reloc checked by the loader /* The minimum bpf_core_relo checked by the loader
* *
* Field relocation captures the following data: * CO-RE relocation captures the following data:
* - insn_off - instruction offset (in bytes) within a BPF program that needs * - insn_off - instruction offset (in bytes) within a BPF program that needs
* its insn->imm field to be relocated with actual field info; * its insn->imm field to be relocated with actual field info;
* - type_id - BTF type ID of the "root" (containing) entity of a relocatable * - type_id - BTF type ID of the "root" (containing) entity of a relocatable
* field; * type or field;
* - access_str_off - offset into corresponding .BTF string section. String * - access_str_off - offset into corresponding .BTF string section. String
* itself encodes an accessed field using a sequence of field and array * interpretation depends on specific relocation kind:
* indicies, separated by colon (:). It's conceptually very close to LLVM's * - for field-based relocations, string encodes an accessed field using
* getelementptr ([0]) instruction's arguments for identifying offset to * a sequence of field and array indices, separated by colon (:). It's
* a field. * conceptually very close to LLVM's getelementptr ([0]) instruction's
* arguments for identifying offset to a field.
* - for type-based relocations, strings is expected to be just "0";
* - for enum value-based relocations, string contains an index of enum
* value within its enum type;
* *
* Example to provide a better feel. * Example to provide a better feel.
* *
...@@ -226,11 +266,11 @@ enum bpf_field_info_kind { ...@@ -226,11 +266,11 @@ enum bpf_field_info_kind {
* *
* [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction * [0] https://llvm.org/docs/LangRef.html#getelementptr-instruction
*/ */
struct bpf_field_reloc { struct bpf_core_relo {
__u32 insn_off; __u32 insn_off;
__u32 type_id; __u32 type_id;
__u32 access_str_off; __u32 access_str_off;
enum bpf_field_info_kind kind; enum bpf_core_relo_kind kind;
}; };
#endif /* __LIBBPF_LIBBPF_INTERNAL_H */ #endif /* __LIBBPF_LIBBPF_INTERNAL_H */
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