Page 1 of 2

Linker problem

Posted: Tue Jun 28, 2005 11:00 pm
by Mikkel
I'm using the following code to compile my OS:

nasm -f aout -o kernel_start.o kernel_start.s
nasm -f obj -o low_kb.obj asm/low_kb.asm

gcc -fno-builtin -fwritable-strings -O0 -I./include -c -o kernel.o kernel.c
gcc -fno-builtin -fwritable-strings -O0 -I./include -c -o screen.o screen.c
gcc -fno-builtin -fwritable-strings -O0 -I./include -c -o consol.o consol.c
gcc -fno-builtin -fwritable-strings -O0 -I./include -c -o kb.o kb.c

ld -T link.ld -o kernel.bin kernel_start.o kernel.o screen.o consol.o low_kb.obj kb.o

rm *.o

But...ld returns this error:
low_kb.obj: file not reconized

How can i solve this? I'm compiling it in Fedora and the ld version is 5.2.1

Thanks!
Mikkel

Re: Linker problem

Posted: Tue Jun 28, 2005 11:00 pm
by carbonBased
Uhm... it's because the file format isn't recognised :)

Any reason why you're compiling kernel_start.s as a A.OUT object, and low_kb.asm as an OBJ (which Linux ld does not support)?

For that matter, any reason why you've swapped from .s to .asm for assembly language files?

Either switch to A.OUT or, better yet, switch them both to ELF... A.OUT is hardly supported anymore.

--Jeff

Re: Linker problem

Posted: Wed Jun 29, 2005 11:00 pm
by Mikkel
Thanks!

I've changed it to elf and the .asm-file i .s.
But now I just get a new error...

kb.o(.text+0x5c): In function `scanf':
: undefined reference to `readkey'

I'm using extern int readkey(); to define the function (wich is in kb_low.s).

kb_low.s :
[bits 16]
[global _readkey]
_readkey: push bp
mov bp, sp
xor ah, ah
int 0x16
and ax, 0xFF
pop bp
ret

What am i'm doing wrong?

Thanks again
Mikkel

btw, my "build-file" is now:

nasm -f aout -o kernel_start.o kernel_start.s
nasm -f obj -o low_kb.o asm/low_kb.s

gcc -fno-builtin -fwritable-strings -O0 -I./include -c -o kernel.o kernel.c
gcc -fno-builtin -fwritable-strings -O0 -I./include -c -o screen.o screen.c
gcc -fno-builtin -fwritable-strings -O0 -I./include -c -o consol.o consol.c
gcc -fno-builtin -fwritable-strings -O0 -I./include -c -o kb.o kb.c

ld -T link.ld -o kernel.bin kernel_start.o kernel.o screen.o consol.o low_kb.o kb.o

rm *.o

Re: Linker problem

Posted: Wed Jun 29, 2005 11:00 pm
by rexlunae
[quote="Mikkel"]Thanks!

I've changed it to elf and the .asm-file i .s.
But now I just get a new error...

kb.o(.text+0x5c): In function `scanf':
: undefined reference to `readkey'
[/quote]

One difference between ELF and A.OUT which can be confusing is the fact that A.OUT required all globl symbols to have an underscore prepended to them, whereas ELF requires that they be named normally. So taking the underscore off of your readkey function's name should help.

Re: Linker problem

Posted: Thu Jun 30, 2005 11:00 pm
by Mikkel
Thanks it works...but...when i'm using readkey() in C, the computer is restarting....

How can I fix this?

Thanks in advance!

Re: Linker problem

Posted: Fri Jul 01, 2005 11:00 pm
by carbonBased
This could be caused by several different things... can you provide some more info?

What type of environment is this code supposed to run in, by the way? Your asm code has a .bits 16, but your C code is compiled with GCC which is 32-bit.

--Jeff

Re: Linker problem

Posted: Fri Jul 01, 2005 11:00 pm
by Mikkel
I've just tried to change it from 16 bit to 32 (by changing the line: [bits 16] to [bits 32]), but the same happened, it's restarting.

My assembler file: (low_kb.s)

Code: Select all

[bits 32]

[global readkey]
readkey:
push bp
mov bp, sp
xor ah, ah
int 0x16
and ax, 0xFF
pop bp
ret 
My C-file that uses the function is: (kb.c)

Code: Select all

#include <screen.h>

#define ENTER_ASCII_KEY 13

void scanf(char strg[60]) { 
     char alfar[60]; 
     int kar; 
     int cont=0; 
      
     int enter_pressed=0;
 
     int iik; 
      
     while (enter_pressed==0) { 
          kar=readkey(); 
          if (kar==ENTER_ASCII_KEY) {      
               strg[cont]='\0'; 
               enter_pressed=1; 
               print("\n"); 
          } else { 
               strg[cont]=kar; 
               printchar(&kar);
               cont+=1; 
          } 
     }  
}
and i'm compiling the two files with:
nasm -f elf -o low_kb.o asm/low_kb.s
gcc -fno-builtin -fwritable-strings -O0 -I./include -c -o kb.o kb.c

Thanks again!

Re: Linker problem

Posted: Sat Jul 02, 2005 11:00 pm
by CopperMan
Note : most of BIOS interrupts (services) can be used only in Real or VM86 processor mode.

Case 1: if You in Real mode.

You should use [bits 16] to get this code working.

Case 2: if You in Protected mode.

If You really want to use BIOS services in PM, You must switch to VM86
before calling it.

Give me your e-mail I'll send how-to doc on VM86.

Re: Linker problem

Posted: Sat Jul 02, 2005 11:00 pm
by Mikkel
I've tried the code both in 32 bit and 16 bit, but either way the computer just restarts.

My email is: [email protected]

Re: Linker problem

Posted: Sat Jul 02, 2005 11:00 pm
by carbonBased
You've tried changing the [bits] directive, but what environment are you actually running this code in? real or protected mode?

Where exactly does the code fail? Which line number?

--Jeff

Re: Linker problem

Posted: Sun Jul 03, 2005 11:00 pm
by Mikkel
I don't know where exactly, but someplace in the assembler.
The error occurs when i'm using readkey() in my C.

All my other code is in 32 bit.

CopperMan > Thanks for the how-to doc on VM86, I'll read it today.

Re: Linker problem

Posted: Tue Jul 05, 2005 11:00 pm
by rexlunae
Mikkel wrote:All my other code is in 32 bit.
Yes, the code may be 32-bit, but when you try to run that code, what mode is the processor in? In other words, have you (either in the kernel or bootloader) switched to protected mode and loaded 32-bit segments?

Re: Linker problem

Posted: Tue Jul 05, 2005 11:00 pm
by Mikkel
well, i've just read some more tutrials and are now using this code in low_kb.s:

Code: Select all

[bits 16]

[global readkey]
readkey:
	in al,64h		; read status byte
	and al,01h	; wait for OBF==1
	jz readkey
	in al,60h		; read scancode byte
ret
This works great, but, it get's the scancode, and not the ascii value. So now i need to convert the scancodes to ascii.
So by reading some more of that article i've now changed kb.c to:

Code: Select all

#include <screen.h>

#define ENTER_ASCII_KEY 13

void scanf(char strg[60]) { 
	//Define keyboard layout:
	unsigned char kbdus[128] =
	{
	    0,  27, '1', '2', '3', '4', '5', '6', '7', '8',	/* 9 */
	  '9', '0', '-', '=', '\b',	/* Backspace */
	  '\t',			/* Tab */
	  'q', 'w', 'e', 'r',	/* 19 */
	  't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n',	/* Enter key */
	    0,			/* 29   - Control */
	  'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',	/* 39 */
	 '\'', '`',   0,		/* Left shift */
	 '\\', 'z', 'x', 'c', 'v', 'b', 'n',			/* 49 */
	  'm', ',', '.', '/',   0,				/* Right shift */
	  '*',
	    0,	/* Alt */
	  ' ',	/* Space bar */
	    0,	/* Caps lock */
	    0,	/* 59 - F1 key ... > */
	    0,   0,   0,   0,   0,   0,   0,   0,
	    0,	/* < ... F10 */
	    0,	/* 69 - Num lock*/
	    0,	/* Scroll Lock */
	    0,	/* Home key */
	    0,	/* Up Arrow */
	    0,	/* Page Up */
	  '-',
	    0,	/* Left Arrow */
	    0,
	    0,	/* Right Arrow */
	  '+',
	    0,	/* 79 - End key*/
	    0,	/* Down Arrow */
	    0,	/* Page Down */
	    0,	/* Insert Key */
	    0,	/* Delete Key */
	    0,   0,   0,
	    0,	/* F11 Key */
	    0,	/* F12 Key */
	    0,	/* All other keys are undefined */
	};	
	
	//Define needed variables:
	char alfar[60]; 
	int kar; 
	int cont=0; 
	
	int enter_pressed=0;
 	
	int iik; 
	
	//Get chars until enter is pressed:
	while (enter_pressed==0) { 
		kar=kbdus[readkey()]; 
		if (kar==ENTER_ASCII_KEY) {      
			strg[cont]='\0'; 
			enter_pressed=1; 
			print("\n"); 
		} else { 
			strg[cont]=kar; 
			printchar(&kar);
			cont+=1; 
		} 
	}  
}
Now, when i'm linking my files, I get this error:
kb.o(.text+0x20): In function `scanf':
: undefined reference to `memcpy'

I can't find the word "memcpy" anywere in my code, so how can I be calling this function?

Thank you very much for the help!
Mikkel

Re: Linker problem

Posted: Tue Jul 05, 2005 11:00 pm
by Mikkel
rexlunae wrote:Yes, the code may be 32-bit, but when you try to run that code, what mode is the processor in? In other words, have you (either in the kernel or bootloader) switched to protected mode and loaded 32-bit segments?
Well, i'm using GRUB as bootloader but I have not switched to protected mode in either my kernel, or in my kernel_start.asm (that have the multiboot header).

Re: Linker problem

Posted: Wed Jul 06, 2005 11:00 pm
by Mikkel
I've just tried to comment some of the lines out, and it's this that makes the error:

unsigned char kbdus[128] = { 0, 27, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\b', '\t', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\n', 0, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '`', 0, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 0, '*', 0, ' ', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '-', 0, 0, 0, '+', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

(same as the other kbdus, just made to a single line).

without it, there's no errors (except this ofcourse dosn't work: kar=kbdus[readkey()];) but with it:

kb.o(.text+0x20): In function `scanf':
: undefined reference to `memcpy'