~clint-fewbar/ubuntu/precise/erlang/merge-15b

« back to all changes in this revision

Viewing changes to erts/etc/win32/msys_tools/vc/coffix.c

  • Committer: Package Import Robot
  • Author(s): Sergei Golovan
  • Date: 2011-12-15 19:20:10 UTC
  • mfrom: (1.1.18) (3.5.15 sid)
  • mto: (3.5.16 sid)
  • mto: This revision was merged to the branch mainline in revision 33.
  • Revision ID: package-import@ubuntu.com-20111215192010-jnxcfe3tbrpp0big
Tags: 1:15.b-dfsg-1
* New upstream release.
* Upload to experimental because this release breaks external drivers
  API along with ABI, so several applications are to be fixed.
* Removed SSL patch because the old SSL implementation is removed from
  the upstream distribution.
* Removed never used patch which added native code to erlang beam files.
* Removed the erlang-docbuilder binary package because the docbuilder
  application was dropped by upstream.
* Documented dropping ${erlang-docbuilder:Depends} substvar in
  erlang-depends(1) manpage.
* Made erlang-base and erlang-base-hipe provide virtual package
  erlang-abi-15.b (the number means the first erlang version, which
  provides current ABI).

Show diffs side-by-side

added added

removed removed

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