Compiler fails when sketch is bigger than ~6.6KB

I am working on a simple Java virtual machine for my Arduino Mega 2560. My Arduino sketch has a somewhat large (~800 byte) block of Java bytecode stored in PROGMEM. The rest of my code is pretty standard and does not use any fancy libraries. When I try to compile my sketch, I get the following error:

/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/
avr/bin/../lib/gcc/avr/4.3.2/../../../../avr/lib/avr6/crtm2560.o:
In function `__vector_default':
(.vectors+0x68): relocation truncated to fit: R_AVR_13_PCREL against
symbol `__vector_26' defined in .text.__vector_26 section in
core.a(HardwareSerial.cpp.o)

If I remove ~20 bytes from the block of Java bytecode, the code compiles without any problem. Another way to make compilation succeed is to remove a Serial.println statement. Strangely, the sketch can also be compiled if I choose the board "Arduino UNO" instead of "Arduino Mega".

Here is a pastebin of the code: Failing Arduino Mega 2560 Sketch - Pastebin.com

I am using Arduino IDE 1.0.5 on Mac OS Mavericks.

Does anyone know what is going on here? I really want to continue working on the Java virtual machine! Thanks.

(I also submitted this problem to www.reddit.com/r/arduino, but with no replies.)

In version 1.5.2 It works for both Uno and Mega:

Binary sketch size: 6,826 bytes (of a 258,048 byte maximum) - 2% used

It bums out in 1.0.5 like you say,
I'll try a few things and see how I go.

One sub-optimal option is, instead of the PROGMEM macro you can use:

const byte JAVA_PROGRAM[] __attribute__((section(".fini7"))) = {

However to access the data you have to use:

pgm_read_byte_far();
memcpy_PF();

instead of the near address versions:

pgm_read_byte_near();
memcpy_P();

It seems the progmem data is pushing some system code to far into memory and might be unreachable.

Thanks for the help!

No worries, I can help with a solution too if accessing far memory slows things down too much ( extra cycle or two per byte ).

Ahh, yes. splitting up the PGM data allows compiling, I added in code to use it, so I know it was not removed by the compiler.

I could help with an automated class, however you could just modify your code to read from the appropriate bank of PGM data.