comp.lang.ada
 help / color / mirror / Atom feed
* Setting up a bare metal x86 toolchain
@ 2019-06-23  1:25 ajxscc
  2019-06-23  9:08 ` Simon Wright
  2019-06-25 16:56 ` Shark8
  0 siblings, 2 replies; 6+ messages in thread
From: ajxscc @ 2019-06-23  1:25 UTC (permalink / raw)


I've recently begun exploring bare-metal development in Ada, with some great successes targeting bare-metal ARM using AdaCore's GPL compiler. I have some minimal experience with bare-metal x86 development, and would like to use Ada for this purpose. I was wondering what the best way to create an x86 bare-metal toolchain is.
I've seen Lucretia's 'Ada Bare Bones' tutorial on osdev.org, which provides some useful information about building the runtime. This is quite straightforward, but I'm a little unsure about how to build the cross compiler for the task.
Am I correct in my assumption that creating a 'freestanding' binary is done by compiling with the right kind of RTS? If I were to create/utilise a proper freestanding RTS, would it be suitable to use either the AdaCore or FSF GNAT targeting x86?
Any help understanding this would be greatly appreciated.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting up a bare metal x86 toolchain
  2019-06-23  1:25 Setting up a bare metal x86 toolchain ajxscc
@ 2019-06-23  9:08 ` Simon Wright
  2019-06-25  9:43   ` ff45t1z
  2019-06-25  9:44   ` ajxscc
  2019-06-25 16:56 ` Shark8
  1 sibling, 2 replies; 6+ messages in thread
From: Simon Wright @ 2019-06-23  9:08 UTC (permalink / raw)


ajxscc@gmail.com writes:

> I've recently begun exploring bare-metal development in Ada, with some
> great successes targeting bare-metal ARM using AdaCore's GPL
> compiler. I have some minimal experience with bare-metal x86
> development, and would like to use Ada for this purpose. I was
> wondering what the best way to create an x86 bare-metal toolchain is.
>
> I've seen Lucretia's 'Ada Bare Bones' tutorial on osdev.org, which
> provides some useful information about building the runtime. This is
> quite straightforward, but I'm a little unsure about how to build the
> cross compiler for the task.
>
> Am I correct in my assumption that creating a 'freestanding' binary is
> done by compiling with the right kind of RTS? If I were to
> create/utilise a proper freestanding RTS, would it be suitable to use
> either the AdaCore or FSF GNAT targeting x86?

In broad terms, hmm, but I think you're maybe developing on macOS? If
so, there's a possible problem with getting the assembler (clang
--cc1as) to do the right thing, and a definite problem with ld.

If you're on Linux, things should be a lot simpler (but I haven't
tried).

The following are all macOS-specific-ish.

$ /opt/gnat-ce-2019/bin/gcc -march=help -c dl.c
cc1: error: bad value ('help') for '-march=' switch
cc1: note: valid arguments to '-march=' switch are: nocona core2 nehalem
corei7 westmere sandybridge corei7-avx ivybridge core-avx-i haswell
core-avx2 broadwell skylake skylake-avx512 cannonlake icelake-client
icelake-server bonnell atom silvermont slm knl knm x86-64 eden-x2 nano
nano-1000 nano-2000 nano-3000 nano-x2 eden-x4 nano-x4 k8 k8-sse3 opteron
opteron-sse3 athlon64 athlon64-sse3 athlon-fx amdfam10 barcelona bdver1
bdver2 bdver3 bdver4 znver1 btver1 btver2 native

$ clang dl.c -march=help
error: unknown target CPU 'help'
note: valid target CPU values are: nocona, core2, penryn, bonnell, atom,
      silvermont, slm, goldmont, goldmont-plus, tremont, nehalem, corei7,
      westmere, sandybridge, corei7-avx, ivybridge, core-avx-i, haswell,
      core-avx2, broadwell, skylake, skylake-avx512, skx, cannonlake,
      icelake-client, icelake-server, knl, knm, k8, athlon64, athlon-fx,
      opteron, k8-sse3, athlon64-sse3, opteron-sse3, amdfam10, barcelona,
      btver1, btver2, bdver1, bdver2, bdver3, bdver4, znver1, x86-64

but what I can't at once see how to do is to get gcc to pass the
arcitecture option on to the assembler.

And in any case the macOS ld won't generate the sort of binary you'll
need for the bare machine; you need GNU ld.

So I think you'll need to do a full cross-compiler build.

I wrote up doing this for ARM at [1], and the current build scripts I've
used for ARM/GCC 9.1.0 are at [2].

In binutils.sh, you'd need to find out a replacement for --target that
covers the cpus you're going to support, and you won't need
--enable-interwork.

In gcc*.sh, again --target; I'm not sure what the x86 equivalent of
$MULTILIB_LIST (--with-multilib-list=rmprofile) would be - this says
"all Cortex-M cpus", basically.

In newlib.sh and gdb.sh, again --target.

[1]
https://forward-in-code.blogspot.com/2015/05/building-gcc-510-for-arm-eabi.html
[2] https://github.com/simonjwright/building-arm-eabi

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting up a bare metal x86 toolchain
  2019-06-23  9:08 ` Simon Wright
@ 2019-06-25  9:43   ` ff45t1z
  2019-06-25  9:44   ` ajxscc
  1 sibling, 0 replies; 6+ messages in thread
From: ff45t1z @ 2019-06-25  9:43 UTC (permalink / raw)


Hi Simon, thanks so much for taking the time to respond!
My host system is x86-64 Linux. So building a GCC cross-compiler isn't so difficult. From piecing together a few different answers I think I've answered some of my questions. It looks like I am able to build an x86 bare-metal executable using the native platform compiler. From what I understand, it appears that my assumption that compiling the equivalent of a 'freestanding' C executable in Ada is a matter of correctly configuring the RTS.
I actually already follow you on GitHub, and have already seen some of your example work on there, which has been a great resource! Thank you for having those resources there.

> but what I can't at once see how to do is to get gcc to pass the
arcitecture option on to the assembler.

From what I've seen FSF GNAT will accept the `-m32` switch, and it appears that AdaCore Gnat will also accept global configuration switches in the gprconfig as follows:
```
   package Builder is
      for Global_Compilation_Switches ("Ada") use ("-m32", "-march=i386");
      for Switches ("Ada") use ("-gnat2012", "-g", "-x", "-gnatg");
   end Builder;
```

Following the answer here: https://stackoverflow.com/questions/56721890/setting-up-a-bare-metal-x86-ada-toolchain I've been able to get a bare metal executable built using FSF GNAT. I'm still working on porting this over to the more modern AdaCore GNAT compiler.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting up a bare metal x86 toolchain
  2019-06-23  9:08 ` Simon Wright
  2019-06-25  9:43   ` ff45t1z
@ 2019-06-25  9:44   ` ajxscc
  1 sibling, 0 replies; 6+ messages in thread
From: ajxscc @ 2019-06-25  9:44 UTC (permalink / raw)


Hi Simon, thanks so much for taking the time to respond!
My host system is x86-64 Linux. So building a GCC cross-compiler isn't so difficult. From piecing together a few different answers I think I've answered some of my questions. It looks like I am able to build an x86 bare-metal executable using the native platform compiler. From what I understand, it appears that my assumption that compiling the equivalent of a 'freestanding' C executable in Ada is a matter of correctly configuring the RTS.
I actually already follow you on GitHub, and have already seen some of your example work on there, which has been a great resource! Thank you for having those resources there.

> but what I can't at once see how to do is to get gcc to pass the
arcitecture option on to the assembler.

From what I've seen FSF GNAT will accept the `-m32` switch, and it appears that AdaCore Gnat will also accept global configuration switches in the gprconfig as follows:
```
   package Builder is
      for Global_Compilation_Switches ("Ada") use ("-m32", "-march=i386");
      for Switches ("Ada") use ("-gnat2012", "-g", "-x", "-gnatg");
   end Builder;
```

Following the answer here: https://stackoverflow.com/questions/56721890/setting-up-a-bare-metal-x86-ada-toolchain I've been able to get a bare metal executable built using FSF GNAT. I'm still working on porting this over to the more modern AdaCore GNAT compiler.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting up a bare metal x86 toolchain
  2019-06-23  1:25 Setting up a bare metal x86 toolchain ajxscc
  2019-06-23  9:08 ` Simon Wright
@ 2019-06-25 16:56 ` Shark8
  2019-06-25 22:59   ` Lucretia
  1 sibling, 1 reply; 6+ messages in thread
From: Shark8 @ 2019-06-25 16:56 UTC (permalink / raw)


If you're doing bare-metal, you'll need a boot-loader. Some years ago I started writing an OS in BP7, and I found a boot-loader which would load an exe directly.

I'm 90% sure the program I used for this was BOOTPROG -- https://github.com/alexfru/BootProg -- but if it wasn't, then it was one of the programs in 'dsutil12.zip' here: http://cd.textfiles.com/simtel/simtel9703/disk2/DISC2/TURBOPAS/00_INDEX.HTM

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Setting up a bare metal x86 toolchain
  2019-06-25 16:56 ` Shark8
@ 2019-06-25 22:59   ` Lucretia
  0 siblings, 0 replies; 6+ messages in thread
From: Lucretia @ 2019-06-25 22:59 UTC (permalink / raw)


On Tuesday, 25 June 2019 17:56:43 UTC+1, Shark8  wrote:
> If you're doing bare-metal, you'll need a boot-loader. Some years ago I started writing an OS in BP7, and I found a boot-loader which would load an exe directly.

Grub2 works, my bare bones shows that too.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-06-25 22:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-23  1:25 Setting up a bare metal x86 toolchain ajxscc
2019-06-23  9:08 ` Simon Wright
2019-06-25  9:43   ` ff45t1z
2019-06-25  9:44   ` ajxscc
2019-06-25 16:56 ` Shark8
2019-06-25 22:59   ` Lucretia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox