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=-8.8 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,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 A3781C169C4 for ; Tue, 12 Feb 2019 00:47:42 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6E96B217FA for ; Tue, 12 Feb 2019 00:47:42 +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="VoY2s0PF" 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 Content-Transfer-Encoding: 8bit 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) -- 2.19.1