diff --git a/include/link/section.hpp b/include/link/section.hpp index 3ea3af26..7b31471b 100644 --- a/include/link/section.hpp +++ b/include/link/section.hpp @@ -48,8 +48,7 @@ struct Section { std::vector *patches; // Extra info computed during linking std::vector *fileSymbols; - uint32_t nbSymbols; - struct Symbol **symbols; + std::vector *symbols; struct Section *nextu; // The next "component" of this unionized sect }; diff --git a/src/link/object.cpp b/src/link/object.cpp index e5d41449..5762f79e 100644 --- a/src/link/object.cpp +++ b/src/link/object.cpp @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: MIT */ +#include #include #include #include @@ -386,26 +387,18 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam */ static void linkSymToSect(struct Symbol *symbol, struct Section *section) { - uint32_t a = 0, b = section->nbSymbols; + uint32_t a = 0, b = section->symbols->size(); while (a != b) { uint32_t c = (a + b) / 2; - if (section->symbols[c]->offset > symbol->offset) + if ((*section->symbols)[c]->offset > symbol->offset) b = c; else a = c + 1; } - struct Symbol *tmp = symbol; - - for (uint32_t i = a; i <= section->nbSymbols; i++) { - symbol = tmp; - tmp = section->symbols[i]; - section->symbols[i] = symbol; - } - - section->nbSymbols++; + section->symbols->insert(section->symbols->begin() + a, symbol); } /* @@ -544,15 +537,10 @@ void obj_ReadFile(char const *fileName, unsigned int fileID) fileSections[i]->nextu = NULL; readSection(file, fileSections[i], fileName, nodes[fileID]); fileSections[i]->fileSymbols = &fileSymbols; - if (nbSymPerSect[i]) { - fileSections[i]->symbols = - (struct Symbol **)malloc(nbSymPerSect[i] * sizeof(*fileSections[i]->symbols)); - if (!fileSections[i]->symbols) - err("%s: Failed to link to symbols", fileName); - } else { - fileSections[i]->symbols = NULL; - } - fileSections[i]->nbSymbols = 0; + fileSections[i]->symbols = new(std::nothrow) std::vector(); + if (!fileSections[i]->symbols) + err("%s: Failed to link to symbols", fileName); + fileSections[i]->symbols->reserve(nbSymPerSect[i]); sect_AddSection(fileSections[i]); } @@ -635,7 +623,7 @@ static void freeSection(struct Section *section) free(section->data); delete section->patches; } - free(section->symbols); + delete section->symbols; free(section); section = next; diff --git a/src/link/output.cpp b/src/link/output.cpp index 856a1594..c8cdf1f6 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -335,7 +335,7 @@ static void writeSymBank(struct SortedSections const &bankSections, uint32_t nbSymbols = 0; forEachSortedSection(sect, { - nbSymbols += sect->nbSymbols; + nbSymbols += sect->symbols->size(); }); if (!nbSymbols) @@ -346,9 +346,7 @@ static void writeSymBank(struct SortedSections const &bankSections, symList.reserve(nbSymbols); forEachSortedSection(sect, { - for (uint32_t i = 0; i < sect->nbSymbols; i++) { - struct Symbol const *sym = sect->symbols[i]; - + for (struct Symbol const *sym : *sect->symbols) { // Don't output symbols that begin with an illegal character if (canStartSymName(sym->name[0])) symList.push_back({ .sym = sym, .addr = (uint16_t)(sym->offset + sect->org) }); @@ -420,11 +418,10 @@ static void writeMapBank(struct SortedSections const §List, enum SectionType uint16_t org = sect->org; while (sect) { - for (size_t i = 0; i < sect->nbSymbols; i++) + for (struct Symbol *sym : *sect->symbols) // Space matches "\tSECTION: $xxxx ..." fprintf(mapFile, "\t $%04" PRIx32 " = %s\n", - sect->symbols[i]->offset + org, - sect->symbols[i]->name); + sym->offset + org, sym->name); if (sect->nextu) { // Announce the following "piece" diff --git a/src/link/sdas_obj.cpp b/src/link/sdas_obj.cpp index 18c09014..5c1eb4fc 100644 --- a/src/link/sdas_obj.cpp +++ b/src/link/sdas_obj.cpp @@ -355,8 +355,10 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectorfileSymbols = &fileSymbols; // IDs are instead per-section - curSection->nbSymbols = 0; - curSection->symbols = NULL; // Will be allocated on demand as well + curSection->symbols = new(std::nothrow) std::vector(); + if (!curSection->symbols) + fatal(where, lineNo, "Failed to alloc new area's symbol list: %s", + strerror(errno)); curSection->nextu = NULL; ++nbSections; @@ -427,17 +429,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectornbSymbols; - section->symbols = (struct Symbol **)realloc(section->symbols, - sizeof(section->symbols[0]) * section->nbSymbols); - if (!section->symbols) - fatal(where, lineNo, "Failed to realloc \"%s\"'s symbol list: %s", - section->name, strerror(errno)); - section->symbols[section->nbSymbols - 1] = symbol; - } + if (nbSections != 0) + fileSections[nbSections - 1].section->symbols->push_back(symbol); expectEol("'S' line is too long"); @@ -757,8 +750,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vectormodifier == SECTION_FRAGMENT) { // Add the fragment's offset to all of its symbols - for (uint32_t j = 0; j < section->nbSymbols; ++j) - section->symbols[j]->offset += section->offset; + for (struct Symbol *symbol : *section->symbols) + symbol->offset += section->offset; } }