From 083f3ae26b13e2c15ac559a5be78febca38f06ed Mon Sep 17 00:00:00 2001 From: laubzega Date: Mon, 25 May 2020 23:43:28 -0700 Subject: [PATCH] Fix for #928. --- src/ld65/bin.c | 8 +++++++- src/ld65/config.c | 6 ++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/ld65/bin.c b/src/ld65/bin.c index 688622415..f2e741be5 100644 --- a/src/ld65/bin.c +++ b/src/ld65/bin.c @@ -195,7 +195,7 @@ static void BinWriteMem (BinDesc* D, MemoryArea* M) if (DoWrite || (M->Flags & MF_FILL) != 0) { /* Seek in "overwrite" segments */ if (S->Flags & SF_OVERWRITE) { - fseek (D->F, NewAddr - M->Start, SEEK_SET); + fseek (D->F, NewAddr - M->Start + M->FileOffs, SEEK_SET); } else { WriteMult (D->F, M->FillVal, NewAddr-Addr); PrintNumVal ("SF_OFFSET", NewAddr - Addr); @@ -226,6 +226,12 @@ static void BinWriteMem (BinDesc* D, MemoryArea* M) unsigned long P = ftell (D->F); SegWrite (D->Filename, D->F, S->Seg, BinWriteExpr, D); PrintNumVal ("Wrote", (unsigned long) (ftell (D->F) - P)); + /* If we have just written an OVERWRITE segement, move position to the + * end of file, so that subsequent segments are written in the correct + * place. */ + if (S->Flags & SF_OVERWRITE) { + fseek (D->F, 0, SEEK_END); + } } else if (M->Flags & MF_FILL) { WriteMult (D->F, S->Seg->FillVal, S->Seg->Size); PrintNumVal ("Filled", (unsigned long) S->Seg->Size); diff --git a/src/ld65/config.c b/src/ld65/config.c index 6606f6976..79c38a108 100644 --- a/src/ld65/config.c +++ b/src/ld65/config.c @@ -2107,10 +2107,12 @@ unsigned CfgProcess (void) Addr += S->Seg->Size; /* If this segment will go out to the file, or its place - ** in the file will be filled, then increase the file size. + ** in the file will be filled, then increase the file size, + ** unless it's an OVERWRITE segment. */ if (S->Load == M && - ((S->Flags & SF_BSS) == 0 || (M->Flags & MF_FILL) != 0)) { + ((S->Flags & SF_BSS) == 0 || (M->Flags & MF_FILL) != 0) + && (S->Flags & SF_OVERWRITE) == 0) { M->F->Size += Addr - StartAddr; } }