1 //===-- ABIWindows_x86_64.cpp ---------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "ABIWindows_x86_64.h"
10 
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "llvm/ADT/Triple.h"
14 
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/PluginManager.h"
17 #include "lldb/Core/Value.h"
18 #include "lldb/Core/ValueObjectConstResult.h"
19 #include "lldb/Core/ValueObjectMemory.h"
20 #include "lldb/Core/ValueObjectRegister.h"
21 #include "lldb/Symbol/UnwindPlan.h"
22 #include "lldb/Target/Process.h"
23 #include "lldb/Target/RegisterContext.h"
24 #include "lldb/Target/StackFrame.h"
25 #include "lldb/Target/Target.h"
26 #include "lldb/Target/Thread.h"
27 #include "lldb/Utility/ConstString.h"
28 #include "lldb/Utility/DataExtractor.h"
29 #include "lldb/Utility/LLDBLog.h"
30 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/RegisterValue.h"
32 #include "lldb/Utility/Status.h"
33 
34 using namespace lldb;
35 using namespace lldb_private;
36 
37 LLDB_PLUGIN_DEFINE(ABIWindows_x86_64)
38 
39 enum dwarf_regnums {
40   dwarf_rax = 0,
41   dwarf_rdx,
42   dwarf_rcx,
43   dwarf_rbx,
44   dwarf_rsi,
45   dwarf_rdi,
46   dwarf_rbp,
47   dwarf_rsp,
48   dwarf_r8,
49   dwarf_r9,
50   dwarf_r10,
51   dwarf_r11,
52   dwarf_r12,
53   dwarf_r13,
54   dwarf_r14,
55   dwarf_r15,
56   dwarf_rip,
57   dwarf_xmm0,
58   dwarf_xmm1,
59   dwarf_xmm2,
60   dwarf_xmm3,
61   dwarf_xmm4,
62   dwarf_xmm5,
63   dwarf_xmm6,
64   dwarf_xmm7,
65   dwarf_xmm8,
66   dwarf_xmm9,
67   dwarf_xmm10,
68   dwarf_xmm11,
69   dwarf_xmm12,
70   dwarf_xmm13,
71   dwarf_xmm14,
72   dwarf_xmm15,
73   dwarf_stmm0,
74   dwarf_stmm1,
75   dwarf_stmm2,
76   dwarf_stmm3,
77   dwarf_stmm4,
78   dwarf_stmm5,
79   dwarf_stmm6,
80   dwarf_stmm7,
81   dwarf_ymm0,
82   dwarf_ymm1,
83   dwarf_ymm2,
84   dwarf_ymm3,
85   dwarf_ymm4,
86   dwarf_ymm5,
87   dwarf_ymm6,
88   dwarf_ymm7,
89   dwarf_ymm8,
90   dwarf_ymm9,
91   dwarf_ymm10,
92   dwarf_ymm11,
93   dwarf_ymm12,
94   dwarf_ymm13,
95   dwarf_ymm14,
96   dwarf_ymm15,
97   dwarf_bnd0 = 126,
98   dwarf_bnd1,
99   dwarf_bnd2,
100   dwarf_bnd3
101 };
102 
103 bool ABIWindows_x86_64::GetPointerReturnRegister(const char *&name) {
104   name = "rax";
105   return true;
106 }
107 
108 size_t ABIWindows_x86_64::GetRedZoneSize() const { return 0; }
109 
110 //------------------------------------------------------------------
111 // Static Functions
112 //------------------------------------------------------------------
113 
114 ABISP
115 ABIWindows_x86_64::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) {
116   if (arch.GetTriple().getArch() == llvm::Triple::x86_64 &&
117       arch.GetTriple().isOSWindows()) {
118     return ABISP(
119         new ABIWindows_x86_64(std::move(process_sp), MakeMCRegisterInfo(arch)));
120   }
121   return ABISP();
122 }
123 
124 bool ABIWindows_x86_64::PrepareTrivialCall(Thread &thread, addr_t sp,
125                                            addr_t func_addr, addr_t return_addr,
126                                            llvm::ArrayRef<addr_t> args) const {
127   Log *log = GetLog(LLDBLog::Expressions);
128 
129   if (log) {
130     StreamString s;
131     s.Printf("ABIWindows_x86_64::PrepareTrivialCall (tid = 0x%" PRIx64
132              ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
133              ", return_addr = 0x%" PRIx64,
134              thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
135              (uint64_t)return_addr);
136 
137     for (size_t i = 0; i < args.size(); ++i)
138       s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
139                args[i]);
140     s.PutCString(")");
141     log->PutString(s.GetString());
142   }
143 
144   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
145   if (!reg_ctx)
146     return false;
147 
148   const RegisterInfo *reg_info = nullptr;
149 
150   if (args.size() > 4) // Windows x64 only put first 4 arguments into registers
151     return false;
152 
153   for (size_t i = 0; i < args.size(); ++i) {
154     reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
155                                         LLDB_REGNUM_GENERIC_ARG1 + i);
156     LLDB_LOGF(log, "About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
157               static_cast<uint64_t>(i + 1), args[i], reg_info->name);
158     if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
159       return false;
160   }
161 
162   // First, align the SP
163 
164   LLDB_LOGF(log, "16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
165             (uint64_t)sp, (uint64_t)(sp & ~0xfull));
166 
167   sp &= ~(0xfull); // 16-byte alignment
168 
169   sp -= 8; // return address
170 
171   Status error;
172   const RegisterInfo *pc_reg_info =
173       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
174   const RegisterInfo *sp_reg_info =
175       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
176   ProcessSP process_sp(thread.GetProcess());
177 
178   RegisterValue reg_value;
179   LLDB_LOGF(log,
180             "Pushing the return address onto the stack: 0x%" PRIx64
181             ": 0x%" PRIx64,
182             (uint64_t)sp, (uint64_t)return_addr);
183 
184   // Save return address onto the stack
185   if (!process_sp->WritePointerToMemory(sp, return_addr, error))
186     return false;
187 
188   // %rsp is set to the actual stack value.
189 
190   LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp);
191 
192   if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
193     return false;
194 
195   // %rip is set to the address of the called function.
196 
197   LLDB_LOGF(log, "Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
198 
199   if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
200     return false;
201 
202   return true;
203 }
204 
205 static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
206                                 bool is_signed, Thread &thread,
207                                 uint32_t *argument_register_ids,
208                                 unsigned int &current_argument_register,
209                                 addr_t &current_stack_argument) {
210   if (bit_width > 64)
211     return false; // Scalar can't hold large integer arguments
212 
213   if (current_argument_register < 4) { // Windows pass first 4 arguments to register
214     scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
215         argument_register_ids[current_argument_register], 0);
216     current_argument_register++;
217     if (is_signed)
218       scalar.SignExtend(bit_width);
219   	return true;
220   }
221   uint32_t byte_size = (bit_width + (CHAR_BIT - 1)) / CHAR_BIT;
222   Status error;
223   if (thread.GetProcess()->ReadScalarIntegerFromMemory(
224           current_stack_argument, byte_size, is_signed, scalar, error)) {
225     current_stack_argument += byte_size;
226     return true;
227   }
228   return false;
229 }
230 
231 bool ABIWindows_x86_64::GetArgumentValues(Thread &thread,
232                                        ValueList &values) const {
233   unsigned int num_values = values.GetSize();
234   unsigned int value_index;
235 
236   // Extract the register context so we can read arguments from registers
237 
238   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
239 
240   if (!reg_ctx)
241     return false;
242 
243   // Get the pointer to the first stack argument so we have a place to start
244   // when reading data
245 
246   addr_t sp = reg_ctx->GetSP(0);
247 
248   if (!sp)
249     return false;
250 
251   addr_t current_stack_argument = sp + 8; // jump over return address
252 
253   uint32_t argument_register_ids[4];
254 
255   argument_register_ids[0] =
256       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)
257           ->kinds[eRegisterKindLLDB];
258   argument_register_ids[1] =
259       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)
260           ->kinds[eRegisterKindLLDB];
261   argument_register_ids[2] =
262       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)
263           ->kinds[eRegisterKindLLDB];
264   argument_register_ids[3] =
265       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)
266           ->kinds[eRegisterKindLLDB];
267 
268   unsigned int current_argument_register = 0;
269 
270   for (value_index = 0; value_index < num_values; ++value_index) {
271     Value *value = values.GetValueAtIndex(value_index);
272 
273     if (!value)
274       return false;
275 
276     CompilerType compiler_type = value->GetCompilerType();
277     llvm::Optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread);
278     if (!bit_size)
279       return false;
280     bool is_signed;
281 
282     if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
283       ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread,
284                           argument_register_ids, current_argument_register,
285                           current_stack_argument);
286     } else if (compiler_type.IsPointerType()) {
287       ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread,
288                           argument_register_ids, current_argument_register,
289                           current_stack_argument);
290     }
291   }
292 
293   return true;
294 }
295 
296 Status ABIWindows_x86_64::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
297                                             lldb::ValueObjectSP &new_value_sp) {
298   Status error;
299   if (!new_value_sp) {
300     error.SetErrorString("Empty value object for return value.");
301     return error;
302   }
303 
304   CompilerType compiler_type = new_value_sp->GetCompilerType();
305   if (!compiler_type) {
306     error.SetErrorString("Null clang type for return value.");
307     return error;
308   }
309 
310   Thread *thread = frame_sp->GetThread().get();
311 
312   bool is_signed;
313   uint32_t count;
314   bool is_complex;
315 
316   RegisterContext *reg_ctx = thread->GetRegisterContext().get();
317 
318   bool set_it_simple = false;
319   if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
320       compiler_type.IsPointerType()) {
321     const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("rax", 0);
322 
323     DataExtractor data;
324     Status data_error;
325     size_t num_bytes = new_value_sp->GetData(data, data_error);
326     if (data_error.Fail()) {
327       error.SetErrorStringWithFormat(
328           "Couldn't convert return value to raw data: %s",
329           data_error.AsCString());
330       return error;
331     }
332     lldb::offset_t offset = 0;
333     if (num_bytes <= 8) {
334       uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
335 
336       if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
337         set_it_simple = true;
338     } else {
339       error.SetErrorString("We don't support returning longer than 64 bit "
340                            "integer values at present.");
341     }
342   } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
343     if (is_complex)
344       error.SetErrorString(
345           "We don't support returning complex values at present");
346     else {
347       llvm::Optional<uint64_t> bit_width =
348           compiler_type.GetBitSize(frame_sp.get());
349       if (!bit_width) {
350         error.SetErrorString("can't get type size");
351         return error;
352       }
353       if (*bit_width <= 64) {
354         const RegisterInfo *xmm0_info =
355             reg_ctx->GetRegisterInfoByName("xmm0", 0);
356         RegisterValue xmm0_value;
357         DataExtractor data;
358         Status data_error;
359         size_t num_bytes = new_value_sp->GetData(data, data_error);
360         if (data_error.Fail()) {
361           error.SetErrorStringWithFormat(
362               "Couldn't convert return value to raw data: %s",
363               data_error.AsCString());
364           return error;
365         }
366 
367         unsigned char buffer[16];
368         ByteOrder byte_order = data.GetByteOrder();
369 
370         data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
371         xmm0_value.SetBytes(buffer, 16, byte_order);
372         reg_ctx->WriteRegister(xmm0_info, xmm0_value);
373         set_it_simple = true;
374       } else {
375         // Windows doesn't support 80 bit FP
376         error.SetErrorString(
377             "Windows-x86_64 doesn't allow FP larger than 64 bits.");
378       }
379     }
380   }
381 
382   if (!set_it_simple) {
383     // Okay we've got a structure or something that doesn't fit in a simple
384     // register.
385     // TODO(wanyi): On Windows, if the return type is a struct:
386     // 1) smaller that 64 bits and return by value -> RAX
387     // 2) bigger than 64 bits, the caller will allocate memory for that struct
388     // and pass the struct pointer in RCX then return the pointer in RAX
389     error.SetErrorString("We only support setting simple integer and float "
390                          "return types at present.");
391   }
392 
393   return error;
394 }
395 
396 ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectSimple(
397     Thread &thread, CompilerType &return_compiler_type) const {
398   ValueObjectSP return_valobj_sp;
399   Value value;
400 
401   if (!return_compiler_type)
402     return return_valobj_sp;
403 
404   value.SetCompilerType(return_compiler_type);
405 
406   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
407   if (!reg_ctx)
408     return return_valobj_sp;
409 
410   const uint32_t type_flags = return_compiler_type.GetTypeInfo();
411   if (type_flags & eTypeIsScalar) {
412     value.SetValueType(Value::ValueType::Scalar);
413 
414     bool success = false;
415     if (type_flags & eTypeIsInteger) {
416       // Extract the register context so we can read arguments from registers
417       llvm::Optional<uint64_t> byte_size =
418           return_compiler_type.GetByteSize(&thread);
419       if (!byte_size)
420         return return_valobj_sp;
421       uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
422           reg_ctx->GetRegisterInfoByName("rax", 0), 0);
423       const bool is_signed = (type_flags & eTypeIsSigned) != 0;
424       switch (*byte_size) {
425       default:
426         break;
427 
428       case sizeof(uint64_t):
429         if (is_signed)
430           value.GetScalar() = (int64_t)(raw_value);
431         else
432           value.GetScalar() = (uint64_t)(raw_value);
433         success = true;
434         break;
435 
436       case sizeof(uint32_t):
437         if (is_signed)
438           value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
439         else
440           value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
441         success = true;
442         break;
443 
444       case sizeof(uint16_t):
445         if (is_signed)
446           value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
447         else
448           value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
449         success = true;
450         break;
451 
452       case sizeof(uint8_t):
453         if (is_signed)
454           value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
455         else
456           value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
457         success = true;
458         break;
459       }
460     } else if (type_flags & eTypeIsFloat) {
461       if (type_flags & eTypeIsComplex) {
462         // Don't handle complex yet.
463       } else {
464         llvm::Optional<uint64_t> byte_size =
465             return_compiler_type.GetByteSize(&thread);
466         if (byte_size && *byte_size <= sizeof(long double)) {
467           const RegisterInfo *xmm0_info =
468               reg_ctx->GetRegisterInfoByName("xmm0", 0);
469           RegisterValue xmm0_value;
470           if (reg_ctx->ReadRegister(xmm0_info, xmm0_value)) {
471             DataExtractor data;
472             if (xmm0_value.GetData(data)) {
473               lldb::offset_t offset = 0;
474               if (*byte_size == sizeof(float)) {
475                 value.GetScalar() = (float)data.GetFloat(&offset);
476                 success = true;
477               } else if (*byte_size == sizeof(double)) {
478                 // double and long double are the same on windows
479                 value.GetScalar() = (double)data.GetDouble(&offset);
480                 success = true;
481               }
482             }
483           }
484         }
485       }
486     }
487 
488     if (success)
489       return_valobj_sp = ValueObjectConstResult::Create(
490           thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
491   } else if ((type_flags & eTypeIsPointer) ||
492              (type_flags & eTypeInstanceIsPointer)) {
493     unsigned rax_id =
494         reg_ctx->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
495     value.GetScalar() =
496         (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
497                                                                       0);
498     value.SetValueType(Value::ValueType::Scalar);
499     return_valobj_sp = ValueObjectConstResult::Create(
500         thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
501   } else if (type_flags & eTypeIsVector) {
502     llvm::Optional<uint64_t> byte_size =
503         return_compiler_type.GetByteSize(&thread);
504     if (byte_size && *byte_size > 0) {
505       const RegisterInfo *xmm_reg =
506           reg_ctx->GetRegisterInfoByName("xmm0", 0);
507       if (xmm_reg == nullptr)
508         xmm_reg = reg_ctx->GetRegisterInfoByName("mm0", 0);
509 
510       if (xmm_reg) {
511         if (*byte_size <= xmm_reg->byte_size) {
512           ProcessSP process_sp(thread.GetProcess());
513           if (process_sp) {
514             std::unique_ptr<DataBufferHeap> heap_data_up(
515                 new DataBufferHeap(*byte_size, 0));
516             const ByteOrder byte_order = process_sp->GetByteOrder();
517             RegisterValue reg_value;
518             if (reg_ctx->ReadRegister(xmm_reg, reg_value)) {
519               Status error;
520               if (reg_value.GetAsMemoryData(
521                       xmm_reg, heap_data_up->GetBytes(),
522                       heap_data_up->GetByteSize(), byte_order, error)) {
523                 DataExtractor data(DataBufferSP(heap_data_up.release()),
524                                    byte_order,
525                                    process_sp->GetTarget()
526                                        .GetArchitecture()
527                                        .GetAddressByteSize());
528                 return_valobj_sp = ValueObjectConstResult::Create(
529                     &thread, return_compiler_type, ConstString(""), data);
530               }
531             }
532           }
533         }
534       }
535     }
536   }
537 
538   return return_valobj_sp;
539 }
540 
541 // The compiler will flatten the nested aggregate type into single
542 // layer and push the value to stack
543 // This helper function will flatten an aggregate type
544 // and return true if it can be returned in register(s) by value
545 // return false if the aggregate is in memory
546 static bool FlattenAggregateType(
547     Thread &thread, ExecutionContext &exe_ctx,
548     CompilerType &return_compiler_type,
549     uint32_t data_byte_offset,
550     std::vector<uint32_t> &aggregate_field_offsets,
551     std::vector<CompilerType> &aggregate_compiler_types) {
552 
553   const uint32_t num_children = return_compiler_type.GetNumFields();
554   for (uint32_t idx = 0; idx < num_children; ++idx) {
555     std::string name;
556     bool is_signed;
557     uint32_t count;
558     bool is_complex;
559 
560     uint64_t field_bit_offset = 0;
561     CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(
562         idx, name, &field_bit_offset, nullptr, nullptr);
563     llvm::Optional<uint64_t> field_bit_width =
564           field_compiler_type.GetBitSize(&thread);
565 
566     // if we don't know the size of the field (e.g. invalid type), exit
567     if (!field_bit_width || *field_bit_width == 0) {
568       return false;
569     }
570     // If there are any unaligned fields, this is stored in memory.
571     if (field_bit_offset % *field_bit_width != 0) {
572       return false;
573     }
574 
575     // add overall offset
576     uint32_t field_byte_offset = field_bit_offset / 8 + data_byte_offset;
577 
578     const uint32_t field_type_flags = field_compiler_type.GetTypeInfo();
579     if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
580         field_compiler_type.IsPointerType() ||
581         field_compiler_type.IsFloatingPointType(count, is_complex)) {
582       aggregate_field_offsets.push_back(field_byte_offset);
583       aggregate_compiler_types.push_back(field_compiler_type);
584     } else if (field_type_flags & eTypeHasChildren) {
585       if (!FlattenAggregateType(thread, exe_ctx, field_compiler_type,
586                                 field_byte_offset, aggregate_field_offsets,
587                                 aggregate_compiler_types)) {
588         return false;
589       }
590     }
591   }
592   return true;
593 }
594 
595 ValueObjectSP ABIWindows_x86_64::GetReturnValueObjectImpl(
596     Thread &thread, CompilerType &return_compiler_type) const {
597   ValueObjectSP return_valobj_sp;
598 
599   if (!return_compiler_type) {
600     return return_valobj_sp;
601   }
602 
603   // try extract value as if it's a simple type
604   return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
605   if (return_valobj_sp) {
606     return return_valobj_sp;
607   }
608 
609   RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
610   if (!reg_ctx_sp) {
611     return return_valobj_sp;
612   }
613 
614   llvm::Optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread);
615   if (!bit_width) {
616     return return_valobj_sp;
617   }
618 
619   // if it's not simple or aggregate type, then we don't know how to handle it
620   if (!return_compiler_type.IsAggregateType()) {
621     return return_valobj_sp;
622   }
623 
624   ExecutionContext exe_ctx(thread.shared_from_this());
625   Target *target = exe_ctx.GetTargetPtr();
626   uint32_t max_register_value_bit_width = 64;
627 
628   // The scenario here is to have a struct/class which is POD
629   // if the return struct/class size is larger than 64 bits,
630   // the caller will allocate memory for it and pass the return addr in RCX
631   // then return the address in RAX
632 
633   // if the struct is returned by value in register (RAX)
634   // its size has to be: 1, 2, 4, 8, 16, 32, or 64 bits (aligned)
635   // for floating point, the return value will be copied over to RAX
636   bool is_memory = *bit_width > max_register_value_bit_width ||
637                    *bit_width & (*bit_width - 1);
638   std::vector<uint32_t> aggregate_field_offsets;
639   std::vector<CompilerType> aggregate_compiler_types;
640   if (!is_memory &&
641       FlattenAggregateType(thread, exe_ctx, return_compiler_type,
642                            0, aggregate_field_offsets,
643                            aggregate_compiler_types)) {
644     ByteOrder byte_order = target->GetArchitecture().GetByteOrder();
645     WritableDataBufferSP data_sp(
646         new DataBufferHeap(max_register_value_bit_width / 8, 0));
647     DataExtractor return_ext(data_sp, byte_order,
648         target->GetArchitecture().GetAddressByteSize());
649 
650     // The only register used to return struct/class by value
651     const RegisterInfo *rax_info =
652         reg_ctx_sp->GetRegisterInfoByName("rax", 0);
653     RegisterValue rax_value;
654     reg_ctx_sp->ReadRegister(rax_info, rax_value);
655     DataExtractor rax_data;
656     rax_value.GetData(rax_data);
657 
658     uint32_t used_bytes =
659         0; // Tracks how much of the rax registers we've consumed so far
660 
661     // in case of the returned type is a subclass of non-abstract-base class
662     // it will have a padding to skip the base content
663     if (aggregate_field_offsets.size())
664       used_bytes = aggregate_field_offsets[0];
665 
666     const uint32_t num_children = aggregate_compiler_types.size();
667     for (uint32_t idx = 0; idx < num_children; idx++) {
668       bool is_signed;
669       bool is_complex;
670       uint32_t count;
671 
672       CompilerType field_compiler_type = aggregate_compiler_types[idx];
673       uint32_t field_byte_width = (uint32_t) (*field_compiler_type.GetByteSize(&thread));
674       uint32_t field_byte_offset = aggregate_field_offsets[idx];
675 
676       // this is unlikely w/o the overall size being greater than 8 bytes
677       // For now, return a nullptr return value object.
678       if (used_bytes >= 8 || used_bytes + field_byte_width > 8) {
679         return return_valobj_sp;
680       }
681 
682       DataExtractor *copy_from_extractor = nullptr;
683       uint32_t copy_from_offset = 0;
684       if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
685           field_compiler_type.IsPointerType() ||
686           field_compiler_type.IsFloatingPointType(count, is_complex)) {
687         copy_from_extractor = &rax_data;
688         copy_from_offset = used_bytes;
689         used_bytes += field_byte_width;
690       }
691       // These two tests are just sanity checks.  If I somehow get the type
692       // calculation wrong above it is better to just return nothing than to
693       // assert or crash.
694       if (!copy_from_extractor) {
695         return return_valobj_sp;
696       }
697       if (copy_from_offset + field_byte_width >
698           copy_from_extractor->GetByteSize()) {
699         return return_valobj_sp;
700       }
701       copy_from_extractor->CopyByteOrderedData(copy_from_offset,
702           field_byte_width, data_sp->GetBytes() + field_byte_offset,
703           field_byte_width, byte_order);
704     }
705     if (!is_memory) {
706       // The result is in our data buffer.  Let's make a variable object out
707       // of it:
708       return_valobj_sp = ValueObjectConstResult::Create(
709           &thread, return_compiler_type, ConstString(""), return_ext);
710     }
711   }
712 
713   // The Windows x86_64 ABI specifies that the return address for MEMORY
714   // objects be placed in rax on exit from the function.
715 
716   // FIXME: This is just taking a guess, rax may very well no longer hold the
717   // return storage location.
718   // If we are going to do this right, when we make a new frame we should
719   // check to see if it uses a memory return, and if we are at the first
720   // instruction and if so stash away the return location.  Then we would
721   // only return the memory return value if we know it is valid.
722   if (is_memory) {
723     unsigned rax_id =
724         reg_ctx_sp->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
725     lldb::addr_t storage_addr =
726         (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
727                                                                       0);
728     return_valobj_sp = ValueObjectMemory::Create(
729         &thread, "", Address(storage_addr, nullptr), return_compiler_type);
730   }
731   return return_valobj_sp;
732 }
733 
734 // This defines the CFA as rsp+8
735 // the saved pc is at CFA-8 (i.e. rsp+0)
736 // The saved rsp is CFA+0
737 
738 bool ABIWindows_x86_64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
739   unwind_plan.Clear();
740   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
741 
742   uint32_t sp_reg_num = dwarf_rsp;
743   uint32_t pc_reg_num = dwarf_rip;
744 
745   UnwindPlan::RowSP row(new UnwindPlan::Row);
746   row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 8);
747   row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, -8, false);
748   row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
749   unwind_plan.AppendRow(row);
750   unwind_plan.SetSourceName("x86_64 at-func-entry default");
751   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
752   return true;
753 }
754 
755 // Windows-x86_64 doesn't use %rbp
756 // No available Unwind information for Windows-x86_64 (section .pdata)
757 // Let's use SysV-x86_64 one for now
758 bool ABIWindows_x86_64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
759   unwind_plan.Clear();
760   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
761 
762   uint32_t fp_reg_num = dwarf_rbp;
763   uint32_t sp_reg_num = dwarf_rsp;
764   uint32_t pc_reg_num = dwarf_rip;
765 
766   UnwindPlan::RowSP row(new UnwindPlan::Row);
767 
768   const int32_t ptr_size = 8;
769   row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_rbp, 2 * ptr_size);
770   row->SetOffset(0);
771   row->SetUnspecifiedRegistersAreUndefined(true);
772 
773   row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true);
774   row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true);
775   row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
776 
777   unwind_plan.AppendRow(row);
778   unwind_plan.SetSourceName("x86_64 default unwind plan");
779   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
780   unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
781 
782   return true;
783 }
784 
785 bool ABIWindows_x86_64::RegisterIsVolatile(const RegisterInfo *reg_info) {
786   return !RegisterIsCalleeSaved(reg_info);
787 }
788 
789 bool ABIWindows_x86_64::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
790   if (!reg_info)
791     return false;
792   assert(reg_info->name != nullptr && "unnamed register?");
793   std::string Name = std::string(reg_info->name);
794   bool IsCalleeSaved =
795       llvm::StringSwitch<bool>(Name)
796           .Cases("rbx", "ebx", "rbp", "ebp", "rdi", "edi", "rsi", "esi", true)
797           .Cases("rsp", "esp", "r12", "r13", "r14", "r15", "sp", "fp", true)
798           .Cases("xmm6", "xmm7", "xmm8", "xmm9", "xmm10", "xmm11", "xmm12",
799                  "xmm13", "xmm14", "xmm15", true)
800           .Default(false);
801   return IsCalleeSaved;
802 }
803 
804 uint32_t ABIWindows_x86_64::GetGenericNum(llvm::StringRef reg) {
805   return llvm::StringSwitch<uint32_t>(reg)
806       .Case("rip", LLDB_REGNUM_GENERIC_PC)
807       .Case("rsp", LLDB_REGNUM_GENERIC_SP)
808       .Case("rbp", LLDB_REGNUM_GENERIC_FP)
809       .Case("rflags", LLDB_REGNUM_GENERIC_FLAGS)
810       // gdbserver uses eflags
811       .Case("eflags", LLDB_REGNUM_GENERIC_FLAGS)
812       .Case("rcx", LLDB_REGNUM_GENERIC_ARG1)
813       .Case("rdx", LLDB_REGNUM_GENERIC_ARG2)
814       .Case("r8", LLDB_REGNUM_GENERIC_ARG3)
815       .Case("r9", LLDB_REGNUM_GENERIC_ARG4)
816       .Default(LLDB_INVALID_REGNUM);
817 }
818 
819 void ABIWindows_x86_64::Initialize() {
820   PluginManager::RegisterPlugin(
821       GetPluginNameStatic(), "Windows ABI for x86_64 targets", CreateInstance);
822 }
823 
824 void ABIWindows_x86_64::Terminate() {
825   PluginManager::UnregisterPlugin(CreateInstance);
826 }
827