1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifdef ENABLE_GDB_JIT_INTERFACE
9 #include "bootstrapper.h"
12 #include "frames-inl.h"
13 #include "global-handles.h"
26 typedef MachO DebugObject;
27 typedef MachOSection DebugSection;
32 typedef ELF DebugObject;
33 typedef ELFSection DebugSection;
36 class Writer BASE_EMBEDDED {
38 explicit Writer(DebugObject* debug_object)
39 : debug_object_(debug_object),
42 buffer_(reinterpret_cast<byte*>(malloc(capacity_))) {
49 uintptr_t position() const {
56 Slot(Writer* w, uintptr_t offset) : w_(w), offset_(offset) { }
59 return w_->RawSlotAt<T>(offset_);
62 void set(const T& value) {
63 *w_->RawSlotAt<T>(offset_) = value;
67 return Slot<T>(w_, offset_ + sizeof(T) * i);
76 void Write(const T& val) {
77 Ensure(position_ + sizeof(T));
78 *RawSlotAt<T>(position_) = val;
79 position_ += sizeof(T);
83 Slot<T> SlotAt(uintptr_t offset) {
84 Ensure(offset + sizeof(T));
85 return Slot<T>(this, offset);
89 Slot<T> CreateSlotHere() {
90 return CreateSlotsHere<T>(1);
94 Slot<T> CreateSlotsHere(uint32_t count) {
95 uintptr_t slot_position = position_;
96 position_ += sizeof(T) * count;
98 return SlotAt<T>(slot_position);
101 void Ensure(uintptr_t pos) {
102 if (capacity_ < pos) {
103 while (capacity_ < pos) capacity_ *= 2;
104 buffer_ = reinterpret_cast<byte*>(realloc(buffer_, capacity_));
108 DebugObject* debug_object() { return debug_object_; }
110 byte* buffer() { return buffer_; }
112 void Align(uintptr_t align) {
113 uintptr_t delta = position_ % align;
114 if (delta == 0) return;
115 uintptr_t padding = align - delta;
116 Ensure(position_ += padding);
117 ASSERT((position_ % align) == 0);
120 void WriteULEB128(uintptr_t value) {
122 uint8_t byte = value & 0x7F;
124 if (value != 0) byte |= 0x80;
125 Write<uint8_t>(byte);
126 } while (value != 0);
129 void WriteSLEB128(intptr_t value) {
132 int8_t byte = value & 0x7F;
133 bool byte_sign = byte & 0x40;
136 if ((value == 0 && !byte_sign) || (value == -1 && byte_sign)) {
146 void WriteString(const char* str) {
153 template<typename T> friend class Slot;
156 T* RawSlotAt(uintptr_t offset) {
157 ASSERT(offset < capacity_ && offset + sizeof(T) <= capacity_);
158 return reinterpret_cast<T*>(&buffer_[offset]);
161 DebugObject* debug_object_;
167 class ELFStringTable;
169 template<typename THeader>
170 class DebugSectionBase : public ZoneObject {
172 virtual ~DebugSectionBase() { }
174 virtual void WriteBody(Writer::Slot<THeader> header, Writer* writer) {
175 uintptr_t start = writer->position();
176 if (WriteBodyInternal(writer)) {
177 uintptr_t end = writer->position();
178 header->offset = start;
179 #if defined(__MACH_O)
182 header->size = end - start;
186 virtual bool WriteBodyInternal(Writer* writer) {
190 typedef THeader Header;
194 struct MachOSectionHeader {
197 #if V8_TARGET_ARCH_IA32
214 class MachOSection : public DebugSectionBase<MachOSectionHeader> {
218 S_ATTR_COALESCED = 0xbu,
219 S_ATTR_SOME_INSTRUCTIONS = 0x400u,
220 S_ATTR_DEBUG = 0x02000000u,
221 S_ATTR_PURE_INSTRUCTIONS = 0x80000000u
224 MachOSection(const char* name,
233 ASSERT(IsPowerOf2(align));
234 align_ = WhichPowerOf2(align_);
238 virtual ~MachOSection() { }
240 virtual void PopulateHeader(Writer::Slot<Header> header) {
244 header->align = align_;
247 header->flags = flags_;
248 header->reserved1 = 0;
249 header->reserved2 = 0;
250 memset(header->sectname, 0, sizeof(header->sectname));
251 memset(header->segname, 0, sizeof(header->segname));
252 ASSERT(strlen(name_) < sizeof(header->sectname));
253 ASSERT(strlen(segment_) < sizeof(header->segname));
254 strncpy(header->sectname, name_, sizeof(header->sectname));
255 strncpy(header->segname, segment_, sizeof(header->segname));
260 const char* segment_;
266 struct ELFSectionHeader {
276 uintptr_t entry_size;
281 class ELFSection : public DebugSectionBase<ELFSectionHeader> {
296 TYPE_LOPROC = 0x70000000,
297 TYPE_X86_64_UNWIND = 0x70000001,
298 TYPE_HIPROC = 0x7fffffff,
299 TYPE_LOUSER = 0x80000000,
300 TYPE_HIUSER = 0xffffffff
309 enum SpecialIndexes {
310 INDEX_ABSOLUTE = 0xfff1
313 ELFSection(const char* name, Type type, uintptr_t align)
314 : name_(name), type_(type), align_(align) { }
316 virtual ~ELFSection() { }
318 void PopulateHeader(Writer::Slot<Header> header, ELFStringTable* strtab);
320 virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
321 uintptr_t start = w->position();
322 if (WriteBodyInternal(w)) {
323 uintptr_t end = w->position();
324 header->offset = start;
325 header->size = end - start;
329 virtual bool WriteBodyInternal(Writer* w) {
333 uint16_t index() const { return index_; }
334 void set_index(uint16_t index) { index_ = index; }
337 virtual void PopulateHeader(Writer::Slot<Header> header) {
344 header->entry_size = 0;
353 #endif // defined(__ELF)
356 #if defined(__MACH_O)
357 class MachOTextSection : public MachOSection {
359 MachOTextSection(uintptr_t align,
362 : MachOSection("__text",
365 MachOSection::S_REGULAR |
366 MachOSection::S_ATTR_SOME_INSTRUCTIONS |
367 MachOSection::S_ATTR_PURE_INSTRUCTIONS),
372 virtual void PopulateHeader(Writer::Slot<Header> header) {
373 MachOSection::PopulateHeader(header);
374 header->addr = addr_;
375 header->size = size_;
382 #endif // defined(__MACH_O)
386 class FullHeaderELFSection : public ELFSection {
388 FullHeaderELFSection(const char* name,
395 : ELFSection(name, type, align),
402 virtual void PopulateHeader(Writer::Slot<Header> header) {
403 ELFSection::PopulateHeader(header);
404 header->address = addr_;
405 header->offset = offset_;
406 header->size = size_;
407 header->flags = flags_;
418 class ELFStringTable : public ELFSection {
420 explicit ELFStringTable(const char* name)
421 : ELFSection(name, TYPE_STRTAB, 1), writer_(NULL), offset_(0), size_(0) {
424 uintptr_t Add(const char* str) {
425 if (*str == '\0') return 0;
427 uintptr_t offset = size_;
432 void AttachWriter(Writer* w) {
434 offset_ = writer_->position();
436 // First entry in the string table should be an empty string.
440 void DetachWriter() {
444 virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
445 ASSERT(writer_ == NULL);
446 header->offset = offset_;
447 header->size = size_;
451 void WriteString(const char* str) {
452 uintptr_t written = 0;
454 writer_->Write(*str);
467 void ELFSection::PopulateHeader(Writer::Slot<ELFSection::Header> header,
468 ELFStringTable* strtab) {
469 header->name = strtab->Add(name_);
470 header->type = type_;
471 header->alignment = align_;
472 PopulateHeader(header);
474 #endif // defined(__ELF)
477 #if defined(__MACH_O)
478 class MachO BASE_EMBEDDED {
480 explicit MachO(Zone* zone) : zone_(zone), sections_(6, zone) { }
482 uint32_t AddSection(MachOSection* section) {
483 sections_.Add(section, zone_);
484 return sections_.length() - 1;
487 void Write(Writer* w, uintptr_t code_start, uintptr_t code_size) {
488 Writer::Slot<MachOHeader> header = WriteHeader(w);
489 uintptr_t load_command_start = w->position();
490 Writer::Slot<MachOSegmentCommand> cmd = WriteSegmentCommand(w,
493 WriteSections(w, cmd, header, load_command_start);
505 #if V8_TARGET_ARCH_X64
510 struct MachOSegmentCommand {
514 #if V8_TARGET_ARCH_IA32
531 enum MachOLoadCommandCmd {
532 LC_SEGMENT_32 = 0x00000001u,
533 LC_SEGMENT_64 = 0x00000019u
537 Writer::Slot<MachOHeader> WriteHeader(Writer* w) {
538 ASSERT(w->position() == 0);
539 Writer::Slot<MachOHeader> header = w->CreateSlotHere<MachOHeader>();
540 #if V8_TARGET_ARCH_IA32
541 header->magic = 0xFEEDFACEu;
542 header->cputype = 7; // i386
543 header->cpusubtype = 3; // CPU_SUBTYPE_I386_ALL
544 #elif V8_TARGET_ARCH_X64
545 header->magic = 0xFEEDFACFu;
546 header->cputype = 7 | 0x01000000; // i386 | 64-bit ABI
547 header->cpusubtype = 3; // CPU_SUBTYPE_I386_ALL
548 header->reserved = 0;
550 #error Unsupported target architecture.
552 header->filetype = 0x1; // MH_OBJECT
554 header->sizeofcmds = 0;
560 Writer::Slot<MachOSegmentCommand> WriteSegmentCommand(Writer* w,
561 uintptr_t code_start,
562 uintptr_t code_size) {
563 Writer::Slot<MachOSegmentCommand> cmd =
564 w->CreateSlotHere<MachOSegmentCommand>();
565 #if V8_TARGET_ARCH_IA32
566 cmd->cmd = LC_SEGMENT_32;
568 cmd->cmd = LC_SEGMENT_64;
570 cmd->vmaddr = code_start;
571 cmd->vmsize = code_size;
577 cmd->nsects = sections_.length();
578 memset(cmd->segname, 0, 16);
579 cmd->cmdsize = sizeof(MachOSegmentCommand) + sizeof(MachOSection::Header) *
585 void WriteSections(Writer* w,
586 Writer::Slot<MachOSegmentCommand> cmd,
587 Writer::Slot<MachOHeader> header,
588 uintptr_t load_command_start) {
589 Writer::Slot<MachOSection::Header> headers =
590 w->CreateSlotsHere<MachOSection::Header>(sections_.length());
591 cmd->fileoff = w->position();
592 header->sizeofcmds = w->position() - load_command_start;
593 for (int section = 0; section < sections_.length(); ++section) {
594 sections_[section]->PopulateHeader(headers.at(section));
595 sections_[section]->WriteBody(headers.at(section), w);
597 cmd->filesize = w->position() - (uintptr_t)cmd->fileoff;
601 ZoneList<MachOSection*> sections_;
603 #endif // defined(__MACH_O)
607 class ELF BASE_EMBEDDED {
609 explicit ELF(Zone* zone) : zone_(zone), sections_(6, zone) {
610 sections_.Add(new(zone) ELFSection("", ELFSection::TYPE_NULL, 0), zone);
611 sections_.Add(new(zone) ELFStringTable(".shstrtab"), zone);
614 void Write(Writer* w) {
616 WriteSectionTable(w);
620 ELFSection* SectionAt(uint32_t index) {
621 return sections_[index];
624 uint32_t AddSection(ELFSection* section) {
625 sections_.Add(section, zone_);
626 section->set_index(sections_.length() - 1);
627 return sections_.length() - 1;
637 uintptr_t pht_offset;
638 uintptr_t sht_offset;
640 uint16_t header_size;
641 uint16_t pht_entry_size;
642 uint16_t pht_entry_num;
643 uint16_t sht_entry_size;
644 uint16_t sht_entry_num;
645 uint16_t sht_strtab_index;
649 void WriteHeader(Writer* w) {
650 ASSERT(w->position() == 0);
651 Writer::Slot<ELFHeader> header = w->CreateSlotHere<ELFHeader>();
652 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_ARM
653 const uint8_t ident[16] =
654 { 0x7f, 'E', 'L', 'F', 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
655 #elif V8_TARGET_ARCH_X64
656 const uint8_t ident[16] =
657 { 0x7f, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
659 #error Unsupported target architecture.
661 OS::MemCopy(header->ident, ident, 16);
663 #if V8_TARGET_ARCH_IA32
665 #elif V8_TARGET_ARCH_X64
666 // Processor identification value for x64 is 62 as defined in
667 // System V ABI, AMD64 Supplement
668 // http://www.x86-64.org/documentation/abi.pdf
669 header->machine = 62;
670 #elif V8_TARGET_ARCH_ARM
671 // Set to EM_ARM, defined as 40, in "ARM ELF File Format" at
672 // infocenter.arm.com/help/topic/com.arm.doc.dui0101a/DUI0101A_Elf.pdf
673 header->machine = 40;
675 #error Unsupported target architecture.
679 header->pht_offset = 0;
680 header->sht_offset = sizeof(ELFHeader); // Section table follows header.
682 header->header_size = sizeof(ELFHeader);
683 header->pht_entry_size = 0;
684 header->pht_entry_num = 0;
685 header->sht_entry_size = sizeof(ELFSection::Header);
686 header->sht_entry_num = sections_.length();
687 header->sht_strtab_index = 1;
690 void WriteSectionTable(Writer* w) {
691 // Section headers table immediately follows file header.
692 ASSERT(w->position() == sizeof(ELFHeader));
694 Writer::Slot<ELFSection::Header> headers =
695 w->CreateSlotsHere<ELFSection::Header>(sections_.length());
697 // String table for section table is the first section.
698 ELFStringTable* strtab = static_cast<ELFStringTable*>(SectionAt(1));
699 strtab->AttachWriter(w);
700 for (int i = 0, length = sections_.length();
703 sections_[i]->PopulateHeader(headers.at(i), strtab);
705 strtab->DetachWriter();
708 int SectionHeaderPosition(uint32_t section_index) {
709 return sizeof(ELFHeader) + sizeof(ELFSection::Header) * section_index;
712 void WriteSections(Writer* w) {
713 Writer::Slot<ELFSection::Header> headers =
714 w->SlotAt<ELFSection::Header>(sizeof(ELFHeader));
716 for (int i = 0, length = sections_.length();
719 sections_[i]->WriteBody(headers.at(i), w);
724 ZoneList<ELFSection*> sections_;
728 class ELFSymbol BASE_EMBEDDED {
748 ELFSymbol(const char* name,
757 info((binding << 4) | type),
762 Binding binding() const {
763 return static_cast<Binding>(info >> 4);
765 #if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_ARM
766 struct SerializedLayout {
767 SerializedLayout(uint32_t name,
776 info((binding << 4) | type),
788 #elif V8_TARGET_ARCH_X64
789 struct SerializedLayout {
790 SerializedLayout(uint32_t name,
797 info((binding << 4) | type),
813 void Write(Writer::Slot<SerializedLayout> s, ELFStringTable* t) {
814 // Convert symbol names from strings to indexes in the string table.
815 s->name = t->Add(name);
820 s->section = section;
833 class ELFSymbolTable : public ELFSection {
835 ELFSymbolTable(const char* name, Zone* zone)
836 : ELFSection(name, TYPE_SYMTAB, sizeof(uintptr_t)),
841 virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
842 w->Align(header->alignment);
843 int total_symbols = locals_.length() + globals_.length() + 1;
844 header->offset = w->position();
846 Writer::Slot<ELFSymbol::SerializedLayout> symbols =
847 w->CreateSlotsHere<ELFSymbol::SerializedLayout>(total_symbols);
849 header->size = w->position() - header->offset;
851 // String table for this symbol table should follow it in the section table.
852 ELFStringTable* strtab =
853 static_cast<ELFStringTable*>(w->debug_object()->SectionAt(index() + 1));
854 strtab->AttachWriter(w);
855 symbols.at(0).set(ELFSymbol::SerializedLayout(0,
858 ELFSymbol::BIND_LOCAL,
859 ELFSymbol::TYPE_NOTYPE,
861 WriteSymbolsList(&locals_, symbols.at(1), strtab);
862 WriteSymbolsList(&globals_, symbols.at(locals_.length() + 1), strtab);
863 strtab->DetachWriter();
866 void Add(const ELFSymbol& symbol, Zone* zone) {
867 if (symbol.binding() == ELFSymbol::BIND_LOCAL) {
868 locals_.Add(symbol, zone);
870 globals_.Add(symbol, zone);
875 virtual void PopulateHeader(Writer::Slot<Header> header) {
876 ELFSection::PopulateHeader(header);
877 // We are assuming that string table will follow symbol table.
878 header->link = index() + 1;
879 header->info = locals_.length() + 1;
880 header->entry_size = sizeof(ELFSymbol::SerializedLayout);
884 void WriteSymbolsList(const ZoneList<ELFSymbol>* src,
885 Writer::Slot<ELFSymbol::SerializedLayout> dst,
886 ELFStringTable* strtab) {
887 for (int i = 0, len = src->length();
890 src->at(i).Write(dst.at(i), strtab);
894 ZoneList<ELFSymbol> locals_;
895 ZoneList<ELFSymbol> globals_;
897 #endif // defined(__ELF)
900 class CodeDescription BASE_EMBEDDED {
902 #if V8_TARGET_ARCH_X64
911 CodeDescription(const char* name,
913 Handle<Script> script,
914 GDBJITLineInfo* lineinfo,
915 GDBJITInterface::CodeTag tag,
916 CompilationInfo* info)
925 const char* name() const {
929 GDBJITLineInfo* lineinfo() const {
933 GDBJITInterface::CodeTag tag() const {
937 CompilationInfo* info() const {
941 bool IsInfoAvailable() const {
942 return info_ != NULL;
945 uintptr_t CodeStart() const {
946 return reinterpret_cast<uintptr_t>(code_->instruction_start());
949 uintptr_t CodeEnd() const {
950 return reinterpret_cast<uintptr_t>(code_->instruction_end());
953 uintptr_t CodeSize() const {
954 return CodeEnd() - CodeStart();
957 bool IsLineInfoAvailable() {
958 return !script_.is_null() &&
959 script_->source()->IsString() &&
960 script_->HasValidSource() &&
961 script_->name()->IsString() &&
965 #if V8_TARGET_ARCH_X64
966 uintptr_t GetStackStateStartAddress(StackState state) const {
967 ASSERT(state < STACK_STATE_MAX);
968 return stack_state_start_addresses_[state];
971 void SetStackStateStartAddress(StackState state, uintptr_t addr) {
972 ASSERT(state < STACK_STATE_MAX);
973 stack_state_start_addresses_[state] = addr;
977 SmartArrayPointer<char> GetFilename() {
978 return String::cast(script_->name())->ToCString();
981 int GetScriptLineNumber(int pos) {
982 return script_->GetLineNumber(pos) + 1;
989 Handle<Script> script_;
990 GDBJITLineInfo* lineinfo_;
991 GDBJITInterface::CodeTag tag_;
992 CompilationInfo* info_;
993 #if V8_TARGET_ARCH_X64
994 uintptr_t stack_state_start_addresses_[STACK_STATE_MAX];
999 static void CreateSymbolsTable(CodeDescription* desc,
1002 int text_section_index) {
1003 ELFSymbolTable* symtab = new(zone) ELFSymbolTable(".symtab", zone);
1004 ELFStringTable* strtab = new(zone) ELFStringTable(".strtab");
1006 // Symbol table should be followed by the linked string table.
1007 elf->AddSection(symtab);
1008 elf->AddSection(strtab);
1010 symtab->Add(ELFSymbol("V8 Code",
1013 ELFSymbol::BIND_LOCAL,
1014 ELFSymbol::TYPE_FILE,
1015 ELFSection::INDEX_ABSOLUTE),
1018 symtab->Add(ELFSymbol(desc->name(),
1021 ELFSymbol::BIND_GLOBAL,
1022 ELFSymbol::TYPE_FUNC,
1023 text_section_index),
1026 #endif // defined(__ELF)
1029 class DebugInfoSection : public DebugSection {
1031 explicit DebugInfoSection(CodeDescription* desc)
1033 : ELFSection(".debug_info", TYPE_PROGBITS, 1),
1035 : MachOSection("__debug_info",
1038 MachOSection::S_REGULAR | MachOSection::S_ATTR_DEBUG),
1043 enum DWARF2LocationOp {
1052 DW_OP_fbreg = 0x91 // 1 param: SLEB128 offset
1055 enum DWARF2Encoding {
1056 DW_ATE_ADDRESS = 0x1,
1060 bool WriteBodyInternal(Writer* w) {
1061 uintptr_t cu_start = w->position();
1062 Writer::Slot<uint32_t> size = w->CreateSlotHere<uint32_t>();
1063 uintptr_t start = w->position();
1064 w->Write<uint16_t>(2); // DWARF version.
1065 w->Write<uint32_t>(0); // Abbreviation table offset.
1066 w->Write<uint8_t>(sizeof(intptr_t));
1068 w->WriteULEB128(1); // Abbreviation code.
1069 w->WriteString(desc_->GetFilename().get());
1070 w->Write<intptr_t>(desc_->CodeStart());
1071 w->Write<intptr_t>(desc_->CodeStart() + desc_->CodeSize());
1072 w->Write<uint32_t>(0);
1074 uint32_t ty_offset = static_cast<uint32_t>(w->position() - cu_start);
1076 w->Write<uint8_t>(kPointerSize);
1077 w->WriteString("v8value");
1079 if (desc_->IsInfoAvailable()) {
1080 Scope* scope = desc_->info()->scope();
1082 w->WriteString(desc_->name());
1083 w->Write<intptr_t>(desc_->CodeStart());
1084 w->Write<intptr_t>(desc_->CodeStart() + desc_->CodeSize());
1085 Writer::Slot<uint32_t> fb_block_size = w->CreateSlotHere<uint32_t>();
1086 uintptr_t fb_block_start = w->position();
1087 #if V8_TARGET_ARCH_IA32
1088 w->Write<uint8_t>(DW_OP_reg5); // The frame pointer's here on ia32
1089 #elif V8_TARGET_ARCH_X64
1090 w->Write<uint8_t>(DW_OP_reg6); // and here on x64.
1091 #elif V8_TARGET_ARCH_ARM
1093 #elif V8_TARGET_ARCH_MIPS
1096 #error Unsupported target architecture.
1098 fb_block_size.set(static_cast<uint32_t>(w->position() - fb_block_start));
1100 int params = scope->num_parameters();
1101 int slots = scope->num_stack_slots();
1102 int context_slots = scope->ContextLocalCount();
1103 // The real slot ID is internal_slots + context_slot_id.
1104 int internal_slots = Context::MIN_CONTEXT_SLOTS;
1105 int locals = scope->StackLocalCount();
1106 int current_abbreviation = 4;
1108 for (int param = 0; param < params; ++param) {
1109 w->WriteULEB128(current_abbreviation++);
1111 scope->parameter(param)->name()->ToCString(DISALLOW_NULLS).get());
1112 w->Write<uint32_t>(ty_offset);
1113 Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
1114 uintptr_t block_start = w->position();
1115 w->Write<uint8_t>(DW_OP_fbreg);
1117 JavaScriptFrameConstants::kLastParameterOffset +
1118 kPointerSize * (params - param - 1));
1119 block_size.set(static_cast<uint32_t>(w->position() - block_start));
1122 EmbeddedVector<char, 256> buffer;
1123 StringBuilder builder(buffer.start(), buffer.length());
1125 for (int slot = 0; slot < slots; ++slot) {
1126 w->WriteULEB128(current_abbreviation++);
1128 builder.AddFormatted("slot%d", slot);
1129 w->WriteString(builder.Finalize());
1132 // See contexts.h for more information.
1133 ASSERT(Context::MIN_CONTEXT_SLOTS == 4);
1134 ASSERT(Context::CLOSURE_INDEX == 0);
1135 ASSERT(Context::PREVIOUS_INDEX == 1);
1136 ASSERT(Context::EXTENSION_INDEX == 2);
1137 ASSERT(Context::GLOBAL_OBJECT_INDEX == 3);
1138 w->WriteULEB128(current_abbreviation++);
1139 w->WriteString(".closure");
1140 w->WriteULEB128(current_abbreviation++);
1141 w->WriteString(".previous");
1142 w->WriteULEB128(current_abbreviation++);
1143 w->WriteString(".extension");
1144 w->WriteULEB128(current_abbreviation++);
1145 w->WriteString(".global");
1147 for (int context_slot = 0;
1148 context_slot < context_slots;
1150 w->WriteULEB128(current_abbreviation++);
1152 builder.AddFormatted("context_slot%d", context_slot + internal_slots);
1153 w->WriteString(builder.Finalize());
1156 ZoneList<Variable*> stack_locals(locals, scope->zone());
1157 ZoneList<Variable*> context_locals(context_slots, scope->zone());
1158 scope->CollectStackAndContextLocals(&stack_locals, &context_locals);
1159 for (int local = 0; local < locals; ++local) {
1160 w->WriteULEB128(current_abbreviation++);
1162 stack_locals[local]->name()->ToCString(DISALLOW_NULLS).get());
1163 w->Write<uint32_t>(ty_offset);
1164 Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
1165 uintptr_t block_start = w->position();
1166 w->Write<uint8_t>(DW_OP_fbreg);
1168 JavaScriptFrameConstants::kLocal0Offset -
1169 kPointerSize * local);
1170 block_size.set(static_cast<uint32_t>(w->position() - block_start));
1174 w->WriteULEB128(current_abbreviation++);
1175 w->WriteString("__function");
1176 w->Write<uint32_t>(ty_offset);
1177 Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
1178 uintptr_t block_start = w->position();
1179 w->Write<uint8_t>(DW_OP_fbreg);
1180 w->WriteSLEB128(JavaScriptFrameConstants::kFunctionOffset);
1181 block_size.set(static_cast<uint32_t>(w->position() - block_start));
1185 w->WriteULEB128(current_abbreviation++);
1186 w->WriteString("__context");
1187 w->Write<uint32_t>(ty_offset);
1188 Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
1189 uintptr_t block_start = w->position();
1190 w->Write<uint8_t>(DW_OP_fbreg);
1191 w->WriteSLEB128(StandardFrameConstants::kContextOffset);
1192 block_size.set(static_cast<uint32_t>(w->position() - block_start));
1195 w->WriteULEB128(0); // Terminate the sub program.
1198 w->WriteULEB128(0); // Terminate the compile unit.
1199 size.set(static_cast<uint32_t>(w->position() - start));
1204 CodeDescription* desc_;
1208 class DebugAbbrevSection : public DebugSection {
1210 explicit DebugAbbrevSection(CodeDescription* desc)
1212 : ELFSection(".debug_abbrev", TYPE_PROGBITS, 1),
1214 : MachOSection("__debug_abbrev",
1217 MachOSection::S_REGULAR | MachOSection::S_ATTR_DEBUG),
1221 // DWARF2 standard, figure 14.
1223 DW_TAG_FORMAL_PARAMETER = 0x05,
1224 DW_TAG_POINTER_TYPE = 0xf,
1225 DW_TAG_COMPILE_UNIT = 0x11,
1226 DW_TAG_STRUCTURE_TYPE = 0x13,
1227 DW_TAG_BASE_TYPE = 0x24,
1228 DW_TAG_SUBPROGRAM = 0x2e,
1229 DW_TAG_VARIABLE = 0x34
1232 // DWARF2 standard, figure 16.
1233 enum DWARF2ChildrenDetermination {
1238 // DWARF standard, figure 17.
1239 enum DWARF2Attribute {
1240 DW_AT_LOCATION = 0x2,
1242 DW_AT_BYTE_SIZE = 0xb,
1243 DW_AT_STMT_LIST = 0x10,
1244 DW_AT_LOW_PC = 0x11,
1245 DW_AT_HIGH_PC = 0x12,
1246 DW_AT_ENCODING = 0x3e,
1247 DW_AT_FRAME_BASE = 0x40,
1251 // DWARF2 standard, figure 19.
1252 enum DWARF2AttributeForm {
1254 DW_FORM_BLOCK4 = 0x4,
1255 DW_FORM_STRING = 0x8,
1256 DW_FORM_DATA4 = 0x6,
1257 DW_FORM_BLOCK = 0x9,
1258 DW_FORM_DATA1 = 0xb,
1263 void WriteVariableAbbreviation(Writer* w,
1264 int abbreviation_code,
1266 bool is_parameter) {
1267 w->WriteULEB128(abbreviation_code);
1268 w->WriteULEB128(is_parameter ? DW_TAG_FORMAL_PARAMETER : DW_TAG_VARIABLE);
1269 w->Write<uint8_t>(DW_CHILDREN_NO);
1270 w->WriteULEB128(DW_AT_NAME);
1271 w->WriteULEB128(DW_FORM_STRING);
1273 w->WriteULEB128(DW_AT_TYPE);
1274 w->WriteULEB128(DW_FORM_REF4);
1275 w->WriteULEB128(DW_AT_LOCATION);
1276 w->WriteULEB128(DW_FORM_BLOCK4);
1282 bool WriteBodyInternal(Writer* w) {
1283 int current_abbreviation = 1;
1284 bool extra_info = desc_->IsInfoAvailable();
1285 ASSERT(desc_->IsLineInfoAvailable());
1286 w->WriteULEB128(current_abbreviation++);
1287 w->WriteULEB128(DW_TAG_COMPILE_UNIT);
1288 w->Write<uint8_t>(extra_info ? DW_CHILDREN_YES : DW_CHILDREN_NO);
1289 w->WriteULEB128(DW_AT_NAME);
1290 w->WriteULEB128(DW_FORM_STRING);
1291 w->WriteULEB128(DW_AT_LOW_PC);
1292 w->WriteULEB128(DW_FORM_ADDR);
1293 w->WriteULEB128(DW_AT_HIGH_PC);
1294 w->WriteULEB128(DW_FORM_ADDR);
1295 w->WriteULEB128(DW_AT_STMT_LIST);
1296 w->WriteULEB128(DW_FORM_DATA4);
1301 Scope* scope = desc_->info()->scope();
1302 int params = scope->num_parameters();
1303 int slots = scope->num_stack_slots();
1304 int context_slots = scope->ContextLocalCount();
1305 // The real slot ID is internal_slots + context_slot_id.
1306 int internal_slots = Context::MIN_CONTEXT_SLOTS;
1307 int locals = scope->StackLocalCount();
1308 // Total children is params + slots + context_slots + internal_slots +
1309 // locals + 2 (__function and __context).
1311 // The extra duplication below seems to be necessary to keep
1312 // gdb from getting upset on OSX.
1313 w->WriteULEB128(current_abbreviation++); // Abbreviation code.
1314 w->WriteULEB128(DW_TAG_SUBPROGRAM);
1315 w->Write<uint8_t>(DW_CHILDREN_YES);
1316 w->WriteULEB128(DW_AT_NAME);
1317 w->WriteULEB128(DW_FORM_STRING);
1318 w->WriteULEB128(DW_AT_LOW_PC);
1319 w->WriteULEB128(DW_FORM_ADDR);
1320 w->WriteULEB128(DW_AT_HIGH_PC);
1321 w->WriteULEB128(DW_FORM_ADDR);
1322 w->WriteULEB128(DW_AT_FRAME_BASE);
1323 w->WriteULEB128(DW_FORM_BLOCK4);
1327 w->WriteULEB128(current_abbreviation++);
1328 w->WriteULEB128(DW_TAG_STRUCTURE_TYPE);
1329 w->Write<uint8_t>(DW_CHILDREN_NO);
1330 w->WriteULEB128(DW_AT_BYTE_SIZE);
1331 w->WriteULEB128(DW_FORM_DATA1);
1332 w->WriteULEB128(DW_AT_NAME);
1333 w->WriteULEB128(DW_FORM_STRING);
1337 for (int param = 0; param < params; ++param) {
1338 WriteVariableAbbreviation(w, current_abbreviation++, true, true);
1341 for (int slot = 0; slot < slots; ++slot) {
1342 WriteVariableAbbreviation(w, current_abbreviation++, false, false);
1345 for (int internal_slot = 0;
1346 internal_slot < internal_slots;
1348 WriteVariableAbbreviation(w, current_abbreviation++, false, false);
1351 for (int context_slot = 0;
1352 context_slot < context_slots;
1354 WriteVariableAbbreviation(w, current_abbreviation++, false, false);
1357 for (int local = 0; local < locals; ++local) {
1358 WriteVariableAbbreviation(w, current_abbreviation++, true, false);
1362 WriteVariableAbbreviation(w, current_abbreviation++, true, false);
1365 WriteVariableAbbreviation(w, current_abbreviation++, true, false);
1367 w->WriteULEB128(0); // Terminate the sibling list.
1370 w->WriteULEB128(0); // Terminate the table.
1375 CodeDescription* desc_;
1379 class DebugLineSection : public DebugSection {
1381 explicit DebugLineSection(CodeDescription* desc)
1383 : ELFSection(".debug_line", TYPE_PROGBITS, 1),
1385 : MachOSection("__debug_line",
1388 MachOSection::S_REGULAR | MachOSection::S_ATTR_DEBUG),
1392 // DWARF2 standard, figure 34.
1393 enum DWARF2Opcodes {
1395 DW_LNS_ADVANCE_PC = 2,
1396 DW_LNS_ADVANCE_LINE = 3,
1397 DW_LNS_SET_FILE = 4,
1398 DW_LNS_SET_COLUMN = 5,
1399 DW_LNS_NEGATE_STMT = 6
1402 // DWARF2 standard, figure 35.
1403 enum DWARF2ExtendedOpcode {
1404 DW_LNE_END_SEQUENCE = 1,
1405 DW_LNE_SET_ADDRESS = 2,
1406 DW_LNE_DEFINE_FILE = 3
1409 bool WriteBodyInternal(Writer* w) {
1411 Writer::Slot<uint32_t> total_length = w->CreateSlotHere<uint32_t>();
1412 uintptr_t start = w->position();
1414 // Used for special opcodes
1415 const int8_t line_base = 1;
1416 const uint8_t line_range = 7;
1417 const int8_t max_line_incr = (line_base + line_range - 1);
1418 const uint8_t opcode_base = DW_LNS_NEGATE_STMT + 1;
1420 w->Write<uint16_t>(2); // Field version.
1421 Writer::Slot<uint32_t> prologue_length = w->CreateSlotHere<uint32_t>();
1422 uintptr_t prologue_start = w->position();
1423 w->Write<uint8_t>(1); // Field minimum_instruction_length.
1424 w->Write<uint8_t>(1); // Field default_is_stmt.
1425 w->Write<int8_t>(line_base); // Field line_base.
1426 w->Write<uint8_t>(line_range); // Field line_range.
1427 w->Write<uint8_t>(opcode_base); // Field opcode_base.
1428 w->Write<uint8_t>(0); // DW_LNS_COPY operands count.
1429 w->Write<uint8_t>(1); // DW_LNS_ADVANCE_PC operands count.
1430 w->Write<uint8_t>(1); // DW_LNS_ADVANCE_LINE operands count.
1431 w->Write<uint8_t>(1); // DW_LNS_SET_FILE operands count.
1432 w->Write<uint8_t>(1); // DW_LNS_SET_COLUMN operands count.
1433 w->Write<uint8_t>(0); // DW_LNS_NEGATE_STMT operands count.
1434 w->Write<uint8_t>(0); // Empty include_directories sequence.
1435 w->WriteString(desc_->GetFilename().get()); // File name.
1436 w->WriteULEB128(0); // Current directory.
1437 w->WriteULEB128(0); // Unknown modification time.
1438 w->WriteULEB128(0); // Unknown file size.
1439 w->Write<uint8_t>(0);
1440 prologue_length.set(static_cast<uint32_t>(w->position() - prologue_start));
1442 WriteExtendedOpcode(w, DW_LNE_SET_ADDRESS, sizeof(intptr_t));
1443 w->Write<intptr_t>(desc_->CodeStart());
1444 w->Write<uint8_t>(DW_LNS_COPY);
1448 bool is_statement = true;
1450 List<GDBJITLineInfo::PCInfo>* pc_info = desc_->lineinfo()->pc_info();
1451 pc_info->Sort(&ComparePCInfo);
1453 int pc_info_length = pc_info->length();
1454 for (int i = 0; i < pc_info_length; i++) {
1455 GDBJITLineInfo::PCInfo* info = &pc_info->at(i);
1456 ASSERT(info->pc_ >= pc);
1458 // Reduce bloating in the debug line table by removing duplicate line
1459 // entries (per DWARF2 standard).
1460 intptr_t new_line = desc_->GetScriptLineNumber(info->pos_);
1461 if (new_line == line) {
1465 // Mark statement boundaries. For a better debugging experience, mark
1466 // the last pc address in the function as a statement (e.g. "}"), so that
1467 // a user can see the result of the last line executed in the function,
1468 // should control reach the end.
1469 if ((i+1) == pc_info_length) {
1470 if (!is_statement) {
1471 w->Write<uint8_t>(DW_LNS_NEGATE_STMT);
1473 } else if (is_statement != info->is_statement_) {
1474 w->Write<uint8_t>(DW_LNS_NEGATE_STMT);
1475 is_statement = !is_statement;
1478 // Generate special opcodes, if possible. This results in more compact
1479 // debug line tables. See the DWARF 2.0 standard to learn more about
1481 uintptr_t pc_diff = info->pc_ - pc;
1482 intptr_t line_diff = new_line - line;
1484 // Compute special opcode (see DWARF 2.0 standard)
1485 intptr_t special_opcode = (line_diff - line_base) +
1486 (line_range * pc_diff) + opcode_base;
1488 // If special_opcode is less than or equal to 255, it can be used as a
1489 // special opcode. If line_diff is larger than the max line increment
1490 // allowed for a special opcode, or if line_diff is less than the minimum
1491 // line that can be added to the line register (i.e. line_base), then
1492 // special_opcode can't be used.
1493 if ((special_opcode >= opcode_base) && (special_opcode <= 255) &&
1494 (line_diff <= max_line_incr) && (line_diff >= line_base)) {
1495 w->Write<uint8_t>(special_opcode);
1497 w->Write<uint8_t>(DW_LNS_ADVANCE_PC);
1498 w->WriteSLEB128(pc_diff);
1499 w->Write<uint8_t>(DW_LNS_ADVANCE_LINE);
1500 w->WriteSLEB128(line_diff);
1501 w->Write<uint8_t>(DW_LNS_COPY);
1504 // Increment the pc and line operands.
1508 // Advance the pc to the end of the routine, since the end sequence opcode
1510 w->Write<uint8_t>(DW_LNS_ADVANCE_PC);
1511 w->WriteSLEB128(desc_->CodeSize() - pc);
1512 WriteExtendedOpcode(w, DW_LNE_END_SEQUENCE, 0);
1513 total_length.set(static_cast<uint32_t>(w->position() - start));
1518 void WriteExtendedOpcode(Writer* w,
1519 DWARF2ExtendedOpcode op,
1520 size_t operands_size) {
1521 w->Write<uint8_t>(0);
1522 w->WriteULEB128(operands_size + 1);
1523 w->Write<uint8_t>(op);
1526 static int ComparePCInfo(const GDBJITLineInfo::PCInfo* a,
1527 const GDBJITLineInfo::PCInfo* b) {
1528 if (a->pc_ == b->pc_) {
1529 if (a->is_statement_ != b->is_statement_) {
1530 return b->is_statement_ ? +1 : -1;
1533 } else if (a->pc_ > b->pc_) {
1540 CodeDescription* desc_;
1544 #if V8_TARGET_ARCH_X64
1546 class UnwindInfoSection : public DebugSection {
1548 explicit UnwindInfoSection(CodeDescription* desc);
1549 virtual bool WriteBodyInternal(Writer* w);
1551 int WriteCIE(Writer* w);
1552 void WriteFDE(Writer* w, int);
1554 void WriteFDEStateOnEntry(Writer* w);
1555 void WriteFDEStateAfterRBPPush(Writer* w);
1556 void WriteFDEStateAfterRBPSet(Writer* w);
1557 void WriteFDEStateAfterRBPPop(Writer* w);
1559 void WriteLength(Writer* w,
1560 Writer::Slot<uint32_t>* length_slot,
1561 int initial_position);
1564 CodeDescription* desc_;
1566 // DWARF3 Specification, Table 7.23
1567 enum CFIInstructions {
1568 DW_CFA_ADVANCE_LOC = 0x40,
1569 DW_CFA_OFFSET = 0x80,
1570 DW_CFA_RESTORE = 0xC0,
1572 DW_CFA_SET_LOC = 0x01,
1573 DW_CFA_ADVANCE_LOC1 = 0x02,
1574 DW_CFA_ADVANCE_LOC2 = 0x03,
1575 DW_CFA_ADVANCE_LOC4 = 0x04,
1576 DW_CFA_OFFSET_EXTENDED = 0x05,
1577 DW_CFA_RESTORE_EXTENDED = 0x06,
1578 DW_CFA_UNDEFINED = 0x07,
1579 DW_CFA_SAME_VALUE = 0x08,
1580 DW_CFA_REGISTER = 0x09,
1581 DW_CFA_REMEMBER_STATE = 0x0A,
1582 DW_CFA_RESTORE_STATE = 0x0B,
1583 DW_CFA_DEF_CFA = 0x0C,
1584 DW_CFA_DEF_CFA_REGISTER = 0x0D,
1585 DW_CFA_DEF_CFA_OFFSET = 0x0E,
1587 DW_CFA_DEF_CFA_EXPRESSION = 0x0F,
1588 DW_CFA_EXPRESSION = 0x10,
1589 DW_CFA_OFFSET_EXTENDED_SF = 0x11,
1590 DW_CFA_DEF_CFA_SF = 0x12,
1591 DW_CFA_DEF_CFA_OFFSET_SF = 0x13,
1592 DW_CFA_VAL_OFFSET = 0x14,
1593 DW_CFA_VAL_OFFSET_SF = 0x15,
1594 DW_CFA_VAL_EXPRESSION = 0x16
1597 // System V ABI, AMD64 Supplement, Version 0.99.5, Figure 3.36
1598 enum RegisterMapping {
1599 // Only the relevant ones have been added to reduce clutter.
1608 CODE_ALIGN_FACTOR = 1,
1609 DATA_ALIGN_FACTOR = 1,
1610 RETURN_ADDRESS_REGISTER = AMD64_RA
1615 void UnwindInfoSection::WriteLength(Writer* w,
1616 Writer::Slot<uint32_t>* length_slot,
1617 int initial_position) {
1618 uint32_t align = (w->position() - initial_position) % kPointerSize;
1621 for (uint32_t i = 0; i < (kPointerSize - align); i++) {
1622 w->Write<uint8_t>(DW_CFA_NOP);
1626 ASSERT((w->position() - initial_position) % kPointerSize == 0);
1627 length_slot->set(w->position() - initial_position);
1631 UnwindInfoSection::UnwindInfoSection(CodeDescription* desc)
1633 : ELFSection(".eh_frame", TYPE_X86_64_UNWIND, 1),
1635 : MachOSection("__eh_frame", "__TEXT", sizeof(uintptr_t),
1636 MachOSection::S_REGULAR),
1640 int UnwindInfoSection::WriteCIE(Writer* w) {
1641 Writer::Slot<uint32_t> cie_length_slot = w->CreateSlotHere<uint32_t>();
1642 uint32_t cie_position = w->position();
1644 // Write out the CIE header. Currently no 'common instructions' are
1645 // emitted onto the CIE; every FDE has its own set of instructions.
1647 w->Write<uint32_t>(CIE_ID);
1648 w->Write<uint8_t>(CIE_VERSION);
1649 w->Write<uint8_t>(0); // Null augmentation string.
1650 w->WriteSLEB128(CODE_ALIGN_FACTOR);
1651 w->WriteSLEB128(DATA_ALIGN_FACTOR);
1652 w->Write<uint8_t>(RETURN_ADDRESS_REGISTER);
1654 WriteLength(w, &cie_length_slot, cie_position);
1656 return cie_position;
1660 void UnwindInfoSection::WriteFDE(Writer* w, int cie_position) {
1661 // The only FDE for this function. The CFA is the current RBP.
1662 Writer::Slot<uint32_t> fde_length_slot = w->CreateSlotHere<uint32_t>();
1663 int fde_position = w->position();
1664 w->Write<int32_t>(fde_position - cie_position + 4);
1666 w->Write<uintptr_t>(desc_->CodeStart());
1667 w->Write<uintptr_t>(desc_->CodeSize());
1669 WriteFDEStateOnEntry(w);
1670 WriteFDEStateAfterRBPPush(w);
1671 WriteFDEStateAfterRBPSet(w);
1672 WriteFDEStateAfterRBPPop(w);
1674 WriteLength(w, &fde_length_slot, fde_position);
1678 void UnwindInfoSection::WriteFDEStateOnEntry(Writer* w) {
1679 // The first state, just after the control has been transferred to the the
1682 // RBP for this function will be the value of RSP after pushing the RBP
1683 // for the previous function. The previous RBP has not been pushed yet.
1684 w->Write<uint8_t>(DW_CFA_DEF_CFA_SF);
1685 w->WriteULEB128(AMD64_RSP);
1686 w->WriteSLEB128(-kPointerSize);
1688 // The RA is stored at location CFA + kCallerPCOffset. This is an invariant,
1689 // and hence omitted from the next states.
1690 w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
1691 w->WriteULEB128(AMD64_RA);
1692 w->WriteSLEB128(StandardFrameConstants::kCallerPCOffset);
1694 // The RBP of the previous function is still in RBP.
1695 w->Write<uint8_t>(DW_CFA_SAME_VALUE);
1696 w->WriteULEB128(AMD64_RBP);
1698 // Last location described by this entry.
1699 w->Write<uint8_t>(DW_CFA_SET_LOC);
1701 desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_PUSH));
1705 void UnwindInfoSection::WriteFDEStateAfterRBPPush(Writer* w) {
1706 // The second state, just after RBP has been pushed.
1708 // RBP / CFA for this function is now the current RSP, so just set the
1709 // offset from the previous rule (from -8) to 0.
1710 w->Write<uint8_t>(DW_CFA_DEF_CFA_OFFSET);
1713 // The previous RBP is stored at CFA + kCallerFPOffset. This is an invariant
1714 // in this and the next state, and hence omitted in the next state.
1715 w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
1716 w->WriteULEB128(AMD64_RBP);
1717 w->WriteSLEB128(StandardFrameConstants::kCallerFPOffset);
1719 // Last location described by this entry.
1720 w->Write<uint8_t>(DW_CFA_SET_LOC);
1722 desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_SET));
1726 void UnwindInfoSection::WriteFDEStateAfterRBPSet(Writer* w) {
1727 // The third state, after the RBP has been set.
1729 // The CFA can now directly be set to RBP.
1730 w->Write<uint8_t>(DW_CFA_DEF_CFA);
1731 w->WriteULEB128(AMD64_RBP);
1734 // Last location described by this entry.
1735 w->Write<uint8_t>(DW_CFA_SET_LOC);
1737 desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_POP));
1741 void UnwindInfoSection::WriteFDEStateAfterRBPPop(Writer* w) {
1742 // The fourth (final) state. The RBP has been popped (just before issuing a
1745 // The CFA can is now calculated in the same way as in the first state.
1746 w->Write<uint8_t>(DW_CFA_DEF_CFA_SF);
1747 w->WriteULEB128(AMD64_RSP);
1748 w->WriteSLEB128(-kPointerSize);
1751 w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
1752 w->WriteULEB128(AMD64_RBP);
1753 w->WriteSLEB128(StandardFrameConstants::kCallerFPOffset);
1755 // Last location described by this entry.
1756 w->Write<uint8_t>(DW_CFA_SET_LOC);
1757 w->Write<uint64_t>(desc_->CodeEnd());
1761 bool UnwindInfoSection::WriteBodyInternal(Writer* w) {
1762 uint32_t cie_position = WriteCIE(w);
1763 WriteFDE(w, cie_position);
1768 #endif // V8_TARGET_ARCH_X64
1770 static void CreateDWARFSections(CodeDescription* desc,
1773 if (desc->IsLineInfoAvailable()) {
1774 obj->AddSection(new(zone) DebugInfoSection(desc));
1775 obj->AddSection(new(zone) DebugAbbrevSection(desc));
1776 obj->AddSection(new(zone) DebugLineSection(desc));
1778 #if V8_TARGET_ARCH_X64
1779 obj->AddSection(new(zone) UnwindInfoSection(desc));
1784 // -------------------------------------------------------------------
1785 // Binary GDB JIT Interface as described in
1786 // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
1794 struct JITCodeEntry {
1795 JITCodeEntry* next_;
1796 JITCodeEntry* prev_;
1797 Address symfile_addr_;
1798 uint64_t symfile_size_;
1801 struct JITDescriptor {
1803 uint32_t action_flag_;
1804 JITCodeEntry* relevant_entry_;
1805 JITCodeEntry* first_entry_;
1808 // GDB will place breakpoint into this function.
1809 // To prevent GCC from inlining or removing it we place noinline attribute
1810 // and inline assembler statement inside.
1811 void __attribute__((noinline)) __jit_debug_register_code() {
1815 // GDB will inspect contents of this descriptor.
1816 // Static initialization is necessary to prevent GDB from seeing
1817 // uninitialized descriptor.
1818 JITDescriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
1821 void __gdb_print_v8_object(Object* object) {
1823 PrintF(stdout, "\n");
1829 static JITCodeEntry* CreateCodeEntry(Address symfile_addr,
1830 uintptr_t symfile_size) {
1831 JITCodeEntry* entry = static_cast<JITCodeEntry*>(
1832 malloc(sizeof(JITCodeEntry) + symfile_size));
1834 entry->symfile_addr_ = reinterpret_cast<Address>(entry + 1);
1835 entry->symfile_size_ = symfile_size;
1836 OS::MemCopy(entry->symfile_addr_, symfile_addr, symfile_size);
1838 entry->prev_ = entry->next_ = NULL;
1844 static void DestroyCodeEntry(JITCodeEntry* entry) {
1849 static void RegisterCodeEntry(JITCodeEntry* entry,
1850 bool dump_if_enabled,
1851 const char* name_hint) {
1852 #if defined(DEBUG) && !V8_OS_WIN
1853 static int file_num = 0;
1854 if (FLAG_gdbjit_dump && dump_if_enabled) {
1855 static const int kMaxFileNameSize = 64;
1856 static const char* kElfFilePrefix = "/tmp/elfdump";
1857 static const char* kObjFileExt = ".o";
1860 OS::SNPrintF(Vector<char>(file_name, kMaxFileNameSize),
1863 (name_hint != NULL) ? name_hint : "",
1866 WriteBytes(file_name, entry->symfile_addr_, entry->symfile_size_);
1870 entry->next_ = __jit_debug_descriptor.first_entry_;
1871 if (entry->next_ != NULL) entry->next_->prev_ = entry;
1872 __jit_debug_descriptor.first_entry_ =
1873 __jit_debug_descriptor.relevant_entry_ = entry;
1875 __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
1876 __jit_debug_register_code();
1880 static void UnregisterCodeEntry(JITCodeEntry* entry) {
1881 if (entry->prev_ != NULL) {
1882 entry->prev_->next_ = entry->next_;
1884 __jit_debug_descriptor.first_entry_ = entry->next_;
1887 if (entry->next_ != NULL) {
1888 entry->next_->prev_ = entry->prev_;
1891 __jit_debug_descriptor.relevant_entry_ = entry;
1892 __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
1893 __jit_debug_register_code();
1897 static JITCodeEntry* CreateELFObject(CodeDescription* desc, Isolate* isolate) {
1900 MachO mach_o(&zone);
1903 mach_o.AddSection(new(&zone) MachOTextSection(kCodeAlignment,
1907 CreateDWARFSections(desc, &zone, &mach_o);
1909 mach_o.Write(&w, desc->CodeStart(), desc->CodeSize());
1915 int text_section_index = elf.AddSection(
1916 new(&zone) FullHeaderELFSection(
1918 ELFSection::TYPE_NOBITS,
1923 ELFSection::FLAG_ALLOC | ELFSection::FLAG_EXEC));
1925 CreateSymbolsTable(desc, &zone, &elf, text_section_index);
1927 CreateDWARFSections(desc, &zone, &elf);
1932 return CreateCodeEntry(w.buffer(), w.position());
1936 static bool SameCodeObjects(void* key1, void* key2) {
1937 return key1 == key2;
1941 static HashMap* GetEntries() {
1942 static HashMap* entries = NULL;
1943 if (entries == NULL) {
1944 entries = new HashMap(&SameCodeObjects);
1950 static uint32_t HashForCodeObject(Code* code) {
1951 static const uintptr_t kGoldenRatio = 2654435761u;
1952 uintptr_t hash = reinterpret_cast<uintptr_t>(code->address());
1953 return static_cast<uint32_t>((hash >> kCodeAlignmentBits) * kGoldenRatio);
1957 static const intptr_t kLineInfoTag = 0x1;
1960 static bool IsLineInfoTagged(void* ptr) {
1961 return 0 != (reinterpret_cast<intptr_t>(ptr) & kLineInfoTag);
1965 static void* TagLineInfo(GDBJITLineInfo* ptr) {
1966 return reinterpret_cast<void*>(
1967 reinterpret_cast<intptr_t>(ptr) | kLineInfoTag);
1971 static GDBJITLineInfo* UntagLineInfo(void* ptr) {
1972 return reinterpret_cast<GDBJITLineInfo*>(
1973 reinterpret_cast<intptr_t>(ptr) & ~kLineInfoTag);
1977 void GDBJITInterface::AddCode(Handle<Name> name,
1978 Handle<Script> script,
1980 CompilationInfo* info) {
1981 if (!FLAG_gdbjit) return;
1983 Script::InitLineEnds(script);
1985 if (!name.is_null() && name->IsString()) {
1986 SmartArrayPointer<char> name_cstring =
1987 Handle<String>::cast(name)->ToCString(DISALLOW_NULLS);
1988 AddCode(name_cstring.get(), *code, GDBJITInterface::FUNCTION, *script,
1991 AddCode("", *code, GDBJITInterface::FUNCTION, *script, info);
1996 static void AddUnwindInfo(CodeDescription* desc) {
1997 #if V8_TARGET_ARCH_X64
1998 if (desc->tag() == GDBJITInterface::FUNCTION) {
1999 // To avoid propagating unwinding information through
2000 // compilation pipeline we use an approximation.
2001 // For most use cases this should not affect usability.
2002 static const int kFramePointerPushOffset = 1;
2003 static const int kFramePointerSetOffset = 4;
2004 static const int kFramePointerPopOffset = -3;
2006 uintptr_t frame_pointer_push_address =
2007 desc->CodeStart() + kFramePointerPushOffset;
2009 uintptr_t frame_pointer_set_address =
2010 desc->CodeStart() + kFramePointerSetOffset;
2012 uintptr_t frame_pointer_pop_address =
2013 desc->CodeEnd() + kFramePointerPopOffset;
2015 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_PUSH,
2016 frame_pointer_push_address);
2017 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_SET,
2018 frame_pointer_set_address);
2019 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_POP,
2020 frame_pointer_pop_address);
2022 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_PUSH,
2024 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_SET,
2026 desc->SetStackStateStartAddress(CodeDescription::POST_RBP_POP,
2029 #endif // V8_TARGET_ARCH_X64
2033 static LazyMutex mutex = LAZY_MUTEX_INITIALIZER;
2036 void GDBJITInterface::AddCode(const char* name,
2038 GDBJITInterface::CodeTag tag,
2040 CompilationInfo* info) {
2041 if (!FLAG_gdbjit) return;
2043 LockGuard<Mutex> lock_guard(mutex.Pointer());
2044 DisallowHeapAllocation no_gc;
2046 HashMap::Entry* e = GetEntries()->Lookup(code, HashForCodeObject(code), true);
2047 if (e->value != NULL && !IsLineInfoTagged(e->value)) return;
2049 GDBJITLineInfo* lineinfo = UntagLineInfo(e->value);
2050 CodeDescription code_desc(name,
2052 script != NULL ? Handle<Script>(script)
2058 if (!FLAG_gdbjit_full && !code_desc.IsLineInfoAvailable()) {
2060 GetEntries()->Remove(code, HashForCodeObject(code));
2064 AddUnwindInfo(&code_desc);
2065 Isolate* isolate = code->GetIsolate();
2066 JITCodeEntry* entry = CreateELFObject(&code_desc, isolate);
2067 ASSERT(!IsLineInfoTagged(entry));
2072 const char* name_hint = NULL;
2073 bool should_dump = false;
2074 if (FLAG_gdbjit_dump) {
2075 if (strlen(FLAG_gdbjit_dump_filter) == 0) {
2078 } else if (name != NULL) {
2079 name_hint = strstr(name, FLAG_gdbjit_dump_filter);
2080 should_dump = (name_hint != NULL);
2083 RegisterCodeEntry(entry, should_dump, name_hint);
2087 void GDBJITInterface::AddCode(GDBJITInterface::CodeTag tag,
2090 if (!FLAG_gdbjit) return;
2092 EmbeddedVector<char, 256> buffer;
2093 StringBuilder builder(buffer.start(), buffer.length());
2095 builder.AddString(Tag2String(tag));
2096 if ((name != NULL) && (*name != '\0')) {
2097 builder.AddString(": ");
2098 builder.AddString(name);
2100 builder.AddFormatted(": code object %p", static_cast<void*>(code));
2103 AddCode(builder.Finalize(), code, tag, NULL, NULL);
2107 void GDBJITInterface::AddCode(GDBJITInterface::CodeTag tag,
2110 if (!FLAG_gdbjit) return;
2111 if (name != NULL && name->IsString()) {
2112 AddCode(tag, String::cast(name)->ToCString(DISALLOW_NULLS).get(), code);
2114 AddCode(tag, "", code);
2119 void GDBJITInterface::AddCode(GDBJITInterface::CodeTag tag, Code* code) {
2120 if (!FLAG_gdbjit) return;
2122 AddCode(tag, "", code);
2126 void GDBJITInterface::RemoveCode(Code* code) {
2127 if (!FLAG_gdbjit) return;
2129 LockGuard<Mutex> lock_guard(mutex.Pointer());
2130 HashMap::Entry* e = GetEntries()->Lookup(code,
2131 HashForCodeObject(code),
2133 if (e == NULL) return;
2135 if (IsLineInfoTagged(e->value)) {
2136 delete UntagLineInfo(e->value);
2138 JITCodeEntry* entry = static_cast<JITCodeEntry*>(e->value);
2139 UnregisterCodeEntry(entry);
2140 DestroyCodeEntry(entry);
2143 GetEntries()->Remove(code, HashForCodeObject(code));
2147 void GDBJITInterface::RemoveCodeRange(Address start, Address end) {
2148 HashMap* entries = GetEntries();
2149 Zone zone(Isolate::Current());
2150 ZoneList<Code*> dead_codes(1, &zone);
2152 for (HashMap::Entry* e = entries->Start(); e != NULL; e = entries->Next(e)) {
2153 Code* code = reinterpret_cast<Code*>(e->key);
2154 if (code->address() >= start && code->address() < end) {
2155 dead_codes.Add(code, &zone);
2159 for (int i = 0; i < dead_codes.length(); i++) {
2160 RemoveCode(dead_codes.at(i));
2165 void GDBJITInterface::RegisterDetailedLineInfo(Code* code,
2166 GDBJITLineInfo* line_info) {
2167 LockGuard<Mutex> lock_guard(mutex.Pointer());
2168 ASSERT(!IsLineInfoTagged(line_info));
2169 HashMap::Entry* e = GetEntries()->Lookup(code, HashForCodeObject(code), true);
2170 ASSERT(e->value == NULL);
2171 e->value = TagLineInfo(line_info);
2175 } } // namespace v8::internal