Description
First, the megaTinyCore has been incredibly useful to me. This is an outstanding project.
For a recent project, my code has been compiling fine, but after adding a few lines in an ISR
if( timersEnabled == 0)
{
PITStop();
}
I get this error:
Linking everything together...
/Users/dalegrover/Library/Arduino15/packages/DxCore/tools/avr-gcc/7.3.0-atmel3.6.1-azduino7b1/bin/avr-gcc -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -Wl,--section-start=.text=0x0 -mmcu=attiny804 -o /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/piezoSwitchV1.ino.elf /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/sketch/piezoSwitchV1.ino.cpp.o /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/../../cores/2ba667bb2ca989f86fea08f356bbd3b4/core.a -L/Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9 -lm
/var/folders/wq/z410yvtd0bxfkg86p3tjt7v80000gn/T//ccUTPZsw.ltrans0.ltrans.o: In function `loop':
/Users/dalegrover/Documents/LittleLakeTechnology/PiezoSwitch/code/piezoSwitchV1/piezoSwitchV1.ino:777:(.text.startup+0x9be): relocation truncated to fit: R_AVR_13_PCREL against `no symbol'
collect2: error: ld returned 1 exit status
I am at 6217 bytes of program, 75% of the memory on this chip.
If I add a bunch of extra (copy & pasted) code to the same location, it compiles and links (6423 bytes, 78%) with no issues. If I then remove the original 3 lines that caused the problem, I get the error. So it does not appear to be up against a limit.
boards.txt seems to turn off -mrelax for the ATtiny804 (line 1280, if I'm correct), and indeed I do not see this in the compiler or linker arguments (attached below). If I do enable -mrelax (and it indeed appears in the compiler and linker arguments), I get the same error.
I have checked that I am compiling the libraries fresh.
ATtiny804
Arduino 2.3.4
OSX 15.3.2
megaTinyCore 2.6.10
Here is the output when it fails:
FQBN: megaTinyCore:megaavr:atxy4:chip=804,clock=5internal,millis=timera
Using board 'atxy4' from platform in folder: /Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10
Using core 'megatinycore' from platform in folder: /Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10
Detecting libraries used...
/Users/dalegrover/Library/Arduino15/packages/DxCore/tools/avr-gcc/7.3.0-atmel3.6.1-azduino7b1/bin/avr-g++ -c -g -Os -Wall -std=gnu++17 -fpermissive -Wno-sized-deallocation -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=attiny804 -DF_CPU=5000000L -DCLOCK_SOURCE=0 -DTWI_MORS -DMILLIS_USE_TIMERA0 -DCORE_ATTACH_ALL -DARDUINO=10607 -DARDUINO_AVR_ATtiny804 -DARDUINO_ARCH_MEGAAVR -DMEGATINYCORE="2.6.10" -DMEGATINYCORE_MAJOR=2UL -DMEGATINYCORE_MINOR=6UL -DMEGATINYCORE_PATCH=10UL -DMEGATINYCORE_RELEASED=1 -DARDUINO_attinyxy4 -I/Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10/cores/megatinycore/api/deprecated -I/Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10/cores/megatinycore -I/Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10/variants/txy4 /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/sketch/piezoSwitchV1.ino.cpp -o /dev/null
Generating function prototypes...
/Users/dalegrover/Library/Arduino15/packages/DxCore/tools/avr-gcc/7.3.0-atmel3.6.1-azduino7b1/bin/avr-g++ -c -g -Os -Wall -std=gnu++17 -fpermissive -Wno-sized-deallocation -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -flto -w -x c++ -E -CC -mmcu=attiny804 -DF_CPU=5000000L -DCLOCK_SOURCE=0 -DTWI_MORS -DMILLIS_USE_TIMERA0 -DCORE_ATTACH_ALL -DARDUINO=10607 -DARDUINO_AVR_ATtiny804 -DARDUINO_ARCH_MEGAAVR -DMEGATINYCORE="2.6.10" -DMEGATINYCORE_MAJOR=2UL -DMEGATINYCORE_MINOR=6UL -DMEGATINYCORE_PATCH=10UL -DMEGATINYCORE_RELEASED=1 -DARDUINO_attinyxy4 -I/Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10/cores/megatinycore/api/deprecated -I/Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10/cores/megatinycore -I/Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10/variants/txy4 /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/sketch/piezoSwitchV1.ino.cpp -o /private/var/folders/wq/z410yvtd0bxfkg86p3tjt7v80000gn/T/495293680/sketch_merged.cpp
/Users/dalegrover/Library/Arduino15/packages/builtin/tools/ctags/5.8-arduino11/ctags -u --language-force=c++ -f - --c++-kinds=svpf --fields=KSTtzns --line-directives /private/var/folders/wq/z410yvtd0bxfkg86p3tjt7v80000gn/T/495293680/sketch_merged.cpp
Compiling sketch...
/Users/dalegrover/Library/Arduino15/packages/DxCore/tools/avr-gcc/7.3.0-atmel3.6.1-azduino7b1/bin/avr-g++ -c -g -Os -Wall -Wextra -std=gnu++17 -fpermissive -Wno-sized-deallocation -fno-exceptions -ffunction-sections -fdata-sections -fno-threadsafe-statics -Wno-error=narrowing -MMD -flto -mmcu=attiny804 -DF_CPU=5000000L -DCLOCK_SOURCE=0 -DTWI_MORS -DMILLIS_USE_TIMERA0 -DCORE_ATTACH_ALL -DARDUINO=10607 -DARDUINO_AVR_ATtiny804 -DARDUINO_ARCH_MEGAAVR "-DMEGATINYCORE=\"2.6.10\"" -DMEGATINYCORE_MAJOR=2UL -DMEGATINYCORE_MINOR=6UL -DMEGATINYCORE_PATCH=10UL -DMEGATINYCORE_RELEASED=1 -DARDUINO_attinyxy4 -I/Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10/cores/megatinycore/api/deprecated -I/Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10/cores/megatinycore -I/Users/dalegrover/Library/Arduino15/packages/megaTinyCore/hardware/megaavr/2.6.10/variants/txy4 /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/sketch/piezoSwitchV1.ino.cpp -o /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/sketch/piezoSwitchV1.ino.cpp.o
Compiling libraries...
Compiling core...
Using precompiled core: /Users/dalegrover/Library/Caches/arduino/cores/2ba667bb2ca989f86fea08f356bbd3b4/core.a
Linking everything together...
/Users/dalegrover/Library/Arduino15/packages/DxCore/tools/avr-gcc/7.3.0-atmel3.6.1-azduino7b1/bin/avr-gcc -Wall -Wextra -Os -g -flto -fuse-linker-plugin -Wl,--gc-sections -Wl,--section-start=.text=0x0 -mmcu=attiny804 -o /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/piezoSwitchV1.ino.elf /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/sketch/piezoSwitchV1.ino.cpp.o /Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9/../../cores/2ba667bb2ca989f86fea08f356bbd3b4/core.a -L/Users/dalegrover/Library/Caches/arduino/sketches/A9A9F95C4AB86CCA43B62053B6C32CA9 -lm
/var/folders/wq/z410yvtd0bxfkg86p3tjt7v80000gn/T//ccNHrPqf.ltrans0.ltrans.o: In function `loop':
/Users/dalegrover/Documents/LittleLakeTechnology/PiezoSwitch/code/piezoSwitchV1/piezoSwitchV1.ino:781:(.text.startup+0x9be): relocation truncated to fit: R_AVR_13_PCREL against `no symbol'
collect2: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
This issue may have some similarity with situations reported in "Compilation error due to -mshort-calls #344"
Further information: I started adding NOPs, one at a time, to this same function. Starting at a program length of 6217 bytes, the linker fails for the following total program lengths:
6219, 6235, 6241, 6261, 6263, 6283, 6289, 6299. All other lengths work (up to 6299). (Each NOP adds 2 bytes, so I'm only checking for odd lengths here.) Adding the NOP further down in the program also seemed to trigger the error.
For the programmer, it would appear random whether adding code would cause an error or not.
further update:
I guessed that I would find a relative call right at the edge (+/- 4095) in the listing, and this seems to be the case. I created listings both sides of a failed link (length 6267 bytes), one at 6265 bytes long, and one at 6269 bytes long. The relative call at 0x13de is -4094 for the first good link, and +4094 for the second. It looks like for some reason the relative call is -4096 for the failed case (but there's no listing generated for the failed case). The excerpts that follow are around the source line, 737, given in the error:
var/folders/wq/z410yvtd0bxfkg86p3tjt7v80000gn/T//ccri3moP.ltrans0.ltrans.o: In function `loop':
/Users/dalegrover/Documents/LittleLakeTechnology/PiezoSwitch/code/piezoSwitchV1/piezoSwitchV1.ino:737:(.text.startup+0x9d4): relocation truncated to fit: R_AVR_13_PCREL against `no symbol'
collect2: error: ld returned 1 exit status
excerpt from listing for 6265 byte long compile:
/Users/dalegrover/Documents/LittleLakeTechnology/PiezoSwitch/code/piezoSwitchV1/piezoSwitchV1.ino:737
debugSerialNNL(interruptSource);
13d6: 80 91 2a 3e lds r24, 0x3E2A ; 0x803e2a
13da: 90 91 2b 3e lds r25, 0x3E2B ; 0x803e2b <interruptSource+0x1>
13de: 01 d8 rcall .-4094 ; 0x3e2 <Print::print(unsigned int, int) [clone .constprop.9]>
13e0: 8b e0 ldi r24, 0x0B ; 11
13e2: 98 e9 ldi r25, 0x98 ; 152
13e4: ec d7 rcall .+4056 ; 0x23be <TEXT_REGION_LENGTH+0x3be>
excerpt from listing for 6269 byte long compile:
/Users/dalegrover/Documents/LittleLakeTechnology/PiezoSwitch/code/piezoSwitchV1/piezoSwitchV1.ino:737
debugSerialNNL(interruptSource);
13da: 80 91 2a 3e lds r24, 0x3E2A ; 0x803e2a
13de: 90 91 2b 3e lds r25, 0x3E2B ; 0x803e2b <interruptSource+0x1>
13e2: ff d7 rcall .+4094 ; 0x23e2 <TEXT_REGION_LENGTH+0x3e2>
13e4: 8f e0 ldi r24, 0x0F ; 15
13e6: 98 e9 ldi r25, 0x98 ; 152
13e8: ea d7 rcall .+4052 ; 0x23be <TEXT_REGION_LENGTH+0x3be>
I do note that Microchip's release notes (https://ww1.microchip.com/downloads/aemDocuments/documents/DEV/ProductDocuments/ReleaseNotes/avr8-gnu-toolchain-3.7.0.1796-readme.pdf) for version 3.7.0.1796 (May 2022) of the GNU AVR toolchain lists the following:
Issue #XC8-1739: Fix PR 24564 - link fails for some rcalls/rjmps with wraparound.
Issue #XC8-1889: Fix PR 24571 - Relaxation does not shorten jmp or call to target at pc-relative range boundary
Not sure if this is a more recent version of the avr-gcc compiler used here, and if this would solve the issue.