Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site hadron.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxn!ihnp4!qantel!lll-crg!seismo!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.unix-wizards Subject: Re: magic numbers? (teach me, please) Message-ID: <99@hadron.UUCP> Date: Wed, 27-Nov-85 23:35:39 EST Article-I.D.: hadron.99 Posted: Wed Nov 27 23:35:39 1985 Date-Received: Fri, 29-Nov-85 10:39:05 EST References: <124@rexago1.UUCP> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 49 In article <124@rexago1.UUCP> rich@rexago1.UUCP (K. Richard Magill) writes: > 1) How does the shell (exec?) know whether the command I just typed > is a shell script or one of several possible types of > executable? Rarely is magic number testing done in the shell. Usually it is done in the kernel, at exec time. Exec() will test the first word for magicity (?), and directly execute it or not. Under 4BSD, the kernel also checks for "#!", and executes the program on the rest of the line with that file as input if it finds this "magic number." If the kernel doesn't execute a file, but the file is executable, the shell will try to execute a sub-shell with the file as input. Whether it does this first or goes down the PATH list looking for another executable file is shell-dependent -- I prefer the latter, personally. Note that in executing sub-shell's, the C shell on non-4BSD(-ish) systems will emulate the 4BSD kernel behaviour for files starting with "#!". > 2) Presuming the answer to #1 above has something to do with > magic numbers, who issues them? is there a common > (definitive) base of them or does each > manufacturer/environment make up their own set? Yes. Both. Both AT&T and Microsoft, and I believe others such as Intel and ISC (?) have issued definitive statements on what the common executable files should look like and what the magic numbers are. All, of course, are different. Look in /usr/include/a.out.h et al for information, and don't believe it until you've verified it, especially if you are unfortunate enough to have a binary-only system. On SysV, look in /etc/magic [I think] -- notice that several different numbers have the same labels; I changed them to read "old ...", "new ...", "COFF ...". If you have 'file.c', and the numbers are hard-coded, that is still a good guide. Provided your file.c compiles into the same as your /bin/file ... The very first original magic number (the old man said, stroking his long white beard) was 0407. This is equivalent to a 'br .+16' instruction in PDP-11 machine language -- useful if you want to execute a file stand-alone, since headers were 16 bytes long. Then came 410, 411, 413, 405, etc. -- many of which were not available on ordinary machines, but were lusted after by the more acquisitive members of the community, and in particular by the recipient of the coveted Golden Chicken award ... but I digress. ;-) -- Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}