From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8B951C4167B for ; Tue, 15 Feb 2022 22:59:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242248AbiBOW7u (ORCPT ); Tue, 15 Feb 2022 17:59:50 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:35966 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244730AbiBOW7s (ORCPT ); Tue, 15 Feb 2022 17:59:48 -0500 Received: from mail-qt1-x836.google.com (mail-qt1-x836.google.com [IPv6:2607:f8b0:4864:20::836]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7E74AF747B for ; Tue, 15 Feb 2022 14:59:34 -0800 (PST) Received: by mail-qt1-x836.google.com with SMTP id q20so372984qtw.8 for ; Tue, 15 Feb 2022 14:59:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kinvolk.io; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zT3uK29FMhfhh8DewxMCadQW6mKbGtNQ3FzMrNuYVHw=; b=D/lIXVd+MDiOWDoEabTMuoFkPvPCPWMHigtsYkCs+cfFzJM64fJvhKHeAYU9dVjZam YFf9kh+seOhpkIhSTazrAWa9lDgLZvIbAeIUfyrNPB6Yn5YTXdYW7ef7Hui/LzKlz/ab 5XfDMbFp4Rv1Lc3c+PTfQ7uWJCmvjIcGt4a8I= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zT3uK29FMhfhh8DewxMCadQW6mKbGtNQ3FzMrNuYVHw=; b=pw43uMhsq13Px4JkJuaS/DGk0GAyofkkajP17fjzzcSG+Olf9FDW4eMR297WcwpP3K ogQqChdPlawhnJIYoFAY12mQkeHBsRHXs8LJ2ahvC227SVosYIFoksY1UBwz428BE0Fn BRuevypyTxlY9VKsWBd81B5fyU4PV+PpqOZyBd5d5kqa0XRFgTR7eq6QGRD8fqzyK5zZ O+1EOs/yqQdagvr2jvFDd3xNsufoFX+nZKtwIobs94RqtMJvETZbXxz/m7AfizrZdwzY 2DUOMsYNkvkvtDapFcR4/H0XHkccA03naDxo+IQzjmzwDS2Ee5vwZGrxQ7/a5ysDxiBZ BHfw== X-Gm-Message-State: AOAM530vx3jNi8oVTrK7cdjnLaP0HgsWqTtw/rAm9wwV3j7tT3tjpxze IIH3e1T+z499C8y0SUbrZQ/ECg== X-Google-Smtp-Source: ABdhPJx7sfTWQxfkUplLloleC+98bFLx389D3qdkBCBg6gyujx77OJsCFvEcE90gSTFh9NgFbW31gg== X-Received: by 2002:a05:622a:1454:b0:2db:65c9:3a89 with SMTP id v20-20020a05622a145400b002db65c93a89mr184382qtx.129.1644965972491; Tue, 15 Feb 2022 14:59:32 -0800 (PST) Received: from localhost.localdomain ([181.136.110.101]) by smtp.gmail.com with ESMTPSA id w19sm15520021qkp.6.2022.02.15.14.59.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Feb 2022 14:59:32 -0800 (PST) From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= To: netdev@vger.kernel.org, bpf@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Quentin Monnet , Rafael David Tinoco , Lorenzo Fontana , Leonardo Di Donato Subject: [PATCH bpf-next v7 5/7] bpftool: Implement btfgen_get_btf() Date: Tue, 15 Feb 2022 17:58:54 -0500 Message-Id: <20220215225856.671072-6-mauricio@kinvolk.io> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220215225856.671072-1-mauricio@kinvolk.io> References: <20220215225856.671072-1-mauricio@kinvolk.io> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org The last part of the BTFGen algorithm is to create a new BTF object with all the types that were recorded in the previous steps. This function performs two different steps: 1. Add the types to the new BTF object by using btf__add_type(). Some special logic around struct and unions is implemented to only add the members that are really used in the field-based relocations. The type ID on the new and old BTF objects is stored on a map. 2. Fix all the type IDs on the new BTF object by using the IDs saved in the previous step. Signed-off-by: Mauricio Vásquez Signed-off-by: Rafael David Tinoco Signed-off-by: Lorenzo Fontana Signed-off-by: Leonardo Di Donato --- tools/bpf/bpftool/gen.c | 100 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c index 806001020841..25a336846d9f 100644 --- a/tools/bpf/bpftool/gen.c +++ b/tools/bpf/bpftool/gen.c @@ -1505,10 +1505,108 @@ static int btfgen_record_obj(struct btfgen_info *info, const char *obj_path) return err; } +static int btfgen_remap_id(__u32 *type_id, void *ctx) +{ + unsigned int *ids = ctx; + + *type_id = ids[*type_id]; + + return 0; +} + /* Generate BTF from relocation information previously recorded */ static struct btf *btfgen_get_btf(struct btfgen_info *info) { - return ERR_PTR(-EOPNOTSUPP); + struct btf *btf_new = NULL; + unsigned int *ids = NULL; + unsigned int i, n = btf__type_cnt(info->marked_btf); + int err = 0; + + btf_new = btf__new_empty(); + if (!btf_new) { + err = -errno; + goto err_out; + } + + ids = calloc(n, sizeof(*ids)); + if (!ids) { + err = -errno; + goto err_out; + } + + /* first pass: add all marked types to btf_new and add their new ids to the ids map */ + for (i = 1; i < n; i++) { + const struct btf_type *cloned_type, *type; + const char *name; + int new_id; + + cloned_type = btf__type_by_id(info->marked_btf, i); + + if (cloned_type->name_off != MARKED) + continue; + + type = btf__type_by_id(info->src_btf, i); + + /* add members for struct and union */ + if (btf_is_composite(type)) { + struct btf_member *cloned_m, *m; + unsigned short vlen; + int idx_src; + + name = btf__str_by_offset(info->src_btf, type->name_off); + + if (btf_is_struct(type)) + err = btf__add_struct(btf_new, name, type->size); + else + err = btf__add_union(btf_new, name, type->size); + + if (err < 0) + goto err_out; + new_id = err; + + cloned_m = btf_members(cloned_type); + m = btf_members(type); + vlen = btf_vlen(cloned_type); + for (idx_src = 0; idx_src < vlen; idx_src++, cloned_m++, m++) { + /* add only members that are marked as used */ + if (cloned_m->name_off != MARKED) + continue; + + name = btf__str_by_offset(info->src_btf, m->name_off); + err = btf__add_field(btf_new, name, m->type, + BTF_MEMBER_BIT_OFFSET(m->offset), + BTF_MEMBER_BITFIELD_SIZE(m->offset)); + if (err < 0) + goto err_out; + } + } else { + err = btf__add_type(btf_new, info->src_btf, type); + if (err < 0) + goto err_out; + new_id = err; + } + + /* add ID mapping */ + ids[i] = new_id; + } + + /* second pass: fix up type ids */ + for (i = 1; i < btf__type_cnt(btf_new); i++) { + struct btf_type *btf_type = (struct btf_type *) btf__type_by_id(btf_new, i); + + err = btf_type_visit_type_ids(btf_type, btfgen_remap_id, ids); + if (err) + goto err_out; + } + + free(ids); + return btf_new; + +err_out: + btf__free(btf_new); + free(ids); + errno = -err; + return NULL; } /* Create minimized BTF file for a set of BPF objects. -- 2.25.1