Updated copyright notices for most files.
[external/binutils.git] / gdb / i386-dicos-tdep.c
1 /* Target-dependent code for DICOS running on i386's, for GDB.
2
3    Copyright (C) 2008, 2009 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 3 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, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "osabi.h"
22 #include "gdb_string.h"
23 #include "solib.h"
24 #include "solib-target.h"
25 #include "inferior.h"
26
27 static CORE_ADDR
28 i386_dicos_push_dummy_code (struct gdbarch *gdbarch,
29                             CORE_ADDR sp, CORE_ADDR funaddr,
30                             struct value **args, int nargs,
31                             struct type *value_type,
32                             CORE_ADDR *real_pc, CORE_ADDR *bp_addr,
33                             struct regcache *regcache)
34 {
35   int bplen;
36   CORE_ADDR bppc = sp;
37
38   gdbarch_breakpoint_from_pc (gdbarch, &bppc, &bplen);
39   *bp_addr = sp - bplen;
40   *real_pc = funaddr;
41
42   return *bp_addr;
43 }
44
45 static void
46 i386_dicos_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
47 {
48   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
49
50   set_solib_ops (gdbarch, &solib_target_so_ops);
51
52   /* Every process, although has its own address space, sees the same
53      list of shared libraries.  */
54   set_gdbarch_has_global_solist (gdbarch, 1);
55
56   /* There's no (standard definition of) entry point or a guaranteed
57      text location we could find with a symbol where to place the call
58      dummy, so we put it on the stack.  */
59   set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
60   set_gdbarch_push_dummy_code (gdbarch, i386_dicos_push_dummy_code);
61
62   /* DICOS rewinds itself.  Need to override the i386 default which is
63      to decrement the PC.  */
64   set_gdbarch_decr_pc_after_break (gdbarch, 0);
65 }
66
67 /* Look in the elf symbol table of ABFD for a symbol named WANTED.
68    Return true if found.  */
69 static int
70 i386_dicos_bfd_has_symbol_p (bfd *abfd, const char *wanted)
71 {
72   long storage_needed;
73   int ret = 0;
74   asymbol **symbol_table = NULL;
75
76   storage_needed = bfd_get_symtab_upper_bound (abfd);
77   if (storage_needed < 0)
78     {
79       warning (_("Can't read elf symbols from %s: %s"), bfd_get_filename (abfd),
80                bfd_errmsg (bfd_get_error ()));
81       return 0;
82     }
83
84   if (storage_needed > 0)
85     {
86       long i, symcount;
87
88       symbol_table = xmalloc (storage_needed);
89       symcount = bfd_canonicalize_symtab (abfd, symbol_table);
90
91       if (symcount < 0)
92         warning (_("Can't read elf symbols from %s: %s"),
93                  bfd_get_filename (abfd),
94                  bfd_errmsg (bfd_get_error ()));
95       else
96         {
97           for (i = 0; i < symcount; i++)
98             {
99               asymbol *sym = symbol_table[i];
100               if (sym->name != NULL
101                   && wanted[0] == sym->name[0]
102                   && strcmp (wanted + 1, sym->name + 1) == 0)
103                 {
104                   ret = 1;
105                   break;
106                 }
107             }
108         }
109     }
110
111   xfree (symbol_table);
112   return ret;
113 }
114
115 static enum gdb_osabi
116 i386_dicos_osabi_sniffer (bfd *abfd)
117 {
118   char *target_name = bfd_get_target (abfd);
119
120   /* DICOS debug info files don't have a .note.ABI-tag marker or
121      something similar.  We do know there's always a "header" section
122      of 36 bytes, and there's always a "Dicos_loadModuleInfo" symbol
123      defined.  Look for the section first, as that should be
124      cheaper.  */
125   if (strcmp (target_name, "elf32-i386") == 0)
126     {
127       asection *section = bfd_get_section_by_name (abfd, "header");
128       if (section
129           && bfd_section_size (abfd, section) == 36
130           && i386_dicos_bfd_has_symbol_p (abfd, "Dicos_loadModuleInfo"))
131         return GDB_OSABI_DICOS;
132     }
133
134   return GDB_OSABI_UNKNOWN;
135 }
136
137 /* Provide a prototype to silence -Wmissing-prototypes.  */
138 void _initialize_i386_dicos_tdep (void);
139
140 void
141 _initialize_i386_dicos_tdep (void)
142 {
143   gdbarch_register_osabi_sniffer (bfd_arch_i386, bfd_target_elf_flavour,
144                                   i386_dicos_osabi_sniffer);
145
146   gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_DICOS,
147                           i386_dicos_init_abi);
148 }