Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-07

How to get address of MyFunction in MyFunction?

6 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Žilvinas Ledas

Hello all,

to get address of function I can use @MyFunction, but if I use it within MyFunction - I get address of Result. How can I get address of current function? (It's not for assigning to something else. It is for a kind of self modifying code.)

Regards
Žilvinas Ledas

Jonas Maebe
to Žilvinas Ledas
Žilvinas Ledas wrote
to get address of function I can use @MyFunction, but if I use it within MyFunction - I get address of Result. How can I get address of current function?

@unitname.function (or @programname.function if you are in the main program file)

Jonas

Jonas Maebe
to Jonas Maebe
Jonas Maebe wrote 5 lines
On 18 Jul 2010, at 12:11, Žilvinas Ledas wrote: > to get address of function I can use @MyFunction, but if I use it within MyFunction - I get address of Result. How can I get address of current function? @unitname.function (or @programname.function if you are in the main program file)

PS: if you want to use this to calculate the size of functions: don't. The linker can reorder the functions compared to how they are declared in your source file.

Jonas

Žilvinas Ledas
to Jonas Maebe
Jonas Maebe wrote 12 lines
On 18 Jul 2010, at 12:50, Jonas Maebe wrote: > On 18 Jul 2010, at 12:11, Žilvinas Ledas wrote: > > >> to get address of function I can use @MyFunction, but if I use it within MyFunction - I get address of Result. How can I get address of current function? >> > @unitname.function (or @programname.function if you are in the main program file) > PS: if you want to use this to calculate the size of functions: don't. The linker can reorder the functions compared to how they are declared in your source file.

Thanks, I'll try it.

I want to use it this way:
1) Gel all @unitname.function addresses.
2) Use disassembler to find nearest next RET.
3) Then I assume that I have start and end of the function in the binary file.

It works at least for the concept-showing app with some functions and enabled optimizations when compiling. Do I have some conceptual flaw? And maybe there is an easier way to get function RET addresses in the binary file?

Regards
Žilvinas

Jonas Maebe
to Žilvinas Ledas
Žilvinas Ledas wrote 6 lines
I want to use it this way: 1) Gel all @unitname.function addresses. 2) Use disassembler to find nearest next RET. 3) Then I assume that I have start and end of the function in the binary file. It works at least for the concept-showing app with some functions and enabled optimizations when compiling. Do I have some conceptual flaw?

Yes: there is no guarantee that a function will contain only one "ret" (it is that way currently for compiler-generated code, but that can change at any time).

Jonas

Žilvinas Ledas
to Jonas Maebe
Jonas Maebe wrote 12 lines
On 18 Jul 2010, at 13:14, Žilvinas Ledas wrote: > I want to use it this way: > 1) Gel all @unitname.function addresses. > 2) Use disassembler to find nearest next RET. > 3) Then I assume that I have start and end of the function in the binary file. > > It works at least for the concept-showing app with some functions and enabled optimizations when compiling. Do I have some conceptual flaw? > Yes: there is no guarantee that a function will contain only one "ret" (it is that way currently for compiler-generated code, but that can change at any time).

I understand that there can be more than one "ret" and I know current version of compiler generates only one so at least for now I am OK. Thanks Jonas for your answers!

P. S. I was unable to get any info with "nm" utility from MinGW.

Regards
Žilvinas