Rebuilding bootloader results in too-big hex file

Hey arduino community, I'm having a strange problem where re-building the unchanged optiboot_atmega328 bootloader results in a hex file that's significantly different than the one included with the arduino IDE, and is also too big to burn.

My ultimate goal is to make a small modification so that one of the digital IO's is set to output low during the bootloader to support my specific hardware setup. But I can't even get the unchanged bootloader to work :slight_smile:

Here's the commands I used to re-build it:

$ pwd
~/.arduino15/packages/arduino/hardware/avr/1.8.3/bootloaders/optiboot
$ mv optiboot_atmega328.hex optiboot_atmega328.hex.original
$ mv optiboot_atmega328.lst optiboot_atmega328.lst.original
$ make atmega328
avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mshort-calls -mmcu=atmega328p -DF_CPU=16000000L  '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200'   -c -o optiboot.o optiboot.c
avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mshort-calls -mmcu=atmega328p -DF_CPU=16000000L  '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200' -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe -Wl,--relax -Wl,--gc-sections -nostartfiles -nostdlib -o optiboot_atmega328.elf optiboot.o 
avr-size optiboot_atmega328.elf
   text	   data	    bss	    dec	    hex	filename
    532	      0	      0	    532	    214	optiboot_atmega328.elf
avr-objcopy -j .text -j .data -j .version --set-section-flags .version=alloc,load -O ihex optiboot_atmega328.elf optiboot_atmega328.hex
avr-objdump -h -S optiboot_atmega328.elf > optiboot_atmega328.lst
rm optiboot.o optiboot_atmega328.elf
$

Here is the error given by the IDE when I try to burn the new bootloader:

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "/home/jon/.arduino15/packages/arduino/hardware/avr/1.8.3/bootloaders/optiboot/optiboot_atmega328.hex"
avrdude: ERROR: address 0x8010 out of range at line 33 of /home/jon/.arduino15/packages/arduino/hardware/avr/1.8.3/bootloaders/optiboot/optiboot_atmega328.hex
avrdude: read from file '/home/jon/.arduino15/packages/arduino/hardware/avr/1.8.3/bootloaders/optiboot/optiboot_atmega328.hex' failed

avrdude done.  Thank you.

Error while burning bootloader.

Here are my toolchain versions:

$ avr-gcc --version
avr-gcc (GCC) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ avr-size --version
GNU size (GNU Binutils) 2.26.20160125
Copyright (C) 2015 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.

$ avr-objcopy --version
GNU objcopy (GNU Binutils) 2.26.20160125
Copyright (C) 2015 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.

$ avr-objdump --version
GNU objdump (GNU Binutils) 2.26.20160125
Copyright (C) 2015 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) any later version.
This program has absolutely no warranty.

$

see attached for the output files in case that helps. Thanks for any help or ideas on what to check next!

Here are the .lst files since I can't add them as attachments...

Recompiled:

Original:

AFAIK, the version of the bootloader distributed with the Arduino was compiled with v4.3.x (from the earlier versions of Arduino.) New compiler versions needed tweeks. If you want to make modifications, you shou;d probably start with the latest optiboot source from GitHub - Optiboot/optiboot: Small and Fast Bootloader for Arduino and other Atmel AVR chips

make atmega328
avr-gcc (AVR_8_bit_GNU_Toolchain_3.6.1_495) 5.4.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

BAUD RATE CHECK: Desired: 115200, Real: 117647, UBRRL = 16, Difference=2.1%
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3              -c -o optiboot.o optiboot.c
optiboot.c:386:6: warning: #warning BAUD_RATE off by greater than 2% [-Wcpp]
avr-gcc -g -Wall -Os -fno-split-wide-types -mrelax -mmcu=atmega328p -DF_CPU=16000000L  -DBAUD_RATE=115200 -DLED_START_FLASHES=3            -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe -Wl,--relax -nostartfiles -o optiboot_atmega328.elf optiboot.o 
avr-size optiboot_atmega328.elf
   text    data     bss     dec     hex filename
    482       0       0     482     1e2 optiboot_atmega328.elf

I get the same size as you with the old source and the new compiler.
You could also download an older compiler (old version of Arduino, or "WINAVR"):

make GCCROOT=/Downloads/Arduino-1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/ atmega328
/Downloads/Arduino-1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mshort-calls -mmcu=atmega328p -DF_CPU=16000000L  '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200'   -c -o optiboot.o optiboot.c
/Downloads/Arduino-1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-gcc -g -Wall -Os -fno-inline-small-functions -fno-split-wide-types -mshort-calls -mmcu=atmega328p -DF_CPU=16000000L  '-DLED_START_FLASHES=3' '-DBAUD_RATE=115200' -Wl,--section-start=.text=0x7e00 -Wl,--section-start=.version=0x7ffe -Wl,--relax -Wl,--gc-sections -nostartfiles -nostdlib -o optiboot_atmega328.elf optiboot.o 
/Downloads/Arduino-1.0.5.app/Contents/Resources/Java/hardware/tools/avr/bin/avr-size optiboot_atmega328.elf
   text    data     bss     dec     hex filename
    500       0       0     500     1f4 optiboot_atmega328.elf

Awesome, thank you so much! Using the latest optiboot code from their github repo worked perfectly