Function Address Storage [ SOLVED ]

Thread Starter

Kittu20

Joined Oct 12, 2022
470
Given the basic memory sections of text, data, stack, and heap, where would the address of a function be stored?

I know it depends on the hardware architecture and compiler, But for general processes, I am interested to understand I think that function addresses are stored in the text memory.

I've written a small code to explore this further. Can you confirm if it's true that the addresses of those functions will indeed be stored in the text memory?

C:
#include <stdio.h>

void foo()
{
    printf("Welcome \n");
}

int add ( int a, int b)
{
    int addition = a + b;
    return addition;
}

int main() {
    
    foo();
    int result =  add ( 5, 2);
    printf("%d\n", result);
    
    return 0;
}
 

ApacheKid

Joined Jan 12, 2015
1,610
Given the basic memory sections of text, data, stack, and heap, where would the address of a function be stored?

I know it depends on the hardware architecture and compiler, But for general processes, I am interested to understand I think that function addresses are stored in the text memory.

I've written a small code to explore this further. Can you confirm if it's true that the addresses of those functions will indeed be stored in the text memory?

C:
#include <stdio.h>

void foo()
{
    printf("Welcome \n");
}

int add ( int a, int b)
{
    int addition = a + b;
    return addition;
}

int main() {
 
    foo();
    int result =  add ( 5, 2);
    printf("%d\n", result);
 
    return 0;
}
the symbol table.

https://refspecs.linuxbase.org/elf/gabi4+/ch4.symtab.html

See STT_FUNC
 

BobTPH

Joined Jun 5, 2013
8,967
Depends entirely on the architecture. Some instruction sets might have an absolute address of the function encoded in the instruction. Others might use a relative offset from the current location. Still others might have to load the address into a register using a different instruction. Of course the address of a function can also be stored in data memory as a function pointer.
 

MrChips

Joined Oct 2, 2009
30,810
Every MCU has relative and absolute branch instructions. The same goes for subroutine calls and jumps.
Read the MCU User Manual to find out where the destination address is stored.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
470
I'm assuming the c code is written on a Windows PC. I'm looking a general explanation that might provide a clue
 

MrChips

Joined Oct 2, 2009
30,810
It doesn't matter what computer platform is being used.
Systems use relative addressing so that code can be relocatable.
Look up BRA and JMP instruction format.
 

Papabravo

Joined Feb 24, 2006
21,225
You really need to get in the habit of looking at the resources that you have available to you. What do I mean by resources?
  1. Compiler listings of generated assembly language
  2. Link Maps from a linkage editor
  3. Disassembly listings from a debugger
Do you really not know of the existence of these things?

These things are NOT black boxes, but you have to open the lid to look inside.
 

ApacheKid

Joined Jan 12, 2015
1,610
There is a book titled Compiler Design in C, I have a paperback copy here but it is now out of print, a bit hard to find. That book is huge and covers a host of real world details that the compiler write faces. It covers all of the stuff you are asking about. I have several excellent books on real world compiler development and this is one of them I used this when designing a compiler myself many years ago.

The book can be downloaded for free too, as a PDF.
 

MrChips

Joined Oct 2, 2009
30,810
The compiler/assembler/linker has to maintain a list of variable and function addresses. After that, the destination address is buried in the instruction.

For a multi-tasking operating system where code is accessible via an API (Application Programming Interface) function addresses need to be stored as handles (indirect addressing).
 

Papabravo

Joined Feb 24, 2006
21,225
In short, there is no single method that covers all situations. There are multiple solutions to the question posed by the TS, and the context is everything when formulating the answer.
 

WBahn

Joined Mar 31, 2012
30,062
Given the basic memory sections of text, data, stack, and heap, where would the address of a function be stored?

I know it depends on the hardware architecture and compiler, But for general processes, I am interested to understand I think that function addresses are stored in the text memory.

I've written a small code to explore this further. Can you confirm if it's true that the addresses of those functions will indeed be stored in the text memory?

C:
#include <stdio.h>

void foo()
{
    printf("Welcome \n");
}

int add ( int a, int b)
{
    int addition = a + b;
    return addition;
}

int main() {
   
    foo();
    int result =  add ( 5, 2);
    printf("%d\n", result);
   
    return 0;
}
How can we confirm something that you've already indicated depends on the hardware architecture and compiler?

For most functions, the function address is hardcoded into the program, since it is referring to another part of the program. For dynamically-linked library functions, things are trickier in that they usually involve some kind of address translation lookup table table.
 
Top