update copyright year range in GDB files
[external/binutils.git] / gdb / nto-tdep.c
1 /* nto-tdep.c - general QNX Neutrino target functionality.
2
3    Copyright (C) 2003-2017 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 3 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, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23 #include <sys/stat.h>
24 #include "nto-tdep.h"
25 #include "top.h"
26 #include "inferior.h"
27 #include "infrun.h"
28 #include "gdbarch.h"
29 #include "bfd.h"
30 #include "elf-bfd.h"
31 #include "solib-svr4.h"
32 #include "gdbcore.h"
33 #include "objfiles.h"
34
35 #define QNX_NOTE_NAME   "QNX"
36 #define QNX_INFO_SECT_NAME "QNX_info"
37
38 #ifdef __CYGWIN__
39 #include <sys/cygwin.h>
40 #endif
41
42 #ifdef __CYGWIN__
43 static char default_nto_target[] = "C:\\QNXsdk\\target\\qnx6";
44 #elif defined(__sun__) || defined(linux)
45 static char default_nto_target[] = "/opt/QNXsdk/target/qnx6";
46 #else
47 static char default_nto_target[] = "";
48 #endif
49
50 struct nto_target_ops current_nto_target;
51
52 static const struct inferior_data *nto_inferior_data_reg;
53
54 static char *
55 nto_target (void)
56 {
57   char *p = getenv ("QNX_TARGET");
58
59 #ifdef __CYGWIN__
60   static char buf[PATH_MAX];
61   if (p)
62     cygwin_conv_path (CCP_WIN_A_TO_POSIX, p, buf, PATH_MAX);
63   else
64     cygwin_conv_path (CCP_WIN_A_TO_POSIX, default_nto_target, buf, PATH_MAX);
65   return buf;
66 #else
67   return p ? p : default_nto_target;
68 #endif
69 }
70
71 /* Take a string such as i386, rs6000, etc. and map it onto CPUTYPE_X86,
72    CPUTYPE_PPC, etc. as defined in nto-share/dsmsgs.h.  */
73 int
74 nto_map_arch_to_cputype (const char *arch)
75 {
76   if (!strcmp (arch, "i386") || !strcmp (arch, "x86"))
77     return CPUTYPE_X86;
78   if (!strcmp (arch, "rs6000") || !strcmp (arch, "powerpc"))
79     return CPUTYPE_PPC;
80   if (!strcmp (arch, "mips"))
81     return CPUTYPE_MIPS;
82   if (!strcmp (arch, "arm"))
83     return CPUTYPE_ARM;
84   if (!strcmp (arch, "sh"))
85     return CPUTYPE_SH;
86   return CPUTYPE_UNKNOWN;
87 }
88
89 int
90 nto_find_and_open_solib (const char *solib, unsigned o_flags,
91                          char **temp_pathname)
92 {
93   char *buf, *arch_path, *nto_root;
94   const char *endian;
95   const char *base;
96   const char *arch;
97   int arch_len, len, ret;
98 #define PATH_FMT \
99   "%s/lib:%s/usr/lib:%s/usr/photon/lib:%s/usr/photon/dll:%s/lib/dll"
100
101   nto_root = nto_target ();
102   if (strcmp (gdbarch_bfd_arch_info (target_gdbarch ())->arch_name, "i386") == 0)
103     {
104       arch = "x86";
105       endian = "";
106     }
107   else if (strcmp (gdbarch_bfd_arch_info (target_gdbarch ())->arch_name,
108                    "rs6000") == 0
109            || strcmp (gdbarch_bfd_arch_info (target_gdbarch ())->arch_name,
110                    "powerpc") == 0)
111     {
112       arch = "ppc";
113       endian = "be";
114     }
115   else
116     {
117       arch = gdbarch_bfd_arch_info (target_gdbarch ())->arch_name;
118       endian = gdbarch_byte_order (target_gdbarch ())
119                == BFD_ENDIAN_BIG ? "be" : "le";
120     }
121
122   /* In case nto_root is short, add strlen(solib)
123      so we can reuse arch_path below.  */
124
125   arch_len = (strlen (nto_root) + strlen (arch) + strlen (endian) + 2
126               + strlen (solib));
127   arch_path = (char *) alloca (arch_len);
128   xsnprintf (arch_path, arch_len, "%s/%s%s", nto_root, arch, endian);
129
130   len = strlen (PATH_FMT) + strlen (arch_path) * 5 + 1;
131   buf = (char *) alloca (len);
132   xsnprintf (buf, len, PATH_FMT, arch_path, arch_path, arch_path, arch_path,
133              arch_path);
134
135   base = lbasename (solib);
136   ret = openp (buf, OPF_TRY_CWD_FIRST | OPF_RETURN_REALPATH, base, o_flags,
137                temp_pathname);
138   if (ret < 0 && base != solib)
139     {
140       xsnprintf (arch_path, arch_len, "/%s", solib);
141       ret = open (arch_path, o_flags, 0);
142       if (temp_pathname)
143         {
144           if (ret >= 0)
145             *temp_pathname = gdb_realpath (arch_path);
146           else
147             *temp_pathname = NULL;
148         }
149     }
150   return ret;
151 }
152
153 void
154 nto_init_solib_absolute_prefix (void)
155 {
156   char buf[PATH_MAX * 2], arch_path[PATH_MAX];
157   char *nto_root;
158   const char *endian;
159   const char *arch;
160
161   nto_root = nto_target ();
162   if (strcmp (gdbarch_bfd_arch_info (target_gdbarch ())->arch_name, "i386") == 0)
163     {
164       arch = "x86";
165       endian = "";
166     }
167   else if (strcmp (gdbarch_bfd_arch_info (target_gdbarch ())->arch_name,
168                    "rs6000") == 0
169            || strcmp (gdbarch_bfd_arch_info (target_gdbarch ())->arch_name,
170                    "powerpc") == 0)
171     {
172       arch = "ppc";
173       endian = "be";
174     }
175   else
176     {
177       arch = gdbarch_bfd_arch_info (target_gdbarch ())->arch_name;
178       endian = gdbarch_byte_order (target_gdbarch ())
179                == BFD_ENDIAN_BIG ? "be" : "le";
180     }
181
182   xsnprintf (arch_path, sizeof (arch_path), "%s/%s%s", nto_root, arch, endian);
183
184   xsnprintf (buf, sizeof (buf), "set solib-absolute-prefix %s", arch_path);
185   execute_command (buf, 0);
186 }
187
188 char **
189 nto_parse_redirection (char *pargv[], const char **pin, const char **pout, 
190                        const char **perr)
191 {
192   char **argv;
193   char *in, *out, *err, *p;
194   int argc, i, n;
195
196   for (n = 0; pargv[n]; n++);
197   if (n == 0)
198     return NULL;
199   in = "";
200   out = "";
201   err = "";
202
203   argv = XCNEWVEC (char *, n + 1);
204   argc = n;
205   for (i = 0, n = 0; n < argc; n++)
206     {
207       p = pargv[n];
208       if (*p == '>')
209         {
210           p++;
211           if (*p)
212             out = p;
213           else
214             out = pargv[++n];
215         }
216       else if (*p == '<')
217         {
218           p++;
219           if (*p)
220             in = p;
221           else
222             in = pargv[++n];
223         }
224       else if (*p++ == '2' && *p++ == '>')
225         {
226           if (*p == '&' && *(p + 1) == '1')
227             err = out;
228           else if (*p)
229             err = p;
230           else
231             err = pargv[++n];
232         }
233       else
234         argv[i++] = pargv[n];
235     }
236   *pin = in;
237   *pout = out;
238   *perr = err;
239   return argv;
240 }
241
242 /* The struct lm_info, lm_addr, and nto_truncate_ptr are copied from
243    solib-svr4.c to support nto_relocate_section_addresses
244    which is different from the svr4 version.  */
245
246 /* Link map info to include in an allocated so_list entry */
247
248 struct lm_info
249   {
250     /* Pointer to copy of link map from inferior.  The type is char *
251        rather than void *, so that we may use byte offsets to find the
252        various fields without the need for a cast.  */
253     gdb_byte *lm;
254
255     /* Amount by which addresses in the binary should be relocated to
256        match the inferior.  This could most often be taken directly
257        from lm, but when prelinking is involved and the prelink base
258        address changes, we may need a different offset, we want to
259        warn about the difference and compute it only once.  */
260     CORE_ADDR l_addr;
261
262     /* The target location of lm.  */
263     CORE_ADDR lm_addr;
264   };
265
266
267 static CORE_ADDR
268 lm_addr (struct so_list *so)
269 {
270   if (so->lm_info->l_addr == (CORE_ADDR)-1)
271     {
272       struct link_map_offsets *lmo = nto_fetch_link_map_offsets ();
273       struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
274
275       so->lm_info->l_addr =
276         extract_typed_address (so->lm_info->lm + lmo->l_addr_offset, ptr_type);
277     }
278   return so->lm_info->l_addr;
279 }
280
281 static CORE_ADDR
282 nto_truncate_ptr (CORE_ADDR addr)
283 {
284   if (gdbarch_ptr_bit (target_gdbarch ()) == sizeof (CORE_ADDR) * 8)
285     /* We don't need to truncate anything, and the bit twiddling below
286        will fail due to overflow problems.  */
287     return addr;
288   else
289     return addr & (((CORE_ADDR) 1 << gdbarch_ptr_bit (target_gdbarch ())) - 1);
290 }
291
292 static Elf_Internal_Phdr *
293 find_load_phdr (bfd *abfd)
294 {
295   Elf_Internal_Phdr *phdr;
296   unsigned int i;
297
298   if (!elf_tdata (abfd))
299     return NULL;
300
301   phdr = elf_tdata (abfd)->phdr;
302   for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++)
303     {
304       if (phdr->p_type == PT_LOAD && (phdr->p_flags & PF_X))
305         return phdr;
306     }
307   return NULL;
308 }
309
310 void
311 nto_relocate_section_addresses (struct so_list *so, struct target_section *sec)
312 {
313   /* Neutrino treats the l_addr base address field in link.h as different than
314      the base address in the System V ABI and so the offset needs to be
315      calculated and applied to relocations.  */
316   Elf_Internal_Phdr *phdr = find_load_phdr (sec->the_bfd_section->owner);
317   unsigned vaddr = phdr ? phdr->p_vaddr : 0;
318
319   sec->addr = nto_truncate_ptr (sec->addr + lm_addr (so) - vaddr);
320   sec->endaddr = nto_truncate_ptr (sec->endaddr + lm_addr (so) - vaddr);
321 }
322
323 /* This is cheating a bit because our linker code is in libc.so.  If we
324    ever implement lazy linking, this may need to be re-examined.  */
325 int
326 nto_in_dynsym_resolve_code (CORE_ADDR pc)
327 {
328   if (in_plt_section (pc))
329     return 1;
330   return 0;
331 }
332
333 void
334 nto_dummy_supply_regset (struct regcache *regcache, char *regs)
335 {
336   /* Do nothing.  */
337 }
338
339 static void
340 nto_sniff_abi_note_section (bfd *abfd, asection *sect, void *obj)
341 {
342   const char *sectname;
343   unsigned int sectsize;
344   /* Buffer holding the section contents.  */
345   char *note;
346   unsigned int namelen;
347   const char *name;
348   const unsigned sizeof_Elf_Nhdr = 12;
349
350   sectname = bfd_get_section_name (abfd, sect);
351   sectsize = bfd_section_size (abfd, sect);
352
353   if (sectsize > 128)
354     sectsize = 128;
355
356   if (sectname != NULL && strstr (sectname, QNX_INFO_SECT_NAME) != NULL)
357     *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
358   else if (sectname != NULL && strstr (sectname, "note") != NULL
359            && sectsize > sizeof_Elf_Nhdr)
360     {
361       note = XNEWVEC (char, sectsize);
362       bfd_get_section_contents (abfd, sect, note, 0, sectsize);
363       namelen = (unsigned int) bfd_h_get_32 (abfd, note);
364       name = note + sizeof_Elf_Nhdr;
365       if (sectsize >= namelen + sizeof_Elf_Nhdr
366           && namelen == sizeof (QNX_NOTE_NAME)
367           && 0 == strcmp (name, QNX_NOTE_NAME))
368         *(enum gdb_osabi *) obj = GDB_OSABI_QNXNTO;
369
370       XDELETEVEC (note);
371     }
372 }
373
374 enum gdb_osabi
375 nto_elf_osabi_sniffer (bfd *abfd)
376 {
377   enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
378
379   bfd_map_over_sections (abfd,
380                          nto_sniff_abi_note_section,
381                          &osabi);
382
383   return osabi;
384 }
385
386 static const char *nto_thread_state_str[] =
387 {
388   "DEAD",               /* 0  0x00 */
389   "RUNNING",    /* 1  0x01 */
390   "READY",      /* 2  0x02 */
391   "STOPPED",    /* 3  0x03 */
392   "SEND",               /* 4  0x04 */
393   "RECEIVE",    /* 5  0x05 */
394   "REPLY",      /* 6  0x06 */
395   "STACK",      /* 7  0x07 */
396   "WAITTHREAD", /* 8  0x08 */
397   "WAITPAGE",   /* 9  0x09 */
398   "SIGSUSPEND", /* 10 0x0a */
399   "SIGWAITINFO",        /* 11 0x0b */
400   "NANOSLEEP",  /* 12 0x0c */
401   "MUTEX",      /* 13 0x0d */
402   "CONDVAR",    /* 14 0x0e */
403   "JOIN",               /* 15 0x0f */
404   "INTR",               /* 16 0x10 */
405   "SEM",                /* 17 0x11 */
406   "WAITCTX",    /* 18 0x12 */
407   "NET_SEND",   /* 19 0x13 */
408   "NET_REPLY"   /* 20 0x14 */
409 };
410
411 char *
412 nto_extra_thread_info (struct target_ops *self, struct thread_info *ti)
413 {
414   if (ti && ti->priv
415       && ti->priv->state < ARRAY_SIZE (nto_thread_state_str))
416     return (char *)nto_thread_state_str [ti->priv->state];
417   return "";
418 }
419
420 void
421 nto_initialize_signals (void)
422 {
423   /* We use SIG45 for pulses, or something, so nostop, noprint
424      and pass them.  */
425   signal_stop_update (gdb_signal_from_name ("SIG45"), 0);
426   signal_print_update (gdb_signal_from_name ("SIG45"), 0);
427   signal_pass_update (gdb_signal_from_name ("SIG45"), 1);
428
429   /* By default we don't want to stop on these two, but we do want to pass.  */
430 #if defined(SIGSELECT)
431   signal_stop_update (SIGSELECT, 0);
432   signal_print_update (SIGSELECT, 0);
433   signal_pass_update (SIGSELECT, 1);
434 #endif
435
436 #if defined(SIGPHOTON)
437   signal_stop_update (SIGPHOTON, 0);
438   signal_print_update (SIGPHOTON, 0);
439   signal_pass_update (SIGPHOTON, 1);
440 #endif
441 }
442
443 /* Read AUXV from initial_stack.  */
444 LONGEST
445 nto_read_auxv_from_initial_stack (CORE_ADDR initial_stack, gdb_byte *readbuf,
446                                   LONGEST len, size_t sizeof_auxv_t)
447 {
448   gdb_byte targ32[4]; /* For 32 bit target values.  */
449   gdb_byte targ64[8]; /* For 64 bit target values.  */
450   CORE_ADDR data_ofs = 0;
451   ULONGEST anint;
452   LONGEST len_read = 0;
453   gdb_byte *buff;
454   enum bfd_endian byte_order;
455   int ptr_size;
456
457   if (sizeof_auxv_t == 16)
458     ptr_size = 8;
459   else
460     ptr_size = 4;
461
462   /* Skip over argc, argv and envp... Comment from ldd.c:
463
464      The startup frame is set-up so that we have:
465      auxv
466      NULL
467      ...
468      envp2
469      envp1 <----- void *frame + (argc + 2) * sizeof(char *)
470      NULL
471      ...
472      argv2
473      argv1
474      argc  <------ void * frame
475
476      On entry to ldd, frame gives the address of argc on the stack.  */
477   /* Read argc. 4 bytes on both 64 and 32 bit arches and luckily little
478    * endian. So we just read first 4 bytes.  */
479   if (target_read_memory (initial_stack + data_ofs, targ32, 4) != 0)
480     return 0;
481
482   byte_order = gdbarch_byte_order (target_gdbarch ());
483
484   anint = extract_unsigned_integer (targ32, sizeof (targ32), byte_order);
485
486   /* Size of pointer is assumed to be 4 bytes (32 bit arch.) */
487   data_ofs += (anint + 2) * ptr_size; /* + 2 comes from argc itself and
488                                                 NULL terminating pointer in
489                                                 argv.  */
490
491   /* Now loop over env table:  */
492   anint = 0;
493   while (target_read_memory (initial_stack + data_ofs, targ64, ptr_size)
494          == 0)
495     {
496       if (extract_unsigned_integer (targ64, ptr_size, byte_order) == 0)
497         anint = 1; /* Keep looping until non-null entry is found.  */
498       else if (anint)
499         break;
500       data_ofs += ptr_size;
501     }
502   initial_stack += data_ofs;
503
504   memset (readbuf, 0, len);
505   buff = readbuf;
506   while (len_read <= len-sizeof_auxv_t)
507     {
508       if (target_read_memory (initial_stack + len_read, buff, sizeof_auxv_t)
509           == 0)
510         {
511           /* Both 32 and 64 bit structures have int as the first field.  */
512           const ULONGEST a_type
513             = extract_unsigned_integer (buff, sizeof (targ32), byte_order);
514
515           if (a_type == AT_NULL)
516             break;
517           buff += sizeof_auxv_t;
518           len_read += sizeof_auxv_t;
519         }
520       else
521         break;
522     }
523   return len_read;
524 }
525
526 /* Allocate new nto_inferior_data object.  */
527
528 static struct nto_inferior_data *
529 nto_new_inferior_data (void)
530 {
531   struct nto_inferior_data *const inf_data
532     = XCNEW (struct nto_inferior_data);
533
534   return inf_data;
535 }
536
537 /* Free inferior data.  */
538
539 static void
540 nto_inferior_data_cleanup (struct inferior *const inf, void *const dat)
541 {
542   xfree (dat);
543 }
544
545 /* Return nto_inferior_data for the given INFERIOR.  If not yet created,
546    construct it.  */
547
548 struct nto_inferior_data *
549 nto_inferior_data (struct inferior *const inferior)
550 {
551   struct inferior *const inf = inferior ? inferior : current_inferior ();
552   struct nto_inferior_data *inf_data;
553
554   gdb_assert (inf != NULL);
555
556   inf_data
557     = (struct nto_inferior_data *) inferior_data (inf, nto_inferior_data_reg);
558   if (inf_data == NULL)
559     {
560       set_inferior_data (inf, nto_inferior_data_reg,
561                          (inf_data = nto_new_inferior_data ()));
562     }
563
564   return inf_data;
565 }
566
567 /* Provide a prototype to silence -Wmissing-prototypes.  */
568 extern initialize_file_ftype _initialize_nto_tdep;
569
570 void
571 _initialize_nto_tdep (void)
572 {
573   nto_inferior_data_reg
574     = register_inferior_data_with_cleanup (NULL, nto_inferior_data_cleanup);
575 }