473,395 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Loading a function @ a particular adddress

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;
}
Nov 13 '05 #1
5 2600

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: su********@altium.com
WWW: http://www.altium.com
-----------------------------------------
tkk <ki*****@hyd.hellosoft.com> schrieb in im Newsbeitrag:
f4**************************@posting.google.com...
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;
}

Nov 13 '05 #2
In <f4**************************@posting.google.com > ki*****@hyd.hellosoft.com (tkk) writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3

"tkk" <ki*****@hyd.hellosoft.com> wrote in message

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.
Nov 13 '05 #4
Default User wrote:

tkk wrote:
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?


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

Nov 13 '05 #5
In article <LL************************@newssvr10.news.prodigy .com>,
Thomas Matthews <Th**************************@sbcglobal.net> wrote:
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).
--
In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://67.40.109.61/torek/index.html (for the moment)
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Aguilar, James | last post by:
My previous example used the concept of a Shape class heirarchy, so I will continue with that. Suppose I have something like fifty different shapes, and I am trying to instantiate one of them. ...
4
by: zborisau | last post by:
Hey good people, I've been given a problem to solve recently - and stuck with the solution for a good 4 days already. i have a link which leads to popup window. the purpose of that popup...
2
by: christopher.secord | last post by:
I would like have a little "loading..." tab not unlike the one that gmail uses and I would like to display that tab while an ajax call is made. The javascript to display the tab works. The...
2
by: Jean Pierre Daviau | last post by:
When I have something like this in a js file. listeImg = new Array(); listeImg = new Image(w,h); listeImg.src = "centre1.jpg"; etc Are the images loaded after the page is loaded? In the same...
5
by: toffee | last post by:
Hi all, I've seen a really cool effect which i would like to use on an intranet site. Am referring to the 'LOADING..' animation you see when switching pages. I've seen it somewhere on a website...
5
by: marfi95 | last post by:
I have a form that has a left and right panel. In the left panel is a treeview. The right panel I want to change dynamically based on the type of node selected. What I'm doing is loading the...
6
by: ernesto.tejeda | last post by:
Hello, I have a couple of questions regarding the loading of .js files for the browser and would like anyone to point me wher to find the answer (or if you know the answer and tell me will do just...
1
by: sidd4one | last post by:
Hello, Hi experts when iam pinging from one of my system in the network. Its show iam "Unable to contact to ip adddress".This is message iam ...
1
by: kailashchandra | last post by:
Below is my code.i want the first division should be load when window is loading. <HTML> <HEAD> <script> function tabFunc(getthis) { var getIn=getthis.innerHTML; var...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.