Added i386 QNX Neutrino support.
[external/binutils.git] / gdb / nto-tdep.c
1 /* nto-tdep.c - general QNX Neutrino target functionality.
2
3    Copyright 2003 Free Software Foundation, Inc.
4
5    Contributed by QNX Software Systems Ltd.
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 2 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, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include <sys/stat.h>
25 #include "gdb_string.h"
26 #include "nto-tdep.h"
27 #include "top.h"
28 #include "cli/cli-decode.h"
29 #include "cli/cli-cmds.h"
30 #include "inferior.h"
31 #include "gdbarch.h"
32 #include "bfd.h"
33 #include "elf-bfd.h"
34 #include "solib-svr4.h"
35 #include "gdbcore.h"
36
37 #ifdef __CYGWIN__
38 #include <sys/cygwin.h>
39 #endif
40
41 #ifdef __CYGWIN__
42 static char default_nto_target[] = "C:\\QNXsdk\\target\\qnx6";
43 #elif defined(__sun__) || defined(linux)
44 static char default_nto_target[] = "/opt/QNXsdk/target/qnx6";
45 #else
46 static char default_nto_target[] = "";
47 #endif
48
49 struct nto_target_ops current_nto_target;
50
51 static char *
52 nto_target (void)
53 {
54   char *p = getenv ("QNX_TARGET");
55
56 #ifdef __CYGWIN__
57   static char buf[PATH_MAX];
58   if (p)
59     cygwin_conv_to_posix_path (p, buf);
60   else
61     cygwin_conv_to_posix_path (default_nto_target, buf);
62   return buf;
63 #else
64   return p ? p : default_nto_target;
65 #endif
66 }
67
68 /* Take a string such as i386, rs6000, etc. and map it onto CPUTYPE_X86,
69    CPUTYPE_PPC, etc. as defined in nto-share/dsmsgs.h.  */
70 int
71 nto_map_arch_to_cputype (const char *arch)
72 {
73   if (!strcmp (arch, "i386") || !strcmp (arch, "x86"))
74     return CPUTYPE_X86;
75   if (!strcmp (arch, "rs6000") || !strcmp (arch, "ppc"))
76     return CPUTYPE_PPC;
77   if (!strcmp (arch, "mips"))
78     return CPUTYPE_MIPS;
79   if (!strcmp (arch, "arm"))
80     return CPUTYPE_ARM;
81   if (!strcmp (arch, "sh"))
82     return CPUTYPE_SH;
83   return CPUTYPE_UNKNOWN;
84 }
85
86 int
87 nto_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
88 {
89   char *buf, arch_path[PATH_MAX], *nto_root, *endian;
90   const char *arch;
91   char *path_fmt = "%s/lib:%s/usr/lib:%s/usr/photon/lib\
92 :%s/usr/photon/dll:%s/lib/dll";
93
94   nto_root = nto_target ();
95   if (strcmp (TARGET_ARCHITECTURE->arch_name, "i386") == 0)
96     {
97       arch = "x86";
98       endian = "";
99     }
100   else if (strcmp (TARGET_ARCHITECTURE->arch_name, "rs6000") == 0)
101     {
102       arch = "ppc";
103       endian = "be";
104     }
105   else
106     {
107       arch = TARGET_ARCHITECTURE->arch_name;
108       endian = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "be" : "le";
109     }
110
111   sprintf (arch_path, "%s/%s%s", nto_root, arch, endian);
112
113   buf = alloca (strlen (path_fmt) + strlen (arch_path) * 5 + 1);
114   sprintf (buf, path_fmt, arch_path, arch_path, arch_path, arch_path,
115            arch_path);
116
117   return openp (buf, 1, solib, o_flags, 0, temp_pathname);
118 }
119
120 void
121 nto_init_solib_absolute_prefix (void)
122 {
123   char buf[PATH_MAX * 2], arch_path[PATH_MAX];
124   char *nto_root, *endian;
125   const char *arch;
126
127   nto_root = nto_target ();
128   if (strcmp (TARGET_ARCHITECTURE->arch_name, "i386") == 0)
129     {
130       arch = "x86";
131       endian = "";
132     }
133   else if (strcmp (TARGET_ARCHITECTURE->arch_name, "rs6000") == 0)
134     {
135       arch = "ppc";
136       endian = "be";
137     }
138   else
139     {
140       arch = TARGET_ARCHITECTURE->arch_name;
141       endian = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "be" : "le";
142     }
143
144   sprintf (arch_path, "%s/%s%s", nto_root, arch, endian);
145
146   sprintf (buf, "set solib-absolute-prefix %s", arch_path);
147   execute_command (buf, 0);
148 }
149
150 char **
151 nto_parse_redirection (char *pargv[], char **pin, char **pout, char **perr)
152 {
153   char **argv;
154   char *in, *out, *err, *p;
155   int argc, i, n;
156
157   for (n = 0; pargv[n]; n++);
158   if (n == 0)
159     return NULL;
160   in = "";
161   out = "";
162   err = "";
163
164   argv = xcalloc (n + 1, sizeof argv[0]);
165   argc = n;
166   for (i = 0, n = 0; n < argc; n++)
167     {
168       p = pargv[n];
169       if (*p == '>')
170         {
171           p++;
172           if (*p)
173             out = p;
174           else
175             out = pargv[++n];
176         }
177       else if (*p == '<')
178         {
179           p++;
180           if (*p)
181             in = p;
182           else
183             in = pargv[++n];
184         }
185       else if (*p++ == '2' && *p++ == '>')
186         {
187           if (*p == '&' && *(p + 1) == '1')
188             err = out;
189           else if (*p)
190             err = p;
191           else
192             err = pargv[++n];
193         }
194       else
195         argv[i++] = pargv[n];
196     }
197   *pin = in;
198   *pout = out;
199   *perr = err;
200   return argv;
201 }
202
203 /* The struct lm_info, LM_ADDR, and nto_truncate_ptr are copied from
204    solib-svr4.c to support nto_relocate_section_addresses
205    which is different from the svr4 version.  */
206
207 struct lm_info
208 {
209   /* Pointer to copy of link map from inferior.  The type is char *
210      rather than void *, so that we may use byte offsets to find the
211      various fields without the need for a cast.  */
212   char *lm;
213 };
214
215 static CORE_ADDR
216 LM_ADDR (struct so_list *so)
217 {
218   struct link_map_offsets *lmo = nto_fetch_link_map_offsets ();
219
220   return (CORE_ADDR) extract_signed_integer (so->lm_info->lm +
221                                              lmo->l_addr_offset,
222                                              lmo->l_addr_size);
223 }
224
225 static CORE_ADDR
226 nto_truncate_ptr (CORE_ADDR addr)
227 {
228   if (TARGET_PTR_BIT == sizeof (CORE_ADDR) * 8)
229     /* We don't need to truncate anything, and the bit twiddling below
230        will fail due to overflow problems.  */
231     return addr;
232   else
233     return addr & (((CORE_ADDR) 1 << TARGET_PTR_BIT) - 1);
234 }
235
236 Elf_Internal_Phdr *
237 find_load_phdr (bfd *abfd)
238 {
239   Elf_Internal_Phdr *phdr;
240   unsigned int i;
241
242   if (!elf_tdata (abfd))
243     return NULL;
244
245   phdr = elf_tdata (abfd)->phdr;
246   for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
247     {
248       if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X))
249         return phdr;
250     }
251   return NULL;
252 }
253
254 void
255 nto_relocate_section_addresses (struct so_list *so, struct section_table *sec)
256 {
257   /* Neutrino treats the l_addr base address field in link.h as different than
258      the base address in the System V ABI and so the offset needs to be
259      calculated and applied to relocations.  */
260   Elf_Internal_Phdr *phdr = find_load_phdr (sec->bfd);
261   unsigned vaddr = phdr ? phdr->p_vaddr : 0;
262
263   sec->addr = nto_truncate_ptr (sec->addr + LM_ADDR (so) - vaddr);
264   sec->endaddr = nto_truncate_ptr (sec->endaddr + LM_ADDR (so) - vaddr);
265 }
266
267 static void
268 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
269                       int which, CORE_ADDR reg_addr)
270 {
271   nto_regset_t regset;
272
273   nto_init_solib_absolute_prefix ();
274
275   if (which == NTO_REG_GENERAL)
276     {
277       memcpy ((char *) &regset, core_reg_sect,
278               min (core_reg_size, sizeof (regset)));
279       nto_supply_gregset ((char *) &regset);
280     }
281   else if (which == NTO_REG_FLOAT)
282     {
283       memcpy ((char *) &regset, core_reg_sect,
284               min (core_reg_size, sizeof (regset)));
285       nto_supply_fpregset ((char *) &regset);
286     }
287   else if (which == NTO_REG_ALT)
288     {
289       memcpy ((char *) &regset, core_reg_sect,
290               min (core_reg_size, sizeof (regset)));
291       nto_supply_altregset ((char *) &regset);
292     }
293 }
294
295 void
296 nto_dummy_supply_regset (char *regs)
297 {
298   /* Do nothing.  */
299 }
300
301 /* Register that we are able to handle ELF file formats using standard
302    procfs "regset" structures.  */
303 static struct core_fns regset_core_fns = {
304   bfd_target_elf_flavour,       /* core_flavour */
305   default_check_format,         /* check_format */
306   default_core_sniffer,         /* core_sniffer */
307   fetch_core_registers,         /* core_read_registers */
308   NULL                          /* next */
309 };
310
311 void
312 _initialize_nto_tdep ()
313 {
314   add_show_from_set (add_set_cmd ("nto-debug", class_maintenance, var_zinteger, (char *) &nto_internal_debugging, "Set QNX NTO internal debugging.\n\
315 When non-zero, nto specific debug info is\n\
316 displayed. Different information is displayed\n\
317 for different positive values.", &setdebuglist),
318                      &showdebuglist);
319
320   /* We use SIG45 for pulses, or something, so nostop, noprint
321      and pass them.  */
322   signal_stop_update (target_signal_from_name ("SIG45"), 0);
323   signal_print_update (target_signal_from_name ("SIG45"), 0);
324   signal_pass_update (target_signal_from_name ("SIG45"), 1);
325
326   /* By default we don't want to stop on these two, but we do want to pass.  */
327 #if defined(SIGSELECT)
328   signal_stop_update (SIGSELECT, 0);
329   signal_print_update (SIGSELECT, 0);
330   signal_pass_update (SIGSELECT, 1);
331 #endif
332
333 #if defined(SIGPHOTON)
334   signal_stop_update (SIGPHOTON, 0);
335   signal_print_update (SIGPHOTON, 0);
336   signal_pass_update (SIGPHOTON, 1);
337 #endif
338
339   /* Register core file support.  */
340   add_core_fns (&regset_core_fns);
341 }