OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Sep 26, 2021 3:35 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: ELF segments vs. link_map's l_addr
PostPosted: Tue Jul 20, 2021 11:02 am 
Offline

Joined: Tue Jul 20, 2021 10:37 am
Posts: 1
Hi,

I'm wondering how ELF segments are loaded into memory. ELF Wiki sounds like each segment is mapped individually to its preferred address (p_vaddr).

But this SO answer sounds like the opposite: https://stackoverflow.com/questions/623 ... 1#62505821
Quote:
For this reason, the dynamic linker (I'll use rtld contraction for it) must perform the mmap of both segments as a single mmap


Individually mapped segments would also explain /proc/maps traces like this, where some libs are interleaved:
Code:
00:00:802    test_bla-23606/23606           7ffff7ddc000-7ffff7dfd000 r-xp 00000000 fe:01 436934                     /lib64/ld-2.19.so
00:00:802    test_bla-23606/23606           7ffff7e6b000-7ffff7e82000 r--p 00000000 fe:01 163598                     /etc/ld.so.cache
00:00:802    test_bla-23606/23606           7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0                          [vdso]
00:00:802    test_bla-23606/23606           7ffff7ffc000-7ffff7ffd000 r--p 00020000 fe:01 436934                     /lib64/ld-2.19.so
00:00:802    test_bla-23606/23606           7ffff7ffd000-7ffff7ffe000 rw-p 00021000 fe:01 436934                     /lib64/ld-2.19.so


But, assuming each segment is mapped individually, how does link_map's l_addr work then?
https://code.woboq.org/userspace/glibc/ ... ap::l_addr
Code:
    ElfW(Addr) l_addr;                /* Difference between the address in the ELF
                                   file and the addresses in memory.  */


Assuming each segment is mapped individually, wouldn't each segment need its own load-offset?


Top
 Profile  
 
 Post subject: Re: ELF segments vs. link_map's l_addr
PostPosted: Wed Jul 21, 2021 9:48 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 3357
The offset between the preferred address and the actual address has to be the same for all segments in the file.


Top
 Profile  
 
 Post subject: Re: ELF segments vs. link_map's l_addr
PostPosted: Wed Jul 21, 2021 12:21 pm 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 973
The SO answer is wrong, you can use MAP_FIXED to map segments individually. For example, you could first reserve a range of virtual memory by mapping MAP_ANONYMOUS memory with PROT_NONE, and then overwrite the appropriate parts of that reservation using MAP_FIXED.

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: ovohasabolepi and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group