1 /*
2 * Copyright (c) 2023 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 "ecmascript/platform/elf.h"
17
18 #include "ecmascript/log_wrapper.h"
19 #include "securec.h"
20
21 #define EI_MAG0 0
22 #define ELFMAG0 0x7f
23
24 #define EI_MAG1 1
25 #define ELFMAG1 'E'
26
27 #define EI_MAG2 2
28 #define ELFMAG2 'L'
29
30 #define EI_MAG3 3
31 #define ELFMAG3 'F'
32
33 #define EI_CLASS 4
34 #define ELFCLASS64 2
35
36 #define EI_DATA 5
37 #define ELFDATA2LSB 1
38
39 #define EI_VERSION 6
40
41 #define ET_DYN 3
42 #define EM_X86_64 62
43 #define EM_ARM 40
44 #define EM_AARCH64 183
45
46 namespace panda::ecmascript {
PackELFHeader(Elf64_Ehdr & header,uint32_t version,kungfu::Triple triple)47 void PackELFHeader(Elf64_Ehdr &header, uint32_t version, kungfu::Triple triple)
48 {
49 if (memset_s(reinterpret_cast<void *>(&header), sizeof(Elf64_Ehdr), 0, sizeof(Elf64_Ehdr)) != EOK) {
50 UNREACHABLE();
51 }
52 header.e_ident[EI_MAG0] = ELFMAG0;
53 header.e_ident[EI_MAG1] = ELFMAG1;
54 header.e_ident[EI_MAG2] = ELFMAG2;
55 header.e_ident[EI_MAG3] = ELFMAG3;
56 header.e_ident[EI_CLASS] = ELFCLASS64;
57 header.e_ident[EI_DATA] = ELFDATA2LSB;
58 header.e_ident[EI_VERSION] = 1;
59
60 header.e_type = ET_DYN;
61 switch (triple) {
62 case kungfu::Triple::TRIPLE_AMD64:
63 header.e_machine = EM_X86_64;
64 break;
65 case kungfu::Triple::TRIPLE_ARM32:
66 header.e_machine = EM_ARM;
67 break;
68 case kungfu::Triple::TRIPLE_AARCH64:
69 header.e_machine = EM_AARCH64;
70 break;
71 default:
72 UNREACHABLE();
73 break;
74 }
75 header.e_version = version;
76 }
77
VerifyELFHeader(const Elf64_Ehdr & header,uint32_t version)78 bool VerifyELFHeader([[maybe_unused]] const Elf64_Ehdr &header, [[maybe_unused]] uint32_t version)
79 {
80 LOG_ECMA(ERROR) << "Elf is not supported on windows platform";
81 return false;
82 }
83 } // namespace panda::ecmascript
84