blob: cad59502c508f35d3c17b6a8f86edb77835584aa [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
359static int getSectionSymNames(ifstream& elfFile, const string& sectionName, vector<string>& names) {
360 int ret;
361 string name;
362 vector<Elf64_Sym> symtab;
363 vector<Elf64_Shdr> shTable;
364
365 ret = readSymTab(elfFile, 1 /* sort */, symtab);
366 if (ret) return ret;
367
368 /* Get index of section */
369 ret = readSectionHeadersAll(elfFile, shTable);
370 if (ret) return ret;
371
372 int sec_idx = -1;
373 for (int i = 0; i < (int)shTable.size(); i++) {
374 ret = getSymName(elfFile, shTable[i].sh_name, name);
375 if (ret) return ret;
376
377 if (!name.compare(sectionName)) {
378 sec_idx = i;
379 break;
380 }
381 }
382
383 /* No section found with matching name*/
384 if (sec_idx == -1) {
Maciej Żenczykowski21f34cb2020-07-20 18:44:33 -0700385 ALOGW("No %s section could be found in elf object\n", sectionName.c_str());
Connor O'Brien3278a162020-02-13 21:45:22 -0800386 return -1;
387 }
388
389 for (int i = 0; i < (int)symtab.size(); i++) {
390 if (symtab[i].st_shndx == sec_idx) {
391 string s;
392 ret = getSymName(elfFile, symtab[i].st_name, s);
393 if (ret) return ret;
394 names.push_back(s);
395 }
396 }
397
398 return 0;
399}
400
Joel Fernandesd76a2002018-10-16 13:19:58 -0700401/* Read a section by its index - for ex to get sec hdr strtab blob */
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800402static int readCodeSections(ifstream& elfFile, vector<codeSection>& cs, size_t sizeOfBpfProgDef) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700403 vector<Elf64_Shdr> shTable;
404 int entries, ret = 0;
405
406 ret = readSectionHeadersAll(elfFile, shTable);
407 if (ret) return ret;
408 entries = shTable.size();
409
Connor O'Brien3278a162020-02-13 21:45:22 -0800410 vector<struct bpf_prog_def> pd;
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800411 ret = readProgDefs(elfFile, pd, sizeOfBpfProgDef);
Connor O'Brien3278a162020-02-13 21:45:22 -0800412 if (ret) return ret;
413 vector<string> progDefNames;
414 ret = getSectionSymNames(elfFile, "progs", progDefNames);
415 if (!pd.empty() && ret) return ret;
416
Joel Fernandesd76a2002018-10-16 13:19:58 -0700417 for (int i = 0; i < entries; i++) {
418 string name;
419 codeSection cs_temp;
420 cs_temp.type = BPF_PROG_TYPE_UNSPEC;
421
422 ret = getSymName(elfFile, shTable[i].sh_name, name);
423 if (ret) return ret;
424
425 enum bpf_prog_type ptype = getSectionType(name);
426 if (ptype != BPF_PROG_TYPE_UNSPEC) {
Connor O'Brien3278a162020-02-13 21:45:22 -0800427 string oldName = name;
Maciej Żenczykowskif7c0d992021-01-21 16:01:04 -0800428
429 // convert all slashes to underscores
430 std::replace(name.begin(), name.end(), '/', '_');
431
Joel Fernandesd76a2002018-10-16 13:19:58 -0700432 cs_temp.type = ptype;
433 cs_temp.name = name;
434
435 ret = readSectionByIdx(elfFile, i, cs_temp.data);
436 if (ret) return ret;
437 ALOGD("Loaded code section %d (%s)\n", i, name.c_str());
Connor O'Brien3278a162020-02-13 21:45:22 -0800438
439 vector<string> csSymNames;
440 ret = getSectionSymNames(elfFile, oldName, csSymNames);
441 if (ret || !csSymNames.size()) return ret;
442 for (size_t i = 0; i < progDefNames.size(); ++i) {
443 if (!progDefNames[i].compare(csSymNames[0] + "_def")) {
444 cs_temp.prog_def = pd[i];
445 break;
446 }
447 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700448 }
449
450 /* Check for rel section */
451 if (cs_temp.data.size() > 0 && i < entries) {
452 ret = getSymName(elfFile, shTable[i + 1].sh_name, name);
453 if (ret) return ret;
454
455 if (isRelSection(cs_temp, name)) {
456 ret = readSectionByIdx(elfFile, i + 1, cs_temp.rel_data);
457 if (ret) return ret;
458 ALOGD("Loaded relo section %d (%s)\n", i, name.c_str());
459 }
460 }
461
462 if (cs_temp.data.size() > 0) {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700463 cs.push_back(std::move(cs_temp));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700464 ALOGD("Adding section %d to cs list\n", i);
465 }
466 }
467 return 0;
468}
469
470static int getSymNameByIdx(ifstream& elfFile, int index, string& name) {
471 vector<Elf64_Sym> symtab;
472 int ret = 0;
473
474 ret = readSymTab(elfFile, 0 /* !sort */, symtab);
475 if (ret) return ret;
476
477 if (index >= (int)symtab.size()) return -1;
478
479 return getSymName(elfFile, symtab[index].st_name, name);
480}
481
Connor O'Brien35425e52022-01-18 21:41:16 -0800482static bool waitpidTimeout(pid_t pid, int timeoutMs) {
483 // Add SIGCHLD to the signal set.
484 sigset_t child_mask, original_mask;
485 sigemptyset(&child_mask);
486 sigaddset(&child_mask, SIGCHLD);
487 if (sigprocmask(SIG_BLOCK, &child_mask, &original_mask) == -1) return false;
488
489 // Wait for a SIGCHLD notification.
490 errno = 0;
491 timespec ts = {0, timeoutMs * 1000000};
492 int wait_result = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, nullptr, &ts));
493
494 // Restore the original signal set.
495 sigprocmask(SIG_SETMASK, &original_mask, nullptr);
496
497 if (wait_result == -1) return false;
498
499 int status;
500 return TEMP_FAILURE_RETRY(waitpid(pid, &status, WNOHANG)) == pid;
501}
502
503static std::optional<unique_fd> getMapBtfInfo(const char* elfPath,
504 std::unordered_map<string, std::pair<uint32_t, uint32_t>> &btfTypeIds) {
505 unique_fd bpfloaderSocket, btfloaderSocket;
506 if (!android::base::Socketpair(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, &bpfloaderSocket,
507 &btfloaderSocket)) {
508 return {};
509 }
510
511 unique_fd pipeRead, pipeWrite;
512 if (!android::base::Pipe(&pipeRead, &pipeWrite, O_NONBLOCK)) {
513 return {};
514 }
515
516 pid_t pid = fork();
517 if (pid < 0) return {};
518 if (!pid) {
519 bpfloaderSocket.reset();
520 pipeRead.reset();
521 auto socketFdStr = std::to_string(btfloaderSocket.release());
522 auto pipeFdStr = std::to_string(pipeWrite.release());
523
524 if (execl("/system/bin/btfloader", "/system/bin/btfloader", socketFdStr.c_str(),
525 pipeFdStr.c_str(), elfPath, NULL) == -1) {
526 ALOGW("exec btfloader failed with errno %d (%s)\n", errno, strerror(errno));
527 exit(EX_UNAVAILABLE);
528 }
529 }
530 btfloaderSocket.reset();
531 pipeWrite.reset();
532 if (!waitpidTimeout(pid, 100)) {
533 kill(pid, SIGKILL);
534 return {};
535 }
536
537 unique_fd btfFd;
538 if (android::base::ReceiveFileDescriptors(bpfloaderSocket, nullptr, 0, &btfFd)) return {};
539
540 std::string btfTypeIdStr;
541 if (!android::base::ReadFdToString(pipeRead, &btfTypeIdStr)) return {};
542 if (btfFd.get() < 0) return {};
543
544 const auto mapTypeIdLines = android::base::Split(btfTypeIdStr, "\n");
545 for (const auto &line : mapTypeIdLines) {
546 const auto vec = android::base::Split(line, " ");
547 // Splitting on newline will give us one empty line
548 if (vec.size() != 3) continue;
549 const int kTid = atoi(vec[1].c_str());
550 const int vTid = atoi(vec[2].c_str());
551 if (!kTid || !vTid) return {};
552 btfTypeIds[vec[0]] = std::make_pair(kTid, vTid);
553 }
554 return btfFd;
555}
556
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800557static int createMaps(const char* elfPath, ifstream& elfFile, vector<unique_fd>& mapFds,
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800558 const char* prefix, size_t sizeOfBpfMapDef) {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700559 int ret;
Connor O'Brien35425e52022-01-18 21:41:16 -0800560 vector<char> mdData, btfData;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700561 vector<struct bpf_map_def> md;
562 vector<string> mapNames;
Connor O'Brien35425e52022-01-18 21:41:16 -0800563 std::unordered_map<string, std::pair<uint32_t, uint32_t>> btfTypeIdMap;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700564 string fname = pathToFilename(string(elfPath), true);
565
566 ret = readSectionByName("maps", elfFile, mdData);
Steven Morelandc0905b42019-12-12 14:21:20 -0800567 if (ret == -2) return 0; // no maps to read
Joel Fernandesd76a2002018-10-16 13:19:58 -0700568 if (ret) return ret;
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800569
570 if (mdData.size() % sizeOfBpfMapDef) {
571 ALOGE("createMaps failed due to improper sized maps section, %zu %% %zu != 0\n",
572 mdData.size(), sizeOfBpfMapDef);
573 return -1;
574 };
575
576 int mapCount = mdData.size() / sizeOfBpfMapDef;
577 md.resize(mapCount);
578 size_t trimmedSize = std::min(sizeOfBpfMapDef, sizeof(struct bpf_map_def));
579
580 const char* dataPtr = mdData.data();
581 for (auto& m : md) {
582 // First we zero initialize
583 memset(&m, 0, sizeof(m));
584 // Then we set non-zero defaults
585 m.bpfloader_max_ver = DEFAULT_BPFLOADER_MAX_VER; // v1.0
Maciej Żenczykowski36c53ba2021-07-05 15:20:34 -0700586 m.max_kver = 0xFFFFFFFFu; // matches KVER_INF from bpf_helpers.h
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800587 // Then we copy over the structure prefix from the ELF file.
588 memcpy(&m, dataPtr, trimmedSize);
589 // Move to next struct in the ELF file
590 dataPtr += sizeOfBpfMapDef;
591 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700592
Connor O'Brien3278a162020-02-13 21:45:22 -0800593 ret = getSectionSymNames(elfFile, "maps", mapNames);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700594 if (ret) return ret;
595
Connor O'Brien35425e52022-01-18 21:41:16 -0800596 std::optional<unique_fd> btfFd;
597 if (!readSectionByName(".BTF", elfFile, btfData)) {
598 btfFd = getMapBtfInfo(elfPath, btfTypeIdMap);
599 }
600
Maciej Żenczykowski36c53ba2021-07-05 15:20:34 -0700601 unsigned kvers = kernelVersion();
Joel Fernandesd76a2002018-10-16 13:19:58 -0700602
Maciej Żenczykowski36c53ba2021-07-05 15:20:34 -0700603 for (int i = 0; i < (int)mapNames.size(); i++) {
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800604 if (BPFLOADER_VERSION < md[i].bpfloader_min_ver) {
605 ALOGI("skipping map %s which requires bpfloader min ver 0x%05x\n", mapNames[i].c_str(),
606 md[i].bpfloader_min_ver);
Maciej Żenczykowskia21256d2021-07-02 00:40:55 -0700607 mapFds.push_back(unique_fd());
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800608 continue;
609 }
610
611 if (BPFLOADER_VERSION >= md[i].bpfloader_max_ver) {
612 ALOGI("skipping map %s which requires bpfloader max ver 0x%05x\n", mapNames[i].c_str(),
613 md[i].bpfloader_max_ver);
Maciej Żenczykowskia21256d2021-07-02 00:40:55 -0700614 mapFds.push_back(unique_fd());
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800615 continue;
616 }
617
Maciej Żenczykowski36c53ba2021-07-05 15:20:34 -0700618 if (kvers < md[i].min_kver) {
619 ALOGI("skipping map %s which requires kernel version 0x%x >= 0x%x\n",
620 mapNames[i].c_str(), kvers, md[i].min_kver);
621 mapFds.push_back(unique_fd());
622 continue;
623 }
624
625 if (kvers >= md[i].max_kver) {
626 ALOGI("skipping map %s which requires kernel version 0x%x < 0x%x\n",
627 mapNames[i].c_str(), kvers, md[i].max_kver);
628 mapFds.push_back(unique_fd());
629 continue;
630 }
631
632 // Format of pin location is /sys/fs/bpf/<prefix>map_<filename>_<mapname>
633 string mapPinLoc =
634 string(BPF_FS_PATH) + prefix + "map_" + fname + "_" + string(mapNames[i]);
635 bool reuse = false;
636 unique_fd fd;
637 int saved_errno;
638
Joel Fernandesd76a2002018-10-16 13:19:58 -0700639 if (access(mapPinLoc.c_str(), F_OK) == 0) {
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700640 fd.reset(bpf_obj_get(mapPinLoc.c_str()));
Maciej Żenczykowski2c372132021-03-01 23:09:54 -0800641 saved_errno = errno;
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700642 ALOGD("bpf_create_map reusing map %s, ret: %d\n", mapNames[i].c_str(), fd.get());
Joel Fernandesd76a2002018-10-16 13:19:58 -0700643 reuse = true;
644 } else {
Maciej Żenczykowskicb358de2021-03-04 07:27:38 -0800645 enum bpf_map_type type = md[i].type;
646 if (type == BPF_MAP_TYPE_DEVMAP && !isAtLeastKernelVersion(4, 14, 0)) {
647 // On Linux Kernels older than 4.14 this map type doesn't exist, but it can kind
648 // of be approximated: ARRAY has the same userspace api, though it is not usable
649 // by the same ebpf programs. However, that's okay because the bpf_redirect_map()
650 // helper doesn't exist on 4.9 anyway (so the bpf program would fail to load,
651 // and thus needs to be tagged as 4.14+ either way), so there's nothing useful you
652 // could do with a DEVMAP anyway (that isn't already provided by an ARRAY)...
653 // Hence using an ARRAY instead of a DEVMAP simply makes life easier for userspace.
654 type = BPF_MAP_TYPE_ARRAY;
655 }
656 if (type == BPF_MAP_TYPE_DEVMAP_HASH && !isAtLeastKernelVersion(5, 4, 0)) {
657 // On Linux Kernels older than 5.4 this map type doesn't exist, but it can kind
658 // of be approximated: HASH has the same userspace visible api.
659 // However it cannot be used by ebpf programs in the same way.
660 // Since bpf_redirect_map() only requires 4.14, a program using a DEVMAP_HASH map
661 // would fail to load (due to trying to redirect to a HASH instead of DEVMAP_HASH).
662 // One must thus tag any BPF_MAP_TYPE_DEVMAP_HASH + bpf_redirect_map() using
663 // programs as being 5.4+...
664 type = BPF_MAP_TYPE_HASH;
665 }
Connor O'Brien35425e52022-01-18 21:41:16 -0800666 struct bpf_create_map_attr attr = {
667 .name = mapNames[i].c_str(),
668 .map_type = type,
669 .map_flags = md[i].map_flags,
670 .key_size = md[i].key_size,
671 .value_size = md[i].value_size,
672 .max_entries = md[i].max_entries,
673 };
674 if (btfFd.has_value() && btfTypeIdMap.find(mapNames[i]) != btfTypeIdMap.end()) {
675 attr.btf_fd = btfFd->get();
676 attr.btf_key_type_id = btfTypeIdMap.at(mapNames[i]).first;
677 attr.btf_value_type_id = btfTypeIdMap.at(mapNames[i]).second;
678 }
679 fd.reset(bcc_create_map_xattr(&attr, true));
Maciej Żenczykowski2c372132021-03-01 23:09:54 -0800680 saved_errno = errno;
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700681 ALOGD("bpf_create_map name %s, ret: %d\n", mapNames[i].c_str(), fd.get());
Joel Fernandesd76a2002018-10-16 13:19:58 -0700682 }
683
Maciej Żenczykowski2c372132021-03-01 23:09:54 -0800684 if (fd < 0) return -saved_errno;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700685
686 if (!reuse) {
687 ret = bpf_obj_pin(fd, mapPinLoc.c_str());
Maciej Żenczykowski83f29772020-01-27 03:11:51 -0800688 if (ret) return -errno;
689 ret = chown(mapPinLoc.c_str(), (uid_t)md[i].uid, (gid_t)md[i].gid);
690 if (ret) return -errno;
691 ret = chmod(mapPinLoc.c_str(), md[i].mode);
692 if (ret) return -errno;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700693 }
694
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700695 mapFds.push_back(std::move(fd));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700696 }
697
698 return ret;
699}
700
701/* For debugging, dump all instructions */
702static void dumpIns(char* ins, int size) {
703 for (int row = 0; row < size / 8; row++) {
704 ALOGE("%d: ", row);
705 for (int j = 0; j < 8; j++) {
706 ALOGE("%3x ", ins[(row * 8) + j]);
707 }
708 ALOGE("\n");
709 }
710}
711
712/* For debugging, dump all code sections from cs list */
713static void dumpAllCs(vector<codeSection>& cs) {
714 for (int i = 0; i < (int)cs.size(); i++) {
715 ALOGE("Dumping cs %d, name %s\n", int(i), cs[i].name.c_str());
716 dumpIns((char*)cs[i].data.data(), cs[i].data.size());
717 ALOGE("-----------\n");
718 }
719}
720
721static void applyRelo(void* insnsPtr, Elf64_Addr offset, int fd) {
722 int insnIndex;
723 struct bpf_insn *insn, *insns;
724
725 insns = (struct bpf_insn*)(insnsPtr);
726
727 insnIndex = offset / sizeof(struct bpf_insn);
728 insn = &insns[insnIndex];
729
730 ALOGD(
731 "applying relo to instruction at byte offset: %d, \
732 insn offset %d , insn %lx\n",
733 (int)offset, (int)insnIndex, *(unsigned long*)insn);
734
735 if (insn->code != (BPF_LD | BPF_IMM | BPF_DW)) {
736 ALOGE("Dumping all instructions till ins %d\n", insnIndex);
737 ALOGE("invalid relo for insn %d: code 0x%x\n", insnIndex, insn->code);
738 dumpIns((char*)insnsPtr, (insnIndex + 3) * 8);
739 return;
740 }
741
742 insn->imm = fd;
743 insn->src_reg = BPF_PSEUDO_MAP_FD;
744}
745
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700746static void applyMapRelo(ifstream& elfFile, vector<unique_fd> &mapFds, vector<codeSection>& cs) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700747 vector<string> mapNames;
748
Connor O'Brien3278a162020-02-13 21:45:22 -0800749 int ret = getSectionSymNames(elfFile, "maps", mapNames);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700750 if (ret) return;
751
752 for (int k = 0; k != (int)cs.size(); k++) {
753 Elf64_Rel* rel = (Elf64_Rel*)(cs[k].rel_data.data());
754 int n_rel = cs[k].rel_data.size() / sizeof(*rel);
755
756 for (int i = 0; i < n_rel; i++) {
757 int symIndex = ELF64_R_SYM(rel[i].r_info);
758 string symName;
759
760 ret = getSymNameByIdx(elfFile, symIndex, symName);
761 if (ret) return;
762
763 /* Find the map fd and apply relo */
764 for (int j = 0; j < (int)mapNames.size(); j++) {
765 if (!mapNames[j].compare(symName)) {
766 applyRelo(cs[k].data.data(), rel[i].r_offset, mapFds[j]);
767 break;
768 }
769 }
770 }
771 }
772}
773
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800774static int loadCodeSections(const char* elfPath, vector<codeSection>& cs, const string& license,
775 const char* prefix) {
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800776 unsigned kvers = kernelVersion();
777 int ret, fd;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700778
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800779 if (!kvers) return -1;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700780
781 string fname = pathToFilename(string(elfPath), true);
782
783 for (int i = 0; i < (int)cs.size(); i++) {
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700784 string name = cs[i].name;
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800785 unsigned bpfMinVer = DEFAULT_BPFLOADER_MIN_VER; // v0.0
786 unsigned bpfMaxVer = DEFAULT_BPFLOADER_MAX_VER; // v1.0
Joel Fernandesd76a2002018-10-16 13:19:58 -0700787
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800788 if (cs[i].prog_def.has_value()) {
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700789 unsigned min_kver = cs[i].prog_def->min_kver;
790 unsigned max_kver = cs[i].prog_def->max_kver;
791 ALOGD("cs[%d].name:%s min_kver:%x .max_kver:%x (kvers:%x)\n", i, name.c_str(), min_kver,
792 max_kver, kvers);
793 if (kvers < min_kver) continue;
794 if (kvers >= max_kver) continue;
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800795
796 bpfMinVer = cs[i].prog_def->bpfloader_min_ver;
797 bpfMaxVer = cs[i].prog_def->bpfloader_max_ver;
Maciej Żenczykowski07375e22020-02-19 14:23:59 -0800798 }
799
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800800 ALOGD("cs[%d].name:%s requires bpfloader version [0x%05x,0x%05x)\n", i, name.c_str(),
801 bpfMinVer, bpfMaxVer);
802 if (BPFLOADER_VERSION < bpfMinVer) continue;
803 if (BPFLOADER_VERSION >= bpfMaxVer) continue;
804
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700805 // strip any potential $foo suffix
806 // this can be used to provide duplicate programs
807 // conditionally loaded based on running kernel version
Maciej Żenczykowski428843d2020-04-23 12:43:44 -0700808 name = name.substr(0, name.find_last_of('$'));
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700809
810 bool reuse = false;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700811 // Format of pin location is
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800812 // /sys/fs/bpf/<prefix>prog_<filename>_<mapname>
813 string progPinLoc = BPF_FS_PATH;
814 progPinLoc += prefix;
815 progPinLoc += "prog_";
Maciej Żenczykowski6c7871b2020-04-23 12:46:00 -0700816 progPinLoc += fname;
817 progPinLoc += '_';
818 progPinLoc += name;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700819 if (access(progPinLoc.c_str(), F_OK) == 0) {
Maciej Żenczykowskiaa295c82020-06-16 17:02:48 -0700820 fd = retrieveProgram(progPinLoc.c_str());
821 ALOGD("New bpf prog load reusing prog %s, ret: %d (%s)\n", progPinLoc.c_str(), fd,
822 (fd < 0 ? std::strerror(errno) : "no error"));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700823 reuse = true;
824 } else {
825 vector<char> log_buf(BPF_LOAD_LOG_SZ, 0);
826
Connor O'Brien52863b62022-01-11 18:38:15 -0800827 fd = bcc_prog_load(cs[i].type, name.c_str(), (struct bpf_insn*)cs[i].data.data(),
Maciej Żenczykowski681f6042020-04-21 15:34:18 -0700828 cs[i].data.size(), license.c_str(), kvers, 0, log_buf.data(),
829 log_buf.size());
Steven Moreland804bca02019-12-12 17:21:23 -0800830 ALOGD("bpf_prog_load lib call for %s (%s) returned fd: %d (%s)\n", elfPath,
831 cs[i].name.c_str(), fd, (fd < 0 ? std::strerror(errno) : "no error"));
Joel Fernandesd76a2002018-10-16 13:19:58 -0700832
Maciej Żenczykowski524deef2020-02-11 11:12:37 -0800833 if (fd < 0) {
Maciej Żenczykowskif7c0d992021-01-21 16:01:04 -0800834 vector<string> lines = android::base::Split(log_buf.data(), "\n");
Maciej Żenczykowski524deef2020-02-11 11:12:37 -0800835
Maciej Żenczykowskiaa295c82020-06-16 17:02:48 -0700836 ALOGW("bpf_prog_load - BEGIN log_buf contents:");
837 for (const auto& line : lines) ALOGW("%s", line.c_str());
838 ALOGW("bpf_prog_load - END log_buf contents.");
839
840 if (cs[i].prog_def->optional) {
841 ALOGW("failed program is marked optional - continuing...");
842 continue;
843 }
844 ALOGE("non-optional program failed to load.");
Maciej Żenczykowski524deef2020-02-11 11:12:37 -0800845 }
Joel Fernandesd76a2002018-10-16 13:19:58 -0700846 }
847
848 if (fd < 0) return fd;
849 if (fd == 0) return -EINVAL;
850
851 if (!reuse) {
852 ret = bpf_obj_pin(fd, progPinLoc.c_str());
Connor O'Brien3278a162020-02-13 21:45:22 -0800853 if (ret) return -errno;
854 if (cs[i].prog_def.has_value()) {
855 if (chown(progPinLoc.c_str(), (uid_t)cs[i].prog_def->uid,
856 (gid_t)cs[i].prog_def->gid)) {
857 return -errno;
858 }
859 }
860 if (chmod(progPinLoc.c_str(), 0440)) return -errno;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700861 }
862
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700863 cs[i].prog_fd.reset(fd);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700864 }
865
866 return 0;
867}
868
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800869int loadProg(const char* elfPath, bool* isCritical, const char* prefix) {
Joel Fernandesd76a2002018-10-16 13:19:58 -0700870 vector<char> license;
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700871 vector<char> critical;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700872 vector<codeSection> cs;
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700873 vector<unique_fd> mapFds;
Joel Fernandesd76a2002018-10-16 13:19:58 -0700874 int ret;
875
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700876 if (!isCritical) return -1;
877 *isCritical = false;
878
Joel Fernandesd76a2002018-10-16 13:19:58 -0700879 ifstream elfFile(elfPath, ios::in | ios::binary);
880 if (!elfFile.is_open()) return -1;
881
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700882 ret = readSectionByName("critical", elfFile, critical);
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700883 *isCritical = !ret;
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700884
Joel Fernandesd76a2002018-10-16 13:19:58 -0700885 ret = readSectionByName("license", elfFile, license);
886 if (ret) {
887 ALOGE("Couldn't find license in %s\n", elfPath);
888 return ret;
889 } else {
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700890 ALOGD("Loading %s%s ELF object %s with license %s\n",
Maciej Żenczykowski89515d92020-06-14 19:27:33 -0700891 *isCritical ? "critical for " : "optional", *isCritical ? (char*)critical.data() : "",
Maciej Żenczykowski4ba8c1c2020-06-10 15:49:31 -0700892 elfPath, (char*)license.data());
Joel Fernandesd76a2002018-10-16 13:19:58 -0700893 }
894
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800895 // the following default values are for bpfloader V0.0 format which does not include them
896 unsigned int bpfLoaderMinVer =
897 readSectionUint("bpfloader_min_ver", elfFile, DEFAULT_BPFLOADER_MIN_VER);
898 unsigned int bpfLoaderMaxVer =
899 readSectionUint("bpfloader_max_ver", elfFile, DEFAULT_BPFLOADER_MAX_VER);
900 size_t sizeOfBpfMapDef =
901 readSectionUint("size_of_bpf_map_def", elfFile, DEFAULT_SIZEOF_BPF_MAP_DEF);
902 size_t sizeOfBpfProgDef =
903 readSectionUint("size_of_bpf_prog_def", elfFile, DEFAULT_SIZEOF_BPF_PROG_DEF);
904
905 // inclusive lower bound check
906 if (BPFLOADER_VERSION < bpfLoaderMinVer) {
907 ALOGI("BpfLoader version 0x%05x ignoring ELF object %s with min ver 0x%05x\n",
908 BPFLOADER_VERSION, elfPath, bpfLoaderMinVer);
909 return 0;
910 }
911
912 // exclusive upper bound check
913 if (BPFLOADER_VERSION >= bpfLoaderMaxVer) {
914 ALOGI("BpfLoader version 0x%05x ignoring ELF object %s with max ver 0x%05x\n",
915 BPFLOADER_VERSION, elfPath, bpfLoaderMaxVer);
916 return 0;
917 }
918
919 ALOGI("BpfLoader version 0x%05x processing ELF object %s with ver [0x%05x,0x%05x)\n",
920 BPFLOADER_VERSION, elfPath, bpfLoaderMinVer, bpfLoaderMaxVer);
921
922 if (sizeOfBpfMapDef < DEFAULT_SIZEOF_BPF_MAP_DEF) {
923 ALOGE("sizeof(bpf_map_def) of %zu is too small (< %d)\n", sizeOfBpfMapDef,
924 DEFAULT_SIZEOF_BPF_MAP_DEF);
925 return -1;
926 }
927
928 if (sizeOfBpfProgDef < DEFAULT_SIZEOF_BPF_PROG_DEF) {
929 ALOGE("sizeof(bpf_prog_def) of %zu is too small (< %d)\n", sizeOfBpfProgDef,
930 DEFAULT_SIZEOF_BPF_PROG_DEF);
931 return -1;
932 }
933
934 ret = readCodeSections(elfFile, cs, sizeOfBpfProgDef);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700935 if (ret) {
936 ALOGE("Couldn't read all code sections in %s\n", elfPath);
937 return ret;
938 }
939
940 /* Just for future debugging */
941 if (0) dumpAllCs(cs);
942
Maciej Żenczykowski9217eee2021-03-03 05:28:52 -0800943 ret = createMaps(elfPath, elfFile, mapFds, prefix, sizeOfBpfMapDef);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700944 if (ret) {
945 ALOGE("Failed to create maps: (ret=%d) in %s\n", ret, elfPath);
946 return ret;
947 }
948
949 for (int i = 0; i < (int)mapFds.size(); i++)
Connor O'Brien8d49fc72019-10-24 18:23:49 -0700950 ALOGD("map_fd found at %d is %d in %s\n", i, mapFds[i].get(), elfPath);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700951
952 applyMapRelo(elfFile, mapFds, cs);
953
Maciej Żenczykowskid8a45782021-01-14 23:36:32 -0800954 ret = loadCodeSections(elfPath, cs, string(license.data()), prefix);
Joel Fernandesd76a2002018-10-16 13:19:58 -0700955 if (ret) ALOGE("Failed to load programs, loadCodeSections ret=%d\n", ret);
956
957 return ret;
958}
959
Joel Fernandesd76a2002018-10-16 13:19:58 -0700960} // namespace bpf
961} // namespace android