Thread: Linking error

  1. #1
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195

    Linking error

    Im using GCC and getting an error linking,
    Quote Originally Posted by ld.exe
    loader.o: In function 'loader':
    loader.s: (.text+0x14): undefined reference to 'kmain'
    here are the source files -

    loader.s (asm)
    Code:
    global loader ; making entry point visible to linker
    extern kmain ; kmain is defined in kernel.o
    ; setting up the Multiboot header
    MODULEALIGN equ 1<<0 ; align loaded modules on page boundaries
    MEMINFO equ 1<<1 ; provide memory map
    FLAGS equ MODULEALIGN | MEMINFO ; this is the Multiboot 'flag' field
    MAGIC equ 0x1BADB002 ; 'magic number' lets bootloader find the header
    CHECKSUM equ -(MAGIC + FLAGS) ; checksum required
    section .text
    align 4
    MultiBootHeader:
    dd MAGIC
    dd FLAGS
    dd CHECKSUM
    ; reserve initial kernel stack space
    STACKSIZE equ 0x4000 ; that's 16k.
    loader:
    mov esp, stack+STACKSIZE ; set up the stack
    push eax ; pass Multiboot magic number
    push ebx ; pass Multiboot info structure
    call kmain ; call kernel proper
    hlt ; halt machine should kernel return
    section .bss
    align 32
    stack:
    resb STACKSIZE ; reserve 16k stack on a quadword boundary
    kernel.cpp
    Code:
    void kmain(void* mbd, unsigned int magic){
    // this is to stop compiler warnigns durign development
    mbd;
    magic;
    }
    my linker file
    Code:
    ENTRY (loader)
    SECTIONS{
    . = 0x00100000;
    .text :{
    *(.text)
    }
    .rodata ALIGN (0x1000) : {
    *(.rodata)
    }
    .data ALIGN (0x1000) : {
    *(.data)
    }
    .bss : {
    sbss = .;
    *(COMMON)
    *(.bss)
    ebss = .;
    }
    }
    and my link sequence
    Code:
    nasm -f elf -o loader.o loader.s  
    gcc -o kernel.o -c kernel.cpp -Wall -Wextra -Werror -nostdlib -nostartfiles -nodefaultlibs  
    ld -T linker.ld -o kernel.bin kernel.o loader.o

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you do
    nm kernel.o
    You'll find that your kmain has been name-decorated because you named it .cpp
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    :bow:

    yes that was it, although it did the same thing when I compiled it as .c

    its now named __Z5kmainv

    so I updated the extern and call lines and it linked just fine now

    now to test it out




  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You did something wrong if it's still decorated after compiling it as a .c file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    Quote Originally Posted by Salem View Post
    You did something wrong if it's still decorated after compiling it as a .c file.
    Turns out I was using the wrong compiler, I was trying to compile for an ELF target, and i was using the MinGW version of GCC. After I built a cross compiler it worked fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM