Contiki-Inga 3.x
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  */
74 #ifndef ELFLOADER_H_
75 #define ELFLOADER_H_
76 
77 #include "cfs/cfs.h"
78 
79 /**
80  * Return value from elfloader_load() indicating that loading worked.
81  */
82 #define ELFLOADER_OK 0
83 /**
84  * Return value from elfloader_load() indicating that the ELF file had
85  * a bad header.
86  */
87 #define ELFLOADER_BAD_ELF_HEADER 1
88 /**
89  * Return value from elfloader_load() indicating that no symbol table
90  * could be found in the ELF file.
91  */
92 #define ELFLOADER_NO_SYMTAB 2
93 /**
94  * Return value from elfloader_load() indicating that no string table
95  * could be found in the ELF file.
96  */
97 #define ELFLOADER_NO_STRTAB 3
98 /**
99  * Return value from elfloader_load() indicating that the size of the
100  * .text segment was zero.
101  */
102 #define ELFLOADER_NO_TEXT 4
103 /**
104  * Return value from elfloader_load() indicating that a symbol
105  * specific symbol could not be found.
106  *
107  * If this value is returned from elfloader_load(), the symbol has
108  * been copied into the elfloader_unknown[] array.
109  */
110 #define ELFLOADER_SYMBOL_NOT_FOUND 5
111 /**
112  * Return value from elfloader_load() indicating that one of the
113  * required segments (.data, .bss, or .text) could not be found.
114  */
115 #define ELFLOADER_SEGMENT_NOT_FOUND 6
116 /**
117  * Return value from elfloader_load() indicating that no starting
118  * point could be found in the loaded module.
119  */
120 #define ELFLOADER_NO_STARTPOINT 7
121 
122 /**
123  * elfloader initialization function.
124  *
125  * This function should be called at boot up to initialize the elfloader.
126  */
127 void elfloader_init(void);
128 
129 /**
130  * \brief Load and relocate an ELF file.
131  * \param fd An open CFS file descriptor.
132  * \return ELFLOADER_OK if loading and relocation worked.
133  * Otherwise an error value.
134  *
135  * This function loads and relocates an ELF file. The ELF
136  * file must have been opened with cfs_open() prior to
137  * calling this function.
138  *
139  * If the function is able to load the ELF file, a pointer
140  * to the process structure in the model is stored in the
141  * elfloader_loaded_process variable.
142  *
143  * \note This function modifies the ELF file opened with cfs_open()!
144  * If the contents of the file is required to be intact,
145  * the file must be backed up first.
146  *
147  */
148 int elfloader_load(int fd);
149 
150 /**
151  * A pointer to the processes loaded with elfloader_load().
152  */
153 extern struct process * const * elfloader_autostart_processes;
154 
155 /**
156  * If elfloader_load() could not find a specific symbol, it is copied
157  * into this array.
158  */
159 extern char elfloader_unknown[30];
160 
161 #ifndef ELFLOADER_DATAMEMORY_SIZE
162 #ifdef ELFLOADER_CONF_DATAMEMORY_SIZE
163 #define ELFLOADER_DATAMEMORY_SIZE ELFLOADER_CONF_DATAMEMORY_SIZE
164 #else
165 #define ELFLOADER_DATAMEMORY_SIZE 0x100
166 #endif
167 #endif /* ELFLOADER_DATAMEMORY_SIZE */
168 
169 #ifndef ELFLOADER_TEXTMEMORY_SIZE
170 #ifdef ELFLOADER_CONF_TEXTMEMORY_SIZE
171 #define ELFLOADER_TEXTMEMORY_SIZE ELFLOADER_CONF_TEXTMEMORY_SIZE
172 #else
173 #define ELFLOADER_TEXTMEMORY_SIZE 0x100
174 #endif
175 #endif /* ELFLOADER_TEXTMEMORY_SIZE */
176 
177 typedef unsigned long elf32_word;
178 typedef signed long elf32_sword;
179 typedef unsigned short elf32_half;
180 typedef unsigned long elf32_off;
181 typedef unsigned long elf32_addr;
182 
183 struct elf32_rela {
184  elf32_addr r_offset; /* Location to be relocated. */
185  elf32_word r_info; /* Relocation type and symbol index. */
186  elf32_sword r_addend; /* Addend. */
187 };
188 
189 
190 #endif /* ELFLOADER_H_ */
191 
192 /** @} */
193 /** @} */