From patchwork Tue Feb 12 00:47:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Stringer X-Patchwork-Id: 1040291 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=wand.net.nz Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="VoY2s0PF"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43z3sW5stPz9sN1 for ; Tue, 12 Feb 2019 11:47:43 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728039AbfBLArl (ORCPT ); Mon, 11 Feb 2019 19:47:41 -0500 Received: from mail-it1-f196.google.com ([209.85.166.196]:54342 "EHLO mail-it1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727791AbfBLArk (ORCPT ); Mon, 11 Feb 2019 19:47:40 -0500 Received: by mail-it1-f196.google.com with SMTP id i145so3063134ita.4; Mon, 11 Feb 2019 16:47:39 -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=2lZYnPJvyC5sEPMBGuVp8FVcSp9r0qdyd3d3LCi1PAk=; b=VoY2s0PFKiUmcqjqYLEQUonFXrMjnUT3mdp0RhmqF20w7F2vhjgm5aj5dRwHBbkhyv oX8ZWOA4lLnsD+Ny/aptWMvXPjcbgZEprcnNnKIKWex+4yBO1g27t4brdZIsZAfzaifP rVfDOxH31ZT8qNRmd3pkAAmO0lWld8guUfCT5dAI+Ka9ph5usPdRbHMCBKikEKoEm3nu IsHHqwhxSz8XuzCEIyrKtylZodBLsQi8CsQCW/+AevgqnmQNl+PVDzfBRcNrV51ngApK BYFxLpmYLExo0i6rZkGWJgeuziizmIeskzJ4cMgg5QLNJnxDLhb0bX/log8XKioeibLZ MMaA== 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=2lZYnPJvyC5sEPMBGuVp8FVcSp9r0qdyd3d3LCi1PAk=; b=l6a1Trv/r+zqQVaGWG+Ktl5GoH2I/eH3QBeWkrg3qPX64Ee3zabXKL3Rn03JGUFB4Z 0ZZV7+DhXzqGEvCbiF3AyVMHFfAeyyW3WTE4b0bc2/4eTbSa9EwGZPMquHxcZ8kYwznq sEwa+DnN01Ks+ZLiIziGF1HpC5ohO5ZBlBrAHHnOiKXJ7u0ZrsttrHJzqRCcMct/jUXh EeLUKK4qTW5uOsr0ITixjunuzLwd31S0nAdMCC3Ks5G+FSSksfKU4x1cxJU5bfIWhlA2 vewhlmycrSDz11j3+VPp/kfVqhUmXOcEPRCqgB1VmqIvgSQ7f6RHbibsmjSiw74fdWEK KazQ== X-Gm-Message-State: AHQUAuYDnHf28mRsQgqtLuNYnLjv474+GLQx0c1NY6VmXu2sX6IaALwN 9iuuhf/P5Yos6M/7rkhGy35fuITC X-Google-Smtp-Source: AHgI3IZ/r4jBKh6AOUl4Em34dFQni0BDBgWPauRDJx4Xks3ktJw/4QnLed7pGMq78Z3++Ges6y73tA== X-Received: by 2002:a5e:d50b:: with SMTP id e11mr559621iom.53.1549932458752; Mon, 11 Feb 2019 16:47:38 -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.37 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Feb 2019 16:47:38 -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 1/4] libbpf: Refactor relocations Date: Mon, 11 Feb 2019 16:47:26 -0800 Message-Id: <20190212004729.535-2-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 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Adjust the code for relocations slightly with no functional changes, so that upcoming patches that will introduce support for relocations into the .data and .bss sections can be added independent of these changes. Signed-off-by: Joe Stringer --- tools/lib/bpf/libbpf.c | 62 ++++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 30 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index e3c39edfb9d3..1ec28d5154dc 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -852,20 +852,20 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) obj->efile.symbols = data; obj->efile.strtabidx = sh.sh_link; } - } else if ((sh.sh_type == SHT_PROGBITS) && - (sh.sh_flags & SHF_EXECINSTR) && - (data->d_size > 0)) { - if (strcmp(name, ".text") == 0) - obj->efile.text_shndx = idx; - err = bpf_object__add_program(obj, data->d_buf, - data->d_size, name, idx); - if (err) { - char errmsg[STRERR_BUFSIZE]; - char *cp = libbpf_strerror_r(-err, errmsg, - sizeof(errmsg)); - - pr_warning("failed to alloc program %s (%s): %s", - name, obj->path, cp); + } else if (sh.sh_type == SHT_PROGBITS && data->d_size > 0) { + if (sh.sh_flags & SHF_EXECINSTR) { + if (strcmp(name, ".text") == 0) + obj->efile.text_shndx = idx; + err = bpf_object__add_program(obj, data->d_buf, + data->d_size, name, idx); + if (err) { + char errmsg[STRERR_BUFSIZE]; + char *cp = libbpf_strerror_r(-err, errmsg, + sizeof(errmsg)); + + pr_warning("failed to alloc program %s (%s): %s", + name, obj->path, cp); + } } } else if (sh.sh_type == SHT_REL) { void *reloc = obj->efile.reloc; @@ -1027,24 +1027,26 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, return -LIBBPF_ERRNO__RELOC; } - /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */ - for (map_idx = 0; map_idx < nr_maps; map_idx++) { - if (maps[map_idx].offset == sym.st_value) { - pr_debug("relocation: find map %zd (%s) for insn %u\n", - map_idx, maps[map_idx].name, insn_idx); - break; + if (sym.st_shndx == maps_shndx) { + /* TODO: 'maps' is sorted. We can use bsearch to make it faster. */ + for (map_idx = 0; map_idx < nr_maps; map_idx++) { + if (maps[map_idx].offset == sym.st_value) { + pr_debug("relocation: find map %zd (%s) for insn %u\n", + map_idx, maps[map_idx].name, insn_idx); + break; + } } - } - if (map_idx >= nr_maps) { - pr_warning("bpf relocation: map_idx %d large than %d\n", - (int)map_idx, (int)nr_maps - 1); - return -LIBBPF_ERRNO__RELOC; - } + if (map_idx >= nr_maps) { + pr_warning("bpf relocation: map_idx %d large than %d\n", + (int)map_idx, (int)nr_maps - 1); + return -LIBBPF_ERRNO__RELOC; + } - prog->reloc_desc[i].type = RELO_LD64; - prog->reloc_desc[i].insn_idx = insn_idx; - prog->reloc_desc[i].map_idx = map_idx; + prog->reloc_desc[i].type = RELO_LD64; + prog->reloc_desc[i].insn_idx = insn_idx; + prog->reloc_desc[i].map_idx = map_idx; + } } return 0; } @@ -1392,7 +1394,7 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) } insns[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; insns[insn_idx].imm = obj->maps[map_idx].fd; - } else { + } else if (prog->reloc_desc[i].type == RELO_CALL) { err = bpf_program__reloc_text(prog, obj, &prog->reloc_desc[i]); if (err) From patchwork Tue Feb 12 00:47:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Stringer X-Patchwork-Id: 1040292 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=wand.net.nz Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="ERsp01pB"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43z3sY4QB1z9sML for ; Tue, 12 Feb 2019 11:47:45 +1100 (AEDT) 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 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; } } From patchwork Tue Feb 12 00:47:28 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Stringer X-Patchwork-Id: 1040293 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=wand.net.nz Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="WqlE4DT6"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43z3sb4tSLz9sML for ; Tue, 12 Feb 2019 11:47:47 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728072AbfBLArq (ORCPT ); Mon, 11 Feb 2019 19:47:46 -0500 Received: from mail-it1-f196.google.com ([209.85.166.196]:39576 "EHLO mail-it1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727791AbfBLArm (ORCPT ); Mon, 11 Feb 2019 19:47:42 -0500 Received: by mail-it1-f196.google.com with SMTP id a6so3154798itl.4; Mon, 11 Feb 2019 16:47:42 -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=aE32RtTDZIDCOZ1D1+gkqVjepLHulcS5q936jT3U5UU=; b=WqlE4DT6FQfmrB8/kp/AS9y/heekktTyaZ2gdUyTwd4+/yiAPWsynmX0AHbSQd0Gc5 LRN0z3w9UeRGeHSIwwtc4I/M7N/SVzwqKhcKaUMDy8d8de+aCIdkokD0/AJmjUS3fTSy xXmDy5/Jm00bSO+rkW17zrDV8+5WjE/Tr2sPd+euu0fFGHWX5ozgRxhCcXBcukB3Z7Hu orgsbNO7KEbBCvd+yGuXIhbmuvtTl60oe33fX8E5xP1JSFm35K7Tc1cR573Wydb4nQex NzIHgexLyfTx8bH1UwEzTZ87axlYVcFVMgPja6NUvRztyKXeuUcbBJU53vo0Q7IRLhI7 vpRQ== 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=aE32RtTDZIDCOZ1D1+gkqVjepLHulcS5q936jT3U5UU=; b=Dfog1NYoIYZCPWzeM5dkDAHhpA7IqO1F2VOwI/C1/Rz9Bix/oYtHk4ZCzu+WjQ/42l qB8JXGEDqRTtHUOA+uVBxwqv+L+v3zHhlBB6Jp1n4TX1HS81GzsT5hfRs0Ffn+Mwq4To AMOrHfUq4o+r7VP8tP9yUDagycL7qty6IlyGq2fo+oYztfDFw3Ar5hXrt+Kb+lB9zj4D 44ttXSf+v7WRFsCpvG7noTAMszebYaoc8xIonH07dxhyW2oqbnKyOINn6kRLAWlATgs0 CqNaifhRTausIfoaqe8ZsE4UKkbvCxCTzUVpQEyhKsNxn9K06hlvrOUD81yOrNAyiykb bpuQ== X-Gm-Message-State: AHQUAuYivF7cZUpBwtrjf1VlpPOLJSahFk5sDLaOZqCP20qJWS0GVowZ XKrY3T5mD1itAtpmRNB3S8Rrdo8U X-Google-Smtp-Source: AHgI3IZyZrINIsNusJmfk0jnxVlJpVNKZYV9AIvyCFaVEnBldW2uBHzI6T2DMbmsckLyvtcIfSEKwA== X-Received: by 2002:a02:8561:: with SMTP id g88mr543843jai.68.1549932461370; Mon, 11 Feb 2019 16:47:41 -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.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Feb 2019 16:47:40 -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 3/4] libbpf: Support relocations for bss. Date: Mon, 11 Feb 2019 16:47:28 -0800 Message-Id: <20190212004729.535-4-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 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The BSS section in an ELF generated by LLVM represents constants for uninitialized variables or variables that are configured with a zero value. Support initializing zeroed static data by parsing the relocations with references to the .bss section and zeroing them. Signed-off-by: Joe Stringer --- tools/lib/bpf/libbpf.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index da35d5559b22..ff66d7e970c9 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -141,6 +141,7 @@ struct bpf_program { RELO_LD64, RELO_CALL, RELO_DATA, + RELO_ZERO, } type; int insn_idx; union { @@ -222,6 +223,7 @@ struct bpf_object { int maps_shndx; int text_shndx; int data_shndx; + int bss_shndx; } efile; /* * All loaded bpf_object is linked in a list, which is @@ -901,6 +903,8 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) obj->efile.reloc[n].shdr = sh; obj->efile.reloc[n].data = data; } + } else if (sh.sh_type == SHT_NOBITS && strcmp(name, ".bss") == 0) { + obj->efile.bss_shndx = idx; } else { pr_debug("skip section(%d) %s\n", idx, name); } @@ -971,6 +975,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, int text_shndx = obj->efile.text_shndx; int maps_shndx = obj->efile.maps_shndx; int data_shndx = obj->efile.data_shndx; + int bss_shndx = obj->efile.bss_shndx; struct bpf_map *maps = obj->maps; size_t nr_maps = obj->nr_maps; int i, nrels; @@ -1010,7 +1015,7 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, (long long) sym.st_value, sym.st_name); if (sym.st_shndx != maps_shndx && sym.st_shndx != text_shndx && - sym.st_shndx != data_shndx) { + sym.st_shndx != data_shndx && sym.st_shndx != bss_shndx) { pr_warning("Program '%s' contains unrecognized relo data pointing to section %u\n", prog->section_name, sym.st_shndx); return -LIBBPF_ERRNO__RELOC; @@ -1070,6 +1075,9 @@ bpf_program__collect_reloc(struct bpf_program *prog, GElf_Shdr *shdr, prog->reloc_desc[i].type = RELO_DATA; prog->reloc_desc[i].insn_idx = insn_idx; prog->reloc_desc[i].data = *static_data; + } else if (sym.st_shndx == bss_shndx) { + prog->reloc_desc[i].type = RELO_ZERO; + prog->reloc_desc[i].insn_idx = insn_idx; } } return 0; @@ -1429,6 +1437,10 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) insn_idx = prog->reloc_desc[i].insn_idx; insns[insn_idx].imm = prog->reloc_desc[i].data; + } else if (prog->reloc_desc[i].type == RELO_ZERO) { + int insn_idx = prog->reloc_desc[i].insn_idx; + + prog->insns[insn_idx].imm = 0; } } From patchwork Tue Feb 12 00:47:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joe Stringer X-Patchwork-Id: 1040294 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=wand.net.nz Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="lX1i9UzO"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 43z3sc3F6Qz9sMr for ; Tue, 12 Feb 2019 11:47:48 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728078AbfBLArr (ORCPT ); Mon, 11 Feb 2019 19:47:47 -0500 Received: from mail-it1-f194.google.com ([209.85.166.194]:40491 "EHLO mail-it1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728050AbfBLAro (ORCPT ); Mon, 11 Feb 2019 19:47:44 -0500 Received: by mail-it1-f194.google.com with SMTP id i2so3125194ite.5; Mon, 11 Feb 2019 16:47:43 -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=lccaXsM6GtqA0x1y9aLbTMuQXwnbt4xZvfRFCA4SW1U=; b=lX1i9UzOeAd/2gbhIBLQnD8zGGwWgk8f5YCTXiWwd+oabNtkqPxzO52bi0rxOUDEQK EfSMU3C9NrDKLkidPTdkTwE9Dyq6Ww/SfvNEjm5x57b7uBn/KQnjcsq/quDfnrv8WbdK mgvEhJgW7Hq46wRRQamJhMP9pORsi1iw3/KM5YZVlCGXMFw1EQjm5nTHgWO9EnHj9XE+ mrnMJN7GWJILPryE4f2qRWdR5NDikn449SadyFMniGmdBWGQeaXhQWw6gWNJNVMmoZ9e LVTR5QEUzk7GjHmnw2ay7VQ0i8ofupEKiXIvaDzoaD9NyxNCVy/mSSqxSs7REdbFMpK4 +nrg== 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=lccaXsM6GtqA0x1y9aLbTMuQXwnbt4xZvfRFCA4SW1U=; b=VyMWlpBPOrjDfqqPREtb2rtZQBi6uTS49wjfkhPwTPLIQSobTMathO+PlST20H1HIl 6l2TD2Hse+rgNXrZ1u5hC9ShcXkzl9qQngYMF6MHwhb5g9qNVrS9qdiB7942fyFyqel2 NNcGZ4UFP5QISNdDL3ZAp6tcA8pV5GoLheGZ97fA3SZX3f0rZPbgEL/qrlrOCqlFIk5c 5tEjAU7gHgeRMo5rL3O/TjfKqceBGN4oZeYOB7DncwezXlQu4YYNBBeZBDsDuD5H1CK3 wPHWo+WzPvgT8Q4FrT5ZwbsScdRzjAM9UUeem5AUdq+96nHUHh+SJfU3gPd1XfKgSHja 4QhQ== X-Gm-Message-State: AHQUAubAAXk1ZK3hOL4RcJs6gTqdp0lIqf748Ovqu3uri2X6v2E1bIbA Ucrjqzcb0rjp4HR4crBaReJIgRyS X-Google-Smtp-Source: AHgI3IahFsXwpc7fdPVb0lh/GB1tlari9L84ks3UBnsa1sGWY+oTShBONBPvtf7dHsTch20ZNRAQyw== X-Received: by 2002:a24:97c3:: with SMTP id k186mr576395ite.125.1549932462490; Mon, 11 Feb 2019 16:47:42 -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.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 11 Feb 2019 16:47:41 -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 4/4] selftests/bpf: Test static data relocation Date: Mon, 11 Feb 2019 16:47:29 -0800 Message-Id: <20190212004729.535-5-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 Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Add tests for libbpf relocation of static variable references into the .data and .bss sections of the ELF. Signed-off-by: Joe Stringer --- tools/testing/selftests/bpf/Makefile | 2 +- tools/testing/selftests/bpf/test_progs.c | 44 +++++++++++++++++ .../selftests/bpf/test_static_data_kern.c | 47 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/bpf/test_static_data_kern.c diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile index c7e1e3255448..ef52a58e2368 100644 --- a/tools/testing/selftests/bpf/Makefile +++ b/tools/testing/selftests/bpf/Makefile @@ -36,7 +36,7 @@ BPF_OBJ_FILES = \ get_cgroup_id_kern.o socket_cookie_prog.o test_select_reuseport_kern.o \ test_skb_cgroup_id_kern.o bpf_flow.o netcnt_prog.o test_xdp_vlan.o \ xdp_dummy.o test_map_in_map.o test_spin_lock.o test_map_lock.o \ - test_sock_fields_kern.o + test_sock_fields_kern.o test_static_data_kern.o # Objects are built with default compilation flags and with sub-register # code-gen enabled. diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c index c52bd90fbb34..72899d58a77c 100644 --- a/tools/testing/selftests/bpf/test_progs.c +++ b/tools/testing/selftests/bpf/test_progs.c @@ -736,6 +736,49 @@ static void test_pkt_md_access(void) bpf_object__close(obj); } +static void test_static_data_access(void) +{ + const char *file = "./test_static_data_kern.o"; + struct bpf_object *obj; + __u32 duration = 0, retval; + int i, err, prog_fd, map_fd; + uint32_t value; + + err = bpf_prog_load(file, BPF_PROG_TYPE_SCHED_CLS, &obj, &prog_fd); + if (CHECK(err, "load program", "error %d loading %s\n", err, file)) + return; + + map_fd = bpf_find_map(__func__, obj, "result"); + if (map_fd < 0) { + error_cnt++; + goto close_prog; + } + + err = bpf_prog_test_run(prog_fd, 1, &pkt_v4, sizeof(pkt_v4), + NULL, NULL, &retval, &duration); + CHECK(err || retval, "pass packet", + "err %d errno %d retval %d duration %d\n", + err, errno, retval, duration); + + struct { + char *name; + uint32_t key; + uint32_t value; + } tests[] = { + { "relocate .bss reference", 0, 0 }, + { "relocate .data reference", 1, 42 }, + }; + for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { + err = bpf_map_lookup_elem(map_fd, &tests[i].key, &value); + CHECK (err || value != tests[i].value, tests[i].name, + "err %d result %d expected %d\n", + err, value, tests[i].value); + } + +close_prog: + bpf_object__close(obj); +} + static void test_obj_name(void) { struct { @@ -2138,6 +2181,7 @@ int main(void) test_flow_dissector(); test_spinlock(); test_map_lock(); + test_static_data_access(); printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt); return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS; diff --git a/tools/testing/selftests/bpf/test_static_data_kern.c b/tools/testing/selftests/bpf/test_static_data_kern.c new file mode 100644 index 000000000000..f2485af6bd0b --- /dev/null +++ b/tools/testing/selftests/bpf/test_static_data_kern.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0 +// Copyright (c) 2019 Isovalent, Inc. + +#include +#include + +#include + +#include "bpf_helpers.h" + +#define NUM_CGROUP_LEVELS 4 + +struct bpf_map_def SEC("maps") result = { + .type = BPF_MAP_TYPE_ARRAY, + .key_size = sizeof(__u32), + .value_size = sizeof(__u32), + .max_entries = 2, +}; + +#define __fetch(x) (__u32)(&(x)) + +static __u32 static_bss = 0; /* Reloc reference to .bss section */ +static __u32 static_data = 42; /* Reloc reference to .data section */ + +/** + * Load a u32 value from a static variable into a map, for the userland test + * program to validate. + */ +SEC("static_data_load") +int load_static_data(struct __sk_buff *skb) +{ + __u32 key, value; + + key = 0; + value = __fetch(static_bss); + bpf_map_update_elem(&result, &key, &value, 0); + + key = 1; + value = __fetch(static_data); + bpf_map_update_elem(&result, &key, &value, 0); + + return TC_ACT_OK; +} + +int _version SEC("version") = 1; + +char _license[] SEC("license") = "GPL";