public inbox for linux-8086@vger.kernel.org
 help / color / mirror / Atom feed
From: Alfonso <a.martone@retepnet.it>
To: linux-8086@vger.kernel.org
Subject: _main and startup code
Date: Wed, 12 Feb 2003 12:15:15 +0100	[thread overview]
Message-ID: <200302121215.15299.a.martone@retepnet.it> (raw)
In-Reply-To: <1044942743.1550.70.camel@Castle.goembel>

> Would the program start symbol also need to be _main,
> or is that only needed by the C runtime library?


The name "main" is internal to the C compiler and its libraries.

As of ELKS 0.1.1, an executable starts always at its CS:0000, where the 
bcc places the startup code (adjust stack parameters, call _main and 
then issue an "exit" syscall).

In the Minix-header there are two "Unused" fields. I found them defined 
somewhere as "starting address" and "lenght of symbol table (or DLL 
data) appended to the file".

Since the compiler sets them to zero by default, the patch below should 
be backward- and forward-compatible (I'm sorry, I don't know how to 
use those weird things like diff, patch, cvs, etc, so I show it to you 
in this jerky mode):


in the file  include/linuxmt/minix.h
change the line #23 from:
    unsigned long     unused;
to:
    unsigned long     startaddr;


in the file  fs/exec.c
add these lines after the line #348 (soon after "tregs->cs=cseg" line):
    tregs->ip = mh.startaddr;    /* guaranteed good at compile time */


This implements a "start address" eventually different from CS:0000.

But bcc won't support it. Every program compiled by bcc has a short 
initial startup section which rearranges argc/argv/envp parameters for 
calling _main (first part) and issue an _exit when main() returns 
(second part).

Yes, the "rearrange" could be easily implemented in the ELKS kernel. 
When the program terminates by exit() the second startup part is not 
needed. But if the main() returns a value, there is need for it.

An ugly hack could be patching the bcc in order to get the "main()" 
with a different stack frame; maybe something like:

_main: mov bp, sp                 ; opening code: not need to push bp

       [maybe: sub sp, NN -- stack space needed for local variables]
       [...rest of main()...]

       mov bx, ax                 ; closing code: bp is no more needed
       mov ax, 1
       int 0x80                   ; call exit(main())

This is transparent to the programmer except in the case of a recursive 
call to _main (well, in sixteen years of C programming I never found a 
case of a recursive _main call).

But this is an ugly hack because it needs a different stackframe for 
_main (the compiler should always check for the function name)...!


A decent hack should provide, when the main() returns, at least the 
three instructions of "closing code" of above. But this means either 
adding a "push return-address-to-closing-code" somewhere in the 
kernel, or... simply placing a "call _main -- closing code follows" at 
the beginning of the program (which can start at CS:0000 without any 
extra work).


I think I would not hack anything. The startup code often does a little 
more than calling _main and exiting with its return value. For example 
you could save in a static variable the envp so that a getenv() 
library function call can get in any moment (without _main support) an 
environment string.


These notes demostrate that, at least for C programs (not only bcc 
compiled programs), a "start address" different from CS:0000 is not 
really needed. Maybe a compiler for some other programming language 
could take some advantage from it.

We still did not discuss assembler-written programs. An assembler 
program does not always need to start at CS:0000 and/or with some 
startup code. The little patch of above seems suffice, but then I 
would add in the fs/exec.c a little extra check (to verify that 
mh.startaddr is less than mh.tseg).

      reply	other threads:[~2003-02-12 11:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-30 13:41 please help this newbie running asm programs!!! Seemanta Dutta
2003-01-30 16:06 ` Gábor Lénárt
2003-02-05 23:13 ` writing programs for elks Alfonso
     [not found]   ` <1044556130.1597.23.camel@Castle.goembel>
2003-02-07 12:22     ` writing programs for ELKS Alfonso
2003-02-07 21:05       ` Dan Olson
2003-02-05 23:16 ` please help this newbie running asm programs!!! Alfonso
2003-02-11  5:52   ` Phil Goembel
2003-02-12 11:15     ` Alfonso [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200302121215.15299.a.martone@retepnet.it \
    --to=a.martone@retepnet.it \
    --cc=linux-8086@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox