/thirdparty/breakpad/common/dwarf/cfi_assembler.cc

http://github.com/tomahawk-player/tomahawk · C++ · 198 lines · 124 code · 26 blank · 48 comment · 14 complexity · 95c23c97c3828f31e365d330b48bc46d MD5 · raw file

  1. // Copyright (c) 2010, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
  30. // cfi_assembler.cc: Implementation of google_breakpad::CFISection class.
  31. // See cfi_assembler.h for details.
  32. #include "common/dwarf/cfi_assembler.h"
  33. #include <assert.h>
  34. #include <stdlib.h>
  35. namespace google_breakpad {
  36. using dwarf2reader::DwarfPointerEncoding;
  37. CFISection &CFISection::CIEHeader(u_int64_t code_alignment_factor,
  38. int data_alignment_factor,
  39. unsigned return_address_register,
  40. u_int8_t version,
  41. const string &augmentation,
  42. bool dwarf64) {
  43. assert(!entry_length_);
  44. entry_length_ = new PendingLength();
  45. in_fde_ = false;
  46. if (dwarf64) {
  47. D32(kDwarf64InitialLengthMarker);
  48. D64(entry_length_->length);
  49. entry_length_->start = Here();
  50. D64(eh_frame_ ? kEHFrame64CIEIdentifier : kDwarf64CIEIdentifier);
  51. } else {
  52. D32(entry_length_->length);
  53. entry_length_->start = Here();
  54. D32(eh_frame_ ? kEHFrame32CIEIdentifier : kDwarf32CIEIdentifier);
  55. }
  56. D8(version);
  57. AppendCString(augmentation);
  58. ULEB128(code_alignment_factor);
  59. LEB128(data_alignment_factor);
  60. if (version == 1)
  61. D8(return_address_register);
  62. else
  63. ULEB128(return_address_register);
  64. return *this;
  65. }
  66. CFISection &CFISection::FDEHeader(Label cie_pointer,
  67. u_int64_t initial_location,
  68. u_int64_t address_range,
  69. bool dwarf64) {
  70. assert(!entry_length_);
  71. entry_length_ = new PendingLength();
  72. in_fde_ = true;
  73. fde_start_address_ = initial_location;
  74. if (dwarf64) {
  75. D32(0xffffffff);
  76. D64(entry_length_->length);
  77. entry_length_->start = Here();
  78. if (eh_frame_)
  79. D64(Here() - cie_pointer);
  80. else
  81. D64(cie_pointer);
  82. } else {
  83. D32(entry_length_->length);
  84. entry_length_->start = Here();
  85. if (eh_frame_)
  86. D32(Here() - cie_pointer);
  87. else
  88. D32(cie_pointer);
  89. }
  90. EncodedPointer(initial_location);
  91. // The FDE length in an .eh_frame section uses the same encoding as the
  92. // initial location, but ignores the base address (selected by the upper
  93. // nybble of the encoding), as it's a length, not an address that can be
  94. // made relative.
  95. EncodedPointer(address_range,
  96. DwarfPointerEncoding(pointer_encoding_ & 0x0f));
  97. return *this;
  98. }
  99. CFISection &CFISection::FinishEntry() {
  100. assert(entry_length_);
  101. Align(address_size_, dwarf2reader::DW_CFA_nop);
  102. entry_length_->length = Here() - entry_length_->start;
  103. delete entry_length_;
  104. entry_length_ = NULL;
  105. in_fde_ = false;
  106. return *this;
  107. }
  108. CFISection &CFISection::EncodedPointer(u_int64_t address,
  109. DwarfPointerEncoding encoding,
  110. const EncodedPointerBases &bases) {
  111. // Omitted data is extremely easy to emit.
  112. if (encoding == dwarf2reader::DW_EH_PE_omit)
  113. return *this;
  114. // If (encoding & dwarf2reader::DW_EH_PE_indirect) != 0, then we assume
  115. // that ADDRESS is the address at which the pointer is stored --- in
  116. // other words, that bit has no effect on how we write the pointer.
  117. encoding = DwarfPointerEncoding(encoding & ~dwarf2reader::DW_EH_PE_indirect);
  118. // Find the base address to which this pointer is relative. The upper
  119. // nybble of the encoding specifies this.
  120. u_int64_t base;
  121. switch (encoding & 0xf0) {
  122. case dwarf2reader::DW_EH_PE_absptr: base = 0; break;
  123. case dwarf2reader::DW_EH_PE_pcrel: base = bases.cfi + Size(); break;
  124. case dwarf2reader::DW_EH_PE_textrel: base = bases.text; break;
  125. case dwarf2reader::DW_EH_PE_datarel: base = bases.data; break;
  126. case dwarf2reader::DW_EH_PE_funcrel: base = fde_start_address_; break;
  127. case dwarf2reader::DW_EH_PE_aligned: base = 0; break;
  128. default: abort();
  129. };
  130. // Make ADDRESS relative. Yes, this is appropriate even for "absptr"
  131. // values; see gcc/unwind-pe.h.
  132. address -= base;
  133. // Align the pointer, if required.
  134. if ((encoding & 0xf0) == dwarf2reader::DW_EH_PE_aligned)
  135. Align(AddressSize());
  136. // Append ADDRESS to this section in the appropriate form. For the
  137. // fixed-width forms, we don't need to differentiate between signed and
  138. // unsigned encodings, because ADDRESS has already been extended to 64
  139. // bits before it was passed to us.
  140. switch (encoding & 0x0f) {
  141. case dwarf2reader::DW_EH_PE_absptr:
  142. Address(address);
  143. break;
  144. case dwarf2reader::DW_EH_PE_uleb128:
  145. ULEB128(address);
  146. break;
  147. case dwarf2reader::DW_EH_PE_sleb128:
  148. LEB128(address);
  149. break;
  150. case dwarf2reader::DW_EH_PE_udata2:
  151. case dwarf2reader::DW_EH_PE_sdata2:
  152. D16(address);
  153. break;
  154. case dwarf2reader::DW_EH_PE_udata4:
  155. case dwarf2reader::DW_EH_PE_sdata4:
  156. D32(address);
  157. break;
  158. case dwarf2reader::DW_EH_PE_udata8:
  159. case dwarf2reader::DW_EH_PE_sdata8:
  160. D64(address);
  161. break;
  162. default:
  163. abort();
  164. }
  165. return *this;
  166. };
  167. const u_int32_t CFISection::kDwarf64InitialLengthMarker;
  168. const u_int32_t CFISection::kDwarf32CIEIdentifier;
  169. const u_int64_t CFISection::kDwarf64CIEIdentifier;
  170. const u_int32_t CFISection::kEHFrame32CIEIdentifier;
  171. const u_int64_t CFISection::kEHFrame64CIEIdentifier;
  172. } // namespace google_breakpad