Annotation of embedaddon/php/ext/fileinfo/libmagic/readelf.c, revision 1.1.1.3

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>