Thread: Find the offset

  1. #1
    Registered User PrimeSec's Avatar
    Join Date
    Sep 2013
    Posts
    4

    Find the offset

    Hi to all. I am new at the forum and exuse me if i make something wrong!!
    I would to learn how could i calculate the offset of simple c program?
    Lets say tha i have that simple program:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int x=1;
        printf("x = %d", x);
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Did you want the offset of main or the offset of x? To get the offset of main(), declare a pointer to function and set it to main(), but note this could be a pointer to a jump to main, if in debugger mode on some compilers. If it's a jump, you'd need to check for that and adjust the offset to point to the actual instance of main(). It could also be an jump via an index to an array of pointers to functions, which will require additional logic to determine where main() is actually located.

    For the offset of x, you use &x.
    Last edited by rcgldr; 09-28-2013 at 12:36 PM.

  3. #3
    Registered User PrimeSec's Avatar
    Join Date
    Sep 2013
    Posts
    4
    Could i get the whole programs offset??

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You might try objdump -f or something like that.
    Adding
    Code:
    printf("main: 0x%p\n", main);
    may show you the address of main, although note what rcgldr said.

    What OS are you on?
    Why do you want to do this?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by PrimeSec View Post
    Could i get the whole programs offset??
    You'd have to use OS specific functions to obtain the memory map for a process. Windows Vista and later version of Windows randomize the offsets of program and static data somewhat, in an effort to make it more difficult to use buffer overflow hacks on programs.

  6. #6
    Registered User PrimeSec's Avatar
    Join Date
    Sep 2013
    Posts
    4
    I am using linux. I want to know how many bytes will cost me in memory!!

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Try objdump -p yourprog.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Offset from what?!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I'm sorry, but this is a silly question.

    The layout of a program (and data it uses) in memory is system dependent. Some systems go out of their way to randomise the layout somewhat (in the sense that some elements of layout differ each time the program runs) for security reasons (make it difficult to exploit knowledge of memory layout for nefarious means). With a lot of systems, the layout is further adapted based on available memory resources.

    You can estimate the minimum memory usage of a variable using sizeof(). That gives the amount of memory used by your program, in the sense that it can be detected by standard-conformant code. However, the compiler and host system can do things to change memory usage (for example, optimise away the existence of a variable, organise variables into pages, etc etc). There is also a certain amount of memory usage of real world programs based on executable instructions since - after all - a variable is rarely of any use unless things are done with it, its value is accessed, etc etc.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #10
    Registered User PrimeSec's Avatar
    Join Date
    Sep 2013
    Posts
    4
    Quote Originally Posted by oogabooga View Post
    You might try objdump -f or something like that.
    Adding
    Code:
    printf("main: 0x%p\n", main);
    That was great!!! It showed me my programs start address in memory
    Is there a way to get the memory where the program ends?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using Offset in C
    By nickman in forum C Programming
    Replies: 1
    Last Post: 11-15-2011, 12:52 PM
  2. Problem with Offset
    By N3utral in forum C++ Programming
    Replies: 0
    Last Post: 03-23-2010, 10:53 AM
  3. help! (int*)+offset
    By RobotGymnast in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2008, 01:12 PM
  4. offset
    By Rhidian in forum C Programming
    Replies: 6
    Last Post: 04-14-2005, 08:57 AM
  5. What exactly is an 'offset' to something?
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2002, 10:28 PM

Tags for this Thread