diff --git a/include/asm/section.hpp b/include/asm/section.hpp index 47be50b8..213127e5 100644 --- a/include/asm/section.hpp +++ b/include/asm/section.hpp @@ -5,6 +5,8 @@ #include #include +#include +#include #include #include "linkdefs.hpp" @@ -49,6 +51,7 @@ struct SectionSpec { }; extern std::deque
sectionList; +extern std::unordered_map sectionMap; // Indexes into `sectionList` extern Section *currentSection; Section *sect_FindSectionByName(char const *name); diff --git a/src/asm/output.cpp b/src/asm/output.cpp index 1641ed34..1152ae42 100644 --- a/src/asm/output.cpp +++ b/src/asm/output.cpp @@ -83,10 +83,8 @@ static uint32_t getSectIDIfAny(Section *sect) { if (!sect) return (uint32_t)-1; - for (auto it = sectionList.begin(); it != sectionList.end(); it++) { - if (&*it == sect) - return it - sectionList.begin(); - } + if (auto search = sectionMap.find(sect->name); search != sectionMap.end()) + return (uint32_t)(sectionMap.size() - search->second - 1); fatalerror("Unknown section '%s'\n", sect->name.c_str()); } @@ -373,8 +371,8 @@ void out_WriteObject() { for (Symbol const *sym : objectSymbols) writesymbol(*sym, f); - for (Section § : sectionList) - writesection(sect, f); + for (auto it = sectionList.rbegin(); it != sectionList.rend(); it++) + writesection(*it, f); putlong(assertions.size(), f); diff --git a/src/asm/section.cpp b/src/asm/section.cpp index e8135d2d..15f1df7e 100644 --- a/src/asm/section.cpp +++ b/src/asm/section.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include "error.hpp" @@ -41,6 +40,7 @@ struct SectionStackEntry { std::stack currentUnionStack; std::deque sectionStack; std::deque
sectionList; +std::unordered_map sectionMap; // Indexes into `sectionList` uint32_t curOffset; // Offset into the current section (see sect_GetSymbolOffset) Section *currentSection = nullptr; static Section *currentLoadSection = nullptr; @@ -110,11 +110,8 @@ attr_(warn_unused_result) static bool reserveSpace(uint32_t delta_size) { } Section *sect_FindSectionByName(char const *name) { - for (Section § : sectionList) { - if (sect.name == name) - return § - } - return nullptr; + auto search = sectionMap.find(name); + return search != sectionMap.end() ? §ionList[search->second] : nullptr; } #define mask(align) ((1U << (align)) - 1) @@ -300,8 +297,9 @@ static Section *createSection( uint16_t alignOffset, SectionModifier mod ) { - // Add the new section to the list (order doesn't matter) - Section § = sectionList.emplace_front(); + // Add the new section to the list + Section § = sectionList.emplace_back(); + sectionMap.emplace(name, sectionMap.size()); sect.name = name; sect.type = type;