map@ukc.UUCP (07/17/86)
Hello, is there anyone who knows, and is willing to explain what is meant by the following, taken from 'A Portable Fortran 77 Compiler', S.I.Feldman and P.J.Weinberger. '......the f77 and cc commands cause slightly different loading sequences to be generated, since Fortran programs need a few extra libraries and a different startup routine than do C programs' I am using a VAX with 4.2BSD and have been scratching my head as to what all this means. So far I have found that ld(1) says 'the entry point of the output is the beginning of the first routine' Fine, using 'nm -p' this implies that I am starting off with crt0.o (taken from /lib) for both C and f77. However, documentation for crt0.c says that it is machine dependent for C and from what I know it looks as if (for C at least) 'when the kernal starts it jumps to location 2' What I am after is a simple but *complete* explanation of how the different environments are set up so that I may in turn explain it to some very curious users (yes mom, I know it looks complicated but listen...) One thing that I would like explained is when I have compiled and loaded an f77 program and I use nm(1) there is a reference to main.o. I take this to be the one found in /usr/lib/libF77.a and looks like: main.o: U _MAIN_ U __cleanup U __flsbuf U _abort 00000218 D _act_fpe 00000268 D _act_ill U _exit U _f_exit U _f_init U _fprintf 00000180 D _id_libF77 00000000 T _main 00000198 D _sig_act 00000058 T _sigdie U _signal U _sigsetmask U _units 00000004 C _xargc 00000004 C _xargv is this where the *major* difference lies? i.e. by calling this the environment is initialized in a manner suited to f77. Any comments and further insight would be most welcome. Mark
sjc@mips.UUCP (07/21/86)
> Hello, is there anyone who knows, and is willing to explain what is > meant by the following, taken from 'A Portable Fortran 77 Compiler', > S.I.Feldman and P.J.Weinberger. > > '......the f77 and cc commands cause slightly different loading > sequences to be generated, since Fortran programs need a few extra libraries Under 4.2BSD, the "f77" and "cc" commands both tell the linker "ld" to place the file "crt0.o" (or "mcrt0.o" if you choose to use the "-p" option) at the beginning of your executable ("a.out") program. Both tell it to search "-lc" ("libc.a", the C library), but "f77" tells it to search "-lF77", "-lI77", "-lU77", and "-lm" (three Fortran-specific libraries and the Unix math library) prior to "-lc". Fortran needs additional libraries (a) to implement Fortran-specific features like read/write/print and the standard subroutines like csin and sinh, and (b) to permit you to call routines like "getenv (3f)" which resemble those in the C library but which use the Fortran calling conventions (arguments generally passed by reference) rather than the C calling conventions (arguments passed by value). When you define or reference a subroutine or function in C, the compiler constructs a symbol for it by prepending an "_". In Fortran, the compiler both prepends and appends an "_". This has two advantages: (a) names you invent within your program will not conflict with those in the C library, and (b) the Fortran libraries can provide wrappers for C library routines ("_getenv_" versus "_getenv"). "crt0.o" (or "mcrt0.o") jumps to "_main" to start your program. In C, the main program is always called "main" (which gets transformed to "_main" as just described). In Fortran, there's no way for you to create an "_main" (the closest you can come is "_main_"), so by providing a module "main.o" with an entry point called "_main", the compiler can guarantee that the runtime library gets initialized before your own code starts to execute. Among other things, it sets up signal handlers and arranges for the subroutine "getargv (3f)" to be able to access the command-line arguments. Then it transfers control to the entry point _MAIN_, which the compiler always puts at the beginning of the Fortran main program (even if you use a "program" statement to give it another name). If you enjoy peeking at what the compiler is doing, "f77 -S" is a useful technique. -- ...decwrl!mips!sjc Steve Correll
woods@hao.UUCP (07/21/86)
In article <1709@eagle.ukc.ac.uk>, map@ukc.ac.uk (M.A.Pralat) writes: > > '......the f77 and cc commands cause slightly different loading > sequences to be generated, since Fortran programs need a few extra libraries > and a different startup routine than do C programs' > My understanding is that when using ld(1), the entry point is ALWAYS called "main". In your f77 program, the main f77 routine is referred to as MAIN_ (any PROGRAM card is ignored). The startup routine for f77 programs is a C program that looks like main(argc,argv,envp) int argc; char **argv,**envp; { f_init(); MAIN_(); f_exit(); } The f_init and f_exit set up and close the F77 I/O drivers, which run with FORTRAN logical unit numbers on top of the usual UNIX file descriptors. This is similar to the stdio library, which uses stream pointers which run on top of the file descriptors, but of course the actual routines used are different. --Greg