* mn10300-linux-tdep.c (mn10300_linux_svr4_fetch_link_map_offsets):
[platform/upstream/binutils.git] / gdb / mn10300-linux-tdep.c
1 /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
2    Copyright 2003, 2004, 2005
3    Free Software Foundation, Inc.
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
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "gdb_string.h"
25 #include "regcache.h"
26 #include "mn10300-tdep.h"
27 #include "gdb_assert.h"
28 #include "bfd.h"
29 #include "elf-bfd.h"
30 #include "osabi.h"
31 #include "regset.h"
32 #include "solib-svr4.h"
33
34 #include <stdlib.h>
35
36 /* Transliterated from <asm-mn10300/elf.h>...  */
37 #define MN10300_ELF_NGREG 28
38 #define MN10300_ELF_NFPREG 32
39
40 typedef gdb_byte   mn10300_elf_greg_t[4];
41 typedef mn10300_elf_greg_t mn10300_elf_gregset_t[MN10300_ELF_NGREG];
42
43 typedef gdb_byte   mn10300_elf_fpreg_t[4];
44 typedef struct
45 {
46   mn10300_elf_fpreg_t fpregs[MN10300_ELF_NFPREG];
47   gdb_byte    fpcr[4];
48 } mn10300_elf_fpregset_t;
49
50 /* elf_gregset_t register indices stolen from include/asm-mn10300/ptrace.h.  */
51 #define MN10300_ELF_GREGSET_T_REG_INDEX_A3      0
52 #define MN10300_ELF_GREGSET_T_REG_INDEX_A2      1
53 #define MN10300_ELF_GREGSET_T_REG_INDEX_D3      2
54 #define MN10300_ELF_GREGSET_T_REG_INDEX_D2      3
55 #define MN10300_ELF_GREGSET_T_REG_INDEX_MCVF    4
56 #define MN10300_ELF_GREGSET_T_REG_INDEX_MCRL    5
57 #define MN10300_ELF_GREGSET_T_REG_INDEX_MCRH    6
58 #define MN10300_ELF_GREGSET_T_REG_INDEX_MDRQ    7
59 #define MN10300_ELF_GREGSET_T_REG_INDEX_E1      8
60 #define MN10300_ELF_GREGSET_T_REG_INDEX_E0      9
61 #define MN10300_ELF_GREGSET_T_REG_INDEX_E7      10
62 #define MN10300_ELF_GREGSET_T_REG_INDEX_E6      11
63 #define MN10300_ELF_GREGSET_T_REG_INDEX_E5      12
64 #define MN10300_ELF_GREGSET_T_REG_INDEX_E4      13
65 #define MN10300_ELF_GREGSET_T_REG_INDEX_E3      14
66 #define MN10300_ELF_GREGSET_T_REG_INDEX_E2      15
67 #define MN10300_ELF_GREGSET_T_REG_INDEX_SP      16
68 #define MN10300_ELF_GREGSET_T_REG_INDEX_LAR     17
69 #define MN10300_ELF_GREGSET_T_REG_INDEX_LIR     18
70 #define MN10300_ELF_GREGSET_T_REG_INDEX_MDR     19
71 #define MN10300_ELF_GREGSET_T_REG_INDEX_A1      20
72 #define MN10300_ELF_GREGSET_T_REG_INDEX_A0      21
73 #define MN10300_ELF_GREGSET_T_REG_INDEX_D1      22
74 #define MN10300_ELF_GREGSET_T_REG_INDEX_D0      23
75 #define MN10300_ELF_GREGSET_T_REG_INDEX_ORIG_D0 24
76 #define MN10300_ELF_GREGSET_T_REG_INDEX_EPSW    25
77 #define MN10300_ELF_GREGSET_T_REG_INDEX_PC      26
78
79 /* New gdbarch API for corefile registers.
80    Given a section name and size, create a struct reg object
81    with a supply_register and a collect_register method.  */
82
83 /* Copy register value of REGNUM from regset to regcache.  
84    If REGNUM is -1, do this for all gp registers in regset.  */
85
86 static void
87 am33_supply_gregset_method (const struct regset *regset, 
88                             struct regcache *regcache, 
89                             int regnum, const void *gregs, size_t len)
90 {
91   char zerobuf[MAX_REGISTER_SIZE];
92   const mn10300_elf_greg_t *regp = (const mn10300_elf_greg_t *) gregs;
93   int i;
94
95   gdb_assert (len == sizeof (mn10300_elf_gregset_t));
96
97   switch (regnum) {
98   case E_D0_REGNUM:
99     regcache_raw_supply (regcache, E_D0_REGNUM, 
100                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_D0));
101     break;
102   case E_D1_REGNUM:
103     regcache_raw_supply (regcache, E_D1_REGNUM, 
104                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_D1));
105     break;
106   case E_D2_REGNUM:
107     regcache_raw_supply (regcache, E_D2_REGNUM, 
108                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_D2));
109     break;
110   case E_D3_REGNUM:
111     regcache_raw_supply (regcache, E_D3_REGNUM, 
112                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_D3));
113     break;
114   case E_A0_REGNUM:
115     regcache_raw_supply (regcache, E_A0_REGNUM, 
116                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_A0));
117     break;
118   case E_A1_REGNUM:
119     regcache_raw_supply (regcache, E_A1_REGNUM, 
120                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_A1));
121     break;
122   case E_A2_REGNUM:
123     regcache_raw_supply (regcache, E_A2_REGNUM, 
124                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_A2));
125     break;
126   case E_A3_REGNUM:
127     regcache_raw_supply (regcache, E_A3_REGNUM, 
128                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_A3));
129     break;
130   case E_SP_REGNUM:
131     regcache_raw_supply (regcache, E_SP_REGNUM, 
132                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_SP));
133     break;
134   case E_PC_REGNUM:
135     regcache_raw_supply (regcache, E_PC_REGNUM, 
136                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_PC));
137     break;
138   case E_MDR_REGNUM:
139     regcache_raw_supply (regcache, E_MDR_REGNUM, 
140                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MDR));
141     break;
142   case E_PSW_REGNUM:
143     regcache_raw_supply (regcache, E_PSW_REGNUM, 
144                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_EPSW));
145     break;
146   case E_LIR_REGNUM:
147     regcache_raw_supply (regcache, E_LIR_REGNUM, 
148                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_LIR));
149     break;
150   case E_LAR_REGNUM:
151     regcache_raw_supply (regcache, E_LAR_REGNUM, 
152                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_LAR));
153     break;
154   case E_MDRQ_REGNUM:
155     regcache_raw_supply (regcache, E_MDRQ_REGNUM, 
156                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MDRQ));
157     break;
158   case E_E0_REGNUM:
159     regcache_raw_supply (regcache, E_E0_REGNUM,   
160                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E0));
161     break;
162   case E_E1_REGNUM:
163     regcache_raw_supply (regcache, E_E1_REGNUM,
164                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E1));
165     break;
166   case E_E2_REGNUM:
167     regcache_raw_supply (regcache, E_E2_REGNUM, 
168                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E2));
169     break;
170   case E_E3_REGNUM:
171     regcache_raw_supply (regcache, E_E3_REGNUM, 
172                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E3));
173     break;
174   case E_E4_REGNUM:
175     regcache_raw_supply (regcache, E_E4_REGNUM, 
176                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E4));
177     break;
178   case E_E5_REGNUM:
179     regcache_raw_supply (regcache, E_E5_REGNUM, 
180                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E5));
181     break;
182   case E_E6_REGNUM:
183     regcache_raw_supply (regcache, E_E6_REGNUM, 
184                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E6));
185     break;
186   case E_E7_REGNUM:
187     regcache_raw_supply (regcache, E_E7_REGNUM, 
188                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E7));
189     break;
190
191     /* ssp, msp, and usp are inaccessible.  */
192   case E_E8_REGNUM:
193     memset (zerobuf, 0, MAX_REGISTER_SIZE);
194     regcache_raw_supply (regcache, E_E8_REGNUM, zerobuf);
195     break;
196   case E_E9_REGNUM:
197     memset (zerobuf, 0, MAX_REGISTER_SIZE);
198     regcache_raw_supply (regcache, E_E9_REGNUM, zerobuf);
199     break;
200   case E_E10_REGNUM:
201     memset (zerobuf, 0, MAX_REGISTER_SIZE);
202     regcache_raw_supply (regcache, E_E10_REGNUM, zerobuf);
203
204     break;
205   case E_MCRH_REGNUM:
206     regcache_raw_supply (regcache, E_MCRH_REGNUM, 
207                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MCRH));
208     break;
209   case E_MCRL_REGNUM:
210     regcache_raw_supply (regcache, E_MCRL_REGNUM, 
211                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MCRL));
212     break;
213   case E_MCVF_REGNUM:
214     regcache_raw_supply (regcache, E_MCVF_REGNUM, 
215                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MCVF));
216     break;
217   case E_FPCR_REGNUM:
218     /* FPCR is numbered among the GP regs, but handled as an FP reg.
219        Do nothing.  */
220     break;
221   case E_FPCR_REGNUM + 1:
222     /* The two unused registers beyond fpcr are inaccessible.  */
223     memset (zerobuf, 0, MAX_REGISTER_SIZE);
224     regcache_raw_supply (regcache, E_FPCR_REGNUM + 1, zerobuf);
225     break;
226   case E_FPCR_REGNUM + 2:
227     memset (zerobuf, 0, MAX_REGISTER_SIZE);
228     regcache_raw_supply (regcache, E_FPCR_REGNUM + 2, zerobuf);
229     break;
230   default:      /* An error, obviously, but should we error out?  */
231     break;
232   case -1:
233     for (i = 0; i < MN10300_ELF_NGREG; i++)
234       am33_supply_gregset_method (regset, regcache, i, gregs, len);
235     break;
236   }
237   return;
238 }
239
240 /* Copy fp register value of REGNUM from regset to regcache.  
241    If REGNUM is -1, do this for all fp registers in regset. */
242
243 static void
244 am33_supply_fpregset_method (const struct regset *regset, 
245                              struct regcache *regcache, 
246                              int regnum, const void *fpregs, size_t len)
247 {
248   const mn10300_elf_fpregset_t *fpregset = fpregs;
249
250   gdb_assert (len == sizeof (mn10300_elf_fpregset_t));
251
252   if (regnum == -1)
253     {
254       int i;
255
256       for (i = 0; i < MN10300_ELF_NFPREG; i++)
257         am33_supply_fpregset_method (regset, regcache,
258                                      E_FS0_REGNUM + i, fpregs, len);
259       am33_supply_fpregset_method (regset, regcache, 
260                                    E_FPCR_REGNUM, fpregs, len);
261     }
262   else if (regnum == E_FPCR_REGNUM)
263     regcache_raw_supply (current_regcache, E_FPCR_REGNUM, 
264                          &fpregset->fpcr);
265   else if (E_FS0_REGNUM <= regnum && regnum < E_FS0_REGNUM + MN10300_ELF_NFPREG)
266     regcache_raw_supply (current_regcache, regnum, 
267                          &fpregset->fpregs[regnum - E_FS0_REGNUM]);
268
269   return;
270 }
271
272 /* Copy register values from regcache to regset.  */
273
274 static void
275 am33_collect_gregset_method (const struct regset *regset, 
276                              const struct regcache *regcache, 
277                              int regnum, void *gregs, size_t len)
278 {
279   mn10300_elf_gregset_t *regp = gregs;
280   int i;
281
282   gdb_assert (len == sizeof (mn10300_elf_gregset_t));
283
284   switch (regnum) {
285   case E_D0_REGNUM:
286     regcache_raw_collect (regcache, E_D0_REGNUM, 
287                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_D0));
288     break;
289   case E_D1_REGNUM:
290     regcache_raw_collect (regcache, E_D1_REGNUM, 
291                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_D1));
292     break;
293   case E_D2_REGNUM:
294     regcache_raw_collect (regcache, E_D2_REGNUM, 
295                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_D2));
296     break;
297   case E_D3_REGNUM:
298     regcache_raw_collect (regcache, E_D3_REGNUM, 
299                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_D3));
300     break;
301   case E_A0_REGNUM:
302     regcache_raw_collect (regcache, E_A0_REGNUM, 
303                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_A0));
304     break;
305   case E_A1_REGNUM:
306     regcache_raw_collect (regcache, E_A1_REGNUM, 
307                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_A1));
308     break;
309   case E_A2_REGNUM:
310     regcache_raw_collect (regcache, E_A2_REGNUM, 
311                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_A2));
312     break;
313   case E_A3_REGNUM:
314     regcache_raw_collect (regcache, E_A3_REGNUM, 
315                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_A3));
316     break;
317   case E_SP_REGNUM:
318     regcache_raw_collect (regcache, E_SP_REGNUM, 
319                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_SP));
320     break;
321   case E_PC_REGNUM:
322     regcache_raw_collect (regcache, E_PC_REGNUM, 
323                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_PC));
324     break;
325   case E_MDR_REGNUM:
326     regcache_raw_collect (regcache, E_MDR_REGNUM, 
327                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MDR));
328     break;
329   case E_PSW_REGNUM:
330     regcache_raw_collect (regcache, E_PSW_REGNUM, 
331                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_EPSW));
332     break;
333   case E_LIR_REGNUM:
334     regcache_raw_collect (regcache, E_LIR_REGNUM, 
335                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_LIR));
336     break;
337   case E_LAR_REGNUM:
338     regcache_raw_collect (regcache, E_LAR_REGNUM, 
339                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_LAR));
340     break;
341   case E_MDRQ_REGNUM:
342     regcache_raw_collect (regcache, E_MDRQ_REGNUM, 
343                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MDRQ));
344     break;
345   case E_E0_REGNUM:
346     regcache_raw_collect (regcache, E_E0_REGNUM,   
347                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E0));
348     break;
349   case E_E1_REGNUM:
350     regcache_raw_collect (regcache, E_E1_REGNUM,
351                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E1));
352     break;
353   case E_E2_REGNUM:
354     regcache_raw_collect (regcache, E_E2_REGNUM, 
355                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E2));
356     break;
357   case E_E3_REGNUM:
358     regcache_raw_collect (regcache, E_E3_REGNUM, 
359                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E3));
360     break;
361   case E_E4_REGNUM:
362     regcache_raw_collect (regcache, E_E4_REGNUM, 
363                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E4));
364     break;
365   case E_E5_REGNUM:
366     regcache_raw_collect (regcache, E_E5_REGNUM, 
367                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E5));
368     break;
369   case E_E6_REGNUM:
370     regcache_raw_collect (regcache, E_E6_REGNUM, 
371                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E6));
372     break;
373   case E_E7_REGNUM:
374     regcache_raw_collect (regcache, E_E7_REGNUM, 
375                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_E7));
376     break;
377
378     /* ssp, msp, and usp are inaccessible.  */
379   case E_E8_REGNUM:
380     /* The gregset struct has noplace to put this: do nothing.  */
381     break;
382   case E_E9_REGNUM:
383     /* The gregset struct has noplace to put this: do nothing.  */
384     break;
385   case E_E10_REGNUM:
386     /* The gregset struct has noplace to put this: do nothing.  */
387     break;
388   case E_MCRH_REGNUM:
389     regcache_raw_collect (regcache, E_MCRH_REGNUM, 
390                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MCRH));
391     break;
392   case E_MCRL_REGNUM:
393     regcache_raw_collect (regcache, E_MCRL_REGNUM, 
394                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MCRL));
395     break;
396   case E_MCVF_REGNUM:
397     regcache_raw_collect (regcache, E_MCVF_REGNUM, 
398                          (regp + MN10300_ELF_GREGSET_T_REG_INDEX_MCVF));
399     break;
400   case E_FPCR_REGNUM:
401     /* FPCR is numbered among the GP regs, but handled as an FP reg.
402        Do nothing.  */
403     break;
404   case E_FPCR_REGNUM + 1:
405     /* The gregset struct has noplace to put this: do nothing.  */
406     break;
407   case E_FPCR_REGNUM + 2:
408     /* The gregset struct has noplace to put this: do nothing.  */
409     break;
410   default:      /* An error, obviously, but should we error out?  */
411     break;
412   case -1:
413     for (i = 0; i < MN10300_ELF_NGREG; i++)
414       am33_collect_gregset_method (regset, regcache, i, gregs, len);
415     break;
416   }
417   return;
418 }
419
420 /* Copy fp register values from regcache to regset.  */
421
422 static void
423 am33_collect_fpregset_method (const struct regset *regset, 
424                               const struct regcache *regcache, 
425                               int regnum, void *fpregs, size_t len)
426 {
427   mn10300_elf_fpregset_t *fpregset = fpregs;
428
429   gdb_assert (len == sizeof (mn10300_elf_fpregset_t));
430
431   if (regnum == -1)
432     {
433       int i;
434       for (i = 0; i < MN10300_ELF_NFPREG; i++)
435         am33_collect_fpregset_method (regset, regcache, E_FS0_REGNUM + i,
436                                       fpregs, len);
437       am33_collect_fpregset_method (regset, regcache, 
438                                     E_FPCR_REGNUM, fpregs, len);
439     }
440   else if (regnum == E_FPCR_REGNUM)
441     regcache_raw_collect (current_regcache, E_FPCR_REGNUM, 
442                           &fpregset->fpcr);
443   else if (E_FS0_REGNUM <= regnum
444            && regnum < E_FS0_REGNUM + MN10300_ELF_NFPREG)
445     regcache_raw_collect (current_regcache, regnum, 
446                           &fpregset->fpregs[regnum - E_FS0_REGNUM]);
447
448   return;
449 }
450
451 /* Create a struct regset from a corefile register section.  */
452
453 static const struct regset *
454 am33_regset_from_core_section (struct gdbarch *gdbarch, 
455                                const char *sect_name, 
456                                size_t sect_size)
457 {
458   /* We will call regset_alloc, and pass the names of the supply and
459      collect methods.  */
460
461   if (sect_size == sizeof (mn10300_elf_fpregset_t))
462     return regset_alloc (gdbarch, 
463                          am33_supply_fpregset_method,
464                          am33_collect_fpregset_method);
465   else
466     return regset_alloc (gdbarch, 
467                          am33_supply_gregset_method,
468                          am33_collect_gregset_method);
469 }
470
471 /* AM33 Linux osabi has been recognized.
472    Now's our chance to register our corefile handling.  */
473
474 static void
475 am33_linux_init_osabi (struct gdbarch_info gdbinfo, struct gdbarch *gdbarch)
476 {
477   set_gdbarch_regset_from_core_section (gdbarch, 
478                                         am33_regset_from_core_section);
479   set_solib_svr4_fetch_link_map_offsets
480     (gdbarch, svr4_ilp32_fetch_link_map_offsets);
481 }
482
483 void
484 _initialize_mn10300_linux_tdep (void)
485 {
486   gdbarch_register_osabi (bfd_arch_mn10300, 0,
487                           GDB_OSABI_LINUX, am33_linux_init_osabi);
488 }
489