41e0efa4e76d34b5b7c94729f22ed910d13531ea
[external/binutils.git] / gdb / amd64-windows-tdep.c
1 /* Copyright (C) 2009-2012 Free Software Foundation, Inc.
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 "defs.h"
19 #include "osabi.h"
20 #include "amd64-tdep.h"
21 #include "solib.h"
22 #include "solib-target.h"
23 #include "gdbtypes.h"
24 #include "gdbcore.h"
25 #include "regcache.h"
26 #include "windows-tdep.h"
27
28 /* The registers used to pass integer arguments during a function call.  */
29 static int amd64_windows_dummy_call_integer_regs[] =
30 {
31   AMD64_RCX_REGNUM,          /* %rcx */
32   AMD64_RDX_REGNUM,          /* %rdx */
33   8,                         /* %r8 */
34   9                          /* %r9 */
35 };
36
37 /* Implement the "classify" method in the gdbarch_tdep structure
38    for amd64-windows.  */
39
40 static void
41 amd64_windows_classify (struct type *type, enum amd64_reg_class class[2])
42 {
43   switch (TYPE_CODE (type))
44     {
45       case TYPE_CODE_ARRAY:
46         /* Arrays are always passed by memory.  */
47         class[0] = class[1] = AMD64_MEMORY;
48         break;
49
50       case TYPE_CODE_STRUCT:
51       case TYPE_CODE_UNION:
52         /* Struct/Union types whose size is 1, 2, 4, or 8 bytes
53            are passed as if they were integers of the same size.
54            Types of different sizes are passed by memory.  */
55         if (TYPE_LENGTH (type) == 1
56             || TYPE_LENGTH (type) == 2
57             || TYPE_LENGTH (type) == 4
58             || TYPE_LENGTH (type) == 8)
59           {
60             class[0] = AMD64_INTEGER;
61             class[1] = AMD64_NO_CLASS;
62           }
63         else
64           class[0] = class[1] = AMD64_MEMORY;
65         break;
66
67       default:
68         /* For all the other types, the conventions are the same as
69            with the System V ABI.  */
70         amd64_classify (type, class);
71     }
72 }
73
74 /* Implement the "return_value" gdbarch method for amd64-windows.  */
75
76 static enum return_value_convention
77 amd64_windows_return_value (struct gdbarch *gdbarch, struct value *function,
78                             struct type *type, struct regcache *regcache,
79                             gdb_byte *readbuf, const gdb_byte *writebuf)
80 {
81   int len = TYPE_LENGTH (type);
82   int regnum = -1;
83
84   /* See if our value is returned through a register.  If it is, then
85      store the associated register number in REGNUM.  */
86   switch (TYPE_CODE (type))
87     {
88       case TYPE_CODE_FLT:
89       case TYPE_CODE_DECFLOAT:
90         /* __m128, __m128i, __m128d, floats, and doubles are returned
91            via XMM0.  */
92         if (len == 4 || len == 8 || len == 16)
93           regnum = AMD64_XMM0_REGNUM;
94         break;
95       default:
96         /* All other values that are 1, 2, 4 or 8 bytes long are returned
97            via RAX.  */
98         if (len == 1 || len == 2 || len == 4 || len == 8)
99           regnum = AMD64_RAX_REGNUM;
100         break;
101     }
102
103   if (regnum < 0)
104     {
105       /* RAX contains the address where the return value has been stored.  */
106       if (readbuf)
107         {
108           ULONGEST addr;
109
110           regcache_raw_read_unsigned (regcache, AMD64_RAX_REGNUM, &addr);
111           read_memory (addr, readbuf, TYPE_LENGTH (type));
112         }
113       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
114     }
115   else
116     {
117       /* Extract the return value from the register where it was stored.  */
118       if (readbuf)
119         regcache_raw_read_part (regcache, regnum, 0, len, readbuf);
120       if (writebuf)
121         regcache_raw_write_part (regcache, regnum, 0, len, writebuf);
122       return RETURN_VALUE_REGISTER_CONVENTION;
123     }
124 }
125
126 /* Check that the code pointed to by PC corresponds to a call to
127    __main, skip it if so.  Return PC otherwise.  */
128
129 static CORE_ADDR
130 amd64_skip_main_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
131 {
132   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
133   gdb_byte op;
134
135   target_read_memory (pc, &op, 1);
136   if (op == 0xe8)
137     {
138       gdb_byte buf[4];
139
140       if (target_read_memory (pc + 1, buf, sizeof buf) == 0)
141         {
142           struct minimal_symbol *s;
143           CORE_ADDR call_dest;
144
145           call_dest = pc + 5 + extract_signed_integer (buf, 4, byte_order);
146           s = lookup_minimal_symbol_by_pc (call_dest);
147           if (s != NULL
148               && SYMBOL_LINKAGE_NAME (s) != NULL
149               && strcmp (SYMBOL_LINKAGE_NAME (s), "__main") == 0)
150             pc += 5;
151         }
152     }
153
154   return pc;
155 }
156
157
158 static void
159 amd64_windows_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
160 {
161   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
162
163   amd64_init_abi (info, gdbarch);
164
165   /* On Windows, "long"s are only 32bit.  */
166   set_gdbarch_long_bit (gdbarch, 32);
167
168   /* Function calls.  */
169   tdep->call_dummy_num_integer_regs =
170     ARRAY_SIZE (amd64_windows_dummy_call_integer_regs);
171   tdep->call_dummy_integer_regs = amd64_windows_dummy_call_integer_regs;
172   tdep->classify = amd64_windows_classify;
173   tdep->memory_args_by_pointer = 1;
174   tdep->integer_param_regs_saved_in_caller_frame = 1;
175   set_gdbarch_return_value (gdbarch, amd64_windows_return_value);
176   set_gdbarch_skip_main_prologue (gdbarch, amd64_skip_main_prologue);
177
178   set_gdbarch_iterate_over_objfiles_in_search_order
179     (gdbarch, windows_iterate_over_objfiles_in_search_order);
180
181   set_solib_ops (gdbarch, &solib_target_so_ops);
182 }
183
184 /* -Wmissing-prototypes */
185 extern initialize_file_ftype _initialize_amd64_windows_tdep;
186
187 void
188 _initialize_amd64_windows_tdep (void)
189 {
190   gdbarch_register_osabi (bfd_arch_i386, bfd_mach_x86_64, GDB_OSABI_CYGWIN,
191                           amd64_windows_init_abi);
192 }
193