* nto-tdep.h: Include osabi.h. Prototypes for generic Neutrino
[external/binutils.git] / gdb / nto-tdep.c
1 /* nto-tdep.c - general QNX Neutrino target functionality.
2
3    Copyright 2003, 2004 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 "gdb_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 void
69 nto_set_target(struct nto_target_ops *targ)
70 {
71   nto_regset_id = targ->regset_id;
72   nto_supply_gregset = targ->supply_gregset;
73   nto_supply_fpregset = targ->supply_fpregset;
74   nto_supply_altregset = targ->supply_altregset;
75   nto_supply_regset = targ->supply_regset;
76   nto_register_area = targ->register_area;
77   nto_regset_fill = targ->regset_fill;
78   nto_fetch_link_map_offsets = targ->fetch_link_map_offsets;
79 }
80
81 /* Take a string such as i386, rs6000, etc. and map it onto CPUTYPE_X86,
82    CPUTYPE_PPC, etc. as defined in nto-share/dsmsgs.h.  */
83 int
84 nto_map_arch_to_cputype (const char *arch)
85 {
86   if (!strcmp (arch, "i386") || !strcmp (arch, "x86"))
87     return CPUTYPE_X86;
88   if (!strcmp (arch, "rs6000") || !strcmp (arch, "powerpc"))
89     return CPUTYPE_PPC;
90   if (!strcmp (arch, "mips"))
91     return CPUTYPE_MIPS;
92   if (!strcmp (arch, "arm"))
93     return CPUTYPE_ARM;
94   if (!strcmp (arch, "sh"))
95     return CPUTYPE_SH;
96   return CPUTYPE_UNKNOWN;
97 }
98
99 int
100 nto_find_and_open_solib (char *solib, unsigned o_flags, char **temp_pathname)
101 {
102   char *buf, *arch_path, *nto_root, *endian, *base;
103   const char *arch;
104   int ret;
105 #define PATH_FMT "%s/lib:%s/usr/lib:%s/usr/photon/lib:%s/usr/photon/dll:%s/lib/dll"
106
107   nto_root = nto_target ();
108   if (strcmp (TARGET_ARCHITECTURE->arch_name, "i386") == 0)
109     {
110       arch = "x86";
111       endian = "";
112     }
113   else if (strcmp (TARGET_ARCHITECTURE->arch_name, "rs6000") == 0
114            || strcmp (TARGET_ARCHITECTURE->arch_name, "powerpc") == 0)
115     {
116       arch = "ppc";
117       endian = "be";
118     }
119   else
120     {
121       arch = TARGET_ARCHITECTURE->arch_name;
122       endian = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "be" : "le";
123     }
124
125   /* In case nto_root is short, add strlen(solib)
126      so we can reuse arch_path below.  */
127   arch_path =
128     alloca (strlen (nto_root) + strlen (arch) + strlen (endian) + 2 +
129             strlen (solib));
130   sprintf (arch_path, "%s/%s%s", nto_root, arch, endian);
131
132   buf = alloca (strlen (PATH_FMT) + strlen (arch_path) * 5 + 1);
133   sprintf (buf, PATH_FMT, arch_path, arch_path, arch_path, arch_path,
134            arch_path);
135
136   /* Don't assume basename() isn't destructive.  */
137   base = strrchr (solib, '/');
138   if (!base)
139     base = solib;
140   else
141     base++;                     /* Skip over '/'.  */
142
143   ret = openp (buf, 1, base, o_flags, 0, temp_pathname);
144   if (ret < 0 && base != solib)
145     {
146       sprintf (arch_path, "/%s", solib);
147       ret = open (arch_path, o_flags, 0);
148       if (temp_pathname)
149         {
150           if (ret >= 0)
151             *temp_pathname = gdb_realpath (arch_path);
152           else
153             **temp_pathname = '\0';
154         }
155     }
156   return ret;
157 }
158
159 void
160 nto_init_solib_absolute_prefix (void)
161 {
162   char buf[PATH_MAX * 2], arch_path[PATH_MAX];
163   char *nto_root, *endian;
164   const char *arch;
165
166   nto_root = nto_target ();
167   if (strcmp (TARGET_ARCHITECTURE->arch_name, "i386") == 0)
168     {
169       arch = "x86";
170       endian = "";
171     }
172   else if (strcmp (TARGET_ARCHITECTURE->arch_name, "rs6000") == 0
173            || strcmp (TARGET_ARCHITECTURE->arch_name, "powerpc") == 0)
174     {
175       arch = "ppc";
176       endian = "be";
177     }
178   else
179     {
180       arch = TARGET_ARCHITECTURE->arch_name;
181       endian = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "be" : "le";
182     }
183
184   sprintf (arch_path, "%s/%s%s", nto_root, arch, endian);
185
186   sprintf (buf, "set solib-absolute-prefix %s", arch_path);
187   execute_command (buf, 0);
188 }
189
190 char **
191 nto_parse_redirection (char *pargv[], char **pin, char **pout, char **perr)
192 {
193   char **argv;
194   char *in, *out, *err, *p;
195   int argc, i, n;
196
197   for (n = 0; pargv[n]; n++);
198   if (n == 0)
199     return NULL;
200   in = "";
201   out = "";
202   err = "";
203
204   argv = xcalloc (n + 1, sizeof argv[0]);
205   argc = n;
206   for (i = 0, n = 0; n < argc; n++)
207     {
208       p = pargv[n];
209       if (*p == '>')
210         {
211           p++;
212           if (*p)
213             out = p;
214           else
215             out = pargv[++n];
216         }
217       else if (*p == '<')
218         {
219           p++;
220           if (*p)
221             in = p;
222           else
223             in = pargv[++n];
224         }
225       else if (*p++ == '2' && *p++ == '>')
226         {
227           if (*p == '&' && *(p + 1) == '1')
228             err = out;
229           else if (*p)
230             err = p;
231           else
232             err = pargv[++n];
233         }
234       else
235         argv[i++] = pargv[n];
236     }
237   *pin = in;
238   *pout = out;
239   *perr = err;
240   return argv;
241 }
242
243 /* The struct lm_info, LM_ADDR, and nto_truncate_ptr are copied from
244    solib-svr4.c to support nto_relocate_section_addresses
245    which is different from the svr4 version.  */
246
247 struct lm_info
248 {
249   /* Pointer to copy of link map from inferior.  The type is char *
250      rather than void *, so that we may use byte offsets to find the
251      various fields without the need for a cast.  */
252   char *lm;
253 };
254
255 static CORE_ADDR
256 LM_ADDR (struct so_list *so)
257 {
258   struct link_map_offsets *lmo = nto_fetch_link_map_offsets ();
259
260   return (CORE_ADDR) extract_signed_integer (so->lm_info->lm +
261                                              lmo->l_addr_offset,
262                                              lmo->l_addr_size);
263 }
264
265 static CORE_ADDR
266 nto_truncate_ptr (CORE_ADDR addr)
267 {
268   if (TARGET_PTR_BIT == sizeof (CORE_ADDR) * 8)
269     /* We don't need to truncate anything, and the bit twiddling below
270        will fail due to overflow problems.  */
271     return addr;
272   else
273     return addr & (((CORE_ADDR) 1 << TARGET_PTR_BIT) - 1);
274 }
275
276 Elf_Internal_Phdr *
277 find_load_phdr (bfd *abfd)
278 {
279   Elf_Internal_Phdr *phdr;
280   unsigned int i;
281
282   if (!elf_tdata (abfd))
283     return NULL;
284
285   phdr = elf_tdata (abfd)->phdr;
286   for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
287     {
288       if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X))
289         return phdr;
290     }
291   return NULL;
292 }
293
294 void
295 nto_relocate_section_addresses (struct so_list *so, struct section_table *sec)
296 {
297   /* Neutrino treats the l_addr base address field in link.h as different than
298      the base address in the System V ABI and so the offset needs to be
299      calculated and applied to relocations.  */
300   Elf_Internal_Phdr *phdr = find_load_phdr (sec->bfd);
301   unsigned vaddr = phdr ? phdr->p_vaddr : 0;
302
303   sec->addr = nto_truncate_ptr (sec->addr + LM_ADDR (so) - vaddr);
304   sec->endaddr = nto_truncate_ptr (sec->endaddr + LM_ADDR (so) - vaddr);
305 }
306
307 /* This is cheating a bit because our linker code is in libc.so.  If we
308    ever implement lazy linking, this may need to be re-examined.  */
309 int
310 nto_in_dynsym_resolve_code (CORE_ADDR pc)
311 {
312   if (in_plt_section (pc, NULL))
313     return 1;
314   return 0;
315 }
316
317 void
318 nto_generic_supply_gpregset (const struct regset *regset,
319                              struct regcache *regcache, int regnum,
320                              const void *gregs, size_t len)
321 {
322 }
323
324 void
325 nto_generic_supply_fpregset (const struct regset *regset,
326                              struct regcache *regcache, int regnum,
327                              const void *fpregs, size_t len)
328 {
329 }
330
331 void
332 nto_generic_supply_altregset (const struct regset *regset,
333                               struct regcache *regcache, int regnum,
334                               const void *altregs, size_t len)
335 {
336 }
337
338 void
339 nto_dummy_supply_regset (char *regs)
340 {
341   /* Do nothing.  */
342 }
343
344 enum gdb_osabi
345 nto_elf_osabi_sniffer (bfd *abfd)
346 {
347   if (nto_is_nto_target)
348       return nto_is_nto_target (abfd);
349   return GDB_OSABI_UNKNOWN;
350 }
351
352 static void
353 fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
354                       int which, CORE_ADDR reg_addr)
355 {
356   nto_regset_t regset;
357
358 /* See corelow.c:get_core_registers for values of WHICH.  */
359   if (which == 0)
360     {
361       memcpy ((char *) &regset, core_reg_sect,
362               min (core_reg_size, sizeof (regset)));
363       nto_supply_gregset ((char *) &regset);
364     }
365   else if (which == 2)
366     {
367       memcpy ((char *) &regset, core_reg_sect,
368               min (core_reg_size, sizeof (regset)));
369       nto_supply_fpregset ((char *) &regset);
370     }
371 }
372
373 /* Register that we are able to handle ELF file formats using standard
374    procfs "regset" structures.  */
375 static struct core_fns regset_core_fns = {
376   bfd_target_elf_flavour,       /* core_flavour */
377   default_check_format,         /* check_format */
378   default_core_sniffer,         /* core_sniffer */
379   fetch_core_registers,         /* core_read_registers */
380   NULL                          /* next */
381 };
382
383 void
384 nto_initialize_signals (void)
385 {
386   /* We use SIG45 for pulses, or something, so nostop, noprint
387      and pass them.  */
388   signal_stop_update (target_signal_from_name ("SIG45"), 0);
389   signal_print_update (target_signal_from_name ("SIG45"), 0);
390   signal_pass_update (target_signal_from_name ("SIG45"), 1);
391
392   /* By default we don't want to stop on these two, but we do want to pass.  */
393 #if defined(SIGSELECT)
394   signal_stop_update (SIGSELECT, 0);
395   signal_print_update (SIGSELECT, 0);
396   signal_pass_update (SIGSELECT, 1);
397 #endif
398
399 #if defined(SIGPHOTON)
400   signal_stop_update (SIGPHOTON, 0);
401   signal_print_update (SIGPHOTON, 0);
402   signal_pass_update (SIGPHOTON, 1);
403 #endif
404 }
405
406 void
407 _initialize_nto_tdep (void)
408 {
409   add_setshow_zinteger_cmd ("nto-debug", class_maintenance,
410                             &nto_internal_debugging, "\
411 Set QNX NTO internal debugging.", "\
412 Show QNX NTO internal debugging.", "\
413 When non-zero, nto specific debug info is\n\
414 displayed. Different information is displayed\n\
415 for different positive values.", "\
416 QNX NTO internal debugging is %s.", NULL, NULL, &setdebuglist, &showdebuglist);
417   /* Register core file support.  */
418   deprecated_add_core_fns (&regset_core_fns);
419 }