• Eduard Zingerman's avatar
    libbpf: Resolve enum fwd as full enum64 and vice versa · de048b6e
    Eduard Zingerman authored
    Changes de-duplication logic for enums in the following way:
    - update btf_hash_enum to ignore size and kind fields to get
      ENUM and ENUM64 types in a same hash bucket;
    - update btf_compat_enum to consider enum fwd to be compatible with
      full enum64 (and vice versa);
    
    This allows BTF de-duplication in the following case:
    
        // CU #1
        enum foo;
    
        struct s {
          enum foo *a;
        } *x;
    
        // CU #2
        enum foo {
          x = 0xfffffffff // big enough to force enum64
        };
    
        struct s {
          enum foo *a;
        } *y;
    
    De-duplicated BTF prior to this commit:
    
        [1] ENUM64 'foo' encoding=UNSIGNED size=8 vlen=1
        	'x' val=68719476735ULL
        [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64
            encoding=(none)
        [3] STRUCT 's' size=8 vlen=1
        	'a' type_id=4 bits_offset=0
        [4] PTR '(anon)' type_id=1
        [5] PTR '(anon)' type_id=3
        [6] STRUCT 's' size=8 vlen=1
        	'a' type_id=8 bits_offset=0
        [7] ENUM 'foo' encoding=UNSIGNED size=4 vlen=0
        [8] PTR '(anon)' type_id=7
        [9] PTR '(anon)' type_id=6
    
    De-duplicated BTF after this commit:
    
        [1] ENUM64 'foo' encoding=UNSIGNED size=8 vlen=1
        	'x' val=68719476735ULL
        [2] INT 'long unsigned int' size=8 bits_offset=0 nr_bits=64
            encoding=(none)
        [3] STRUCT 's' size=8 vlen=1
        	'a' type_id=4 bits_offset=0
        [4] PTR '(anon)' type_id=1
        [5] PTR '(anon)' type_id=3
    
    Enum forward declarations in C do not provide information about
    enumeration values range. Thus the `btf_type->size` field is
    meaningless for forward enum declarations. In fact, GCC does not
    encode size in DWARF for forward enum declarations
    (but dwarves sets enumeration size to a default value of `sizeof(int) * 8`
    when size is not specified see dwarf_loader.c:die__create_new_enumeration).
    Signed-off-by: default avatarEduard Zingerman <eddyz87@gmail.com>
    Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20221101235413.1824260-1-eddyz87@gmail.com
    de048b6e
btf.c 127 KB