> On Jul 26, 2019, at 10:11 PM, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote: > > On Wed, Jul 24, 2019 at 10:20 PM Song Liu <songliubraving@xxxxxx> wrote: >> >> >> >>> On Jul 24, 2019, at 5:37 PM, Andrii Nakryiko <andrii.nakryiko@xxxxxxxxx> wrote: >>> >>> On Wed, Jul 24, 2019 at 5:00 PM Song Liu <songliubraving@xxxxxx> wrote: >>>> >>>> >>>> >>>>> On Jul 24, 2019, at 12:27 PM, Andrii Nakryiko <andriin@xxxxxx> wrote: >>>>> >>>>> Add support for BPF CO-RE offset relocations. Add section/record >>>>> iteration macros for .BTF.ext. These macro are useful for iterating over >>>>> each .BTF.ext record, either for dumping out contents or later for BPF >>>>> CO-RE relocation handling. >>>>> >>>>> To enable other parts of libbpf to work with .BTF.ext contents, moved >>>>> a bunch of type definitions into libbpf_internal.h. >>>>> >>>>> Signed-off-by: Andrii Nakryiko <andriin@xxxxxx> >>>>> --- >>>>> tools/lib/bpf/btf.c | 64 +++++++++-------------- >>>>> tools/lib/bpf/btf.h | 4 ++ >>>>> tools/lib/bpf/libbpf_internal.h | 91 +++++++++++++++++++++++++++++++++ >>>>> 3 files changed, 118 insertions(+), 41 deletions(-) >>>>> >>> >>> [...] >>> >>>>> + >>>>> static int btf_ext_parse_hdr(__u8 *data, __u32 data_size) >>>>> { >>>>> const struct btf_ext_header *hdr = (struct btf_ext_header *)data; >>>>> @@ -1004,6 +979,13 @@ struct btf_ext *btf_ext__new(__u8 *data, __u32 size) >>>>> if (err) >>>>> goto done; >>>>> >>>>> + /* check if there is offset_reloc_off/offset_reloc_len fields */ >>>>> + if (btf_ext->hdr->hdr_len < sizeof(struct btf_ext_header)) >>>> >>>> This check will break when we add more optional sections to btf_ext_header. >>>> Maybe use offsetof() instead? >>> >>> I didn't do it, because there are no fields after offset_reloc_len. >>> But now I though that maybe it would be ok to add zero-sized marker >>> field, kind of like marking off various versions of btf_ext header? >>> >>> Alternatively, I can add offsetofend() macro somewhere in libbpf_internal.h. >>> >>> Do you have any preference? >> >> We only need a stable number to compare against. offsetofend() works. >> Or we can simply have something like >> >> if (btf_ext->hdr->hdr_len <= offsetof(struct btf_ext_header, offset_reloc_off)) >> goto done; >> or >> if (btf_ext->hdr->hdr_len < offsetof(struct btf_ext_header, offset_reloc_len)) >> goto done; >> >> Does this make sense? > > I think offsetofend() is the cleanest solution, I'll do just that. Agreed that offsetofend() is the best. Song