]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libmach/setmach.c
libmach: support disassembling from memory
[plan9front.git] / sys / src / libmach / setmach.c
1 #include        <u.h>
2 #include        <libc.h>
3 #include        <bio.h>
4 #include        <mach.h>
5                 /* table for selecting machine-dependent parameters */
6
7 typedef struct machtab Machtab;
8
9 struct machtab
10 {
11         char            *name;                  /* machine name */
12         short           type;                   /* executable type */
13         short           boottype;               /* bootable type */
14         int             asstype;                /* disassembler code */
15         Mach            *mach;                  /* machine description */
16         Machdata        *machdata;              /* machine functions */
17 };
18
19 extern  Mach            mmips, msparc, m68020, mi386, mamd64,
20                         marm, mmips2be, mmips2le, mpower, mpower64, msparc64;
21 extern  Machdata        mipsmach, mipsmachle, sparcmach, m68020mach, i386mach,
22                         armmach, mipsmach2le, powermach, sparc64mach;
23
24 /*
25  *      machine selection table.  machines with native disassemblers should
26  *      follow the plan 9 variant in the table; native modes are selectable
27  *      only by name.
28  */
29 Machtab machines[] =
30 {
31         {       "68020",                        /*68020*/
32                 F68020,
33                 F68020B,
34                 A68020,
35                 &m68020,
36                 &m68020mach,    },
37         {       "68020",                        /*Next 68040 bootable*/
38                 F68020,
39                 FNEXTB,
40                 A68020,
41                 &m68020,
42                 &m68020mach,    },
43         {       "mips2LE",                      /*plan 9 mips2 little endian*/
44                 FMIPS2LE,
45                 0,
46                 AMIPS,
47                 &mmips2le,
48                 &mipsmach2le,   },
49         {       "mipsLE",                               /*plan 9 mips little endian*/
50                 FMIPSLE,
51                 0,
52                 AMIPS,
53                 &mmips,
54                 &mipsmachle,    },
55         {       "mips",                         /*plan 9 mips*/
56                 FMIPS,
57                 FMIPSB,
58                 AMIPS,
59                 &mmips,
60                 &mipsmach,      },
61         {       "mips2",                        /*plan 9 mips2*/
62                 FMIPS2BE,
63                 FMIPSB,
64                 AMIPS,
65                 &mmips2be,
66                 &mipsmach,      },              /* shares debuggers with native mips */
67         {       "mipsco",                       /*native mips - must follow plan 9*/
68                 FMIPS,
69                 FMIPSB,
70                 AMIPSCO,
71                 &mmips,
72                 &mipsmach,      },
73         {       "sparc",                        /*plan 9 sparc */
74                 FSPARC,
75                 FSPARCB,
76                 ASPARC,
77                 &msparc,
78                 &sparcmach,     },
79         {       "sunsparc",                     /*native sparc - must follow plan 9*/
80                 FSPARC,
81                 FSPARCB,
82                 ASUNSPARC,
83                 &msparc,
84                 &sparcmach,     },
85         {       "386",                          /*plan 9 386*/
86                 FI386,
87                 FI386B,
88                 AI386,
89                 &mi386,
90                 &i386mach,      },
91         {       "86",                           /*8086 - a peach of a machine*/
92                 FI386,
93                 FI386B,
94                 AI8086,
95                 &mi386,
96                 &i386mach,      },
97         {       "amd64",                        /*amd64*/
98                 FAMD64,
99                 FAMD64B,
100                 AAMD64,
101                 &mamd64,
102                 &i386mach,      },
103         {       "arm",                          /*ARM*/
104                 FARM,
105                 FARMB,
106                 AARM,
107                 &marm,
108                 &armmach,       },
109         {       "power",                        /*PowerPC*/
110                 FPOWER,
111                 FPOWERB,
112                 APOWER,
113                 &mpower,
114                 &powermach,     },
115         {       "power64",                      /*PowerPC*/
116                 FPOWER64,
117                 FPOWER64B,
118                 APOWER64,
119                 &mpower64,
120                 &powermach,     },
121         {       "sparc64",                      /*plan 9 sparc64 */
122                 FSPARC64,
123                 FSPARCB,                        /* XXX? */
124                 ASPARC64,
125                 &msparc64,
126                 &sparc64mach,   },
127         {       0               },              /*the terminator*/
128 };
129
130 /*
131  *      select a machine by executable file type
132  */
133 void
134 machbytype(int type)
135 {
136         Machtab *mp;
137
138         for (mp = machines; mp->name; mp++){
139                 if (mp->type == type || mp->boottype == type) {
140                         asstype = mp->asstype;
141                         machdata = mp->machdata;
142                         break;
143                 }
144         }
145 }
146 /*
147  *      select a machine by name
148  */
149 int
150 machbyname(char *name)
151 {
152         Machtab *mp;
153
154         if (!name) {
155                 asstype = AMIPS;
156                 machdata = &mipsmach;
157                 mach = &mmips;
158                 return 1;
159         }
160         for (mp = machines; mp->name; mp++){
161                 if (strcmp(mp->name, name) == 0) {
162                         asstype = mp->asstype;
163                         machdata = mp->machdata;
164                         mach = mp->mach;
165                         return 1;
166                 }
167         }
168         return 0;
169 }