[Bug lto/83967] New: LTO removes C functions declared as weak in assembler(depending on files order in linking)

2018-01-22 Thread hurwic8 at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83967

Bug ID: 83967
   Summary: LTO removes C functions declared as weak in
assembler(depending on files order in linking)
   Product: gcc
   Version: 7.2.1
Status: UNCONFIRMED
  Severity: normal
  Priority: P3
 Component: lto
  Assignee: unassigned at gcc dot gnu.org
  Reporter: hurwic8 at gmail dot com
CC: marxin at gcc dot gnu.org
  Target Milestone: ---

I've observed strange behaviour of the link-time optimization on GCC 7.2.1,
exact version: gcc version 7.2.1 20170904 (release) [ARM/embedded-7-branch
revision 255204] (GNU Tools for Arm Embedded Processors 7-2017-q4-major)

I have project that is compiled for the microcontroller. In startup file, that
is written in assembler, there are weak definitions of the interrupt handlers.
Some of those weak functions are defined in the project C files.

Strange behaviour occurs when I set -flto flag on GCC 7.2.1: during
optimization, the defined in C files interrupt functions are perceived as
unused and removed(replaced by its weak definitions) - I've checked that in the
.map file.

This issue does not occur when startup file is first on the source files list,
which is given as an argument to the linker. If I change order of source files,
all interrupt handlers from C files, that are placed on the source files list
before startup are removed and replaced by weak definitions from startup.

For me it seems that linker is removing interrupt handler functions before
noticing its usage in startup file. Or maybe weak function definitions from
startup override definitions from C files.

I've checked if this issue happens on the GCC 4.9.3 - it worked fine, so this
is rather new behaviour.

I've also confirmed this bug with other developer from Germany:
https://www.mikrocontroller.net/topic/443262 
Take a look at the latest posts, which are written in English.

Is this a bug or should I place some extra pragmas/attributes to extort proper
linker behaviour?

Please let me know if you want me to prepare some basic examples of this issue.

[Bug lto/83967] LTO removes C functions declared as weak in assembler(depending on files order in linking)

2018-01-23 Thread hurwic8 at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83967

--- Comment #4 from Rafał Mszal  ---
Created attachment 43210
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=43210&action=edit
Basic example of main funtion with LTO failure.

This is simple example of observed LTO issue. RTC1_IRQHandler is removed
despite it is used to change volatile variable. RTC1_IRQHandler is defined weak
in the assembler startup file.

[Bug lto/83967] LTO removes C functions declared as weak in assembler(depending on files order in linking)

2018-01-23 Thread hurwic8 at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83967

--- Comment #5 from Rafał Mszal  ---
Created attachment 43211
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=43211&action=edit
Makefile that causes failure.

[Bug lto/83967] LTO removes C functions declared as weak in assembler(depending on files order in linking)

2018-01-23 Thread hurwic8 at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83967

--- Comment #6 from Rafał Mszal  ---
Created attachment 43212
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=43212&action=edit
Startup file for the nRF52 microcontroller.

This is typical startup for nRF5x microcontrollers. The main thing that is
important is the interrupts vector table. All interrupts used in table are
declared as weak at the end of file.

[Bug lto/83967] LTO removes C functions declared as weak in assembler(depending on files order in linking)

2018-01-23 Thread hurwic8 at gmail dot com
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83967

--- Comment #7 from Rafał Mszal  ---
Thanks for response.

I've send some basic example of the failure. However, it needs some others file
to compile and link, like linker, SystemInit function decalration and so on.

I've just send you most important files, now I am working on removing sources
that are not important, but this will take me a while, since i need to remove
lot of dependencies between those files, makefile and linker.

I've checked 'used' attribute, it doesn't solve the problem. Same with the
suggested '-fuse-linker-plugin' flag.