Add alignment of sections to objects

Aligned sections can now be created with out_NewAlignedSection(). This information is stored in created object files, and read by the linker.

The names of each section are also included in the object file, enabling potential improvements to error messages in the future.
This commit is contained in:
Ben10do
2017-02-19 22:35:32 +00:00
parent b07c04cd74
commit e4cbf773f6
4 changed files with 52 additions and 18 deletions

View File

@@ -10,6 +10,7 @@ struct Section {
ULONG nPC; ULONG nPC;
ULONG nOrg; ULONG nOrg;
ULONG nBank; ULONG nBank;
ULONG nAlign;
struct Section *pNext; struct Section *pNext;
struct Patch *pPatches; struct Patch *pPatches;
struct Charmap *charmap; struct Charmap *charmap;
@@ -20,6 +21,7 @@ void out_PrepPass2(void);
void out_SetFileName(char *s); void out_SetFileName(char *s);
void out_NewSection(char *pzName, ULONG secttype); void out_NewSection(char *pzName, ULONG secttype);
void out_NewAbsSection(char *pzName, ULONG secttype, SLONG org, SLONG bank); void out_NewAbsSection(char *pzName, ULONG secttype, SLONG org, SLONG bank);
void out_NewAlignedSection(char *pzName, ULONG secttype, SLONG alignment, SLONG bank);
void out_AbsByte(int b); void out_AbsByte(int b);
void out_AbsByteGroup(char *s, int length); void out_AbsByteGroup(char *s, int length);
void out_RelByte(struct Expression * expr); void out_RelByte(struct Expression * expr);

View File

@@ -63,8 +63,10 @@ enum eSectionType {
struct sSection { struct sSection {
SLONG nBank; SLONG nBank;
SLONG nOrg; SLONG nOrg;
SLONG nAlign;
BBOOL oAssigned; BBOOL oAssigned;
char *pzName;
SLONG nByteSize; SLONG nByteSize;
enum eSectionType Type; enum eSectionType Type;
UBYTE *pData; UBYTE *pData;

View File

@@ -201,8 +201,7 @@ writepatch(struct Patch * pPatch, FILE * f)
void void
writesection(struct Section * pSect, FILE * f) writesection(struct Section * pSect, FILE * f)
{ {
//printf("SECTION: %s, ID: %d\n", pSect->pzName, getsectid(pSect)); fputstring(pSect->pzName, f); // RGB3 addition
fputlong(pSect->nPC, f); fputlong(pSect->nPC, f);
fputc(pSect->nType, f); fputc(pSect->nType, f);
fputlong(pSect->nOrg, f); fputlong(pSect->nOrg, f);
@@ -211,6 +210,8 @@ writesection(struct Section * pSect, FILE * f)
fputlong(pSect->nBank, f); fputlong(pSect->nBank, f);
//RGB1 addition //RGB1 addition
fputlong(pSect->nAlign, f); // RGB3 addition
if ((pSect->nType == SECT_ROM0) if ((pSect->nType == SECT_ROM0)
|| (pSect->nType == SECT_ROMX)) { || (pSect->nType == SECT_ROMX)) {
struct Patch *pPatch; struct Patch *pPatch;
@@ -490,7 +491,7 @@ out_WriteObject(void)
struct PatchSymbol *pSym; struct PatchSymbol *pSym;
struct Section *pSect; struct Section *pSect;
fwrite("RGB2", 1, 4, f); fwrite("RGB3", 1, 4, f);
fputlong(countsymbols(), f); fputlong(countsymbols(), f);
fputlong(countsections(), f); fputlong(countsections(), f);
@@ -546,7 +547,7 @@ out_SetFileName(char *s)
* Find a section by name and type. If it doesn't exist, create it * Find a section by name and type. If it doesn't exist, create it
*/ */
struct Section * struct Section *
out_FindSection(char *pzName, ULONG secttype, SLONG org, SLONG bank) out_FindSection(char *pzName, ULONG secttype, SLONG org, SLONG bank, SLONG alignment)
{ {
struct Section *pSect, **ppSect; struct Section *pSect, **ppSect;
@@ -557,7 +558,8 @@ out_FindSection(char *pzName, ULONG secttype, SLONG org, SLONG bank)
if (strcmp(pzName, pSect->pzName) == 0) { if (strcmp(pzName, pSect->pzName) == 0) {
if (secttype == pSect->nType if (secttype == pSect->nType
&& ((ULONG) org) == pSect->nOrg && ((ULONG) org) == pSect->nOrg
&& ((ULONG) bank) == pSect->nBank) { && ((ULONG) bank) == pSect->nBank
&& ((ULONG) alignment == pSect->nAlign)) {
return (pSect); return (pSect);
} else } else
fatalerror fatalerror
@@ -574,6 +576,7 @@ out_FindSection(char *pzName, ULONG secttype, SLONG org, SLONG bank)
pSect->nPC = 0; pSect->nPC = 0;
pSect->nOrg = org; pSect->nOrg = org;
pSect->nBank = bank; pSect->nBank = bank;
pSect->nAlign = alignment;
pSect->pNext = NULL; pSect->pNext = NULL;
pSect->pPatches = NULL; pSect->pPatches = NULL;
pSect->charmap = NULL; pSect->charmap = NULL;
@@ -610,7 +613,7 @@ out_SetCurrentSection(struct Section * pSect)
void void
out_NewSection(char *pzName, ULONG secttype) out_NewSection(char *pzName, ULONG secttype)
{ {
out_SetCurrentSection(out_FindSection(pzName, secttype, -1, -1)); out_SetCurrentSection(out_FindSection(pzName, secttype, -1, -1, 1));
} }
/* /*
@@ -619,7 +622,16 @@ out_NewSection(char *pzName, ULONG secttype)
void void
out_NewAbsSection(char *pzName, ULONG secttype, SLONG org, SLONG bank) out_NewAbsSection(char *pzName, ULONG secttype, SLONG org, SLONG bank)
{ {
out_SetCurrentSection(out_FindSection(pzName, secttype, org, bank)); out_SetCurrentSection(out_FindSection(pzName, secttype, org, bank, 1));
}
/*
* Set the current section by name and type, using a given byte alignment
*/
void
out_NewAlignedSection(char *pzName, ULONG secttype, SLONG alignment, SLONG bank)
{
out_SetCurrentSection(out_FindSection(pzName, secttype, -1, bank, alignment));
} }
/* /*

View File

@@ -18,6 +18,11 @@ struct sSection *pLibSections = NULL;
UBYTE dummymem; UBYTE dummymem;
BBOOL oReadLib = 0; BBOOL oReadLib = 0;
enum ObjectFileContents {
CONTAINS_SECTION_NAME = 1 << 0,
CONTAINS_SECTION_ALIGNMENT = 1 << 1
};
/* /*
* The usual byte order stuff * The usual byte order stuff
* *
@@ -143,10 +148,12 @@ obj_ReadRGB0Section(FILE * f)
pSection = AllocSection(); pSection = AllocSection();
pSection->pzName = "";
pSection->nByteSize = readlong(f); pSection->nByteSize = readlong(f);
pSection->Type = (enum eSectionType) fgetc(f); pSection->Type = (enum eSectionType) fgetc(f);
pSection->nOrg = -1; pSection->nOrg = -1;
pSection->nBank = -1; pSection->nBank = -1;
pSection->nAlign = 1;
/* does the user want the -s mode? */ /* does the user want the -s mode? */
@@ -277,22 +284,29 @@ obj_ReadRGB0(FILE * pObjfile)
*/ */
struct sSection * struct sSection *
obj_ReadRGB1Section(FILE * f) obj_ReadRGBSection(FILE * f, enum ObjectFileContents contents)
{ {
struct sSection *pSection; struct sSection *pSection;
pSection = AllocSection(); pSection = AllocSection();
if (contents & CONTAINS_SECTION_NAME) {
readasciiz(&pSection->pzName, f);
} else {
pSection->pzName = "";
}
pSection->nByteSize = readlong(f); pSection->nByteSize = readlong(f);
pSection->Type = (enum eSectionType) fgetc(f); pSection->Type = (enum eSectionType) fgetc(f);
/*
* And because of THIS new feature I'll have to rewrite loads and
* loads of stuff... oh well it needed to be done anyway
*
*/
pSection->nOrg = readlong(f); pSection->nOrg = readlong(f);
pSection->nBank = readlong(f); pSection->nBank = readlong(f);
if (contents & CONTAINS_SECTION_ALIGNMENT) {
pSection->nAlign = readlong(f);
} else {
pSection->nAlign = 1;
}
/* does the user want the -s mode? */ /* does the user want the -s mode? */
if ((options & OPT_SMALL) && (pSection->Type == SECT_ROMX)) { if ((options & OPT_SMALL) && (pSection->Type == SECT_ROMX)) {
@@ -356,7 +370,7 @@ obj_ReadRGB1Section(FILE * f)
} }
void void
obj_ReadRGB1(FILE * pObjfile) obj_ReadRGB(FILE * pObjfile, enum ObjectFileContents contents)
{ {
struct sSection *pFirstSection; struct sSection *pFirstSection;
SLONG nNumberOfSymbols, nNumberOfSections, i; SLONG nNumberOfSymbols, nNumberOfSections, i;
@@ -383,7 +397,7 @@ obj_ReadRGB1(FILE * pObjfile)
while (nNumberOfSections--) { while (nNumberOfSections--) {
struct sSection *pNewSection; struct sSection *pNewSection;
pNewSection = obj_ReadRGB1Section(pObjfile); pNewSection = obj_ReadRGBSection(pObjfile, contents);
pNewSection->nNumberOfSymbols = nNumberOfSymbols; pNewSection->nNumberOfSymbols = nNumberOfSymbols;
if (pFirstSection == NULL) if (pFirstSection == NULL)
pFirstSection = pNewSection; pFirstSection = pNewSection;
@@ -430,7 +444,11 @@ obj_ReadOpenFile(FILE * pObjfile, char *tzObjectfile)
case '1': case '1':
case '2': case '2':
//V2 is really the same but the are new patch types //V2 is really the same but the are new patch types
obj_ReadRGB1(pObjfile); obj_ReadRGB(pObjfile, 0);
break;
case '3':
// V3 is very similiar, but contains section names and byte alignment
obj_ReadRGB(pObjfile, CONTAINS_SECTION_NAME | CONTAINS_SECTION_ALIGNMENT);
break; break;
default: default:
errx(1, "'%s' is an unsupported version", tzObjectfile); errx(1, "'%s' is an unsupported version", tzObjectfile);