Loading a function @ a particular adddress

T

tkk

Hi All,
Is it possible to load a function at a particular address/memory
through the source program specification itself (or) is it upto
linker/loader combination which decides this?

For ex:
int hi()
{
return 0;
}

int
main()
{
/* Write Code To Put "hi" @ 0xaa123456 */
return 0;
}
 
J

Jan Homuth

The locating of the function itself is to be done by the linker/locator of
the toolchain in use.

The compiler will not do this, since it has no control over allocation of
addresses.
Except for most embedded toolchains that allow to do so via #pragma.

It is NOT however an ANSI/ ISO compiler feature.

Embedded toolchains all have a linker/locator that allows to specify which
code sections and segments go to what address. Thus it is also possible to
specifiy the section a function resides in.


The use of that function is demonstrated below:

typedef void (*t_void_function_void) (void);

t_void_function_void my_located_func;

void main(void)
{
my_located_func = (t_void_function_void) 0x1234;

my_located_func();
}

In this example a function pointer is used to access an externally defined
and explicitely located function.


--
with kind regards


--
-----------------------------------------
Jan Homuth
Application Engineer
Technical Support , Embedded Tools

Altium - Making Electronics Design Easier

Altium Germany GmbH
Technologiepark Karlsruhe
Alber-Nestler Str. 7
D-76131 Karlsruhe
Phone: +49 721 8244 310
Fax: +49 721 8244 320

E-Mail: (e-mail address removed)
WWW: http://www.altium.com
 
D

Dan Pop

In said:
Is it possible to load a function at a particular address/memory
through the source program specification itself (or) is it upto
linker/loader combination which decides this?

For ex:
int hi()
{
return 0;
}

int
main()
{
/* Write Code To Put "hi" @ 0xaa123456 */
return 0;
}

Since you're using the main() function, I'll assume a hosted
implementation and point out that what you want to do is pointless:
how do you know that 0xaa123456 is not already in use by the time your
program is executed? Or that such an address exists at all (it's way
above 2 GB and most computers have less than 1 GB these days).

Furthermore, considering that most current hosted implementations use
virtual memory, there is really no point in insisting that a certain
function is loaded at a specific virtual memory address.

If you *really* know what you want to do, the usual way to achieve such
things is by playing with the linker/loader, which is the one making
these decisions. There is no portable way of passing this kind of
hints/requests to the linker from the C code.

Dan
 
M

Malcolm

tkk said:
Hi All,
Is it possible to load a function at a particular address/memory
through the source program specification itself (or) is it upto
linker/loader combination which decides this?
No. You can play silly hack tricks

eg

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

/* illegally cast the function address to a char * */
unsigned char * ptr = (unsigned char *) foo;

/* set the first byte of the function to something, maybe machine code for
return */
*ptr = 123;

These will work on some platforms and not others.

If code needs to be at an absolute location in memory you really need to
resort to the assembler, which might provide facilities for this.
 
T

Thomas Matthews

Default said:
What exactly are you trying to accomplish? Why would you want to load a
function in a particular place? It sounds like a case of fixing the
wrong problem. If you tell us what your real goal is, we may be able to
help.




Brian Rodenborn

Brian,

There are times when a function needs to be in one location for
execution, but reside in another.

One example is programming a Flash Memory. The functions for
programming a Flash memory can reside in the Flash. However,
many Flash's do not support simultaneous execution (read) and
programming. Because of this, the code to program the Flash
must be relocated before it is executed. In the system that
I am working on, the code to program a sector of the Flash
must be copied into RAM and then executed. During
initialization, the code is copied into a reserved area into
RAM and function pointers are set appropriately.

As far as I have researched (and I did post to this newsgroup)
there is no method to copy a function from one location to
another. There is no standard method for obtaining either
the length of a function nor its ending address.

In my system, the issue of moving code into another location
was best accomplished by writing all of the code in assembly
language (including the code to move it).

--
Thomas Matthews

C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
C

Chris Torek

There are times when a function needs to be in one location for
execution, but reside in another.

One example is programming a Flash Memory. The functions for
programming a Flash memory can reside in the Flash. However,
many Flash's do not support simultaneous execution (read) and
programming. Because of this, the code to program the Flash
must be relocated before it is executed. ...
As far as I have researched (and I did post to this newsgroup)
there is no method to copy a function from one location to
another. There is no standard method for obtaining either
the length of a function nor its ending address.

Indeed, this is quite the case. Not only is there no Standard
(as in C89 or C99) method, neither is there a standard (as in
"de facto" / "this works on almost every system") method.

Many, if not most, C systems that target "freestanding" environments
-- partiuclarly those where the code is written on System A but
run on Completely Different System B, such as "written on Unix-clone"
and "run on microwave oven" -- have quite fancy linkers. Many of
these can even be instructed to link code to *load* at location X
yet *run* at location Y.

Unfortunately, the exact set of instructions varies from one linker
to the next. So you need a system- or even linker-specific newsgroup
to talk about this (otherwise you will get wrong answers, and nobody
will even know they are wrong).
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top