Commit | Line | Data |
---|---|---|
5bd67f35 | 1 | /* ARC target-dependent stuff. Extension structure access functions |
53c9ebc5 AM |
2 | Copyright 1995, 1997, 2000, 2001, 2004, 2005 |
3 | Free Software Foundation, Inc. | |
0d2bcfaf NC |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
f4321104 | 19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
0d2bcfaf | 20 | |
5bd67f35 | 21 | #include "sysdep.h" |
0d2bcfaf NC |
22 | #include <stdlib.h> |
23 | #include <stdio.h> | |
24 | #include "bfd.h" | |
25 | #include "arc-ext.h" | |
26 | #include "libiberty.h" | |
27 | ||
28 | /* Extension structure */ | |
29 | static struct arcExtMap arc_extension_map; | |
30 | ||
31 | /* Get the name of an extension instruction. */ | |
32 | ||
33 | const char * | |
5bd67f35 | 34 | arcExtMap_instName(int opcode, int minor, int *flags) |
0d2bcfaf | 35 | { |
5bd67f35 | 36 | if (opcode == 3) |
0d2bcfaf NC |
37 | { |
38 | /* FIXME: ??? need to also check 0/1/2 in bit0 for (3f) brk/sleep/swi */ | |
39 | if (minor < 0x09 || minor == 0x3f) | |
40 | return 0; | |
41 | else | |
42 | opcode = 0x1f - 0x10 + minor - 0x09 + 1; | |
43 | } | |
44 | else | |
45 | if (opcode < 0x10) | |
46 | return 0; | |
47 | else | |
48 | opcode -= 0x10; | |
49 | if (!arc_extension_map.instructions[opcode]) | |
50 | return 0; | |
51 | *flags = arc_extension_map.instructions[opcode]->flags; | |
52 | return arc_extension_map.instructions[opcode]->name; | |
53 | } | |
54 | ||
55 | /* Get the name of an extension core register. */ | |
56 | ||
57 | const char * | |
5bd67f35 | 58 | arcExtMap_coreRegName(int value) |
0d2bcfaf NC |
59 | { |
60 | if (value < 32) | |
61 | return 0; | |
53c9ebc5 | 62 | return arc_extension_map.coreRegisters[value-32]; |
0d2bcfaf NC |
63 | } |
64 | ||
65 | /* Get the name of an extension condition code. */ | |
66 | ||
67 | const char * | |
5bd67f35 | 68 | arcExtMap_condCodeName(int value) |
0d2bcfaf NC |
69 | { |
70 | if (value < 16) | |
71 | return 0; | |
53c9ebc5 | 72 | return arc_extension_map.condCodes[value-16]; |
0d2bcfaf NC |
73 | } |
74 | ||
75 | /* Get the name of an extension aux register. */ | |
76 | ||
77 | const char * | |
78 | arcExtMap_auxRegName(long address) | |
79 | { | |
80 | /* walk the list of aux reg names and find the name */ | |
81 | struct ExtAuxRegister *r; | |
82 | ||
83 | for (r = arc_extension_map.auxRegisters; r; r = r->next) { | |
84 | if (r->address == address) | |
85 | return (const char *) r->name; | |
86 | } | |
87 | return 0; | |
88 | } | |
89 | ||
90 | /* Recursively free auxilliary register strcture pointers until | |
91 | the list is empty. */ | |
92 | ||
5bd67f35 | 93 | static void |
0d2bcfaf NC |
94 | clean_aux_registers(struct ExtAuxRegister *r) |
95 | { | |
96 | if (r -> next) | |
97 | { | |
98 | clean_aux_registers( r->next); | |
99 | free(r -> name); | |
100 | free(r -> next); | |
101 | r ->next = NULL; | |
102 | } | |
5bd67f35 | 103 | else |
0d2bcfaf NC |
104 | free(r -> name); |
105 | } | |
5bd67f35 | 106 | |
0d2bcfaf NC |
107 | /* Free memory that has been allocated for the extensions. */ |
108 | ||
5bd67f35 AJ |
109 | static void |
110 | cleanup_ext_map(void) | |
0d2bcfaf NC |
111 | { |
112 | struct ExtAuxRegister *r; | |
113 | struct ExtInstruction *insn; | |
114 | int i; | |
115 | ||
116 | /* clean aux reg structure */ | |
117 | r = arc_extension_map.auxRegisters; | |
5bd67f35 | 118 | if (r) |
0d2bcfaf NC |
119 | { |
120 | (clean_aux_registers(r)); | |
121 | free(r); | |
122 | } | |
5bd67f35 | 123 | |
0d2bcfaf | 124 | /* clean instructions */ |
5bd67f35 | 125 | for (i = 0; i < NUM_EXT_INST; i++) |
0d2bcfaf NC |
126 | { |
127 | insn = arc_extension_map.instructions[i]; | |
128 | if (insn) | |
129 | free(insn->name); | |
130 | } | |
5bd67f35 | 131 | |
0d2bcfaf | 132 | /* clean core reg struct */ |
5bd67f35 | 133 | for (i = 0; i < NUM_EXT_CORE; i++) |
0d2bcfaf NC |
134 | { |
135 | if (arc_extension_map.coreRegisters[i]) | |
136 | free(arc_extension_map.coreRegisters[i]); | |
137 | } | |
5bd67f35 | 138 | |
0d2bcfaf NC |
139 | for (i = 0; i < NUM_EXT_COND; i++) { |
140 | if (arc_extension_map.condCodes[i]) | |
141 | free(arc_extension_map.condCodes[i]); | |
142 | } | |
5bd67f35 AJ |
143 | |
144 | memset(&arc_extension_map, 0, sizeof(struct arcExtMap)); | |
0d2bcfaf NC |
145 | } |
146 | ||
5bd67f35 AJ |
147 | int |
148 | arcExtMap_add(void *base, unsigned long length) | |
0d2bcfaf NC |
149 | { |
150 | unsigned char *block = base; | |
151 | unsigned char *p = block; | |
5bd67f35 | 152 | |
0d2bcfaf NC |
153 | /* Clean up and reset everything if needed. */ |
154 | cleanup_ext_map(); | |
155 | ||
5bd67f35 | 156 | while (p && p < (block + length)) |
0d2bcfaf NC |
157 | { |
158 | /* p[0] == length of record | |
159 | p[1] == type of record | |
160 | For instructions: | |
161 | p[2] = opcode | |
162 | p[3] = minor opcode (if opcode == 3) | |
163 | p[4] = flags | |
164 | p[5]+ = name | |
165 | For core regs and condition codes: | |
166 | p[2] = value | |
167 | p[3]+ = name | |
168 | For aux regs: | |
169 | p[2..5] = value | |
170 | p[6]+ = name | |
171 | (value is p[2]<<24|p[3]<<16|p[4]<<8|p[5]) */ | |
172 | ||
173 | if (p[0] == 0) | |
174 | return -1; | |
5bd67f35 | 175 | |
0d2bcfaf NC |
176 | switch (p[1]) |
177 | { | |
178 | case EXT_INSTRUCTION: | |
179 | { | |
180 | char opcode = p[2]; | |
181 | char minor = p[3]; | |
182 | char * insn_name = (char *) xmalloc(( (int)*p-5) * sizeof(char)); | |
5bd67f35 | 183 | struct ExtInstruction * insn = |
0d2bcfaf | 184 | (struct ExtInstruction *) xmalloc(sizeof(struct ExtInstruction)); |
5bd67f35 | 185 | |
0d2bcfaf NC |
186 | if (opcode==3) |
187 | opcode = 0x1f - 0x10 + minor - 0x09 + 1; | |
188 | else | |
189 | opcode -= 0x10; | |
190 | insn -> flags = (char) *(p+4); | |
53c9ebc5 | 191 | strcpy (insn_name, (char *) (p+5)); |
0d2bcfaf NC |
192 | insn -> name = insn_name; |
193 | arc_extension_map.instructions[(int) opcode] = insn; | |
194 | } | |
195 | break; | |
5bd67f35 AJ |
196 | |
197 | case EXT_CORE_REGISTER: | |
0d2bcfaf NC |
198 | { |
199 | char * core_name = (char *) xmalloc(((int)*p-3) * sizeof(char)); | |
200 | ||
53c9ebc5 | 201 | strcpy(core_name, (char *) (p+3)); |
0d2bcfaf NC |
202 | arc_extension_map.coreRegisters[p[2]-32] = core_name; |
203 | } | |
204 | break; | |
5bd67f35 AJ |
205 | |
206 | case EXT_COND_CODE: | |
0d2bcfaf NC |
207 | { |
208 | char * cc_name = (char *) xmalloc( ((int)*p-3) * sizeof(char)); | |
53c9ebc5 | 209 | strcpy(cc_name, (char *) (p+3)); |
0d2bcfaf | 210 | arc_extension_map.condCodes[p[2]-16] = cc_name; |
5bd67f35 | 211 | } |
0d2bcfaf | 212 | break; |
5bd67f35 AJ |
213 | |
214 | case EXT_AUX_REGISTER: | |
0d2bcfaf NC |
215 | { |
216 | /* trickier -- need to store linked list to these */ | |
5bd67f35 | 217 | struct ExtAuxRegister *newAuxRegister = |
0d2bcfaf NC |
218 | (struct ExtAuxRegister *)malloc(sizeof(struct ExtAuxRegister)); |
219 | char * aux_name = (char *) xmalloc ( ((int)*p-6) * sizeof(char)); | |
220 | ||
53c9ebc5 | 221 | strcpy (aux_name, (char *) (p+6)); |
0d2bcfaf NC |
222 | newAuxRegister->name = aux_name; |
223 | newAuxRegister->address = p[2]<<24 | p[3]<<16 | p[4]<<8 | p[5]; | |
224 | newAuxRegister->next = arc_extension_map.auxRegisters; | |
225 | arc_extension_map.auxRegisters = newAuxRegister; | |
226 | } | |
227 | break; | |
5bd67f35 | 228 | |
0d2bcfaf NC |
229 | default: |
230 | return -1; | |
5bd67f35 | 231 | |
0d2bcfaf NC |
232 | } |
233 | p += p[0]; /* move to next record */ | |
234 | } | |
5bd67f35 | 235 | |
0d2bcfaf NC |
236 | return 0; |
237 | } | |
238 | ||
239 | /* Load hw extension descibed in .extArcMap ELF section. */ | |
240 | ||
241 | void | |
242 | build_ARC_extmap (text_bfd) | |
243 | bfd *text_bfd; | |
244 | { | |
245 | char *arcExtMap; | |
246 | bfd_size_type count; | |
247 | asection *p; | |
248 | ||
249 | for (p = text_bfd->sections; p != NULL; p = p->next) | |
250 | if (!strcmp (p->name, ".arcextmap")) | |
251 | { | |
52886d70 | 252 | count = bfd_get_section_size (p); |
0d2bcfaf NC |
253 | arcExtMap = (char *) xmalloc (count); |
254 | if (bfd_get_section_contents (text_bfd, p, (PTR) arcExtMap, 0, count)) | |
255 | { | |
256 | arcExtMap_add ((PTR) arcExtMap, count); | |
257 | break; | |
258 | } | |
259 | free ((PTR) arcExtMap); | |
260 | } | |
261 | } |