-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathelf.c
307 lines (249 loc) · 8.53 KB
/
elf.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/****************************************************************************
* binfmt/elf.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/param.h>
#include <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/arch.h>
#include <nuttx/binfmt/binfmt.h>
#include <nuttx/kmalloc.h>
#ifdef CONFIG_ELF
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* CONFIG_DEBUG_FEATURES, CONFIG_DEBUG_INFO, and CONFIG_DEBUG_BINFMT
* have to be defined or CONFIG_MODLIB_DUMPBUFFER does nothing.
*/
#if !defined(CONFIG_DEBUG_INFO) || !defined(CONFIG_DEBUG_BINFMT)
# undef CONFIG_MODLIB_DUMPBUFFER
#endif
#ifndef CONFIG_ELF_STACKSIZE
# define CONFIG_ELF_STACKSIZE 2048
#endif
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int elf_loadbinary(FAR struct binary_s *binp,
FAR const char *filename,
FAR const struct symtab_s *exports,
int nexports);
static int elf_unloadbinary(FAR struct binary_s *binp);
/****************************************************************************
* Private Data
****************************************************************************/
static struct binfmt_s g_elfbinfmt =
{
NULL, /* next */
elf_loadbinary, /* load */
elf_unloadbinary, /* unload */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: elf_loadbinary
*
* Description:
* Verify that the file is an ELF binary and, if so, load the ELF
* binary into memory
*
****************************************************************************/
static int elf_loadbinary(FAR struct binary_s *binp,
FAR const char *filename,
FAR const struct symtab_s *exports,
int nexports)
{
struct mod_loadinfo_s loadinfo;
int ret;
binfo("Loading file: %s\n", filename);
/* Initialize the ELF library to load the program binary. */
ret = modlib_initialize(filename, &loadinfo);
modlib_dumploadinfo(&loadinfo);
if (ret != 0)
{
berr("Failed to initialize to load ELF program binary: %d\n", ret);
return ret;
}
/* Load the program binary */
ret = modlib_load_with_addrenv(&loadinfo);
modlib_dumploadinfo(&loadinfo);
if (ret != 0)
{
berr("Failed to load ELF program binary: %d\n", ret);
goto errout_with_init;
}
/* Bind the program to the exported symbol table */
if (loadinfo.ehdr.e_type == ET_REL || loadinfo.gotindex >= 0)
{
ret = modlib_bind(&binp->mod, &loadinfo, exports, nexports);
if (ret != 0)
{
berr("Failed to bind symbols program binary: %d\n", ret);
goto errout_with_load;
}
binp->entrypt = (main_t)(loadinfo.textalloc + loadinfo.ehdr.e_entry);
}
else if (loadinfo.ehdr.e_type == ET_EXEC)
{
if (nexports > 0)
{
berr("Cannot bind exported symbols to a "
"fully linked executable\n");
ret = -ENOEXEC;
goto errout_with_load;
}
/* The entrypoint for a fully linked executable can be found directly */
binp->entrypt = (main_t)(loadinfo.ehdr.e_entry);
}
else
{
berr("Unexpected elf type %d\n", loadinfo.ehdr.e_type);
ret = -ENOEXEC;
goto errout_with_load;
}
/* Return the load information */
binp->stacksize = CONFIG_ELF_STACKSIZE;
/* Add the ELF allocation to the alloc[] only if there is no address
* environment. If there is an address environment, it will automatically
* be freed when the function exits
*
* REVISIT: If the module is loaded then unloaded, wouldn't this cause
* a memory leak?
*/
#ifdef CONFIG_ARCH_ADDRENV
/* Save the address environment in the binfmt structure. This will be
* needed when the module is executed.
*/
binp->addrenv = loadinfo.addrenv;
#else
# ifdef CONFIG_ARCH_USE_SEPARATED_SECTION
if (loadinfo.ehdr.e_type == ET_REL)
{
binp->mod.sectalloc = (FAR void *)loadinfo.sectalloc;
binp->mod.nsect = loadinfo.ehdr.e_shnum;
}
# endif
binp->mod.textalloc = (FAR void *)loadinfo.textalloc;
binp->mod.dataalloc = (FAR void *)loadinfo.datastart;
# ifdef CONFIG_BINFMT_CONSTRUCTORS
binp->mod.initarr = loadinfo.initarr;
binp->mod.finiarr = loadinfo.finiarr;
# endif
#endif
#ifdef CONFIG_BINFMT_CONSTRUCTORS
/* Save information about constructors and destructors. */
binp->mod.initarr = loadinfo.initarr;
binp->mod.ninit = loadinfo.ninit;
binp->mod.finiarr = loadinfo.finiarr;
binp->mod.nfini = loadinfo.nfini;
#endif
#ifdef CONFIG_SCHED_USER_IDENTITY
/* Save IDs and mode from file system */
binp->uid = loadinfo.fileuid;
binp->gid = loadinfo.filegid;
binp->mode = loadinfo.filemode;
#endif
modlib_dumpentrypt(&loadinfo);
#ifdef CONFIG_PIC
if (loadinfo.gotindex >= 0)
{
FAR struct dspace_s *dspaces = kmm_zalloc(sizeof(struct dspace_s));
if (dspaces == NULL)
{
ret = -ENOMEM;
goto errout_with_load;
}
dspaces->region = (FAR void *)loadinfo.shdr[loadinfo.gotindex].sh_addr;
dspaces->crefs = 1;
binp->picbase = (FAR void *)dspaces;
}
#endif
modlib_uninitialize(&loadinfo);
return OK;
errout_with_load:
modlib_unload(&loadinfo);
errout_with_init:
modlib_uninitialize(&loadinfo);
return ret;
}
/****************************************************************************
* Name: elf_unloadbinary
*
* Description:
* Unload the ELF binary that was loaded into memory by elf_loadbinary.
*
****************************************************************************/
static int elf_unloadbinary(FAR struct binary_s *binp)
{
binfo("Unloading %p\n", binp);
modlib_uninit(&binp->mod);
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: elf_initialize
*
* Description:
* In order to use the ELF binary format, this function must be called
* during system initialization to register the ELF binary format.
*
* Returned Value:
* This is a NuttX internal function so it follows the convention that
* 0 (OK) is returned on success and a negated errno is returned on
* failure.
*
****************************************************************************/
int elf_initialize(void)
{
int ret;
/* Register ourselves as a binfmt loader */
binfo("Registering ELF\n");
ret = register_binfmt(&g_elfbinfmt);
if (ret != 0)
{
berr("Failed to register binfmt: %d\n", ret);
}
return ret;
}
/****************************************************************************
* Name: elf_uninitialize
*
* Description:
* Unregister the ELF binary loader
*
* Returned Value:
* None
*
****************************************************************************/
void elf_uninitialize(void)
{
unregister_binfmt(&g_elfbinfmt);
}
#endif /* CONFIG_ELF */