shithub: rgbds

Download patch

ref: e4cbf773f69913a36519e71f7ad159913c382ce7
parent: b07c04cd74cb97c9dd2d1c083cfac54c8e22f718
author: Ben10do <[email protected]>
date: Sun Feb 19 17:35:32 EST 2017

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.

--- a/include/asm/output.h
+++ b/include/asm/output.h
@@ -10,6 +10,7 @@
 	ULONG nPC;
 	ULONG nOrg;
 	ULONG nBank;
+	ULONG nAlign;
 	struct Section *pNext;
 	struct Patch *pPatches;
 	struct Charmap *charmap;
@@ -20,6 +21,7 @@
 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);
--- a/include/link/mylink.h
+++ b/include/link/mylink.h
@@ -63,8 +63,10 @@
 struct sSection {
 	SLONG nBank;
 	SLONG nOrg;
+	SLONG nAlign;
 	BBOOL oAssigned;
 
+	char *pzName;
 	SLONG nByteSize;
 	enum eSectionType Type;
 	UBYTE *pData;
--- a/src/asm/output.c
+++ b/src/asm/output.c
@@ -201,17 +201,18 @@
 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 @@
 		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 @@
  * 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 @@
 		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 @@
 			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 @@
 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 @@
 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));
 }
 
 /*
--- a/src/link/object.c
+++ b/src/link/object.c
@@ -18,6 +18,11 @@
 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 @@
 
 	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 @@
  */
 
 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 @@
 }
 
 void 
-obj_ReadRGB1(FILE * pObjfile)
+obj_ReadRGB(FILE * pObjfile, enum ObjectFileContents contents)
 {
 	struct sSection *pFirstSection;
 	SLONG nNumberOfSymbols, nNumberOfSections, i;
@@ -383,7 +397,7 @@
 	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 @@
 		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);