How to get the size of a function in bytes in GNU assembler with Intel syntax?

╄→尐↘猪︶ㄣ 提交于 2020-12-13 04:05:11

问题


I need to compute the size of a function in bytes at assembly time. I've tried various ways, including:

.set chk0_sz, offset chk0_e -  offset chk0_s 

and then using mov rcx, offset chk0_sz to get the value.

However, it gives the error:

error: cannot use more than one symbol in memory operand.

Here chk0_e and chk0_s are two labels denoting the end and start of the function, respectively.

Any ideas?


回答1:


You only need the offset keyword when using an address as an immediate. In other contexts, like as data, it can't be dereferenced anyway so the symbol is the address.

Compilers typically use stuff like .size chk0, . - chk0. So you probably want

.equ  chk0_sz, . - chk0     # at the end of chk0

. is the current position, it replaces using chk0_e if you put the .equ at that position.


And obviously you can't use rcx (a register) as part of an assemble-time-constant calculation. Or did you mean mov rcx, offset chk0_sz to use the size?

If you define it as an assemble-time constant with .equ, you'd just do mov ecx, chk0_sz. MASM-style syntax is inconsistent, so this is a mov-immediate not a load because of how chk0_sz was defined.



来源:https://stackoverflow.com/questions/54822792/how-to-get-the-size-of-a-function-in-bytes-in-gnu-assembler-with-intel-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!