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::Solaris:
99 case llvm::Triple::OSType::UnknownOS:
100 return ABISP(
101 new ABISysV_x86_64(std::move(process_sp), MakeMCRegisterInfo(arch)));
102 default:
103 return ABISP();
104 }
105 }
106 return ABISP();
107}
108
110 addr_t func_addr, addr_t return_addr,
111 llvm::ArrayRef<addr_t> args) const {
112 Log *log = GetLog(LLDBLog::Expressions);
113
114 if (log) {
115 StreamString s;
116 s.Printf("ABISysV_x86_64::PrepareTrivialCall (tid = 0x%" PRIx64
117 ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
118 ", return_addr = 0x%" PRIx64,
119 thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
120 (uint64_t)return_addr);
121
122 for (size_t i = 0; i < args.size(); ++i)
123 s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
124 args[i]);
125 s.PutCString(")");
126 log->PutString(s.GetString());
127 }
128
129 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
130 if (!reg_ctx)
131 return false;
132
133 const RegisterInfo *reg_info = nullptr;
134
135 if (args.size() > 6) // TODO handle more than 6 arguments
136 return false;
137
138 for (size_t i = 0; i < args.size(); ++i) {
139 reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
141 LLDB_LOGF(log, "About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
142 static_cast<uint64_t>(i + 1), args[i], reg_info->name);
143 if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
144 return false;
145 }
146
147 // First, align the SP
148
149 LLDB_LOGF(log, "16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
150 (uint64_t)sp, (uint64_t)(sp & ~0xfull));
151
152 sp &= ~(0xfull); // 16-byte alignment
153
154 sp -= 8;
155
157 const RegisterInfo *pc_reg_info =
159 const RegisterInfo *sp_reg_info =
161 ProcessSP process_sp(thread.GetProcess());
162
163 RegisterValue reg_value;
164 LLDB_LOGF(log,
165 "Pushing the return address onto the stack: 0x%" PRIx64
166 ": 0x%" PRIx64,
167 (uint64_t)sp, (uint64_t)return_addr);
168
169 // Save return address onto the stack
170 if (!process_sp->WritePointerToMemory(sp, return_addr, error))
171 return false;
172
173 // %rsp is set to the actual stack value.
174
175 LLDB_LOGF(log, "Writing SP: 0x%" PRIx64, (uint64_t)sp);
176
177 if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
178 return false;
179
180 // %rip is set to the address of the called function.
181
182 LLDB_LOGF(log, "Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
183
184 if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
185 return false;
186
187 return true;
188}
189
190static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
191 bool is_signed, Thread &thread,
192 uint32_t *argument_register_ids,
193 unsigned int &current_argument_register,
194 addr_t &current_stack_argument) {
195 if (bit_width > 64)
196 return false; // Scalar can't hold large integer arguments
197
198 if (current_argument_register < 6) {
199 scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
200 argument_register_ids[current_argument_register], 0);
201 current_argument_register++;
202 if (is_signed)
203 scalar.SignExtend(bit_width);
204 } else {
205 uint32_t byte_size = (bit_width + (8 - 1)) / 8;
207 if (thread.GetProcess()->ReadScalarIntegerFromMemory(
208 current_stack_argument, byte_size, is_signed, scalar, error)) {
209 current_stack_argument += byte_size;
210 return true;
211 }
212 return false;
213 }
214 return true;
215}
216
218 ValueList &values) const {
219 unsigned int num_values = values.GetSize();
220 unsigned int value_index;
221
222 // Extract the register context so we can read arguments from registers
223
224 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
225
226 if (!reg_ctx)
227 return false;
228
229 // Get the pointer to the first stack argument so we have a place to start
230 // when reading data
231
232 addr_t sp = reg_ctx->GetSP(0);
233
234 if (!sp)
235 return false;
236
237 addr_t current_stack_argument = sp + 8; // jump over return address
238
239 uint32_t argument_register_ids[6];
240
241 argument_register_ids[0] =
244 argument_register_ids[1] =
247 argument_register_ids[2] =
250 argument_register_ids[3] =
253 argument_register_ids[4] =
256 argument_register_ids[5] =
259
260 unsigned int current_argument_register = 0;
261
262 for (value_index = 0; value_index < num_values; ++value_index) {
263 Value *value = values.GetValueAtIndex(value_index);
264
265 if (!value)
266 return false;
267
268 // We currently only support extracting values with Clang QualTypes. Do we
269 // care about others?
270 CompilerType compiler_type = value->GetCompilerType();
271 std::optional<uint64_t> bit_size = compiler_type.GetBitSize(&thread);
272 if (!bit_size)
273 return false;
274 bool is_signed;
275
276 if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
277 ReadIntegerArgument(value->GetScalar(), *bit_size, is_signed, thread,
278 argument_register_ids, current_argument_register,
279 current_stack_argument);
280 } else if (compiler_type.IsPointerType()) {
281 ReadIntegerArgument(value->GetScalar(), *bit_size, false, thread,
282 argument_register_ids, current_argument_register,
283 current_stack_argument);
284 }
285 }
286
287 return true;
288}
289
291 lldb::ValueObjectSP &new_value_sp) {
293 if (!new_value_sp) {
294 error = Status::FromErrorString("Empty value object for return value.");
295 return error;
296 }
297
298 CompilerType compiler_type = new_value_sp->GetCompilerType();
299 if (!compiler_type) {
300 error = Status::FromErrorString("Null clang type for return value.");
301 return error;
302 }
303
304 Thread *thread = frame_sp->GetThread().get();
305
306 bool is_signed;
307 uint32_t count;
308 bool is_complex;
309
310 RegisterContext *reg_ctx = thread->GetRegisterContext().get();
311
312 bool set_it_simple = false;
313 if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
314 compiler_type.IsPointerType()) {
315 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("rax", 0);
316
317 DataExtractor data;
318 Status data_error;
319 size_t num_bytes = new_value_sp->GetData(data, data_error);
320 if (data_error.Fail()) {
322 "Couldn't convert return value to raw data: %s",
323 data_error.AsCString());
324 return error;
325 }
326 lldb::offset_t offset = 0;
327 if (num_bytes <= 8) {
328 uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
329
330 if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
331 set_it_simple = true;
332 } else {
334 "We don't support returning longer than 64 bit "
335 "integer values at present.");
336 }
337 } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
338 if (is_complex)
340 "We don't support returning complex values at present");
341 else {
342 std::optional<uint64_t> bit_width =
343 compiler_type.GetBitSize(frame_sp.get());
344 if (!bit_width) {
345 error = Status::FromErrorString("can't get type size");
346 return error;
347 }
348 if (*bit_width <= 64) {
349 const RegisterInfo *xmm0_info =
350 reg_ctx->GetRegisterInfoByName("xmm0", 0);
351 RegisterValue xmm0_value;
352 DataExtractor data;
353 Status data_error;
354 size_t num_bytes = new_value_sp->GetData(data, data_error);
355 if (data_error.Fail()) {
357 "Couldn't convert return value to raw data: %s",
358 data_error.AsCString());
359 return error;
360 }
361
362 unsigned char buffer[16];
363 ByteOrder byte_order = data.GetByteOrder();
364
365 data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
366 xmm0_value.SetBytes(buffer, 16, byte_order);
367 reg_ctx->WriteRegister(xmm0_info, xmm0_value);
368 set_it_simple = true;
369 } else {
370 // FIXME - don't know how to do 80 bit long doubles yet.
372 "We don't support returning float values > 64 bits at present");
373 }
374 }
375 }
376
377 if (!set_it_simple) {
378 // Okay we've got a structure or something that doesn't fit in a simple
379 // register. We should figure out where it really goes, but we don't
380 // support this yet.
382 "We only support setting simple integer and float "
383 "return types at present.");
384 }
385
386 return error;
387}
388
390 Thread &thread, CompilerType &return_compiler_type) const {
391 ValueObjectSP return_valobj_sp;
392 Value value;
393
394 if (!return_compiler_type)
395 return return_valobj_sp;
396
397 // value.SetContext (Value::eContextTypeClangType, return_value_type);
398 value.SetCompilerType(return_compiler_type);
399
400 RegisterContext *reg_ctx = thread.GetRegisterContext().get();
401 if (!reg_ctx)
402 return return_valobj_sp;
403
404 const uint32_t type_flags = return_compiler_type.GetTypeInfo();
405 if (type_flags & eTypeIsScalar) {
406 value.SetValueType(Value::ValueType::Scalar);
407
408 bool success = false;
409 if (type_flags & eTypeIsInteger) {
410 // Extract the register context so we can read arguments from registers
411
412 std::optional<uint64_t> byte_size =
413 return_compiler_type.GetByteSize(&thread);
414 if (!byte_size)
415 return return_valobj_sp;
416 uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
417 reg_ctx->GetRegisterInfoByName("rax", 0), 0);
418 const bool is_signed = (type_flags & eTypeIsSigned) != 0;
419 switch (*byte_size) {
420 default:
421 break;
422
423 case sizeof(uint64_t):
424 if (is_signed)
425 value.GetScalar() = (int64_t)(raw_value);
426 else
427 value.GetScalar() = (uint64_t)(raw_value);
428 success = true;
429 break;
430
431 case sizeof(uint32_t):
432 if (is_signed)
433 value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
434 else
435 value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
436 success = true;
437 break;
438
439 case sizeof(uint16_t):
440 if (is_signed)
441 value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
442 else
443 value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
444 success = true;
445 break;
446
447 case sizeof(uint8_t):
448 if (is_signed)
449 value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
450 else
451 value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
452 success = true;
453 break;
454 }
455 } else if (type_flags & eTypeIsFloat) {
456 if (type_flags & eTypeIsComplex) {
457 // Don't handle complex yet.
458 } else {
459 std::optional<uint64_t> byte_size =
460 return_compiler_type.GetByteSize(&thread);
461 if (byte_size && *byte_size <= sizeof(long double)) {
462 const RegisterInfo *xmm0_info =
463 reg_ctx->GetRegisterInfoByName("xmm0", 0);
464 RegisterValue xmm0_value;
465 if (reg_ctx->ReadRegister(xmm0_info, xmm0_value)) {
466 DataExtractor data;
467 if (xmm0_value.GetData(data)) {
468 lldb::offset_t offset = 0;
469 if (*byte_size == sizeof(float)) {
470 value.GetScalar() = (float)data.GetFloat(&offset);
471 success = true;
472 } else if (*byte_size == sizeof(double)) {
473 value.GetScalar() = (double)data.GetDouble(&offset);
474 success = true;
475 } else if (*byte_size == sizeof(long double)) {
476 // Don't handle long double since that can be encoded as 80 bit
477 // floats...
478 }
479 }
480 }
481 }
482 }
483 }
484
485 if (success)
486 return_valobj_sp = ValueObjectConstResult::Create(
487 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
488 } else if (type_flags & eTypeIsPointer) {
489 unsigned rax_id =
490 reg_ctx->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
491 value.GetScalar() =
492 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
493 0);
494 value.SetValueType(Value::ValueType::Scalar);
495 return_valobj_sp = ValueObjectConstResult::Create(
496 thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
497 } else if (type_flags & eTypeIsVector) {
498 std::optional<uint64_t> byte_size =
499 return_compiler_type.GetByteSize(&thread);
500 if (byte_size && *byte_size > 0) {
501 const RegisterInfo *altivec_reg =
502 reg_ctx->GetRegisterInfoByName("xmm0", 0);
503 if (altivec_reg == nullptr)
504 altivec_reg = reg_ctx->GetRegisterInfoByName("mm0", 0);
505
506 if (altivec_reg) {
507 if (*byte_size <= altivec_reg->byte_size) {
508 ProcessSP process_sp(thread.GetProcess());
509 if (process_sp) {
510 std::unique_ptr<DataBufferHeap> heap_data_up(
511 new DataBufferHeap(*byte_size, 0));
512 const ByteOrder byte_order = process_sp->GetByteOrder();
513 RegisterValue reg_value;
514 if (reg_ctx->ReadRegister(altivec_reg, reg_value)) {
516 if (reg_value.GetAsMemoryData(
517 *altivec_reg, heap_data_up->GetBytes(),
518 heap_data_up->GetByteSize(), byte_order, error)) {
519 DataExtractor data(DataBufferSP(heap_data_up.release()),
520 byte_order,
521 process_sp->GetTarget()
522 .GetArchitecture()
523 .GetAddressByteSize());
524 return_valobj_sp = ValueObjectConstResult::Create(
525 &thread, return_compiler_type, ConstString(""), data);
526 }
527 }
528 }
529 } else if (*byte_size <= altivec_reg->byte_size * 2) {
530 const RegisterInfo *altivec_reg2 =
531 reg_ctx->GetRegisterInfoByName("xmm1", 0);
532 if (altivec_reg2) {
533 ProcessSP process_sp(thread.GetProcess());
534 if (process_sp) {
535 std::unique_ptr<DataBufferHeap> heap_data_up(
536 new DataBufferHeap(*byte_size, 0));
537 const ByteOrder byte_order = process_sp->GetByteOrder();
538 RegisterValue reg_value;
539 RegisterValue reg_value2;
540 if (reg_ctx->ReadRegister(altivec_reg, reg_value) &&
541 reg_ctx->ReadRegister(altivec_reg2, reg_value2)) {
542
544 if (reg_value.GetAsMemoryData(
545 *altivec_reg, heap_data_up->GetBytes(),
546 altivec_reg->byte_size, byte_order, error) &&
547 reg_value2.GetAsMemoryData(
548 *altivec_reg2,
549 heap_data_up->GetBytes() + altivec_reg->byte_size,
550 heap_data_up->GetByteSize() - altivec_reg->byte_size,
551 byte_order, error)) {
552 DataExtractor data(DataBufferSP(heap_data_up.release()),
553 byte_order,
554 process_sp->GetTarget()
555 .GetArchitecture()
556 .GetAddressByteSize());
557 return_valobj_sp = ValueObjectConstResult::Create(
558 &thread, return_compiler_type, ConstString(""), data);
559 }
560 }
561 }
562 }
563 }
564 }
565 }
566 }
567
568 return return_valobj_sp;
569}
570
571// The compiler will flatten the nested aggregate type into single
572// layer and push the value to stack
573// This helper function will flatten an aggregate type
574// and return true if it can be returned in register(s) by value
575// return false if the aggregate is in memory
577 Thread &thread, ExecutionContext &exe_ctx,
578 CompilerType &return_compiler_type,
579 uint32_t data_byte_offset,
580 std::vector<uint32_t> &aggregate_field_offsets,
581 std::vector<CompilerType> &aggregate_compiler_types) {
582
583 const uint32_t num_children = return_compiler_type.GetNumFields();
584 for (uint32_t idx = 0; idx < num_children; ++idx) {
585 std::string name;
586 bool is_signed;
587 uint32_t count;
588 bool is_complex;
589
590 uint64_t field_bit_offset = 0;
591 CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(
592 idx, name, &field_bit_offset, nullptr, nullptr);
593 std::optional<uint64_t> field_bit_width =
594 field_compiler_type.GetBitSize(&thread);
595
596 // if we don't know the size of the field (e.g. invalid type), exit
597 if (!field_bit_width || *field_bit_width == 0) {
598 return false;
599 }
600
601 uint32_t field_byte_offset = field_bit_offset / 8 + data_byte_offset;
602
603 const uint32_t field_type_flags = field_compiler_type.GetTypeInfo();
604 if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
605 field_compiler_type.IsPointerType() ||
606 field_compiler_type.IsFloatingPointType(count, is_complex)) {
607 aggregate_field_offsets.push_back(field_byte_offset);
608 aggregate_compiler_types.push_back(field_compiler_type);
609 } else if (field_type_flags & eTypeHasChildren) {
610 if (!FlattenAggregateType(thread, exe_ctx, field_compiler_type,
611 field_byte_offset, aggregate_field_offsets,
612 aggregate_compiler_types)) {
613 return false;
614 }
615 }
616 }
617 return true;
618}
619
621 Thread &thread, CompilerType &return_compiler_type) const {
622 ValueObjectSP return_valobj_sp;
623
624 if (!return_compiler_type)
625 return return_valobj_sp;
626
627 ExecutionContext exe_ctx(thread.shared_from_this());
628 return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
629 if (return_valobj_sp)
630 return return_valobj_sp;
631
632 RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
633 if (!reg_ctx_sp)
634 return return_valobj_sp;
635
636 std::optional<uint64_t> bit_width = return_compiler_type.GetBitSize(&thread);
637 if (!bit_width)
638 return return_valobj_sp;
639 if (return_compiler_type.IsAggregateType()) {
640 Target *target = exe_ctx.GetTargetPtr();
641 bool is_memory = true;
642 std::vector<uint32_t> aggregate_field_offsets;
643 std::vector<CompilerType> aggregate_compiler_types;
644 auto ts = return_compiler_type.GetTypeSystem();
645 if (ts && ts->CanPassInRegisters(return_compiler_type) &&
646 *bit_width <= 128 &&
647 FlattenAggregateType(thread, exe_ctx, return_compiler_type, 0,
648 aggregate_field_offsets,
649 aggregate_compiler_types)) {
650 ByteOrder byte_order = target->GetArchitecture().GetByteOrder();
651 WritableDataBufferSP data_sp(new DataBufferHeap(16, 0));
652 DataExtractor return_ext(data_sp, byte_order,
654
655 const RegisterInfo *rax_info =
656 reg_ctx_sp->GetRegisterInfoByName("rax", 0);
657 const RegisterInfo *rdx_info =
658 reg_ctx_sp->GetRegisterInfoByName("rdx", 0);
659 const RegisterInfo *xmm0_info =
660 reg_ctx_sp->GetRegisterInfoByName("xmm0", 0);
661 const RegisterInfo *xmm1_info =
662 reg_ctx_sp->GetRegisterInfoByName("xmm1", 0);
663
664 RegisterValue rax_value, rdx_value, xmm0_value, xmm1_value;
665 reg_ctx_sp->ReadRegister(rax_info, rax_value);
666 reg_ctx_sp->ReadRegister(rdx_info, rdx_value);
667 reg_ctx_sp->ReadRegister(xmm0_info, xmm0_value);
668 reg_ctx_sp->ReadRegister(xmm1_info, xmm1_value);
669
670 DataExtractor rax_data, rdx_data, xmm0_data, xmm1_data;
671
672 rax_value.GetData(rax_data);
673 rdx_value.GetData(rdx_data);
674 xmm0_value.GetData(xmm0_data);
675 xmm1_value.GetData(xmm1_data);
676
677 uint32_t fp_bytes =
678 0; // Tracks how much of the xmm registers we've consumed so far
679 uint32_t integer_bytes =
680 0; // Tracks how much of the rax/rds registers we've consumed so far
681
682 // in case of the returned type is a subclass of non-abstract-base class
683 // it will have a padding to skip the base content
684 if (aggregate_field_offsets.size()) {
685 fp_bytes = aggregate_field_offsets[0];
686 integer_bytes = aggregate_field_offsets[0];
687 }
688
689 const uint32_t num_children = aggregate_compiler_types.size();
690
691 // Since we are in the small struct regime, assume we are not in memory.
692 is_memory = false;
693 for (uint32_t idx = 0; idx < num_children; idx++) {
694 bool is_signed;
695 uint32_t count;
696 bool is_complex;
697
698 CompilerType field_compiler_type = aggregate_compiler_types[idx];
699 uint32_t field_byte_width = (uint32_t) (*field_compiler_type.GetByteSize(&thread));
700 uint32_t field_byte_offset = aggregate_field_offsets[idx];
701
702 uint32_t field_bit_width = field_byte_width * 8;
703
704 DataExtractor *copy_from_extractor = nullptr;
705 uint32_t copy_from_offset = 0;
706
707 if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
708 field_compiler_type.IsPointerType()) {
709 if (integer_bytes < 8) {
710 if (integer_bytes + field_byte_width <= 8) {
711 // This is in RAX, copy from register to our result structure:
712 copy_from_extractor = &rax_data;
713 copy_from_offset = integer_bytes;
714 integer_bytes += field_byte_width;
715 } else {
716 // The next field wouldn't fit in the remaining space, so we
717 // pushed it to rdx.
718 copy_from_extractor = &rdx_data;
719 copy_from_offset = 0;
720 integer_bytes = 8 + field_byte_width;
721 }
722 } else if (integer_bytes + field_byte_width <= 16) {
723 copy_from_extractor = &rdx_data;
724 copy_from_offset = integer_bytes - 8;
725 integer_bytes += field_byte_width;
726 } else {
727 // The last field didn't fit. I can't see how that would happen
728 // w/o the overall size being greater than 16 bytes. For now,
729 // return a nullptr return value object.
730 return return_valobj_sp;
731 }
732 } else if (field_compiler_type.IsFloatingPointType(count, is_complex)) {
733 // Structs with long doubles are always passed in memory.
734 if (field_bit_width == 128) {
735 is_memory = true;
736 break;
737 } else if (field_bit_width == 64) {
738 // These have to be in a single xmm register.
739 if (fp_bytes == 0)
740 copy_from_extractor = &xmm0_data;
741 else
742 copy_from_extractor = &xmm1_data;
743
744 copy_from_offset = 0;
745 fp_bytes += field_byte_width;
746 } else if (field_bit_width == 32) {
747 // This one is kind of complicated. If we are in an "eightbyte"
748 // with another float, we'll be stuffed into an xmm register with
749 // it. If we are in an "eightbyte" with one or more ints, then we
750 // will be stuffed into the appropriate GPR with them.
751 bool in_gpr;
752 if (field_byte_offset % 8 == 0) {
753 // We are at the beginning of one of the eightbytes, so check the
754 // next element (if any)
755 if (idx == num_children - 1) {
756 in_gpr = false;
757 } else {
758 CompilerType next_field_compiler_type =
759 aggregate_compiler_types[idx + 1];
760 if (next_field_compiler_type.IsIntegerOrEnumerationType(
761 is_signed)) {
762 in_gpr = true;
763 } else {
764 copy_from_offset = 0;
765 in_gpr = false;
766 }
767 }
768 } else if (field_byte_offset % 4 == 0) {
769 // We are inside of an eightbyte, so see if the field before us
770 // is floating point: This could happen if somebody put padding
771 // in the structure.
772 if (idx == 0) {
773 in_gpr = false;
774 } else {
775 CompilerType prev_field_compiler_type =
776 aggregate_compiler_types[idx - 1];
777 if (prev_field_compiler_type.IsIntegerOrEnumerationType(
778 is_signed)) {
779 in_gpr = true;
780 } else {
781 copy_from_offset = 4;
782 in_gpr = false;
783 }
784 }
785 } else {
786 is_memory = true;
787 continue;
788 }
789
790 // Okay, we've figured out whether we are in GPR or XMM, now figure
791 // out which one.
792 if (in_gpr) {
793 if (integer_bytes < 8) {
794 // This is in RAX, copy from register to our result structure:
795 copy_from_extractor = &rax_data;
796 copy_from_offset = integer_bytes;
797 integer_bytes += field_byte_width;
798 } else {
799 copy_from_extractor = &rdx_data;
800 copy_from_offset = integer_bytes - 8;
801 integer_bytes += field_byte_width;
802 }
803 } else {
804 if (fp_bytes < 8)
805 copy_from_extractor = &xmm0_data;
806 else
807 copy_from_extractor = &xmm1_data;
808
809 fp_bytes += field_byte_width;
810 }
811 }
812 }
813 // These two tests are just sanity checks. If I somehow get the type
814 // calculation wrong above it is better to just return nothing than to
815 // assert or crash.
816 if (!copy_from_extractor)
817 return return_valobj_sp;
818 if (copy_from_offset + field_byte_width >
819 copy_from_extractor->GetByteSize())
820 return return_valobj_sp;
821 copy_from_extractor->CopyByteOrderedData(
822 copy_from_offset, field_byte_width,
823 data_sp->GetBytes() + field_byte_offset, field_byte_width,
824 byte_order);
825 }
826 if (!is_memory) {
827 // The result is in our data buffer. Let's make a variable object out
828 // of it:
829 return_valobj_sp = ValueObjectConstResult::Create(
830 &thread, return_compiler_type, ConstString(""), return_ext);
831 }
832 }
833
834 // FIXME: This is just taking a guess, rax may very well no longer hold the
835 // return storage location.
836 // If we are going to do this right, when we make a new frame we should
837 // check to see if it uses a memory return, and if we are at the first
838 // instruction and if so stash away the return location. Then we would
839 // only return the memory return value if we know it is valid.
840
841 if (is_memory) {
842 unsigned rax_id =
843 reg_ctx_sp->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
844 lldb::addr_t storage_addr =
845 (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
846 0);
847 return_valobj_sp = ValueObjectMemory::Create(
848 &thread, "", Address(storage_addr, nullptr), return_compiler_type);
849 }
850 }
851
852 return return_valobj_sp;
853}
854
855// This defines the CFA as rsp+8
856// the saved pc is at CFA-8 (i.e. rsp+0)
857// The saved rsp is CFA+0
858
860 unwind_plan.Clear();
862
863 uint32_t sp_reg_num = dwarf_rsp;
864 uint32_t pc_reg_num = dwarf_rip;
865
867 row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 8);
868 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, -8, false);
869 row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
870 unwind_plan.AppendRow(row);
871 unwind_plan.SetSourceName("x86_64 at-func-entry default");
873 return true;
874}
875
876// This defines the CFA as rbp+16
877// The saved pc is at CFA-8 (i.e. rbp+8)
878// The saved rbp is at CFA-16 (i.e. rbp+0)
879// The saved rsp is CFA+0
880
882 unwind_plan.Clear();
884
885 uint32_t fp_reg_num = dwarf_rbp;
886 uint32_t sp_reg_num = dwarf_rsp;
887 uint32_t pc_reg_num = dwarf_rip;
888
890
891 const int32_t ptr_size = 8;
892 row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_rbp, 2 * ptr_size);
893 row->SetOffset(0);
894 row->SetUnspecifiedRegistersAreUndefined(true);
895
896 row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true);
897 row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true);
898 row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
899
900 unwind_plan.AppendRow(row);
901 unwind_plan.SetSourceName("x86_64 default unwind plan");
905 return true;
906}
907
909 return !RegisterIsCalleeSaved(reg_info);
910}
911
912// See "Register Usage" in the
913// "System V Application Binary Interface"
914// "AMD64 Architecture Processor Supplement" (or "x86-64(tm) Architecture
915// Processor Supplement" in earlier revisions) (this doc is also commonly
916// referred to as the x86-64/AMD64 psABI) Edited by Michael Matz, Jan Hubicka,
917// Andreas Jaeger, and Mark Mitchell current version is 0.99.6 released
918// 2012-07-02 at http://refspecs.linuxfoundation.org/elf/x86-64-abi-0.99.pdf
919// It's being revised & updated at https://github.com/hjl-tools/x86-psABI/
920
922 if (!reg_info)
923 return false;
924 assert(reg_info->name != nullptr && "unnamed register?");
925 std::string Name = std::string(reg_info->name);
926 bool IsCalleeSaved =
927 llvm::StringSwitch<bool>(Name)
928 .Cases("r12", "r13", "r14", "r15", "rbp", "ebp", "rbx", "ebx", true)
929 .Cases("rip", "eip", "rsp", "esp", "sp", "fp", "pc", true)
930 .Default(false);
931 return IsCalleeSaved;
932}
933
934uint32_t ABISysV_x86_64::GetGenericNum(llvm::StringRef name) {
935 return llvm::StringSwitch<uint32_t>(name)
936 .Case("rip", LLDB_REGNUM_GENERIC_PC)
937 .Case("rsp", LLDB_REGNUM_GENERIC_SP)
938 .Case("rbp", LLDB_REGNUM_GENERIC_FP)
939 .Case("rflags", LLDB_REGNUM_GENERIC_FLAGS)
940 // gdbserver uses eflags
941 .Case("eflags", LLDB_REGNUM_GENERIC_FLAGS)
942 .Case("rdi", LLDB_REGNUM_GENERIC_ARG1)
943 .Case("rsi", LLDB_REGNUM_GENERIC_ARG2)
944 .Case("rdx", LLDB_REGNUM_GENERIC_ARG3)
945 .Case("rcx", LLDB_REGNUM_GENERIC_ARG4)
946 .Case("r8", LLDB_REGNUM_GENERIC_ARG5)
947 .Case("r9", LLDB_REGNUM_GENERIC_ARG6)
948 .Default(LLDB_INVALID_REGNUM);
949}
950
953 GetPluginNameStatic(), "System V ABI for x86_64 targets", CreateInstance);
954}
955
958}
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:115
static Status FromErrorStringWithFormat(const char *format,...) __attribute__((format(printf
Definition: Status.cpp:106
static Status FromErrorString(const char *str)
Definition: Status.h:138
bool Fail() const
Test for error condition.
Definition: Status.cpp:270
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