Page 1 of 2

Problem with linking kernel

Posted: Sun Dec 07, 2003 7:28 pm
by beyondsociety
Im trying to compile my OS on Windows 2000 using Cygwin and am having problems with ld linking my kernel. I am using a makefile.

In my makefile, if I set ld to

Code: Select all

ld -T linker.ld -o $(KERNEL_NAME)
ld gives me this error: ld = bad reloc address 0x820 in section .data. I searched on google and on a post, it said to use the "-r" command with ld to link without relocations.

So I use the "-r" and make gives me a segmentation fault.

My ld on cygwin is set up for 'pe-i386" file format only and at the moment I am re-compiling ld to support all the file format targets. I not sure what Im doing wrong, but heres my makefile

Code: Select all

# Basic Makefile for IBOX OS, Ver.0.01, Date: 04/22/03 1:14PM

# Name of IBOX OS kernel
#KERNEL_NAME = ibox.img
KERNEL_NAME = kernel.exe   

# C compiler to use
CC = gcc   
CFLAGS = -c -Wall -I./include/   

# Assembler to use
NASM = nasm   
NASMFLAGS = -f coff    

# Linker to use
LD = ld   # we're using the GNU linker
#LDFLAGS = -T linker.ld -o $(KERNEL_NAME) # -T, Link with relocation
LDFLAGS = -r linker.ld -o $(KERNEL_NAME)  # -r, Link without relocation

# OBJCOPY
OBJC = objcopy
OBJCFLAGS = -0 binary kernel.exe kernel.bin

OBJS = boot/stub.o kernel/kernel.o kernel/8259.o kernel/exceptions.o kernel/io.o kernel/stdio.o kernel/string.o kernel/timer.o kernel/vsprintf.o boot/handlers.o
all: $(KERNEL_NAME)

install: 
   # this will install the kernel on a floppy
   # you may change this easily
   mount /floppy/
   cp $(KERNEL_NAME) /floppy/
   umount /floppy/

clean:
   rm $(OBJS)

# compile all assembler files
%.o : %.asm
   $(NASM) $(NASMFLAGS) -o $@ $<

# compile all C files
%.o : %.c
   $(CC) $(CFLAGS) -o $@ $<

# and link them all together
$(KERNEL_NAME): $(OBJS)   
   $(LD) $(LDFLAGS) $(OBJS)
   $(OBJC) $(OBJCFLAGS)
Linker.ld

Code: Select all

OUTPUT_FORMAT("pe-i386")

ENTRY("_start")
SECTIONS
{
  /* Kernel .text section */
  .text 0x100000 :              
  {
         kernel_code = .; 
         *(.text)
  }
  
  /* Kernel .data section */
  .data :    
  {
         kernel_data = .; 
         *(.data)
  }
  
  /* Kernel .bss section */
  .bss : 
  {
         kernel_bss = .; 
         *(.bss)
         *(.common)
  }  
  /* Kernel .end section */
  kernel_end = .; 
}


   
   

Re:Problem with linking kernel

Posted: Sun Dec 07, 2003 10:43 pm
by Chris Giese
>NASMFLAGS = -f coff

Try this instead:

NASMFLAGS = -f win32

COFF and Win32 PE COFF object files look identical, but they're not (the relocations work differently).

"ld -r ..." just combines two or more .o files into one .o file, so that's probably not what you want.

Re:Problem with linking kernel

Posted: Mon Dec 08, 2003 6:01 pm
by beyondsociety

Code: Select all

>NASMFLAGS = -f coff    

Try this instead:

NASMFLAGS = -f win32

COFF and Win32 PE COFF object files look identical, but they're not (the relocations work differently).

"ld -r ..." just combines two or more .o files into one .o file, so that's probably not what you want.
Changed NASMFLAGS = "win32" and it didnt change anything. Got rid of the LD -r.

Re:Problem with linking kernel

Posted: Tue Dec 09, 2003 6:41 pm
by beyondsociety
I did some testing and have narrowed my problem down to my 'stub.o' file. If I dont compile with the 'stub.o' file, it works fine. I dont know whats wrong, so I asking for help.

Code: Select all

stub.asm compiled to stub.o

objump -d -r -s -x stub.o >dump.txt
Files are attacted below

[attachment deleted by admin]

Re:Problem with linking kernel

Posted: Wed Dec 10, 2003 11:23 am
by df
youve set your stack to START of stack, stack goes DOWN. thus your first push kills your IDT stuff... if the end idt entries get called.....

put

stack: resd 1024
stackend:

mov esp,stackend!

hmm well when its linked it will overwrite whateve data/bss is underneath stack label...

Re:Problem with linking kernel

Posted: Wed Dec 10, 2003 4:08 pm
by beyondsociety
Changed it, but it was of no help.

Re:Problem with linking kernel

Posted: Wed Dec 10, 2003 9:17 pm
by Chris Giese
I can build it with MinGW based on GCC 2.95 but when I use GCC 3.2 the linker crashes. Commenting out OUTPUT_FORMAT in the linker script prevents the crash. I don't know why -- "pe-i386" is listed as a supported format. (Of course, ld also lists "elf32-i386" as a supported format, even though it doesn't really work.)

There was a bug involving the BSS section size in MinGW based on GCC 2.95 (details: http://my.execpc.com/~geezer/embed/bugs.htm#mingbug), but I think this has been addressed in newer versions. The bug prevents the creation of a working executable file when you link with code from other tool chains (e.g. NASM). So the next thing I'd try is converting the NASM code to AS.

MinGW is the retarded child of the GCC family. I don't know if CygWin is any better.

Re:Problem with linking kernel

Posted: Thu Dec 11, 2003 2:06 am
by Tim
"pei-i386" works better. I don't know how it is different from pe-i386, but I get better results.

Re:Problem with linking kernel

Posted: Thu Dec 11, 2003 2:14 am
by df
i trust cygwin more than I trust mingw.. there is something bout mingw that isnt right ;D

Re:Problem with linking kernel

Posted: Fri Dec 12, 2003 4:47 pm
by beyondsociety
I have noticed something weird. When I type 'make' I get the relocation address error. If I delete all my .o files from within my directories that I compiling my sourcecode from, the ld linker doesnt dump the error.

Weird huh?

Code: Select all

ld -v = 2.14.90 20030901
gcc -v = 3.3.1 (Cygwin special)

Re:Problem with linking kernel

Posted: Fri Dec 12, 2003 8:32 pm
by beyondsociety
Im having a problem with linking the libgcc.a library. I am linking my code like this

Code: Select all

LDFLAGS = -T linker.ld -o $(KERNEL_NAME) -lgcc -L/lib/gcc-lib/i686-pc-cygwin/3.3.1
When I type 'make', I get a "Undefined reference: _alloca, undefined reference: __main. I have been working off this old post http://www.mega-tokyo.com/forum/index.php?board=1;action=display;threadid=4939 But for some reason its not working. Any help would be appreciated.

Re:Problem with linking kernel

Posted: Sat Dec 13, 2003 4:17 am
by df
you need -ffreestanding -nostdlib -fno-builtin
on your CFLAGS

Re:Problem with linking kernel

Posted: Sat Dec 13, 2003 3:28 pm
by beyondsociety
Doesnt change a thing. Why is this not working.

Re:Problem with linking kernel

Posted: Sat Dec 13, 2003 4:35 pm
by df
freestanding, nobuiltin etc remove the alloca and stuff (gcc's built in functions).

Re:Problem with linking kernel

Posted: Sat Dec 13, 2003 9:06 pm
by beyondsociety
you need -ffreestanding -nostdlib -fno-builtin
on your CFLAGS
I have done this, but I still get the: undefined references for _alloca, __main.

This error happens when the code gets linked by ld. Do I have to add any parameters to ld when it links?

The link I posted earlier states that I have to add "-lgcc -L/lib/gcc-lib/i686-pc-cygwin/3.3.1" to LDFLAGS but when I do, I get the undefined reference errors. IF I add libgcc.a to the end of that command, I get "ld: cannot find -lgcc".

I have the libgcc.a file in the correct directory. Any help would be appreciated.