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.
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.
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...
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????
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..
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)