-
Notifications
You must be signed in to change notification settings - Fork 2
/
elf-loader.c
209 lines (172 loc) · 5.61 KB
/
elf-loader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/***************************************************************************
* Copyright (C) 2014 by Franck Jullien *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <gelf.h>
#include <fcntl.h>
#ifdef __cplusplus
extern "C" {
#endif
uint8_t big_endian;
uint8_t *dump_program_data(Elf *elf_object, int *size)
{
uint8_t *buffer = NULL;
Elf_Data *data = NULL;
size_t phdr_num;
size_t max_paddr = 0;
GElf_Phdr phdr;
*size = 0;
int ret = elf_getphdrnum(elf_object, &phdr_num);
if (ret) {
printf("Problem during ELF parsing\n");
return NULL;
}
if (phdr_num == 0)
return NULL;
for (size_t i = 0; i < phdr_num; i++) {
if (gelf_getphdr(elf_object, i, &phdr) != &phdr) {
printf("Problem during ELF parsing\n");
return NULL;
}
printf("Program header %lu: addr 0x%08X,", i, (unsigned int)phdr.p_paddr);
printf(" size 0x%08X\n", (unsigned int)phdr.p_filesz);
if (phdr.p_paddr + phdr.p_filesz >= max_paddr) {
max_paddr = phdr.p_paddr + phdr.p_filesz;
buffer = (uint8_t *)realloc(buffer, max_paddr);
}
data = elf_getdata_rawchunk(elf_object, phdr.p_offset, phdr.p_filesz, ELF_T_BYTE);
if (data != NULL)
memcpy(buffer + phdr.p_paddr, data->d_buf, data->d_size);
else {
printf("Couldn't load program data chunk\n");
return NULL;
}
}
*size = max_paddr;
return buffer;
}
uint8_t *dump_section_data(Elf *elf_object, int *size)
{
uint8_t *buffer = NULL;
Elf_Data *data = NULL;
size_t shdr_num;
size_t max_saddr = 0;
GElf_Shdr shdr;
size_t shstrndx;
char *name = NULL;
*size = 0;
int ret = elf_getshdrnum(elf_object, &shdr_num);
if (ret) {
printf("Problem during ELF parsing\n");
return NULL;
}
if (shdr_num == 0)
return NULL;
Elf_Scn *cur_section = NULL;
ret = elf_getshdrstrndx(elf_object, &shstrndx);
if (ret)
printf("No string table found\n");
while ((cur_section = elf_nextscn(elf_object, cur_section)) != NULL ) {
if (gelf_getshdr(cur_section, &shdr) != &shdr) {
printf("Problem during ELF parsing\n");
return NULL;
}
if ((shdr.sh_type == SHT_PROGBITS) && (shdr.sh_flags & SHF_ALLOC) && shdr.sh_size != 0) {
name = elf_strptr(elf_object, shstrndx , shdr.sh_name);
printf("Loading section %s, size 0x%08X lma 0x%08X\n",
name ? name : "??", (unsigned int)shdr.sh_size, (unsigned int)shdr.sh_addr);
if (shdr.sh_addr + shdr.sh_size >= max_saddr) {
max_saddr = shdr.sh_addr + shdr.sh_size;
buffer = (uint8_t *)realloc(buffer, max_saddr);
}
data = elf_getdata(cur_section, data);
if (data != NULL)
memcpy(buffer + shdr.sh_addr, data->d_buf, data->d_size);
else {
printf("Couldn't load section data chunk\n");
return NULL;
}
}
}
*size = max_saddr;
return buffer;
}
uint8_t *load_elf_file(char *elf_file_name, int *size)
{
uint8_t *buf = NULL;
char *id;
if (elf_version(EV_CURRENT) == EV_NONE)
return NULL;
int fd = open(elf_file_name, O_RDONLY , 0);
if (fd < 0) {
printf("Can't open %s\n", elf_file_name);
return NULL;
}
Elf *elf_object = elf_begin(fd , ELF_C_READ , NULL);
if (elf_object == NULL) {
printf("Problem while starting ELF parsing\n");
close(fd);
return NULL;
}
if (elf_kind(elf_object) != ELF_K_ELF) {
printf("%s is not an ELF file\n", elf_file_name);
elf_end(elf_object);
close(fd);
return NULL;
}
if (( id = elf_getident (elf_object , NULL )) == NULL )
printf("getident() failed : %s.",
elf_errmsg(-1));
if (id[EI_DATA] == ELFDATA2LSB)
big_endian = 0;
else if (id[EI_DATA] == ELFDATA2MSB)
big_endian = 1;
else {
printf("%s has unknown endianness '%d'\n", elf_file_name, id[EI_DATA]);
elf_end(elf_object);
close(fd);
return NULL;
}
buf = dump_program_data(elf_object, size);
if (buf == NULL)
buf = dump_section_data(elf_object, size);
elf_end(elf_object);
close(fd);
return buf;
}
unsigned int read_32(uint8_t *bin_file, unsigned int address)
{
if (big_endian)
return (bin_file[address] << 24) | (bin_file[address + 1] << 16) |
(bin_file[address + 2] << 8) | (bin_file[address + 3]);
else
return (bin_file[address + 3] << 24) | (bin_file[address + 2] << 16) |
(bin_file[address + 1] << 8) | (bin_file[address + 0]);
}
unsigned short read_16(uint8_t *bin_file, unsigned int address)
{
return (bin_file[address] << 8) | (bin_file[address + 1]);
}
#ifdef __cplusplus
}
#endif