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