From e4cbf773f69913a36519e71f7ad159913c382ce7 Mon Sep 17 00:00:00 2001 From: Ben10do Date: Sun, 19 Feb 2017 22:35:32 +0000 Subject: [PATCH] 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. --- include/asm/output.h | 2 ++ include/link/mylink.h | 2 ++ src/asm/output.c | 30 +++++++++++++++++++++--------- src/link/object.c | 36 +++++++++++++++++++++++++++--------- 4 files changed, 52 insertions(+), 18 deletions(-) diff --git a/include/asm/output.h b/include/asm/output.h index 2afbccc5..fab83e36 100644 --- a/include/asm/output.h +++ b/include/asm/output.h @@ -10,6 +10,7 @@ struct Section { ULONG nPC; ULONG nOrg; ULONG nBank; + ULONG nAlign; struct Section *pNext; struct Patch *pPatches; struct Charmap *charmap; @@ -20,6 +21,7 @@ void out_PrepPass2(void); void out_SetFileName(char *s); void out_NewSection(char *pzName, ULONG secttype); 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_AbsByteGroup(char *s, int length); void out_RelByte(struct Expression * expr); diff --git a/include/link/mylink.h b/include/link/mylink.h index 707d5fbf..9ce6e4e4 100644 --- a/include/link/mylink.h +++ b/include/link/mylink.h @@ -63,8 +63,10 @@ enum eSectionType { struct sSection { SLONG nBank; SLONG nOrg; + SLONG nAlign; BBOOL oAssigned; + char *pzName; SLONG nByteSize; enum eSectionType Type; UBYTE *pData; diff --git a/src/asm/output.c b/src/asm/output.c index d4147a1b..601dff76 100644 --- a/src/asm/output.c +++ b/src/asm/output.c @@ -201,17 +201,18 @@ writepatch(struct Patch * pPatch, FILE * f) void 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); fputc(pSect->nType, f); fputlong(pSect->nOrg, f); //RGB1 addition - fputlong(pSect->nBank, f); + fputlong(pSect->nBank, f); //RGB1 addition + + fputlong(pSect->nAlign, f); // RGB3 addition - if ((pSect->nType == SECT_ROM0) + if ((pSect->nType == SECT_ROM0) || (pSect->nType == SECT_ROMX)) { struct Patch *pPatch; @@ -490,7 +491,7 @@ out_WriteObject(void) struct PatchSymbol *pSym; struct Section *pSect; - fwrite("RGB2", 1, 4, f); + fwrite("RGB3", 1, 4, f); fputlong(countsymbols(), 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 */ 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; @@ -557,7 +558,8 @@ out_FindSection(char *pzName, ULONG secttype, SLONG org, SLONG bank) if (strcmp(pzName, pSect->pzName) == 0) { if (secttype == pSect->nType && ((ULONG) org) == pSect->nOrg - && ((ULONG) bank) == pSect->nBank) { + && ((ULONG) bank) == pSect->nBank + && ((ULONG) alignment == pSect->nAlign)) { return (pSect); } else fatalerror @@ -574,6 +576,7 @@ out_FindSection(char *pzName, ULONG secttype, SLONG org, SLONG bank) pSect->nPC = 0; pSect->nOrg = org; pSect->nBank = bank; + pSect->nAlign = alignment; pSect->pNext = NULL; pSect->pPatches = NULL; pSect->charmap = NULL; @@ -610,7 +613,7 @@ out_SetCurrentSection(struct Section * pSect) void 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 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)); } /* diff --git a/src/link/object.c b/src/link/object.c index 47539041..5d0e7195 100644 --- a/src/link/object.c +++ b/src/link/object.c @@ -18,6 +18,11 @@ struct sSection *pLibSections = NULL; UBYTE dummymem; BBOOL oReadLib = 0; +enum ObjectFileContents { + CONTAINS_SECTION_NAME = 1 << 0, + CONTAINS_SECTION_ALIGNMENT = 1 << 1 +}; + /* * The usual byte order stuff * @@ -143,10 +148,12 @@ obj_ReadRGB0Section(FILE * f) pSection = AllocSection(); + pSection->pzName = ""; pSection->nByteSize = readlong(f); pSection->Type = (enum eSectionType) fgetc(f); pSection->nOrg = -1; pSection->nBank = -1; + pSection->nAlign = 1; /* does the user want the -s mode? */ @@ -277,21 +284,28 @@ obj_ReadRGB0(FILE * pObjfile) */ struct sSection * -obj_ReadRGB1Section(FILE * f) +obj_ReadRGBSection(FILE * f, enum ObjectFileContents contents) { struct sSection *pSection; pSection = AllocSection(); + if (contents & CONTAINS_SECTION_NAME) { + readasciiz(&pSection->pzName, f); + } else { + pSection->pzName = ""; + } + pSection->nByteSize = readlong(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->nBank = readlong(f); + + if (contents & CONTAINS_SECTION_ALIGNMENT) { + pSection->nAlign = readlong(f); + } else { + pSection->nAlign = 1; + } /* does the user want the -s mode? */ @@ -356,7 +370,7 @@ obj_ReadRGB1Section(FILE * f) } void -obj_ReadRGB1(FILE * pObjfile) +obj_ReadRGB(FILE * pObjfile, enum ObjectFileContents contents) { struct sSection *pFirstSection; SLONG nNumberOfSymbols, nNumberOfSections, i; @@ -383,7 +397,7 @@ obj_ReadRGB1(FILE * pObjfile) while (nNumberOfSections--) { struct sSection *pNewSection; - pNewSection = obj_ReadRGB1Section(pObjfile); + pNewSection = obj_ReadRGBSection(pObjfile, contents); pNewSection->nNumberOfSymbols = nNumberOfSymbols; if (pFirstSection == NULL) pFirstSection = pNewSection; @@ -430,7 +444,11 @@ obj_ReadOpenFile(FILE * pObjfile, char *tzObjectfile) case '1': case '2': //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; default: errx(1, "'%s' is an unsupported version", tzObjectfile);