3
/* test.c */

void func1()
{

}

int main()
{
   func1();
}

Hello, I am making kernel code using C. But I tested above code to know how to build C kernel code. Below command is what I gave to prompt. I am using MinGW on Windows 8.1.

gcc -c -m32 test.c
ld -o test -Ttext 0x00 -e _main test.o

But this error was occurred from ld.

test.o:test.c:(.text+0x7): undefined reference to `__main'

So, I tried different way. add -nostdlib and --freestanding option to gcc. But the result was same. Is __main function in CRT0 ? What should I do to solve this problem.. ?

knolz
  • 181
  • 4
  • 10

2 Answers2

2

The only viable way if you're really into operating system development is by using some Unix-like OS like GNU/Linux or Mac OS X.

The following two are a must:

-ffreestanding -nostdlib -lgcc

Then things like -Wall, -Wextra, and -Werror are recommended because bugs in kernel code are extremely hard to debug.

With respect to the entry point, you usually use a linker script that you pass to ld via -T linker.ld. For example, mine (don't copy paste it!) looks as follows. It's for a higher-half kernel with support for virtual memory:

ENTRY(__start__)
OUTPUT_FORMAT(elf32-i386)

SECTIONS {
    . = 0xC0100000;

    .text BLOCK(4K) : AT(ADDR(.text) - 0xC0000000) {
        KEEP(*(.multiboot))
        KEEP(*(.boot))
        *(.text)
    }

    .rodata ALIGN(0x1000) : AT(ADDR(.rodata) - 0xC0000000) {
        *(.rodata*)
    }

    .data ALIGN(0x1000) : AT(ADDR(.data) - 0xC0000000) {
        *(.data)
    }

    .bss : AT(ADDR(.bss) - 0xC0000000) {
        *(COMMON)
        *(.bss)
        *(.stack)
    }

    __kend__ = .;
}
user229044
  • 232,980
  • 40
  • 330
  • 338
3442
  • 8,248
  • 2
  • 19
  • 41
0

You could use gcc instead of ld to perform the linking:

gcc -o test test.o -nostdlib -lgcc

The -lgcc option provides the __main function.

Timma
  • 663
  • 7
  • 19
  • This answer is not accurate if you don't know what a [kernel](https://en.wikipedia.org/wiki/Kernel_(operating_system)) is, and it's definitively **not** an [application](https://en.wikipedia.org/wiki/Application_software) – 3442 Aug 23 '15 at 09:18
  • @KemyLand I was too vague with what I was trying to ask, I will fix this. What I meant is, which kernel is he referring to? If I knew that, I could try and workout what linker options can be used to omit unnecessary libraries. But given his example, it looks like he's trying to build an application, contrary to the claim of building *C kernel code*. – Timma Aug 23 '15 at 09:37
  • 2
    The op *is* building kernel code. The kernel smell is everywhere! In `-nostdlib`, `-ffreestanding`, and even `-Ttext 0x00`. I'm not sure if you've the required knowledge to answer this question (not to insult you in any way, and to avoid flamewars: I'm saying this because you said "What I meant is, which kernel is he refering to?" (his own, of course!)), as there's too much theory (and practice) behind OSDeving that its simply inappropiate to go and find the options in `ld`'s man pages. **Edit**: You did however got the `-lgcc` right :). – 3442 Aug 23 '15 at 09:43
  • @KemyLand you are right. I totally misinterpreted what op was trying to do. I jumped to conclusions because I have seen many times someone try to build an app and link using `ld` directly, ending up with undefined references and then throw other options at it to solve it. That's definitely not what is happening here. – Timma Aug 23 '15 at 10:12