mirrored from https://chromium.googlesource.com/v8/v8.git
/
gdb-jit.cc
2159 lines (1815 loc) · 60.4 KB
/
gdb-jit.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifdef ENABLE_GDB_JIT_INTERFACE
#include "v8.h"
#include "gdb-jit.h"
#include "bootstrapper.h"
#include "compiler.h"
#include "global-handles.h"
#include "messages.h"
#include "platform.h"
#include "natives.h"
#include "scopeinfo.h"
namespace v8 {
namespace internal {
#ifdef __APPLE__
#define __MACH_O
class MachO;
class MachOSection;
typedef MachO DebugObject;
typedef MachOSection DebugSection;
#else
#define __ELF
class ELF;
class ELFSection;
typedef ELF DebugObject;
typedef ELFSection DebugSection;
#endif
class Writer BASE_EMBEDDED {
public:
explicit Writer(DebugObject* debug_object)
: debug_object_(debug_object),
position_(0),
capacity_(1024),
buffer_(reinterpret_cast<byte*>(malloc(capacity_))) {
}
~Writer() {
free(buffer_);
}
uintptr_t position() const {
return position_;
}
template<typename T>
class Slot {
public:
Slot(Writer* w, uintptr_t offset) : w_(w), offset_(offset) { }
T* operator-> () {
return w_->RawSlotAt<T>(offset_);
}
void set(const T& value) {
*w_->RawSlotAt<T>(offset_) = value;
}
Slot<T> at(int i) {
return Slot<T>(w_, offset_ + sizeof(T) * i);
}
private:
Writer* w_;
uintptr_t offset_;
};
template<typename T>
void Write(const T& val) {
Ensure(position_ + sizeof(T));
*RawSlotAt<T>(position_) = val;
position_ += sizeof(T);
}
template<typename T>
Slot<T> SlotAt(uintptr_t offset) {
Ensure(offset + sizeof(T));
return Slot<T>(this, offset);
}
template<typename T>
Slot<T> CreateSlotHere() {
return CreateSlotsHere<T>(1);
}
template<typename T>
Slot<T> CreateSlotsHere(uint32_t count) {
uintptr_t slot_position = position_;
position_ += sizeof(T) * count;
Ensure(position_);
return SlotAt<T>(slot_position);
}
void Ensure(uintptr_t pos) {
if (capacity_ < pos) {
while (capacity_ < pos) capacity_ *= 2;
buffer_ = reinterpret_cast<byte*>(realloc(buffer_, capacity_));
}
}
DebugObject* debug_object() { return debug_object_; }
byte* buffer() { return buffer_; }
void Align(uintptr_t align) {
uintptr_t delta = position_ % align;
if (delta == 0) return;
uintptr_t padding = align - delta;
Ensure(position_ += padding);
ASSERT((position_ % align) == 0);
}
void WriteULEB128(uintptr_t value) {
do {
uint8_t byte = value & 0x7F;
value >>= 7;
if (value != 0) byte |= 0x80;
Write<uint8_t>(byte);
} while (value != 0);
}
void WriteSLEB128(intptr_t value) {
bool more = true;
while (more) {
int8_t byte = value & 0x7F;
bool byte_sign = byte & 0x40;
value >>= 7;
if ((value == 0 && !byte_sign) || (value == -1 && byte_sign)) {
more = false;
} else {
byte |= 0x80;
}
Write<int8_t>(byte);
}
}
void WriteString(const char* str) {
do {
Write<char>(*str);
} while (*str++);
}
private:
template<typename T> friend class Slot;
template<typename T>
T* RawSlotAt(uintptr_t offset) {
ASSERT(offset < capacity_ && offset + sizeof(T) <= capacity_);
return reinterpret_cast<T*>(&buffer_[offset]);
}
DebugObject* debug_object_;
uintptr_t position_;
uintptr_t capacity_;
byte* buffer_;
};
class StringTable;
template<typename THeader>
class DebugSectionBase : public ZoneObject {
public:
virtual ~DebugSectionBase() { }
virtual void WriteBody(Writer::Slot<THeader> header, Writer* writer) {
uintptr_t start = writer->position();
if (WriteBody(writer)) {
uintptr_t end = writer->position();
header->offset = start;
#if defined(__MACH_O)
header->addr = 0;
#endif
header->size = end - start;
}
}
virtual bool WriteBody(Writer* writer) {
return false;
}
typedef THeader Header;
};
struct MachOSectionHeader {
char sectname[16];
char segname[16];
#if defined(V8_TARGET_ARCH_IA32)
uint32_t addr;
uint32_t size;
#else
uint64_t addr;
uint64_t size;
#endif
uint32_t offset;
uint32_t align;
uint32_t reloff;
uint32_t nreloc;
uint32_t flags;
uint32_t reserved1;
uint32_t reserved2;
};
class MachOSection : public DebugSectionBase<MachOSectionHeader> {
public:
enum Type {
S_REGULAR = 0x0u,
S_ATTR_COALESCED = 0xbu,
S_ATTR_SOME_INSTRUCTIONS = 0x400u,
S_ATTR_DEBUG = 0x02000000u,
S_ATTR_PURE_INSTRUCTIONS = 0x80000000u
};
MachOSection(const char* name,
const char* segment,
uintptr_t align,
uint32_t flags)
: name_(name),
segment_(segment),
align_(align),
flags_(flags) {
ASSERT(IsPowerOf2(align));
if (align_ != 0) {
align_ = WhichPowerOf2(align_);
}
}
virtual ~MachOSection() { }
virtual void PopulateHeader(Writer::Slot<Header> header) {
header->addr = 0;
header->size = 0;
header->offset = 0;
header->align = align_;
header->reloff = 0;
header->nreloc = 0;
header->flags = flags_;
header->reserved1 = 0;
header->reserved2 = 0;
memset(header->sectname, 0, sizeof(header->sectname));
memset(header->segname, 0, sizeof(header->segname));
ASSERT(strlen(name_) < sizeof(header->sectname));
ASSERT(strlen(segment_) < sizeof(header->segname));
strncpy(header->sectname, name_, sizeof(header->sectname));
strncpy(header->segname, segment_, sizeof(header->segname));
}
private:
const char* name_;
const char* segment_;
uintptr_t align_;
uint32_t flags_;
};
struct ELFSectionHeader {
uint32_t name;
uint32_t type;
uintptr_t flags;
uintptr_t address;
uintptr_t offset;
uintptr_t size;
uint32_t link;
uint32_t info;
uintptr_t alignment;
uintptr_t entry_size;
};
#if defined(__ELF)
class ELFSection : public DebugSectionBase<ELFSectionHeader> {
public:
enum Type {
TYPE_NULL = 0,
TYPE_PROGBITS = 1,
TYPE_SYMTAB = 2,
TYPE_STRTAB = 3,
TYPE_RELA = 4,
TYPE_HASH = 5,
TYPE_DYNAMIC = 6,
TYPE_NOTE = 7,
TYPE_NOBITS = 8,
TYPE_REL = 9,
TYPE_SHLIB = 10,
TYPE_DYNSYM = 11,
TYPE_LOPROC = 0x70000000,
TYPE_X86_64_UNWIND = 0x70000001,
TYPE_HIPROC = 0x7fffffff,
TYPE_LOUSER = 0x80000000,
TYPE_HIUSER = 0xffffffff
};
enum Flags {
FLAG_WRITE = 1,
FLAG_ALLOC = 2,
FLAG_EXEC = 4
};
enum SpecialIndexes {
INDEX_ABSOLUTE = 0xfff1
};
ELFSection(const char* name, Type type, uintptr_t align)
: name_(name), type_(type), align_(align) { }
virtual ~ELFSection() { }
void PopulateHeader(Writer::Slot<Header> header, StringTable* strtab);
virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
uintptr_t start = w->position();
if (WriteBody(w)) {
uintptr_t end = w->position();
header->offset = start;
header->size = end - start;
}
}
virtual bool WriteBody(Writer* w) {
return false;
}
uint16_t index() const { return index_; }
void set_index(uint16_t index) { index_ = index; }
protected:
virtual void PopulateHeader(Writer::Slot<Header> header) {
header->flags = 0;
header->address = 0;
header->offset = 0;
header->size = 0;
header->link = 0;
header->info = 0;
header->entry_size = 0;
}
private:
const char* name_;
Type type_;
uintptr_t align_;
uint16_t index_;
};
#endif // defined(__ELF)
#if defined(__MACH_O)
class MachOTextSection : public MachOSection {
public:
MachOTextSection(uintptr_t align,
uintptr_t addr,
uintptr_t size)
: MachOSection("__text",
"__TEXT",
align,
MachOSection::S_REGULAR |
MachOSection::S_ATTR_SOME_INSTRUCTIONS |
MachOSection::S_ATTR_PURE_INSTRUCTIONS),
addr_(addr),
size_(size) { }
protected:
virtual void PopulateHeader(Writer::Slot<Header> header) {
MachOSection::PopulateHeader(header);
header->addr = addr_;
header->size = size_;
}
private:
uintptr_t addr_;
uintptr_t size_;
};
#endif // defined(__MACH_O)
#if defined(__ELF)
class FullHeaderELFSection : public ELFSection {
public:
FullHeaderELFSection(const char* name,
Type type,
uintptr_t align,
uintptr_t addr,
uintptr_t offset,
uintptr_t size,
uintptr_t flags)
: ELFSection(name, type, align),
addr_(addr),
offset_(offset),
size_(size),
flags_(flags) { }
protected:
virtual void PopulateHeader(Writer::Slot<Header> header) {
ELFSection::PopulateHeader(header);
header->address = addr_;
header->offset = offset_;
header->size = size_;
header->flags = flags_;
}
private:
uintptr_t addr_;
uintptr_t offset_;
uintptr_t size_;
uintptr_t flags_;
};
class StringTable : public ELFSection {
public:
explicit StringTable(const char* name)
: ELFSection(name, TYPE_STRTAB, 1), writer_(NULL), offset_(0), size_(0) {
}
uintptr_t Add(const char* str) {
if (*str == '\0') return 0;
uintptr_t offset = size_;
WriteString(str);
return offset;
}
void AttachWriter(Writer* w) {
writer_ = w;
offset_ = writer_->position();
// First entry in the string table should be an empty string.
WriteString("");
}
void DetachWriter() {
writer_ = NULL;
}
virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
ASSERT(writer_ == NULL);
header->offset = offset_;
header->size = size_;
}
private:
void WriteString(const char* str) {
uintptr_t written = 0;
do {
writer_->Write(*str);
written++;
} while (*str++);
size_ += written;
}
Writer* writer_;
uintptr_t offset_;
uintptr_t size_;
};
void ELFSection::PopulateHeader(Writer::Slot<ELFSection::Header> header,
StringTable* strtab) {
header->name = strtab->Add(name_);
header->type = type_;
header->alignment = align_;
PopulateHeader(header);
}
#endif // defined(__ELF)
#if defined(__MACH_O)
class MachO BASE_EMBEDDED {
public:
MachO() : sections_(6) { }
uint32_t AddSection(MachOSection* section) {
sections_.Add(section);
return sections_.length() - 1;
}
void Write(Writer* w, uintptr_t code_start, uintptr_t code_size) {
Writer::Slot<MachOHeader> header = WriteHeader(w);
uintptr_t load_command_start = w->position();
Writer::Slot<MachOSegmentCommand> cmd = WriteSegmentCommand(w,
code_start,
code_size);
WriteSections(w, cmd, header, load_command_start);
}
private:
struct MachOHeader {
uint32_t magic;
uint32_t cputype;
uint32_t cpusubtype;
uint32_t filetype;
uint32_t ncmds;
uint32_t sizeofcmds;
uint32_t flags;
#if defined(V8_TARGET_ARCH_X64)
uint32_t reserved;
#endif
};
struct MachOSegmentCommand {
uint32_t cmd;
uint32_t cmdsize;
char segname[16];
#if defined(V8_TARGET_ARCH_IA32)
uint32_t vmaddr;
uint32_t vmsize;
uint32_t fileoff;
uint32_t filesize;
#else
uint64_t vmaddr;
uint64_t vmsize;
uint64_t fileoff;
uint64_t filesize;
#endif
uint32_t maxprot;
uint32_t initprot;
uint32_t nsects;
uint32_t flags;
};
enum MachOLoadCommandCmd {
LC_SEGMENT_32 = 0x00000001u,
LC_SEGMENT_64 = 0x00000019u
};
Writer::Slot<MachOHeader> WriteHeader(Writer* w) {
ASSERT(w->position() == 0);
Writer::Slot<MachOHeader> header = w->CreateSlotHere<MachOHeader>();
#if defined(V8_TARGET_ARCH_IA32)
header->magic = 0xFEEDFACEu;
header->cputype = 7; // i386
header->cpusubtype = 3; // CPU_SUBTYPE_I386_ALL
#elif defined(V8_TARGET_ARCH_X64)
header->magic = 0xFEEDFACFu;
header->cputype = 7 | 0x01000000; // i386 | 64-bit ABI
header->cpusubtype = 3; // CPU_SUBTYPE_I386_ALL
header->reserved = 0;
#else
#error Unsupported target architecture.
#endif
header->filetype = 0x1; // MH_OBJECT
header->ncmds = 1;
header->sizeofcmds = 0;
header->flags = 0;
return header;
}
Writer::Slot<MachOSegmentCommand> WriteSegmentCommand(Writer* w,
uintptr_t code_start,
uintptr_t code_size) {
Writer::Slot<MachOSegmentCommand> cmd =
w->CreateSlotHere<MachOSegmentCommand>();
#if defined(V8_TARGET_ARCH_IA32)
cmd->cmd = LC_SEGMENT_32;
#else
cmd->cmd = LC_SEGMENT_64;
#endif
cmd->vmaddr = code_start;
cmd->vmsize = code_size;
cmd->fileoff = 0;
cmd->filesize = 0;
cmd->maxprot = 7;
cmd->initprot = 7;
cmd->flags = 0;
cmd->nsects = sections_.length();
memset(cmd->segname, 0, 16);
cmd->cmdsize = sizeof(MachOSegmentCommand) + sizeof(MachOSection::Header) *
cmd->nsects;
return cmd;
}
void WriteSections(Writer* w,
Writer::Slot<MachOSegmentCommand> cmd,
Writer::Slot<MachOHeader> header,
uintptr_t load_command_start) {
Writer::Slot<MachOSection::Header> headers =
w->CreateSlotsHere<MachOSection::Header>(sections_.length());
cmd->fileoff = w->position();
header->sizeofcmds = w->position() - load_command_start;
for (int section = 0; section < sections_.length(); ++section) {
sections_[section]->PopulateHeader(headers.at(section));
sections_[section]->WriteBody(headers.at(section), w);
}
cmd->filesize = w->position() - (uintptr_t)cmd->fileoff;
}
ZoneList<MachOSection*> sections_;
};
#endif // defined(__MACH_O)
#if defined(__ELF)
class ELF BASE_EMBEDDED {
public:
ELF() : sections_(6) {
sections_.Add(new ELFSection("", ELFSection::TYPE_NULL, 0));
sections_.Add(new StringTable(".shstrtab"));
}
void Write(Writer* w) {
WriteHeader(w);
WriteSectionTable(w);
WriteSections(w);
}
ELFSection* SectionAt(uint32_t index) {
return sections_[index];
}
uint32_t AddSection(ELFSection* section) {
sections_.Add(section);
section->set_index(sections_.length() - 1);
return sections_.length() - 1;
}
private:
struct ELFHeader {
uint8_t ident[16];
uint16_t type;
uint16_t machine;
uint32_t version;
uintptr_t entry;
uintptr_t pht_offset;
uintptr_t sht_offset;
uint32_t flags;
uint16_t header_size;
uint16_t pht_entry_size;
uint16_t pht_entry_num;
uint16_t sht_entry_size;
uint16_t sht_entry_num;
uint16_t sht_strtab_index;
};
void WriteHeader(Writer* w) {
ASSERT(w->position() == 0);
Writer::Slot<ELFHeader> header = w->CreateSlotHere<ELFHeader>();
#if defined(V8_TARGET_ARCH_IA32) || defined(V8_TARGET_ARCH_ARM)
const uint8_t ident[16] =
{ 0x7f, 'E', 'L', 'F', 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
#elif defined(V8_TARGET_ARCH_X64)
const uint8_t ident[16] =
{ 0x7f, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0 , 0, 0, 0, 0, 0, 0};
#else
#error Unsupported target architecture.
#endif
memcpy(header->ident, ident, 16);
header->type = 1;
#if defined(V8_TARGET_ARCH_IA32)
header->machine = 3;
#elif defined(V8_TARGET_ARCH_X64)
// Processor identification value for x64 is 62 as defined in
// System V ABI, AMD64 Supplement
// http://www.x86-64.org/documentation/abi.pdf
header->machine = 62;
#elif defined(V8_TARGET_ARCH_ARM)
// Set to EM_ARM, defined as 40, in "ARM ELF File Format" at
// infocenter.arm.com/help/topic/com.arm.doc.dui0101a/DUI0101A_Elf.pdf
header->machine = 40;
#else
#error Unsupported target architecture.
#endif
header->version = 1;
header->entry = 0;
header->pht_offset = 0;
header->sht_offset = sizeof(ELFHeader); // Section table follows header.
header->flags = 0;
header->header_size = sizeof(ELFHeader);
header->pht_entry_size = 0;
header->pht_entry_num = 0;
header->sht_entry_size = sizeof(ELFSection::Header);
header->sht_entry_num = sections_.length();
header->sht_strtab_index = 1;
}
void WriteSectionTable(Writer* w) {
// Section headers table immediately follows file header.
ASSERT(w->position() == sizeof(ELFHeader));
Writer::Slot<ELFSection::Header> headers =
w->CreateSlotsHere<ELFSection::Header>(sections_.length());
// String table for section table is the first section.
StringTable* strtab = static_cast<StringTable*>(SectionAt(1));
strtab->AttachWriter(w);
for (int i = 0, length = sections_.length();
i < length;
i++) {
sections_[i]->PopulateHeader(headers.at(i), strtab);
}
strtab->DetachWriter();
}
int SectionHeaderPosition(uint32_t section_index) {
return sizeof(ELFHeader) + sizeof(ELFSection::Header) * section_index;
}
void WriteSections(Writer* w) {
Writer::Slot<ELFSection::Header> headers =
w->SlotAt<ELFSection::Header>(sizeof(ELFHeader));
for (int i = 0, length = sections_.length();
i < length;
i++) {
sections_[i]->WriteBody(headers.at(i), w);
}
}
ZoneList<ELFSection*> sections_;
};
class ELFSymbol BASE_EMBEDDED {
public:
enum Type {
TYPE_NOTYPE = 0,
TYPE_OBJECT = 1,
TYPE_FUNC = 2,
TYPE_SECTION = 3,
TYPE_FILE = 4,
TYPE_LOPROC = 13,
TYPE_HIPROC = 15
};
enum Binding {
BIND_LOCAL = 0,
BIND_GLOBAL = 1,
BIND_WEAK = 2,
BIND_LOPROC = 13,
BIND_HIPROC = 15
};
ELFSymbol(const char* name,
uintptr_t value,
uintptr_t size,
Binding binding,
Type type,
uint16_t section)
: name(name),
value(value),
size(size),
info((binding << 4) | type),
other(0),
section(section) {
}
Binding binding() const {
return static_cast<Binding>(info >> 4);
}
#if defined(V8_TARGET_ARCH_IA32) || defined(V8_TARGET_ARCH_ARM)
struct SerializedLayout {
SerializedLayout(uint32_t name,
uintptr_t value,
uintptr_t size,
Binding binding,
Type type,
uint16_t section)
: name(name),
value(value),
size(size),
info((binding << 4) | type),
other(0),
section(section) {
}
uint32_t name;
uintptr_t value;
uintptr_t size;
uint8_t info;
uint8_t other;
uint16_t section;
};
#elif defined(V8_TARGET_ARCH_X64)
struct SerializedLayout {
SerializedLayout(uint32_t name,
uintptr_t value,
uintptr_t size,
Binding binding,
Type type,
uint16_t section)
: name(name),
info((binding << 4) | type),
other(0),
section(section),
value(value),
size(size) {
}
uint32_t name;
uint8_t info;
uint8_t other;
uint16_t section;
uintptr_t value;
uintptr_t size;
};
#endif
void Write(Writer::Slot<SerializedLayout> s, StringTable* t) {
// Convert symbol names from strings to indexes in the string table.
s->name = t->Add(name);
s->value = value;
s->size = size;
s->info = info;
s->other = other;
s->section = section;
}
private:
const char* name;
uintptr_t value;
uintptr_t size;
uint8_t info;
uint8_t other;
uint16_t section;
};
class ELFSymbolTable : public ELFSection {
public:
explicit ELFSymbolTable(const char* name)
: ELFSection(name, TYPE_SYMTAB, sizeof(uintptr_t)),
locals_(1),
globals_(1) {
}
virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
w->Align(header->alignment);
int total_symbols = locals_.length() + globals_.length() + 1;
header->offset = w->position();
Writer::Slot<ELFSymbol::SerializedLayout> symbols =
w->CreateSlotsHere<ELFSymbol::SerializedLayout>(total_symbols);
header->size = w->position() - header->offset;
// String table for this symbol table should follow it in the section table.
StringTable* strtab =
static_cast<StringTable*>(w->debug_object()->SectionAt(index() + 1));
strtab->AttachWriter(w);
symbols.at(0).set(ELFSymbol::SerializedLayout(0,
0,
0,
ELFSymbol::BIND_LOCAL,
ELFSymbol::TYPE_NOTYPE,
0));
WriteSymbolsList(&locals_, symbols.at(1), strtab);
WriteSymbolsList(&globals_, symbols.at(locals_.length() + 1), strtab);
strtab->DetachWriter();
}
void Add(const ELFSymbol& symbol) {
if (symbol.binding() == ELFSymbol::BIND_LOCAL) {
locals_.Add(symbol);
} else {
globals_.Add(symbol);
}
}
protected:
virtual void PopulateHeader(Writer::Slot<Header> header) {
ELFSection::PopulateHeader(header);
// We are assuming that string table will follow symbol table.
header->link = index() + 1;
header->info = locals_.length() + 1;
header->entry_size = sizeof(ELFSymbol::SerializedLayout);
}
private:
void WriteSymbolsList(const ZoneList<ELFSymbol>* src,
Writer::Slot<ELFSymbol::SerializedLayout> dst,
StringTable* strtab) {
for (int i = 0, len = src->length();
i < len;
i++) {
src->at(i).Write(dst.at(i), strtab);
}
}
ZoneList<ELFSymbol> locals_;
ZoneList<ELFSymbol> globals_;
};
#endif // defined(__ELF)
class CodeDescription BASE_EMBEDDED {
public:
#ifdef V8_TARGET_ARCH_X64
enum StackState {
POST_RBP_PUSH,
POST_RBP_SET,
POST_RBP_POP,
STACK_STATE_MAX
};
#endif
CodeDescription(const char* name,
Code* code,
Handle<Script> script,
GDBJITLineInfo* lineinfo,
GDBJITInterface::CodeTag tag,
CompilationInfo* info)
: name_(name),
code_(code),
script_(script),
lineinfo_(lineinfo),
tag_(tag),
info_(info) {
}
const char* name() const {
return name_;
}
GDBJITLineInfo* lineinfo() const {
return lineinfo_;
}
GDBJITInterface::CodeTag tag() const {
return tag_;
}
CompilationInfo* info() const {
return info_;
}
bool IsInfoAvailable() const {
return info_ != NULL;
}
uintptr_t CodeStart() const {
return reinterpret_cast<uintptr_t>(code_->instruction_start());
}
uintptr_t CodeEnd() const {
return reinterpret_cast<uintptr_t>(code_->instruction_end());
}
uintptr_t CodeSize() const {
return CodeEnd() - CodeStart();
}
bool IsLineInfoAvailable() {
return !script_.is_null() &&
script_->source()->IsString() &&
script_->HasValidSource() &&
script_->name()->IsString() &&
lineinfo_ != NULL;
}
#ifdef V8_TARGET_ARCH_X64
uintptr_t GetStackStateStartAddress(StackState state) const {
ASSERT(state < STACK_STATE_MAX);
return stack_state_start_addresses_[state];
}
void SetStackStateStartAddress(StackState state, uintptr_t addr) {
ASSERT(state < STACK_STATE_MAX);
stack_state_start_addresses_[state] = addr;
}
#endif
SmartArrayPointer<char> GetFilename() {
return String::cast(script_->name())->ToCString();
}