magic numbers? (teach me, please)
Joseph S. D. Yao
jsdy at hadron.UUCP
Thu Nov 28 14:35:39 AEST 1985
In article <124 at rexago1.UUCP> rich at 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 at seismo.{CSS.GOV,ARPA,UUCP}
More information about the Comp.unix.wizards
mailing list