Merge pull request #2843 from colinleroy/warn-about-fill-bytes

Emit warnings for wasted bytes when aligning
This commit is contained in:
Bob Andrews
2025-11-02 18:14:27 +01:00
committed by GitHub
13 changed files with 72 additions and 7 deletions

View File

@@ -133,6 +133,7 @@ Long options:
--target sys Set the target system --target sys Set the target system
--verbose Increase verbosity --verbose Increase verbosity
--version Print the assembler version --version Print the assembler version
--warn-align-waste Print bytes "wasted" for alignment
--warnings-as-errors Treat warnings as errors --warnings-as-errors Treat warnings as errors
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
</verb></tscreen> </verb></tscreen>
@@ -407,6 +408,13 @@ Here is a description of all the command line options:
something lower. something lower.
<label id="option--warn-align-waste">
<tag><tt>--warn-align-waste</tt></tag>
Warnings will be generated when alignment requirements cause emission of
fill bytes.
<label id="option--warnings-as-errors"> <label id="option--warnings-as-errors">
<tag><tt>--warnings-as-errors</tt></tag> <tag><tt>--warnings-as-errors</tt></tag>
@@ -2372,7 +2380,8 @@ Here's a list of all control commands and a description, what they do:
calculated in advance by the assembler. You are therefore required to calculated in advance by the assembler. You are therefore required to
specify a matching alignment for the segment in the linker config. The specify a matching alignment for the segment in the linker config. The
linker will output a warning if the alignment of the segment is less than linker will output a warning if the alignment of the segment is less than
what is necessary to have a correct alignment in the object file. what is necessary to have a correct alignment in the object file. It will
also emit a warning to inform about the number of bytes wasted for alignment.
Example: Example:

View File

@@ -119,6 +119,8 @@ Long options:
--target sys Set the target system --target sys Set the target system
--version Print the version number --version Print the version number
--verbose Verbose mode --verbose Verbose mode
--warn-align-waste Print bytes "wasted" for alignment
--warnings-as-errors Treat warnings as errors
--zeropage-label name Define and export a ZEROPAGE segment label --zeropage-label name Define and export a ZEROPAGE segment label
--zeropage-name seg Set the name of the ZEROPAGE segment --zeropage-name seg Set the name of the ZEROPAGE segment
--------------------------------------------------------------------------- ---------------------------------------------------------------------------

View File

@@ -92,6 +92,8 @@ Long options:
--start-group Start a library group --start-group Start a library group
--target sys Set the target system --target sys Set the target system
--version Print the linker version --version Print the linker version
--warn-align-waste Print bytes "wasted" for alignment
--warnings-as-errors Treat warnings as errors
--------------------------------------------------------------------------- ---------------------------------------------------------------------------
</verb></tscreen> </verb></tscreen>
@@ -349,6 +351,13 @@ Here is a description of all of the command-line options:
directories given by environment variables, and in a built-in default directory. directories given by environment variables, and in a built-in default directory.
<label id="option--warn-align-waste">
<tag><tt>--warn-align-waste</tt></tag>
Warnings will be generated when alignment requirements cause emission of
fill bytes.
<label id="option--warnings-as-errors"> <label id="option--warnings-as-errors">
<tag><tt>--warnings-as-errors</tt></tag> <tag><tt>--warnings-as-errors</tt></tag>

View File

@@ -68,6 +68,7 @@ unsigned char LargeAlignment = 0; /* Don't warn about large alignments */
unsigned char RelaxChecks = 0; /* Relax a few assembler checks */ unsigned char RelaxChecks = 0; /* Relax a few assembler checks */
unsigned char StringEscapes = 0; /* Allow C-style escapes in strings */ unsigned char StringEscapes = 0; /* Allow C-style escapes in strings */
unsigned char LongJsrJmpRts = 0; /* Allow JSR/JMP/RTS as alias for JSL/JML/RTL */ unsigned char LongJsrJmpRts = 0; /* Allow JSR/JMP/RTS as alias for JSL/JML/RTL */
unsigned char WarnAlignWaste = 0; /* Warn about "wasted" bytes when aligning */
unsigned char WarningsAsErrors = 0; /* Error if any warnings */ unsigned char WarningsAsErrors = 0; /* Error if any warnings */
unsigned char SegList = 0; /* Show segments in listing */ unsigned char SegList = 0; /* Show segments in listing */
unsigned char ExpandMacros = 0; /* Expand macros in listing */ unsigned char ExpandMacros = 0; /* Expand macros in listing */

View File

@@ -70,6 +70,7 @@ extern unsigned char LargeAlignment; /* Don't warn about large alignments
extern unsigned char RelaxChecks; /* Relax a few assembler checks */ extern unsigned char RelaxChecks; /* Relax a few assembler checks */
extern unsigned char StringEscapes; /* Allow C-style escapes in strings */ extern unsigned char StringEscapes; /* Allow C-style escapes in strings */
extern unsigned char LongJsrJmpRts; /* Allow JSR/JMP/RTS as alias for JSL/JML/RTL */ extern unsigned char LongJsrJmpRts; /* Allow JSR/JMP/RTS as alias for JSL/JML/RTL */
extern unsigned char WarnAlignWaste; /* Warn about "wasted" bytes when aligning */
extern unsigned char WarningsAsErrors; /* Error if any warnings */ extern unsigned char WarningsAsErrors; /* Error if any warnings */
extern unsigned char SegList; /* Show segments in listing */ extern unsigned char SegList; /* Show segments in listing */
extern unsigned char ExpandMacros; /* Expand macros in listing */ extern unsigned char ExpandMacros; /* Expand macros in listing */

View File

@@ -138,6 +138,7 @@ static void Usage (void)
" --target sys\t\t\tSet the target system\n" " --target sys\t\t\tSet the target system\n"
" --verbose\t\t\tIncrease verbosity\n" " --verbose\t\t\tIncrease verbosity\n"
" --version\t\t\tPrint the assembler version\n" " --version\t\t\tPrint the assembler version\n"
" --warn-align-waste\t\tPrint bytes \"wasted\" for alignment\n"
" --warnings-as-errors\t\tTreat warnings as errors\n", " --warnings-as-errors\t\tTreat warnings as errors\n",
ProgName); ProgName);
} }
@@ -750,6 +751,15 @@ static void OptSeglist (const char* Opt attribute ((unused)),
static void OptWarnAlignWaste (const char* Opt attribute ((unused)),
const char* Arg attribute ((unused)))
/* Warn about bytes "wasted" for alignment */
{
WarnAlignWaste = 1;
}
static void OptWarningsAsErrors (const char* Opt attribute ((unused)), static void OptWarningsAsErrors (const char* Opt attribute ((unused)),
const char* Arg attribute ((unused))) const char* Arg attribute ((unused)))
/* Generate an error if any warnings occur */ /* Generate an error if any warnings occur */
@@ -1088,6 +1098,7 @@ int main (int argc, char* argv [])
{ "--target", 1, OptTarget }, { "--target", 1, OptTarget },
{ "--verbose", 0, OptVerbose }, { "--verbose", 0, OptVerbose },
{ "--version", 0, OptVersion }, { "--version", 0, OptVersion },
{ "--warn-align-waste", 0, OptWarnAlignWaste },
{ "--warnings-as-errors", 0, OptWarningsAsErrors }, { "--warnings-as-errors", 0, OptWarningsAsErrors },
}; };

View File

@@ -313,7 +313,9 @@ void SegAlign (unsigned long Alignment, int FillVal)
/* Calculate the number of fill bytes */ /* Calculate the number of fill bytes */
Count = AlignCount (ActiveSeg->PC, Alignment); Count = AlignCount (ActiveSeg->PC, Alignment);
if (WarnAlignWaste && Count != 0) {
Warning(0, "Wasting %lu bytes for alignment", Count);
}
} }

View File

@@ -897,9 +897,10 @@ static void Usage (void)
" --target sys\t\t\tSet the target system\n" " --target sys\t\t\tSet the target system\n"
" --version\t\t\tPrint the version number\n" " --version\t\t\tPrint the version number\n"
" --verbose\t\t\tVerbose mode\n" " --verbose\t\t\tVerbose mode\n"
" --warn-align-waste\t\tPrint bytes \"wasted\" for alignment\n"
" --warnings-as-errors\t\tTreat warnings as errors\n"
" --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n" " --zeropage-label name\t\tDefine and export a ZEROPAGE segment label\n"
" --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n" " --zeropage-name seg\t\tSet the name of the ZEROPAGE segment\n",
" --warnings-as-errors\t\tTreat warnings as errors\n",
ProgName); ProgName);
} }
@@ -1436,6 +1437,16 @@ static void OptZeropageName (const char* Opt attribute ((unused)), const char* A
static void OptWarnAlignWaste (const char* Opt attribute ((unused)),
const char* Arg attribute ((unused)))
/* Handle the --warn-align-waste option */
{
CmdAddArg (&CA65, "--warn-align-waste");
CmdAddArg (&LD65, "--warn-align-waste");
}
static void OptWarningsAsErrors (const char* Opt attribute ((unused)), static void OptWarningsAsErrors (const char* Opt attribute ((unused)),
const char* Arg attribute ((unused))) const char* Arg attribute ((unused)))
/* Handle the --warnings-as-errors option */ /* Handle the --warnings-as-errors option */
@@ -1506,6 +1517,7 @@ int main (int argc, char* argv [])
{ "--version", 0, OptVersion }, { "--version", 0, OptVersion },
{ "--zeropage-label", 1, OptZeropageLabel }, { "--zeropage-label", 1, OptZeropageLabel },
{ "--zeropage-name", 1, OptZeropageName }, { "--zeropage-name", 1, OptZeropageName },
{ "--warn-align-waste", 0, OptWarnAlignWaste },
{ "--warnings-as-errors", 0, OptWarningsAsErrors }, { "--warnings-as-errors", 0, OptWarningsAsErrors },
}; };

View File

@@ -2035,8 +2035,8 @@ unsigned CfgProcess (void)
if (M->FillLevel == 0 && NewAddr > Addr) { if (M->FillLevel == 0 && NewAddr > Addr) {
PWarning (GetSourcePos (S->LI), PWarning (GetSourcePos (S->LI),
"The first segment in memory area `%s' " "The first segment in memory area `%s' "
"needs fill bytes for alignment.", "needs %lu fill bytes for alignment.",
GetString (M->Name)); GetString (M->Name), NewAddr - Addr);
} }
/* Use the aligned address */ /* Use the aligned address */

View File

@@ -55,6 +55,7 @@ unsigned long StartAddr = 0x200; /* Start address */
unsigned char VerboseMap = 0; /* Verbose map file */ unsigned char VerboseMap = 0; /* Verbose map file */
unsigned char AllowMultDef = 0; /* Allow multiple definitions */ unsigned char AllowMultDef = 0; /* Allow multiple definitions */
unsigned char LargeAlignment = 0; /* Don't warn about large alignments */ unsigned char LargeAlignment = 0; /* Don't warn about large alignments */
unsigned char WarnAlignWaste = 0; /* Warn about "wasted" bytes when aligning */
unsigned char WarningsAsErrors = 0; /* Error if any warnings */ unsigned char WarningsAsErrors = 0; /* Error if any warnings */
const char* MapFileName = 0; /* Name of the map file */ const char* MapFileName = 0; /* Name of the map file */

View File

@@ -55,6 +55,7 @@ extern unsigned long StartAddr; /* Start address */
extern unsigned char VerboseMap; /* Verbose map file */ extern unsigned char VerboseMap; /* Verbose map file */
extern unsigned char AllowMultDef; /* Allow multiple definitions */ extern unsigned char AllowMultDef; /* Allow multiple definitions */
extern unsigned char LargeAlignment; /* Don't warn about large alignments */ extern unsigned char LargeAlignment; /* Don't warn about large alignments */
extern unsigned char WarnAlignWaste; /* Warn about "wasted" bytes when aligning */
extern unsigned char WarningsAsErrors; /* Error if any warnings */ extern unsigned char WarningsAsErrors; /* Error if any warnings */
extern const char* MapFileName; /* Name of the map file */ extern const char* MapFileName; /* Name of the map file */

View File

@@ -179,7 +179,9 @@ static void Usage (void)
" --start-addr addr\t\tSet the default start address\n" " --start-addr addr\t\tSet the default start address\n"
" --start-group\t\t\tStart a library group\n" " --start-group\t\t\tStart a library group\n"
" --target sys\t\t\tSet the target system\n" " --target sys\t\t\tSet the target system\n"
" --version\t\t\tPrint the linker version\n", " --version\t\t\tPrint the linker version\n"
" --warn-align-waste\t\tPrint bytes \"wasted\" for alignment\n"
" --warnings-as-errors\t\tTreat warnings as errors\n",
ProgName); ProgName);
} }
@@ -608,6 +610,15 @@ static void OptVersion (const char* Opt attribute ((unused)),
static void OptWarnAlignWaste (const char* Opt attribute ((unused)),
const char* Arg attribute ((unused)))
/* Warn about bytes "wasted" for alignment */
{
WarnAlignWaste = 1;
}
static void OptWarningsAsErrors (const char* Opt attribute ((unused)), static void OptWarningsAsErrors (const char* Opt attribute ((unused)),
const char* Arg attribute ((unused))) const char* Arg attribute ((unused)))
/* Generate an error if any warnings occur */ /* Generate an error if any warnings occur */
@@ -691,6 +702,7 @@ static void ParseCommandLine (void)
{ "--start-group", 0, CmdlOptStartGroup }, { "--start-group", 0, CmdlOptStartGroup },
{ "--target", 1, CmdlOptTarget }, { "--target", 1, CmdlOptTarget },
{ "--version", 0, OptVersion }, { "--version", 0, OptVersion },
{ "--warn-align-waste", 0, OptWarnAlignWaste },
{ "--warnings-as-errors", 0, OptWarningsAsErrors }, { "--warnings-as-errors", 0, OptWarningsAsErrors },
}; };

View File

@@ -236,6 +236,10 @@ Section* ReadSection (FILE* F, ObjData* O)
GetString (Name), Alignment, GetObjFileName (O)); GetString (Name), Alignment, GetObjFileName (O));
} }
S->Alignment = Alignment; S->Alignment = Alignment;
if (WarnAlignWaste && Sec->Fill != 0) {
Warning("%s: Wasting %lu bytes for `%s' alignment",
GetObjFileName (O), Sec->Fill, GetString (Name));
}
} }
/* Start reading fragments from the file and insert them into the section . */ /* Start reading fragments from the file and insert them into the section . */