[BACK]Return to readelf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / file

Annotation of src/usr.bin/file/readelf.c, Revision 1.11

1.11    ! deraadt     1: /*     $OpenBSD: readelf.c,v 1.10 2009/04/24 18:54:34 chl Exp $ */
1.5       ian         2: /*
1.8       tedu        3:  * Copyright (c) Christos Zoulas 2003.
                      4:  * All Rights Reserved.
1.5       ian         5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice immediately at the beginning of the file, without modification,
                     11:  *    this list of conditions, and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
                     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
                     17:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     18:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     19:  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
                     20:  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     21:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     22:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     23:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     24:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     25:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     26:  * SUCH DAMAGE.
                     27:  */
1.4       ian        28: #include "file.h"
1.1       millert    29:
                     30: #ifdef BUILTIN_ELF
                     31: #include <string.h>
                     32: #include <ctype.h>
                     33: #include <stdlib.h>
1.4       ian        34: #ifdef HAVE_UNISTD_H
1.1       millert    35: #include <unistd.h>
1.4       ian        36: #endif
1.1       millert    37:
                     38: #include "readelf.h"
1.10      chl        39: #include "magic.h"
1.4       ian        40:
                     41: #ifdef ELFCORE
1.9       chl        42: private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
                     43:     off_t, int *);
1.4       ian        44: #endif
1.9       chl        45: private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
                     46:     off_t, int *);
                     47: private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, int *);
1.8       tedu       48: private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
1.9       chl        49:     int, size_t, int *);
1.8       tedu       50:
                     51: #define        ELF_ALIGN(a)    ((((a) + align - 1) / align) * align)
                     52:
1.9       chl        53: #define isquote(c) (strchr("'\"`", (c)) != NULL)
                     54:
1.8       tedu       55: private uint16_t getu16(int, uint16_t);
                     56: private uint32_t getu32(int, uint32_t);
                     57: private uint64_t getu64(int, uint64_t);
1.4       ian        58:
1.8       tedu       59: private uint16_t
1.4       ian        60: getu16(int swap, uint16_t value)
                     61: {
                     62:        union {
                     63:                uint16_t ui;
                     64:                char c[2];
                     65:        } retval, tmpval;
                     66:
                     67:        if (swap) {
                     68:                tmpval.ui = value;
                     69:
                     70:                retval.c[0] = tmpval.c[1];
                     71:                retval.c[1] = tmpval.c[0];
                     72:
                     73:                return retval.ui;
                     74:        } else
                     75:                return value;
                     76: }
                     77:
1.8       tedu       78: private uint32_t
1.4       ian        79: getu32(int swap, uint32_t value)
                     80: {
                     81:        union {
                     82:                uint32_t ui;
                     83:                char c[4];
                     84:        } retval, tmpval;
                     85:
                     86:        if (swap) {
                     87:                tmpval.ui = value;
                     88:
                     89:                retval.c[0] = tmpval.c[3];
                     90:                retval.c[1] = tmpval.c[2];
                     91:                retval.c[2] = tmpval.c[1];
                     92:                retval.c[3] = tmpval.c[0];
                     93:
                     94:                return retval.ui;
                     95:        } else
                     96:                return value;
                     97: }
                     98:
1.8       tedu       99: private uint64_t
1.4       ian       100: getu64(int swap, uint64_t value)
                    101: {
                    102:        union {
                    103:                uint64_t ui;
                    104:                char c[8];
                    105:        } retval, tmpval;
                    106:
                    107:        if (swap) {
                    108:                tmpval.ui = value;
                    109:
                    110:                retval.c[0] = tmpval.c[7];
                    111:                retval.c[1] = tmpval.c[6];
                    112:                retval.c[2] = tmpval.c[5];
                    113:                retval.c[3] = tmpval.c[4];
                    114:                retval.c[4] = tmpval.c[3];
                    115:                retval.c[5] = tmpval.c[2];
                    116:                retval.c[6] = tmpval.c[1];
                    117:                retval.c[7] = tmpval.c[0];
                    118:
                    119:                return retval.ui;
                    120:        } else
                    121:                return value;
                    122: }
                    123:
1.10      chl       124: #define elf_getu16(swap, value) getu16(swap, value)
                    125: #define elf_getu32(swap, value) getu32(swap, value)
1.9       chl       126: #ifdef USE_ARRAY_FOR_64BIT_TYPES
                    127: # define elf_getu64(swap, array) \
1.10      chl       128:        ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
                    129:         (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
1.9       chl       130: #else
                    131: # define elf_getu64(swap, value) getu64(swap, value)
                    132: #endif
                    133:
1.10      chl       134: #define xsh_addr       (class == ELFCLASS32                    \
                    135:                         ? (void *) &sh32                       \
1.4       ian       136:                         : (void *) &sh64)
1.10      chl       137: #define xsh_sizeof     (class == ELFCLASS32                    \
                    138:                         ? sizeof sh32                          \
1.4       ian       139:                         : sizeof sh64)
1.10      chl       140: #define xsh_size       (class == ELFCLASS32                    \
                    141:                         ? elf_getu32(swap, sh32.sh_size)       \
                    142:                         : elf_getu64(swap, sh64.sh_size))
                    143: #define xsh_offset     (class == ELFCLASS32                    \
                    144:                         ? elf_getu32(swap, sh32.sh_offset)     \
                    145:                         : elf_getu64(swap, sh64.sh_offset))
                    146: #define xsh_type       (class == ELFCLASS32                    \
                    147:                         ? elf_getu32(swap, sh32.sh_type)       \
                    148:                         : elf_getu32(swap, sh64.sh_type))
                    149: #define xph_addr       (class == ELFCLASS32                    \
                    150:                         ? (void *) &ph32                       \
1.4       ian       151:                         : (void *) &ph64)
1.10      chl       152: #define xph_sizeof     (class == ELFCLASS32                    \
                    153:                         ? sizeof ph32                          \
1.4       ian       154:                         : sizeof ph64)
1.10      chl       155: #define xph_type       (class == ELFCLASS32                    \
                    156:                         ? elf_getu32(swap, ph32.p_type)        \
                    157:                         : elf_getu32(swap, ph64.p_type))
                    158: #define xph_offset     (off_t)(class == ELFCLASS32             \
                    159:                         ? elf_getu32(swap, ph32.p_offset)      \
                    160:                         : elf_getu64(swap, ph64.p_offset))
                    161: #define xph_align      (size_t)((class == ELFCLASS32           \
                    162:                         ? (off_t) (ph32.p_align ?              \
                    163:                            elf_getu32(swap, ph32.p_align) : 4) \
                    164:                         : (off_t) (ph64.p_align ?              \
                    165:                            elf_getu64(swap, ph64.p_align) : 4)))
                    166: #define xph_filesz     (size_t)((class == ELFCLASS32           \
                    167:                         ? elf_getu32(swap, ph32.p_filesz)      \
                    168:                         : elf_getu64(swap, ph64.p_filesz)))
                    169: #define xnh_addr       (class == ELFCLASS32                    \
                    170:                         ? (void *) &nh32                       \
1.9       chl       171:                         : (void *) &nh64)
1.10      chl       172: #define xph_memsz      (size_t)((class == ELFCLASS32           \
                    173:                         ? elf_getu32(swap, ph32.p_memsz)       \
                    174:                         : elf_getu64(swap, ph64.p_memsz)))
                    175: #define xnh_sizeof     (class == ELFCLASS32                    \
                    176:                         ? sizeof nh32                          \
1.8       tedu      177:                         : sizeof nh64)
1.10      chl       178: #define xnh_type       (class == ELFCLASS32                    \
                    179:                         ? elf_getu32(swap, nh32.n_type)        \
                    180:                         : elf_getu32(swap, nh64.n_type))
                    181: #define xnh_namesz     (class == ELFCLASS32                    \
                    182:                         ? elf_getu32(swap, nh32.n_namesz)      \
                    183:                         : elf_getu32(swap, nh64.n_namesz))
                    184: #define xnh_descsz     (class == ELFCLASS32                    \
                    185:                         ? elf_getu32(swap, nh32.n_descsz)      \
                    186:                         : elf_getu32(swap, nh64.n_descsz))
                    187: #define prpsoffsets(i) (class == ELFCLASS32                    \
                    188:                         ? prpsoffsets32[i]                     \
1.4       ian       189:                         : prpsoffsets64[i])
1.1       millert   190:
1.4       ian       191: #ifdef ELFCORE
1.10      chl       192: /*
                    193:  * Try larger offsets first to avoid false matches
                    194:  * from earlier data that happen to look like strings.
                    195:  */
                    196: static const size_t    prpsoffsets32[] = {
                    197: #ifdef USE_NT_PSINFO
                    198:        104,            /* SunOS 5.x (command line) */
                    199:        88,             /* SunOS 5.x (short name) */
                    200: #endif /* USE_NT_PSINFO */
                    201:
                    202:        100,            /* SunOS 5.x (command line) */
                    203:        84,             /* SunOS 5.x (short name) */
                    204:
                    205:        44,             /* Linux (command line) */
                    206:        28,             /* Linux 2.0.36 (short name) */
                    207:
1.4       ian       208:        8,              /* FreeBSD */
                    209: };
                    210:
1.10      chl       211: static const size_t    prpsoffsets64[] = {
                    212: #ifdef USE_NT_PSINFO
                    213:        152,            /* SunOS 5.x (command line) */
                    214:        136,            /* SunOS 5.x (short name) */
                    215: #endif /* USE_NT_PSINFO */
                    216:
                    217:        136,            /* SunOS 5.x, 64-bit (command line) */
                    218:        120,            /* SunOS 5.x, 64-bit (short name) */
                    219:
                    220:        56,             /* Linux (command line) */
                    221:        40,             /* Linux (tested on core from 2.4.x, short name) */
                    222:
1.9       chl       223:        16,             /* FreeBSD, 64-bit */
1.1       millert   224: };
                    225:
1.4       ian       226: #define        NOFFSETS32      (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
                    227: #define NOFFSETS64     (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
                    228:
                    229: #define NOFFSETS       (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
1.1       millert   230:
                    231: /*
                    232:  * Look through the program headers of an executable image, searching
1.4       ian       233:  * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
                    234:  * "FreeBSD"; if one is found, try looking in various places in its
                    235:  * contents for a 16-character string containing only printable
                    236:  * characters - if found, that string should be the name of the program
                    237:  * that dropped core.  Note: right after that 16-character string is,
                    238:  * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
                    239:  * Linux, a longer string (80 characters, in 5.x, probably other
                    240:  * SVR4-flavored systems, and Linux) containing the start of the
                    241:  * command line for that program.
                    242:  *
1.10      chl       243:  * SunOS 5.x core files contain two PT_NOTE sections, with the types
                    244:  * NT_PRPSINFO (old) and NT_PSINFO (new).  These structs contain the
                    245:  * same info about the command name and command line, so it probably
                    246:  * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
                    247:  * above (see USE_NT_PSINFO), in case we ever decide to do so.  The
                    248:  * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
                    249:  * the SunOS 5.x file command relies on this (and prefers the latter).
                    250:  *
1.4       ian       251:  * The signal number probably appears in a section of type NT_PRSTATUS,
                    252:  * but that's also rather OS-dependent, in ways that are harder to
                    253:  * dissect with heuristics, so I'm not bothering with the signal number.
                    254:  * (I suppose the signal number could be of interest in situations where
                    255:  * you don't have the binary of the program that dropped core; if you
                    256:  * *do* have that binary, the debugger will probably tell you what
                    257:  * signal it was.)
1.1       millert   258:  */
1.4       ian       259:
                    260: #define        OS_STYLE_SVR4           0
                    261: #define        OS_STYLE_FREEBSD        1
                    262: #define        OS_STYLE_NETBSD         2
                    263:
1.10      chl       264: private const char os_style_names[][8] = {
1.4       ian       265:        "SVR4",
                    266:        "FreeBSD",
                    267:        "NetBSD",
                    268: };
                    269:
1.9       chl       270: #define FLAGS_DID_CORE         1
                    271: #define FLAGS_DID_NOTE         2
1.10      chl       272: #define FLAGS_DID_CORE_STYLE   4
1.9       chl       273:
1.8       tedu      274: private int
                    275: dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
1.9       chl       276:     int num, size_t size, off_t fsize, int *flags)
1.1       millert   277: {
1.4       ian       278:        Elf32_Phdr ph32;
                    279:        Elf64_Phdr ph64;
1.8       tedu      280:        size_t offset;
                    281:        unsigned char nbuf[BUFSIZ];
                    282:        ssize_t bufsize;
1.9       chl       283:        off_t savedoffset;
                    284:        struct stat st;
1.8       tedu      285:
1.9       chl       286:        if (fstat(fd, &st) < 0) {
                    287:                file_badread(ms);
                    288:                return -1;
                    289:        }
                    290:
                    291:        if (size != xph_sizeof) {
1.8       tedu      292:                if (file_printf(ms, ", corrupted program header size") == -1)
                    293:                        return -1;
                    294:                return 0;
                    295:        }
1.9       chl       296:
1.4       ian       297:        /*
                    298:         * Loop through all the program headers.
                    299:         */
1.1       millert   300:        for ( ; num; num--) {
1.9       chl       301:                if ((savedoffset = lseek(fd, off, SEEK_SET)) == (off_t)-1) {
1.8       tedu      302:                        file_badseek(ms);
                    303:                        return -1;
                    304:                }
1.9       chl       305:                if (read(fd, xph_addr, xph_sizeof) == -1) {
1.8       tedu      306:                        file_badread(ms);
                    307:                        return -1;
                    308:                }
1.9       chl       309:                if (xph_offset > fsize) {
                    310:                        if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
                    311:                                file_badseek(ms);
                    312:                                return -1;
                    313:                        }
                    314:                        continue;
                    315:                }
                    316:
1.1       millert   317:                off += size;
1.9       chl       318:                if (xph_type != PT_NOTE)
1.1       millert   319:                        continue;
1.4       ian       320:
                    321:                /*
                    322:                 * This is a PT_NOTE section; loop through all the notes
                    323:                 * in the section.
                    324:                 */
1.9       chl       325:                if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
1.8       tedu      326:                        file_badseek(ms);
                    327:                        return -1;
                    328:                }
1.9       chl       329:                bufsize = read(fd, nbuf,
                    330:                    ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
1.8       tedu      331:                if (bufsize == -1) {
                    332:                        file_badread(ms);
                    333:                        return -1;
                    334:                }
1.1       millert   335:                offset = 0;
                    336:                for (;;) {
1.8       tedu      337:                        if (offset >= (size_t)bufsize)
                    338:                                break;
                    339:                        offset = donote(ms, nbuf, offset, (size_t)bufsize,
1.9       chl       340:                            class, swap, 4, flags);
1.8       tedu      341:                        if (offset == 0)
1.1       millert   342:                                break;
                    343:
1.8       tedu      344:                }
                    345:        }
                    346:        return 0;
                    347: }
                    348: #endif
                    349:
                    350: private size_t
                    351: donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
1.9       chl       352:     int class, int swap, size_t align, int *flags)
1.8       tedu      353: {
                    354:        Elf32_Nhdr nh32;
                    355:        Elf64_Nhdr nh64;
                    356:        size_t noff, doff;
                    357: #ifdef ELFCORE
                    358:        int os_style = -1;
                    359: #endif
                    360:        uint32_t namesz, descsz;
                    361:
1.9       chl       362:        (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
                    363:        offset += xnh_sizeof;
1.8       tedu      364:
1.9       chl       365:        namesz = xnh_namesz;
                    366:        descsz = xnh_descsz;
1.8       tedu      367:        if ((namesz == 0) && (descsz == 0)) {
                    368:                /*
                    369:                 * We're out of note headers.
                    370:                 */
1.9       chl       371:                return (offset >= size) ? offset : size;
1.8       tedu      372:        }
                    373:
                    374:        if (namesz & 0x80000000) {
                    375:            (void)file_printf(ms, ", bad note name size 0x%lx",
                    376:                (unsigned long)namesz);
                    377:            return offset;
                    378:        }
                    379:
                    380:        if (descsz & 0x80000000) {
                    381:            (void)file_printf(ms, ", bad note description size 0x%lx",
                    382:                (unsigned long)descsz);
                    383:            return offset;
                    384:        }
                    385:
                    386:
                    387:        noff = offset;
                    388:        doff = ELF_ALIGN(offset + namesz);
                    389:
1.9       chl       390:        if (offset + namesz > size) {
1.8       tedu      391:                /*
                    392:                 * We're past the end of the buffer.
                    393:                 */
                    394:                return doff;
                    395:        }
                    396:
                    397:        offset = ELF_ALIGN(doff + descsz);
1.9       chl       398:        if (doff + descsz > size) {
                    399:                /*
                    400:                 * We're past the end of the buffer.
                    401:                 */
                    402:                return (offset >= size) ? offset : size;
1.8       tedu      403:        }
                    404:
1.9       chl       405:        if (*flags & FLAGS_DID_NOTE)
                    406:                goto core;
                    407:
1.8       tedu      408:        if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
1.9       chl       409:            xnh_type == NT_GNU_VERSION && descsz == 16) {
1.8       tedu      410:                uint32_t desc[4];
                    411:                (void)memcpy(desc, &nbuf[doff], sizeof(desc));
                    412:
                    413:                if (file_printf(ms, ", for GNU/") == -1)
                    414:                        return size;
1.10      chl       415:                switch (elf_getu32(swap, desc[0])) {
1.8       tedu      416:                case GNU_OS_LINUX:
                    417:                        if (file_printf(ms, "Linux") == -1)
                    418:                                return size;
                    419:                        break;
                    420:                case GNU_OS_HURD:
                    421:                        if (file_printf(ms, "Hurd") == -1)
                    422:                                return size;
                    423:                        break;
                    424:                case GNU_OS_SOLARIS:
                    425:                        if (file_printf(ms, "Solaris") == -1)
                    426:                                return size;
                    427:                        break;
1.10      chl       428:                case GNU_OS_KFREEBSD:
                    429:                        if (file_printf(ms, "kFreeBSD") == -1)
                    430:                                return size;
                    431:                        break;
                    432:                case GNU_OS_KNETBSD:
                    433:                        if (file_printf(ms, "kNetBSD") == -1)
                    434:                                return size;
                    435:                        break;
1.8       tedu      436:                default:
                    437:                        if (file_printf(ms, "<unknown>") == -1)
                    438:                                return size;
                    439:                }
1.10      chl       440:                if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
                    441:                    elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
1.8       tedu      442:                        return size;
1.9       chl       443:                *flags |= FLAGS_DID_NOTE;
1.8       tedu      444:                return size;
                    445:        }
                    446:
                    447:        if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
1.9       chl       448:            xnh_type == NT_NETBSD_VERSION && descsz == 4) {
1.8       tedu      449:                uint32_t desc;
                    450:                (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
1.10      chl       451:                desc = elf_getu32(swap, desc);
1.8       tedu      452:
                    453:                if (file_printf(ms, ", for NetBSD") == -1)
                    454:                        return size;
                    455:                /*
                    456:                 * The version number used to be stuck as 199905, and was thus
                    457:                 * basically content-free.  Newer versions of NetBSD have fixed
                    458:                 * this and now use the encoding of __NetBSD_Version__:
                    459:                 *
                    460:                 *      MMmmrrpp00
                    461:                 *
                    462:                 * M = major version
                    463:                 * m = minor version
                    464:                 * r = release ["",A-Z,Z[A-Z] but numeric]
                    465:                 * p = patchlevel
                    466:                 */
                    467:                if (desc > 100000000U) {
1.9       chl       468:                        uint32_t ver_patch = (desc / 100) % 100;
                    469:                        uint32_t ver_rel = (desc / 10000) % 100;
                    470:                        uint32_t ver_min = (desc / 1000000) % 100;
                    471:                        uint32_t ver_maj = desc / 100000000;
1.8       tedu      472:
                    473:                        if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
                    474:                                return size;
                    475:                        if (ver_rel == 0 && ver_patch != 0) {
                    476:                                if (file_printf(ms, ".%u", ver_patch) == -1)
                    477:                                        return size;
                    478:                        } else if (ver_rel != 0) {
                    479:                                while (ver_rel > 26) {
1.9       chl       480:                                        if (file_printf(ms, "Z") == -1)
                    481:                                                return size;
1.8       tedu      482:                                        ver_rel -= 26;
                    483:                                }
1.9       chl       484:                                if (file_printf(ms, "%c", 'A' + ver_rel - 1)
                    485:                                    == -1)
                    486:                                        return size;
1.1       millert   487:                        }
1.8       tedu      488:                }
1.9       chl       489:                *flags |= FLAGS_DID_NOTE;
1.8       tedu      490:                return size;
                    491:        }
1.4       ian       492:
1.8       tedu      493:        if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
1.9       chl       494:            xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
1.8       tedu      495:                uint32_t desc;
                    496:                (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
1.10      chl       497:                desc = elf_getu32(swap, desc);
1.8       tedu      498:                if (file_printf(ms, ", for FreeBSD") == -1)
                    499:                        return size;
1.1       millert   500:
1.8       tedu      501:                /*
                    502:                 * Contents is __FreeBSD_version, whose relation to OS
1.9       chl       503:                 * versions is defined by a huge table in the Porter's
                    504:                 * Handbook.  This is the general scheme:
                    505:                 *
                    506:                 * Releases:
                    507:                 *      Mmp000 (before 4.10)
                    508:                 *      Mmi0p0 (before 5.0)
                    509:                 *      Mmm0p0
                    510:                 *
                    511:                 * Development branches:
                    512:                 *      Mmpxxx (before 4.6)
                    513:                 *      Mmp1xx (before 4.10)
                    514:                 *      Mmi1xx (before 5.0)
                    515:                 *      M000xx (pre-M.0)
                    516:                 *      Mmm1xx
                    517:                 *
                    518:                 * M = major version
                    519:                 * m = minor version
                    520:                 * i = minor version increment (491000 -> 4.10)
                    521:                 * p = patchlevel
                    522:                 * x = revision
                    523:                 *
                    524:                 * The first release of FreeBSD to use ELF by default
                    525:                 * was version 3.0.
1.8       tedu      526:                 */
1.9       chl       527:                if (desc == 460002) {
                    528:                        if (file_printf(ms, " 4.6.2") == -1)
                    529:                                return size;
                    530:                } else if (desc < 460100) {
1.8       tedu      531:                        if (file_printf(ms, " %d.%d", desc / 100000,
                    532:                            desc / 10000 % 10) == -1)
                    533:                                return size;
                    534:                        if (desc / 1000 % 10 > 0)
                    535:                                if (file_printf(ms, ".%d", desc / 1000 % 10)
                    536:                                    == -1)
                    537:                                        return size;
1.9       chl       538:                        if ((desc % 1000 > 0) || (desc % 100000 == 0))
                    539:                                if (file_printf(ms, " (%d)", desc) == -1)
                    540:                                        return size;
                    541:                } else if (desc < 500000) {
                    542:                        if (file_printf(ms, " %d.%d", desc / 100000,
                    543:                            desc / 10000 % 10 + desc / 1000 % 10) == -1)
                    544:                                return size;
                    545:                        if (desc / 100 % 10 > 0) {
                    546:                                if (file_printf(ms, " (%d)", desc) == -1)
                    547:                                        return size;
                    548:                        } else if (desc / 10 % 10 > 0) {
                    549:                                if (file_printf(ms, ".%d", desc / 10 % 10)
                    550:                                    == -1)
                    551:                                        return size;
                    552:                        }
1.8       tedu      553:                } else {
                    554:                        if (file_printf(ms, " %d.%d", desc / 100000,
                    555:                            desc / 1000 % 100) == -1)
                    556:                                return size;
1.9       chl       557:                        if ((desc / 100 % 10 > 0) ||
                    558:                            (desc % 100000 / 100 == 0)) {
                    559:                                if (file_printf(ms, " (%d)", desc) == -1)
1.8       tedu      560:                                        return size;
1.9       chl       561:                        } else if (desc / 10 % 10 > 0) {
                    562:                                if (file_printf(ms, ".%d", desc / 10 % 10)
                    563:                                    == -1)
1.8       tedu      564:                                        return size;
1.4       ian       565:                        }
1.8       tedu      566:                }
1.9       chl       567:                *flags |= FLAGS_DID_NOTE;
1.8       tedu      568:                return size;
                    569:        }
                    570:
                    571:        if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
1.9       chl       572:            xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
1.8       tedu      573:                if (file_printf(ms, ", for OpenBSD") == -1)
                    574:                        return size;
                    575:                /* Content of note is always 0 */
1.9       chl       576:                *flags |= FLAGS_DID_NOTE;
1.8       tedu      577:                return size;
                    578:        }
                    579:
1.9       chl       580:        if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
                    581:            xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
                    582:                uint32_t desc;
                    583:                if (file_printf(ms, ", for DragonFly") == -1)
                    584:                        return size;
                    585:                (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
1.10      chl       586:                desc = elf_getu32(swap, desc);
1.9       chl       587:                if (file_printf(ms, " %d.%d.%d", desc / 100000,
                    588:                    desc / 10000 % 10, desc % 10000) == -1)
                    589:                        return size;
                    590:                *flags |= FLAGS_DID_NOTE;
                    591:                return size;
                    592:        }
                    593:
                    594: core:
1.8       tedu      595:        /*
                    596:         * Sigh.  The 2.0.36 kernel in Debian 2.1, at
                    597:         * least, doesn't correctly implement name
                    598:         * sections, in core dumps, as specified by
                    599:         * the "Program Linking" section of "UNIX(R) System
                    600:         * V Release 4 Programmer's Guide: ANSI C and
                    601:         * Programming Support Tools", because my copy
                    602:         * clearly says "The first 'namesz' bytes in 'name'
                    603:         * contain a *null-terminated* [emphasis mine]
                    604:         * character representation of the entry's owner
                    605:         * or originator", but the 2.0.36 kernel code
                    606:         * doesn't include the terminating null in the
                    607:         * name....
                    608:         */
                    609:        if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
                    610:            (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
                    611:                os_style = OS_STYLE_SVR4;
                    612:        }
                    613:
                    614:        if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
                    615:                os_style = OS_STYLE_FREEBSD;
                    616:        }
                    617:
                    618:        if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
                    619:            == 0)) {
                    620:                os_style = OS_STYLE_NETBSD;
                    621:        }
1.4       ian       622:
1.8       tedu      623: #ifdef ELFCORE
1.9       chl       624:        if ((*flags & FLAGS_DID_CORE) != 0)
                    625:                return size;
                    626:
1.10      chl       627:        if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
1.9       chl       628:                if (file_printf(ms, ", %s-style", os_style_names[os_style])
                    629:                    == -1)
1.8       tedu      630:                        return size;
1.10      chl       631:                *flags |= FLAGS_DID_CORE_STYLE;
1.9       chl       632:        }
1.4       ian       633:
1.9       chl       634:        switch (os_style) {
                    635:        case OS_STYLE_NETBSD:
                    636:                if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
                    637:                        uint32_t signo;
                    638:                        /*
                    639:                         * Extract the program name.  It is at
                    640:                         * offset 0x7c, and is up to 32-bytes,
                    641:                         * including the terminating NUL.
                    642:                         */
                    643:                        if (file_printf(ms, ", from '%.31s'",
                    644:                            &nbuf[doff + 0x7c]) == -1)
                    645:                                return size;
                    646:
                    647:                        /*
                    648:                         * Extract the signal number.  It is at
                    649:                         * offset 0x08.
                    650:                         */
                    651:                        (void)memcpy(&signo, &nbuf[doff + 0x08],
                    652:                            sizeof(signo));
                    653:                        if (file_printf(ms, " (signal %u)",
1.10      chl       654:                            elf_getu32(swap, signo)) == -1)
1.9       chl       655:                                return size;
1.10      chl       656:                        *flags |= FLAGS_DID_CORE;
1.8       tedu      657:                        return size;
1.9       chl       658:                }
                    659:                break;
1.8       tedu      660:
1.9       chl       661:        default:
                    662:                if (xnh_type == NT_PRPSINFO) {
                    663:                        size_t i, j;
                    664:                        unsigned char c;
                    665:                        /*
                    666:                         * Extract the program name.  We assume
                    667:                         * it to be 16 characters (that's what it
                    668:                         * is in SunOS 5.x and Linux).
                    669:                         *
                    670:                         * Unfortunately, it's at a different offset
                    671:                         * in various OSes, so try multiple offsets.
                    672:                         * If the characters aren't all printable,
                    673:                         * reject it.
                    674:                         */
                    675:                        for (i = 0; i < NOFFSETS; i++) {
1.10      chl       676:                                unsigned char *cname, *cp;
1.9       chl       677:                                size_t reloffset = prpsoffsets(i);
                    678:                                size_t noffset = doff + reloffset;
                    679:                                for (j = 0; j < 16; j++, noffset++,
                    680:                                    reloffset++) {
1.8       tedu      681:                                        /*
1.9       chl       682:                                         * Make sure we're not past
                    683:                                         * the end of the buffer; if
                    684:                                         * we are, just give up.
1.8       tedu      685:                                         */
1.9       chl       686:                                        if (noffset >= size)
1.8       tedu      687:                                                goto tryanother;
1.9       chl       688:
1.1       millert   689:                                        /*
1.9       chl       690:                                         * Make sure we're not past
                    691:                                         * the end of the contents;
                    692:                                         * if we are, this obviously
                    693:                                         * isn't the right offset.
1.1       millert   694:                                         */
1.9       chl       695:                                        if (reloffset >= descsz)
1.8       tedu      696:                                                goto tryanother;
1.9       chl       697:
                    698:                                        c = nbuf[noffset];
                    699:                                        if (c == '\0') {
                    700:                                                /*
                    701:                                                 * A '\0' at the
                    702:                                                 * beginning is
                    703:                                                 * obviously wrong.
                    704:                                                 * Any other '\0'
                    705:                                                 * means we're done.
                    706:                                                 */
                    707:                                                if (j == 0)
                    708:                                                        goto tryanother;
                    709:                                                else
                    710:                                                        break;
                    711:                                        } else {
                    712:                                                /*
                    713:                                                 * A nonprintable
                    714:                                                 * character is also
                    715:                                                 * wrong.
                    716:                                                 */
                    717:                                                if (!isprint(c) || isquote(c))
                    718:                                                        goto tryanother;
                    719:                                        }
1.8       tedu      720:                                }
1.9       chl       721:                                /*
                    722:                                 * Well, that worked.
                    723:                                 */
1.10      chl       724:                                cname = (unsigned char *)
                    725:                                    &nbuf[doff + prpsoffsets(i)];
                    726:                                for (cp = cname; *cp && isprint(*cp); cp++)
                    727:                                        continue;
                    728:                                /*
                    729:                                 * Linux apparently appends a space at the end
                    730:                                 * of the command line: remove it.
                    731:                                 */
                    732:                                while (cp > cname && isspace(cp[-1]))
                    733:                                        cp--;
                    734:                                if (file_printf(ms, ", from '%.*s'",
                    735:                                    (int)(cp - cname), cname) == -1)
1.9       chl       736:                                        return size;
1.10      chl       737:                                *flags |= FLAGS_DID_CORE;
1.8       tedu      738:                                return size;
                    739:
1.9       chl       740:                        tryanother:
                    741:                                ;
                    742:                        }
1.8       tedu      743:                }
1.9       chl       744:                break;
1.8       tedu      745:        }
                    746: #endif
                    747:        return offset;
                    748: }
                    749:
                    750: private int
                    751: doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
1.9       chl       752:     size_t size, int *flags)
1.8       tedu      753: {
                    754:        Elf32_Shdr sh32;
                    755:        Elf64_Shdr sh64;
1.9       chl       756:        int stripped = 1;
                    757:        void *nbuf;
                    758:        off_t noff;
1.8       tedu      759:
1.9       chl       760:        if (size != xsh_sizeof) {
1.8       tedu      761:                if (file_printf(ms, ", corrupted section header size") == -1)
                    762:                        return -1;
                    763:                return 0;
                    764:        }
                    765:
                    766:        if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
                    767:                file_badseek(ms);
                    768:                return -1;
                    769:        }
                    770:
                    771:        for ( ; num; num--) {
1.9       chl       772:                if (read(fd, xsh_addr, xsh_sizeof) == -1) {
1.8       tedu      773:                        file_badread(ms);
                    774:                        return -1;
                    775:                }
1.9       chl       776:                switch (xsh_type) {
                    777:                case SHT_SYMTAB:
                    778: #if 0
                    779:                case SHT_DYNSYM:
                    780: #endif
                    781:                        stripped = 0;
                    782:                        break;
                    783:                case SHT_NOTE:
                    784:                        if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
                    785:                            (off_t)-1) {
                    786:                                file_badread(ms);
                    787:                                return -1;
                    788:                        }
                    789:                        if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
                    790:                                file_error(ms, errno, "Cannot allocate memory"
                    791:                                    " for note");
                    792:                                return -1;
                    793:                        }
                    794:                        if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
                    795:                            (off_t)-1) {
                    796:                                file_badread(ms);
                    797:                                free(nbuf);
                    798:                                return -1;
                    799:                        }
                    800:                        if (read(fd, nbuf, (size_t)xsh_size) !=
                    801:                            (ssize_t)xsh_size) {
                    802:                                free(nbuf);
                    803:                                file_badread(ms);
                    804:                                return -1;
                    805:                        }
                    806:
                    807:                        noff = 0;
                    808:                        for (;;) {
                    809:                                if (noff >= (size_t)xsh_size)
                    810:                                        break;
                    811:                                noff = donote(ms, nbuf, (size_t)noff,
                    812:                                    (size_t)xsh_size, class, swap, 4,
                    813:                                    flags);
                    814:                                if (noff == 0)
                    815:                                        break;
                    816:                        }
                    817:                        if ((lseek(fd, off, SEEK_SET)) == (off_t)-1) {
                    818:                                free(nbuf);
                    819:                                file_badread(ms);
1.8       tedu      820:                                return -1;
1.9       chl       821:                        }
                    822:                        free(nbuf);
                    823:                        break;
1.8       tedu      824:                }
                    825:        }
1.9       chl       826:        if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1.8       tedu      827:                return -1;
                    828:        return 0;
                    829: }
                    830:
                    831: /*
                    832:  * Look through the program headers of an executable image, searching
                    833:  * for a PT_INTERP section; if one is found, it's dynamically linked,
                    834:  * otherwise it's statically linked.
                    835:  */
                    836: private int
                    837: dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
1.9       chl       838:     int num, size_t size, off_t fsize, int *flags)
1.8       tedu      839: {
                    840:        Elf32_Phdr ph32;
                    841:        Elf64_Phdr ph64;
                    842:        const char *linking_style = "statically";
                    843:        const char *shared_libraries = "";
                    844:        unsigned char nbuf[BUFSIZ];
                    845:        int bufsize;
                    846:        size_t offset, align;
1.9       chl       847:        off_t savedoffset = (off_t)-1;
                    848:        struct stat st;
1.8       tedu      849:
1.9       chl       850:        if (fstat(fd, &st) < 0) {
                    851:                file_badread(ms);
                    852:                return -1;
                    853:        }
                    854:
                    855:        if (size != xph_sizeof) {
1.8       tedu      856:                if (file_printf(ms, ", corrupted program header size") == -1)
1.10      chl       857:                        return -1;
1.8       tedu      858:                return 0;
                    859:        }
1.9       chl       860:
1.8       tedu      861:        if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
                    862:                file_badseek(ms);
                    863:                return -1;
                    864:        }
                    865:
                    866:        for ( ; num; num--) {
1.9       chl       867:                if (read(fd, xph_addr, xph_sizeof) == -1) {
1.8       tedu      868:                        file_badread(ms);
                    869:                        return -1;
                    870:                }
1.9       chl       871:                if (xph_offset > st.st_size && savedoffset != (off_t)-1) {
                    872:                        if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
                    873:                                file_badseek(ms);
                    874:                                return -1;
                    875:                        }
                    876:                        continue;
                    877:                }
                    878:
1.8       tedu      879:                if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
                    880:                        file_badseek(ms);
                    881:                        return -1;
                    882:                }
                    883:
1.9       chl       884:                if (xph_offset > fsize) {
                    885:                        if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
                    886:                                file_badseek(ms);
                    887:                                return -1;
                    888:                        }
                    889:                        continue;
                    890:                }
                    891:
                    892:                switch (xph_type) {
1.8       tedu      893:                case PT_DYNAMIC:
                    894:                        linking_style = "dynamically";
                    895:                        break;
                    896:                case PT_INTERP:
                    897:                        shared_libraries = " (uses shared libs)";
                    898:                        break;
                    899:                case PT_NOTE:
1.9       chl       900:                        if ((align = xph_align) & 0x80000000) {
1.8       tedu      901:                                if (file_printf(ms,
                    902:                                    ", invalid note alignment 0x%lx",
                    903:                                    (unsigned long)align) == -1)
                    904:                                        return -1;
                    905:                                align = 4;
                    906:                        }
                    907:                        /*
                    908:                         * This is a PT_NOTE section; loop through all the notes
                    909:                         * in the section.
                    910:                         */
1.9       chl       911:                        if (lseek(fd, xph_offset, SEEK_SET)
1.8       tedu      912:                            == (off_t)-1) {
                    913:                                file_badseek(ms);
                    914:                                return -1;
                    915:                        }
1.9       chl       916:                        bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
                    917:                            xph_filesz : sizeof(nbuf)));
1.8       tedu      918:                        if (bufsize == -1) {
                    919:                                file_badread(ms);
                    920:                                return -1;
                    921:                        }
                    922:                        offset = 0;
                    923:                        for (;;) {
                    924:                                if (offset >= (size_t)bufsize)
                    925:                                        break;
                    926:                                offset = donote(ms, nbuf, offset,
1.9       chl       927:                                    (size_t)bufsize, class, swap, align,
                    928:                                    flags);
1.8       tedu      929:                                if (offset == 0)
1.4       ian       930:                                        break;
1.1       millert   931:                        }
1.9       chl       932:                        if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
1.8       tedu      933:                                file_badseek(ms);
                    934:                                return -1;
                    935:                        }
                    936:                        break;
1.10      chl       937:                default:
                    938:                        break;
1.1       millert   939:                }
                    940:        }
1.8       tedu      941:        if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
                    942:            == -1)
                    943:            return -1;
                    944:        return 0;
1.1       millert   945: }
                    946:
1.8       tedu      947:
                    948: protected int
                    949: file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
                    950:     size_t nbytes)
1.1       millert   951: {
                    952:        union {
1.3       itojun    953:                int32_t l;
                    954:                char c[sizeof (int32_t)];
1.1       millert   955:        } u;
1.4       ian       956:        int class;
                    957:        int swap;
1.9       chl       958:        struct stat st;
                    959:        off_t fsize;
                    960:        int flags = 0;
1.10      chl       961:        Elf32_Ehdr elf32hdr;
                    962:        Elf64_Ehdr elf64hdr;
                    963:        uint16_t type;
                    964:
                    965:        if (ms->flags & MAGIC_MIME)
                    966:                return 0;
                    967:        /*
                    968:         * ELF executables have multiple section headers in arbitrary
                    969:         * file locations and thus file(1) cannot determine it from easily.
                    970:         * Instead we traverse thru all section headers until a symbol table
                    971:         * one is found or else the binary is stripped.
                    972:         * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
                    973:         */
                    974:        if (buf[EI_MAG0] != ELFMAG0
                    975:            || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
                    976:            || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
                    977:                return 0;
1.4       ian       978:
                    979:        /*
1.8       tedu      980:         * If we cannot seek, it must be a pipe, socket or fifo.
1.4       ian       981:         */
                    982:        if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1.8       tedu      983:                fd = file_pipe2file(ms, fd, buf, nbytes);
1.1       millert   984:
1.9       chl       985:        if (fstat(fd, &st) == -1) {
                    986:                file_badread(ms);
                    987:                return -1;
                    988:        }
                    989:        fsize = st.st_size;
                    990:
                    991:        class = buf[EI_CLASS];
1.4       ian       992:
1.10      chl       993:        switch (class) {
                    994:        case ELFCLASS32:
                    995: #undef elf_getu
                    996: #define elf_getu(a, b) elf_getu32(a, b)
                    997: #undef elfhdr
                    998: #define elfhdr elf32hdr
                    999: #include "elfclass.h"
                   1000:        case ELFCLASS64:
                   1001: #undef elf_getu
                   1002: #define elf_getu(a, b) elf_getu64(a, b)
                   1003: #undef elfhdr
                   1004: #define elfhdr elf64hdr
                   1005: #include "elfclass.h"
                   1006:        default:
                   1007:            if (file_printf(ms, ", unknown class %d", class) == -1)
                   1008:                    return -1;
                   1009:            break;
1.1       millert  1010:        }
1.8       tedu     1011:        return 0;
1.1       millert  1012: }
                   1013: #endif