• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #include "get_elf_info.h"
13 #include <stdio.h>
14 #include <elf.h>
15 #include <tee_log.h>
16 
get_elf_class(const char * ehdr,uint32_t ehdr_size)17 int32_t get_elf_class(const char *ehdr, uint32_t ehdr_size)
18 {
19     if ((ehdr == NULL) || (ehdr_size < EI_NIDENT)) {
20         tloge("elf header or size:%u is invalid\n", ehdr_size);
21         return -1;
22     }
23 
24     if (ehdr[EI_MAG0] != (char)0x7f ||
25         ehdr[EI_MAG1] != 'E' ||
26         ehdr[EI_MAG2] != 'L' ||
27         ehdr[EI_MAG3] != 'F') {
28             tloge("invalid elf format, magic mismatch\n");
29             return -1;
30     }
31 
32     return (int32_t)ehdr[EI_CLASS];
33 }
34 
get_elf_type(const char * ehdr,uint32_t ehdr_size,int32_t elf_class)35 int32_t get_elf_type(const char *ehdr, uint32_t ehdr_size, int32_t elf_class)
36 {
37     if (ehdr == NULL) {
38         tloge("invalid ehdr\n");
39         return -1;
40     }
41 
42     if (elf_class == ELFCLASS32) {
43         if (ehdr_size < sizeof(Elf32_Ehdr)) {
44             tloge("invalid ehdr size:%u\n", ehdr_size);
45             return -1;
46         }
47 
48         return ((Elf32_Ehdr *)ehdr)->e_type;
49     }
50 
51     if (elf_class == ELFCLASS64) {
52         if (ehdr_size < sizeof(Elf64_Ehdr)) {
53             tloge("invalid 64 ehdr size:%u\n", ehdr_size);
54             return -1;
55         }
56 
57         return ((Elf64_Ehdr *)ehdr)->e_type;
58     }
59 
60     return -1;
61 }
62