Cross-compile nasm linux problem

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.
Post Reply
nake
Posts: 5
Joined: Tue Mar 30, 2010 8:42 am

Cross-compile nasm linux problem

Post by nake »

Hi all, this is my first post here ^^

I'm starting to develop my kernel, but I get those
start.o:(.text+0x2d): undefined reference to `_kmain'
As I'm quite new on compiling on linux I think I did something wrong while ./configure or so...
On the wiki there is a tutorial on how compiling gcc an binutils, but nothing for nasm (or at least i couldn find it...)
This is what I did:

I downloaded the nasm source code, untared it and cd into it's folder, then

Code: Select all

export PREFIX=/usr/cross
export TARGET=i586-elf
./configure --prefix=$PREFIX --target=$TARGET
make all
make install

export PATH=$PATH:$PREFIX/bin
$TARGET-nasm -f elf -o start.o start.asm
It says: i586-elf-nasm: no se encontró la orden (which means that couldn't find the order)
If I assembly it using

Code: Select all

nasm -f elf -o start.o start.asm
I get the _kmain error while linking... yagh

Thanks in advance
There are 10 kinds of people, the ones that know binary and those who dont.
Keyboard not detected, press F1 to continue...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Cross-compile nasm linux problem

Post by Combuster »

Obviously, "_kmain" and "kmain" are not the same thing... Fix your spelling
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
nake
Posts: 5
Joined: Tue Mar 30, 2010 8:42 am

Re: Cross-compile nasm linux problem

Post by nake »

... It fixed it, however in the wiki says:
I get unresolved references to kmain, or others because of missing / superfluous underscores in the object files !?
As an immediate resolve, you can use -fno-leading-underscores or -fleading-underscores to get the object file variant you need; in the long run, you might want to set up a GCC Cross-Compiler with the correct default behavior.
I want to do it the good way, not the fast one...

Thanks for the fast reply :)
There are 10 kinds of people, the ones that know binary and those who dont.
Keyboard not detected, press F1 to continue...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Cross-compile nasm linux problem

Post by Combuster »

As quoted, if you have the GCC Cross-Compiler built according to those instructions, it has the correct behaviour by default. Which means there are no leading underscores.

Note that the default depends on the host you are compiling for - on DOS and Windows leading underscores are customary, while on practically all others they are not. Which is one of the reasons why you should use a cross-compiler: to get rid of the host platform-specific features the compiler throws into your OS.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
nake
Posts: 5
Joined: Tue Mar 30, 2010 8:42 am

Re: Cross-compile nasm linux problem

Post by nake »

mmm....still having trouble.
I couldnt find anything on the forums nor wiki.

If I try to call a function made in asm i get an undefined reference error while linking. And this time is not a spelling error.

The assembly code:

Code: Select all

global gdt_flush
extern gp
gdt_flush:
    lgdt [gp]
    mov ax, 0x10
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush2
flush2:
    ret
and the C code:

Code: Select all

struct gdt_entry gdt[3];
struct gdt_ptr gp;

extern void gdt_flush(void);

void gdt_install(void)
{
    /* Setup the GDT pointer and limit */
    gp.limit = (sizeof(struct gdt_entry) * 3) - 1;
    gp.base = (int)&gdt;

    gdt_set_gate(0, 0, 0, 0, 0);
    gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
    gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
    gdt_flush();
}
Then I just assembly, compile everything, and then link it:

Code: Select all

nasm -f elf -o start.o start.asm
$TARGET-gcc -Wall -Wextra -Werror -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -fno-leading-underscore -I./include -c -o gdt.o gdt.c
$TARGET-ld -T link.ld -o kernel.bin start.o kernel.o gdt.o display.o
I've really no clue why it fails, I even tried adding '_' everywhere xD

Code: Select all

gdt.o: In function `gdt_install':
gdt.c:(.text+0xb2): undefined reference to `gdt_flush'
Thanks
There are 10 kinds of people, the ones that know binary and those who dont.
Keyboard not detected, press F1 to continue...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Cross-compile nasm linux problem

Post by Combuster »

did you use sections?
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
nake
Posts: 5
Joined: Tue Mar 30, 2010 8:42 am

Re: Cross-compile nasm linux problem

Post by nake »

Only the .bss for the stack... Do I have to define all of them?
However I think I'm going to start again following another tutorial.
There are 10 kinds of people, the ones that know binary and those who dont.
Keyboard not detected, press F1 to continue...
nake
Posts: 5
Joined: Tue Mar 30, 2010 8:42 am

Re: Cross-compile nasm linux problem

Post by nake »

My .ld looks like this:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
SECTIONS
{
  .text 0x100000 :
  {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
  }

  .data :
  {
     data = .; _data = .; __data = .;
     *(.data)
     *(.rodata)
     . = ALIGN(4096);
  }

  .bss :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }

  end = .; _end = .; __end = .;
}
And I think in the assembly everything is .text except the part that says:

Code: Select all

SECTION .bss
    resb 8192               ; This reserves 8KBytes of memory here
sys_stack:
And thats the last thing I have on my assembly file. I feel like a big noob xD
I dont really know too much about assembly, apart from microcontroler's asm.
There are 10 kinds of people, the ones that know binary and those who dont.
Keyboard not detected, press F1 to continue...
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Cross-compile nasm linux problem

Post by Combuster »

nake wrote:I think
Don't think, know. Especially when you have already proven yourself wrong somewhere :wink:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply