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 |
|
|
|
82 |
|
|
int
|
83 |
|
|
elf64_checkFile(void *elfFile)
|
84 |
|
|
{
|
85 |
|
|
struct Elf64_Header *fileHdr = (struct Elf64_Header *) elfFile;
|
86 |
|
|
if (fileHdr->e_ident[EI_MAG0] != ELFMAG0
|
87 |
|
|
|| fileHdr->e_ident[EI_MAG1] != ELFMAG1
|
88 |
|
|
|| fileHdr->e_ident[EI_MAG2] != ELFMAG2
|
89 |
|
|
|| fileHdr->e_ident[EI_MAG3] != ELFMAG3)
|
90 |
|
|
return -1; /* not an elf file */
|
91 |
|
|
if (fileHdr->e_ident[EI_CLASS] != ELFCLASS64)
|
92 |
|
|
return -2; /* not 64-bit file */
|
93 |
|
|
#if 0
|
94 |
|
|
if (fileHdr->e_ident[EI_DATA] != ELFDATA2LSB)
|
95 |
|
|
return -3; /* not big-endian file */
|
96 |
|
|
if (fileHdr->e_ident[EI_VERSION] != 1)
|
97 |
|
|
return -4; /* wrong version of elf */
|
98 |
|
|
if (fileHdr->e_machine != 8)
|
99 |
|
|
return -5; /* wrong architecture (not MIPS) */
|
100 |
|
|
if (fileHdr->e_type != 2)
|
101 |
|
|
return -6; /* not an executable program */
|
102 |
|
|
if (fileHdr->e_phentsize != sizeof(struct Elf64_Phdr))
|
103 |
|
|
return -7; /* unexpected size of program segment
|
104 |
|
|
* header */
|
105 |
|
|
if (fileHdr->e_phnum == 0)
|
106 |
|
|
return -8; /* no program segments */
|
107 |
|
|
if ((fileHdr->e_flags & 0x7e) != 0)
|
108 |
|
|
return -9; /* wrong flags (did you forgot to compile
|
109 |
|
|
* with -mno-abicalls?) */
|
110 |
|
|
#endif
|
111 |
|
|
return 0; /* elf file looks OK */
|
112 |
|
|
}
|
113 |
|
|
|
114 |
|
|
struct Elf64_Phdr *
|
115 |
|
|
elf64_getProgramSegmentTable(void *elfFile)
|
116 |
|
|
/*
|
117 |
|
|
* Returns a pointer to the program segment table, which is an array of
|
118 |
|
|
* ELF64_Phdr_t structs. The size of the array can be found by calling
|
119 |
|
|
* getNumProgramSegments.
|
120 |
|
|
*/
|
121 |
|
|
{
|
122 |
|
|
struct Elf64_Header *fileHdr = (struct Elf64_Header *) elfFile;
|
123 |
|
|
return (struct Elf64_Phdr *) ((size_t)fileHdr->e_phoff + (size_t) elfFile);
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
unsigned
|
127 |
|
|
elf64_getNumSections(void *elfFile)
|
128 |
|
|
/*
|
129 |
|
|
* Returns the number of program segments in this elf file.
|
130 |
|
|
*/
|
131 |
|
|
{
|
132 |
|
|
struct Elf64_Header *fileHdr = (struct Elf64_Header *) elfFile;
|
133 |
|
|
return fileHdr->e_shnum;
|
134 |
|
|
}
|
135 |
|
|
|
136 |
|
|
char *
|
137 |
|
|
elf64_getStringTable(void *elfFile, int string_segment)
|
138 |
|
|
{
|
139 |
|
|
struct Elf64_Shdr *sections = elf64_getSectionTable(elfFile);
|
140 |
|
|
return (char *) elfFile + sections[string_segment].sh_offset;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
char *
|
144 |
|
|
elf64_getSegmentStringTable(void *elfFile)
|
145 |
|
|
{
|
146 |
|
|
struct Elf64_Header *fileHdr = (struct Elf64_Header *) elfFile;
|
147 |
|
|
if (fileHdr->e_shstrndx == 0) {
|
148 |
|
|
return NULL;
|
149 |
|
|
} else {
|
150 |
|
|
return elf64_getStringTable(elfFile, fileHdr->e_shstrndx);
|
151 |
|
|
}
|
152 |
|
|
}
|
153 |
|
|
|
154 |
|
|
char *
|
155 |
|
|
elf64_getSectionName(void *elfFile, int i)
|
156 |
|
|
{
|
157 |
|
|
struct Elf64_Shdr *sections = elf64_getSectionTable(elfFile);
|
158 |
|
|
char *str_table = elf64_getSegmentStringTable(elfFile);
|
159 |
|
|
if (str_table == NULL) {
|
160 |
|
|
return "<corrupted>";
|
161 |
|
|
} else {
|
162 |
|
|
return str_table + sections[i].sh_name;
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
uint64_t
|
167 |
|
|
elf64_getSectionSize(void *elfFile, int i)
|
168 |
|
|
{
|
169 |
|
|
struct Elf64_Shdr *sections = elf64_getSectionTable(elfFile);
|
170 |
|
|
return sections[i].sh_size;
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
uint64_t
|
174 |
|
|
elf64_getSectionAddr(struct Elf64_Header *elfFile, int i)
|
175 |
|
|
{
|
176 |
|
|
struct Elf64_Shdr *sections = elf64_getSectionTable(elfFile);
|
177 |
|
|
return sections[i].sh_addr;
|
178 |
|
|
}
|
179 |
|
|
|
180 |
|
|
void *
|
181 |
|
|
elf64_getSection(void *elfFile, int i)
|
182 |
|
|
{
|
183 |
|
|
struct Elf64_Shdr *sections = elf64_getSectionTable(elfFile);
|
184 |
|
|
return (char *)elfFile + sections[i].sh_offset;
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
void *
|
188 |
|
|
elf64_getSectionNamed(void *elfFile, char *str)
|
189 |
|
|
{
|
190 |
|
|
int numSections = elf64_getNumSections(elfFile);
|
191 |
|
|
int i;
|
192 |
|
|
for (i = 0; i < numSections; i++) {
|
193 |
|
|
if (strcmp(str, elf64_getSectionName(elfFile, i)) == 0) {
|
194 |
|
|
return elf64_getSection(elfFile, i);
|
195 |
|
|
}
|
196 |
|
|
}
|
197 |
|
|
return NULL;
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
uint16_t
|
201 |
|
|
elf64_getNumProgramHeaders(struct Elf64_Header *elfFile)
|
202 |
|
|
{
|
203 |
|
|
return elfFile->e_phnum;
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
int
|
207 |
|
|
elf64_getSegmentType (void *elfFile, int segment)
|
208 |
|
|
{
|
209 |
|
|
return elf64_getProgramSegmentTable(elfFile)[segment].p_type;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
void
|
213 |
|
|
elf64_getSegmentInfo(void *elfFile, int segment, uint64_t *p_vaddr,
|
214 |
|
|
uint64_t *p_paddr, uint64_t *p_filesz, uint64_t *p_offset,
|
215 |
|
|
uint64_t *p_memsz)
|
216 |
|
|
{
|
217 |
|
|
struct Elf64_Phdr *segments;
|
218 |
|
|
|
219 |
|
|
segments = elf64_getProgramSegmentTable(elfFile);
|
220 |
|
|
*p_vaddr = segments[segment].p_vaddr;
|
221 |
|
|
*p_paddr = segments[segment].p_paddr;
|
222 |
|
|
*p_filesz = segments[segment].p_filesz;
|
223 |
|
|
*p_offset = segments[segment].p_offset;
|
224 |
|
|
*p_memsz = segments[segment].p_memsz;
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
uint64_t
|
228 |
|
|
elf64_getEntryPoint (struct Elf64_Header *elfFile)
|
229 |
|
|
{
|
230 |
|
|
return elfFile->e_entry;
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
/*
|
234 |
|
|
* Debugging functions
|
235 |
|
|
*/
|
236 |
|
|
|
237 |
|
|
#if 0
|
238 |
|
|
/*
|
239 |
|
|
* prints out some details of one elf file
|
240 |
|
|
*/
|
241 |
|
|
void
|
242 |
|
|
elf64_showDetails(void *elfFile, int size, char *name)
|
243 |
|
|
{
|
244 |
|
|
struct Elf64_Phdr *segments;
|
245 |
|
|
unsigned numSegments;
|
246 |
|
|
struct Elf64_Shdr *sections;
|
247 |
|
|
unsigned numSections;
|
248 |
|
|
int i,
|
249 |
|
|
r;
|
250 |
|
|
char *str_table;
|
251 |
|
|
|
252 |
|
|
printf("Found an elf64 file called \"%s\" located "
|
253 |
|
|
"at address 0x%lx\n", name, elfFile);
|
254 |
|
|
|
255 |
|
|
if ((r = elf64_checkFile(elfFile)) != 0) {
|
256 |
|
|
char *magic = elfFile;
|
257 |
|
|
printf("Invalid elf file (%d)\n", r);
|
258 |
|
|
printf("Magic is: %02.2hhx %02.2hhx %02.2hhx %02.2hhx\n",
|
259 |
|
|
magic[0], magic[1], magic[2], magic[3]);
|
260 |
|
|
return;
|
261 |
|
|
}
|
262 |
|
|
|
263 |
|
|
str_table = elf64_getSegmentStringTable(elfFile);
|
264 |
|
|
|
265 |
|
|
printf("Got str_table... %p\n", str_table);
|
266 |
|
|
|
267 |
|
|
/*
|
268 |
|
|
* get a pointer to the table of program segments
|
269 |
|
|
*/
|
270 |
|
|
segments = elf64_getProgramSegmentTable(elfFile);
|
271 |
|
|
numSegments = elf64_getNumProgramSegments(elfFile);
|
272 |
|
|
|
273 |
|
|
sections = elf64_getSectionTable(elfFile);
|
274 |
|
|
numSections = elf64_getNumSections(elfFile);
|
275 |
|
|
|
276 |
|
|
if ((void *) sections > (void *) elfFile + size ||
|
277 |
|
|
(((uintptr_t) sections & 0xf) != 0)) {
|
278 |
|
|
printf("Corrupted elfFile..\n");
|
279 |
|
|
return;
|
280 |
|
|
}
|
281 |
|
|
printf("Sections: %p\n", sections);
|
282 |
|
|
/*
|
283 |
|
|
* print out info about each section
|
284 |
|
|
*/
|
285 |
|
|
|
286 |
|
|
|
287 |
|
|
/*
|
288 |
|
|
* print out info about each program segment
|
289 |
|
|
*/
|
290 |
|
|
printf("Program Headers:\n");
|
291 |
|
|
printf(" Type Offset VirtAddr PhysAddr "
|
292 |
|
|
"FileSiz MemSiz Flg Align\n");
|
293 |
|
|
for (i = 0; i < numSegments; i++) {
|
294 |
|
|
|
295 |
|
|
if (segments[i].p_type != 1) {
|
296 |
|
|
printf("segment %d is not loadable, "
|
297 |
|
|
"skipping\n", i);
|
298 |
|
|
} else {
|
299 |
|
|
printf(" LOAD 0x%06lx 0x%08lx 0x%08lx"
|
300 |
|
|
" 0x%05lx 0x%05lx %c%c%c 0x%04lx\n",
|
301 |
|
|
segments[i].p_offset, segments[i].p_vaddr,
|
302 |
|
|
segments[i].p_vaddr,
|
303 |
|
|
segments[i].p_filesz, segments[i].p_memsz,
|
304 |
|
|
segments[i].p_flags & PF_R ? 'R' : ' ',
|
305 |
|
|
segments[i].p_flags & PF_W ? 'W' : ' ',
|
306 |
|
|
segments[i].p_flags & PF_X ? 'E' : ' ',
|
307 |
|
|
segments[i].p_align);
|
308 |
|
|
}
|
309 |
|
|
}
|
310 |
|
|
|
311 |
|
|
printf("Section Headers:\n");
|
312 |
|
|
printf(" [Nr] Name Type Addr Off\n");
|
313 |
|
|
for (i = 0; i < numSections; i++) {
|
314 |
|
|
if (elf_checkSection(elfFile, i) == 0) {
|
315 |
|
|
printf("%-17.17s %-15.15s %08x %06x\n", elf64_getSectionName(elfFile, i), " " /* sections[i].sh_type
|
316 |
|
|
*/ ,
|
317 |
|
|
sections[i].sh_addr, sections[i].sh_offset);
|
318 |
|
|
}
|
319 |
|
|
}
|
320 |
|
|
}
|
321 |
|
|
#endif
|