* config/arm/linux.mt: Remove code protected by GDBSERVER define.
[deliverable/binutils-gdb.git] / gdb / arm-linux-tdep.c
1 /* GNU/Linux on ARM target support.
2 Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "target.h"
23 #include "value.h"
24 #include "gdbtypes.h"
25 #include "floatformat.h"
26 #include "gdbcore.h"
27 #include "frame.h"
28 #include "regcache.h"
29 #include "doublest.h"
30 #include "solib-svr4.h"
31 #include "osabi.h"
32
33 #include "arm-tdep.h"
34
35 /* For shared library handling. */
36 #include "symtab.h"
37 #include "symfile.h"
38 #include "objfiles.h"
39
40 /* Under ARM GNU/Linux the traditional way of performing a breakpoint
41 is to execute a particular software interrupt, rather than use a
42 particular undefined instruction to provoke a trap. Upon exection
43 of the software interrupt the kernel stops the inferior with a
44 SIGTRAP, and wakes the debugger. Since ARM GNU/Linux is little
45 endian, and doesn't support Thumb at the moment we only override
46 the ARM little-endian breakpoint. */
47
48 static const char arm_linux_arm_le_breakpoint[] = {0x01,0x00,0x9f,0xef};
49
50 /* DEPRECATED_CALL_DUMMY_WORDS:
51 This sequence of words is the instructions
52
53 mov lr, pc
54 mov pc, r4
55 swi bkpt_swi
56
57 Note this is 12 bytes. */
58
59 LONGEST arm_linux_call_dummy_words[] =
60 {
61 0xe1a0e00f, 0xe1a0f004, 0xef9f001
62 };
63
64 /* Description of the longjmp buffer. */
65 #define ARM_LINUX_JB_ELEMENT_SIZE INT_REGISTER_RAW_SIZE
66 #define ARM_LINUX_JB_PC 21
67
68 /* Extract from an array REGBUF containing the (raw) register state
69 a function return value of type TYPE, and copy that, in virtual format,
70 into VALBUF. */
71 /* FIXME rearnsha/2002-02-23: This function shouldn't be necessary.
72 The ARM generic one should be able to handle the model used by
73 linux and the low-level formatting of the registers should be
74 hidden behind the regcache abstraction. */
75 static void
76 arm_linux_extract_return_value (struct type *type,
77 char regbuf[],
78 char *valbuf)
79 {
80 /* ScottB: This needs to be looked at to handle the different
81 floating point emulators on ARM GNU/Linux. Right now the code
82 assumes that fetch inferior registers does the right thing for
83 GDB. I suspect this won't handle NWFPE registers correctly, nor
84 will the default ARM version (arm_extract_return_value()). */
85
86 int regnum = ((TYPE_CODE_FLT == TYPE_CODE (type))
87 ? ARM_F0_REGNUM : ARM_A1_REGNUM);
88 memcpy (valbuf, &regbuf[REGISTER_BYTE (regnum)], TYPE_LENGTH (type));
89 }
90
91 /* Note: ScottB
92
93 This function does not support passing parameters using the FPA
94 variant of the APCS. It passes any floating point arguments in the
95 general registers and/or on the stack.
96
97 FIXME: This and arm_push_arguments should be merged. However this
98 function breaks on a little endian host, big endian target
99 using the COFF file format. ELF is ok.
100
101 ScottB. */
102
103 /* Addresses for calling Thumb functions have the bit 0 set.
104 Here are some macros to test, set, or clear bit 0 of addresses. */
105 #define IS_THUMB_ADDR(addr) ((addr) & 1)
106 #define MAKE_THUMB_ADDR(addr) ((addr) | 1)
107 #define UNMAKE_THUMB_ADDR(addr) ((addr) & ~1)
108
109 static CORE_ADDR
110 arm_linux_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
111 int struct_return, CORE_ADDR struct_addr)
112 {
113 char *fp;
114 int argnum, argreg, nstack_size;
115
116 /* Walk through the list of args and determine how large a temporary
117 stack is required. Need to take care here as structs may be
118 passed on the stack, and we have to to push them. */
119 nstack_size = -4 * DEPRECATED_REGISTER_SIZE; /* Some arguments go into A1-A4. */
120
121 if (struct_return) /* The struct address goes in A1. */
122 nstack_size += DEPRECATED_REGISTER_SIZE;
123
124 /* Walk through the arguments and add their size to nstack_size. */
125 for (argnum = 0; argnum < nargs; argnum++)
126 {
127 int len;
128 struct type *arg_type;
129
130 arg_type = check_typedef (VALUE_TYPE (args[argnum]));
131 len = TYPE_LENGTH (arg_type);
132
133 /* ANSI C code passes float arguments as integers, K&R code
134 passes float arguments as doubles. Correct for this here. */
135 if (TYPE_CODE_FLT == TYPE_CODE (arg_type) && DEPRECATED_REGISTER_SIZE == len)
136 nstack_size += FP_REGISTER_VIRTUAL_SIZE;
137 else
138 nstack_size += len;
139 }
140
141 /* Allocate room on the stack, and initialize our stack frame
142 pointer. */
143 fp = NULL;
144 if (nstack_size > 0)
145 {
146 sp -= nstack_size;
147 fp = (char *) sp;
148 }
149
150 /* Initialize the integer argument register pointer. */
151 argreg = ARM_A1_REGNUM;
152
153 /* The struct_return pointer occupies the first parameter passing
154 register. */
155 if (struct_return)
156 write_register (argreg++, struct_addr);
157
158 /* Process arguments from left to right. Store as many as allowed
159 in the parameter passing registers (A1-A4), and save the rest on
160 the temporary stack. */
161 for (argnum = 0; argnum < nargs; argnum++)
162 {
163 int len;
164 char *val;
165 CORE_ADDR regval;
166 enum type_code typecode;
167 struct type *arg_type, *target_type;
168
169 arg_type = check_typedef (VALUE_TYPE (args[argnum]));
170 target_type = TYPE_TARGET_TYPE (arg_type);
171 len = TYPE_LENGTH (arg_type);
172 typecode = TYPE_CODE (arg_type);
173 val = (char *) VALUE_CONTENTS (args[argnum]);
174
175 /* ANSI C code passes float arguments as integers, K&R code
176 passes float arguments as doubles. The .stabs record for
177 for ANSI prototype floating point arguments records the
178 type as FP_INTEGER, while a K&R style (no prototype)
179 .stabs records the type as FP_FLOAT. In this latter case
180 the compiler converts the float arguments to double before
181 calling the function. */
182 if (TYPE_CODE_FLT == typecode && DEPRECATED_REGISTER_SIZE == len)
183 {
184 DOUBLEST dblval;
185 dblval = deprecated_extract_floating (val, len);
186 len = TARGET_DOUBLE_BIT / TARGET_CHAR_BIT;
187 val = alloca (len);
188 deprecated_store_floating (val, len, dblval);
189 }
190
191 /* If the argument is a pointer to a function, and it is a Thumb
192 function, set the low bit of the pointer. */
193 if (TYPE_CODE_PTR == typecode
194 && NULL != target_type
195 && TYPE_CODE_FUNC == TYPE_CODE (target_type))
196 {
197 CORE_ADDR regval = extract_unsigned_integer (val, len);
198 if (arm_pc_is_thumb (regval))
199 store_unsigned_integer (val, len, MAKE_THUMB_ADDR (regval));
200 }
201
202 /* Copy the argument to general registers or the stack in
203 register-sized pieces. Large arguments are split between
204 registers and stack. */
205 while (len > 0)
206 {
207 int partial_len = len < DEPRECATED_REGISTER_SIZE ? len : DEPRECATED_REGISTER_SIZE;
208
209 if (argreg <= ARM_LAST_ARG_REGNUM)
210 {
211 /* It's an argument being passed in a general register. */
212 regval = extract_unsigned_integer (val, partial_len);
213 write_register (argreg++, regval);
214 }
215 else
216 {
217 /* Push the arguments onto the stack. */
218 write_memory ((CORE_ADDR) fp, val, DEPRECATED_REGISTER_SIZE);
219 fp += DEPRECATED_REGISTER_SIZE;
220 }
221
222 len -= partial_len;
223 val += partial_len;
224 }
225 }
226
227 /* Return adjusted stack pointer. */
228 return sp;
229 }
230
231 /*
232 Dynamic Linking on ARM GNU/Linux
233 --------------------------------
234
235 Note: PLT = procedure linkage table
236 GOT = global offset table
237
238 As much as possible, ELF dynamic linking defers the resolution of
239 jump/call addresses until the last minute. The technique used is
240 inspired by the i386 ELF design, and is based on the following
241 constraints.
242
243 1) The calling technique should not force a change in the assembly
244 code produced for apps; it MAY cause changes in the way assembly
245 code is produced for position independent code (i.e. shared
246 libraries).
247
248 2) The technique must be such that all executable areas must not be
249 modified; and any modified areas must not be executed.
250
251 To do this, there are three steps involved in a typical jump:
252
253 1) in the code
254 2) through the PLT
255 3) using a pointer from the GOT
256
257 When the executable or library is first loaded, each GOT entry is
258 initialized to point to the code which implements dynamic name
259 resolution and code finding. This is normally a function in the
260 program interpreter (on ARM GNU/Linux this is usually
261 ld-linux.so.2, but it does not have to be). On the first
262 invocation, the function is located and the GOT entry is replaced
263 with the real function address. Subsequent calls go through steps
264 1, 2 and 3 and end up calling the real code.
265
266 1) In the code:
267
268 b function_call
269 bl function_call
270
271 This is typical ARM code using the 26 bit relative branch or branch
272 and link instructions. The target of the instruction
273 (function_call is usually the address of the function to be called.
274 In position independent code, the target of the instruction is
275 actually an entry in the PLT when calling functions in a shared
276 library. Note that this call is identical to a normal function
277 call, only the target differs.
278
279 2) In the PLT:
280
281 The PLT is a synthetic area, created by the linker. It exists in
282 both executables and libraries. It is an array of stubs, one per
283 imported function call. It looks like this:
284
285 PLT[0]:
286 str lr, [sp, #-4]! @push the return address (lr)
287 ldr lr, [pc, #16] @load from 6 words ahead
288 add lr, pc, lr @form an address for GOT[0]
289 ldr pc, [lr, #8]! @jump to the contents of that addr
290
291 The return address (lr) is pushed on the stack and used for
292 calculations. The load on the second line loads the lr with
293 &GOT[3] - . - 20. The addition on the third leaves:
294
295 lr = (&GOT[3] - . - 20) + (. + 8)
296 lr = (&GOT[3] - 12)
297 lr = &GOT[0]
298
299 On the fourth line, the pc and lr are both updated, so that:
300
301 pc = GOT[2]
302 lr = &GOT[0] + 8
303 = &GOT[2]
304
305 NOTE: PLT[0] borrows an offset .word from PLT[1]. This is a little
306 "tight", but allows us to keep all the PLT entries the same size.
307
308 PLT[n+1]:
309 ldr ip, [pc, #4] @load offset from gotoff
310 add ip, pc, ip @add the offset to the pc
311 ldr pc, [ip] @jump to that address
312 gotoff: .word GOT[n+3] - .
313
314 The load on the first line, gets an offset from the fourth word of
315 the PLT entry. The add on the second line makes ip = &GOT[n+3],
316 which contains either a pointer to PLT[0] (the fixup trampoline) or
317 a pointer to the actual code.
318
319 3) In the GOT:
320
321 The GOT contains helper pointers for both code (PLT) fixups and
322 data fixups. The first 3 entries of the GOT are special. The next
323 M entries (where M is the number of entries in the PLT) belong to
324 the PLT fixups. The next D (all remaining) entries belong to
325 various data fixups. The actual size of the GOT is 3 + M + D.
326
327 The GOT is also a synthetic area, created by the linker. It exists
328 in both executables and libraries. When the GOT is first
329 initialized , all the GOT entries relating to PLT fixups are
330 pointing to code back at PLT[0].
331
332 The special entries in the GOT are:
333
334 GOT[0] = linked list pointer used by the dynamic loader
335 GOT[1] = pointer to the reloc table for this module
336 GOT[2] = pointer to the fixup/resolver code
337
338 The first invocation of function call comes through and uses the
339 fixup/resolver code. On the entry to the fixup/resolver code:
340
341 ip = &GOT[n+3]
342 lr = &GOT[2]
343 stack[0] = return address (lr) of the function call
344 [r0, r1, r2, r3] are still the arguments to the function call
345
346 This is enough information for the fixup/resolver code to work
347 with. Before the fixup/resolver code returns, it actually calls
348 the requested function and repairs &GOT[n+3]. */
349
350 /* Find the minimal symbol named NAME, and return both the minsym
351 struct and its objfile. This probably ought to be in minsym.c, but
352 everything there is trying to deal with things like C++ and
353 SOFUN_ADDRESS_MAYBE_TURQUOISE, ... Since this is so simple, it may
354 be considered too special-purpose for general consumption. */
355
356 static struct minimal_symbol *
357 find_minsym_and_objfile (char *name, struct objfile **objfile_p)
358 {
359 struct objfile *objfile;
360
361 ALL_OBJFILES (objfile)
362 {
363 struct minimal_symbol *msym;
364
365 ALL_OBJFILE_MSYMBOLS (objfile, msym)
366 {
367 if (DEPRECATED_SYMBOL_NAME (msym)
368 && strcmp (DEPRECATED_SYMBOL_NAME (msym), name) == 0)
369 {
370 *objfile_p = objfile;
371 return msym;
372 }
373 }
374 }
375
376 return 0;
377 }
378
379
380 /* Fetch, and possibly build, an appropriate link_map_offsets structure
381 for ARM linux targets using the struct offsets defined in <link.h>.
382 Note, however, that link.h is not actually referred to in this file.
383 Instead, the relevant structs offsets were obtained from examining
384 link.h. (We can't refer to link.h from this file because the host
385 system won't necessarily have it, or if it does, the structs which
386 it defines will refer to the host system, not the target). */
387
388 static struct link_map_offsets *
389 arm_linux_svr4_fetch_link_map_offsets (void)
390 {
391 static struct link_map_offsets lmo;
392 static struct link_map_offsets *lmp = 0;
393
394 if (lmp == 0)
395 {
396 lmp = &lmo;
397
398 lmo.r_debug_size = 8; /* Actual size is 20, but this is all we
399 need. */
400
401 lmo.r_map_offset = 4;
402 lmo.r_map_size = 4;
403
404 lmo.link_map_size = 20; /* Actual size is 552, but this is all we
405 need. */
406
407 lmo.l_addr_offset = 0;
408 lmo.l_addr_size = 4;
409
410 lmo.l_name_offset = 4;
411 lmo.l_name_size = 4;
412
413 lmo.l_next_offset = 12;
414 lmo.l_next_size = 4;
415
416 lmo.l_prev_offset = 16;
417 lmo.l_prev_size = 4;
418 }
419
420 return lmp;
421 }
422
423 static CORE_ADDR
424 skip_hurd_resolver (CORE_ADDR pc)
425 {
426 /* The HURD dynamic linker is part of the GNU C library, so many
427 GNU/Linux distributions use it. (All ELF versions, as far as I
428 know.) An unresolved PLT entry points to "_dl_runtime_resolve",
429 which calls "fixup" to patch the PLT, and then passes control to
430 the function.
431
432 We look for the symbol `_dl_runtime_resolve', and find `fixup' in
433 the same objfile. If we are at the entry point of `fixup', then
434 we set a breakpoint at the return address (at the top of the
435 stack), and continue.
436
437 It's kind of gross to do all these checks every time we're
438 called, since they don't change once the executable has gotten
439 started. But this is only a temporary hack --- upcoming versions
440 of GNU/Linux will provide a portable, efficient interface for
441 debugging programs that use shared libraries. */
442
443 struct objfile *objfile;
444 struct minimal_symbol *resolver
445 = find_minsym_and_objfile ("_dl_runtime_resolve", &objfile);
446
447 if (resolver)
448 {
449 struct minimal_symbol *fixup
450 = lookup_minimal_symbol ("fixup", NULL, objfile);
451
452 if (fixup && SYMBOL_VALUE_ADDRESS (fixup) == pc)
453 return (DEPRECATED_SAVED_PC_AFTER_CALL (get_current_frame ()));
454 }
455
456 return 0;
457 }
458
459 /* See the comments for SKIP_SOLIB_RESOLVER at the top of infrun.c.
460 This function:
461 1) decides whether a PLT has sent us into the linker to resolve
462 a function reference, and
463 2) if so, tells us where to set a temporary breakpoint that will
464 trigger when the dynamic linker is done. */
465
466 CORE_ADDR
467 arm_linux_skip_solib_resolver (CORE_ADDR pc)
468 {
469 CORE_ADDR result;
470
471 /* Plug in functions for other kinds of resolvers here. */
472 result = skip_hurd_resolver (pc);
473
474 if (result)
475 return result;
476
477 return 0;
478 }
479
480 /* The constants below were determined by examining the following files
481 in the linux kernel sources:
482
483 arch/arm/kernel/signal.c
484 - see SWI_SYS_SIGRETURN and SWI_SYS_RT_SIGRETURN
485 include/asm-arm/unistd.h
486 - see __NR_sigreturn, __NR_rt_sigreturn, and __NR_SYSCALL_BASE */
487
488 #define ARM_LINUX_SIGRETURN_INSTR 0xef900077
489 #define ARM_LINUX_RT_SIGRETURN_INSTR 0xef9000ad
490
491 /* arm_linux_in_sigtramp determines if PC points at one of the
492 instructions which cause control to return to the Linux kernel upon
493 return from a signal handler. FUNC_NAME is unused. */
494
495 int
496 arm_linux_in_sigtramp (CORE_ADDR pc, char *func_name)
497 {
498 unsigned long inst;
499
500 inst = read_memory_integer (pc, 4);
501
502 return (inst == ARM_LINUX_SIGRETURN_INSTR
503 || inst == ARM_LINUX_RT_SIGRETURN_INSTR);
504
505 }
506
507 /* arm_linux_sigcontext_register_address returns the address in the
508 sigcontext of register REGNO given a stack pointer value SP and
509 program counter value PC. The value 0 is returned if PC is not
510 pointing at one of the signal return instructions or if REGNO is
511 not saved in the sigcontext struct. */
512
513 CORE_ADDR
514 arm_linux_sigcontext_register_address (CORE_ADDR sp, CORE_ADDR pc, int regno)
515 {
516 unsigned long inst;
517 CORE_ADDR reg_addr = 0;
518
519 inst = read_memory_integer (pc, 4);
520
521 if (inst == ARM_LINUX_SIGRETURN_INSTR
522 || inst == ARM_LINUX_RT_SIGRETURN_INSTR)
523 {
524 CORE_ADDR sigcontext_addr;
525
526 /* The sigcontext structure is at different places for the two
527 signal return instructions. For ARM_LINUX_SIGRETURN_INSTR,
528 it starts at the SP value. For ARM_LINUX_RT_SIGRETURN_INSTR,
529 it is at SP+8. For the latter instruction, it may also be
530 the case that the address of this structure may be determined
531 by reading the 4 bytes at SP, but I'm not convinced this is
532 reliable.
533
534 In any event, these magic constants (0 and 8) may be
535 determined by examining struct sigframe and struct
536 rt_sigframe in arch/arm/kernel/signal.c in the Linux kernel
537 sources. */
538
539 if (inst == ARM_LINUX_RT_SIGRETURN_INSTR)
540 sigcontext_addr = sp + 8;
541 else /* inst == ARM_LINUX_SIGRETURN_INSTR */
542 sigcontext_addr = sp + 0;
543
544 /* The layout of the sigcontext structure for ARM GNU/Linux is
545 in include/asm-arm/sigcontext.h in the Linux kernel sources.
546
547 There are three 4-byte fields which precede the saved r0
548 field. (This accounts for the 12 in the code below.) The
549 sixteen registers (4 bytes per field) follow in order. The
550 PSR value follows the sixteen registers which accounts for
551 the constant 19 below. */
552
553 if (0 <= regno && regno <= ARM_PC_REGNUM)
554 reg_addr = sigcontext_addr + 12 + (4 * regno);
555 else if (regno == ARM_PS_REGNUM)
556 reg_addr = sigcontext_addr + 19 * 4;
557 }
558
559 return reg_addr;
560 }
561
562 static void
563 arm_linux_init_abi (struct gdbarch_info info,
564 struct gdbarch *gdbarch)
565 {
566 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
567
568 tdep->lowest_pc = 0x8000;
569 tdep->arm_breakpoint = arm_linux_arm_le_breakpoint;
570 tdep->arm_breakpoint_size = sizeof (arm_linux_arm_le_breakpoint);
571
572 tdep->fp_model = ARM_FLOAT_FPA;
573
574 tdep->jb_pc = ARM_LINUX_JB_PC;
575 tdep->jb_elt_size = ARM_LINUX_JB_ELEMENT_SIZE;
576
577 set_solib_svr4_fetch_link_map_offsets
578 (gdbarch, arm_linux_svr4_fetch_link_map_offsets);
579
580 set_gdbarch_deprecated_call_dummy_words (gdbarch, arm_linux_call_dummy_words);
581 set_gdbarch_deprecated_sizeof_call_dummy_words (gdbarch, sizeof (arm_linux_call_dummy_words));
582
583 /* The following two overrides shouldn't be needed. */
584 set_gdbarch_deprecated_extract_return_value (gdbarch, arm_linux_extract_return_value);
585 set_gdbarch_deprecated_push_arguments (gdbarch, arm_linux_push_arguments);
586
587 /* Shared library handling. */
588 set_gdbarch_in_solib_call_trampoline (gdbarch, in_plt_section);
589 set_gdbarch_skip_trampoline_code (gdbarch, find_solib_trampoline_target);
590 }
591
592 void
593 _initialize_arm_linux_tdep (void)
594 {
595 gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_LINUX,
596 arm_linux_init_abi);
597 }
This page took 0.061704 seconds and 4 git commands to generate.