blob: 13bd3697b1a25be5f04c226426633f9ee0dbae56 [file] [log] [blame]
From aab6e5f524e783fda038c53b04a7a06531797bfd Mon Sep 17 00:00:00 2001
From: Joel Fernandes <joel@joelfernandes.org>
Date: Sun, 8 Jul 2018 22:55:29 -0700
Subject: [PATCH] clang: Allow user to override kernel version
BCC currently requires exactly matching headers. Sometimes this is quite
inconvenient especially if the kernel version is only very slightly
different such as updates in a stable kernel. This patch gives the user
the flexibility to override the the LINUX_VERSION_CODE provided in the
linux kernel headers, so that the eBPF program may load. We also print a
message when this is done, so that the user is warned about the override
happening and that results may be unpredictable.
Signed-off-by: Joel Fernandes <joel@joelfernandes.org>
---
src/cc/export/helpers.h | 4 ++++
src/cc/frontends/clang/loader.cc | 11 +++++++++++
2 files changed, 15 insertions(+)
mode change 100644 => 100755 src/cc/export/helpers.h
mode change 100644 => 100755 src/cc/frontends/clang/loader.cc
diff --git a/src/cc/export/helpers.h b/src/cc/export/helpers.h
old mode 100644
new mode 100755
index a05a9f740603..e4af0bcdd1f5
--- a/src/cc/export/helpers.h
+++ b/src/cc/export/helpers.h
@@ -224,7 +224,11 @@ struct _name##_table_t _name = { .max_entries = (_max_entries) }
char _license[4] SEC("license") = "GPL";
+#ifdef LINUX_VERSION_CODE_OVERRIDE
+unsigned _version SEC("version") = LINUX_VERSION_CODE_OVERRIDE;
+#else
unsigned _version SEC("version") = LINUX_VERSION_CODE;
+#endif
/* helper functions called from eBPF programs written in C */
static void *(*bpf_map_lookup_elem)(void *map, void *key) =
diff --git a/src/cc/frontends/clang/loader.cc b/src/cc/frontends/clang/loader.cc
old mode 100644
new mode 100755
index 72c5843ec808..81e429032551
--- a/src/cc/frontends/clang/loader.cc
+++ b/src/cc/frontends/clang/loader.cc
@@ -112,7 +112,9 @@ int ClangLoader::parse(unique_ptr<llvm::Module> *mod, TableStorage &ts,
uname(&un);
string kdir, kpath;
const char *kpath_env = ::getenv("BCC_KERNEL_SOURCE");
+ const char *version_override = ::getenv("LINUX_VERSION_CODE_OVERRIDE");
bool has_kpath_source = false;
+ string vmacro;
if (kpath_env) {
kpath = string(kpath_env);
@@ -173,6 +175,15 @@ int ClangLoader::parse(unique_ptr<llvm::Module> *mod, TableStorage &ts,
flags_cstr.push_back(it->c_str());
vector<const char *> flags_cstr_rem;
+
+ if (version_override) {
+ vmacro = "-DLINUX_VERSION_CODE_OVERRIDE=" + string(version_override);
+
+ std::cout << "WARNING: Linux version for eBPF program is being overridden with: " << version_override << "\n";
+ std::cout << "WARNING: Due to this, the results of the program may be unpredictable\n";
+ flags_cstr_rem.push_back(vmacro.c_str());
+ }
+
flags_cstr_rem.push_back("-include");
flags_cstr_rem.push_back("/virtual/include/bcc/helpers.h");
flags_cstr_rem.push_back("-isystem");
--
2.18.0.203.gfac676dfb9-goog