| 22 | }; |
| 23 | |
| 24 | class MDebugInfo { |
| 25 | public: |
| 26 | MDebugInfo(const N64Recomp::ElfParsingConfig& config, const char* mdebug_section, uint32_t mdebug_offset) { |
| 27 | using namespace N64Recomp; |
| 28 | good_ = false; |
| 29 | // Read, byteswap and relocate the symbolic header. Relocation here means convert file-relative offsets to section-relative offsets. |
| 30 | |
| 31 | MDebug::HDRR hdrr; |
| 32 | std::memcpy(&hdrr, mdebug_section, sizeof(MDebug::HDRR)); |
| 33 | hdrr.swap(); |
| 34 | hdrr.relocate(mdebug_offset); |
| 35 | |
| 36 | // Check the magic value and version number are what we expect. |
| 37 | |
| 38 | if (hdrr.magic != MDebug::MAGIC || hdrr.vstamp != 0) { |
| 39 | fmt::print(stderr, "Warning: Found an mdebug section with bad magic value or version (magic={} version={}). Skipping.\n", hdrr.magic, hdrr.vstamp); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // Read the various records that are relevant for collecting static symbols and where they are declared. |
| 44 | |
| 45 | std::vector<MDebug::FDR> fdrs = hdrr.read_fdrs(mdebug_section); |
| 46 | std::vector<MDebug::AUX> all_auxs = hdrr.read_auxs(mdebug_section); |
| 47 | std::vector<MDebug::PDR> all_pdrs = hdrr.read_pdrs(mdebug_section); |
| 48 | std::vector<MDebug::SYMR> all_symrs = hdrr.read_symrs(mdebug_section); |
| 49 | |
| 50 | // For each file descriptor |
| 51 | for (size_t fdr_index = 0; fdr_index < fdrs.size(); fdr_index++) { |
| 52 | MDebug::FDR& fdr = fdrs[fdr_index]; |
| 53 | MDebugSymbol pending_sym{}; |
| 54 | bool pending_sym_ready = false; |
| 55 | |
| 56 | auto flush_pending_sym = [&]() { |
| 57 | if (pending_sym_ready) { |
| 58 | // Handle ignored symbols. |
| 59 | pending_sym.ignored = config.ignored_syms.contains(pending_sym.name); |
| 60 | // Add the symbol. |
| 61 | add_symbol(fdr_index, std::move(pending_sym)); |
| 62 | } |
| 63 | pending_sym_ready = false; |
| 64 | }; |
| 65 | |
| 66 | const char* fdr_name = fdr.get_name(mdebug_section + hdrr.cbSsOffset); |
| 67 | add_file(fdr_name); |
| 68 | |
| 69 | // For every symbol record in the file descriptor record. |
| 70 | for (auto symr : fdr.get_symrs(all_symrs)) { |
| 71 | MDebug::ST type = symr.get_st(); |
| 72 | MDebug::SC storage_class = symr.get_sc(); |
| 73 | |
| 74 | switch (type) { |
| 75 | case MDebug::ST_PROC: |
| 76 | case MDebug::ST_STATICPROC: |
| 77 | flush_pending_sym(); |
| 78 | if (symr.value != 0) { |
| 79 | pending_sym.name = fdr.get_string(mdebug_section + hdrr.cbSsOffset, symr.iss); |
| 80 | pending_sym.address = symr.value; |
| 81 | pending_sym.size = 0; |
nothing calls this directly
no outgoing calls
no test coverage detected