LLDB mainline
ABISysV_x86_64.cpp
Go to the documentation of this file.
1//===-- ABISysV_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 "ABISysV_x86_64.h"
10
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/StringSwitch.h"
13#include "llvm/TargetParser/Triple.h"
14
15#include "lldb/Core/Module.h"
17#include "lldb/Core/Value.h"
19#include "lldb/Target/Process.h"
22#include "lldb/Target/Target.h"
23#include "lldb/Target/Thread.h"
27#include "lldb/Utility/Log.h"
29#include "lldb/Utility/Status.h"
33
34#include <optional>
35#include <vector>
36
37using namespace lldb;
38using namespace lldb_private;
39
41
60};
61
63 name = "rax";
64 return true;
65}
66
67size_t ABISysV_x86_64::GetRedZoneSize() const { return 128; }
68
69// Static Functions
70
73 const llvm::Triple::ArchType arch_type = arch.GetTriple().getArch();
74 const llvm::Triple::OSType os_type = arch.GetTriple().getOS();
75 const llvm::Triple::EnvironmentType os_env =
76 arch.GetTriple().getEnvironment();
77 if (arch_type == llvm::Triple::x86_64) {
78 switch(os_type) {
79 case llvm::Triple::OSType::IOS:
80 case llvm::Triple::OSType::TvOS:
81 case llvm::Triple::OSType::WatchOS:
82 switch (os_env) {
83 case llvm::Triple::EnvironmentType::MacABI:
84 case llvm::Triple::EnvironmentType::Simulator:
85 case llvm::Triple::EnvironmentType::UnknownEnvironment:
86 // UnknownEnvironment is needed for older compilers that don't
87 // support the simulator environment.
88 return ABISP(new ABISysV_x86_64(std::move(process_sp),
89 MakeMCRegisterInfo(arch)));
90 default:
91 return ABISP();
92 }
93 case llvm::Triple::OSType::Darwin:
94 case llvm::Triple::OSType::FreeBSD:
95 case llvm::Triple::OSType::Linux:
96 case llvm::Triple::OSType::MacOSX:
97 case llvm::Triple::OSType::NetBSD:
98 case llvm::Triple::OSType::OpenBSD:
99 case llvm::Triple::OSType::Solaris:
100 case llvm::Triple::OSType::UnknownOS:
101 return ABISP(
102 new ABISysV_x86_64(std::move(process_sp), MakeMCRegisterInfo(arch)));
103 default:
104 return ABISP();
105 }
106 }
107 return ABISP();
108}
109
111 addr_t func_addr, addr_t return_addr,
112 llvm::ArrayRef<addr_t> args) const {
113 Log *log = GetLog(LLDBLog::Expressions);
114
115 if (log) {
116 StreamString s;
117 s.Printf("ABISysV_x86_64::PrepareTrivialCall (tid = 0x%" PRIx64
118 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
119 ", return_addr = 0x%" PRIx64,
120 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
121 (uint64_t)return_addr);
122
123 for (size_t i = 0; i < args.size(); ++i)
124 s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
125 args[i]);
126 s.PutCString(")");
127 log->PutString(s.GetString());
128 }
129
130 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
131 if (!reg_ctx)
132 return false;
133
134 const RegisterInfo *reg_info = nullptr;
135
136 if (args.size() > 6) // TODO handle more than 6 arguments
137 return false;
138
139 for (size_t i = 0; i < args.size(); ++i) {
140 reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
142 LLDB_LOGF(log, "About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
143 static_cast<uint64_t>(i + 1), args[i], reg_info->name);
144 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
145 return false;
146 }
147
148 // First, align the SP
149
150 LLDB_LOGF(log, "16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
151 (uint64_t)sp, (uint64_t)(sp & ~0xfull));
152
153 sp &= ~(0xfull); // 16-byte alignment
154
155 sp -= 8;
156
158 const RegisterInfo *pc_reg_info =
160 const RegisterInfo *sp_reg_info =
162 ProcessSP process_sp(thread.GetProcess());
163
164 RegisterValue reg_value;
165 LLDB_LOGF(log,
166 "Pushing the return address onto the stack: 0x%" PRIx64
167 ": 0x%" PRIx64,
168 (uint64_t)sp, (uint64_t)return_addr);
169
170 // Save return address onto the stack
171 if (!process_sp->WritePointerToMemory(sp, return_addr, error))
172 return false;
173
174 // %rsp is set to the actual stack value.
175
176 LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp);
177
178 if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
179 return false;
180
181 // %rip is set to the address of the called function.
182
183 LLDB_LOGF(log, "Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
184
185 if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
186 return false;
187
188 return true;
189}
190
191static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
192 bool is_signed, Thread &thread,
193 uint32_t *argument_register_ids,
194 unsigned int &current_argument_register,
195 addr_t &current_stack_argument) {
196 if (bit_width > 64)
197 return false; // Scalar can't hold large integer arguments
198
199 if (current_argument_register < 6) {
200 scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
201 argument_register_ids[current_argument_register], 0);
202 current_argument_register++;
203 if (is_signed)
204 scalar.SignExtend(bit_width);
205 } else {
206 uint32_t byte_size = (bit_width + (8 - 1)) / 8;
208 if (thread.GetProcess()->ReadScalarIntegerFromMemory(
209 current_stack_argument, byte_size, is_signed, scalar, error)) {
210 current_stack_argument += byte_size;
211 return true;
212 }
213 return false;
214 }
215 return true;
216}
217
219 ValueList &values) const {
220 unsigned int num_values = values.GetSize();
221 unsigned int value_index;
222
223 // Extract the register context so we can read arguments from registers
224
225 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
226
227 if (!reg_ctx)
228 return false;
229
230 // Get the pointer to the first stack argument so we have a place to start
231 // when reading data
232
233 addr_t sp = reg_ctx->GetSP(0);
234
235 if (!sp)
236 return false;
237
238 addr_t current_stack_argument = sp + 8; // jump over return address
239
240 uint32_t argument_register_ids[6];
241
242 argument_register_ids[0] =
245 argument_register_ids[1] =
248 argument_register_ids[2] =
251 argument_register_ids[3] =
254 argument_register_ids[4] =
257 argument_register_ids[5] =
260
261 unsigned int current_argument_register = 0;
262
263 for (value_index = 0; value_index < num_values; ++value_index) {
264 Value *value = values.GetValueAtIndex(value_index);
265
266 if (!value)
267 return false;
268
269 // We currently only support extracting values with Clang QualTypes. Do we
270 // care about others?
271 CompilerType compiler_type = value->GetCompilerType();
272 std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread);
273 if (!bit_size)
274 return false;
275 bool is_signed;
276
277 if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
278 ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread,
279 argument_register_ids, current_argument_register,
280 current_stack_argument);
281 } else if (compiler_type.IsPointerType()) {
282 ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread,
283 argument_register_ids, current_argument_register,
284 current_stack_argument);
285 }
286 }
287
288 return true;
289}
290
292 lldb::ValueObjectSP &new_value_sp) {
294 if (!new_value_sp) {
295 error = Status::FromErrorString("Empty value object for return value.");
296 return error;
297 }
298
299 CompilerType compiler_type = new_value_sp->GetCompilerType();
300 if (!compiler_type) {
301 error = Status::FromErrorString("Null clang type for return value.");
302 return error;
303 }
304
305 Thread *thread = frame_sp->GetThread().get();
306
307 bool is_signed;
308 uint32_t count;
309 bool is_complex;
310
311 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
312
313 bool set_it_simple = false;
314 if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
315 compiler_type.IsPointerType()) {
316 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("rax", 0);
317
318 DataExtractor data;
319 Status data_error;
320 size_t num_bytes = new_value_sp->GetData(data, data_error);
321 if (data_error.Fail()) {
323 "Couldn't convert return value to raw data: %s",
324 data_error.AsCString());
325 return error;
326 }
327 lldb::offset_t offset = 0;
328 if (num_bytes <= 8) {
329 uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
330
331 if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
332 set_it_simple = true;
333 } else {
335 "We don't support returning longer than 64 bit "
336 "integer values at present.");
337 }
338 } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
339 if (is_complex)
341 "We don't support returning complex values at present");
342 else {
343 std::optional<uint64_t> bit_width =
344 compiler_type.GetBitSize(frame_sp.get());
345 if (!bit_width) {
346 error = Status::FromErrorString("can't get type size");
347 return error;
348 }
349 if (*bit_width <= 64) {
350 const RegisterInfo *xmm0_info =
351 reg_ctx->GetRegisterInfoByName("xmm0", 0);
352 RegisterValue xmm0_value;
353 DataExtractor data;
354 Status data_error;
355 size_t num_bytes = new_value_sp->GetData(data, data_error);
356 if (data_error.Fail()) {
358 "Couldn't convert return value to raw data: %s",
359 data_error.AsCString());
360 return error;
361 }
362
363 unsigned char buffer[16];
364 ByteOrder byte_order = data.GetByteOrder();
365
366 data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
367 xmm0_value.SetBytes(buffer, 16, byte_order);
368 reg_ctx->WriteRegister(xmm0_info, xmm0_value);
369 set_it_simple = true;
370 } else {
371 // FIXME - don't know how to do 80 bit long doubles yet.
373 "We don't support returning float values > 64 bits at present");
374 }
375 }
376 }
377
378 if (!set_it_simple) {
379 // Okay we've got a structure or something that doesn't fit in a simple
380 // register. We should figure out where it really goes, but we don't
381 // support this yet.
383 "We only support setting simple integer and float "
384 "return types at present.");
385 }
386
387 return error;
388}
389
391 Thread &thread, CompilerType &return_compiler_type) const {
392 ValueObjectSP return_valobj_sp;
393 Value value;
394
395 if (!return_compiler_type)
396 return return_valobj_sp;
397
398 // value.SetContext (Value::eContextTypeClangType, return_value_type);
399 value.SetCompilerType(return_compiler_type);
400
401 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
402 if (!reg_ctx)
403 return return_valobj_sp;
404
405 const uint32_t type_flags = return_compiler_type.GetTypeInfo();
406 if (type_flags & eTypeIsScalar) {
407 value.SetValueType(Value::ValueType::Scalar);
408
409 bool success = false;
410 if (type_flags & eTypeIsInteger) {
411 // Extract the register context so we can read arguments from registers
412
413 std::optional<uint64_t> byte_size =
414 return_compiler_type.GetByteSize(&thread);
415 if (!byte_size)
416 return return_valobj_sp;
417 uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
418 reg_ctx->GetRegisterInfoByName("rax", 0), 0);
419 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
420 switch (*byte_size) {
421 default:
422 break;
423
424 case sizeof(uint64_t):
425 if (is_signed)
426 value.GetScalar() = (int64_t)(raw_value);
427 else
428 value.GetScalar() = (uint64_t)(raw_value);
429 success = true;
430 break;
431
432 case sizeof(uint32_t):
433 if (is_signed)
434 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
435 else
436 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
437 success = true;
438 break;
439
440 case sizeof(uint16_t):
441 if (is_signed)
442 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
443 else
444 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
445 success = true;
446 break;
447
448 case sizeof(uint8_t):
449 if (is_signed)
450 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
451 else
452 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
453 success = true;
454 break;
455 }
456 } else if (type_flags & eTypeIsFloat) {
457 if (type_flags & eTypeIsComplex) {
458 // Don't handle complex yet.
459 } else {
460 std::optional<uint64_t> byte_size =
461 return_compiler_type.GetByteSize(&thread);
462 if (byte_size && *byte_size <= sizeof(long double)) {
463 const RegisterInfo *xmm0_info =
464 reg_ctx->GetRegisterInfoByName("xmm0", 0);
465 RegisterValue xmm0_value;
466 if (reg_ctx->ReadRegister(xmm0_info, xmm0_value)) {
467 DataExtractor data;
468 if (xmm0_value.GetData(data)) {
469 lldb::offset_t offset = 0;
470 if (*byte_size == sizeof(float)) {
471 value.GetScalar() = (float)data.GetFloat(&offset);
472 success = true;
473 } else if (*byte_size == sizeof(double)) {
474 value.GetScalar() = (double)data.GetDouble(&offset);
475 success = true;
476 } else if (*byte_size == sizeof(long double)) {
477 // Don't handle long double since that can be encoded as 80 bit
478 // floats...
479 }
480 }
481 }
482 }
483 }
484 }
485
486 if (success)
487 return_valobj_sp = ValueObjectConstResult::Create(
488 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
489 } else if (type_flags & eTypeIsPointer) {
490 unsigned rax_id =
491 reg_ctx->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
492 value.GetScalar() =
493 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
494 0);
495 value.SetValueType(Value::ValueType::Scalar);
496 return_valobj_sp = ValueObjectConstResult::Create(
497 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
498 } else if (type_flags & eTypeIsVector) {
499 std::optional<uint64_t> byte_size =
500 return_compiler_type.GetByteSize(&thread);
501 if (byte_size && *byte_size > 0) {
502 const RegisterInfo *altivec_reg =
503 reg_ctx->GetRegisterInfoByName("xmm0", 0);
504 if (altivec_reg == nullptr)
505 altivec_reg = reg_ctx->GetRegisterInfoByName("mm0", 0);
506
507 if (altivec_reg) {
508 if (*byte_size <= altivec_reg->byte_size) {
509 ProcessSP process_sp(thread.GetProcess());
510 if (process_sp) {
511 std::unique_ptr<DataBufferHeap> heap_data_up(
512 new DataBufferHeap(*byte_size, 0));
513 const ByteOrder byte_order = process_sp->GetByteOrder();
514 RegisterValue reg_value;
515 if (reg_ctx->ReadRegister(altivec_reg, reg_value)) {
517 if (reg_value.GetAsMemoryData(
518 *altivec_reg, heap_data_up->GetBytes(),
519 heap_data_up->GetByteSize(), byte_order, error)) {
520 DataExtractor data(DataBufferSP(heap_data_up.release()),
521 byte_order,
522 process_sp->GetTarget()
523 .GetArchitecture()
524 .GetAddressByteSize());
525 return_valobj_sp = ValueObjectConstResult::Create(
526 &thread, return_compiler_type, ConstString(""), data);
527 }
528 }
529 }
530 } else if (*byte_size <= altivec_reg->byte_size * 2) {
531 const RegisterInfo *altivec_reg2 =
532 reg_ctx->GetRegisterInfoByName("xmm1", 0);
533 if (altivec_reg2) {
534 ProcessSP process_sp(thread.GetProcess());
535 if (process_sp) {
536 std::unique_ptr<DataBufferHeap> heap_data_up(
537 new DataBufferHeap(*byte_size, 0));
538 const ByteOrder byte_order = process_sp->GetByteOrder();
539 RegisterValue reg_value;
540 RegisterValue reg_value2;
541 if (reg_ctx->ReadRegister(altivec_reg, reg_value) &&
542 reg_ctx->ReadRegister(altivec_reg2, reg_value2)) {
543
545 if (reg_value.GetAsMemoryData(
546 *altivec_reg, heap_data_up->GetBytes(),
547 altivec_reg->byte_size, byte_order, error) &&
548 reg_value2.GetAsMemoryData(
549 *altivec_reg2,
550 heap_data_up->GetBytes() + altivec_reg->byte_size,
551 heap_data_up->GetByteSize() - altivec_reg->byte_size,
552 byte_order, error)) {
553 DataExtractor data(DataBufferSP(heap_data_up.release()),
554 byte_order,
555 process_sp->GetTarget()
556 .GetArchitecture()
557 .GetAddressByteSize());
558 return_valobj_sp = ValueObjectConstResult::Create(
559 &thread, return_compiler_type, ConstString(""), data);
560 }
561 }
562 }
563 }
564 }
565 }
566 }
567 }
568
569 return return_valobj_sp;
570}
571
572// The compiler will flatten the nested aggregate type into single
573// layer and push the value to stack
574// This helper function will flatten an aggregate type
575// and return true if it can be returned in register(s) by value
576// return false if the aggregate is in memory
578 Thread &thread, ExecutionContext &exe_ctx,
579 CompilerType &return_compiler_type,
580 uint32_t data_byte_offset,
581 std::vector<uint32_t> &aggregate_field_offsets,
582 std::vector<CompilerType> &aggregate_compiler_types) {
583
584 const uint32_t num_children = return_compiler_type.GetNumFields();
585 for (uint32_t idx = 0; idx < num_children; ++idx) {
586 std::string name;
587 bool is_signed;
588 uint32_t count;
589 bool is_complex;
590
591 uint64_t field_bit_offset = 0;
592 CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(
593 idx, name, &field_bit_offset, nullptr, nullptr);
594 std::optional<uint64_t> field_bit_width =
595 field_compiler_type.GetBitSize(&thread);
596
597 // if we don't know the size of the field (e.g. invalid type), exit
598 if (!field_bit_width || *field_bit_width == 0) {
599 return false;
600 }
601
602 uint32_t field_byte_offset = field_bit_offset / 8 + data_byte_offset;
603
604 const uint32_t field_type_flags = field_compiler_type.GetTypeInfo();
605 if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
606 field_compiler_type.IsPointerType() ||
607 field_compiler_type.IsFloatingPointType(count, is_complex)) {
608 aggregate_field_offsets.push_back(field_byte_offset);
609 aggregate_compiler_types.push_back(field_compiler_type);
610 } else if (field_type_flags & eTypeHasChildren) {
611 if (!FlattenAggregateType(thread, exe_ctx, field_compiler_type,
612 field_byte_offset, aggregate_field_offsets,
613 aggregate_compiler_types)) {
614 return false;
615 }
616 }
617 }
618 return true;
619}
620
622 Thread &thread, CompilerType &return_compiler_type) const {
623 ValueObjectSP return_valobj_sp;
624
625 if (!return_compiler_type)
626 return return_valobj_sp;
627
628 ExecutionContext exe_ctx(thread.shared_from_this());
629 return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
630 if (return_valobj_sp)
631 return return_valobj_sp;
632
633 RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
634 if (!reg_ctx_sp)
635 return return_valobj_sp;
636
637 std::optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread);
638 if (!bit_width)
639 return return_valobj_sp;
640 if (return_compiler_type.IsAggregateType()) {
641 Target *target = exe_ctx.GetTargetPtr();
642 bool is_memory = true;
643 std::vector<uint32_t> aggregate_field_offsets;
644 std::vector<CompilerType> aggregate_compiler_types;
645 auto ts = return_compiler_type.GetTypeSystem();
646 if (ts && ts->CanPassInRegisters(return_compiler_type) &&
647 *bit_width <= 128 &&
648 FlattenAggregateType(thread, exe_ctx, return_compiler_type, 0,
649 aggregate_field_offsets,
650 aggregate_compiler_types)) {
651 ByteOrder byte_order = target->GetArchitecture().GetByteOrder();
652 WritableDataBufferSP data_sp(new DataBufferHeap(16, 0));
653 DataExtractor return_ext(data_sp, byte_order,
655
656 const RegisterInfo *rax_info =
657 reg_ctx_sp->GetRegisterInfoByName("rax", 0);
658 const RegisterInfo *rdx_info =
659 reg_ctx_sp->GetRegisterInfoByName("rdx", 0);
660 const RegisterInfo *xmm0_info =
661 reg_ctx_sp->GetRegisterInfoByName("xmm0", 0);
662 const RegisterInfo *xmm1_info =
663 reg_ctx_sp->GetRegisterInfoByName("xmm1", 0);
664
665 RegisterValue rax_value, rdx_value, xmm0_value, xmm1_value;
666 reg_ctx_sp->ReadRegister(rax_info, rax_value);
667 reg_ctx_sp->ReadRegister(rdx_info, rdx_value);
668 reg_ctx_sp->ReadRegister(xmm0_info, xmm0_value);
669 reg_ctx_sp->ReadRegister(xmm1_info, xmm1_value);
670
671 DataExtractor rax_data, rdx_data, xmm0_data, xmm1_data;
672
673 rax_value.GetData(rax_data);
674 rdx_value.GetData(rdx_data);
675 xmm0_value.GetData(xmm0_data);
676 xmm1_value.GetData(xmm1_data);
677
678 uint32_t fp_bytes =
679 0; // Tracks how much of the xmm registers we've consumed so far
680 uint32_t integer_bytes =
681 0; // Tracks how much of the rax/rds registers we've consumed so far
682
683 // in case of the returned type is a subclass of non-abstract-base class
684 // it will have a padding to skip the base content
685 if (aggregate_field_offsets.size()) {
686 fp_bytes = aggregate_field_offsets[0];
687 integer_bytes = aggregate_field_offsets[0];
688 }
689
690 const uint32_t num_children = aggregate_compiler_types.size();
691
692 // Since we are in the small struct regime, assume we are not in memory.
693 is_memory = false;
694 for (uint32_t idx = 0; idx < num_children; idx++) {
695 bool is_signed;
696 uint32_t count;
697 bool is_complex;
698
699 CompilerType field_compiler_type = aggregate_compiler_types[idx];
700 uint32_t field_byte_width = (uint32_t) (*field_compiler_type.GetByteSize(&thread));
701 uint32_t field_byte_offset = aggregate_field_offsets[idx];
702
703 uint32_t field_bit_width = field_byte_width * 8;
704
705 DataExtractor *copy_from_extractor = nullptr;
706 uint32_t copy_from_offset = 0;
707
708 if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
709 field_compiler_type.IsPointerType()) {
710 if (integer_bytes < 8) {
711 if (integer_bytes + field_byte_width <= 8) {
712 // This is in RAX, copy from register to our result structure:
713 copy_from_extractor = &rax_data;
714 copy_from_offset = integer_bytes;
715 integer_bytes += field_byte_width;
716 } else {
717 // The next field wouldn't fit in the remaining space, so we
718 // pushed it to rdx.
719 copy_from_extractor = &rdx_data;
720 copy_from_offset = 0;
721 integer_bytes = 8 + field_byte_width;
722 }
723 } else if (integer_bytes + field_byte_width <= 16) {
724 copy_from_extractor = &rdx_data;
725 copy_from_offset = integer_bytes - 8;
726 integer_bytes += field_byte_width;
727 } else {
728 // The last field didn't fit. I can't see how that would happen
729 // w/o the overall size being greater than 16 bytes. For now,
730 // return a nullptr return value object.
731 return return_valobj_sp;
732 }
733 } else if (field_compiler_type.IsFloatingPointType(count, is_complex)) {
734 // Structs with long doubles are always passed in memory.
735 if (field_bit_width == 128) {
736 is_memory = true;
737 break;
738 } else if (field_bit_width == 64) {
739 // These have to be in a single xmm register.
740 if (fp_bytes == 0)
741 copy_from_extractor = &xmm0_data;
742 else
743 copy_from_extractor = &xmm1_data;
744
745 copy_from_offset = 0;
746 fp_bytes += field_byte_width;
747 } else if (field_bit_width == 32) {
748 // This one is kind of complicated. If we are in an "eightbyte"
749 // with another float, we'll be stuffed into an xmm register with
750 // it. If we are in an "eightbyte" with one or more ints, then we
751 // will be stuffed into the appropriate GPR with them.
752 bool in_gpr;
753 if (field_byte_offset % 8 == 0) {
754 // We are at the beginning of one of the eightbytes, so check the
755 // next element (if any)
756 if (idx == num_children - 1) {
757 in_gpr = false;
758 } else {
759 CompilerType next_field_compiler_type =
760 aggregate_compiler_types[idx + 1];
761 if (next_field_compiler_type.IsIntegerOrEnumerationType(
762 is_signed)) {
763 in_gpr = true;
764 } else {
765 copy_from_offset = 0;
766 in_gpr = false;
767 }
768 }
769 } else if (field_byte_offset % 4 == 0) {
770 // We are inside of an eightbyte, so see if the field before us
771 // is floating point: This could happen if somebody put padding
772 // in the structure.
773 if (idx == 0) {
774 in_gpr = false;
775 } else {
776 CompilerType prev_field_compiler_type =
777 aggregate_compiler_types[idx - 1];
778 if (prev_field_compiler_type.IsIntegerOrEnumerationType(
779 is_signed)) {
780 in_gpr = true;
781 } else {
782 copy_from_offset = 4;
783 in_gpr = false;
784 }
785 }
786 } else {
787 is_memory = true;
788 continue;
789 }
790
791 // Okay, we've figured out whether we are in GPR or XMM, now figure
792 // out which one.
793 if (in_gpr) {
794 if (integer_bytes < 8) {
795 // This is in RAX, copy from register to our result structure:
796 copy_from_extractor = &rax_data;
797 copy_from_offset = integer_bytes;
798 integer_bytes += field_byte_width;
799 } else {
800 copy_from_extractor = &rdx_data;
801 copy_from_offset = integer_bytes - 8;
802 integer_bytes += field_byte_width;
803 }
804 } else {
805 if (fp_bytes < 8)
806 copy_from_extractor = &xmm0_data;
807 else
808 copy_from_extractor = &xmm1_data;
809
810 fp_bytes += field_byte_width;
811 }
812 }
813 }
814 // These two tests are just sanity checks. If I somehow get the type
815 // calculation wrong above it is better to just return nothing than to
816 // assert or crash.
817 if (!copy_from_extractor)
818 return return_valobj_sp;
819 if (copy_from_offset + field_byte_width >
820 copy_from_extractor->GetByteSize())
821 return return_valobj_sp;
822 copy_from_extractor->CopyByteOrderedData(
823 copy_from_offset, field_byte_width,
824 data_sp->GetBytes() + field_byte_offset, field_byte_width,
825 byte_order);
826 }
827 if (!is_memory) {
828 // The result is in our data buffer. Let's make a variable object out
829 // of it:
830 return_valobj_sp = ValueObjectConstResult::Create(
831 &thread, return_compiler_type, ConstString(""), return_ext);
832 }
833 }
834
835 // FIXME: This is just taking a guess, rax may very well no longer hold the
836 // return storage location.
837 // If we are going to do this right, when we make a new frame we should
838 // check to see if it uses a memory return, and if we are at the first
839 // instruction and if so stash away the return location. Then we would
840 // only return the memory return value if we know it is valid.
841
842 if (is_memory) {
843 unsigned rax_id =
844 reg_ctx_sp->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
845 lldb::addr_t storage_addr =
846 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
847 0);
848 return_valobj_sp = ValueObjectMemory::Create(
849 &thread, "", Address(storage_addr, nullptr), return_compiler_type);
850 }
851 }
852
853 return return_valobj_sp;
854}
855
856// This defines the CFA as rsp+8
857// the saved pc is at CFA-8 (i.e. rsp+0)
858// The saved rsp is CFA+0
859
861 unwind_plan.Clear();
863
864 uint32_t sp_reg_num = dwarf_rsp;
865 uint32_t pc_reg_num = dwarf_rip;
866
868 row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 8);
869 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, -8, false);
870 row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
871 unwind_plan.AppendRow(row);
872 unwind_plan.SetSourceName("x86_64 at-func-entry default");
874 return true;
875}
876
877// This defines the CFA as rbp+16
878// The saved pc is at CFA-8 (i.e. rbp+8)
879// The saved rbp is at CFA-16 (i.e. rbp+0)
880// The saved rsp is CFA+0
881
883 unwind_plan.Clear();
885
886 uint32_t fp_reg_num = dwarf_rbp;
887 uint32_t sp_reg_num = dwarf_rsp;
888 uint32_t pc_reg_num = dwarf_rip;
889
891
892 const int32_t ptr_size = 8;
893 row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_rbp, 2 * ptr_size);
894 row->SetOffset(0);
895 row->SetUnspecifiedRegistersAreUndefined(true);
896
897 row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true);
898 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true);
899 row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
900
901 unwind_plan.AppendRow(row);
902 unwind_plan.SetSourceName("x86_64 default unwind plan");
906 return true;
907}
908
910 return !RegisterIsCalleeSaved(reg_info);
911}
912
913// See "Register Usage" in the
914// "System V Application Binary Interface"
915// "AMD64 Architecture Processor Supplement" (or "x86-64(tm) Architecture
916// Processor Supplement" in earlier revisions) (this doc is also commonly
917// referred to as the x86-64/AMD64 psABI) Edited by Michael Matz, Jan Hubicka,
918// Andreas Jaeger, and Mark Mitchell current version is 0.99.6 released
919// 2012-07-02 at http://refspecs.linuxfoundation.org/elf/x86-64-abi-0.99.pdf
920// It's being revised & updated at https://github.com/hjl-tools/x86-psABI/
921
923 if (!reg_info)
924 return false;
925 assert(reg_info->name != nullptr && "unnamed register?");
926 std::string Name = std::string(reg_info->name);
927 bool IsCalleeSaved =
928 llvm::StringSwitch<bool>(Name)
929 .Cases("r12", "r13", "r14", "r15", "rbp", "ebp", "rbx", "ebx", true)
930 .Cases("rip", "eip", "rsp", "esp", "sp", "fp", "pc", true)
931 .Default(false);
932 return IsCalleeSaved;
933}
934
935uint32_t ABISysV_x86_64::GetGenericNum(llvm::StringRef name) {
936 return llvm::StringSwitch<uint32_t>(name)
937 .Case("rip", LLDB_REGNUM_GENERIC_PC)
938 .Case("rsp", LLDB_REGNUM_GENERIC_SP)
939 .Case("rbp", LLDB_REGNUM_GENERIC_FP)
940 .Case("rflags", LLDB_REGNUM_GENERIC_FLAGS)
941 // gdbserver uses eflags
942 .Case("eflags", LLDB_REGNUM_GENERIC_FLAGS)
943 .Case("rdi", LLDB_REGNUM_GENERIC_ARG1)
944 .Case("rsi", LLDB_REGNUM_GENERIC_ARG2)
945 .Case("rdx", LLDB_REGNUM_GENERIC_ARG3)
946 .Case("rcx", LLDB_REGNUM_GENERIC_ARG4)
947 .Case("r8", LLDB_REGNUM_GENERIC_ARG5)
948 .Case("r9", LLDB_REGNUM_GENERIC_ARG6)
949 .Default(LLDB_INVALID_REGNUM);
950}
951
954 GetPluginNameStatic(), "System V ABI for x86_64 targets", CreateInstance);
955}
956
959}
dwarf_regnums
static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width, bool is_signed, Thread &thread, uint32_t *argument_register_ids, unsigned int &current_argument_register, addr_t &current_stack_argument)
static bool FlattenAggregateType(Thread &thread, ExecutionContext &exe_ctx, CompilerType &return_compiler_type, uint32_t data_byte_offset, std::vector< uint32_t > &aggregate_field_offsets, std::vector< CompilerType > &aggregate_compiler_types)
@ dwarf_rdi
@ dwarf_r12
@ dwarf_r13
@ dwarf_r8
@ dwarf_r11
@ dwarf_r9
@ dwarf_rcx
@ dwarf_rip
@ dwarf_rsi
@ dwarf_r15
@ dwarf_rax
@ dwarf_rsp
@ dwarf_rbp
@ dwarf_r10
@ dwarf_r14
@ dwarf_rdx
@ dwarf_rbx
static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width, bool is_signed, Thread &thread, uint32_t *argument_register_ids, unsigned int &current_argument_register, addr_t &current_stack_argument)
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:376
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:32
bool GetArgumentValues(lldb_private::Thread &thread, lldb_private::ValueList &values) const override
static void Terminate()
bool CreateDefaultUnwindPlan(lldb_private::UnwindPlan &unwind_plan) override
static lldb::ABISP CreateInstance(lldb::ProcessSP process_sp, const lldb_private::ArchSpec &arch)
bool RegisterIsCalleeSaved(const lldb_private::RegisterInfo *reg_info)
bool GetPointerReturnRegister(const char *&name) override
lldb_private::Status SetReturnValueObject(lldb::StackFrameSP &frame_sp, lldb::ValueObjectSP &new_value) override
uint32_t GetGenericNum(llvm::StringRef reg) override
Return the generic number of the given register.
lldb::ValueObjectSP GetReturnValueObjectImpl(lldb_private::Thread &thread, lldb_private::CompilerType &type) const override
static void Initialize()
size_t GetRedZoneSize() const override
lldb::ValueObjectSP GetReturnValueObjectSimple(lldb_private::Thread &thread, lldb_private::CompilerType &ast_type) const
static llvm::StringRef GetPluginNameStatic()
bool CreateFunctionEntryUnwindPlan(lldb_private::UnwindPlan &unwind_plan) override
bool RegisterIsVolatile(const lldb_private::RegisterInfo *reg_info) override
bool PrepareTrivialCall(lldb_private::Thread &thread, lldb::addr_t sp, lldb::addr_t functionAddress, lldb::addr_t returnAddress, llvm::ArrayRef< lldb::addr_t > args) const override
static std::unique_ptr< llvm::MCRegisterInfo > MakeMCRegisterInfo(const ArchSpec &arch)
Utility function to construct a MCRegisterInfo using the ArchSpec triple.
Definition: ABI.cpp:234
A section + offset based address class.
Definition: Address.h:62
An architecture specification class.
Definition: ArchSpec.h:31
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition: ArchSpec.cpp:709
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition: ArchSpec.h:461
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
Definition: ArchSpec.cpp:756
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
TypeSystemSPWrapper GetTypeSystem() const
Accessors.
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
CompilerType GetFieldAtIndex(size_t idx, std::string &name, uint64_t *bit_offset_ptr, uint32_t *bitfield_bit_size_ptr, bool *is_bitfield_ptr) const
bool IsFloatingPointType(uint32_t &count, bool &is_complex) const
uint32_t GetNumFields() const
bool IsIntegerOrEnumerationType(bool &is_signed) const
uint32_t GetTypeInfo(CompilerType *pointee_or_element_compiler_type=nullptr) const
std::optional< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
bool IsPointerType(CompilerType *pointee_type=nullptr) const
A uniqued constant string class.
Definition: ConstString.h:40
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
Definition: DataExtractor.h:48
float GetFloat(lldb::offset_t *offset_ptr) const
Extract a float from *offset_ptr.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
uint64_t GetMaxU64(lldb::offset_t *offset_ptr, size_t byte_size) const
Extract an unsigned integer of size byte_size from *offset_ptr.
lldb::ByteOrder GetByteOrder() const
Get the current byte order value.
lldb::offset_t CopyByteOrderedData(lldb::offset_t src_offset, lldb::offset_t src_len, void *dst, lldb::offset_t dst_len, lldb::ByteOrder dst_byte_order) const
Copy dst_len bytes from *offset_ptr and ensure the copied data is treated as a value that can be swap...
double GetDouble(lldb::offset_t *offset_ptr) const
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Target * GetTargetPtr() const
Returns a pointer to the target object.
void PutString(llvm::StringRef str)
Definition: Log.cpp:147
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
uint64_t GetSP(uint64_t fail_value=LLDB_INVALID_ADDRESS)
virtual bool WriteRegister(const RegisterInfo *reg_info, const RegisterValue &reg_value)=0
const RegisterInfo * GetRegisterInfo(lldb::RegisterKind reg_kind, uint32_t reg_num)
bool WriteRegisterFromUnsigned(uint32_t reg, uint64_t uval)
const RegisterInfo * GetRegisterInfoByName(llvm::StringRef reg_name, uint32_t start_idx=0)
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
bool GetData(DataExtractor &data) const
uint32_t GetAsMemoryData(const RegisterInfo &reg_info, void *dst, uint32_t dst_len, lldb::ByteOrder dst_byte_order, Status &error) const
void SetBytes(const void *bytes, size_t length, lldb::ByteOrder byte_order)
bool SignExtend(uint32_t bit_pos)
Definition: Scalar.cpp:745
An error handling class.
Definition: Status.h:118
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: Status.cpp:106
static Status FromErrorString(const char *str)
Definition: Status.h:141
bool Fail() const
Test for error condition.
Definition: Status.cpp:294
const char * AsCString(const char *default_error_str="unknown error") const
Get the error string associated with the current error.
Definition: Status.cpp:195
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:65
const ArchSpec & GetArchitecture() const
Definition: Target.h:1039
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:408
virtual lldb::RegisterContextSP GetRegisterContext()=0
lldb::ProcessSP GetProcess() const
Definition: Thread.h:157
void SetUnwindPlanForSignalTrap(lldb_private::LazyBool is_for_signal_trap)
Definition: UnwindPlan.h:536
void SetRegisterKind(lldb::RegisterKind kind)
Definition: UnwindPlan.h:471
void AppendRow(const RowSP &row_sp)
Definition: UnwindPlan.cpp:392
std::shared_ptr< Row > RowSP
Definition: UnwindPlan.h:429
void SetSourcedFromCompiler(lldb_private::LazyBool from_compiler)
Definition: UnwindPlan.h:512
void SetSourceName(const char *)
Definition: UnwindPlan.cpp:594
void SetUnwindPlanValidAtAllInstructions(lldb_private::LazyBool valid_at_all_insn)
Definition: UnwindPlan.h:524
Value * GetValueAtIndex(size_t idx)
Definition: Value.cpp:691
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, lldb::ByteOrder byte_order, uint32_t addr_byte_size, lldb::addr_t address=LLDB_INVALID_ADDRESS)
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, llvm::StringRef name, const Address &address, lldb::TypeSP &type_sp)
const Scalar & GetScalar() const
Definition: Value.h:112
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:268
void SetValueType(ValueType value_type)
Definition: Value.h:89
const CompilerType & GetCompilerType()
Definition: Value.cpp:239
#define LLDB_REGNUM_GENERIC_ARG6
Definition: lldb-defines.h:71
#define LLDB_REGNUM_GENERIC_SP
Definition: lldb-defines.h:57
#define LLDB_REGNUM_GENERIC_ARG4
Definition: lldb-defines.h:67
#define LLDB_REGNUM_GENERIC_ARG3
Definition: lldb-defines.h:65
#define LLDB_REGNUM_GENERIC_ARG1
Definition: lldb-defines.h:61
#define LLDB_REGNUM_GENERIC_FLAGS
Definition: lldb-defines.h:60
#define UINT32_MAX
Definition: lldb-defines.h:19
#define LLDB_INVALID_REGNUM
Definition: lldb-defines.h:87
#define LLDB_REGNUM_GENERIC_ARG2
Definition: lldb-defines.h:63
#define LLDB_REGNUM_GENERIC_PC
Definition: lldb-defines.h:56
#define LLDB_REGNUM_GENERIC_FP
Definition: lldb-defines.h:58
#define LLDB_REGNUM_GENERIC_ARG5
Definition: lldb-defines.h:69
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:332
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ABI > ABISP
Definition: lldb-forward.h:317
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
Definition: lldb-forward.h:424
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:484
uint64_t offset_t
Definition: lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
Definition: lldb-forward.h:336
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
Definition: lldb-forward.h:337
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::RegisterContext > RegisterContextSP
Definition: lldb-forward.h:394
@ eRegisterKindGeneric
insn ptr reg, stack ptr reg, etc not specific to any particular target
@ eRegisterKindLLDB
lldb's internal register numbers
@ eRegisterKindDWARF
the register numbers seen DWARF
Every register is described in detail including its name, alternate name (optional),...
uint32_t byte_size
Size in bytes of the register.
uint32_t kinds[lldb::kNumRegisterKinds]
Holds all of the various register numbers for all register kinds.
const char * name
Name of this register, can't be NULL.
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47