From 7227fbba0f081305394f7f6449f6ac69e6770f97 Mon Sep 17 00:00:00 2001 From: Anurag Saxena Date: Mon, 13 Apr 2026 22:31:15 -0700 Subject: [PATCH 1/4] fix Signed-off-by: Anurag Saxena --- src/io/elf_map_parser.cpp | 31 +++++++++++++++++++++++++++---- src/spec/type_descriptors.hpp | 3 ++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/io/elf_map_parser.cpp b/src/io/elf_map_parser.cpp index ad3320d2f..b199d9ced 100644 --- a/src/io/elf_map_parser.cpp +++ b/src/io/elf_map_parser.cpp @@ -291,28 +291,51 @@ ElfGlobalData parse_map_sections(const parse_params_t& parse_params, const ELFIO return global; } +/// @brief Mark descriptors that serve only as inner map templates. At runtime +/// the actual inner map can be any map with matching structure, not necessarily +/// the template defined in the ELF. +void mark_inner_map_templates(ElfGlobalData& global) { + for (const auto& desc : global.map_descriptors) { + if (desc.inner_map_fd != DEFAULT_MAP_FD) { + for (auto& inner_desc : global.map_descriptors) { + if (inner_desc.original_fd == desc.inner_map_fd) { + inner_desc.is_inner_map_template = true; + } + } + } + } +} + } // namespace ElfGlobalData extract_global_data(const parse_params_t& params, const ELFIO::elfio& reader, const ELFIO::const_symbol_section_accessor& symbols) { if (reader.sections[".BTF"] && reader.sections[".maps"]) { try { - return parse_btf_section(params, reader); + auto global = parse_btf_section(params, reader); + mark_inner_map_templates(global); + return global; } catch (const UnmarshalError& e) { // BTF-defined maps can't be decoded; fall back to section-based map descriptors. std::cerr << "BTF map parsing failed, falling back to section-based maps: " << e.what() << std::endl; } - return parse_map_sections(params, reader, symbols); + auto global = parse_map_sections(params, reader, symbols); + mark_inner_map_templates(global); + return global; } const bool has_legacy_maps = std::ranges::any_of(reader.sections, [](const auto& s) { return is_map_section(s->get_name()); }); if (has_legacy_maps) { - return parse_map_sections(params, reader, symbols); + auto global = parse_map_sections(params, reader, symbols); + mark_inner_map_templates(global); + return global; } if (reader.sections[".BTF"]) { - return parse_btf_section(params, reader); + auto global = parse_btf_section(params, reader); + mark_inner_map_templates(global); + return global; } return create_global_variable_maps(reader); diff --git a/src/spec/type_descriptors.hpp b/src/spec/type_descriptors.hpp index e298eaab9..31aa4cbb1 100644 --- a/src/spec/type_descriptors.hpp +++ b/src/spec/type_descriptors.hpp @@ -30,7 +30,8 @@ struct EbpfMapDescriptor { unsigned int value_size; unsigned int max_entries; int inner_map_fd; - std::string name; // Map name from ELF (empty if not available). + std::string name; // Map name from ELF (empty if not available). + bool is_inner_map_template{false}; // True if this descriptor is only an inner map template. }; struct EbpfProgramType { From 369e3a219672995810adb0cbef4df7ed6945ac65 Mon Sep 17 00:00:00 2001 From: Anurag Saxena <43585259+saxena-anurag@users.noreply.github.com> Date: Tue, 14 Apr 2026 14:03:15 -0700 Subject: [PATCH 2/4] Update src/spec/type_descriptors.hpp Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Anurag Saxena <43585259+saxena-anurag@users.noreply.github.com> --- src/spec/type_descriptors.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/spec/type_descriptors.hpp b/src/spec/type_descriptors.hpp index 31aa4cbb1..52ea7745b 100644 --- a/src/spec/type_descriptors.hpp +++ b/src/spec/type_descriptors.hpp @@ -31,7 +31,7 @@ struct EbpfMapDescriptor { unsigned int max_entries; int inner_map_fd; std::string name; // Map name from ELF (empty if not available). - bool is_inner_map_template{false}; // True if this descriptor is only an inner map template. + bool is_inner_map_template{false}; // True if this descriptor is referenced as an inner map template. }; struct EbpfProgramType { From d7549e86ffa3da60b98b48c0caad0f4f7fae495c Mon Sep 17 00:00:00 2001 From: Anurag Saxena Date: Wed, 15 Apr 2026 12:01:23 -0700 Subject: [PATCH 3/4] cr comments Signed-off-by: Anurag Saxena --- src/io/elf_map_parser.cpp | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/src/io/elf_map_parser.cpp b/src/io/elf_map_parser.cpp index b199d9ced..7a51f64ec 100644 --- a/src/io/elf_map_parser.cpp +++ b/src/io/elf_map_parser.cpp @@ -310,35 +310,26 @@ void mark_inner_map_templates(ElfGlobalData& global) { ElfGlobalData extract_global_data(const parse_params_t& params, const ELFIO::elfio& reader, const ELFIO::const_symbol_section_accessor& symbols) { + ElfGlobalData global; + if (reader.sections[".BTF"] && reader.sections[".maps"]) { try { - auto global = parse_btf_section(params, reader); - mark_inner_map_templates(global); - return global; + global = parse_btf_section(params, reader); } catch (const UnmarshalError& e) { // BTF-defined maps can't be decoded; fall back to section-based map descriptors. std::cerr << "BTF map parsing failed, falling back to section-based maps: " << e.what() << std::endl; + global = parse_map_sections(params, reader, symbols); } - auto global = parse_map_sections(params, reader, symbols); - mark_inner_map_templates(global); - return global; - } - - const bool has_legacy_maps = - std::ranges::any_of(reader.sections, [](const auto& s) { return is_map_section(s->get_name()); }); - if (has_legacy_maps) { - auto global = parse_map_sections(params, reader, symbols); - mark_inner_map_templates(global); - return global; + } else if (std::ranges::any_of(reader.sections, [](const auto& s) { return is_map_section(s->get_name()); })) { + global = parse_map_sections(params, reader, symbols); + } else if (reader.sections[".BTF"]) { + global = parse_btf_section(params, reader); + } else { + global = create_global_variable_maps(reader); } - if (reader.sections[".BTF"]) { - auto global = parse_btf_section(params, reader); - mark_inner_map_templates(global); - return global; - } - - return create_global_variable_maps(reader); + mark_inner_map_templates(global); + return global; } void update_line_info(std::vector& raw_programs, const ELFIO::section* btf_section, From 1ae0ed3e8fee60f30bff4764232df6db49a9f7f4 Mon Sep 17 00:00:00 2001 From: Anurag Saxena Date: Wed, 15 Apr 2026 15:13:54 -0700 Subject: [PATCH 4/4] refactor code Signed-off-by: Anurag Saxena --- src/io/elf_map_parser.cpp | 55 ++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/src/io/elf_map_parser.cpp b/src/io/elf_map_parser.cpp index 7a51f64ec..2f570166f 100644 --- a/src/io/elf_map_parser.cpp +++ b/src/io/elf_map_parser.cpp @@ -291,10 +291,32 @@ ElfGlobalData parse_map_sections(const parse_params_t& parse_params, const ELFIO return global; } -/// @brief Mark descriptors that serve only as inner map templates. At runtime -/// the actual inner map can be any map with matching structure, not necessarily -/// the template defined in the ELF. -void mark_inner_map_templates(ElfGlobalData& global) { +} // namespace + +ElfGlobalData extract_global_data(const parse_params_t& params, const ELFIO::elfio& reader, + const ELFIO::const_symbol_section_accessor& symbols) { + ElfGlobalData global = [&] { + if (reader.sections[".BTF"] && reader.sections[".maps"]) { + try { + return parse_btf_section(params, reader); + } catch (const UnmarshalError& e) { + // BTF-defined maps can't be decoded; fall back to section-based map descriptors. + std::cerr << "BTF map parsing failed, falling back to section-based maps: " << e.what() << std::endl; + return parse_map_sections(params, reader, symbols); + } + } + if (std::ranges::any_of(reader.sections, [](const auto& s) { return is_map_section(s->get_name()); })) { + return parse_map_sections(params, reader, symbols); + } + if (reader.sections[".BTF"]) { + return parse_btf_section(params, reader); + } + return create_global_variable_maps(reader); + }(); + + /// Mark descriptors that serve only as inner map templates. + /// At runtime the actual inner map can be any map with matching structure, + /// not necessarily the template defined in the ELF. for (const auto& desc : global.map_descriptors) { if (desc.inner_map_fd != DEFAULT_MAP_FD) { for (auto& inner_desc : global.map_descriptors) { @@ -304,31 +326,6 @@ void mark_inner_map_templates(ElfGlobalData& global) { } } } -} - -} // namespace - -ElfGlobalData extract_global_data(const parse_params_t& params, const ELFIO::elfio& reader, - const ELFIO::const_symbol_section_accessor& symbols) { - ElfGlobalData global; - - if (reader.sections[".BTF"] && reader.sections[".maps"]) { - try { - global = parse_btf_section(params, reader); - } catch (const UnmarshalError& e) { - // BTF-defined maps can't be decoded; fall back to section-based map descriptors. - std::cerr << "BTF map parsing failed, falling back to section-based maps: " << e.what() << std::endl; - global = parse_map_sections(params, reader, symbols); - } - } else if (std::ranges::any_of(reader.sections, [](const auto& s) { return is_map_section(s->get_name()); })) { - global = parse_map_sections(params, reader, symbols); - } else if (reader.sections[".BTF"]) { - global = parse_btf_section(params, reader); - } else { - global = create_global_variable_maps(reader); - } - - mark_inner_map_templates(global); return global; }