[Bug target/56533] New: Linker problem on avr with lto and main function inside archive

matthijs at stdin dot nl gcc-bugzilla@gcc.gnu.org
Tue Mar 5 12:33:00 GMT 2013


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56533

             Bug #: 56533
           Summary: Linker problem on avr with lto and main function
                    inside archive
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: matthijs@stdin.nl


When trying to add lto to my Arduino program, it stopped compiling complaining
about missing symbols. I've managed to reduce the problem to below minimal
example. Note that removing anything from below example makes the problem
disappear. In particular, the problem disappears when:
 * any of the linker options is removed: -mmcu=atmega328p -Os -flto
-fwhole-program
 * the -flto compiler option is removed
 * using normal gcc (amd64) instead of avr-gcc
 * linking main.o instead of main.a
 * declaring realmain as externally_visible in realmain.c

Note that in this example, the actual main() function is inside an archive,
which is probably the reason for this bug / problem.



$ avr-gcc --version
    avr-gcc (GCC) 4.7.2
    Copyright (C) 2012 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-ld --version
    GNU ld (GNU Binutils) 2.20.1.20100303
    Copyright 2009 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) a later
version.
    This program has absolutely no warranty.

$ cat main.c
    int realmain(void);

    int main(void)
    {
        return realmain();
    }

$ cat realmain.c
    int realmain(void) {
    }

$ cat do
    #!/bin/sh
    set -x

    rm -f main.a

    /usr/bin/avr-gcc -c main.c -o main.o
    /usr/bin/avr-ar rcs main.a  main.o
    /usr/bin/avr-gcc -c -flto realmain.c -o realmain.o
    /usr/bin/avr-gcc -mmcu=atmega328p -Os -flto -fwhole-program realmain.o
main.a

$ ./do
    + rm -f main.a
    + /usr/bin/avr-gcc -c main.c -o main.o
    + /usr/bin/avr-ar rcs main.a main.o
    + /usr/bin/avr-gcc -c -flto realmain.c -o realmain.o
    + /usr/bin/avr-gcc -mmcu=atmega328p -Os -flto -fwhole-program realmain.o
main.a
    main.a(main.o): In function `main':
    main.c:(.text+0x8): undefined reference to `realmain'
    collect2: error: ld returned 1 exit status



More information about the Gcc-bugs mailing list