implementing a linux-based os from scratch, how to start?

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.
Octocontrabass
Member
Member
Posts: 5578
Joined: Mon Mar 25, 2013 7:01 pm

Re: implementing a linux-based os from scratch, how to start

Post by Octocontrabass »

bzt wrote:FYI, it looks like Clang can be compiled to support builtins the same way as gcc does, using so called VARIANTs, read the comment on this test case for example.
The comment about variants is discussing using the same test code to test two different built-in functions, where one function is tested when VARIANT is defined, and the other is tested when VARIANT is not defined, such as in this test case. Built-in functions are always available in Clang, the same way they're always available in GCC. Both compilers will assume your hosted implementation follows the standard C library API unless you tell them otherwise with -fno-builtin. Both compilers will warn you if you try to redefine a built-in function with the wrong signature.

Here's an example of a built-in function in Clang.
mmdmine
Member
Member
Posts: 47
Joined: Sat Dec 28, 2019 5:19 am
Location: Iran
Contact:

Re: implementing a linux-based os from scratch, how to start

Post by mmdmine »

I am not as serious as you on this project. I'll implement everything needed by compiler. no matter.
User avatar
bzt
Member
Member
Posts: 1584
Joined: Thu Oct 13, 2016 4:55 pm
Contact:

Re: implementing a linux-based os from scratch, how to start

Post by bzt »

Octocontrabass wrote:Built-in functions are always available in Clang, the same way they're always available in GCC.
Now this is not true, see the example I've liked before. There's a Makefile for gcc and another for Clang, so you can test the same source with both easily.
Octocontrabass wrote:Both compilers will assume your hosted implementation
That's the thing, we are not talking about hosted implementation, because you can't write a libc in a hosted environment, you must use freestanding.
Now I see why are you confused.

Cheers,
bzt
Octocontrabass
Member
Member
Posts: 5578
Joined: Mon Mar 25, 2013 7:01 pm

Re: implementing a linux-based os from scratch, how to start

Post by Octocontrabass »

bzt wrote:
Octocontrabass wrote:Built-in functions are always available in Clang, the same way they're always available in GCC.
Now this is not true, see the example I've liked before. There's a Makefile for gcc and another for Clang, so you can test the same source with both easily.
I didn't say they will always be inlined, just that they are always available. When they're not inlined, they behave as an ordinary function call. Did you try changing -O2 to -Os in your makefile yet?
bzt wrote:That's the thing, we are not talking about hosted implementation, because you can't write a libc in a hosted environment, you must use freestanding.
Why? As long as you're careful to avoid including old headers, I see no reason why you couldn't.
nullplan
Member
Member
Posts: 1794
Joined: Wed Aug 30, 2017 8:24 am

Re: implementing a linux-based os from scratch, how to start

Post by nullplan »

Octocontrabass wrote:Why? As long as you're careful to avoid including old headers, I see no reason why you couldn't.
Because, in a hosted environment, all the header files and all the identifiers from chapter 7 are reserved. Defining a function called "printf" is undefined behavior in hosted mode, since the identifier is reserved. Creating a "stdio.h" is undefined for the same reason. Etc. pp.
Carpe diem!
Octocontrabass
Member
Member
Posts: 5578
Joined: Mon Mar 25, 2013 7:01 pm

Re: implementing a linux-based os from scratch, how to start

Post by Octocontrabass »

That's fair. GCC and Clang seem to assume you'll compile your standard C library in hosted mode, though.
Post Reply