Motherboard Forums


Reply
Thread Tools Display Modes

Getting the code size of a function in C

 
 
Tosca Berisha
Guest
Posts: n/a
 
      11-01-2005, 04:26 PM
I need to determine the size of a function or module since I need to
temporary relocate the function or module from flash into sram for
firmware updates.

How can I find that out during runtime. The
sizeof( myfunction)
generates an error saying "size of function unknown".

Thanks.
 
Reply With Quote
 
 
 
 
Grant Edwards
Guest
Posts: n/a
 
      11-01-2005, 04:30 PM
On 2005-11-01, Tosca Berisha <(E-Mail Removed)> wrote:

> I need to determine the size of a function or module since I
> need to temporary relocate the function or module from flash
> into sram for firmware updates.
>
> How can I find that out during runtime.


You'll need to do something in your linker script to create
global symbols before and after the function. In your C code,
you should then be able to take the address of those global
symboals and do some math to determine the size.

--
Grant Edwards grante Yow! UH-OH!! We're out
at of AUTOMOBILE PARTS and
visi.com RUBBER GOODS!
 
Reply With Quote
 
 
 
 
Arie de Muynck
Guest
Posts: n/a
 
      11-01-2005, 05:20 PM
"Grant Edwards" ...
> On 2005-11-01, Tosca Berisha <(E-Mail Removed)> wrote:
>
> > I need to determine the size of a function or module since I
> > need to temporary relocate the function or module from flash
> > into sram for firmware updates.
> >
> > How can I find that out during runtime.

>
> You'll need to do something in your linker script to create
> global symbols before and after the function. In your C code,
> you should then be able to take the address of those global
> symboals and do some math to determine the size.


I've often seen compiler generated (optimized) code that jumps to common
sections of code, shared between functions. Labels around a function won't
help you when that happens.
Also, don't expect multiple functions in a module to stay in the same
sequence as they were in the source file, not even with all optimisation
off.

You could put the function as the only function in a **module** (a single
source file), which is also the only way I know with a (GNU) linker script
to put labels before and after it.

Regards,
Arie de Muynck




 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      11-01-2005, 05:51 PM
On 2005-11-01, Arie de Muynck <(E-Mail Removed)> wrote:

> I've often seen compiler generated (optimized) code that jumps
> to common sections of code, shared between functions. Labels
> around a function won't help you when that happens.


Such labels will allow him to determine the size of a function
(which is what he asked for). I didn't guarantee that answering
his question would solve his problem.

> Also, don't expect multiple functions in a module to stay in
> the same sequence as they were in the source file, not even
> with all optimisation off.


Ordering is probably not guaranteed, but I've never seen it
change.

> You could put the function as the only function in a
> **module** (a single source file), which is also the only way
> I know with a (GNU) linker script to put labels before and
> after it.


If you're using GCC, the easiest thing to do is to put the
function in question in its own section using
__attribute__((section "whatever")). Then you can put labels
at the start and end of section "whatever". Or you can define
a global symbol having the value of the size of the section
(which even more directly answers the original question).

Of course you could also put all functions in individual
sections using -ffunction-sections.

--
Grant Edwards grante Yow! You were s'posed
at to laugh!
visi.com
 
Reply With Quote
 
Tosca Berisha
Guest
Posts: n/a
 
      11-01-2005, 06:00 PM
Arie de Muynck wrote:
> "Grant Edwards" ...
>
>>On 2005-11-01, Tosca Berisha <(E-Mail Removed)> wrote:
>>
>>
>>>I need to determine the size of a function or module since I
>>>need to temporary relocate the function or module from flash
>>>into sram for firmware updates.
>>>
>>>How can I find that out during runtime.

>>
>>You'll need to do something in your linker script to create
>>global symbols before and after the function. In your C code,
>>you should then be able to take the address of those global
>>symboals and do some math to determine the size.

>
>
> I've often seen compiler generated (optimized) code that jumps to common
> sections of code, shared between functions. Labels around a function won't
> help you when that happens.
> Also, don't expect multiple functions in a module to stay in the same
> sequence as they were in the source file, not even with all optimisation
> off.
>
> You could put the function as the only function in a **module** (a single
> source file), which is also the only way I know with a (GNU) linker script
> to put labels before and after it.
>
> Regards,
> Arie de Muynck
>


So then the only proper way would be to access the symbol table and see
whats the next entry after my function and get the difference between
the two.

Is there a standard way of accessing the symbol table?

For now (after reading Grants answer, thank you) I created a dummy
function after the needed function, and I am getting the size by
subtracting the pointers to those functions to get the size. This linker
I am using seems to keep the functions in the same order as written in
the .c file.


But I don't like relying on this solution, so I still seek for an
alternative.

Thanks Arie,
Tosca
 
Reply With Quote
 
Tauno Voipio
Guest
Posts: n/a
 
      11-01-2005, 06:17 PM
Tosca Berisha wrote:
> I need to determine the size of a function or module since I need to
> temporary relocate the function or module from flash into sram for
> firmware updates.
>
> How can I find that out during runtime. The
> sizeof( myfunction)
> generates an error saying "size of function unknown".


If you're using GCC, probably you do not need to
copy anything, just locate your function to the initialized
data section:

/* Write unlock sequence */

static void unlock(void) __attribute__((section(".data")));
static void unlock(void)
{
FLASHMEM[KEYLOC1] = KEY1;
FLASHMEM[KEYLOC2] = KEY2;
}

This is the Flash unlock routine from an embedded
system.

--

Tauno Voipio
tauno voipio (at) iki fi

 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      11-01-2005, 07:11 PM
On 2005-11-01, Tauno Voipio <(E-Mail Removed)> wrote:

>> I need to determine the size of a function or module since I
>> need to temporary relocate the function or module from flash
>> into sram for firmware updates.
>>
>> How can I find that out during runtime. The sizeof(
>> myfunction) generates an error saying "size of function
>> unknown".

>
> If you're using GCC, probably you do not need to copy
> anything, just locate your function to the initialized data
> section:
>
> /* Write unlock sequence */
>
> static void unlock(void) __attribute__((section(".data")));
> static void unlock(void)
> {
> FLASHMEM[KEYLOC1] = KEY1;
> FLASHMEM[KEYLOC2] = KEY2;
> }
>
> This is the Flash unlock routine from an embedded
> system.


That works, but it can be a bit of a waste since the function
is sitting in both flash and RAM 100% of the time. If there
are multiple routines that need to be run from RAM at different
points in time, you can save a bit of RAM by overlaying them.

--
Grant Edwards grante Yow! I'll show you MY
at telex number if you show
visi.com me YOURS...
 
Reply With Quote
 
johnspeth@yahoo.com
Guest
Posts: n/a
 
      11-01-2005, 07:22 PM
Tosca Berisha wrote:
> I need to determine the size of a function or module since I need to
> temporary relocate the function or module from flash into sram for
> firmware updates.
>
> How can I find that out during runtime. The
> sizeof( myfunction)
> generates an error saying "size of function unknown".


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

 
Reply With Quote
 
Tauno Voipio
Guest
Posts: n/a
 
      11-01-2005, 07:43 PM
Grant Edwards wrote:
> On 2005-11-01, Tauno Voipio <(E-Mail Removed)> wrote:
>
>
>>>I need to determine the size of a function or module since I
>>>need to temporary relocate the function or module from flash
>>>into sram for firmware updates.
>>>
>>>How can I find that out during runtime. The sizeof(
>>>myfunction) generates an error saying "size of function
>>>unknown".

>>
>>If you're using GCC, probably you do not need to copy
>>anything, just locate your function to the initialized data
>>section:
>>
>>/* Write unlock sequence */
>>
>>static void unlock(void) __attribute__((section(".data")));
>>static void unlock(void)
>> {
>> FLASHMEM[KEYLOC1] = KEY1;
>> FLASHMEM[KEYLOC2] = KEY2;
>> }
>>
>>This is the Flash unlock routine from an embedded
>>system.

>
>
> That works, but it can be a bit of a waste since the function
> is sitting in both flash and RAM 100% of the time. If there
> are multiple routines that need to be run from RAM at different
> points in time, you can save a bit of RAM by overlaying them.
>


The amount of code needed in RAM for Flash writing is pretty
small: about 250 bytes on an ARM for garden-variety AMD Flashes.

--

Tauno Voipio
tauno voipio (at) iki fi
 
Reply With Quote
 
Grant Edwards
Guest
Posts: n/a
 
      11-01-2005, 08:03 PM
On 2005-11-01, Tauno Voipio <(E-Mail Removed)> wrote:

>>>>I need to determine the size of a function or module since I
>>>>need to temporary relocate the function or module from flash
>>>>into sram for firmware updates.


>>>If you're using GCC, probably you do not need to copy
>>>anything, just locate your function to the initialized data
>>>section:


>> That works, but it can be a bit of a waste since the function
>> is sitting in both flash and RAM 100% of the time. If there
>> are multiple routines that need to be run from RAM at different
>> points in time, you can save a bit of RAM by overlaying them.


> The amount of code needed in RAM for Flash writing is pretty
> small: about 250 bytes on an ARM for garden-variety AMD
> Flashes.


But where is the "flash writing" code getting the data?

In cases I've dealt with the "flash writing" code has to
impliment some sort of communications protocol in order to
receive the data to be programmed into flash. In the product
I'm working with right now, it takes 3KB of code to do a flash
update. That's a pretty significant chunk out of my 32KB of
RAM.

--
Grant Edwards grante Yow! Xerox your lunch
at and file it under "sex
visi.com offenders"!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Make laptop function keys behave like desktop function keys BillW50 Laptops 2 06-29-2010 10:58 AM
Re: Make laptop function keys behave like desktop function keys Aaron Laptops 0 06-28-2010 06:37 PM
Getting the size of a C function john Embedded 62 02-01-2010 04:56 PM
how to find the stack size used by a function noor.fatma@gmail.com Embedded 14 07-09-2006 10:43 PM
size of physical memory is given by size of address registers in CPU or size of address bus?? Arunaabh Intel 4 05-06-2006 05:05 PM


All times are GMT. The time now is 11:56 PM.


Welcome!
Welcome to Motherboard Point
 

Advertisment