Contiki 2.5
elfloader.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  *
41  */
42 
43 /*
44  * Copyright (c) 2005, Swedish Institute of Computer Science
45  * All rights reserved.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  * notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in the
54  * documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the Institute nor the names of its contributors
56  * may be used to endorse or promote products derived from this software
57  * without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  * This file is part of the Contiki operating system.
72  *
73  * @(#)$Id: elfloader.h,v 1.4 2010/04/26 14:02:07 fros4943 Exp $
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 /** @} */