michael@0: // Copyright (c) 2010, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // Original author: Jim Blandy michael@0: michael@0: // cfi_assembler.cc: Implementation of google_breakpad::CFISection class. michael@0: // See cfi_assembler.h for details. michael@0: michael@0: #include "common/dwarf/cfi_assembler.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: namespace google_breakpad { michael@0: michael@0: using dwarf2reader::DwarfPointerEncoding; michael@0: michael@0: CFISection &CFISection::CIEHeader(uint64_t code_alignment_factor, michael@0: int data_alignment_factor, michael@0: unsigned return_address_register, michael@0: uint8_t version, michael@0: const string &augmentation, michael@0: bool dwarf64) { michael@0: assert(!entry_length_); michael@0: entry_length_ = new PendingLength(); michael@0: in_fde_ = false; michael@0: michael@0: if (dwarf64) { michael@0: D32(kDwarf64InitialLengthMarker); michael@0: D64(entry_length_->length); michael@0: entry_length_->start = Here(); michael@0: D64(eh_frame_ ? kEHFrame64CIEIdentifier : kDwarf64CIEIdentifier); michael@0: } else { michael@0: D32(entry_length_->length); michael@0: entry_length_->start = Here(); michael@0: D32(eh_frame_ ? kEHFrame32CIEIdentifier : kDwarf32CIEIdentifier); michael@0: } michael@0: D8(version); michael@0: AppendCString(augmentation); michael@0: ULEB128(code_alignment_factor); michael@0: LEB128(data_alignment_factor); michael@0: if (version == 1) michael@0: D8(return_address_register); michael@0: else michael@0: ULEB128(return_address_register); michael@0: return *this; michael@0: } michael@0: michael@0: CFISection &CFISection::FDEHeader(Label cie_pointer, michael@0: uint64_t initial_location, michael@0: uint64_t address_range, michael@0: bool dwarf64) { michael@0: assert(!entry_length_); michael@0: entry_length_ = new PendingLength(); michael@0: in_fde_ = true; michael@0: fde_start_address_ = initial_location; michael@0: michael@0: if (dwarf64) { michael@0: D32(0xffffffff); michael@0: D64(entry_length_->length); michael@0: entry_length_->start = Here(); michael@0: if (eh_frame_) michael@0: D64(Here() - cie_pointer); michael@0: else michael@0: D64(cie_pointer); michael@0: } else { michael@0: D32(entry_length_->length); michael@0: entry_length_->start = Here(); michael@0: if (eh_frame_) michael@0: D32(Here() - cie_pointer); michael@0: else michael@0: D32(cie_pointer); michael@0: } michael@0: EncodedPointer(initial_location); michael@0: // The FDE length in an .eh_frame section uses the same encoding as the michael@0: // initial location, but ignores the base address (selected by the upper michael@0: // nybble of the encoding), as it's a length, not an address that can be michael@0: // made relative. michael@0: EncodedPointer(address_range, michael@0: DwarfPointerEncoding(pointer_encoding_ & 0x0f)); michael@0: return *this; michael@0: } michael@0: michael@0: CFISection &CFISection::FinishEntry() { michael@0: assert(entry_length_); michael@0: Align(address_size_, dwarf2reader::DW_CFA_nop); michael@0: entry_length_->length = Here() - entry_length_->start; michael@0: delete entry_length_; michael@0: entry_length_ = NULL; michael@0: in_fde_ = false; michael@0: return *this; michael@0: } michael@0: michael@0: CFISection &CFISection::EncodedPointer(uint64_t address, michael@0: DwarfPointerEncoding encoding, michael@0: const EncodedPointerBases &bases) { michael@0: // Omitted data is extremely easy to emit. michael@0: if (encoding == dwarf2reader::DW_EH_PE_omit) michael@0: return *this; michael@0: michael@0: // If (encoding & dwarf2reader::DW_EH_PE_indirect) != 0, then we assume michael@0: // that ADDRESS is the address at which the pointer is stored --- in michael@0: // other words, that bit has no effect on how we write the pointer. michael@0: encoding = DwarfPointerEncoding(encoding & ~dwarf2reader::DW_EH_PE_indirect); michael@0: michael@0: // Find the base address to which this pointer is relative. The upper michael@0: // nybble of the encoding specifies this. michael@0: uint64_t base; michael@0: switch (encoding & 0xf0) { michael@0: case dwarf2reader::DW_EH_PE_absptr: base = 0; break; michael@0: case dwarf2reader::DW_EH_PE_pcrel: base = bases.cfi + Size(); break; michael@0: case dwarf2reader::DW_EH_PE_textrel: base = bases.text; break; michael@0: case dwarf2reader::DW_EH_PE_datarel: base = bases.data; break; michael@0: case dwarf2reader::DW_EH_PE_funcrel: base = fde_start_address_; break; michael@0: case dwarf2reader::DW_EH_PE_aligned: base = 0; break; michael@0: default: abort(); michael@0: }; michael@0: michael@0: // Make ADDRESS relative. Yes, this is appropriate even for "absptr" michael@0: // values; see gcc/unwind-pe.h. michael@0: address -= base; michael@0: michael@0: // Align the pointer, if required. michael@0: if ((encoding & 0xf0) == dwarf2reader::DW_EH_PE_aligned) michael@0: Align(AddressSize()); michael@0: michael@0: // Append ADDRESS to this section in the appropriate form. For the michael@0: // fixed-width forms, we don't need to differentiate between signed and michael@0: // unsigned encodings, because ADDRESS has already been extended to 64 michael@0: // bits before it was passed to us. michael@0: switch (encoding & 0x0f) { michael@0: case dwarf2reader::DW_EH_PE_absptr: michael@0: Address(address); michael@0: break; michael@0: michael@0: case dwarf2reader::DW_EH_PE_uleb128: michael@0: ULEB128(address); michael@0: break; michael@0: michael@0: case dwarf2reader::DW_EH_PE_sleb128: michael@0: LEB128(address); michael@0: break; michael@0: michael@0: case dwarf2reader::DW_EH_PE_udata2: michael@0: case dwarf2reader::DW_EH_PE_sdata2: michael@0: D16(address); michael@0: break; michael@0: michael@0: case dwarf2reader::DW_EH_PE_udata4: michael@0: case dwarf2reader::DW_EH_PE_sdata4: michael@0: D32(address); michael@0: break; michael@0: michael@0: case dwarf2reader::DW_EH_PE_udata8: michael@0: case dwarf2reader::DW_EH_PE_sdata8: michael@0: D64(address); michael@0: break; michael@0: michael@0: default: michael@0: abort(); michael@0: } michael@0: michael@0: return *this; michael@0: }; michael@0: michael@0: const uint32_t CFISection::kDwarf64InitialLengthMarker; michael@0: const uint32_t CFISection::kDwarf32CIEIdentifier; michael@0: const uint64_t CFISection::kDwarf64CIEIdentifier; michael@0: const uint32_t CFISection::kEHFrame32CIEIdentifier; michael@0: const uint64_t CFISection::kEHFrame64CIEIdentifier; michael@0: michael@0: } // namespace google_breakpad