Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Magic number (programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Origin {{anchor|Magic number origin}} === Format indicators were first used in early [[Version 7 Unix]] source code.{{citation needed|date=November 2019}} [[Unix]] was ported to one of the first [[Digital Equipment Corporation|DEC]] [[PDP-11]]/20s, which did not have [[memory protection]]. So early versions of Unix used the [[Position-independent code|relocatable memory reference]] model.<ref name="dmr">{{cite web |url=http://cm.bell-labs.com/cm/cs/who/dmr/odd.html |title=Odd Comments and Strange Doings in Unix |date=22 June 2002 |website=[[Bell Labs]] |url-status=dead |archive-url=https://web.archive.org/web/20061104034450/http://cm.bell-labs.com/cm/cs/who/dmr/odd.html |archive-date=2006-11-04}}</ref> Pre-[[Sixth Edition Unix]] versions read an executable file into [[magnetic-core memory|memory]] and jumped to the first low memory address of the program, [[relative address]] zero. With the development of [[Memory page|paged]] versions of Unix, a [[header (computing)|header]] was created to describe the [[executable|executable image]] components. Also, a [[branch instruction]] was inserted as the first word of the header to skip the header and start the program. In this way a program could be run in the older relocatable memory reference (regular) mode or in paged mode. As more executable formats were developed, new constants were added by incrementing the branch [[Offset (computer science)|offset]].<ref>Personal communication with Dennis M. Ritchie.</ref> In the [[Version 6 Unix|Sixth Edition]] [[Lions' Commentary on UNIX 6th Edition, with Source Code|source code]] of the Unix program loader, the exec() function read the executable ([[Binary numeral system|binary]]) image from the file system. The first 8 [[byte]]s of the file was a [[header (computing)|header]] containing the sizes of the program (text) and initialized (global) data areas. Also, the first 16-bit word of the header was compared to two [[constant (programming)|constant]]s to determine if the [[Executable|executable image]] contained [[Position-independent code|relocatable memory references]] (normal), the newly implemented [[Memory page|paged]] read-only executable image, or the separated instruction and data paged image.<ref name="V6sys1">{{cite web |url=https://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/sys/ken/sys1.c |title=The Unix Tree V6/usr/sys/ken/sys1.c |work=[[The Unix Heritage Society]] |archive-url=https://web.archive.org/web/20230326024616/https://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/sys/ken/sys1.c |archive-date=26 March 2023 |url-status=live }}</ref> There was no mention of the dual role of the header constant, but the high order byte of the constant was, in fact, the [[operation code]] for the PDP-11 branch instruction ([[octal]] 000407 or [[Hexadecimal|hex]] 0107). Adding seven to the program counter showed that if this constant was [[Executable|executed]], it would branch the Unix exec() service over the executable image eight byte header and start the program. Since the Sixth and Seventh Editions of Unix employed paging code, the dual role of the header constant was hidden. That is, the exec() service read the executable file header ([[Meta (prefix)|meta]]) data into a [[kernel space]] buffer, but read the executable image into [[user space]], thereby not using the constant's branching feature. Magic number creation was implemented in the Unix [[Linker (computing)|linker]] and [[Loader (computing)|loader]] and magic number branching was probably still used in the suite of [[Standalone program|stand-alone]] [[diagnostic program]]s that came with the Sixth and Seventh Editions. Thus, the header constant did provide an illusion and met the criteria for [[Magic (programming)|magic]]. In Version Seven Unix, the header constant was not tested directly, but assigned to a variable labeled '''ux_mag'''<ref name="V7sys1">{{cite web |url=https://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/sys/sys/sys1.c |title=The Unix Tree V7/usr/sys/sys/sys1.c |work=[[The Unix Heritage Society]] |archive-url=https://web.archive.org/web/20230326024632/https://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/sys/sys/sys1.c |archive-date=26 March 2023 |url-status=live }}</ref> and subsequently referred to as the '''magic number'''. Probably because of its uniqueness, the term '''magic number''' came to mean executable format type, then expanded to mean file system type, and expanded again to mean any type of file.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)