Cc65: Apple2: LOWCODE segment overflow not reported

Created on 24 Oct 2020  路  5Comments  路  Source: cc65/cc65

I have been using a 2018 version of CC65 for a while, and decided to upgrade to latest for next release of 8bit-Unity. I noticed something strange on the apple2-hgr config:

In older version, the linker would report any overflow of the LOWCODE segment (which could be fixed by adjusting pragmas). The latest version does not seem to detect such overflows, and thus produces binaries that do not work (the portion overflowing into DHR memory is wiped out by the program).

question

Most helpful comment

Indeed the behavior changed with https://github.com/cc65/cc65/commit/80b09ba9fc42f65b84f91ea2d170325abe84fac1. However, when used as intended the new variant still shows the desired behavior.

The HGR segment is marked as optional (see https://github.com/cc65/cc65/blob/master/cfg/apple2-hgr.cfg#L26). So if you don't add anything to that segment the linker presumes that it's okay to have the LOWCODE segment "flow" into the the HGR segment.

The HGR segment is supposed to hold the initial graphics data (usually a splash screen or alike). That makes sense because otherwise the program file contains just $2000 zeros. See https://github.com/cc65/cc65/blob/master/targettest/apple2/werner.s for an example how to do so.

If you, however, don't want the HGR segment to hold graphics data you need to add at least one dummy byte to it to make the linker understand that there's something you want to have placed exactly at address $2000. Then the linker will give you an error when LOWCODE grows over $1FFFF. You can do so from C with e.g.:

#pragma rodata-name (push, "HGR")
const char hgr = 0;
#pragma rodata-name (pop)

All 5 comments

Indeed the behavior changed with https://github.com/cc65/cc65/commit/80b09ba9fc42f65b84f91ea2d170325abe84fac1. However, when used as intended the new variant still shows the desired behavior.

The HGR segment is marked as optional (see https://github.com/cc65/cc65/blob/master/cfg/apple2-hgr.cfg#L26). So if you don't add anything to that segment the linker presumes that it's okay to have the LOWCODE segment "flow" into the the HGR segment.

The HGR segment is supposed to hold the initial graphics data (usually a splash screen or alike). That makes sense because otherwise the program file contains just $2000 zeros. See https://github.com/cc65/cc65/blob/master/targettest/apple2/werner.s for an example how to do so.

If you, however, don't want the HGR segment to hold graphics data you need to add at least one dummy byte to it to make the linker understand that there's something you want to have placed exactly at address $2000. Then the linker will give you an error when LOWCODE grows over $1FFFF. You can do so from C with e.g.:

#pragma rodata-name (push, "HGR")
const char hgr = 0;
#pragma rodata-name (pop)

Maybe add this example, how to trigger LOWCODE overflow, to ld65.sgml?

Thanks for the quick reply Oliver, it makes sense.

It would be useful to add this info in the related doc section:
https://cc65.github.io/doc/apple2.html#toc4.3

Reconsidering the situation, I decided to make the HGR segment obligatory (https://github.com/cc65/cc65/commit/2fcd8b934ac4cbc577a02a83cc4c83f9f90d7561). This way you'll always get a linker error of one sort or another instead of the "silent misbehavior" you experienced.

Right, then users can specify #pragma HGR to fill up that section with code if they want!

Was this page helpful?
0 / 5 - 0 ratings