2

So I have two files: kernel.o(the kernel of my operating system) and libbios.o(which ported the bios interrupt to c),and ld told me : "undefined reference to 'printchar'".

I checked libbios.o with objdump -t and this is the result:

source/libbios.o:     file format elf32-i386

SYMBOL TABLE:
00000000 l    df *ABS*  00000000 include/libbios.asm
00000000 l    d  .TEXT  00000000 .TEXT
00000000 l       .TEXT  00000000 printchar

printchar exists in the .o file.

And this is libbios.asm:

printchar:
MOV AH,0x0e
MOV AL,[ESP+4]
INT 0X10
RET

and kernel.c:

#define TRUE 1
extern void printchar(char l);
void main(){
  printchar('a');
fin:
   asm volatile ("hlt");
   goto fin;
 }

and the command line to compile kernel.o,libbios.o and link them:

i686-elf-gcc kernel.c -c -B ../i686 -o cpartofkernel.o -m16 -masm=intel  -O0
nasm -O0 -felf include/libbios.asm -o libbios.o

i686-elf-ld --oformat binary libbios.o cpartofkernel.o  -o kernel.bin -Ttext 0x20000000 -emain

Also this is real mode code,and because I can't find any raw binary linker in linux, I must use elf.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Clement Poon
  • 79
  • 1
  • 6
  • You need `global printchar` in your NASM source to mark the symbol as something that can be linked against. What you have now is the equivalent of C `static printchar(){...}` – Peter Cordes May 25 '20 at 02:24
  • I looked for duplicates but the closest I found was [How to use asm function in C](https://stackoverflow.com/q/43656379) which is the same problem with GAS, not NASM. – Peter Cordes May 25 '20 at 02:36

1 Answers1

2

solved by adding global printchar at the top of libbios.asm.

Clement Poon
  • 79
  • 1
  • 6