Make the linker return an error status if it fails to merge ARM binaries with
[deliverable/binutils-gdb.git] / gdb / i386gnu-nat.c
1 /* Low level interface to i386 running the GNU Hurd.
2
3 Copyright (C) 1992-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "floatformat.h"
23 #include "regcache.h"
24
25 #include <mach.h>
26 #include <mach_error.h>
27 #include <mach/message.h>
28 #include <mach/exception.h>
29
30 #include "i386-tdep.h"
31
32 #include "gnu-nat.h"
33 #include "i387-tdep.h"
34
35 #ifdef HAVE_SYS_PROCFS_H
36 # include <sys/procfs.h>
37 # include "gregset.h"
38 #endif
39
40 /* Offset to the thread_state_t location where REG is stored. */
41 #define REG_OFFSET(reg) offsetof (struct i386_thread_state, reg)
42
43 /* At REG_OFFSET[N] is the offset to the thread_state_t location where
44 the GDB register N is stored. */
45 static int reg_offset[] =
46 {
47 REG_OFFSET (eax), REG_OFFSET (ecx), REG_OFFSET (edx), REG_OFFSET (ebx),
48 REG_OFFSET (uesp), REG_OFFSET (ebp), REG_OFFSET (esi), REG_OFFSET (edi),
49 REG_OFFSET (eip), REG_OFFSET (efl), REG_OFFSET (cs), REG_OFFSET (ss),
50 REG_OFFSET (ds), REG_OFFSET (es), REG_OFFSET (fs), REG_OFFSET (gs)
51 };
52
53 /* Offset to the greg_t location where REG is stored. */
54 #define CREG_OFFSET(reg) (REG_##reg * 4)
55
56 /* At CREG_OFFSET[N] is the offset to the greg_t location where
57 the GDB register N is stored. */
58 static int creg_offset[] =
59 {
60 CREG_OFFSET (EAX), CREG_OFFSET (ECX), CREG_OFFSET (EDX), CREG_OFFSET (EBX),
61 CREG_OFFSET (UESP), CREG_OFFSET (EBP), CREG_OFFSET (ESI), CREG_OFFSET (EDI),
62 CREG_OFFSET (EIP), CREG_OFFSET (EFL), CREG_OFFSET (CS), CREG_OFFSET (SS),
63 CREG_OFFSET (DS), CREG_OFFSET (ES), CREG_OFFSET (FS), CREG_OFFSET (GS)
64 };
65
66 #define REG_ADDR(state, regnum) ((char *)(state) + reg_offset[regnum])
67 #define CREG_ADDR(state, regnum) ((const char *)(state) + creg_offset[regnum])
68
69 \f
70 /* Get the whole floating-point state of THREAD and record the values
71 of the corresponding (pseudo) registers. */
72
73 static void
74 fetch_fpregs (struct regcache *regcache, struct proc *thread)
75 {
76 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
77 struct i386_float_state state;
78 error_t err;
79
80 err = thread_get_state (thread->port, i386_FLOAT_STATE,
81 (thread_state_t) &state, &count);
82 if (err)
83 {
84 warning (_("Couldn't fetch floating-point state from %s"),
85 proc_string (thread));
86 return;
87 }
88
89 if (!state.initialized)
90 {
91 /* The floating-point state isn't initialized. */
92 i387_supply_fsave (regcache, -1, NULL);
93 }
94 else
95 {
96 /* Supply the floating-point registers. */
97 i387_supply_fsave (regcache, -1, state.hw_state);
98 }
99 }
100
101 #ifdef HAVE_SYS_PROCFS_H
102 /* These two calls are used by the core-regset.c code for
103 reading ELF core files. */
104 void
105 supply_gregset (struct regcache *regcache, const gdb_gregset_t *gregs)
106 {
107 int i;
108 for (i = 0; i < I386_NUM_GREGS; i++)
109 regcache_raw_supply (regcache, i, CREG_ADDR (gregs, i));
110 }
111
112 void
113 supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
114 {
115 i387_supply_fsave (regcache, -1, fpregs);
116 }
117 #endif
118
119 /* Fetch register REGNO, or all regs if REGNO is -1. */
120 static void
121 gnu_fetch_registers (struct target_ops *ops,
122 struct regcache *regcache, int regno)
123 {
124 struct proc *thread;
125
126 /* Make sure we know about new threads. */
127 inf_update_procs (gnu_current_inf);
128
129 thread = inf_tid_to_thread (gnu_current_inf,
130 ptid_get_lwp (inferior_ptid));
131 if (!thread)
132 error (_("Can't fetch registers from thread %s: No such thread"),
133 target_pid_to_str (inferior_ptid));
134
135 if (regno < I386_NUM_GREGS || regno == -1)
136 {
137 thread_state_t state;
138
139 /* This does the dirty work for us. */
140 state = proc_get_state (thread, 0);
141 if (!state)
142 {
143 warning (_("Couldn't fetch registers from %s"),
144 proc_string (thread));
145 return;
146 }
147
148 if (regno == -1)
149 {
150 int i;
151
152 proc_debug (thread, "fetching all register");
153
154 for (i = 0; i < I386_NUM_GREGS; i++)
155 regcache_raw_supply (regcache, i, REG_ADDR (state, i));
156 thread->fetched_regs = ~0;
157 }
158 else
159 {
160 proc_debug (thread, "fetching register %s",
161 gdbarch_register_name (get_regcache_arch (regcache),
162 regno));
163
164 regcache_raw_supply (regcache, regno,
165 REG_ADDR (state, regno));
166 thread->fetched_regs |= (1 << regno);
167 }
168 }
169
170 if (regno >= I386_NUM_GREGS || regno == -1)
171 {
172 proc_debug (thread, "fetching floating-point registers");
173
174 fetch_fpregs (regcache, thread);
175 }
176 }
177 \f
178
179 /* Store the whole floating-point state into THREAD using information
180 from the corresponding (pseudo) registers. */
181 static void
182 store_fpregs (const struct regcache *regcache, struct proc *thread, int regno)
183 {
184 mach_msg_type_number_t count = i386_FLOAT_STATE_COUNT;
185 struct i386_float_state state;
186 error_t err;
187
188 err = thread_get_state (thread->port, i386_FLOAT_STATE,
189 (thread_state_t) &state, &count);
190 if (err)
191 {
192 warning (_("Couldn't fetch floating-point state from %s"),
193 proc_string (thread));
194 return;
195 }
196
197 /* FIXME: kettenis/2001-07-15: Is this right? Should we somehow
198 take into account DEPRECATED_REGISTER_VALID like the old code did? */
199 i387_collect_fsave (regcache, regno, state.hw_state);
200
201 err = thread_set_state (thread->port, i386_FLOAT_STATE,
202 (thread_state_t) &state, i386_FLOAT_STATE_COUNT);
203 if (err)
204 {
205 warning (_("Couldn't store floating-point state into %s"),
206 proc_string (thread));
207 return;
208 }
209 }
210
211 /* Store at least register REGNO, or all regs if REGNO == -1. */
212 static void
213 gnu_store_registers (struct target_ops *ops,
214 struct regcache *regcache, int regno)
215 {
216 struct proc *thread;
217 struct gdbarch *gdbarch = get_regcache_arch (regcache);
218
219 /* Make sure we know about new threads. */
220 inf_update_procs (gnu_current_inf);
221
222 thread = inf_tid_to_thread (gnu_current_inf,
223 ptid_get_lwp (inferior_ptid));
224 if (!thread)
225 error (_("Couldn't store registers into thread %s: No such thread"),
226 target_pid_to_str (inferior_ptid));
227
228 if (regno < I386_NUM_GREGS || regno == -1)
229 {
230 thread_state_t state;
231 thread_state_data_t old_state;
232 int was_aborted = thread->aborted;
233 int was_valid = thread->state_valid;
234 int trace;
235
236 if (!was_aborted && was_valid)
237 memcpy (&old_state, &thread->state, sizeof (old_state));
238
239 state = proc_get_state (thread, 1);
240 if (!state)
241 {
242 warning (_("Couldn't store registers into %s"),
243 proc_string (thread));
244 return;
245 }
246
247 /* Save the T bit. We might try to restore the %eflags register
248 below, but changing the T bit would seriously confuse GDB. */
249 trace = ((struct i386_thread_state *)state)->efl & 0x100;
250
251 if (!was_aborted && was_valid)
252 /* See which registers have changed after aborting the thread. */
253 {
254 int check_regno;
255
256 for (check_regno = 0; check_regno < I386_NUM_GREGS; check_regno++)
257 if ((thread->fetched_regs & (1 << check_regno))
258 && memcpy (REG_ADDR (&old_state, check_regno),
259 REG_ADDR (state, check_regno),
260 register_size (gdbarch, check_regno)))
261 /* Register CHECK_REGNO has changed! Ack! */
262 {
263 warning (_("Register %s changed after the thread was aborted"),
264 gdbarch_register_name (gdbarch, check_regno));
265 if (regno >= 0 && regno != check_regno)
266 /* Update GDB's copy of the register. */
267 regcache_raw_supply (regcache, check_regno,
268 REG_ADDR (state, check_regno));
269 else
270 warning (_("... also writing this register! "
271 "Suspicious..."));
272 }
273 }
274
275 if (regno == -1)
276 {
277 int i;
278
279 proc_debug (thread, "storing all registers");
280
281 for (i = 0; i < I386_NUM_GREGS; i++)
282 if (REG_VALID == regcache_register_status (regcache, i))
283 regcache_raw_collect (regcache, i, REG_ADDR (state, i));
284 }
285 else
286 {
287 proc_debug (thread, "storing register %s",
288 gdbarch_register_name (gdbarch, regno));
289
290 gdb_assert (REG_VALID == regcache_register_status (regcache, regno));
291 regcache_raw_collect (regcache, regno, REG_ADDR (state, regno));
292 }
293
294 /* Restore the T bit. */
295 ((struct i386_thread_state *)state)->efl &= ~0x100;
296 ((struct i386_thread_state *)state)->efl |= trace;
297 }
298
299 if (regno >= I386_NUM_GREGS || regno == -1)
300 {
301 proc_debug (thread, "storing floating-point registers");
302
303 store_fpregs (regcache, thread, regno);
304 }
305 }
306
307 /* Provide a prototype to silence -Wmissing-prototypes. */
308 extern initialize_file_ftype _initialize_i386gnu_nat;
309
310 void
311 _initialize_i386gnu_nat (void)
312 {
313 struct target_ops *t;
314
315 /* Fill in the generic GNU/Hurd methods. */
316 t = gnu_target ();
317
318 t->to_fetch_registers = gnu_fetch_registers;
319 t->to_store_registers = gnu_store_registers;
320
321 /* Register the target. */
322 add_target (t);
323 }
This page took 0.036629 seconds and 5 git commands to generate.