Thread: Local variable address in memory

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    6

    Local variable address in memory

    For a homework assignment I have been given some c files, and compiled them using arm-linux-gcc (we will eventually be targeting gumstix boards, but for these exercises we have been working with qemu and ema). One of the questions confuses me a bit-- we are told to "..use arm-linux-objdump to find the location of variables declared in main() in the executable binary." However, these variables are local and thus shouldn't have addresses until runtime, correct? I'm thinking that maybe what I need to find is the offset in the stack frame, which can in fact be found using objdump (not that I know how). Anyways, any insight into the matter would be greatly appreciated, and I would be happy to post the source code if necessary.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Unless the local variables are declared "static," they will not have symbols in the object file. You are correct in thinking that they do not get addresses until runtime, and then it depends on where the stack is located. Even if the location of the stack is fixed, there is no guarantee that the variables will be in any specific location -- if there is a recursive function, there may even be multiple copies of these variables on the stack. So the address of a local variable, unless it is static, cannot be determined until runtime.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    Thank you brewbuck, this is indeed what I suspected. The function doesn't utilize any recursion, and for the purposes of the assignment I think it is safe to assume the program will only be run once (I imagine multiple instances might lead to further confusion). This being the case, would you be able to provide insight into where in the code these variables are actually getting there memory? The question asks for there location (in hex), but I'm fairly certain my response should be the offset from the stack pointer. This stuff is somewhat over my head in terms of understanding, and I've currently been digging around rather blindly through the output from arm-linux-objdump -D a.out. I've also been hunting around with objdump -S to get a better idea of what the source code is actually doing, but I can't seem to find the information I need. I would equally appreciate any good links; a lot of my searching for objdump has yielded really high-level stuff...well, high-level relative to my expertise

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    What exactly is the wording of the assignment?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you're meant to look at the generated assembler (using objdump) to work out the call stack frames.
    Stack Frame
    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.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    6
    We are given source code and the makefile (which we must modify to compile for arm-linux- ). The exact wording of the problem is "Use arm-linux-objdump to find the addresses of the variables MAXSIZE and RealIn (declared in main()) in the binary code."

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps the answer is "local variables don't have addresses"
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: address of local variable ‘f’ returned
    By dayalsoap in forum C++ Programming
    Replies: 9
    Last Post: 07-20-2011, 10:51 PM
  2. Replies: 4
    Last Post: 07-11-2011, 10:50 AM
  3. Replies: 5
    Last Post: 08-14-2009, 03:15 AM
  4. Returning the Address of a Local Variable (Array)
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 08-20-2008, 02:03 AM
  5. Error - function returns address of local variable
    By tltying in forum C Programming
    Replies: 5
    Last Post: 05-28-2007, 02:26 AM

Tags for this Thread