Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > alt.os.development > #9550

Re: Questions about the Implementation of the ELF format

From Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm>
Newsgroups alt.os.development
Subject Re: Questions about the Implementation of the ELF format
Date 2016-05-24 03:48 -0400
Organization Aioe.org NNTP Server
Message-ID <20160524034801.357f351a@_> (permalink)
References <06b38764-b3ee-4eed-9b82-f62c1d68b0ec@googlegroups.com>

Show all headers | View raw


On Mon, 23 May 2016 22:07:38 -0700 (PDT)
"Isaac D. Cohen" <8switchsoftware@gmail.com> wrote:

> In any case, I’m trying to figure out how to include C/C++ in my
> operating system.

1) in-lined assembly in C
2) link assembly object with C object

I tend to prefer #1 since you only need a few special instructions and
maybe one or two routines in assembly for an OS.  Everything else can
be done in C.  Having most of the code in C reduces compiler dependence
if you're using multiple C compilers.

Others tend to like the convenience of using an assembler of their
choice and linking the assembly with C, e.g. GCC C compiler with NASM
assembler instead of GNU AS (GAS).

Notice that I didn't mention C++.  My understanding is that if you use
C++ code in an OS that you must provide your own routines for "new" and
"delete" since C++ code frequently calls these.  If this is actually an
issue with C++, i.e., if the C++ code is not independent of them, then
you'll most likely know when you try to link the C++ code as
stand-alone code, i.e., missing new and delete functions, or it may
crash when executed.

I would stick with C or assembly for OS development.  I just happen to
find assembly to be more tedious, but some people, like Wolfgang,
perhaps Mike Gonta, have had excellent results with an assembly only
approach.  Many of the people who have posted here over many years seem
to develop from Linux using C.  I'm one of the rare ones to develop
from DOS using redundant C compilers.

The advantage of developing from DOS is that there isn't a protected OS
blocking you from running code that directly programs the hardware.
So, you can develop and test your OS code in DOS without having to boot
or re-boot your OS repeatedly.  You also don't have the issue of
incorrect emulation of various emulators.  E.g., I developed my mouse
driver, keyboard driver, and video driver, code to initialize and
reprogram PICs, RTC, video registers, mouse, keyboard, etc and even OS
setup such as GDT etc, all while running DOS.  After the code was
working, I merged the code into my OS.

> So I googled around and found that I have to cross-compile the
> C/C++ file into some kind of format, for example ELF. 

Um ...

Generally, C compilers have certain native formats which are needed by
the host OS.  E.g., Linux may only support a few flavors of ELF, e.g.,
32-bit and 64-bit, while an old DOS C compiler, like Watcom or
OpenWatcom may support a half-dozen formats, since many formats were
used for different reasons with DOS, historically.  So, you have no
choice but to produce code for the intended format(s) that can be used
by the OS, but C compilers can usually produce both executable and
object files.  The latter are linked together to create an executable
file.

> So my first question is, why? Why can’t the compiler just
> generate plain binary files?

So, I think the base answer is that compilers produce output that the
host OS can use, i.e., execute, otherwise they don't.  I.e., the file
must be executable by the OS, or linkable by the compiler into an
executable for that OS to be of use.  Sometimes C compilers have
utilities to strip the stub or header off of an executable or object
file.

AFAIK, 32-bit DOS C compilers, Linux C compilers, and Windows C
compilers can only produce executable and object formats.  However,
16-bit DOS C compilers can produce plain binary, since DOS can execute
16-bit .com files which are plain binary.  As an aside, it's also a
security risk to be able to execute raw code.  The OS isn't really able
to perform any safety checks or relocation of the code without a header.

Certain C compilers, like GCC for Linux, have switches needed to
produce stand-alone OS images, since said compilers are used to produce
the host OS.  Other C compilers, e.g., Windows or DOS, aren't used to
produce the host OS.  So, there is no need for them to have such
functionality.  If they could, it would be a major security concern.
E.g., imagine if you could modify, re-compile, and re-install Windows
10 yourself, or someone else could ...

Some C compilers have switches which will reduce or eliminate dependence
on the host's OS calls, but that has nothing to do with the object or
executable format.  That just makes sure your OS doesn't call an
external C library or CRT start-up code, which is half the problem of
producing stand-alone code for the OS.  The other half is getting the
code into a bootable format, e.g., ELF, COFF, Multiboot compliant, DOS
OMF, etc.

> Anyway, I quickly realized that I’m going to have to learn all about
> ELFs, because otherwise, I won’t know how to jump into the executable
> from an assembly file in my OS. So I got the ELF specification (from
> here: http://flint.cs.yale.edu/cs422/doc/ELF_Format.pdf), and began
> reading it.

Chris Giese ELF format summary
http://geezer.osdevbrasil.net/osd/exec/elf.txt

> Also, I thought it would be helpful to look at an ELF
> executable as I read the specification and follow along. So for
> example, if the specification says that an ELF file begins with 0x7F
> followed by the ASCII characters ‘E’, ‘L’ and ‘F’, I would take a
> look at the beginning of the ELF file and see if it has the 0x7F
> ‘ELF’. So I opened a 64-bit ELF executable compiled for Linux, in
> Bless Hex Editor and began to follow along. It did not take long
> until I noticed that the program does not appear to be following the
> specification.

...

> [ELF questions]

I'm not familiar with the ELF format.  I know that GRUB and GRUB4DOS
should load ELF format OSes.

I know that some executable formats also have an object format of the
same name, e.g., COFF has both COFF executables and COFF objects.
I.e., make sure you're looking at the correct data.


Rod Pemberton

Back to alt.os.development | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-23 22:07 -0700
  Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-24 03:48 -0400
  Questions about the Implementation of the ELF format Peter Cheung <mcheung63@gmail.com> - 2016-05-24 01:10 -0700
  Re: Questions about the Implementation of the ELF format "Alexei A. Frounze" <alexfrunews@gmail.com> - 2016-05-24 01:18 -0700
  Re: Questions about the Implementation of the ELF format "Mike Gonta" <mikegonta@gmail.com> - 2016-05-24 10:34 -0400
  Re: Questions about the Implementation of the ELF format Herbert Kleebauer <klee@unibwm.de> - 2016-05-24 17:24 +0200
  Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-24 14:18 -0700
    Re: Questions about the Implementation of the ELF format "Alexei A. Frounze" <alexfrunews@gmail.com> - 2016-05-25 02:21 -0700
  Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-25 15:02 -0700
    Re: Questions about the Implementation of the ELF format "Mike Gonta" <mikegonta@gmail.com> - 2016-05-25 19:03 -0400
      Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-26 14:51 -0700
        Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-27 02:55 -0400
          Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-27 13:43 -0700
            Re: Questions about the Implementation of the ELF format "Mike Gonta" <mikegonta@gmail.com> - 2016-05-27 17:40 -0400
              Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-05-28 20:15 -0700
                Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-29 02:35 -0400
                Re: Questions about the Implementation of the ELF format "Isaac D. Cohen" <8switchsoftware@gmail.com> - 2016-06-15 13:51 -0700
        Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-27 03:09 -0400
        Re: Questions about the Implementation of the ELF format "Alexei A. Frounze" <alexfrunews@gmail.com> - 2016-05-27 00:10 -0700
        Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-06-02 16:25 -0400
      Re: Questions about the Implementation of the ELF format Rod Pemberton <NoHaveNotOne@bcczxcfre.cmm> - 2016-05-27 02:38 -0400
        Re: Questions about the Implementation of the ELF format "Mike Gonta" <mikegonta@gmail.com> - 2016-05-27 04:11 -0400
    Re: Questions about the Implementation of the ELF format "Alexei A. Frounze" <alexfrunews@gmail.com> - 2016-05-25 22:18 -0700

csiph-web