微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32函数放入段section中

stm32函数放入段section中

时间:12-03 来源:互联网 点击:
关键字,__attribute__((section)).

对于这样一个需求,不管你写多少个硬件底层初始化函数,我都能通过固定的循环进行执行,是不动的一个状态,这种实现方式,可以通过以下介绍的方式操作。

思路,有两种办法,一种是指定一个段,这个段需要固定,然后,在这个段之间的区域将函数写入进去。一种是直接将函数一直写入,编译器知道写的函数有多少个,调用编译器得到的函数个数来操作,对于写的函数个数同样灵活。

第一种办法:

指定段的办法。

操作示例:

先定义一个函数类型。

typedef int (*MyFun)(void);

#define INIT_FUN(fn,level) \

const MyFun __myFun_##fn __attribute__((section(".myFun."level))) = fn

让其在初始化动作的时候,写入一个段中,在程序上看起来是一个text文本段了。

这里有一个知识点,如果这样写的话,后期程序遍历的时候,发现在程序上无法执行初始化的操作,根源是在map文件中:

Section Cross References

startup_stm32f10x_hd.o(RESET) refers to startup_stm32f10x_hd.o(STACK) for __initial_sp

startup_stm32f10x_hd.o(RESET) refers to startup_stm32f10x_hd.o(.text) for Reset_Handler

startup_stm32f10x_hd.o(RESET) refers to stm32f10x_it.o(.text) for NMI_Handler

startup_stm32f10x_hd.o(.text) refers to system_stm32f10x.o(.text) for SystemInit

startup_stm32f10x_hd.o(.text) refers to entry.o(.ARM.Collect$$$$00000000) for __main

main.o(.text) refers to printf1.o(i.__0printf$1) for __2printf

main.o(.text) refers to usart1.o(.text) for USART1_Config

main.o(.text) refers to main.o(.myFun.0) for __myFun_init_begin

main.o(.text) refers to main.o(.myFun.7) for __myFun_init_end

main.o(.myFun.0) refers to main.o(.text) for init_begin

main.o(.myFun.0s) refers to main.o(.text) for init_fun1

main.o(.myFun.2) refers to main.o(.text) for init_fun2

main.o(.myFun.7) refers to main.o(.text) for init_end

Removing Unused input sections from the image.

Removing startup_stm32f10x_hd.o(HEAP), (512 bytes).

Removing main.o(.myFun.0s), (4 bytes).

Removing main.o(.myFun.2), (4 bytes).

Removing core_cm3.o(.emb_text), (32 bytes).

Removing dadd.o(.text), (330 bytes).

Removing dmul.o(.text), (226 bytes).

Removing ddiv.o(.text), (222 bytes).

Removing dfixul.o(.text), (48 bytes).

Removing cdrcmple.o(.text), (40 bytes).

Removing depilogue.o(.text), (194 bytes).

10 unused section(s) (total 1612 bytes) removed from the image.

刚开始建立了,但是在程序上没有使用,就给删除了段。

那么这个缘由肯定是由于编译器动了手脚,因此,查看Arm Development Tool可以查到,在RealView Linker User Guide这个栏目下的Section elimination下的unused section elimination中有相关的叙述:

Unused section elimination

RealView Compilation Tools for ?Vision Linker User Guide

Version 4.0

Home > Using the Basic Linker Functionality > Section elimination > Unused section elimination

Unused section elimination removes unreachable code and data from the final image. This optimization can be controlled by the--remove,--no_remove,--first,--last, and--keeplinker options and, indirectly, with--entry. Use the--info unusedlinker option to instruct the linker to generate a list of the unused sections that have been eliminated.

Unused section elimination is suppressed in those cases that might result in the removal of all sections.

An input section is retained in the final image under the following conditions:

  • if it contains an entry point
  • if it is referred to, directly or indirectly, by a non-weak reference from an input section containing an entry point
  • if it is specified as the first or last input section by the--firstor--lastoption (or a scatter-loading equivalent)
  • if it is marked as unremovable by the--keepoption.

Note

Compilers will normally collect functions and data together and emit one section for each category. The linker can only eliminate a section if it is entirely unused.

里面谈

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top