3

So I'm following this tutorial on rolling out your own toy unix.

I'm stuck at compiling the sample source on this page. This is a download link to the sample source code

The Makefile looks like this:

SOURCES=boot.o main.o
CC=gcc
CFLAGS=-nostdlib -nostdinc -fno-builtin -fno-stack-protector
LDFLAGS=-Tlink.ld
ASFLAGS=-felf

all: $(SOURCES) link
clean:
    -rm *.o kernel
link:
    ld $(LDFLAGS) -o kernel $(SOURCES)
.s.o:
    nasm $(ASFLAGS) $<

On OSX I couldn't manage to make because Apple's ld doesn't support a -T switch. I tried compiling on CentOS and I got this:

[gideon@centosbox src]$ make 
nasm -felf boot.s
cc -nostdlib -nostdinc -fno-builtin -fno-stack-protector   -c -o main.o main.c
main.c:4: warning: 'struct multiboot' declared inside parameter list
main.c:4: warning: its scope is only this definition or declaration, which is probably not what you want
ld -Tlink.ld -o kernel boot.o main.o
boot.o: could not read symbols: File in wrong format
make: *** [link] Error 1

Of course my only problem is File in wrong format.

What does this mean? How do I correct it?

gideon
  • 353

1 Answers1

5

These sound like you're mixing code for different architectures. See here: could not read symbols, file in wrong format.

excerpt

You get that error when you change architectures. Are those CHOST/CFLAGS settings new?

Questions

  • I wonder if the Toy Unix OS can only be built on x86 and not x64. Something to look into.
  • make clean. Did the files you're using on CentOS come from OSX in any way?
  • I see boot.o being used but don't see it being compiled, unless the nasm -felf boot.s builds it, so perhaps it's the OSX version of this file.

The fix

If you take a look at the options for nasm you'll notice that it's using the switches:

$ nasm -felf boot.s

The elf format is a 32-bit format. On newer systems that are 64-bit you need to change this to elf64. You can see these formats using the nasm -hf option:

$ nasm -hf
...
valid output formats for -f are (`*' denotes default):
  * bin       flat-form binary files (e.g. DOS .COM, .SYS)
    ith       Intel hex
    srec      Motorola S-records
    aout      Linux a.out object files
    aoutb     NetBSD/FreeBSD a.out object files
    coff      COFF (i386) object files (e.g. DJGPP for DOS)
    elf32     ELF32 (i386) object files (e.g. Linux)
    elf64     ELF64 (x86_64) object files (e.g. Linux)
    as86      Linux as86 (bin86 version 0.3) object files
...

So changing this line in the Make file:

ASFLAGS=-felf64

and re-running make:

$ make
ld -Tlink.ld -o kernel boot.o main.o
$

solves the problem.

slm
  • 369,824
  • I'm not sure about (1), I will find out. (2) I did do a make clean. (3) : Yea, seems like there aren't instructions on how to build boot.o. Thanks for your answer, I'm somewhat new to this stuff. – gideon Oct 17 '13 at 03:36
  • @gideon - np. I hadn't heard of this OS before either, will be checking it out as well, looked interesting in learning a lot of the basics. – slm Oct 17 '13 at 03:40
  • @gideon - I just tried it and get the same error as you. BTW the line nasm -felf boot.s builds boot.o. – slm Oct 17 '13 at 03:44
  • so are you saying that it only will compile on an x86 machine ? – gideon Oct 17 '13 at 03:46
  • @gideon - i figured it out. See updates. – slm Oct 17 '13 at 03:52