Linking to a static library, not equivalent to linking to its objects

Problem:

The firmware image created when linking to the static library is different from the firmware image generated when linking to objects directly extracted from the static library.

Both firmware images are connected without errors and successfully downloaded to the microcontroller.

The last binary (associated with the objects) runs successfully and, as expected, while the first (linked to the static library) does not.

The only warnings during compilation are those unused-but-set-variablesupplied by the manufacturer of the HAL, which, due to various macro definitions, is not required for the compiled implementation; and unused-parameterin various weak functions, also supplied by the manufacturer HAL.

Description:

I am developing an embedded application for the STM32F407. So far, I have worked with one code base, including the HAL microprocessor and installation code, a driver for a specific peripheral device, and an application that uses the first two.

Since I want to develop several applications using the same driver and HAL (both are complete and tested, so they will not change often), I want to compile and distribute HAL and the driver as a static library, which can be associated with the application source.

, . , , .

:

, :

$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(APPOBJECTS) Library/libtest.a

, , :

@cd Library && $(AR) x libtest.a && cd ..
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(APPOBJECTS) Library/*.o

:

CFLAGS = $(INCLUDES) $(DEFS) -ggdb3 -O0 -std=c99 -Wall -specs=nano.specs -nodefaultlibs
CFLAGS+= -fdata-sections -ffunction-sections -mcpu=cortex-m4 -march=armv7e-m -mthumb
CFLAGS+= -mfloat-abi=hard -mfpu=fpv4-sp-d16 -MD -MP -MF $@.d

LDFLAGS = -T$(LDSCRIPT) -Wl,-static -Wl,-Map=$(@:.elf=.map),--cref -Wl,--gc-sections

-Wl,--print-gc-sections, app.map, , . -Wl,--gc-sections, .

arm-none-eabi-size :

 text      data     bss     dec     hex filename
43464        76    8568   52108    cb8c workingapp.elf

 text      data     bss     dec     hex filename
17716        44    8568   26328    66d8 brokenapp.elf

-Wl,--gc-sections

arm-none-eabi-gdb , , WWDG. , , , Default_Handler ( ). .

WWDG ,

-

+3
3

:

, . --whole-archive --no-whole-archive:

 $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(APPOBJECTS) -Wl,--whole-archive Library/libtest.a -Wl,--no-whole-archive

- , , () . , , , .

:

arm-none-eabi-gdb , , WWDG Default_Handler. ... , "STM32 WWDG , " postoverflow post.

, gdb , , .map , WWDG_IRQHandler , IRQHandlers, IRQHandlers , (, ).

, , stm32f4xx_it.o ( IRQHandlers , ) Default_Handler IRQHandler startup_stm32f407xx.o.

, (perl -n -e '/libtest\.a\((.*?)\)/ && print "$1\n"' app.map | sort -u), , .

startup_stm32f407xx.s , , :

.weak TIM2_IRQHandler

undefined . undefined, undefined, .

, , , undefined startup_stm32f407xx.o . , IRQHandler . stm32f4xx_it.o , undefined. , ; , , , . ( ) , , , , , () .

, , . ?

+4

, , " " .

, ?

, .

-, ?

, . , - , , .

, , , , . , . -Wextra -Wall .

: , LDSCRIPT RAM (.. ).

+1

MCU. , "" ST .

, ( . . - / tink (LTO) .

/. -Os -O3 LTO/no LTO ( , -O). , , gcc/ld LTO, & linked (!) . ABI (C- gcc-libs).

- reset WWDG- > CR. EWI; .

+1

All Articles