Received: by 10.192.165.148 with SMTP id m20csp537236imm; Wed, 25 Apr 2018 03:54:26 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/FEPnuES8Hmqx5xUJQL3IB9nLc5rai66fxnyMjQHbE0hMhhPRuor6AZXiYsmJV/F2MQ5D6 X-Received: by 10.99.185.8 with SMTP id z8mr23311985pge.436.1524653666717; Wed, 25 Apr 2018 03:54:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524653666; cv=none; d=google.com; s=arc-20160816; b=ufxNVAFUjJdHksTib/lT8w9vftJN3DigeheUgRpO/FuVBldqLzhaaool43g0QJqxLJ pnAui5auTf2xmKFsnWIz6bCXzQ7GX0MwNm+PSnvNDsHr/Bo8EuNmQdFMtoLU/WbUWGBl 4pF8dXSaoBdDgCpysgP3CMdz3GGM5X67Cq9vUItgpzMX8lm3ijBoOoKMaksqY2PA1VAo QVE+7JaCyjnMhVAqbnWEE+b5EhB2bOw7GPXDAuRmnhS82pIW0CSwFw2xO93gZX5jm4q8 3riPrZEMu2cNdnLFMnSK+PPcBw4PyWnH0mFlArZT41K6Kh4McWAfpzj1fnnNryTZOaYt yIPw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=d2TmqYvAPuGaqp7SIJLzfa3fPmRystVfbypmJ6pPoic=; b=KUleu5vUudb7pBlfRnF/Bv0CPZO+0hnO2VLNhIgpnsQs5OOnAagCFJ/fL3lWx4SoFW CjaDMPWpptmVTpkqKfg4xmrXBNWgUY5Mmqmo2MQzv6psfbDTfGcgwRrZkJcbkUhdn2aZ +NSO+iJu0pKCR79W/wEQrNZ/kFjxfpwA8gPsnyGaLWaQgoQ1AShatT6bT6VS54XC5/Ic JNIXtm2M03roRlyYwxME5jshHMqmrqF6ovpT6UahEseXMYGkneo9ZdoN/fpX+hNJQ3Dj R9hurq47yiSvBK/hk71/johDLg7ri1MCXqmewHiAlwBWrRDiN+EYErF9b/ybQf7FK7v2 ZQKA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q6si1861801pfh.17.2018.04.25.03.54.12; Wed, 25 Apr 2018 03:54:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754257AbeDYKnr (ORCPT + 99 others); Wed, 25 Apr 2018 06:43:47 -0400 Received: from mail.linuxfoundation.org ([140.211.169.12]:53096 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753921AbeDYKnl (ORCPT ); Wed, 25 Apr 2018 06:43:41 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 1A44F36; Wed, 25 Apr 2018 10:43:40 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jesper Dangaard Brouer , Daniel Borkmann , Sasha Levin Subject: [PATCH 4.14 156/183] tools/libbpf: handle issues with bpf ELF objects containing .eh_frames Date: Wed, 25 Apr 2018 12:36:16 +0200 Message-Id: <20180425103248.808956475@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180425103242.532713678@linuxfoundation.org> References: <20180425103242.532713678@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jesper Dangaard Brouer [ Upstream commit e3d91b0ca523d53158f435a3e13df7f0cb360ea2 ] V3: More generic skipping of relo-section (suggested by Daniel) If clang >= 4.0.1 is missing the option '-target bpf', it will cause llc/llvm to create two ELF sections for "Exception Frames", with section names '.eh_frame' and '.rel.eh_frame'. The BPF ELF loader library libbpf fails when loading files with these sections. The other in-kernel BPF ELF loader in samples/bpf/bpf_load.c, handle this gracefully. And iproute2 loader also seems to work with these "eh" sections. The issue in libbpf is caused by bpf_object__elf_collect() skipping some sections, and later when performing relocation it will be pointing to a skipped section, as these sections cannot be found by bpf_object__find_prog_by_idx() in bpf_object__collect_reloc(). This is a general issue that also occurs for other sections, like debug sections which are also skipped and can have relo section. As suggested by Daniel. To avoid keeping state about all skipped sections, instead perform a direct qlookup in the ELF object. Lookup the section that the relo-section points to and check if it contains executable machine instructions (denoted by the sh_flags SHF_EXECINSTR). Use this check to also skip irrelevant relo-sections. Note, for samples/bpf/ the '-target bpf' parameter to clang cannot be used due to incompatibility with asm embedded headers, that some of the samples include. This is explained in more details by Yonghong Song in bpf_devel_QA. Signed-off-by: Jesper Dangaard Brouer Signed-off-by: Daniel Borkmann Signed-off-by: Sasha Levin Signed-off-by: Greg Kroah-Hartman --- tools/lib/bpf/libbpf.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -661,6 +661,24 @@ bpf_object__init_maps(struct bpf_object return bpf_object__validate_maps(obj); } +static bool section_have_execinstr(struct bpf_object *obj, int idx) +{ + Elf_Scn *scn; + GElf_Shdr sh; + + scn = elf_getscn(obj->efile.elf, idx); + if (!scn) + return false; + + if (gelf_getshdr(scn, &sh) != &sh) + return false; + + if (sh.sh_flags & SHF_EXECINSTR) + return true; + + return false; +} + static int bpf_object__elf_collect(struct bpf_object *obj) { Elf *elf = obj->efile.elf; @@ -742,6 +760,14 @@ static int bpf_object__elf_collect(struc } else if (sh.sh_type == SHT_REL) { void *reloc = obj->efile.reloc; int nr_reloc = obj->efile.nr_reloc + 1; + int sec = sh.sh_info; /* points to other section */ + + /* Only do relo for section with exec instructions */ + if (!section_have_execinstr(obj, sec)) { + pr_debug("skip relo %s(%d) for section(%d)\n", + name, idx, sec); + continue; + } reloc = realloc(reloc, sizeof(*obj->efile.reloc) * nr_reloc);