From 6238ab6eb73168ef79093f6cfb5b27d6ec4b15a3 Mon Sep 17 00:00:00 2001 From: Dave Thaler Date: Thu, 26 Sep 2024 21:39:32 -0700 Subject: [PATCH] PR feedback Signed-off-by: Dave Thaler --- src/asm_files.cpp | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/asm_files.cpp b/src/asm_files.cpp index 3a92b4f8a..874156290 100644 --- a/src/asm_files.cpp +++ b/src/asm_files.cpp @@ -208,33 +208,36 @@ struct function_relocation { std::string target_function_name; }; -static void append_subprogram(raw_program& prog, ELFIO::section* subprogram_section, - ELFIO::const_symbol_section_accessor& symbols, std::string symbol_name) { +static void append_subprogram(raw_program& prog, ELFIO::section& subprogram_section, + ELFIO::const_symbol_section_accessor& symbols, const std::string& symbol_name) { // Find subprogram by name. - for (ELFIO::Elf_Xword subprogram_offset = 0; subprogram_offset < subprogram_section->get_size();) { + for (ELFIO::Elf_Xword subprogram_offset = 0; subprogram_offset < subprogram_section.get_size();) { auto [subprogram_name, subprogram_size] = - get_program_name_and_size(*subprogram_section, subprogram_offset, symbols); + get_program_name_and_size(subprogram_section, subprogram_offset, symbols); if (subprogram_size == 0) { throw std::runtime_error("Zero-size subprogram '" + subprogram_name + "' in section '" + - subprogram_section->get_name() + "'"); + subprogram_section.get_name() + "'"); } if (subprogram_name == symbol_name) { // Append subprogram instructions to the main program. - auto subprogram = vector_of(subprogram_section->get_data() + subprogram_offset, subprogram_size); + auto subprogram = vector_of(subprogram_section.get_data() + subprogram_offset, subprogram_size); prog.prog.insert(prog.prog.end(), subprogram.begin(), subprogram.end()); return; } subprogram_offset += subprogram_size; } throw std::runtime_error("Subprogram '" + symbol_name + "' not found in section '" + - subprogram_section->get_name() + "'"); + subprogram_section.get_name() + "'"); } -static void append_subprograms(raw_program& prog, vector& res, vector& function_relocations, ELFIO::elfio& reader, +static void append_subprograms(raw_program& prog, vector& res, const vector& function_relocations, ELFIO::elfio& reader, ELFIO::const_symbol_section_accessor& symbols) { // Perform function relocations and fill in the inst.imm values of CallLocal instructions. std::map subprogram_offsets; for (auto& reloc : function_relocations) { + if (reloc.prog_index >= res.size()) { + continue; + } if (res[reloc.prog_index].function_name != prog.function_name) { continue; } @@ -244,7 +247,7 @@ static void append_subprograms(raw_program& prog, vector& res, vect subprogram_offsets[reloc.target_function_name] = prog.prog.size(); auto [symbol_name, section_index] = get_symbol_name_and_section_index(symbols, reloc.relocation_entry_index); - ELFIO::section* subprogram_section = reader.sections[section_index]; + ELFIO::section& subprogram_section = *reader.sections[section_index]; append_subprogram(prog, subprogram_section, symbols, symbol_name); } @@ -378,12 +381,6 @@ vector read_elf(std::istream& input_stream, const std::string& path vector_of(section->get_data() + program_offset, program_size), info}; - // We will need to recursively append any subprograms called, but only once - // for each subprogram no matter how many times called. So initialize a set - // to hold the list of subprogram names included. - std::set subprograms{}; - subprograms.insert(program_name); - auto prelocs = reader.sections[string(".rel") + name]; if (!prelocs) { prelocs = reader.sections[string(".rela") + name];