Symbian config
[platform/upstream/binutils.git] / gdb / arm-symbian-tdep.c
1 /* ARM Symbian OS target support.
2
3    Copyright (C) 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "frame.h"
23 #include "objfiles.h"
24 #include "osabi.h"
25 #include "solib.h"
26 #include "solib-target.h"
27 #include "target.h"
28 #include "elf-bfd.h"
29
30 /* If PC is in a DLL import stub, return the address of the `real'
31    function belonging to the stub.  */
32
33 CORE_ADDR
34 arm_symbian_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
35 {
36   struct gdbarch *gdbarch;
37   enum bfd_endian byte_order;
38   ULONGEST insn;
39   CORE_ADDR dest;
40   gdb_byte buf[4];
41
42   if (!in_plt_section (pc, NULL))
43     return 0;
44
45   if (target_read_memory (pc, buf, 4) != 0)
46     return 0;
47
48   gdbarch = get_frame_arch (frame);
49   byte_order = gdbarch_byte_order (gdbarch);
50
51   /* ldr pc, [pc, #-4].  */
52   insn = extract_unsigned_integer (buf, 4, byte_order);
53   if (insn != 0xe51ff004)
54     return 0;
55
56   if (target_read_memory (pc + 4, buf, 4) != 0)
57     return 0;
58
59   dest = extract_unsigned_integer (buf, 4, byte_order);
60   return gdbarch_addr_bits_remove (gdbarch, dest);
61 }
62
63 static void
64 arm_symbian_init_abi (struct gdbarch_info info,
65                       struct gdbarch *gdbarch)
66 {
67   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
68
69   /* Shared library handling.  */
70   set_gdbarch_skip_trampoline_code (gdbarch, arm_symbian_skip_trampoline_code);
71
72   set_solib_ops (gdbarch, &solib_target_so_ops);
73 }
74
75 /* Recognize Symbian object files.  */
76
77 static enum gdb_osabi
78 arm_symbian_osabi_sniffer (bfd *abfd)
79 {
80   Elf_Internal_Phdr *phdrs, **segments;
81   long phdrs_size;
82   int num_phdrs, i;
83
84   /* Symbian executables are always shared objects (ET_DYN).  */
85   if (elf_elfheader (abfd)->e_type == ET_EXEC)
86     return GDB_OSABI_UNKNOWN;
87
88   if (elf_elfheader (abfd)->e_ident[EI_OSABI] != ELFOSABI_NONE)
89     return GDB_OSABI_UNKNOWN;
90
91   /* Check for the ELF headers not being part of any PT_LOAD segment.
92      Symbian is the only GDB supported (or GNU binutils supported) ARM
93      target which uses a postlinker to flatten ELF files, dropping the
94      ELF dynamic info in the process.  */
95   phdrs_size = bfd_get_elf_phdr_upper_bound (abfd);
96   if (phdrs_size == -1)
97     return GDB_OSABI_UNKNOWN;
98
99   phdrs = alloca (phdrs_size);
100   num_phdrs = bfd_get_elf_phdrs (abfd, phdrs);
101   if (num_phdrs == -1)
102     return GDB_OSABI_UNKNOWN;
103
104   for (i = 0; i < num_phdrs; i++)
105     if (phdrs[i].p_type == PT_LOAD && phdrs[i].p_offset == 0)
106       return GDB_OSABI_UNKNOWN;
107
108   /* Looks like a Symbian binary.  */
109   return GDB_OSABI_SYMBIAN;
110 }
111
112 void
113 _initialize_arm_symbian_tdep (void)
114 {
115   gdbarch_register_osabi_sniffer (bfd_arch_arm,
116                                   bfd_target_elf_flavour,
117                                   arm_symbian_osabi_sniffer);
118
119   gdbarch_register_osabi (bfd_arch_arm, 0, GDB_OSABI_SYMBIAN,
120                           arm_symbian_init_abi);
121 }