lib.c 7.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/*
 *  Dump a COFF library (lib) file
 *
 * Copyright 2006 Dmitry Timoshkov
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"
#include "wine/port.h"

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <fcntl.h>

#define NONAMELESSUNION
#define NONAMELESSSTRUCT
#include "windef.h"
#include "winbase.h"
#include "winnt.h"

#include "winedump.h"

static void dump_import_object(const IMPORT_OBJECT_HEADER *ioh)
{
    if (ioh->Version >= 1)
    {
#if 0 /* FIXME: supposed to handle uuid.lib but it doesn't */
        const ANON_OBJECT_HEADER *aoh = (const ANON_OBJECT_HEADER *)ioh;

        printf("CLSID {%08x-%04x-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X}",
               aoh->ClassID.Data1, aoh->ClassID.Data2, aoh->ClassID.Data3,
               aoh->ClassID.Data4[0], aoh->ClassID.Data4[1], aoh->ClassID.Data4[2], aoh->ClassID.Data4[3],
               aoh->ClassID.Data4[4], aoh->ClassID.Data4[5], aoh->ClassID.Data4[6], aoh->ClassID.Data4[7]);
#endif
    }
    else
    {
        static const char * const obj_type[] = { "code", "data", "const" };
        static const char * const name_type[] = { "ordinal", "name", "no prefix", "undecorate" };
        const char *name;

        printf("  Version      : %X\n", ioh->Version);
        printf("  Machine      : %X (%s)\n", ioh->Machine, get_machine_str(ioh->Machine));
        printf("  TimeDateStamp: %08X %s\n", ioh->TimeDateStamp, get_time_str(ioh->TimeDateStamp));
        printf("  SizeOfData   : %08X\n", ioh->SizeOfData);
        name = (const char *)ioh + sizeof(*ioh);
        printf("  DLL name     : %s\n", name + strlen(name) + 1);
        printf("  Symbol name  : %s\n", name);
        printf("  Type         : %s\n", (ioh->Type < sizeof(obj_type)/sizeof(obj_type[0])) ? obj_type[ioh->Type] : "unknown");
        printf("  Name type    : %s\n", (ioh->NameType < sizeof(name_type)/sizeof(name_type[0])) ? name_type[ioh->NameType] : "unknown");
        printf("  %-13s: %u\n", (ioh->NameType == IMPORT_OBJECT_ORDINAL) ? "Ordinal" : "Hint", ioh->u.Ordinal);
        printf("\n");
    }
}

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
static void dump_long_import(const void *base, const IMAGE_SECTION_HEADER *ish, unsigned num_sect)
{
    unsigned i;
    const DWORD *imp_data5 = NULL;
    const WORD *imp_data6 = NULL;

    if (globals.do_dumpheader)
        printf("Section Table\n");

    for (i = 0; i < num_sect; i++)
    {
        if (globals.do_dumpheader)
            dump_section(&ish[i]);

        if (globals.do_dump_rawdata)
        {
            dump_data((const unsigned char *)base + ish[i].PointerToRawData, ish[i].SizeOfRawData, "    " );
            printf("\n");
        }

        if (!strcmp((const char *)ish[i].Name, ".idata$5"))
        {
            imp_data5 = (const DWORD *)((const char *)base + ish[i].PointerToRawData);
        }
        else if (!strcmp((const char *)ish[i].Name, ".idata$6"))
        {
            imp_data6 = (const WORD *)((const char *)base + ish[i].PointerToRawData);
        }
        else if (globals.do_debug && !strcmp((const char *)ish[i].Name, ".debug$S"))
        {
109
            const char *imp_debugS = (const char *)base + ish[i].PointerToRawData;
110

111
            codeview_dump_symbols(imp_debugS, ish[i].SizeOfRawData);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
            printf("\n");
        }
    }

    if (imp_data5)
    {
        WORD ordinal = 0;
        const char *name = NULL;

        if (imp_data5[0] & 0x80000000)
            ordinal = (WORD)(imp_data5[0] & ~0x80000000);

        if (imp_data6)
        {
            if (!ordinal) ordinal = imp_data6[0];
            name = (const char *)(imp_data6 + 1);
        }
        else
        {
            /* FIXME: find out a name in the section's data */
        }

        if (ordinal)
        {
            printf("  Symbol name  : %s\n", name ? name : "(ordinal import) /* FIXME */");
            printf("  %-13s: %u\n", (imp_data5[0] & 0x80000000) ? "Ordinal" : "Hint", ordinal);
            printf("\n");
        }
    }
}

143
enum FileSig get_kind_lib(void)
144
{
145 146 147 148 149
    const char*         arch = PRD(0, IMAGE_ARCHIVE_START_SIZE);
    if (arch && !strncmp(arch, IMAGE_ARCHIVE_START, IMAGE_ARCHIVE_START_SIZE))
        return SIG_COFFLIB;
    return SIG_UNKNOWN;
}
150

151 152 153 154
void lib_dump(void)
{
    long cur_file_pos;
    const IMAGE_ARCHIVE_MEMBER_HEADER *iamh;
155 156 157

    cur_file_pos = IMAGE_ARCHIVE_START_SIZE;

158
    for (;;)
159
    {
160
        const IMPORT_OBJECT_HEADER *ioh;
161 162
        long size;

163
        if (!(iamh = PRD(cur_file_pos, sizeof(*iamh)))) break;
164

165
        if (globals.do_dumpheader)
166
        {
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
            printf("Archive member name at %08lx\n", Offset(iamh));

            printf("Name %.16s", iamh->Name);
            if (!strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LINKER_MEMBER, sizeof(iamh->Name)))
            {
                printf(" - %s archive linker member\n",
                       cur_file_pos == IMAGE_ARCHIVE_START_SIZE ? "1st" : "2nd");
            }
            else
                printf("\n");
            printf("Date %.12s %s\n", iamh->Date, get_time_str(strtoul((const char *)iamh->Date, NULL, 10)));
            printf("UserID %.6s\n", iamh->UserID);
            printf("GroupID %.6s\n", iamh->GroupID);
            printf("Mode %.8s\n", iamh->Mode);
            printf("Size %.10s\n\n", iamh->Size);
182
        }
183 184 185

        cur_file_pos += sizeof(IMAGE_ARCHIVE_MEMBER_HEADER);

186 187 188
        size = strtoul((const char *)iamh->Size, NULL, 10);
        size = (size + 1) & ~1; /* align to an even address */

189 190 191
        /* FIXME: only import library contents with the short format are
         * recognized.
         */
192
        if (!(ioh = PRD(cur_file_pos, sizeof(*ioh)))) break;
193 194 195 196
        if (ioh->Sig1 == IMAGE_FILE_MACHINE_UNKNOWN && ioh->Sig2 == IMPORT_OBJECT_HDR_SIG2)
        {
            dump_import_object(ioh);
        }
197 198
        else if (strncmp((const char *)iamh->Name, IMAGE_ARCHIVE_LINKER_MEMBER, sizeof(iamh->Name)))
        {
199
            long expected_size;
200 201 202 203 204 205 206 207 208 209 210
            const IMAGE_FILE_HEADER *fh = (const IMAGE_FILE_HEADER *)ioh;

            if (globals.do_dumpheader)
            {
                dump_file_header(fh);
                if (fh->SizeOfOptionalHeader)
                {
                    const IMAGE_OPTIONAL_HEADER32 *oh = (const IMAGE_OPTIONAL_HEADER32 *)((const char *)fh + sizeof(*fh));
                    dump_optional_header(oh, fh->SizeOfOptionalHeader);
                }
            }
211 212 213 214
            /* Sanity check */
            expected_size = sizeof(*fh) + fh->SizeOfOptionalHeader + fh->NumberOfSections * sizeof(IMAGE_SECTION_HEADER);
            if (size > expected_size)
                dump_long_import(fh, (const IMAGE_SECTION_HEADER *)((const char *)fh + sizeof(*fh) + fh->SizeOfOptionalHeader), fh->NumberOfSections);
215
        }
216

217
        cur_file_pos += size;
218 219
    }
}