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 X-Spam-Level: X-Spam-Status: No, score=-6.0 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,UNWANTED_LANGUAGE_BODY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AC286C282CE for ; Tue, 12 Feb 2019 00:47:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 71BD0217FA for ; Tue, 12 Feb 2019 00:47:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ERsp01pB" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728059AbfBLAro (ORCPT ); Mon, 11 Feb 2019 19:47:44 -0500 Received: from mail-it1-f195.google.com ([209.85.166.195]:50517 "EHLO mail-it1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728011AbfBLArm (ORCPT ); Mon, 11 Feb 2019 19:47:42 -0500 Received: by mail-it1-f195.google.com with SMTP id z7so3140207iti.0; Mon, 11 Feb 2019 16:47:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MjHlHssDheHKshUJ1ShLjFSGsAlmn1CmVj6NNY2/zYo=; b=ERsp01pBVpzgvSYVR68a8klzTsBB+RvDisnxChvd1fzuLplPxgvpzxwkTsGNZZkGA9 ++4nZql1EGyaIXvSbisr/qnpicQtIibL9gkfUJyyjMSSSEQO15elkXPHF0eKtXUz1ZWV aiZvbAtoOHfJnKMNxibbRxodKmS7GNcVLH1w+Tmu946pkgHjW59OOHlJfjSihZ50BAx2 ww4hyeEwzPf8l3/tpoeGAYW6uqc5goIxPjhpnEjY3PxgFRztyKGZpN09UaYJ26NHh/10 mmrXlUMxRBE/0Y7lQPrMgL/OY72s0/XQjWYMhyjFVtZAZ7cEbzvFQEHVOSZC5Ki2A4Lm iftA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=MjHlHssDheHKshUJ1ShLjFSGsAlmn1CmVj6NNY2/zYo=; b=A/4wIHZHfwMfCnJ38orgKIXeU1hxQtPKnYw5x0Unsp9qi4+wWZTkfT98A3ED2xNkod lxCD4h9LH8tN1fTyCFi5QU0JGVLeRdJG1Gc8uC/Bxd/5yPy9AuC11OJNHwxUhbNhFlTs D7/EPtavP+pbhsrjYXI5ELIo/5OvZPs6YEEEs2yM6kY5Ei7AmPKBu5hzVYt9dHyL60qK ejO+EhIP9NGYr+IbFnVlNN5vgSD9pp1ubc/CdV2KeOjBTDDw/vUi3Eegtr6tEEeTsnwA jIyqxfCV8SHbvAnr4WTN/V9ypgSovxHSDVh1lz3erytWvK4PEddf331i5TJU44Jn84KZ 42fg== X-Gm-Message-State: AHQUAuapVCiH8DbUHCCnkTlvjuWxRY8kJrnD9TGuEMYvtsV2k2fn55TC aNMZ3Kr4vyIA5ZnplD+1uxbYAewd X-Google-Smtp-Source: AHgI3IbVt1OStfUk2ckhNnhpv3qOoePANTVJ6aUx9hgSI2Q7DAepZ9cBmP/m3ZL18q3qHmqWEGRrhQ== X-Received: by 2002:a24:750f:: with SMTP id y15mr467763itc.177.1549932459939; Mon, 11 Feb 2019 16:47:39 -0800 (PST) Received: from localhost.localdomain (mr-urb-183-235.dmisinetworks.net. [66.253.183.235]) by smtp.gmail.com with ESMTPSA id v193sm766829itv.32.2019.02.11.16.47.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Feb 2019 16:47:39 -0800 (PST) From: Joe Stringer To: bpf@vger.kernel.org Cc: netdev@vger.kernel.org, daniel@iogearbox.net, ast@kernel.org Subject: [PATCH bpf-next 2/4] libbpf: Support 32-bit static data loads Date: Mon, 11 Feb 2019 16:47:27 -0800 Message-Id: <20190212004729.535-3-joe@wand.net.nz> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190212004729.535-1-joe@wand.net.nz> References: <20190212004729.535-1-joe@wand.net.nz> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Support loads of static 32-bit data when BPF writers make use of convenience macros for accessing static global data variables. A later patch in this series will demonstrate its usage in a selftest. As of LLVM-7, this technique only works with 32-bit data, as LLVM will complain if this technique is attempted with data of other sizes: LLVM ERROR: Unsupported relocation: try to compile with -O2 or above, or check your static variable usage Based on the proof of concept by Daniel Borkmann (presented at LPC 2018). Signed-off-by: Joe Stringer --- tools/lib/bpf/libbpf.c | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 1ec28d5154dc..da35d5559b22 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -140,11 +140,13 @@ struct bpf_program { enum { RELO_LD64, RELO_CALL, + RELO_DATA, } type; int insn_idx; union { int map_idx; int text_off; + uint32_t data; }; } *reloc_desc; int nr_reloc; @@ -210,6 +212,7 @@ struct bpf_object { Elf *elf; GElf_Ehdr ehdr; Elf_Data *symbols; + Elf_Data *global_data; size_t strtabidx; struct { GElf_Shdr shdr; @@ -218,6 +221,7 @@ struct bpf_object { int nr_reloc; int maps_shndx; int text_shndx; + int data_shndx; } efile; /* * All loaded bpf_object is linked in a list, which is @@ -476,6 +480,7 @@ static void bpf_object__elf_finish(struct bpf_object *obj) obj->efile.elf = NULL; } obj->efile.symbols = NULL; + obj->efile.global_data = NULL; zfree(&obj->efile.reloc); obj->efile.nr_reloc = 0; @@ -866,6 +871,9 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) pr_warning("failed to alloc program %s (%s): %s", name, obj->path, cp); } + } else if (strcmp(name, ".data") == 0) { + obj->efile.global_data = data; + obj->efile.data_shndx = idx; } } else if (sh.sh_type == SHT_REL) { void *reloc = obj->efile.reloc; @@ -962,6 +970,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, Elf_Data *symbols = obj->efile.symbols; int text_shndx = obj->efile.text_shndx; int maps_shndx = obj->efile.maps_shndx; + int data_shndx = obj->efile.data_shndx; struct bpf_map *maps = obj->maps; size_t nr_maps = obj->nr_maps; int i, nrels; @@ -1000,8 +1009,9 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, (long long) (rel.r_info >> 32), (long long) sym.st_value, sym.st_name); - if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx) { - pr_warning("Program '%s' contains non-map related relo data pointing to section %u\n", + if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx && + sym.st_shndx != data_shndx) { + pr_warning("Program '%s' contains unrecognized relo data pointing to section %u\n", prog->section_name, sym.st_shndx); return -LIBBPF_ERRNO__RELOC; } @@ -1046,6 +1056,20 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, prog->reloc_desc[i].type = RELO_LD64; prog->reloc_desc[i].insn_idx = insn_idx; prog->reloc_desc[i].map_idx = map_idx; + } else if (sym.st_shndx == data_shndx) { + Elf_Data *global_data = obj->efile.global_data; + uint32_t *static_data; + + if (sym.st_value + sizeof(uint32_t) > (int)global_data->d_size) { + pr_warning("bpf relocation: static data load beyond data size %lu\n", + global_data->d_size); + return -LIBBPF_ERRNO__RELOC; + } + + static_data = global_data->d_buf + sym.st_value; + prog->reloc_desc[i].type = RELO_DATA; + prog->reloc_desc[i].insn_idx = insn_idx; + prog->reloc_desc[i].data = *static_data; } } return 0; @@ -1399,6 +1423,12 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) &prog->reloc_desc[i]); if (err) return err; + } else if (prog->reloc_desc[i].type == RELO_DATA) { + struct bpf_insn *insns = prog->insns; + int insn_idx; + + insn_idx = prog->reloc_desc[i].insn_idx; + insns[insn_idx].imm = prog->reloc_desc[i].data; } } -- 2.19.1