aboutsummaryrefslogtreecommitdiff
path: root/gdb/i386-nbsd-tdep.c
blob: d39d1b4d4c36215d4ea6f82a89065257ffd0e10e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* Target-dependent code for NetBSD/i386.

   Copyright (C) 1988-2019 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "defs.h"
#include "arch-utils.h"
#include "frame.h"
#include "gdbcore.h"
#include "regcache.h"
#include "regset.h"
#include "osabi.h"
#include "symtab.h"
#include "trad-frame.h"
#include "tramp-frame.h"

#include "i386-tdep.h"
#include "i387-tdep.h"
#include "nbsd-tdep.h"
#include "solib-svr4.h"

/* From <machine/reg.h>.  */
static int i386nbsd_r_reg_offset[] =
{
  0 * 4,			/* %eax */
  1 * 4,			/* %ecx */
  2 * 4,			/* %edx */
  3 * 4,			/* %ebx */
  4 * 4,			/* %esp */
  5 * 4,			/* %ebp */
  6 * 4,			/* %esi */
  7 * 4,			/* %edi */
  8 * 4,			/* %eip */
  9 * 4,			/* %eflags */
  10 * 4,			/* %cs */
  11 * 4,			/* %ss */
  12 * 4,			/* %ds */
  13 * 4,			/* %es */
  14 * 4,			/* %fs */
  15 * 4			/* %gs */
};

/* From <machine/signal.h>.  */
int i386nbsd_sc_reg_offset[] =
{
  10 * 4,			/* %eax */
  9 * 4,			/* %ecx */
  8 * 4,			/* %edx */
  7 * 4,			/* %ebx */
  14 * 4,			/* %esp */
  6 * 4,			/* %ebp */
  5 * 4,			/* %esi */
  4 * 4,			/* %edi */
  11 * 4,			/* %eip */
  13 * 4,			/* %eflags */
  12 * 4,			/* %cs */
  15 * 4,			/* %ss */
  3 * 4,			/* %ds */
  2 * 4,			/* %es */
  1 * 4,			/* %fs */
  0 * 4				/* %gs */
};

/* From <machine/mcontext.h>.  */
int i386nbsd_mc_reg_offset[] =
{
  11 * 4,			/* %eax */
  10 * 4,			/* %ecx */
  9 * 4,			/* %edx */
  8 * 4,			/* %ebx */
  7 * 4,			/* %esp */
  6 * 4,			/* %ebp */
  5 * 4,			/* %esi */
  4 * 4,			/* %edi */
  14 * 4,			/* %eip */
  16 * 4,			/* %eflags */
  15 * 4,			/* %cs */
  18 * 4,			/* %ss */
  3 * 4,			/* %ds */
  2 * 4,			/* %es */
  1 * 4,			/* %fs */
  0 * 4				/* %gs */
};

static void i386nbsd_sigtramp_cache_init (const struct tramp_frame *,
					  struct frame_info *,
					  struct trad_frame_cache *,
					  CORE_ADDR);

static const struct tramp_frame i386nbsd_sigtramp_sc16 =
{
  SIGTRAMP_FRAME,
  1,
  {
   /* leal  0x10(%esp), %eax */
   { 0x8d, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x10, ULONGEST_MAX },

   /* pushl %eax */
   { 0x50, ULONGEST_MAX },

   /* pushl %eax */
   { 0x50, ULONGEST_MAX },

   /* movl  $0x127, %eax		# __sigreturn14 */
   { 0xb8, ULONGEST_MAX },
   { 0x27, ULONGEST_MAX },
   {0x01, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },

   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX},

   /* movl  $0x1, %eax		# exit */
   { 0xb8, ULONGEST_MAX },
   { 0x01, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },

   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX},

   { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
  },
  i386nbsd_sigtramp_cache_init
};

static const struct tramp_frame i386nbsd_sigtramp_sc2 =
{
  SIGTRAMP_FRAME,
  1,
  {
   /* leal  0x0c(%esp), %eax */
   { 0x8d, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x0c, ULONGEST_MAX },
   /* movl  %eax, 0x4(%esp) */
   { 0x89, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x04, ULONGEST_MAX },
   /* movl  $0x127, %eax		# __sigreturn14 */
   { 0xb8, ULONGEST_MAX },
   { 0x27, ULONGEST_MAX },
   {0x01, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX},
   /* movl  %eax, 0x4(%esp) */
   { 0x89, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x04, ULONGEST_MAX },
   /* movl  $0x1, %eax */
   { 0xb8, ULONGEST_MAX },
   { 0x01, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX},
   { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
  },
  i386nbsd_sigtramp_cache_init
};

static const struct tramp_frame i386nbsd_sigtramp_si2 =
{
  SIGTRAMP_FRAME,
  1,
  {
   /* movl  8(%esp),%eax */
   { 0x8b, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x08, ULONGEST_MAX },
   /* movl  %eax, 0x4(%esp) */
   { 0x89, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x04, ULONGEST_MAX },
   /* movl  $0x134, %eax            # setcontext */
   { 0xb8, ULONGEST_MAX },
   { 0x34, ULONGEST_MAX },
   { 0x01, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX },
   /* movl  %eax, 0x4(%esp) */
   { 0x89, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x04, ULONGEST_MAX },
   /* movl  $0x1, %eax */
   { 0xb8, ULONGEST_MAX },
   { 0x01, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX },
   { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
  },
  i386nbsd_sigtramp_cache_init
};

static const struct tramp_frame i386nbsd_sigtramp_si31 =
{
  SIGTRAMP_FRAME,
  1,
  {
   /* leal  0x8c(%esp), %eax */
   { 0x8d, ULONGEST_MAX },
   { 0x84, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x8c, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   /* movl  %eax, 0x4(%esp) */
   { 0x89, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x04, ULONGEST_MAX },
   /* movl  $0x134, %eax            # setcontext */
   { 0xb8, ULONGEST_MAX },
   { 0x34, ULONGEST_MAX },
   { 0x01, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX},
   /* movl  %eax, 0x4(%esp) */
   { 0x89, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x04, ULONGEST_MAX },
   /* movl  $0x1, %eax */
   { 0xb8, ULONGEST_MAX },
   { 0x01, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX},
   { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
  },
  i386nbsd_sigtramp_cache_init
};

static const struct tramp_frame i386nbsd_sigtramp_si4 =
{
  SIGTRAMP_FRAME,
  1,
  {
   /* leal  0x8c(%esp), %eax */
   { 0x8d, ULONGEST_MAX },
   { 0x84, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x8c, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   /* movl  %eax, 0x4(%esp) */
   { 0x89, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x04, ULONGEST_MAX },
   /* movl  $0x134, %eax            # setcontext */
   { 0xb8, ULONGEST_MAX },
   { 0x34, ULONGEST_MAX },
   { 0x01, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   { 0x00, ULONGEST_MAX },
   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX},
   /* movl   $0xffffffff,0x4(%esp) */
   { 0xc7, ULONGEST_MAX },
   { 0x44, ULONGEST_MAX },
   { 0x24, ULONGEST_MAX },
   { 0x04, ULONGEST_MAX },
   { 0xff, ULONGEST_MAX },
   { 0xff, ULONGEST_MAX },
   { 0xff, ULONGEST_MAX },
   { 0xff, ULONGEST_MAX },
   /* movl  $0x1, %eax */
   { 0xb8, ULONGEST_MAX },
   { 0x01, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   {0x00, ULONGEST_MAX },
   /* int   $0x80 */
   { 0xcd, ULONGEST_MAX },
   { 0x80, ULONGEST_MAX},
   { TRAMP_SENTINEL_INSN, ULONGEST_MAX }
  },
  i386nbsd_sigtramp_cache_init
};

static void
i386nbsd_sigtramp_cache_init (const struct tramp_frame *self,
			      struct frame_info *this_frame,
			      struct trad_frame_cache *this_cache,
			      CORE_ADDR func)
{
  struct gdbarch *gdbarch = get_frame_arch (this_frame);
  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  CORE_ADDR sp = get_frame_register_unsigned (this_frame, I386_ESP_REGNUM);
  CORE_ADDR base;
  int *reg_offset;
  int num_regs;
  int i;

  if (self == &i386nbsd_sigtramp_sc16 || self == &i386nbsd_sigtramp_sc2)
    {
      reg_offset = i386nbsd_sc_reg_offset;
      num_regs = ARRAY_SIZE (i386nbsd_sc_reg_offset);

      /* Read in the sigcontext address.  */
      base = read_memory_unsigned_integer (sp + 8, 4, byte_order);
    }
  else
    {
      reg_offset = i386nbsd_mc_reg_offset;
      num_regs = ARRAY_SIZE (i386nbsd_mc_reg_offset);

      /* Read in the ucontext address.  */
      base = read_memory_unsigned_integer (sp + 8, 4, byte_order);
      /* offsetof(ucontext_t, uc_mcontext) == 36 */
      base += 36;
    }

  for (i = 0; i < num_regs; i++)
    if (reg_offset[i] != -1)
      trad_frame_set_reg_addr (this_cache, i, base + reg_offset[i]);

  /* Construct the frame ID using the function start.  */
  trad_frame_set_id (this_cache, frame_id_build (sp, func));
}


static void 
i386nbsd_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  /* Obviously NetBSD is BSD-based.  */
  i386bsd_init_abi (info, gdbarch);

  /* NetBSD has a different `struct reg'.  */
  tdep->gregset_reg_offset = i386nbsd_r_reg_offset;
  tdep->gregset_num_regs = ARRAY_SIZE (i386nbsd_r_reg_offset);
  tdep->sizeof_gregset = 16 * 4;

  /* NetBSD uses -freg-struct-return by default.  */
  tdep->struct_return = reg_struct_return;

  /* NetBSD uses tramp_frame sniffers for signal trampolines.  */
  tdep->sigcontext_addr= 0;
  tdep->sigtramp_start = 0;
  tdep->sigtramp_end = 0;
  tdep->sigtramp_p = 0;
  tdep->sc_reg_offset = 0;
  tdep->sc_num_regs = 0;

  tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_sc16);
  tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_sc2);
  tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_si2);
  tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_si31);
  tramp_frame_prepend_unwinder (gdbarch, &i386nbsd_sigtramp_si4);
}

/* NetBSD ELF.  */

static void
i386nbsdelf_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
{
  struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  /* It's still NetBSD.  */
  i386nbsd_init_abi (info, gdbarch);

  /* But ELF-based.  */
  i386_elf_init_abi (info, gdbarch);

  /* NetBSD ELF uses SVR4-style shared libraries.  */
  set_solib_svr4_fetch_link_map_offsets
    (gdbarch, svr4_ilp32_fetch_link_map_offsets);

  /* NetBSD ELF uses -fpcc-struct-return by default.  */
  tdep->struct_return = pcc_struct_return;
}

void
_initialize_i386nbsd_tdep (void)
{
  gdbarch_register_osabi (bfd_arch_i386, 0, GDB_OSABI_NETBSD,
			  i386nbsdelf_init_abi);
}