Compiled program memory addresses c

I am trying to understand the following:

Given the little Hello World program in C

#include <stdio.h> int main() { int i; for(i=0; i < 10; i++) { printf("Hello, world!\n"); } } 

When you compile this with gcc and then examine the resulting .out file with objdump, you will get something like the following:

 08048374 <main>: 8048374: 55 push ebp 8048375: 89 e5 mov ebp,esp 8048377: 83 ec 08 sub esp,0x8 804837a: 83 e4 f0 and esp,0xfffffff0 804837d: b8 00 00 00 00 mov eax,0x0 8048382: 29 c4 sub esp,eax 8048384: c7 45 fc 00 00 00 00 mov DWORD PTR [ebp-4],0x0 804838b: 83 7d fc 09 cmp DWORD PTR [ebp-4],0x9 804838f: 7e 02 jle 8048393 <main+0x1f> 8048391: eb 13 jmp 80483a6 <main+0x32> 8048393: c7 04 24 84 84 04 08 mov DWORD PTR [esp],0x8048484 804839a: e8 01 ff ff ff call 80482a0 < printf@plt > 804839f: 8d 45 fc lea eax,[ebp-4] 80483a2: ff 00 inc DWORD PTR [eax] 80483a4: eb e5 jmp 804838b <main+0x17> 80483a6: c9 leave 80483a7: c3 ret 80483a8: 90 nop 80483a9: 90 nop 80483aa: 90 nop 

The first column of values ​​in the resulting .out file are memory addresses, if I understand correctly, these addresses contain instructions that follow in other columns.

Now my question is: if you copy the file to another computer (or another location on the same computer) and download the file again, should these addresses change to something else, because the program will be in a different place in memory, correct? But if I do this, I get the same result, the same address values. Why is this? I clearly misunderstand the meaning of this first column, can someone please explain to me what exactly these addresses are? Thanks in advance!

Update: As I understand it, now, thanks to Paul R's answer and some reading on wikipedia, these addresses refer to the virtual address space in which the code is executed by the operating system of the machine on which it works. These virtual addresses are mapped to absolute addresses on the computer itself by the operating system.

+4
source share
2 answers

The addresses in the left column are the (virtual) addresses to which your code will be loaded when it starts. If the code is position-independent , it must be loaded at these addresses for it to work properly.

+6
source

Each process on a 32-bit OS runs in its own 4 GB memory area. This area is shared between the kernel and your process, usually in the form of 3 GB / 1 GB shared memory, where an application with lower 3 GB of memory, starting at 0x00000000, is used, while the upper 1 GB is used by the kernel.

Now, if we look at the lower area of ​​the user space of 3 GB of the application, the area will be further divided into different segments, such as the text segment, the initialized data segment, the uninitialized data segment, etc.

So, the code you write is placed in this text area, which usually starts with 08048374 in your example.

Therefore, all assembly code is placed in this virtual address, regardless of any machine that you use to start it, since this is predefined at the build stage. Therefore, this address will not change. Hope this helps.

+1
source

All Articles