8

My question is about some popular but not well documented code I have found in ARM CORTEX M startup files like this STM32.

The 'pattern' is:

.size X,.-X

,where X is a symbol or label.

I have found this answer and understand how .size directive and dot special symbol work, but still complete line seems to do nothing to me. The result of the operation .-X isn't stored anywhere.

Could anybody explain what the line does?

FemtoFarad
  • 83
  • 5
  • 1
    The result is stored in the metadata automatically by the `.size` directive. – Jester Oct 15 '18 at 13:53
  • 1
    The size calculation works exactly like `$ - X` in NASM: [How does $ work in NASM, exactly?](https://stackoverflow.com/q/47494744). But the `.size` directive emits ELF metadata with the function size, which is useful for shared libraries. – Peter Cordes Oct 15 '18 at 14:06
  • 1
    Thank you for answers! What will happen if I do not put this code after the function? Will it have negative impact on optimization only or my code won't work as expected? – FemtoFarad Oct 15 '18 at 14:32
  • 2
    The effect of leaving it out is stuff like [ARM Assembly Functions Appear With Size 0 in Symtab](https://stackoverflow.com/q/52820466). I'm not sure if there are implications for correctness of static or dynamic linking. But it's just metadata, so if it works at all, there aren't any performance implications. – Peter Cordes Oct 15 '18 at 16:15
  • If you miss it out it might affect how well the debugging of the assembler works for the reasons in the comment above. – Realtime Rik Oct 16 '18 at 07:32

1 Answers1

7

That is placed at the end of function X, and the size of the function is the difference between the end of the function and the beginning. . is the current location so it's saying .size x is the difference between here and the label x.

Colin
  • 3,394
  • 1
  • 21
  • 29