Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751756AbbD3Kxk (ORCPT ); Thu, 30 Apr 2015 06:53:40 -0400 Received: from szxga03-in.huawei.com ([119.145.14.66]:14325 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751261AbbD3Kxd (ORCPT ); Thu, 30 Apr 2015 06:53:33 -0400 From: Wang Nan To: , , , , , , CC: , , , Subject: [RFC PATCH 06/22] perf bpf: check swap according to EHDR. Date: Thu, 30 Apr 2015 10:52:29 +0000 Message-ID: <1430391165-30267-7-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1430391165-30267-1-git-send-email-wangnan0@huawei.com> References: <1430391165-30267-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.197.210] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020202.554209AA.0164,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: cfa2f915e3883e9b19d0058f61cfc8c3 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2148 Lines: 82 Check endianess according to EHDR to support loading eBPF objects into big endian machines. Code is taken from util/symbol-elf.c. Signed-off-by: Wang Nan --- tools/perf/util/bpf-loader.c | 28 ++++++++++++++++++++++++++++ tools/perf/util/bpf-loader.h | 1 + 2 files changed, 29 insertions(+) diff --git a/tools/perf/util/bpf-loader.c b/tools/perf/util/bpf-loader.c index 3eb7504..14d76f6 100644 --- a/tools/perf/util/bpf-loader.c +++ b/tools/perf/util/bpf-loader.c @@ -76,6 +76,7 @@ static struct bpf_obj *bpf_obj_alloc(const char *path) if (!obj) goto out; + obj->needs_swap = false; obj->elf.fd = -1; return obj; out: @@ -131,6 +132,31 @@ errout: return err; } +static int +bpf_obj_swap_init(struct bpf_obj *obj) +{ + static unsigned int const endian = 1; + + obj->needs_swap = false; + + switch (obj->elf.ehdr.e_ident[EI_DATA]) { + case ELFDATA2LSB: + /* We are big endian, BPF obj is little endian. */ + if (*(unsigned char const *)&endian != 1) + obj->needs_swap = true; + return 0; + + case ELFDATA2MSB: + /* We are little endian, BPF obj is big endian. */ + if (*(unsigned char const *)&endian != 0) + obj->needs_swap = true; + return 0; + + default: + return -EINVAL; + } +} + int bpf__load(const char *path) { struct bpf_obj *obj; @@ -151,6 +177,8 @@ int bpf__load(const char *path) if ((err = bpf_obj_elf_init(obj))) goto out; + if ((err = bpf_obj_swap_init(obj))) + goto out; list_add(&obj->list, &bpf_obj_list); return 0; diff --git a/tools/perf/util/bpf-loader.h b/tools/perf/util/bpf-loader.h index 6a6651b..c27b0ac 100644 --- a/tools/perf/util/bpf-loader.h +++ b/tools/perf/util/bpf-loader.h @@ -22,6 +22,7 @@ struct bpf_obj { /* All bpf objs should be linked together. */ struct list_head list; char *path; + bool needs_swap; /* * Information when doing elf related work. Only valid if fd -- 1.8.3.4 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/