Writing to flash memory fails with Optiboot
The bootloader which ships with Arduino IDE is version 4 which was already in the Uno from factory. To use the do_spm function you need Optiboot 8 build with this support enabled. It is not enabled in the default builds, because it doesn't fit in the usual Optiboot size for Uno.

The hex file of the bootloader is here. Simply replace the hex file in AVR boards package with this one.