gcc linker address


 
Thread Tools Search this Thread
Top Forums Programming gcc linker address
# 1  
Old 01-18-2011
gcc linker address

Hai,

I have two (Pgm1.c and Pgm2.c) simple programs, which is compiled using gcc. Now we have two exe's (Pgm1 and Pgm2). When i executed the nm Pgm1 and nm Pgm2, in the listed symbols the address of main is same for both programs (08048344 T main) at run time also.

Doubt:

1) What is this address (08048344 T main) ?.
2) If both the address are same how it is loaded into memory?
3) Whether gcc will generate the same address for all programs.
# 2  
Old 01-18-2011
main's location is probably a coincidence from main being the first(or maybe even only?) function in your code. It doesn't need to be at anywhere in particular.

There is a tiny bit of code that's at a fixed location. When the executable loads, it doesn't call any functions -- it just jumps immediately to 0x08048000(for linux x86 anyway, won't speak for other architectures) and begins executing whatever's there. Which in 99.9% of cases is going to be a tiny bit of hand-crafted assembly language code. It does a little bit of setup before calling your main() function, then calls exit() after main returns.
Code:
$ readelf --syms /usr/lib/crt1.o

Symbol table '.symtab' contains 18 entries:
   Num:    Value  Size Type    Bind   Vis      Ndx Name
     0: 00000000     0 NOTYPE  LOCAL  DEFAULT  UND
     1: 00000000     0 SECTION LOCAL  DEFAULT    1
     2: 00000000     0 SECTION LOCAL  DEFAULT    2
     3: 00000000     0 SECTION LOCAL  DEFAULT    4
     4: 00000000     0 SECTION LOCAL  DEFAULT    5
     5: 00000000     0 SECTION LOCAL  DEFAULT    6
     6: 00000000     0 SECTION LOCAL  DEFAULT    7
     7: 00000000     0 SECTION LOCAL  DEFAULT    8
     8: 00000000     0 SECTION LOCAL  DEFAULT    9
     9: 00000000     4 OBJECT  GLOBAL DEFAULT    4 _fp_hw
    10: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND __libc_csu_fini
    11: 00000000     0 FUNC    GLOBAL DEFAULT    2 _start
    12: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND __libc_csu_init
    13: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND main
    14: 00000000     0 NOTYPE  WEAK   DEFAULT    6 data_start
    15: 00000000     4 OBJECT  GLOBAL DEFAULT    5 _IO_stdin_used
    16: 00000000     0 NOTYPE  GLOBAL DEFAULT  UND __libc_start_main
    17: 00000000     0 NOTYPE  GLOBAL DEFAULT    6 __data_start
$

Note the 'UND main', the library doesn't actually supply a main, just demands that one exists. It also doesn't seem to actually define the start point here, that's decided later and probably adjustable. But by tradition is kept at 0x08048000 for linux x86. I don't know why it doesn't link _exit, maybe it's doing a raw system call.

As for why these things are always kept at the same place, that's the beauty of virtual memory. Instead of having to torture your code into running from any possible location and working around whatever holes are left in global memory, the memory layout itself is adjustable. Each process gets a private little universe with the same startup location and memory layout as everything else. Any x86 processor since the 80386 can do this memory translation in hardware. The kernel's left in charge of assigning what memory truly belongs to each process...


You may find A Whirlwind Tutorial on Creating Really Teensy ELF Executables for Linux an illuminating description of the really low-level inner workings of an executable.

Last edited by Corona688; 01-18-2011 at 11:32 AM..
These 2 Users Gave Thanks to Corona688 For This Post:
# 3  
Old 01-21-2011
I, however, really couldn't figure out this 0x08048000 location in the realelf output of your example???

Mine on a Linux machine, an executable gave me main's location as follows:

000000000040069a 82 FUNC GLOBAL DEFAULT 12 main

However, this is a 64 bit address; so do we assume that main, in any elf binary over 64-bit x86 architecture, would have an start address as 000000000040069a; where as in a 32-bi the same was at 0x08048000.

Shoul'd the same been 0x0000000008048000 over a 64-bit system????
# 4  
Old 01-21-2011
Quote:
Originally Posted by Praveen_218
I, however, really couldn't figure out this 0x08048000 location in the realelf output of your example???
I think that's because crt1.o is a library. That stuff gets decided when the final executable is linked and not before.
Quote:
000000000040069a 82 FUNC GLOBAL DEFAULT 12 main
That's because:
Quote:
Originally Posted by Corona688
main's location ... doesn't need to be at anywhere in particular.

There is a tiny bit of code that's at a fixed location. When the executable loads, it doesn't call any functions -- it just jumps immediately to 0x08048000(for linux x86 anyway, won't speak for other architectures) and begins executing whatever's there.
The bit that ends up at the start location is called _start.

The start location might have been even less fixed than I thought, too. 0x08048000 is just where linux x86 begins loading code for x86, not where _start has to be.

Last edited by Corona688; 01-21-2011 at 11:15 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Gcc linker search path order

I have a build where I wish to link against and load a specific version of a library and a different version of the same library is installed on the system. I'm using a -L option to point to the version that I wish to link against but gcc still seems to choose the installed version. Is there a way... (4 Replies)
Discussion started by: Richard Johnson
4 Replies

2. Solaris

runtime linker errors

Hey guys I have a solaris 10 OS, with a zone configured. In that zone, I am trying to install an Oracle Client. However, when I run the oracle installer, I get the following error: ld.so.1: java: fatal: libexpat.so.0: open failed: No such file or directory ld.so.1: java: fatal:... (2 Replies)
Discussion started by: goodvikings
2 Replies

3. Programming

AIX linker oddities...what is going on?

I've tried to figure out what the linker is smoking in AIX to no avail...so I'm trying to find a little information to see why it's being inconsistent. I have the following code in a shared library, it doesn't do anything useful, it's just there to exercise functions in a few system libraries: ... (11 Replies)
Discussion started by: DreamWarrior
11 Replies

4. Programming

GCC: Choose my own linker

Hi, I do not use the default linker, and instead us another one and pass this argument -Wl,--dynamic-linker=<path to linker> to gcc when compiling. However, what happens if the linker is not under /lib and /lib64 and I am not able to create a symlink to the linker in /lib or /lib64 due to no... (2 Replies)
Discussion started by: Shompis
2 Replies

5. HP-UX

Hp-ux linker option -c

Hi, The necessary symbols in a shared library can be exported to the application using linker option --version-script in Linux. The same can be done in Hp-ux using linker option +e. This can also be done by listing all the global symbols with +e in a file with linker option ld -c filename in... (0 Replies)
Discussion started by: roopa_v
0 Replies

6. Solaris

gcc & linker question (Solaris 10)

Hi, I'm confused about the GCC & Linking process could somebody please briefly explain? I have GCC 3.4.6 installed from SunFreeware. Reading the narrative alongside the package states "It was compiled to use the SUN assembler and loader usually in /usr/ccs/bin if the SUNW developer packages... (1 Reply)
Discussion started by: Celtic_Monkey
1 Replies

7. Solaris

Installing gcc - recieve error message gcc : cannot execute

AIM- Install Oracle 11g on Solaris using VMWare Steps 1.Logged on as root 2.Created subfolders à /usr/local/bin & /usr/local/bin/gcc 3.Downloaded gcc & libiconv & unzipped them on my harddrive & burnt them on CD 4.Copied files from CD to /usr/local/bin/gcc 5.Terminal (root) à pkgadd -d... (8 Replies)
Discussion started by: Ackers
8 Replies

8. Programming

XWindows programming + gcc linker options

Im trying to compile some code that involves using X-Windows based functions (XDisplayName() and XOpenDisplay() ), however each time I run the compiler, Im getting a problem with the linker. The linker says that both XDisplayName() and XOpenDisplay() are unresolved text symbols. Is there a... (1 Reply)
Discussion started by: JamesGoh
1 Replies

9. Linux

G++ Linker Error

Hi All, I m new to this group. I m facing one problem during my linking of CPP code at Linux env. after compliation i m getting error "undefined reference to ....." Please anyone help me to resolve this error. Regards, ASR make: Leaving directory /fwk' echo g++ -o server ... (1 Reply)
Discussion started by: ASRRAJ
1 Replies

10. UNIX for Dummies Questions & Answers

Linker (ld)

hai I have installed Linux 7.0 on my system and i have an doubt the linker i ran simple hello.c program with gcc compiler gcc -c hello.c i want linker to produce the output so i put on the command line as ld first.o -lc but it is not running ver properly but there is an... (1 Reply)
Discussion started by: rajashekaran
1 Replies
Login or Register to Ask a Question