问题
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