Undefined reference in asm-code

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
david-h
Posts: 17
Joined: Sun Apr 08, 2007 5:48 am
Location: Frankfurt/Main, Germany
Contact:

Undefined reference in asm-code

Post by david-h »

Hi,
I wanted to implement ISR´s like in Bran´s Kernel Development Tutorial. But my .asm file can´t call my fault_handler-function. The code that should call fault_handler looks like this:

Code: Select all

[extern _fault_handler

; ...

isr_common_stub:
    pusha
    push ds
    push es
    push fs
    push gs
    mov ax, 0x10   ; Load the Kernel Data Segment descriptor!
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov eax, esp   ; Push us the stack
    push eax
    mov eax, _fault_handler
    call eax       ; A special call, preserves the 'eip' register
    pop eax
    pop gs
    pop fs
    pop es
    pop ds
    popa
    add esp, 8     ; Cleans up the pushed error code and pushed ISR number
    iret           ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP!
fault_handler gets this struct as parameter:

Code: Select all

struct regs
{
	unsigned int gs, fs, es, ds;      /* pushed the segs last */
	unsigned int edi, esi, ebp, esp, ebx, edx, ecx, eax;  /* pushed by 'pusha' */
	unsigned int int_no, err_code;    /* our 'push byte #' and ecodes do this */
	unsigned int eip, cs, eflags, useresp, ss;   /* pushed by the processor automatically */ 
};
The function itself just displays an error message on the screen.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Code: Select all

[extern _fault_handler
Should just be

Code: Select all

extern _fault_handler
Make sure you have a fault_handler() function.

The code in his tutorial works - I've used it twice now, and translated it to AT&T syntax. If you could say exactly what the problem is, it would be very helpful.
david-h
Posts: 17
Joined: Sun Apr 08, 2007 5:48 am
Location: Frankfurt/Main, Germany
Contact:

Post by david-h »

The problem is, that ld doesn´t link the .o files. I receive the following message:
Loader.o:Loader.o:<.test+0x1a1>: undefined reference to '_fault_handler'
The function is defined like this:

Code: Select all

void fault_handler(regs *r)
{
	if (r->int_no < 32)
	{
		std::cout << exception_messages[r->int_no];
		std::cout << "\nSystem halts now.";
		for(;;);
	}
}
I cna call other functions from my .asm file, like main().
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post by urxae »

If you're compiling to ELF (i.e. if you're using a cross-compiler to an ELF target, or the default compiler of most Linux systems), remove the leading underscore in the name.
If you're compiling from anything other than C, make sure fault_handler uses the C calling convention (i.e. use 'extern "C"' in C++ code, 'extern(C)' in D code, etc.).
EDIT: I didn't see your new post yet. That's clearly C++, so put extern "C" in front of that function.
Last edited by urxae on Sat May 05, 2007 2:51 am, edited 1 time in total.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

Show us your makefile / command line.

Based on that we can help. Specifically, show the parts that compile the fault handler and the assembly code, and your LD command.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

This is just LAME!!

It's almost weekly someone posts about the "undefined reference" tidbit.

Solutions:
Change extern _fault_handler to extern fault_handler

Change mov eax, _fault_handler to mov eax, fault_handler

Actually, Remove ALL underscores from both C and ASM files and then add this to your GCC/G++ argument list:
-fno-leading-underscore

It's just foolish... This little piece of information should be a "Sticky" thread..
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

It's almost weekly someone posts about the "undefined reference" tidbit.
Almost weekly someone decides to make an OS and finds osdever.net.

And yes... I always forget about the leading underscores.

Someone has got to modify that tutorial - make it a bit more readable and not just a source for code.

Either that or do what Brynet suggested - put up a sticky!
david-h
Posts: 17
Joined: Sun Apr 08, 2007 5:48 am
Location: Frankfurt/Main, Germany
Contact:

Post by david-h »

urxae wrote:If you're compiling to ELF (i.e. if you're using a cross-compiler to an ELF target, or the default compiler of most Linux systems), remove the leading underscore in the name.
If you're compiling from anything other than C, make sure fault_handler uses the C calling convention (i.e. use 'extern "C"' in C++ code, 'extern(C)' in D code, etc.).
EDIT: I didn't see your new post yet. That's clearly C++, so put extern "C" in front of that function.
Thanks, it linked and compiled correctly with that. But the ISR´s don´t work. I wanted to test it with an 'int i = 10 / 0', but if the code starts I get an processor error. So here´s my command line:

Code: Select all

C:\nasm\nasmw.exe -f aout Sourcecode/Loader.asm -o Loader.o
...
gxx -c Sourcecode/IDT.cpp -I Sourcecode/include -nostdlib -fno-builtin -fno-rtti -fno-exceptions
gxx -c Sourcecode/ISRs.cpp -I Sourcecode/include -I Sourcecode/stdlib -nostdlib -fno-builtin -fno-rtti -fno-exceptions
...
ld -T Link.ld -o Kernel.bin Loader.o Support.o IDT.o ISRs.o iostream.o GDT.o Kernel.o Video.o System.o
Here´s the Link.ld:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
  .text 0x100000 :
  {
    code = .; _code = .; __code = .;
    *(.text)
    *(.rodata)
    . = ALIGN(4096);
  }
  
  .data : 
  {
     __CTOR_LIST__ = .; LONG((__CTOR_END__ -
    __CTOR_LIST__) / 4 - 2)*(.ctors)  LONG(0) __CTOR_END__
    = .;
    
    __DTOR_LIST__ = .; LONG((__DTOR_END__ -
   __DTOR_LIST__) / 4 - 2)*(.dtors)  LONG(0) __DTOR_END__ =
   .;
   
    data = .; _data = .; __data = .;
    *(.data)
    . = ALIGN(4096);
  }
  
  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  
  end = .; _end = .; __end = .;
}

Any other ideas?
urxae
Member
Member
Posts: 149
Joined: Sun Jul 30, 2006 8:16 am
Location: The Netherlands

Post by urxae »

david-h wrote:Thanks, it linked and compiled correctly with that. But the ISR´s don´t work. I wanted to test it with an 'int i = 10 / 0', but if the code starts I get an processor error.
Ehm... Isn't that exactly what you want to happen on a divide by zero? Or are you getting some error other than ISR #0? (In which case, you might want to be more specific about what error you get...)
david-h
Posts: 17
Joined: Sun Apr 08, 2007 5:48 am
Location: Frankfurt/Main, Germany
Contact:

Post by david-h »

I did the thing with the underscores. Everything compiles and links correctly, but the ISR´s still don´t work.

I don´t get an error message from my kernel, I get a messagebox from Virtual PC saying something like processor error, your Virtual PC will be reseted.
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

Post by neon »

I don´t get an error message from my kernel, I get a messagebox from Virtual PC saying something like processor error, your Virtual PC will be reseted
What is the exact error? I dont use Virtual PC, but I assume it logs
the current state of the processor?
david-h
Posts: 17
Joined: Sun Apr 08, 2007 5:48 am
Location: Frankfurt/Main, Germany
Contact:

Post by david-h »

The exact error message is in german. I tried to translate it:

"Unrecoverable processor error!

The Virtual PC will now be reseted"

I can´t find any logs, neither in User\AppData nor in Program Files\Microsoft Virtual PC. Maybe I should Bochs?
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

david-h wrote:The exact error message is in german. I tried to translate it:

"Unrecoverable processor error!

The Virtual PC will now be reseted"

I can´t find any logs, neither in User\AppData nor in Program Files\Microsoft Virtual PC. Maybe I should Bochs?
Virtual PC sounds like an emulator for users.. Not developers.. Try QEMU or Bochs.
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
david-h
Posts: 17
Joined: Sun Apr 08, 2007 5:48 am
Location: Frankfurt/Main, Germany
Contact:

Post by david-h »

In the attachment you can find my protocoll file of Bochs. Could anyone take a look at it and say what is wrong?

I guess the problem is that it doesn´t load the ISRs into the processor but I´m not sure.
Attachments
protocoll.txt
(8.8 KiB) Downloaded 28 times
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

Have you added anything more than just ints 0 - 31? Looks like you haven't set up the IDT properly - could be an unhandled IRQ firing...
Post Reply