Page 1 of 1

Assembly doesn't link properly.

Posted: Fri Sep 11, 2009 6:25 pm
by Aurdal
Hi, I'm following Bran's Kernel Development tutorial. I just seem to have one nagging little problem (or large one, depending on how you look at it), ld doesn't seem to handle start.o properly. It can't find "start", neither can it find the declaration of "gtd_flush".

Here is the output from make:

Code: Select all

aurdal@tree:~/Dev/OS$ make
nasm -f elf -o start.o start.asm
gcc -m32 -fno-stack-protector -fno-builtin -nostdinc -O -g -Wall -I. -c -o main.o main.c
gcc -m32 -fno-stack-protector -fno-builtin -nostdinc -O -g -Wall -I. -c -o scrn.o scrn.c
gcc -m32 -fno-stack-protector -fno-builtin -nostdinc -O -g -Wall -I. -c -o gdt.o gdt.c
ld -melf_i386 -nostdlib -N -Ttext 100000 -T link.ld -o kernel.bin start.o main.o scrn.o gdt.o
ld: warning: cannot find entry symbol start; defaulting to 0000000000100000
gdt.o: In function `gdt_install':
/aurdal/Desktop/Dev/gdt.c:77: undefined reference to `gdt_flush'
make: *** [kernel.bin] Error 1
Here is my linkerscript link.ld:

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
	.text phys : AT(phys) {
		code = .;
		*(.text)
		*(.rodata)
		. = ALIGN(4096);
	}
	.data : AT(phys + (data - code)) {
		data = .;
		*(.data)
		. = ALIGN(4096);
	}
	.bss : AT(phys + (bss - code)) {
		bss = .;
		*(.bss)
		. = ALIGN(4096);
	}
	end = .;
}
And here is start.asm:

Code: Select all

; This is the kernel's entry point. We could either call main here,
; or we can use this to setup the stack or other nice stuff, like
; perhaps setting up the GDT and segments. Please note that interrupts
; are disabled at this point: More on interrupts later!
[BITS 32]
[global start]
start:
	mov esp, _sys_stack	 ; This points the stack to our new stack area
	jmp stublet
	
; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
ALIGN 4
mboot:
	; Multiboot macros to make a few lines later more readable
	MULTIBOOT_PAGE_ALIGN	equ 1<<0
	MULTIBOOT_MEMORY_INFO	equ 1<<1
	MULTIBOOT_AOUT_KLUDGE	equ 1<<16
	MULTIBOOT_HEADER_MAGIC	equ 0x1BADB002
	MULTIBOOT_HEADER_FLAGS	equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
	MULTIBOOT_CHECKSUM	equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
	EXTERN code, bss, end

	; This is the GRUB Multiboot header. A boot signature
	dd MULTIBOOT_HEADER_MAGIC
	dd MULTIBOOT_HEADER_FLAGS
	dd MULTIBOOT_CHECKSUM
	
	; AOUT kludge - must be physical addresses. Make a note of these:
	; The linker script fills in the data for these ones!
	dd mboot
	dd code
	dd bss
	dd end
	dd start

stublet:
	extern cmain
    call cmain
	jmp $


; Code for loading the GDT.
global gdt_flush   ; Allows the C code to link to this
extern gp            ; Says that '_gp' is in another file
gdt_flush:
    lgdt [gp]        ; Load the GDT with our '_gp' which is a special pointer
    mov ax, 0x10      ; 0x10 is the offset in the GDT to our data segment
    mov ds, ax
    mov es, ax
    mov fs, ax
    mov gs, ax
    mov ss, ax
    jmp 0x08:flush2   ; 0x08 is the offset to our code segment: Far jump!
flush2:
    ret               ; Returns back to the C code!

; Here is the definition of our BSS section. R
SECTION .bss
	resb 8192			   ; This reserves 8KBytes of memory here
_sys_stack:
If more code is needed to provide an answer, please do let me know.

I thank you all in advance.

Re: Assembly doesn't link properly.

Posted: Fri Sep 11, 2009 7:00 pm
by earlz
try prefixing start with an _ in your assembly code so it's _start

Re: Assembly doesn't link properly.

Posted: Fri Sep 11, 2009 7:08 pm
by Aurdal
I'm using Linux, so my set up shouldn't add underscores in the first place. That hasn't kept me from trying, when it didn't work properly though. So I'm afraid that's not the problem. =\

Thank you for your quick response though. :)

Re: Assembly doesn't link properly.

Posted: Fri Sep 11, 2009 8:04 pm
by accelleon
Check out this. Maybe it'll help. The only way he got it to work is to change the output format to aout.

Re: Assembly doesn't link properly.

Posted: Fri Sep 11, 2009 9:11 pm
by Troy Martin
Enable as many warning flags as you can, that might weed out a few possible issues.

My warning flags are -Wall -Wextra -Wformat-security -Wformat-nonliteral -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wno-unused -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Wno-long-long -Wstrict-prototypes -Wtrigraphs

Re: Assembly doesn't link properly.

Posted: Fri Sep 11, 2009 10:29 pm
by Hangin10
Explicitly add a "section .text" after "[global start]".

Re: Assembly doesn't link properly.

Posted: Fri Sep 11, 2009 11:30 pm
by Troy Martin
Hangin10 wrote:Explicitly add a "section .text" after "[global start]".
Ooohhh yeah, forgot that was the solution to a link error I once had... It was the "can't find entry symbol start, defaulting to 0000000000100000" one.

Re: Assembly doesn't link properly.

Posted: Fri Sep 11, 2009 11:35 pm
by Hangin10
I can't decide if assembler bug is more or less "fun" than accidentally using .code instead of .text and wondering why things aren't working. I wonder who did that... :oops: :)

Re: Assembly doesn't link properly.

Posted: Sat Sep 12, 2009 5:25 am
by Aurdal
Hangin10 wrote:Explicitly add a "section .text" after "[global start]".
That solved the problem, Thanks.