13446Smrj /*
23446Smrj * CDDL HEADER START
33446Smrj *
43446Smrj * The contents of this file are subject to the terms of the
53446Smrj * Common Development and Distribution License (the "License").
63446Smrj * You may not use this file except in compliance with the License.
73446Smrj *
83446Smrj * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93446Smrj * or http://www.opensolaris.org/os/licensing.
103446Smrj * See the License for the specific language governing permissions
113446Smrj * and limitations under the License.
123446Smrj *
133446Smrj * When distributing Covered Code, include this CDDL HEADER in each
143446Smrj * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153446Smrj * If applicable, add the following below this CDDL HEADER, with the
163446Smrj * fields enclosed by brackets "[]" replaced with your own identifying
173446Smrj * information: Portions Copyright [yyyy] [name of copyright owner]
183446Smrj *
193446Smrj * CDDL HEADER END
203446Smrj */
213446Smrj
223446Smrj /*
233446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
243446Smrj * Use is subject to license terms.
253446Smrj */
263446Smrj
273446Smrj #pragma ident "%Z%%M% %I% %E% SMI"
283446Smrj
293446Smrj #include <stdlib.h>
303446Smrj #include <fcntl.h>
313446Smrj #include <strings.h>
323446Smrj #include <stdio.h>
333446Smrj #include <errno.h>
343446Smrj #include <sys/types.h>
353446Smrj #include <sys/inttypes.h>
363446Smrj #include <sys/elf.h>
373446Smrj #include <sys/elf_notes.h>
383446Smrj #include <sys/mman.h>
393446Smrj #include <sys/stat.h>
403446Smrj #include <sys/statvfs.h>
413446Smrj
423446Smrj static char *pname;
433446Smrj static char *fname;
443446Smrj static char *image; /* pointer to the ELF file in memory */
453446Smrj
463446Smrj #define ELFSEEK(offset) ((void *)(image + offset))
473446Smrj
483446Smrj /*
493446Smrj * Extract the PT_LOAD bits and format them into a .s
503446Smrj */
513446Smrj static void
extract32(Elf32_Ehdr * eh)523446Smrj extract32(Elf32_Ehdr *eh)
533446Smrj {
543446Smrj Elf32_Phdr *phdr;
553446Smrj caddr_t allphdrs;
563446Smrj int i;
573446Smrj int c;
583446Smrj unsigned char *bytes;
593446Smrj uint_t cnt = 10;
603446Smrj
613446Smrj allphdrs = NULL;
623446Smrj
633446Smrj if (eh->e_type != ET_EXEC) {
643446Smrj (void) fprintf(stderr, "%s: not ET_EXEC, e_type = 0x%x\n",
653446Smrj pname, eh->e_type);
663446Smrj exit(1);
673446Smrj }
683446Smrj if (eh->e_phnum == 0 || eh->e_phoff == 0) {
693446Smrj (void) fprintf(stderr, "%s: no program headers\n", pname);
703446Smrj exit(1);
713446Smrj }
723446Smrj
733446Smrj /*
743446Smrj * Get the program headers.
753446Smrj */
763446Smrj allphdrs = ELFSEEK(eh->e_phoff);
773446Smrj if (allphdrs == NULL) {
783446Smrj (void) fprintf(stderr, "%s: Failed to get %d program hdrs\n",
793446Smrj pname, eh->e_phnum);
803446Smrj exit(1);
813446Smrj }
823446Smrj
833446Smrj /*
843446Smrj * Find the PT_LOAD section
853446Smrj */
863446Smrj for (i = 0; i < eh->e_phnum; i++) {
873446Smrj /*LINTED [ELF program header alignment]*/
883446Smrj phdr = (Elf32_Phdr *)(allphdrs + eh->e_phentsize * i);
893446Smrj
903446Smrj if (phdr->p_type != PT_LOAD)
913446Smrj continue;
923446Smrj
933446Smrj if (phdr->p_memsz == 0)
943446Smrj continue;
953446Smrj
963446Smrj bytes = ELFSEEK(phdr->p_offset);
973446Smrj for (c = 0; c < phdr->p_filesz; ++c) {
983446Smrj if (c % cnt == 0)
993446Smrj (void) printf("\n .byte ");
1003446Smrj else
1013446Smrj (void) printf(",");
1023446Smrj (void) printf("0x%x", bytes[c]);
1033446Smrj }
1043446Smrj for (; c < phdr->p_memsz; ++c) {
1053446Smrj if (c % cnt == 0) {
1063446Smrj (void) printf("\n .byte ");
1073446Smrj cnt = 20;
1083446Smrj } else {
1093446Smrj (void) printf(", ");
1103446Smrj }
1113446Smrj (void) printf("0");
1123446Smrj }
1133446Smrj (void) printf("\n");
1143446Smrj return;
1153446Smrj }
1163446Smrj
1173446Smrj (void) fprintf(stderr, "%s: Failed finding PT_LOAD section\n", pname);
1183446Smrj exit(1);
1193446Smrj }
1203446Smrj
1213446Smrj static void
extract64(Elf64_Ehdr * eh)1223446Smrj extract64(Elf64_Ehdr *eh)
1233446Smrj {
1243446Smrj Elf64_Phdr *phdr;
1253446Smrj caddr_t allphdrs;
1263446Smrj int i;
1273446Smrj int c;
1283446Smrj unsigned char *bytes;
1293446Smrj uint_t cnt = 10;
1303446Smrj
1313446Smrj allphdrs = NULL;
1323446Smrj
1333446Smrj if (eh->e_type != ET_EXEC) {
1343446Smrj (void) fprintf(stderr, "%s: not ET_EXEC, e_type = 0x%x\n",
1353446Smrj pname, eh->e_type);
1363446Smrj exit(1);
1373446Smrj }
1383446Smrj if (eh->e_phnum == 0 || eh->e_phoff == 0) {
1393446Smrj (void) fprintf(stderr, "%s: no program headers\n", pname);
1403446Smrj exit(1);
1413446Smrj }
1423446Smrj
1433446Smrj /*
1443446Smrj * Get the program headers.
1453446Smrj */
1463446Smrj allphdrs = ELFSEEK(eh->e_phoff);
1473446Smrj if (allphdrs == NULL) {
1483446Smrj (void) fprintf(stderr, "%s: Failed to get %d program hdrs\n",
1493446Smrj pname, eh->e_phnum);
1503446Smrj exit(1);
1513446Smrj }
1523446Smrj
1533446Smrj /*
1543446Smrj * Find the PT_LOAD section
1553446Smrj */
1563446Smrj for (i = 0; i < eh->e_phnum; i++) {
1573446Smrj /*LINTED [ELF program header alignment]*/
1583446Smrj phdr = (Elf64_Phdr *)(allphdrs + eh->e_phentsize * i);
1593446Smrj
1603446Smrj if (phdr->p_type != PT_LOAD)
1613446Smrj continue;
1623446Smrj
1633446Smrj if (phdr->p_memsz == 0)
1643446Smrj continue;
1653446Smrj
1663446Smrj bytes = ELFSEEK(phdr->p_offset);
1673446Smrj for (c = 0; c < phdr->p_filesz; ++c) {
1683446Smrj if (c % cnt == 0)
1693446Smrj (void) printf("\n .byte ");
1703446Smrj else
1713446Smrj (void) printf(",");
1723446Smrj (void) printf("0x%x", bytes[c]);
1733446Smrj }
1743446Smrj for (; c < phdr->p_memsz; ++c) {
1753446Smrj if (c % cnt == 0) {
1763446Smrj (void) printf("\n .byte ");
1773446Smrj cnt = 20;
1783446Smrj } else {
1793446Smrj (void) printf(", ");
1803446Smrj }
1813446Smrj (void) printf("0");
1823446Smrj }
1833446Smrj (void) printf("\n");
1843446Smrj return;
1853446Smrj }
1863446Smrj
1873446Smrj (void) fprintf(stderr, "%s: Failed finding PT_LOAD section\n", pname);
1883446Smrj exit(1);
1893446Smrj }
1903446Smrj
1913446Smrj int
main(int argc,char ** argv)1923446Smrj main(int argc, char **argv)
1933446Smrj {
1943446Smrj int fd;
1953446Smrj uchar_t *ident;
1963446Smrj void *hdr = NULL;
1973446Smrj struct stat stats;
1983446Smrj ssize_t r;
199*5084Sjohnlev size_t pgsz;
2003446Smrj uint_t len;
2013446Smrj
2023446Smrj /*
2033446Smrj * we expect one argument -- the elf file
2043446Smrj */
2053446Smrj if (argc != 2) {
2063446Smrj (void) fprintf(stderr, "usage: %s <unix-elf-file>\n", argv[0]);
2073446Smrj exit(1);
2083446Smrj }
2093446Smrj
2103446Smrj pname = strrchr(argv[0], '/');
2113446Smrj if (pname == NULL)
2123446Smrj pname = argv[0];
2133446Smrj else
2143446Smrj ++pname;
2153446Smrj
2163446Smrj fname = argv[1];
2173446Smrj fd = open(fname, O_RDONLY);
2183446Smrj if (fd < 0) {
2193446Smrj (void) fprintf(stderr, "%s: open(%s, O_RDONLY) failed, %s\n",
2203446Smrj pname, fname, strerror(errno));
2213446Smrj exit(1);
2223446Smrj }
2233446Smrj
2243446Smrj if (stat(fname, &stats) < 0) {
2253446Smrj (void) fprintf(stderr, "%s: stat(%s, ...) failed, %s\n",
2263446Smrj pname, fname, strerror(errno));
2273446Smrj exit(1);
2283446Smrj }
2293446Smrj
230*5084Sjohnlev pgsz = getpagesize();
231*5084Sjohnlev len = (stats.st_size + (pgsz - 1)) & (~(pgsz - 1));
2323446Smrj
2333446Smrj /*
2343446Smrj * mmap the file
2353446Smrj */
2363446Smrj image = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
2373446Smrj if (image == MAP_FAILED) {
2383446Smrj (void) fprintf(stderr, "%s: mmap() of %s failed, %s\n",
2393446Smrj pname, fname, strerror(errno));
2403446Smrj exit(1);
2413446Smrj }
2423446Smrj
2433446Smrj ident = ELFSEEK(0);
2443446Smrj if (ident[EI_MAG0] != ELFMAG0 || ident[EI_MAG1] != ELFMAG1 ||
2453446Smrj ident[EI_MAG2] != ELFMAG2 || ident[EI_MAG3] != ELFMAG3) {
2463446Smrj (void) fprintf(stderr, "%s: not an ELF file!\n", pname);
2473446Smrj exit(1);
2483446Smrj }
2493446Smrj
2503446Smrj if (ident[EI_CLASS] == ELFCLASS32) {
2513446Smrj hdr = ELFSEEK(0);
2523446Smrj extract32(hdr);
2533446Smrj } else if (ident[EI_CLASS] == ELFCLASS64) {
2543446Smrj hdr = ELFSEEK(0);
2553446Smrj extract64(hdr);
2563446Smrj } else {
2573446Smrj (void) fprintf(stderr, "%s: Wrong ELF class 0x%x\n", pname,
2583446Smrj ident[EI_CLASS]);
2593446Smrj exit(1);
2603446Smrj }
2613446Smrj return (0);
2623446Smrj }
263