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 "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__
42static char default_nto_target[] = "C:\\QNXsdk\\target\\qnx6";
43#elif defined(__sun__) || defined(linux)
44static char default_nto_target[] = "/opt/QNXsdk/target/qnx6";
45#else
46static char default_nto_target[] = "";
47#endif
48
49struct nto_target_ops current_nto_target;
50
51static char *
52nto_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.  */
70int
71nto_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, "powerpc"))
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
86int
87nto_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	   || strcmp (TARGET_ARCHITECTURE->arch_name, "powerpc") == 0)
102    {
103      arch = "ppc";
104      endian = "be";
105    }
106  else
107    {
108      arch = TARGET_ARCHITECTURE->arch_name;
109      endian = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "be" : "le";
110    }
111
112  sprintf (arch_path, "%s/%s%s", nto_root, arch, endian);
113
114  buf = alloca (strlen (path_fmt) + strlen (arch_path) * 5 + 1);
115  sprintf (buf, path_fmt, arch_path, arch_path, arch_path, arch_path,
116	   arch_path);
117
118  return openp (buf, 1, solib, o_flags, 0, temp_pathname);
119}
120
121void
122nto_init_solib_absolute_prefix (void)
123{
124  char buf[PATH_MAX * 2], arch_path[PATH_MAX];
125  char *nto_root, *endian;
126  const char *arch;
127
128  nto_root = nto_target ();
129  if (strcmp (TARGET_ARCHITECTURE->arch_name, "i386") == 0)
130    {
131      arch = "x86";
132      endian = "";
133    }
134  else if (strcmp (TARGET_ARCHITECTURE->arch_name, "rs6000") == 0
135	   || strcmp (TARGET_ARCHITECTURE->arch_name, "powerpc") == 0)
136    {
137      arch = "ppc";
138      endian = "be";
139    }
140  else
141    {
142      arch = TARGET_ARCHITECTURE->arch_name;
143      endian = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? "be" : "le";
144    }
145
146  sprintf (arch_path, "%s/%s%s", nto_root, arch, endian);
147
148  sprintf (buf, "set solib-absolute-prefix %s", arch_path);
149  execute_command (buf, 0);
150}
151
152char **
153nto_parse_redirection (char *pargv[], char **pin, char **pout, char **perr)
154{
155  char **argv;
156  char *in, *out, *err, *p;
157  int argc, i, n;
158
159  for (n = 0; pargv[n]; n++);
160  if (n == 0)
161    return NULL;
162  in = "";
163  out = "";
164  err = "";
165
166  argv = xcalloc (n + 1, sizeof argv[0]);
167  argc = n;
168  for (i = 0, n = 0; n < argc; n++)
169    {
170      p = pargv[n];
171      if (*p == '>')
172	{
173	  p++;
174	  if (*p)
175	    out = p;
176	  else
177	    out = pargv[++n];
178	}
179      else if (*p == '<')
180	{
181	  p++;
182	  if (*p)
183	    in = p;
184	  else
185	    in = pargv[++n];
186	}
187      else if (*p++ == '2' && *p++ == '>')
188	{
189	  if (*p == '&' && *(p + 1) == '1')
190	    err = out;
191	  else if (*p)
192	    err = p;
193	  else
194	    err = pargv[++n];
195	}
196      else
197	argv[i++] = pargv[n];
198    }
199  *pin = in;
200  *pout = out;
201  *perr = err;
202  return argv;
203}
204
205/* The struct lm_info, LM_ADDR, and nto_truncate_ptr are copied from
206   solib-svr4.c to support nto_relocate_section_addresses
207   which is different from the svr4 version.  */
208
209struct lm_info
210{
211  /* Pointer to copy of link map from inferior.  The type is char *
212     rather than void *, so that we may use byte offsets to find the
213     various fields without the need for a cast.  */
214  char *lm;
215};
216
217static CORE_ADDR
218LM_ADDR (struct so_list *so)
219{
220  struct link_map_offsets *lmo = nto_fetch_link_map_offsets ();
221
222  return (CORE_ADDR) extract_signed_integer (so->lm_info->lm +
223					     lmo->l_addr_offset,
224					     lmo->l_addr_size);
225}
226
227static CORE_ADDR
228nto_truncate_ptr (CORE_ADDR addr)
229{
230  if (TARGET_PTR_BIT == sizeof (CORE_ADDR) * 8)
231    /* We don't need to truncate anything, and the bit twiddling below
232       will fail due to overflow problems.  */
233    return addr;
234  else
235    return addr & (((CORE_ADDR) 1 << TARGET_PTR_BIT) - 1);
236}
237
238Elf_Internal_Phdr *
239find_load_phdr (bfd *abfd)
240{
241  Elf_Internal_Phdr *phdr;
242  unsigned int i;
243
244  if (!elf_tdata (abfd))
245    return NULL;
246
247  phdr = elf_tdata (abfd)->phdr;
248  for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
249    {
250      if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X))
251	return phdr;
252    }
253  return NULL;
254}
255
256void
257nto_relocate_section_addresses (struct so_list *so, struct section_table *sec)
258{
259  /* Neutrino treats the l_addr base address field in link.h as different than
260     the base address in the System V ABI and so the offset needs to be
261     calculated and applied to relocations.  */
262  Elf_Internal_Phdr *phdr = find_load_phdr (sec->bfd);
263  unsigned vaddr = phdr ? phdr->p_vaddr : 0;
264
265  sec->addr = nto_truncate_ptr (sec->addr + LM_ADDR (so) - vaddr);
266  sec->endaddr = nto_truncate_ptr (sec->endaddr + LM_ADDR (so) - vaddr);
267}
268
269static void
270fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
271		      int which, CORE_ADDR reg_addr)
272{
273  nto_regset_t regset;
274
275/* See corelow.c:get_core_registers for values of WHICH.  */
276  if (which == 0)
277    {
278      memcpy ((char *) &regset, core_reg_sect,
279	      min (core_reg_size, sizeof (regset)));
280      nto_supply_gregset ((char *) &regset);
281    }
282  else if (which == 2)
283    {
284      memcpy ((char *) &regset, core_reg_sect,
285	      min (core_reg_size, sizeof (regset)));
286      nto_supply_fpregset ((char *) &regset);
287    }
288}
289
290void
291nto_dummy_supply_regset (char *regs)
292{
293  /* Do nothing.  */
294}
295
296/* Register that we are able to handle ELF file formats using standard
297   procfs "regset" structures.  */
298static struct core_fns regset_core_fns = {
299  bfd_target_elf_flavour,	/* core_flavour */
300  default_check_format,		/* check_format */
301  default_core_sniffer,		/* core_sniffer */
302  fetch_core_registers,		/* core_read_registers */
303  NULL				/* next */
304};
305
306void
307_initialize_nto_tdep (void)
308{
309  add_setshow_cmd ("nto-debug", class_maintenance, var_zinteger,
310		   &nto_internal_debugging, "Set QNX NTO internal debugging.\n\
311When non-zero, nto specific debug info is\n\
312displayed. Different information is displayed\n\
313for different positive values.", "Show QNX NTO internal debugging.\n",
314		   NULL, NULL, &setdebuglist, &showdebuglist);
315
316  /* We use SIG45 for pulses, or something, so nostop, noprint
317     and pass them.  */
318  signal_stop_update (target_signal_from_name ("SIG45"), 0);
319  signal_print_update (target_signal_from_name ("SIG45"), 0);
320  signal_pass_update (target_signal_from_name ("SIG45"), 1);
321
322  /* By default we don't want to stop on these two, but we do want to pass.  */
323#if defined(SIGSELECT)
324  signal_stop_update (SIGSELECT, 0);
325  signal_print_update (SIGSELECT, 0);
326  signal_pass_update (SIGSELECT, 1);
327#endif
328
329#if defined(SIGPHOTON)
330  signal_stop_update (SIGPHOTON, 0);
331  signal_print_update (SIGPHOTON, 0);
332  signal_pass_update (SIGPHOTON, 1);
333#endif
334
335  /* Register core file support.  */
336  add_core_fns (&regset_core_fns);
337}
338