Skip enabling BTF on kernel older than 4.18
BPF_BTF_LOAD command was introduced in 4.18
Bug: 403381101
Test: boot on 4.9 kernel
Change-Id: Ibf2070750334417bd1052486c1f59a5d3f7fc20c
diff --git a/bpf/loader/NetBpfLoad.cpp b/bpf/loader/NetBpfLoad.cpp
index f11b9a3..d70a2c8 100644
--- a/bpf/loader/NetBpfLoad.cpp
+++ b/bpf/loader/NetBpfLoad.cpp
@@ -856,20 +856,24 @@
ret = getSectionSymNames(elfFile, "maps", mapNames);
if (ret) return ret;
- ret = readSectionByName(".BTF", elfFile, btfData);
- if (ret) {
- ALOGE("Failed to read .BTF section, ret:%d", ret);
- return ret;
- }
- struct btf *btf = btf__new(btfData.data(), btfData.size());
- if (btf == NULL) {
- ALOGE("btf__new failed, errno: %d", errno);
- return -errno;
- }
- auto scopeGuard = base::make_scope_guard([btf] { btf__free(btf); });
+ struct btf *btf = NULL;
+ auto scopeGuard = base::make_scope_guard([btf] { if (btf) btf__free(btf); });
+ if (isAtLeastKernelVersion(4, 18, 0)) {
+ // On Linux Kernels older than 4.18 BPF_BTF_LOAD command doesn't exist.
+ ret = readSectionByName(".BTF", elfFile, btfData);
+ if (ret) {
+ ALOGE("Failed to read .BTF section, ret:%d", ret);
+ return ret;
+ }
+ struct btf *btf = btf__new(btfData.data(), btfData.size());
+ if (btf == NULL) {
+ ALOGE("btf__new failed, errno: %d", errno);
+ return -errno;
+ }
- ret = loadBtf(elfFile, btf);
- if (ret) return ret;
+ ret = loadBtf(elfFile, btf);
+ if (ret) return ret;
+ }
unsigned kvers = kernelVersion();
@@ -997,7 +1001,7 @@
if (isAtLeastKernelVersion(4, 15, 0))
strlcpy(req.map_name, mapNames[i].c_str(), sizeof(req.map_name));
- bool haveBtf = isBtfSupported(type);
+ bool haveBtf = btf && isBtfSupported(type);
if (haveBtf) {
uint32_t kTid, vTid;
ret = getKeyValueTids(btf, mapNames[i].c_str(), md[i].key_size,