Page 1 of 1

[Solved] OpenBSD _global_offset_table_ woes

Posted: Sat Jun 28, 2014 10:28 pm
by jebes
I recently changes to openbsd and attempted to compile my little OS, after much modification to the make file. When it finally did compile, I attempted to use the basic video libary I had, only to have it failing in the linking phase. The exact output is

Code: Select all

kern.o(.text+0x16): In function `main':
: undefined reference to `_GLOBAL_OFFSET_TABLE_'
Here is my makefile:

Code: Select all

include makeconfig

ALL=print.o kernel.sys krnldr.sys boot.bin floppy.img

all: $(ALL)
        echo Build Done

kernel.sys: kern/kern.o kern/test.o
        $(LD) $(LDFLAGS) -T linker.ld kern.o test.o -o kernel.sys 

print.o: lib/i386/print.asm
        $(AS) $(ASFLAGS) lib/i386/print.asm -o print.o

krnldr.sys: bootloader/stage2.asm bootloader/floppy.asm bootloader/fat12.asm
        $(AS) bootloader/stage2.asm -o krnldr.sys

boot.bin: bootloader/bootloader.asm bootloader/floppy.asm bootloader/fat12.asm
        $(AS) bootloader/bootloader.asm -o boot.bin

floppy.img: boot.bin
        dd if=/dev/zero of=floppy.img bs=512 count=2880
        sudo vnconfig vnd0 floppy.img
        echo "a\n\n\n\n\nw\nq\n" | sudo disklabel -E vnd0 
        sudo newfs_msdos -a 9 -b 512 -c 1 -e 224 -F 12 -f 1440 -h 2 -r 1 -S 512 -u 18 vnd0a
        mkdir -p floppy/
        sudo mount -t msdos /dev/vnd0a /mnt/
        sudo cp krnldr.sys kernel.sys /mnt/
        sudo umount /mnt/
        sudo vnconfig -u vnd0
        dd if=boot.bin of=floppy.img count=1 bs=512 conv=notrunc

clean:
        - rm *.bin *.sys *.img *.o *.a
and the makeconfig file included by it

Code: Select all

CPU=i386
CC=gcc
AS=nasm
ASFLAGS=-f elf32
CFLAGS=-fpic -std=c99 -ffreestanding -nostdlib -nostdinc -fno-builtin -fno-stack-protector -m32 -Iheaders/
LD=ld
LDFLAGS=-melf_i386 -static
This was attempted on openBSD 5.5 on an amd64 computer. In case anyone wants to build the version that ran on linux, here is the git repository for it: https://github.com/Jebes/gensokyo-os

Re: OpenBSD _global_offset_table_ woes

Posted: Sat Jun 28, 2014 11:49 pm
by Brynet-Inc
You're using OpenBSD's system compiler, don't do that. It's only meant to compile software for OpenBSD, several things are enabled by default like PIE (Position Independent Executables) and other security extensions as documented in gcc-local(1).

The same rules apply, follow the GCC_Cross-Compiler tutorial on the Wiki.

Note: You're clobbering your disk image with disklabel(8), just use vnd0c with newfs -t msdos. A pseudo label will be generated by the kernel.
Note 2: There's plenty more to pick apart, but it's already turd polishing.

Re: OpenBSD _global_offset_table_ woes

Posted: Sun Jun 29, 2014 6:17 am
by jebes
Thank you.

As you can tell, I'm still rather new at openBSD... and BSD in general.

Re: OpenBSD _global_offset_table_ woes

Posted: Sun Jun 29, 2014 6:43 am
by sortie
Hi jebes,

OpenBSD is great and all that. Its gcc has a lot of awesome protections enabled by default. However, you must use a cross-compiler regardless of what the host compiler does. In the long run it certainly would be useful to support such security measures in your operating system - but that's not important now. It looks like you have been following bad osdev tutorials that suggest using the host compiler and give a lot of other bad advise.

As Brynet-Inc points out, you will want to build yourself a cross-compiler for your operating system: http://wiki.osdev.org/GCC_Cross-Compiler

The osdev tutorials you have been following suggest you pass a lot of bad options, some unnecessary options, and might not be passing important options. Please follow the http://wiki.osdev.org/Bare_Bones tutorial and compile and link the files exactly as suggested there, adding only options not mentioned if you truly understand them and why you want to use it. You can safely delete options like -fpic -nostdinc -fno-builtin -fno-stack-protector -m32 -melf_i386 -static when using a cross-compiler. The -nostdlib option should only be passed when linking. You are not linking in libgcc.

In the case that you followed a particular tutorial, please also read the list of known bugs: http://wiki.osdev.org/James_Molloy%27s_ ... Known_Bugs