Some bit shifting arithmetic generates bad code. I will attach two source files. The command lines below demonstrate the problem. chead@ritchie $ avr-gcc -mmcu=atmega328p -Os -ffreestanding -otest.elf main.c foo.c chead@ritchie $ avr-objcopy -O ihex test.elf test.mcs chead@ritchie $ avrdude -p m328p -c arduino -P /dev/ttyACM0 -U flash:w:test.mcs:i [lots of irrelevant output snipped] chead@ritchie $ picocom -b9600 /dev/ttyACM0 picocom v1.6 port is : /dev/ttyACM0 flowcontrol : none baudrate is : 9600 parity is : none databits are : 8 escape is : C-a local echo is : no noinit is : no noreset is : no nolock is : no send_cmd is : sz -vv receive_cmd is : rz -vv imap is : omap is : emap is : crcrlf,delbs, Terminal ready 834 Thanks for using picocom Reproducible: Always Actual Results: The value printed to the serial port is 834 Expected Results: The value SHOULD be 1234 $ emerge --info cross-avr/gcc Portage 2.1.10.49 (default/linux/amd64/10.0, gcc-4.5.3, glibc-2.13-r4, 3.2.12-gentoo x86_64) ================================================================= System Settings ================================================================= System uname: Linux-3.2.12-gentoo-x86_64-Intel-R-_Core-TM-_i5_CPU_750_@_2.67GHz-with-gentoo-2.0.3 Timestamp of tree: Sat, 14 Apr 2012 04:45:01 +0000 app-shells/bash: 4.2_p20 dev-java/java-config: 2.1.11-r3 dev-lang/python: 2.7.2-r3, 3.2.2 dev-util/cmake: 2.8.6-r4 dev-util/pkgconfig: 0.26 sys-apps/baselayout: 2.0.3 sys-apps/openrc: 0.9.8.4 sys-apps/sandbox: 2.5 sys-devel/autoconf: 2.13, 2.68 sys-devel/automake: 1.11.1 sys-devel/binutils: 2.21.1-r1 sys-devel/gcc: 4.5.3-r2 sys-devel/gcc-config: 1.5-r2 sys-devel/libtool: 2.4-r1 sys-devel/make: 3.82-r1 sys-kernel/linux-headers: 3.1 (virtual/os-headers) sys-libs/glibc: 2.13-r4 Repositories: gentoo ritchie-local ibormuth hawk777 sunrise ACCEPT_KEYWORDS="amd64" ACCEPT_LICENSE="* -@EULA AdobeFlash-10.3 dlj-1.1 Oracle-BCLA-JavaSE" CBUILD="x86_64-pc-linux-gnu" CFLAGS="-march=native -msse4 -mcx16 -mpopcnt -msahf -O2 -pipe" CHOST="x86_64-pc-linux-gnu" CONFIG_PROTECT="/etc /usr/share/gnupg/qualified.txt /usr/share/openvpn/easy-rsa /var/bind /var/lib/hsqldb" CONFIG_PROTECT_MASK="/etc/ca-certificates.conf /etc/dconf /etc/env.d /etc/env.d/java/ /etc/fonts/fonts.conf /etc/gconf /etc/gentoo-release /etc/revdep-rebuild /etc/sandbox.d /etc/terminfo /etc/texmf/language.dat.d /etc/texmf/language.def.d /etc/texmf/updmap.d /etc/texmf/web2c" CXXFLAGS="-march=native -msse4 -mcx16 -mpopcnt -msahf -O2 -pipe" DISTDIR="/var/cache/distfiles" EMERGE_DEFAULT_OPTS="--jobs 3 --load-average 4 --with-bdeps y" FEATURES="assume-digests binpkg-logs distlocks ebuild-locks fixlafiles news parallel-fetch protect-owned sandbox sfperms splitdebug strict unknown-features-warn unmerge-logs unmerge-orphans userfetch xattr" FFLAGS="" GENTOO_MIRRORS="ftp://gentoo.arcticnetwork.ca/pub/gentoo ftp://mirror.csclub.uwaterloo.ca/gentoo-distfiles http://gentoo.osuosl.org/" LANG="en_CA.UTF-8" LDFLAGS="-Wl,-O1 -Wl,--as-needed" LINGUAS="en en_CA" MAKEOPTS="--jobs 5 --load-average 4" PKGDIR="/usr/portage/packages" PORTAGE_CONFIGROOT="/" PORTAGE_RSYNC_EXTRA_OPTS="--exclude ChangeLog --delete-excluded" PORTAGE_RSYNC_OPTS="--recursive --links --safe-links --perms --times --compress --force --whole-file --delete --stats --human-readable --timeout=180 --exclude=/distfiles --exclude=/local --exclude=/packages" PORTAGE_TMPDIR="/tmp" PORTDIR="/usr/portage" PORTDIR_OVERLAY="/usr/local/portage /var/lib/layman/ibormuth /var/lib/layman/hawk777 /var/lib/layman/sunrise" SYNC="rsync://rsync.ca.gentoo.org/gentoo-portage" USE="X acpi aio alsa amd64 archive bzip2 cairo caps cli cups cxx dbus dedicated dillo double-precision dri faac faad ffmpeg fontconfig fortran gif gmp gtk hpn iconv idn ipv6 jpeg libnotify mikmod mmx modules mp3 multilib ncurses nls nodot nptl nptlonly ogg opengl openmp pam png pppd readline sdl session spell sse sse2 startup-notification svg symlink sysfs threads thunar truetype unicode vdpau vim-syntax vorbis xattr xft xinerama xorg xv zlib" ALSA_CARDS="ali5451 als4000 atiixp atiixp-modem bt87x ca0106 cmipci emu10k1x ens1370 ens1371 es1938 es1968 fm801 hda-intel intel8x0 intel8x0m maestro3 trident usb-audio via82xx via82xx-modem ymfpci" ALSA_PCM_PLUGINS="adpcm alaw asym copy dmix dshare dsnoop empty extplug file hooks iec958 ioplug ladspa lfloat linear meter mmap_emul mulaw multi null plug rate route share shm softvol" APACHE2_MODULES="actions alias auth_basic authn_alias authn_anon authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_host authz_owner authz_user autoindex cache cgi cgid dav dav_fs dav_lock deflate dir disk_cache env expires ext_filter file_cache filter headers include info log_config logio mem_cache mime mime_magic negotiation rewrite setenvif speling status unique_id userdir usertrack vhost_alias" CALLIGRA_FEATURES="kexi words flow plan sheets stage tables krita karbon braindump" CAMERAS="ptp2" COLLECTD_PLUGINS="df interface irq load memory rrdtool swap syslog" ELIBC="glibc" GPSD_PROTOCOLS="ashtech aivdm earthmate evermore fv18 garmin garmintxt gpsclock itrax mtk3301 nmea ntrip navcom oceanserver oldstyle oncore rtcm104v2 rtcm104v3 sirf superstar2 timing tsip tripmate tnt ubx" INPUT_DEVICES="evdev" KERNEL="linux" LCD_DEVICES="bayrad cfontz cfontz633 glk hd44780 lb216 lcdm001 mtxorb ncurses text" LINGUAS="en en_CA" PHP_TARGETS="php5-3" QEMU_SOFTMMU_TARGETS="x86_64" RUBY_TARGETS="ruby18" USERLAND="GNU" VIDEO_CARDS="nvidia" XFCE_PLUGINS="logout menu" XTABLES_ADDONS="quota2 psd pknock lscan length2 ipv4options ipset ipp2p iface geoip fuzzy condition tee tarpit sysrq steal rawnat logmark ipmark dhcpmac delude chaos account" Unset: CPPFLAGS, CTARGET, INSTALL_MASK, LC_ALL, PORTAGE_BUNZIP2_COMMAND, PORTAGE_COMPRESS, PORTAGE_COMPRESS_FLAGS, USE_PYTHON ================================================================= Package Settings ================================================================= cross-avr/gcc-4.5.3-r2 was built with the following: USE="cxx nls (-altivec) -bootstrap -build -doc (-fixed-point) -fortran -gcj -graphite -gtk -hardened -libssp -lto -mudflap -multilib -multislot -nocxx -nopie -nossp -nptl -objc -objc++ -objc-gc -openmp -test -vanilla" CFLAGS="-O2 -pipe" CXXFLAGS=""
Created attachment 309207 [details] The main.c file that demonstrates the bug
Created attachment 309209 [details] The auxiliary foo.c used in the demonstration test case
Note that removing the infinite loop around the code, or replacing -Os with -O0, -O1, -O2, -O3, or even -O4 fixes the bug. For interest, here's a disassembly of the main function via avr-objdump: 000000c2 <main>: c2: 87 e6 ldi r24, 0x67 ; 103 c4: 90 e0 ldi r25, 0x00 ; 0 c6: 90 93 c5 00 sts 0x00C5, r25 ca: 80 93 c4 00 sts 0x00C4, r24 ce: 10 92 c0 00 sts 0x00C0, r1 d2: 80 e4 ldi r24, 0x40 ; 64 d4: 9f e1 ldi r25, 0x1F ; 31 d6: 01 97 sbiw r24, 0x01 ; 1 d8: f1 f7 brne .-4 ; 0xd6 <main+0x14> da: 88 e0 ldi r24, 0x08 ; 8 dc: 80 93 c1 00 sts 0x00C1, r24 e0: 85 e0 ldi r24, 0x05 ; 5 e2: 91 e0 ldi r25, 0x01 ; 1 e4: 90 93 17 01 sts 0x0117, r25 e8: 80 93 16 01 sts 0x0116, r24 ec: 80 e0 ldi r24, 0x00 ; 0 ee: e8 2e mov r14, r24 f0: 81 e0 ldi r24, 0x01 ; 1 f2: f8 2e mov r15, r24 f4: 00 e9 ldi r16, 0x90 ; 144 f6: 11 e0 ldi r17, 0x01 ; 1 f8: 0e 94 54 00 call 0xa8 ; 0xa8 <get_msb> fc: 0e 94 56 00 call 0xac ; 0xac <get_lsb> 100: 00 d0 rcall .+0 ; 0x102 <main+0x40> 102: 00 d0 rcall .+0 ; 0x104 <main+0x42> 104: ed b7 in r30, 0x3d ; 61 106: fe b7 in r31, 0x3e ; 62 108: f2 82 std Z+2, r15 ; 0x02 10a: e1 82 std Z+1, r14 ; 0x01 10c: e0 e0 ldi r30, 0x00 ; 0 10e: ef 01 movw r28, r30 110: 90 e0 ldi r25, 0x00 ; 0 112: 8c 2b or r24, r28 114: 9d 2b or r25, r29 116: ed b7 in r30, 0x3d ; 61 118: fe b7 in r31, 0x3e ; 62 11a: 94 83 std Z+4, r25 ; 0x04 11c: 83 83 std Z+3, r24 ; 0x03 11e: 0e 94 9d 00 call 0x13a ; 0x13a <printf> 122: 0f 90 pop r0 124: 0f 90 pop r0 126: 0f 90 pop r0 128: 0f 90 pop r0 12a: 80 e1 ldi r24, 0x10 ; 16 12c: 97 e2 ldi r25, 0x27 ; 39 12e: f8 01 movw r30, r16 130: 31 97 sbiw r30, 0x01 ; 1 132: f1 f7 brne .-4 ; 0x130 <main+0x6e> 134: 01 97 sbiw r24, 0x01 ; 1 136: d9 f7 brne .-10 ; 0x12e <main+0x6c> 138: df cf rjmp .-66 ; 0xf8 <main+0x36> Observe that get_msb() is called and its return value is completely discarded.
sorry, we have no avr experts here, or really anyone skilled in working with code generation. please file these bugs upstream: http://gcc.gnu.org/bugzilla/ feel free to CC us though
Are they likely to care about a bug in 4.5?
they'll most likely want you to look at 4.7 first. if that's fixed (you can test easily with portage now), then there's nothing to do. if it's a simple fix, we can look at adding to our 4.5 patchset if you like.
Actually, this is fixed in 4.5.4 (a "mov r13, r24" instruction was added between the two function calls).