ELF question

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

ELF question

Post by osdever »

Should I load into the memory something more than the segments that are specified in program header? I don't use paging for now, ELFs can corrupt my memory, but I just want to make them execute normally and not trigger GPF when anything such as the variables or functions are used. So should I load something more than the program segments?

P.S. This post definitely has grammar mistakes. I should work on my English...
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Re: ELF question

Post by Korona »

If the ELF file is an executable without PT_INTERP and PT_DYNAMIC it is enough to just load the program headers. If it has a PT_INTERP/PT_DYNAMIC segment you need a dynamic linker that fixes the GOT, performs load-time relocations and calls library initialization functions.

Note that you need a different linker script for each executable if you do not use paging which is quite ugly and inflexible.
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].
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: ELF question

Post by bzt »

Yes. You should load all segments marked as PT_LOAD (so not all program headers), and also don't forget about bss. You don't have to load it from the file, but you'll have to allocate memory for it and fill it with zeros.

Other than that I agree with Korona, you should first implement paging (it can be a headache at first glance, but later on it will pay out!)
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: ELF question

Post by Boris »

if you need to load elfs before paging, why not. Beware of two little things:
- Be sure you won't override used/non free physical ram. Yes, grub doesn't that. you can load your kernel at 0xB8000 and literally see what happens.
- When you activate paging, relocations you have you did before will be broken. I don't suggest you running your elf code after .
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: ELF question

Post by xenos »

Boris wrote:Yes, grub doesn't that. you can load your kernel at 0xB8000 and literally see what happens.
Does GRUB really load anything under the 1MB barrier? I thought it would throw some error in that case. I haven't tried it, though.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: ELF question

Post by Schol-R-LEA »

bzt wrote:don't forget about bss. You don't have to load it from the file, but you'll have to allocate memory for it and fill it with zeros.
For that matter, don't forget that the .data and .rodata sections are 'program' as well, in addition to the .text and .bss sections. I am assuming you are loading those, but a reminder doesn't hurt.

Needless to say, you don't need to load .comment and .note sections, and you only need to work with .stab and .stabstr for debugging or other things that need to map the symbols to the addresses at runtime. Whether you use the .eh_frame, if any, will depend on how your OS handles unwinding the stack under certain conditions, but unless you are running a profiler or your OS swizzles userland stacks for some other purposes, you probably don't need to load it either.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Boris
Member
Member
Posts: 145
Joined: Sat Nov 07, 2015 3:12 pm

Re: ELF question

Post by Boris »

XenOS wrote:
Boris wrote:Yes, grub doesn't that. you can load your kernel at 0xB8000 and literally see what happens.
Does GRUB really load anything under the 1MB barrier? I thought it would throw some error in that case. I haven't tried it, though.
Ha, true . It was not grub, but it was multiboot of qemu !
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: ELF question

Post by osdever »

Schol-R-LEA wrote:
bzt wrote:don't forget about bss. You don't have to load it from the file, but you'll have to allocate memory for it and fill it with zeros.
For that matter, don't forget that the .data and .rodata sections are 'program' as well, in addition to the .text and .bss sections. I am assuming you are loading those, but a reminder doesn't hurt.

Needless to say, you don't need to load .comment and .note sections, and you only need to work with .stab and .stabstr for debugging or other things that need to map the symbols to the addresses at runtime. Whether you use the .eh_frame, if any, will depend on how your OS handles unwinding the stack under certain conditions, but unless you are running a profiler or your OS swizzles userland stacks for some other purposes, you probably don't need to load it either.
Thanks. I will try to fix it :)
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
User avatar
osdever
Member
Member
Posts: 492
Joined: Fri Apr 03, 2015 9:41 am
Contact:

Re: ELF question

Post by osdever »

bzt wrote:Yes. You should load all segments marked as PT_LOAD (so not all program headers), and also don't forget about bss. You don't have to load it from the file, but you'll have to allocate memory for it and fill it with zeros.

Other than that I agree with Korona, you should first implement paging (it can be a headache at first glance, but later on it will pay out!)
I know that I should load only PT_LOAD ones.
Developing U365.
Source:
only testing: http://gitlab.com/bps-projs/U365/tree/testing

OSDev newbies can copy any code from my repositories, just leave a notice that this code was written by U365 development team, not by you.
billcu
Posts: 17
Joined: Tue Aug 26, 2014 9:12 pm

Re: ELF question

Post by billcu »

Boris wrote:if you need to load elfs before paging, why not. Beware of two little things:
- Be sure you won't override used/non free physical ram. Yes, grub doesn't that. you can load your kernel at 0xB8000 and literally see what happens.
- When you activate paging, relocations you have you did before will be broken. I don't suggest you running your elf code after .
How do you get grub2 to do this? I am assuming you mean it rather than legacy. Can you boot something like MS-DOS, than wants the first sector?
Post Reply