From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753514AbbEQLC7 (ORCPT ); Sun, 17 May 2015 07:02:59 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:26275 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753245AbbEQK5v (ORCPT ); Sun, 17 May 2015 06:57:51 -0400 From: Wang Nan To: , , , , , , , , , , CC: , , Subject: [RFC PATCH v3 17/37] bpf tools: Collect relocation sections from SHT_REL sections Date: Sun, 17 May 2015 10:56:42 +0000 Message-ID: <1431860222-61636-18-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1431860222-61636-1-git-send-email-wangnan0@huawei.com> References: <1431860222-61636-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.200] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020205.5558741E.0016,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-05-26 15:14:31, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 79bb011e1b0da98d119c0ea78607fb8b Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch collects relocation sections into 'struct object'. Such sections are used for connecting maps to bpf programs. 'reloc' field in 'struct bpf_object' is introduced for storing such informations. This patch simply store the data into 'reloc' field. Following patch will parse them to know the exact instructions which are needed to be relocated. Note that the collected data will be invalid after ELF object file is closed. Signed-off-by: Wang Nan --- tools/lib/bpf/libbpf.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 18b221f..8fd29a9 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -110,6 +110,11 @@ struct bpf_object { Elf *elf; GElf_Ehdr ehdr; Elf_Data *symbols; + struct { + GElf_Shdr shdr; + Elf_Data *data; + } *reloc; + int nr_reloc; } elf; }; #define obj_elf_valid(o) ((o)->elf.fd >= 0) @@ -233,6 +238,12 @@ static void bpf_obj_clear_elf(struct bpf_object *obj) } obj->elf.symbols = NULL; + if (obj->elf.reloc) { + free(obj->elf.reloc); + obj->elf.reloc = NULL; + obj->elf.nr_reloc = 0; + } + if (obj->elf.fd >= 0) { close(obj->elf.fd); obj->elf.fd = -1; @@ -484,6 +495,24 @@ static int bpf_obj_elf_collect(struct bpf_object *obj) } else pr_debug("found program %s\n", prog->section_name); + } else if (sh.sh_type == SHT_REL) { + void *reloc = obj->elf.reloc; + int nr_reloc = obj->elf.nr_reloc; + + reloc = realloc(reloc, + sizeof(*obj->elf.reloc) * (++nr_reloc)); + if (!reloc) { + pr_warning("realloc failed\n"); + err = -ENOMEM; + } else { + int n = nr_reloc - 1; + + obj->elf.reloc = reloc; + obj->elf.nr_reloc = nr_reloc; + + obj->elf.reloc[n].shdr = sh; + obj->elf.reloc[n].data = data; + } } if (err) goto out; -- 1.8.3.4