Linker feedback

来源:互联网 发布:java用什么软件编写 编辑:程序博客网 时间:2024/05/18 10:35

armlink提供一个反馈,作用于下次文件编译时使用,告知编译器未使用的函数有哪些。这些为使用的函数都是放在自己的.o文件中,将来由链接器去除这些未使用的函数。

——内联优化选项打开时,链接器也会把内联函数写入反馈文件,这些函数也放置在自己.O文件中。

——如果反馈文件选项打开,会生成一个反馈文件,包含输出反馈文件文件名,日志,和未使用的符号函数和函数属于的文件,例如:

;#<FEEDBACK># ARM Linker, RVCT3.0 [Build num]: Last Updated: Date
;VERSION 0.2
;FILE dhry_1.o
unused_func1 <= USED 0
inlined_func <= LINKER_INLINED
;FILE dhry_2.o
unused_func2 <= USED 0

当你下一次编译源码时,如果打开编译器选项-反馈文件,那么要指定linker-generated反馈文件。如果没有反馈文件存在,编译器会发出一个警告消息。



Linker feedback example据一个例子

来解释linker feedback 运行

To see how linker feedback works:

  1. Create a file fb.c containing the code shown in Example 3.2.

    Example 3.2. Feedback example

    #include <stdio.h>void legacy() {    printf("This is a legacy function, that is no longer used.\n");}int cubed(int i) {    return i*i*i;}void main() {    int n = 3;    printf("%d cubed = %d\n",n,cubed(n));}

2.Compile the program (ignore the warning that the feedback file does not exist):

用下面的命令编译程序,feedback file文件是不存在的,但是忽略这带来的编译警告。
armcc --asm -c --feedback fb.txt fb.c 使用指定feedback文件来编译fb.c,生成.s文件

默认cubed()内联,并且生成fb.s and an object file fb.o.

在汇编文件中,legacy() and cubed() 还是存在。 因为内联的原因,在 main中不调用 cubed()。

因为 cubed()没有申明static类型,所以 out-of-line copy of cubed()。

3.使用下面命令链接object文件,生成linker feedback文件。



armlink --info sizes --list fbout1.txt --feedback fb.txt fb.o -o fb.axf

armlink的信息输出到fbout1.txt文件中。

在feedback文件中包括了unused functions,在comment中描述。并且entries for the legacy() and cubed() functions的入口。


;#<FEEDBACK># ARM Linker, RVCT 3.0 [Build num]: Last Updated: Date
;VERSION 0.2
;FILE fb.o
cubed <= USED 0
legacy <= USED 0

This shows that the functions are not used.

这就是显示, legacy() and cubed() unused。


4.Repeat the compile and link stages with a different diagnostics file:

重新编译和链接原来的程序文件,比较一下诊断log信息,log信息会输出到fbout2.txt


armcc --asm -c --feedback fb.txt fb.c


armlink --info sizes --list fbout2.txt fb.o -o fb.axf

5.Compare the two diagnostics files, fbout1.txt and fbout2.txt, to see the sizes of the image components (for example,CodeRO DataRW Data, and ZI Data). The Code component is smaller.

比较两个不同的log信息,文件fbout1.txt and fbout2.txt, 查看image 部分的size区别,code size 减小了。

In the assembler file, fb.s, the legacy() and cubed() functions are no longer in the main .text area. They are compiled into their own ELF sections. Therefore, armlink can remove the legacy() and cubed() functions from the final image.

fb.s汇编文件中,main .text area中不存在legacy() and cubed(),他们只是存在自己的axf文件中。因此armlink把legacy() and cubed()从image中删除掉了。



总结:如果开启feedback选项,每次编译结束后,会armlink根据上一次的feedback内容,去优化image。同时在编译时,生成.o文件后,会把unused 函数信息,写入到feedback文件中,为下一次编译做准备。

以cp侧库上代码为例

拉好代码后,编译整个项目,这时feedback使用库上的feedback,来优化image,同时会把新的信息写入这个feedback中,为下一次编译link时优化image使用。

注意1.假如项目中没有feedback,则会报警告。假如feedback是空文件,不会报警告但是优化image失效,因为得不到unsued信息

2.在提交代码时要求研发人员同时上传feedback文件,这样可以避免在拉完代码后,发生第一次编译和第二次编译,产生的image文件有区别。



对于网上所述,在keil中选中feedback后,keil会自动对源代码进行两次编译,这个可能是对的。

但是如果自己写makefile文件,armcc只编译一次,而且使用上一次产生的feedback文件进行优化。

原创粉丝点击