106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#ifndef DWARF_ENCODING_H
1606f6ba60Sopenharmony_ci#define DWARF_ENCODING_H
1706f6ba60Sopenharmony_ci
1806f6ba60Sopenharmony_ci#include "utilities.h"
1906f6ba60Sopenharmony_ci
2006f6ba60Sopenharmony_ci// now we only support 64 bit.
2106f6ba60Sopenharmony_ciusing uleb128_t = uint64_t;
2206f6ba60Sopenharmony_ciusing sleb128_t = int64_t;
2306f6ba60Sopenharmony_ci
2406f6ba60Sopenharmony_cinamespace OHOS {
2506f6ba60Sopenharmony_cinamespace Developtools {
2606f6ba60Sopenharmony_cinamespace NativeDaemon {
2706f6ba60Sopenharmony_cistatic constexpr const int LEB_BYTE_EFFECTIVE_LENGTH = 7;
2806f6ba60Sopenharmony_cistatic constexpr const int SIGN_BIT_OF_BYTE = 0x40;
2906f6ba60Sopenharmony_cistatic constexpr const int MAX_VALUE_OF_BYTE = 0x7f;
3006f6ba60Sopenharmony_cistatic constexpr const int MORE_BIT_OF_BYTE = 0x80;
3106f6ba60Sopenharmony_ci
3206f6ba60Sopenharmony_ci/*
3306f6ba60Sopenharmony_ci10.5.1. DWARF Exception Header Encoding
3406f6ba60Sopenharmony_ciThe DWARF Exception Header Encoding is used to describe the type of data used in the .eh_frame and
3506f6ba60Sopenharmony_ci.eh_frame_hdr section. The upper 4 bits indicate how the value is to be applied. The lower 4 bits
3606f6ba60Sopenharmony_ciindicate the format of the data.
3706f6ba60Sopenharmony_ciusing dw_encode_t = unsigned char; // 4 bits + 4 bits
3806f6ba60Sopenharmony_ci*/
3906f6ba60Sopenharmony_ci
4006f6ba60Sopenharmony_ciusing dw_encode_t = unsigned char; // 4 bits + 4 bits
4106f6ba60Sopenharmony_ci
4206f6ba60Sopenharmony_ci// Table 10-5. DWARF Exception Header value format
4306f6ba60Sopenharmony_ci
4406f6ba60Sopenharmony_cienum DW_EH_PE_VF {
4506f6ba60Sopenharmony_ci    DW_EH_PE_absptr = 0x00,  // a literal pointer whose size is determined by the architecture.
4606f6ba60Sopenharmony_ci    DW_EH_PE_uleb128 = 0x01, // Unsigned value is encoded using the Little Endian Base 128 (LEB128)
4706f6ba60Sopenharmony_ci    DW_EH_PE_udata2 = 0x02,  // A 2 bytes unsigned value.
4806f6ba60Sopenharmony_ci    DW_EH_PE_udata4 = 0x03,  // A 4 bytes unsigned value.
4906f6ba60Sopenharmony_ci    DW_EH_PE_udata8 = 0x04,  // An 8 bytes unsigned value.
5006f6ba60Sopenharmony_ci    DW_EH_PE_sleb128 = 0x09, // Signed value is encoded using the Little Endian Base 128(LEB128)
5106f6ba60Sopenharmony_ci    DW_EH_PE_sdata2 = 0x0A,  // A 2 bytes signed value.
5206f6ba60Sopenharmony_ci    DW_EH_PE_sdata4 = 0x0B,  // A 4 bytes signed value.
5306f6ba60Sopenharmony_ci    DW_EH_PE_sdata8 = 0x0C,  // An 8 bytes signed value.
5406f6ba60Sopenharmony_ci};
5506f6ba60Sopenharmony_ci
5606f6ba60Sopenharmony_ci// Table 10-6. DWARF Exception Header application
5706f6ba60Sopenharmony_cienum DW_EH_PE_A {
5806f6ba60Sopenharmony_ci    DW_EH_PE_nothing = 0x00, // nothing to do
5906f6ba60Sopenharmony_ci    DW_EH_PE_pcrel = 0x10,   // relative to the current program counter.
6006f6ba60Sopenharmony_ci    DW_EH_PE_textrel = 0x20, // relative to the beginning of the .text section.
6106f6ba60Sopenharmony_ci    DW_EH_PE_datarel = 0x30, // relative to the beginning of the .got or .eh_frame_hdr section.
6206f6ba60Sopenharmony_ci    DW_EH_PE_funcrel = 0x40, // relative to the beginning of the function.
6306f6ba60Sopenharmony_ci    DW_EH_PE_aligned = 0x50, // aligned to an address unit sized boundary.
6406f6ba60Sopenharmony_ci    DW_EH_PE_omit = 0xff,    // indicate that no value ispresent.
6506f6ba60Sopenharmony_ci};
6606f6ba60Sopenharmony_ci
6706f6ba60Sopenharmony_ciconst std::map<dw_encode_t, size_t> DWFormatSizeMap = {
6806f6ba60Sopenharmony_ci#ifdef ARM
6906f6ba60Sopenharmony_ci    {DW_EH_PE_absptr, 4},
7006f6ba60Sopenharmony_ci#else
7106f6ba60Sopenharmony_ci    {DW_EH_PE_absptr, 8},
7206f6ba60Sopenharmony_ci#endif
7306f6ba60Sopenharmony_ci#ifdef NOT_USE
7406f6ba60Sopenharmony_ci    {DW_EH_PE_uleb128, sizeof(char) * 128},
7506f6ba60Sopenharmony_ci#endif
7606f6ba60Sopenharmony_ci    {DW_EH_PE_udata2, sizeof(char) * 2},
7706f6ba60Sopenharmony_ci    {DW_EH_PE_udata4, sizeof(char) * 4},
7806f6ba60Sopenharmony_ci    {DW_EH_PE_udata8, sizeof(char) * 8},
7906f6ba60Sopenharmony_ci#ifdef NOT_USE
8006f6ba60Sopenharmony_ci    {DW_EH_PE_sleb128, sizeof(char) * 128},
8106f6ba60Sopenharmony_ci#endif
8206f6ba60Sopenharmony_ci    {DW_EH_PE_sdata2, sizeof(char) * 2},
8306f6ba60Sopenharmony_ci    {DW_EH_PE_sdata4, sizeof(char) * 4},
8406f6ba60Sopenharmony_ci    {DW_EH_PE_sdata8, sizeof(char) * 8},
8506f6ba60Sopenharmony_ci};
8606f6ba60Sopenharmony_ci
8706f6ba60Sopenharmony_citemplate<class T>
8806f6ba60Sopenharmony_ciuint64_t dwReadAnyTypeData(const unsigned char *&buffer, T)
8906f6ba60Sopenharmony_ci{
9006f6ba60Sopenharmony_ci    T value;
9106f6ba60Sopenharmony_ci    if (memcpy_s(&value, sizeof(T), buffer, sizeof(T)) != EOK) {
9206f6ba60Sopenharmony_ci        return 0;
9306f6ba60Sopenharmony_ci    }
9406f6ba60Sopenharmony_ci    buffer += sizeof(T);
9506f6ba60Sopenharmony_ci    return static_cast<uint64_t>(value);
9606f6ba60Sopenharmony_ci}
9706f6ba60Sopenharmony_ci
9806f6ba60Sopenharmony_ciclass DwarfEncoding {
9906f6ba60Sopenharmony_cipublic:
10006f6ba60Sopenharmony_ci    DwarfEncoding(dw_encode_t dw, const unsigned char *&data, uint64_t vaddrBase = 0,
10106f6ba60Sopenharmony_ci                  uint64_t vaddrPC = 0, uint64_t vaddrText = 0);
10206f6ba60Sopenharmony_ci
10306f6ba60Sopenharmony_ci    const std::string ToString() const;
10406f6ba60Sopenharmony_ci
10506f6ba60Sopenharmony_ci    const unsigned char *GetEnd() const;
10606f6ba60Sopenharmony_ci
10706f6ba60Sopenharmony_ci    const unsigned char *GetData() const;
10806f6ba60Sopenharmony_ci
10906f6ba60Sopenharmony_ci    size_t GetSize() const;
11006f6ba60Sopenharmony_ci
11106f6ba60Sopenharmony_ci    uint64_t GetValue() const;
11206f6ba60Sopenharmony_ci
11306f6ba60Sopenharmony_ci    uint64_t GetAppliedValue() const;
11406f6ba60Sopenharmony_ci
11506f6ba60Sopenharmony_ci    bool IsOmit() const;
11606f6ba60Sopenharmony_ci
11706f6ba60Sopenharmony_ciprivate:
11806f6ba60Sopenharmony_ci    dw_encode_t dw_;
11906f6ba60Sopenharmony_ci    const unsigned char *data_;
12006f6ba60Sopenharmony_ci    uint64_t vaddrBase_ = 0;
12106f6ba60Sopenharmony_ci    uint64_t vaddrPC_ = 0;
12206f6ba60Sopenharmony_ci    uint64_t vaddrText_ = 0;
12306f6ba60Sopenharmony_ci    uint64_t value_[2] = {0, 0};
12406f6ba60Sopenharmony_ci
12506f6ba60Sopenharmony_ci    dw_encode_t Format() const;
12606f6ba60Sopenharmony_ci
12706f6ba60Sopenharmony_ci    dw_encode_t Application() const;
12806f6ba60Sopenharmony_ci
12906f6ba60Sopenharmony_ci    uint64_t ReadValue(const unsigned char *&data) const;
13006f6ba60Sopenharmony_ci
13106f6ba60Sopenharmony_ci    const std::string FormatName() const;
13206f6ba60Sopenharmony_ci
13306f6ba60Sopenharmony_ci    const std::string ApplicationName() const;
13406f6ba60Sopenharmony_ci};
13506f6ba60Sopenharmony_ci
13606f6ba60Sopenharmony_ci/*
13706f6ba60Sopenharmony_ciLinux Standard Base Core Specification 4.1
13806f6ba60Sopenharmony_ciChapter 10. Object Format
13906f6ba60Sopenharmony_ci10.6.2. The .eh_frame_hdr section
14006f6ba60Sopenharmony_ci
14106f6ba60Sopenharmony_ciTable 10-11. .eh_frame_hdr Section Format
14206f6ba60Sopenharmony_ci
14306f6ba60Sopenharmony_ciEncoding	Field
14406f6ba60Sopenharmony_ciunsigned byte	version
14506f6ba60Sopenharmony_ciunsigned byte	eh_frame_ptr_enc
14606f6ba60Sopenharmony_ciunsigned byte	fde_count_enc
14706f6ba60Sopenharmony_ciunsigned byte	table_enc
14806f6ba60Sopenharmony_ciencoded	eh_frame_ptr
14906f6ba60Sopenharmony_ciencoded	fde_count
15006f6ba60Sopenharmony_ci    binary search table
15106f6ba60Sopenharmony_ci*/
15206f6ba60Sopenharmony_ci
15306f6ba60Sopenharmony_cistruct binary_search_table {
15406f6ba60Sopenharmony_ci    uint64_t ipVaddrOffset;
15506f6ba60Sopenharmony_ci    uint64_t fdeVaddrOffset;
15606f6ba60Sopenharmony_ci};
15706f6ba60Sopenharmony_ci
15806f6ba60Sopenharmony_cistruct eh_frame_hdr {
15906f6ba60Sopenharmony_ci    // Version of the .eh_frame_hdr format. This value shall be 1.
16006f6ba60Sopenharmony_ci    dw_encode_t version;
16106f6ba60Sopenharmony_ci
16206f6ba60Sopenharmony_ci    // The encoding format of the eh_frame_ptr field.
16306f6ba60Sopenharmony_ci    dw_encode_t eh_frame_ptr_enc;
16406f6ba60Sopenharmony_ci
16506f6ba60Sopenharmony_ci    // The encoding format of the fde_count field. A value of DW_EH_PE_omit indicates the binary
16606f6ba60Sopenharmony_ci    // search table is not present.
16706f6ba60Sopenharmony_ci    dw_encode_t fde_count_enc;
16806f6ba60Sopenharmony_ci
16906f6ba60Sopenharmony_ci    // The encoding format of the entries in the binary search table. A value of DW_EH_PE_omit
17006f6ba60Sopenharmony_ci    // indicates the binary search table is not present.
17106f6ba60Sopenharmony_ci    dw_encode_t table_enc;
17206f6ba60Sopenharmony_ci
17306f6ba60Sopenharmony_ci    // The encoded value of the pointer to the start of the .eh_frame section.
17406f6ba60Sopenharmony_ci    /*
17506f6ba60Sopenharmony_ci    dw_encode_t eh_frame_ptr
17606f6ba60Sopenharmony_ci    dw_encode_t fde_count
17706f6ba60Sopenharmony_ci    */
17806f6ba60Sopenharmony_ci    // A binary search table containing fde_count entries. Each entry of the table consist of two
17906f6ba60Sopenharmony_ci    // encoded values, the initial location, and the address. The entries are sorted in an
18006f6ba60Sopenharmony_ci    // increasing order by the initial location value.
18106f6ba60Sopenharmony_ci
18206f6ba60Sopenharmony_ci    /*
18306f6ba60Sopenharmony_ci     * struct {
18406f6ba60Sopenharmony_ci     *    encoded start_ip
18506f6ba60Sopenharmony_ci     *    encoded fde_addr
18606f6ba60Sopenharmony_ci     * } binary_search_table[fde_count]
18706f6ba60Sopenharmony_ci     */
18806f6ba60Sopenharmony_ci    unsigned char encode_data[0];
18906f6ba60Sopenharmony_ci} PACKED;
19006f6ba60Sopenharmony_ci} // namespace NativeDaemon
19106f6ba60Sopenharmony_ci} // namespace Developtools
19206f6ba60Sopenharmony_ci} // namespace OHOS
19306f6ba60Sopenharmony_ci#endif
194