Bona Fide OS Developer
View unanswered posts | View active topics It is currently Wed Jun 20, 2018 8:22 am



Post new topic Reply to topic  [ 67 posts ]  Go to page 1, 2, 3, 4, 5 ... 7  Next
 How do i get my kernel to load binary programs? 
Author Message
Post How do i get my kernel to load binary programs?
i successfully made a real mode OS kernel and it is booting and running :D but
i cannot figure out how to load a program relocatable
the functions and data misalign depending on where the code is loaded
the question is is how do i make it so it loads binaries relocatable?


Mon Oct 05, 2009 4:47 pm

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
how are you compiling them?
elaborate on functions and data missaligning?

on the x86 most code by default is relocatable because jump instructions are relative for the most part only on special occasions does a compiler, compile using absolute addresses.

its all about SPECS my friend, SPECS!!!... what are the SPECS?? are you trying to load an EXE or COM file or soemthing else?

if you assembled a flat binary you can load it anywhere in memory as long as it starts at the begining of a segment BUT that can be changed by the [ORG] directive in the code.

You can load .COM files the same way except they begin 256 bytes away from the begining of the segment. Its as thou you specified [ORG 0100h] on the source code...

.EXEs have their little header, which is simple but I nevered bothered with because compilers I'm familiar with create .EXEs that issue DOS dependent interrupts



see http://www.delorie.com/djgpp/doc/exe/


Mon Oct 05, 2009 7:05 pm
Profile
Post Re: How do i get my kernel to load binary programs?
i am loading dos .COM programs
but how do i change the segment?
wont that make it so i cannot make kernel calls?


Mon Oct 05, 2009 8:11 pm
Post Re: How do i get my kernel to load binary programs?
here is the part that executes code in the kernel:
Code:
char kernel_execute(char *file){
kernel_print("\r\nExecuting program : ");
kernel_print(file);
char *memory;
FILE *program = fopen(file, "r");
if(program == NULL){return FALSE;}
memory = (char *)malloc(____len(program)+1);
if(memory == NULL){fclose(program); return FALSE;}
int ex;
kernel_print("\r\n");
while(!feof(program)){
kernel_print("|");
fread(&memory[ex], 1, 1, program);
ex++;
}
kernel_print("\r\n");
fclose(program);
remove_unwanted_instructions(memory);
((void (*)())memory)();
free(memory);
return TRUE;
}


Mon Oct 05, 2009 8:39 pm
Post Re: How do i get my kernel to load binary programs?
DudeOfX wrote:
how are you compiling them?


.EXEs have their little header, which is simple but I nevered bothered with because compilers I'm familiar with create .EXEs that issue DOS dependent interrupts



see http://www.delorie.com/djgpp/doc/exe/

all compilers use dos dependent interrupts but i just don't use the libraries and write my own


Mon Oct 05, 2009 9:04 pm

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
ok, firrst.... I don't think you can just treat the pointer 'memory' as a function and call it because you have to prep the registers first. COM programs assume that all segment registers equal each other (DS = ES = SS = CS) which is defined by the segment portion of pointer 'memory'

SP = 0FFFEh if you are using a stack

and if you are using a stack you would need to allocate 65278 bytes not just the file size, but this is not fatal, the stack can be less...

make sure the offset portion of the pointer 'memory' must equal 0100h because thats what .COM programs expect.

the fopen makes me a little nervous because of the "r" instead of "rb" I just had a bug once where it read the file as text.

if you are issuing a CALL instruction instead of a JMP it means your program and the kernel will share the stack. If you go this route don't change the SS and SP registers, just let them be.

the consequence of not updating the DS register and maybe the ES register is that the program would be accessing the kernel's data segments thinking its his own.


Mon Oct 05, 2009 10:05 pm
Profile
Post Re: How do i get my kernel to load binary programs?
DudeOfX wrote:
ok, firrst.... I don't think you can just treat the pointer 'memory' as a function and call it because you have to prep the registers first. COM programs assume that all segment registers equal each other (DS = ES = SS = CS) which is defined by the segment portion of pointer 'memory'

yes you can
Quote:
SP = 0FFFEh if you are using a stack

and if you are using a stack you would need to allocate 65278 bytes not just the file size, but this is not fatal, the stack can be less...

huh?
Quote:
make sure the offset portion of the pointer 'memory' must equal 0100h because thats what .COM programs expect.

how can you change that? its getting its address from malloc
Quote:
the fopen makes me a little nervous because of the "r" instead of "rb" I just had a bug once where it read the file as text.

i created the fopen myself and it is always binary
Quote:
if you are issuing a CALL instruction instead of a JMP it means your program and the kernel will share the stack. If you go this route don't change the SS and SP registers, just let them be.

typecasting makes the compiler issue a call instruction
Quote:
the consequence of not updating the DS register and maybe the ES register is that the program would be accessing the kernel's data segments thinking its his own.

and what is it suppose to point to?
sorry i am terriable with segments and data alignment


Mon Oct 05, 2009 10:29 pm
Post Re: How do i get my kernel to load binary programs?
it can run this program:
Code:
mov ax,4h
int 10h

and it goes into 320x200 graphics mode but if i do this:
Code:
#define ADDRESS_OF_KERNEL_PRINT (0x259) //Nice hardcoded address
int main(){
((void (*)(char *))ADDRESS_OF_KERNEL_PRINT)("HI!");
}

i get nothing


Mon Oct 05, 2009 10:34 pm

Joined: Sat Jul 25, 2009 9:15 am
Posts: 257
Post Re: How do i get my kernel to load binary programs?
let me see your source code for your malloc and I'll show you how to make it work...

some time today I'll try to get you a working C example of loading and executing a .COM file...


Tue Oct 06, 2009 8:37 am
Profile
Post Re: How do i get my kernel to load binary programs?
Ahm, why do you need my malloc source?


Tue Oct 06, 2009 12:19 pm
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 67 posts ]  Go to page 1, 2, 3, 4, 5 ... 7  Next


Who is online

Users browsing this forum: No registered users and 4 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by Vjacheslav Trushkin and tweaked by the BF Team.