1 |
2 |
drasko |
/*
|
2 |
|
|
* Australian Public Licence B (OZPLB)
|
3 |
|
|
*
|
4 |
|
|
* Version 1-0
|
5 |
|
|
*
|
6 |
|
|
* Copyright (c) 1999-2004 University of New South Wales
|
7 |
|
|
*
|
8 |
|
|
* All rights reserved.
|
9 |
|
|
*
|
10 |
|
|
* Developed by: Operating Systems and Distributed Systems Group (DiSy)
|
11 |
|
|
* University of New South Wales
|
12 |
|
|
* http://www.disy.cse.unsw.edu.au
|
13 |
|
|
*
|
14 |
|
|
* Permission is granted by University of New South Wales, free of charge, to
|
15 |
|
|
* any person obtaining a copy of this software and any associated
|
16 |
|
|
* documentation files (the "Software") to deal with the Software without
|
17 |
|
|
* restriction, including (without limitation) the rights to use, copy,
|
18 |
|
|
* modify, adapt, merge, publish, distribute, communicate to the public,
|
19 |
|
|
* sublicense, and/or sell, lend or rent out copies of the Software, and
|
20 |
|
|
* to permit persons to whom the Software is furnished to do so, subject
|
21 |
|
|
* to the following conditions:
|
22 |
|
|
*
|
23 |
|
|
* * Redistributions of source code must retain the above copyright
|
24 |
|
|
* notice, this list of conditions and the following disclaimers.
|
25 |
|
|
*
|
26 |
|
|
* * Redistributions in binary form must reproduce the above
|
27 |
|
|
* copyright notice, this list of conditions and the following
|
28 |
|
|
* disclaimers in the documentation and/or other materials provided
|
29 |
|
|
* with the distribution.
|
30 |
|
|
*
|
31 |
|
|
* * Neither the name of University of New South Wales, nor the names of its
|
32 |
|
|
* contributors, may be used to endorse or promote products derived
|
33 |
|
|
* from this Software without specific prior written permission.
|
34 |
|
|
*
|
35 |
|
|
* EXCEPT AS EXPRESSLY STATED IN THIS LICENCE AND TO THE FULL EXTENT
|
36 |
|
|
* PERMITTED BY APPLICABLE LAW, THE SOFTWARE IS PROVIDED "AS-IS", AND
|
37 |
|
|
* NATIONAL ICT AUSTRALIA AND ITS CONTRIBUTORS MAKE NO REPRESENTATIONS,
|
38 |
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
|
39 |
|
|
* BUT NOT LIMITED TO ANY REPRESENTATIONS, WARRANTIES OR CONDITIONS
|
40 |
|
|
* REGARDING THE CONTENTS OR ACCURACY OF THE SOFTWARE, OR OF TITLE,
|
41 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT,
|
42 |
|
|
* THE ABSENCE OF LATENT OR OTHER DEFECTS, OR THE PRESENCE OR ABSENCE OF
|
43 |
|
|
* ERRORS, WHETHER OR NOT DISCOVERABLE.
|
44 |
|
|
*
|
45 |
|
|
* TO THE FULL EXTENT PERMITTED BY APPLICABLE LAW, IN NO EVENT SHALL
|
46 |
|
|
* NATIONAL ICT AUSTRALIA OR ITS CONTRIBUTORS BE LIABLE ON ANY LEGAL
|
47 |
|
|
* THEORY (INCLUDING, WITHOUT LIMITATION, IN AN ACTION OF CONTRACT,
|
48 |
|
|
* NEGLIGENCE OR OTHERWISE) FOR ANY CLAIM, LOSS, DAMAGES OR OTHER
|
49 |
|
|
* LIABILITY, INCLUDING (WITHOUT LIMITATION) LOSS OF PRODUCTION OR
|
50 |
|
|
* OPERATION TIME, LOSS, DAMAGE OR CORRUPTION OF DATA OR RECORDS; OR LOSS
|
51 |
|
|
* OF ANTICIPATED SAVINGS, OPPORTUNITY, REVENUE, PROFIT OR GOODWILL, OR
|
52 |
|
|
* OTHER ECONOMIC LOSS; OR ANY SPECIAL, INCIDENTAL, INDIRECT,
|
53 |
|
|
* CONSEQUENTIAL, PUNITIVE OR EXEMPLARY DAMAGES, ARISING OUT OF OR IN
|
54 |
|
|
* CONNECTION WITH THIS LICENCE, THE SOFTWARE OR THE USE OF OR OTHER
|
55 |
|
|
* DEALINGS WITH THE SOFTWARE, EVEN IF NATIONAL ICT AUSTRALIA OR ITS
|
56 |
|
|
* CONTRIBUTORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH CLAIM, LOSS,
|
57 |
|
|
* DAMAGES OR OTHER LIABILITY.
|
58 |
|
|
*
|
59 |
|
|
* If applicable legislation implies representations, warranties, or
|
60 |
|
|
* conditions, or imposes obligations or liability on University of New South
|
61 |
|
|
* Wales or one of its contributors in respect of the Software that
|
62 |
|
|
* cannot be wholly or partly excluded, restricted or modified, the
|
63 |
|
|
* liability of University of New South Wales or the contributor is limited, to
|
64 |
|
|
* the full extent permitted by the applicable legislation, at its
|
65 |
|
|
* option, to:
|
66 |
|
|
* a. in the case of goods, any one or more of the following:
|
67 |
|
|
* i. the replacement of the goods or the supply of equivalent goods;
|
68 |
|
|
* ii. the repair of the goods;
|
69 |
|
|
* iii. the payment of the cost of replacing the goods or of acquiring
|
70 |
|
|
* equivalent goods;
|
71 |
|
|
* iv. the payment of the cost of having the goods repaired; or
|
72 |
|
|
* b. in the case of services:
|
73 |
|
|
* i. the supplying of the services again; or
|
74 |
|
|
* ii. the payment of the cost of having the services supplied again.
|
75 |
|
|
*
|
76 |
|
|
* The construction, validity and performance of this licence is governed
|
77 |
|
|
* by the laws in force in New South Wales, Australia.
|
78 |
|
|
*/
|
79 |
|
|
|
80 |
|
|
/*
|
81 |
|
|
Authors: Luke Deller, Ben Leslie
|
82 |
|
|
Created: 24/Sep/1999
|
83 |
|
|
*/
|
84 |
|
|
|
85 |
|
|
/**
|
86 |
|
|
\file
|
87 |
|
|
|
88 |
|
|
\brief Generic ELF library
|
89 |
|
|
|
90 |
|
|
The ELF library is designed to make the task of parsing and getting information
|
91 |
|
|
out of an ELF file easier.
|
92 |
|
|
|
93 |
|
|
It provides function to obtain the various different fields in the ELF header, and
|
94 |
|
|
the program and segment information.
|
95 |
|
|
|
96 |
|
|
Also importantly, it provides a function elf_loadFile which will load a given
|
97 |
|
|
ELF file into memory.
|
98 |
|
|
|
99 |
|
|
*/
|
100 |
|
|
|
101 |
|
|
#ifndef __ELF_ELF_H__
|
102 |
|
|
#define __ELF_ELF_H__
|
103 |
|
|
|
104 |
|
|
#include <stdint.h>
|
105 |
|
|
#include <stdbool.h>
|
106 |
|
|
#include <stdio.h>
|
107 |
|
|
|
108 |
|
|
#include "elf32.h"
|
109 |
|
|
#include "elf64.h"
|
110 |
|
|
|
111 |
|
|
/*
|
112 |
|
|
* constants for Elf32_Phdr.p_flags
|
113 |
|
|
*/
|
114 |
|
|
#define PF_X 1 /* readable segment */
|
115 |
|
|
#define PF_W 2 /* writeable segment */
|
116 |
|
|
#define PF_R 4 /* executable segment */
|
117 |
|
|
|
118 |
|
|
/*
|
119 |
|
|
* constants for indexing into Elf64_Header_t.e_ident
|
120 |
|
|
*/
|
121 |
|
|
#define EI_MAG0 0
|
122 |
|
|
#define EI_MAG1 1
|
123 |
|
|
#define EI_MAG2 2
|
124 |
|
|
#define EI_MAG3 3
|
125 |
|
|
#define EI_CLASS 4
|
126 |
|
|
#define EI_DATA 5
|
127 |
|
|
#define EI_VERSION 6
|
128 |
|
|
|
129 |
|
|
#define ELFMAG0 '\177'
|
130 |
|
|
#define ELFMAG1 'E'
|
131 |
|
|
#define ELFMAG2 'L'
|
132 |
|
|
#define ELFMAG3 'F'
|
133 |
|
|
|
134 |
|
|
#define ELFCLASS32 1
|
135 |
|
|
#define ELFCLASS64 2
|
136 |
|
|
|
137 |
|
|
#define PT_NULL 0
|
138 |
|
|
#define PT_LOAD 1
|
139 |
|
|
#define PT_DYNAMIC 2
|
140 |
|
|
#define PT_INTERP 3
|
141 |
|
|
#define PT_NOTE 4
|
142 |
|
|
|
143 |
|
|
#define ELFDATA2LSB 1
|
144 |
|
|
#define ELFDATA2MSB 2
|
145 |
|
|
|
146 |
|
|
/* Section Header type bits */
|
147 |
|
|
#define SHT_PROGBITS 1
|
148 |
|
|
#define SHT_SYMTAB 2
|
149 |
|
|
#define SHT_NOBITS 8
|
150 |
|
|
#define SHT_REL 9
|
151 |
|
|
|
152 |
|
|
/* Section Header flag bits */
|
153 |
|
|
#define SHF_WRITE 1
|
154 |
|
|
#define SHF_ALLOC 2
|
155 |
|
|
#define SHF_EXECINSTR 4
|
156 |
|
|
|
157 |
|
|
/**/
|
158 |
|
|
#define ELF_PRINT_PROGRAM_HEADERS 1
|
159 |
|
|
#define ELF_PRINT_SECTIONS 2
|
160 |
|
|
#define ELF_PRINT_ALL (ELF_PRINT_PROGRAM_HEADERS | ELF_PRINT_SECTIONS)
|
161 |
|
|
|
162 |
|
|
/**
|
163 |
|
|
* Checks that elfFile points to a valid elf file.
|
164 |
|
|
*
|
165 |
|
|
* @param elfFile Potential ELF file to check
|
166 |
|
|
*
|
167 |
|
|
* \return 0 on success. -1 if not and elf, -2 if not 32 bit.
|
168 |
|
|
*/
|
169 |
|
|
int elf_checkFile(void *elfFile);
|
170 |
|
|
|
171 |
|
|
/**
|
172 |
|
|
* Determine number of sections in an ELF file.
|
173 |
|
|
*
|
174 |
|
|
* @param elfFile Pointer to a valid ELF header.
|
175 |
|
|
*
|
176 |
|
|
* \return Number of sections in the ELF file.
|
177 |
|
|
*/
|
178 |
|
|
unsigned elf_getNumSections(void *elfFile);
|
179 |
|
|
|
180 |
|
|
/**
|
181 |
|
|
* Determine number of program headers in an ELF file.
|
182 |
|
|
*
|
183 |
|
|
* @param elfFile Pointer to a valid ELF header.
|
184 |
|
|
*
|
185 |
|
|
* \return Number of program headers in the ELF file.
|
186 |
|
|
*/
|
187 |
|
|
uint16_t elf_getNumProgramHeaders(void *elfFile);
|
188 |
|
|
|
189 |
|
|
/**
|
190 |
|
|
* Return the base physical address of given program header in an ELF file
|
191 |
|
|
*
|
192 |
|
|
* @param elfFile Pointer to a valid ELF header
|
193 |
|
|
* @param ph Index of the program header
|
194 |
|
|
*
|
195 |
|
|
* \return The memory size of the specified program header
|
196 |
|
|
*/
|
197 |
|
|
uint64_t elf_getProgramHeaderPaddr(void *elfFile, uint16_t ph);
|
198 |
|
|
|
199 |
|
|
/**
|
200 |
|
|
* Return the base virtual address of given program header in an ELF file
|
201 |
|
|
*
|
202 |
|
|
* @param elfFile Pointer to a valid ELF header
|
203 |
|
|
* @param ph Index of the program header
|
204 |
|
|
*
|
205 |
|
|
* \return The memory size of the specified program header
|
206 |
|
|
*/
|
207 |
|
|
uint64_t elf_getProgramHeaderVaddr(void *elfFile, uint16_t ph);
|
208 |
|
|
|
209 |
|
|
/**
|
210 |
|
|
* Return the memory size of a given program header in an ELF file
|
211 |
|
|
*
|
212 |
|
|
* @param elfFile Pointer to a valid ELF header
|
213 |
|
|
* @param ph Index of the program header
|
214 |
|
|
*
|
215 |
|
|
* \return The memory size of the specified program header
|
216 |
|
|
*/
|
217 |
|
|
uint64_t elf_getProgramHeaderMemorySize(void *elfFile, uint16_t ph);
|
218 |
|
|
|
219 |
|
|
/**
|
220 |
|
|
* Return the file size of a given program header in an ELF file
|
221 |
|
|
*
|
222 |
|
|
* @param elfFile Pointer to a valid ELF header
|
223 |
|
|
* @param ph Index of the program header
|
224 |
|
|
*
|
225 |
|
|
* \return The file size of the specified program header
|
226 |
|
|
*/
|
227 |
|
|
uint64_t elf_getProgramHeaderFileSize(void *elfFile, uint16_t ph);
|
228 |
|
|
|
229 |
|
|
/**
|
230 |
|
|
* Return the start offset of he file
|
231 |
|
|
*
|
232 |
|
|
* @param elfFile Pointer to a valid ELF header
|
233 |
|
|
* @param ph Index of the program header
|
234 |
|
|
*
|
235 |
|
|
* \return The offset of this program header with relation to the start
|
236 |
|
|
* of the elfFile.
|
237 |
|
|
*/
|
238 |
|
|
uint64_t elf_getProgramHeaderOffset(void *elfFile, uint16_t ph);
|
239 |
|
|
|
240 |
|
|
/**
|
241 |
|
|
* Return the flags for a given program header
|
242 |
|
|
*
|
243 |
|
|
* @param elfFile Pointer to a valid ELF header
|
244 |
|
|
* @param ph Index of the program header
|
245 |
|
|
*
|
246 |
|
|
* \return The flags of a given program header
|
247 |
|
|
*/
|
248 |
|
|
uint32_t elf_getProgramHeaderFlags(void *elfFile, uint16_t ph);
|
249 |
|
|
|
250 |
|
|
/**
|
251 |
|
|
* Return the type for a given program header
|
252 |
|
|
*
|
253 |
|
|
* @param elfFile Pointer to a valid ELF header
|
254 |
|
|
* @param ph Index of the program header
|
255 |
|
|
*
|
256 |
|
|
* \return The type of a given program header
|
257 |
|
|
*/
|
258 |
|
|
uint32_t elf_getProgramHeaderType(void *elfFile, uint16_t ph);
|
259 |
|
|
|
260 |
|
|
/**
|
261 |
|
|
* Return the physical translation of a physical address, with respect
|
262 |
|
|
* to a given program header
|
263 |
|
|
*
|
264 |
|
|
*/
|
265 |
|
|
uint64_t elf_vtopProgramHeader(void *elfFile, uint16_t ph, uint64_t vaddr);
|
266 |
|
|
|
267 |
|
|
|
268 |
|
|
/**
|
269 |
|
|
*
|
270 |
|
|
* \return true if the address in in this program header
|
271 |
|
|
*/
|
272 |
|
|
bool elf_vaddrInProgramHeader(void *elfFile, uint16_t ph, uint64_t vaddr);
|
273 |
|
|
|
274 |
|
|
/**
|
275 |
|
|
* Determine the memory bounds of an ELF file
|
276 |
|
|
*
|
277 |
|
|
* @param elfFile Pointer to a valid ELF header
|
278 |
|
|
* @param phys If true return bounds of physical memory, otherwise return
|
279 |
|
|
* bounds of virtual memory
|
280 |
|
|
* @param min Pointer to return value of the minimum
|
281 |
|
|
* @param max Pointer to return value of the maximum
|
282 |
|
|
*
|
283 |
|
|
* \return true on success. false on failure, if for example, it is an invalid ELF file
|
284 |
|
|
*/
|
285 |
|
|
bool elf_getMemoryBounds(void *elfFile, bool phys, uint64_t *min, uint64_t *max);
|
286 |
|
|
|
287 |
|
|
/**
|
288 |
|
|
* Find the entry point of an ELF file.
|
289 |
|
|
*
|
290 |
|
|
* @param elfFile Pointer to a valid ELF header
|
291 |
|
|
*
|
292 |
|
|
* \return The entry point address as a 64-bit integer.
|
293 |
|
|
*/
|
294 |
|
|
uint64_t elf_getEntryPoint(void *elfFile);
|
295 |
|
|
|
296 |
|
|
/**
|
297 |
|
|
* Load an ELF file into memory
|
298 |
|
|
*
|
299 |
|
|
* @param elfFile Pointer to a valid ELF file
|
300 |
|
|
* @param phys If true load using the physical address, otherwise using the virtual addresses
|
301 |
|
|
*
|
302 |
|
|
* \return true on success, false on failure.
|
303 |
|
|
*
|
304 |
|
|
* The function assumes that the ELF file is loaded in memory at some
|
305 |
|
|
* address different to the target address at which it will be loaded.
|
306 |
|
|
* It also assumes direct access to the source and destination address, i.e:
|
307 |
|
|
* Memory must be ale to me loaded with a simple memcpy.
|
308 |
|
|
*
|
309 |
|
|
* Obviously this also means that if we are loading a 64bit ELF on a 32bit
|
310 |
|
|
* platform, we assume that any memory address are within the first 4GB.
|
311 |
|
|
*
|
312 |
|
|
*/
|
313 |
|
|
bool elf_loadFile(void *elfFile, bool phys);
|
314 |
|
|
|
315 |
|
|
char *elf_getStringTable(void *elfFile, int string_segment);
|
316 |
|
|
char *elf_getSegmentStringTable(void *elfFile);
|
317 |
|
|
void *elf_getSectionNamed(void *elfFile, char *str);
|
318 |
|
|
char *elf_getSectionName(void *elfFile, int i);
|
319 |
|
|
uint64_t elf_getSectionSize(void *elfFile, int i);
|
320 |
|
|
uint64_t elf_getSectionAddr(void *elfFile, int i);
|
321 |
|
|
|
322 |
|
|
/**
|
323 |
|
|
* Return the flags for a given sections
|
324 |
|
|
*
|
325 |
|
|
* @param elfFile Pointer to a valid ELF header
|
326 |
|
|
* @param i Index of the sections
|
327 |
|
|
*
|
328 |
|
|
* \return The flags of a given section
|
329 |
|
|
*/
|
330 |
|
|
uint32_t elf_getSectionFlags(void *elfFile, int i);
|
331 |
|
|
|
332 |
|
|
/**
|
333 |
|
|
* Return the type for a given sections
|
334 |
|
|
*
|
335 |
|
|
* @param elfFile Pointer to a valid ELF header
|
336 |
|
|
* @param i Index of the sections
|
337 |
|
|
*
|
338 |
|
|
* \return The type of a given section
|
339 |
|
|
*/
|
340 |
|
|
uint32_t elf_getSectionType(void *elfFile, int i);
|
341 |
|
|
|
342 |
|
|
void *elf_getSection(void *elfFile, int i);
|
343 |
|
|
void elf_getProgramHeaderInfo(void *elfFile, uint16_t ph, uint64_t *p_vaddr,
|
344 |
|
|
uint64_t *p_paddr, uint64_t *p_filesz,
|
345 |
|
|
uint64_t *p_offset, uint64_t *p_memsz);
|
346 |
|
|
|
347 |
|
|
|
348 |
|
|
/**
|
349 |
|
|
* output the details of an ELF file to the stream f
|
350 |
|
|
*/
|
351 |
|
|
void elf_fprintf(FILE *f, void *elfFile, int size, const char *name, int flags);
|
352 |
|
|
|
353 |
|
|
#if 0
|
354 |
|
|
/*
|
355 |
|
|
* Returns a pointer to the program segment table, which is an array of
|
356 |
|
|
* ELF64_Phdr_t structs. The size of the array can be found by calling
|
357 |
|
|
* getNumProgramSegments.
|
358 |
|
|
*/
|
359 |
|
|
struct Elf32_Phdr *elf_getProgramSegmentTable(void *elfFile);
|
360 |
|
|
#endif
|
361 |
|
|
#if 0
|
362 |
|
|
/**
|
363 |
|
|
* Returns a pointer to the program segment table, which is an array of
|
364 |
|
|
* ELF64_Phdr_t structs. The size of the array can be found by calling
|
365 |
|
|
* getNumProgramSegments.
|
366 |
|
|
*/
|
367 |
|
|
struct Elf32_Shdr *elf_getSectionTable(void *elfFile);
|
368 |
|
|
#endif
|
369 |
|
|
|
370 |
|
|
#endif
|