1*56bb7041Schristos /* Copyright (C) 2019-2020 Free Software Foundation, Inc.
2*56bb7041Schristos
3*56bb7041Schristos This file is part of GNU Binutils.
4*56bb7041Schristos
5*56bb7041Schristos This program is free software; you can redistribute it and/or modify
6*56bb7041Schristos it under the terms of the GNU General Public License as published by
7*56bb7041Schristos the Free Software Foundation; either version 3 of the License, or
8*56bb7041Schristos (at your option) any later version.
9*56bb7041Schristos
10*56bb7041Schristos This program is distributed in the hope that it will be useful,
11*56bb7041Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
12*56bb7041Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*56bb7041Schristos GNU General Public License for more details.
14*56bb7041Schristos
15*56bb7041Schristos You should have received a copy of the GNU General Public License
16*56bb7041Schristos along with this program; if not, write to the Free Software
17*56bb7041Schristos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
18*56bb7041Schristos 02110-1301, USA. */
19*56bb7041Schristos
20*56bb7041Schristos
21*56bb7041Schristos /* This file generates a number of DLL (PE/COFF binaries traditionally
22*56bb7041Schristos used on Windows) that we can then utilize in various tests to
23*56bb7041Schristos ensure objdump can parse these file correctly.
24*56bb7041Schristos
25*56bb7041Schristos See:
26*56bb7041Schristos https://www.ecma-international.org/publications/files/ECMA-ST/ECMA-335.pdf */
27*56bb7041Schristos
28*56bb7041Schristos #include <memory.h>
29*56bb7041Schristos #include <stdint.h>
30*56bb7041Schristos #include <stdio.h>
31*56bb7041Schristos #include <stdlib.h>
32*56bb7041Schristos #include <string.h>
33*56bb7041Schristos #include <unistd.h>
34*56bb7041Schristos
35*56bb7041Schristos #define INCORRECT_USAGE 2
36*56bb7041Schristos #define IO_ERROR 3
37*56bb7041Schristos
38*56bb7041Schristos static void
write_dos_header_and_stub(FILE * file)39*56bb7041Schristos write_dos_header_and_stub (FILE* file)
40*56bb7041Schristos {
41*56bb7041Schristos /* See ECMA-335 II.25.2.1.
42*56bb7041Schristos Instead of lfanew, lets just hardcode the offset of the next byte
43*56bb7041Schristos after this header (0x80). */
44*56bb7041Schristos char buffer[128] =
45*56bb7041Schristos {
46*56bb7041Schristos 0x4d, 0x5a, 0x90, 0x00, 0x03, 0x00, 0x00, 0x00,
47*56bb7041Schristos 0x04, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00,
48*56bb7041Schristos 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49*56bb7041Schristos 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50*56bb7041Schristos 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51*56bb7041Schristos 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52*56bb7041Schristos 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53*56bb7041Schristos 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, /* Last 4 bytes are precomputed lfanew. */
54*56bb7041Schristos 0x0e, 0x1f, 0xba, 0x0e, 0x00, 0xb4, 0x09, 0xcd,
55*56bb7041Schristos 0x21, 0xb8, 0x01, 0x4c, 0xcd, 0x21, 0x54, 0x68,
56*56bb7041Schristos 0x69, 0x73, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72,
57*56bb7041Schristos 0x61, 0x6d, 0x20, 0x63, 0x61, 0x6e, 0x6e, 0x6f,
58*56bb7041Schristos 0x74, 0x20, 0x62, 0x65, 0x20, 0x72, 0x75, 0x6e,
59*56bb7041Schristos 0x20, 0x69, 0x6e, 0x20, 0x44, 0x4f, 0x53, 0x20,
60*56bb7041Schristos 0x6d, 0x6f, 0x64, 0x65, 0x2e, 0x0d, 0x0d, 0x0a,
61*56bb7041Schristos 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
62*56bb7041Schristos };
63*56bb7041Schristos
64*56bb7041Schristos fwrite (buffer, 1, 128, file);
65*56bb7041Schristos }
66*56bb7041Schristos
67*56bb7041Schristos static void
write_pe_signature(FILE * file)68*56bb7041Schristos write_pe_signature (FILE* file)
69*56bb7041Schristos {
70*56bb7041Schristos char buffer[4];
71*56bb7041Schristos
72*56bb7041Schristos buffer[0] = 'P';
73*56bb7041Schristos buffer[1] = 'E';
74*56bb7041Schristos buffer[2] = 0;
75*56bb7041Schristos buffer[3] = 0;
76*56bb7041Schristos fwrite (buffer, 1, 4, file);
77*56bb7041Schristos }
78*56bb7041Schristos
79*56bb7041Schristos static void
write_coff_header(FILE * file,uint16_t machine)80*56bb7041Schristos write_coff_header (FILE* file, uint16_t machine)
81*56bb7041Schristos {
82*56bb7041Schristos char buffer[128];
83*56bb7041Schristos
84*56bb7041Schristos memset (buffer, 0, sizeof (buffer));
85*56bb7041Schristos
86*56bb7041Schristos /* Machine. ECMA-335 says this must be 0x14c but that's not true anymore. */
87*56bb7041Schristos buffer[0] = machine & 0xff;
88*56bb7041Schristos buffer[1] = machine >> 0x8;
89*56bb7041Schristos fwrite (buffer, 2, 1, file);
90*56bb7041Schristos memset (buffer, 0, sizeof (buffer));
91*56bb7041Schristos /* NumberOfSections = 0. */
92*56bb7041Schristos fwrite (buffer, 2, 1, file);
93*56bb7041Schristos /* TimeDateStamp = 0. */
94*56bb7041Schristos fwrite (buffer, 4, 1, file);
95*56bb7041Schristos /* PointerToSymbolTable = 0. */
96*56bb7041Schristos fwrite (buffer, 4, 1, file);
97*56bb7041Schristos /* NumberOfSymbols = 0. */
98*56bb7041Schristos fwrite (buffer, 4, 1, file);
99*56bb7041Schristos /* OptionalHeaderSize = 0. */
100*56bb7041Schristos fwrite (buffer, 2, 1, file);
101*56bb7041Schristos /* Characteristics = 0x2000. */
102*56bb7041Schristos buffer[0] = 0x00;
103*56bb7041Schristos buffer[1] = 0x20;
104*56bb7041Schristos fwrite (buffer, 2, 1, file);
105*56bb7041Schristos memset (buffer, 0 , sizeof (buffer));
106*56bb7041Schristos }
107*56bb7041Schristos
108*56bb7041Schristos static void
write_simple_dll(const char * name,uint16_t machine)109*56bb7041Schristos write_simple_dll (const char* name, uint16_t machine)
110*56bb7041Schristos {
111*56bb7041Schristos FILE* file = fopen (name, "w");
112*56bb7041Schristos
113*56bb7041Schristos if (file == NULL)
114*56bb7041Schristos {
115*56bb7041Schristos fprintf (stderr, "error: unable to open file for writing\n");
116*56bb7041Schristos exit (IO_ERROR);
117*56bb7041Schristos }
118*56bb7041Schristos
119*56bb7041Schristos write_dos_header_and_stub (file);
120*56bb7041Schristos write_pe_signature (file);
121*56bb7041Schristos write_coff_header (file, machine);
122*56bb7041Schristos fclose (file);
123*56bb7041Schristos file = NULL;
124*56bb7041Schristos printf ("wrote %s\n", name);
125*56bb7041Schristos }
126*56bb7041Schristos
127*56bb7041Schristos int
main(int argc,char ** argv)128*56bb7041Schristos main (int argc, char** argv)
129*56bb7041Schristos {
130*56bb7041Schristos char* program_name = argv[0];
131*56bb7041Schristos char* output_directory = argv[1];
132*56bb7041Schristos int i;
133*56bb7041Schristos
134*56bb7041Schristos if (argc < 3)
135*56bb7041Schristos {
136*56bb7041Schristos fprintf (stderr, "usage: %s output-directory format [format ...] \n\n", program_name);
137*56bb7041Schristos fprintf (stderr, "format is an objdump-style format string, like pei-i386\n");
138*56bb7041Schristos exit (INCORRECT_USAGE);
139*56bb7041Schristos }
140*56bb7041Schristos
141*56bb7041Schristos if (chdir (output_directory) != 0)
142*56bb7041Schristos {
143*56bb7041Schristos fprintf (stderr, "error: unable to change directory to %s\n", output_directory);
144*56bb7041Schristos exit (INCORRECT_USAGE);
145*56bb7041Schristos }
146*56bb7041Schristos
147*56bb7041Schristos /* We generate a simple PEI format files, and then .NET Core on
148*56bb7041Schristos Linux-style PEI files for a number of architectures. As opposed
149*56bb7041Schristos to the more common PEI files that contain bytecode (CIL/MSIL), many
150*56bb7041Schristos .NET Core DLLs are pre-compiled for specific architectures and
151*56bb7041Schristos platforms. See https://github.com/jbevain/cecil/issues/337 for an
152*56bb7041Schristos example of this value being used in practice. */
153*56bb7041Schristos
154*56bb7041Schristos for (i = 2; i < argc; i++)
155*56bb7041Schristos {
156*56bb7041Schristos char* wanted_format = argv[i];
157*56bb7041Schristos
158*56bb7041Schristos if (strcmp ("pei-i386", wanted_format) == 0)
159*56bb7041Schristos {
160*56bb7041Schristos write_simple_dll ("simple-pei-i386.dll", 0x14c);
161*56bb7041Schristos
162*56bb7041Schristos write_simple_dll ("linux-pei-i386.dll", 0x14c ^ 0x7b79 /* i386 + Linux */);
163*56bb7041Schristos }
164*56bb7041Schristos else if (strcmp ("pei-x86-64", wanted_format) == 0)
165*56bb7041Schristos {
166*56bb7041Schristos write_simple_dll ("simple-pei-x86-64.dll", 0x8664);
167*56bb7041Schristos
168*56bb7041Schristos write_simple_dll ("linux-pei-x86-64.dll", 0x8664 ^ 0x7b79 /* x86-64 + Linux */);
169*56bb7041Schristos }
170*56bb7041Schristos else
171*56bb7041Schristos {
172*56bb7041Schristos fprintf (stderr, "error: can't handle format %s\n", wanted_format);
173*56bb7041Schristos exit (INCORRECT_USAGE);
174*56bb7041Schristos }
175*56bb7041Schristos }
176*56bb7041Schristos
177*56bb7041Schristos return 0;
178*56bb7041Schristos }
179