sim: aarch64: switch to common disassembler tracing
[deliverable/binutils-gdb.git] / sim / aarch64 / interp.c
1 /* interp.c -- AArch64 sim interface to GDB.
2
3 Copyright (C) 2015-2016 Free Software Foundation, Inc.
4
5 Contributed by Red Hat.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include <stdio.h>
24 #include <assert.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <stdlib.h>
29
30 #include "ansidecl.h"
31 #include "gdb/callback.h"
32 #include "gdb/remote-sim.h"
33 #include "gdb/signals.h"
34 #include "gdb/sim-aarch64.h"
35
36 #include "sim-main.h"
37 #include "sim-options.h"
38 #include "memory.h"
39 #include "simulator.h"
40
41 static unsigned long symcount = 0;
42 static asymbol ** symtab = NULL;
43
44 /* Filter out (in place) symbols that are useless for disassembly.
45 COUNT is the number of elements in SYMBOLS.
46 Return the number of useful symbols. */
47
48 static unsigned long
49 remove_useless_symbols (asymbol **symbols, unsigned long count)
50 {
51 asymbol **in_ptr = symbols;
52 asymbol **out_ptr = symbols;
53
54 while (count-- > 0)
55 {
56 asymbol *sym = *in_ptr++;
57
58 if (strstr (sym->name, "gcc2_compiled"))
59 continue;
60 if (sym->name == NULL || sym->name[0] == '\0')
61 continue;
62 if (sym->flags & (BSF_DEBUGGING))
63 continue;
64 if ( bfd_is_und_section (sym->section)
65 || bfd_is_com_section (sym->section))
66 continue;
67 if (sym->name[0] == '$')
68 continue;
69
70 *out_ptr++ = sym;
71 }
72 return out_ptr - symbols;
73 }
74
75 static signed int
76 compare_symbols (const void *ap, const void *bp)
77 {
78 const asymbol *a = * (const asymbol **) ap;
79 const asymbol *b = * (const asymbol **) bp;
80
81 if (bfd_asymbol_value (a) > bfd_asymbol_value (b))
82 return 1;
83 if (bfd_asymbol_value (a) < bfd_asymbol_value (b))
84 return -1;
85 return 0;
86 }
87
88 /* Find the name of the function at ADDR. */
89 const char *
90 aarch64_get_func (uint64_t addr)
91 {
92 int min, max;
93
94 min = -1;
95 max = symcount;
96 while (min < max - 1)
97 {
98 int sym;
99 bfd_vma sa;
100
101 sym = (min + max) / 2;
102 sa = bfd_asymbol_value (symtab[sym]);
103
104 if (sa > addr)
105 max = sym;
106 else if (sa < addr)
107 min = sym;
108 else
109 {
110 min = sym;
111 break;
112 }
113 }
114
115 if (min != -1)
116 return bfd_asymbol_name (symtab [min]);
117
118 return "";
119 }
120
121 uint64_t
122 aarch64_get_sym_value (const char *name)
123 {
124 unsigned long i;
125
126 for (i = 0; i < symcount; i++)
127 if (strcmp (bfd_asymbol_name (symtab[i]), name) == 0)
128 return bfd_asymbol_value (symtab[i]);
129
130 return 0;
131 }
132
133 SIM_RC
134 sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
135 {
136 sim_cpu *cpu = STATE_CPU (sd, 0);
137 long storage;
138 bfd_vma addr = 0;
139
140 if (abfd != NULL)
141 addr = bfd_get_start_address (abfd);
142
143 aarch64_set_next_PC (cpu, addr);
144 aarch64_update_PC (cpu);
145
146 /* Standalone mode (i.e. `run`) will take care of the argv for us in
147 sim_open() -> sim_parse_args(). But in debug mode (i.e. 'target sim'
148 with `gdb`), we need to handle it because the user can change the
149 argv on the fly via gdb's 'run'. */
150 if (STATE_PROG_ARGV (sd) != argv)
151 {
152 freeargv (STATE_PROG_ARGV (sd));
153 STATE_PROG_ARGV (sd) = dupargv (argv);
154 }
155
156 storage = bfd_get_symtab_upper_bound (abfd);
157 if (storage > 0)
158 {
159 symtab = (asymbol **) xmalloc (storage);
160 symcount = bfd_canonicalize_symtab (abfd, symtab);
161 symcount = remove_useless_symbols (symtab, symcount);
162 qsort (symtab, symcount, sizeof (asymbol *), compare_symbols);
163 }
164
165 aarch64_init (cpu, bfd_get_start_address (abfd));
166
167 return SIM_RC_OK;
168 }
169
170 /* Read the LENGTH bytes at BUF as a little-endian value. */
171
172 static bfd_vma
173 get_le (unsigned char *buf, unsigned int length)
174 {
175 bfd_vma acc = 0;
176
177 while (length -- > 0)
178 acc = (acc << 8) + buf[length];
179
180 return acc;
181 }
182
183 /* Store VAL as a little-endian value in the LENGTH bytes at BUF. */
184
185 static void
186 put_le (unsigned char *buf, unsigned int length, bfd_vma val)
187 {
188 int i;
189
190 for (i = 0; i < length; i++)
191 {
192 buf[i] = val & 0xff;
193 val >>= 8;
194 }
195 }
196
197 static int
198 check_regno (int regno)
199 {
200 return 0 <= regno && regno < AARCH64_MAX_REGNO;
201 }
202
203 static size_t
204 reg_size (int regno)
205 {
206 if (regno == AARCH64_CPSR_REGNO || regno == AARCH64_FPSR_REGNO)
207 return 32;
208 return 64;
209 }
210
211 static int
212 aarch64_reg_get (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
213 {
214 size_t size;
215 bfd_vma val;
216
217 if (!check_regno (regno))
218 return 0;
219
220 size = reg_size (regno);
221
222 if (length != size)
223 return 0;
224
225 switch (regno)
226 {
227 case AARCH64_MIN_GR ... AARCH64_MAX_GR:
228 val = aarch64_get_reg_u64 (cpu, regno, 0);
229 break;
230
231 case AARCH64_MIN_FR ... AARCH64_MAX_FR:
232 val = aarch64_get_FP_double (cpu, regno - 32);
233 break;
234
235 case AARCH64_PC_REGNO:
236 val = aarch64_get_PC (cpu);
237 break;
238
239 case AARCH64_CPSR_REGNO:
240 val = aarch64_get_CPSR (cpu);
241 break;
242
243 case AARCH64_FPSR_REGNO:
244 val = aarch64_get_FPSR (cpu);
245 break;
246
247 default:
248 sim_io_eprintf (CPU_STATE (cpu),
249 "sim: unrecognized register number: %d\n", regno);
250 return -1;
251 }
252
253 put_le (buf, length, val);
254
255 return size;
256 }
257
258 static int
259 aarch64_reg_set (SIM_CPU *cpu, int regno, unsigned char *buf, int length)
260 {
261 size_t size;
262 bfd_vma val;
263
264 if (!check_regno (regno))
265 return -1;
266
267 size = reg_size (regno);
268
269 if (length != size)
270 return -1;
271
272 val = get_le (buf, length);
273
274 switch (regno)
275 {
276 case AARCH64_MIN_GR ... AARCH64_MAX_GR:
277 aarch64_set_reg_u64 (cpu, regno, 1, val);
278 break;
279
280 case AARCH64_MIN_FR ... AARCH64_MAX_FR:
281 aarch64_set_FP_double (cpu, regno - 32, (double) val);
282 break;
283
284 case AARCH64_PC_REGNO:
285 aarch64_set_next_PC (cpu, val);
286 aarch64_update_PC (cpu);
287 break;
288
289 case AARCH64_CPSR_REGNO:
290 aarch64_set_CPSR (cpu, val);
291 break;
292
293 case AARCH64_FPSR_REGNO:
294 aarch64_set_FPSR (cpu, val);
295 break;
296
297 default:
298 sim_io_eprintf (CPU_STATE (cpu),
299 "sim: unrecognized register number: %d\n", regno);
300 return 0;
301 }
302
303 return size;
304 }
305
306 static sim_cia
307 aarch64_pc_get (sim_cpu *cpu)
308 {
309 return aarch64_get_PC (cpu);
310 }
311
312 static void
313 aarch64_pc_set (sim_cpu *cpu, sim_cia pc)
314 {
315 aarch64_set_next_PC (cpu, pc);
316 aarch64_update_PC (cpu);
317 }
318
319 static void
320 free_state (SIM_DESC sd)
321 {
322 if (STATE_MODULES (sd) != NULL)
323 sim_module_uninstall (sd);
324 sim_cpu_free_all (sd);
325 sim_state_free (sd);
326 }
327
328 SIM_DESC
329 sim_open (SIM_OPEN_KIND kind,
330 struct host_callback_struct * callback,
331 struct bfd * abfd,
332 char ** argv)
333 {
334 int i;
335 sim_cpu *cpu;
336 SIM_DESC sd = sim_state_alloc (kind, callback);
337
338 if (sd == NULL)
339 return sd;
340
341 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
342
343 /* Perform the initialization steps one by one. */
344 if (sim_cpu_alloc_all (sd, 1, 0) != SIM_RC_OK
345 || sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK
346 || sim_parse_args (sd, argv) != SIM_RC_OK
347 || sim_analyze_program (sd,
348 (STATE_PROG_ARGV (sd) != NULL
349 ? *STATE_PROG_ARGV (sd)
350 : NULL), abfd) != SIM_RC_OK
351 || sim_config (sd) != SIM_RC_OK
352 || sim_post_argv_init (sd) != SIM_RC_OK)
353 {
354 free_state (sd);
355 return NULL;
356 }
357
358 aarch64_init_LIT_table ();
359
360 assert (MAX_NR_PROCESSORS == 1);
361 cpu = STATE_CPU (sd, 0);
362 CPU_PC_FETCH (cpu) = aarch64_pc_get;
363 CPU_PC_STORE (cpu) = aarch64_pc_set;
364 CPU_REG_FETCH (cpu) = aarch64_reg_get;
365 CPU_REG_STORE (cpu) = aarch64_reg_set;
366
367 /* Set SP, FP and PC to 0 and set LR to -1
368 so we can detect a top-level return. */
369 aarch64_set_reg_u64 (cpu, SP, 1, 0);
370 aarch64_set_reg_u64 (cpu, FP, 1, 0);
371 aarch64_set_reg_u64 (cpu, LR, 1, TOP_LEVEL_RETURN_PC);
372 aarch64_set_next_PC (cpu, 0);
373 aarch64_update_PC (cpu);
374
375 /* Default to a 128 Mbyte (== 2^27) memory space. */
376 sim_do_commandf (sd, "memory-size 0x8000000");
377
378 return sd;
379 }
380
381 void
382 sim_engine_run (SIM_DESC sd,
383 int next_cpu_nr ATTRIBUTE_UNUSED,
384 int nr_cpus ATTRIBUTE_UNUSED,
385 int siggnal ATTRIBUTE_UNUSED)
386 {
387 aarch64_run (sd);
388 }
This page took 0.043277 seconds and 4 git commands to generate.