Tool/software:
Hi .
I have encountered some problems with linker here, and the following describes the problem phenomenon in detail.
I declare a variable in the code with the __attribute__ method and put it in a section called "test", but this variable has not been called in the function yet.
static __attribute__((used, section("test"))) int attribute_test_var = 0;
Then add a description of the corresponding section in the SECTIONS field of the cmd file. And add two variables to the link script to observe the first address of the section, to verify that the section is indeed allocated the appropriate amount of memory.
test : {
*(test)
} LOAD_START(_test_start_address),
LOAD_END(_test_end_address)
> FLASH0
The compiler will give an alarm like warning #10068-D: no matching section
In the section description here, it will be found that the section named "test" is not allocated to the actual memory, and the length is 0.
However, if you look down to see if the variable was compiled, you will find that it was compiled and that it was marked as the "test" section. Note The linker discarded the variable as invalid.
The output of the map file is shown below:
SECTION ALLOCATION MAP output attributes/ section page origin length input sections -------- ---- ---------- ---------- ---------------- test 0 00000020 00000000 UNINITIALIZED GLOBAL SYMBOLS: SORTED ALPHABETICALLY BY Name address name ------- ---- 00000020 _test_end_address 00000020 _test_start_address GLOBAL SYMBOLS: SORTED BY Symbol Address address name ------- ---- 00000020 _test_end_address 00000020 _test_start_address ================================================================================ main.obj Run/Load Value Binding Name (Section) -------- -------- --------------- 00000000 local attribute_test_var (test)
In this case, if the variable is called again as an lvalue or an rvalue and compiled again. In the map file, you can see that the corresponding variable appears in the "test" section and is allocated real memory.
output attributes/ section page origin length input sections -------- ---- ---------- ---------- ---------------- test 0 00000020 00000004 UNINITIALIZED 00000020 00000004 main.obj (test) GLOBAL SYMBOLS: SORTED ALPHABETICALLY BY Name address name ------- ---- 00000024 _test_end_address 00000020 _test_start_address GLOBAL SYMBOLS: SORTED BY Symbol Address address name ------- ---- 00000020 _test_start_address 00000024 _test_end_address
It is basically confirmed from the above that __attribute__((used)) has not yet taken effect.
In addition, through code modification and compilation test down. A variable found to be const modified, even if used as an rvalue in code, is discarded by the linker and cannot be linked to a section.
Consult the compiler manual:
ARM Optimizing C/C + + Compiler v20.2.0. LTS: https://www.ti.com/lit/ug/spnu151w/spnu151w.pdf?ts=1717108109945
The section 5.17.4 Variable Attributes confirms that the compiler should be able to support the used attribute in __attribute__.
In addition, a very bad solution was found in another manual.
ARM Assembly Language Tools v20.2.0.LTS:https://www.ti.com/lit/ug/spnu118z/spnu118z.pdf?ts=1717084542730
In 8.4.10 Do Not Remove Unused Sections, adjust the link option to --unused_section_elimination=off
You can force all unused variables to be retained and linked.But this will link all the variables in my library file to the output file.
So I hope I can help solve the problems mentioned above.Thanks.