Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp6872225ybc; Thu, 28 Nov 2019 06:57:38 -0800 (PST) X-Google-Smtp-Source: APXvYqzl4+R5TxQ3YuktTrG/B2BMSd1OxWbNIEf9a4kLSblPyH8JMl5sOaYZtbR8gieLLpV63xqF X-Received: by 2002:a17:906:4899:: with SMTP id v25mr54909003ejq.235.1574953058846; Thu, 28 Nov 2019 06:57:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574953058; cv=none; d=google.com; s=arc-20160816; b=NyRqLToPibI+w3zqBDZqorpAQexYthNJR2A/ax7N1XYR3tkCDjT5wvW1UuBBcnb0JU Hw5zpNwIdUigtD3EjIPxm6hR3vO2A7osKl1r2SCSOaWarcvAsNW74zS5xV8AJaljbsnJ jOvOf9gsLxcXS3ILLzLpBwQXm8YUx/wdDcuA36NqGOuu+E5KpRjGsP3GEo7AT8P780x3 jI/x+BOYIP3/hnwYHwkkNEkPyz4JLNdKsLjiOzJJQDURyCIhAvfwoPDHGemXpa4AM5rA mG4D+D/MQGKqUEcru8ngpGDCk8+0CSB2fCc5OuXHHqICWIvXWykjyeSz5+blA2U+2S+H FPnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=rSRdu36kdw7cp4aj5uiQyY93zJOiHwsJ9kURwhoRP88=; b=1C9sYBDYZ604YAWBtNjB3GM4O+xgJpaohrlokrbWwysoCY04wHh2b3iMwOH6t9yp3X kiOF0blu1gmRg1S/mIfwFdZ4dv2y1u9T4NH8Rftgplnw0fqPn4peXok+HeKUxFTB/XbH CCudpv7F5qqvGSzYjMa7FsiNLL9Sgi416k0gvDED5ds5ZYWima/AJ+nD5mZ/nI5Ugu1V LpyOBYaMzPW0PQZk2aeC0x6+PaqixFoAzmNseaPo1dciHDe3PCY/39fSjOaTzUJVJFPr SqKsBYEXqU4nauqAmSGJqMMu7NzxGgHRg03Y/3WGc6i9HQGex7SXv2jG3FqltJHKYDzr 4FCw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="OqrDtNi/"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m14si12985799edp.78.2019.11.28.06.57.14; Thu, 28 Nov 2019 06:57:38 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b="OqrDtNi/"; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726909AbfK1Oxb (ORCPT + 99 others); Thu, 28 Nov 2019 09:53:31 -0500 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:37700 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726594AbfK1Oxb (ORCPT ); Thu, 28 Nov 2019 09:53:31 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1574952810; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=rSRdu36kdw7cp4aj5uiQyY93zJOiHwsJ9kURwhoRP88=; b=OqrDtNi/w4ql95WxW9pAzMfWN2FB4wzJHck8v9lqrjTxFwwDM+RFJfKYdri4YkS5F2PBd2 +MfdGd3i9yBmjEr88uTAFyH9sieB6Mx9to2GEtrbf77Qu0/GV9qrx4+xWCm1cF014QgeNt 4FenBwfDlO+oSSvne8SeqPHpUC55ZsY= Received: from mail-lj1-f198.google.com (mail-lj1-f198.google.com [209.85.208.198]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-127-KJXzcXpOOFC5p361d-hRKA-1; Thu, 28 Nov 2019 09:53:28 -0500 Received: by mail-lj1-f198.google.com with SMTP id o20so5096816ljg.0 for ; Thu, 28 Nov 2019 06:53:28 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=DFfjqzHfd87DTRbEbayt6s48Ee8N6vIkSod5kqZnQ5Y=; b=CR9hzg6ZHwcpIeA4bKpRY1qFqJf1/dIDlGObdiwCpLEVLRRwr0pRgHH24Y7Quqr+o5 E0Q4Jhh8yhc1y3AgJauPcUmpTrDUYrnNxJwDil76NjwccygL+3XN0JkuSCub5t82YQK/ lMQFFZkIEeCYSPBS+zg7t9m0uOuRtkXLyO/2yTuXrlFOKRm5L5urUKn8phnyJPyrQl4P lKQg6hwoVC15+st+KhNh+slV3cgcR5AwQNBcOvXQeQkFV7l7Lc+iUwsLyG74xcO78aMs f/TV88eDlwe1i8fsHF22EvnmbNUZL6Q/jAtp6h+XmG3JhjPQynSmjxj+HYFhPQhTBPiY 0xZg== X-Gm-Message-State: APjAAAWlYD9jmYSUbNiBCA4EJT6KYQx3iu0wI6R07K3oi7pw3qEz4VZs Rptox/wJmfaRaeUASPu+NA1uv7P6ECm8m+sQro3PBwgQZGfFt4juLrfikGME7yOmL+i7ZWbaznP C9O1KdJnhGU5+VsqIninpt0sw X-Received: by 2002:a2e:b007:: with SMTP id y7mr34551252ljk.69.1574952807522; Thu, 28 Nov 2019 06:53:27 -0800 (PST) X-Received: by 2002:a2e:b007:: with SMTP id y7mr34551238ljk.69.1574952807282; Thu, 28 Nov 2019 06:53:27 -0800 (PST) Received: from alrua-x1.borgediget.toke.dk ([2a0c:4d80:42:443::2]) by smtp.gmail.com with ESMTPSA id k10sm8611145lfo.76.2019.11.28.06.53.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2019 06:53:26 -0800 (PST) Received: by alrua-x1.borgediget.toke.dk (Postfix, from userid 1000) id 62FC9180339; Thu, 28 Nov 2019 15:53:24 +0100 (CET) From: =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= To: Daniel Borkmann , Alexei Starovoitov Cc: Jiri Olsa , lkml , netdev@vger.kernel.org, bpf@vger.kernel.org, Ingo Molnar , Namhyung Kim , Alexander Shishkin , Peter Zijlstra , Michael Petlan , =?UTF-8?q?Toke=20H=C3=B8iland-J=C3=B8rgensen?= , Jesper Dangaard Brouer , Martin KaFai Lau , Song Liu , Yonghong Song , Andrii Nakryiko , Arnaldo Carvalho de Melo Subject: [PATCH bpf v2] bpftool: Allow to link libbpf dynamically Date: Thu, 28 Nov 2019 15:53:16 +0100 Message-Id: <20191128145316.1044912-1-toke@redhat.com> X-Mailer: git-send-email 2.24.0 In-Reply-To: <20191127094837.4045-1-jolsa@kernel.org> References: MIME-Version: 1.0 X-MC-Unique: KJXzcXpOOFC5p361d-hRKA-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jiri Olsa Currently we support only static linking with kernel's libbpf (tools/lib/bpf). This patch adds LIBBPF_DYNAMIC compile variable that triggers libbpf detection and bpf dynamic linking: $ make -C tools/bpf/bpftool make LIBBPF_DYNAMIC=3D1 If libbpf is not installed, build (with LIBBPF_DYNAMIC=3D1) stops with: $ make -C tools/bpf/bpftool LIBBPF_DYNAMIC=3D1 Auto-detecting system features: ... libbfd: [ on ] ... disassembler-four-args: [ on ] ... zlib: [ on ] ... libbpf: [ OFF ] Makefile:102: *** Error: No libbpf devel library found, please install-de= vel or libbpf-dev. Adding LIBBPF_DIR compile variable to allow linking with libbpf installed into specific directory: $ make -C tools/lib/bpf/ prefix=3D/tmp/libbpf/ install_lib install_header= s $ make -C tools/bpf/bpftool/ LIBBPF_DYNAMIC=3D1 LIBBPF_DIR=3D/tmp/libbpf/ It might be needed to clean build tree first because features framework does not detect the change properly: $ make -C tools/build/feature clean $ make -C tools/bpf/bpftool/ clean Since bpftool uses bits of libbpf that are not exported as public API in the .so version, we also pass in libbpf.a to the linker, which allows it to pick up the private functions from the static library without having to expose them as ABI. Signed-off-by: Jiri Olsa Signed-off-by: Toke H=C3=B8iland-J=C3=B8rgensen --- v2: - Pass .a file to linker when dynamically linking, so bpftool can use private functions from libbpf without exposing them as API. =20 tools/bpf/bpftool/Makefile | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/tools/bpf/bpftool/Makefile b/tools/bpf/bpftool/Makefile index 39bc6f0f4f0b..397051ed9e41 100644 --- a/tools/bpf/bpftool/Makefile +++ b/tools/bpf/bpftool/Makefile @@ -1,6 +1,15 @@ # SPDX-License-Identifier: GPL-2.0-only +# LIBBPF_DYNAMIC to enable libbpf dynamic linking. + include ../../scripts/Makefile.include include ../../scripts/utilities.mak +include ../../scripts/Makefile.arch + +ifeq ($(LP64), 1) + libdir_relative =3D lib64 +else + libdir_relative =3D lib +endif =20 ifeq ($(srctree),) srctree :=3D $(patsubst %/,%,$(dir $(CURDIR))) @@ -55,7 +64,7 @@ ifneq ($(EXTRA_LDFLAGS),) LDFLAGS +=3D $(EXTRA_LDFLAGS) endif =20 -LIBS =3D $(LIBBPF) -lelf -lz +LIBS =3D -lelf -lz =20 INSTALL ?=3D install RM ?=3D rm -f @@ -63,6 +72,19 @@ RM ?=3D rm -f FEATURE_USER =3D .bpftool FEATURE_TESTS =3D libbfd disassembler-four-args reallocarray zlib FEATURE_DISPLAY =3D libbfd disassembler-four-args zlib +ifdef LIBBPF_DYNAMIC + FEATURE_TESTS +=3D libbpf + FEATURE_DISPLAY +=3D libbpf + + # for linking with debug library run: + # make LIBBPF_DYNAMIC=3D1 LIBBPF_DIR=3D/opt/libbpf + ifdef LIBBPF_DIR + LIBBPF_CFLAGS :=3D -I$(LIBBPF_DIR)/include + LIBBPF_LDFLAGS :=3D -L$(LIBBPF_DIR)/$(libdir_relative) + FEATURE_CHECK_CFLAGS-libbpf :=3D $(LIBBPF_CFLAGS) + FEATURE_CHECK_LDFLAGS-libbpf :=3D $(LIBBPF_LDFLAGS) + endif +endif =20 check_feat :=3D 1 NON_CHECK_FEAT_TARGETS :=3D clean uninstall doc doc-clean doc-install doc-= uninstall @@ -88,6 +110,20 @@ ifeq ($(feature-reallocarray), 0) CFLAGS +=3D -DCOMPAT_NEED_REALLOCARRAY endif =20 +ifdef LIBBPF_DYNAMIC + ifeq ($(feature-libbpf), 1) + # bpftool uses non-exported functions from libbpf, so pass both dynami= c and + # static versions and let the linker figure it out + LIBS :=3D -lbpf $(LIBBPF) $(LIBS) + CFLAGS +=3D $(LIBBPF_CFLAGS) + LDFLAGS +=3D $(LIBBPF_LDFLAGS) + else + dummy :=3D $(error Error: No libbpf devel library found, please instal= l-devel or libbpf-dev.) + endif +else + LIBS :=3D $(LIBBPF) $(LIBS) +endif + include $(wildcard $(OUTPUT)*.d) =20 all: $(OUTPUT)bpftool --=20 2.24.0