Thread: gcc: Speify address for a function while compiling

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    19

    gcc: Speify address for a function while compiling

    Hi,

    I am working on security topics, and I need to write a C file that has
    only one function in it (without the main routine). I need to compile
    it.
    Normally the compilation commands for it would be gcc -c blank.c -o
    blank.o This crates a .o file starting at address 0 with just the single
    routine inside the .o file. My question is whether I can make the
    routine be given a specific address, say for example 8048000 instead of
    starting at location 0.
    Thanks in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can do that by setting the right parameters for "ld" when you link it to form an executable. gcc doesn't have any clue about absolute addresses in the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    It's -e func specifically.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by hauzer View Post
    It's -e func specifically.
    That tells the linker you want to start in func, but not what the original post asked for, which is that the code should be located at a particular address.

    Edit: To locate the code at a specific address, "ld -Ttext org ... " is the command to use, where org is the origin. I'm not sure if you can actually specify this to any byte address - I'm pretty sure that the OS loads a page at a time, so even if it's a precise address, "funny things" may happen with an address that is not precisely on a 4KB boundary.


    --
    Mats
    Last edited by matsp; 11-18-2008 at 08:00 AM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    8048000 is the start of most Linux distros for the stack. I would not hard compile that either as that would not be portable. You can also see this within your /proc dirctory as well during alive session of the program or within gdb for stack analysis. Funny that the book 'Self-Service Linux' talks about this a bit.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by slingerland3g View Post
    8048000 is the start of most Linux distros for the stack. I would not hard compile that either as that would not be portable. You can also see this within your /proc dirctory as well during alive session of the program or within gdb for stack analysis. Funny that the book 'Self-Service Linux' talks about this a bit.
    Really. Most Linux processes I've looked at have the stack around 0xBFFF0000.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    For local variables that is correct. The 0804 addressess range would be the code segment.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by slingerland3g View Post
    For local variables that is correct. The 0804 addressess range would be the code segment.
    Right, that makes more sense.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Quote Originally Posted by matsp View Post
    That tells the linker you want to start in func, but not what the original post asked for, which is that the code should be located at a particular address.

    --
    Mats
    Ah, yes.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by raghu2383 View Post
    Hi,

    I am working on security topics, and I need to write a C file that has
    only one function in it (without the main routine). I need to compile
    it.
    Normally the compilation commands for it would be gcc -c blank.c -o
    blank.o This crates a .o file starting at address 0 with just the single
    routine inside the .o file. My question is whether I can make the
    routine be given a specific address, say for example 8048000 instead of
    starting at location 0.
    Thanks in advance.
    Why would you want to do something like that in the first place?

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by itCbitC View Post
    Why would you want to do something like that in the first place?
    Usually to inject code into an existing application, I would say. Whether that is allowed to be discussed on this forum is another interesting aspect.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Replies: 2
    Last Post: 12-07-2004, 02:31 AM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

Tags for this Thread