OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Mar 29, 2024 6:02 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Kernel ELF Header - Not Present & Dynamic Linking for Mo
PostPosted: Sun Oct 29, 2017 7:13 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
I don't think you can force LD not to align sections in your file; at least I don't know if there is any command line option to do that (-z common-page-size and -z max-page-size also affect the VMA alignment).

You could try to compress the resulting ELF file; that should get rid of all padding.

_________________
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  
 
 Post subject: Re: Kernel ELF Header - Not Present & Dynamic Linking for Mo
PostPosted: Sun Oct 29, 2017 7:35 am 
Offline
Member
Member
User avatar

Joined: Fri Feb 17, 2017 4:01 pm
Posts: 641
Location: Ukraine, Bachmut
Thanks. Doing relocations is the way then. It's harder, but gives much more power for the utility.

_________________
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).


Top
 Profile  
 
 Post subject: Re: Kernel ELF Header - Not Present & Dynamic Linking for Mo
PostPosted: Mon Oct 30, 2017 9:02 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
Why do you need relocation? Don't you just need to change file offsets while leaving virtual addresses invariant? ELF executable files cannot be relocated, you would essentially need to write your own linker to do that (which is a lot of work, especially when you want to support all those GNU extensions to ELF).

_________________
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  
 
 Post subject: Re: Kernel ELF Header - Not Present & Dynamic Linking for Mo
PostPosted: Mon Oct 30, 2017 1:31 pm 
Offline
Member
Member
User avatar

Joined: Fri Feb 17, 2017 4:01 pm
Posts: 641
Location: Ukraine, Bachmut
Korona wrote:
Why do you need relocation? Don't you just need to change file offsets while leaving virtual addresses invariant? ELF executable files cannot be relocated, you would essentially need to write your own linker to do that (which is a lot of work, especially when you want to support all those GNU extensions to ELF).

The problem is in that for PE the first thing that goes at the start of an image is a set of headers, varying in size, it depends on the section count. For Elf it's a text segment, code section basically. I need to guess the size of headers when linking an elf file, and tell Elf linker put code section at my ImageBase+delta. Conceptually it's quite OK, practically it turns ugly and error prone.
Applying relocations to an Elf executable is possible. You need to tell the linker to preserve relocations. There is such an option. They don't prohibit executable be relocated, they say "typically" it has an absolute address, as well as they don't dictate "shared objects" be PIC, again - only "typically" they are. But hey, all that got plt as well as the whole dynamic linking thing in Elf is just pfff. PE mechanism for dynamic linking is so much easier.
But with the relocation capable utility, everything gets much more comfortable, you just tell your utility the desired Image Base and section alignment and it happily puts it all there (into VA space). without worrying about where the elf linker has put them. I wasted much more time trying to "guess right" Image Base in my utility analyzing the Elf input than it would take for me to learn about relocations. Applying relocations is a quite straight forward process.

_________________
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).


Top
 Profile  
 
 Post subject: Re: Kernel ELF Header - Not Present & Dynamic Linking for Mo
PostPosted: Tue Oct 31, 2017 4:37 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
I see. I didn't know GNU LD had such an option, that is quite interesting.

Yes, ELF dynamic linking via GOT and PLT is quite complicated but as a result a) lazy binding is made possible, b) the text segment is read-only, even during relocation and c) as a result of b), the text segment can truly be shared among different processes, even if the base addresses of the shared object inside these processes differ.

_________________
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  
 
 Post subject: Re: Kernel ELF Header - Not Present & Dynamic Linking for Mo
PostPosted: Thu Nov 02, 2017 3:51 pm 
Offline
Member
Member
User avatar

Joined: Fri Feb 17, 2017 4:01 pm
Posts: 641
Location: Ukraine, Bachmut
Korona wrote:
I see. I didn't know GNU LD had such an option, that is quite interesting.

from the gnu ld manual page.
Quote:
-q
--emit-relocs

Leave relocation sections and contents in fully linked executables. Post link analysis and optimization tools may need this information in order to perform correct modifications of executables. This results in larger executables.

This option is currently only supported on ELF platforms.

Quote:
Yes, ELF dynamic linking via GOT and PLT is quite complicated but as a result a) lazy binding is made possible, b) the text segment is read-only, even during relocation and c) as a result of b), the text segment can truly be shared among different processes, even if the base addresses of the shared object inside these processes differ.

PE has a notion of "delayed import" which probably serves the same purpose, but doesn't require PIC.
PE text sections among others are really read only too.
there might be some effort applied to make the same DLL have the same Image Base for different processes' images consuming them. It's easy for "system DLLs", but not impossible for any else. It's a broad area for investigating. But then "real" sharing RO ages are possible for PE images.

_________________
ANT - NT-like OS for x64 and arm64.
efify - UEFI for a couple of boards (mips and arm). suspended due to lost of all the target park boards (russians destroyed our town).


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page Previous  1, 2

All times are UTC - 6 hours


Who is online

Users browsing this forum: Majestic-12 [Bot] and 151 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