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 {
MakeUnique(char * const phdrBuf,const size_t bufSize)22 std::unique_ptr<ProgramHeader> ProgramHeader::MakeUnique(char * const phdrBuf, const size_t bufSize)
23 {
24 std::unique_ptr<ProgramHeader> phdr {new (std::nothrow) ProgramHeader()};
25 if (phdr == nullptr) {
26 HLOGE("ProgramHeader() failed");
27 return nullptr;
28 }
29 if (!phdr->Init(phdrBuf, bufSize)) {
30 HLOGE("ProgramHeader::Init(phdrBuf, bufSize) failed");
31 DumpPhdrBuf(phdrBuf, bufSize);
32 return nullptr;
33 }
34 return phdr;
35 }
36
ParsePrgHeader32(char * const phdrBuf)37 bool ProgramHeader::ParsePrgHeader32(char * const phdrBuf)
38 {
39 uint32_t *u4Buf = reinterpret_cast<uint32_t *>(phdrBuf);
40 size_t index {0};
41 type_ = u4Buf[index];
42 ++index;
43 offset_ = u4Buf[index];
44 ++index;
45 vaddr_ = u4Buf[index];
46 ++index;
47 paddr_ = u4Buf[index];
48 ++index;
49 fileSize_ = u4Buf[index];
50 ++index;
51 memSize_ = u4Buf[index];
52 ++index;
53 flags_ = u4Buf[index];
54 ++index;
55 secAlign_ = u4Buf[index];
56 return true;
57 }
58
ParsePrgHeader64(char * const phdrBuf)59 bool ProgramHeader::ParsePrgHeader64(char * const phdrBuf)
60 {
61 uint32_t *u4Buf = reinterpret_cast<uint32_t *>(phdrBuf);
62 size_t index {0};
63 type_ = u4Buf[index];
64 ++index;
65 flags_ = u4Buf[index];
66
67 uint64_t *u8Buf = reinterpret_cast<uint64_t *>(phdrBuf);
68 offset_ = u8Buf[index];
69 ++index;
70 vaddr_ = u8Buf[index];
71 ++index;
72 paddr_ = u8Buf[index];
73 ++index;
74 fileSize_ = u8Buf[index];
75 ++index;
76 memSize_ = u8Buf[index];
77 ++index;
78 secAlign_ = u8Buf[index];
79 return true;
80 }
81 } // namespace HiPerf
82 } // namespace Developtools
83 } // namespace OHOS
84