Bona Fide OS Development
http://forums.osdever.net/

How do i get my kernel to load binary programs?
http://forums.osdever.net/viewtopic.php?f=6&t=36
Page 1 of 7

Author:  smeezekitty [ Mon Oct 05, 2009 4:47 pm ]
Post subject:  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?

Author:  DudeOfX [ Mon Oct 05, 2009 7:05 pm ]
Post subject:  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/

Author:  smeezekitty [ Mon Oct 05, 2009 8:11 pm ]
Post subject:  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?

Author:  smeezekitty [ Mon Oct 05, 2009 8:39 pm ]
Post subject:  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;
}

Author:  smeezekitty [ Mon Oct 05, 2009 9:04 pm ]
Post subject:  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

Author:  DudeOfX [ Mon Oct 05, 2009 10:05 pm ]
Post subject:  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.

Author:  smeezekitty [ Mon Oct 05, 2009 10:29 pm ]
Post subject:  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

Author:  smeezekitty [ Mon Oct 05, 2009 10:34 pm ]
Post subject:  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

Author:  DudeOfX [ Tue Oct 06, 2009 8:37 am ]
Post subject:  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...

Author:  smeezekitty [ Tue Oct 06, 2009 12:19 pm ]
Post subject:  Re: How do i get my kernel to load binary programs?

Ahm, why do you need my malloc source?

Page 1 of 7 All times are UTC - 6 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/