blob: c26ecf5ea84f302869102b7f2543b99ffeb9c762 [file] [log] [blame]
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifdef ENABLE_GDB_JIT_INTERFACE
29#include "gdb-jit.h"
30
31#include "bootstrapper.h"
32#include "compiler.h"
33#include "global-handles.h"
34#include "messages.h"
35#include "natives.h"
36
37namespace v8 {
38namespace internal {
39
40class ELF;
41
42class Writer BASE_EMBEDDED {
43 public:
44 explicit Writer(ELF* elf)
45 : elf_(elf),
46 position_(0),
47 capacity_(1024),
48 buffer_(reinterpret_cast<byte*>(malloc(capacity_))) {
49 }
50
51 ~Writer() {
52 free(buffer_);
53 }
54
55 uintptr_t position() const {
56 return position_;
57 }
58
59 template<typename T>
60 class Slot {
61 public:
62 Slot(Writer* w, uintptr_t offset) : w_(w), offset_(offset) { }
63
64 T* operator-> () {
65 return w_->RawSlotAt<T>(offset_);
66 }
67
68 void set(const T& value) {
69 *w_->RawSlotAt<T>(offset_) = value;
70 }
71
72 Slot<T> at(int i) {
73 return Slot<T>(w_, offset_ + sizeof(T) * i);
74 }
75
76 private:
77 Writer* w_;
78 uintptr_t offset_;
79 };
80
81 template<typename T>
82 void Write(const T& val) {
83 Ensure(position_ + sizeof(T));
84 *RawSlotAt<T>(position_) = val;
85 position_ += sizeof(T);
86 }
87
88 template<typename T>
89 Slot<T> SlotAt(uintptr_t offset) {
90 Ensure(offset + sizeof(T));
91 return Slot<T>(this, offset);
92 }
93
94 template<typename T>
95 Slot<T> CreateSlotHere() {
96 return CreateSlotsHere<T>(1);
97 }
98
99 template<typename T>
100 Slot<T> CreateSlotsHere(uint32_t count) {
101 uintptr_t slot_position = position_;
102 position_ += sizeof(T) * count;
103 Ensure(position_);
104 return SlotAt<T>(slot_position);
105 }
106
107 void Ensure(uintptr_t pos) {
108 if (capacity_ < pos) {
109 while (capacity_ < pos) capacity_ *= 2;
110 buffer_ = reinterpret_cast<byte*>(realloc(buffer_, capacity_));
111 }
112 }
113
114 ELF* elf() { return elf_; }
115
116 byte* buffer() { return buffer_; }
117
118 void Align(uintptr_t align) {
119 uintptr_t delta = position_ % align;
120 if (delta == 0) return;
121 uintptr_t padding = align - delta;
122 Ensure(position_ += padding);
123 ASSERT((position_ % align) == 0);
124 }
125
126 void WriteULEB128(uintptr_t value) {
127 do {
128 uint8_t byte = value & 0x7F;
129 value >>= 7;
130 if (value != 0) byte |= 0x80;
131 Write<uint8_t>(byte);
132 } while (value != 0);
133 }
134
135 void WriteSLEB128(intptr_t value) {
136 bool more = true;
137 while (more) {
138 int8_t byte = value & 0x7F;
139 bool byte_sign = byte & 0x40;
140 value >>= 7;
141
142 if ((value == 0 && !byte_sign) || (value == -1 && byte_sign)) {
143 more = false;
144 } else {
145 byte |= 0x80;
146 }
147
148 Write<int8_t>(byte);
149 }
150 }
151
152 void WriteString(const char* str) {
153 do {
154 Write<char>(*str);
155 } while (*str++);
156 }
157
158 private:
159 template<typename T> friend class Slot;
160
161 template<typename T>
162 T* RawSlotAt(uintptr_t offset) {
163 ASSERT(offset < capacity_ && offset + sizeof(T) <= capacity_);
164 return reinterpret_cast<T*>(&buffer_[offset]);
165 }
166
167 ELF* elf_;
168 uintptr_t position_;
169 uintptr_t capacity_;
170 byte* buffer_;
171};
172
173class StringTable;
174
175class ELFSection : public ZoneObject {
176 public:
177 struct Header {
178 uint32_t name;
179 uint32_t type;
180 uintptr_t flags;
181 uintptr_t address;
182 uintptr_t offset;
183 uintptr_t size;
184 uint32_t link;
185 uint32_t info;
186 uintptr_t alignment;
187 uintptr_t entry_size;
188 };
189
190 enum Type {
191 TYPE_NULL = 0,
192 TYPE_PROGBITS = 1,
193 TYPE_SYMTAB = 2,
194 TYPE_STRTAB = 3,
195 TYPE_RELA = 4,
196 TYPE_HASH = 5,
197 TYPE_DYNAMIC = 6,
198 TYPE_NOTE = 7,
199 TYPE_NOBITS = 8,
200 TYPE_REL = 9,
201 TYPE_SHLIB = 10,
202 TYPE_DYNSYM = 11,
203 TYPE_LOPROC = 0x70000000,
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000204 TYPE_X86_64_UNWIND = 0x70000001,
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000205 TYPE_HIPROC = 0x7fffffff,
206 TYPE_LOUSER = 0x80000000,
207 TYPE_HIUSER = 0xffffffff
208 };
209
210 enum Flags {
211 FLAG_WRITE = 1,
212 FLAG_ALLOC = 2,
213 FLAG_EXEC = 4
214 };
215
216 enum SpecialIndexes {
217 INDEX_ABSOLUTE = 0xfff1
218 };
219
220 ELFSection(const char* name, Type type, uintptr_t align)
221 : name_(name), type_(type), align_(align) { }
222
223 virtual ~ELFSection() { }
224
225 void PopulateHeader(Writer::Slot<Header> header, StringTable* strtab);
226
227 virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
228 uintptr_t start = w->position();
229 if (WriteBody(w)) {
230 uintptr_t end = w->position();
231 header->offset = start;
232 header->size = end - start;
233 }
234 }
235
236 virtual bool WriteBody(Writer* w) {
237 return false;
238 }
239
240 uint16_t index() const { return index_; }
241 void set_index(uint16_t index) { index_ = index; }
242
243 protected:
244 virtual void PopulateHeader(Writer::Slot<Header> header) {
245 header->flags = 0;
246 header->address = 0;
247 header->offset = 0;
248 header->size = 0;
249 header->link = 0;
250 header->info = 0;
251 header->entry_size = 0;
252 }
253
254
255 private:
256 const char* name_;
257 Type type_;
258 uintptr_t align_;
259 uint16_t index_;
260};
261
262
263class FullHeaderELFSection : public ELFSection {
264 public:
265 FullHeaderELFSection(const char* name,
266 Type type,
267 uintptr_t align,
268 uintptr_t addr,
269 uintptr_t offset,
270 uintptr_t size,
271 uintptr_t flags)
272 : ELFSection(name, type, align),
273 addr_(addr),
274 offset_(offset),
275 size_(size),
276 flags_(flags) { }
277
278 protected:
279 virtual void PopulateHeader(Writer::Slot<Header> header) {
280 ELFSection::PopulateHeader(header);
281 header->address = addr_;
282 header->offset = offset_;
283 header->size = size_;
284 header->flags = flags_;
285 }
286
287 private:
288 uintptr_t addr_;
289 uintptr_t offset_;
290 uintptr_t size_;
291 uintptr_t flags_;
292};
293
294
295class StringTable : public ELFSection {
296 public:
297 explicit StringTable(const char* name)
298 : ELFSection(name, TYPE_STRTAB, 1), writer_(NULL), offset_(0), size_(0) {
299 }
300
301 uintptr_t Add(const char* str) {
302 if (*str == '\0') return 0;
303
304 uintptr_t offset = size_;
305 WriteString(str);
306 return offset;
307 }
308
309 void AttachWriter(Writer* w) {
310 writer_ = w;
311 offset_ = writer_->position();
312
313 // First entry in the string table should be an empty string.
314 WriteString("");
315 }
316
317 void DetachWriter() {
318 writer_ = NULL;
319 }
320
321 virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
322 ASSERT(writer_ == NULL);
323 header->offset = offset_;
324 header->size = size_;
325 }
326
327 private:
328 void WriteString(const char* str) {
329 uintptr_t written = 0;
330 do {
331 writer_->Write(*str);
332 written++;
333 } while (*str++);
334 size_ += written;
335 }
336
337 Writer* writer_;
338
339 uintptr_t offset_;
340 uintptr_t size_;
341};
342
343
344void ELFSection::PopulateHeader(Writer::Slot<ELFSection::Header> header,
345 StringTable* strtab) {
346 header->name = strtab->Add(name_);
347 header->type = type_;
348 header->alignment = align_;
349 PopulateHeader(header);
350}
351
352
353class ELF BASE_EMBEDDED {
354 public:
355 ELF() : sections_(6) {
356 sections_.Add(new ELFSection("", ELFSection::TYPE_NULL, 0));
357 sections_.Add(new StringTable(".shstrtab"));
358 }
359
360 void Write(Writer* w) {
361 WriteHeader(w);
362 WriteSectionTable(w);
363 WriteSections(w);
364 }
365
366 ELFSection* SectionAt(uint32_t index) {
367 return sections_[index];
368 }
369
370 uint32_t AddSection(ELFSection* section) {
371 sections_.Add(section);
372 section->set_index(sections_.length() - 1);
373 return sections_.length() - 1;
374 }
375
376 private:
377 struct ELFHeader {
378 uint8_t ident[16];
379 uint16_t type;
380 uint16_t machine;
381 uint32_t version;
382 uintptr_t entry;
383 uintptr_t pht_offset;
384 uintptr_t sht_offset;
385 uint32_t flags;
386 uint16_t header_size;
387 uint16_t pht_entry_size;
388 uint16_t pht_entry_num;
389 uint16_t sht_entry_size;
390 uint16_t sht_entry_num;
391 uint16_t sht_strtab_index;
392 };
393
394
395 void WriteHeader(Writer* w) {
396 ASSERT(w->position() == 0);
397 Writer::Slot<ELFHeader> header = w->CreateSlotHere<ELFHeader>();
398#if defined(V8_TARGET_ARCH_IA32)
399 const uint8_t ident[16] =
400 { 0x7f, 'E', 'L', 'F', 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
401#elif defined(V8_TARGET_ARCH_X64)
402 const uint8_t ident[16] =
403 { 0x7f, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0 , 0, 0, 0, 0, 0, 0};
404#else
405#error Unsupported target architecture.
406#endif
407 memcpy(header->ident, ident, 16);
408 header->type = 1;
409#if defined(V8_TARGET_ARCH_IA32)
410 header->machine = 3;
411#elif defined(V8_TARGET_ARCH_X64)
412 // Processor identification value for x64 is 62 as defined in
413 // System V ABI, AMD64 Supplement
414 // http://www.x86-64.org/documentation/abi.pdf
415 header->machine = 62;
416#else
417#error Unsupported target architecture.
418#endif
419 header->version = 1;
420 header->entry = 0;
421 header->pht_offset = 0;
422 header->sht_offset = sizeof(ELFHeader); // Section table follows header.
423 header->flags = 0;
424 header->header_size = sizeof(ELFHeader);
425 header->pht_entry_size = 0;
426 header->pht_entry_num = 0;
427 header->sht_entry_size = sizeof(ELFSection::Header);
428 header->sht_entry_num = sections_.length();
429 header->sht_strtab_index = 1;
430 }
431
432 void WriteSectionTable(Writer* w) {
433 // Section headers table immediately follows file header.
434 ASSERT(w->position() == sizeof(ELFHeader));
435
436 Writer::Slot<ELFSection::Header> headers =
437 w->CreateSlotsHere<ELFSection::Header>(sections_.length());
438
439 // String table for section table is the first section.
440 StringTable* strtab = static_cast<StringTable*>(SectionAt(1));
441 strtab->AttachWriter(w);
442 for (int i = 0, length = sections_.length();
443 i < length;
444 i++) {
445 sections_[i]->PopulateHeader(headers.at(i), strtab);
446 }
447 strtab->DetachWriter();
448 }
449
450 int SectionHeaderPosition(uint32_t section_index) {
451 return sizeof(ELFHeader) + sizeof(ELFSection::Header) * section_index;
452 }
453
454 void WriteSections(Writer* w) {
455 Writer::Slot<ELFSection::Header> headers =
456 w->SlotAt<ELFSection::Header>(sizeof(ELFHeader));
457
458 for (int i = 0, length = sections_.length();
459 i < length;
460 i++) {
461 sections_[i]->WriteBody(headers.at(i), w);
462 }
463 }
464
465 ZoneList<ELFSection*> sections_;
466};
467
468
469class ELFSymbol BASE_EMBEDDED {
470 public:
471 enum Type {
472 TYPE_NOTYPE = 0,
473 TYPE_OBJECT = 1,
474 TYPE_FUNC = 2,
475 TYPE_SECTION = 3,
476 TYPE_FILE = 4,
477 TYPE_LOPROC = 13,
478 TYPE_HIPROC = 15
479 };
480
481 enum Binding {
482 BIND_LOCAL = 0,
483 BIND_GLOBAL = 1,
484 BIND_WEAK = 2,
485 BIND_LOPROC = 13,
486 BIND_HIPROC = 15
487 };
488
489 ELFSymbol(const char* name,
490 uintptr_t value,
491 uintptr_t size,
492 Binding binding,
493 Type type,
494 uint16_t section)
495 : name(name),
496 value(value),
497 size(size),
498 info((binding << 4) | type),
499 other(0),
500 section(section) {
501 }
502
503 Binding binding() const {
504 return static_cast<Binding>(info >> 4);
505 }
506
507#if defined(V8_TARGET_ARCH_IA32)
508 struct SerializedLayout {
509 SerializedLayout(uint32_t name,
510 uintptr_t value,
511 uintptr_t size,
512 Binding binding,
513 Type type,
514 uint16_t section)
515 : name(name),
516 value(value),
517 size(size),
518 info((binding << 4) | type),
519 other(0),
520 section(section) {
521 }
522
523 uint32_t name;
524 uintptr_t value;
525 uintptr_t size;
526 uint8_t info;
527 uint8_t other;
528 uint16_t section;
529 };
530#elif defined(V8_TARGET_ARCH_X64)
531 struct SerializedLayout {
532 SerializedLayout(uint32_t name,
533 uintptr_t value,
534 uintptr_t size,
535 Binding binding,
536 Type type,
537 uint16_t section)
538 : name(name),
539 info((binding << 4) | type),
540 other(0),
541 section(section),
542 value(value),
543 size(size) {
544 }
545
546 uint32_t name;
547 uint8_t info;
548 uint8_t other;
549 uint16_t section;
550 uintptr_t value;
551 uintptr_t size;
552 };
553#endif
554
555 void Write(Writer::Slot<SerializedLayout> s, StringTable* t) {
556 // Convert symbol names from strings to indexes in the string table.
557 s->name = t->Add(name);
558 s->value = value;
559 s->size = size;
560 s->info = info;
561 s->other = other;
562 s->section = section;
563 }
564
565 private:
566 const char* name;
567 uintptr_t value;
568 uintptr_t size;
569 uint8_t info;
570 uint8_t other;
571 uint16_t section;
572};
573
574
575class ELFSymbolTable : public ELFSection {
576 public:
577 explicit ELFSymbolTable(const char* name)
578 : ELFSection(name, TYPE_SYMTAB, sizeof(uintptr_t)),
579 locals_(1),
580 globals_(1) {
581 }
582
583 virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
584 w->Align(header->alignment);
585 int total_symbols = locals_.length() + globals_.length() + 1;
586 header->offset = w->position();
587
588 Writer::Slot<ELFSymbol::SerializedLayout> symbols =
589 w->CreateSlotsHere<ELFSymbol::SerializedLayout>(total_symbols);
590
591 header->size = w->position() - header->offset;
592
593 // String table for this symbol table should follow it in the section table.
594 StringTable* strtab =
595 static_cast<StringTable*>(w->elf()->SectionAt(index() + 1));
596 strtab->AttachWriter(w);
597 symbols.at(0).set(ELFSymbol::SerializedLayout(0,
598 0,
599 0,
600 ELFSymbol::BIND_LOCAL,
601 ELFSymbol::TYPE_NOTYPE,
602 0));
603 WriteSymbolsList(&locals_, symbols.at(1), strtab);
604 WriteSymbolsList(&globals_, symbols.at(locals_.length() + 1), strtab);
605 strtab->DetachWriter();
606 }
607
608 void Add(const ELFSymbol& symbol) {
609 if (symbol.binding() == ELFSymbol::BIND_LOCAL) {
610 locals_.Add(symbol);
611 } else {
612 globals_.Add(symbol);
613 }
614 }
615
616 protected:
617 virtual void PopulateHeader(Writer::Slot<Header> header) {
618 ELFSection::PopulateHeader(header);
619 // We are assuming that string table will follow symbol table.
620 header->link = index() + 1;
621 header->info = locals_.length() + 1;
622 header->entry_size = sizeof(ELFSymbol::SerializedLayout);
623 }
624
625 private:
626 void WriteSymbolsList(const ZoneList<ELFSymbol>* src,
627 Writer::Slot<ELFSymbol::SerializedLayout> dst,
628 StringTable* strtab) {
629 for (int i = 0, len = src->length();
630 i < len;
631 i++) {
632 src->at(i).Write(dst.at(i), strtab);
633 }
634 }
635
636 ZoneList<ELFSymbol> locals_;
637 ZoneList<ELFSymbol> globals_;
638};
639
640
641class CodeDescription BASE_EMBEDDED {
642 public:
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000643
644#ifdef V8_TARGET_ARCH_X64
645 enum StackState {
646 POST_RBP_PUSH,
647 POST_RBP_SET,
648 POST_RBP_POP,
649 STACK_STATE_MAX
650 };
651#endif
652
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000653 CodeDescription(const char* name,
654 Code* code,
655 Handle<Script> script,
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000656 GDBJITLineInfo* lineinfo,
657 GDBJITInterface::CodeTag tag)
658 : name_(name),
659 code_(code),
660 script_(script),
661 lineinfo_(lineinfo),
662 tag_(tag) {
663 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000664
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000665 const char* name() const {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000666 return name_;
667 }
668
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000669 GDBJITLineInfo* lineinfo() const {
670 return lineinfo_;
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000671 }
672
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000673 GDBJITInterface::CodeTag tag() const {
674 return tag_;
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000675 }
676
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000677 uintptr_t CodeStart() const {
678 return reinterpret_cast<uintptr_t>(code_->instruction_start());
679 }
680
681 uintptr_t CodeEnd() const {
682 return reinterpret_cast<uintptr_t>(code_->instruction_end());
683 }
684
685 uintptr_t CodeSize() const {
686 return CodeEnd() - CodeStart();
687 }
688
689 bool IsLineInfoAvailable() {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000690 return !script_.is_null() &&
691 script_->source()->IsString() &&
692 script_->HasValidSource() &&
693 script_->name()->IsString() &&
694 lineinfo_ != NULL;
695 }
696
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000697#ifdef V8_TARGET_ARCH_X64
698 uintptr_t GetStackStateStartAddress(StackState state) const {
699 ASSERT(state < STACK_STATE_MAX);
700 return stack_state_start_addresses_[state];
701 }
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000702
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000703 void SetStackStateStartAddress(StackState state, uintptr_t addr) {
704 ASSERT(state < STACK_STATE_MAX);
705 stack_state_start_addresses_[state] = addr;
706 }
707#endif
708
709 SmartPointer<char> GetFilename() {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000710 return String::cast(script_->name())->ToCString();
711 }
712
713 int GetScriptLineNumber(int pos) {
714 return GetScriptLineNumberSafe(script_, pos) + 1;
715 }
716
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000717
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000718 private:
719 const char* name_;
720 Code* code_;
721 Handle<Script> script_;
722 GDBJITLineInfo* lineinfo_;
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000723 GDBJITInterface::CodeTag tag_;
724#ifdef V8_TARGET_ARCH_X64
725 uintptr_t stack_state_start_addresses_[STACK_STATE_MAX];
726#endif
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000727};
728
729
730static void CreateSymbolsTable(CodeDescription* desc,
731 ELF* elf,
732 int text_section_index) {
733 ELFSymbolTable* symtab = new ELFSymbolTable(".symtab");
734 StringTable* strtab = new StringTable(".strtab");
735
736 // Symbol table should be followed by the linked string table.
737 elf->AddSection(symtab);
738 elf->AddSection(strtab);
739
740 symtab->Add(ELFSymbol("V8 Code",
741 0,
742 0,
743 ELFSymbol::BIND_LOCAL,
744 ELFSymbol::TYPE_FILE,
745 ELFSection::INDEX_ABSOLUTE));
746
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000747 symtab->Add(ELFSymbol(desc->name(),
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000748 0,
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000749 desc->CodeSize(),
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000750 ELFSymbol::BIND_GLOBAL,
751 ELFSymbol::TYPE_FUNC,
752 text_section_index));
753}
754
755
756class DebugInfoSection : public ELFSection {
757 public:
758 explicit DebugInfoSection(CodeDescription* desc)
759 : ELFSection(".debug_info", TYPE_PROGBITS, 1), desc_(desc) { }
760
761 bool WriteBody(Writer* w) {
762 Writer::Slot<uint32_t> size = w->CreateSlotHere<uint32_t>();
763 uintptr_t start = w->position();
764 w->Write<uint16_t>(2); // DWARF version.
765 w->Write<uint32_t>(0); // Abbreviation table offset.
766 w->Write<uint8_t>(sizeof(intptr_t));
767
768 w->WriteULEB128(1); // Abbreviation code.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000769 w->WriteString(*desc_->GetFilename());
770 w->Write<intptr_t>(desc_->CodeStart());
771 w->Write<intptr_t>(desc_->CodeStart() + desc_->CodeSize());
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000772 w->Write<uint32_t>(0);
773 size.set(static_cast<uint32_t>(w->position() - start));
774 return true;
775 }
776
777 private:
778 CodeDescription* desc_;
779};
780
781
782class DebugAbbrevSection : public ELFSection {
783 public:
784 DebugAbbrevSection() : ELFSection(".debug_abbrev", TYPE_PROGBITS, 1) { }
785
786 // DWARF2 standard, figure 14.
787 enum DWARF2Tags {
788 DW_TAG_COMPILE_UNIT = 0x11
789 };
790
791 // DWARF2 standard, figure 16.
792 enum DWARF2ChildrenDetermination {
793 DW_CHILDREN_NO = 0,
794 DW_CHILDREN_YES = 1
795 };
796
797 // DWARF standard, figure 17.
798 enum DWARF2Attribute {
799 DW_AT_NAME = 0x3,
800 DW_AT_STMT_LIST = 0x10,
801 DW_AT_LOW_PC = 0x11,
802 DW_AT_HIGH_PC = 0x12
803 };
804
805 // DWARF2 standard, figure 19.
806 enum DWARF2AttributeForm {
807 DW_FORM_ADDR = 0x1,
808 DW_FORM_STRING = 0x8,
809 DW_FORM_DATA4 = 0x6
810 };
811
812 bool WriteBody(Writer* w) {
813 w->WriteULEB128(1);
814 w->WriteULEB128(DW_TAG_COMPILE_UNIT);
815 w->Write<uint8_t>(DW_CHILDREN_NO);
816 w->WriteULEB128(DW_AT_NAME);
817 w->WriteULEB128(DW_FORM_STRING);
818 w->WriteULEB128(DW_AT_LOW_PC);
819 w->WriteULEB128(DW_FORM_ADDR);
820 w->WriteULEB128(DW_AT_HIGH_PC);
821 w->WriteULEB128(DW_FORM_ADDR);
822 w->WriteULEB128(DW_AT_STMT_LIST);
823 w->WriteULEB128(DW_FORM_DATA4);
824 w->WriteULEB128(0);
825 w->WriteULEB128(0);
826 w->WriteULEB128(0);
827 return true;
828 }
829};
830
831
832class DebugLineSection : public ELFSection {
833 public:
834 explicit DebugLineSection(CodeDescription* desc)
835 : ELFSection(".debug_line", TYPE_PROGBITS, 1),
836 desc_(desc) { }
837
838 // DWARF2 standard, figure 34.
839 enum DWARF2Opcodes {
840 DW_LNS_COPY = 1,
841 DW_LNS_ADVANCE_PC = 2,
842 DW_LNS_ADVANCE_LINE = 3,
843 DW_LNS_SET_FILE = 4,
844 DW_LNS_SET_COLUMN = 5,
845 DW_LNS_NEGATE_STMT = 6
846 };
847
848 // DWARF2 standard, figure 35.
849 enum DWARF2ExtendedOpcode {
850 DW_LNE_END_SEQUENCE = 1,
851 DW_LNE_SET_ADDRESS = 2,
852 DW_LNE_DEFINE_FILE = 3
853 };
854
855 bool WriteBody(Writer* w) {
856 // Write prologue.
857 Writer::Slot<uint32_t> total_length = w->CreateSlotHere<uint32_t>();
858 uintptr_t start = w->position();
859
860 w->Write<uint16_t>(2); // Field version.
861 Writer::Slot<uint32_t> prologue_length = w->CreateSlotHere<uint32_t>();
862 uintptr_t prologue_start = w->position();
863 w->Write<uint8_t>(1); // Field minimum_instruction_length.
864 w->Write<uint8_t>(1); // Field default_is_stmt.
865 w->Write<int8_t>(0); // Field line_base.
866 w->Write<uint8_t>(2); // Field line_range.
867 w->Write<uint8_t>(DW_LNS_NEGATE_STMT + 1); // Field opcode_base.
868 w->Write<uint8_t>(0); // DW_LNS_COPY operands count.
869 w->Write<uint8_t>(1); // DW_LNS_ADVANCE_PC operands count.
870 w->Write<uint8_t>(1); // DW_LNS_ADVANCE_LINE operands count.
871 w->Write<uint8_t>(1); // DW_LNS_SET_FILE operands count.
872 w->Write<uint8_t>(1); // DW_LNS_SET_COLUMN operands count.
873 w->Write<uint8_t>(0); // DW_LNS_NEGATE_STMT operands count.
874 w->Write<uint8_t>(0); // Empty include_directories sequence.
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000875 w->WriteString(*desc_->GetFilename()); // File name.
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000876 w->WriteULEB128(0); // Current directory.
877 w->WriteULEB128(0); // Unknown modification time.
878 w->WriteULEB128(0); // Unknown file size.
879 w->Write<uint8_t>(0);
880 prologue_length.set(static_cast<uint32_t>(w->position() - prologue_start));
881
882 WriteExtendedOpcode(w, DW_LNE_SET_ADDRESS, sizeof(intptr_t));
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000883 w->Write<intptr_t>(desc_->CodeStart());
erik.corry@gmail.com0511e242011-01-19 11:11:08 +0000884
885 intptr_t pc = 0;
886 intptr_t line = 1;
887 bool is_statement = true;
888
889 List<GDBJITLineInfo::PCInfo>* pc_info = desc_->lineinfo()->pc_info();
890 pc_info->Sort(&ComparePCInfo);
891 for (int i = 0; i < pc_info->length(); i++) {
892 GDBJITLineInfo::PCInfo* info = &pc_info->at(i);
893 uintptr_t pc_diff = info->pc_ - pc;
894 ASSERT(info->pc_ >= pc);
895 if (pc_diff != 0) {
896 w->Write<uint8_t>(DW_LNS_ADVANCE_PC);
897 w->WriteSLEB128(pc_diff);
898 pc += pc_diff;
899 }
900 intptr_t line_diff = desc_->GetScriptLineNumber(info->pos_) - line;
901 if (line_diff != 0) {
902 w->Write<uint8_t>(DW_LNS_ADVANCE_LINE);
903 w->WriteSLEB128(line_diff);
904 line += line_diff;
905 }
906 if (is_statement != info->is_statement_) {
907 w->Write<uint8_t>(DW_LNS_NEGATE_STMT);
908 is_statement = !is_statement;
909 }
910 if (pc_diff != 0 || i == 0) {
911 w->Write<uint8_t>(DW_LNS_COPY);
912 }
913 }
914 WriteExtendedOpcode(w, DW_LNE_END_SEQUENCE, 0);
915 total_length.set(static_cast<uint32_t>(w->position() - start));
916 return true;
917 }
918
919 private:
920 void WriteExtendedOpcode(Writer* w,
921 DWARF2ExtendedOpcode op,
922 size_t operands_size) {
923 w->Write<uint8_t>(0);
924 w->WriteULEB128(operands_size + 1);
925 w->Write<uint8_t>(op);
926 }
927
928 static int ComparePCInfo(const GDBJITLineInfo::PCInfo* a,
929 const GDBJITLineInfo::PCInfo* b) {
930 if (a->pc_ == b->pc_) {
931 if (a->is_statement_ != b->is_statement_) {
932 return b->is_statement_ ? +1 : -1;
933 }
934 return 0;
935 } else if (a->pc_ > b->pc_) {
936 return +1;
937 } else {
938 return -1;
939 }
940 }
941
942 CodeDescription* desc_;
943};
944
945
kmillikin@chromium.org31b12772011-02-02 16:08:26 +0000946#ifdef V8_TARGET_ARCH_X64
947
948
949class UnwindInfoSection : public ELFSection {
950 public:
951 explicit UnwindInfoSection(CodeDescription *desc);
952 virtual bool WriteBody(Writer *w);
953
954 int WriteCIE(Writer *w);
955 void WriteFDE(Writer *w, int);
956
957 void WriteFDEStateOnEntry(Writer *w);
958 void WriteFDEStateAfterRBPPush(Writer *w);
959 void WriteFDEStateAfterRBPSet(Writer *w);
960 void WriteFDEStateAfterRBPPop(Writer *w);
961
962 void WriteLength(Writer *w,
963 Writer::Slot<uint32_t>* length_slot,
964 int initial_position);
965
966 private:
967 CodeDescription *desc_;
968
969 // DWARF3 Specification, Table 7.23
970 enum CFIInstructions {
971 DW_CFA_ADVANCE_LOC = 0x40,
972 DW_CFA_OFFSET = 0x80,
973 DW_CFA_RESTORE = 0xC0,
974 DW_CFA_NOP = 0x00,
975 DW_CFA_SET_LOC = 0x01,
976 DW_CFA_ADVANCE_LOC1 = 0x02,
977 DW_CFA_ADVANCE_LOC2 = 0x03,
978 DW_CFA_ADVANCE_LOC4 = 0x04,
979 DW_CFA_OFFSET_EXTENDED = 0x05,
980 DW_CFA_RESTORE_EXTENDED = 0x06,
981 DW_CFA_UNDEFINED = 0x07,
982 DW_CFA_SAME_VALUE = 0x08,
983 DW_CFA_REGISTER = 0x09,
984 DW_CFA_REMEMBER_STATE = 0x0A,
985 DW_CFA_RESTORE_STATE = 0x0B,
986 DW_CFA_DEF_CFA = 0x0C,
987 DW_CFA_DEF_CFA_REGISTER = 0x0D,
988 DW_CFA_DEF_CFA_OFFSET = 0x0E,
989
990 DW_CFA_DEF_CFA_EXPRESSION = 0x0F,
991 DW_CFA_EXPRESSION = 0x10,
992 DW_CFA_OFFSET_EXTENDED_SF = 0x11,
993 DW_CFA_DEF_CFA_SF = 0x12,
994 DW_CFA_DEF_CFA_OFFSET_SF = 0x13,
995 DW_CFA_VAL_OFFSET = 0x14,
996 DW_CFA_VAL_OFFSET_SF = 0x15,
997 DW_CFA_VAL_EXPRESSION = 0x16
998 };
999
1000 // System V ABI, AMD64 Supplement, Version 0.99.5, Figure 3.36
1001 enum RegisterMapping {
1002 // Only the relevant ones have been added to reduce clutter.
1003 AMD64_RBP = 6,
1004 AMD64_RSP = 7,
1005 AMD64_RA = 16
1006 };
1007
1008 enum CFIConstants {
1009 CIE_ID = 0,
1010 CIE_VERSION = 1,
1011 CODE_ALIGN_FACTOR = 1,
1012 DATA_ALIGN_FACTOR = 1,
1013 RETURN_ADDRESS_REGISTER = AMD64_RA
1014 };
1015};
1016
1017
1018void UnwindInfoSection::WriteLength(Writer *w,
1019 Writer::Slot<uint32_t>* length_slot,
1020 int initial_position) {
1021 uint32_t align = (w->position() - initial_position) % kPointerSize;
1022
1023 if (align != 0) {
1024 for (uint32_t i = 0; i < (kPointerSize - align); i++) {
1025 w->Write<uint8_t>(DW_CFA_NOP);
1026 }
1027 }
1028
1029 ASSERT((w->position() - initial_position) % kPointerSize == 0);
1030 length_slot->set(w->position() - initial_position);
1031}
1032
1033
1034UnwindInfoSection::UnwindInfoSection(CodeDescription *desc)
1035 : ELFSection(".eh_frame", TYPE_X86_64_UNWIND, 1), desc_(desc)
1036{ }
1037
1038int UnwindInfoSection::WriteCIE(Writer *w) {
1039 Writer::Slot<uint32_t> cie_length_slot = w->CreateSlotHere<uint32_t>();
1040 uint32_t cie_position = w->position();
1041
1042 // Write out the CIE header. Currently no 'common instructions' are
1043 // emitted onto the CIE; every FDE has its own set of instructions.
1044
1045 w->Write<uint32_t>(CIE_ID);
1046 w->Write<uint8_t>(CIE_VERSION);
1047 w->Write<uint8_t>(0); // Null augmentation string.
1048 w->WriteSLEB128(CODE_ALIGN_FACTOR);
1049 w->WriteSLEB128(DATA_ALIGN_FACTOR);
1050 w->Write<uint8_t>(RETURN_ADDRESS_REGISTER);
1051
1052 WriteLength(w, &cie_length_slot, cie_position);
1053
1054 return cie_position;
1055}
1056
1057
1058void UnwindInfoSection::WriteFDE(Writer *w, int cie_position) {
1059 // The only FDE for this function. The CFA is the current RBP.
1060 Writer::Slot<uint32_t> fde_length_slot = w->CreateSlotHere<uint32_t>();
1061 int fde_position = w->position();
1062 w->Write<int32_t>(fde_position - cie_position + 4);
1063
1064 w->Write<uintptr_t>(desc_->CodeStart());
1065 w->Write<uintptr_t>(desc_->CodeSize());
1066
1067 WriteFDEStateOnEntry(w);
1068 WriteFDEStateAfterRBPPush(w);
1069 WriteFDEStateAfterRBPSet(w);
1070 WriteFDEStateAfterRBPPop(w);
1071
1072 WriteLength(w, &fde_length_slot, fde_position);
1073}
1074
1075
1076void UnwindInfoSection::WriteFDEStateOnEntry(Writer *w) {
1077 // The first state, just after the control has been transferred to the the
1078 // function.
1079
1080 // RBP for this function will be the value of RSP after pushing the RBP
1081 // for the previous function. The previous RBP has not been pushed yet.
1082 w->Write<uint8_t>(DW_CFA_DEF_CFA_SF);
1083 w->WriteULEB128(AMD64_RSP);
1084 w->WriteSLEB128(-kPointerSize);
1085
1086 // The RA is stored at location CFA + kCallerPCOffset. This is an invariant,
1087 // and hence omitted from the next states.
1088 w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
1089 w->WriteULEB128(AMD64_RA);
1090 w->WriteSLEB128(StandardFrameConstants::kCallerPCOffset);
1091
1092 // The RBP of the previous function is still in RBP.
1093 w->Write<uint8_t>(DW_CFA_SAME_VALUE);
1094 w->WriteULEB128(AMD64_RBP);
1095
1096 // Last location described by this entry.
1097 w->Write<uint8_t>(DW_CFA_SET_LOC);
1098 w->Write<uint64_t>(
1099 desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_PUSH));
1100}
1101
1102
1103void UnwindInfoSection::WriteFDEStateAfterRBPPush(Writer *w) {
1104 // The second state, just after RBP has been pushed.
1105
1106 // RBP / CFA for this function is now the current RSP, so just set the
1107 // offset from the previous rule (from -8) to 0.
1108 w->Write<uint8_t>(DW_CFA_DEF_CFA_OFFSET);
1109 w->WriteULEB128(0);
1110
1111 // The previous RBP is stored at CFA + kCallerFPOffset. This is an invariant
1112 // in this and the next state, and hence omitted in the next state.
1113 w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
1114 w->WriteULEB128(AMD64_RBP);
1115 w->WriteSLEB128(StandardFrameConstants::kCallerFPOffset);
1116
1117 // Last location described by this entry.
1118 w->Write<uint8_t>(DW_CFA_SET_LOC);
1119 w->Write<uint64_t>(
1120 desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_SET));
1121}
1122
1123
1124void UnwindInfoSection::WriteFDEStateAfterRBPSet(Writer *w) {
1125 // The third state, after the RBP has been set.
1126
1127 // The CFA can now directly be set to RBP.
1128 w->Write<uint8_t>(DW_CFA_DEF_CFA);
1129 w->WriteULEB128(AMD64_RBP);
1130 w->WriteULEB128(0);
1131
1132 // Last location described by this entry.
1133 w->Write<uint8_t>(DW_CFA_SET_LOC);
1134 w->Write<uint64_t>(
1135 desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_POP));
1136}
1137
1138
1139void UnwindInfoSection::WriteFDEStateAfterRBPPop(Writer *w) {
1140 // The fourth (final) state. The RBP has been popped (just before issuing a
1141 // return).
1142
1143 // The CFA can is now calculated in the same way as in the first state.
1144 w->Write<uint8_t>(DW_CFA_DEF_CFA_SF);
1145 w->WriteULEB128(AMD64_RSP);
1146 w->WriteSLEB128(-kPointerSize);
1147
1148 // The RBP
1149 w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
1150 w->WriteULEB128(AMD64_RBP);
1151 w->WriteSLEB128(StandardFrameConstants::kCallerFPOffset);
1152
1153 // Last location described by this entry.
1154 w->Write<uint8_t>(DW_CFA_SET_LOC);
1155 w->Write<uint64_t>(desc_->CodeEnd());
1156}
1157
1158
1159bool UnwindInfoSection::WriteBody(Writer *w) {
1160 uint32_t cie_position = WriteCIE(w);
1161 WriteFDE(w, cie_position);
1162 return true;
1163}
1164
1165
1166#endif // V8_TARGET_ARCH_X64
1167
1168
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001169static void CreateDWARFSections(CodeDescription* desc, ELF* elf) {
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001170 if (desc->IsLineInfoAvailable()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001171 elf->AddSection(new DebugInfoSection(desc));
1172 elf->AddSection(new DebugAbbrevSection);
1173 elf->AddSection(new DebugLineSection(desc));
1174 }
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001175#ifdef V8_TARGET_ARCH_X64
1176 elf->AddSection(new UnwindInfoSection(desc));
1177#endif
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001178}
1179
1180
1181// -------------------------------------------------------------------
1182// Binary GDB JIT Interface as described in
1183// http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
1184extern "C" {
1185 typedef enum {
1186 JIT_NOACTION = 0,
1187 JIT_REGISTER_FN,
1188 JIT_UNREGISTER_FN
1189 } JITAction;
1190
1191 struct JITCodeEntry {
1192 JITCodeEntry* next_;
1193 JITCodeEntry* prev_;
1194 Address symfile_addr_;
1195 uint64_t symfile_size_;
1196 };
1197
1198 struct JITDescriptor {
1199 uint32_t version_;
1200 uint32_t action_flag_;
1201 JITCodeEntry *relevant_entry_;
1202 JITCodeEntry *first_entry_;
1203 };
1204
1205 // GDB will place breakpoint into this function.
1206 // To prevent GCC from inlining or removing it we place noinline attribute
1207 // and inline assembler statement inside.
1208 void __attribute__((noinline)) __jit_debug_register_code() {
1209 __asm__("");
1210 }
1211
1212 // GDB will inspect contents of this descriptor.
1213 // Static initialization is necessary to prevent GDB from seeing
1214 // uninitialized descriptor.
1215 JITDescriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
1216}
1217
1218
1219static JITCodeEntry* CreateCodeEntry(Address symfile_addr,
1220 uintptr_t symfile_size) {
1221 JITCodeEntry* entry = static_cast<JITCodeEntry*>(
1222 malloc(sizeof(JITCodeEntry) + symfile_size));
1223
1224 entry->symfile_addr_ = reinterpret_cast<Address>(entry + 1);
1225 entry->symfile_size_ = symfile_size;
1226 memcpy(entry->symfile_addr_, symfile_addr, symfile_size);
1227
1228 entry->prev_ = entry->next_ = NULL;
1229
1230 return entry;
1231}
1232
1233
1234static void DestroyCodeEntry(JITCodeEntry* entry) {
1235 free(entry);
1236}
1237
1238
1239static void RegisterCodeEntry(JITCodeEntry* entry) {
1240 entry->next_ = __jit_debug_descriptor.first_entry_;
1241 if (entry->next_ != NULL) entry->next_->prev_ = entry;
1242 __jit_debug_descriptor.first_entry_ =
1243 __jit_debug_descriptor.relevant_entry_ = entry;
1244
1245 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
1246 __jit_debug_register_code();
1247}
1248
1249
1250static void UnregisterCodeEntry(JITCodeEntry* entry) {
1251 if (entry->prev_ != NULL) {
1252 entry->prev_->next_ = entry->next_;
1253 } else {
1254 __jit_debug_descriptor.first_entry_ = entry->next_;
1255 }
1256
1257 if (entry->next_ != NULL) {
1258 entry->next_->prev_ = entry->prev_;
1259 }
1260
1261 __jit_debug_descriptor.relevant_entry_ = entry;
1262 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
1263 __jit_debug_register_code();
1264}
1265
1266
1267static JITCodeEntry* CreateELFObject(CodeDescription* desc) {
1268 ZoneScope zone_scope(DELETE_ON_EXIT);
1269
1270 ELF elf;
1271 Writer w(&elf);
1272
1273 int text_section_index = elf.AddSection(
1274 new FullHeaderELFSection(".text",
1275 ELFSection::TYPE_NOBITS,
1276 kCodeAlignment,
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001277 desc->CodeStart(),
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001278 0,
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001279 desc->CodeSize(),
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001280 ELFSection::FLAG_ALLOC | ELFSection::FLAG_EXEC));
1281
1282 CreateSymbolsTable(desc, &elf, text_section_index);
1283
1284 CreateDWARFSections(desc, &elf);
1285
1286 elf.Write(&w);
1287
1288 return CreateCodeEntry(w.buffer(), w.position());
1289}
1290
1291
1292static bool SameCodeObjects(void* key1, void* key2) {
1293 return key1 == key2;
1294}
1295
1296
1297static HashMap entries(&SameCodeObjects);
1298
1299
1300static uint32_t HashForCodeObject(Code* code) {
1301 static const uintptr_t kGoldenRatio = 2654435761u;
1302 uintptr_t hash = reinterpret_cast<uintptr_t>(code->address());
1303 return static_cast<uint32_t>((hash >> kCodeAlignmentBits) * kGoldenRatio);
1304}
1305
1306
1307static const intptr_t kLineInfoTag = 0x1;
1308
1309
1310static bool IsLineInfoTagged(void* ptr) {
1311 return 0 != (reinterpret_cast<intptr_t>(ptr) & kLineInfoTag);
1312}
1313
1314
1315static void* TagLineInfo(GDBJITLineInfo* ptr) {
1316 return reinterpret_cast<void*>(
1317 reinterpret_cast<intptr_t>(ptr) | kLineInfoTag);
1318}
1319
1320
1321static GDBJITLineInfo* UntagLineInfo(void* ptr) {
1322 return reinterpret_cast<GDBJITLineInfo*>(
1323 reinterpret_cast<intptr_t>(ptr) & ~kLineInfoTag);
1324}
1325
1326
1327void GDBJITInterface::AddCode(Handle<String> name,
1328 Handle<Script> script,
1329 Handle<Code> code) {
1330 if (!FLAG_gdbjit) return;
1331
1332 // Force initialization of line_ends array.
1333 GetScriptLineNumber(script, 0);
1334
1335 if (!name.is_null()) {
1336 SmartPointer<char> name_cstring = name->ToCString(DISALLOW_NULLS);
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001337 AddCode(*name_cstring, *code, GDBJITInterface::FUNCTION, *script);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001338 } else {
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001339 AddCode("", *code, GDBJITInterface::FUNCTION, *script);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001340 }
1341}
1342
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001343static void AddUnwindInfo(CodeDescription *desc) {
1344#ifdef V8_TARGET_ARCH_X64
1345 if (desc->tag() == GDBJITInterface::FUNCTION) {
1346 // To avoid propagating unwinding information through
1347 // compilation pipeline we rely on function prologue
1348 // and epilogue being the same for all code objects generated
1349 // by the full code generator.
1350 static const int kFramePointerPushOffset = 1;
1351 static const int kFramePointerSetOffset = 4;
1352 static const int kFramePointerPopOffset = -3;
1353
1354 uintptr_t frame_pointer_push_address =
1355 desc->CodeStart() + kFramePointerPushOffset;
1356
1357 uintptr_t frame_pointer_set_address =
1358 desc->CodeStart() + kFramePointerSetOffset;
1359
1360 uintptr_t frame_pointer_pop_address =
1361 desc->CodeEnd() + kFramePointerPopOffset;
1362
1363#ifdef DEBUG
1364 static const uint8_t kFramePointerPushInstruction = 0x48; // push ebp
1365 static const uint16_t kFramePointerSetInstruction = 0x5756; // mov ebp, esp
1366 static const uint8_t kFramePointerPopInstruction = 0xBE; // pop ebp
1367
1368 ASSERT(*reinterpret_cast<uint8_t*>(frame_pointer_push_address) ==
1369 kFramePointerPushInstruction);
1370 ASSERT(*reinterpret_cast<uint16_t*>(frame_pointer_set_address) ==
1371 kFramePointerSetInstruction);
1372 ASSERT(*reinterpret_cast<uint8_t*>(frame_pointer_pop_address) ==
1373 kFramePointerPopInstruction);
1374#endif
1375
1376 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_PUSH,
1377 frame_pointer_push_address);
1378 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_SET,
1379 frame_pointer_set_address);
1380 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_POP,
1381 frame_pointer_pop_address);
1382 } else {
1383 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_PUSH,
1384 desc->CodeStart());
1385 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_SET,
1386 desc->CodeStart());
1387 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_POP,
1388 desc->CodeEnd());
1389 }
1390#endif // V8_TARGET_ARCH_X64
1391}
1392
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001393
1394void GDBJITInterface::AddCode(const char* name,
1395 Code* code,
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001396 GDBJITInterface::CodeTag tag,
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001397 Script* script) {
1398 if (!FLAG_gdbjit) return;
1399 AssertNoAllocation no_gc;
1400
1401 HashMap::Entry* e = entries.Lookup(code, HashForCodeObject(code), true);
1402 if (e->value != NULL && !IsLineInfoTagged(e->value)) return;
1403
1404 GDBJITLineInfo* lineinfo = UntagLineInfo(e->value);
1405 CodeDescription code_desc(name,
1406 code,
1407 script != NULL ? Handle<Script>(script)
1408 : Handle<Script>(),
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001409 lineinfo,
1410 tag);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001411
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001412 if (!FLAG_gdbjit_full && !code_desc.IsLineInfoAvailable()) {
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001413 delete lineinfo;
1414 entries.Remove(code, HashForCodeObject(code));
1415 return;
1416 }
1417
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001418 AddUnwindInfo(&code_desc);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001419 JITCodeEntry* entry = CreateELFObject(&code_desc);
1420 ASSERT(!IsLineInfoTagged(entry));
1421
1422 delete lineinfo;
1423 e->value = entry;
1424
1425 RegisterCodeEntry(entry);
1426}
1427
1428
1429void GDBJITInterface::AddCode(GDBJITInterface::CodeTag tag,
1430 const char* name,
1431 Code* code) {
1432 if (!FLAG_gdbjit) return;
1433
1434 EmbeddedVector<char, 256> buffer;
1435 StringBuilder builder(buffer.start(), buffer.length());
1436
1437 builder.AddString(Tag2String(tag));
1438 if ((name != NULL) && (*name != '\0')) {
1439 builder.AddString(": ");
1440 builder.AddString(name);
1441 } else {
1442 builder.AddFormatted(": code object %p", static_cast<void*>(code));
1443 }
1444
kmillikin@chromium.org31b12772011-02-02 16:08:26 +00001445 AddCode(builder.Finalize(), code, tag);
erik.corry@gmail.com0511e242011-01-19 11:11:08 +00001446}
1447
1448
1449void GDBJITInterface::AddCode(GDBJITInterface::CodeTag tag,
1450 String* name,
1451 Code* code) {
1452 if (!FLAG_gdbjit) return;
1453 AddCode(tag, name != NULL ? *name->ToCString(DISALLOW_NULLS) : NULL, code);
1454}
1455
1456
1457void GDBJITInterface::AddCode(GDBJITInterface::CodeTag tag, Code* code) {
1458 if (!FLAG_gdbjit) return;
1459
1460 AddCode(tag, "", code);
1461}
1462
1463
1464void GDBJITInterface::RemoveCode(Code* code) {
1465 if (!FLAG_gdbjit) return;
1466
1467 HashMap::Entry* e = entries.Lookup(code, HashForCodeObject(code), false);
1468 if (e == NULL) return;
1469
1470 if (IsLineInfoTagged(e->value)) {
1471 delete UntagLineInfo(e->value);
1472 } else {
1473 JITCodeEntry* entry = static_cast<JITCodeEntry*>(e->value);
1474 UnregisterCodeEntry(entry);
1475 DestroyCodeEntry(entry);
1476 }
1477 e->value = NULL;
1478 entries.Remove(code, HashForCodeObject(code));
1479}
1480
1481
1482void GDBJITInterface::RegisterDetailedLineInfo(Code* code,
1483 GDBJITLineInfo* line_info) {
1484 ASSERT(!IsLineInfoTagged(line_info));
1485 HashMap::Entry* e = entries.Lookup(code, HashForCodeObject(code), true);
1486 ASSERT(e->value == NULL);
1487 e->value = TagLineInfo(line_info);
1488}
1489
1490
1491} } // namespace v8::internal
1492#endif