148f512ceSopenharmony_ci/* 248f512ceSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 348f512ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 448f512ceSopenharmony_ci * you may not use this file except in compliance with the License. 548f512ceSopenharmony_ci * You may obtain a copy of the License at 648f512ceSopenharmony_ci * 748f512ceSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 848f512ceSopenharmony_ci * 948f512ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1048f512ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1148f512ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1248f512ceSopenharmony_ci * See the License for the specific language governing permissions and 1348f512ceSopenharmony_ci * limitations under the License. 1448f512ceSopenharmony_ci */ 1548f512ceSopenharmony_ci#ifndef DWARF_ENCODING_H 1648f512ceSopenharmony_ci#define DWARF_ENCODING_H 1748f512ceSopenharmony_ci 1848f512ceSopenharmony_ci#include "utilities.h" 1948f512ceSopenharmony_ci 2048f512ceSopenharmony_ci// now we only support 64 bit. 2148f512ceSopenharmony_ciusing uleb128_t = uint64_t; 2248f512ceSopenharmony_ciusing sleb128_t = int64_t; 2348f512ceSopenharmony_ci 2448f512ceSopenharmony_cinamespace OHOS { 2548f512ceSopenharmony_cinamespace Developtools { 2648f512ceSopenharmony_cinamespace HiPerf { 2748f512ceSopenharmony_ciconstexpr const int LEB_BYTE_EFFECTIVE_LENGTH = 7; 2848f512ceSopenharmony_ciconstexpr const int SIGN_BIT_OF_BYTE = 0x40; 2948f512ceSopenharmony_ciconstexpr const int MAX_VALUE_OF_BYTE = 0x7f; 3048f512ceSopenharmony_ciconstexpr const int MORE_BIT_OF_BYTE = 0x80; 3148f512ceSopenharmony_ci 3248f512ceSopenharmony_ci/* 3348f512ceSopenharmony_ci10.5.1. DWARF Exception Header Encoding 3448f512ceSopenharmony_ciThe DWARF Exception Header Encoding is used to describe the type of data used in the .eh_frame and 3548f512ceSopenharmony_ci.eh_frame_hdr section. The upper 4 bits indicate how the value is to be applied. The lower 4 bits 3648f512ceSopenharmony_ciindicate the format of the data. 3748f512ceSopenharmony_ciusing dw_encode_t = unsigned char; // 4 bits + 4 bits 3848f512ceSopenharmony_ci*/ 3948f512ceSopenharmony_ci 4048f512ceSopenharmony_ciusing dw_encode_t = unsigned char; // 4 bits + 4 bits 4148f512ceSopenharmony_ci 4248f512ceSopenharmony_ci// Table 10-5. DWARF Exception Header value format 4348f512ceSopenharmony_ci 4448f512ceSopenharmony_cienum DW_EH_PE_VF { 4548f512ceSopenharmony_ci DW_EH_PE_absptr = 0x00, // a literal pointer whose size is determined by the architecture. 4648f512ceSopenharmony_ci DW_EH_PE_uleb128 = 0x01, // Unsigned value is encoded using the Little Endian Base 128 (LEB128) 4748f512ceSopenharmony_ci DW_EH_PE_udata2 = 0x02, // A 2 bytes unsigned value. 4848f512ceSopenharmony_ci DW_EH_PE_udata4 = 0x03, // A 4 bytes unsigned value. 4948f512ceSopenharmony_ci DW_EH_PE_udata8 = 0x04, // An 8 bytes unsigned value. 5048f512ceSopenharmony_ci DW_EH_PE_sleb128 = 0x09, // Signed value is encoded using the Little Endian Base 128(LEB128) 5148f512ceSopenharmony_ci DW_EH_PE_sdata2 = 0x0A, // A 2 bytes signed value. 5248f512ceSopenharmony_ci DW_EH_PE_sdata4 = 0x0B, // A 4 bytes signed value. 5348f512ceSopenharmony_ci DW_EH_PE_sdata8 = 0x0C, // An 8 bytes signed value. 5448f512ceSopenharmony_ci}; 5548f512ceSopenharmony_ci 5648f512ceSopenharmony_ci// Table 10-6. DWARF Exception Header application 5748f512ceSopenharmony_cienum DW_EH_PE_A { 5848f512ceSopenharmony_ci DW_EH_PE_nothing = 0x00, // nothing to do 5948f512ceSopenharmony_ci DW_EH_PE_pcrel = 0x10, // relative to the current program counter. 6048f512ceSopenharmony_ci DW_EH_PE_textrel = 0x20, // relative to the beginning of the .text section. 6148f512ceSopenharmony_ci DW_EH_PE_datarel = 0x30, // relative to the beginning of the .got or .eh_frame_hdr section. 6248f512ceSopenharmony_ci DW_EH_PE_funcrel = 0x40, // relative to the beginning of the function. 6348f512ceSopenharmony_ci DW_EH_PE_aligned = 0x50, // aligned to an address unit sized boundary. 6448f512ceSopenharmony_ci DW_EH_PE_omit = 0xff, // indicate that no value is present. 6548f512ceSopenharmony_ci}; 6648f512ceSopenharmony_ci 6748f512ceSopenharmony_ciconst std::map<dw_encode_t, size_t> DWFormatSizeMap = { 6848f512ceSopenharmony_ci#ifdef ARM 6948f512ceSopenharmony_ci {DW_EH_PE_absptr, 4}, 7048f512ceSopenharmony_ci#else 7148f512ceSopenharmony_ci {DW_EH_PE_absptr, 8}, 7248f512ceSopenharmony_ci#endif 7348f512ceSopenharmony_ci#ifdef NOT_USE 7448f512ceSopenharmony_ci {DW_EH_PE_uleb128, sizeof(char) * 128}, 7548f512ceSopenharmony_ci#endif 7648f512ceSopenharmony_ci {DW_EH_PE_udata2, sizeof(char) * 2}, 7748f512ceSopenharmony_ci {DW_EH_PE_udata4, sizeof(char) * 4}, 7848f512ceSopenharmony_ci {DW_EH_PE_udata8, sizeof(char) * 8}, 7948f512ceSopenharmony_ci#ifdef NOT_USE 8048f512ceSopenharmony_ci {DW_EH_PE_sleb128, sizeof(char) * 128}, 8148f512ceSopenharmony_ci#endif 8248f512ceSopenharmony_ci {DW_EH_PE_sdata2, sizeof(char) * 2}, 8348f512ceSopenharmony_ci {DW_EH_PE_sdata4, sizeof(char) * 4}, 8448f512ceSopenharmony_ci {DW_EH_PE_sdata8, sizeof(char) * 8}, 8548f512ceSopenharmony_ci}; 8648f512ceSopenharmony_ci 8748f512ceSopenharmony_citemplate<class T> 8848f512ceSopenharmony_ciuint64_t dwReadAnyTypeData(const unsigned char *&buffer, T) 8948f512ceSopenharmony_ci{ 9048f512ceSopenharmony_ci T value; 9148f512ceSopenharmony_ci if (memcpy_s(&value, sizeof(T), buffer, sizeof(T)) != 0) { 9248f512ceSopenharmony_ci return 0; 9348f512ceSopenharmony_ci } 9448f512ceSopenharmony_ci buffer += sizeof(T); 9548f512ceSopenharmony_ci return static_cast<uint64_t>(value); 9648f512ceSopenharmony_ci} 9748f512ceSopenharmony_ci 9848f512ceSopenharmony_ciclass DwarfEncoding { 9948f512ceSopenharmony_cipublic: 10048f512ceSopenharmony_ci DwarfEncoding(dw_encode_t dw, const unsigned char *&data, uint64_t vaddrBase = 0, 10148f512ceSopenharmony_ci uint64_t vaddrPC = 0, uint64_t vaddrText = 0); 10248f512ceSopenharmony_ci 10348f512ceSopenharmony_ci const std::string ToString() const; 10448f512ceSopenharmony_ci 10548f512ceSopenharmony_ci const unsigned char *GetEnd() const; 10648f512ceSopenharmony_ci 10748f512ceSopenharmony_ci const unsigned char *GetData() const; 10848f512ceSopenharmony_ci 10948f512ceSopenharmony_ci size_t GetSize() const; 11048f512ceSopenharmony_ci 11148f512ceSopenharmony_ci uint64_t GetValue() const; 11248f512ceSopenharmony_ci 11348f512ceSopenharmony_ci uint64_t GetAppliedValue() const; 11448f512ceSopenharmony_ci 11548f512ceSopenharmony_ci bool IsOmit() const; 11648f512ceSopenharmony_ci 11748f512ceSopenharmony_ciprivate: 11848f512ceSopenharmony_ci dw_encode_t dw_; 11948f512ceSopenharmony_ci const unsigned char *data_; 12048f512ceSopenharmony_ci uint64_t vaddrBase_ = 0; 12148f512ceSopenharmony_ci uint64_t vaddrPC_ = 0; 12248f512ceSopenharmony_ci uint64_t vaddrText_ = 0; 12348f512ceSopenharmony_ci uint64_t value_[2] = {0, 0}; 12448f512ceSopenharmony_ci 12548f512ceSopenharmony_ci dw_encode_t Format() const; 12648f512ceSopenharmony_ci 12748f512ceSopenharmony_ci dw_encode_t Application() const; 12848f512ceSopenharmony_ci 12948f512ceSopenharmony_ci uint64_t ReadValue(const unsigned char *&data) const; 13048f512ceSopenharmony_ci 13148f512ceSopenharmony_ci const std::string FormatName() const; 13248f512ceSopenharmony_ci 13348f512ceSopenharmony_ci const std::string ApplicationName() const; 13448f512ceSopenharmony_ci}; 13548f512ceSopenharmony_ci 13648f512ceSopenharmony_ci/* 13748f512ceSopenharmony_ciLinux Standard Base Core Specification 4.1 13848f512ceSopenharmony_ciChapter 10. Object Format 13948f512ceSopenharmony_ci10.6.2. The .eh_frame_hdr section 14048f512ceSopenharmony_ci 14148f512ceSopenharmony_ciTable 10-11. .eh_frame_hdr Section Format 14248f512ceSopenharmony_ci 14348f512ceSopenharmony_ciEncoding Field 14448f512ceSopenharmony_ciunsigned byte version 14548f512ceSopenharmony_ciunsigned byte eh_frame_ptr_enc 14648f512ceSopenharmony_ciunsigned byte fde_count_enc 14748f512ceSopenharmony_ciunsigned byte table_enc 14848f512ceSopenharmony_ciencoded eh_frame_ptr 14948f512ceSopenharmony_ciencoded fde_count 15048f512ceSopenharmony_ci binary search table 15148f512ceSopenharmony_ci*/ 15248f512ceSopenharmony_ci 15348f512ceSopenharmony_cistruct eh_frame_hdr { 15448f512ceSopenharmony_ci // Version of the .eh_frame_hdr format. This value shall be 1. 15548f512ceSopenharmony_ci dw_encode_t version; 15648f512ceSopenharmony_ci 15748f512ceSopenharmony_ci // The encoding format of the eh_frame_ptr field. 15848f512ceSopenharmony_ci dw_encode_t eh_frame_ptr_enc; 15948f512ceSopenharmony_ci 16048f512ceSopenharmony_ci // The encoding format of the fde_count field. A value of DW_EH_PE_omit indicates the binary 16148f512ceSopenharmony_ci // search table is not present. 16248f512ceSopenharmony_ci dw_encode_t fde_count_enc; 16348f512ceSopenharmony_ci 16448f512ceSopenharmony_ci // The encoding format of the entries in the binary search table. A value of DW_EH_PE_omit 16548f512ceSopenharmony_ci // indicates the binary search table is not present. 16648f512ceSopenharmony_ci dw_encode_t table_enc; 16748f512ceSopenharmony_ci 16848f512ceSopenharmony_ci // The encoded value of the pointer to the start of the .eh_frame section. 16948f512ceSopenharmony_ci /* 17048f512ceSopenharmony_ci dw_encode_t eh_frame_ptr 17148f512ceSopenharmony_ci dw_encode_t fde_count 17248f512ceSopenharmony_ci */ 17348f512ceSopenharmony_ci // A binary search table containing fde_count entries. Each entry of the table consist of two 17448f512ceSopenharmony_ci // encoded values, the initial location, and the address. The entries are sorted in an 17548f512ceSopenharmony_ci // increasing order by the initial location value. 17648f512ceSopenharmony_ci 17748f512ceSopenharmony_ci unsigned char encode_data[0]; 17848f512ceSopenharmony_ci} PACKED; 17948f512ceSopenharmony_ci} // namespace HiPerf 18048f512ceSopenharmony_ci} // namespace Developtools 18148f512ceSopenharmony_ci} // namespace OHOS 18248f512ceSopenharmony_ci#endif // DWARF_ENCODING_H 183