-
Notifications
You must be signed in to change notification settings - Fork 526
Add changes to get custom section data from xrt::elf #9664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -244,6 +244,10 @@ class elf::kernel_impl | |||||
| xrt_core::xclbin::kernel_properties m_properties; | ||||||
| std::vector<elf::kernel::instance> m_instances; | ||||||
|
|
||||||
| // Map for custom sections of a kernel | ||||||
| // key - custom section name, value - custom section data | ||||||
| std::map<std::string, detail::span<const char>> m_custom_section_map; | ||||||
|
|
||||||
|
Comment on lines
+247
to
+250
|
||||||
| xrt_core::xclbin::kernel_properties | ||||||
| construct_properties(const std::string& name) const | ||||||
| { | ||||||
|
|
@@ -301,11 +305,29 @@ class elf::kernel_impl | |||||
| { | ||||||
| return {m_properties, m_args}; | ||||||
| } | ||||||
|
|
||||||
| void | ||||||
| add_custom_section(const std::string& section_name, detail::span<const char> section_data) | ||||||
| { | ||||||
| m_custom_section_map[section_name] = section_data; | ||||||
| } | ||||||
|
|
||||||
| detail::span<const char> | ||||||
| get_custom_section(const std::string& section_name) const | ||||||
| { | ||||||
| if (auto it = m_custom_section_map.find(section_name); it != m_custom_section_map.end()) | ||||||
| return it->second; | ||||||
|
|
||||||
| throw std::runtime_error("Cannot get custom section " + section_name + " data, section not found for kernel " + m_name); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| class elf::kernel::instance_impl | ||||||
| { | ||||||
| std::string m_name; | ||||||
| // Map for custom sections of an instance | ||||||
| // key - custom section name, value - custom section data | ||||||
| std::map<std::string, detail::span<const char>> m_custom_section_map; | ||||||
|
Comment on lines
+328
to
+330
|
||||||
|
|
||||||
| public: | ||||||
| explicit | ||||||
|
|
@@ -318,6 +340,21 @@ class elf::kernel::instance_impl | |||||
| { | ||||||
| return m_name; | ||||||
| } | ||||||
|
|
||||||
| void | ||||||
| add_custom_section(const std::string& section_name, detail::span<const char> section_data) | ||||||
| { | ||||||
| m_custom_section_map[section_name] = section_data; | ||||||
| } | ||||||
|
|
||||||
| detail::span<const char> | ||||||
| get_custom_section(const std::string& section_name) const | ||||||
| { | ||||||
| if (auto it = m_custom_section_map.find(section_name); it != m_custom_section_map.end()) | ||||||
| return it->second; | ||||||
|
|
||||||
| throw std::runtime_error("Cannot get custom section " + section_name + " data, section not found for instance " + m_name); | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| //////////////////////////////////////////////////////////////// | ||||||
|
|
@@ -351,8 +388,9 @@ get_symbol_from_symtab(uint32_t sym_index) const | |||||
|
|
||||||
| if (!symbols.get_symbol(sym_index, info.name, value, size, bind, | ||||||
| info.type, info.section_index, other)) | ||||||
| throw std::runtime_error("Unable to find symbol in .symtab section with index: " + | ||||||
| std::to_string(sym_index)); | ||||||
| throw std::runtime_error( | ||||||
| "Unable to find symbol in .symtab section with index: " + | ||||||
| std::to_string(sym_index)); | ||||||
|
|
||||||
| return info; | ||||||
| } | ||||||
|
|
@@ -412,12 +450,14 @@ get_kernel_subkernel_from_symtab(uint32_t sym_index) | |||||
| // Get subkernel symbol - must be of type OBJECT | ||||||
| auto subkernel_sym = get_symbol_from_symtab(sym_index); | ||||||
| if (subkernel_sym.type != ELFIO::STT_OBJECT) | ||||||
| throw std::runtime_error("Symbol doesn't point to subkernel entry (expected STT_OBJECT)"); | ||||||
| throw std::runtime_error( | ||||||
| "Symbol doesn't point to subkernel entry (expected STT_OBJECT)"); | ||||||
|
|
||||||
| // Get parent kernel symbol - must be of type FUNC | ||||||
| auto kernel_sym = get_symbol_from_symtab(subkernel_sym.section_index); | ||||||
| if (kernel_sym.type != ELFIO::STT_FUNC) | ||||||
| throw std::runtime_error("Subkernel doesn't point to kernel entry (expected STT_FUNC)"); | ||||||
| throw std::runtime_error( | ||||||
| "Subkernel doesn't point to kernel entry (expected STT_FUNC)"); | ||||||
|
|
||||||
| // Demangle kernel name and extract signature | ||||||
| auto demangled_signature = demangle(kernel_sym.name); | ||||||
|
|
@@ -462,14 +502,16 @@ parse_single_group_section(const ELFIO::section* section) | |||||
| return; | ||||||
|
|
||||||
| // Parse kernel/subkernel from symtab using .group's info field | ||||||
| auto [kernel_name, subkernel_name] = get_kernel_subkernel_from_symtab(section->get_info()); | ||||||
| auto [kernel_name, subkernel_name] = | ||||||
| get_kernel_subkernel_from_symtab(section->get_info()); | ||||||
|
|
||||||
| // Update kernel maps | ||||||
| m_kernel_to_subkernels_map[kernel_name].push_back(subkernel_name); | ||||||
| m_kernel_name_to_id_map[kernel_name + subkernel_name] = group_id; | ||||||
|
|
||||||
| // Parse member section indices (skip flags at index 0) | ||||||
| const auto* word_data = reinterpret_cast<const ELFIO::Elf_Word*>(data); | ||||||
| const auto* word_data = | ||||||
| reinterpret_cast<const ELFIO::Elf_Word*>(data); | ||||||
| const auto word_count = size / sizeof(ELFIO::Elf_Word); | ||||||
|
|
||||||
| std::vector<uint32_t> member_sections; | ||||||
|
|
@@ -482,24 +524,116 @@ parse_single_group_section(const ELFIO::section* section) | |||||
| m_group_to_sections_map.emplace(group_id, std::move(member_sections)); | ||||||
| } | ||||||
|
|
||||||
| // Parse .group sections in the ELF file and populate all maps | ||||||
| // Helper function to add custom section to kernel (instance_name is empty) | ||||||
| // or to an instance. | ||||||
| void | ||||||
| elf_impl:: | ||||||
| parse_group_sections() | ||||||
| add_custom_section_to_kernel_or_instance( | ||||||
| const std::string& kernel_name, | ||||||
| const std::string& instance_name, | ||||||
| const std::string& sec_name, | ||||||
| detail::span<const char> data) | ||||||
| { | ||||||
| if (!is_group_elf()) { | ||||||
| for (auto& kernel : m_kernels) { | ||||||
| if (kernel.get_name() != kernel_name) | ||||||
| continue; | ||||||
|
|
||||||
| if (instance_name.empty()) { | ||||||
| kernel.get_handle()->add_custom_section(sec_name, data); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| for (auto& instance : kernel.get_instances()) { | ||||||
| if (instance.get_name() != instance_name) | ||||||
| continue; | ||||||
|
|
||||||
| instance.get_handle()->add_custom_section(sec_name, data); | ||||||
| return; | ||||||
|
Comment on lines
+546
to
+551
|
||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
Comment on lines
+537
to
+554
|
||||||
|
|
||||||
| void | ||||||
| elf_impl:: | ||||||
| parse_custom_sections(const std::vector<uint32_t>& custom_section_ids) | ||||||
| { | ||||||
| constexpr uint32_t no_symtab_idx = 0; | ||||||
| auto section_span = [](const ELFIO::section* sec) { | ||||||
| return detail::span<const char>(sec->get_data(), sec->get_size()); | ||||||
| }; | ||||||
|
|
||||||
| for (auto sec_id : custom_section_ids) { | ||||||
| auto sec = m_elfio.sections[sec_id]; | ||||||
| if (!sec) | ||||||
| continue; | ||||||
|
|
||||||
| auto symtab_idx = sec->get_info(); | ||||||
| auto sec_name = sec->get_name(); | ||||||
| auto data = section_span(sec); | ||||||
|
|
||||||
| if (m_section_to_group_map.find(sec_id) == m_section_to_group_map.end()) { | ||||||
| // Section not in any group: global or kernel-level custom section. | ||||||
| if (symtab_idx == no_symtab_idx) { | ||||||
| m_global_custom_section_map[sec_name] = data; | ||||||
| continue; | ||||||
| } | ||||||
| // Kernel-level custom section: symtab points to kernel (STT_FUNC). | ||||||
| auto kernel_sym = get_symbol_from_symtab(symtab_idx); | ||||||
| if (kernel_sym.type != ELFIO::STT_FUNC) | ||||||
| throw std::runtime_error( | ||||||
| "custom section points to invalid symtab index (expected STT_FUNC)"); | ||||||
| auto demangled = demangle(kernel_sym.name); | ||||||
| auto kernel_name = extract_kernel_name(demangled); | ||||||
|
|
||||||
| add_custom_section_to_kernel_or_instance( | ||||||
| kernel_name, "", sec_name, data); | ||||||
| } | ||||||
| else { | ||||||
| // Section belongs to a group: instance-level custom section. | ||||||
| auto [kernel_name, subkernel_name] = | ||||||
| get_kernel_subkernel_from_symtab(symtab_idx); | ||||||
|
|
||||||
| add_custom_section_to_kernel_or_instance( | ||||||
| kernel_name, subkernel_name, sec_name, data); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // Parse ELF sections and populate all maps | ||||||
| void | ||||||
| elf_impl:: | ||||||
| parse_sections() | ||||||
| { | ||||||
| if (!is_group_elf()) { // older ELF format without .group sections | ||||||
| init_legacy_section_maps(); | ||||||
| finalize_kernels(); | ||||||
| m_kernel_args_map.clear(); | ||||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // collect custom sections and parse .group sections to populate maps | ||||||
| std::vector<uint32_t> custom_section_ids; | ||||||
| auto CUSTOM_SECTION_TYPE = ELFIO::SHT_LOUSER + 1; | ||||||
|
||||||
| auto CUSTOM_SECTION_TYPE = ELFIO::SHT_LOUSER + 1; | |
| constexpr ELFIO::Elf_Word CUSTOM_SECTION_TYPE = ELFIO::SHT_LOUSER + 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elf_impl::get_custom_section()is a read-only accessor but is declared/defined non-const. Making itconstwould better match the rest of the getter-style API, allow calling it onconst elf_impl, and communicate that it does not mutate internal state.