C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-18-2008, 03:46 AM   #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.
raghu2383 is offline   Reply With Quote
Old 11-18-2008, 03:50 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 11-18-2008, 07:51 AM   #3
The wheel reinvent0^r
 
hauzer's Avatar
 
Join Date: Aug 2008
Location: Србија
Posts: 115
It's -e func specifically.
__________________
I reinvent the wheel to understand how it works.

Platform:
Windows XP SP2 Professional Edition
Compiler: GCC 4.3.0
Editor: Notepad++ 5.4.2
Notes: Successfully using MSYS, loving my Windows makefiles. Never, ever use Cygwin.

--Quotes--
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
hauzer is offline   Reply With Quote
Old 11-18-2008, 07:55 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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
__________________
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.

Last edited by matsp; 11-18-2008 at 08:00 AM.
matsp is offline   Reply With Quote
Old 11-18-2008, 10:05 AM   #5
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 476
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.
slingerland3g is offline   Reply With Quote
Old 11-18-2008, 10:08 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 11-18-2008, 10:18 AM   #7
Registered User
 
slingerland3g's Avatar
 
Join Date: Jan 2008
Location: Seattle
Posts: 476
For local variables that is correct. The 0804 addressess range would be the code segment.
slingerland3g is offline   Reply With Quote
Old 11-18-2008, 10:28 AM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Old 11-18-2008, 11:11 AM   #9
The wheel reinvent0^r
 
hauzer's Avatar
 
Join Date: Aug 2008
Location: Србија
Posts: 115
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.
__________________
I reinvent the wheel to understand how it works.

Platform:
Windows XP SP2 Professional Edition
Compiler: GCC 4.3.0
Editor: Notepad++ 5.4.2
Notes: Successfully using MSYS, loving my Windows makefiles. Never, ever use Cygwin.

--Quotes--
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
hauzer is offline   Reply With Quote
Old 11-18-2008, 12:40 PM   #10
Registered User
 
Join Date: Oct 2008
Location: TX
Posts: 1,262
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?
itCbitC is offline   Reply With Quote
Old 11-19-2008, 03:28 AM   #11
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
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.
matsp is offline   Reply With Quote
Reply

Tags
gcc, position dependent code

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Seg Fault in Compare Function tytelizgal C Programming 1 10-25-2008 03:06 PM
In over my head Shelnutt2 C Programming 1 07-08-2008 06:54 PM
How to fix misaligned assignment statements in the source code? biggyK C++ Programming 28 07-16-2006 11:35 PM
Compiling Errors - return makes integer..., function returns address... cproghelp C Programming 2 12-07-2004 02:31 AM
qt help Unregistered Linux Programming 1 04-20-2002 09:51 AM


All times are GMT -6. The time now is 07:28 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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