How to specify address for a function

This is a discussion on How to specify address for a function within the Linux Programming forums, part of the Platform Specific Boards category; Hi, how do I place a variable/function at some specified addresses, e.g. I have this program test.c: Code: char variable ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    How to specify address for a function

    Hi,

    how do I place a variable/function at some specified addresses, e.g. I have this program test.c:
    Code:
    char variable = 0x01;  // Want this "variable" placed in address 0x600000
    // Want this "function" placed at address 0x400000
    void function(void) __attribute__ ((section (".test")));
    void function(void) {
     printf("hello\n");
    }
    main() {
     function();
    }
    I tried to compile it using:
    gcc -g -Wl,--section-start=.test=0x400000 test.c
    But when I run it, I get Segmentation fault.

    Can someone give me a sample linker script to cause the linker to place the objects (function and variable) at the specified addresses?

    Thanks in advance.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Are you trying to run that on your desktop Linux machine?

    Well it's unlikely to work since all addresses are virtualised, so the idea of "at a specific address" is meaningless.

    Now if you were on a system without an OS virtualising everything, then you could certainly place functions and variables at specific locations, in the manner you describe.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Yes, I'm trying to run that on my desktop Linux machine. brewbuck said it could be done in this thread:

    Specify address for a function in Relocatable Code

    You could write a linker script to cause the linker to place the object at a specified address, but it would no longer be an object file, and could not participate in any further linking. If you are trying to make the object load somewhere specific, you have to do this when it is linked into a complete, working program.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Quote Originally Posted by pilgrim View Post
    Yes, I'm trying to run that on my desktop Linux machine. brewbuck said it could be done in this thread:

    Specify address for a function in Relocatable Code
    Could you please tell why you need data/function on specific address ?

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    I have a simulation that takes a long time to run and I want to be able to save/restore the state of my simulation and continue where I left off. That means I save many of the variables some of which store pointers to function addresses. This works fine until I recompile and the linker places my functions at different addresses than when I last save out my "binary state file" and now the my "binary state file" is obsolete and I can't restore the state of my last simulation and I have to restart the simulation from
    time 0 which is a time consuming process. I only need to specify addresses for 4 functions
    and 1 variables, the rest of the functions/variables I don't care about.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Quote Originally Posted by Valery Reznic View Post
    Could you please tell why you need data/function on specific address ?
    I have a simulation that takes a long time to run and I want to be able to save/restore the state of my simulation and continue where I left off. That means I save many of the variables some of which store pointers to function addresses. This works fine until I recompile and the linker places my functions at different addresses than when I last save out my "binary state file" and now the my "binary state file" is obsolete and I can't restore the state of my last simulation and I have to restart the simulation from time 0 which is a time consuming process. I only need to specify addresses for 4 functions and 1 variable, the rest of the functions/variables I don't care about.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    Simply re-running the code might mean things are at different addresses.
    Address space layout randomization - Wikipedia, the free encyclopedia

    What you need to store in the file is some kind of enumeration of the possible function pointer values.
    Code:
    void (*possibles)(void)[3] = {
      good,
      bad,
      ugly
    };
    int current = 1;
    possibles[current]();
    What you actually store in the file is the value stored in current, not the address of function bad.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    ASLR on Linux only affects the dynamic libraries, not the main program. If the main program was randomized then everything would need to be compiled with -fPIC.

    But I still agree with Salem -- don't save function pointers in your state file. Convert them to some enumerate type when you save and then convert them back when you load.

    My original comment about link edit commands to place functions at specific addresses was (I think) in the context of writing some systems code, and the addresses in question were actual physical addresses, not virtual addresses like you are dealing with.

    A user program would only extremely rarely need a link script.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Quote Originally Posted by pilgrim View Post
    I have a simulation that takes a long time to run and I want to be able to save/restore the state of my simulation and continue where I left off. That means I save many of the variables some of which store pointers to function addresses. This works fine until I recompile and the linker places my functions at different addresses than when I last save out my "binary state file" and now the my "binary state file" is obsolete and I can't restore the state of my last simulation and I have to restart the simulation from
    time 0 which is a time consuming process. I only need to specify addresses for 4 functions
    and 1 variables, the rest of the functions/variables I don't care about.
    You can try CryoPID. While it not works on all systems if it will works on yours it will aloow you to freeze and re-run your program.

  10. #10
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    I can already freeze and re-run my program now but if I make modifications to functions and recompile
    then I can't re-run my new program, even though all the data structures hasn't changed. If I could
    specify address for functions/variable, then I can re-run my new program as long as the data structures
    hasn't changed. I'll try to see if CryoPID will allow me to restart my program if it has changed.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21