Getting the code size of a function in C - Page 2

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
Re: Getting the code size of a function in C
Quoted text here. Click to load it

What you want to do is highly non-portable but try this working code
snippet I use for the MSP430:

// Blinks the LED
void codeFunction(void)
{
    uartPrintf(": BLINK Press ESC to quit\r\n");

    for(;;)
    {
        if(uartGetByte() == ESC) break;

        LED_ON(P3OUT,BIT0);
        delay(100);
        LED_OFF(P3OUT,BIT0);
        delay(100);
    }
}

void codeFunction_END(void) { }

// Executes the BLINK command
void funcBLINK(int count,char *cmd,char *arg1,char *arg2)
{
    byte codeRam[90];
    size_t loadSize;
    void (*pfnFunction)(void);

    USE(count);
    USE(cmd);
    USE(arg1);
    USE(arg2);

    loadSize = (size_t)(codeFunction_END) - (size_t)(codeFunction);
    uartPrintf(": BLINK Load size = %u\r\n",loadSize);

    if(loadSize >= sizeof(codeRam))
    {
        uartPrintf(": BLINK Can't load code\r\n");
    }
    else
    {
        // Copy the code to RAM
        memset(codeRam,0,sizeof(codeRam));
        memcpy(codeRam,codeFunction,loadSize);

        // Point to the function starting in RAM
        pfnFunction = (void (*)(void))(codeRam);

        // Execute from RAM
        pfnFunction();

        uartPrintf(": BLINK ok\r\n");
    }
}

Note that delay() and uartPrintf() and uartGetByte() do NOT get copied
to RAM.  That's part of the challenge.

JJS


Re: Getting the code size of a function in C

Quoted text here. Click to load it

In ELF files generated by GCC, the symbol table has not only the name,
type (FUNC), and address of each symbol, but its size as well. Thus the
symbol table in the ELF file will tell you how big the code for the
function is; something like objdump usually does not print out this
information, but it is there.


Re: Getting the code size of a function in C
Quoted text here. Click to load it

The ELF file isn't available to the program at run-time.  It's
quite simple to put global simbols at the beginning and end of
a section.  Then all you have to do in C is declare those
symbols as external char arrays.

--
Grant Edwards                   grante             Yow!  I'm having an
                                  at               EMOTIONAL OUTBURST!! But,
We've slightly trimmed the long signature. Click to see the full one.
Re: Getting the code size of a function in C

Quoted text here. Click to load it


True, but you could reserve initialized memory space to hold the
information and have a post-link tool that extracts the desired
information from the ELF header and then stores it in the space you
reserved...

More basically, the ELF header might give you some idea if the block of
code in question is contiguous - ie, if the scheme is going to be
possible provided you can provide the addresses at runtime, or if the
compiler/linker is going to need adjustment in order to produce
contiguous functions.


Re: Getting the code size of a function in C
Quoted text here. Click to load it

Perhaps, but it's way, way, way easier to do this in the linker
script.  After all keeping track of what's where in memory is
what linkers _do_.  Those ELF headers were createdy by the
linker, and all that information is availably in a trivial
manner to the linker script.

--
Grant Edwards                   grante             Yow!  Yow! Maybe I should
                                  at               have asked for my Neutron
We've slightly trimmed the long signature. Click to see the full one.
Re: Getting the code size of a function in C

Quoted text here. Click to load it

No way.  A portable program has no way at all of finding this out,
i.e. it cannot be done "in C" at all.  And even a non-portable
program, using features outside the C programming language proper,
typically won't be able to do this at run-time.  Stuff like this has
to be set up at link time, the latest, and often you'll need to do it
at compile time already.  There's even a non-negligible probability
that it can't be done at all, or only in assembly.

Some toolsets have special extensions (#pragma or __attribute__()) in
the source, or some magic incantation to put in the linker commands)
that tell the linker a particular function will be stored in a
location different from the one it's meant to run from.

--
Hans-Bernhard Broeker ( snipped-for-privacy@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.

Site Timeline