works with aout not with elf why
works with aout not with elf why
hello
i was studying various tutorials and other articles and books regarding os development. closely following mr xsism's xosdev chapter1 and 2 and some others i built a boot loader and a hello world kernel. the bootloader sets a20, pm,gdt copies kernel to 0x100000 and jumps to it.
after writing the programs i used the following commands
nasm -f aout kernel_start.asm -o ks.o
gcc -c kernel.c -o kernel.o
ld -T link.ld -o kernel.bin ks.o kernel.o
nasm -f bin bootloader.asm -o bootloader.bin
cat bootloader.bin kernel.bin > os.img
dd if=os.img of=/dev/fd0
bochs -q
but it i gave command
nasm -f elf kernel_start.asm -o ks.o
then when i run ld the output is a big file.
and the os doesnt work
whats the reason?
when i referred to a tutorial written by joachin bock and KJ on Making a simple kernel with basic printf
he had also used nasm -f aout.
so whats the reason why not elf.
Kiran
i was studying various tutorials and other articles and books regarding os development. closely following mr xsism's xosdev chapter1 and 2 and some others i built a boot loader and a hello world kernel. the bootloader sets a20, pm,gdt copies kernel to 0x100000 and jumps to it.
after writing the programs i used the following commands
nasm -f aout kernel_start.asm -o ks.o
gcc -c kernel.c -o kernel.o
ld -T link.ld -o kernel.bin ks.o kernel.o
nasm -f bin bootloader.asm -o bootloader.bin
cat bootloader.bin kernel.bin > os.img
dd if=os.img of=/dev/fd0
bochs -q
but it i gave command
nasm -f elf kernel_start.asm -o ks.o
then when i run ld the output is a big file.
and the os doesnt work
whats the reason?
when i referred to a tutorial written by joachin bock and KJ on Making a simple kernel with basic printf
he had also used nasm -f aout.
so whats the reason why not elf.
Kiran
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:works with aout not with elf why
i fail to see a link.ld file in those tutorial .
If you could post the output of "objdump -x kernel_start.o", it could help me pointing at what could go wrong.
If you could post the output of "objdump -x kernel_start.o", it could help me pointing at what could go wrong.
Re:works with aout not with elf why
aout format is only useful if you want to mix 16 and 32 bit code else its obsolete.
.Look at http://my.execpc.com/~geezer/osd/exec/ for that.so whats the reason why not elf
Re:works with aout not with elf why
if you do the same compilation proccess on windows with mingw you will get error "PE operations on non PE files"
although it has nothing to do with PE binary.
although it has nothing to do with PE binary.
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:works with aout not with elf why
how 'big' is 'a big file' ?kiran wrote: then when i run ld the output is a big file.
and the os doesnt work
- creating an ELF asm makes nasm add its .comment to the thing, AOUT has no .comment section ...
- more padding is added at the end of .text in ELF mode
That being said, with ld version 2.13.90.0.18, gcc (GCC) 3.3 and NASM version 0.98.34, the files generated with a 'aout' or an 'elf' startup asm are byte-to-byte identical as soon as i use --oformat binary link statement ...
k_init.asm
Code: Select all
[bits 32] ; 32bit code here
[global start] ; start is a global function
[extern _k_main] ; this is the kernel function
start:
call _k_main ; jump to k_main() in kernel.c
hlt ; halt the cpu
Code: Select all
void _k_main()
{
int num;
char ch;
char *text_video = (char*)0xB8000;
char attrib = 0x07;
char *str="Kernel Loaded";
while(*str!=0)
{
*text_video = *str;
*text_video++;
*text_video = attrib;
*text_video++;
*str++;
}
return;
}
Code: Select all
> nasm k_init.asm -f aout -o k_init_aout.o
> gcc kernel.c -c
> ld --oformat binary -Ttext 0x100000 k_init_aout.o kernel.o -o kaout.bin
> l kaout.bin
-rwxr-xr-x 1 martin run 93 2004-08-05 15:28 kaout.bin
Re:works with aout not with elf why
hello
here are my k_init.asm and kernel.c and linker.ld
k_init.asm
----------
GLOBAL __start
EXTERN _k_main
[BITS 32]
__start:
call _k_main
jmp $
kernel.c
--------
#define COLUMNS 80
#define LINES 24
#define ATTRIBUTE 7
#define VIDEO 0xB8000
static int xpos;
static int ypos;
static volatile unsigned char *text_video;
void _k_main ();
void _clear_screen (void);
static void itoa (char *buf, int base, int d);
static void putchar (int c);
void printk (const char *format, ...);
void
_k_main ()
{
_clear_screen ();
printk ("\n \n Welcome to the Ekalavya Operating System \n \n");
}
void
_clear_screen ()
{
int i;
text_video = (unsigned char *) VIDEO;
for (i = 0; i < COLUMNS * LINES * 2; i++)
*(text_video + i) = 0;
xpos = 0;
ypos = 0;
}
static void
itoa (char *buf, int base, int d)
{
char *p = buf;
char *p1, *p2;
unsigned long ud = d;
int divisor = 10;
if (base == 'd' && d < 0)
{
*p++ = '-';
buf++;
ud = -d;
}
else if (base == 'x')
divisor = 16;
do
{
int remainder = ud % divisor;
*p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
}
while (ud /= divisor);
*p = 0;
p1 = buf;
p2 = p - 1;
while (p1 < p2)
{
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
}
static void
putchar (int c)
{
if (c == '\n' || c == '\r')
{
newline:
xpos = 0;
ypos++;
if (ypos >= LINES)
ypos = 0;
return;
}
*(text_video + (xpos + ypos * COLUMNS) * 2) = c & 0xFF;
*(text_video + (xpos + ypos * COLUMNS) * 2 + 1) = ATTRIBUTE;
xpos++;
if (xpos >= COLUMNS)
goto newline;
}
void
printk (const char *format, ...)
{
char **arg = (char **) &format;
int c;
char buf[20];
arg++;
while ((c = *format++) != 0)
{
if (c != '%')
putchar (c);
else
{
char *p;
c = *format++;
switch (c)
{
case 'd':
case 'u':
case 'x':
itoa (buf, c, *((int *) arg++));
p = buf;
goto string;
break;
case 's':
p = *arg++;
if (!p)
p = "(null)";
string:
while (*p)
putchar (*p++);
break;
default:
putchar (*((int *) arg++));
}
}
}
}
linker.ld
-------
OUTPUT_FORMAT("binary")
ENTRY(__start)
SECTIONS
{
.text 0x100000 : {
*(.text)
}
.data : {
*(.data)
}
.bss : {
*(.bss)
}
}
here are my k_init.asm and kernel.c and linker.ld
k_init.asm
----------
GLOBAL __start
EXTERN _k_main
[BITS 32]
__start:
call _k_main
jmp $
kernel.c
--------
#define COLUMNS 80
#define LINES 24
#define ATTRIBUTE 7
#define VIDEO 0xB8000
static int xpos;
static int ypos;
static volatile unsigned char *text_video;
void _k_main ();
void _clear_screen (void);
static void itoa (char *buf, int base, int d);
static void putchar (int c);
void printk (const char *format, ...);
void
_k_main ()
{
_clear_screen ();
printk ("\n \n Welcome to the Ekalavya Operating System \n \n");
}
void
_clear_screen ()
{
int i;
text_video = (unsigned char *) VIDEO;
for (i = 0; i < COLUMNS * LINES * 2; i++)
*(text_video + i) = 0;
xpos = 0;
ypos = 0;
}
static void
itoa (char *buf, int base, int d)
{
char *p = buf;
char *p1, *p2;
unsigned long ud = d;
int divisor = 10;
if (base == 'd' && d < 0)
{
*p++ = '-';
buf++;
ud = -d;
}
else if (base == 'x')
divisor = 16;
do
{
int remainder = ud % divisor;
*p++ = (remainder < 10) ? remainder + '0' : remainder + 'a' - 10;
}
while (ud /= divisor);
*p = 0;
p1 = buf;
p2 = p - 1;
while (p1 < p2)
{
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
p1++;
p2--;
}
}
static void
putchar (int c)
{
if (c == '\n' || c == '\r')
{
newline:
xpos = 0;
ypos++;
if (ypos >= LINES)
ypos = 0;
return;
}
*(text_video + (xpos + ypos * COLUMNS) * 2) = c & 0xFF;
*(text_video + (xpos + ypos * COLUMNS) * 2 + 1) = ATTRIBUTE;
xpos++;
if (xpos >= COLUMNS)
goto newline;
}
void
printk (const char *format, ...)
{
char **arg = (char **) &format;
int c;
char buf[20];
arg++;
while ((c = *format++) != 0)
{
if (c != '%')
putchar (c);
else
{
char *p;
c = *format++;
switch (c)
{
case 'd':
case 'u':
case 'x':
itoa (buf, c, *((int *) arg++));
p = buf;
goto string;
break;
case 's':
p = *arg++;
if (!p)
p = "(null)";
string:
while (*p)
putchar (*p++);
break;
default:
putchar (*((int *) arg++));
}
}
}
}
linker.ld
-------
OUTPUT_FORMAT("binary")
ENTRY(__start)
SECTIONS
{
.text 0x100000 : {
*(.text)
}
.data : {
*(.data)
}
.bss : {
*(.bss)
}
}
Re:works with aout not with elf why
hello
i used the following commands with the above files and got different results for aout and elf
For aout
gcc -c kernel.c -o kernel.o
nasm -f aout -o k_init.o k_init.asm
ld -T linker.ld -o kernel.bin k_init.o kernel.o
ll -h
kernel.bin 824
k_init.o 92
kernel.o 1.9K
For elf
gcc -c kernel.c -o kernel.o
nasm -f elf -o k_init.o k_init.asm
ld -T linker.ld -o kernel.bin k_init.o kernel.o
ll -h
kernel.bin 1.1M
k_init.o 608
kernel.o 1.9K
THat kernel is 1.1 M. big right ???
should i give an objdump also?
I use linux for all these
Kiran
i used the following commands with the above files and got different results for aout and elf
For aout
gcc -c kernel.c -o kernel.o
nasm -f aout -o k_init.o k_init.asm
ld -T linker.ld -o kernel.bin k_init.o kernel.o
ll -h
kernel.bin 824
k_init.o 92
kernel.o 1.9K
For elf
gcc -c kernel.c -o kernel.o
nasm -f elf -o k_init.o k_init.asm
ld -T linker.ld -o kernel.bin k_init.o kernel.o
ll -h
kernel.bin 1.1M
k_init.o 608
kernel.o 1.9K
THat kernel is 1.1 M. big right ???
should i give an objdump also?
I use linux for all these
Kiran
Re:works with aout not with elf why
Your linker thinks that the file will be loaded to 0.
And as the code starts at the 1MB mark, it adds a 1MB padding, so that when it gets loaded to 0, the code is at 1MB ...
And as the code starts at the 1MB mark, it adds a 1MB padding, so that when it gets loaded to 0, the code is at 1MB ...
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:works with aout not with elf why
I second that. Really, we could better help you if we could see your linker script ...Legend wrote: Your linker thinks that the file will be loaded to 0.
And as the code starts at the 1MB mark, it adds a 1MB padding, so that when it gets loaded to 0, the code is at 1MB ...
Re:works with aout not with elf why
hello
I already gave my linker script. Here is it.
linker.ld
-------
OUTPUT_FORMAT("binary")
ENTRY(__start)
SECTIONS
{
.text 0x100000 : {
*(.text)
}
.data : {
*(.data)
}
.bss : {
*(.bss)
}
}
Then how did it work with aout?
Kiran
I already gave my linker script. Here is it.
linker.ld
-------
OUTPUT_FORMAT("binary")
ENTRY(__start)
SECTIONS
{
.text 0x100000 : {
*(.text)
}
.data : {
*(.data)
}
.bss : {
*(.bss)
}
}
Then how did it work with aout?
Kiran
Re:works with aout not with elf why
hello
Yes i found that there is a lot of padding happening.
So what should be the change in my linker script.
kiran
Yes i found that there is a lot of padding happening.
So what should be the change in my linker script.
kiran
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:works with aout not with elf why
you're missing .rodata* in your linker script.
Probably the linker default them at 00000000 while the rest starts at 0x100000, hence the padding...
Probably the linker default them at 00000000 while the rest starts at 0x100000, hence the padding...