• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vbpf / ebpf-verifier / 13884484247

16 Mar 2025 02:44PM UTC coverage: 88.134% (-0.04%) from 88.176%
13884484247

push

github

elazarg
refactor asm_files.cpp

Signed-off-by: Elazar Gershuni <elazarg@gmail.com>

159 of 178 new or added lines in 1 file covered. (89.33%)

24 existing lines in 1 file now uncovered.

8549 of 9700 relevant lines covered (88.13%)

7060988.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.29
/src/asm_files.cpp
1
// Copyright (c) Prevail Verifier contributors.
2
// SPDX-License-Identifier: MIT
3
#include <iostream>
4
#include <map>
5
#include <set>
6
#include <string>
7
#include <sys/stat.h>
8
#include <vector>
9

10
#include "elfio/elfio.hpp"
11
#include "libbtf/btf_json.h"
12
#include "libbtf/btf_map.h"
13
#include "libbtf/btf_parse.h"
14

15
#include "asm_files.hpp"
16
#include "crab_utils/num_safety.hpp"
17
#include "platform.hpp"
18

19
template <typename T>
20
    requires std::is_trivially_copyable_v<T>
21
static std::vector<T> vector_of(const char* data, const ELFIO::Elf_Xword size) {
7,604✔
22
    if (size % sizeof(T) != 0 || size > std::numeric_limits<uint32_t>::max() || !data) {
7,604✔
UNCOV
23
        throw UnmarshalError("Invalid argument to vector_of");
×
24
    }
25
    return {reinterpret_cast<const T*>(data), reinterpret_cast<const T*>(data + size)};
15,208✔
26
}
27

28
template <typename T>
29
    requires std::is_trivially_copyable_v<T>
30
static std::vector<T> vector_of(const ELFIO::section& sec) {
488✔
31
    return vector_of<T>(sec.get_data(), sec.get_size());
488✔
32
}
33

34
int create_map_crab(const EbpfMapType& map_type, const uint32_t key_size, const uint32_t value_size,
5,800✔
35
                    const uint32_t max_entries, ebpf_verifier_options_t) {
36
    const EquivalenceKey equiv{map_type.value_type, key_size, value_size, map_type.is_array ? max_entries : 0};
5,800✔
37
    if (!thread_local_program_info->cache.contains(equiv)) {
5,800✔
38
        // +1 so 0 is the null FD
39
        thread_local_program_info->cache[equiv] = gsl::narrow<int>(thread_local_program_info->cache.size()) + 1;
5,022✔
40
    }
41
    return thread_local_program_info->cache.at(equiv);
8,700✔
42
}
43

44
EbpfMapDescriptor* find_map_descriptor(const int map_fd) {
17,354✔
45
    for (EbpfMapDescriptor& map : thread_local_program_info->map_descriptors) {
152,376✔
46
        if (map.original_fd == map_fd) {
152,374✔
47
            return &map;
17,352✔
48
        }
49
    }
50
    return nullptr;
1✔
51
}
52

53
// Maps sections are identified as any section called "maps", or matching "maps/<map-name>".
54
static bool is_map_section(const std::string& name) {
38,954✔
55
    const std::string maps_prefix = "maps/";
38,954✔
56
    return name == "maps" || (name.length() > 5 && name.compare(0, maps_prefix.length(), maps_prefix) == 0);
77,908✔
57
}
38,954✔
58

59
struct symbol_details_t {
336,343✔
60
    std::string name;
61
    ELFIO::Elf64_Addr value{}; // also relocation offset
62
    ELFIO::Elf_Xword size{};
63
    unsigned char bind{};
64
    unsigned char type{};
65
    ELFIO::Elf_Half section_index{};
66
    unsigned char other{};
67
};
68

69
static symbol_details_t get_symbol_details(const ELFIO::const_symbol_section_accessor& symbols,
30,441,468✔
70
                                           const ELFIO::Elf_Xword index) {
71
    symbol_details_t details;
30,441,468✔
72
    symbols.get_symbol(index, details.name, details.value, details.size, details.bind, details.type,
30,441,468✔
73
                       details.section_index, details.other);
30,441,468✔
74
    return details;
30,441,468✔
UNCOV
75
}
×
76

77
struct parse_params_t {
3✔
78
    const std::string& path;
79
    const ebpf_verifier_options_t& options;
80
    const ebpf_platform_t* platform;
81
    const std::string desired_section;
82
};
83

84
std::vector<raw_program> read_elf(const std::string& path, const std::string& desired_section,
756✔
85
                                  const ebpf_verifier_options_t& options, const ebpf_platform_t* platform) {
86
    if (std::ifstream stream{path, std::ios::in | std::ios::binary}) {
756✔
87
        return read_elf(stream, path, desired_section, options, platform);
1,502✔
88
    }
756✔
89
    struct stat st;
1✔
90
    if (stat(path.c_str(), &st)) {
2✔
91
        throw UnmarshalError(std::string(strerror(errno)) + " opening " + path);
6✔
92
    }
UNCOV
93
    throw UnmarshalError("Can't process ELF file " + path);
×
94
}
95

96
static std::tuple<std::string, ELFIO::Elf_Xword>
97
get_program_name_and_size(const ELFIO::section& sec, const ELFIO::Elf_Xword start,
7,116✔
98
                          const ELFIO::const_symbol_section_accessor& symbols) {
99
    const ELFIO::Elf_Xword symbol_count = symbols.get_symbols_num();
7,116✔
100
    const ELFIO::Elf_Half section_index = sec.get_index();
7,116✔
101
    std::string program_name = sec.get_name();
7,116✔
102
    ELFIO::Elf_Xword size = sec.get_size() - start;
7,116✔
103
    for (ELFIO::Elf_Xword index = 0; index < symbol_count; index++) {
29,619,048✔
104
        auto symbol_details = get_symbol_details(symbols, index);
29,611,932✔
105
        if (symbol_details.section_index == section_index && !symbol_details.name.empty()) {
29,611,932✔
106
            if (symbol_details.type != ELFIO::STT_FUNC) {
258,322✔
107
                continue;
255,140✔
108
            }
109
            const auto relocation_offset = symbol_details.value;
3,182✔
110
            if (relocation_offset == start) {
3,182✔
111
                // We found the program name for this program.
112
                program_name = symbol_details.name;
14,681,366✔
113
            } else if (relocation_offset > start && relocation_offset < start + size) {
212✔
114
                // We found another program that follows, so truncate the size of this program.
115
                size = relocation_offset - start;
76✔
116
            }
117
        }
118
    }
29,611,932✔
119
    return {program_name, size};
14,232✔
120
}
7,116✔
121

NEW
122
static std::string bad_reloc_value(const size_t reloc_value) {
×
NEW
123
    return "Bad reloc value (" + std::to_string(reloc_value) + "). " + "Make sure to compile with -O2.";
×
124
}
125

126
// Structure used to keep track of subprogram relocation data until any subprograms
127
// are loaded and can be appended to the calling program.
128
struct function_relocation {
68,676✔
129
    size_t prog_index{};              // Index of source program in std::vector of raw programs.
130
    ELFIO::Elf_Xword source_offset{}; // Instruction offset in source section of source instruction.
131
    ELFIO::Elf_Xword relocation_entry_index{};
132
    std::string target_function_name;
133
};
134

135
static raw_program* find_subprogram(std::vector<raw_program>& programs, const ELFIO::section& subprogram_section,
1,958✔
136
                                    const std::string& symbol_name) {
137
    // Find subprogram by name.
138
    for (auto& subprog : programs) {
186,646✔
139
        if (subprog.section_name == subprogram_section.get_name() && subprog.function_name == symbol_name) {
277,050✔
140
            return &subprog;
12✔
141
        }
142
    }
143
    return nullptr;
973✔
144
}
145

146
using map_offsets_t = std::map<std::string, size_t>;
147

148
struct elf_global_data {
340✔
149
    std::set<ELFIO::Elf_Half> map_section_indices;
150
    std::vector<EbpfMapDescriptor> map_descriptors;
151
    std::variant<size_t, map_offsets_t> map_record_size_or_map_offsets;
152
    std::set<ELFIO::Elf_Half> variable_section_indices;
153
};
154

155
static std::map<int, int> map_typeid_to_fd(const std::vector<EbpfMapDescriptor>& map_descriptors) {
116✔
156
    std::map<int, int> type_id_to_fd_map;
116✔
157
    int pseudo_fd = 1;
116✔
158
    // Gather the typeids for each map and assign a pseudo-fd to each map.
159
    for (const auto& map_descriptor : map_descriptors) {
208✔
160
        if (!type_id_to_fd_map.contains(map_descriptor.original_fd)) {
92✔
161
            type_id_to_fd_map[map_descriptor.original_fd] = pseudo_fd++;
92✔
162
        }
163
    }
164
    return type_id_to_fd_map;
116✔
UNCOV
165
}
×
166

167
static ELFIO::const_symbol_section_accessor read_and_validate_symbol_section(const ELFIO::elfio& reader,
754✔
168
                                                                             const std::string& path) {
169
    const ELFIO::section* symbol_section = reader.sections[".symtab"];
754✔
170
    if (!symbol_section) {
754✔
171
        throw UnmarshalError("No symbol section found in ELF file " + path);
×
172
    }
173

174
    // Make sure the ELFIO library will be able to parse the symbol section correctly.
175
    const auto expected_entry_size =
377✔
176
        reader.get_class() == ELFIO::ELFCLASS32 ? sizeof(ELFIO::Elf32_Sym) : sizeof(ELFIO::Elf64_Sym);
754✔
177

178
    if (symbol_section->get_entry_size() != expected_entry_size) {
754✔
179
        throw UnmarshalError("Invalid symbol section found in ELF file " + path);
3✔
180
    }
181
    return ELFIO::const_symbol_section_accessor{reader, symbol_section};
752✔
182
}
183

184
static ELFIO::elfio load_elf(std::istream& input_stream, const std::string& path) {
754✔
185
    ELFIO::elfio reader;
754✔
186
    if (!reader.load(input_stream)) {
754✔
UNCOV
187
        throw UnmarshalError("Can't process ELF file " + path);
×
188
    }
189
    return reader;
754✔
UNCOV
190
}
×
191

UNCOV
192
static void dump_btf_types(const libbtf::btf_type_data& btf_data, const std::string& path) {
×
UNCOV
193
    std::stringstream output;
×
UNCOV
194
    std::cout << "Dumping BTF data for" << path << std::endl;
×
195
    // Dump the BTF data to stdout for debugging purposes.
UNCOV
196
    btf_data.to_json(output);
×
UNCOV
197
    std::cout << libbtf::pretty_print_json(output.str()) << std::endl;
×
UNCOV
198
    std::cout << std::endl;
×
199
}
×
200

201
static void update_line_info(std::vector<raw_program>& raw_programs, const ELFIO::section* btf_section,
186✔
202
                             const ELFIO::section* btf_ext) {
203
    auto visitor = [&raw_programs](const std::string& section, const uint32_t instruction_offset,
1,256,823✔
204
                                   const std::string& file_name, const std::string& source, const uint32_t line_number,
205
                                   const uint32_t column_number) {
206
        for (auto& program : raw_programs) {
61,377,566✔
207
            if (program.section_name == section && instruction_offset >= program.insn_off &&
60,749,352✔
208
                instruction_offset < program.insn_off + program.prog.size() * sizeof(ebpf_inst)) {
1,257,032✔
209
                const size_t inst_index = (instruction_offset - program.insn_off) / sizeof(ebpf_inst);
1,256,730✔
210
                if (inst_index >= program.prog.size()) {
1,256,730✔
UNCOV
211
                    throw UnmarshalError("Invalid BTF data");
×
212
                }
213
                program.info.line_info.insert_or_assign(inst_index,
628,365✔
214
                                                        btf_line_info_t{file_name, source, line_number, column_number});
1,885,095✔
215
            }
216
        }
217
    };
1,256,730✔
218

219
    libbtf::btf_parse_line_information(vector_of<std::byte>(*btf_section), vector_of<std::byte>(*btf_ext), visitor);
279✔
220

221
    // BTF doesn't include line info for every instruction, only on the first instruction per source line.
222
    for (auto& program : raw_programs) {
3,154✔
223
        for (size_t i = 1; i < program.prog.size(); i++) {
4,884,832✔
224
            // If the previous PC has line info, copy it.
225
            if (program.info.line_info[i].line_number == 0 && program.info.line_info[i - 1].line_number != 0) {
4,881,864✔
226
                program.info.line_info[i] = program.info.line_info[i - 1];
3,782,354✔
227
            }
228
        }
229
    }
230
}
186✔
231

232
static elf_global_data parse_btf_section(const parse_params_t& parse_params, const ELFIO::elfio& reader) {
188✔
233
    const auto btf_section = reader.sections[".BTF"];
188✔
234
    if (!btf_section) {
188✔
235
        return {};
72✔
236
    }
237
    const libbtf::btf_type_data btf_data = vector_of<std::byte>(*btf_section);
116✔
238
    if (parse_params.options.verbosity_opts.dump_btf_types_json) {
116✔
NEW
239
        dump_btf_types(btf_data, parse_params.path);
×
240
    }
241

242
    elf_global_data global;
116✔
243

244
    {
58✔
245
        map_offsets_t map_offsets;
116✔
246
        for (const auto& map : parse_btf_map_section(btf_data)) {
208✔
247
            map_offsets.emplace(map.name, global.map_descriptors.size());
92✔
248
            global.map_descriptors.push_back({
92✔
249
                .original_fd = gsl::narrow_cast<int>(map.type_id),
92✔
250
                .type = map.map_type,
92✔
251
                .key_size = map.key_size,
92✔
252
                .value_size = map.value_size,
92✔
253
                .max_entries = map.max_entries,
92✔
254
                .inner_map_fd = map.inner_map_type_id != 0 ? map.inner_map_type_id : DEFAULT_MAP_FD,
92✔
255
            });
256
        }
116✔
257
        global.map_record_size_or_map_offsets = std::move(map_offsets);
116✔
258
    }
58✔
259

260
    {
58✔
261
        // Prevail requires:
262
        // Map fds are sequential starting from 1.
263
        // Map fds are assigned in the order of the maps in the ELF file.
264
        const std::map<int, int> type_id_to_fd_map = map_typeid_to_fd(global.map_descriptors);
116✔
265
        for (auto& map_descriptor : global.map_descriptors) {
208✔
266
            map_descriptor.original_fd = type_id_to_fd_map.at(map_descriptor.original_fd);
92✔
267
            if (map_descriptor.inner_map_fd != DEFAULT_MAP_FD) {
92✔
268
                map_descriptor.inner_map_fd = type_id_to_fd_map.at(map_descriptor.inner_map_fd);
6✔
269
            }
270
        }
271
    }
58✔
272

273
    if (const auto maps_section = reader.sections[".maps"]) {
116✔
274
        global.map_section_indices.insert(maps_section->get_index());
74✔
275
    }
276

277
    for (const auto section_name : {".rodata", ".data", ".bss"}) {
464✔
278
        if (const auto section = reader.sections[section_name]) {
348✔
279
            if (section->get_size() != 0) {
6✔
280
                global.variable_section_indices.insert(section->get_index());
6✔
281
            }
282
        }
283
    }
284
    return global;
116✔
285
}
188✔
286

287
// parse_maps_sections processes all maps sections in the provided ELF file by calling the platform-specific maps'
288
// parser. The section index of each maps section is inserted into map_section_indices.
289
static elf_global_data parse_map_sections(const parse_params_t& parse_params, const ELFIO::elfio& reader,
564✔
290
                                          const ELFIO::const_symbol_section_accessor& symbols) {
291
    elf_global_data global;
564✔
292
    size_t map_record_size = parse_params.platform->map_record_size;
564✔
293
    for (ELFIO::Elf_Half i = 0; i < reader.sections.size(); ++i) {
20,532✔
294
        const auto s = reader.sections[i];
19,968✔
295
        if (!is_map_section(s->get_name())) {
19,968✔
296
            continue;
19,402✔
297
        }
298

299
        // Count the number of symbols that point into this maps section.
300
        int map_count = 0;
283✔
301
        for (ELFIO::Elf_Xword index = 0; index < symbols.get_symbols_num(); index++) {
484,974✔
302
            const auto symbol_details = get_symbol_details(symbols, index);
484,408✔
303
            if (symbol_details.section_index == i && !symbol_details.name.empty()) {
484,408✔
304
                map_count++;
5,800✔
305
            }
306
        }
484,408✔
307

308
        if (map_count > 0) {
566✔
309
            map_record_size = s->get_size() / map_count;
566✔
310
            if (s->get_data() == nullptr || map_record_size == 0) {
566✔
NEW
311
                throw UnmarshalError("bad maps section");
×
312
            }
313
            if (s->get_size() % map_record_size != 0) {
566✔
NEW
314
                throw UnmarshalError("bad maps section size");
×
315
            }
316
            parse_params.platform->parse_maps_section(global.map_descriptors, s->get_data(), map_record_size, map_count,
566✔
317
                                                      parse_params.platform, parse_params.options);
566✔
318
        }
319
        global.map_section_indices.insert(s->get_index());
566✔
320
    }
321
    parse_params.platform->resolve_inner_map_references(global.map_descriptors);
564✔
322
    global.map_record_size_or_map_offsets = map_record_size;
564✔
323
    return global;
846✔
UNCOV
324
}
×
325

326
static elf_global_data extract_global_data(const parse_params_t& parse_params, const ELFIO::elfio& reader,
752✔
327
                                           const ELFIO::const_symbol_section_accessor& symbols) {
328
    if (std::ranges::any_of(reader.sections, [](const auto& section) { return is_map_section(section->get_name()); })) {
19,738✔
329
        return parse_map_sections(parse_params, reader, symbols);
564✔
330
    }
331
    return parse_btf_section(parse_params, reader);
188✔
332
}
333

334
class program_reader_t {
335
    const parse_params_t& parse_params;
336
    const ELFIO::elfio& reader;
337
    const ELFIO::const_symbol_section_accessor& symbols;
338
    const elf_global_data& global;
339

340
    std::vector<function_relocation> function_relocations;
341
    std::vector<std::string> unresolved_symbol_errors;
342
    std::map<const raw_program*, bool> resolved_subprograms;
343

344
  public:
345
    std::vector<raw_program> raw_programs;
346

347
    program_reader_t(const parse_params_t& parse_params, const ELFIO::elfio& reader,
752✔
348
                     const ELFIO::const_symbol_section_accessor& symbols, const elf_global_data& global)
349
        : parse_params{parse_params}, reader{reader}, symbols{symbols}, global{global} {}
752✔
350

351
    // Returns an error message, or empty string on success.
352
    std::string append_subprograms(raw_program& prog) {
7,128✔
353
        if (resolved_subprograms[&prog]) {
7,128✔
354
            // We've already appended any relevant subprograms.
355
            return {};
12✔
356
        }
357
        resolved_subprograms[&prog] = true;
7,116✔
358

359
        // Perform function relocations and fill in the inst.imm values of CallLocal instructions.
360
        std::map<std::string, ELFIO::Elf_Xword> subprogram_offsets;
7,116✔
361
        for (const auto& reloc : function_relocations) {
1,880,892✔
362
            if (reloc.prog_index >= raw_programs.size()) {
1,875,722✔
363
                continue;
1,873,764✔
364
            }
365
            if (raw_programs[reloc.prog_index].function_name != prog.function_name) {
1,875,722✔
366
                continue;
1,873,752✔
367
            }
368

369
            // Check whether we already appended the target program, and append it if not.
370
            if (!subprogram_offsets.contains(reloc.target_function_name)) {
1,970✔
371
                subprogram_offsets[reloc.target_function_name] = prog.prog.size();
1,958✔
372

373
                const auto symbol_details = get_symbol_details(symbols, reloc.relocation_entry_index);
1,958✔
374
                if (symbol_details.section_index >= reader.sections.size()) {
1,958✔
NEW
375
                    throw UnmarshalError("Invalid section index " + std::to_string(symbol_details.section_index) +
×
UNCOV
376
                                         " at source offset " + std::to_string(reloc.source_offset));
×
377
                }
378
                const ELFIO::section& subprogram_section = *reader.sections[symbol_details.section_index];
1,958✔
379

380
                if (const auto subprogram = find_subprogram(raw_programs, subprogram_section, symbol_details.name)) {
1,958✔
381
                    // Make sure subprogram has already had any subprograms of its own appended.
382
                    std::string error = append_subprograms(*subprogram);
12✔
383
                    if (!error.empty()) {
12✔
UNCOV
384
                        return error;
×
385
                    }
386

387
                    // Append subprogram to program.
388
                    prog.prog.insert(prog.prog.end(), subprogram->prog.begin(), subprogram->prog.end());
12✔
389
                    for (size_t i = 0; i < subprogram->info.line_info.size(); i++) {
216✔
390
                        prog.info.line_info[prog.info.line_info.size()] = subprogram->info.line_info[i];
204✔
391
                    }
392
                } else {
12✔
393
                    // The program will be invalid, but continue rather than throwing an exception
394
                    // since we might be verifying a different program in the file.
395
                    return std::string("Subprogram '" + symbol_details.name + "' not found in section '" +
4,865✔
396
                                       subprogram_section.get_name() + "'");
5,838✔
397
                }
398
            }
1,958✔
399

400
            // Fill in the PC offset into the imm field of the CallLocal instruction.
401
            const int64_t target_offset = gsl::narrow_cast<int64_t>(subprogram_offsets[reloc.target_function_name]);
24✔
402
            const auto offset_diff = target_offset - gsl::narrow<int64_t>(reloc.source_offset) - 1;
24✔
403
            if (offset_diff < std::numeric_limits<int32_t>::min() ||
36✔
404
                offset_diff > std::numeric_limits<int32_t>::max()) {
12✔
UNCOV
405
                throw UnmarshalError("Offset difference out of int32_t range for instruction at source offset " +
×
UNCOV
406
                                     std::to_string(reloc.source_offset));
×
407
            }
408
            prog.prog[reloc.source_offset].imm = gsl::narrow_cast<int32_t>(offset_diff);
24✔
409
        }
410
        return {};
5,170✔
411
    }
7,116✔
412

413
    int relocate_map(const std::string& symbol_name, const ELFIO::Elf_Word index) const {
156,922✔
414
        // Relocation value is an offset into the "maps" or ".maps" section.
415
        size_t reloc_value{};
156,922✔
416
        if (const auto* map_record_size = std::get_if<size_t>(&global.map_record_size_or_map_offsets)) {
156,922✔
417
            // The older maps section format uses a single map_record_size value,
418
            // so we can calculate the map descriptor index directly.
419
            const auto symbol_details = get_symbol_details(symbols, index);
156,838✔
420
            const auto relocation_offset = symbol_details.value;
156,838✔
421
            reloc_value = relocation_offset / *map_record_size;
156,838✔
422
        } else {
156,838✔
423
            // The newer .maps section format uses a variable-length map descriptor array,
424
            // so we need to look up the map descriptor index in a map.
425
            const auto& map_descriptors_offsets = std::get<map_offsets_t>(global.map_record_size_or_map_offsets);
84✔
426
            const auto it = map_descriptors_offsets.find(symbol_name);
84✔
427
            if (it == map_descriptors_offsets.end()) {
84✔
NEW
428
                throw UnmarshalError("Map descriptor not found for symbol " + symbol_name);
×
429
            }
430
            reloc_value = it->second;
84✔
431
        }
432
        if (reloc_value >= global.map_descriptors.size()) {
156,922✔
NEW
433
            throw UnmarshalError(bad_reloc_value(reloc_value));
×
434
        }
435
        return global.map_descriptors.at(reloc_value).original_fd;
156,922✔
436
    }
437

438
    int relocate_global_variable(const std::string& symbol_name) const {
8✔
439
        const auto map_descriptors_offsets = std::get_if<map_offsets_t>(&global.map_record_size_or_map_offsets);
8✔
440
        if (!map_descriptors_offsets) {
8✔
NEW
441
            throw UnmarshalError("Invalid map_offsets");
×
442
        }
443
        const auto it = map_descriptors_offsets->find(symbol_name);
8✔
444
        if (it == map_descriptors_offsets->end()) {
8✔
NEW
445
            throw UnmarshalError("Map descriptor not found for symbol " + symbol_name);
×
446
        }
447
        const size_t reloc_value = it->second;
8✔
448
        if (reloc_value >= global.map_descriptors.size()) {
8✔
NEW
449
            throw UnmarshalError(bad_reloc_value(reloc_value));
×
450
        }
451
        return global.map_descriptors.at(reloc_value).original_fd;
12✔
452
    }
453

454
    bool try_reloc(const std::string& symbol_name, const ELFIO::Elf_Half symbol_section_index,
186,332✔
455
                   std::vector<ebpf_inst>& instructions, const size_t location, const ELFIO::Elf_Word index) {
456
        ebpf_inst& instruction_to_relocate = instructions[location];
186,332✔
457

458
        // Queue up relocation for function symbols.
459
        if (instruction_to_relocate.opcode == INST_OP_CALL && instruction_to_relocate.src == INST_CALL_LOCAL) {
186,332✔
460
            function_relocations.emplace_back(function_relocation{
29,402✔
461
                .prog_index = raw_programs.size(),
29,402✔
462
                .source_offset = location,
463
                .relocation_entry_index = index,
29,402✔
464
                .target_function_name = symbol_name,
465
            });
466
            return true;
29,402✔
467
        }
468

469
        // Verify that this is a map or global variable relocation.
470
        if ((instruction_to_relocate.opcode & INST_CLS_MASK) != INST_CLS_LD) {
156,930✔
UNCOV
471
            throw UnmarshalError("Illegal operation on symbol " + symbol_name + " at location " +
×
UNCOV
472
                                 std::to_string(location));
×
473
        }
474

475
        // Perform relocation for symbols located in the maps section.
476
        if (global.map_section_indices.contains(symbol_section_index)) {
156,930✔
477
            instruction_to_relocate.src = INST_LD_MODE_MAP_FD;
156,922✔
478
            instruction_to_relocate.imm = relocate_map(symbol_name, index);
156,922✔
479
            return true;
156,922✔
480
        }
481

482
        if (global.variable_section_indices.contains(symbol_section_index)) {
8✔
483
            // Load instructions are two instructions long, so we need to check the next instruction.
484
            if (instructions.size() <= location + 1) {
8✔
UNCOV
485
                throw UnmarshalError("Invalid relocation data");
×
486
            }
487
            // Copy the immediate value to the next instruction.
488
            instructions[location + 1].imm = instruction_to_relocate.imm;
8✔
489
            instruction_to_relocate.src = INST_LD_MODE_MAP_VALUE;
8✔
490
            instruction_to_relocate.imm = relocate_global_variable(reader.sections[symbol_section_index]->get_name());
12✔
491
            return true;
8✔
492
        }
493
        return false;
494
    }
495

496
    void process_relocations(std::vector<ebpf_inst>& instructions,
6,676✔
497
                             const ELFIO::const_relocation_section_accessor& reloc, const std::string& section_name,
498
                             const ELFIO::Elf_Xword program_offset, const size_t program_size) {
499
        for (ELFIO::Elf_Xword i = 0; i < reloc.get_entries_num(); i++) {
193,060✔
500
            ELFIO::Elf64_Addr offset{};
186,384✔
501
            ELFIO::Elf_Word index{};
186,384✔
502
            unsigned type{};
186,384✔
503
            ELFIO::Elf_Sxword addend{};
186,384✔
504
            if (reloc.get_entry(i, offset, index, type, addend)) {
186,384✔
505
                if (offset < program_offset || offset >= program_offset + program_size) {
186,384✔
506
                    // Relocation is not for this program.
507
                    continue;
52✔
508
                }
509
                offset -= program_offset;
186,332✔
510
                const unsigned long location = offset / sizeof(ebpf_inst);
186,332✔
511
                if (location >= instructions.size()) {
186,332✔
NEW
512
                    throw UnmarshalError("Invalid relocation data");
×
513
                }
514

515
                const auto symbol_details = get_symbol_details(symbols, index);
186,332✔
516
                if (!try_reloc(symbol_details.name, symbol_details.section_index, instructions, location, index)) {
186,332✔
NEW
517
                    unresolved_symbol_errors.push_back("Unresolved external symbol " + symbol_details.name +
×
NEW
518
                                                       " in section " + section_name + " at location " +
×
NEW
519
                                                       std::to_string(location));
×
520
                }
521
            }
186,332✔
522
        }
523
    }
6,676✔
524

525
    const ELFIO::section* get_relocation_section(const std::string& section_name) const {
7,116✔
526
        const ELFIO::section* prelocs = reader.sections[".rel" + section_name];
7,116✔
527
        if (!prelocs) {
7,116✔
528
            prelocs = reader.sections[".rela" + section_name];
1,070✔
529
        }
530
        if (!prelocs) {
4,093✔
531
            return nullptr;
220✔
532
        }
533
        if (!prelocs->get_data()) {
6,676✔
NEW
534
            throw UnmarshalError("Malformed relocation data");
×
535
        }
536
        return prelocs;
3,338✔
537
    }
538

539
    void read_programs() {
752✔
540
        for (const auto& section : reader.sections) {
24,200✔
541
            if (!(section->get_flags() & ELFIO::SHF_EXECINSTR)) {
23,448✔
542
                // Section does not contain executable instructions.
543
                continue;
16,378✔
544
            }
545
            const auto section_size = section->get_size();
7,738✔
546
            if (section_size == 0) {
7,738✔
547
                continue;
668✔
548
            }
549
            const auto section_data = section->get_data();
7,070✔
550
            if (section_data == nullptr) {
7,070✔
UNCOV
551
                continue;
×
552
            }
553
            const std::string section_name = section->get_name();
7,070✔
554
            const EbpfProgramType program_type =
3,535✔
555
                parse_params.platform->get_program_type(section_name, parse_params.path);
7,070✔
556

557
            for (ELFIO::Elf_Xword program_offset = 0; program_offset < section_size;) {
14,186✔
558
                auto [program_name, program_size] = get_program_name_and_size(*section, program_offset, symbols);
7,116✔
559
                std::vector<ebpf_inst> instructions = vector_of<ebpf_inst>(section_data + program_offset, program_size);
7,116✔
560

561
                if (const ELFIO::section* reloc_section = get_relocation_section(section_name)) {
7,116✔
562
                    const ELFIO::const_relocation_section_accessor reloc{reader, reloc_section};
6,676✔
563
                    process_relocations(instructions, reloc, section_name, program_offset, program_size);
6,676✔
564
                }
565

566
                raw_programs.emplace_back(raw_program{
17,790✔
567
                    parse_params.path,
7,116✔
568
                    section_name,
569
                    gsl::narrow_cast<uint32_t>(program_offset),
7,116✔
570
                    program_name,
571
                    std::move(instructions),
3,558✔
572
                    program_info{
573
                        .platform = parse_params.platform,
7,116✔
574
                        .map_descriptors = global.map_descriptors,
7,116✔
575
                        .type = program_type,
576
                    },
577
                });
578
                program_offset += program_size;
7,116✔
579
            }
7,116✔
580
        }
7,070✔
581

582
        // Below, only relocations of symbols located in the maps sections are allowed,
583
        // so if there are relocations there needs to be a maps section.
584
        if (!unresolved_symbol_errors.empty()) {
752✔
NEW
585
            for (const auto& unresolved_symbol : unresolved_symbol_errors) {
×
UNCOV
586
                std::cerr << unresolved_symbol << std::endl;
×
587
            }
NEW
588
            throw UnmarshalError("There are relocations in section but no maps sections in file " + parse_params.path +
×
UNCOV
589
                                 "\nMake sure to inline all function calls.");
×
590
        }
591

592
        if (const auto btf_section = reader.sections[".BTF"]) {
752✔
593
            if (const auto btf_ext = reader.sections[".BTF.ext"]) {
186✔
594
                update_line_info(raw_programs, btf_section, btf_ext);
186✔
595
            }
596
        }
597

598
        // Now that we have all programs in the list, we can recursively append any subprograms
599
        // to the calling programs.  We have to keep them as programs themselves in case the caller
600
        // wants to verify them separately, but we also have to append them if used as subprograms to
601
        // allow the caller to be fully verified since inst.imm can only point into the same program.
602
        for (auto& prog : raw_programs) {
7,866✔
603
            std::string error = append_subprograms(prog);
7,116✔
604
            if (!error.empty()) {
7,116✔
605
                if (prog.section_name == parse_params.desired_section) {
1,946✔
606
                    throw UnmarshalError(error);
2✔
607
                }
608
            }
609
        }
7,116✔
610

611
        // Now that we've incorporated any subprograms from other sections, we can narrow the list
612
        // to return to just those programs in the desired section, if any.
613
        if (!parse_params.desired_section.empty() && !raw_programs.empty()) {
750✔
614
            for (int index = raw_programs.size() - 1; index >= 0; index--) {
7,784✔
615
                if (raw_programs[index].section_name != parse_params.desired_section) {
7,072✔
616
                    raw_programs.erase(raw_programs.begin() + index);
6,318✔
617
                }
618
            }
619
        }
620

621
        if (raw_programs.empty()) {
750✔
622
            if (parse_params.desired_section.empty()) {
2✔
NEW
623
                throw UnmarshalError("Can't find any non-empty TEXT sections in file " + parse_params.path);
×
624
            }
625
            throw UnmarshalError("Can't find section " + parse_params.desired_section + " in file " +
4✔
626
                                 parse_params.path);
6✔
627
        }
628
    }
748✔
629
};
630

631
std::vector<raw_program> read_elf(std::istream& input_stream, const std::string& path,
754✔
632
                                  const std::string& desired_section, const ebpf_verifier_options_t& options,
633
                                  const ebpf_platform_t* platform) {
634
    const parse_params_t parse_params{
377✔
635
        .path = path, .options = options, .platform = platform, .desired_section = desired_section};
754✔
636
    const ELFIO::elfio reader = load_elf(input_stream, path);
754✔
637
    const ELFIO::const_symbol_section_accessor symbols = read_and_validate_symbol_section(reader, path);
754✔
638
    const elf_global_data global = extract_global_data(parse_params, reader, symbols);
752✔
639
    program_reader_t program_reader{parse_params, reader, symbols, global};
752✔
640
    program_reader.read_programs();
752✔
641
    return std::move(program_reader.raw_programs);
1,122✔
642
}
765✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc