Contiki 2.5
elfloader-otf.h
Go to the documentation of this file.
1 /**
2  * \addtogroup loader
3  * @{
4  */
5 
6 /**
7  * \defgroup elfloader The Contiki ELF loader
8  *
9  * The Contiki ELF loader links, relocates, and loads ELF
10  * (Executable Linkable Format) object files into a running Contiki
11  * system.
12  *
13  * ELF is a standard format for relocatable object code and executable
14  * files. ELF is the standard program format for Linux, Solaris, and
15  * other operating systems.
16  *
17  * An ELF file contains either a standalone executable program or a
18  * program module. The file contains both the program code, the
19  * program data, as well as information about how to link, relocate,
20  * and load the program into a running system.
21  *
22  * The ELF file is composed of a set of sections. The sections contain
23  * program code, data, or relocation information, but can also contain
24  * debugging information.
25  *
26  * To link and relocate an ELF file, the Contiki ELF loader first
27  * parses the ELF file structure to find the appropriate ELF
28  * sections. It then allocates memory for the program code and data in
29  * ROM and RAM, respectively. After allocating memory, the Contiki ELF
30  * loader starts relocating the code found in the ELF file.
31  *
32  * @{
33  */
34 
35 /**
36  * \file
37  * Header file for the Contiki ELF loader.
38  * \author
39  * Adam Dunkels <adam@sics.se>
40  * Simon Berg <ksb@users.sourceforge.net>
41  *
42  */
43 
44 /*
45  * Copyright (c) 2005, Swedish Institute of Computer Science
46  * Copyright (c) 2007, Simon Berg
47  * All rights reserved.
48  *
49  * Redistribution and use in source and binary forms, with or without
50  * modification, are permitted provided that the following conditions
51  * are met:
52  * 1. Redistributions of source code must retain the above copyright
53  * notice, this list of conditions and the following disclaimer.
54  * 2. Redistributions in binary form must reproduce the above copyright
55  * notice, this list of conditions and the following disclaimer in the
56  * documentation and/or other materials provided with the distribution.
57  * 3. Neither the name of the Institute nor the names of its contributors
58  * may be used to endorse or promote products derived from this software
59  * without specific prior written permission.
60  *
61  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
62  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
65  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71  * SUCH DAMAGE.
72  *
73  * This file is part of the Contiki operating system.
74  *
75  */
76 
77 #ifndef __ELFLOADER_H__
78 #define __ELFLOADER_H__
79 
80 #include "cfs/cfs.h"
81 
82 /**
83  * Return value from elfloader_load() indicating that loading worked.
84  */
85 #define ELFLOADER_OK 0
86 /**
87  * Return value from elfloader_load() indicating that the ELF file had
88  * a bad header.
89  */
90 #define ELFLOADER_BAD_ELF_HEADER 1
91 /**
92  * Return value from elfloader_load() indicating that no symbol table
93  * could be find in the ELF file.
94  */
95 #define ELFLOADER_NO_SYMTAB 2
96 /**
97  * Return value from elfloader_load() indicating that no string table
98  * could be find in the ELF file.
99  */
100 #define ELFLOADER_NO_STRTAB 3
101 /**
102  * Return value from elfloader_load() indicating that the size of the
103  * .text segment was zero.
104  */
105 #define ELFLOADER_NO_TEXT 4
106 /**
107  * Return value from elfloader_load() indicating that a symbol
108  * specific symbol could not be found.
109  *
110  * If this value is returned from elfloader_load(), the symbol has
111  * been copied into the elfloader_unknown[] array.
112  */
113 #define ELFLOADER_SYMBOL_NOT_FOUND 5
114 /**
115  * Return value from elfloader_load() indicating that one of the
116  * required segments (.data, .bss, or .text) could not be found.
117  */
118 #define ELFLOADER_SEGMENT_NOT_FOUND 6
119 /**
120  * Return value from elfloader_load() indicating that no starting
121  * point could be found in the loaded module.
122  */
123 #define ELFLOADER_NO_STARTPOINT 7
124 
125 /**
126  * Return value from elfloader_load() indicating that the ELF file contained
127  * a relocation type that the implementation can't handle.
128  */
129 #define ELFLOADER_UNHANDLED_RELOC 8
130 
131 /**
132  * Return value from elfloader_load() indicating that the offset for
133  * a relative addressing mode was too big.
134  */
135 #define ELFLOADER_OUTOF_RANGE 9
136 
137 /**
138  * Return value from elfloader_load() indicating that the relocations
139  * where not sorted by offset
140  */
141 #define ELFLOADER_RELOC_NOT_SORTED 10
142 
143 /**
144  * Return value from elfloader_load() indicating that reading from the
145  * ELF file failed in some way.
146  */
147 #define ELFLOADER_INPUT_ERROR 11
148 
149 /**
150  * Return value from elfloader_load() indicating that writing to a segment
151  * failed.
152  */
153 #define ELFLOADER_OUTPUT_ERROR 12
154 
155 
156 #define ELFLOADER_SEG_TEXT 1
157 #define ELFLOADER_SEG_RODATA 2
158 #define ELFLOADER_SEG_DATA 3
159 #define ELFLOADER_SEG_BSS 4
160 
161 /**
162  * elfloader output object
163  *
164  * This object defines methods (callbacks) for writing the segments to memory.
165  * It can be extended by the user to include any necessary state.
166  */
167 
169  const struct elfloader_output_ops *ops;
170 };
171 /**
172  * \brief Allocate a new segment
173  * \param input The output object
174  * \param type Type of segment
175  * \param size Size of segment in bytes
176  * \return A pointer to the start of the segment.
177  *
178  * The returned address doesn't need to correspond to any real memory,
179  * since it's only used for calculating the relocations.
180  */
181 
182 void *elfloader_allocate_segment(struct elfloader_output *output,
183  unsigned int type, int size);
184 
185 /**
186  * \brief Start writing to a new segment
187  * \param input The output object
188  * \param type Type of segment
189  * \param addr Address of segment from elfloader_allocate_segment
190  * \param size Size of segment in bytes
191  * \return Returns ELFLOADER_OK if successful, otherwise an error code
192  *
193  */
194 
195 int elfloader_start_segment(struct elfloader_output *output,
196  unsigned int type, void *addr, int size);
197 /**
198  * \brief Mark end of segment
199  * \param input The output object
200  * \return Zero if successful
201  */
202 
203 int elfloader_end_segment(struct elfloader_output *output);
204 
205 /**
206  * \brief Write data to a segment
207  * \param input The output object
208  * \param buf Data to be written
209  * \param len Length of data
210  * \return The number of bytes actually written, or negative if failed.
211  */
212 
213 int elfloader_write_segment(struct elfloader_output *output, const char *buf,
214  unsigned int len);
215 
216 /**
217  * \brief Get the current offset in the file where the next data will
218  * be written.
219  * \param input The output object
220  * \return The current offset.
221  */
222 
223 unsigned int elfloader_segment_offset(struct elfloader_output *output);
224 
225 #define elfloader_output_alloc_segment(output, type, size) \
226 ((output)->ops->allocate_segment(output, type, size))
227 
228 #define elfloader_output_start_segment(output, type, addr, size) \
229 ((output)->ops->start_segment(output, type, addr, size))
230 
231 #define elfloader_output_end_segment(output) \
232 ((output)->ops->end_segment(output))
233 
234 #define elfloader_output_write_segment(output, buf, len) \
235 ((output)->ops->write_segment(output, buf, len))
236 
237 #define elfloader_output_segment_offset(output) \
238 ((output)->ops->segment_offset(output))
239 
240 
241 struct elfloader_output_ops {
242  void * (*allocate_segment)(struct elfloader_output *output,
243  unsigned int type, int size);
244  int (*start_segment)(struct elfloader_output *output,
245  unsigned int type, void *addr, int size);
246  int (*end_segment)(struct elfloader_output *output);
247  int (*write_segment)(struct elfloader_output *output, const char *buf,
248  unsigned int len);
249  unsigned int (*segment_offset)(struct elfloader_output *output);
250 };
251 
252 
253 /**
254  * elfloader initialization function.
255  *
256  * This function should be called at boot up to initilize the elfloader.
257  */
258 void elfloader_init(void);
259 
260 /**
261  * \brief Load and relocate an ELF file.
262  * \param input Input object defining how to read from the ELF file
263  * \param output Output object defining how to create and write to seegments.
264  * \return ELFLOADER_OK if loading and relocation worked.
265  * Otherwise an error value.
266  *
267  * If the function is able to load the ELF file, a pointer
268  * to the process structure in the model is stored in the
269  * elfloader_loaded_process variable.
270  *
271  */
272 int elfloader_load(int input_fd,
273  struct elfloader_output *output);
274 
275 /**
276  * A pointer to the processes loaded with elfloader_load().
277  */
278 extern struct process **elfloader_autostart_processes;
279 
280 /**
281  * If elfloader_load() could not find a specific symbol, it is copied
282  * into this array.
283  */
284 extern char elfloader_unknown[30];
285 
286 #ifdef ELFLOADER_CONF_DATAMEMORY_SIZE
287 #define ELFLOADER_DATAMEMORY_SIZE ELFLOADER_CONF_DATAMEMORY_SIZE
288 #else
289 #define ELFLOADER_DATAMEMORY_SIZE 0x100
290 #endif
291 
292 #ifdef ELFLOADER_CONF_TEXTMEMORY_SIZE
293 #define ELFLOADER_TEXTMEMORY_SIZE ELFLOADER_CONF_TEXTMEMORY_SIZE
294 #else
295 #define ELFLOADER_TEXTMEMORY_SIZE 0x100
296 #endif
297 
298 typedef unsigned long elf32_word;
299 typedef signed long elf32_sword;
300 typedef unsigned short elf32_half;
301 typedef unsigned long elf32_off;
302 typedef unsigned long elf32_addr;
303 
304 struct elf32_rela {
305  elf32_addr r_offset; /* Location to be relocated. */
306  elf32_word r_info; /* Relocation type and symbol index. */
307  elf32_sword r_addend; /* Addend. */
308 };
309 
310 
311 #endif /* __ELFLOADER_H__ */
312 
313 /** @} */
314 /** @} */