forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinfmt.h
377 lines (324 loc) · 13.9 KB
/
binfmt.h
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/****************************************************************************
* include/nuttx/binfmt/binfmt.h
*
* 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_BINFMT_BINFMT_H
#define __INCLUDE_NUTTX_BINFMT_BINFMT_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <spawn.h>
#include <sys/types.h>
#include <nuttx/sched.h>
#include <nuttx/lib/modlib.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define BINFMT_NALLOC 4
/****************************************************************************
* Public Types
****************************************************************************/
/* The type of one C++ constructor or destructor */
typedef CODE void (*binfmt_ctor_t)(void);
typedef CODE void (*binfmt_dtor_t)(void);
/* This describes the file to be loaded.
*
* NOTE 1: The 'filename' must be the full, absolute path to the file to be
* executed unless CONFIG_LIBC_ENVPATH is defined. In that case,
* 'filename' may be a relative path; a set of candidate absolute paths
* will be generated using the PATH environment variable and load_module()
* will attempt to load each file that is found at those absolute paths.
*/
struct symtab_s;
struct binary_s
{
/* Information provided from the loader (if successful) describing the
* resources used by the loaded module.
*/
main_t entrypt; /* Entry point into a program module */
FAR void *mapped; /* Memory-mapped, address space */
#ifdef CONFIG_LIBC_MODLIB
struct module_s mod; /* Module context */
#endif
#ifdef CONFIG_PIC
FAR void *picbase; /* Position-independent */
#endif
#ifdef CONFIG_ARCH_ADDRENV
/* Address environment.
*
* addrenv - This is the handle created by addrenv_allocate() that can be
* used to manage the tasks address space.
*/
FAR addrenv_t *addrenv; /* Address environment */
FAR addrenv_t *oldenv; /* Saved address environment */
#endif
size_t mapsize; /* Size of the mapped address region (needed for munmap) */
/* Start-up information that is provided by the loader, but may be modified
* by the caller between load_module() and exec_module() calls.
*/
uint8_t priority; /* Task execution priority */
size_t stacksize; /* Size of the stack in bytes (unallocated) */
#ifdef CONFIG_SCHED_USER_IDENTITY
uid_t uid; /* File owner user identity */
gid_t gid; /* File owner group user identity */
int mode; /* File mode added to */
#endif
#ifndef CONFIG_BUILD_KERNEL
FAR void *stackaddr; /* Task stack address */
#endif
/* Unload module callback */
CODE int (*unload)(FAR struct binary_s *bin);
};
/* This describes one binary format handler */
struct binfmt_s
{
/* Supports a singly-linked list */
FAR struct binfmt_s *next;
/* Verify and load binary into memory */
CODE int (*load)(FAR struct binary_s *bin,
FAR const char *filename,
FAR const struct symtab_s *exports,
int nexports);
/* Unload module callback */
CODE int (*unload)(FAR struct binary_s *bin);
};
/****************************************************************************
* Public Data
****************************************************************************/
#if defined(__cplusplus)
extern "C"
{
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: binfmt_initialize
*
* Description:
* initialize binfmt subsystem
*
****************************************************************************/
void binfmt_initialize(void);
/****************************************************************************
* Name: register_binfmt
*
* Description:
* Register a loader for a 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 register_binfmt(FAR struct binfmt_s *binfmt);
/****************************************************************************
* Name: unregister_binfmt
*
* Description:
* Register a loader for a 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 unregister_binfmt(FAR struct binfmt_s *binfmt);
/****************************************************************************
* Name: load_module
*
* Description:
* Load a module into memory, bind it to an exported symbol take, and
* prep the module for execution.
*
* 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 load_module(FAR struct binary_s *bin, FAR const char *filename,
FAR const struct symtab_s *exports, int nexports);
/****************************************************************************
* Name: unload_module
*
* Description:
* Unload a (non-executing) module from memory. If the module has
* been started (via exec_module) and has not exited, calling this will
* be fatal.
*
* However, this function must be called after the module exist. How
* this is done is up to your logic. Perhaps you register it to be
* called by on_exit()?
*
* 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 unload_module(FAR struct binary_s *bin);
/****************************************************************************
* Name: exec_module
*
* Description:
* Execute a module that has been loaded into memory by load_module().
*
* 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 exec_module(FAR struct binary_s *binp,
FAR const char *filename, FAR char * const *argv,
FAR char * const *envp,
FAR const posix_spawn_file_actions_t *actions,
FAR const posix_spawnattr_t *attr,
bool spawn);
/****************************************************************************
* Name: exec
*
* Description:
* This is a convenience function that wraps load_ and exec_module into
* one call. If CONFIG_BINFMT_LOADABLE is defined, this function will
* schedule to unload the module when task exits.
*
* This non-standard, NuttX function is similar to execv() and
* posix_spawn() but differs in the following ways;
*
* - Unlike execv() and posix_spawn() this function accepts symbol table
* information as input parameters. This means that the symbol table
* used to link the application prior to execution is provided by the
* caller, not by the system.
* - Unlike execv(), this function always returns.
*
* This non-standard interface is included as a official NuttX API only
* because it is needed in certain build modes: exec() is probably the
* only want to load programs in the PROTECTED mode. Other file execution
* APIs rely on a symbol table provided by the OS. In the PROTECTED build
* mode, the OS cannot provide any meaningful symbolic information for
* execution of code in the user-space blob so that is the exec() function
* is really needed in that build case
*
* The interface is available in the FLAT build mode although it is not
* really necessary in that case. It is currently used by some example
* code under the apps/ that that generate their own symbol tables for
* linking test programs. So although it is not necessary, it can still
* be useful.
*
* The interface would be completely useless and will not be supported in
* in the KERNEL build mode where the contrary is true: An application
* process cannot provide any meaning symbolic information for use in
* linking a different process.
*
* NOTE: This function is flawed and useless without CONFIG_BINFMT_LOADABLE
* because without that features there is then no mechanism to unload the
* module once it exits.
*
* Input Parameters:
* filename - The path to the program to be executed. If
* CONFIG_LIBC_ENVPATH is defined in the configuration, then
* this may be a relative path from the current working
* directory. Otherwise, path must be the absolute path to the
* program.
* argv - A pointer to an array of string arguments. The end of the
* array is indicated with a NULL entry.
* envp - An array of character pointers to null-terminated strings
* that provide the environment for the new process image.
* The environment array is terminated by a null pointer.
* exports - The address of the start of the caller-provided symbol
* table. This symbol table contains the addresses of symbols
* exported by the caller and made available for linking the
* module into the system.
* nexports - The number of symbols in the exports table.
*
* Returned Value:
* It returns the PID of the exec'ed module. On failure, it returns
* the negative errno value appropriately.
*
****************************************************************************/
int exec(FAR const char *filename, FAR char * const *argv,
FAR char * const *envp, FAR const struct symtab_s *exports,
int nexports);
/****************************************************************************
* Name: exec_spawn
*
* Description:
* exec() configurable version, delivery the spawn attribute if this
* process has special customization.
*
* Input Parameters:
* filename - The path to the program to be executed. If
* CONFIG_LIBC_ENVPATH is defined in the configuration, then
* this may be a relative path from the current working
* directory. Otherwise, path must be the absolute path to the
* program.
* argv - A pointer to an array of string arguments. The end of the
* array is indicated with a NULL entry.
* envp - A pointer to an array of environment strings. Terminated with
* a NULL entry.
* exports - The address of the start of the caller-provided symbol
* table. This symbol table contains the addresses of symbols
* exported by the caller and made available for linking the
* module into the system.
* nexports - The number of symbols in the exports table.
* actions - The spawn file actions
* attr - The spawn attributes.
*
* Returned Value:
* This is an end-user function, so it follows the normal convention:
* It returns the PID of the exec'ed module. On failure, it returns
* -1 (ERROR) and sets errno appropriately.
*
****************************************************************************/
int exec_spawn(FAR const char *filename, FAR char * const *argv,
FAR char * const *envp, FAR const struct symtab_s *exports,
int nexports, FAR const posix_spawn_file_actions_t *actions,
FAR const posix_spawnattr_t *attr);
/****************************************************************************
* Name: binfmt_exit
*
* Description:
* This function may be called when a tasked loaded into RAM exits.
* This function will unload the module when the task exits and reclaim
* all resources used by the module.
*
* Input Parameters:
* bin - This structure must have been allocated with kmm_malloc() and must
* persist until the task unloads
*
* 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.
*
****************************************************************************/
#ifdef CONFIG_BINFMT_LOADABLE
int binfmt_exit(FAR struct binary_s *bin);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __INCLUDE_NUTTX_BINFMT_BINFMT_H */