1 /*
2 * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
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 <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <sys/stat.h>
16 #include <malloc.h>
17 #include "elf.h"
18
19 #define le16_to_cpu(x) (x)
20 #define le32_to_cpu(x) (x)
21 #define le64_to_cpu(x) (x)
22
23 #define be16_to_cpu(x) ((((x)&0xff) << 8) | (((x) >> 8) & 0xff))
24 #define be32_to_cpu(x) ((be16_to_cpu((x)) << 16) | (be16_to_cpu((x) >> 16)))
25 #define be64_to_cpu(x) ((be32_to_cpu((x)) << 32) | (be32_to_cpu((x) >> 32)))
26
27 #define be128ptr_to_cpu_hi(x) (be64_to_cpu(*(u64 *)(x)))
28 #define be128ptr_to_cpu_lo(x) (be64_to_cpu(*((u64 *)(x) + 1)))
29
30 #define be96ptr_to_cpu_hi(x) (be32_to_cpu(*(u32 *)(x)))
31 #define be96ptr_to_cpu_lo(x) \
32 (((u64)(be32_to_cpu(*((u32 *)(x) + 1)))) << 32 \
33 | (be32_to_cpu(*((u32 *)(x)) + 2)))
34
get_elf_info(const char * binary,struct elf_info * info)35 void get_elf_info(const char *binary, struct elf_info *info)
36 {
37 struct elf_file *elf;
38 int i;
39 u64 size = 0;
40 u64 offset;
41 u64 min_vaddr, max_vaddr;
42
43 elf = elf_parse_file(binary, info);
44 if (!elf) {
45 printf("parse elf fail\n");
46 return;
47 }
48
49 min_vaddr = (u64)-1;
50 max_vaddr = 0;
51 for (i = 0; i < elf->header.e_phnum; ++i) {
52 if (elf->p_headers[i].p_type != PT_LOAD)
53 continue;
54 if (elf->p_headers[i].p_vaddr < min_vaddr)
55 min_vaddr = elf->p_headers[i].p_vaddr;
56 if (elf->p_headers[i].p_vaddr + elf->p_headers[i].p_memsz > max_vaddr)
57 max_vaddr = elf->p_headers[i].p_vaddr + elf->p_headers[i].p_memsz;
58 }
59
60 size = max_vaddr - min_vaddr;
61
62 info->entry = elf->header.e_entry;
63 info->flags = elf->header.e_flags;
64 info->mem_size = size;
65 info->phentsize = elf->header.e_phentsize;
66 info->phnum = elf->header.e_phnum;
67 info->phdr_addr = elf->p_headers[0].p_vaddr + elf->header.e_phoff;
68
69 free(elf);
70 }
71
main(int argc,char * argv[])72 int main(int argc, char *argv[])
73 {
74 int fd;
75 struct stat st;
76 char *buf;
77 struct elf_info info;
78
79 if (argc == 1) {
80 printf("Need a path points to the procmgr.elf\n");
81 }
82
83 fd = open(argv[1], O_RDONLY);
84 if (fd < 0) {
85 printf("Can not open elf file!\n");
86 }
87 fstat(fd, &st);
88 buf = malloc(st.st_size);
89 read(fd, buf, st.st_size);
90 get_elf_info(buf, &info);
91
92 free(buf);
93 close(fd);
94 info.entry = be64_to_cpu(info.entry);
95 info.flags = be64_to_cpu(info.flags);
96 info.mem_size = be64_to_cpu(info.mem_size);
97 info.phentsize = be64_to_cpu(info.phentsize);
98 info.phnum = be64_to_cpu(info.phnum);
99 info.phdr_addr = be64_to_cpu(info.phdr_addr);
100
101 fd = open("./elf_info.temp", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
102 if (fd < 0) {
103 printf("Create file failed!\n");
104 }
105
106 write(fd, (void *)&info, sizeof(struct elf_info));
107 close(fd);
108
109 return 0;
110 }
111