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;
// Extra info computed during linking
std::vector<struct Symbol *> *fileSymbols;
uint32_t nbSymbols;
struct Symbol **symbols;
std::vector<struct Symbol *> *symbols;
struct Section *nextu; // The next "component" of this unionized sect
};

View File

@@ -1,5 +1,6 @@
/* SPDX-License-Identifier: MIT */
#include <algorithm>
#include <deque>
#include <errno.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)
{
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));
fileSections[i]->symbols = new(std::nothrow) std::vector<struct Symbol *>();
if (!fileSections[i]->symbols)
err("%s: Failed to link to symbols", fileName);
} else {
fileSections[i]->symbols = NULL;
}
fileSections[i]->nbSymbols = 0;
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;

View File

@@ -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 &sectList, 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"

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",
strerror(errno));
curSection->fileSymbols = &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<struct Symbol *>();
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::vector<s
if (strncasecmp(&token[1], "ef", 2) != 0)
fatal(where, lineNo, "'S' line is neither \"Def\" nor \"Ref\"");
if (nbSections != 0) {
struct Section *section = fileSections[nbSections - 1].section;
++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;
}
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::vector<s
if (section->modifier == 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;
}
}