Minimal changes to get m6502 to compile (won't work)
[Apout.git] / magic.c
1 /* magic.c - determine the environment for certain PDP-11 a.out binaries
2  *
3  * Some binaries in V1, V2, V5, V6, V7 and 2.11BSD are not caught with the
4  * magic numbers in aout.c. If this is the case, we fall into the
5  * special_magic() function, which calculates a checksum on the
6  * a.out header. If it matches any of the checksums below, it returns
7  * the appropriate environment value. Otherwise, it returns IS_UNKNOWN.
8  *
9  * $Revision: 1.15 $
10  * $Date: 2008/05/15 03:20:52 $
11  */
12 #include "defines.h"
13
14 struct spec_aout {
15     u_int32_t cksum;
16     int environment;
17 };
18
19 static struct spec_aout S[] = {
20     {0x1042c2, IS_V6},          /* V6 bin/dc */
21     {0x10f02, IS_V5},           /* V5 etc/update */
22     {0x11002, IS_V5},           /* V5 bin/clri */
23     {0x1117c2, IS_V7},          /* V7 bin/roff */
24     {0x11702, IS_V6},           /* V6 etc/update */
25     {0x11a82, IS_V5},           /* V5 bin/sum */
26     {0x1319c2, IS_V5},          /* V5 usr/fort/fc1 */
27     {0x1332c2, IS_V2},          /* /lib/c0 dated Jun 30 1973 from s2 tape */
28     {0x13642, IS_V5},           /* V5 bin/rew */
29     {0x139e02, IS_V5},          /* V5 bin/dc */
30     {0x13c0, IS_V6},            /* V6 usr/lib/tmgc */
31     {0x14042, IS_V6},           /* V6 bin/tty */
32     {0x143c2, IS_V5},           /* V5 bin/tty */
33     {0x152ac2, IS_V6},          /* V6 usr/lib/tmg */
34     {0x15f42, IS_V5},           /* V5 bin/kill */
35     {0x16802, IS_V5},           /* V5 bin/dsw */
36     {0x16902, IS_V5},           /* V5 bin/mkdir */
37     {0x1720c2, IS_V6},          /* V6 bin/cdb */
38     {0x17742, IS_V5},           /* V5 usr/bin/pfe */
39     {0x17cc2, IS_V5},           /* V5 usr/bin/mesg */
40     {0x18702, IS_V5},           /* V5 bin/rmdir */
41     {0x194c2, IS_V6},           /* V6 bin/chgrp */
42     {0x197c2, IS_V6},           /* V6 bin/chown */
43     {0x19a42, IS_V5},           /* V5 bin/chown */
44     {0x19b342, IS_V6},          /* V6 usr/bin/nroff */
45     {0x19f682, IS_V6},          /* V6 usr/fort/fc1 */
46     {0x1ae00, IS_V2},           /* V2 bin/strip */
47     {0x1b102, IS_V5},           /* V5 bin/strip */
48     {0x1ba02, IS_V6},           /* V6 bin/strip */
49     {0x1c342, IS_V5},           /* V5 bin/cat */
50     {0x1c8442, IS_V7},          /* V7 usr/games/maze */
51     {0x1cc782, IS_V6},          /* V6 lib/fc0 */
52     {0x1dfc2, IS_V5},           /* V5 etc/getty */
53     {0x1f9c2, IS_V2},           /* /bin/nm dated Jun 30 1973 from s2 tape */
54     {0x20202, IS_V5},           /* V5 usr/games/bj */
55     {0x21e42, IS_V6},           /* V6 usr/bin/units */
56     {0x23f82, IS_V5},           /* V5 usr/bin/passwd */
57     {0x260642, IS_V6},          /* V6 lib/fc1 */
58     {0x262a82, IS_211BSD},      /* 2.11 usr/new/m11 */
59     {0x27e82, IS_V5},           /* V5 usr/bin/grep */
60     {0x290c2, IS_V7},           /* V7 usr/games/cubic */
61     {0x299c2, IS_V5},           /* V5 usr/games/cubic */
62     {0x2f482, IS_V5},           /* V5 usr/bin/form */
63     {0x3382, IS_V6},            /* V6 bin/write */
64     {0x326642, IS_V7},          /* 2.9 awk */
65     {0x33c42, IS_211BSD},       /* 2.11 usr/games/moo */
66     {0x351382, IS_211BSD},      /* 2.11 usr/games/lib/zork */
67     {0x3702, IS_V5},            /* V5 usr/games/moo */
68     {0x3b402, IS_V5},           /* V5 bin/ar */
69     {0x3cc02, IS_V2},           /* /bin/size from from s2 tape */
70     {0x4382, IS_V5},            /* V5 bin/write */
71     {0x451f42, IS_V7},          /* 2.9 /lib/c1 */
72     {0x47042, IS_211BSD},       /* 2.11 usr/games/ttt */
73     {0x4fa02, IS_V5},           /* V5 bin/ld */
74     {0x51342, IS_211BSD},       /* 2.11 usr/games/bj */
75     {0x53302, IS_V6},           /* V6 usr/lib/suftab */
76     {0x55882, IS_V7},           /* 2.9 /bin/as */
77     {0x54702, IS_V5},           /* V5 usr/games/ttt */
78     {0x55702, IS_V7},           /* V7 bin/as */
79     {0x5c342, IS_V2},           /* /bin/cc dated Jun 30 1973 from s2 tape */
80     {0x6f742, IS_V6},           /* V6 usr/bin/sa */
81     {0x7042, IS_V7},            /* V7 bin/factor */
82     {0x71702, IS_V7},           /* V7 lib/as2 */
83     {0x7342, IS_V5},            /* V5 bin/du */
84     {0x73782, IS_V7},           /* 2.9 /lib/as2 */
85     {0x73e00, IS_V2},           /* /bin/ld from s2 tape */
86     {0x7a242, IS_V6},           /* V6 lib/as2 */
87     {0x7b102, IS_V6},           /* V6 bin/as */
88     {0x7d082, IS_V5},           /* V5 bin/as */
89     {0x7d6844, IS_V1},          /* bin/cal from s2 tape */
90     {0x7d942, IS_V5},           /* V5 lib/as2 */
91     {0x8002, IS_V5},            /* V5 etc/lpd */
92     {0x85842, IS_V5},           /* V5 bin/ed */
93     {0x8f00, IS_V6},            /* V6 usr/lib/tmga */
94     {0x915c2, IS_V6},           /* V6 bin/bas */
95     {0x94542, IS_V5},           /* V5 bin/db */
96     {0x98442, IS_V6},           /* V6 usr/bin/ac */
97     {0x9adc2, IS_V6},           /* V6 bin/db */
98     {0xa242, IS_V7},            /* V7 bin/primes */
99     {0xa4602, IS_V2},           /* /bin/as from s2 tape */
100     {0xa702, IS_V5},            /* V5 bin/time */
101     {0xad882, IS_V7},           /* V7 bin/bas */
102     {0xadc42, IS_V2},           /* /usr/lib/c1 from s2 tape */
103     {0xb5a82, IS_V6},           /* V6 usr/bin/prof */
104     {0xc1e42, IS_V5},           /* V5 usr/bin/fed */
105     {0xc3102, IS_V6},           /* V6 bin/tp */
106     {0xc8bc2, IS_V5},           /* V5 bin/tp */
107     {0xe1642, IS_V6},           /* V6 usr/bin/roff */
108     {0xe1f42, IS_V5},           /* V5 usr/bin/roff */
109     {0xec582, IS_V5},           /* V5 bin/bas */
110     {0xfc2, IS_V6},             /* V6 usr/bin/typo */
111     {0xfc002, IS_V2},           /* /bin/as dated Jun 30 1973 from s2 tape */
112     {0x38ec0, IS_V5},           /* V5 bin/ar, Warrens */
113     {0, 0}
114 };
115
116 /* cptr points at the start of the a.out header */
117 int special_magic(u_int16_t * cptr)
118 {
119     u_int32_t cksum = 0;
120     int i;
121     char *unix_version;
122
123     if (cptr == NULL)
124         return (IS_UNKNOWN);
125     /* Calculate the checksum */
126     for (i = 0; i < 8; i++) {
127         cksum ^= cptr[i];
128         cksum = cksum << 1;
129     }
130
131     /* Try and find a match */
132     for (i = 0; S[i].cksum != 0; i++)
133         if (S[i].cksum == cksum) {
134             TrapDebug((dbg_file, "This a.out has special magic %d\n", i));
135             return (S[i].environment);
136         }
137
138     /* See if user tells us what version to use */
139     if ((unix_version = getenv("APOUT_UNIX_VERSION"))) {
140         if (!strcmp(unix_version, "V1"))
141             return (IS_V1);
142         if (!strcmp(unix_version, "V2"))
143             return (IS_V2);
144         if (!strcmp(unix_version, "V3"))
145             return (IS_V3);
146         if (!strcmp(unix_version, "V4"))
147             return (IS_V4);
148         if (!strcmp(unix_version, "V5"))
149             return (IS_V5);
150         if (!strcmp(unix_version, "V6"))
151             return (IS_V6);
152         if (!strcmp(unix_version, "V7"))
153             return (IS_V7);
154         if (!strcmp(unix_version, "2.9BSD"))
155             return (IS_29BSD);
156         if (!strcmp(unix_version, "2.11BSD"))
157             return (IS_211BSD);
158     }
159
160     /* We can't tell what version of Unix, give up */
161     (void) printf("Apout - unknown magic in header: 0x%x\n", cksum);
162     return (IS_UNKNOWN);
163 }