Contiki 3.x
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  */
168  const struct elfloader_output_ops *ops;
169 };
170 
171 /**
172  * \brief Allocate a new segment
173  * \param output 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  */
182  unsigned int type, int size);
183 
184 /**
185  * \brief Start writing to a new segment
186  * \param output The output object
187  * \param type Type of segment
188  * \param addr Address of segment from elfloader_allocate_segment
189  * \param size Size of segment in bytes
190  * \return Returns ELFLOADER_OK if successful, otherwise an error code
191  *
192  */
194  unsigned int type, void *addr, int size);
195 
196 /**
197  * \brief Mark end of segment
198  * \param output The output object
199  * \return Zero if successful
200  */
202 
203 /**
204  * \brief Write data to a segment
205  * \param output The output object
206  * \param buf Data to be written
207  * \param len Length of data
208  * \return The number of bytes actually written, or negative if failed.
209  */
210 int elfloader_write_segment(struct elfloader_output *output, const char *buf,
211  unsigned int len);
212 
213 /**
214  * \brief Get the current offset in the file where the next data will
215  * be written.
216  * \param output The output object
217  * \return The current offset.
218  */
219 unsigned int elfloader_segment_offset(struct elfloader_output *output);
220 
221 #define elfloader_output_alloc_segment(output, type, size) \
222 ((output)->ops->allocate_segment(output, type, size))
223 
224 #define elfloader_output_start_segment(output, type, addr, size) \
225 ((output)->ops->start_segment(output, type, addr, size))
226 
227 #define elfloader_output_end_segment(output) \
228 ((output)->ops->end_segment(output))
229 
230 #define elfloader_output_write_segment(output, buf, len) \
231 ((output)->ops->write_segment(output, buf, len))
232 
233 #define elfloader_output_segment_offset(output) \
234 ((output)->ops->segment_offset(output))
235 
236 
237 struct elfloader_output_ops {
238  void * (*allocate_segment)(struct elfloader_output *output,
239  unsigned int type, int size);
240  int (*start_segment)(struct elfloader_output *output,
241  unsigned int type, void *addr, int size);
242  int (*end_segment)(struct elfloader_output *output);
243  int (*write_segment)(struct elfloader_output *output, const char *buf,
244  unsigned int len);
245  unsigned int (*segment_offset)(struct elfloader_output *output);
246 };
247 
248 
249 /**
250  * elfloader initialization function.
251  *
252  * This function should be called at boot up to initilize the elfloader.
253  */
254 void elfloader_init(void);
255 
256 /**
257  * \brief Load and relocate an ELF file.
258  * \param input_fd Input object defining how to read from the ELF file
259  * \param output Output object defining how to create and write to seegments.
260  * \return ELFLOADER_OK if loading and relocation worked.
261  * Otherwise an error value.
262  *
263  * If the function is able to load the ELF file, a pointer
264  * to the process structure in the model is stored in the
265  * elfloader_loaded_process variable.
266  *
267  */
268 int elfloader_load(int input_fd, struct elfloader_output *output);
269 
270 /**
271  * A pointer to the processes loaded with elfloader_load().
272  */
273 extern struct process **elfloader_autostart_processes;
274 
275 /**
276  * If elfloader_load() could not find a specific symbol, it is copied
277  * into this array.
278  */
279 extern char elfloader_unknown[30];
280 
281 #ifdef ELFLOADER_CONF_DATAMEMORY_SIZE
282 #define ELFLOADER_DATAMEMORY_SIZE ELFLOADER_CONF_DATAMEMORY_SIZE
283 #else
284 #define ELFLOADER_DATAMEMORY_SIZE 0x100
285 #endif
286 
287 #ifdef ELFLOADER_CONF_TEXTMEMORY_SIZE
288 #define ELFLOADER_TEXTMEMORY_SIZE ELFLOADER_CONF_TEXTMEMORY_SIZE
289 #else
290 #define ELFLOADER_TEXTMEMORY_SIZE 0x100
291 #endif
292 
293 typedef unsigned long elf32_word;
294 typedef signed long elf32_sword;
295 typedef unsigned short elf32_half;
296 typedef unsigned long elf32_off;
297 typedef unsigned long elf32_addr;
298 
299 struct elf32_rela {
300  elf32_addr r_offset; /* Location to be relocated. */
301  elf32_word r_info; /* Relocation type and symbol index. */
302  elf32_sword r_addend; /* Addend. */
303 };
304 
305 
306 #endif /* ELFLOADER_H_ */
307 
308 /** @} */
309 /** @} */
char elfloader_unknown[30]
If elfloader_load() could not find a specific symbol, it is copied into this array.
Definition: tcp_loader2.c:105
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
struct process ** elfloader_autostart_processes
A pointer to the processes loaded with elfloader_load().
int elfloader_end_segment(struct elfloader_output *output)
Mark end of segment.
unsigned int elfloader_segment_offset(struct elfloader_output *output)
Get the current offset in the file where the next data will be written.
elfloader output object
void elfloader_init(void)
elfloader initialization function.
int elfloader_load(int input_fd, struct elfloader_output *output)
Load and relocate an ELF file.
int elfloader_start_segment(struct elfloader_output *output, unsigned int type, void *addr, int size)
Start writing to a new segment.
int elfloader_write_segment(struct elfloader_output *output, const char *buf, unsigned int len)
Write data to a segment.
static uint8_t output(const uip_lladdr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1275
void * elfloader_allocate_segment(struct elfloader_output *output, unsigned int type, int size)
Allocate a new segment.
CFS header file.