Use automatic allocation for section names

This commit is contained in:
Rangi42
2024-02-28 19:02:00 -05:00
committed by Sylvie
parent 3c0af94c5c
commit 5a26a48d11
7 changed files with 77 additions and 79 deletions

View File

@@ -30,7 +30,7 @@ struct Patch {
struct Section { struct Section {
// Info contained in the object files // Info contained in the object files
std::string *name; std::string name;
uint16_t size; uint16_t size;
uint16_t offset; uint16_t offset;
enum SectionType type; enum SectionType type;

View File

@@ -318,17 +318,17 @@ static void placeSection(struct Section *section)
// If a section failed to go to several places, nothing we can report // If a section failed to go to several places, nothing we can report
if (!section->isBankFixed || !section->isAddressFixed) if (!section->isBankFixed || !section->isAddressFixed)
errx("Unable to place \"%s\" (%s section) %s", errx("Unable to place \"%s\" (%s section) %s",
section->name->c_str(), sectionTypeInfo[section->type].name.c_str(), where); section->name.c_str(), sectionTypeInfo[section->type].name.c_str(), where);
// If the section just can't fit the bank, report that // If the section just can't fit the bank, report that
else if (section->org + section->size > endaddr(section->type) + 1) else if (section->org + section->size > endaddr(section->type) + 1)
errx("Unable to place \"%s\" (%s section) %s: section runs past end of region ($%04x > $%04x)", errx("Unable to place \"%s\" (%s section) %s: section runs past end of region ($%04x > $%04x)",
section->name->c_str(), sectionTypeInfo[section->type].name.c_str(), where, section->name.c_str(), sectionTypeInfo[section->type].name.c_str(), where,
section->org + section->size, endaddr(section->type) + 1); section->org + section->size, endaddr(section->type) + 1);
// Otherwise there is overlap with another section // Otherwise there is overlap with another section
else else
errx("Unable to place \"%s\" (%s section) %s: section overlaps with \"%s\"", errx("Unable to place \"%s\" (%s section) %s: section overlaps with \"%s\"",
section->name->c_str(), sectionTypeInfo[section->type].name.c_str(), where, section->name.c_str(), sectionTypeInfo[section->type].name.c_str(), where,
out_OverlappingSection(section)->name->c_str()); out_OverlappingSection(section)->name.c_str());
} }
#define BANK_CONSTRAINED (1 << 2) #define BANK_CONSTRAINED (1 << 2)
@@ -396,7 +396,7 @@ void assign_AssignSections(void)
constraints >= 0; constraints--) { constraints >= 0; constraints--) {
for (struct Section *section : unassignedSections[constraints]) { for (struct Section *section : unassignedSections[constraints]) {
fprintf(stderr, "%c \"%s\"", nbSections == 0 ? ';': ',', fprintf(stderr, "%c \"%s\"", nbSections == 0 ? ';': ',',
section->name->c_str()); section->name.c_str());
nbSections++; nbSections++;
if (nbSections == 10) if (nbSections == 10)
goto max_out; goto max_out;

View File

@@ -219,7 +219,7 @@ static void readSymbol(FILE *file, struct Symbol *symbol, char const *fileName,
* @param fileName The filename to report in errors * @param fileName The filename to report in errors
* @param i The number of the patch to report in errors * @param i The number of the patch to report in errors
*/ */
static void readPatch(FILE *file, struct Patch *patch, char const *fileName, char const *sectName, static void readPatch(FILE *file, struct Patch *patch, char const *fileName, std::string const &sectName,
uint32_t i, std::vector<struct FileStackNode> const &fileNodes) uint32_t i, std::vector<struct FileStackNode> const &fileNodes)
{ {
uint32_t nodeID, rpnSize; uint32_t nodeID, rpnSize;
@@ -227,34 +227,34 @@ static void readPatch(FILE *file, struct Patch *patch, char const *fileName, cha
tryReadlong(nodeID, file, tryReadlong(nodeID, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s node ID: %s", "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s node ID: %s",
fileName, sectName, i); fileName, sectName.c_str(), i);
patch->src = &fileNodes[nodeID]; patch->src = &fileNodes[nodeID];
tryReadlong(patch->lineNo, file, tryReadlong(patch->lineNo, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s line number: %s", "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s line number: %s",
fileName, sectName, i); fileName, sectName.c_str(), i);
tryReadlong(patch->offset, file, tryReadlong(patch->offset, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s offset: %s", "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s offset: %s",
fileName, sectName, i); fileName, sectName.c_str(), i);
tryReadlong(patch->pcSectionID, file, tryReadlong(patch->pcSectionID, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s", "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
fileName, sectName, i); fileName, sectName.c_str(), i);
tryReadlong(patch->pcOffset, file, tryReadlong(patch->pcOffset, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s", "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s PC offset: %s",
fileName, sectName, i); fileName, sectName.c_str(), i);
tryGetc(enum PatchType, type, file, tryGetc(enum PatchType, type, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s type: %s", "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s type: %s",
fileName, sectName, i); fileName, sectName.c_str(), i);
patch->type = type; patch->type = type;
tryReadlong(rpnSize, file, tryReadlong(rpnSize, file,
"%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s RPN size: %s", "%s: Unable to read \"%s\"'s patch #%" PRIu32 "'s RPN size: %s",
fileName, sectName, i); fileName, sectName.c_str(), i);
patch->rpnExpression.resize(rpnSize); patch->rpnExpression.resize(rpnSize);
size_t nbElementsRead = fread(&patch->rpnExpression[0], 1, rpnSize, file); size_t nbElementsRead = fread(&patch->rpnExpression[0], 1, rpnSize, file);
if (nbElementsRead != rpnSize) if (nbElementsRead != rpnSize)
errx("%s: Cannot read \"%s\"'s patch #%" PRIu32 "'s RPN expression: %s", errx("%s: Cannot read \"%s\"'s patch #%" PRIu32 "'s RPN expression: %s",
fileName, sectName, i, fileName, sectName.c_str(), i,
feof(file) ? "Unexpected end of file" : strerror(errno)); feof(file) ? "Unexpected end of file" : strerror(errno));
} }
@@ -279,15 +279,18 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
{ {
int32_t tmp; int32_t tmp;
uint8_t byte; uint8_t byte;
std::string *str;
tryReadstring(section->name, file, "%s: Cannot read section name: %s", fileName); tryReadstring(str, file, "%s: Cannot read section name: %s", fileName);
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s' size: %s", fileName, section->name->c_str()); section->name = *str;
delete str;
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s' size: %s", fileName, section->name.c_str());
if (tmp < 0 || tmp > UINT16_MAX) if (tmp < 0 || tmp > UINT16_MAX)
errx("\"%s\"'s section size (%" PRId32 ") is invalid", section->name->c_str(), tmp); errx("\"%s\"'s section size (%" PRId32 ") is invalid", section->name.c_str(), tmp);
section->size = tmp; section->size = tmp;
section->offset = 0; section->offset = 0;
tryGetc(uint8_t, byte, file, "%s: Cannot read \"%s\"'s type: %s", fileName, tryGetc(uint8_t, byte, file, "%s: Cannot read \"%s\"'s type: %s", fileName,
section->name->c_str()); section->name.c_str());
section->type = (enum SectionType)(byte & 0x3F); section->type = (enum SectionType)(byte & 0x3F);
if (byte >> 7) if (byte >> 7)
section->modifier = SECTION_UNION; section->modifier = SECTION_UNION;
@@ -295,27 +298,27 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
section->modifier = SECTION_FRAGMENT; section->modifier = SECTION_FRAGMENT;
else else
section->modifier = SECTION_NORMAL; section->modifier = SECTION_NORMAL;
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s org: %s", fileName, section->name->c_str()); tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s org: %s", fileName, section->name.c_str());
section->isAddressFixed = tmp >= 0; section->isAddressFixed = tmp >= 0;
if (tmp > UINT16_MAX) { if (tmp > UINT16_MAX) {
error(NULL, 0, "\"%s\"'s org is too large (%" PRId32 ")", section->name->c_str(), tmp); error(NULL, 0, "\"%s\"'s org is too large (%" PRId32 ")", section->name.c_str(), tmp);
tmp = UINT16_MAX; tmp = UINT16_MAX;
} }
section->org = tmp; section->org = tmp;
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s bank: %s", fileName, section->name->c_str()); tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s bank: %s", fileName, section->name.c_str());
section->isBankFixed = tmp >= 0; section->isBankFixed = tmp >= 0;
section->bank = tmp; section->bank = tmp;
tryGetc(uint8_t, byte, file, "%s: Cannot read \"%s\"'s alignment: %s", fileName, tryGetc(uint8_t, byte, file, "%s: Cannot read \"%s\"'s alignment: %s", fileName,
section->name->c_str()); section->name.c_str());
if (byte > 16) if (byte > 16)
byte = 16; byte = 16;
section->isAlignFixed = byte != 0; section->isAlignFixed = byte != 0;
section->alignMask = (1 << byte) - 1; section->alignMask = (1 << byte) - 1;
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s alignment offset: %s", fileName, tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s alignment offset: %s", fileName,
section->name->c_str()); section->name.c_str());
if (tmp > UINT16_MAX) { if (tmp > UINT16_MAX) {
error(NULL, 0, "\"%s\"'s alignment offset is too large (%" PRId32 ")", error(NULL, 0, "\"%s\"'s alignment offset is too large (%" PRId32 ")",
section->name->c_str(), tmp); section->name.c_str(), tmp);
tmp = UINT16_MAX; tmp = UINT16_MAX;
} }
section->alignOfs = tmp; section->alignOfs = tmp;
@@ -323,11 +326,11 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
if (sect_HasData(section->type)) { if (sect_HasData(section->type)) {
section->data = new(std::nothrow) std::vector<uint8_t>(section->size); section->data = new(std::nothrow) std::vector<uint8_t>(section->size);
if (!section->data) if (!section->data)
err("%s: Unable to read \"%s\"'s data", fileName, section->name->c_str()); err("%s: Unable to read \"%s\"'s data", fileName, section->name.c_str());
if (section->size) { if (section->size) {
if (size_t nbRead = fread(&(*section->data)[0], 1, section->size, file); if (size_t nbRead = fread(&(*section->data)[0], 1, section->size, file);
nbRead != section->size) nbRead != section->size)
errx("%s: Cannot read \"%s\"'s data: %s", fileName, section->name->c_str(), errx("%s: Cannot read \"%s\"'s data: %s", fileName, section->name.c_str(),
feof(file) ? "Unexpected end of file" : strerror(errno)); feof(file) ? "Unexpected end of file" : strerror(errno));
} }
@@ -335,12 +338,12 @@ static void readSection(FILE *file, struct Section *section, char const *fileNam
tryReadlong(nbPatches, file, tryReadlong(nbPatches, file,
"%s: Cannot read \"%s\"'s number of patches: %s", fileName, "%s: Cannot read \"%s\"'s number of patches: %s", fileName,
section->name->c_str()); section->name.c_str());
section->patches.resize(nbPatches); section->patches.resize(nbPatches);
for (uint32_t i = 0; i < nbPatches; i++) for (uint32_t i = 0; i < nbPatches; i++)
readPatch(file, &section->patches[i], fileName, section->name->c_str(), readPatch(file, &section->patches[i], fileName, section->name, i,
i, fileNodes); fileNodes);
} else { } else {
section->data = NULL; // `mergeSections()` expects to be able to always read the ptr section->data = NULL; // `mergeSections()` expects to be able to always read the ptr
} }
@@ -387,7 +390,7 @@ static void readAssertion(FILE *file, struct Assertion *assert, char const *file
static struct Section *getMainSection(struct Section *section) static struct Section *getMainSection(struct Section *section)
{ {
if (section->modifier != SECTION_NORMAL) if (section->modifier != SECTION_NORMAL)
section = sect_GetSection(*section->name); section = sect_GetSection(section->name);
return section; return section;
} }
@@ -569,7 +572,6 @@ static void freeSection(struct Section *section)
do { do {
struct Section *next = section->nextu; struct Section *next = section->nextu;
delete section->name;
if (sect_HasData(section->type)) if (sect_HasData(section->type))
delete section->data; delete section->data;
delete section->symbols; delete section->symbols;

View File

@@ -71,7 +71,7 @@ void out_AddSection(struct Section const *section)
if (minNbBanks > maxNbBanks[section->type]) if (minNbBanks > maxNbBanks[section->type])
errx("Section \"%s\" has an invalid bank range (%" PRIu32 " > %" PRIu32 ")", errx("Section \"%s\" has an invalid bank range (%" PRIu32 " > %" PRIu32 ")",
section->name->c_str(), section->bank, section->name.c_str(), section->bank,
maxNbBanks[section->type] - 1); maxNbBanks[section->type] - 1);
for (uint32_t i = sections[section->type].size(); i < minNbBanks; i++) for (uint32_t i = sections[section->type].size(); i < minNbBanks; i++)
@@ -408,10 +408,10 @@ static void writeMapBank(struct SortedSections const &sectList, enum SectionType
fprintf(mapFile, "\tSECTION: $%04" PRIx16 "-$%04x ($%04" PRIx16 fprintf(mapFile, "\tSECTION: $%04" PRIx16 "-$%04x ($%04" PRIx16
" byte%s) [\"%s\"]\n", " byte%s) [\"%s\"]\n",
sect->org, prevEndAddr - 1, sect->size, sect->size == 1 ? "" : "s", sect->org, prevEndAddr - 1, sect->size, sect->size == 1 ? "" : "s",
sect->name->c_str()); sect->name.c_str());
else else
fprintf(mapFile, "\tSECTION: $%04" PRIx16 " (0 bytes) [\"%s\"]\n", fprintf(mapFile, "\tSECTION: $%04" PRIx16 " (0 bytes) [\"%s\"]\n",
sect->org, sect->name->c_str()); sect->org, sect->name.c_str());
if (!noSymInMap) { if (!noSymInMap) {
// Also print symbols in the following "pieces" // Also print symbols in the following "pieces"

View File

@@ -451,7 +451,7 @@ void patch_CheckAssertions(std::deque<struct Assertion> &assertions)
*/ */
static void applyFilePatches(struct Section *section, struct Section *dataSection) static void applyFilePatches(struct Section *section, struct Section *dataSection)
{ {
verbosePrint("Patching section \"%s\"...\n", section->name->c_str()); verbosePrint("Patching section \"%s\"...\n", section->name.c_str());
for (struct Patch &patch : section->patches) { for (struct Patch &patch : section->patches) {
int32_t value = computeRPNExpr(&patch, *section->fileSymbols); int32_t value = computeRPNExpr(&patch, *section->fileSymbols);
uint16_t offset = patch.offset + section->offset; uint16_t offset = patch.offset + section->offset;

View File

@@ -264,7 +264,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
assert(strlen(token) != 0); // This should be impossible, tokens are non-empty assert(strlen(token) != 0); // This should be impossible, tokens are non-empty
// The following is required for fragment offsets to be reliably predicted // The following is required for fragment offsets to be reliably predicted
for (struct FileSection &entry : fileSections) { for (struct FileSection &entry : fileSections) {
if (!strcmp(token, entry.section->name->c_str())) if (!strcmp(token, entry.section->name.c_str()))
fatal(where, lineNo, "Area \"%s\" already defined earlier", fatal(where, lineNo, "Area \"%s\" already defined earlier",
token); token);
} }
@@ -280,7 +280,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
if (tmp > UINT16_MAX) if (tmp > UINT16_MAX)
fatal(where, lineNo, "Area \"%s\" is larger than the GB address space!?", fatal(where, lineNo, "Area \"%s\" is larger than the GB address space!?",
curSection->name->c_str()); curSection->name.c_str());
curSection->size = tmp; curSection->size = tmp;
expectToken("flags", 'A'); expectToken("flags", 'A');
@@ -293,16 +293,12 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
curSection->isBankFixed = curSection->isAddressFixed; curSection->isBankFixed = curSection->isAddressFixed;
curSection->modifier = curSection->isAddressFixed || (tmp & (1 << AREA_TYPE)) curSection->modifier = curSection->isAddressFixed || (tmp & (1 << AREA_TYPE))
? SECTION_NORMAL : SECTION_FRAGMENT; ? SECTION_NORMAL : SECTION_FRAGMENT;
curSection->name = new(std::nothrow) std::string();
if (!curSection->name)
fatal(where, lineNo, "Failed to alloc new area's name: %s",
strerror(errno));
// If the section is absolute, its name might not be unique; thus, mangle the name // If the section is absolute, its name might not be unique; thus, mangle the name
if (curSection->modifier == SECTION_NORMAL) { if (curSection->modifier == SECTION_NORMAL) {
curSection->name->append(where->name()); curSection->name.append(where->name());
curSection->name->append(" "); curSection->name.append(" ");
} }
curSection->name->append(sectionName); curSection->name.append(sectionName);
expectToken("addr", 'A'); expectToken("addr", 'A');
@@ -470,7 +466,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
if (section->isAddressFixed) { if (section->isAddressFixed) {
if (addr < section->org) if (addr < section->org)
fatal(where, lineNo, "'T' line reports address $%04" PRIx16 " in \"%s\", which starts at $%04" PRIx16, fatal(where, lineNo, "'T' line reports address $%04" PRIx16 " in \"%s\", which starts at $%04" PRIx16,
addr, section->name->c_str(), section->org); addr, section->name.c_str(), section->org);
addr -= section->org; addr -= section->org;
} }
// Lines are emitted that violate this check but contain no "payload"; // Lines are emitted that violate this check but contain no "payload";
@@ -484,7 +480,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
section->data = new(std::nothrow) std::vector<uint8_t>(section->size); section->data = new(std::nothrow) std::vector<uint8_t>(section->size);
if (!section->data) if (!section->data)
fatal(where, lineNo, "Failed to alloc data for \"%s\": %s", fatal(where, lineNo, "Failed to alloc data for \"%s\": %s",
section->name->c_str(), strerror(errno)); section->name.c_str(), strerror(errno));
} }
} }
@@ -613,8 +609,8 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
// the section gets moved for any reason. // the section gets moved for any reason.
if (fileSections[idx].section->isAddressFixed) if (fileSections[idx].section->isAddressFixed)
baseValue -= fileSections[idx].section->org; baseValue -= fileSections[idx].section->org;
std::string const *name = fileSections[idx].section->name; std::string const &name = fileSections[idx].section->name;
struct Section const *other = sect_GetSection(*name); struct Section const *other = sect_GetSection(name);
// Unlike with `s_<AREA>`, referencing an area in this way // Unlike with `s_<AREA>`, referencing an area in this way
// wants the beginning of this fragment, so we must add the // wants the beginning of this fragment, so we must add the
@@ -626,10 +622,10 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
// current size. // current size.
if (other) if (other)
baseValue += other->size; baseValue += other->size;
patch.rpnExpression.resize(1 + name->length() + 1); patch.rpnExpression.resize(1 + name.length() + 1);
patch.rpnExpression[0] = RPN_STARTOF_SECT; patch.rpnExpression[0] = RPN_STARTOF_SECT;
// The cast is fine, it's just different signedness // The cast is fine, it's just different signedness
memcpy((char *)&patch.rpnExpression[1], name->c_str(), name->length() + 1); memcpy((char *)&patch.rpnExpression[1], name.c_str(), name.length() + 1);
} }
patch.rpnExpression.push_back(RPN_CONST); patch.rpnExpression.push_back(RPN_CONST);
@@ -645,7 +641,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
if (flags & 1 << RELOC_EXPR16) { if (flags & 1 << RELOC_EXPR16) {
if (*writeIndex + (offset - writtenOfs) > section->size) if (*writeIndex + (offset - writtenOfs) > section->size)
fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%u > %" PRIu16 ")", fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%u > %" PRIu16 ")",
section->name->c_str(), *writeIndex + (offset - writtenOfs), section->size); section->name.c_str(), *writeIndex + (offset - writtenOfs), section->size);
// Copy all bytes up to those (plus the byte that we'll overwrite) // Copy all bytes up to those (plus the byte that we'll overwrite)
memcpy(&(*section->data)[*writeIndex], &data[writtenOfs], offset - writtenOfs + 1); memcpy(&(*section->data)[*writeIndex], &data[writtenOfs], offset - writtenOfs + 1);
*writeIndex += offset - writtenOfs + 1; *writeIndex += offset - writtenOfs + 1;
@@ -694,7 +690,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
assert(nbBytes > writtenOfs); assert(nbBytes > writtenOfs);
if (*writeIndex + (nbBytes - writtenOfs) > section->size) if (*writeIndex + (nbBytes - writtenOfs) > section->size)
fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%zu > %" PRIu16 ")", fatal(where, lineNo, "'T' line writes past \"%s\"'s end (%zu > %" PRIu16 ")",
section->name->c_str(), *writeIndex + (nbBytes - writtenOfs), section->size); section->name.c_str(), *writeIndex + (nbBytes - writtenOfs), section->size);
memcpy(&(*section->data)[*writeIndex], &data[writtenOfs], nbBytes - writtenOfs); memcpy(&(*section->data)[*writeIndex], &data[writtenOfs], nbBytes - writtenOfs);
*writeIndex += nbBytes - writtenOfs; *writeIndex += nbBytes - writtenOfs;
} }
@@ -727,7 +723,7 @@ void sdobj_ReadFile(struct FileStackNode const *where, FILE *file, std::vector<s
// RAM sections can have a size, but don't get any data (they shouldn't have any) // RAM sections can have a size, but don't get any data (they shouldn't have any)
if (entry.writeIndex != section->size && entry.writeIndex != 0) if (entry.writeIndex != section->size && entry.writeIndex != 0)
fatal(where, lineNo, "\"%s\" was not fully written (%" PRIu16 " < %" PRIu16 ")", fatal(where, lineNo, "\"%s\" was not fully written (%" PRIu16 " < %" PRIu16 ")",
section->name->c_str(), entry.writeIndex, section->size); section->name.c_str(), entry.writeIndex, section->size);
// This must be done last, so that `->data` is not NULL anymore // This must be done last, so that `->data` is not NULL anymore
sect_AddSection(section); sect_AddSection(section);

View File

@@ -27,12 +27,12 @@ static void checkSectUnionCompat(struct Section *target, struct Section *other)
if (target->isAddressFixed) { if (target->isAddressFixed) {
if (target->org != other->org) if (target->org != other->org)
errx("Section \"%s\" is defined with conflicting addresses $%04" errx("Section \"%s\" is defined with conflicting addresses $%04"
PRIx16 " and $%04" PRIx16, other->name->c_str(), target->org, PRIx16 " and $%04" PRIx16, other->name.c_str(), target->org,
other->org); other->org);
} else if (target->isAlignFixed) { } else if (target->isAlignFixed) {
if ((other->org - target->alignOfs) & target->alignMask) if ((other->org - target->alignOfs) & target->alignMask)
errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %" errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %"
PRIu16 ") and address $%04" PRIx16, other->name->c_str(), PRIu16 ") and address $%04" PRIx16, other->name.c_str(),
target->alignMask + 1, target->alignOfs, other->org); target->alignMask + 1, target->alignOfs, other->org);
} }
target->isAddressFixed = true; target->isAddressFixed = true;
@@ -43,14 +43,14 @@ static void checkSectUnionCompat(struct Section *target, struct Section *other)
if ((target->org - other->alignOfs) & other->alignMask) if ((target->org - other->alignOfs) & other->alignMask)
errx("Section \"%s\" is defined with conflicting address $%04" errx("Section \"%s\" is defined with conflicting address $%04"
PRIx16 " and %d-byte alignment (offset %" PRIu16 ")", PRIx16 " and %d-byte alignment (offset %" PRIu16 ")",
other->name->c_str(), target->org, other->alignMask + 1, other->name.c_str(), target->org, other->alignMask + 1,
other->alignOfs); other->alignOfs);
} else if (target->isAlignFixed } else if (target->isAlignFixed
&& (other->alignMask & target->alignOfs) && (other->alignMask & target->alignOfs)
!= (target->alignMask & other->alignOfs)) { != (target->alignMask & other->alignOfs)) {
errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %" errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %"
PRIu16 ") and %d-byte alignment (offset %" PRIu16 ")", PRIu16 ") and %d-byte alignment (offset %" PRIu16 ")",
other->name->c_str(), target->alignMask + 1, target->alignOfs, other->name.c_str(), target->alignMask + 1, target->alignOfs,
other->alignMask + 1, other->alignOfs); other->alignMask + 1, other->alignOfs);
} else if (!target->isAlignFixed || (other->alignMask > target->alignMask)) { } else if (!target->isAlignFixed || (other->alignMask > target->alignMask)) {
target->isAlignFixed = true; target->isAlignFixed = true;
@@ -67,13 +67,13 @@ static void checkFragmentCompat(struct Section *target, struct Section *other)
if (target->isAddressFixed) { if (target->isAddressFixed) {
if (target->org != org) if (target->org != org)
errx("Section \"%s\" is defined with conflicting addresses $%04" errx("Section \"%s\" is defined with conflicting addresses $%04"
PRIx16 " and $%04" PRIx16, other->name->c_str(), target->org, PRIx16 " and $%04" PRIx16, other->name.c_str(), target->org,
other->org); other->org);
} else if (target->isAlignFixed) { } else if (target->isAlignFixed) {
if ((org - target->alignOfs) & target->alignMask) if ((org - target->alignOfs) & target->alignMask)
errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %" errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %"
PRIu16 ") and address $%04" PRIx16, other->name->c_str(), PRIu16 ") and address $%04" PRIx16, other->name.c_str(),
target->alignMask + 1, target->alignOfs, other->org); target->alignMask + 1, target->alignOfs, other->org);
} }
target->isAddressFixed = true; target->isAddressFixed = true;
@@ -89,14 +89,14 @@ static void checkFragmentCompat(struct Section *target, struct Section *other)
if ((target->org - ofs) & other->alignMask) if ((target->org - ofs) & other->alignMask)
errx("Section \"%s\" is defined with conflicting address $%04" errx("Section \"%s\" is defined with conflicting address $%04"
PRIx16 " and %d-byte alignment (offset %" PRIu16 ")", PRIx16 " and %d-byte alignment (offset %" PRIu16 ")",
other->name->c_str(), target->org, other->alignMask + 1, other->name.c_str(), target->org, other->alignMask + 1,
other->alignOfs); other->alignOfs);
} else if (target->isAlignFixed } else if (target->isAlignFixed
&& (other->alignMask & target->alignOfs) != (target->alignMask & ofs)) { && (other->alignMask & target->alignOfs) != (target->alignMask & ofs)) {
errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %" errx("Section \"%s\" is defined with conflicting %d-byte alignment (offset %"
PRIu16 ") and %d-byte alignment (offset %" PRIu16 ")", PRIu16 ") and %d-byte alignment (offset %" PRIu16 ")",
other->name->c_str(), target->alignMask + 1, target->alignOfs, other->name.c_str(), target->alignMask + 1, target->alignOfs,
other->alignMask + 1, other->alignOfs); other->alignMask + 1, other->alignOfs);
} else if (!target->isAlignFixed || (other->alignMask > target->alignMask)) { } else if (!target->isAlignFixed || (other->alignMask > target->alignMask)) {
@@ -113,7 +113,7 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
if (target->type != other->type) if (target->type != other->type)
errx("Section \"%s\" is defined with conflicting types %s and %s", errx("Section \"%s\" is defined with conflicting types %s and %s",
other->name->c_str(), sectionTypeInfo[target->type].name.c_str(), other->name.c_str(), sectionTypeInfo[target->type].name.c_str(),
sectionTypeInfo[other->type].name.c_str()); sectionTypeInfo[other->type].name.c_str());
if (other->isBankFixed) { if (other->isBankFixed) {
@@ -122,7 +122,7 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
target->bank = other->bank; target->bank = other->bank;
} else if (target->bank != other->bank) { } else if (target->bank != other->bank) {
errx("Section \"%s\" is defined with conflicting banks %" PRIu32 " and %" errx("Section \"%s\" is defined with conflicting banks %" PRIu32 " and %"
PRIu32, other->name->c_str(), target->bank, other->bank); PRIu32, other->name.c_str(), target->bank, other->bank);
} }
} }
@@ -171,20 +171,20 @@ static void mergeSections(struct Section *target, struct Section *other, enum Se
void sect_AddSection(struct Section *section) void sect_AddSection(struct Section *section)
{ {
// Check if the section already exists // Check if the section already exists
if (struct Section *other = sect_GetSection(*section->name); other) { if (struct Section *other = sect_GetSection(section->name); other) {
if (section->modifier != other->modifier) if (section->modifier != other->modifier)
errx("Section \"%s\" defined as %s and %s", section->name->c_str(), errx("Section \"%s\" defined as %s and %s", section->name.c_str(),
sectionModNames[section->modifier], sectionModNames[other->modifier]); sectionModNames[section->modifier], sectionModNames[other->modifier]);
else if (section->modifier == SECTION_NORMAL) else if (section->modifier == SECTION_NORMAL)
errx("Section name \"%s\" is already in use", section->name->c_str()); errx("Section name \"%s\" is already in use", section->name.c_str());
else else
mergeSections(other, section, section->modifier); mergeSections(other, section, section->modifier);
} else if (section->modifier == SECTION_UNION && sect_HasData(section->type)) { } else if (section->modifier == SECTION_UNION && sect_HasData(section->type)) {
errx("Section \"%s\" is of type %s, which cannot be unionized", errx("Section \"%s\" is of type %s, which cannot be unionized",
section->name->c_str(), sectionTypeInfo[section->type].name.c_str()); section->name.c_str(), sectionTypeInfo[section->type].name.c_str());
} else { } else {
// If not, add it // If not, add it
sections[*section->name] = section; sections[section->name] = section;
} }
} }
@@ -203,27 +203,27 @@ static void doSanityChecks(struct Section *section)
{ {
// Sanity check the section's type // Sanity check the section's type
if (section->type < 0 || section->type >= SECTTYPE_INVALID) { if (section->type < 0 || section->type >= SECTTYPE_INVALID) {
error(NULL, 0, "Section \"%s\" has an invalid type", section->name->c_str()); error(NULL, 0, "Section \"%s\" has an invalid type", section->name.c_str());
return; return;
} }
if (is32kMode && section->type == SECTTYPE_ROMX) { if (is32kMode && section->type == SECTTYPE_ROMX) {
if (section->isBankFixed && section->bank != 1) if (section->isBankFixed && section->bank != 1)
error(NULL, 0, "%s: ROMX sections must be in bank 1 (if any) with option -t", error(NULL, 0, "%s: ROMX sections must be in bank 1 (if any) with option -t",
section->name->c_str()); section->name.c_str());
else else
section->type = SECTTYPE_ROM0; section->type = SECTTYPE_ROM0;
} }
if (isWRA0Mode && section->type == SECTTYPE_WRAMX) { if (isWRA0Mode && section->type == SECTTYPE_WRAMX) {
if (section->isBankFixed && section->bank != 1) if (section->isBankFixed && section->bank != 1)
error(NULL, 0, "%s: WRAMX sections must be in bank 1 with options -w or -d", error(NULL, 0, "%s: WRAMX sections must be in bank 1 with options -w or -d",
section->name->c_str()); section->name.c_str());
else else
section->type = SECTTYPE_WRAM0; section->type = SECTTYPE_WRAM0;
} }
if (isDmgMode && section->type == SECTTYPE_VRAM && section->bank == 1) if (isDmgMode && section->type == SECTTYPE_VRAM && section->bank == 1)
error(NULL, 0, "%s: VRAM bank 1 can't be used with option -d", error(NULL, 0, "%s: VRAM bank 1 can't be used with option -d",
section->name->c_str()); section->name.c_str());
// Check if alignment is reasonable, this is important to avoid UB // Check if alignment is reasonable, this is important to avoid UB
// An alignment of zero is equivalent to no alignment, basically // An alignment of zero is equivalent to no alignment, basically
@@ -233,7 +233,7 @@ static void doSanityChecks(struct Section *section)
// Too large an alignment may not be satisfiable // Too large an alignment may not be satisfiable
if (section->isAlignFixed && (section->alignMask & sectionTypeInfo[section->type].startAddr)) if (section->isAlignFixed && (section->alignMask & sectionTypeInfo[section->type].startAddr))
error(NULL, 0, "%s: %s sections cannot be aligned to $%04x bytes", error(NULL, 0, "%s: %s sections cannot be aligned to $%04x bytes",
section->name->c_str(), sectionTypeInfo[section->type].name.c_str(), section->name.c_str(), sectionTypeInfo[section->type].name.c_str(),
section->alignMask + 1); section->alignMask + 1);
uint32_t minbank = sectionTypeInfo[section->type].firstBank, maxbank = sectionTypeInfo[section->type].lastBank; uint32_t minbank = sectionTypeInfo[section->type].firstBank, maxbank = sectionTypeInfo[section->type].lastBank;
@@ -242,13 +242,13 @@ static void doSanityChecks(struct Section *section)
error(NULL, 0, minbank == maxbank error(NULL, 0, minbank == maxbank
? "Cannot place section \"%s\" in bank %" PRIu32 ", it must be %" PRIu32 ? "Cannot place section \"%s\" in bank %" PRIu32 ", it must be %" PRIu32
: "Cannot place section \"%s\" in bank %" PRIu32 ", it must be between %" PRIu32 " and %" PRIu32, : "Cannot place section \"%s\" in bank %" PRIu32 ", it must be between %" PRIu32 " and %" PRIu32,
section->name->c_str(), section->bank, minbank, maxbank); section->name.c_str(), section->bank, minbank, maxbank);
// Check if section has a chance to be placed // Check if section has a chance to be placed
if (section->size > sectionTypeInfo[section->type].size) if (section->size > sectionTypeInfo[section->type].size)
error(NULL, 0, "Section \"%s\" is bigger than the max size for that type: $%" error(NULL, 0, "Section \"%s\" is bigger than the max size for that type: $%"
PRIx16 " > $%" PRIx16, PRIx16 " > $%" PRIx16,
section->name->c_str(), section->size, sectionTypeInfo[section->type].size); section->name.c_str(), section->size, sectionTypeInfo[section->type].size);
// Translate loose constraints to strong ones when they're equivalent // Translate loose constraints to strong ones when they're equivalent
@@ -262,7 +262,7 @@ static void doSanityChecks(struct Section *section)
if (section->isAlignFixed) { if (section->isAlignFixed) {
if ((section->org & section->alignMask) != section->alignOfs) if ((section->org & section->alignMask) != section->alignOfs)
error(NULL, 0, "Section \"%s\"'s fixed address doesn't match its alignment", error(NULL, 0, "Section \"%s\"'s fixed address doesn't match its alignment",
section->name->c_str()); section->name.c_str());
section->isAlignFixed = false; section->isAlignFixed = false;
} }
@@ -270,12 +270,12 @@ static void doSanityChecks(struct Section *section)
if (section->org < sectionTypeInfo[section->type].startAddr if (section->org < sectionTypeInfo[section->type].startAddr
|| section->org > endaddr(section->type)) || section->org > endaddr(section->type))
error(NULL, 0, "Section \"%s\"'s fixed address $%04" PRIx16 " is outside of range [$%04" error(NULL, 0, "Section \"%s\"'s fixed address $%04" PRIx16 " is outside of range [$%04"
PRIx16 "; $%04" PRIx16 "]", section->name->c_str(), section->org, PRIx16 "; $%04" PRIx16 "]", section->name.c_str(), section->org,
sectionTypeInfo[section->type].startAddr, endaddr(section->type)); sectionTypeInfo[section->type].startAddr, endaddr(section->type));
if (section->org + section->size > endaddr(section->type) + 1) if (section->org + section->size > endaddr(section->type) + 1)
error(NULL, 0, "Section \"%s\"'s end address $%04x is greater than last address $%04x", error(NULL, 0, "Section \"%s\"'s end address $%04x is greater than last address $%04x",
section->name->c_str(), section->org + section->size, section->name.c_str(), section->org + section->size,
endaddr(section->type) + 1); endaddr(section->type) + 1);
} }
} }