Same song, second verse (was Re: Q: Changing C entry point from main)

Same song, second verse (was Re: Q: Changing C entry point from main)

Post by William E. Ho » Thu, 31 Aug 1995 04:00:00

Cross posted from comp.unix.programmer,comp.sys.sgi.misc.   Sorry.

In a closely related subject....

Basic Scenerio (SUN SPARC/SunOS 4.1.3 & Solaris):
I've got an Ada main calling a C function, word().  The C function needs to handle
misaligned data.

It works if I compile like so:

   acc -misalign main.c word.c

The following fails to work (with the Sun (Rational/Verdix) Ada compiler):

   acc -misalign -c word.c
   ada adaMain.a
   a.ld adaMain word.o  (Where adaMain calls word())

From using the -v option with acc, I see that /apps/lang/SC3.0.1/lib/crt0.o
is replaced by /apps/lang/SC3.0.1/lib/misalign/crt0.o

(For those following along with cc, it's /usr/lib/crt0.o & /usr/lib/misalign/crt0.o)

What is in .../misalign/crt0.o that I could call from the Ada program to set
up the handling of misaligned data?

Any hints, ideas or straws would be appreciated.

The example code has been cut-down from the original program, or dreamed up
(I didn't do it, can't prove it, nobody saw me...  )

Example source code:

  procedure adamain is

    procedure cmain;
    pragma interface(c, cmain);
    pragma interface_name(cmain, "_cmain");

  end adamain;

#include <stdio.h>

struct {
   unsigned byte0 : 8;
   unsigned byte1 : 8;
   unsigned byte2 : 8;
   unsigned byte3 : 8;
   unsigned byte4 : 8;
   unsigned byte5 : 8;
   unsigned byte6 : 8;
   unsigned byte7 : 8;
   } two_words;

void cmain()
   unsigned           integer;
   unsigned           *ptr;
   unsigned char      byte;
   unsigned short int halfword;

   two_words.byte0 = 0xDE;
   two_words.byte1 = 0xAD;
   two_words.byte2 = 0xBE;
   two_words.byte3 = 0xEF;

   two_words.byte4 = 0x60;
   two_words.byte5 = 0x0D;
   two_words.byte6 = 0xF0;
   two_words.byte7 = 0x0D;

   byte = two_words.byte3;
   halfword = two_words.byte3;
   ptr = (unsigned *) ((unsigned) &two_words + 2);
   integer = *ptr;

printf ("Made it!\n");


main (int argc, char * argv [])
   cmain ();

||  Bill Hope                 ||  All Opinions are personal and have not   ||
||  DSEG Software Automation  ||     been approved by Upper Management.    ||
||  Texas Instruments, Inc.   ||                                           ||

||  voice: 214.995.5618       ||     Others may have more accurate Facts.  ||


1. Q: Changing C entry point from main() to something else

I would like to find an easy and portable way to specify a different
entry point into my C program that is other than main().

Consider the simple C program below.  If I compile with "cc main.c"
and then run the a.out, the function main() is executed, as expected.

What I want is something that allows me to set the "main" function as
being secondmain() instead.  So, ideally, something like
"cc <voodoo magic flags> main.c" will result in an a.out that, when
executed, will run secondmain() and then exit.  main() is never run in
this special a.out.

Why do I want this?  I'm porting a programming environment that needs
to do some initialization with the command line arguments before turning
control to the user's main().  However, I want to make it possible for
the user to still have the symbol main() in his code.

So far, I have been playing with cc under Irix 5.3.  I've tried both
the -e and -init flags with the loader (ld), but they jump to
secondmain() before argc, argv and envp have been properly initialized,
which is not what I want.

I'm most interested in a solution that works under Irix, but I'm also
interested in a general Unix solution.  Ideally, such a solution would
avoid having to rewrite the executable file or having to modify code
at run time.

Any ideas/suggestions welcome.  Thanks in advance.  I'll summarize
the responses, if there is interest.



----- cut -----
#include <stdio.h>

int main( int argc, char ** argv, char ** envp )
        int     i;

        printf( "Inside main:  argc %d argv %x envp %x\n", argc, argv, envp );

        for( i = 0; i < argc; i++ )
                printf( "argv[ %d ] = %s\n", i, argv[ i ] );

int secondmain( int argc, char ** argv, char ** envp )
        int     i;

        printf( "Inside secondmain:  argc %d argv %x envp %x\n",
                argc, argv, envp );

        for( i = 0; i < argc; i++ )
                printf( "argv[ %d ] = %s\n", i, argv[ i ] );

2. Audio support

3. Linux verses FreeBSD verses MS

4. Modem problem in PPP

5. Changing program entry point -help

6. Unix sys admin looking for advice on Lixus distro for home pc...

7. change the entry point of a C program

8. Debian: Removing useless things

9. Changing , transparently, a C program Entry point

10. tunnel point to point vs physical point to point

11. Vhosts are still pointing to main directory

12. Two Debian 2.0 Qs

13. For Two NIC -> Two Entries in /etc/hosts