1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <elf_parser.h>
17
18 using namespace OHOS::Developtools::HiPerf::ELF;
19 namespace OHOS {
20 namespace Developtools {
21 namespace HiPerf {
22 enum class NUMBER : int {
23 ZERO = 0,
24 ONE = 1,
25 TWO = 2,
26 THREE = 3,
27 FOUR = 4,
28 FIVE = 5,
29 SIX = 6,
30 SEVEN = 7,
31 EIGHT = 8,
32 NINE = 9,
33 TEN = 10,
34 ELEVEN = 11,
35 TWELVE = 12,
36 };
37
MakeUnique(char * const shdrBuf,const size_t bufSize,const size_t index)38 std::unique_ptr<SectionHeader> SectionHeader::MakeUnique(char * const shdrBuf, const size_t bufSize,
39 const size_t index)
40 {
41 std::unique_ptr<SectionHeader> shdr {new (std::nothrow) SectionHeader()};
42 if (shdr == nullptr) {
43 return nullptr;
44 }
45 if (!shdr->Init(shdrBuf, bufSize, index)) {
46 HLOGE("SectionHeader::Init(shdrBuf, bufSize, index) failed");
47 DumpShdrBuf(shdrBuf, bufSize);
48 return nullptr;
49 }
50 return shdr;
51 }
52
ParseSecHeader32(char * const shdrBuf)53 bool SectionHeader::ParseSecHeader32(char * const shdrBuf)
54 {
55 uint32_t *u4Buf = reinterpret_cast<uint32_t *>(shdrBuf);
56 int index {0};
57 nameIndex_ = u4Buf[index];
58 index = static_cast<int>(NUMBER::ONE);
59 secType_ = u4Buf[index];
60 index = static_cast<int>(NUMBER::TWO);
61 secFlags_ = u4Buf[index];
62 index = static_cast<int>(NUMBER::SIX);
63 link_ = u4Buf[index];
64 index = static_cast<int>(NUMBER::SEVEN);
65 info_ = u4Buf[index];
66 index = static_cast<int>(NUMBER::THREE);
67 secVaddr_ = u4Buf[index];
68 index = static_cast<int>(NUMBER::FOUR);
69 fileOffset_ = u4Buf[index];
70 index = static_cast<int>(NUMBER::FIVE);
71 secSize_ = u4Buf[index];
72 index = static_cast<int>(NUMBER::EIGHT);
73 secAddrAlign_ = u4Buf[index];
74 index = static_cast<int>(NUMBER::NINE);
75 secEntrySize_ = u4Buf[index];
76 return true;
77 }
78
ParseSecHeader64(char * const shdrBuf)79 bool SectionHeader::ParseSecHeader64(char * const shdrBuf)
80 {
81 uint64_t *u8Buf = reinterpret_cast<uint64_t *>(shdrBuf);
82 uint32_t *u4Buf = reinterpret_cast<uint32_t *>(shdrBuf);
83 size_t index {0};
84 nameIndex_ = u4Buf[index];
85 index = static_cast<size_t>(NUMBER::ONE);
86 secType_ = u4Buf[index];
87 secFlags_ = u8Buf[index];
88 index = static_cast<size_t>(NUMBER::TEN);
89 link_ = u4Buf[index];
90 index = static_cast<size_t>(NUMBER::ELEVEN);
91 info_ = u4Buf[index];
92 index = static_cast<size_t>(NUMBER::TWO);
93 secVaddr_ = u8Buf[index];
94 index = static_cast<size_t>(NUMBER::THREE);
95 fileOffset_ = u8Buf[index];
96 index = static_cast<size_t>(NUMBER::FOUR);
97 secSize_ = u8Buf[index];
98 index = static_cast<size_t>(NUMBER::SIX);
99 secAddrAlign_ = u8Buf[index];
100 index = static_cast<size_t>(NUMBER::SEVEN);
101 secEntrySize_ = u8Buf[index];
102 return true;
103 }
104 } // namespace HiPerf
105 } // namespace Developtools
106 } // namespace OHOS
107