]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbserver/netbsd-low.cc
refactor: Simplify SVE interface to read/write registers
[thirdparty/binutils-gdb.git] / gdbserver / netbsd-low.cc
CommitLineData
213516ef 1/* Copyright (C) 2020-2023 Free Software Foundation, Inc.
62ba5048
KR
2
3 This file is part of GDB.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17
18#include "server.h"
19#include "target.h"
20#include "netbsd-low.h"
21#include "nat/netbsd-nat.h"
22
23#include <sys/param.h>
24#include <sys/types.h>
25
26#include <sys/ptrace.h>
27#include <sys/sysctl.h>
28
29#include <limits.h>
30#include <unistd.h>
31#include <signal.h>
32
33#include <elf.h>
34
35#include <type_traits>
36
37#include "gdbsupport/eintr.h"
38#include "gdbsupport/gdb_wait.h"
39#include "gdbsupport/filestuff.h"
40#include "gdbsupport/common-inferior.h"
41#include "nat/fork-inferior.h"
42#include "hostio.h"
43
44int using_threads = 1;
45
62ba5048
KR
46/* Callback used by fork_inferior to start tracing the inferior. */
47
48static void
49netbsd_ptrace_fun ()
50{
51 /* Switch child to its own process group so that signals won't
52 directly affect GDBserver. */
53 if (setpgid (0, 0) < 0)
54 trace_start_error_with_name (("setpgid"));
55
56 if (ptrace (PT_TRACE_ME, 0, nullptr, 0) < 0)
57 trace_start_error_with_name (("ptrace"));
58
59 /* If GDBserver is connected to gdb via stdio, redirect the inferior's
60 stdout to stderr so that inferior i/o doesn't corrupt the connection.
61 Also, redirect stdin to /dev/null. */
62 if (remote_connection_is_stdio ())
63 {
64 if (close (0) < 0)
65 trace_start_error_with_name (("close"));
66 if (open ("/dev/null", O_RDONLY) < 0)
67 trace_start_error_with_name (("open"));
68 if (dup2 (2, 1) < 0)
69 trace_start_error_with_name (("dup2"));
70 if (write (2, "stdin/stdout redirected\n",
71 sizeof ("stdin/stdout redirected\n") - 1) < 0)
72 {
73 /* Errors ignored. */
74 }
75 }
76}
77
78/* Implement the create_inferior method of the target_ops vector. */
79
80int
81netbsd_process_target::create_inferior (const char *program,
82 const std::vector<char *> &program_args)
83{
84 std::string str_program_args = construct_inferior_arguments (program_args);
85
86 pid_t pid = fork_inferior (program, str_program_args.c_str (),
87 get_environ ()->envp (), netbsd_ptrace_fun,
88 nullptr, nullptr, nullptr, nullptr);
89
b07993f6 90 add_process (pid, 0);
62ba5048
KR
91
92 post_fork_inferior (pid, program);
93
94 return pid;
95}
96
97/* Implement the post_create_inferior target_ops method. */
98
99void
100netbsd_process_target::post_create_inferior ()
101{
102 pid_t pid = current_process ()->pid;
103 netbsd_nat::enable_proc_events (pid);
15397b0e
KR
104
105 low_arch_setup ();
62ba5048
KR
106}
107
108/* Implement the attach target_ops method. */
109
110int
111netbsd_process_target::attach (unsigned long pid)
112{
113 /* Unimplemented. */
114 return -1;
115}
116
117/* Returns true if GDB is interested in any child syscalls. */
118
119static bool
120gdb_catching_syscalls_p (pid_t pid)
121{
122 struct process_info *proc = find_process_pid (pid);
123 return !proc->syscalls_to_catch.empty ();
124}
125
126/* Implement the resume target_ops method. */
127
128void
129netbsd_process_target::resume (struct thread_resume *resume_info, size_t n)
130{
131 ptid_t resume_ptid = resume_info[0].thread;
132 const int signal = resume_info[0].sig;
133 const bool step = resume_info[0].kind == resume_step;
134
135 if (resume_ptid == minus_one_ptid)
136 resume_ptid = ptid_of (current_thread);
137
138 const pid_t pid = resume_ptid.pid ();
139 const lwpid_t lwp = resume_ptid.lwp ();
140 regcache_invalidate_pid (pid);
141
142 auto fn
143 = [&] (ptid_t ptid)
144 {
145 if (step)
146 {
147 if (ptid.lwp () == lwp || n != 1)
148 {
149 if (ptrace (PT_SETSTEP, pid, NULL, ptid.lwp ()) == -1)
150 perror_with_name (("ptrace"));
151 if (ptrace (PT_RESUME, pid, NULL, ptid.lwp ()) == -1)
152 perror_with_name (("ptrace"));
153 }
154 else
155 {
156 if (ptrace (PT_CLEARSTEP, pid, NULL, ptid.lwp ()) == -1)
157 perror_with_name (("ptrace"));
158 if (ptrace (PT_SUSPEND, pid, NULL, ptid.lwp ()) == -1)
159 perror_with_name (("ptrace"));
160 }
161 }
162 else
163 {
164 if (ptrace (PT_CLEARSTEP, pid, NULL, ptid.lwp ()) == -1)
165 perror_with_name (("ptrace"));
166 if (ptrace (PT_RESUME, pid, NULL, ptid.lwp ()) == -1)
167 perror_with_name (("ptrace"));
168 }
169 };
170
171 netbsd_nat::for_each_thread (pid, fn);
172
173 int request = gdb_catching_syscalls_p (pid) ? PT_CONTINUE : PT_SYSCALL;
174
175 errno = 0;
176 ptrace (request, pid, (void *)1, signal);
177 if (errno)
178 perror_with_name (("ptrace"));
179}
180
181/* Returns true if GDB is interested in the reported SYSNO syscall. */
182
183static bool
184netbsd_catch_this_syscall (int sysno)
185{
186 struct process_info *proc = current_process ();
187
188 if (proc->syscalls_to_catch.empty ())
189 return false;
190
191 if (proc->syscalls_to_catch[0] == ANY_SYSCALL)
192 return true;
193
194 for (int iter : proc->syscalls_to_catch)
195 if (iter == sysno)
196 return true;
197
198 return false;
199}
200
201/* Helper function for child_wait and the derivatives of child_wait.
202 HOSTSTATUS is the waitstatus from wait() or the equivalent; store our
203 translation of that in OURSTATUS. */
204
205static void
206netbsd_store_waitstatus (struct target_waitstatus *ourstatus, int hoststatus)
207{
208 if (WIFEXITED (hoststatus))
183be222 209 ourstatus->set_exited (WEXITSTATUS (hoststatus));
62ba5048 210 else if (!WIFSTOPPED (hoststatus))
183be222 211 ourstatus->set_signalled (gdb_signal_from_host (WTERMSIG (hoststatus)));
62ba5048 212 else
183be222 213 ourstatus->set_stopped (gdb_signal_from_host (WSTOPSIG (hoststatus)));
62ba5048
KR
214}
215
216/* Implement a safe wrapper around waitpid(). */
217
218static pid_t
b60cea74
TT
219netbsd_waitpid (ptid_t ptid, struct target_waitstatus *ourstatus,
220 target_wait_flags target_options)
62ba5048
KR
221{
222 int status;
b60cea74 223 int options = (target_options & TARGET_WNOHANG) ? WNOHANG : 0;
62ba5048
KR
224
225 pid_t pid
d744f0f9 226 = gdb::handle_eintr (-1, ::waitpid, ptid.pid (), &status, options);
62ba5048
KR
227
228 if (pid == -1)
229 perror_with_name (_("Child process unexpectedly missing"));
230
231 netbsd_store_waitstatus (ourstatus, status);
232 return pid;
233}
234
235
236/* Implement the wait target_ops method.
237
238 Wait for the child specified by PTID to do something. Return the
239 process ID of the child, or MINUS_ONE_PTID in case of error; store
240 the status in *OURSTATUS. */
241
242static ptid_t
243netbsd_wait (ptid_t ptid, struct target_waitstatus *ourstatus,
b60cea74 244 target_wait_flags target_options)
62ba5048
KR
245{
246 pid_t pid = netbsd_waitpid (ptid, ourstatus, target_options);
247 ptid_t wptid = ptid_t (pid);
248
249 if (pid == 0)
250 {
251 gdb_assert (target_options & TARGET_WNOHANG);
183be222 252 ourstatus->set_ignore ();
62ba5048
KR
253 return null_ptid;
254 }
255
256 gdb_assert (pid != -1);
257
258 /* If the child stopped, keep investigating its status. */
183be222 259 if (ourstatus->kind () != TARGET_WAITKIND_STOPPED)
62ba5048
KR
260 return wptid;
261
262 /* Extract the event and thread that received a signal. */
263 ptrace_siginfo_t psi;
264 if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) == -1)
265 perror_with_name (("ptrace"));
266
267 /* Pick child's siginfo_t. */
268 siginfo_t *si = &psi.psi_siginfo;
269
270 lwpid_t lwp = psi.psi_lwpid;
271
272 int signo = si->si_signo;
273 const int code = si->si_code;
274
275 /* Construct PTID with a specified thread that received the event.
276 If a signal was targeted to the whole process, lwp is 0. */
277 wptid = ptid_t (pid, lwp, 0);
278
279 /* Bail out on non-debugger oriented signals. */
280 if (signo != SIGTRAP)
281 return wptid;
282
283 /* Stop examining non-debugger oriented SIGTRAP codes. */
284 if (code <= SI_USER || code == SI_NOINFO)
285 return wptid;
286
287 /* Process state for threading events. */
288 ptrace_state_t pst = {};
289 if (code == TRAP_LWP)
290 if (ptrace (PT_GET_PROCESS_STATE, pid, &pst, sizeof (pst)) == -1)
291 perror_with_name (("ptrace"));
292
293 if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_EXIT)
294 {
295 /* If GDB attaches to a multi-threaded process, exiting
296 threads might be skipped during post_attach that
297 have not yet reported their PTRACE_LWP_EXIT event.
298 Ignore exited events for an unknown LWP. */
299 thread_info *thr = find_thread_ptid (wptid);
300 if (thr == nullptr)
183be222 301 ourstatus->set_spurious ();
62ba5048
KR
302 else
303 {
62ba5048 304 /* NetBSD does not store an LWP exit status. */
183be222 305 ourstatus->set_thread_exited (0);
62ba5048
KR
306
307 remove_thread (thr);
308 }
309 return wptid;
310 }
311
312 if (find_thread_ptid (ptid_t (pid)))
24583e45 313 switch_to_thread (find_thread_ptid (wptid));
62ba5048
KR
314
315 if (code == TRAP_LWP && pst.pe_report_event == PTRACE_LWP_CREATE)
316 {
317 /* If GDB attaches to a multi-threaded process, newborn
318 threads might be added by nbsd_add_threads that have
319 not yet reported their PTRACE_LWP_CREATE event. Ignore
320 born events for an already-known LWP. */
321 if (find_thread_ptid (wptid))
183be222 322 ourstatus->set_spurious ();
62ba5048
KR
323 else
324 {
325 add_thread (wptid, NULL);
183be222 326 ourstatus->set_thread_created ();
62ba5048
KR
327 }
328 return wptid;
329 }
330
331 if (code == TRAP_EXEC)
332 {
183be222
SM
333 ourstatus->set_execd
334 (make_unique_xstrdup (netbsd_nat::pid_to_exec_file (pid)));
62ba5048
KR
335 return wptid;
336 }
337
338 if (code == TRAP_TRACE)
339 return wptid;
340
341 if (code == TRAP_SCE || code == TRAP_SCX)
342 {
343 int sysnum = si->si_sysnum;
344
345 if (!netbsd_catch_this_syscall(sysnum))
346 {
347 /* If the core isn't interested in this event, ignore it. */
183be222 348 ourstatus->set_spurious ();
62ba5048
KR
349 return wptid;
350 }
351
183be222
SM
352 if (code == TRAP_SCE)
353 ourstatus->set_syscall_entry (sysnum);
354 else
355 ourstatus->set_syscall_return (sysnum);
356
62ba5048
KR
357 return wptid;
358 }
359
360 if (code == TRAP_BRKPT)
361 {
362#ifdef PTRACE_BREAKPOINT_ADJ
363 CORE_ADDR pc;
364 struct reg r;
365 ptrace (PT_GETREGS, pid, &r, psi.psi_lwpid);
366 pc = PTRACE_REG_PC (&r);
367 PTRACE_REG_SET_PC (&r, pc - PTRACE_BREAKPOINT_ADJ);
368 ptrace (PT_SETREGS, pid, &r, psi.psi_lwpid);
369#endif
370 return wptid;
371 }
372
373 /* Unclassified SIGTRAP event. */
183be222 374 ourstatus->set_spurious ();
62ba5048
KR
375 return wptid;
376}
377
378/* Implement the wait target_ops method. */
379
380ptid_t
381netbsd_process_target::wait (ptid_t ptid, struct target_waitstatus *ourstatus,
b60cea74 382 target_wait_flags target_options)
62ba5048
KR
383{
384 while (true)
385 {
386 ptid_t wptid = netbsd_wait (ptid, ourstatus, target_options);
387
388 /* Register thread in the gdbcore if a thread was not reported earlier.
389 This is required after ::create_inferior, when the gdbcore does not
390 know about the first internal thread.
391 This may also happen on attach, when an event is registered on a thread
392 that was not fully initialized during the attach stage. */
393 if (wptid.lwp () != 0 && !find_thread_ptid (wptid)
183be222 394 && ourstatus->kind () != TARGET_WAITKIND_THREAD_EXITED)
62ba5048
KR
395 add_thread (wptid, nullptr);
396
183be222 397 switch (ourstatus->kind ())
62ba5048
KR
398 {
399 case TARGET_WAITKIND_EXITED:
400 case TARGET_WAITKIND_STOPPED:
401 case TARGET_WAITKIND_SIGNALLED:
402 case TARGET_WAITKIND_FORKED:
403 case TARGET_WAITKIND_VFORKED:
404 case TARGET_WAITKIND_EXECD:
405 case TARGET_WAITKIND_VFORK_DONE:
406 case TARGET_WAITKIND_SYSCALL_ENTRY:
407 case TARGET_WAITKIND_SYSCALL_RETURN:
408 /* Pass the result to the generic code. */
409 return wptid;
410 case TARGET_WAITKIND_THREAD_CREATED:
411 case TARGET_WAITKIND_THREAD_EXITED:
412 /* The core needlessly stops on these events. */
413 /* FALLTHROUGH */
414 case TARGET_WAITKIND_SPURIOUS:
415 /* Spurious events are unhandled by the gdbserver core. */
416 if (ptrace (PT_CONTINUE, current_process ()->pid, (void *) 1, 0)
417 == -1)
418 perror_with_name (("ptrace"));
419 break;
420 default:
421 error (("Unknown stopped status"));
422 }
423 }
424}
425
426/* Implement the kill target_ops method. */
427
428int
429netbsd_process_target::kill (process_info *process)
430{
431 pid_t pid = process->pid;
432 if (ptrace (PT_KILL, pid, nullptr, 0) == -1)
433 return -1;
434
435 int status;
d744f0f9 436 if (gdb::handle_eintr (-1, ::waitpid, pid, &status, 0) == -1)
62ba5048
KR
437 return -1;
438 mourn (process);
439 return 0;
440}
441
442/* Implement the detach target_ops method. */
443
444int
445netbsd_process_target::detach (process_info *process)
446{
447 pid_t pid = process->pid;
448
449 ptrace (PT_DETACH, pid, (void *) 1, 0);
450 mourn (process);
451 return 0;
452}
453
454/* Implement the mourn target_ops method. */
455
456void
457netbsd_process_target::mourn (struct process_info *proc)
458{
459 for_each_thread (proc->pid, remove_thread);
460
461 remove_process (proc);
462}
463
464/* Implement the join target_ops method. */
465
466void
467netbsd_process_target::join (int pid)
468{
469 /* The PT_DETACH is sufficient to detach from the process.
470 So no need to do anything extra. */
471}
472
473/* Implement the thread_alive target_ops method. */
474
475bool
476netbsd_process_target::thread_alive (ptid_t ptid)
477{
478 return netbsd_nat::thread_alive (ptid);
479}
480
481/* Implement the fetch_registers target_ops method. */
482
483void
484netbsd_process_target::fetch_registers (struct regcache *regcache, int regno)
485{
15397b0e 486 const netbsd_regset_info *regset = get_regs_info ();
62ba5048
KR
487 ptid_t inferior_ptid = ptid_of (current_thread);
488
489 while (regset->size >= 0)
490 {
491 std::vector<char> buf;
492 buf.resize (regset->size);
493 int res = ptrace (regset->get_request, inferior_ptid.pid (), buf.data (),
494 inferior_ptid.lwp ());
495 if (res == -1)
496 perror_with_name (("ptrace"));
497 regset->store_function (regcache, buf.data ());
498 regset++;
499 }
500}
501
502/* Implement the store_registers target_ops method. */
503
504void
505netbsd_process_target::store_registers (struct regcache *regcache, int regno)
506{
15397b0e 507 const netbsd_regset_info *regset = get_regs_info ();
62ba5048
KR
508 ptid_t inferior_ptid = ptid_of (current_thread);
509
510 while (regset->size >= 0)
511 {
512 std::vector<char> buf;
513 buf.resize (regset->size);
514 int res = ptrace (regset->get_request, inferior_ptid.pid (), buf.data (),
515 inferior_ptid.lwp ());
516 if (res == -1)
517 perror_with_name (("ptrace"));
518
519 /* Then overlay our cached registers on that. */
520 regset->fill_function (regcache, buf.data ());
521 /* Only now do we write the register set. */
522 res = ptrace (regset->set_request, inferior_ptid.pid (), buf. data (),
523 inferior_ptid.lwp ());
524 if (res == -1)
525 perror_with_name (("ptrace"));
526 regset++;
527 }
528}
529
530/* Implement the read_memory target_ops method. */
531
532int
533netbsd_process_target::read_memory (CORE_ADDR memaddr, unsigned char *myaddr,
534 int size)
535{
62ba5048 536 pid_t pid = current_process ()->pid;
91e5e8db 537 return netbsd_nat::read_memory (pid, myaddr, memaddr, size, nullptr);
62ba5048
KR
538}
539
540/* Implement the write_memory target_ops method. */
541
542int
543netbsd_process_target::write_memory (CORE_ADDR memaddr,
544 const unsigned char *myaddr, int size)
545{
62ba5048 546 pid_t pid = current_process ()->pid;
91e5e8db 547 return netbsd_nat::write_memory (pid, myaddr, memaddr, size, nullptr);
62ba5048
KR
548}
549
550/* Implement the request_interrupt target_ops method. */
551
552void
553netbsd_process_target::request_interrupt ()
554{
555 ptid_t inferior_ptid = ptid_of (get_first_thread ());
556
2be01f63 557 ::kill (inferior_ptid.pid (), SIGINT);
62ba5048
KR
558}
559
560/* Read the AUX Vector for the specified PID, wrapping the ptrace(2) call
561 with the PIOD_READ_AUXV operation and using the PT_IO standard input
562 and output arguments. */
563
564static size_t
565netbsd_read_auxv(pid_t pid, void *offs, void *addr, size_t len)
566{
567 struct ptrace_io_desc pio;
568
569 pio.piod_op = PIOD_READ_AUXV;
570 pio.piod_offs = offs;
571 pio.piod_addr = addr;
572 pio.piod_len = len;
573
574 if (ptrace (PT_IO, pid, &pio, 0) == -1)
575 perror_with_name (("ptrace"));
576
577 return pio.piod_len;
578}
579
580/* Copy LEN bytes from inferior's auxiliary vector starting at OFFSET
581 to debugger memory starting at MYADDR. */
582
583int
43e5fbd8 584netbsd_process_target::read_auxv (int pid, CORE_ADDR offset,
62ba5048
KR
585 unsigned char *myaddr, unsigned int len)
586{
62ba5048
KR
587 return netbsd_read_auxv (pid, (void *) (intptr_t) offset, myaddr, len);
588}
589
590bool
591netbsd_process_target::supports_z_point_type (char z_type)
592{
593 switch (z_type)
594 {
595 case Z_PACKET_SW_BP:
596 return true;
597 case Z_PACKET_HW_BP:
598 case Z_PACKET_WRITE_WP:
599 case Z_PACKET_READ_WP:
600 case Z_PACKET_ACCESS_WP:
601 default:
602 return false; /* Not supported. */
603 }
604}
605
606/* Insert {break/watch}point at address ADDR. SIZE is not used. */
607
608int
609netbsd_process_target::insert_point (enum raw_bkpt_type type, CORE_ADDR addr,
610 int size, struct raw_breakpoint *bp)
611{
612 switch (type)
613 {
614 case raw_bkpt_type_sw:
615 return insert_memory_breakpoint (bp);
616 case raw_bkpt_type_hw:
617 case raw_bkpt_type_write_wp:
618 case raw_bkpt_type_read_wp:
619 case raw_bkpt_type_access_wp:
620 default:
621 return 1; /* Not supported. */
622 }
623}
624
625/* Remove {break/watch}point at address ADDR. SIZE is not used. */
626
627int
628netbsd_process_target::remove_point (enum raw_bkpt_type type, CORE_ADDR addr,
629 int size, struct raw_breakpoint *bp)
630{
631 switch (type)
632 {
633 case raw_bkpt_type_sw:
634 return remove_memory_breakpoint (bp);
635 case raw_bkpt_type_hw:
636 case raw_bkpt_type_write_wp:
637 case raw_bkpt_type_read_wp:
638 case raw_bkpt_type_access_wp:
639 default:
640 return 1; /* Not supported. */
641 }
642}
643
644/* Implement the stopped_by_sw_breakpoint target_ops method. */
645
646bool
647netbsd_process_target::stopped_by_sw_breakpoint ()
648{
649 ptrace_siginfo_t psi;
650 pid_t pid = current_process ()->pid;
651
652 if (ptrace (PT_GET_SIGINFO, pid, &psi, sizeof (psi)) == -1)
653 perror_with_name (("ptrace"));
654
655 return psi.psi_siginfo.si_signo == SIGTRAP &&
656 psi.psi_siginfo.si_code == TRAP_BRKPT;
657}
658
659/* Implement the supports_stopped_by_sw_breakpoint target_ops method. */
660
661bool
662netbsd_process_target::supports_stopped_by_sw_breakpoint ()
663{
664 return true;
665}
666
667/* Implement the supports_qxfer_siginfo target_ops method. */
668
669bool
670netbsd_process_target::supports_qxfer_siginfo ()
671{
672 return true;
673}
674
675/* Implement the qxfer_siginfo target_ops method. */
676
677int
678netbsd_process_target::qxfer_siginfo (const char *annex, unsigned char *readbuf,
679 unsigned const char *writebuf,
680 CORE_ADDR offset, int len)
681{
682 if (current_thread == nullptr)
683 return -1;
684
685 pid_t pid = current_process ()->pid;
686
687 return netbsd_nat::qxfer_siginfo(pid, annex, readbuf, writebuf, offset, len);
688}
689
690/* Implement the supports_non_stop target_ops method. */
691
692bool
693netbsd_process_target::supports_non_stop ()
694{
695 return false;
696}
697
698/* Implement the supports_multi_process target_ops method. */
699
700bool
701netbsd_process_target::supports_multi_process ()
702{
703 return true;
704}
705
706/* Check if fork events are supported. */
707
708bool
709netbsd_process_target::supports_fork_events ()
710{
711 return false;
712}
713
714/* Check if vfork events are supported. */
715
716bool
717netbsd_process_target::supports_vfork_events ()
718{
719 return false;
720}
721
722/* Check if exec events are supported. */
723
724bool
725netbsd_process_target::supports_exec_events ()
726{
727 return true;
728}
729
730/* Implement the supports_disable_randomization target_ops method. */
731
732bool
733netbsd_process_target::supports_disable_randomization ()
734{
735 return false;
736}
737
738/* Extract &phdr and num_phdr in the inferior. Return 0 on success. */
739
740template <typename T>
741int get_phdr_phnum_from_proc_auxv (const pid_t pid,
742 CORE_ADDR *phdr_memaddr, int *num_phdr)
743{
744 typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
745 Aux64Info, Aux32Info>::type auxv_type;
746 const size_t auxv_size = sizeof (auxv_type);
747 const size_t auxv_buf_size = 128 * sizeof (auxv_type);
748
749 std::vector<char> auxv_buf;
750 auxv_buf.resize (auxv_buf_size);
751
752 netbsd_read_auxv (pid, nullptr, auxv_buf.data (), auxv_buf_size);
753
754 *phdr_memaddr = 0;
755 *num_phdr = 0;
756
757 for (char *buf = auxv_buf.data ();
758 buf < (auxv_buf.data () + auxv_buf_size);
759 buf += auxv_size)
760 {
761 auxv_type *const aux = (auxv_type *) buf;
762
763 switch (aux->a_type)
764 {
765 case AT_PHDR:
766 *phdr_memaddr = aux->a_v;
767 break;
768 case AT_PHNUM:
769 *num_phdr = aux->a_v;
770 break;
771 }
772
773 if (*phdr_memaddr != 0 && *num_phdr != 0)
774 break;
775 }
776
777 if (*phdr_memaddr == 0 || *num_phdr == 0)
778 {
779 warning ("Unexpected missing AT_PHDR and/or AT_PHNUM: "
780 "phdr_memaddr = %s, phdr_num = %d",
781 core_addr_to_string (*phdr_memaddr), *num_phdr);
782 return 2;
783 }
784
785 return 0;
786}
787
788/* Return &_DYNAMIC (via PT_DYNAMIC) in the inferior, or 0 if not present. */
789
790template <typename T>
791static CORE_ADDR
48491055 792get_dynamic (const pid_t pid)
62ba5048
KR
793{
794 typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
795 Elf64_Phdr, Elf32_Phdr>::type phdr_type;
796 const int phdr_size = sizeof (phdr_type);
797
798 CORE_ADDR phdr_memaddr;
799 int num_phdr;
800 if (get_phdr_phnum_from_proc_auxv<T> (pid, &phdr_memaddr, &num_phdr))
801 return 0;
802
803 std::vector<unsigned char> phdr_buf;
804 phdr_buf.resize (num_phdr * phdr_size);
805
48491055
KR
806 if (netbsd_nat::read_memory (pid, phdr_buf.data (), phdr_memaddr,
807 phdr_buf.size (), nullptr))
62ba5048
KR
808 return 0;
809
810 /* Compute relocation: it is expected to be 0 for "regular" executables,
811 non-zero for PIE ones. */
812 CORE_ADDR relocation = -1;
813 for (int i = 0; relocation == -1 && i < num_phdr; i++)
814 {
2be01f63 815 phdr_type *const p = (phdr_type *) (phdr_buf.data () + i * phdr_size);
62ba5048
KR
816
817 if (p->p_type == PT_PHDR)
818 relocation = phdr_memaddr - p->p_vaddr;
819 }
820
821 if (relocation == -1)
822 {
823 /* PT_PHDR is optional, but necessary for PIE in general. Fortunately
824 any real world executables, including PIE executables, have always
825 PT_PHDR present. PT_PHDR is not present in some shared libraries or
826 in fpc (Free Pascal 2.4) binaries but neither of those have a need for
827 or present DT_DEBUG anyway (fpc binaries are statically linked).
828
829 Therefore if there exists DT_DEBUG there is always also PT_PHDR.
830
831 GDB could find RELOCATION also from AT_ENTRY - e_entry. */
832
833 return 0;
834 }
835
836 for (int i = 0; i < num_phdr; i++)
837 {
838 phdr_type *const p = (phdr_type *) (phdr_buf.data () + i * phdr_size);
839
840 if (p->p_type == PT_DYNAMIC)
841 return p->p_vaddr + relocation;
842 }
843
844 return 0;
845}
846
847/* Return &_r_debug in the inferior, or -1 if not present. Return value
848 can be 0 if the inferior does not yet have the library list initialized.
849 We look for DT_MIPS_RLD_MAP first. MIPS executables use this instead of
850 DT_DEBUG, although they sometimes contain an unused DT_DEBUG entry too. */
851
852template <typename T>
853static CORE_ADDR
48491055 854get_r_debug (const pid_t pid)
62ba5048
KR
855{
856 typedef typename std::conditional<sizeof(T) == sizeof(int64_t),
857 Elf64_Dyn, Elf32_Dyn>::type dyn_type;
858 const int dyn_size = sizeof (dyn_type);
859 unsigned char buf[sizeof (dyn_type)]; /* The larger of the two. */
860 CORE_ADDR map = -1;
861
48491055 862 CORE_ADDR dynamic_memaddr = get_dynamic<T> (pid);
62ba5048
KR
863 if (dynamic_memaddr == 0)
864 return map;
865
48491055
KR
866 while (netbsd_nat::read_memory (pid, buf, dynamic_memaddr, dyn_size, nullptr)
867 == 0)
62ba5048
KR
868 {
869 dyn_type *const dyn = (dyn_type *) buf;
870#if defined DT_MIPS_RLD_MAP
871 union
872 {
873 T map;
874 unsigned char buf[sizeof (T)];
875 }
876 rld_map;
877
878 if (dyn->d_tag == DT_MIPS_RLD_MAP)
879 {
48491055
KR
880 if (netbsd_nat::read_memory (pid, rld_map.buf, dyn->d_un.d_val,
881 sizeof (rld_map.buf), nullptr) == 0)
62ba5048
KR
882 return rld_map.map;
883 else
884 break;
885 }
886#endif /* DT_MIPS_RLD_MAP */
887
888 if (dyn->d_tag == DT_DEBUG && map == -1)
889 map = dyn->d_un.d_val;
890
891 if (dyn->d_tag == DT_NULL)
892 break;
893
894 dynamic_memaddr += dyn_size;
895 }
896
897 return map;
898}
899
900/* Read one pointer from MEMADDR in the inferior. */
901
902static int
48491055 903read_one_ptr (const pid_t pid, CORE_ADDR memaddr, CORE_ADDR *ptr, int ptr_size)
62ba5048
KR
904{
905 /* Go through a union so this works on either big or little endian
906 hosts, when the inferior's pointer size is smaller than the size
907 of CORE_ADDR. It is assumed the inferior's endianness is the
908 same of the superior's. */
909
910 union
911 {
912 CORE_ADDR core_addr;
913 unsigned int ui;
914 unsigned char uc;
915 } addr;
916
48491055 917 int ret = netbsd_nat::read_memory (pid, &addr.uc, memaddr, ptr_size, nullptr);
62ba5048
KR
918 if (ret == 0)
919 {
920 if (ptr_size == sizeof (CORE_ADDR))
921 *ptr = addr.core_addr;
922 else if (ptr_size == sizeof (unsigned int))
923 *ptr = addr.ui;
924 else
925 gdb_assert_not_reached ("unhandled pointer size");
926 }
927 return ret;
928}
929
930/* Construct qXfer:libraries-svr4:read reply. */
931
932template <typename T>
933int
48491055 934netbsd_qxfer_libraries_svr4 (const pid_t pid, const char *annex,
62ba5048
KR
935 unsigned char *readbuf,
936 unsigned const char *writebuf,
937 CORE_ADDR offset, int len)
938{
939 struct link_map_offsets
940 {
941 /* Offset and size of r_debug.r_version. */
942 int r_version_offset;
943
944 /* Offset and size of r_debug.r_map. */
945 int r_map_offset;
946
947 /* Offset to l_addr field in struct link_map. */
948 int l_addr_offset;
949
950 /* Offset to l_name field in struct link_map. */
951 int l_name_offset;
952
953 /* Offset to l_ld field in struct link_map. */
954 int l_ld_offset;
955
956 /* Offset to l_next field in struct link_map. */
957 int l_next_offset;
958
959 /* Offset to l_prev field in struct link_map. */
960 int l_prev_offset;
961 };
962
963 static const struct link_map_offsets lmo_32bit_offsets =
964 {
965 0, /* r_version offset. */
966 4, /* r_debug.r_map offset. */
967 0, /* l_addr offset in link_map. */
968 4, /* l_name offset in link_map. */
969 8, /* l_ld offset in link_map. */
970 12, /* l_next offset in link_map. */
971 16 /* l_prev offset in link_map. */
972 };
973
974 static const struct link_map_offsets lmo_64bit_offsets =
975 {
976 0, /* r_version offset. */
977 8, /* r_debug.r_map offset. */
978 0, /* l_addr offset in link_map. */
979 8, /* l_name offset in link_map. */
980 16, /* l_ld offset in link_map. */
981 24, /* l_next offset in link_map. */
982 32 /* l_prev offset in link_map. */
983 };
984
985 CORE_ADDR lm_addr = 0, lm_prev = 0;
986 CORE_ADDR l_name, l_addr, l_ld, l_next, l_prev;
987 int header_done = 0;
988
989 const struct link_map_offsets *lmo
990 = ((sizeof (T) == sizeof (int64_t))
991 ? &lmo_64bit_offsets : &lmo_32bit_offsets);
992 int ptr_size = sizeof (T);
993
994 while (annex[0] != '\0')
995 {
996 const char *sep = strchr (annex, '=');
997 if (sep == nullptr)
998 break;
999
1000 int name_len = sep - annex;
1001 CORE_ADDR *addrp;
1002 if (name_len == 5 && startswith (annex, "start"))
1003 addrp = &lm_addr;
1004 else if (name_len == 4 && startswith (annex, "prev"))
1005 addrp = &lm_prev;
1006 else
1007 {
1008 annex = strchr (sep, ';');
1009 if (annex == nullptr)
1010 break;
1011 annex++;
1012 continue;
1013 }
1014
1015 annex = decode_address_to_semicolon (addrp, sep + 1);
1016 }
1017
1018 if (lm_addr == 0)
1019 {
48491055 1020 CORE_ADDR r_debug = get_r_debug<T> (pid);
62ba5048
KR
1021
1022 /* We failed to find DT_DEBUG. Such situation will not change
1023 for this inferior - do not retry it. Report it to GDB as
1024 E01, see for the reasons at the GDB solib-svr4.c side. */
1025 if (r_debug == (CORE_ADDR) -1)
1026 return -1;
1027
1028 if (r_debug != 0)
1029 {
1030 CORE_ADDR map_offset = r_debug + lmo->r_map_offset;
48491055 1031 if (read_one_ptr (pid, map_offset, &lm_addr, ptr_size) != 0)
62ba5048
KR
1032 warning ("unable to read r_map from %s",
1033 core_addr_to_string (map_offset));
1034 }
1035 }
1036
1037 std::string document = "<library-list-svr4 version=\"1.0\"";
1038
1039 while (lm_addr
48491055 1040 && read_one_ptr (pid, lm_addr + lmo->l_name_offset,
62ba5048 1041 &l_name, ptr_size) == 0
48491055 1042 && read_one_ptr (pid, lm_addr + lmo->l_addr_offset,
62ba5048 1043 &l_addr, ptr_size) == 0
48491055 1044 && read_one_ptr (pid, lm_addr + lmo->l_ld_offset,
62ba5048 1045 &l_ld, ptr_size) == 0
48491055 1046 && read_one_ptr (pid, lm_addr + lmo->l_prev_offset,
62ba5048 1047 &l_prev, ptr_size) == 0
48491055 1048 && read_one_ptr (pid, lm_addr + lmo->l_next_offset,
62ba5048
KR
1049 &l_next, ptr_size) == 0)
1050 {
1051 if (lm_prev != l_prev)
1052 {
1053 warning ("Corrupted shared library list: 0x%lx != 0x%lx",
1054 (long) lm_prev, (long) l_prev);
1055 break;
1056 }
1057
1058 /* Ignore the first entry even if it has valid name as the first entry
1059 corresponds to the main executable. The first entry should not be
1060 skipped if the dynamic loader was loaded late by a static executable
1061 (see solib-svr4.c parameter ignore_first). But in such case the main
1062 executable does not have PT_DYNAMIC present and this function already
1063 exited above due to failed get_r_debug. */
1064 if (lm_prev == 0)
1065 string_appendf (document, " main-lm=\"0x%lx\"",
1066 (unsigned long) lm_addr);
1067 else
1068 {
1069 unsigned char libname[PATH_MAX];
1070
1071 /* Not checking for error because reading may stop before
1072 we've got PATH_MAX worth of characters. */
1073 libname[0] = '\0';
48491055
KR
1074 netbsd_nat::read_memory (pid, libname, l_name, sizeof (libname) - 1,
1075 nullptr);
62ba5048
KR
1076 libname[sizeof (libname) - 1] = '\0';
1077 if (libname[0] != '\0')
1078 {
1079 if (!header_done)
1080 {
1081 /* Terminate `<library-list-svr4'. */
1082 document += '>';
1083 header_done = 1;
1084 }
1085
1086 string_appendf (document, "<library name=\"");
de75275f 1087 xml_escape_text_append (document, (char *) libname);
62ba5048
KR
1088 string_appendf (document, "\" lm=\"0x%lx\" "
1089 "l_addr=\"0x%lx\" l_ld=\"0x%lx\"/>",
1090 (unsigned long) lm_addr, (unsigned long) l_addr,
1091 (unsigned long) l_ld);
1092 }
1093 }
1094
1095 lm_prev = lm_addr;
1096 lm_addr = l_next;
1097 }
1098
1099 if (!header_done)
1100 {
1101 /* Empty list; terminate `<library-list-svr4'. */
1102 document += "/>";
1103 }
1104 else
1105 document += "</library-list-svr4>";
1106
1107 int document_len = document.length ();
1108 if (offset < document_len)
1109 document_len -= offset;
1110 else
1111 document_len = 0;
1112 if (len > document_len)
1113 len = document_len;
1114
1115 memcpy (readbuf, document.data () + offset, len);
1116
1117 return len;
1118}
1119
1120/* Return true if FILE is a 64-bit ELF file,
1121 false if the file is not a 64-bit ELF file,
1122 and error if the file is not accessible or doesn't exist. */
1123
1124static bool
1125elf_64_file_p (const char *file)
1126{
d744f0f9 1127 int fd = gdb::handle_eintr (-1, ::open, file, O_RDONLY);
62ba5048
KR
1128 if (fd < 0)
1129 perror_with_name (("open"));
1130
1131 Elf64_Ehdr header;
d744f0f9 1132 ssize_t ret = gdb::handle_eintr (-1, ::read, fd, &header, sizeof (header));
62ba5048
KR
1133 if (ret == -1)
1134 perror_with_name (("read"));
d744f0f9 1135 gdb::handle_eintr (-1, ::close, fd);
62ba5048
KR
1136 if (ret != sizeof (header))
1137 error ("Cannot read ELF file header: %s", file);
1138
1139 if (header.e_ident[EI_MAG0] != ELFMAG0
1140 || header.e_ident[EI_MAG1] != ELFMAG1
1141 || header.e_ident[EI_MAG2] != ELFMAG2
1142 || header.e_ident[EI_MAG3] != ELFMAG3)
1143 error ("Unrecognized ELF file header: %s", file);
1144
1145 return header.e_ident[EI_CLASS] == ELFCLASS64;
1146}
1147
1148/* Construct qXfer:libraries-svr4:read reply. */
1149
1150int
1151netbsd_process_target::qxfer_libraries_svr4 (const char *annex,
1152 unsigned char *readbuf,
1153 unsigned const char *writebuf,
1154 CORE_ADDR offset, int len)
1155{
1156 if (writebuf != nullptr)
1157 return -2;
1158 if (readbuf == nullptr)
1159 return -1;
1160
1161 struct process_info *proc = current_process ();
1162 pid_t pid = proc->pid;
1163 bool is_elf64 = elf_64_file_p (netbsd_nat::pid_to_exec_file (pid));
1164
1165 if (is_elf64)
48491055 1166 return netbsd_qxfer_libraries_svr4<int64_t> (pid, annex, readbuf,
62ba5048
KR
1167 writebuf, offset, len);
1168 else
48491055 1169 return netbsd_qxfer_libraries_svr4<int32_t> (pid, annex, readbuf,
62ba5048
KR
1170 writebuf, offset, len);
1171}
1172
1173/* Implement the supports_qxfer_libraries_svr4 target_ops method. */
1174
1175bool
1176netbsd_process_target::supports_qxfer_libraries_svr4 ()
1177{
1178 return true;
1179}
1180
1181/* Return the name of a file that can be opened to get the symbols for
1182 the child process identified by PID. */
1183
04977957 1184const char *
62ba5048
KR
1185netbsd_process_target::pid_to_exec_file (pid_t pid)
1186{
04977957 1187 return netbsd_nat::pid_to_exec_file (pid);
62ba5048
KR
1188}
1189
1190/* Implementation of the target_ops method "supports_pid_to_exec_file". */
1191
1192bool
1193netbsd_process_target::supports_pid_to_exec_file ()
1194{
1195 return true;
1196}
1197
1198/* Implementation of the target_ops method "supports_hardware_single_step". */
1199bool
1200netbsd_process_target::supports_hardware_single_step ()
1201{
1202 return true;
1203}
1204
1205/* Implementation of the target_ops method "sw_breakpoint_from_kind". */
1206
1207const gdb_byte *
1208netbsd_process_target::sw_breakpoint_from_kind (int kind, int *size)
1209{
1210 static gdb_byte brkpt[PTRACE_BREAKPOINT_SIZE] = {*PTRACE_BREAKPOINT};
1211
1212 *size = PTRACE_BREAKPOINT_SIZE;
1213
1214 return brkpt;
1215}
1216
1217/* Implement the thread_name target_ops method. */
1218
1219const char *
1220netbsd_process_target::thread_name (ptid_t ptid)
1221{
1222 return netbsd_nat::thread_name (ptid);
1223}
1224
1225/* Implement the supports_catch_syscall target_ops method. */
1226
1227bool
1228netbsd_process_target::supports_catch_syscall ()
1229{
1230 return true;
1231}
1232
1233/* Implement the supports_read_auxv target_ops method. */
1234
1235bool
1236netbsd_process_target::supports_read_auxv ()
1237{
1238 return true;
1239}
1240
62ba5048
KR
1241void
1242initialize_low ()
1243{
15397b0e 1244 set_target_ops (the_netbsd_target);
62ba5048 1245}