我想在我的二进制文件中嵌入一个git版本标记,但是链接器似乎将其删除。
Makefile:
CPPFLAGS += -DREV=$(shell git describe)
file.c:
#define xstr(s) str(s)
#define str(s) #s
const char rev[]="rev[" xstr(REV) "]";
标记位于file.o中:
$ strings file.o | grep rev
rev[v1.0.1-5-ga45db7f-dirty]
GCC: (15:7-2018-q2-6) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
但在file.elf中丢失:
$ strings file.elf | grep rev
GCC: (15:7-2018-q2-6) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
GCC: (15:7-2018-q2-5) 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907]
GNU C11 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907] -mthumb -march=armv6s-m -g -g -g -O2 -O2 -O2 -fbuilding-libgcc -fno-stack-protector -fno-inline -fvisibility=hidden
GNU C11 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907] -mthumb -march=armv6s-m -g -g -g -O2 -O2 -O2 -fbuilding-libgcc -fno-stack-protector -fno-inline -fexceptions -fvisibility=hidden
GNU C11 7.3.1 20180622 (release) [ARM/embedded-7-branch revision 261907] -mthumb -march=armv6s-m -mthumb -march=armv6s-m -g -O2 -fno-builtin
prev_tzenv
LoopForever
prev_tzenv
.debug_abbrev
链接器命令行:
arm-none-eabi-gcc -std=c99 -O3 -Wall -ffunction-sections -mcpu=cortex-m0 -mthumb -mfloat-abi=soft -Tst/stm32f0xx-32-6.ld -Wl,-Map=dtc_main.map -Wl,--gc-sections -specs=nano.specs -o file.elf file.o startup_stm32f0xx.o system_stm32f0xx.ao syscalls.o
您可以使用rev
和-fno-data-sections
编译文件,也可以在某处添加虚拟使用:
volatile int x = 0;
if (x)
memcpy(NULL, rev, 1);