~ubuntu-branches/debian/squeeze/erlang/squeeze

« back to all changes in this revision

Viewing changes to erts/etc/win32/cygwin_tools/coffix.c

  • Committer: Bazaar Package Importer
  • Author(s): Erlang Packagers, Sergei Golovan
  • Date: 2006-12-03 17:07:44 UTC
  • mfrom: (2.1.11 feisty)
  • Revision ID: james.westby@ubuntu.com-20061203170744-rghjwupacqlzs6kv
Tags: 1:11.b.2-4
[ Sergei Golovan ]
Fixed erlang-base and erlang-base-hipe prerm scripts.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ``The contents of this file are subject to the Erlang Public License,
2
 
 * Version 1.1, (the "License"); you may not use this file except in
3
 
 * compliance with the License. You should have received a copy of the
4
 
 * Erlang Public License along with this software. If not, it can be
5
 
 * retrieved via the world wide web at http://www.erlang.org/.
6
 
 * 
7
 
 * Software distributed under the License is distributed on an "AS IS"
8
 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9
 
 * the License for the specific language governing rights and limitations
10
 
 * under the License.
11
 
 * 
12
 
 * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
13
 
 * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
14
 
 * AB. All Rights Reserved.''
15
 
 * 
16
 
 *     $Id$
17
 
 */
18
 
/*
19
 
** This mini tool fixes an incompatibility between 
20
 
** Microsoft's tools, who dont like the virtual size being put in
21
 
** the physical address field, but rely on the raw size field for
22
 
** sizing the ".bss" section.
23
 
** This fixes some of the problems with linking gcc compiled objects
24
 
** together with MSVC dito.
25
 
**
26
 
** Courtesy DJ Delorie for describing the COFF file format on 
27
 
** http://www.delorie.com/djgpp/doc/coff/
28
 
** The coff structures are fetched from Microsofts headers though.
29
 
*/
30
 
 
31
 
#include <stdio.h>
32
 
#include <stdlib.h>
33
 
#include <string.h>
34
 
#include <stdarg.h>
35
 
 
36
 
#include <windows.h>
37
 
#include <winnt.h> /* Structure definitions for PE (COFF) */
38
 
 
39
 
static int dump_edit(char *filename, int edit);
40
 
static int v_printf(char *format, ...);
41
 
 
42
 
 
43
 
char *progname;
44
 
int verbouse = 0;
45
 
 
46
 
int main(int argc, char **argv) 
47
 
{
48
 
    int findex = 1;
49
 
    int edit = 0;
50
 
    int ret;
51
 
 
52
 
    progname = argv[0];
53
 
    if (argc == 1) {
54
 
        fprintf(stderr,"Format : %s [-e] [-v] <filename>\n", progname);
55
 
        return 1;
56
 
    }
57
 
    for (findex = 1; 
58
 
         findex < argc && (*argv[findex] == '-' || *argv[findex] == '/');
59
 
         ++findex)
60
 
        switch (argv[findex][1]) {
61
 
        case 'e':
62
 
        case 'E':
63
 
            edit = 1;
64
 
            break;
65
 
        case 'v':
66
 
        case 'V':
67
 
            verbouse = 1;
68
 
        default:
69
 
            fprintf(stderr, "%s: unknown option %s\n", progname, argv[findex]);
70
 
            break;
71
 
        }
72
 
    if (findex == argc) {
73
 
        fprintf(stderr,"%s: No filenames given.\n", progname);
74
 
        return 1;
75
 
    }
76
 
    for(; findex < argc; ++findex)
77
 
        if ((ret = dump_edit(argv[findex],edit)) != 0)
78
 
            return ret;
79
 
    return 0;
80
 
}
81
 
 
82
 
int dump_edit(char *filename, int edit) 
83
 
{
84
 
    FILE *f = fopen(filename, (edit) ? "r+b" : "rb");
85
 
    IMAGE_FILE_HEADER filhdr;
86
 
    IMAGE_SECTION_HEADER scnhdr;
87
 
    int i;
88
 
 
89
 
    if (f == NULL) {
90
 
        fprintf(stderr, "%s: cannot open %s.\n", progname, filename);
91
 
        return 1;
92
 
    }
93
 
    
94
 
    if (fread(&filhdr, sizeof(filhdr), 1, f) == 0) {
95
 
        fprintf(stderr,"%s: Could not read COFF header from %s,"
96
 
                " is this a PE (COFF) file?\n", progname, filename);
97
 
        fclose(f);
98
 
        return 1;
99
 
    }
100
 
    v_printf("File: %s\n", filename);
101
 
    v_printf("Magic number: 0x%08x\n", filhdr.Machine);
102
 
    v_printf("Number of sections: %d\n",filhdr.NumberOfSections);
103
 
    
104
 
    if (fseek(f, (long) filhdr.SizeOfOptionalHeader, SEEK_CUR) != 0) {
105
 
        fprintf(stderr,"%s: Could not read COFF optional header from %s,"
106
 
                " is this a PE (COFF) file?\n", progname, filename);
107
 
        fclose(f);
108
 
        return 1;
109
 
    }
110
 
    
111
 
    for (i = 0; i < filhdr.NumberOfSections; ++i) {
112
 
        if (fread(&scnhdr, sizeof(scnhdr), 1, f) == 0) {
113
 
            fprintf(stderr,"%s: Could not read section header from %s,"
114
 
                    " is this a PE (COFF) file?\n", progname, filename);
115
 
            fclose(f);
116
 
            return 1;
117
 
        }
118
 
        v_printf("Section %s:\n", scnhdr.Name);
119
 
        v_printf("Physical address: 0x%08x\n", scnhdr.Misc.PhysicalAddress);
120
 
        v_printf("Size: 0x%08x\n", scnhdr.SizeOfRawData);
121
 
        if (scnhdr.Misc.PhysicalAddress != 0 &&
122
 
            scnhdr.SizeOfRawData == 0) {
123
 
            printf("Section header %s in file %s will confuse MSC linker, "
124
 
                   "virtual size is 0x%08x and raw size is 0\n",
125
 
                   scnhdr.Name, filename, scnhdr.Misc.PhysicalAddress, 
126
 
                   scnhdr.SizeOfRawData);
127
 
            if (edit) {
128
 
                scnhdr.SizeOfRawData = scnhdr.Misc.PhysicalAddress;
129
 
                scnhdr.Misc.PhysicalAddress = 0;
130
 
                if (fseek(f, (long) -((long)sizeof(scnhdr)), SEEK_CUR) != 0 ||
131
 
                    fwrite(&scnhdr, sizeof(scnhdr), 1, f) == 0) {
132
 
                    fprintf(stderr,"%s: could not edit file %s.\n",
133
 
                            progname, filename);
134
 
                    fclose(f);
135
 
                    return 1;
136
 
                }
137
 
                printf("Edited object, virtual size is now 0, and "
138
 
                       "raw size is 0x%08x.\n", scnhdr.SizeOfRawData);
139
 
            } else {
140
 
                printf("Specify option '-e' to correct the problem.\n");
141
 
            }
142
 
        }
143
 
    }
144
 
    fclose(f);
145
 
    return 0;
146
 
}
147
 
            
148
 
 
149
 
static int v_printf(char *format, ...)
150
 
{
151
 
    va_list ap;
152
 
    int ret = 0;
153
 
    if (verbouse) {
154
 
        va_start(ap, format);
155
 
        ret = vfprintf(stdout, format, ap);
156
 
        va_end(ap);
157
 
    }
158
 
    return ret;
159
 
}
160