Use std::vector for section symbols

This commit is contained in:
Rangi42
2024-02-27 12:00:34 -05:00
committed by Sylvie
parent f47ce337bf
commit dead69eb2c
4 changed files with 22 additions and 45 deletions

View File

@@ -48,8 +48,7 @@ struct Section {
std::vector<struct Patch> *patches; std::vector<struct Patch> *patches;
// Extra info computed during linking // Extra info computed during linking
std::vector<struct Symbol *> *fileSymbols; std::vector<struct Symbol *> *fileSymbols;
uint32_t nbSymbols; std::vector<struct Symbol *> *symbols;
struct Symbol **symbols;
struct Section *nextu; // The next "component" of this unionized sect struct Section *nextu; // The next "component" of this unionized sect
}; };

View File

@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: MIT */ /* SPDX-License-Identifier: MIT */
#include <algorithm>
#include <deque> #include <deque>
#include <errno.h> #include <errno.h>
#include <inttypes.h> #include <inttypes.h>
@@ -386,26 +387,18 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
*/ */
static void linkSymToSect(struct Symbol *symbol, struct Section *section) 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) { while (a != b) {
uint32_t c = (a + b) / 2; uint32_t c = (a + b) / 2;
if (section->symbols[c]->offset > symbol->offset) if ((*section->symbols)[c]->offset > symbol->offset)
b = c; b = c;
else else
a = c + 1; a = c + 1;
} }
struct Symbol *tmp = symbol; section->symbols->insert(section->symbols->begin() + a, symbol);
for (uint32_t i = a; i <= section->nbSymbols; i++) {
symbol = tmp;
tmp = section->symbols[i];
section->symbols[i] = symbol;
}
section->nbSymbols++;
} }
/* /*
@@ -544,15 +537,10 @@ void obj_ReadFile(char const *fileName, unsigned int fileID)
fileSections[i]->nextu = NULL; fileSections[i]->nextu = NULL;
readSection(file, fileSections[i], fileName, nodes[fileID]); readSection(file, fileSections[i], fileName, nodes[fileID]);
fileSections[i]->fileSymbols = &fileSymbols; fileSections[i]->fileSymbols = &fileSymbols;
if (nbSymPerSect[i]) { fileSections[i]->symbols = new(std::nothrow) std::vector<struct Symbol *>();
fileSections[i]->symbols = if (!fileSections[i]->symbols)
(struct Symbol **)malloc(nbSymPerSect[i] * sizeof(*fileSections[i]->symbols)); err("%s: Failed to link to symbols", fileName);
if (!fileSections[i]->symbols) fileSections[i]->symbols->reserve(nbSymPerSect[i]);
err("%s: Failed to link to symbols", fileName);
} else {
fileSections[i]->symbols = NULL;
}
fileSections[i]->nbSymbols = 0;
sect_AddSection(fileSections[i]); sect_AddSection(fileSections[i]);
} }
@@ -635,7 +623,7 @@ static void freeSection(struct Section *section)
free(section->data); free(section->data);
delete section->patches; delete section->patches;
} }
free(section->symbols); delete section->symbols;
free(section); free(section);
section = next; section = next;

View File

@@ -335,7 +335,7 @@ static void writeSymBank(struct SortedSections const &bankSections,
uint32_t nbSymbols = 0; uint32_t nbSymbols = 0;
forEachSortedSection(sect, { forEachSortedSection(sect, {
nbSymbols += sect->nbSymbols; nbSymbols += sect->symbols->size();
}); });
if (!nbSymbols) if (!nbSymbols)
@@ -346,9 +346,7 @@ static void writeSymBank(struct SortedSections const &bankSections,
symList.reserve(nbSymbols); symList.reserve(nbSymbols);
forEachSortedSection(sect, { forEachSortedSection(sect, {
for (uint32_t i = 0; i < sect->nbSymbols; i++) { for (struct Symbol const *sym : *sect->symbols) {
struct Symbol const *sym = sect->symbols[i];
// Don't output symbols that begin with an illegal character // Don't output symbols that begin with an illegal character
if (canStartSymName(sym->name[0])) if (canStartSymName(sym->name[0]))
symList.push_back({ .sym = sym, .addr = (uint16_t)(sym->offset + sect->org) }); symList.push_back({ .sym = sym, .addr = (uint16_t)(sym->offset + sect->org) });
@@ -420,11 +418,10 @@ static void writeMapBank(struct SortedSections const &sectList, enum SectionType
uint16_t org = sect->org; uint16_t org = sect->org;
while (sect) { while (sect) {
for (size_t i = 0; i < sect->nbSymbols; i++) for (struct Symbol *sym : *sect->symbols)
// Space matches "\tSECTION: $xxxx ..." // Space matches "\tSECTION: $xxxx ..."
fprintf(mapFile, "\t $%04" PRIx32 " = %s\n", fprintf(mapFile, "\t $%04" PRIx32 " = %s\n",
sect->symbols[i]->offset + org, sym->offset + org, sym->name);
sect->symbols[i]->name);
if (sect->nextu) { if (sect->nextu) {
// Announce the following "piece" // Announce the following "piece"

View File

@@ -355,8 +355,10 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
fatal(where, lineNo, "Failed to alloc new area's patches: %s", fatal(where, lineNo, "Failed to alloc new area's patches: %s",
strerror(errno)); strerror(errno));
curSection->fileSymbols = &fileSymbols; // IDs are instead per-section curSection->fileSymbols = &fileSymbols; // IDs are instead per-section
curSection->nbSymbols = 0; curSection->symbols = new(std::nothrow) std::vector<struct Symbol *>();
curSection->symbols = NULL; // Will be allocated on demand as well if (!curSection->symbols)
fatal(where, lineNo, "Failed to alloc new area's symbol list: %s",
strerror(errno));
curSection->nextu = NULL; curSection->nextu = NULL;
++nbSections; ++nbSections;
@@ -427,17 +429,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
if (strncasecmp(&token[1], "ef", 2) != 0) if (strncasecmp(&token[1], "ef", 2) != 0)
fatal(where, lineNo, "'S' line is neither \"Def\" nor \"Ref\""); fatal(where, lineNo, "'S' line is neither \"Def\" nor \"Ref\"");
if (nbSections != 0) { if (nbSections != 0)
struct Section *section = fileSections[nbSections - 1].section; fileSections[nbSections - 1].section->symbols->push_back(symbol);
++section->nbSymbols;
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;
}
expectEol("'S' line is too long"); expectEol("'S' line is too long");
@@ -757,8 +750,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
if (section->modifier == SECTION_FRAGMENT) { if (section->modifier == SECTION_FRAGMENT) {
// Add the fragment's offset to all of its symbols // Add the fragment's offset to all of its symbols
for (uint32_t j = 0; j < section->nbSymbols; ++j) for (struct Symbol *symbol : *section->symbols)
section->symbols[j]->offset += section->offset; symbol->offset += section->offset;
} }
} }