LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-11-2021, 03:35 PM   #1
smegmaLord
LQ Newbie
 
Registered: Mar 2021
Posts: 16

Rep: Reputation: Disabled
Absolute Adresses in compiled program


I have a question thats been bothering me for a while.

We have been taught at uni that after a compilation ( so even .o file) there is
assembly instructions for certain processor.
Also: the adresses of the instructions should be absolute? I am not talking about dynamic libraries or stub as those are inserted during program loading or during execution.
But how come the instruction adresses can be absolute? What happens if on that adress there is a different code? I feel like all programs should be PID

It would be of great help if some of you would answer my question
 
Old 12-11-2021, 05:51 PM   #2
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,340

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
The addresses referred to are the location within the compiled code of functions, etc. In other words once the program is loaded into memory those specific instructions are at a fixed offset from the beginning of the code. How else do you think the program would be able to access its own instructions and know where they were located?
What do you think would happen if the code were to be relocated to a different address while the app was running?
 
Old 12-11-2021, 06:00 PM   #3
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 996

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
The details depend on the CPU architecture. All architectures can specify absolute addresses. Most can specify addresses relative to a base address (offsets may be limited to 8, 16, or 32-bits). The linker and loader can relocate absolute addresses as needed to make an executable run at a specific address.
Ed
 
Old 12-11-2021, 07:20 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
As the above answers indicate, in user-space all absolute addresses are relative ...

Virtual-this, virtual-that ... enough to make your head spin.
 
Old 12-12-2021, 05:07 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,778

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
yes, every process has its own assigned memory and the address (absolute or relative) is valid only inside.
https://www.kernel.org/doc/html/late.../mm/index.html
 
Old 12-12-2021, 06:37 AM   #6
smegmaLord
LQ Newbie
 
Registered: Mar 2021
Posts: 16

Original Poster
Rep: Reputation: Disabled
First of all. Thank you all for such a quick answer!

Ok, so the adresses are virtual so it is actually relative.

So whats the difference between PIC program and normally compiled code?
Because the way you guys have answered seems like every program is PIC
 
Old 12-12-2021, 08:10 AM   #7
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,555
Blog Entries: 19

Rep: Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444
The addresses created by the compiler are virtual. These are then translated on a page by page basis into physical memory addresses. There are translation tables inside the kernel that do the page mapping. But as far as the program code is concerned, the virtual addresses are perfectly real.

PIC, as used by libraries, is different in that these addresses are relative even by the program's own standards. It must be so because a particular library function could be called several times by different parts of the program (or indeed by different programs) and has to run the same way every time.
 
Old 12-12-2021, 08:47 AM   #8
smegmaLord
LQ Newbie
 
Registered: Mar 2021
Posts: 16

Original Poster
Rep: Reputation: Disabled
aaaaaah, okey.

One more thing .

so lets say one program's code virt. addr. is mapped to phys. addr. and then to memory.

But if I try to load another program with similar(or same) virt. address as the first one then the mapping tables should see these virtual addresses are already mapped right? So there might be some kind of collision. Or do the mapping tables have some workaround when 2 processes have the same virt memory?
 
Old 12-12-2021, 09:02 AM   #9
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,555
Blog Entries: 19

Rep: Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444Reputation: 4444
Quote:
Originally Posted by smegmaLord View Post
so lets say one program's code virt. addr. is mapped to phys. addr. and then to memory.

But if I try to load another program with similar(or same) virt. address as the first one then the mapping tables should see these virtual addresses are already mapped right?
Exactly! If a page is already mapped (say because it's a function in a common library and another program is running it concurrently), then the virtual page will be mapped directly to that physical page. So one physical page might correspond to several virtual pages in different programs.
Quote:
So there might be some kind of collision. Or do the mapping tables have some workaround when 2 processes have the same virt memory?
For every type of page, there are kernel functions that handle "page faults", such as not finding the virtual page in the translation table. If the page is code, the handler function for such a fault would be to check whether the content of the page is already in memory as part of another program. If so, there's no need to copy it a second time. A translation table entry can be created for the virtual page pointing to the existing physical memory page.
 
Old 12-12-2021, 11:03 AM   #10
smegmaLord
LQ Newbie
 
Registered: Mar 2021
Posts: 16

Original Poster
Rep: Reputation: Disabled
Ok!

But that means that the 2 codes will have the same virtual memory if and only if the code is the same( or it is some library function) but how would compiler know such information?
 
Old 12-12-2021, 11:22 AM   #11
EdGr
Member
 
Registered: Dec 2010
Location: California, USA
Distribution: I run my own OS
Posts: 996

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
In general, shared objects get loaded at different virtual addresses because the loader does not control a processes' virtual address map. The goal is for the kernel to map all instances of a shared object to the same physical pages so that only one copy resides in memory. To do this, the shared object should be compiled with -fPIC. The compiler emits extra instructions (if needed) to make the code run at any virtual address.
Ed
 
1 members found this post helpful.
Old 12-12-2021, 02:52 PM   #12
smegmaLord
LQ Newbie
 
Registered: Mar 2021
Posts: 16

Original Poster
Rep: Reputation: Disabled
Ok. Thank you very much!
 
Old 12-13-2021, 04:04 PM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,632
Blog Entries: 4

Rep: Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931Reputation: 3931
Unfortunately, this discussion wandered almost immediately into error, apparently based on the ambiguous term, "absolute address."

When the compiler generates an ".o" file, that file contains not only binary instructions, but relocation information that will be needed to adjust memory locations within the loaded program image in order to account for where in memory it was loaded. The file also contains a list of symbols defined by this file, and a list of symbols that are needed. The so-called "loader" is responsible for bringing all of the required materials into memory, thus deciding their address within the [virtual ...] memory space, and patching-up all the memory addresses everywhere.

It is possible for a symbol to be defined as equal to a fixed address-value, as opposed to being relative to the program unit in which it is defined. This facility is very rarely used.

However, nothing allows you to know anything about "absolute" as in "within physical CPU RAM." All programs run in a virtual memory space and perceive "memory" to be that space ... which it is, for them. Meanwhile, the virtual memory subsystem maps a portion of this (the portion that is currently being used) to physical RAM addresses – which are subject to change at any time. Only the operating system kernel knows what this mapping is at any particular microsecond.

In fact, the virtual memory address that you next reference might not be in physical RAM at all. This will produce a "page fault" interrupt. The operating system will then retrieve the "page" from backing storage (disk) where it had previously saved it, and it is free to put it anywhere in physical memory – any so-called "page frame."

Last edited by sundialsvcs; 12-14-2021 at 08:52 AM.
 
Old 12-14-2021, 01:49 AM   #14
smegmaLord
LQ Newbie
 
Registered: Mar 2021
Posts: 16

Original Poster
Rep: Reputation: Disabled
Ah cool.
Thank you!

And how does PIC play a role in this?
 
Old 12-14-2021, 08:46 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,778

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
PIC means Position Independent Code, with other words it is relocatable.
In general it is explained here: https://en.wikipedia.org/wiki/Position-independent_code
In sort: non-relocatable code have a fixed address inside the memory of the process.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"Unknown symbol __fentry__ (err 0)" - loading compiled module on compiled kernel 3.18 Jason_25 Linux - Kernel 1 12-25-2014 05:45 AM
Compiled program not showing up as compiled and not able to run. hjoshuaj Linux - Newbie 3 03-27-2014 05:49 AM
LXer: Absolute Linux is an absolute winner LXer Syndicated Linux News 0 08-07-2007 06:32 PM
iptables and dynamic ip adresses (use of a dyndns service) markus1982 Linux - Security 6 09-02-2004 09:27 AM
How do I forward IP Adresses? teeno Linux - Networking 2 07-16-2001 09:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration