10511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com// Copyright 2010 the V8 project authors. All rights reserved.
23484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org// Use of this source code is governed by a BSD-style license that can be
33484964a86451e86dcf04be9bd8c0d76ee04f081rossberg@chromium.org// found in the LICENSE file.
40511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
50511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#ifdef ENABLE_GDB_JIT_INTERFACE
6196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "src/v8.h"
70511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
821d700eedcdd6570eff22ece724b63a5eefe78cbmachenbach@chromium.org#include "src/base/bits.h"
95de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org#include "src/base/platform/platform.h"
10196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "src/bootstrapper.h"
11196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "src/compiler.h"
12196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "src/frames-inl.h"
134b0feeef5d01dbc2948080b4f69daa37e1083461machenbach@chromium.org#include "src/frames.h"
144b0feeef5d01dbc2948080b4f69daa37e1083461machenbach@chromium.org#include "src/gdb-jit.h"
15196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "src/global-handles.h"
16196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "src/messages.h"
17196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "src/natives.h"
187d299ad4dc0ca26e0017b190b48362ad71328ce4machenbach@chromium.org#include "src/ostreams.h"
19196eb601290dc49c3754da728dc58700dff2de1bmachenbach@chromium.org#include "src/scopes.h"
200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comnamespace v8 {
220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comnamespace internal {
230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
244f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#ifdef __APPLE__
254f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#define __MACH_O
264f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass MachO;
274f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass MachOSection;
284f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgtypedef MachO DebugObject;
294f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgtypedef MachOSection DebugSection;
304f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
314f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#define __ELF
320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comclass ELF;
334f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass ELFSection;
344f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgtypedef ELF DebugObject;
354f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgtypedef ELFSection DebugSection;
364f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comclass Writer BASE_EMBEDDED {
390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
404f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  explicit Writer(DebugObject* debug_object)
414f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      : debug_object_(debug_object),
420511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        position_(0),
430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        capacity_(1024),
440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        buffer_(reinterpret_cast<byte*>(malloc(capacity_))) {
450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
470511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  ~Writer() {
480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    free(buffer_);
490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t position() const {
520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return position_;
530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  template<typename T>
560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  class Slot {
570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com   public:
580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Slot(Writer* w, uintptr_t offset) : w_(w), offset_(offset) { }
590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    T* operator-> () {
610511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      return w_->RawSlotAt<T>(offset_);
620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
630511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
640511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    void set(const T& value) {
650511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      *w_->RawSlotAt<T>(offset_) = value;
660511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
680511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Slot<T> at(int i) {
690511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      return Slot<T>(w_, offset_ + sizeof(T) * i);
700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
710511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
720511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com   private:
730511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Writer* w_;
740511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t offset_;
750511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
760511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
770511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  template<typename T>
780511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void Write(const T& val) {
790511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Ensure(position_ + sizeof(T));
800511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    *RawSlotAt<T>(position_) = val;
810511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    position_ += sizeof(T);
820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
840511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  template<typename T>
850511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  Slot<T> SlotAt(uintptr_t offset) {
860511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Ensure(offset + sizeof(T));
870511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return Slot<T>(this, offset);
880511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
890511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
900511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  template<typename T>
910511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  Slot<T> CreateSlotHere() {
920511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return CreateSlotsHere<T>(1);
930511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
940511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
950511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  template<typename T>
960511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  Slot<T> CreateSlotsHere(uint32_t count) {
970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t slot_position = position_;
980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    position_ += sizeof(T) * count;
990511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Ensure(position_);
1000511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return SlotAt<T>(slot_position);
1010511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
1020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void Ensure(uintptr_t pos) {
1040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    if (capacity_ < pos) {
1050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      while (capacity_ < pos) capacity_ *= 2;
1060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      buffer_ = reinterpret_cast<byte*>(realloc(buffer_, capacity_));
1070511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
1080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
1090511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1104f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  DebugObject* debug_object() { return debug_object_; }
1110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  byte* buffer() { return buffer_; }
1130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void Align(uintptr_t align) {
1150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t delta = position_ % align;
1160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    if (delta == 0) return;
1170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t padding = align - delta;
1180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Ensure(position_ += padding);
119e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK((position_ % align) == 0);
1200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
1210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void WriteULEB128(uintptr_t value) {
1230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    do {
1240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      uint8_t byte = value & 0x7F;
1250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      value >>= 7;
1260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      if (value != 0) byte |= 0x80;
1270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      Write<uint8_t>(byte);
1280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    } while (value != 0);
1290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
1300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1310511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void WriteSLEB128(intptr_t value) {
1320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    bool more = true;
1330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    while (more) {
1340511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      int8_t byte = value & 0x7F;
1350511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      bool byte_sign = byte & 0x40;
1360511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      value >>= 7;
1370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      if ((value == 0 && !byte_sign) || (value == -1 && byte_sign)) {
1390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        more = false;
1400511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      } else {
1410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        byte |= 0x80;
1420511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      }
1430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      Write<int8_t>(byte);
1450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
1460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
1470511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void WriteString(const char* str) {
1490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    do {
1500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      Write<char>(*str);
1510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    } while (*str++);
1520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
1530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
1550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  template<typename T> friend class Slot;
1560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  template<typename T>
1580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  T* RawSlotAt(uintptr_t offset) {
159e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(offset < capacity_ && offset + sizeof(T) <= capacity_);
1600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return reinterpret_cast<T*>(&buffer_[offset]);
1610511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
1620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1634f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  DebugObject* debug_object_;
1640511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t position_;
1650511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t capacity_;
1660511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  byte* buffer_;
1670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
1680511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1697bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.orgclass ELFStringTable;
1700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
1714f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgtemplate<typename THeader>
1724f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass DebugSectionBase : public ZoneObject {
1730511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
1744f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  virtual ~DebugSectionBase() { }
1754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
1764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  virtual void WriteBody(Writer::Slot<THeader> header, Writer* writer) {
1774f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uintptr_t start = writer->position();
17833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    if (WriteBodyInternal(writer)) {
1794f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      uintptr_t end = writer->position();
1804f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      header->offset = start;
1814f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#if defined(__MACH_O)
1824f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      header->addr = 0;
1834f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
1844f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      header->size = end - start;
1854f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    }
1864f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
1874f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
18833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  virtual bool WriteBodyInternal(Writer* writer) {
1894f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    return false;
1904f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
1914f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
1924f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  typedef THeader Header;
1934f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org};
1944f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
1954f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
1964f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgstruct MachOSectionHeader {
1974f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  char sectname[16];
1984f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  char segname[16];
199864abd7677f434b5aef191e3388e71cd4dd1e6c8machenbach@chromium.org#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
2004f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t addr;
2014f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t size;
2024f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
2034f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint64_t addr;
2044f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint64_t size;
2054f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
2064f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t offset;
2074f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t align;
2084f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t reloff;
2094f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t nreloc;
2104f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t flags;
2114f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t reserved1;
2124f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t reserved2;
2134f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org};
2144f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
2154f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
2164f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass MachOSection : public DebugSectionBase<MachOSectionHeader> {
2174f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org public:
2184f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  enum Type {
2194f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    S_REGULAR = 0x0u,
2204f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    S_ATTR_COALESCED = 0xbu,
2214f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    S_ATTR_SOME_INSTRUCTIONS = 0x400u,
2224f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    S_ATTR_DEBUG = 0x02000000u,
2234f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    S_ATTR_PURE_INSTRUCTIONS = 0x80000000u
2240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
2250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
22621d700eedcdd6570eff22ece724b63a5eefe78cbmachenbach@chromium.org  MachOSection(const char* name, const char* segment, uint32_t align,
2274f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org               uint32_t flags)
22821d700eedcdd6570eff22ece724b63a5eefe78cbmachenbach@chromium.org      : name_(name), segment_(segment), align_(align), flags_(flags) {
2294f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    if (align_ != 0) {
23021d700eedcdd6570eff22ece724b63a5eefe78cbmachenbach@chromium.org      DCHECK(base::bits::IsPowerOfTwo32(align));
2314f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      align_ = WhichPowerOf2(align_);
2324f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    }
2334f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
2344f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
2354f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  virtual ~MachOSection() { }
2364f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
2374f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  virtual void PopulateHeader(Writer::Slot<Header> header) {
2384f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->addr = 0;
2394f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->size = 0;
2404f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->offset = 0;
2414f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->align = align_;
2424f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->reloff = 0;
2434f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->nreloc = 0;
2444f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->flags = flags_;
2454f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->reserved1 = 0;
2464f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->reserved2 = 0;
2474f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    memset(header->sectname, 0, sizeof(header->sectname));
2484f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    memset(header->segname, 0, sizeof(header->segname));
249e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(strlen(name_) < sizeof(header->sectname));
250e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(strlen(segment_) < sizeof(header->segname));
2514f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    strncpy(header->sectname, name_, sizeof(header->sectname));
2524f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    strncpy(header->segname, segment_, sizeof(header->segname));
2534f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
2544f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
2554f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org private:
2564f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  const char* name_;
2574f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  const char* segment_;
25821d700eedcdd6570eff22ece724b63a5eefe78cbmachenbach@chromium.org  uint32_t align_;
2594f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t flags_;
2604f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org};
2614f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
2624f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
2634f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgstruct ELFSectionHeader {
2644f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t name;
2654f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t type;
2664f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uintptr_t flags;
2674f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uintptr_t address;
2684f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uintptr_t offset;
2694f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uintptr_t size;
2704f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t link;
2714f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t info;
2724f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uintptr_t alignment;
2734f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uintptr_t entry_size;
2744f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org};
2754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
2764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
2774f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#if defined(__ELF)
2784f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass ELFSection : public DebugSectionBase<ELFSectionHeader> {
2794f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org public:
2800511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum Type {
2810511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_NULL = 0,
2820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_PROGBITS = 1,
2830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_SYMTAB = 2,
2840511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_STRTAB = 3,
2850511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_RELA = 4,
2860511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_HASH = 5,
2870511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_DYNAMIC = 6,
2880511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_NOTE = 7,
2890511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_NOBITS = 8,
2900511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_REL = 9,
2910511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_SHLIB = 10,
2920511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_DYNSYM = 11,
2930511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_LOPROC = 0x70000000,
29431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    TYPE_X86_64_UNWIND = 0x70000001,
2950511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_HIPROC = 0x7fffffff,
2960511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_LOUSER = 0x80000000,
2970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_HIUSER = 0xffffffff
2980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
2990511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3000511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum Flags {
3010511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    FLAG_WRITE = 1,
3020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    FLAG_ALLOC = 2,
3030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    FLAG_EXEC = 4
3040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
3050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum SpecialIndexes {
3070511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    INDEX_ABSOLUTE = 0xfff1
3080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
3090511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3100511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  ELFSection(const char* name, Type type, uintptr_t align)
3110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      : name_(name), type_(type), align_(align) { }
3120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  virtual ~ELFSection() { }
3140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3157bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org  void PopulateHeader(Writer::Slot<Header> header, ELFStringTable* strtab);
3160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
3180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t start = w->position();
31933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    if (WriteBodyInternal(w)) {
3200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      uintptr_t end = w->position();
3210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      header->offset = start;
3220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      header->size = end - start;
3230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
3240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
3250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
32633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  virtual bool WriteBodyInternal(Writer* w) {
3270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return false;
3280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
3290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uint16_t index() const { return index_; }
3310511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void set_index(uint16_t index) { index_ = index; }
3320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com protected:
3340511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  virtual void PopulateHeader(Writer::Slot<Header> header) {
3350511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->flags = 0;
3360511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->address = 0;
3370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->offset = 0;
3380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->size = 0;
3390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->link = 0;
3400511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->info = 0;
3410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->entry_size = 0;
3420511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
3430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
3450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  const char* name_;
3460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  Type type_;
3470511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t align_;
3480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uint16_t index_;
3490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
3504f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif  // defined(__ELF)
3514f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
3524f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
3534f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#if defined(__MACH_O)
3544f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass MachOTextSection : public MachOSection {
3554f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org public:
3564f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  MachOTextSection(uintptr_t align,
3574f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                   uintptr_t addr,
3584f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                   uintptr_t size)
3594f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      : MachOSection("__text",
3604f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     "__TEXT",
3614f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     align,
3624f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     MachOSection::S_REGULAR |
3634f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                         MachOSection::S_ATTR_SOME_INSTRUCTIONS |
3644f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                         MachOSection::S_ATTR_PURE_INSTRUCTIONS),
3654f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        addr_(addr),
3664f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        size_(size) { }
3670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3684f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org protected:
3694f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  virtual void PopulateHeader(Writer::Slot<Header> header) {
3704f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    MachOSection::PopulateHeader(header);
3714f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->addr = addr_;
3724f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->size = size_;
3734f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
3740511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org private:
3764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uintptr_t addr_;
3774f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uintptr_t size_;
3784f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org};
3794f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif  // defined(__MACH_O)
3804f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
3814f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
3824f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#if defined(__ELF)
3830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comclass FullHeaderELFSection : public ELFSection {
3840511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
3850511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  FullHeaderELFSection(const char* name,
3860511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                       Type type,
3870511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                       uintptr_t align,
3880511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                       uintptr_t addr,
3890511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                       uintptr_t offset,
3900511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                       uintptr_t size,
3910511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                       uintptr_t flags)
3920511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      : ELFSection(name, type, align),
3930511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        addr_(addr),
3940511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        offset_(offset),
3950511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        size_(size),
3960511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        flags_(flags) { }
3970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
3980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com protected:
3990511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  virtual void PopulateHeader(Writer::Slot<Header> header) {
4000511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    ELFSection::PopulateHeader(header);
4010511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->address = addr_;
4020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->offset = offset_;
4030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->size = size_;
4040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->flags = flags_;
4050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
4060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4070511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
4080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t addr_;
4090511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t offset_;
4100511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t size_;
4110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t flags_;
4120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
4130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4157bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.orgclass ELFStringTable : public ELFSection {
4160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
4177bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org  explicit ELFStringTable(const char* name)
4180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      : ELFSection(name, TYPE_STRTAB, 1), writer_(NULL), offset_(0), size_(0) {
4190511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
4200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t Add(const char* str) {
4220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    if (*str == '\0') return 0;
4230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t offset = size_;
4250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    WriteString(str);
4260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return offset;
4270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
4280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void AttachWriter(Writer* w) {
4300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    writer_ = w;
4310511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    offset_ = writer_->position();
4320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    // First entry in the string table should be an empty string.
4340511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    WriteString("");
4350511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
4360511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void DetachWriter() {
4380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    writer_ = NULL;
4390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
4400511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
442e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(writer_ == NULL);
4430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->offset = offset_;
4440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->size = size_;
4450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
4460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4470511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
4480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void WriteString(const char* str) {
4490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t written = 0;
4500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    do {
4510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      writer_->Write(*str);
4520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      written++;
4530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    } while (*str++);
4540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    size_ += written;
4550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
4560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  Writer* writer_;
4580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t offset_;
4600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t size_;
4610511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
4620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4630511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
4640511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comvoid ELFSection::PopulateHeader(Writer::Slot<ELFSection::Header> header,
4657bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org                                ELFStringTable* strtab) {
4660511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  header->name = strtab->Add(name_);
4670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  header->type = type_;
4680511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  header->alignment = align_;
4690511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  PopulateHeader(header);
4700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
4714f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif  // defined(__ELF)
4724f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
4734f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
4744f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#if defined(__MACH_O)
4754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass MachO BASE_EMBEDDED {
4764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org public:
477c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  explicit MachO(Zone* zone) : zone_(zone), sections_(6, zone) { }
4784f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
4794f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  uint32_t AddSection(MachOSection* section) {
480c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org    sections_.Add(section, zone_);
4814f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    return sections_.length() - 1;
4824f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
4834f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
4844f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  void Write(Writer* w, uintptr_t code_start, uintptr_t code_size) {
4854f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    Writer::Slot<MachOHeader> header = WriteHeader(w);
4864f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uintptr_t load_command_start = w->position();
4874f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    Writer::Slot<MachOSegmentCommand> cmd = WriteSegmentCommand(w,
4884f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                                                                code_start,
4894f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                                                                code_size);
4904f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    WriteSections(w, cmd, header, load_command_start);
4914f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
4924f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
4934f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org private:
4944f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  struct MachOHeader {
4954f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t magic;
4964f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t cputype;
4974f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t cpusubtype;
4984f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t filetype;
4994f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t ncmds;
5004f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t sizeofcmds;
5014f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t flags;
50293a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#if V8_TARGET_ARCH_X64
5034f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t reserved;
5044f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
5054f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  };
5064f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
5074f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  struct MachOSegmentCommand {
5084f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t cmd;
5094f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t cmdsize;
5104f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    char segname[16];
511864abd7677f434b5aef191e3388e71cd4dd1e6c8machenbach@chromium.org#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
5124f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t vmaddr;
5134f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t vmsize;
5144f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t fileoff;
5154f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t filesize;
5164f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
5174f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint64_t vmaddr;
5184f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint64_t vmsize;
5194f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint64_t fileoff;
5204f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint64_t filesize;
5214f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
5224f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t maxprot;
5234f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t initprot;
5244f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t nsects;
5254f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t flags;
5264f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  };
5274f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
5284f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  enum MachOLoadCommandCmd {
5294f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    LC_SEGMENT_32 = 0x00000001u,
5304f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    LC_SEGMENT_64 = 0x00000019u
5314f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  };
5324f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
5334f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
5344f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  Writer::Slot<MachOHeader> WriteHeader(Writer* w) {
535e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(w->position() == 0);
5364f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    Writer::Slot<MachOHeader> header = w->CreateSlotHere<MachOHeader>();
537864abd7677f434b5aef191e3388e71cd4dd1e6c8machenbach@chromium.org#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
5384f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->magic = 0xFEEDFACEu;
5394f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->cputype = 7;  // i386
5404f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->cpusubtype = 3;  // CPU_SUBTYPE_I386_ALL
54193a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#elif V8_TARGET_ARCH_X64
5424f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->magic = 0xFEEDFACFu;
5434f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->cputype = 7 | 0x01000000;  // i386 | 64-bit ABI
5444f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->cpusubtype = 3;  // CPU_SUBTYPE_I386_ALL
5454f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->reserved = 0;
5464f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
5474f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#error Unsupported target architecture.
5484f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
5494f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->filetype = 0x1;  // MH_OBJECT
5504f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->ncmds = 1;
5514f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->sizeofcmds = 0;
5524f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->flags = 0;
5534f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    return header;
5544f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
5554f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
5564f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
5574f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  Writer::Slot<MachOSegmentCommand> WriteSegmentCommand(Writer* w,
5584f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                                                        uintptr_t code_start,
5594f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                                                        uintptr_t code_size) {
5604f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    Writer::Slot<MachOSegmentCommand> cmd =
5614f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->CreateSlotHere<MachOSegmentCommand>();
562864abd7677f434b5aef191e3388e71cd4dd1e6c8machenbach@chromium.org#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
5634f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->cmd = LC_SEGMENT_32;
5644f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
5654f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->cmd = LC_SEGMENT_64;
5664f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
5674f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->vmaddr = code_start;
5684f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->vmsize = code_size;
5694f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->fileoff = 0;
5704f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->filesize = 0;
5714f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->maxprot = 7;
5724f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->initprot = 7;
5734f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->flags = 0;
5744f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->nsects = sections_.length();
5754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    memset(cmd->segname, 0, 16);
5764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->cmdsize = sizeof(MachOSegmentCommand) + sizeof(MachOSection::Header) *
5774f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        cmd->nsects;
5784f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    return cmd;
5794f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
5804f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
5814f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
5824f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  void WriteSections(Writer* w,
5834f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     Writer::Slot<MachOSegmentCommand> cmd,
5844f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     Writer::Slot<MachOHeader> header,
5854f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     uintptr_t load_command_start) {
5864f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    Writer::Slot<MachOSection::Header> headers =
5874f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->CreateSlotsHere<MachOSection::Header>(sections_.length());
5884f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->fileoff = w->position();
5894f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    header->sizeofcmds = w->position() - load_command_start;
5904f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    for (int section = 0; section < sections_.length(); ++section) {
5914f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      sections_[section]->PopulateHeader(headers.at(section));
5924f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      sections_[section]->WriteBody(headers.at(section), w);
5934f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    }
5944f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    cmd->filesize = w->position() - (uintptr_t)cmd->fileoff;
5954f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
5964f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
597c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  Zone* zone_;
5984f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  ZoneList<MachOSection*> sections_;
5994f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org};
6004f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif  // defined(__MACH_O)
6010511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6034f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#if defined(__ELF)
6040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comclass ELF BASE_EMBEDDED {
6050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
606c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  explicit ELF(Zone* zone) : zone_(zone), sections_(6, zone) {
60733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org    sections_.Add(new(zone) ELFSection("", ELFSection::TYPE_NULL, 0), zone);
6087bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org    sections_.Add(new(zone) ELFStringTable(".shstrtab"), zone);
6090511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
6100511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void Write(Writer* w) {
6120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    WriteHeader(w);
6130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    WriteSectionTable(w);
6140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    WriteSections(w);
6150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
6160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  ELFSection* SectionAt(uint32_t index) {
6180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return sections_[index];
6190511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
6200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
621c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  uint32_t AddSection(ELFSection* section) {
622c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org    sections_.Add(section, zone_);
6230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    section->set_index(sections_.length() - 1);
6240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return sections_.length() - 1;
6250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
6260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
6280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  struct ELFHeader {
6290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint8_t ident[16];
6300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t type;
6310511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t machine;
6320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint32_t version;
6330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t entry;
6340511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t pht_offset;
6350511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t sht_offset;
6360511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint32_t flags;
6370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t header_size;
6380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t pht_entry_size;
6390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t pht_entry_num;
6400511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t sht_entry_size;
6410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t sht_entry_num;
6420511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t sht_strtab_index;
6430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
6440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void WriteHeader(Writer* w) {
647e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(w->position() == 0);
6480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Writer::Slot<ELFHeader> header = w->CreateSlotHere<ELFHeader>();
64908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#if (V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_X87 || \
65008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org     (V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_32_BIT))
6510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    const uint8_t ident[16] =
6520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        { 0x7f, 'E', 'L', 'F', 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
65308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#elif V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_64_BIT
6540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    const uint8_t ident[16] =
65533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        { 0x7f, 'E', 'L', 'F', 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0};
6560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#else
6570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#error Unsupported target architecture.
6580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#endif
659d06b9264b1c886fc80a100e9915cf8ae07fdb4e5machenbach@chromium.org    memcpy(header->ident, ident, 16);
6600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->type = 1;
661864abd7677f434b5aef191e3388e71cd4dd1e6c8machenbach@chromium.org#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
6620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->machine = 3;
66393a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#elif V8_TARGET_ARCH_X64
6640511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    // Processor identification value for x64 is 62 as defined in
6650511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    //    System V ABI, AMD64 Supplement
6660511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    //    http://www.x86-64.org/documentation/abi.pdf
6670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->machine = 62;
66893a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#elif V8_TARGET_ARCH_ARM
66949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    // Set to EM_ARM, defined as 40, in "ARM ELF File Format" at
67049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    // infocenter.arm.com/help/topic/com.arm.doc.dui0101a/DUI0101A_Elf.pdf
67149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    header->machine = 40;
6720511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#else
6730511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#error Unsupported target architecture.
6740511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#endif
6750511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->version = 1;
6760511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->entry = 0;
6770511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->pht_offset = 0;
6780511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->sht_offset = sizeof(ELFHeader);  // Section table follows header.
6790511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->flags = 0;
6800511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->header_size = sizeof(ELFHeader);
6810511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->pht_entry_size = 0;
6820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->pht_entry_num = 0;
6830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->sht_entry_size = sizeof(ELFSection::Header);
6840511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->sht_entry_num = sections_.length();
6850511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->sht_strtab_index = 1;
6860511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
6870511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6880511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void WriteSectionTable(Writer* w) {
6890511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    // Section headers table immediately follows file header.
690e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(w->position() == sizeof(ELFHeader));
6910511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6920511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Writer::Slot<ELFSection::Header> headers =
6930511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        w->CreateSlotsHere<ELFSection::Header>(sections_.length());
6940511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
6950511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    // String table for section table is the first section.
6967bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org    ELFStringTable* strtab = static_cast<ELFStringTable*>(SectionAt(1));
6970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    strtab->AttachWriter(w);
6980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    for (int i = 0, length = sections_.length();
6990511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com         i < length;
7000511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com         i++) {
7010511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      sections_[i]->PopulateHeader(headers.at(i), strtab);
7020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
7030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    strtab->DetachWriter();
7040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
7050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
7060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  int SectionHeaderPosition(uint32_t section_index) {
7070511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return sizeof(ELFHeader) + sizeof(ELFSection::Header) * section_index;
7080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
7090511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
7100511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void WriteSections(Writer* w) {
7110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Writer::Slot<ELFSection::Header> headers =
7120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        w->SlotAt<ELFSection::Header>(sizeof(ELFHeader));
7130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
7140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    for (int i = 0, length = sections_.length();
7150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com         i < length;
7160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com         i++) {
7170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      sections_[i]->WriteBody(headers.at(i), w);
7180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
7190511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
7200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
721c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  Zone* zone_;
7220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  ZoneList<ELFSection*> sections_;
7230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
7240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
7250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
7260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comclass ELFSymbol BASE_EMBEDDED {
7270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
7280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum Type {
7290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_NOTYPE = 0,
7300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_OBJECT = 1,
7310511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_FUNC = 2,
7320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_SECTION = 3,
7330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_FILE = 4,
7340511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_LOPROC = 13,
7350511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    TYPE_HIPROC = 15
7360511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
7370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
7380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum Binding {
7390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    BIND_LOCAL = 0,
7400511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    BIND_GLOBAL = 1,
7410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    BIND_WEAK = 2,
7420511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    BIND_LOPROC = 13,
7430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    BIND_HIPROC = 15
7440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
7450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
7460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  ELFSymbol(const char* name,
7470511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com            uintptr_t value,
7480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com            uintptr_t size,
7490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com            Binding binding,
7500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com            Type type,
7510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com            uint16_t section)
7520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      : name(name),
7530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        value(value),
7540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        size(size),
7550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        info((binding << 4) | type),
7560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        other(0),
7570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        section(section) {
7580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
7590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
7600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  Binding binding() const {
7610511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return static_cast<Binding>(info >> 4);
7620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
76308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#if (V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_ARM || V8_TARGET_ARCH_X87 || \
76408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org     (V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_32_BIT))
7650511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  struct SerializedLayout {
7660511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    SerializedLayout(uint32_t name,
7670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     uintptr_t value,
7680511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     uintptr_t size,
7690511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     Binding binding,
7700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     Type type,
7710511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     uint16_t section)
7720511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        : name(name),
7730511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          value(value),
7740511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          size(size),
7750511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          info((binding << 4) | type),
7760511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          other(0),
7770511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          section(section) {
7780511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
7790511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
7800511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint32_t name;
7810511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t value;
7820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t size;
7830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint8_t info;
7840511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint8_t other;
7850511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t section;
7860511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
78708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#elif V8_TARGET_ARCH_X64 && V8_TARGET_ARCH_64_BIT
7880511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  struct SerializedLayout {
7890511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    SerializedLayout(uint32_t name,
7900511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     uintptr_t value,
7910511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     uintptr_t size,
7920511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     Binding binding,
7930511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     Type type,
7940511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                     uint16_t section)
7950511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        : name(name),
7960511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          info((binding << 4) | type),
7970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          other(0),
7980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          section(section),
7990511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          value(value),
8000511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com          size(size) {
8010511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
8020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint32_t name;
8040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint8_t info;
8050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint8_t other;
8060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint16_t section;
8070511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t value;
8080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t size;
8090511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
8100511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#endif
8110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8127bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org  void Write(Writer::Slot<SerializedLayout> s, ELFStringTable* t) {
8130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    // Convert symbol names from strings to indexes in the string table.
8140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    s->name = t->Add(name);
8150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    s->value = value;
8160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    s->size = size;
8170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    s->info = info;
8180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    s->other = other;
8190511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    s->section = section;
8200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
8210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
8230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  const char* name;
8240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t value;
8250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t size;
8260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uint8_t info;
8270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uint8_t other;
8280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uint16_t section;
8290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
8300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8310511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comclass ELFSymbolTable : public ELFSection {
8330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
83433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  ELFSymbolTable(const char* name, Zone* zone)
8350511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      : ELFSection(name, TYPE_SYMTAB, sizeof(uintptr_t)),
83633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        locals_(1, zone),
83733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org        globals_(1, zone) {
8380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
8390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8400511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  virtual void WriteBody(Writer::Slot<Header> header, Writer* w) {
8410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Align(header->alignment);
8420511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    int total_symbols = locals_.length() + globals_.length() + 1;
8430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->offset = w->position();
8440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Writer::Slot<ELFSymbol::SerializedLayout> symbols =
8460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        w->CreateSlotsHere<ELFSymbol::SerializedLayout>(total_symbols);
8470511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->size = w->position() - header->offset;
8490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    // String table for this symbol table should follow it in the section table.
8517bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org    ELFStringTable* strtab =
8527bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org        static_cast<ELFStringTable*>(w->debug_object()->SectionAt(index() + 1));
8530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    strtab->AttachWriter(w);
8540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    symbols.at(0).set(ELFSymbol::SerializedLayout(0,
8550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                                                  0,
8560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                                                  0,
8570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                                                  ELFSymbol::BIND_LOCAL,
8580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                                                  ELFSymbol::TYPE_NOTYPE,
8590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                                                  0));
8600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    WriteSymbolsList(&locals_, symbols.at(1), strtab);
8610511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    WriteSymbolsList(&globals_, symbols.at(locals_.length() + 1), strtab);
8620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    strtab->DetachWriter();
8630511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
8640511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
86533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  void Add(const ELFSymbol& symbol, Zone* zone) {
8660511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    if (symbol.binding() == ELFSymbol::BIND_LOCAL) {
86733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      locals_.Add(symbol, zone);
8680511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    } else {
86933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      globals_.Add(symbol, zone);
8700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
8710511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
8720511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8730511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com protected:
8740511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  virtual void PopulateHeader(Writer::Slot<Header> header) {
8750511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    ELFSection::PopulateHeader(header);
8760511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    // We are assuming that string table will follow symbol table.
8770511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->link = index() + 1;
8780511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->info = locals_.length() + 1;
8790511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    header->entry_size = sizeof(ELFSymbol::SerializedLayout);
8800511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
8810511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
8830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void WriteSymbolsList(const ZoneList<ELFSymbol>* src,
8840511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                        Writer::Slot<ELFSymbol::SerializedLayout> dst,
8857bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org                        ELFStringTable* strtab) {
8860511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    for (int i = 0, len = src->length();
8870511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com         i < len;
8880511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com         i++) {
8890511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      src->at(i).Write(dst.at(i), strtab);
8900511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
8910511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
8920511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8930511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  ZoneList<ELFSymbol> locals_;
8940511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  ZoneList<ELFSymbol> globals_;
8950511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
8964f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif  // defined(__ELF)
8970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
8980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
89970d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.orgclass LineInfo : public Malloced {
90070d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org public:
90170d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  LineInfo() : pc_info_(10) {}
90270d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
90370d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  void SetPosition(intptr_t pc, int pos, bool is_statement) {
90470d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    AddPCInfo(PCInfo(pc, pos, is_statement));
90570d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  }
90670d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
90770d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  struct PCInfo {
90870d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    PCInfo(intptr_t pc, int pos, bool is_statement)
90970d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org        : pc_(pc), pos_(pos), is_statement_(is_statement) {}
91070d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
91170d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    intptr_t pc_;
91270d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    int pos_;
91370d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    bool is_statement_;
91470d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  };
91570d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
91670d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  List<PCInfo>* pc_info() { return &pc_info_; }
91770d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
91870d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org private:
91970d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  void AddPCInfo(const PCInfo& pc_info) { pc_info_.Add(pc_info); }
92070d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
92170d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  List<PCInfo> pc_info_;
92270d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org};
92370d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
92470d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
9250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comclass CodeDescription BASE_EMBEDDED {
9260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
92793a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#if V8_TARGET_ARCH_X64
92831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  enum StackState {
92931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    POST_RBP_PUSH,
93031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    POST_RBP_SET,
93131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    POST_RBP_POP,
93231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    STACK_STATE_MAX
93331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  };
93431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org#endif
93531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
93670d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  CodeDescription(const char* name, Code* code, Handle<Script> script,
93770d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org                  LineInfo* lineinfo, GDBJITInterface::CodeTag tag,
9384f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                  CompilationInfo* info)
93931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org      : name_(name),
94031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org        code_(code),
94131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org        script_(script),
94231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org        lineinfo_(lineinfo),
9434f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        tag_(tag),
94470d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org        info_(info) {}
9450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
94631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  const char* name() const {
9470511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return name_;
9480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
9490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
95070d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  LineInfo* lineinfo() const { return lineinfo_; }
95131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
95231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  GDBJITInterface::CodeTag tag() const {
95331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    return tag_;
95431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  }
95531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
9564f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  CompilationInfo* info() const {
9574f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    return info_;
9584f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
9594f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
9604f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  bool IsInfoAvailable() const {
9614f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    return info_ != NULL;
9624f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
9634f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
96431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  uintptr_t CodeStart() const {
96531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    return reinterpret_cast<uintptr_t>(code_->instruction_start());
9660511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
9670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
96831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  uintptr_t CodeEnd() const {
96931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    return reinterpret_cast<uintptr_t>(code_->instruction_end());
9700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
9710511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
97231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  uintptr_t CodeSize() const {
97331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    return CodeEnd() - CodeStart();
97431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  }
97531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
97631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  bool IsLineInfoAvailable() {
9770511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return !script_.is_null() &&
9780511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        script_->source()->IsString() &&
9790511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        script_->HasValidSource() &&
9800511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        script_->name()->IsString() &&
9810511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        lineinfo_ != NULL;
9820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
9830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
98493a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#if V8_TARGET_ARCH_X64
98531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  uintptr_t GetStackStateStartAddress(StackState state) const {
986e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(state < STACK_STATE_MAX);
98731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    return stack_state_start_addresses_[state];
98831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  }
9890511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
99031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  void SetStackStateStartAddress(StackState state, uintptr_t addr) {
991e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(state < STACK_STATE_MAX);
99231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    stack_state_start_addresses_[state] = addr;
99331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  }
99431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org#endif
99531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
99683e168294456ca2f02db421a635f7d5f5d023966kmillikin@chromium.org  SmartArrayPointer<char> GetFilename() {
9970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return String::cast(script_->name())->ToCString();
9980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
9990511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
10000511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  int GetScriptLineNumber(int pos) {
10019fa619507474a4c1c21c6935b3209070bc13a218machenbach@chromium.org    return script_->GetLineNumber(pos) + 1;
10020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
10030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
100431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
10050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
10060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  const char* name_;
10070511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  Code* code_;
10080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  Handle<Script> script_;
100970d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  LineInfo* lineinfo_;
101031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  GDBJITInterface::CodeTag tag_;
10114f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  CompilationInfo* info_;
101293a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#if V8_TARGET_ARCH_X64
101331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  uintptr_t stack_state_start_addresses_[STACK_STATE_MAX];
101431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org#endif
10150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
10160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
10174f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#if defined(__ELF)
10180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comstatic void CreateSymbolsTable(CodeDescription* desc,
1019e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org                               Zone* zone,
10200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                               ELF* elf,
10210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                               int text_section_index) {
102233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  ELFSymbolTable* symtab = new(zone) ELFSymbolTable(".symtab", zone);
10237bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org  ELFStringTable* strtab = new(zone) ELFStringTable(".strtab");
10240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
10250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // Symbol table should be followed by the linked string table.
1026c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  elf->AddSection(symtab);
1027c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  elf->AddSection(strtab);
10280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
10290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  symtab->Add(ELFSymbol("V8 Code",
10300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                        0,
10310511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                        0,
10320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                        ELFSymbol::BIND_LOCAL,
10330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                        ELFSymbol::TYPE_FILE,
103433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                        ELFSection::INDEX_ABSOLUTE),
103533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org              zone);
10360511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
103731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  symtab->Add(ELFSymbol(desc->name(),
10380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                        0,
103931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                        desc->CodeSize(),
10400511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                        ELFSymbol::BIND_GLOBAL,
10410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                        ELFSymbol::TYPE_FUNC,
104233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org                        text_section_index),
104333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org              zone);
10440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
10454f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif  // defined(__ELF)
10460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
10470511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
10484f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass DebugInfoSection : public DebugSection {
10490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
10500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  explicit DebugInfoSection(CodeDescription* desc)
10514f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#if defined(__ELF)
10524f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      : ELFSection(".debug_info", TYPE_PROGBITS, 1),
10534f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
10544f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      : MachOSection("__debug_info",
10554f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     "__DWARF",
10564f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     1,
10574f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     MachOSection::S_REGULAR | MachOSection::S_ATTR_DEBUG),
10584f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
10594f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        desc_(desc) { }
10604f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
10614f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  // DWARF2 standard
10624f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  enum DWARF2LocationOp {
10634f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_OP_reg0 = 0x50,
10644f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_OP_reg1 = 0x51,
10654f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_OP_reg2 = 0x52,
10664f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_OP_reg3 = 0x53,
10674f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_OP_reg4 = 0x54,
10684f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_OP_reg5 = 0x55,
10694f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_OP_reg6 = 0x56,
10704f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_OP_reg7 = 0x57,
10714f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_OP_fbreg = 0x91  // 1 param: SLEB128 offset
10724f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  };
10734f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
10744f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  enum DWARF2Encoding {
10754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_ATE_ADDRESS = 0x1,
10764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_ATE_SIGNED = 0x5
10774f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  };
10780511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
107933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  bool WriteBodyInternal(Writer* w) {
10804f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uintptr_t cu_start = w->position();
10810511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Writer::Slot<uint32_t> size = w->CreateSlotHere<uint32_t>();
10820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t start = w->position();
10830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint16_t>(2);  // DWARF version.
10840511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint32_t>(0);  // Abbreviation table offset.
10850511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(sizeof(intptr_t));
10860511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
10870511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(1);  // Abbreviation code.
1088afbdadc5f06365a7889e7c1c1fdb7dbf596cce68machenbach@chromium.org    w->WriteString(desc_->GetFilename().get());
108931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    w->Write<intptr_t>(desc_->CodeStart());
109031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    w->Write<intptr_t>(desc_->CodeStart() + desc_->CodeSize());
10910511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint32_t>(0);
10924f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
10934f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    uint32_t ty_offset = static_cast<uint32_t>(w->position() - cu_start);
10944f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteULEB128(3);
10954f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->Write<uint8_t>(kPointerSize);
10964f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteString("v8value");
10974f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
10984f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    if (desc_->IsInfoAvailable()) {
109933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      Scope* scope = desc_->info()->scope();
11004f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(2);
11014f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteString(desc_->name());
11024f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->Write<intptr_t>(desc_->CodeStart());
11034f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->Write<intptr_t>(desc_->CodeStart() + desc_->CodeSize());
11044f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      Writer::Slot<uint32_t> fb_block_size = w->CreateSlotHere<uint32_t>();
11054f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      uintptr_t fb_block_start = w->position();
1106864abd7677f434b5aef191e3388e71cd4dd1e6c8machenbach@chromium.org#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
11074f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->Write<uint8_t>(DW_OP_reg5);  // The frame pointer's here on ia32
110893a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#elif V8_TARGET_ARCH_X64
11094f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->Write<uint8_t>(DW_OP_reg6);  // and here on x64.
111093a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#elif V8_TARGET_ARCH_ARM
111133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      UNIMPLEMENTED();
111293a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#elif V8_TARGET_ARCH_MIPS
111389e18f5599cb4cd462cb1ed324addd7388fb4d60rossberg@chromium.org      UNIMPLEMENTED();
111412e05e8fde625d746b998a15049e8487c43a3b17machenbach@chromium.org#elif V8_TARGET_ARCH_MIPS64
111512e05e8fde625d746b998a15049e8487c43a3b17machenbach@chromium.org      UNIMPLEMENTED();
11164f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
11174f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#error Unsupported target architecture.
11184f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
11194f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      fb_block_size.set(static_cast<uint32_t>(w->position() - fb_block_start));
11204f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
112133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      int params = scope->num_parameters();
112233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      int slots = scope->num_stack_slots();
112333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      int context_slots = scope->ContextLocalCount();
11244f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      // The real slot ID is internal_slots + context_slot_id.
11254f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      int internal_slots = Context::MIN_CONTEXT_SLOTS;
112633e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      int locals = scope->StackLocalCount();
11274f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      int current_abbreviation = 4;
11284f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
11294f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      for (int param = 0; param < params; ++param) {
11304f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteULEB128(current_abbreviation++);
11314f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteString(
1132afbdadc5f06365a7889e7c1c1fdb7dbf596cce68machenbach@chromium.org            scope->parameter(param)->name()->ToCString(DISALLOW_NULLS).get());
11334f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->Write<uint32_t>(ty_offset);
11344f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
11354f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        uintptr_t block_start = w->position();
11364f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->Write<uint8_t>(DW_OP_fbreg);
11374f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteSLEB128(
11384f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org          JavaScriptFrameConstants::kLastParameterOffset +
11394f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org              kPointerSize * (params - param - 1));
11404f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        block_size.set(static_cast<uint32_t>(w->position() - block_start));
11414f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
11424f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
11434f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      EmbeddedVector<char, 256> buffer;
11444f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      StringBuilder builder(buffer.start(), buffer.length());
11454f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
11464f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      for (int slot = 0; slot < slots; ++slot) {
11474f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteULEB128(current_abbreviation++);
11484f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        builder.Reset();
11494f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        builder.AddFormatted("slot%d", slot);
11504f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteString(builder.Finalize());
11514f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
11524f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
11534f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      // See contexts.h for more information.
1154e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org      DCHECK(Context::MIN_CONTEXT_SLOTS == 4);
1155e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org      DCHECK(Context::CLOSURE_INDEX == 0);
1156e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org      DCHECK(Context::PREVIOUS_INDEX == 1);
1157e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org      DCHECK(Context::EXTENSION_INDEX == 2);
1158e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org      DCHECK(Context::GLOBAL_OBJECT_INDEX == 3);
11594f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(current_abbreviation++);
11604f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteString(".closure");
11614f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(current_abbreviation++);
11624f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteString(".previous");
11634f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(current_abbreviation++);
11644f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteString(".extension");
11654f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(current_abbreviation++);
11664f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteString(".global");
11674f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
11684f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      for (int context_slot = 0;
11694f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org           context_slot < context_slots;
11704f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org           ++context_slot) {
11714f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteULEB128(current_abbreviation++);
11724f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        builder.Reset();
11734f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        builder.AddFormatted("context_slot%d", context_slot + internal_slots);
11744f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteString(builder.Finalize());
11754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
11764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
117733e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      ZoneList<Variable*> stack_locals(locals, scope->zone());
117833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      ZoneList<Variable*> context_locals(context_slots, scope->zone());
117933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      scope->CollectStackAndContextLocals(&stack_locals, &context_locals);
11804f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      for (int local = 0; local < locals; ++local) {
11814f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteULEB128(current_abbreviation++);
11824f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteString(
1183afbdadc5f06365a7889e7c1c1fdb7dbf596cce68machenbach@chromium.org            stack_locals[local]->name()->ToCString(DISALLOW_NULLS).get());
11844f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->Write<uint32_t>(ty_offset);
11854f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
11864f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        uintptr_t block_start = w->position();
11874f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->Write<uint8_t>(DW_OP_fbreg);
11884f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteSLEB128(
11894f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org          JavaScriptFrameConstants::kLocal0Offset -
11904f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org              kPointerSize * local);
11914f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        block_size.set(static_cast<uint32_t>(w->position() - block_start));
11924f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
11934f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
11944f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      {
11954f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteULEB128(current_abbreviation++);
11964f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteString("__function");
11974f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->Write<uint32_t>(ty_offset);
11984f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
11994f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        uintptr_t block_start = w->position();
12004f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->Write<uint8_t>(DW_OP_fbreg);
12014f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteSLEB128(JavaScriptFrameConstants::kFunctionOffset);
12024f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        block_size.set(static_cast<uint32_t>(w->position() - block_start));
12034f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
12044f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
12054f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      {
12064f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteULEB128(current_abbreviation++);
12074f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteString("__context");
12084f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->Write<uint32_t>(ty_offset);
12094f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        Writer::Slot<uint32_t> block_size = w->CreateSlotHere<uint32_t>();
12104f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        uintptr_t block_start = w->position();
12114f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->Write<uint8_t>(DW_OP_fbreg);
12124f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        w->WriteSLEB128(StandardFrameConstants::kContextOffset);
12134f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        block_size.set(static_cast<uint32_t>(w->position() - block_start));
12144f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
1215e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org
1216e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org      w->WriteULEB128(0);  // Terminate the sub program.
12174f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    }
12184f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
1219e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org    w->WriteULEB128(0);  // Terminate the compile unit.
12200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    size.set(static_cast<uint32_t>(w->position() - start));
12210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return true;
12220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
12230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
12240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
12250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  CodeDescription* desc_;
12260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
12270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
12280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
12294f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass DebugAbbrevSection : public DebugSection {
12300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
12314f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  explicit DebugAbbrevSection(CodeDescription* desc)
12324f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#ifdef __ELF
12334f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      : ELFSection(".debug_abbrev", TYPE_PROGBITS, 1),
12344f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
12354f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      : MachOSection("__debug_abbrev",
12364f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     "__DWARF",
12374f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     1,
12384f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     MachOSection::S_REGULAR | MachOSection::S_ATTR_DEBUG),
12394f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
12404f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        desc_(desc) { }
12410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
12420511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // DWARF2 standard, figure 14.
12430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum DWARF2Tags {
12444f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_TAG_FORMAL_PARAMETER = 0x05,
12454f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_TAG_POINTER_TYPE = 0xf,
12464f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_TAG_COMPILE_UNIT = 0x11,
12474f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_TAG_STRUCTURE_TYPE = 0x13,
12484f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_TAG_BASE_TYPE = 0x24,
12494f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_TAG_SUBPROGRAM = 0x2e,
12504f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_TAG_VARIABLE = 0x34
12510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
12520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
12530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // DWARF2 standard, figure 16.
12540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum DWARF2ChildrenDetermination {
12550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_CHILDREN_NO = 0,
12560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_CHILDREN_YES = 1
12570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
12580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
12590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // DWARF standard, figure 17.
12600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum DWARF2Attribute {
12614f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_AT_LOCATION = 0x2,
12620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_AT_NAME = 0x3,
12634f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_AT_BYTE_SIZE = 0xb,
12640511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_AT_STMT_LIST = 0x10,
12650511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_AT_LOW_PC = 0x11,
12664f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_AT_HIGH_PC = 0x12,
12674f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_AT_ENCODING = 0x3e,
12684f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_AT_FRAME_BASE = 0x40,
12694f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_AT_TYPE = 0x49
12700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
12710511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
12720511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // DWARF2 standard, figure 19.
12730511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum DWARF2AttributeForm {
12740511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_FORM_ADDR = 0x1,
12754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_FORM_BLOCK4 = 0x4,
12760511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_FORM_STRING = 0x8,
12774f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_FORM_DATA4 = 0x6,
12784f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_FORM_BLOCK = 0x9,
12794f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_FORM_DATA1 = 0xb,
12804f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_FORM_FLAG = 0xc,
12814f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    DW_FORM_REF4 = 0x13
12820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
12830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
12844f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  void WriteVariableAbbreviation(Writer* w,
12854f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                                 int abbreviation_code,
12864f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                                 bool has_value,
12874f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                                 bool is_parameter) {
12884f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteULEB128(abbreviation_code);
12894f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteULEB128(is_parameter ? DW_TAG_FORMAL_PARAMETER : DW_TAG_VARIABLE);
12904f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->Write<uint8_t>(DW_CHILDREN_NO);
12914f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteULEB128(DW_AT_NAME);
12924f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteULEB128(DW_FORM_STRING);
12934f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    if (has_value) {
12944f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_AT_TYPE);
12954f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_FORM_REF4);
12964f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_AT_LOCATION);
12974f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_FORM_BLOCK4);
12984f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    }
12994f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteULEB128(0);
13004f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteULEB128(0);
13014f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
13024f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
130333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  bool WriteBodyInternal(Writer* w) {
13044f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    int current_abbreviation = 1;
13054f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    bool extra_info = desc_->IsInfoAvailable();
1306e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(desc_->IsLineInfoAvailable());
13074f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteULEB128(current_abbreviation++);
13080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(DW_TAG_COMPILE_UNIT);
13094f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->Write<uint8_t>(extra_info ? DW_CHILDREN_YES : DW_CHILDREN_NO);
13100511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(DW_AT_NAME);
13110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(DW_FORM_STRING);
13120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(DW_AT_LOW_PC);
13130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(DW_FORM_ADDR);
13140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(DW_AT_HIGH_PC);
13150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(DW_FORM_ADDR);
13160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(DW_AT_STMT_LIST);
13170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(DW_FORM_DATA4);
13180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(0);
13190511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(0);
13204f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13214f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    if (extra_info) {
132233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      Scope* scope = desc_->info()->scope();
132333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      int params = scope->num_parameters();
132433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      int slots = scope->num_stack_slots();
132533e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      int context_slots = scope->ContextLocalCount();
13264f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      // The real slot ID is internal_slots + context_slot_id.
13274f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      int internal_slots = Context::MIN_CONTEXT_SLOTS;
132833e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org      int locals = scope->StackLocalCount();
1329e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org      // Total children is params + slots + context_slots + internal_slots +
1330e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org      // locals + 2 (__function and __context).
13314f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13324f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      // The extra duplication below seems to be necessary to keep
13334f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      // gdb from getting upset on OSX.
13344f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(current_abbreviation++);  // Abbreviation code.
13354f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_TAG_SUBPROGRAM);
1336e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org      w->Write<uint8_t>(DW_CHILDREN_YES);
13374f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_AT_NAME);
13384f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_FORM_STRING);
13394f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_AT_LOW_PC);
13404f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_FORM_ADDR);
13414f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_AT_HIGH_PC);
13424f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_FORM_ADDR);
13434f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_AT_FRAME_BASE);
13444f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_FORM_BLOCK4);
13454f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(0);
13464f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(0);
13474f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13484f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(current_abbreviation++);
13494f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_TAG_STRUCTURE_TYPE);
13504f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->Write<uint8_t>(DW_CHILDREN_NO);
13514f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_AT_BYTE_SIZE);
13524f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_FORM_DATA1);
13534f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_AT_NAME);
13544f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(DW_FORM_STRING);
13554f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(0);
13564f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      w->WriteULEB128(0);
13574f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13584f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      for (int param = 0; param < params; ++param) {
13594f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        WriteVariableAbbreviation(w, current_abbreviation++, true, true);
13604f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
13614f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13624f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      for (int slot = 0; slot < slots; ++slot) {
13634f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        WriteVariableAbbreviation(w, current_abbreviation++, false, false);
13644f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
13654f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13664f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      for (int internal_slot = 0;
13674f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org           internal_slot < internal_slots;
13684f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org           ++internal_slot) {
13694f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        WriteVariableAbbreviation(w, current_abbreviation++, false, false);
13704f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
13714f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13724f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      for (int context_slot = 0;
13734f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org           context_slot < context_slots;
13744f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org           ++context_slot) {
13754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        WriteVariableAbbreviation(w, current_abbreviation++, false, false);
13764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
13774f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13784f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      for (int local = 0; local < locals; ++local) {
13794f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org        WriteVariableAbbreviation(w, current_abbreviation++, true, false);
13804f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      }
13814f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13824f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      // The function.
13834f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      WriteVariableAbbreviation(w, current_abbreviation++, true, false);
13844f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13854f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      // The context.
13864f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      WriteVariableAbbreviation(w, current_abbreviation++, true, false);
13874f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
1388e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org      w->WriteULEB128(0);  // Terminate the sibling list.
13894f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    }
13904f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13914f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    w->WriteULEB128(0);  // Terminate the table.
13920511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return true;
13930511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
13944f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
13954f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org private:
13964f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  CodeDescription* desc_;
13970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
13980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
13990511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
14004f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass DebugLineSection : public DebugSection {
14010511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com public:
14020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  explicit DebugLineSection(CodeDescription* desc)
14034f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#ifdef __ELF
14040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      : ELFSection(".debug_line", TYPE_PROGBITS, 1),
14054f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
14064f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      : MachOSection("__debug_line",
14074f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     "__DWARF",
14084f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     1,
14094f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                     MachOSection::S_REGULAR | MachOSection::S_ATTR_DEBUG),
14104f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
14110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        desc_(desc) { }
14120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
14130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // DWARF2 standard, figure 34.
14140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum DWARF2Opcodes {
14150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_LNS_COPY = 1,
14160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_LNS_ADVANCE_PC = 2,
14170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_LNS_ADVANCE_LINE = 3,
14180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_LNS_SET_FILE = 4,
14190511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_LNS_SET_COLUMN = 5,
14200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_LNS_NEGATE_STMT = 6
14210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
14220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
14230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // DWARF2 standard, figure 35.
14240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  enum DWARF2ExtendedOpcode {
14250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_LNE_END_SEQUENCE = 1,
14260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_LNE_SET_ADDRESS = 2,
14270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DW_LNE_DEFINE_FILE = 3
14280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
14290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
143033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  bool WriteBodyInternal(Writer* w) {
14310511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    // Write prologue.
14320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Writer::Slot<uint32_t> total_length = w->CreateSlotHere<uint32_t>();
14330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t start = w->position();
14340511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
143549edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    // Used for special opcodes
143649edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    const int8_t line_base = 1;
143749edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    const uint8_t line_range = 7;
143849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    const int8_t max_line_incr = (line_base + line_range - 1);
143949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    const uint8_t opcode_base = DW_LNS_NEGATE_STMT + 1;
144049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
14410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint16_t>(2);  // Field version.
14420511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Writer::Slot<uint32_t> prologue_length = w->CreateSlotHere<uint32_t>();
14430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uintptr_t prologue_start = w->position();
14440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(1);  // Field minimum_instruction_length.
14450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(1);  // Field default_is_stmt.
144649edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    w->Write<int8_t>(line_base);  // Field line_base.
144749edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    w->Write<uint8_t>(line_range);  // Field line_range.
144849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    w->Write<uint8_t>(opcode_base);  // Field opcode_base.
14490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(0);  // DW_LNS_COPY operands count.
14500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(1);  // DW_LNS_ADVANCE_PC operands count.
14510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(1);  // DW_LNS_ADVANCE_LINE operands count.
14520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(1);  // DW_LNS_SET_FILE operands count.
14530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(1);  // DW_LNS_SET_COLUMN operands count.
14540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(0);  // DW_LNS_NEGATE_STMT operands count.
14550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(0);  // Empty include_directories sequence.
1456afbdadc5f06365a7889e7c1c1fdb7dbf596cce68machenbach@chromium.org    w->WriteString(desc_->GetFilename().get());  // File name.
14570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(0);  // Current directory.
14580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(0);  // Unknown modification time.
14590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(0);  // Unknown file size.
14600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(0);
14610511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    prologue_length.set(static_cast<uint32_t>(w->position() - prologue_start));
14620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
14630511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    WriteExtendedOpcode(w, DW_LNE_SET_ADDRESS, sizeof(intptr_t));
146431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    w->Write<intptr_t>(desc_->CodeStart());
146549edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    w->Write<uint8_t>(DW_LNS_COPY);
14660511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
14670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    intptr_t pc = 0;
14680511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    intptr_t line = 1;
14690511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    bool is_statement = true;
14700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
147170d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    List<LineInfo::PCInfo>* pc_info = desc_->lineinfo()->pc_info();
14720511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    pc_info->Sort(&ComparePCInfo);
147349edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
147449edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    int pc_info_length = pc_info->length();
147549edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    for (int i = 0; i < pc_info_length; i++) {
147670d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      LineInfo::PCInfo* info = &pc_info->at(i);
1477e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org      DCHECK(info->pc_ >= pc);
147849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
147949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // Reduce bloating in the debug line table by removing duplicate line
148049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // entries (per DWARF2 standard).
148149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      intptr_t  new_line = desc_->GetScriptLineNumber(info->pos_);
148249edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      if (new_line == line) {
148349edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org        continue;
14840511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      }
148549edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
148649edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // Mark statement boundaries.  For a better debugging experience, mark
148749edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // the last pc address in the function as a statement (e.g. "}"), so that
148849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // a user can see the result of the last line executed in the function,
148949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // should control reach the end.
149049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      if ((i+1) == pc_info_length) {
149149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org        if (!is_statement) {
149249edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org          w->Write<uint8_t>(DW_LNS_NEGATE_STMT);
149349edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org        }
149449edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      } else if (is_statement != info->is_statement_) {
14950511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        w->Write<uint8_t>(DW_LNS_NEGATE_STMT);
14960511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        is_statement = !is_statement;
14970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      }
149849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
149949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // Generate special opcodes, if possible.  This results in more compact
150049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // debug line tables.  See the DWARF 2.0 standard to learn more about
150149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // special opcodes.
150249edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      uintptr_t pc_diff = info->pc_ - pc;
150349edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      intptr_t line_diff = new_line - line;
150449edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
150549edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // Compute special opcode (see DWARF 2.0 standard)
150649edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      intptr_t special_opcode = (line_diff - line_base) +
150749edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org                                (line_range * pc_diff) + opcode_base;
150849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
150949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // If special_opcode is less than or equal to 255, it can be used as a
151049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // special opcode.  If line_diff is larger than the max line increment
151149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // allowed for a special opcode, or if line_diff is less than the minimum
151249edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // line that can be added to the line register (i.e. line_base), then
151349edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // special_opcode can't be used.
151449edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      if ((special_opcode >= opcode_base) && (special_opcode <= 255) &&
151549edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org          (line_diff <= max_line_incr) && (line_diff >= line_base)) {
151649edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org        w->Write<uint8_t>(special_opcode);
151749edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      } else {
151849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org        w->Write<uint8_t>(DW_LNS_ADVANCE_PC);
151949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org        w->WriteSLEB128(pc_diff);
152049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org        w->Write<uint8_t>(DW_LNS_ADVANCE_LINE);
152149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org        w->WriteSLEB128(line_diff);
15220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        w->Write<uint8_t>(DW_LNS_COPY);
15230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      }
152449edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
152549edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      // Increment the pc and line operands.
152649edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      pc += pc_diff;
152749edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org      line += line_diff;
15280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
152949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    // Advance the pc to the end of the routine, since the end sequence opcode
153049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    // requires this.
153149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    w->Write<uint8_t>(DW_LNS_ADVANCE_PC);
153249edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    w->WriteSLEB128(desc_->CodeSize() - pc);
15330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    WriteExtendedOpcode(w, DW_LNE_END_SEQUENCE, 0);
15340511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    total_length.set(static_cast<uint32_t>(w->position() - start));
15350511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return true;
15360511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
15370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
15380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com private:
15390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void WriteExtendedOpcode(Writer* w,
15400511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                           DWARF2ExtendedOpcode op,
15410511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                           size_t operands_size) {
15420511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(0);
15430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->WriteULEB128(operands_size + 1);
15440511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    w->Write<uint8_t>(op);
15450511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
15460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
154770d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  static int ComparePCInfo(const LineInfo::PCInfo* a,
154870d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org                           const LineInfo::PCInfo* b) {
15490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    if (a->pc_ == b->pc_) {
15500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      if (a->is_statement_ != b->is_statement_) {
15510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com        return b->is_statement_ ? +1 : -1;
15520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      }
15530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      return 0;
15540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    } else if (a->pc_ > b->pc_) {
15550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      return +1;
15560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    } else {
15570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      return -1;
15580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    }
15590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
15600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
15610511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  CodeDescription* desc_;
15620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com};
15630511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
15640511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
156593a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#if V8_TARGET_ARCH_X64
156631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
15674f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgclass UnwindInfoSection : public DebugSection {
156831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org public:
1569f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com  explicit UnwindInfoSection(CodeDescription* desc);
157033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org  virtual bool WriteBodyInternal(Writer* w);
157131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1572f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com  int WriteCIE(Writer* w);
1573f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com  void WriteFDE(Writer* w, int);
157431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1575f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com  void WriteFDEStateOnEntry(Writer* w);
1576f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com  void WriteFDEStateAfterRBPPush(Writer* w);
1577f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com  void WriteFDEStateAfterRBPSet(Writer* w);
1578f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com  void WriteFDEStateAfterRBPPop(Writer* w);
157931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1580f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com  void WriteLength(Writer* w,
158131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                   Writer::Slot<uint32_t>* length_slot,
158231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                   int initial_position);
158331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
158431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org private:
1585f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com  CodeDescription* desc_;
158631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
158731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // DWARF3 Specification, Table 7.23
158831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  enum CFIInstructions {
158931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_ADVANCE_LOC = 0x40,
159031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_OFFSET = 0x80,
159131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_RESTORE = 0xC0,
159231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_NOP = 0x00,
159331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_SET_LOC = 0x01,
159431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_ADVANCE_LOC1 = 0x02,
159531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_ADVANCE_LOC2 = 0x03,
159631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_ADVANCE_LOC4 = 0x04,
159731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_OFFSET_EXTENDED = 0x05,
159831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_RESTORE_EXTENDED = 0x06,
159931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_UNDEFINED = 0x07,
160031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_SAME_VALUE = 0x08,
160131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_REGISTER = 0x09,
160231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_REMEMBER_STATE = 0x0A,
160331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_RESTORE_STATE = 0x0B,
160431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_DEF_CFA = 0x0C,
160531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_DEF_CFA_REGISTER = 0x0D,
160631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_DEF_CFA_OFFSET = 0x0E,
160731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
160831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_DEF_CFA_EXPRESSION = 0x0F,
160931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_EXPRESSION = 0x10,
161031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_OFFSET_EXTENDED_SF = 0x11,
161131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_DEF_CFA_SF = 0x12,
161231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_DEF_CFA_OFFSET_SF = 0x13,
161331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_VAL_OFFSET = 0x14,
161431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_VAL_OFFSET_SF = 0x15,
161531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DW_CFA_VAL_EXPRESSION = 0x16
161631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  };
161731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
161831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // System V ABI, AMD64 Supplement, Version 0.99.5, Figure 3.36
161931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  enum RegisterMapping {
162031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    // Only the relevant ones have been added to reduce clutter.
162131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    AMD64_RBP = 6,
162231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    AMD64_RSP = 7,
162331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    AMD64_RA = 16
162431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  };
162531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
162631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  enum CFIConstants {
162731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    CIE_ID = 0,
162831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    CIE_VERSION = 1,
162931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    CODE_ALIGN_FACTOR = 1,
163031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    DATA_ALIGN_FACTOR = 1,
163131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    RETURN_ADDRESS_REGISTER = AMD64_RA
163231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  };
163331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org};
163431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
163531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1636f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.comvoid UnwindInfoSection::WriteLength(Writer* w,
163731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                                    Writer::Slot<uint32_t>* length_slot,
163831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                                    int initial_position) {
163931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  uint32_t align = (w->position() - initial_position) % kPointerSize;
164031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
164131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  if (align != 0) {
164231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    for (uint32_t i = 0; i < (kPointerSize - align); i++) {
164331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org      w->Write<uint8_t>(DW_CFA_NOP);
164431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    }
164531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  }
164631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1647e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org  DCHECK((w->position() - initial_position) % kPointerSize == 0);
164831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  length_slot->set(w->position() - initial_position);
164931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org}
165031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
165131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1652f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.comUnwindInfoSection::UnwindInfoSection(CodeDescription* desc)
16534f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#ifdef __ELF
16544f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    : ELFSection(".eh_frame", TYPE_X86_64_UNWIND, 1),
16554f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
16564f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    : MachOSection("__eh_frame", "__TEXT", sizeof(uintptr_t),
16574f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                   MachOSection::S_REGULAR),
16584f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
16594f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      desc_(desc) { }
166031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1661f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.comint UnwindInfoSection::WriteCIE(Writer* w) {
166231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  Writer::Slot<uint32_t> cie_length_slot = w->CreateSlotHere<uint32_t>();
166331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  uint32_t cie_position = w->position();
166431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
166531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // Write out the CIE header. Currently no 'common instructions' are
166631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // emitted onto the CIE; every FDE has its own set of instructions.
166731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
166831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint32_t>(CIE_ID);
166931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(CIE_VERSION);
167031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(0);  // Null augmentation string.
167131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteSLEB128(CODE_ALIGN_FACTOR);
167231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteSLEB128(DATA_ALIGN_FACTOR);
167331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(RETURN_ADDRESS_REGISTER);
167431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
167531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  WriteLength(w, &cie_length_slot, cie_position);
167631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
167731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  return cie_position;
167831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org}
167931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
168031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1681f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.comvoid UnwindInfoSection::WriteFDE(Writer* w, int cie_position) {
168231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The only FDE for this function. The CFA is the current RBP.
168331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  Writer::Slot<uint32_t> fde_length_slot = w->CreateSlotHere<uint32_t>();
168431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  int fde_position = w->position();
168531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<int32_t>(fde_position - cie_position + 4);
168631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
168731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uintptr_t>(desc_->CodeStart());
168831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uintptr_t>(desc_->CodeSize());
168931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
169031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  WriteFDEStateOnEntry(w);
169131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  WriteFDEStateAfterRBPPush(w);
169231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  WriteFDEStateAfterRBPSet(w);
169331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  WriteFDEStateAfterRBPPop(w);
169431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
169531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  WriteLength(w, &fde_length_slot, fde_position);
169631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org}
169731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
169831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1699f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.comvoid UnwindInfoSection::WriteFDEStateOnEntry(Writer* w) {
170031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The first state, just after the control has been transferred to the the
170131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // function.
170231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
170331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // RBP for this function will be the value of RSP after pushing the RBP
170431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // for the previous function. The previous RBP has not been pushed yet.
170531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_DEF_CFA_SF);
170631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteULEB128(AMD64_RSP);
170731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteSLEB128(-kPointerSize);
170831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
170931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The RA is stored at location CFA + kCallerPCOffset. This is an invariant,
171031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // and hence omitted from the next states.
171131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
171231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteULEB128(AMD64_RA);
171331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteSLEB128(StandardFrameConstants::kCallerPCOffset);
171431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
171531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The RBP of the previous function is still in RBP.
171631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_SAME_VALUE);
171731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteULEB128(AMD64_RBP);
171831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
171931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // Last location described by this entry.
172031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_SET_LOC);
172131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint64_t>(
172231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org      desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_PUSH));
172331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org}
172431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
172531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1726f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.comvoid UnwindInfoSection::WriteFDEStateAfterRBPPush(Writer* w) {
172731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The second state, just after RBP has been pushed.
172831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
172931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // RBP / CFA for this function is now the current RSP, so just set the
173031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // offset from the previous rule (from -8) to 0.
173131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_DEF_CFA_OFFSET);
173231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteULEB128(0);
173331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
173431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The previous RBP is stored at CFA + kCallerFPOffset. This is an invariant
173531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // in this and the next state, and hence omitted in the next state.
173631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
173731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteULEB128(AMD64_RBP);
173831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteSLEB128(StandardFrameConstants::kCallerFPOffset);
173931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
174031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // Last location described by this entry.
174131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_SET_LOC);
174231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint64_t>(
174331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org      desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_SET));
174431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org}
174531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
174631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1747f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.comvoid UnwindInfoSection::WriteFDEStateAfterRBPSet(Writer* w) {
174831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The third state, after the RBP has been set.
174931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
175031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The CFA can now directly be set to RBP.
175131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_DEF_CFA);
175231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteULEB128(AMD64_RBP);
175331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteULEB128(0);
175431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
175531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // Last location described by this entry.
175631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_SET_LOC);
175731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint64_t>(
175831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org      desc_->GetStackStateStartAddress(CodeDescription::POST_RBP_POP));
175931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org}
176031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
176131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1762f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.comvoid UnwindInfoSection::WriteFDEStateAfterRBPPop(Writer* w) {
176331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The fourth (final) state. The RBP has been popped (just before issuing a
176431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // return).
176531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
176631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The CFA can is now calculated in the same way as in the first state.
176731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_DEF_CFA_SF);
176831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteULEB128(AMD64_RSP);
176931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteSLEB128(-kPointerSize);
177031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
177131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // The RBP
177231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_OFFSET_EXTENDED);
177331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteULEB128(AMD64_RBP);
177431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->WriteSLEB128(StandardFrameConstants::kCallerFPOffset);
177531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
177631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  // Last location described by this entry.
177731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint8_t>(DW_CFA_SET_LOC);
177831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  w->Write<uint64_t>(desc_->CodeEnd());
177931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org}
178031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
178131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
178233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.orgbool UnwindInfoSection::WriteBodyInternal(Writer* w) {
178331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  uint32_t cie_position = WriteCIE(w);
178431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  WriteFDE(w, cie_position);
178531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  return true;
178631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org}
178731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
178831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
178931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org#endif  // V8_TARGET_ARCH_X64
179031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
1791e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.orgstatic void CreateDWARFSections(CodeDescription* desc,
1792e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org                                Zone* zone,
1793e27d617298263725e8a48c2aa14029759b952623mstarzinger@chromium.org                                DebugObject* obj) {
179431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  if (desc->IsLineInfoAvailable()) {
1795c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org    obj->AddSection(new(zone) DebugInfoSection(desc));
1796c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org    obj->AddSection(new(zone) DebugAbbrevSection(desc));
1797c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org    obj->AddSection(new(zone) DebugLineSection(desc));
17980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
179993a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#if V8_TARGET_ARCH_X64
1800c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  obj->AddSection(new(zone) UnwindInfoSection(desc));
180131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org#endif
18020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
18030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com// -------------------------------------------------------------------
18060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com// Binary GDB JIT Interface as described in
18070511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com//   http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html
18080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comextern "C" {
18090511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  typedef enum {
18100511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    JIT_NOACTION = 0,
18110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    JIT_REGISTER_FN,
18120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    JIT_UNREGISTER_FN
18130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  } JITAction;
18140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  struct JITCodeEntry {
18160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    JITCodeEntry* next_;
18170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    JITCodeEntry* prev_;
18180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    Address symfile_addr_;
18190511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint64_t symfile_size_;
18200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
18210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  struct JITDescriptor {
18230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint32_t version_;
18240511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    uint32_t action_flag_;
1825f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com    JITCodeEntry* relevant_entry_;
1826f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.com    JITCodeEntry* first_entry_;
18270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  };
18280511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18290511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // GDB will place breakpoint into this function.
18300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // To prevent GCC from inlining or removing it we place noinline attribute
18310511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // and inline assembler statement inside.
18320511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  void __attribute__((noinline)) __jit_debug_register_code() {
18330511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    __asm__("");
18340511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
18350511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18360511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // GDB will inspect contents of this descriptor.
18370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // Static initialization is necessary to prevent GDB from seeing
18380511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  // uninitialized descriptor.
18390511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  JITDescriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
18404f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
18414f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#ifdef OBJECT_PRINT
1842a86d416fb652b1936026eee315eccd4f17ca1002machenbach@chromium.org  void __gdb_print_v8_object(Object* object) {
1843f15d0cdbef11a212e108432465f014a7d3c3aa12machenbach@chromium.org    OFStream os(stdout);
1844f15d0cdbef11a212e108432465f014a7d3c3aa12machenbach@chromium.org    object->Print(os);
1845f15d0cdbef11a212e108432465f014a7d3c3aa12machenbach@chromium.org    os << flush;
18464f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
18474f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
18480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
18490511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comstatic JITCodeEntry* CreateCodeEntry(Address symfile_addr,
18520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                                     uintptr_t symfile_size) {
18530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  JITCodeEntry* entry = static_cast<JITCodeEntry*>(
18540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      malloc(sizeof(JITCodeEntry) + symfile_size));
18550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  entry->symfile_addr_ = reinterpret_cast<Address>(entry + 1);
18570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  entry->symfile_size_ = symfile_size;
1858d06b9264b1c886fc80a100e9915cf8ae07fdb4e5machenbach@chromium.org  MemCopy(entry->symfile_addr_, symfile_addr, symfile_size);
18590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  entry->prev_ = entry->next_ = NULL;
18610511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  return entry;
18630511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
18640511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18650511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18660511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comstatic void DestroyCodeEntry(JITCodeEntry* entry) {
18670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  free(entry);
18680511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
18690511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18714f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.orgstatic void RegisterCodeEntry(JITCodeEntry* entry,
18724f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                              bool dump_if_enabled,
18734f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                              const char* name_hint) {
1874662436e7b124b3535773535c671c53db322070b5verwaest@chromium.org#if defined(DEBUG) && !V8_OS_WIN
187549edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  static int file_num = 0;
18764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  if (FLAG_gdbjit_dump && dump_if_enabled) {
187749edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    static const int kMaxFileNameSize = 64;
187849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    static const char* kElfFilePrefix = "/tmp/elfdump";
187949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    static const char* kObjFileExt = ".o";
188049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    char file_name[64];
188149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
188270ec1a2160dd946b9578d04d97d631a6d4ab4f8cbmeurer@chromium.org    SNPrintF(Vector<char>(file_name, kMaxFileNameSize),
188370ec1a2160dd946b9578d04d97d631a6d4ab4f8cbmeurer@chromium.org             "%s%s%d%s",
188470ec1a2160dd946b9578d04d97d631a6d4ab4f8cbmeurer@chromium.org             kElfFilePrefix,
188570ec1a2160dd946b9578d04d97d631a6d4ab4f8cbmeurer@chromium.org             (name_hint != NULL) ? name_hint : "",
188670ec1a2160dd946b9578d04d97d631a6d4ab4f8cbmeurer@chromium.org             file_num++,
188770ec1a2160dd946b9578d04d97d631a6d4ab4f8cbmeurer@chromium.org             kObjFileExt);
188849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    WriteBytes(file_name, entry->symfile_addr_, entry->symfile_size_);
188949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  }
189049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org#endif
189149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org
18920511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  entry->next_ = __jit_debug_descriptor.first_entry_;
18930511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  if (entry->next_ != NULL) entry->next_->prev_ = entry;
18940511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  __jit_debug_descriptor.first_entry_ =
18950511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      __jit_debug_descriptor.relevant_entry_ = entry;
18960511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
18970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN;
18980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  __jit_debug_register_code();
18990511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
19000511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19010511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19020511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comstatic void UnregisterCodeEntry(JITCodeEntry* entry) {
19030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  if (entry->prev_ != NULL) {
19040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    entry->prev_->next_ = entry->next_;
19050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  } else {
19060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    __jit_debug_descriptor.first_entry_ = entry->next_;
19070511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
19080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19090511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  if (entry->next_ != NULL) {
19100511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    entry->next_->prev_ = entry->prev_;
19110511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
19120511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19130511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  __jit_debug_descriptor.relevant_entry_ = entry;
19140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN;
19150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  __jit_debug_register_code();
19160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
19170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19191510d58cbcf57c82a10e7d390bfe21a7ae68ba43mstarzinger@chromium.orgstatic JITCodeEntry* CreateELFObject(CodeDescription* desc, Isolate* isolate) {
19204f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#ifdef __MACH_O
1921c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  Zone zone(isolate);
1922c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  MachO mach_o(&zone);
19234f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  Writer w(&mach_o);
19244f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
1925c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  mach_o.AddSection(new(&zone) MachOTextSection(kCodeAlignment,
1926c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org                                                desc->CodeStart(),
1927c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org                                                desc->CodeSize()));
19284f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org
1929c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  CreateDWARFSections(desc, &zone, &mach_o);
19300511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19314f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  mach_o.Write(&w, desc->CodeStart(), desc->CodeSize());
19324f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#else
19331510d58cbcf57c82a10e7d390bfe21a7ae68ba43mstarzinger@chromium.org  Zone zone(isolate);
19341510d58cbcf57c82a10e7d390bfe21a7ae68ba43mstarzinger@chromium.org  ELF elf(&zone);
19350511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  Writer w(&elf);
19360511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19370511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  int text_section_index = elf.AddSection(
19381510d58cbcf57c82a10e7d390bfe21a7ae68ba43mstarzinger@chromium.org      new(&zone) FullHeaderELFSection(
193933e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          ".text",
194033e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          ELFSection::TYPE_NOBITS,
194133e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          kCodeAlignment,
194233e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          desc->CodeStart(),
194333e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          0,
194433e09c8efd078308de3c77a88301566f65c07befverwaest@chromium.org          desc->CodeSize(),
1945c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org          ELFSection::FLAG_ALLOC | ELFSection::FLAG_EXEC));
19460511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19471510d58cbcf57c82a10e7d390bfe21a7ae68ba43mstarzinger@chromium.org  CreateSymbolsTable(desc, &zone, &elf, text_section_index);
19480511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19491510d58cbcf57c82a10e7d390bfe21a7ae68ba43mstarzinger@chromium.org  CreateDWARFSections(desc, &zone, &elf);
19500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  elf.Write(&w);
19524f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org#endif
19530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  return CreateCodeEntry(w.buffer(), w.position());
19550511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
19560511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19570511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comstatic bool SameCodeObjects(void* key1, void* key2) {
19590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  return key1 == key2;
19600511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
19610511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19620511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
196349edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.orgstatic HashMap* GetEntries() {
196449edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  static HashMap* entries = NULL;
196549edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  if (entries == NULL) {
196649edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    entries = new HashMap(&SameCodeObjects);
196749edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  }
196849edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  return entries;
196949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org}
19700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19710511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19720511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comstatic uint32_t HashForCodeObject(Code* code) {
19730511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  static const uintptr_t kGoldenRatio = 2654435761u;
19740511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  uintptr_t hash = reinterpret_cast<uintptr_t>(code->address());
19750511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  return static_cast<uint32_t>((hash >> kCodeAlignmentBits) * kGoldenRatio);
19760511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
19770511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19780511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19790511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comstatic const intptr_t kLineInfoTag = 0x1;
19800511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19810511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comstatic bool IsLineInfoTagged(void* ptr) {
19830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  return 0 != (reinterpret_cast<intptr_t>(ptr) & kLineInfoTag);
19840511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
19850511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19860511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
198770d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.orgstatic void* TagLineInfo(LineInfo* ptr) {
19880511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  return reinterpret_cast<void*>(
19890511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com      reinterpret_cast<intptr_t>(ptr) | kLineInfoTag);
19900511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
19910511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19920511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
199370d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.orgstatic LineInfo* UntagLineInfo(void* ptr) {
199470d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  return reinterpret_cast<LineInfo*>(reinterpret_cast<intptr_t>(ptr) &
199570d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org                                     ~kLineInfoTag);
19960511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
19970511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19980511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
19997bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.orgvoid GDBJITInterface::AddCode(Handle<Name> name,
20000511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                              Handle<Script> script,
20014f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                              Handle<Code> code,
20024f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                              CompilationInfo* info) {
20030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  if (!FLAG_gdbjit) return;
20040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
20059fa619507474a4c1c21c6935b3209070bc13a218machenbach@chromium.org  Script::InitLineEnds(script);
20060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
20077bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org  if (!name.is_null() && name->IsString()) {
20087bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org    SmartArrayPointer<char> name_cstring =
20097bd87f0fe09b5d7c12de8f1db0bdb759dc4130acjkummerow@chromium.org        Handle<String>::cast(name)->ToCString(DISALLOW_NULLS);
2010afbdadc5f06365a7889e7c1c1fdb7dbf596cce68machenbach@chromium.org    AddCode(name_cstring.get(), *code, GDBJITInterface::FUNCTION, *script,
2011afbdadc5f06365a7889e7c1c1fdb7dbf596cce68machenbach@chromium.org            info);
201231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  } else {
20134f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    AddCode("", *code, GDBJITInterface::FUNCTION, *script, info);
201431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  }
201531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org}
201631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
2017e0e1b0d3e70c933d36ed381d511e9fda39f2a751mstarzinger@chromium.org
2018f2038fb01417bcf7698b87a5dfaa4a861539618aerik.corry@gmail.comstatic void AddUnwindInfo(CodeDescription* desc) {
201993a47f4837f2137c8d8349250fd8e91da3108126jkummerow@chromium.org#if V8_TARGET_ARCH_X64
202031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  if (desc->tag() == GDBJITInterface::FUNCTION) {
202131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    // To avoid propagating unwinding information through
20223a5fd78f0ca6c2827bb05f69a373d152a9ce6ff3fschneider@chromium.org    // compilation pipeline we use an approximation.
20233a5fd78f0ca6c2827bb05f69a373d152a9ce6ff3fschneider@chromium.org    // For most use cases this should not affect usability.
202431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    static const int kFramePointerPushOffset = 1;
202531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    static const int kFramePointerSetOffset = 4;
202631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    static const int kFramePointerPopOffset = -3;
202731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
202831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    uintptr_t frame_pointer_push_address =
202931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org        desc->CodeStart() + kFramePointerPushOffset;
203031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
203131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    uintptr_t frame_pointer_set_address =
203231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org        desc->CodeStart() + kFramePointerSetOffset;
203331b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
203431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    uintptr_t frame_pointer_pop_address =
203531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org        desc->CodeEnd() + kFramePointerPopOffset;
203631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org
203731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    desc->SetStackStateStartAddress(CodeDescription::POST_RBP_PUSH,
203831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                                    frame_pointer_push_address);
203931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    desc->SetStackStateStartAddress(CodeDescription::POST_RBP_SET,
204031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                                    frame_pointer_set_address);
204131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    desc->SetStackStateStartAddress(CodeDescription::POST_RBP_POP,
204231b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                                    frame_pointer_pop_address);
20430511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  } else {
204431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    desc->SetStackStateStartAddress(CodeDescription::POST_RBP_PUSH,
204531b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                                    desc->CodeStart());
204631b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    desc->SetStackStateStartAddress(CodeDescription::POST_RBP_SET,
204731b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                                    desc->CodeStart());
204831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org    desc->SetStackStateStartAddress(CodeDescription::POST_RBP_POP,
204931b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                                    desc->CodeEnd());
20500511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
205131b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org#endif  // V8_TARGET_ARCH_X64
20520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
20530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
20540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
20555de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.orgstatic base::LazyMutex mutex = LAZY_MUTEX_INITIALIZER;
205644bc7080a85cc25bff3b9b77cd53f7beffab711bkarlklose@chromium.org
205744bc7080a85cc25bff3b9b77cd53f7beffab711bkarlklose@chromium.org
20580511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comvoid GDBJITInterface::AddCode(const char* name,
20590511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                              Code* code,
206031b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                              GDBJITInterface::CodeTag tag,
20614f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                              Script* script,
20624f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                              CompilationInfo* info) {
20635de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org  base::LockGuard<base::Mutex> lock_guard(mutex.Pointer());
2064837a67edd9afdbfe1b59482b41693f59c48846ffulan@chromium.org  DisallowHeapAllocation no_gc;
20650511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
206649edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  HashMap::Entry* e = GetEntries()->Lookup(code, HashForCodeObject(code), true);
20670511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  if (e->value != NULL && !IsLineInfoTagged(e->value)) return;
20680511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
206970d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  LineInfo* lineinfo = UntagLineInfo(e->value);
20700511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  CodeDescription code_desc(name,
20710511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                            code,
20720511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                            script != NULL ? Handle<Script>(script)
20730511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com                                           : Handle<Script>(),
207431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org                            lineinfo,
20754f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                            tag,
20764f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org                            info);
20770511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
207831b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  if (!FLAG_gdbjit_full && !code_desc.IsLineInfoAvailable()) {
20790511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    delete lineinfo;
208049edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org    GetEntries()->Remove(code, HashForCodeObject(code));
20810511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    return;
20820511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
20830511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
208431b1277ec3b8cd17acb01c66d85a456159072157kmillikin@chromium.org  AddUnwindInfo(&code_desc);
20851510d58cbcf57c82a10e7d390bfe21a7ae68ba43mstarzinger@chromium.org  Isolate* isolate = code->GetIsolate();
20861510d58cbcf57c82a10e7d390bfe21a7ae68ba43mstarzinger@chromium.org  JITCodeEntry* entry = CreateELFObject(&code_desc, isolate);
2087e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org  DCHECK(!IsLineInfoTagged(entry));
20880511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
20890511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  delete lineinfo;
20900511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  e->value = entry;
20910511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
20924f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  const char* name_hint = NULL;
20934f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  bool should_dump = false;
20944f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  if (FLAG_gdbjit_dump) {
20954f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    if (strlen(FLAG_gdbjit_dump_filter) == 0) {
20964f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      name_hint = name;
20974f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      should_dump = true;
20984f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    } else if (name != NULL) {
20994f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      name_hint = strstr(name, FLAG_gdbjit_dump_filter);
21004f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org      should_dump = (name_hint != NULL);
21014f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org    }
21024f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  }
21034f693d6b99ffdbc05e5e211e08ed5039e13279d2ricow@chromium.org  RegisterCodeEntry(entry, should_dump, name_hint);
21040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
21050511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
21060511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
21070511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.comvoid GDBJITInterface::RemoveCode(Code* code) {
21080511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  if (!FLAG_gdbjit) return;
21090511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
21105de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org  base::LockGuard<base::Mutex> lock_guard(mutex.Pointer());
211149edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  HashMap::Entry* e = GetEntries()->Lookup(code,
211249edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org                                           HashForCodeObject(code),
211349edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org                                           false);
21140511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  if (e == NULL) return;
21150511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
21160511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  if (IsLineInfoTagged(e->value)) {
21170511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    delete UntagLineInfo(e->value);
21180511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  } else {
21190511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    JITCodeEntry* entry = static_cast<JITCodeEntry*>(e->value);
21200511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    UnregisterCodeEntry(entry);
21210511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com    DestroyCodeEntry(entry);
21220511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  }
21230511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  e->value = NULL;
212449edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  GetEntries()->Remove(code, HashForCodeObject(code));
21250511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
21260511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
21270511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
2128c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.orgvoid GDBJITInterface::RemoveCodeRange(Address start, Address end) {
2129c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  HashMap* entries = GetEntries();
2130c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  Zone zone(Isolate::Current());
2131c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  ZoneList<Code*> dead_codes(1, &zone);
2132c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org
2133c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  for (HashMap::Entry* e = entries->Start(); e != NULL; e = entries->Next(e)) {
2134c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org    Code* code = reinterpret_cast<Code*>(e->key);
2135c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org    if (code->address() >= start && code->address() < end) {
2136c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org      dead_codes.Add(code, &zone);
2137c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org    }
2138c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  }
2139c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org
2140c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  for (int i = 0; i < dead_codes.length(); i++) {
2141c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org    RemoveCode(dead_codes.at(i));
2142c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org  }
2143c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org}
2144c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org
2145c1789eecd43bf9c5497636592bf14fa754d04c89machenbach@chromium.org
214670d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.orgstatic void RegisterDetailedLineInfo(Code* code, LineInfo* line_info) {
21475de0074a922429f5e0ec2cf140c2d2989bf88140yangguo@chromium.org  base::LockGuard<base::Mutex> lock_guard(mutex.Pointer());
2148e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org  DCHECK(!IsLineInfoTagged(line_info));
214949edbdf52640c88918f8e6638ab4965819eb1dfekmillikin@chromium.org  HashMap::Entry* e = GetEntries()->Lookup(code, HashForCodeObject(code), true);
2150e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org  DCHECK(e->value == NULL);
21510511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com  e->value = TagLineInfo(line_info);
21520511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com}
21530511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
21540511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com
215570d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.orgvoid GDBJITInterface::EventHandler(const v8::JitCodeEvent* event) {
215670d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  if (!FLAG_gdbjit) return;
215770d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  switch (event->type) {
2158fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org    case v8::JitCodeEvent::CODE_ADDED: {
2159fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      Code* code = Code::GetCodeFromTargetAddress(
2160fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org          reinterpret_cast<Address>(event->code_start));
2161fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      if (code->kind() == Code::OPTIMIZED_FUNCTION ||
2162fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org          code->kind() == Code::FUNCTION) {
2163fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org        break;
2164fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      }
2165fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      EmbeddedVector<char, 256> buffer;
2166fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      StringBuilder builder(buffer.start(), buffer.length());
2167fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      builder.AddSubstring(event->name.str, static_cast<int>(event->name.len));
2168fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      AddCode(builder.Finalize(), code, NON_FUNCTION, NULL, NULL);
2169fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      break;
2170fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org    }
217170d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    case v8::JitCodeEvent::CODE_MOVED:
217270d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      break;
2173fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org    case v8::JitCodeEvent::CODE_REMOVED: {
2174fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      Code* code = Code::GetCodeFromTargetAddress(
2175fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org          reinterpret_cast<Address>(event->code_start));
2176fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      RemoveCode(code);
2177fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org      break;
2178fda8f0c912147597a3060a3d80f557840635ca01machenbach@chromium.org    }
217970d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    case v8::JitCodeEvent::CODE_ADD_LINE_POS_INFO: {
218070d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      LineInfo* line_info = reinterpret_cast<LineInfo*>(event->user_data);
218170d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      line_info->SetPosition(static_cast<intptr_t>(event->line_info.offset),
218270d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org                             static_cast<int>(event->line_info.pos),
218370d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org                             event->line_info.position_type ==
218470d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org                                 v8::JitCodeEvent::STATEMENT_POSITION);
218570d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      break;
218670d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    }
218770d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    case v8::JitCodeEvent::CODE_START_LINE_INFO_RECORDING: {
218870d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      v8::JitCodeEvent* mutable_event = const_cast<v8::JitCodeEvent*>(event);
218970d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      mutable_event->user_data = new LineInfo();
219070d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      break;
219170d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    }
219270d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    case v8::JitCodeEvent::CODE_END_LINE_INFO_RECORDING: {
219370d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      LineInfo* line_info = reinterpret_cast<LineInfo*>(event->user_data);
219470d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      Code* code = Code::GetCodeFromTargetAddress(
219570d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org          reinterpret_cast<Address>(event->code_start));
219670d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      RegisterDetailedLineInfo(code, line_info);
219770d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org      break;
219870d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org    }
219970d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org  }
220070d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org}
220170d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
220270d29e1ad7a6f1c163f625252ca32ecc522cb155machenbach@chromium.org
22030511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com} }  // namespace v8::internal
22040511e24c6ebf94594a7e03bdcd58157ac2971d69erik.corry@gmail.com#endif
2205