From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 02D9D1B0F1E for ; Wed, 22 Jan 2025 06:24:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737527096; cv=none; b=D1E5HTVwh246bh4pHqbqlZLmuCjtpGrhfng2GwZJIHYFSMMyxJyGqhoV/MHNvKVIJfEyCtbdPjj6X7sLMqXi+91ajySkiCg1kVjPgyjPXNFUFPAzDyzs03va74NWzNLGkIng0+jPuTDBcwKGpdrNVJtN01TjvIr8Nvriq3UHLug= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737527096; c=relaxed/simple; bh=jZ4IkRMbc56FrM1l0SIjkdbfl4Olsr1QAGf6JHIVKBU=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=U3/0zy47YXeEKhRhpl/rz0yMmCnnjzWwL2Ow0mjHvCwyJK9ZnyPIn4eVUU+HHDMP8V9pwnxipwaBw6hKVE4q26WuX0e420RreGUUb/Nvtx9okpGwVFMl9z271eTWvVtH9RP/MjtGAZkOCJxaScR3O9Z3wn0BzennICXK8lzwAW4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=cnPFxEok; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="cnPFxEok" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e54cb50c3baso1583719276.0 for ; Tue, 21 Jan 2025 22:24:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737527094; x=1738131894; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=voJ/Pq/GvJ/AGxjOFOtV9ZWeGm6Z21/zyADHQU1xORI=; b=cnPFxEokuVIaRdJ13k6bGdjalik9aTqYFe25+VOsRS6AMi2q4JvArcVeJAIPTok/ok 3yA7scf9JXWhnscBSg8xsrpuAB2blZrv7yy+jSwfca1pNTdNekm+zen80KOiVQF3pgDa EotPTI5XkppmJ0qtUPjd+ZjuhKauXaaMnPAbHFxzPDTaaJ/XooHsk44vDTapNQy/jM09 D37PYxI+6u12LPt941CvAV92AWl5C/4fGIshSnAMlIqZfFKS8/nFew7rKRC49adowp6W dgCQxLefgf6v0JUhQCpUlGhkKBhtZCuIY77vq7pB2Q7E9mpFIaEK64MBDbKKzmqT16OR qbCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737527094; x=1738131894; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=voJ/Pq/GvJ/AGxjOFOtV9ZWeGm6Z21/zyADHQU1xORI=; b=LnzZYGAkbUpPMfnx6137px73csWfkj0V/pdji2NLQXVURUxKPK6BQryLvQqxpxEjHm bgQ4qMRsJSW33uPn7dEYchU/3FkVaHXdyDUR2uDRcLG71LmqsYMAEYMGtL8n8cXFQViQ /k+Qyj3PmL/+pQnXTKD3OcCoFhjuDPRkHk51zb/MwgK5biP+EZRS0aUaV4nU61OP299V Av3pK88cUijc+p0YkJR89y8afXIAOMReKnYWw6LBkxDqHdXXSOVD6SjuJDX8UZqjkzMn gwu/NqzV3hm82d+51T7M8ZiXMuMslXetNdwb0Z6Qk7febI1KoOWU48hvZAgB8XH6dAhb 5j6A== X-Forwarded-Encrypted: i=1; AJvYcCXbtTTuK/olqPrZlD3YH3EwUtsNWkwu0PJ8El0bVKpZpvNTKgIfKgCvGv1o2Knv23UhVqTDGh+/6TmyFIFsZ4wZ@vger.kernel.org X-Gm-Message-State: AOJu0YydO3NjCnrVR7FUcdg5l1vExmAoKgsVQ0Bpt3+yQsKWek7YA6Vu vMEI0UlZ697QCuSOPlJ2aAP01uBe81A5rFwrGBiCP6hMofKSH1DrZOsyz2wz/MkuTfzLwUSD3P9 TrziQiw== X-Google-Smtp-Source: AGHT+IGGtA0A2c0NnRtlqmsTYya1bjH/d2LCz7w5U/h3QISaG3Lb0h0KMLBS05lL6WSmZVrAohMkiUR9M9Hj X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:807b:be79:d5c3:ee5c]) (user=irogers job=sendgmr) by 2002:a25:ace7:0:b0:e57:965e:37ac with SMTP id 3f1490d57ef6-e57965e39fcmr71558276.0.1737527093964; Tue, 21 Jan 2025 22:24:53 -0800 (PST) Date: Tue, 21 Jan 2025 22:23:23 -0800 In-Reply-To: <20250122062332.577009-1-irogers@google.com> Message-Id: <20250122062332.577009-9-irogers@google.com> Precedence: bulk X-Mailing-List: linux-perf-users@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250122062332.577009-1-irogers@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Subject: [PATCH v2 08/17] perf llvm: Mangle libperf-llvm.so function names From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Aditya Gupta , "Steinar H. Gunderson" , Charlie Jenkins , Changbin Du , "Masami Hiramatsu (Google)" , James Clark , Kajol Jain , Athira Rajeev , Li Huafei , Dmitry Vyukov , Andi Kleen , Chaitanya S Prakash , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev, Song Liu , bpf@vger.kernel.org Content-Type: text/plain; charset="UTF-8" For a function like llvm_addr2line having the libperf-llvm.so exported symbol named llvm_addr2line meant that the perf llvm_addr2line could sometimes erroneously be returned. This led to infinite recursion and eventual stack overflow. To avoid this conflict add a new BUILDING_PERF_LLVMSO when libperf-llvm.so is being built and use it to alter the behavior of MANGLE_PERF_LLVM_API, a macro that prefixes the name when libperf-llvm.so is being built. The prefixed named avoids the name collision. Signed-off-by: Ian Rogers --- tools/perf/Makefile.perf | 3 ++- tools/perf/util/llvm-c-helpers.cpp | 29 ++++++++++++++++++----------- tools/perf/util/llvm-c-helpers.h | 24 ++++++++++++++++-------- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/tools/perf/Makefile.perf b/tools/perf/Makefile.perf index eae77f6af59d..a2886abd4f02 100644 --- a/tools/perf/Makefile.perf +++ b/tools/perf/Makefile.perf @@ -1026,7 +1026,8 @@ $(LIBSYMBOL)-clean: $(Q)$(RM) -r -- $(LIBSYMBOL_OUTPUT) ifdef LIBLLVM_DYNAMIC -LIBPERF_LLVM_CXXFLAGS := $(call filter-out,-DHAVE_LIBLLVM_DYNAMIC,$(CXXFLAGS)) -DHAVE_LIBLLVM_SUPPORT +LIBPERF_LLVM_CXXFLAGS := $(call filter-out,-DHAVE_LIBLLVM_DYNAMIC,$(CXXFLAGS)) +LIBPERF_LLVM_CXXFLAGS += -DHAVE_LIBLLVM_SUPPORT -DBUILDING_PERF_LLVMSO LIBPERF_LLVM_LIBS = -L$(shell $(LLVM_CONFIG) --libdir) $(LIBLLVM) -lstdc++ $(OUTPUT)$(LIBPERF_LLVM): util/llvm-c-helpers.cpp diff --git a/tools/perf/util/llvm-c-helpers.cpp b/tools/perf/util/llvm-c-helpers.cpp index 5a6f76e6b705..8cea380be5c2 100644 --- a/tools/perf/util/llvm-c-helpers.cpp +++ b/tools/perf/util/llvm-c-helpers.cpp @@ -99,10 +99,12 @@ static int extract_file_and_line(const DILineInfo &line_info, char **file, #endif extern "C" -int llvm_addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_unused, - char **file __maybe_unused, unsigned int *line __maybe_unused, - bool unwind_inlines __maybe_unused, - llvm_a2l_frame **inline_frames __maybe_unused) +int MANGLE_PERF_LLVM_API(llvm_addr2line)(const char *dso_name __maybe_unused, + u64 addr __maybe_unused, + char **file __maybe_unused, + unsigned int *line __maybe_unused, + bool unwind_inlines __maybe_unused, + llvm_a2l_frame **inline_frames __maybe_unused) { #if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) LLVMSymbolizer *symbolizer = get_symbolizer(); @@ -177,7 +179,8 @@ int llvm_addr2line(const char *dso_name __maybe_unused, u64 addr __maybe_unused, if (!handle) return 0; - fn = reinterpret_cast(dlsym(handle, "llvm_addr2line")); + fn = reinterpret_cast( + dlsym(handle, MANGLE_PERF_LLVM_API_STR(llvm_addr2line))); if (!fn) pr_debug("dlsym failed for llvm_addr2line\n"); fn_init = true; @@ -215,8 +218,9 @@ make_symbol_relative_string(struct dso *dso, const char *sym_name, #endif extern "C" -char *llvm_name_for_code(struct dso *dso __maybe_unused, const char *dso_name __maybe_unused, - u64 addr __maybe_unused) +char *MANGLE_PERF_LLVM_API(llvm_name_for_code)(struct dso *dso __maybe_unused, + const char *dso_name __maybe_unused, + u64 addr __maybe_unused) { #if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) LLVMSymbolizer *symbolizer = get_symbolizer(); @@ -242,7 +246,8 @@ char *llvm_name_for_code(struct dso *dso __maybe_unused, const char *dso_name __ if (!handle) return NULL; - fn = reinterpret_cast(dlsym(handle, "llvm_name_for_code")); + fn = reinterpret_cast( + dlsym(handle, MANGLE_PERF_LLVM_API_STR(llvm_name_for_code))); if (!fn) pr_debug("dlsym failed for llvm_name_for_code\n"); fn_init = true; @@ -256,8 +261,9 @@ char *llvm_name_for_code(struct dso *dso __maybe_unused, const char *dso_name __ } extern "C" -char *llvm_name_for_data(struct dso *dso __maybe_unused, const char *dso_name __maybe_unused, - u64 addr __maybe_unused) +char *MANGLE_PERF_LLVM_API(llvm_name_for_data)(struct dso *dso __maybe_unused, + const char *dso_name __maybe_unused, + u64 addr __maybe_unused) { #if defined(HAVE_LIBLLVM_SUPPORT) && !defined(HAVE_LIBLLVM_DYNAMIC) LLVMSymbolizer *symbolizer = get_symbolizer(); @@ -283,7 +289,8 @@ char *llvm_name_for_data(struct dso *dso __maybe_unused, const char *dso_name __ if (!handle) return NULL; - fn = reinterpret_cast(dlsym(handle, "llvm_name_for_data")); + fn = reinterpret_cast( + dlsym(handle, MANGLE_PERF_LLVM_API_STR(llvm_name_for_data))); if (!fn) pr_debug("dlsym failed for llvm_name_for_data\n"); fn_init = true; diff --git a/tools/perf/util/llvm-c-helpers.h b/tools/perf/util/llvm-c-helpers.h index d2b99637a28a..cfcfd540cdae 100644 --- a/tools/perf/util/llvm-c-helpers.h +++ b/tools/perf/util/llvm-c-helpers.h @@ -13,6 +13,14 @@ extern "C" { #endif +/* Support name mangling so that libperf_llvm.so's names don't match those in perf. */ +#ifdef BUILDING_PERF_LLVMSO +#define MANGLE_PERF_LLVM_API(x) PERF_LLVM_SO_ ## x +#else +#define MANGLE_PERF_LLVM_API(x) x +#endif +#define MANGLE_PERF_LLVM_API_STR(x) "PERF_LLVM_SO_" #x + struct dso; struct llvm_a2l_frame { @@ -37,12 +45,12 @@ struct llvm_a2l_frame { * a newly allocated array with that length. The caller is then responsible * for freeing both the strings and the array itself. */ -int llvm_addr2line(const char* dso_name, - u64 addr, - char** file, - unsigned int* line, - bool unwind_inlines, - struct llvm_a2l_frame** inline_frames); +int MANGLE_PERF_LLVM_API(llvm_addr2line)(const char *dso_name, + u64 addr, + char **file, + unsigned int *line, + bool unwind_inlines, + struct llvm_a2l_frame **inline_frames); /* * Simple symbolizers for addresses; will convert something like @@ -50,8 +58,8 @@ int llvm_addr2line(const char* dso_name, * * The returned value must be freed by the caller, with free(). */ -char *llvm_name_for_code(struct dso *dso, const char *dso_name, u64 addr); -char *llvm_name_for_data(struct dso *dso, const char *dso_name, u64 addr); +char *MANGLE_PERF_LLVM_API(llvm_name_for_code)(struct dso *dso, const char *dso_name, u64 addr); +char *MANGLE_PERF_LLVM_API(llvm_name_for_data)(struct dso *dso, const char *dso_name, u64 addr); #ifdef __cplusplus } -- 2.48.0.rc2.279.g1de40edade-goog