7

I have messed around a few times by making a small assembly boot loader on a floppy disk and was wondering if it's possible to make a boot loader in c++ and if so where might I begin? For all I know im not sure it would even use int main().

Thanks for any help.

2
  • possible duplicate of To write a bootloader in C or C++? Commented Jul 17, 2010 at 22:22
  • I believe there is a difference. This one asks how should I proceed and the other one is about choice between C and C++. But there is some overlap. Commented Jul 17, 2010 at 22:28

4 Answers 4

10

If you're writing a boot loader, you're essentially starting from nothing: a small chunk of code is loaded into memory, and executed. You can write the majority of your boot loader in C++, but you will need to bootstrap your own C++ runtime environment first.

Assembly is really the only option for the first stage, as you need to set up a sensible environment for running anything higher-level. Doing enough to run C code is fairly straightforward -- you need:

  • code and data loaded in the right place;
  • there may be an additional part of the data area which must be zero-initialised;
  • you need to point the stack pointer at a suitable area of memory for the stack.

Then you can jump into the code at an appropriate point (e.g. main()) and expect that the basic language features will work. (It's possible that any features of the standard library that may have been implemented or linked in might require additional initialisation at this stage.)

Getting a suitable environment going for C++ requires more effort, as it needs more initialisation here, and also has core language features which require runtime support (again, this is before considering library features). These include:

  • running static constructors;
  • memory allocation to support new and delete;
  • support for run-time type information (RTTI);
  • support for exceptions;
  • probably some other things I've forgotten to mention.

None of these are required until the C environment is up and running, so the code that handles these can be written in C rather than assembler (or even in a subset of C++ that does not make use of the above features).

(The same principles apply in embedded systems, and it's not uncommon for such systems to make use of C++, but only in a limited way -- e.g. no exceptions and/or RTTI because the runtime support isn't implemented.)

Sign up to request clarification or add additional context in comments.

Comments

3

It's been a while since I played with writing bootloaders, so I'm going off memory.

For an x86 bootloader, you need to have a C++ compiler that can emit x86 assembly, or, at the very least, you need to write your own preamble in 16-bit assembly that will put the CPU into 32-bit protected (or 64-bit long) mode, before you can call your C++ functions.

Once you've done that, though, you should be able to make use of most, if not all, of C++'s language features, so long as you stay away from things that require an underlying libc. But statically link everything without the CRT and you're golden.

Comments

2

Bootloaders don't have "int main()"s, unless you write assembly code to call it. If you are writing a stage 1 bootloader, then it is seriously discouraged.

Otherwise, the osdev.org has great documentation on the topic.
While it is probably possible to make a bootloader in C++, remember not to link your code to any dynamic libraries, and remember that just because it is C++, that doesn't mean you can/should use the STL, etc.

11 Comments

If it is C++, you can use the STL. That is end-of-story. If you can't use the STL, it's not C++.
If it has a tail it's a dog, if it has not it's not a dog end of story
@DeadMG You can't possibly be serious. STL is an API, not the language. It's a bootloader. Period. That means no "new"/"delete" keywords unless you explicitly define them, no File I/O, unless you explicitly define your own file functions. In others words, unless you statically link a standard library specifically designed for these purposes, or write your own STL, you won't be able to use almost anything from the STL. And, either way, it might be a bad idea to use it anyway.
@luiscubal: If you compile in C++, your compiler is obliged to provide you with a new/delete implementation. I'm not going to argue whether or not you should. But, those are facts of C++ life, that those implementations and those headers are mandatory of a C++ compiler. If you can compile C++ for the "bootloader" environment, then it's the compiler's job to solve the aforementioned problems and provide for you an STL to use. The technical difficulties of implementing the STL in a bootloader environment are not the problem of the user of said compiler- they just make it unlikely such exists.
@DeadMG new/delete are the C++ equivalent for malloc/calloc/free. If there is NO malloc/calloc/free, how do you expect C++ to provide you with those? There is NO new/delete in bootloader C++ unless you write your own new/delete operator implementations.
|
2

Yes it is possible. You have elements of answer and usefull links in this question

You also can have a look here, there is a C++ bootloader example.

The main thing to understand is that you need to create a flat binary instead of the usual fancy executable file formats (PE on windows, or ELF on Unixes), because these file format need an OS to load them, and in a boot loader you don't have an OS yet.

Using library is not a problem if you link statically (no dynamic link because again of the above executable problem). But obviously all OS API related entry points are not available...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.