• J
    libbpf: Fix BTF dump of pointer-to-array-of-struct · 83ac0dcc
    Jean-Philippe Brucker 提交于
    stable inclusion
    from stable-5.10.27
    commit eeadce8811d35990da78fa05c8db0308727dd210
    bugzilla: 51493
    
    --------------------------------
    
    [ Upstream commit 901ee1d7 ]
    
    The vmlinux.h generated from BTF is invalid when building
    drivers/phy/ti/phy-gmii-sel.c with clang:
    
    vmlinux.h:61702:27: error: array type has incomplete element type ‘struct reg_field’
    61702 |  const struct reg_field (*regfields)[3];
          |                           ^~~~~~~~~
    
    bpftool generates a forward declaration for this struct regfield, which
    compilers aren't happy about. Here's a simplified reproducer:
    
    	struct inner {
    		int val;
    	};
    	struct outer {
    		struct inner (*ptr_to_array)[2];
    	} A;
    
    After build with clang -> bpftool btf dump c -> clang/gcc:
    ./def-clang.h:11:23: error: array has incomplete element type 'struct inner'
            struct inner (*ptr_to_array)[2];
    
    Member ptr_to_array of struct outer is a pointer to an array of struct
    inner. In the DWARF generated by clang, struct outer appears before
    struct inner, so when converting BTF of struct outer into C, bpftool
    issues a forward declaration to struct inner. With GCC the DWARF info is
    reversed so struct inner gets fully defined.
    
    That forward declaration is not sufficient when compilers handle an
    array of the struct, even when it's only used through a pointer. Note
    that we can trigger the same issue with an intermediate typedef:
    
    	struct inner {
    	        int val;
    	};
    	typedef struct inner inner2_t[2];
    	struct outer {
    	        inner2_t *ptr_to_array;
    	} A;
    
    Becomes:
    
    	struct inner;
    	typedef struct inner inner2_t[2];
    
    And causes:
    
    ./def-clang.h:10:30: error: array has incomplete element type 'struct inner'
    	typedef struct inner inner2_t[2];
    
    To fix this, clear through_ptr whenever we encounter an intermediate
    array, to make the inner struct part of a strong link and force full
    declaration.
    
    Fixes: 351131b5 ("libbpf: add btf_dump API for BTF-to-C conversion")
    Signed-off-by: NJean-Philippe Brucker <jean-philippe@linaro.org>
    Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
    Link: https://lore.kernel.org/bpf/20210319112554.794552-2-jean-philippe@linaro.orgSigned-off-by: NSasha Levin <sashal@kernel.org>
    Signed-off-by: NChen Jun <chenjun102@huawei.com>
    Acked-by: N  Weilong Chen <chenweilong@huawei.com>
    Signed-off-by: NZheng Zengkai <zhengzengkai@huawei.com>
    83ac0dcc
btf_dump.c 39.9 KB