1 |
2 |
drasko |
/*
|
2 |
|
|
* Australian Public Licence B (OZPLB)
|
3 |
|
|
*
|
4 |
|
|
* Version 1-0
|
5 |
|
|
*
|
6 |
|
|
* Copyright (c) 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 |
|
|
#include <elf/elf.h>
|
80 |
|
|
#include <string.h>
|
81 |
|
|
#include <inttypes.h>
|
82 |
|
|
|
83 |
|
|
int
|
84 |
|
|
elf32_checkFile(struct Elf32_Header *file)
|
85 |
|
|
{
|
86 |
|
|
if (file->e_ident[EI_MAG0] != ELFMAG0
|
87 |
|
|
|| file->e_ident[EI_MAG1] != ELFMAG1
|
88 |
|
|
|| file->e_ident[EI_MAG2] != ELFMAG2
|
89 |
|
|
|| file->e_ident[EI_MAG3] != ELFMAG3)
|
90 |
|
|
return -1; /* not an elf file */
|
91 |
|
|
if (file->e_ident[EI_CLASS] != ELFCLASS32)
|
92 |
|
|
return -2; /* not 32-bit file */
|
93 |
|
|
return 0; /* elf file looks OK */
|
94 |
|
|
}
|
95 |
|
|
|
96 |
|
|
/*
|
97 |
|
|
* Returns the number of program segments in this elf file.
|
98 |
|
|
*/
|
99 |
|
|
unsigned
|
100 |
|
|
elf32_getNumSections(struct Elf32_Header *elfFile)
|
101 |
|
|
{
|
102 |
|
|
return elfFile->e_shnum;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
char *
|
106 |
|
|
elf32_getStringTable(struct Elf32_Header *elfFile)
|
107 |
|
|
{
|
108 |
|
|
struct Elf32_Shdr *sections = elf32_getSectionTable(elfFile);
|
109 |
|
|
return (char *)elfFile + sections[elfFile->e_shstrndx].sh_offset;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
/* Returns a pointer to the program segment table, which is an array of
|
113 |
|
|
* ELF32_Phdr_t structs. The size of the array can be found by calling
|
114 |
|
|
* getNumProgramSegments. */
|
115 |
|
|
struct Elf32_Phdr *
|
116 |
|
|
elf32_getProgramSegmentTable(struct Elf32_Header *elfFile)
|
117 |
|
|
{
|
118 |
|
|
struct Elf32_Header *fileHdr = elfFile;
|
119 |
|
|
return (struct Elf32_Phdr *) (fileHdr->e_phoff + (long) elfFile);
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
/* Returns the number of program segments in this elf file. */
|
123 |
|
|
uint16_t
|
124 |
|
|
elf32_getNumProgramHeaders(struct Elf32_Header *elfFile)
|
125 |
|
|
{
|
126 |
|
|
struct Elf32_Header *fileHdr = elfFile;
|
127 |
|
|
return fileHdr->e_phnum;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
char *
|
131 |
|
|
elf32_getSectionName(struct Elf32_Header *elfFile, int i)
|
132 |
|
|
{
|
133 |
|
|
struct Elf32_Shdr *sections = elf32_getSectionTable(elfFile);
|
134 |
|
|
char *str_table = elf32_getSegmentStringTable(elfFile);
|
135 |
|
|
if (str_table == NULL) {
|
136 |
|
|
return "<corrupted>";
|
137 |
|
|
} else {
|
138 |
|
|
return str_table + sections[i].sh_name;
|
139 |
|
|
}
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
uint32_t
|
143 |
|
|
elf32_getSectionSize(struct Elf32_Header *elfFile, int i)
|
144 |
|
|
{
|
145 |
|
|
struct Elf32_Shdr *sections = elf32_getSectionTable(elfFile);
|
146 |
|
|
return sections[i].sh_size;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
uint32_t
|
150 |
|
|
elf32_getSectionAddr(struct Elf32_Header *elfFile, int i)
|
151 |
|
|
{
|
152 |
|
|
struct Elf32_Shdr *sections = elf32_getSectionTable(elfFile);
|
153 |
|
|
return sections[i].sh_addr;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
void *
|
157 |
|
|
elf32_getSection(struct Elf32_Header *elfFile, int i)
|
158 |
|
|
{
|
159 |
|
|
struct Elf32_Shdr *sections = elf32_getSectionTable(elfFile);
|
160 |
|
|
return (char *)elfFile + sections[i].sh_offset;
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
void *
|
164 |
|
|
elf32_getSectionNamed(struct Elf32_Header *elfFile, char *str)
|
165 |
|
|
{
|
166 |
|
|
int numSections = elf32_getNumSections(elfFile);
|
167 |
|
|
int i;
|
168 |
|
|
for (i = 0; i < numSections; i++) {
|
169 |
|
|
if (strcmp(str, elf32_getSectionName(elfFile, i)) == 0) {
|
170 |
|
|
return elf32_getSection(elfFile, i);
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
return NULL;
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
char *
|
177 |
|
|
elf32_getSegmentStringTable(struct Elf32_Header *elfFile)
|
178 |
|
|
{
|
179 |
|
|
struct Elf32_Header *fileHdr = (struct Elf32_Header *) elfFile;
|
180 |
|
|
if (fileHdr->e_shstrndx == 0) {
|
181 |
|
|
return NULL;
|
182 |
|
|
} else {
|
183 |
|
|
return elf32_getStringTable(elfFile);
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
#ifdef ELF_DEBUG
|
188 |
|
|
void
|
189 |
|
|
elf32_printStringTable(struct Elf32_Header *elfFile)
|
190 |
|
|
{
|
191 |
|
|
int counter;
|
192 |
|
|
struct Elf32_Shdr *sections = elf32_getSectionTable(elfFile);
|
193 |
|
|
char * stringTable;
|
194 |
|
|
|
195 |
|
|
if (!sections) {
|
196 |
|
|
printf("No sections.\n");
|
197 |
|
|
return;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
stringTable = ((void *)elfFile) + sections[elfFile->e_shstrndx].sh_offset;
|
201 |
|
|
|
202 |
|
|
printf("File is %p; sections is %p; string table is %p\n", elfFile, sections, stringTable);
|
203 |
|
|
|
204 |
|
|
for (counter=0; counter < sections[elfFile->e_shstrndx].sh_size; counter++) {
|
205 |
|
|
printf("%02x %c ", stringTable[counter],
|
206 |
|
|
stringTable[counter] >= 0x20 ? stringTable[counter] : '.');
|
207 |
|
|
}
|
208 |
|
|
}
|
209 |
|
|
#endif
|
210 |
|
|
|
211 |
|
|
int
|
212 |
|
|
elf32_getSegmentType (struct Elf32_Header *elfFile, int segment)
|
213 |
|
|
{
|
214 |
|
|
return elf32_getProgramSegmentTable(elfFile)[segment].p_type;
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
void
|
218 |
|
|
elf32_getSegmentInfo(struct Elf32_Header *elfFile, int segment, uint64_t *p_vaddr, uint64_t *p_addr, uint64_t *p_filesz, uint64_t *p_offset, uint64_t *p_memsz)
|
219 |
|
|
{
|
220 |
|
|
struct Elf32_Phdr *segments;
|
221 |
|
|
|
222 |
|
|
segments = elf32_getProgramSegmentTable(elfFile);
|
223 |
|
|
*p_addr = segments[segment].p_paddr;
|
224 |
|
|
*p_vaddr = segments[segment].p_vaddr;
|
225 |
|
|
*p_filesz = segments[segment].p_filesz;
|
226 |
|
|
*p_offset = segments[segment].p_offset;
|
227 |
|
|
*p_memsz = segments[segment].p_memsz;
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
uint32_t
|
231 |
|
|
elf32_getEntryPoint (struct Elf32_Header *elfFile)
|
232 |
|
|
{
|
233 |
|
|
return elfFile->e_entry;
|
234 |
|
|
}
|
235 |
|
|
|
236 |
|
|
/*
|
237 |
|
|
* Debugging functions
|
238 |
|
|
*/
|
239 |
|
|
|
240 |
|
|
/*
|
241 |
|
|
* prints out some details of one elf file
|
242 |
|
|
*/
|
243 |
|
|
void
|
244 |
|
|
elf32_fprintf(FILE *f, struct Elf32_Header *file, int size, const char *name, int flags)
|
245 |
|
|
{
|
246 |
|
|
struct Elf32_Phdr *segments;
|
247 |
|
|
unsigned numSegments;
|
248 |
|
|
struct Elf32_Shdr *sections;
|
249 |
|
|
unsigned numSections;
|
250 |
|
|
int i, r;
|
251 |
|
|
char *str_table;
|
252 |
|
|
|
253 |
|
|
fprintf(f, "Found an elf32 file called \"%s\" located "
|
254 |
|
|
"at address 0x%p\n", name, file);
|
255 |
|
|
|
256 |
|
|
if ((r = elf32_checkFile(file)) != 0) {
|
257 |
|
|
char *magic = (char*) file;
|
258 |
|
|
fprintf(f, "Invalid elf file (%d)\n", r);
|
259 |
|
|
fprintf(f, "Magic is: %2.2hhx %2.2hhx %2.2hhx %2.2hhx\n",
|
260 |
|
|
magic[0], magic[1], magic[2], magic[3]);
|
261 |
|
|
return;
|
262 |
|
|
}
|
263 |
|
|
|
264 |
|
|
|
265 |
|
|
/*
|
266 |
|
|
* get a pointer to the table of program segments
|
267 |
|
|
*/
|
268 |
|
|
segments = elf32_getProgramHeaderTable(file);
|
269 |
|
|
numSegments = elf32_getNumProgramHeaders(file);
|
270 |
|
|
|
271 |
|
|
sections = elf32_getSectionTable(file);
|
272 |
|
|
numSections = elf32_getNumSections(file);
|
273 |
|
|
|
274 |
|
|
if ((uintptr_t) sections > ((uintptr_t) file + size)) {
|
275 |
|
|
fprintf(f, "Corrupted elfFile..\n");
|
276 |
|
|
return;
|
277 |
|
|
}
|
278 |
|
|
|
279 |
|
|
/*
|
280 |
|
|
* print out info about each section
|
281 |
|
|
*/
|
282 |
|
|
|
283 |
|
|
if (flags & ELF_PRINT_PROGRAM_HEADERS) {
|
284 |
|
|
/*
|
285 |
|
|
* print out info about each program segment
|
286 |
|
|
*/
|
287 |
|
|
fprintf(f, "Program Headers:\n");
|
288 |
|
|
fprintf(f, " Type Offset VirtAddr PhysAddr "
|
289 |
|
|
"FileSiz MemSiz Flg Align\n");
|
290 |
|
|
for (i = 0; i < numSegments; i++) {
|
291 |
|
|
if (segments[i].p_type != 1) {
|
292 |
|
|
fprintf(f, "segment %d is not loadable, "
|
293 |
|
|
"skipping\n", i);
|
294 |
|
|
} else {
|
295 |
|
|
fprintf(f, " LOAD 0x%06" PRIx32 " 0x%08" PRIx32 " 0x%08" PRIx32 \
|
296 |
|
|
" 0x%05" PRIx32" 0x%05" PRIx32 " %c%c%c 0x%04" PRIx32 "\n",
|
297 |
|
|
//fprintf(f, " LOAD 0x%" PRIxPTR " 0x%" PRIxPTR " 0x%" PRIxPTR
|
298 |
|
|
// " 0x%" PRIxPTR" 0x%" PRIxPTR " %c%c%c 0x%" PRIxPTR "\n",
|
299 |
|
|
segments[i].p_offset, segments[i].p_vaddr,
|
300 |
|
|
segments[i].p_paddr,
|
301 |
|
|
segments[i].p_filesz, segments[i].p_memsz,
|
302 |
|
|
segments[i].p_flags & PF_R ? 'R' : ' ',
|
303 |
|
|
segments[i].p_flags & PF_W ? 'W' : ' ',
|
304 |
|
|
segments[i].p_flags & PF_X ? 'E' : ' ',
|
305 |
|
|
segments[i].p_align);
|
306 |
|
|
}
|
307 |
|
|
}
|
308 |
|
|
}
|
309 |
|
|
if (flags & ELF_PRINT_SECTIONS) {
|
310 |
|
|
str_table = elf32_getSegmentStringTable(file);
|
311 |
|
|
|
312 |
|
|
printf("Section Headers:\n");
|
313 |
|
|
printf(" [Nr] Name Type Addr Off\n");
|
314 |
|
|
for (i = 0; i < numSections; i++) {
|
315 |
|
|
//if (elf_checkSection(file, i) == 0) {
|
316 |
|
|
fprintf(f, "[%2d] %s %x %x\n", i, elf32_getSectionName(file, i),
|
317 |
|
|
//fprintf(f, "[%2d] %-17.17s %-15.15s %x %x\n", i, elf32_getSectionName(file, i), " ",
|
318 |
|
|
///fprintf(f, "%-17.17s %-15.15s %08x %06x\n", elf32_getSectionName(file, i), " " /* sections[i].sh_type
|
319 |
|
|
// */ ,
|
320 |
|
|
sections[i].sh_addr, sections[i].sh_offset);
|
321 |
|
|
//}
|
322 |
|
|
}
|
323 |
|
|
}
|
324 |
|
|
}
|