4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
34 #include <sys/types.h>
35 #include <sys/inttypes.h>
37 #include <sys/elf_notes.h>
40 #include <sys/statvfs.h>
44 static char *image
; /* pointer to the ELF file in memory */
46 #define ELFSEEK(offset) ((void *)(image + offset))
49 * Extract the PT_LOAD bits and format them into a .s
52 extract32(Elf32_Ehdr
*eh
)
63 if (eh
->e_type
!= ET_EXEC
) {
64 (void) fprintf(stderr
, "%s: not ET_EXEC, e_type = 0x%x\n",
68 if (eh
->e_phnum
== 0 || eh
->e_phoff
== 0) {
69 (void) fprintf(stderr
, "%s: no program headers\n", pname
);
74 * Get the program headers.
76 allphdrs
= ELFSEEK(eh
->e_phoff
);
77 if (allphdrs
== NULL
) {
78 (void) fprintf(stderr
, "%s: Failed to get %d program hdrs\n",
84 * Find the PT_LOAD section
86 for (i
= 0; i
< eh
->e_phnum
; i
++) {
87 /*LINTED [ELF program header alignment]*/
88 phdr
= (Elf32_Phdr
*)(allphdrs
+ eh
->e_phentsize
* i
);
90 if (phdr
->p_type
!= PT_LOAD
)
93 if (phdr
->p_memsz
== 0)
96 bytes
= ELFSEEK(phdr
->p_offset
);
97 for (c
= 0; c
< phdr
->p_filesz
; ++c
) {
99 (void) printf("\n .byte ");
102 (void) printf("0x%x", bytes
[c
]);
104 for (; c
< phdr
->p_memsz
; ++c
) {
106 (void) printf("\n .byte ");
117 (void) fprintf(stderr
, "%s: Failed finding PT_LOAD section\n", pname
);
122 extract64(Elf64_Ehdr
*eh
)
128 unsigned char *bytes
;
133 if (eh
->e_type
!= ET_EXEC
) {
134 (void) fprintf(stderr
, "%s: not ET_EXEC, e_type = 0x%x\n",
138 if (eh
->e_phnum
== 0 || eh
->e_phoff
== 0) {
139 (void) fprintf(stderr
, "%s: no program headers\n", pname
);
144 * Get the program headers.
146 allphdrs
= ELFSEEK(eh
->e_phoff
);
147 if (allphdrs
== NULL
) {
148 (void) fprintf(stderr
, "%s: Failed to get %d program hdrs\n",
154 * Find the PT_LOAD section
156 for (i
= 0; i
< eh
->e_phnum
; i
++) {
157 /*LINTED [ELF program header alignment]*/
158 phdr
= (Elf64_Phdr
*)(allphdrs
+ eh
->e_phentsize
* i
);
160 if (phdr
->p_type
!= PT_LOAD
)
163 if (phdr
->p_memsz
== 0)
166 bytes
= ELFSEEK(phdr
->p_offset
);
167 for (c
= 0; c
< phdr
->p_filesz
; ++c
) {
169 (void) printf("\n .byte ");
172 (void) printf("0x%x", bytes
[c
]);
174 for (; c
< phdr
->p_memsz
; ++c
) {
176 (void) printf("\n .byte ");
187 (void) fprintf(stderr
, "%s: Failed finding PT_LOAD section\n", pname
);
192 main(int argc
, char **argv
)
203 * we expect one argument -- the elf file
206 (void) fprintf(stderr
, "usage: %s <unix-elf-file>\n", argv
[0]);
210 pname
= strrchr(argv
[0], '/');
217 fd
= open(fname
, O_RDONLY
);
219 (void) fprintf(stderr
, "%s: open(%s, O_RDONLY) failed, %s\n",
220 pname
, fname
, strerror(errno
));
224 if (stat(fname
, &stats
) < 0) {
225 (void) fprintf(stderr
, "%s: stat(%s, ...) failed, %s\n",
226 pname
, fname
, strerror(errno
));
230 pgsz
= getpagesize();
231 len
= (stats
.st_size
+ (pgsz
- 1)) & (~(pgsz
- 1));
236 image
= mmap(NULL
, len
, PROT_READ
, MAP_SHARED
, fd
, 0);
237 if (image
== MAP_FAILED
) {
238 (void) fprintf(stderr
, "%s: mmap() of %s failed, %s\n",
239 pname
, fname
, strerror(errno
));
244 if (ident
[EI_MAG0
] != ELFMAG0
|| ident
[EI_MAG1
] != ELFMAG1
||
245 ident
[EI_MAG2
] != ELFMAG2
|| ident
[EI_MAG3
] != ELFMAG3
) {
246 (void) fprintf(stderr
, "%s: not an ELF file!\n", pname
);
250 if (ident
[EI_CLASS
] == ELFCLASS32
) {
253 } else if (ident
[EI_CLASS
] == ELFCLASS64
) {
257 (void) fprintf(stderr
, "%s: Wrong ELF class 0x%x\n", pname
,