Commit | Line | Data |
---|---|---|
1b883d35 KW |
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 | ||
47979a4b | 24 | #include "gdb_stat.h" |
1b883d35 KW |
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 | ||
1b883d35 KW |
273 | if (which == NTO_REG_GENERAL) |
274 | { | |
275 | memcpy ((char *) ®set, core_reg_sect, | |
276 | min (core_reg_size, sizeof (regset))); | |
277 | nto_supply_gregset ((char *) ®set); | |
278 | } | |
279 | else if (which == NTO_REG_FLOAT) | |
280 | { | |
281 | memcpy ((char *) ®set, core_reg_sect, | |
282 | min (core_reg_size, sizeof (regset))); | |
283 | nto_supply_fpregset ((char *) ®set); | |
284 | } | |
285 | else if (which == NTO_REG_ALT) | |
286 | { | |
287 | memcpy ((char *) ®set, core_reg_sect, | |
288 | min (core_reg_size, sizeof (regset))); | |
289 | nto_supply_altregset ((char *) ®set); | |
290 | } | |
291 | } | |
292 | ||
293 | void | |
294 | nto_dummy_supply_regset (char *regs) | |
295 | { | |
296 | /* Do nothing. */ | |
297 | } | |
298 | ||
299 | /* Register that we are able to handle ELF file formats using standard | |
300 | procfs "regset" structures. */ | |
301 | static struct core_fns regset_core_fns = { | |
302 | bfd_target_elf_flavour, /* core_flavour */ | |
303 | default_check_format, /* check_format */ | |
304 | default_core_sniffer, /* core_sniffer */ | |
305 | fetch_core_registers, /* core_read_registers */ | |
306 | NULL /* next */ | |
307 | }; | |
308 | ||
309 | void | |
47979a4b | 310 | _initialize_nto_tdep (void) |
1b883d35 | 311 | { |
47979a4b KW |
312 | add_setshow_cmd ("nto-debug", class_maintenance, var_zinteger, |
313 | &nto_internal_debugging, "Set QNX NTO internal debugging.\n\ | |
314 | When non-zero, nto specific debug info is\n\ | |
315 | displayed. Different information is displayed\n\ | |
316 | for different positive values.", "Show QNX NTO internal debugging.\n", | |
317 | NULL, NULL, &setdebuglist, &showdebuglist); | |
1b883d35 KW |
318 | |
319 | /* We use SIG45 for pulses, or something, so nostop, noprint | |
320 | and pass them. */ | |
321 | signal_stop_update (target_signal_from_name ("SIG45"), 0); | |
322 | signal_print_update (target_signal_from_name ("SIG45"), 0); | |
323 | signal_pass_update (target_signal_from_name ("SIG45"), 1); | |
324 | ||
325 | /* By default we don't want to stop on these two, but we do want to pass. */ | |
326 | #if defined(SIGSELECT) | |
327 | signal_stop_update (SIGSELECT, 0); | |
328 | signal_print_update (SIGSELECT, 0); | |
329 | signal_pass_update (SIGSELECT, 1); | |
330 | #endif | |
331 | ||
332 | #if defined(SIGPHOTON) | |
333 | signal_stop_update (SIGPHOTON, 0); | |
334 | signal_print_update (SIGPHOTON, 0); | |
335 | signal_pass_update (SIGPHOTON, 1); | |
336 | #endif | |
337 | ||
338 | /* Register core file support. */ | |
339 | add_core_fns (®set_core_fns); | |
340 | } |