Assembly doesn't link properly.

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
Aurdal
Posts: 11
Joined: Fri Sep 11, 2009 6:08 pm
Location: Norway

Assembly doesn't link properly.

Post 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.
Caution: Enjoy above remarks responsibly. Overuse may be hazardous to your mental health.
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Re: Assembly doesn't link properly.

Post by earlz »

try prefixing start with an _ in your assembly code so it's _start
Aurdal
Posts: 11
Joined: Fri Sep 11, 2009 6:08 pm
Location: Norway

Re: Assembly doesn't link properly.

Post 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. :)
Caution: Enjoy above remarks responsibly. Overuse may be hazardous to your mental health.
User avatar
accelleon
Member
Member
Posts: 28
Joined: Wed Jul 22, 2009 6:49 pm

Re: Assembly doesn't link properly.

Post 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.
Accel OS website.
Pimpanime-#1 source for English dubbed anime
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Assembly doesn't link properly.

Post 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
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: Assembly doesn't link properly.

Post by Hangin10 »

Explicitly add a "section .text" after "[global start]".
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: Assembly doesn't link properly.

Post 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.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
Hangin10
Member
Member
Posts: 162
Joined: Wed Feb 27, 2008 12:40 am

Re: Assembly doesn't link properly.

Post 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: :)
Aurdal
Posts: 11
Joined: Fri Sep 11, 2009 6:08 pm
Location: Norway

Re: Assembly doesn't link properly.

Post by Aurdal »

Hangin10 wrote:Explicitly add a "section .text" after "[global start]".
That solved the problem, Thanks.
Caution: Enjoy above remarks responsibly. Overuse may be hazardous to your mental health.
Post Reply