00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "sysdep.h"
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include "bfd.h"
00029 #include "arc-ext.h"
00030 #include "libiberty.h"
00031
00032
00033 static struct arcExtMap arc_extension_map;
00034
00035
00036
00037 const char *
00038 arcExtMap_instName(int opcode, int minor, int *flags)
00039 {
00040 if (opcode == 3)
00041 {
00042
00043 if (minor < 0x09 || minor == 0x3f)
00044 return 0;
00045 else
00046 opcode = 0x1f - 0x10 + minor - 0x09 + 1;
00047 }
00048 else
00049 if (opcode < 0x10)
00050 return 0;
00051 else
00052 opcode -= 0x10;
00053 if (!arc_extension_map.instructions[opcode])
00054 return 0;
00055 *flags = arc_extension_map.instructions[opcode]->flags;
00056 return arc_extension_map.instructions[opcode]->name;
00057 }
00058
00059
00060
00061 const char *
00062 arcExtMap_coreRegName(int value)
00063 {
00064 if (value < 32)
00065 return 0;
00066 return arc_extension_map.coreRegisters[value-32];
00067 }
00068
00069
00070
00071 const char *
00072 arcExtMap_condCodeName(int value)
00073 {
00074 if (value < 16)
00075 return 0;
00076 return arc_extension_map.condCodes[value-16];
00077 }
00078
00079
00080
00081 const char *
00082 arcExtMap_auxRegName(long address)
00083 {
00084
00085 struct ExtAuxRegister *r;
00086
00087 for (r = arc_extension_map.auxRegisters; r; r = r->next) {
00088 if (r->address == address)
00089 return (const char *) r->name;
00090 }
00091 return 0;
00092 }
00093
00094
00095
00096
00097 static void
00098 clean_aux_registers(struct ExtAuxRegister *r)
00099 {
00100 if (r -> next)
00101 {
00102 clean_aux_registers( r->next);
00103 free(r -> name);
00104 free(r -> next);
00105 r ->next = NULL;
00106 }
00107 else
00108 free(r -> name);
00109 }
00110
00111
00112
00113 static void
00114 cleanup_ext_map(void)
00115 {
00116 struct ExtAuxRegister *r;
00117 struct ExtInstruction *insn;
00118 int i;
00119
00120
00121 r = arc_extension_map.auxRegisters;
00122 if (r)
00123 {
00124 (clean_aux_registers(r));
00125 free(r);
00126 }
00127
00128
00129 for (i = 0; i < NUM_EXT_INST; i++)
00130 {
00131 insn = arc_extension_map.instructions[i];
00132 if (insn)
00133 free(insn->name);
00134 }
00135
00136
00137 for (i = 0; i < NUM_EXT_CORE; i++)
00138 {
00139 if (arc_extension_map.coreRegisters[i])
00140 free(arc_extension_map.coreRegisters[i]);
00141 }
00142
00143 for (i = 0; i < NUM_EXT_COND; i++) {
00144 if (arc_extension_map.condCodes[i])
00145 free(arc_extension_map.condCodes[i]);
00146 }
00147
00148 memset(&arc_extension_map, 0, sizeof(struct arcExtMap));
00149 }
00150
00151 int
00152 arcExtMap_add(void *base, unsigned long length)
00153 {
00154 unsigned char *block = base;
00155 unsigned char *p = block;
00156
00157
00158 cleanup_ext_map();
00159
00160 while (p && p < (block + length))
00161 {
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 if (p[0] == 0)
00178 return -1;
00179
00180 switch (p[1])
00181 {
00182 case EXT_INSTRUCTION:
00183 {
00184 char opcode = p[2];
00185 char minor = p[3];
00186 char * insn_name = (char *) xmalloc(( (int)*p-5) * sizeof(char));
00187 struct ExtInstruction * insn =
00188 (struct ExtInstruction *) xmalloc(sizeof(struct ExtInstruction));
00189
00190 if (opcode==3)
00191 opcode = 0x1f - 0x10 + minor - 0x09 + 1;
00192 else
00193 opcode -= 0x10;
00194 insn -> flags = (char) *(p+4);
00195 strcpy (insn_name, (char *) (p+5));
00196 insn -> name = insn_name;
00197 arc_extension_map.instructions[(int) opcode] = insn;
00198 }
00199 break;
00200
00201 case EXT_CORE_REGISTER:
00202 {
00203 char * core_name = (char *) xmalloc(((int)*p-3) * sizeof(char));
00204
00205 strcpy(core_name, (char *) (p+3));
00206 arc_extension_map.coreRegisters[p[2]-32] = core_name;
00207 }
00208 break;
00209
00210 case EXT_COND_CODE:
00211 {
00212 char * cc_name = (char *) xmalloc( ((int)*p-3) * sizeof(char));
00213 strcpy(cc_name, (char *) (p+3));
00214 arc_extension_map.condCodes[p[2]-16] = cc_name;
00215 }
00216 break;
00217
00218 case EXT_AUX_REGISTER:
00219 {
00220
00221 struct ExtAuxRegister *newAuxRegister =
00222 (struct ExtAuxRegister *)malloc(sizeof(struct ExtAuxRegister));
00223 char * aux_name = (char *) xmalloc ( ((int)*p-6) * sizeof(char));
00224
00225 strcpy (aux_name, (char *) (p+6));
00226 newAuxRegister->name = aux_name;
00227 newAuxRegister->address = p[2]<<24 | p[3]<<16 | p[4]<<8 | p[5];
00228 newAuxRegister->next = arc_extension_map.auxRegisters;
00229 arc_extension_map.auxRegisters = newAuxRegister;
00230 }
00231 break;
00232
00233 default:
00234 return -1;
00235
00236 }
00237 p += p[0];
00238 }
00239
00240 return 0;
00241 }
00242
00243
00244
00245 void
00246 build_ARC_extmap (text_bfd)
00247 bfd *text_bfd;
00248 {
00249 char *arcExtMap;
00250 bfd_size_type count;
00251 asection *p;
00252
00253 for (p = text_bfd->sections; p != NULL; p = p->next)
00254 if (!strcmp (p->name, ".arcextmap"))
00255 {
00256 count = bfd_get_section_size (p);
00257 arcExtMap = (char *) xmalloc (count);
00258 if (bfd_get_section_contents (text_bfd, p, (PTR) arcExtMap, 0, count))
00259 {
00260 arcExtMap_add ((PTR) arcExtMap, count);
00261 break;
00262 }
00263 free ((PTR) arcExtMap);
00264 }
00265 }