I have quite large ld
link script for embedded platform which is low on RAM and ROM. I want to know how much memory is left available after I have relocated all the code. Actually, I want to print out the value
of location counter .
to stdout.
How can I do it? Is there some magic command like print(.)
?
4 Answers
I have a post-link step in my projects that dumps the size of stuff so I can see how close I'm getting. Just add something along the lines of:
arm-none-eabi-size binary_image.axf
That will get you output like:
text data bss dec hex filename
204808 704 23188 228700 37d5c Foo.axf
On my cortex-m3 chip, this would be text+data = flash usage, data+bss = ram usage. dec/hex are useless values.
And as Olaf says, use a map file for more specific memory consumption. I have this added to my link step:
-Xlinker -Map=Foo.map
Another solution might be to add the following command to the linker:
-Xlinker --print-memory-usage
This gives me the following output:
Memory region Used Size Region Size %age Used
m_interrupts: 576 B 576 B 100.00%
m_text: 22988 B 32192 B 71.41%
m_data: 26552 B 32 KB 81.03%
Read the manual. There are no such commands - there cannot be.
Linker "scripts" are actually more like configuration/descriptor files. They are not "executed" like a script. There is also not a single .
(how could be for different memory areas?).
You can, however, output a map which might exactly be what you need. Try option -M
. If you have set up the memory regions in the linker script correctly, the linker will warn if some memory area overflows, which is actually what you want for automatic builds.
Update: You could grep/filter the map file if you want to insist seeing the section sizes on each build.
-
But I want to know how much space do I have for the code I will write in the future. However, you're right that there is no such command to print message in ld script, so I will mark your answer as correct. Jul 9, 2015 at 15:13
-
1@RostakaGmfun: Read the last paragraph again, I edited slightly. There is a way ! Jul 9, 2015 at 15:17
-
-
1They are clearly executed like scripts.
.
is a variable that is updated as the script is "executed". I don't know why you think this would be impossible.– TimmmmJun 18, 2018 at 12:30 -
@Timmmm Just because there is a variable does not imply they are executable files. As much as makefiles are not scripts (although those can even contain snippets of code). Jun 18, 2018 at 13:18
You can't print the value of a symbol while the script is being executed, but you can create a symbol and the look it up afterwards with nm
. Like this:
value_of_dot = .;
Then
nm my_file.elf | grep value_of_dot
Edit: If you really want it printed to stdout
you would have to modify the linker. E.g. for lld
, add printf
s in LinkerScript.cpp
in LinkerScript::assignSymbol()
.
For your particular use-case of checking how much memory is used, it is probably better to use size
, as escrafford suggested, or objdump -section-headers
.
-
This is in fact even worse than simply checking the map file and it does not what OP asks for (which is clearly impossible). Jun 18, 2018 at 13:20
-
It does do what OP asks for if you put it at the end of your script after all the sections (assuming they are in order).– TimmmmJun 18, 2018 at 13:37
-
Using
size
is clearly better for his application though. But people coming to this question from Google might not want to do exactly the same thing.– TimmmmJun 18, 2018 at 13:38 -
No, it does not do what OP apparently wants: output this information to stdout by the linker. Which is exactly what I wrote in my answer: not possible. Plus it is practically not relevant for embedded development. What one wants here is to check if the code/data fits into a memory region. That's what the linker automatically checks as I wrote in my answer. And that's what OP needs. Jun 18, 2018 at 13:41
-
Sure but I came to this question because I wanted to print a symbol value for a different reason, so answering the question he asked is still useful to other people. Please remove your downvote. This answer clearly works.– TimmmmJun 18, 2018 at 14:43
ASSERT(0,"Hello there.");
will print a message and exit. You can use it to assert when the free room has reach some limit. Replace '0' with an expression that is enough room.