Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2474693imm; Mon, 28 May 2018 08:46:59 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLwj4I8N+RWNaLnUzJ6MB1zYzB7V+fhrwAYKaqkuGoLpBfAEIChlcWcpXYEAW8/86V3swSB X-Received: by 2002:a65:420d:: with SMTP id c13-v6mr4890034pgq.265.1527522418964; Mon, 28 May 2018 08:46:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527522418; cv=none; d=google.com; s=arc-20160816; b=PsQ3lJdTQPxlPno50wVEM/IyEDSrF5qNealILG9NhcWplAslkM/1pa/lXam/rbbcbf w3EOr0QtG5zLVqyEpjJeNPcqjDq319/Git3SZElbyQ7VaqLQFRyLAnNUyB3hnr344COD NghRfAuDqo1e2BXsvvTv/rwmwtmy8Zj/nxsPwSB0eCJvM+GCQtsA2JUInQTbUZacfbtK 2JT9Vy7rguYkvsrq/KizEyrKviIr1mQCPvYqAk62DgWo5937buX/ZefO48lA32ONdvLy Cxoy2nBcYkk+Lwk+4XEM30qPoTxDB/7u54oVJq8hy4YuByOGHSCoPZCD8dtYUw7iLRHn A25A== 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:dkim-signature :arc-authentication-results; bh=P35p1po+nvIR20FnVeh9J41d9UjyVfd7FePkPhuqf1c=; b=cKZtjVfVHiut2SLJ7lfWiTvm+KitisfZbaS/unOLWPge86iePnVCyfdKuhA6AgpCHE lpQfdNY1C0qmN3YmQll2BXitTt5KxIB2gxgTL2wurOCRF8B0LCU5BkJvPmeFJKMIaTO6 VpgOysbcRituDJQGE7H4lFHU8i6XSQjSLYiCJxE6ZymwrpbpL3tC6nW+yPB+FLLuDEvw 9Z3vHU2VfQ1XLZ2HkhFurSqtenTiHgFTqd4EvTuL2KDRFbD/WWFCJKxTv6PIvpjyNLpm /NvJdZLU4krNMMxkb0hAzZub1pDSjMG1YwWuhMNpvKiq+thEP+iLLKMfs5iyPn0WgCwX 6XUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=viLOISG2; 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 v17-v6si30488393pfg.222.2018.05.28.08.46.44; Mon, 28 May 2018 08:46:58 -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; dkim=pass header.i=@kernel.org header.s=default header.b=viLOISG2; 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 S969161AbeE1PpI (ORCPT + 99 others); Mon, 28 May 2018 11:45:08 -0400 Received: from mail.kernel.org ([198.145.29.99]:37246 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S968626AbeE1KRF (ORCPT ); Mon, 28 May 2018 06:17:05 -0400 Received: from localhost (LFbn-1-12247-202.w90-92.abo.wanadoo.fr [90.92.61.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DE61820843; Mon, 28 May 2018 10:17:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1527502625; bh=8jErKc2oqn71C0TB/xsFWKCcdz1N9cWMW9CgQk5bOmc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=viLOISG2vnhDb7aQORfTSkJX4rPbm1sL2dFoeN6Ab5JJPoKU1JOFuhhfAYIcjLwND u49L0Dh4E2DtIBqUY3fLLWGJSQ+y0owKLY201nKLElViiJL2rJ6GZSAntQ1ENSC5yb wKBuHnYlr4y6IqDfvavdc4uSTWBUe4Q1OCFuqpX0= 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.4 069/268] tools/libbpf: handle issues with bpf ELF objects containing .eh_frames Date: Mon, 28 May 2018 12:00:43 +0200 Message-Id: <20180528100209.680966894@linuxfoundation.org> X-Mailer: git-send-email 2.17.0 In-Reply-To: <20180528100202.045206534@linuxfoundation.org> References: <20180528100202.045206534@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.4-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 @@ -487,6 +487,24 @@ bpf_object__init_maps(struct bpf_object return 0; } +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; @@ -567,6 +585,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);