]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Plugins/ABI/SysV-x86_64/ABISysV_x86_64.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r306956, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Plugins / ABI / SysV-x86_64 / ABISysV_x86_64.cpp
1 //===-- ABISysV_x86_64.cpp --------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "ABISysV_x86_64.h"
11
12 // C Includes
13 // C++ Includes
14 // Other libraries and framework includes
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/Triple.h"
17
18 // Project includes
19 #include "lldb/Core/Module.h"
20 #include "lldb/Core/PluginManager.h"
21 #include "lldb/Core/RegisterValue.h"
22 #include "lldb/Core/Value.h"
23 #include "lldb/Core/ValueObjectConstResult.h"
24 #include "lldb/Core/ValueObjectMemory.h"
25 #include "lldb/Core/ValueObjectRegister.h"
26 #include "lldb/Symbol/UnwindPlan.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/RegisterContext.h"
29 #include "lldb/Target/StackFrame.h"
30 #include "lldb/Target/Target.h"
31 #include "lldb/Target/Thread.h"
32 #include "lldb/Utility/ConstString.h"
33 #include "lldb/Utility/DataExtractor.h"
34 #include "lldb/Utility/Log.h"
35 #include "lldb/Utility/Status.h"
36
37 using namespace lldb;
38 using namespace lldb_private;
39
40 enum dwarf_regnums {
41   dwarf_rax = 0,
42   dwarf_rdx,
43   dwarf_rcx,
44   dwarf_rbx,
45   dwarf_rsi,
46   dwarf_rdi,
47   dwarf_rbp,
48   dwarf_rsp,
49   dwarf_r8,
50   dwarf_r9,
51   dwarf_r10,
52   dwarf_r11,
53   dwarf_r12,
54   dwarf_r13,
55   dwarf_r14,
56   dwarf_r15,
57   dwarf_rip,
58   dwarf_xmm0,
59   dwarf_xmm1,
60   dwarf_xmm2,
61   dwarf_xmm3,
62   dwarf_xmm4,
63   dwarf_xmm5,
64   dwarf_xmm6,
65   dwarf_xmm7,
66   dwarf_xmm8,
67   dwarf_xmm9,
68   dwarf_xmm10,
69   dwarf_xmm11,
70   dwarf_xmm12,
71   dwarf_xmm13,
72   dwarf_xmm14,
73   dwarf_xmm15,
74   dwarf_stmm0,
75   dwarf_stmm1,
76   dwarf_stmm2,
77   dwarf_stmm3,
78   dwarf_stmm4,
79   dwarf_stmm5,
80   dwarf_stmm6,
81   dwarf_stmm7,
82   dwarf_ymm0,
83   dwarf_ymm1,
84   dwarf_ymm2,
85   dwarf_ymm3,
86   dwarf_ymm4,
87   dwarf_ymm5,
88   dwarf_ymm6,
89   dwarf_ymm7,
90   dwarf_ymm8,
91   dwarf_ymm9,
92   dwarf_ymm10,
93   dwarf_ymm11,
94   dwarf_ymm12,
95   dwarf_ymm13,
96   dwarf_ymm14,
97   dwarf_ymm15,
98   dwarf_bnd0 = 126,
99   dwarf_bnd1,
100   dwarf_bnd2,
101   dwarf_bnd3
102 };
103
104 static RegisterInfo g_register_infos[] = {
105     //  NAME      ALT      SZ OFF ENCODING         FORMAT              EH_FRAME
106     //  DWARF                 GENERIC                     PROCESS PLUGIN
107     //  LLDB NATIVE
108     //  ========  =======  == === =============    ===================
109     //  ======================= =====================
110     //  =========================== ===================== ======================
111     {"rax",
112      nullptr,
113      8,
114      0,
115      eEncodingUint,
116      eFormatHex,
117      {dwarf_rax, dwarf_rax, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
118       LLDB_INVALID_REGNUM},
119      nullptr,
120      nullptr,
121      nullptr,
122      0},
123     {"rbx",
124      nullptr,
125      8,
126      0,
127      eEncodingUint,
128      eFormatHex,
129      {dwarf_rbx, dwarf_rbx, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
130       LLDB_INVALID_REGNUM},
131      nullptr,
132      nullptr,
133      nullptr,
134      0},
135     {"rcx",
136      "arg4",
137      8,
138      0,
139      eEncodingUint,
140      eFormatHex,
141      {dwarf_rcx, dwarf_rcx, LLDB_REGNUM_GENERIC_ARG4, LLDB_INVALID_REGNUM,
142       LLDB_INVALID_REGNUM},
143      nullptr,
144      nullptr,
145      nullptr,
146      0},
147     {"rdx",
148      "arg3",
149      8,
150      0,
151      eEncodingUint,
152      eFormatHex,
153      {dwarf_rdx, dwarf_rdx, LLDB_REGNUM_GENERIC_ARG3, LLDB_INVALID_REGNUM,
154       LLDB_INVALID_REGNUM},
155      nullptr,
156      nullptr,
157      nullptr,
158      0},
159     {"rsi",
160      "arg2",
161      8,
162      0,
163      eEncodingUint,
164      eFormatHex,
165      {dwarf_rsi, dwarf_rsi, LLDB_REGNUM_GENERIC_ARG2, LLDB_INVALID_REGNUM,
166       LLDB_INVALID_REGNUM},
167      nullptr,
168      nullptr,
169      nullptr,
170      0},
171     {"rdi",
172      "arg1",
173      8,
174      0,
175      eEncodingUint,
176      eFormatHex,
177      {dwarf_rdi, dwarf_rdi, LLDB_REGNUM_GENERIC_ARG1, LLDB_INVALID_REGNUM,
178       LLDB_INVALID_REGNUM},
179      nullptr,
180      nullptr,
181      nullptr,
182      0},
183     {"rbp",
184      "fp",
185      8,
186      0,
187      eEncodingUint,
188      eFormatHex,
189      {dwarf_rbp, dwarf_rbp, LLDB_REGNUM_GENERIC_FP, LLDB_INVALID_REGNUM,
190       LLDB_INVALID_REGNUM},
191      nullptr,
192      nullptr,
193      nullptr,
194      0},
195     {"rsp",
196      "sp",
197      8,
198      0,
199      eEncodingUint,
200      eFormatHex,
201      {dwarf_rsp, dwarf_rsp, LLDB_REGNUM_GENERIC_SP, LLDB_INVALID_REGNUM,
202       LLDB_INVALID_REGNUM},
203      nullptr,
204      nullptr,
205      nullptr,
206      0},
207     {"r8",
208      "arg5",
209      8,
210      0,
211      eEncodingUint,
212      eFormatHex,
213      {dwarf_r8, dwarf_r8, LLDB_REGNUM_GENERIC_ARG5, LLDB_INVALID_REGNUM,
214       LLDB_INVALID_REGNUM},
215      nullptr,
216      nullptr,
217      nullptr,
218      0},
219     {"r9",
220      "arg6",
221      8,
222      0,
223      eEncodingUint,
224      eFormatHex,
225      {dwarf_r9, dwarf_r9, LLDB_REGNUM_GENERIC_ARG6, LLDB_INVALID_REGNUM,
226       LLDB_INVALID_REGNUM},
227      nullptr,
228      nullptr,
229      nullptr,
230      0},
231     {"r10",
232      nullptr,
233      8,
234      0,
235      eEncodingUint,
236      eFormatHex,
237      {dwarf_r10, dwarf_r10, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
238       LLDB_INVALID_REGNUM},
239      nullptr,
240      nullptr,
241      nullptr,
242      0},
243     {"r11",
244      nullptr,
245      8,
246      0,
247      eEncodingUint,
248      eFormatHex,
249      {dwarf_r11, dwarf_r11, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
250       LLDB_INVALID_REGNUM},
251      nullptr,
252      nullptr,
253      nullptr,
254      0},
255     {"r12",
256      nullptr,
257      8,
258      0,
259      eEncodingUint,
260      eFormatHex,
261      {dwarf_r12, dwarf_r12, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
262       LLDB_INVALID_REGNUM},
263      nullptr,
264      nullptr,
265      nullptr,
266      0},
267     {"r13",
268      nullptr,
269      8,
270      0,
271      eEncodingUint,
272      eFormatHex,
273      {dwarf_r13, dwarf_r13, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
274       LLDB_INVALID_REGNUM},
275      nullptr,
276      nullptr,
277      nullptr,
278      0},
279     {"r14",
280      nullptr,
281      8,
282      0,
283      eEncodingUint,
284      eFormatHex,
285      {dwarf_r14, dwarf_r14, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
286       LLDB_INVALID_REGNUM},
287      nullptr,
288      nullptr,
289      nullptr,
290      0},
291     {"r15",
292      nullptr,
293      8,
294      0,
295      eEncodingUint,
296      eFormatHex,
297      {dwarf_r15, dwarf_r15, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
298       LLDB_INVALID_REGNUM},
299      nullptr,
300      nullptr,
301      nullptr,
302      0},
303     {"rip",
304      "pc",
305      8,
306      0,
307      eEncodingUint,
308      eFormatHex,
309      {dwarf_rip, dwarf_rip, LLDB_REGNUM_GENERIC_PC, LLDB_INVALID_REGNUM,
310       LLDB_INVALID_REGNUM},
311      nullptr,
312      nullptr,
313      nullptr,
314      0},
315     {"rflags",
316      nullptr,
317      4,
318      0,
319      eEncodingUint,
320      eFormatHex,
321      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_REGNUM_GENERIC_FLAGS,
322       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
323      nullptr,
324      nullptr,
325      nullptr,
326      0},
327     {"cs",
328      nullptr,
329      4,
330      0,
331      eEncodingUint,
332      eFormatHex,
333      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
334       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
335      nullptr,
336      nullptr,
337      nullptr,
338      0},
339     {"ss",
340      nullptr,
341      4,
342      0,
343      eEncodingUint,
344      eFormatHex,
345      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
346       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
347      nullptr,
348      nullptr,
349      nullptr,
350      0},
351     {"ds",
352      nullptr,
353      4,
354      0,
355      eEncodingUint,
356      eFormatHex,
357      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
358       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
359      nullptr,
360      nullptr,
361      nullptr,
362      0},
363     {"es",
364      nullptr,
365      4,
366      0,
367      eEncodingUint,
368      eFormatHex,
369      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
370       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
371      nullptr,
372      nullptr,
373      nullptr,
374      0},
375     {"fs",
376      nullptr,
377      4,
378      0,
379      eEncodingUint,
380      eFormatHex,
381      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
382       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
383      nullptr,
384      nullptr,
385      nullptr,
386      0},
387     {"gs",
388      nullptr,
389      4,
390      0,
391      eEncodingUint,
392      eFormatHex,
393      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
394       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
395      nullptr,
396      nullptr,
397      nullptr,
398      0},
399     {"stmm0",
400      nullptr,
401      10,
402      0,
403      eEncodingVector,
404      eFormatVectorOfUInt8,
405      {dwarf_stmm0, dwarf_stmm0, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
406       LLDB_INVALID_REGNUM},
407      nullptr,
408      nullptr,
409      nullptr,
410      0},
411     {"stmm1",
412      nullptr,
413      10,
414      0,
415      eEncodingVector,
416      eFormatVectorOfUInt8,
417      {dwarf_stmm1, dwarf_stmm1, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
418       LLDB_INVALID_REGNUM},
419      nullptr,
420      nullptr,
421      nullptr,
422      0},
423     {"stmm2",
424      nullptr,
425      10,
426      0,
427      eEncodingVector,
428      eFormatVectorOfUInt8,
429      {dwarf_stmm2, dwarf_stmm2, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
430       LLDB_INVALID_REGNUM},
431      nullptr,
432      nullptr,
433      nullptr,
434      0},
435     {"stmm3",
436      nullptr,
437      10,
438      0,
439      eEncodingVector,
440      eFormatVectorOfUInt8,
441      {dwarf_stmm3, dwarf_stmm3, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
442       LLDB_INVALID_REGNUM},
443      nullptr,
444      nullptr,
445      nullptr,
446      0},
447     {"stmm4",
448      nullptr,
449      10,
450      0,
451      eEncodingVector,
452      eFormatVectorOfUInt8,
453      {dwarf_stmm4, dwarf_stmm4, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
454       LLDB_INVALID_REGNUM},
455      nullptr,
456      nullptr,
457      nullptr,
458      0},
459     {"stmm5",
460      nullptr,
461      10,
462      0,
463      eEncodingVector,
464      eFormatVectorOfUInt8,
465      {dwarf_stmm5, dwarf_stmm5, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
466       LLDB_INVALID_REGNUM},
467      nullptr,
468      nullptr,
469      nullptr,
470      0},
471     {"stmm6",
472      nullptr,
473      10,
474      0,
475      eEncodingVector,
476      eFormatVectorOfUInt8,
477      {dwarf_stmm6, dwarf_stmm6, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
478       LLDB_INVALID_REGNUM},
479      nullptr,
480      nullptr,
481      nullptr,
482      0},
483     {"stmm7",
484      nullptr,
485      10,
486      0,
487      eEncodingVector,
488      eFormatVectorOfUInt8,
489      {dwarf_stmm7, dwarf_stmm7, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
490       LLDB_INVALID_REGNUM},
491      nullptr,
492      nullptr,
493      nullptr,
494      0},
495     {"fctrl",
496      nullptr,
497      4,
498      0,
499      eEncodingUint,
500      eFormatHex,
501      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
502       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
503      nullptr,
504      nullptr,
505      nullptr,
506      0},
507     {"fstat",
508      nullptr,
509      4,
510      0,
511      eEncodingUint,
512      eFormatHex,
513      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
514       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
515      nullptr,
516      nullptr,
517      nullptr,
518      0},
519     {"ftag",
520      nullptr,
521      4,
522      0,
523      eEncodingUint,
524      eFormatHex,
525      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
526       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
527      nullptr,
528      nullptr,
529      nullptr,
530      0},
531     {"fiseg",
532      nullptr,
533      4,
534      0,
535      eEncodingUint,
536      eFormatHex,
537      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
538       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
539      nullptr,
540      nullptr,
541      nullptr,
542      0},
543     {"fioff",
544      nullptr,
545      4,
546      0,
547      eEncodingUint,
548      eFormatHex,
549      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
550       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
551      nullptr,
552      nullptr,
553      nullptr,
554      0},
555     {"foseg",
556      nullptr,
557      4,
558      0,
559      eEncodingUint,
560      eFormatHex,
561      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
562       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
563      nullptr,
564      nullptr,
565      nullptr,
566      0},
567     {"fooff",
568      nullptr,
569      4,
570      0,
571      eEncodingUint,
572      eFormatHex,
573      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
574       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
575      nullptr,
576      nullptr,
577      nullptr,
578      0},
579     {"fop",
580      nullptr,
581      4,
582      0,
583      eEncodingUint,
584      eFormatHex,
585      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
586       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
587      nullptr,
588      nullptr,
589      nullptr,
590      0},
591     {"xmm0",
592      nullptr,
593      16,
594      0,
595      eEncodingVector,
596      eFormatVectorOfUInt8,
597      {dwarf_xmm0, dwarf_xmm0, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
598       LLDB_INVALID_REGNUM},
599      nullptr,
600      nullptr,
601      nullptr,
602      0},
603     {"xmm1",
604      nullptr,
605      16,
606      0,
607      eEncodingVector,
608      eFormatVectorOfUInt8,
609      {dwarf_xmm1, dwarf_xmm1, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
610       LLDB_INVALID_REGNUM},
611      nullptr,
612      nullptr,
613      nullptr,
614      0},
615     {"xmm2",
616      nullptr,
617      16,
618      0,
619      eEncodingVector,
620      eFormatVectorOfUInt8,
621      {dwarf_xmm2, dwarf_xmm2, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
622       LLDB_INVALID_REGNUM},
623      nullptr,
624      nullptr,
625      nullptr,
626      0},
627     {"xmm3",
628      nullptr,
629      16,
630      0,
631      eEncodingVector,
632      eFormatVectorOfUInt8,
633      {dwarf_xmm3, dwarf_xmm3, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
634       LLDB_INVALID_REGNUM},
635      nullptr,
636      nullptr,
637      nullptr,
638      0},
639     {"xmm4",
640      nullptr,
641      16,
642      0,
643      eEncodingVector,
644      eFormatVectorOfUInt8,
645      {dwarf_xmm4, dwarf_xmm4, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
646       LLDB_INVALID_REGNUM},
647      nullptr,
648      nullptr,
649      nullptr,
650      0},
651     {"xmm5",
652      nullptr,
653      16,
654      0,
655      eEncodingVector,
656      eFormatVectorOfUInt8,
657      {dwarf_xmm5, dwarf_xmm5, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
658       LLDB_INVALID_REGNUM},
659      nullptr,
660      nullptr,
661      nullptr,
662      0},
663     {"xmm6",
664      nullptr,
665      16,
666      0,
667      eEncodingVector,
668      eFormatVectorOfUInt8,
669      {dwarf_xmm6, dwarf_xmm6, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
670       LLDB_INVALID_REGNUM},
671      nullptr,
672      nullptr,
673      nullptr,
674      0},
675     {"xmm7",
676      nullptr,
677      16,
678      0,
679      eEncodingVector,
680      eFormatVectorOfUInt8,
681      {dwarf_xmm7, dwarf_xmm7, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
682       LLDB_INVALID_REGNUM},
683      nullptr,
684      nullptr,
685      nullptr,
686      0},
687     {"xmm8",
688      nullptr,
689      16,
690      0,
691      eEncodingVector,
692      eFormatVectorOfUInt8,
693      {dwarf_xmm8, dwarf_xmm8, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
694       LLDB_INVALID_REGNUM},
695      nullptr,
696      nullptr,
697      nullptr,
698      0},
699     {"xmm9",
700      nullptr,
701      16,
702      0,
703      eEncodingVector,
704      eFormatVectorOfUInt8,
705      {dwarf_xmm9, dwarf_xmm9, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
706       LLDB_INVALID_REGNUM},
707      nullptr,
708      nullptr,
709      nullptr,
710      0},
711     {"xmm10",
712      nullptr,
713      16,
714      0,
715      eEncodingVector,
716      eFormatVectorOfUInt8,
717      {dwarf_xmm10, dwarf_xmm10, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
718       LLDB_INVALID_REGNUM},
719      nullptr,
720      nullptr,
721      nullptr,
722      0},
723     {"xmm11",
724      nullptr,
725      16,
726      0,
727      eEncodingVector,
728      eFormatVectorOfUInt8,
729      {dwarf_xmm11, dwarf_xmm11, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
730       LLDB_INVALID_REGNUM},
731      nullptr,
732      nullptr,
733      nullptr,
734      0},
735     {"xmm12",
736      nullptr,
737      16,
738      0,
739      eEncodingVector,
740      eFormatVectorOfUInt8,
741      {dwarf_xmm12, dwarf_xmm12, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
742       LLDB_INVALID_REGNUM},
743      nullptr,
744      nullptr,
745      nullptr,
746      0},
747     {"xmm13",
748      nullptr,
749      16,
750      0,
751      eEncodingVector,
752      eFormatVectorOfUInt8,
753      {dwarf_xmm13, dwarf_xmm13, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
754       LLDB_INVALID_REGNUM},
755      nullptr,
756      nullptr,
757      nullptr,
758      0},
759     {"xmm14",
760      nullptr,
761      16,
762      0,
763      eEncodingVector,
764      eFormatVectorOfUInt8,
765      {dwarf_xmm14, dwarf_xmm14, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
766       LLDB_INVALID_REGNUM},
767      nullptr,
768      nullptr,
769      nullptr,
770      0},
771     {"xmm15",
772      nullptr,
773      16,
774      0,
775      eEncodingVector,
776      eFormatVectorOfUInt8,
777      {dwarf_xmm15, dwarf_xmm15, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
778       LLDB_INVALID_REGNUM},
779      nullptr,
780      nullptr,
781      nullptr,
782      0},
783     {"mxcsr",
784      nullptr,
785      4,
786      0,
787      eEncodingUint,
788      eFormatHex,
789      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
790       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
791      nullptr,
792      nullptr,
793      nullptr,
794      0},
795     {"ymm0",
796      nullptr,
797      32,
798      0,
799      eEncodingVector,
800      eFormatVectorOfUInt8,
801      {dwarf_ymm0, dwarf_ymm0, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
802       LLDB_INVALID_REGNUM},
803      nullptr,
804      nullptr,
805      nullptr,
806      0},
807     {"ymm1",
808      nullptr,
809      32,
810      0,
811      eEncodingVector,
812      eFormatVectorOfUInt8,
813      {dwarf_ymm1, dwarf_ymm1, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
814       LLDB_INVALID_REGNUM},
815      nullptr,
816      nullptr,
817      nullptr,
818      0},
819     {"ymm2",
820      nullptr,
821      32,
822      0,
823      eEncodingVector,
824      eFormatVectorOfUInt8,
825      {dwarf_ymm2, dwarf_ymm2, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
826       LLDB_INVALID_REGNUM},
827      nullptr,
828      nullptr,
829      nullptr,
830      0},
831     {"ymm3",
832      nullptr,
833      32,
834      0,
835      eEncodingVector,
836      eFormatVectorOfUInt8,
837      {dwarf_ymm3, dwarf_ymm3, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
838       LLDB_INVALID_REGNUM},
839      nullptr,
840      nullptr,
841      nullptr,
842      0},
843     {"ymm4",
844      nullptr,
845      32,
846      0,
847      eEncodingVector,
848      eFormatVectorOfUInt8,
849      {dwarf_ymm4, dwarf_ymm4, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
850       LLDB_INVALID_REGNUM},
851      nullptr,
852      nullptr,
853      nullptr,
854      0},
855     {"ymm5",
856      nullptr,
857      32,
858      0,
859      eEncodingVector,
860      eFormatVectorOfUInt8,
861      {dwarf_ymm5, dwarf_ymm5, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
862       LLDB_INVALID_REGNUM},
863      nullptr,
864      nullptr,
865      nullptr,
866      0},
867     {"ymm6",
868      nullptr,
869      32,
870      0,
871      eEncodingVector,
872      eFormatVectorOfUInt8,
873      {dwarf_ymm6, dwarf_ymm6, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
874       LLDB_INVALID_REGNUM},
875      nullptr,
876      nullptr,
877      nullptr,
878      0},
879     {"ymm7",
880      nullptr,
881      32,
882      0,
883      eEncodingVector,
884      eFormatVectorOfUInt8,
885      {dwarf_ymm7, dwarf_ymm7, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
886       LLDB_INVALID_REGNUM},
887      nullptr,
888      nullptr,
889      nullptr,
890      0},
891     {"ymm8",
892      nullptr,
893      32,
894      0,
895      eEncodingVector,
896      eFormatVectorOfUInt8,
897      {dwarf_ymm8, dwarf_ymm8, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
898       LLDB_INVALID_REGNUM},
899      nullptr,
900      nullptr,
901      nullptr,
902      0},
903     {"ymm9",
904      nullptr,
905      32,
906      0,
907      eEncodingVector,
908      eFormatVectorOfUInt8,
909      {dwarf_ymm9, dwarf_ymm9, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
910       LLDB_INVALID_REGNUM},
911      nullptr,
912      nullptr,
913      nullptr,
914      0},
915     {"ymm10",
916      nullptr,
917      32,
918      0,
919      eEncodingVector,
920      eFormatVectorOfUInt8,
921      {dwarf_ymm10, dwarf_ymm10, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
922       LLDB_INVALID_REGNUM},
923      nullptr,
924      nullptr,
925      nullptr,
926      0},
927     {"ymm11",
928      nullptr,
929      32,
930      0,
931      eEncodingVector,
932      eFormatVectorOfUInt8,
933      {dwarf_ymm11, dwarf_ymm11, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
934       LLDB_INVALID_REGNUM},
935      nullptr,
936      nullptr,
937      nullptr,
938      0},
939     {"ymm12",
940      nullptr,
941      32,
942      0,
943      eEncodingVector,
944      eFormatVectorOfUInt8,
945      {dwarf_ymm12, dwarf_ymm12, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
946       LLDB_INVALID_REGNUM},
947      nullptr,
948      nullptr,
949      nullptr,
950      0},
951     {"ymm13",
952      nullptr,
953      32,
954      0,
955      eEncodingVector,
956      eFormatVectorOfUInt8,
957      {dwarf_ymm13, dwarf_ymm13, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
958       LLDB_INVALID_REGNUM},
959      nullptr,
960      nullptr,
961      nullptr,
962      0},
963     {"ymm14",
964      nullptr,
965      32,
966      0,
967      eEncodingVector,
968      eFormatVectorOfUInt8,
969      {dwarf_ymm14, dwarf_ymm14, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
970       LLDB_INVALID_REGNUM},
971      nullptr,
972      nullptr,
973      nullptr,
974      0},
975     {"ymm15",
976      nullptr,
977      32,
978      0,
979      eEncodingVector,
980      eFormatVectorOfUInt8,
981      {dwarf_ymm15, dwarf_ymm15, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
982       LLDB_INVALID_REGNUM},
983      nullptr,
984      nullptr,
985      nullptr,
986      0},
987     {"bnd0",
988      nullptr,
989      16,
990      0,
991      eEncodingVector,
992      eFormatVectorOfUInt64,
993      {dwarf_bnd0, dwarf_bnd0, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
994       LLDB_INVALID_REGNUM},
995      nullptr,
996      nullptr,
997      nullptr,
998      0},
999     {"bnd1",
1000      nullptr,
1001      16,
1002      0,
1003      eEncodingVector,
1004      eFormatVectorOfUInt64,
1005      {dwarf_bnd1, dwarf_bnd1, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1006       LLDB_INVALID_REGNUM},
1007      nullptr,
1008      nullptr,
1009      nullptr,
1010      0},
1011     {"bnd2",
1012      nullptr,
1013      16,
1014      0,
1015      eEncodingVector,
1016      eFormatVectorOfUInt64,
1017      {dwarf_bnd2, dwarf_bnd2, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1018       LLDB_INVALID_REGNUM},
1019      nullptr,
1020      nullptr,
1021      nullptr,
1022      0},
1023     {"bnd3",
1024      nullptr,
1025      16,
1026      0,
1027      eEncodingVector,
1028      eFormatVectorOfUInt64,
1029      {dwarf_bnd3, dwarf_bnd3, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1030       LLDB_INVALID_REGNUM},
1031      nullptr,
1032      nullptr,
1033      nullptr,
1034      0},
1035     {"bndcfgu",
1036      nullptr,
1037      8,
1038      0,
1039      eEncodingVector,
1040      eFormatVectorOfUInt8,
1041      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1042       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
1043      nullptr,
1044      nullptr,
1045      nullptr,
1046      0},
1047     {"bndstatus",
1048      nullptr,
1049      8,
1050      0,
1051      eEncodingVector,
1052      eFormatVectorOfUInt8,
1053      {LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM,
1054       LLDB_INVALID_REGNUM, LLDB_INVALID_REGNUM},
1055      nullptr,
1056      nullptr,
1057      nullptr,
1058      0}};
1059
1060 static const uint32_t k_num_register_infos =
1061     llvm::array_lengthof(g_register_infos);
1062 static bool g_register_info_names_constified = false;
1063
1064 const lldb_private::RegisterInfo *
1065 ABISysV_x86_64::GetRegisterInfoArray(uint32_t &count) {
1066   // Make the C-string names and alt_names for the register infos into const
1067   // C-string values by having the ConstString unique the names in the global
1068   // constant C-string pool.
1069   if (!g_register_info_names_constified) {
1070     g_register_info_names_constified = true;
1071     for (uint32_t i = 0; i < k_num_register_infos; ++i) {
1072       if (g_register_infos[i].name)
1073         g_register_infos[i].name =
1074             ConstString(g_register_infos[i].name).GetCString();
1075       if (g_register_infos[i].alt_name)
1076         g_register_infos[i].alt_name =
1077             ConstString(g_register_infos[i].alt_name).GetCString();
1078     }
1079   }
1080   count = k_num_register_infos;
1081   return g_register_infos;
1082 }
1083
1084 bool ABISysV_x86_64::GetPointerReturnRegister(const char *&name) {
1085   name = "rax";
1086   return true;
1087 }
1088
1089 size_t ABISysV_x86_64::GetRedZoneSize() const { return 128; }
1090
1091 //------------------------------------------------------------------
1092 // Static Functions
1093 //------------------------------------------------------------------
1094
1095 ABISP
1096 ABISysV_x86_64::CreateInstance(lldb::ProcessSP process_sp, const ArchSpec &arch) {
1097   static ABISP g_abi_sp;
1098   if (arch.GetTriple().getArch() == llvm::Triple::x86_64) {
1099     if (!g_abi_sp)
1100       g_abi_sp.reset(new ABISysV_x86_64(process_sp));
1101     return g_abi_sp;
1102   }
1103   return ABISP();
1104 }
1105
1106 bool ABISysV_x86_64::PrepareTrivialCall(Thread &thread, addr_t sp,
1107                                         addr_t func_addr, addr_t return_addr,
1108                                         llvm::ArrayRef<addr_t> args) const {
1109   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1110
1111   if (log) {
1112     StreamString s;
1113     s.Printf("ABISysV_x86_64::PrepareTrivialCall (tid = 0x%" PRIx64
1114              ", sp = 0x%" PRIx64 ", func_addr = 0x%" PRIx64
1115              ", return_addr = 0x%" PRIx64,
1116              thread.GetID(), (uint64_t)sp, (uint64_t)func_addr,
1117              (uint64_t)return_addr);
1118
1119     for (size_t i = 0; i < args.size(); ++i)
1120       s.Printf(", arg%" PRIu64 " = 0x%" PRIx64, static_cast<uint64_t>(i + 1),
1121                args[i]);
1122     s.PutCString(")");
1123     log->PutString(s.GetString());
1124   }
1125
1126   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1127   if (!reg_ctx)
1128     return false;
1129
1130   const RegisterInfo *reg_info = nullptr;
1131
1132   if (args.size() > 6) // TODO handle more than 6 arguments
1133     return false;
1134
1135   for (size_t i = 0; i < args.size(); ++i) {
1136     reg_info = reg_ctx->GetRegisterInfo(eRegisterKindGeneric,
1137                                         LLDB_REGNUM_GENERIC_ARG1 + i);
1138     if (log)
1139       log->Printf("About to write arg%" PRIu64 " (0x%" PRIx64 ") into %s",
1140                   static_cast<uint64_t>(i + 1), args[i], reg_info->name);
1141     if (!reg_ctx->WriteRegisterFromUnsigned(reg_info, args[i]))
1142       return false;
1143   }
1144
1145   // First, align the SP
1146
1147   if (log)
1148     log->Printf("16-byte aligning SP: 0x%" PRIx64 " to 0x%" PRIx64,
1149                 (uint64_t)sp, (uint64_t)(sp & ~0xfull));
1150
1151   sp &= ~(0xfull); // 16-byte alignment
1152
1153   sp -= 8;
1154
1155   Status error;
1156   const RegisterInfo *pc_reg_info =
1157       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC);
1158   const RegisterInfo *sp_reg_info =
1159       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_SP);
1160   ProcessSP process_sp(thread.GetProcess());
1161
1162   RegisterValue reg_value;
1163
1164 #if 0
1165     // This code adds an extra frame so that we don't lose the function that we came from
1166     // by pushing the PC and the FP and then writing the current FP to point to the FP value
1167     // we just pushed. It is disabled for now until the stack backtracing code can be debugged.
1168
1169     // Save current PC
1170     const RegisterInfo *fp_reg_info = reg_ctx->GetRegisterInfo (eRegisterKindGeneric, LLDB_REGNUM_GENERIC_FP);
1171     if (reg_ctx->ReadRegister(pc_reg_info, reg_value))
1172     {
1173         if (log)
1174             log->Printf("Pushing the current PC onto the stack: 0x%" PRIx64 ": 0x%" PRIx64, (uint64_t)sp, reg_value.GetAsUInt64());
1175         
1176         if (!process_sp->WritePointerToMemory(sp, reg_value.GetAsUInt64(), error))
1177             return false;
1178
1179         sp -= 8;
1180         
1181         // Save current FP
1182         if (reg_ctx->ReadRegister(fp_reg_info, reg_value))
1183         {
1184             if (log)
1185                 log->Printf("Pushing the current FP onto the stack: 0x%" PRIx64 ": 0x%" PRIx64, (uint64_t)sp, reg_value.GetAsUInt64());
1186             
1187             if (!process_sp->WritePointerToMemory(sp, reg_value.GetAsUInt64(), error))
1188                 return false;
1189         }
1190         // Setup FP backchain
1191         reg_value.SetUInt64 (sp);
1192         
1193         if (log)
1194             log->Printf("Writing FP:  0x%" PRIx64 " (for FP backchain)", reg_value.GetAsUInt64());
1195
1196         if (!reg_ctx->WriteRegister(fp_reg_info, reg_value))
1197         {
1198             return false;
1199         }
1200         
1201         sp -= 8;
1202     }
1203 #endif
1204
1205   if (log)
1206     log->Printf("Pushing the return address onto the stack: 0x%" PRIx64
1207                 ": 0x%" PRIx64,
1208                 (uint64_t)sp, (uint64_t)return_addr);
1209
1210   // Save return address onto the stack
1211   if (!process_sp->WritePointerToMemory(sp, return_addr, error))
1212     return false;
1213
1214   // %rsp is set to the actual stack value.
1215
1216   if (log)
1217     log->Printf("Writing SP: 0x%" PRIx64, (uint64_t)sp);
1218
1219   if (!reg_ctx->WriteRegisterFromUnsigned(sp_reg_info, sp))
1220     return false;
1221
1222   // %rip is set to the address of the called function.
1223
1224   if (log)
1225     log->Printf("Writing IP: 0x%" PRIx64, (uint64_t)func_addr);
1226
1227   if (!reg_ctx->WriteRegisterFromUnsigned(pc_reg_info, func_addr))
1228     return false;
1229
1230   return true;
1231 }
1232
1233 static bool ReadIntegerArgument(Scalar &scalar, unsigned int bit_width,
1234                                 bool is_signed, Thread &thread,
1235                                 uint32_t *argument_register_ids,
1236                                 unsigned int &current_argument_register,
1237                                 addr_t &current_stack_argument) {
1238   if (bit_width > 64)
1239     return false; // Scalar can't hold large integer arguments
1240
1241   if (current_argument_register < 6) {
1242     scalar = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
1243         argument_register_ids[current_argument_register], 0);
1244     current_argument_register++;
1245     if (is_signed)
1246       scalar.SignExtend(bit_width);
1247   } else {
1248     uint32_t byte_size = (bit_width + (8 - 1)) / 8;
1249     Status error;
1250     if (thread.GetProcess()->ReadScalarIntegerFromMemory(
1251             current_stack_argument, byte_size, is_signed, scalar, error)) {
1252       current_stack_argument += byte_size;
1253       return true;
1254     }
1255     return false;
1256   }
1257   return true;
1258 }
1259
1260 bool ABISysV_x86_64::GetArgumentValues(Thread &thread,
1261                                        ValueList &values) const {
1262   unsigned int num_values = values.GetSize();
1263   unsigned int value_index;
1264
1265   // Extract the register context so we can read arguments from registers
1266
1267   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1268
1269   if (!reg_ctx)
1270     return false;
1271
1272   // Get the pointer to the first stack argument so we have a place to start
1273   // when reading data
1274
1275   addr_t sp = reg_ctx->GetSP(0);
1276
1277   if (!sp)
1278     return false;
1279
1280   addr_t current_stack_argument = sp + 8; // jump over return address
1281
1282   uint32_t argument_register_ids[6];
1283
1284   argument_register_ids[0] =
1285       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG1)
1286           ->kinds[eRegisterKindLLDB];
1287   argument_register_ids[1] =
1288       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG2)
1289           ->kinds[eRegisterKindLLDB];
1290   argument_register_ids[2] =
1291       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG3)
1292           ->kinds[eRegisterKindLLDB];
1293   argument_register_ids[3] =
1294       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG4)
1295           ->kinds[eRegisterKindLLDB];
1296   argument_register_ids[4] =
1297       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG5)
1298           ->kinds[eRegisterKindLLDB];
1299   argument_register_ids[5] =
1300       reg_ctx->GetRegisterInfo(eRegisterKindGeneric, LLDB_REGNUM_GENERIC_ARG6)
1301           ->kinds[eRegisterKindLLDB];
1302
1303   unsigned int current_argument_register = 0;
1304
1305   for (value_index = 0; value_index < num_values; ++value_index) {
1306     Value *value = values.GetValueAtIndex(value_index);
1307
1308     if (!value)
1309       return false;
1310
1311     // We currently only support extracting values with Clang QualTypes.
1312     // Do we care about others?
1313     CompilerType compiler_type = value->GetCompilerType();
1314     if (!compiler_type)
1315       return false;
1316     bool is_signed;
1317
1318     if (compiler_type.IsIntegerOrEnumerationType(is_signed)) {
1319       ReadIntegerArgument(value->GetScalar(), compiler_type.GetBitSize(&thread),
1320                           is_signed, thread, argument_register_ids,
1321                           current_argument_register, current_stack_argument);
1322     } else if (compiler_type.IsPointerType()) {
1323       ReadIntegerArgument(value->GetScalar(), compiler_type.GetBitSize(&thread),
1324                           false, thread, argument_register_ids,
1325                           current_argument_register, current_stack_argument);
1326     }
1327   }
1328
1329   return true;
1330 }
1331
1332 Status ABISysV_x86_64::SetReturnValueObject(lldb::StackFrameSP &frame_sp,
1333                                             lldb::ValueObjectSP &new_value_sp) {
1334   Status error;
1335   if (!new_value_sp) {
1336     error.SetErrorString("Empty value object for return value.");
1337     return error;
1338   }
1339
1340   CompilerType compiler_type = new_value_sp->GetCompilerType();
1341   if (!compiler_type) {
1342     error.SetErrorString("Null clang type for return value.");
1343     return error;
1344   }
1345
1346   Thread *thread = frame_sp->GetThread().get();
1347
1348   bool is_signed;
1349   uint32_t count;
1350   bool is_complex;
1351
1352   RegisterContext *reg_ctx = thread->GetRegisterContext().get();
1353
1354   bool set_it_simple = false;
1355   if (compiler_type.IsIntegerOrEnumerationType(is_signed) ||
1356       compiler_type.IsPointerType()) {
1357     const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoByName("rax", 0);
1358
1359     DataExtractor data;
1360     Status data_error;
1361     size_t num_bytes = new_value_sp->GetData(data, data_error);
1362     if (data_error.Fail()) {
1363       error.SetErrorStringWithFormat(
1364           "Couldn't convert return value to raw data: %s",
1365           data_error.AsCString());
1366       return error;
1367     }
1368     lldb::offset_t offset = 0;
1369     if (num_bytes <= 8) {
1370       uint64_t raw_value = data.GetMaxU64(&offset, num_bytes);
1371
1372       if (reg_ctx->WriteRegisterFromUnsigned(reg_info, raw_value))
1373         set_it_simple = true;
1374     } else {
1375       error.SetErrorString("We don't support returning longer than 64 bit "
1376                            "integer values at present.");
1377     }
1378   } else if (compiler_type.IsFloatingPointType(count, is_complex)) {
1379     if (is_complex)
1380       error.SetErrorString(
1381           "We don't support returning complex values at present");
1382     else {
1383       size_t bit_width = compiler_type.GetBitSize(frame_sp.get());
1384       if (bit_width <= 64) {
1385         const RegisterInfo *xmm0_info =
1386             reg_ctx->GetRegisterInfoByName("xmm0", 0);
1387         RegisterValue xmm0_value;
1388         DataExtractor data;
1389         Status data_error;
1390         size_t num_bytes = new_value_sp->GetData(data, data_error);
1391         if (data_error.Fail()) {
1392           error.SetErrorStringWithFormat(
1393               "Couldn't convert return value to raw data: %s",
1394               data_error.AsCString());
1395           return error;
1396         }
1397
1398         unsigned char buffer[16];
1399         ByteOrder byte_order = data.GetByteOrder();
1400
1401         data.CopyByteOrderedData(0, num_bytes, buffer, 16, byte_order);
1402         xmm0_value.SetBytes(buffer, 16, byte_order);
1403         reg_ctx->WriteRegister(xmm0_info, xmm0_value);
1404         set_it_simple = true;
1405       } else {
1406         // FIXME - don't know how to do 80 bit long doubles yet.
1407         error.SetErrorString(
1408             "We don't support returning float values > 64 bits at present");
1409       }
1410     }
1411   }
1412
1413   if (!set_it_simple) {
1414     // Okay we've got a structure or something that doesn't fit in a simple
1415     // register.
1416     // We should figure out where it really goes, but we don't support this yet.
1417     error.SetErrorString("We only support setting simple integer and float "
1418                          "return types at present.");
1419   }
1420
1421   return error;
1422 }
1423
1424 ValueObjectSP ABISysV_x86_64::GetReturnValueObjectSimple(
1425     Thread &thread, CompilerType &return_compiler_type) const {
1426   ValueObjectSP return_valobj_sp;
1427   Value value;
1428
1429   if (!return_compiler_type)
1430     return return_valobj_sp;
1431
1432   // value.SetContext (Value::eContextTypeClangType, return_value_type);
1433   value.SetCompilerType(return_compiler_type);
1434
1435   RegisterContext *reg_ctx = thread.GetRegisterContext().get();
1436   if (!reg_ctx)
1437     return return_valobj_sp;
1438
1439   const uint32_t type_flags = return_compiler_type.GetTypeInfo();
1440   if (type_flags & eTypeIsScalar) {
1441     value.SetValueType(Value::eValueTypeScalar);
1442
1443     bool success = false;
1444     if (type_flags & eTypeIsInteger) {
1445       // Extract the register context so we can read arguments from registers
1446
1447       const size_t byte_size = return_compiler_type.GetByteSize(nullptr);
1448       uint64_t raw_value = thread.GetRegisterContext()->ReadRegisterAsUnsigned(
1449           reg_ctx->GetRegisterInfoByName("rax", 0), 0);
1450       const bool is_signed = (type_flags & eTypeIsSigned) != 0;
1451       switch (byte_size) {
1452       default:
1453         break;
1454
1455       case sizeof(uint64_t):
1456         if (is_signed)
1457           value.GetScalar() = (int64_t)(raw_value);
1458         else
1459           value.GetScalar() = (uint64_t)(raw_value);
1460         success = true;
1461         break;
1462
1463       case sizeof(uint32_t):
1464         if (is_signed)
1465           value.GetScalar() = (int32_t)(raw_value & UINT32_MAX);
1466         else
1467           value.GetScalar() = (uint32_t)(raw_value & UINT32_MAX);
1468         success = true;
1469         break;
1470
1471       case sizeof(uint16_t):
1472         if (is_signed)
1473           value.GetScalar() = (int16_t)(raw_value & UINT16_MAX);
1474         else
1475           value.GetScalar() = (uint16_t)(raw_value & UINT16_MAX);
1476         success = true;
1477         break;
1478
1479       case sizeof(uint8_t):
1480         if (is_signed)
1481           value.GetScalar() = (int8_t)(raw_value & UINT8_MAX);
1482         else
1483           value.GetScalar() = (uint8_t)(raw_value & UINT8_MAX);
1484         success = true;
1485         break;
1486       }
1487     } else if (type_flags & eTypeIsFloat) {
1488       if (type_flags & eTypeIsComplex) {
1489         // Don't handle complex yet.
1490       } else {
1491         const size_t byte_size = return_compiler_type.GetByteSize(nullptr);
1492         if (byte_size <= sizeof(long double)) {
1493           const RegisterInfo *xmm0_info =
1494               reg_ctx->GetRegisterInfoByName("xmm0", 0);
1495           RegisterValue xmm0_value;
1496           if (reg_ctx->ReadRegister(xmm0_info, xmm0_value)) {
1497             DataExtractor data;
1498             if (xmm0_value.GetData(data)) {
1499               lldb::offset_t offset = 0;
1500               if (byte_size == sizeof(float)) {
1501                 value.GetScalar() = (float)data.GetFloat(&offset);
1502                 success = true;
1503               } else if (byte_size == sizeof(double)) {
1504                 value.GetScalar() = (double)data.GetDouble(&offset);
1505                 success = true;
1506               } else if (byte_size == sizeof(long double)) {
1507                 // Don't handle long double since that can be encoded as 80 bit
1508                 // floats...
1509               }
1510             }
1511           }
1512         }
1513       }
1514     }
1515
1516     if (success)
1517       return_valobj_sp = ValueObjectConstResult::Create(
1518           thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
1519   } else if (type_flags & eTypeIsPointer) {
1520     unsigned rax_id =
1521         reg_ctx->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
1522     value.GetScalar() =
1523         (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
1524                                                                       0);
1525     value.SetValueType(Value::eValueTypeScalar);
1526     return_valobj_sp = ValueObjectConstResult::Create(
1527         thread.GetStackFrameAtIndex(0).get(), value, ConstString(""));
1528   } else if (type_flags & eTypeIsVector) {
1529     const size_t byte_size = return_compiler_type.GetByteSize(nullptr);
1530     if (byte_size > 0) {
1531       const RegisterInfo *altivec_reg =
1532           reg_ctx->GetRegisterInfoByName("xmm0", 0);
1533       if (altivec_reg == nullptr)
1534         altivec_reg = reg_ctx->GetRegisterInfoByName("mm0", 0);
1535
1536       if (altivec_reg) {
1537         if (byte_size <= altivec_reg->byte_size) {
1538           ProcessSP process_sp(thread.GetProcess());
1539           if (process_sp) {
1540             std::unique_ptr<DataBufferHeap> heap_data_ap(
1541                 new DataBufferHeap(byte_size, 0));
1542             const ByteOrder byte_order = process_sp->GetByteOrder();
1543             RegisterValue reg_value;
1544             if (reg_ctx->ReadRegister(altivec_reg, reg_value)) {
1545               Status error;
1546               if (reg_value.GetAsMemoryData(
1547                       altivec_reg, heap_data_ap->GetBytes(),
1548                       heap_data_ap->GetByteSize(), byte_order, error)) {
1549                 DataExtractor data(DataBufferSP(heap_data_ap.release()),
1550                                    byte_order, process_sp->GetTarget()
1551                                                    .GetArchitecture()
1552                                                    .GetAddressByteSize());
1553                 return_valobj_sp = ValueObjectConstResult::Create(
1554                     &thread, return_compiler_type, ConstString(""), data);
1555               }
1556             }
1557           }
1558         } else if (byte_size <= altivec_reg->byte_size * 2) {
1559           const RegisterInfo *altivec_reg2 =
1560               reg_ctx->GetRegisterInfoByName("xmm1", 0);
1561           if (altivec_reg2) {
1562             ProcessSP process_sp(thread.GetProcess());
1563             if (process_sp) {
1564               std::unique_ptr<DataBufferHeap> heap_data_ap(
1565                   new DataBufferHeap(byte_size, 0));
1566               const ByteOrder byte_order = process_sp->GetByteOrder();
1567               RegisterValue reg_value;
1568               RegisterValue reg_value2;
1569               if (reg_ctx->ReadRegister(altivec_reg, reg_value) &&
1570                   reg_ctx->ReadRegister(altivec_reg2, reg_value2)) {
1571
1572                 Status error;
1573                 if (reg_value.GetAsMemoryData(
1574                         altivec_reg, heap_data_ap->GetBytes(),
1575                         altivec_reg->byte_size, byte_order, error) &&
1576                     reg_value2.GetAsMemoryData(
1577                         altivec_reg2,
1578                         heap_data_ap->GetBytes() + altivec_reg->byte_size,
1579                         heap_data_ap->GetByteSize() - altivec_reg->byte_size,
1580                         byte_order, error)) {
1581                   DataExtractor data(DataBufferSP(heap_data_ap.release()),
1582                                      byte_order, process_sp->GetTarget()
1583                                                      .GetArchitecture()
1584                                                      .GetAddressByteSize());
1585                   return_valobj_sp = ValueObjectConstResult::Create(
1586                       &thread, return_compiler_type, ConstString(""), data);
1587                 }
1588               }
1589             }
1590           }
1591         }
1592       }
1593     }
1594   }
1595
1596   return return_valobj_sp;
1597 }
1598
1599 ValueObjectSP ABISysV_x86_64::GetReturnValueObjectImpl(
1600     Thread &thread, CompilerType &return_compiler_type) const {
1601   ValueObjectSP return_valobj_sp;
1602
1603   if (!return_compiler_type)
1604     return return_valobj_sp;
1605
1606   ExecutionContext exe_ctx(thread.shared_from_this());
1607   return_valobj_sp = GetReturnValueObjectSimple(thread, return_compiler_type);
1608   if (return_valobj_sp)
1609     return return_valobj_sp;
1610
1611   RegisterContextSP reg_ctx_sp = thread.GetRegisterContext();
1612   if (!reg_ctx_sp)
1613     return return_valobj_sp;
1614
1615   const size_t bit_width = return_compiler_type.GetBitSize(&thread);
1616   if (return_compiler_type.IsAggregateType()) {
1617     Target *target = exe_ctx.GetTargetPtr();
1618     bool is_memory = true;
1619     if (bit_width <= 128) {
1620       ByteOrder target_byte_order = target->GetArchitecture().GetByteOrder();
1621       DataBufferSP data_sp(new DataBufferHeap(16, 0));
1622       DataExtractor return_ext(data_sp, target_byte_order,
1623                                target->GetArchitecture().GetAddressByteSize());
1624
1625       const RegisterInfo *rax_info =
1626           reg_ctx_sp->GetRegisterInfoByName("rax", 0);
1627       const RegisterInfo *rdx_info =
1628           reg_ctx_sp->GetRegisterInfoByName("rdx", 0);
1629       const RegisterInfo *xmm0_info =
1630           reg_ctx_sp->GetRegisterInfoByName("xmm0", 0);
1631       const RegisterInfo *xmm1_info =
1632           reg_ctx_sp->GetRegisterInfoByName("xmm1", 0);
1633
1634       RegisterValue rax_value, rdx_value, xmm0_value, xmm1_value;
1635       reg_ctx_sp->ReadRegister(rax_info, rax_value);
1636       reg_ctx_sp->ReadRegister(rdx_info, rdx_value);
1637       reg_ctx_sp->ReadRegister(xmm0_info, xmm0_value);
1638       reg_ctx_sp->ReadRegister(xmm1_info, xmm1_value);
1639
1640       DataExtractor rax_data, rdx_data, xmm0_data, xmm1_data;
1641
1642       rax_value.GetData(rax_data);
1643       rdx_value.GetData(rdx_data);
1644       xmm0_value.GetData(xmm0_data);
1645       xmm1_value.GetData(xmm1_data);
1646
1647       uint32_t fp_bytes =
1648           0; // Tracks how much of the xmm registers we've consumed so far
1649       uint32_t integer_bytes =
1650           0; // Tracks how much of the rax/rds registers we've consumed so far
1651
1652       const uint32_t num_children = return_compiler_type.GetNumFields();
1653
1654       // Since we are in the small struct regime, assume we are not in memory.
1655       is_memory = false;
1656
1657       for (uint32_t idx = 0; idx < num_children; idx++) {
1658         std::string name;
1659         uint64_t field_bit_offset = 0;
1660         bool is_signed;
1661         bool is_complex;
1662         uint32_t count;
1663
1664         CompilerType field_compiler_type = return_compiler_type.GetFieldAtIndex(
1665             idx, name, &field_bit_offset, nullptr, nullptr);
1666         const size_t field_bit_width = field_compiler_type.GetBitSize(&thread);
1667
1668         // if we don't know the size of the field (e.g. invalid type), just bail
1669         // out
1670         if (field_bit_width == 0)
1671           break;
1672
1673         // If there are any unaligned fields, this is stored in memory.
1674         if (field_bit_offset % field_bit_width != 0) {
1675           is_memory = true;
1676           break;
1677         }
1678
1679         uint32_t field_byte_width = field_bit_width / 8;
1680         uint32_t field_byte_offset = field_bit_offset / 8;
1681
1682         DataExtractor *copy_from_extractor = nullptr;
1683         uint32_t copy_from_offset = 0;
1684
1685         if (field_compiler_type.IsIntegerOrEnumerationType(is_signed) ||
1686             field_compiler_type.IsPointerType()) {
1687           if (integer_bytes < 8) {
1688             if (integer_bytes + field_byte_width <= 8) {
1689               // This is in RAX, copy from register to our result structure:
1690               copy_from_extractor = &rax_data;
1691               copy_from_offset = integer_bytes;
1692               integer_bytes += field_byte_width;
1693             } else {
1694               // The next field wouldn't fit in the remaining space, so we
1695               // pushed it to rdx.
1696               copy_from_extractor = &rdx_data;
1697               copy_from_offset = 0;
1698               integer_bytes = 8 + field_byte_width;
1699             }
1700           } else if (integer_bytes + field_byte_width <= 16) {
1701             copy_from_extractor = &rdx_data;
1702             copy_from_offset = integer_bytes - 8;
1703             integer_bytes += field_byte_width;
1704           } else {
1705             // The last field didn't fit.  I can't see how that would happen w/o
1706             // the overall size being
1707             // greater than 16 bytes.  For now, return a nullptr return value
1708             // object.
1709             return return_valobj_sp;
1710           }
1711         } else if (field_compiler_type.IsFloatingPointType(count, is_complex)) {
1712           // Structs with long doubles are always passed in memory.
1713           if (field_bit_width == 128) {
1714             is_memory = true;
1715             break;
1716           } else if (field_bit_width == 64) {
1717             // These have to be in a single xmm register.
1718             if (fp_bytes == 0)
1719               copy_from_extractor = &xmm0_data;
1720             else
1721               copy_from_extractor = &xmm1_data;
1722
1723             copy_from_offset = 0;
1724             fp_bytes += field_byte_width;
1725           } else if (field_bit_width == 32) {
1726             // This one is kind of complicated.  If we are in an "eightbyte"
1727             // with another float, we'll
1728             // be stuffed into an xmm register with it.  If we are in an
1729             // "eightbyte" with one or more ints,
1730             // then we will be stuffed into the appropriate GPR with them.
1731             bool in_gpr;
1732             if (field_byte_offset % 8 == 0) {
1733               // We are at the beginning of one of the eightbytes, so check the
1734               // next element (if any)
1735               if (idx == num_children - 1)
1736                 in_gpr = false;
1737               else {
1738                 uint64_t next_field_bit_offset = 0;
1739                 CompilerType next_field_compiler_type =
1740                     return_compiler_type.GetFieldAtIndex(idx + 1, name,
1741                                                          &next_field_bit_offset,
1742                                                          nullptr, nullptr);
1743                 if (next_field_compiler_type.IsIntegerOrEnumerationType(
1744                         is_signed))
1745                   in_gpr = true;
1746                 else {
1747                   copy_from_offset = 0;
1748                   in_gpr = false;
1749                 }
1750               }
1751             } else if (field_byte_offset % 4 == 0) {
1752               // We are inside of an eightbyte, so see if the field before us is
1753               // floating point:
1754               // This could happen if somebody put padding in the structure.
1755               if (idx == 0)
1756                 in_gpr = false;
1757               else {
1758                 uint64_t prev_field_bit_offset = 0;
1759                 CompilerType prev_field_compiler_type =
1760                     return_compiler_type.GetFieldAtIndex(idx - 1, name,
1761                                                          &prev_field_bit_offset,
1762                                                          nullptr, nullptr);
1763                 if (prev_field_compiler_type.IsIntegerOrEnumerationType(
1764                         is_signed))
1765                   in_gpr = true;
1766                 else {
1767                   copy_from_offset = 4;
1768                   in_gpr = false;
1769                 }
1770               }
1771             } else {
1772               is_memory = true;
1773               continue;
1774             }
1775
1776             // Okay, we've figured out whether we are in GPR or XMM, now figure
1777             // out which one.
1778             if (in_gpr) {
1779               if (integer_bytes < 8) {
1780                 // This is in RAX, copy from register to our result structure:
1781                 copy_from_extractor = &rax_data;
1782                 copy_from_offset = integer_bytes;
1783                 integer_bytes += field_byte_width;
1784               } else {
1785                 copy_from_extractor = &rdx_data;
1786                 copy_from_offset = integer_bytes - 8;
1787                 integer_bytes += field_byte_width;
1788               }
1789             } else {
1790               if (fp_bytes < 8)
1791                 copy_from_extractor = &xmm0_data;
1792               else
1793                 copy_from_extractor = &xmm1_data;
1794
1795               fp_bytes += field_byte_width;
1796             }
1797           }
1798         }
1799
1800         // These two tests are just sanity checks.  If I somehow get the
1801         // type calculation wrong above it is better to just return nothing
1802         // than to assert or crash.
1803         if (!copy_from_extractor)
1804           return return_valobj_sp;
1805         if (copy_from_offset + field_byte_width >
1806             copy_from_extractor->GetByteSize())
1807           return return_valobj_sp;
1808
1809         copy_from_extractor->CopyByteOrderedData(
1810             copy_from_offset, field_byte_width,
1811             data_sp->GetBytes() + field_byte_offset, field_byte_width,
1812             target_byte_order);
1813       }
1814
1815       if (!is_memory) {
1816         // The result is in our data buffer.  Let's make a variable object out
1817         // of it:
1818         return_valobj_sp = ValueObjectConstResult::Create(
1819             &thread, return_compiler_type, ConstString(""), return_ext);
1820       }
1821     }
1822
1823     // FIXME: This is just taking a guess, rax may very well no longer hold the
1824     // return storage location.
1825     // If we are going to do this right, when we make a new frame we should
1826     // check to see if it uses a memory
1827     // return, and if we are at the first instruction and if so stash away the
1828     // return location.  Then we would
1829     // only return the memory return value if we know it is valid.
1830
1831     if (is_memory) {
1832       unsigned rax_id =
1833           reg_ctx_sp->GetRegisterInfoByName("rax", 0)->kinds[eRegisterKindLLDB];
1834       lldb::addr_t storage_addr =
1835           (uint64_t)thread.GetRegisterContext()->ReadRegisterAsUnsigned(rax_id,
1836                                                                         0);
1837       return_valobj_sp = ValueObjectMemory::Create(
1838           &thread, "", Address(storage_addr, nullptr), return_compiler_type);
1839     }
1840   }
1841
1842   return return_valobj_sp;
1843 }
1844
1845 // This defines the CFA as rsp+8
1846 // the saved pc is at CFA-8 (i.e. rsp+0)
1847 // The saved rsp is CFA+0
1848
1849 bool ABISysV_x86_64::CreateFunctionEntryUnwindPlan(UnwindPlan &unwind_plan) {
1850   unwind_plan.Clear();
1851   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
1852
1853   uint32_t sp_reg_num = dwarf_rsp;
1854   uint32_t pc_reg_num = dwarf_rip;
1855
1856   UnwindPlan::RowSP row(new UnwindPlan::Row);
1857   row->GetCFAValue().SetIsRegisterPlusOffset(sp_reg_num, 8);
1858   row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, -8, false);
1859   row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
1860   unwind_plan.AppendRow(row);
1861   unwind_plan.SetSourceName("x86_64 at-func-entry default");
1862   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
1863   return true;
1864 }
1865
1866 // This defines the CFA as rbp+16
1867 // The saved pc is at CFA-8 (i.e. rbp+8)
1868 // The saved rbp is at CFA-16 (i.e. rbp+0)
1869 // The saved rsp is CFA+0
1870
1871 bool ABISysV_x86_64::CreateDefaultUnwindPlan(UnwindPlan &unwind_plan) {
1872   unwind_plan.Clear();
1873   unwind_plan.SetRegisterKind(eRegisterKindDWARF);
1874
1875   uint32_t fp_reg_num = dwarf_rbp;
1876   uint32_t sp_reg_num = dwarf_rsp;
1877   uint32_t pc_reg_num = dwarf_rip;
1878
1879   UnwindPlan::RowSP row(new UnwindPlan::Row);
1880
1881   const int32_t ptr_size = 8;
1882   row->GetCFAValue().SetIsRegisterPlusOffset(dwarf_rbp, 2 * ptr_size);
1883   row->SetOffset(0);
1884
1885   row->SetRegisterLocationToAtCFAPlusOffset(fp_reg_num, ptr_size * -2, true);
1886   row->SetRegisterLocationToAtCFAPlusOffset(pc_reg_num, ptr_size * -1, true);
1887   row->SetRegisterLocationToIsCFAPlusOffset(sp_reg_num, 0, true);
1888
1889   unwind_plan.AppendRow(row);
1890   unwind_plan.SetSourceName("x86_64 default unwind plan");
1891   unwind_plan.SetSourcedFromCompiler(eLazyBoolNo);
1892   unwind_plan.SetUnwindPlanValidAtAllInstructions(eLazyBoolNo);
1893   return true;
1894 }
1895
1896 bool ABISysV_x86_64::RegisterIsVolatile(const RegisterInfo *reg_info) {
1897   return !RegisterIsCalleeSaved(reg_info);
1898 }
1899
1900 // See "Register Usage" in the
1901 // "System V Application Binary Interface"
1902 // "AMD64 Architecture Processor Supplement"
1903 // (or "x86-64(tm) Architecture Processor Supplement" in earlier revisions)
1904 // (this doc is also commonly referred to as the x86-64/AMD64 psABI)
1905 // Edited by Michael Matz, Jan Hubicka, Andreas Jaeger, and Mark Mitchell
1906 // current version is 0.99.6 released 2012-07-02 at
1907 // http://refspecs.linuxfoundation.org/elf/x86-64-abi-0.99.pdf
1908 // It's being revised & updated at https://github.com/hjl-tools/x86-psABI/
1909
1910 bool ABISysV_x86_64::RegisterIsCalleeSaved(const RegisterInfo *reg_info) {
1911   if (reg_info) {
1912     // Preserved registers are :
1913     //    rbx, rsp, rbp, r12, r13, r14, r15
1914     //    mxcsr (partially preserved)
1915     //    x87 control word
1916
1917     const char *name = reg_info->name;
1918     if (name[0] == 'r') {
1919       switch (name[1]) {
1920       case '1': // r12, r13, r14, r15
1921         if (name[2] >= '2' && name[2] <= '5')
1922           return name[3] == '\0';
1923         break;
1924
1925       default:
1926         break;
1927       }
1928     }
1929
1930     // Accept shorter-variant versions, rbx/ebx, rip/ eip, etc.
1931     if (name[0] == 'r' || name[0] == 'e') {
1932       switch (name[1]) {
1933       case 'b': // rbp, rbx
1934         if (name[2] == 'p' || name[2] == 'x')
1935           return name[3] == '\0';
1936         break;
1937
1938       case 'i': // rip
1939         if (name[2] == 'p')
1940           return name[3] == '\0';
1941         break;
1942
1943       case 's': // rsp
1944         if (name[2] == 'p')
1945           return name[3] == '\0';
1946         break;
1947       }
1948     }
1949     if (name[0] == 's' && name[1] == 'p' && name[2] == '\0') // sp
1950       return true;
1951     if (name[0] == 'f' && name[1] == 'p' && name[2] == '\0') // fp
1952       return true;
1953     if (name[0] == 'p' && name[1] == 'c' && name[2] == '\0') // pc
1954       return true;
1955   }
1956   return false;
1957 }
1958
1959 void ABISysV_x86_64::Initialize() {
1960   PluginManager::RegisterPlugin(
1961       GetPluginNameStatic(), "System V ABI for x86_64 targets", CreateInstance);
1962 }
1963
1964 void ABISysV_x86_64::Terminate() {
1965   PluginManager::UnregisterPlugin(CreateInstance);
1966 }
1967
1968 lldb_private::ConstString ABISysV_x86_64::GetPluginNameStatic() {
1969   static ConstString g_name("sysv-x86_64");
1970   return g_name;
1971 }
1972
1973 //------------------------------------------------------------------
1974 // PluginInterface protocol
1975 //------------------------------------------------------------------
1976
1977 lldb_private::ConstString ABISysV_x86_64::GetPluginName() {
1978   return GetPluginNameStatic();
1979 }
1980
1981 uint32_t ABISysV_x86_64::GetPluginVersion() { return 1; }