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
|
/*
* Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
*
* This file is part of nios2sim-ng.
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file "COPYING" in the main directory of this archive
* for more details.
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <libelf.h>
#include <gelf.h>
#include "nios2sim-ng.h"
#include "image.h"
/* not defined with libelf */
#define EM_ALTERA_NIOS2 113
/*
* http://elftoolchain.sourceforge.net/for-review/libelf-by-example-20100112.pdf
*/
static bool elf_is_text_section(GElf_Shdr *shdr)
{
if (shdr->sh_type == SHT_PROGBITS
&& (shdr->sh_flags & ~SHF_WRITE) == (SHF_ALLOC | SHF_EXECINSTR))
return true;
return false;
}
static bool elf_is_data_section(GElf_Shdr *shdr)
{
if (shdr->sh_type == SHT_PROGBITS
&& (shdr->sh_flags & ~SHF_WRITE) == SHF_ALLOC)
return true;
if (shdr->sh_type == SHT_NOBITS
&& (shdr->sh_flags == (SHF_ALLOC | SHF_WRITE)))
return true;
return false;
}
static int elf_read_sections(Elf *e, struct memory *mem)
{
Elf_Scn *scn = NULL;
size_t shstrndx;
char *name;
bool found_text = false;
bool found_data = false;
if (elf_getshdrstrndx(e, &shstrndx) != 0) {
err("elf_getshdrstrndx() failed: %s\n", elf_errmsg(-1));
return -1;
}
while ((scn = elf_nextscn(e, scn)) != NULL) {
GElf_Shdr shdr;
if (gelf_getshdr(scn, &shdr) == NULL) {
err("Couldn't get section header: %s\n", elf_errmsg(-1));
return -1;
}
/* Is it a .text section? */
if (elf_is_text_section(&shdr)) {
Elf_Data *text;
size_t n;
if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) {
err("Couldn't get section name: %s\n", elf_errmsg(-1));
return -1;
}
dbg("text section %zu: %s (size=%zu, flags=%08lx, addr=%08lx, off=%08lx)\n",
elf_ndxscn(scn), name, shdr.sh_size, shdr.sh_flags, shdr.sh_addr, shdr.sh_offset);
text = NULL;
n = 0;
while (n < shdr.sh_size &&
(text = elf_getdata(scn, text)) != NULL) {
if (shdr.sh_addr + n + text->d_size > mem->size) {
err("Would write behind memory\n");
return -1;
}
memcpy(&mem->base[(shdr.sh_addr + n) / 4], text->d_buf, text->d_size);
n += text->d_size;
}
found_text = true;
} else if (elf_is_data_section(&shdr)) {
Elf_Data *data;
size_t n;
if ((name = elf_strptr(e, shstrndx, shdr.sh_name)) == NULL) {
err("Couldn't get section name: %s\n", elf_errmsg(-1));
return -1;
}
dbg("data section %zu: %s (size=%zu, flags=%08lx, addr=%08lx, off=%08lx)\n",
elf_ndxscn(scn), name, shdr.sh_size, shdr.sh_flags, shdr.sh_addr, shdr.sh_offset);
data = NULL;
n = 0;
while (n < shdr.sh_size &&
(data = elf_getdata(scn, data)) != NULL) {
if (shdr.sh_addr + n + data->d_size > mem->size) {
err("Would write behind memory\n");
return -1;
}
/* XXX: why could this ever be NULL? */
if (data->d_buf == NULL) {
err("data section %s is empty\n", name);
break;
}
memcpy(&mem->base[(shdr.sh_addr + n) / 4], data->d_buf, data->d_size);
n += data->d_size;
}
found_data = true;
}
}
if (!found_text) {
err("No text section found in binary\n");
return -1;
}
if (!found_data) {
err("No data section found in binary\n");
return -1;
}
return 0;
}
int elf_load(FILE *fp, const char *name, struct memory *mem)
{
int fd = fileno(fp);
int ret;
Elf *e;
GElf_Ehdr ehdr;
if (unlikely(fd < 0)) {
err("Couldn't get file descriptor for '%s'\n", name);
return -1;
}
if (elf_version(EV_CURRENT) == EV_NONE) {
err("ELF library initialization failed\n");
return -1;
}
ret = -1;
e = elf_begin(fd, ELF_C_READ, NULL);
if (unlikely(e == NULL)) {
err("Couldn't get ELF file handle for '%s': %s\n", name, elf_errmsg(-1));
goto out;
}
/* Check whether we've got a valid ELF executable for Nios II */
if (elf_kind(e) != ELF_K_ELF) {
err("'%s' is not an ELF object\n", name);
goto out;
}
if (gelf_getclass(e) != ELFCLASS32) {
err("'%s' has invalid ELF class\n", name);
goto out;
}
if (gelf_getehdr(e, &ehdr) == NULL) {
err("gelf_getehdr() failed: %s\n", elf_errmsg(-1));
goto out;
}
if (ehdr.e_type != ET_EXEC || ehdr.e_machine != EM_ALTERA_NIOS2) {
err("'%s' is not a Nios2 ELF executable (%x, %x)\n", name, ehdr.e_type, ehdr.e_machine);
goto out;
}
ret = elf_read_sections(e, mem);
out:
elf_end(e);
return ret;
}
|