Contiki 3.x
elfloader.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Header file for the Contiki ELF loader.
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  *
39  */
40 
41 /**
42  * \addtogroup loader
43  * @{
44  */
45 
46 /**
47  * \defgroup elfloader The Contiki ELF loader
48  *
49  * The Contiki ELF loader links, relocates, and loads ELF
50  * (Executable Linkable Format) object files into a running Contiki
51  * system.
52  *
53  * ELF is a standard format for relocatable object code and executable
54  * files. ELF is the standard program format for Linux, Solaris, and
55  * other operating systems.
56  *
57  * An ELF file contains either a standalone executable program or a
58  * program module. The file contains both the program code, the
59  * program data, as well as information about how to link, relocate,
60  * and load the program into a running system.
61  *
62  * The ELF file is composed of a set of sections. The sections contain
63  * program code, data, or relocation information, but can also contain
64  * debugging information.
65  *
66  * To link and relocate an ELF file, the Contiki ELF loader first
67  * parses the ELF file structure to find the appropriate ELF
68  * sections. It then allocates memory for the program code and data in
69  * ROM and RAM, respectively. After allocating memory, the Contiki ELF
70  * loader starts relocating the code found in the ELF file.
71  *
72  * @{
73  */
74 
75 #ifndef ELFLOADER_H_
76 #define ELFLOADER_H_
77 
78 #include "cfs/cfs.h"
79 
80 /**
81  * Return value from elfloader_load() indicating that loading worked.
82  */
83 #define ELFLOADER_OK 0
84 /**
85  * Return value from elfloader_load() indicating that the ELF file had
86  * a bad header.
87  */
88 #define ELFLOADER_BAD_ELF_HEADER 1
89 /**
90  * Return value from elfloader_load() indicating that no symbol table
91  * could be found in the ELF file.
92  */
93 #define ELFLOADER_NO_SYMTAB 2
94 /**
95  * Return value from elfloader_load() indicating that no string table
96  * could be found in the ELF file.
97  */
98 #define ELFLOADER_NO_STRTAB 3
99 /**
100  * Return value from elfloader_load() indicating that the size of the
101  * .text segment was zero.
102  */
103 #define ELFLOADER_NO_TEXT 4
104 /**
105  * Return value from elfloader_load() indicating that a symbol
106  * specific symbol could not be found.
107  *
108  * If this value is returned from elfloader_load(), the symbol has
109  * been copied into the elfloader_unknown[] array.
110  */
111 #define ELFLOADER_SYMBOL_NOT_FOUND 5
112 /**
113  * Return value from elfloader_load() indicating that one of the
114  * required segments (.data, .bss, or .text) could not be found.
115  */
116 #define ELFLOADER_SEGMENT_NOT_FOUND 6
117 /**
118  * Return value from elfloader_load() indicating that no starting
119  * point could be found in the loaded module.
120  */
121 #define ELFLOADER_NO_STARTPOINT 7
122 
123 /**
124  * elfloader initialization function.
125  *
126  * This function should be called at boot up to initialize the elfloader.
127  */
128 void elfloader_init(void);
129 
130 /**
131  * \brief Load and relocate an ELF file.
132  * \param fd An open CFS file descriptor.
133  * \return ELFLOADER_OK if loading and relocation worked.
134  * Otherwise an error value.
135  *
136  * This function loads and relocates an ELF file. The ELF
137  * file must have been opened with cfs_open() prior to
138  * calling this function.
139  *
140  * If the function is able to load the ELF file, a pointer
141  * to the process structure in the model is stored in the
142  * elfloader_loaded_process variable.
143  *
144  * \note This function modifies the ELF file opened with cfs_open()!
145  * If the contents of the file is required to be intact,
146  * the file must be backed up first.
147  *
148  */
149 int elfloader_load(int fd);
150 
151 /**
152  * A pointer to the processes loaded with elfloader_load().
153  */
154 extern struct process * const * elfloader_autostart_processes;
155 
156 /**
157  * If elfloader_load() could not find a specific symbol, it is copied
158  * into this array.
159  */
160 extern char elfloader_unknown[30];
161 
162 #ifndef ELFLOADER_DATAMEMORY_SIZE
163 #ifdef ELFLOADER_CONF_DATAMEMORY_SIZE
164 #define ELFLOADER_DATAMEMORY_SIZE ELFLOADER_CONF_DATAMEMORY_SIZE
165 #else
166 #define ELFLOADER_DATAMEMORY_SIZE 0x100
167 #endif
168 #endif /* ELFLOADER_DATAMEMORY_SIZE */
169 
170 #ifndef ELFLOADER_TEXTMEMORY_SIZE
171 #ifdef ELFLOADER_CONF_TEXTMEMORY_SIZE
172 #define ELFLOADER_TEXTMEMORY_SIZE ELFLOADER_CONF_TEXTMEMORY_SIZE
173 #else
174 #define ELFLOADER_TEXTMEMORY_SIZE 0x100
175 #endif
176 #endif /* ELFLOADER_TEXTMEMORY_SIZE */
177 
178 typedef unsigned long elf32_word;
179 typedef signed long elf32_sword;
180 typedef unsigned short elf32_half;
181 typedef unsigned long elf32_off;
182 typedef unsigned long elf32_addr;
183 
184 struct elf32_rela {
185  elf32_addr r_offset; /* Location to be relocated. */
186  elf32_word r_info; /* Relocation type and symbol index. */
187  elf32_sword r_addend; /* Addend. */
188 };
189 
190 
191 #endif /* ELFLOADER_H_ */
192 
193 /** @} */
194 /** @} */
void elfloader_init(void)
elfloader initialization function.
Definition: elfloader.c:317
int elfloader_load(int fd)
Load and relocate an ELF file.
Definition: elfloader.c:339
cle_scratch elfloader_unknown
If elfloader_load() could not find a specific symbol, it is copied into this array.
Definition: tcp_loader2.c:105
struct process *const * elfloader_autostart_processes
A pointer to the processes loaded with elfloader_load().
Definition: elfloader.c:136
CFS header file.