mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-22 19:22:05 +00:00
Implement unionized sections in RGBLINK
This commit is contained in:
@@ -189,7 +189,7 @@ static void writesection(struct Section *pSect, FILE *f)
|
||||
|
||||
fputlong(pSect->size, f);
|
||||
|
||||
fputc(pSect->nType, f);
|
||||
fputc(pSect->nType | pSect->isUnion << 7, f);
|
||||
|
||||
fputlong(pSect->nOrg, f);
|
||||
fputlong(pSect->nBank, f);
|
||||
|
||||
@@ -245,6 +245,7 @@ static void readSection(FILE *file, struct Section *section,
|
||||
char const *fileName)
|
||||
{
|
||||
int32_t tmp;
|
||||
uint8_t type;
|
||||
|
||||
tryReadstr(section->name, file, "%s: Cannot read section name: %s",
|
||||
fileName);
|
||||
@@ -254,8 +255,10 @@ static void readSection(FILE *file, struct Section *section,
|
||||
errx(1, "\"%s\"'s section size (%d) is invalid", section->name,
|
||||
tmp);
|
||||
section->size = tmp;
|
||||
tryGetc(section->type, file, "%s: Cannot read \"%s\"'s type: %s",
|
||||
tryGetc(type, file, "%s: Cannot read \"%s\"'s type: %s",
|
||||
fileName, section->name);
|
||||
section->type = type & 0x7F;
|
||||
section->isUnion = type >> 7;
|
||||
tryReadlong(tmp, file, "%s: Cannot read \"%s\"'s org: %s",
|
||||
fileName, section->name);
|
||||
section->isAddressFixed = tmp >= 0;
|
||||
@@ -358,6 +361,14 @@ static void readAssertion(FILE *file, struct Assertion *assert,
|
||||
fileName);
|
||||
}
|
||||
|
||||
static inline struct Section *getMainSection(struct Section *section)
|
||||
{
|
||||
if (section->isUnion)
|
||||
section = sect_GetSection(section->name);
|
||||
|
||||
return section;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads an object file of any supported format
|
||||
* @param fileName The filename to report for errors
|
||||
@@ -448,6 +459,7 @@ void obj_ReadFile(char const *fileName)
|
||||
|
||||
if (!section)
|
||||
err(1, "%s: Couldn't create new section", fileName);
|
||||
section->nextu = NULL;
|
||||
readSection(file, section, fileName);
|
||||
section->fileSymbols = fileSymbols;
|
||||
|
||||
@@ -472,9 +484,10 @@ void obj_ReadFile(char const *fileName)
|
||||
if (sectionID == -1) {
|
||||
fileSymbols[i]->section = NULL;
|
||||
} else {
|
||||
fileSymbols[i]->section = fileSections[sectionID];
|
||||
/* Give the section a pointer to the symbol as well */
|
||||
linkSymToSect(fileSymbols[i], fileSections[sectionID]);
|
||||
fileSymbols[i]->section =
|
||||
getMainSection(fileSections[sectionID]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -117,6 +117,24 @@ static uint32_t getRPNByte(uint8_t const **expression, int32_t *size,
|
||||
return *(*expression)++;
|
||||
}
|
||||
|
||||
static struct Symbol const *getSymbol(struct Symbol ** const symbolList,
|
||||
uint32_t index, char const *fileName)
|
||||
{
|
||||
struct Symbol const *symbol = symbolList[index];
|
||||
|
||||
/* If the symbol is defined elsewhere... */
|
||||
if (symbol->type == SYMTYPE_IMPORT) {
|
||||
struct Symbol const *symbolDefinition =
|
||||
sym_GetSymbol(symbol->name);
|
||||
if (!symbolDefinition)
|
||||
errx(1, "%s: Unknown symbol \"%s\"", fileName,
|
||||
symbol->name);
|
||||
symbol = symbolDefinition;
|
||||
}
|
||||
|
||||
return symbol;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute a patch's value from its RPN string.
|
||||
* @param patch The patch to compute the value of
|
||||
@@ -238,19 +256,8 @@ static int32_t computeRPNExpr(struct Patch const *patch,
|
||||
value |= getRPNByte(&expression, &size,
|
||||
patch->fileName) << shift;
|
||||
|
||||
symbol = section->fileSymbols[value];
|
||||
|
||||
/* If the symbol is defined elsewhere... */
|
||||
if (symbol->type == SYMTYPE_IMPORT) {
|
||||
struct Symbol const *symbolDefinition =
|
||||
sym_GetSymbol(symbol->name);
|
||||
if (!symbolDefinition)
|
||||
errx(1, "%s: Unknown symbol \"%s\"",
|
||||
patch->fileName, symbol->name);
|
||||
symbol = symbolDefinition;
|
||||
}
|
||||
|
||||
value = symbol->section->bank;
|
||||
value = getSymbol(section->fileSymbols, value,
|
||||
patch->fileName)->section->bank;
|
||||
break;
|
||||
|
||||
case RPN_BANK_SECT:
|
||||
@@ -305,17 +312,8 @@ static int32_t computeRPNExpr(struct Patch const *patch,
|
||||
value |= getRPNByte(&expression, &size,
|
||||
patch->fileName) << shift;
|
||||
|
||||
symbol = section->fileSymbols[value];
|
||||
|
||||
/* If the symbol is defined elsewhere... */
|
||||
if (symbol->type == SYMTYPE_IMPORT) {
|
||||
struct Symbol const *symbolDefinition =
|
||||
sym_GetSymbol(symbol->name);
|
||||
if (!symbolDefinition)
|
||||
errx(1, "%s: Unknown symbol \"%s\"",
|
||||
patch->fileName, symbol->name);
|
||||
symbol = symbolDefinition;
|
||||
}
|
||||
symbol = getSymbol(section->fileSymbols, value,
|
||||
patch->fileName);
|
||||
|
||||
if (!strcmp(symbol->name, "@")) {
|
||||
value = section->org + patch->offset;
|
||||
@@ -387,10 +385,8 @@ void patch_CheckAssertions(struct Assertion *assert)
|
||||
* @param section The section to patch
|
||||
* @param arg Ignored callback arg
|
||||
*/
|
||||
static void applyPatches(struct Section *section, void *arg)
|
||||
static void applyFilePatches(struct Section *section)
|
||||
{
|
||||
(void)arg;
|
||||
|
||||
if (!sect_HasData(section->type))
|
||||
return;
|
||||
|
||||
@@ -435,6 +431,22 @@ static void applyPatches(struct Section *section, void *arg)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies all of a section's patches, iterating over "components" of
|
||||
* unionized sections
|
||||
* @param section The section to patch
|
||||
* @param arg Ignored callback arg
|
||||
*/
|
||||
static void applyPatches(struct Section *section, void *arg)
|
||||
{
|
||||
(void)arg;
|
||||
|
||||
do {
|
||||
applyFilePatches(section);
|
||||
section = section->nextu;
|
||||
} while (section);
|
||||
}
|
||||
|
||||
void patch_ApplyPatches(void)
|
||||
{
|
||||
initRPNStack();
|
||||
|
||||
@@ -36,17 +36,76 @@ void sect_ForEach(void (*callback)(struct Section *, void *), void *arg)
|
||||
hash_ForEach(sections, forEach, &callbackArg);
|
||||
}
|
||||
|
||||
static void mergeSections(struct Section *target, struct Section *other)
|
||||
{
|
||||
if (other->isAddressFixed) {
|
||||
if (target->isAddressFixed) {
|
||||
if (target->org != other->org)
|
||||
errx(1, "Section \"%s\" is defined with conflicting addresses $%x and $%x",
|
||||
other->name, target->org, other->org);
|
||||
} else if (target->isAlignFixed) {
|
||||
if (other->org & target->alignMask)
|
||||
errx(1, "Section \"%s\" is defined with conflicting %u-byte alignment and address $%x",
|
||||
other->name, target->alignMask + 1,
|
||||
other->org);
|
||||
}
|
||||
target->isAddressFixed = true;
|
||||
target->org = other->org;
|
||||
} else if (other->isAlignFixed) {
|
||||
if (target->isAddressFixed) {
|
||||
if (target->org & other->alignMask)
|
||||
errx(1, "Section \"%s\" is defined with conflicting address $%x and %u-byte alignment",
|
||||
other->name, target->org,
|
||||
other->alignMask + 1);
|
||||
} else if (!target->isAlignFixed
|
||||
|| other->alignMask > target->alignMask) {
|
||||
target->isAlignFixed = true;
|
||||
target->alignMask = other->alignMask;
|
||||
}
|
||||
}
|
||||
|
||||
if (other->isBankFixed) {
|
||||
if (!target->isBankFixed) {
|
||||
target->isBankFixed = true;
|
||||
target->bank = other->bank;
|
||||
} else if (target->bank != other->bank) {
|
||||
errx(1, "Section \"%s\" is defined with conflicting banks %u and %u",
|
||||
other->name, target->bank, other->bank);
|
||||
}
|
||||
}
|
||||
|
||||
if (other->size > target->size)
|
||||
target->size = other->size;
|
||||
|
||||
target->nextu = other;
|
||||
}
|
||||
|
||||
void sect_AddSection(struct Section *section)
|
||||
{
|
||||
/* Check if the section already exists */
|
||||
if (hash_GetElement(sections, section->name))
|
||||
errx(1, "Section name \"%s\" is already in use", section->name);
|
||||
struct Section *other = hash_GetElement(sections, section->name);
|
||||
|
||||
/* If not, add it */
|
||||
bool collided = hash_AddElement(sections, section->name, section);
|
||||
if (other) {
|
||||
if (other->isUnion && section->isUnion) {
|
||||
mergeSections(other, section);
|
||||
} else if (section->isUnion || other->isUnion) {
|
||||
errx(1, "Section \"%s\" defined as both unionized and not",
|
||||
section->name);
|
||||
} else {
|
||||
errx(1, "Section name \"%s\" is already in use",
|
||||
section->name);
|
||||
}
|
||||
} else if (section->isUnion && sect_HasData(section->type)) {
|
||||
errx(1, "Section \"%s\" is of type %s, which cannot be unionized",
|
||||
section->name, typeNames[section->type]);
|
||||
} else {
|
||||
/* If not, add it */
|
||||
bool collided = hash_AddElement(sections, section->name,
|
||||
section);
|
||||
|
||||
if (beVerbose && collided)
|
||||
warnx("Section hashmap collision occurred!");
|
||||
if (beVerbose && collided)
|
||||
warnx("Section hashmap collision occurred!");
|
||||
}
|
||||
}
|
||||
|
||||
struct Section *sect_GetSection(char const *name)
|
||||
|
||||
Reference in New Issue
Block a user