Page 1 of 1
ELF question
Posted: Wed Jan 18, 2017 10:45 am
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...
Re: ELF question
Posted: Wed Jan 18, 2017 10:58 am
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.
Re: ELF question
Posted: Wed Jan 18, 2017 11:28 am
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!)
Re: ELF question
Posted: Wed Jan 18, 2017 11:47 am
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 .
Re: ELF question
Posted: Wed Jan 18, 2017 12:02 pm
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.
Re: ELF question
Posted: Wed Jan 18, 2017 1:38 pm
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.
Re: ELF question
Posted: Wed Jan 18, 2017 1:51 pm
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 !
Re: ELF question
Posted: Thu Jan 19, 2017 2:34 am
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
Re: ELF question
Posted: Thu Jan 19, 2017 2:37 am
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.
Re: ELF question
Posted: Sat Jan 21, 2017 3:22 pm
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?