blob: eb48cd087aa473fe97ebc793460229f83968fe18 [file] [log] [blame]
Joel Fernandesd76a2002018-10-16 13:19:58 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "LibBpfLoader"
18
19#include <errno.h>
20#include <linux/bpf.h>
21#include <linux/elf.h>
22#include <log/log.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Connor O'Brien35425e52022-01-18 21:41:16 -080027#include <sysexits.h>
Maciej Żenczykowski83f29772020-01-27 03:11:51 -080028#include <sys/stat.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070029#include <sys/utsname.h>
Connor O'Brien35425e52022-01-18 21:41:16 -080030#include <sys/wait.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070031#include <unistd.h>
32
Ken Chen6d697842022-01-17 17:22:34 +080033// This is BpfLoader v0.9
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -080034#define BPFLOADER_VERSION_MAJOR 0u
Ken Chen6d697842022-01-17 17:22:34 +080035#define BPFLOADER_VERSION_MINOR 9u
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -080036#define BPFLOADER_VERSION ((BPFLOADER_VERSION_MAJOR << 16) | BPFLOADER_VERSION_MINOR)
37
Maciej Żenczykowski07375e22020-02-19 14:23:59 -080038#include "bpf/BpfUtils.h"
Ken Chend5689472021-12-20 18:07:21 +080039#include "bpf/bpf_map_def.h"
Joel Fernandesd76a2002018-10-16 13:19:58 -070040#include "include/libbpf_android.h"
41
Connor O'Brien35425e52022-01-18 21:41:16 -080042#include <bpf/bpf.h>
43
Joel Fernandesd76a2002018-10-16 13:19:58 -070044#include <cstdlib>
45#include <fstream>
46#include <iostream>
Connor O'Brien3278a162020-02-13 21:45:22 -080047#include <optional>
Joel Fernandesd76a2002018-10-16 13:19:58 -070048#include <string>
Connor O'Brien35425e52022-01-18 21:41:16 -080049#include <unordered_map>
Christopher Ferrisc151c672019-02-01 15:31:26 -080050#include <vector>
Joel Fernandesd76a2002018-10-16 13:19:58 -070051
Connor O'Brien35425e52022-01-18 21:41:16 -080052#include <android-base/cmsg.h>
53#include <android-base/file.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070054#include <android-base/strings.h>
Connor O'Brien8d49fc72019-10-24 18:23:49 -070055#include <android-base/unique_fd.h>
Joel Fernandesd76a2002018-10-16 13:19:58 -070056
57#define BPF_FS_PATH "/sys/fs/bpf/"
58
59// Size of the BPF log buffer for verifier logging
Stephane Leeeb61b732021-10-21 17:03:11 -070060#define BPF_LOAD_LOG_SZ 0xfffff
Joel Fernandesd76a2002018-10-16 13:19:58 -070061
62using android::base::StartsWith;
Connor O'Brien8d49fc72019-10-24 18:23:49 -070063using android::base::unique_fd;
Joel Fernandesd76a2002018-10-16 13:19:58 -070064using std::ifstream;
65using std::ios;
Connor O'Brien3278a162020-02-13 21:45:22 -080066using std::optional;
Christopher Ferrisc151c672019-02-01 15:31:26 -080067using std::string;
Joel Fernandesd76a2002018-10-16 13:19:58 -070068using std::vector;
69
70namespace android {
71namespace bpf {
72
Maciej Żenczykowskif7c0d992021-01-21 16:01:04 -080073static string pathToFilename(const string& path, bool noext = false) {
74 vector<string> spath = android::base::Split(path, "/");
75 string ret = spath.back();
76
77 if (noext) {
78 size_t lastindex = ret.find_last_of('.');
79 return ret.substr(0, lastindex);
80 }
81 return ret;
82}
83
Joel Fernandesd76a2002018-10-16 13:19:58 -070084typedef struct {
85 const char* name;
86 enum bpf_prog_type type;
87} sectionType;
88
89/*
90 * Map section name prefixes to program types, the section name will be:
Maciej Żenczykowski3adb1d52021-10-22 19:27:10 -070091 * SECTION(<prefix>/<name-of-program>)
Joel Fernandesd76a2002018-10-16 13:19:58 -070092 * For example:
Maciej Żenczykowski3adb1d52021-10-22 19:27:10 -070093 * SECTION("tracepoint/sched_switch_func") where sched_switch_funcs
Joel Fernandesd76a2002018-10-16 13:19:58 -070094 * is the name of the program, and tracepoint is the type.
Maciej Żenczykowski3adb1d52021-10-22 19:27:10 -070095 *
96 * However, be aware that you should not be directly using the SECTION() macro.
97 * Instead use the DEFINE_(BPF|XDP)_(PROG|MAP)... & LICENSE/CRITICAL macros.
Joel Fernandesd76a2002018-10-16 13:19:58 -070098 */
99sectionType sectionNameTypes[] = {
Maciej Żenczykowski2b203132021-11-18 15:13:36 -0800100 {"cgroupskb/", BPF_PROG_TYPE_CGROUP_SKB},
101 {"cgroupsock/", BPF_PROG_TYPE_CGROUP_SOCK},
102 {"cgroupsockaddr/", BPF_PROG_TYPE_CGROUP_SOCK_ADDR},
103 {"kprobe/", BPF_PROG_TYPE_KPROBE},
104 {"schedact/", BPF_PROG_TYPE_SCHED_ACT},
105 {"schedcls/", BPF_PROG_TYPE_SCHED_CLS},
106 {"skfilter/", BPF_PROG_TYPE_SOCKET_FILTER},
107 {"tracepoint/", BPF_PROG_TYPE_TRACEPOINT},
108 {"xdp/", BPF_PROG_TYPE_XDP},
Joel Fernandesd76a2002018-10-16 13:19:58 -0700109};
110
111typedef struct {
112 enum bpf_prog_type type;
113 string name;
114 vector<char> data;
115 vector<char> rel_data;
Connor O'Brien3278a162020-02-13 21:45:22 -0800116 optional<struct bpf_prog_def> prog_def;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700117
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700118 unique_fd prog_fd; /* fd after loading */
Joel Fernandesd76a2002018-10-16 13:19:58 -0700119} codeSection;
120
Joel Fernandesd76a2002018-10-16 13:19:58 -0700121static int readElfHeader(ifstream& elfFile, Elf64_Ehdr* eh) {
122 elfFile.seekg(0);
123 if (elfFile.fail()) return -1;
124
125 if (!elfFile.read((char*)eh, sizeof(*eh))) return -1;
126
127 return 0;
128}
129
130/* Reads all section header tables into an Shdr array */
131static int readSectionHeadersAll(ifstream& elfFile, vector<Elf64_Shdr>& shTable) {
132 Elf64_Ehdr eh;
133 int ret = 0;
134
135 ret = readElfHeader(elfFile, &eh);
136 if (ret) return ret;
137
138 elfFile.seekg(eh.e_shoff);
139 if (elfFile.fail()) return -1;
140
141 /* Read shdr table entries */
142 shTable.resize(eh.e_shnum);
143
144 if (!elfFile.read((char*)shTable.data(), (eh.e_shnum * eh.e_shentsize))) return -ENOMEM;
145
146 return 0;
147}
148
149/* Read a section by its index - for ex to get sec hdr strtab blob */
150static int readSectionByIdx(ifstream& elfFile, int id, vector<char>& sec) {
151 vector<Elf64_Shdr> shTable;
Maciej Żenczykowskid56ec052021-01-15 00:27:04 -0800152 int ret = readSectionHeadersAll(elfFile, shTable);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700153 if (ret) return ret;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700154
155 elfFile.seekg(shTable[id].sh_offset);
156 if (elfFile.fail()) return -1;
157
158 sec.resize(shTable[id].sh_size);
159 if (!elfFile.read(sec.data(), shTable[id].sh_size)) return -1;
160
161 return 0;
162}
163
164/* Read whole section header string table */
165static int readSectionHeaderStrtab(ifstream& elfFile, vector<char>& strtab) {
166 Elf64_Ehdr eh;
Maciej Żenczykowskid56ec052021-01-15 00:27:04 -0800167 int ret = readElfHeader(elfFile, &eh);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700168 if (ret) return ret;
169
170 ret = readSectionByIdx(elfFile, eh.e_shstrndx, strtab);
171 if (ret) return ret;
172
173 return 0;
174}
175
176/* Get name from offset in strtab */
177static int getSymName(ifstream& elfFile, int nameOff, string& name) {
178 int ret;
179 vector<char> secStrTab;
180
181 ret = readSectionHeaderStrtab(elfFile, secStrTab);
182 if (ret) return ret;
183
184 if (nameOff >= (int)secStrTab.size()) return -1;
185
186 name = string((char*)secStrTab.data() + nameOff);
187 return 0;
188}
189
190/* Reads a full section by name - example to get the GPL license */
191static int readSectionByName(const char* name, ifstream& elfFile, vector<char>& data) {
192 vector<char> secStrTab;
193 vector<Elf64_Shdr> shTable;
194 int ret;
195
196 ret = readSectionHeadersAll(elfFile, shTable);
197 if (ret) return ret;
198
199 ret = readSectionHeaderStrtab(elfFile, secStrTab);
200 if (ret) return ret;
201
202 for (int i = 0; i < (int)shTable.size(); i++) {
203 char* secname = secStrTab.data() + shTable[i].sh_name;
204 if (!secname) continue;
205
206 if (!strcmp(secname, name)) {
207 vector<char> dataTmp;
208 dataTmp.resize(shTable[i].sh_size);
209
210 elfFile.seekg(shTable[i].sh_offset);
211 if (elfFile.fail()) return -1;
212
213 if (!elfFile.read((char*)dataTmp.data(), shTable[i].sh_size)) return -1;
214
215 data = dataTmp;
216 return 0;
217 }
218 }
219 return -2;
220}
221
Maciej Żenczykowski7ed94ef2021-07-06 01:47:15 -0700222unsigned int readSectionUint(const char* name, ifstream& elfFile, unsigned int defVal) {
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800223 vector<char> theBytes;
224 int ret = readSectionByName(name, elfFile, theBytes);
225 if (ret) {
226 ALOGD("Couldn't find section %s (defaulting to %u [0x%x]).\n", name, defVal, defVal);
227 return defVal;
228 } else if (theBytes.size() < sizeof(unsigned int)) {
229 ALOGE("Section %s too short (defaulting to %u [0x%x]).\n", name, defVal, defVal);
230 return defVal;
231 } else {
232 // decode first 4 bytes as LE32 uint, there will likely be more bytes due to alignment.
233 unsigned int value = static_cast<unsigned char>(theBytes[3]);
234 value <<= 8;
235 value += static_cast<unsigned char>(theBytes[2]);
236 value <<= 8;
237 value += static_cast<unsigned char>(theBytes[1]);
238 value <<= 8;
239 value += static_cast<unsigned char>(theBytes[0]);
240 ALOGI("Section %s value is %u [0x%x]\n", name, value, value);
241 return value;
242 }
243}
244
Joel Fernandesd76a2002018-10-16 13:19:58 -0700245static int readSectionByType(ifstream& elfFile, int type, vector<char>& data) {
246 int ret;
247 vector<Elf64_Shdr> shTable;
248
249 ret = readSectionHeadersAll(elfFile, shTable);
250 if (ret) return ret;
251
252 for (int i = 0; i < (int)shTable.size(); i++) {
253 if ((int)shTable[i].sh_type != type) continue;
254
255 vector<char> dataTmp;
256 dataTmp.resize(shTable[i].sh_size);
257
258 elfFile.seekg(shTable[i].sh_offset);
259 if (elfFile.fail()) return -1;
260
261 if (!elfFile.read((char*)dataTmp.data(), shTable[i].sh_size)) return -1;
262
263 data = dataTmp;
264 return 0;
265 }
266 return -2;
267}
268
269static bool symCompare(Elf64_Sym a, Elf64_Sym b) {
270 return (a.st_value < b.st_value);
271}
272
273static int readSymTab(ifstream& elfFile, int sort, vector<Elf64_Sym>& data) {
274 int ret, numElems;
275 Elf64_Sym* buf;
276 vector<char> secData;
277
278 ret = readSectionByType(elfFile, SHT_SYMTAB, secData);
279 if (ret) return ret;
280
281 buf = (Elf64_Sym*)secData.data();
282 numElems = (secData.size() / sizeof(Elf64_Sym));
283 data.assign(buf, buf + numElems);
284
285 if (sort) std::sort(data.begin(), data.end(), symCompare);
286 return 0;
287}
288
289static enum bpf_prog_type getSectionType(string& name) {
Maciej Żenczykowski2b203132021-11-18 15:13:36 -0800290 for (auto& snt : sectionNameTypes)
291 if (StartsWith(name, snt.name)) return snt.type;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700292
Paul Lawrence9548f9f2021-11-09 16:32:43 +0000293 // TODO Remove this code when fuse-bpf is upstream and this BPF_PROG_TYPE_FUSE is fixed
294 if (StartsWith(name, "fuse/")) {
295 int result = BPF_PROG_TYPE_UNSPEC;
296 ifstream("/sys/fs/fuse/bpf_prog_type_fuse") >> result;
297 return static_cast<bpf_prog_type>(result);
298 }
299
Joel Fernandesd76a2002018-10-16 13:19:58 -0700300 return BPF_PROG_TYPE_UNSPEC;
301}
302
303/* If ever needed
304static string getSectionName(enum bpf_prog_type type)
305{
Maciej Żenczykowski2b203132021-11-18 15:13:36 -0800306 for (auto& snt : sectionNameTypes)
307 if (snt.type == type)
308 return string(snt.name);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700309
310 return NULL;
311}
312*/
313
314static bool isRelSection(codeSection& cs, string& name) {
Maciej Żenczykowski2b203132021-11-18 15:13:36 -0800315 for (auto& snt : sectionNameTypes) {
316 if (snt.type != cs.type) continue;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700317
Maciej Żenczykowski2b203132021-11-18 15:13:36 -0800318 if (StartsWith(name, string(".rel") + snt.name))
Joel Fernandesd76a2002018-10-16 13:19:58 -0700319 return true;
320 else
321 return false;
322 }
323 return false;
324}
325
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800326static int readProgDefs(ifstream& elfFile, vector<struct bpf_prog_def>& pd,
327 size_t sizeOfBpfProgDef) {
Connor O'Brien3278a162020-02-13 21:45:22 -0800328 vector<char> pdData;
329 int ret = readSectionByName("progs", elfFile, pdData);
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800330 // Older file formats do not require a 'progs' section at all.
331 // (We should probably figure out whether this is behaviour which is safe to remove now.)
Connor O'Brien3278a162020-02-13 21:45:22 -0800332 if (ret == -2) return 0;
333 if (ret) return ret;
334
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800335 if (pdData.size() % sizeOfBpfProgDef) {
336 ALOGE("readProgDefs failed due to improper sized progs section, %zu %% %zu != 0\n",
337 pdData.size(), sizeOfBpfProgDef);
338 return -1;
339 };
340
341 int progCount = pdData.size() / sizeOfBpfProgDef;
342 pd.resize(progCount);
343 size_t trimmedSize = std::min(sizeOfBpfProgDef, sizeof(struct bpf_prog_def));
344
345 const char* dataPtr = pdData.data();
346 for (auto& p : pd) {
347 // First we zero initialize
348 memset(&p, 0, sizeof(p));
349 // Then we set non-zero defaults
350 p.bpfloader_max_ver = DEFAULT_BPFLOADER_MAX_VER; // v1.0
351 // Then we copy over the structure prefix from the ELF file.
352 memcpy(&p, dataPtr, trimmedSize);
353 // Move to next struct in the ELF file
354 dataPtr += sizeOfBpfProgDef;
355 }
Connor O'Brien3278a162020-02-13 21:45:22 -0800356 return 0;
357}
358
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800359static int getSectionSymNames(ifstream& elfFile, const string& sectionName, vector<string>& names,
360 optional<unsigned> symbolType = std::nullopt) {
Connor O'Brien3278a162020-02-13 21:45:22 -0800361 int ret;
362 string name;
363 vector<Elf64_Sym> symtab;
364 vector<Elf64_Shdr> shTable;
365
366 ret = readSymTab(elfFile, 1 /* sort */, symtab);
367 if (ret) return ret;
368
369 /* Get index of section */
370 ret = readSectionHeadersAll(elfFile, shTable);
371 if (ret) return ret;
372
373 int sec_idx = -1;
374 for (int i = 0; i < (int)shTable.size(); i++) {
375 ret = getSymName(elfFile, shTable[i].sh_name, name);
376 if (ret) return ret;
377
378 if (!name.compare(sectionName)) {
379 sec_idx = i;
380 break;
381 }
382 }
383
384 /* No section found with matching name*/
385 if (sec_idx == -1) {
Maciej Żenczykowski21f34cb2020-07-20 18:44:33 -0700386 ALOGW("No %s section could be found in elf object\n", sectionName.c_str());
Connor O'Brien3278a162020-02-13 21:45:22 -0800387 return -1;
388 }
389
390 for (int i = 0; i < (int)symtab.size(); i++) {
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800391 if (symbolType.has_value() && ELF_ST_TYPE(symtab[i].st_info) != symbolType) continue;
392
Connor O'Brien3278a162020-02-13 21:45:22 -0800393 if (symtab[i].st_shndx == sec_idx) {
394 string s;
395 ret = getSymName(elfFile, symtab[i].st_name, s);
396 if (ret) return ret;
397 names.push_back(s);
398 }
399 }
400
401 return 0;
402}
403
Joel Fernandesd76a2002018-10-16 13:19:58 -0700404/* Read a section by its index - for ex to get sec hdr strtab blob */
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800405static int readCodeSections(ifstream& elfFile, vector<codeSection>& cs, size_t sizeOfBpfProgDef) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700406 vector<Elf64_Shdr> shTable;
407 int entries, ret = 0;
408
409 ret = readSectionHeadersAll(elfFile, shTable);
410 if (ret) return ret;
411 entries = shTable.size();
412
Connor O'Brien3278a162020-02-13 21:45:22 -0800413 vector<struct bpf_prog_def> pd;
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800414 ret = readProgDefs(elfFile, pd, sizeOfBpfProgDef);
Connor O'Brien3278a162020-02-13 21:45:22 -0800415 if (ret) return ret;
416 vector<string> progDefNames;
417 ret = getSectionSymNames(elfFile, "progs", progDefNames);
418 if (!pd.empty() && ret) return ret;
419
Joel Fernandesd76a2002018-10-16 13:19:58 -0700420 for (int i = 0; i < entries; i++) {
421 string name;
422 codeSection cs_temp;
423 cs_temp.type = BPF_PROG_TYPE_UNSPEC;
424
425 ret = getSymName(elfFile, shTable[i].sh_name, name);
426 if (ret) return ret;
427
428 enum bpf_prog_type ptype = getSectionType(name);
429 if (ptype != BPF_PROG_TYPE_UNSPEC) {
Connor O'Brien3278a162020-02-13 21:45:22 -0800430 string oldName = name;
Maciej Żenczykowskif7c0d992021-01-21 16:01:04 -0800431
432 // convert all slashes to underscores
433 std::replace(name.begin(), name.end(), '/', '_');
434
Joel Fernandesd76a2002018-10-16 13:19:58 -0700435 cs_temp.type = ptype;
436 cs_temp.name = name;
437
438 ret = readSectionByIdx(elfFile, i, cs_temp.data);
439 if (ret) return ret;
440 ALOGD("Loaded code section %d (%s)\n", i, name.c_str());
Connor O'Brien3278a162020-02-13 21:45:22 -0800441
442 vector<string> csSymNames;
Connor O'Brien0ea4c6b2022-01-14 00:18:11 -0800443 ret = getSectionSymNames(elfFile, oldName, csSymNames, STT_FUNC);
Connor O'Brien3278a162020-02-13 21:45:22 -0800444 if (ret || !csSymNames.size()) return ret;
445 for (size_t i = 0; i < progDefNames.size(); ++i) {
446 if (!progDefNames[i].compare(csSymNames[0] + "_def")) {
447 cs_temp.prog_def = pd[i];
448 break;
449 }
450 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700451 }
452
453 /* Check for rel section */
454 if (cs_temp.data.size() > 0 && i < entries) {
455 ret = getSymName(elfFile, shTable[i + 1].sh_name, name);
456 if (ret) return ret;
457
458 if (isRelSection(cs_temp, name)) {
459 ret = readSectionByIdx(elfFile, i + 1, cs_temp.rel_data);
460 if (ret) return ret;
461 ALOGD("Loaded relo section %d (%s)\n", i, name.c_str());
462 }
463 }
464
465 if (cs_temp.data.size() > 0) {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700466 cs.push_back(std::move(cs_temp));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700467 ALOGD("Adding section %d to cs list\n", i);
468 }
469 }
470 return 0;
471}
472
473static int getSymNameByIdx(ifstream& elfFile, int index, string& name) {
474 vector<Elf64_Sym> symtab;
475 int ret = 0;
476
477 ret = readSymTab(elfFile, 0 /* !sort */, symtab);
478 if (ret) return ret;
479
480 if (index >= (int)symtab.size()) return -1;
481
482 return getSymName(elfFile, symtab[index].st_name, name);
483}
484
Connor O'Brien35425e52022-01-18 21:41:16 -0800485static bool waitpidTimeout(pid_t pid, int timeoutMs) {
486 // Add SIGCHLD to the signal set.
487 sigset_t child_mask, original_mask;
488 sigemptyset(&child_mask);
489 sigaddset(&child_mask, SIGCHLD);
490 if (sigprocmask(SIG_BLOCK, &child_mask, &original_mask) == -1) return false;
491
492 // Wait for a SIGCHLD notification.
493 errno = 0;
494 timespec ts = {0, timeoutMs * 1000000};
495 int wait_result = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, nullptr, &ts));
496
497 // Restore the original signal set.
498 sigprocmask(SIG_SETMASK, &original_mask, nullptr);
499
500 if (wait_result == -1) return false;
501
502 int status;
503 return TEMP_FAILURE_RETRY(waitpid(pid, &status, WNOHANG)) == pid;
504}
505
506static std::optional<unique_fd> getMapBtfInfo(const char* elfPath,
507 std::unordered_map<string, std::pair<uint32_t, uint32_t>> &btfTypeIds) {
508 unique_fd bpfloaderSocket, btfloaderSocket;
509 if (!android::base::Socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, &bpfloaderSocket,
510 &btfloaderSocket)) {
511 return {};
512 }
513
514 unique_fd pipeRead, pipeWrite;
515 if (!android::base::Pipe(&pipeRead, &pipeWrite, O_NONBLOCK)) {
516 return {};
517 }
518
519 pid_t pid = fork();
520 if (pid < 0) return {};
521 if (!pid) {
522 bpfloaderSocket.reset();
523 pipeRead.reset();
524 auto socketFdStr = std::to_string(btfloaderSocket.release());
525 auto pipeFdStr = std::to_string(pipeWrite.release());
526
527 if (execl("/system/bin/btfloader", "/system/bin/btfloader", socketFdStr.c_str(),
528 pipeFdStr.c_str(), elfPath, NULL) == -1) {
529 ALOGW("exec btfloader failed with errno %d (%s)\n", errno, strerror(errno));
530 exit(EX_UNAVAILABLE);
531 }
532 }
533 btfloaderSocket.reset();
534 pipeWrite.reset();
535 if (!waitpidTimeout(pid, 100)) {
536 kill(pid, SIGKILL);
537 return {};
538 }
539
540 unique_fd btfFd;
541 if (android::base::ReceiveFileDescriptors(bpfloaderSocket, nullptr, 0, &btfFd)) return {};
542
543 std::string btfTypeIdStr;
544 if (!android::base::ReadFdToString(pipeRead, &btfTypeIdStr)) return {};
545 if (btfFd.get() < 0) return {};
546
547 const auto mapTypeIdLines = android::base::Split(btfTypeIdStr, "\n");
548 for (const auto &line : mapTypeIdLines) {
549 const auto vec = android::base::Split(line, " ");
550 // Splitting on newline will give us one empty line
551 if (vec.size() != 3) continue;
552 const int kTid = atoi(vec[1].c_str());
553 const int vTid = atoi(vec[2].c_str());
554 if (!kTid || !vTid) return {};
555 btfTypeIds[vec[0]] = std::make_pair(kTid, vTid);
556 }
557 return btfFd;
558}
559
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800560static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& mapFds,
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800561 const char* prefix, size_t sizeOfBpfMapDef) {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700562 int ret;
Connor O'Brien35425e52022-01-18 21:41:16 -0800563 vector<char> mdData, btfData;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700564 vector<struct bpf_map_def> md;
565 vector<string> mapNames;
Connor O'Brien35425e52022-01-18 21:41:16 -0800566 std::unordered_map<string, std::pair<uint32_t, uint32_t>> btfTypeIdMap;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700567 string fname = pathToFilename(string(elfPath), true);
568
569 ret = readSectionByName("maps", elfFile, mdData);
Steven Morelandc0905b42019-12-12 14:21:20 -0800570 if (ret == -2) return 0; // no maps to read
Joel Fernandesd76a2002018-10-16 13:19:58 -0700571 if (ret) return ret;
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800572
573 if (mdData.size() % sizeOfBpfMapDef) {
574 ALOGE("createMaps failed due to improper sized maps section, %zu %% %zu != 0\n",
575 mdData.size(), sizeOfBpfMapDef);
576 return -1;
577 };
578
579 int mapCount = mdData.size() / sizeOfBpfMapDef;
580 md.resize(mapCount);
581 size_t trimmedSize = std::min(sizeOfBpfMapDef, sizeof(struct bpf_map_def));
582
583 const char* dataPtr = mdData.data();
584 for (auto& m : md) {
585 // First we zero initialize
586 memset(&m, 0, sizeof(m));
587 // Then we set non-zero defaults
588 m.bpfloader_max_ver = DEFAULT_BPFLOADER_MAX_VER; // v1.0
Maciej Żenczykowski36c53ba2021-07-05 15:20:34 -0700589 m.max_kver = 0xFFFFFFFFu; // matches KVER_INF from bpf_helpers.h
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800590 // Then we copy over the structure prefix from the ELF file.
591 memcpy(&m, dataPtr, trimmedSize);
592 // Move to next struct in the ELF file
593 dataPtr += sizeOfBpfMapDef;
594 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700595
Connor O'Brien3278a162020-02-13 21:45:22 -0800596 ret = getSectionSymNames(elfFile, "maps", mapNames);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700597 if (ret) return ret;
598
Connor O'Brien35425e52022-01-18 21:41:16 -0800599 std::optional<unique_fd> btfFd;
600 if (!readSectionByName(".BTF", elfFile, btfData)) {
601 btfFd = getMapBtfInfo(elfPath, btfTypeIdMap);
602 }
603
Maciej Żenczykowski36c53ba2021-07-05 15:20:34 -0700604 unsigned kvers = kernelVersion();
Joel Fernandesd76a2002018-10-16 13:19:58 -0700605
Maciej Żenczykowski36c53ba2021-07-05 15:20:34 -0700606 for (int i = 0; i < (int)mapNames.size(); i++) {
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800607 if (BPFLOADER_VERSION < md[i].bpfloader_min_ver) {
608 ALOGI("skipping map %s which requires bpfloader min ver 0x%05x\n", mapNames[i].c_str(),
609 md[i].bpfloader_min_ver);
Maciej Żenczykowskia21256d2021-07-02 00:40:55 -0700610 mapFds.push_back(unique_fd());
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800611 continue;
612 }
613
614 if (BPFLOADER_VERSION >= md[i].bpfloader_max_ver) {
615 ALOGI("skipping map %s which requires bpfloader max ver 0x%05x\n", mapNames[i].c_str(),
616 md[i].bpfloader_max_ver);
Maciej Żenczykowskia21256d2021-07-02 00:40:55 -0700617 mapFds.push_back(unique_fd());
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800618 continue;
619 }
620
Maciej Żenczykowski36c53ba2021-07-05 15:20:34 -0700621 if (kvers < md[i].min_kver) {
622 ALOGI("skipping map %s which requires kernel version 0x%x >= 0x%x\n",
623 mapNames[i].c_str(), kvers, md[i].min_kver);
624 mapFds.push_back(unique_fd());
625 continue;
626 }
627
628 if (kvers >= md[i].max_kver) {
629 ALOGI("skipping map %s which requires kernel version 0x%x < 0x%x\n",
630 mapNames[i].c_str(), kvers, md[i].max_kver);
631 mapFds.push_back(unique_fd());
632 continue;
633 }
634
635 // Format of pin location is /sys/fs/bpf/<prefix>map_<filename>_<mapname>
636 string mapPinLoc =
637 string(BPF_FS_PATH) + prefix + "map_" + fname + "_" + string(mapNames[i]);
638 bool reuse = false;
639 unique_fd fd;
640 int saved_errno;
641
Joel Fernandesd76a2002018-10-16 13:19:58 -0700642 if (access(mapPinLoc.c_str(), F_OK) == 0) {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700643 fd.reset(bpf_obj_get(mapPinLoc.c_str()));
Maciej Żenczykowski2c372132021-03-01 23:09:54 -0800644 saved_errno = errno;
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700645 ALOGD("bpf_create_map reusing map %s, ret: %d\n", mapNames[i].c_str(), fd.get());
Joel Fernandesd76a2002018-10-16 13:19:58 -0700646 reuse = true;
647 } else {
Maciej Żenczykowskicb358de2021-03-04 07:27:38 -0800648 enum bpf_map_type type = md[i].type;
649 if (type == BPF_MAP_TYPE_DEVMAP && !isAtLeastKernelVersion(4, 14, 0)) {
650 // On Linux Kernels older than 4.14 this map type doesn't exist, but it can kind
651 // of be approximated: ARRAY has the same userspace api, though it is not usable
652 // by the same ebpf programs. However, that's okay because the bpf_redirect_map()
653 // helper doesn't exist on 4.9 anyway (so the bpf program would fail to load,
654 // and thus needs to be tagged as 4.14+ either way), so there's nothing useful you
655 // could do with a DEVMAP anyway (that isn't already provided by an ARRAY)...
656 // Hence using an ARRAY instead of a DEVMAP simply makes life easier for userspace.
657 type = BPF_MAP_TYPE_ARRAY;
658 }
659 if (type == BPF_MAP_TYPE_DEVMAP_HASH && !isAtLeastKernelVersion(5, 4, 0)) {
660 // On Linux Kernels older than 5.4 this map type doesn't exist, but it can kind
661 // of be approximated: HASH has the same userspace visible api.
662 // However it cannot be used by ebpf programs in the same way.
663 // Since bpf_redirect_map() only requires 4.14, a program using a DEVMAP_HASH map
664 // would fail to load (due to trying to redirect to a HASH instead of DEVMAP_HASH).
665 // One must thus tag any BPF_MAP_TYPE_DEVMAP_HASH + bpf_redirect_map() using
666 // programs as being 5.4+...
667 type = BPF_MAP_TYPE_HASH;
668 }
Connor O'Brien35425e52022-01-18 21:41:16 -0800669 struct bpf_create_map_attr attr = {
670 .name = mapNames[i].c_str(),
671 .map_type = type,
672 .map_flags = md[i].map_flags,
673 .key_size = md[i].key_size,
674 .value_size = md[i].value_size,
675 .max_entries = md[i].max_entries,
676 };
677 if (btfFd.has_value() && btfTypeIdMap.find(mapNames[i]) != btfTypeIdMap.end()) {
678 attr.btf_fd = btfFd->get();
679 attr.btf_key_type_id = btfTypeIdMap.at(mapNames[i]).first;
680 attr.btf_value_type_id = btfTypeIdMap.at(mapNames[i]).second;
681 }
682 fd.reset(bcc_create_map_xattr(&attr, true));
Maciej Żenczykowski2c372132021-03-01 23:09:54 -0800683 saved_errno = errno;
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700684 ALOGD("bpf_create_map name %s, ret: %d\n", mapNames[i].c_str(), fd.get());
Joel Fernandesd76a2002018-10-16 13:19:58 -0700685 }
686
Maciej Żenczykowski2c372132021-03-01 23:09:54 -0800687 if (fd < 0) return -saved_errno;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700688
689 if (!reuse) {
690 ret = bpf_obj_pin(fd, mapPinLoc.c_str());
Maciej Żenczykowski83f29772020-01-27 03:11:51 -0800691 if (ret) return -errno;
692 ret = chown(mapPinLoc.c_str(), (uid_t)md[i].uid, (gid_t)md[i].gid);
693 if (ret) return -errno;
694 ret = chmod(mapPinLoc.c_str(), md[i].mode);
695 if (ret) return -errno;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700696 }
697
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700698 mapFds.push_back(std::move(fd));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700699 }
700
701 return ret;
702}
703
704/* For debugging, dump all instructions */
705static void dumpIns(char* ins, int size) {
706 for (int row = 0; row < size / 8; row++) {
707 ALOGE("%d: ", row);
708 for (int j = 0; j < 8; j++) {
709 ALOGE("%3x ", ins[(row * 8) + j]);
710 }
711 ALOGE("\n");
712 }
713}
714
715/* For debugging, dump all code sections from cs list */
716static void dumpAllCs(vector<codeSection>& cs) {
717 for (int i = 0; i < (int)cs.size(); i++) {
718 ALOGE("Dumping cs %d, name %s\n", int(i), cs[i].name.c_str());
719 dumpIns((char*)cs[i].data.data(), cs[i].data.size());
720 ALOGE("-----------\n");
721 }
722}
723
724static void applyRelo(void* insnsPtr, Elf64_Addr offset, int fd) {
725 int insnIndex;
726 struct bpf_insn *insn, *insns;
727
728 insns = (struct bpf_insn*)(insnsPtr);
729
730 insnIndex = offset / sizeof(struct bpf_insn);
731 insn = &insns[insnIndex];
732
733 ALOGD(
734 "applying relo to instruction at byte offset: %d, \
735 insn offset %d , insn %lx\n",
736 (int)offset, (int)insnIndex, *(unsigned long*)insn);
737
738 if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
739 ALOGE("Dumping all instructions till ins %d\n", insnIndex);
740 ALOGE("invalid relo for insn %d: code 0x%x\n", insnIndex, insn->code);
741 dumpIns((char*)insnsPtr, (insnIndex + 3) * 8);
742 return;
743 }
744
745 insn->imm = fd;
746 insn->src_reg = BPF_PSEUDO_MAP_FD;
747}
748
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700749static void applyMapRelo(ifstream& elfFile, vector<unique_fd> &mapFds, vector<codeSection>& cs) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700750 vector<string> mapNames;
751
Connor O'Brien3278a162020-02-13 21:45:22 -0800752 int ret = getSectionSymNames(elfFile, "maps", mapNames);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700753 if (ret) return;
754
755 for (int k = 0; k != (int)cs.size(); k++) {
756 Elf64_Rel* rel = (Elf64_Rel*)(cs[k].rel_data.data());
757 int n_rel = cs[k].rel_data.size() / sizeof(*rel);
758
759 for (int i = 0; i < n_rel; i++) {
760 int symIndex = ELF64_R_SYM(rel[i].r_info);
761 string symName;
762
763 ret = getSymNameByIdx(elfFile, symIndex, symName);
764 if (ret) return;
765
766 /* Find the map fd and apply relo */
767 for (int j = 0; j < (int)mapNames.size(); j++) {
768 if (!mapNames[j].compare(symName)) {
769 applyRelo(cs[k].data.data(), rel[i].r_offset, mapFds[j]);
770 break;
771 }
772 }
773 }
774 }
775}
776
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800777static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const string& license,
778 const char* prefix) {
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800779 unsigned kvers = kernelVersion();
780 int ret, fd;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700781
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800782 if (!kvers) return -1;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700783
784 string fname = pathToFilename(string(elfPath), true);
785
786 for (int i = 0; i < (int)cs.size(); i++) {
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700787 string name = cs[i].name;
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800788 unsigned bpfMinVer = DEFAULT_BPFLOADER_MIN_VER; // v0.0
789 unsigned bpfMaxVer = DEFAULT_BPFLOADER_MAX_VER; // v1.0
Joel Fernandesd76a2002018-10-16 13:19:58 -0700790
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800791 if (cs[i].prog_def.has_value()) {
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700792 unsigned min_kver = cs[i].prog_def->min_kver;
793 unsigned max_kver = cs[i].prog_def->max_kver;
794 ALOGD("cs[%d].name:%s min_kver:%x .max_kver:%x (kvers:%x)\n", i, name.c_str(), min_kver,
795 max_kver, kvers);
796 if (kvers < min_kver) continue;
797 if (kvers >= max_kver) continue;
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800798
799 bpfMinVer = cs[i].prog_def->bpfloader_min_ver;
800 bpfMaxVer = cs[i].prog_def->bpfloader_max_ver;
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800801 }
802
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800803 ALOGD("cs[%d].name:%s requires bpfloader version [0x%05x,0x%05x)\n", i, name.c_str(),
804 bpfMinVer, bpfMaxVer);
805 if (BPFLOADER_VERSION < bpfMinVer) continue;
806 if (BPFLOADER_VERSION >= bpfMaxVer) continue;
807
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700808 // strip any potential $foo suffix
809 // this can be used to provide duplicate programs
810 // conditionally loaded based on running kernel version
Maciej Żenczykowski428843d2020-04-23 12:43:44 -0700811 name = name.substr(0, name.find_last_of('$'));
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700812
813 bool reuse = false;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700814 // Format of pin location is
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800815 // /sys/fs/bpf/<prefix>prog_<filename>_<mapname>
816 string progPinLoc = BPF_FS_PATH;
817 progPinLoc += prefix;
818 progPinLoc += "prog_";
Maciej Żenczykowski6c7871b2020-04-23 12:46:00 -0700819 progPinLoc += fname;
820 progPinLoc += '_';
821 progPinLoc += name;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700822 if (access(progPinLoc.c_str(), F_OK) == 0) {
Maciej Żenczykowskiaa295c82020-06-16 17:02:48 -0700823 fd = retrieveProgram(progPinLoc.c_str());
824 ALOGD("New bpf prog load reusing prog %s, ret: %d (%s)\n", progPinLoc.c_str(), fd,
825 (fd < 0 ? std::strerror(errno) : "no error"));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700826 reuse = true;
827 } else {
828 vector<char> log_buf(BPF_LOAD_LOG_SZ, 0);
829
Connor O'Brien52863b62022-01-11 18:38:15 -0800830 fd = bcc_prog_load(cs[i].type, name.c_str(), (struct bpf_insn*)cs[i].data.data(),
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700831 cs[i].data.size(), license.c_str(), kvers, 0, log_buf.data(),
832 log_buf.size());
Steven Moreland804bca02019-12-12 17:21:23 -0800833 ALOGD("bpf_prog_load lib call for %s (%s) returned fd: %d (%s)\n", elfPath,
834 cs[i].name.c_str(), fd, (fd < 0 ? std::strerror(errno) : "no error"));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700835
Maciej Żenczykowski524deef2020-02-11 11:12:37 -0800836 if (fd < 0) {
Maciej Żenczykowskif7c0d992021-01-21 16:01:04 -0800837 vector<string> lines = android::base::Split(log_buf.data(), "\n");
Maciej Żenczykowski524deef2020-02-11 11:12:37 -0800838
Maciej Żenczykowskiaa295c82020-06-16 17:02:48 -0700839 ALOGW("bpf_prog_load - BEGIN log_buf contents:");
840 for (const auto& line : lines) ALOGW("%s", line.c_str());
841 ALOGW("bpf_prog_load - END log_buf contents.");
842
843 if (cs[i].prog_def->optional) {
844 ALOGW("failed program is marked optional - continuing...");
845 continue;
846 }
847 ALOGE("non-optional program failed to load.");
Maciej Żenczykowski524deef2020-02-11 11:12:37 -0800848 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700849 }
850
851 if (fd < 0) return fd;
852 if (fd == 0) return -EINVAL;
853
854 if (!reuse) {
855 ret = bpf_obj_pin(fd, progPinLoc.c_str());
Connor O'Brien3278a162020-02-13 21:45:22 -0800856 if (ret) return -errno;
857 if (cs[i].prog_def.has_value()) {
858 if (chown(progPinLoc.c_str(), (uid_t)cs[i].prog_def->uid,
859 (gid_t)cs[i].prog_def->gid)) {
860 return -errno;
861 }
862 }
863 if (chmod(progPinLoc.c_str(), 0440)) return -errno;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700864 }
865
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700866 cs[i].prog_fd.reset(fd);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700867 }
868
869 return 0;
870}
871
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800872int loadProg(const char* elfPath, bool* isCritical, const char* prefix) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700873 vector<char> license;
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700874 vector<char> critical;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700875 vector<codeSection> cs;
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700876 vector<unique_fd> mapFds;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700877 int ret;
878
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700879 if (!isCritical) return -1;
880 *isCritical = false;
881
Joel Fernandesd76a2002018-10-16 13:19:58 -0700882 ifstream elfFile(elfPath, ios::in | ios::binary);
883 if (!elfFile.is_open()) return -1;
884
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700885 ret = readSectionByName("critical", elfFile, critical);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700886 *isCritical = !ret;
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700887
Joel Fernandesd76a2002018-10-16 13:19:58 -0700888 ret = readSectionByName("license", elfFile, license);
889 if (ret) {
890 ALOGE("Couldn't find license in %s\n", elfPath);
891 return ret;
892 } else {
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700893 ALOGD("Loading %s%s ELF object %s with license %s\n",
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700894 *isCritical ? "critical for " : "optional", *isCritical ? (char*)critical.data() : "",
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700895 elfPath, (char*)license.data());
Joel Fernandesd76a2002018-10-16 13:19:58 -0700896 }
897
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800898 // the following default values are for bpfloader V0.0 format which does not include them
899 unsigned int bpfLoaderMinVer =
900 readSectionUint("bpfloader_min_ver", elfFile, DEFAULT_BPFLOADER_MIN_VER);
901 unsigned int bpfLoaderMaxVer =
902 readSectionUint("bpfloader_max_ver", elfFile, DEFAULT_BPFLOADER_MAX_VER);
903 size_t sizeOfBpfMapDef =
904 readSectionUint("size_of_bpf_map_def", elfFile, DEFAULT_SIZEOF_BPF_MAP_DEF);
905 size_t sizeOfBpfProgDef =
906 readSectionUint("size_of_bpf_prog_def", elfFile, DEFAULT_SIZEOF_BPF_PROG_DEF);
907
908 // inclusive lower bound check
909 if (BPFLOADER_VERSION < bpfLoaderMinVer) {
910 ALOGI("BpfLoader version 0x%05x ignoring ELF object %s with min ver 0x%05x\n",
911 BPFLOADER_VERSION, elfPath, bpfLoaderMinVer);
912 return 0;
913 }
914
915 // exclusive upper bound check
916 if (BPFLOADER_VERSION >= bpfLoaderMaxVer) {
917 ALOGI("BpfLoader version 0x%05x ignoring ELF object %s with max ver 0x%05x\n",
918 BPFLOADER_VERSION, elfPath, bpfLoaderMaxVer);
919 return 0;
920 }
921
922 ALOGI("BpfLoader version 0x%05x processing ELF object %s with ver [0x%05x,0x%05x)\n",
923 BPFLOADER_VERSION, elfPath, bpfLoaderMinVer, bpfLoaderMaxVer);
924
925 if (sizeOfBpfMapDef < DEFAULT_SIZEOF_BPF_MAP_DEF) {
926 ALOGE("sizeof(bpf_map_def) of %zu is too small (< %d)\n", sizeOfBpfMapDef,
927 DEFAULT_SIZEOF_BPF_MAP_DEF);
928 return -1;
929 }
930
931 if (sizeOfBpfProgDef < DEFAULT_SIZEOF_BPF_PROG_DEF) {
932 ALOGE("sizeof(bpf_prog_def) of %zu is too small (< %d)\n", sizeOfBpfProgDef,
933 DEFAULT_SIZEOF_BPF_PROG_DEF);
934 return -1;
935 }
936
937 ret = readCodeSections(elfFile, cs, sizeOfBpfProgDef);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700938 if (ret) {
939 ALOGE("Couldn't read all code sections in %s\n", elfPath);
940 return ret;
941 }
942
943 /* Just for future debugging */
944 if (0) dumpAllCs(cs);
945
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800946 ret = createMaps(elfPath, elfFile, mapFds, prefix, sizeOfBpfMapDef);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700947 if (ret) {
948 ALOGE("Failed to create maps: (ret=%d) in %s\n", ret, elfPath);
949 return ret;
950 }
951
952 for (int i = 0; i < (int)mapFds.size(); i++)
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700953 ALOGD("map_fd found at %d is %d in %s\n", i, mapFds[i].get(), elfPath);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700954
955 applyMapRelo(elfFile, mapFds, cs);
956
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800957 ret = loadCodeSections(elfPath, cs, string(license.data()), prefix);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700958 if (ret) ALOGE("Failed to load programs, loadCodeSections ret=%d\n", ret);
959
960 return ret;
961}
962
Joel Fernandesd76a2002018-10-16 13:19:58 -0700963} // namespace bpf
964} // namespace android