| 149 | } |
| 150 | |
| 151 | bool populate_context(const N64Recomp::ElfParsingConfig& elf_config, N64Recomp::Context& context, N64Recomp::DataSymbolMap& data_syms) { |
| 152 | size_t num_files = files_.size(); |
| 153 | std::vector<uint16_t> file_text_sections{}; |
| 154 | std::vector<uint16_t> file_data_sections{}; |
| 155 | std::vector<uint16_t> file_rodata_sections{}; |
| 156 | std::vector<uint16_t> file_bss_sections{}; |
| 157 | file_text_sections.resize(num_files, (uint16_t)-1); |
| 158 | file_data_sections.resize(num_files, (uint16_t)-1); |
| 159 | file_rodata_sections.resize(num_files, (uint16_t)-1); |
| 160 | file_bss_sections.resize(num_files, (uint16_t)-1); |
| 161 | std::unordered_map<std::string, std::vector<size_t>> mdebug_symbol_names{}; // Maps symbol name to list of files that have a symbol of that name. |
| 162 | |
| 163 | // Build a lookup of section name to elf section index. |
| 164 | std::unordered_map<std::string, uint16_t> elf_sections_by_name{}; |
| 165 | for (uint16_t section_index = 0; section_index < context.sections.size(); section_index++) { |
| 166 | const N64Recomp::Section& section = context.sections[section_index]; |
| 167 | elf_sections_by_name.emplace(section.name, section_index); |
| 168 | } |
| 169 | |
| 170 | // First pass to collect symbol names and map mdebug files to elf sections. |
| 171 | for (size_t file_index = 0; file_index < num_files; file_index++) { |
| 172 | const MDebugFile& file = files_[file_index]; |
| 173 | |
| 174 | if (file.symbols.empty()) { |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | bool has_funcs = false; |
| 179 | bool has_data = false; |
| 180 | bool has_rodata = false; |
| 181 | bool has_bss = false; |
| 182 | // Find the section that this file's .text was placed into by looking up global functions. |
| 183 | int file_text_section = -1; |
| 184 | uint32_t min_data_address = 0x0; |
| 185 | uint32_t max_data_address = 0x0; |
| 186 | uint32_t min_rodata_address = 0x0; |
| 187 | uint32_t max_rodata_address = 0x0; |
| 188 | uint32_t min_bss_address = 0x0; |
| 189 | uint32_t max_bss_address = 0x0; |
| 190 | for (const auto& sym : file.symbols) { |
| 191 | if (sym.ignored) { |
| 192 | continue; |
| 193 | } |
| 194 | mdebug_symbol_names[sym.name].emplace_back(file_index); |
| 195 | if (sym.is_func) { |
| 196 | has_funcs = true; |
| 197 | if (!sym.is_static) { |
| 198 | auto find_it = context.functions_by_name.find(sym.name); |
| 199 | if (find_it != context.functions_by_name.end()) { |
| 200 | const auto& found_sym = context.functions[find_it->second]; |
| 201 | file_text_section = found_sym.section_index; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | else { |
| 206 | if (sym.address != 0) { |
| 207 | // .bss |
| 208 | if (sym.is_bss) { |