6
votes

I can't seem to figure out why some printf library functions get linked into my code from libc_nano.a even though I never use any printf. It steals at least 2K of valuable flash memory space. I can see the sections _printf_i, _vfprintf_r, _vfiprintf_r, etc. in my linker map file.

I tried

  • Wl,--exclude-libs option
  • EXCLUDE_FILE(..) in linker script

None of these make the symbols disappear from the map file..

My gcc options:

CFLAGS = -Og -Wall -g3 -Wdouble-promotion -mcpu=cortex-m0 -mthumb -fmessage-length=0 -ffunction-sections -mfloat-abi=soft -DUSE_HAL_DRIVER

LFLAGS = -mcpu=cortex-m0 -mthumb -mfloat-abi=soft -specs=nosys.specs -specs=nano.specs -Wl,--gc-sections 

arm-none-eabi-gcc.exe (GNU Tools for ARM Embedded Processors) 5.2.1 20151202 (re lease) [ARM/embedded-5-branch revision 231848] Copyright (C) 2015 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.

First of all, why do they get linked? Is there any method to exclude them?

1
Have you tried compiling with -nostdinc and linking with -nostdlib ?Jean-François Fabre♦
Yes I did, and strangely enough the code compiles and links successfully, but, size gets so much bigger that it won't fit in the flash memory of the device. I suppose the linker falls back using something else instead of libc_nano.hgabe

1 Answers

7
votes

Tell the linker to generate a cross reference: -Wl,--cref

--cref

Output a cross reference table. If a linker map file is being generated, the cross reference table is printed to the map file. Otherwise, it is printed on the standard output. The format of the table is intentionally simple, so that it may be easily processed by a script if necessary. The symbols are printed out, sorted by name. For each symbol, a list of file names is given. If the symbol is defined, the first file listed is the location of the definition. The remaining files contain references to the symbol.

Look for a line starting with one of the print symbols, and the lines below it.

grep -A5 _printf *.map

There you'll find the library function that uses printf internally.