function size

G

Gordon Burditt

Is there any way to find function size in C ?

Only in an extremely unportable manner (like, for example, running
the UNIX command "size" on the object file when only one function
is present in the compilation unit).

Incidentally, if you have 4 separate functions in 4 separate files,
there is no guarantee that the sizes of these match the combined
size when you put all 4 functions in the same file and look at the
size of the combined object file. In this situation, "function size"
as a concept is a bit fuzzy.

Gordon L. Burditt
 
J

Jack Klein

Is there any way to find function size in C ?

Regards,
Galoma B

No, not in the standard C language.

Your toolset might have some facilities for providing this
information, but that is a tool specific issue, not a language one.

In C types and objects, and only types and objects, have size. A
function is neither of these.
 
P

Paul

Keith Thompson said:
Not portably.

Thanks for your reply. But I see some one doing that, check out
http://help.madshi.net/CopyFunction.htm. I was wondering how he must
have done that and can we do the same in C. May be its less to do with
language and more to do with OS.

Tricks like this won't help.
void foo()
{
//do something
}
void dummy()
{
//do nothing
}

main()
{
int sizeoffoo = (int)((long)dummy - (long)foo);
}

any rescue???

Regards,
Galoma B.
 
T

Thomas Matthews

Paul said:
Thanks for your reply. But I see some one doing that, check out
http://help.madshi.net/CopyFunction.htm. I was wondering how he must
have done that and can we do the same in C. May be its less to do with
language and more to do with OS.

Tricks like this won't help.
void foo()
{
//do something
}
void dummy()
{
//do nothing
}

main()
{
int sizeoffoo = (int)((long)dummy - (long)foo);
}

any rescue???

Regards,
Galoma B.

I've already researched this issue. The information that
other posters have given you is true: There is no portable
method to find the size of a function using _standard_ C.
(Search the newsgroups for "function size"
author == "Thomas Matthews")

Your options are:
1. Write the whole function in assembly language.

2. Check your compiler and linker manuals to see how you
can place functions into specific areas or segments.

3. Compile the function into a binary file and place it
into a constant array of bytes.

I chose #1 because it was faster than to use #2. Some
of my associates use option #3. You issue is well known
when code has to be copied from a source container in
order to be executed.

Try also

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
F

Flash Gordon

On 16 Aug 2004 06:24:10 -0700
Thanks for your reply. But I see some one doing that, check out
http://help.madshi.net/CopyFunction.htm. I was wondering how he must
have done that and can we do the same in C.

As stated. Not portably. I.e. the C language provides no mechanism to do
this but some language extensions might. Any such language extensions
are outside the scope of what is discussed here so you will have to ask
in a group dedicated to your implementation and/or OS.
May be its less to do with
language and more to do with OS.

Tricks like this won't help.
void foo()
{
//do something
}
void dummy()
{
//do nothing
}

main()
{
int sizeoffoo = (int)((long)dummy - (long)foo);
}

any rescue???

Not portably. Firstly there is no guarantee that function pointers will
fit in to ANY integer type, including long. Secondly, the compiler is
free to rearrange the functions so that dummy is before foo and main is
between dummy and foo. There *are* implementations which reorder
functions, gcc 3.4 with -O2 being one example, although I don't think it
would put main between dummy and foo.

So go ask in a group/mailing list dedicated to your system if you need
this functionality.
 
D

Dag Viken

Paul said:
Keith Thompson <[email protected]> wrote in message

Thanks for your reply. But I see some one doing that, check out
http://help.madshi.net/CopyFunction.htm. I was wondering how he must
have done that and can we do the same in C. May be its less to do with
language and more to do with OS.

Tricks like this won't help.
void foo()
{
//do something
}
void dummy()
{
//do nothing
}

main()
{
int sizeoffoo = (int)((long)dummy - (long)foo);
}

any rescue???

Regards,
Galoma B.

While it is not portable, I got the above code to work with VC7 as long as
incremental linking was disabled. I suspect function level linking must also
be disabled (not tested). Further, in release mode, if foo() and dummy() are
both empty they will be optimized to a single RET instruction at the same
address - so the size of foo will come out as zero.

The web page you refer to is obviously for Win32 platforms. However, if you
want to copy a function and execute it you need more information than just
the function address and size. Function calls and references to global
variables must be relocated since relative addressing is used almost
exclusively. When a program or DLL is loaded into memory, the OS relocates
addresses based on the relocation tables inside the Portable Executable
(PE). When copying a function you will need to do the same thing, i.e. read
the relocation info and update the relative addresses. It is not too
difficult but you will need to study the PE format and understand how it
works.

Dag
 
P

Paul

Dag Viken said:
While it is not portable, I got the above code to work with VC7 as long as
incremental linking was disabled. I suspect function level linking must also
be disabled (not tested). Further, in release mode, if foo() and dummy() are
both empty they will be optimized to a single RET instruction at the same
address - so the size of foo will come out as zero.

The web page you refer to is obviously for Win32 platforms. However, if you
want to copy a function and execute it you need more information than just
the function address and size. Function calls and references to global
variables must be relocated since relative addressing is used almost
exclusively. When a program or DLL is loaded into memory, the OS relocates
addresses based on the relocation tables inside the Portable Executable
(PE). When copying a function you will need to do the same thing, i.e. read
the relocation info and update the relative addresses. It is not too
difficult but you will need to study the PE format and understand how it
works.

Dag

Dag many thanks for your explanation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,145
Messages
2,570,828
Members
47,374
Latest member
anuragag27

Latest Threads

Top