From 74f43d4e094d6ed6048d784a9275f0a7a26a7097 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Sun, 8 Sep 2019 23:40:16 +0200 Subject: [PATCH] Add a way to seek a SECTION by name without creating one --- include/asm/output.h | 1 + src/asm/output.c | 52 ++++++++++++++++++++++++++------------------ 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/include/asm/output.h b/include/asm/output.h index be396b16..46fffb3b 100644 --- a/include/asm/output.h +++ b/include/asm/output.h @@ -29,6 +29,7 @@ extern char *tzObjectname; void out_PrepPass2(void); void out_SetFileName(char *s); +struct Section *out_FindSectionByName(const char *pzName); void out_NewSection(char *pzName, uint32_t secttype); void out_NewAbsSection(char *pzName, uint32_t secttype, int32_t org, int32_t bank); diff --git a/src/asm/output.c b/src/asm/output.c index 21aa0ead..c319a153 100644 --- a/src/asm/output.c +++ b/src/asm/output.c @@ -599,48 +599,52 @@ void out_SetFileName(char *s) printf("Output filename %s\n", s); } +struct Section *out_FindSectionByName(const char *pzName) +{ + struct Section *pSect = pSectionList; + + while (pSect) { + if (strcmp(pzName, pSect->pzName) == 0) + return pSect; + + pSect = pSect->pNext; + } + + return NULL; +} + /* * Find a section by name and type. If it doesn't exist, create it */ struct Section *out_FindSection(char *pzName, uint32_t secttype, int32_t org, int32_t bank, int32_t alignment) { - struct Section *pSect, **ppSect; + struct Section *pSect = out_FindSectionByName(pzName); - ppSect = &pSectionList; - pSect = pSectionList; - - while (pSect) { - if (strcmp(pzName, pSect->pzName) == 0) { - if (secttype == pSect->nType - && ((uint32_t)org) == pSect->nOrg - && ((uint32_t)bank) == pSect->nBank - && ((uint32_t)alignment == pSect->nAlign)) { - return pSect; - } - - fatalerror("Section already exists but with a different type"); + if (pSect) { + if (secttype == pSect->nType + && ((uint32_t)org) == pSect->nOrg + && ((uint32_t)bank) == pSect->nBank + && ((uint32_t)alignment == pSect->nAlign)) { + return pSect; } - ppSect = &(pSect->pNext); - pSect = pSect->pNext; + fatalerror("Section already exists but with a different type"); } pSect = malloc(sizeof(struct Section)); - *ppSect = pSect; if (pSect == NULL) fatalerror("Not enough memory for section"); - pSect->pzName = malloc(strlen(pzName) + 1); + pSect->pzName = strdup(pzName); if (pSect->pzName == NULL) fatalerror("Not enough memory for sectionname"); - strcpy(pSect->pzName, pzName); pSect->nType = secttype; pSect->nPC = 0; pSect->nOrg = org; pSect->nBank = bank; pSect->nAlign = alignment; - pSect->pNext = NULL; + pSect->pNext = pSectionList; pSect->pPatches = NULL; /* It is only needed to allocate memory for ROM sections. */ @@ -655,7 +659,13 @@ struct Section *out_FindSection(char *pzName, uint32_t secttype, int32_t org, pSect->tData = NULL; } - return (pSect); + /* + * Add the new section to the list + * at the beginning because order doesn't matter + */ + pSectionList = pSect; + + return pSect; } /*