Coverage Report

Created: 2026-05-11 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/binutils/fuzz_dlltool.h
Line
Count
Source
1
/* dlltool.c -- tool to generate stuff for PE style DLLs
2
   Copyright (C) 1995-2026 Free Software Foundation, Inc.
3
4
   This file is part of GNU Binutils.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
19
   02110-1301, USA.  */
20
21
22
/* This program allows you to build the files necessary to create
23
   DLLs to run on a system which understands PE format image files.
24
   (eg, Windows NT)
25
26
   See "Peering Inside the PE: A Tour of the Win32 Portable Executable
27
   File Format", MSJ 1994, Volume 9 for more information.
28
   Also see "Microsoft Portable Executable and Common Object File Format,
29
   Specification 4.1" for more information.
30
31
   A DLL contains an export table which contains the information
32
   which the runtime loader needs to tie up references from a
33
   referencing program.
34
35
   The export table is generated by this program by reading
36
   in a .DEF file or scanning the .a and .o files which will be in the
37
   DLL.  A .o file can contain information in special  ".drectve" sections
38
   with export information.
39
40
   A DEF file contains any number of the following commands:
41
42
43
   NAME <name> [ , <base> ]
44
   The result is going to be <name>.EXE
45
46
   LIBRARY <name> [ , <base> ]
47
   The result is going to be <name>.DLL
48
49
   EXPORTS  ( (  ( <name1> [ = <name2> ] )
50
               | ( <name1> = <module-name> . <external-name>))
51
            [ @ <integer> ] [ NONAME ] [CONSTANT] [DATA] [PRIVATE] ) *
52
   Declares name1 as an exported symbol from the
53
   DLL, with optional ordinal number <integer>.
54
   Or declares name1 as an alias (forward) of the function <external-name>
55
   in the DLL <module-name>.
56
57
   IMPORTS  (  (   <internal-name> =   <module-name> . <integer> )
58
             | ( [ <internal-name> = ] <module-name> . <external-name> )) *
59
   Declares that <external-name> or the exported function whose ordinal number
60
   is <integer> is to be imported from the file <module-name>.  If
61
   <internal-name> is specified then this is the name that the imported
62
   function will be refereed to in the body of the DLL.
63
64
   DESCRIPTION <string>
65
   Puts <string> into output .exp file in the .rdata section
66
67
   [STACKSIZE|HEAPSIZE] <number-reserve> [ , <number-commit> ]
68
   Generates --stack|--heap <number-reserve>,<number-commit>
69
   in the output .drectve section.  The linker will
70
   see this and act upon it.
71
72
   [CODE|DATA] <attr>+
73
   SECTIONS ( <sectionname> <attr>+ )*
74
   <attr> = READ | WRITE | EXECUTE | SHARED
75
   Generates --attr <sectionname> <attr> in the output
76
   .drectve section.  The linker will see this and act
77
   upon it.
78
79
80
   A -export:<name> in a .drectve section in an input .o or .a
81
   file to this program is equivalent to a EXPORTS <name>
82
   in a .DEF file.
83
84
85
86
   The program generates output files with the prefix supplied
87
   on the command line, or in the def file, or taken from the first
88
   supplied argument.
89
90
   The .exp.s file contains the information necessary to export
91
   the routines in the DLL.  The .lib.s file contains the information
92
   necessary to use the DLL's routines from a referencing program.
93
94
95
96
   Example:
97
98
 file1.c:
99
   asm (".section .drectve");
100
   asm (".ascii \"-export:adef\"");
101
102
   void adef (char * s)
103
   {
104
     printf ("hello from the dll %s\n", s);
105
   }
106
107
   void bdef (char * s)
108
   {
109
     printf ("hello from the dll and the other entry point %s\n", s);
110
   }
111
112
 file2.c:
113
   asm (".section .drectve");
114
   asm (".ascii \"-export:cdef\"");
115
   asm (".ascii \"-export:ddef\"");
116
117
   void cdef (char * s)
118
   {
119
     printf ("hello from the dll %s\n", s);
120
   }
121
122
   void ddef (char * s)
123
   {
124
     printf ("hello from the dll and the other entry point %s\n", s);
125
   }
126
127
   int printf (void)
128
   {
129
     return 9;
130
   }
131
132
 themain.c:
133
   int main (void)
134
   {
135
     cdef ();
136
     return 0;
137
   }
138
139
 thedll.def
140
141
   LIBRARY thedll
142
   HEAPSIZE 0x40000, 0x2000
143
   EXPORTS bdef @ 20
144
           cdef @ 30 NONAME
145
146
   SECTIONS donkey READ WRITE
147
   aardvark EXECUTE
148
149
 # Compile up the parts of the dll and the program
150
151
   gcc -c file1.c file2.c themain.c
152
153
 # Optional: put the dll objects into a library
154
 # (you don't have to, you could name all the object
155
 # files on the dlltool line)
156
157
   ar  qcv thedll.in file1.o file2.o
158
   ranlib thedll.in
159
160
 # Run this tool over the DLL's .def file and generate an exports
161
 # file (thedll.o) and an imports file (thedll.a).
162
 # (You may have to use -S to tell dlltool where to find the assembler).
163
164
   dlltool --def thedll.def --output-exp thedll.o --output-lib thedll.a
165
166
 # Build the dll with the library and the export table
167
168
   ld -o thedll.dll thedll.o thedll.in
169
170
 # Link the executable with the import library
171
172
   gcc -o themain.exe themain.o thedll.a
173
174
 This example can be extended if relocations are needed in the DLL:
175
176
 # Compile up the parts of the dll and the program
177
178
   gcc -c file1.c file2.c themain.c
179
180
 # Run this tool over the DLL's .def file and generate an imports file.
181
182
   dlltool --def thedll.def --output-lib thedll.lib
183
184
 # Link the executable with the import library and generate a base file
185
 # at the same time
186
187
   gcc -o themain.exe themain.o thedll.lib -Wl,--base-file -Wl,themain.base
188
189
 # Run this tool over the DLL's .def file and generate an exports file
190
 # which includes the relocations from the base file.
191
192
   dlltool --def thedll.def --base-file themain.base --output-exp thedll.exp
193
194
 # Build the dll with file1.o, file2.o and the export table
195
196
   ld -o thedll.dll thedll.exp file1.o file2.o  */
197
198
/* .idata section description
199
200
   The .idata section is the import table.  It is a collection of several
201
   subsections used to keep the pieces for each dll together: .idata$[234567].
202
   IE: Each dll's .idata$2's are catenated together, each .idata$3's, etc.
203
204
   .idata$2 = Import Directory Table
205
   = array of IMAGE_IMPORT_DESCRIPTOR's.
206
207
  DWORD   Import Lookup Table;  - pointer to .idata$4
208
  DWORD   TimeDateStamp;        - currently always 0
209
  DWORD   ForwarderChain;       - currently always 0
210
  DWORD   Name;                 - pointer to dll's name
211
  PIMAGE_THUNK_DATA FirstThunk; - pointer to .idata$5
212
213
   .idata$3 = null terminating entry for .idata$2.
214
215
   .idata$4 = Import Lookup Table
216
   = array of array of numbers, which has meaning based on its highest bit:
217
     - when cleared - pointer to entry in Hint Name Table
218
     - when set - 16-bit function's ordinal number (rest of the bits are zeros)
219
   Function ordinal number subtracted by Export Directory Table's
220
   Ordinal Base is an index entry into the Export Address Table.
221
   There is one for each dll being imported from, and each dll's set is
222
   terminated by a trailing NULL.
223
224
   .idata$5 = Import Address Table
225
   There is one for each dll being imported from, and each dll's set is
226
   terminated by a trailing NULL.
227
   Initially, this table is identical to the Import Lookup Table.  However,
228
   at load time, the loader overwrites the entries with the address of the
229
   function.
230
231
   .idata$6 = Hint Name Table
232
   = Array of { short, asciz } entries, one for each imported function.
233
   The `short' is the name hint - index into Export Name Pointer Table.
234
   The `asciz` is the name string - value in Export Name Table referenced
235
   by some entry in Export Name Pointer Table.  Name hint should be the
236
   index of that entry in Export Name Pointer Table.  It has no connection
237
   with the function's ordinal number.
238
239
   .idata$7 = dll name (eg: "kernel32.dll").  */
240
241
#include "sysdep.h"
242
#include "bfd.h"
243
#include "libiberty.h"
244
#include "getopt.h"
245
#include "demangle.h"
246
#include "dyn-string.h"
247
#include "bucomm.h"
248
#include "dlltool.h"
249
#include "safe-ctype.h"
250
#include "coff-bfd.h"
251
252
#include <time.h>
253
#include <assert.h>
254
255
#ifdef DLLTOOL_ARM
256
#include "coff/arm.h"
257
#include "coff/internal.h"
258
#endif
259
#ifdef DLLTOOL_DEFAULT_MX86_64
260
#include "coff/x86_64.h"
261
#endif
262
#ifdef DLLTOOL_DEFAULT_I386
263
#include "coff/i386.h"
264
#endif
265
266
#ifndef COFF_PAGE_SIZE
267
#define COFF_PAGE_SIZE ((bfd_vma) 4096)
268
#endif
269
270
#ifndef PAGE_MASK
271
0
#define PAGE_MASK ((bfd_vma) (- COFF_PAGE_SIZE))
272
#endif
273
274
#ifndef NAME_MAX
275
#define NAME_MAX  255
276
#endif
277
278
#ifdef HAVE_SYS_WAIT_H
279
#include <sys/wait.h>
280
#else /* ! HAVE_SYS_WAIT_H */
281
#if ! defined (_WIN32) || defined (__CYGWIN32__)
282
#ifndef WIFEXITED
283
#define WIFEXITED(w)  (((w) & 0377) == 0)
284
#endif
285
#ifndef WIFSIGNALED
286
#define WIFSIGNALED(w)  (((w) & 0377) != 0177 && ((w) & ~0377) == 0)
287
#endif
288
#ifndef WTERMSIG
289
#define WTERMSIG(w) ((w) & 0177)
290
#endif
291
#ifndef WEXITSTATUS
292
#define WEXITSTATUS(w)  (((w) >> 8) & 0377)
293
#endif
294
#else /* defined (_WIN32) && ! defined (__CYGWIN32__) */
295
#ifndef WIFEXITED
296
#define WIFEXITED(w)  (((w) & 0xff) == 0)
297
#endif
298
#ifndef WIFSIGNALED
299
#define WIFSIGNALED(w)  (((w) & 0xff) != 0 && ((w) & 0xff) != 0x7f)
300
#endif
301
#ifndef WTERMSIG
302
#define WTERMSIG(w) ((w) & 0x7f)
303
#endif
304
#ifndef WEXITSTATUS
305
#define WEXITSTATUS(w)  (((w) & 0xff00) >> 8)
306
#endif
307
#endif /* defined (_WIN32) && ! defined (__CYGWIN32__) */
308
#endif /* ! HAVE_SYS_WAIT_H */
309
310
0
#define show_allnames 0
311
312
/* ifunc and ihead data structures: ttk@cygnus.com 1997
313
314
   When IMPORT declarations are encountered in a .def file the
315
   function import information is stored in a structure referenced by
316
   the global variable IMPORT_LIST.  The structure is a linked list
317
   containing the names of the dll files each function is imported
318
   from and a linked list of functions being imported from that dll
319
   file.  This roughly parallels the structure of the .idata section
320
   in the PE object file.
321
322
   The contents of .def file are interpreted from within the
323
   process_def_file function.  Every time an IMPORT declaration is
324
   encountered, it is broken up into its component parts and passed to
325
   def_import.  IMPORT_LIST is initialized to NULL in function main.  */
326
327
typedef struct ifunct
328
{
329
  const char *   name;  /* Name of function being imported.  */
330
  const char *its_name; /* Optional import table symbol name.  */
331
  int    ord; /* Two-byte ordinal value associated with function.  */
332
  struct ifunct *next;
333
} ifunctype;
334
335
typedef struct iheadt
336
{
337
  const char *   dllname;  /* Name of dll file imported from.  */
338
  long     nfuncs;   /* Number of functions in list.  */
339
  struct ifunct *funchead; /* First function in list.  */
340
  struct ifunct *functail; /* Last  function in list.  */
341
  struct iheadt *next;     /* Next dll file in list.  */
342
} iheadtype;
343
344
/* Structure containing all import information as defined in .def file
345
   (qv "ihead structure").  */
346
347
static iheadtype *import_list = NULL;
348
static char *as_name = NULL;
349
static char * as_flags = "";
350
static char *tmp_prefix = NULL;
351
static int no_idata4;
352
static int no_idata5;
353
static char *exp_name;
354
static char *imp_name;
355
static char *delayimp_name;
356
static char *identify_imp_name;
357
static bool identify_strict;
358
static bool deterministic = DEFAULT_AR_DETERMINISTIC;
359
360
/* Types used to implement a linked list of dllnames associated
361
   with the specified import lib. Used by the identify_* code.
362
   The head entry is acts as a sentinal node and is always empty
363
   (head->dllname is NULL).  */
364
typedef struct dll_name_list_node_t
365
{
366
  char *      dllname;
367
  struct dll_name_list_node_t * next;
368
} dll_name_list_node_type;
369
370
typedef struct dll_name_list_t
371
{
372
  dll_name_list_node_type * head;
373
  dll_name_list_node_type * tail;
374
} dll_name_list_type;
375
376
/* Types used to pass data to iterator functions.  */
377
typedef struct symname_search_data_t
378
{
379
  const char *symname;
380
  bool found;
381
} symname_search_data_type;
382
383
typedef struct identify_data_t
384
{
385
   dll_name_list_type *list;
386
   bool ms_style_implib;
387
} identify_data_type;
388
389
390
static char *head_label;
391
static char *imp_name_lab;
392
static char *dll_name;
393
static int dll_name_set_by_exp_name;
394
static int add_indirect = 0;
395
static int add_underscore = 0;
396
static int add_stdcall_underscore = 0;
397
static char *leading_underscore = NULL;
398
static int dontdeltemps = 0;
399
400
/* TRUE if we should export all symbols.  Otherwise, we only export
401
   symbols listed in .drectve sections or in the def file.  */
402
static bool export_all_symbols;
403
404
/* TRUE if we should exclude the symbols in DEFAULT_EXCLUDES when
405
   exporting all symbols.  */
406
static bool do_default_excludes = true;
407
408
static bool use_nul_prefixed_import_tables = false;
409
410
/* Default symbols to exclude when exporting all the symbols.  */
411
static const char *default_excludes = "DllMain@12,DllEntryPoint@0,impure_ptr";
412
413
/* TRUE if we should add __imp_<SYMBOL> to import libraries for backward
414
   compatibility to old Cygwin releases.  */
415
static bool create_compat_implib;
416
417
/* TRUE if we have to write PE+ import libraries.  */
418
static bool create_for_pep;
419
420
static char *def_file;
421
422
extern char * program_name;
423
424
static int machine;
425
static int killat;
426
static int add_stdcall_alias;
427
static const char *ext_prefix_alias;
428
static int verbose;
429
static FILE *output_def;
430
static FILE *base_file;
431
432
#ifdef DLLTOOL_DEFAULT_ARM
433
static const char *mname = "arm";
434
#endif
435
436
#ifdef DLLTOOL_DEFAULT_ARM_WINCE
437
static const char *mname = "arm-wince";
438
#endif
439
440
#ifdef DLLTOOL_DEFAULT_AARCH64
441
/* arm64 rather than aarch64 to match llvm-dlltool */
442
static const char *mname = "arm64";
443
#endif
444
445
#ifdef DLLTOOL_DEFAULT_I386
446
static const char *mname = "i386";
447
#endif
448
449
#ifdef DLLTOOL_DEFAULT_MX86_64
450
static const char *mname = "i386:x86-64";
451
#endif
452
453
#ifdef DLLTOOL_DEFAULT_SH
454
static const char *mname = "sh";
455
#endif
456
457
#ifdef DLLTOOL_DEFAULT_MIPS
458
static const char *mname = "mips";
459
#endif
460
461
#ifdef DLLTOOL_DEFAULT_MCORE
462
static const char * mname = "mcore-le";
463
#endif
464
465
#ifdef DLLTOOL_DEFAULT_MCORE_ELF
466
static const char * mname = "mcore-elf";
467
static char * mcore_elf_out_file = NULL;
468
static char * mcore_elf_linker   = NULL;
469
static char * mcore_elf_linker_flags = NULL;
470
471
#define DRECTVE_SECTION_NAME ((machine == MMCORE_ELF || machine == MMCORE_ELF_LE) ? ".exports" : ".drectve")
472
#endif
473
474
#ifndef DRECTVE_SECTION_NAME
475
316
#define DRECTVE_SECTION_NAME ".drectve"
476
#endif
477
478
/* External name alias numbering starts here.  */
479
0
#define PREFIX_ALIAS_BASE 20000
480
481
static char *tmp_asm_buf;
482
static char *tmp_head_s_buf;
483
static char *tmp_head_o_buf;
484
static char *tmp_tail_s_buf;
485
static char *tmp_tail_o_buf;
486
static char *tmp_stub_buf;
487
488
0
#define TMP_ASM   dlltmp (&tmp_asm_buf, "%sc.s")
489
0
#define TMP_HEAD_S  dlltmp (&tmp_head_s_buf, "%sh.s")
490
0
#define TMP_HEAD_O  dlltmp (&tmp_head_o_buf, "%sh.o")
491
0
#define TMP_TAIL_S  dlltmp (&tmp_tail_s_buf, "%st.s")
492
0
#define TMP_TAIL_O  dlltmp (&tmp_tail_o_buf, "%st.o")
493
0
#define TMP_STUB  dlltmp (&tmp_stub_buf, "%ssnnnnn.o")
494
495
/* This bit of assembly does jmp * ....  */
496
static const unsigned char i386_jtab[] =
497
{
498
  0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
499
};
500
501
static const unsigned char i386_dljtab[] =
502
{
503
  0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, /* jmp __imp__function     */
504
  0xB8, 0x00, 0x00, 0x00, 0x00,       /* mov eax, offset __imp__function */
505
  0xE9, 0x00, 0x00, 0x00, 0x00        /* jmp __tailMerge__dllname  */
506
};
507
508
static const unsigned char i386_x64_dljtab[] =
509
{
510
  0xFF, 0x25, 0x00, 0x00, 0x00, 0x00, /* jmp __imp__function     */
511
  0x48, 0x8d, 0x05,         /* leaq rax, (__imp__function)   */
512
  0x00, 0x00, 0x00, 0x00,
513
  0xE9, 0x00, 0x00, 0x00, 0x00        /* jmp __tailMerge__dllname  */
514
};
515
516
static const unsigned char arm_jtab[] =
517
{
518
  0x00, 0xc0, 0x9f, 0xe5, /* ldr  ip, [pc] */
519
  0x00, 0xf0, 0x9c, 0xe5, /* ldr  pc, [ip] */
520
  0,  0,    0,    0
521
};
522
523
static const unsigned char arm_interwork_jtab[] =
524
{
525
  0x04, 0xc0, 0x9f, 0xe5, /* ldr  ip, [pc] */
526
  0x00, 0xc0, 0x9c, 0xe5, /* ldr  ip, [ip] */
527
  0x1c, 0xff, 0x2f, 0xe1, /* bx ip   */
528
  0,  0,    0,    0
529
};
530
531
static const unsigned char thumb_jtab[] =
532
{
533
  0x40, 0xb4,   /* push {r6}       */
534
  0x02, 0x4e,   /* ldr  r6, [pc, #8] */
535
  0x36, 0x68,   /* ldr  r6, [r6]     */
536
  0xb4, 0x46,   /* mov  ip, r6       */
537
  0x40, 0xbc,   /* pop  {r6}       */
538
  0x60, 0x47,   /* bx ip       */
539
  0,  0,    0,    0
540
};
541
542
static const unsigned char mcore_be_jtab[] =
543
{
544
  0x71, 0x02,    /* lrw r1,2     */
545
  0x81, 0x01,    /* ld.w r1,(r1,0) */
546
  0x00, 0xC1,    /* jmp r1     */
547
  0x12, 0x00,    /* nop      */
548
  0x00, 0x00, 0x00, 0x00 /* <address>    */
549
};
550
551
static const unsigned char mcore_le_jtab[] =
552
{
553
  0x02, 0x71,    /* lrw r1,2     */
554
  0x01, 0x81,    /* ld.w r1,(r1,0) */
555
  0xC1, 0x00,    /* jmp r1     */
556
  0x00, 0x12,    /* nop      */
557
  0x00, 0x00, 0x00, 0x00 /* <address>    */
558
};
559
560
static const unsigned char aarch64_jtab[] =
561
{
562
  0x10, 0x00, 0x00, 0x90, /* adrp x16, 0  */
563
  0x10, 0x02, 0x00, 0x91, /* add x16, x16, #0x0 */
564
  0x10, 0x02, 0x40, 0xf9, /* ldr x16, [x16] */
565
  0x00, 0x02, 0x1f, 0xd6  /* br x16   */
566
};
567
568
static const char i386_trampoline[] =
569
  "\tpushl %%ecx\n"
570
  "\tpushl %%edx\n"
571
  "\tpushl %%eax\n"
572
  "\tpushl $__DELAY_IMPORT_DESCRIPTOR_%s\n"
573
  "\tcall ___delayLoadHelper2@8\n"
574
  "\tpopl %%edx\n"
575
  "\tpopl %%ecx\n"
576
  "\tjmp *%%eax\n";
577
578
/* Save integer arg regs in parameter space reserved by our caller
579
   above the return address.  Allocate space for six fp arg regs plus
580
   parameter space possibly used by __delayLoadHelper2 plus alignment.
581
   We enter with the stack offset from 16-byte alignment by the return
582
   address, so allocate 96 + 32 + 8 = 136 bytes.  Note that only the
583
   first four xmm regs are used to pass fp args, but the first six
584
   vector ymm (zmm too?) are used to pass vector args.  We are
585
   assuming that volatile vector regs are not modified inside
586
   __delayLoadHelper2.  However, it is known that at least xmm0 and
587
   xmm1 are trashed in some versions of Microsoft dlls, and if xmm4 or
588
   xmm5 are also used then that would trash the lower bits of ymm4 and
589
   ymm5.  If it turns out that vector insns with a vex prefix are used
590
   then we'll need to save ymm0-5 here but that can't be done without
591
   first testing cpuid and xcr0.  */
592
static const char i386_x64_trampoline[] =
593
  "\tsubq $136, %%rsp\n"
594
  "\t.seh_stackalloc 136\n"
595
  "\t.seh_endprologue\n"
596
  "\tmovq %%rcx, 136+8(%%rsp)\n"
597
  "\tmovq %%rdx, 136+16(%%rsp)\n"
598
  "\tmovq %%r8, 136+24(%%rsp)\n"
599
  "\tmovq %%r9, 136+32(%%rsp)\n"
600
  "\tmovaps %%xmm0, 32(%%rsp)\n"
601
  "\tmovaps %%xmm1, 48(%%rsp)\n"
602
  "\tmovaps %%xmm2, 64(%%rsp)\n"
603
  "\tmovaps %%xmm3, 80(%%rsp)\n"
604
  "\tmovaps %%xmm4, 96(%%rsp)\n"
605
  "\tmovaps %%xmm5, 112(%%rsp)\n"
606
  "\tmovq %%rax, %%rdx\n"
607
  "\tleaq __DELAY_IMPORT_DESCRIPTOR_%s(%%rip), %%rcx\n"
608
  "\tcall __delayLoadHelper2\n"
609
  "\tmovq 136+8(%%rsp), %%rcx\n"
610
  "\tmovq 136+16(%%rsp), %%rdx\n"
611
  "\tmovq 136+24(%%rsp), %%r8\n"
612
  "\tmovq 136+32(%%rsp), %%r9\n"
613
  "\tmovaps 32(%%rsp), %%xmm0\n"
614
  "\tmovaps 48(%%rsp), %%xmm1\n"
615
  "\tmovaps 64(%%rsp), %%xmm2\n"
616
  "\tmovaps 80(%%rsp), %%xmm3\n"
617
  "\tmovaps 96(%%rsp), %%xmm4\n"
618
  "\tmovaps 112(%%rsp), %%xmm5\n"
619
  "\taddq $136, %%rsp\n"
620
  "\tjmp *%%rax\n";
621
622
struct mac
623
{
624
  const char *type;
625
  const char *how_byte;
626
  const char *how_short;
627
  const char *how_long;
628
  const char *how_asciz;
629
  const char *how_comment;
630
  const char *how_jump;
631
  const char *how_global;
632
  const char *how_space;
633
  const char *how_align_short;
634
  const char *how_align_long;
635
  const char *how_default_as_switches;
636
  const char *how_bfd_target;
637
  enum bfd_architecture how_bfd_arch;
638
  const unsigned char *how_jtab;
639
  int how_jtab_size; /* Size of the jtab entry.  */
640
  int how_jtab_roff; /* Offset into it for the ind 32 reloc into idata 5.  */
641
  const unsigned char *how_dljtab;
642
  int how_dljtab_size; /* Size of the dljtab entry.  */
643
  int how_dljtab_roff1; /* Offset for the ind 32 reloc into idata 5.  */
644
  int how_dljtab_roff2; /* Offset for the ind 32 reloc into idata 5.  */
645
  int how_dljtab_roff3; /* Offset for the ind 32 reloc into idata 5.  */
646
  bool how_seh;
647
  const char *trampoline;
648
};
649
650
static const struct mac
651
mtable[] =
652
{
653
  {
654
0
#define MARM 0
655
    "arm", ".byte", ".short", ".long", ".asciz", "@",
656
    "ldr\tip,[pc]\n\tldr\tpc,[ip]\n\t.long",
657
    ".global", ".space", ".align\t2", ".align\t4", "-mapcs-32",
658
    "pe-arm-little", bfd_arch_arm,
659
    arm_jtab, sizeof (arm_jtab), 8,
660
    0, 0, 0, 0, 0, false, 0
661
  }
662
  ,
663
  {
664
0
#define M386 1
665
    "i386", ".byte", ".short", ".long", ".asciz", "#",
666
    "jmp *", ".global", ".space", ".align\t2", ".align\t4", "",
667
    "pe-i386",bfd_arch_i386,
668
    i386_jtab, sizeof (i386_jtab), 2,
669
    i386_dljtab, sizeof (i386_dljtab), 2, 7, 12, false, i386_trampoline
670
  }
671
  ,
672
  {
673
0
#define MTHUMB 2
674
    "thumb", ".byte", ".short", ".long", ".asciz", "@",
675
    "push\t{r6}\n\tldr\tr6, [pc, #8]\n\tldr\tr6, [r6]\n\tmov\tip, r6\n\tpop\t{r6}\n\tbx\tip",
676
    ".global", ".space", ".align\t2", ".align\t4", "-mthumb-interwork",
677
    "pe-arm-little", bfd_arch_arm,
678
    thumb_jtab, sizeof (thumb_jtab), 12,
679
    0, 0, 0, 0, 0, false, 0
680
  }
681
  ,
682
0
#define MARM_INTERWORK 3
683
  {
684
    "arm_interwork", ".byte", ".short", ".long", ".asciz", "@",
685
    "ldr\tip,[pc]\n\tldr\tip,[ip]\n\tbx\tip\n\t.long",
686
    ".global", ".space", ".align\t2", ".align\t4", "-mthumb-interwork",
687
    "pe-arm-little", bfd_arch_arm,
688
    arm_interwork_jtab, sizeof (arm_interwork_jtab), 12,
689
    0, 0, 0, 0, 0, false, 0
690
  }
691
  ,
692
  {
693
0
#define MMCORE_BE 4
694
    "mcore-be", ".byte", ".short", ".long", ".asciz", "//",
695
    "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
696
    ".global", ".space", ".align\t2", ".align\t4", "",
697
    "pe-mcore-big", bfd_arch_mcore,
698
    mcore_be_jtab, sizeof (mcore_be_jtab), 8,
699
    0, 0, 0, 0, 0, false, 0
700
  }
701
  ,
702
  {
703
0
#define MMCORE_LE 5
704
    "mcore-le", ".byte", ".short", ".long", ".asciz", "//",
705
    "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
706
    ".global", ".space", ".align\t2", ".align\t4", "-EL",
707
    "pe-mcore-little", bfd_arch_mcore,
708
    mcore_le_jtab, sizeof (mcore_le_jtab), 8,
709
    0, 0, 0, 0, 0, false, 0
710
  }
711
  ,
712
  {
713
0
#define MMCORE_ELF 6
714
    "mcore-elf-be", ".byte", ".short", ".long", ".asciz", "//",
715
    "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
716
    ".global", ".space", ".align\t2", ".align\t4", "",
717
    "elf32-mcore-big", bfd_arch_mcore,
718
    mcore_be_jtab, sizeof (mcore_be_jtab), 8,
719
    0, 0, 0, 0, 0, false, 0
720
  }
721
  ,
722
  {
723
0
#define MMCORE_ELF_LE 7
724
    "mcore-elf-le", ".byte", ".short", ".long", ".asciz", "//",
725
    "lrw r1,[1f]\n\tld.w r1,(r1,0)\n\tjmp r1\n\tnop\n1:.long",
726
    ".global", ".space", ".align\t2", ".align\t4", "-EL",
727
    "elf32-mcore-little", bfd_arch_mcore,
728
    mcore_le_jtab, sizeof (mcore_le_jtab), 8,
729
    0, 0, 0, 0, 0, false, 0
730
  }
731
  ,
732
  {
733
0
#define MARM_WINCE 8
734
    "arm-wince", ".byte", ".short", ".long", ".asciz", "@",
735
    "ldr\tip,[pc]\n\tldr\tpc,[ip]\n\t.long",
736
    ".global", ".space", ".align\t2", ".align\t4", "-mapcs-32",
737
    "pe-arm-wince-little", bfd_arch_arm,
738
    arm_jtab, sizeof (arm_jtab), 8,
739
    0, 0, 0, 0, 0, false, 0
740
  }
741
  ,
742
  {
743
0
#define MX86 9
744
    "i386:x86-64", ".byte", ".short", ".long", ".asciz", "#",
745
    "jmp *", ".global", ".space", ".align\t2", ".align\t4", "",
746
    "pe-x86-64",bfd_arch_i386,
747
    i386_jtab, sizeof (i386_jtab), 2,
748
    i386_x64_dljtab, sizeof (i386_x64_dljtab), 2, 9, 14, true, i386_x64_trampoline
749
  }
750
  ,
751
  {
752
0
#define MAARCH64 10
753
    "arm64", ".byte", ".short", ".long", ".asciz", "//",
754
    "bl ", ".global", ".space", ".balign\t2", ".balign\t4", "",
755
    "pe-aarch64-little", bfd_arch_aarch64,
756
    aarch64_jtab, sizeof (aarch64_jtab), 0,
757
    0, 0, 0, 0, 0, false, 0
758
  }
759
  ,
760
  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
761
};
762
763
typedef struct dlist
764
{
765
  const char *text;
766
  struct dlist *next;
767
}
768
dlist_type;
769
770
typedef struct export
771
{
772
  const char *name;
773
  const char *internal_name;
774
  const char *import_name;
775
  const char *its_name;
776
  int ordinal;
777
  int constant;
778
  int noname;   /* Don't put name in image file.  */
779
  int private;  /* Don't put reference in import lib.  */
780
  int data;
781
  int hint;
782
  int forward;  /* Number of forward label, 0 means no forward.  */
783
  struct export *next;
784
}
785
export_type;
786
787
/* A list of symbols which we should not export.  */
788
789
struct string_list
790
{
791
  struct string_list *next;
792
  char *string;
793
};
794
795
static struct string_list *excludes;
796
797
/* Forward references.  */
798
static char *deduce_name (const char *);
799
static char *xlate (const char *);
800
static void dll_name_list_free_contents (dll_name_list_node_type *);
801
static void identify_search_archive
802
  (bfd *, void (*) (bfd *, bfd *, void *),  void *);
803
static void identify_search_member (bfd *, bfd *, void *);
804
static void identify_search_section (bfd *, asection *, void *);
805
static void inform (const char *, ...) ATTRIBUTE_PRINTF_1;
806
807
#ifdef DLLTOOL_MCORE_ELF
808
static void mcore_elf_cache_filename (const char *);
809
static void mcore_elf_gen_out_file (void);
810
#endif
811
812
/* Get current BFD error message.  */
813
static inline const char *
814
bfd_get_errmsg (void)
815
0
{
816
0
  return bfd_errmsg (bfd_get_error ());
817
0
}
818
819
static char *
820
prefix_encode (char *start, unsigned code)
821
0
{
822
0
  static char buf[32];
823
0
  char *p = stpcpy (buf, start);
824
0
  do
825
0
    *p++ = "abcdefghijklmnopqrstuvwxyz"[code % 26];
826
0
  while ((code /= 26) != 0);
827
0
  *p = '\0';
828
0
  return buf;
829
0
}
830
831
static char *
832
dlltmp (char **buf, const char *fmt)
833
0
{
834
0
  if (!*buf)
835
0
    {
836
0
      *buf = xmalloc (strlen (tmp_prefix) + 64);
837
0
      sprintf (*buf, fmt, tmp_prefix);
838
0
    }
839
0
  return *buf;
840
0
}
841
842
static void
843
inform (const char * message, ...)
844
3.64k
{
845
3.64k
  va_list args;
846
847
3.64k
  va_start (args, message);
848
849
3.64k
  if (!verbose)
850
3.64k
    return;
851
852
0
  report (message, args);
853
854
0
  va_end (args);
855
0
}
856
857
static const char *
858
rvaafter (int mach)
859
0
{
860
0
  switch (mach)
861
0
    {
862
0
    case MARM:
863
0
    case M386:
864
0
    case MX86:
865
0
    case MTHUMB:
866
0
    case MARM_INTERWORK:
867
0
    case MMCORE_BE:
868
0
    case MMCORE_LE:
869
0
    case MMCORE_ELF:
870
0
    case MMCORE_ELF_LE:
871
0
    case MARM_WINCE:
872
0
    case MAARCH64:
873
0
      break;
874
0
    default:
875
      /* xgettext:c-format */
876
0
      fatal (_("Internal error: Unknown machine type: %d"), mach);
877
0
      break;
878
0
    }
879
0
  return "";
880
0
}
881
882
static const char *
883
rvabefore (int mach)
884
0
{
885
0
  switch (mach)
886
0
    {
887
0
    case MARM:
888
0
    case M386:
889
0
    case MX86:
890
0
    case MTHUMB:
891
0
    case MARM_INTERWORK:
892
0
    case MMCORE_BE:
893
0
    case MMCORE_LE:
894
0
    case MMCORE_ELF:
895
0
    case MMCORE_ELF_LE:
896
0
    case MARM_WINCE:
897
0
    case MAARCH64:
898
0
      return ".rva\t";
899
0
    default:
900
      /* xgettext:c-format */
901
0
      fatal (_("Internal error: Unknown machine type: %d"), mach);
902
0
      break;
903
0
    }
904
0
  return "";
905
0
}
906
907
static const char *
908
asm_prefix (const char *name)
909
0
{
910
  /* Symbol names starting with ? do not have a leading underscore.  */
911
0
  if (name && *name == '?')
912
0
    return "";
913
0
  return leading_underscore;
914
0
}
915
916
0
#define ASM_BYTE    mtable[machine].how_byte
917
0
#define ASM_SHORT   mtable[machine].how_short
918
0
#define ASM_LONG    mtable[machine].how_long
919
0
#define ASM_TEXT    mtable[machine].how_asciz
920
0
#define ASM_C     mtable[machine].how_comment
921
#define ASM_JUMP    mtable[machine].how_jump
922
0
#define ASM_GLOBAL    mtable[machine].how_global
923
#define ASM_SPACE   mtable[machine].how_space
924
#define ASM_ALIGN_SHORT   mtable[machine].how_align_short
925
0
#define ASM_RVA_BEFORE    rvabefore (machine)
926
0
#define ASM_RVA_AFTER   rvaafter (machine)
927
0
#define ASM_PREFIX(NAME)  asm_prefix (NAME)
928
0
#define ASM_ALIGN_LONG    mtable[machine].how_align_long
929
0
#define HOW_BFD_READ_TARGET 0  /* Always default.  */
930
0
#define HOW_BFD_WRITE_TARGET  mtable[machine].how_bfd_target
931
#define HOW_BFD_ARCH    mtable[machine].how_bfd_arch
932
0
#define HOW_JTAB    (delay ? mtable[machine].how_dljtab \
933
0
          : mtable[machine].how_jtab)
934
0
#define HOW_JTAB_SIZE   (delay ? mtable[machine].how_dljtab_size \
935
0
          : mtable[machine].how_jtab_size)
936
0
#define HOW_JTAB_ROFF   (delay ? mtable[machine].how_dljtab_roff1 \
937
0
          : mtable[machine].how_jtab_roff)
938
0
#define HOW_JTAB_ROFF2    (delay ? mtable[machine].how_dljtab_roff2 : 0)
939
0
#define HOW_JTAB_ROFF3    (delay ? mtable[machine].how_dljtab_roff3 : 0)
940
0
#define ASM_SWITCHES    mtable[machine].how_default_as_switches
941
0
#define HOW_SEH     mtable[machine].how_seh
942
943
static char **oav;
944
945
static void
946
process_def_file (const char *name)
947
1.00k
{
948
1.00k
  FILE *f = fopen (name, FOPEN_RT);
949
950
1.00k
  if (!f)
951
    /* xgettext:c-format */
952
0
    fatal (_("Can't open def file: %s"), name);
953
954
1.00k
  yyin = f;
955
956
  /* xgettext:c-format */
957
1.00k
  inform (_("Processing def file: %s"), name);
958
959
1.00k
  yyparse ();
960
961
1.00k
  inform (_("Processed def file"));
962
1.00k
}
963
964
/**********************************************************************/
965
966
/* Communications with the parser.  */
967
968
static int d_nfuncs;    /* Number of functions exported.  */
969
static int d_named_nfuncs;  /* Number of named functions exported.  */
970
static int d_low_ord;   /* Lowest ordinal index.  */
971
static int d_high_ord;    /* Highest ordinal index.  */
972
static export_type *d_exports;  /* List of exported functions.  */
973
static export_type **d_exports_lexically;  /* Vector of exported functions in alpha order.  */
974
static dlist_type *d_list;  /* Descriptions.  */
975
static dlist_type *a_list;  /* Stuff to go in directives.  */
976
static int d_nforwards = 0; /* Number of forwarded exports.  */
977
978
static int d_is_dll;
979
static int d_is_exe;
980
981
void
982
yyerror (const char * err ATTRIBUTE_UNUSED)
983
1.00k
{
984
  /* xgettext:c-format */
985
1.00k
  non_fatal (_("Syntax error in def file %s:%d"), def_file, linenumber);
986
1.00k
}
987
988
void
989
def_exports (const char *name, const char *internal_name, int ordinal,
990
       int noname, int constant, int data, int private,
991
       const char *its_name)
992
979
{
993
979
  struct export *p = (struct export *) xmalloc (sizeof (*p));
994
995
979
  p->name = name;
996
979
  p->internal_name = internal_name ? internal_name : name;
997
979
  p->its_name = its_name;
998
979
  p->import_name = name;
999
979
  p->ordinal = ordinal;
1000
979
  p->constant = constant;
1001
979
  p->noname = noname;
1002
979
  p->private = private;
1003
979
  p->data = data;
1004
979
  p->next = d_exports;
1005
979
  d_exports = p;
1006
979
  d_nfuncs++;
1007
1008
979
  if (internal_name != NULL
1009
0
      && strchr (internal_name, '.') != NULL)
1010
0
    p->forward = ++d_nforwards;
1011
979
  else
1012
979
    p->forward = 0; /* no forward */
1013
979
}
1014
1015
static void
1016
set_dll_name_from_def (const char *name, char is_dll)
1017
0
{
1018
0
  const char *image_basename = lbasename (name);
1019
0
  if (image_basename != name)
1020
0
    non_fatal (_("%s: Path components stripped from image name, '%s'."),
1021
0
         def_file, name);
1022
  /* Append the default suffix, if none specified.  */
1023
0
  if (strchr (image_basename, '.') == 0)
1024
0
    dll_name = xasprintf ("%s%s", image_basename, is_dll ? ".dll" : ".exe");
1025
0
  else
1026
0
    dll_name = xstrdup (image_basename);
1027
0
}
1028
1029
void
1030
def_name (const char *name, int base)
1031
0
{
1032
  /* xgettext:c-format */
1033
0
  inform (_("NAME: %s base: %x"), name, base);
1034
1035
0
  if (d_is_dll)
1036
0
    non_fatal (_("Can't have LIBRARY and NAME"));
1037
1038
0
  if (dll_name_set_by_exp_name && name && *name != 0)
1039
0
    {
1040
0
      free (dll_name);
1041
0
      dll_name = NULL;
1042
0
      dll_name_set_by_exp_name = 0;
1043
0
    }
1044
  /* If --dllname not provided, use the one in the DEF file.
1045
     FIXME: Is this appropriate for executables?  */
1046
0
  if (!dll_name)
1047
0
    set_dll_name_from_def (name, 0);
1048
0
  free ((char *) name);
1049
0
  d_is_exe = 1;
1050
0
}
1051
1052
void
1053
def_library (const char *name, int base)
1054
0
{
1055
  /* xgettext:c-format */
1056
0
  inform (_("LIBRARY: %s base: %x"), name, base);
1057
1058
0
  if (d_is_exe)
1059
0
    non_fatal (_("Can't have LIBRARY and NAME"));
1060
1061
0
  if (dll_name_set_by_exp_name && name && *name != 0)
1062
0
    {
1063
0
      free (dll_name);
1064
0
      dll_name = NULL;
1065
0
      dll_name_set_by_exp_name = 0;
1066
0
    }
1067
1068
  /* If --dllname not provided, use the one in the DEF file.  */
1069
0
  if (!dll_name)
1070
0
    set_dll_name_from_def (name, 1);
1071
0
  free ((char *) name);
1072
0
  d_is_dll = 1;
1073
0
}
1074
1075
void
1076
def_description (const char *desc)
1077
0
{
1078
0
  dlist_type *d = (dlist_type *) xmalloc (sizeof (dlist_type));
1079
0
  d->text = desc;
1080
0
  d->next = d_list;
1081
0
  d_list = d;
1082
0
}
1083
1084
static void
1085
new_directive (char *dir)
1086
0
{
1087
0
  dlist_type *d = (dlist_type *) xmalloc (sizeof (dlist_type));
1088
0
  d->text = dir;
1089
0
  d->next = a_list;
1090
0
  a_list = d;
1091
0
}
1092
1093
void
1094
def_heapsize (int reserve, int commit)
1095
0
{
1096
0
  char *s;
1097
0
  if (commit > 0)
1098
0
    s = xasprintf ("-heap 0x%x,0x%x ", reserve, commit);
1099
0
  else
1100
0
    s = xasprintf ("-heap 0x%x ", reserve);
1101
0
  new_directive (s);
1102
0
}
1103
1104
void
1105
def_stacksize (int reserve, int commit)
1106
0
{
1107
0
  char *s;
1108
0
  if (commit > 0)
1109
0
    s = xasprintf ("-stack 0x%x,0x%x ", reserve, commit);
1110
0
  else
1111
0
    s = xasprintf ( "-stack 0x%x ", reserve);
1112
0
  new_directive (s);
1113
0
}
1114
1115
/* append_import simply adds the given import definition to the global
1116
   import_list.  It is used by def_import.  */
1117
1118
static void
1119
append_import (const char *symbol_name, const char *dllname, int func_ordinal,
1120
         const char *its_name)
1121
0
{
1122
0
  iheadtype **pq;
1123
0
  iheadtype *q;
1124
1125
0
  for (pq = &import_list; *pq != NULL; pq = &(*pq)->next)
1126
0
    {
1127
0
      if (strcmp ((*pq)->dllname, dllname) == 0)
1128
0
  {
1129
0
    q = *pq;
1130
0
    q->functail->next = xmalloc (sizeof (ifunctype));
1131
0
    q->functail = q->functail->next;
1132
0
    q->functail->ord  = func_ordinal;
1133
0
    q->functail->name = symbol_name;
1134
0
    q->functail->its_name = its_name;
1135
0
    q->functail->next = NULL;
1136
0
    q->nfuncs++;
1137
0
    return;
1138
0
  }
1139
0
    }
1140
1141
0
  q = xmalloc (sizeof (iheadtype));
1142
0
  q->dllname = dllname;
1143
0
  q->nfuncs = 1;
1144
0
  q->funchead = xmalloc (sizeof (ifunctype));
1145
0
  q->functail = q->funchead;
1146
0
  q->next = NULL;
1147
0
  q->functail->name = symbol_name;
1148
0
  q->functail->its_name = its_name;
1149
0
  q->functail->ord  = func_ordinal;
1150
0
  q->functail->next = NULL;
1151
1152
0
  *pq = q;
1153
0
}
1154
1155
/* def_import is called from within defparse.y when an IMPORT
1156
   declaration is encountered.  Depending on the form of the
1157
   declaration, the module name may or may not need ".dll" to be
1158
   appended to it, the name of the function may be stored in internal
1159
   or entry, and there may or may not be an ordinal value associated
1160
   with it.  */
1161
1162
/* A note regarding the parse modes:
1163
   In defparse.y we have to accept import declarations which follow
1164
   any one of the following forms:
1165
     <func_name_in_app> = <dll_name>.<func_name_in_dll>
1166
     <func_name_in_app> = <dll_name>.<number>
1167
     <dll_name>.<func_name_in_dll>
1168
     <dll_name>.<number>
1169
   Furthermore, the dll's name may or may not end with ".dll", which
1170
   complicates the parsing a little.  Normally the dll's name is
1171
   passed to def_import() in the "module" parameter, but when it ends
1172
   with ".dll" it gets passed in "module" sans ".dll" and that needs
1173
   to be reappended.
1174
1175
  def_import gets five parameters:
1176
  APP_NAME - the name of the function in the application, if
1177
             present, or NULL if not present.
1178
  MODULE   - the name of the dll, possibly sans extension (ie, '.dll').
1179
  DLLEXT   - the extension of the dll, if present, NULL if not present.
1180
  ENTRY    - the name of the function in the dll, if present, or NULL.
1181
  ORD_VAL  - the numerical tag of the function in the dll, if present,
1182
             or NULL.  Exactly one of <entry> or <ord_val> must be
1183
             present (i.e., not NULL).  */
1184
1185
void
1186
def_import (const char *app_name, const char *module, const char *dllext,
1187
      const char *entry, int ord_val, const char *its_name)
1188
0
{
1189
0
  const char *application_name;
1190
1191
0
  if (entry != NULL)
1192
0
    application_name = entry;
1193
0
  else
1194
0
    {
1195
0
      if (app_name != NULL)
1196
0
  application_name = app_name;
1197
0
      else
1198
0
  application_name = "";
1199
0
    }
1200
1201
0
  const char *mod_name = module;
1202
0
  if (dllext != NULL)
1203
0
    {
1204
0
      mod_name = concat (module, ".", dllext, NULL);
1205
0
      free ((char *) module);
1206
0
    }
1207
1208
0
  append_import (application_name, mod_name, ord_val, its_name);
1209
0
}
1210
1211
void
1212
def_version (int major, int minor)
1213
0
{
1214
0
  printf (_("VERSION %d.%d\n"), major, minor);
1215
0
}
1216
1217
void
1218
def_section (const char *name, int attr)
1219
0
{
1220
0
  char atts[5];
1221
0
  char *d = atts;
1222
0
  if (attr & 1)
1223
0
    *d++ = 'R';
1224
0
  if (attr & 2)
1225
0
    *d++ = 'W';
1226
0
  if (attr & 4)
1227
0
    *d++ = 'X';
1228
0
  if (attr & 8)
1229
0
    *d++ = 'S';
1230
0
  *d++ = 0;
1231
0
  char *s = xasprintf ("-attr %s %s", name, atts);
1232
0
  new_directive (s);
1233
0
}
1234
1235
void
1236
def_code (int attr)
1237
0
{
1238
0
  def_section ("CODE", attr);
1239
0
}
1240
1241
void
1242
def_data (int attr)
1243
0
{
1244
0
  def_section ("DATA", attr);
1245
0
}
1246
1247
/**********************************************************************/
1248
1249
static void
1250
run (const char *what, char *args)
1251
0
{
1252
0
  char *s;
1253
0
  int pid, wait_status;
1254
0
  int i;
1255
0
  const char **argv;
1256
0
  char *errmsg_fmt = NULL, *errmsg_arg = NULL;
1257
0
  char *temp_base = make_temp_file ("");
1258
1259
0
  inform (_("run: %s %s"), what, args);
1260
1261
  /* Count the args */
1262
0
  i = 0;
1263
0
  for (s = args; *s; s++)
1264
0
    if (*s == ' ')
1265
0
      i++;
1266
0
  i++;
1267
0
  argv = xmalloc (sizeof (char *) * (i + 3));
1268
0
  i = 0;
1269
0
  argv[i++] = what;
1270
0
  s = args;
1271
0
  while (1)
1272
0
    {
1273
0
      while (*s == ' ')
1274
0
  ++s;
1275
0
      argv[i++] = s;
1276
0
      while (*s != ' ' && *s != 0)
1277
0
  s++;
1278
0
      if (*s == 0)
1279
0
  break;
1280
0
      *s++ = 0;
1281
0
    }
1282
0
  argv[i++] = NULL;
1283
1284
0
  pid = pexecute (argv[0], (char * const *) argv, program_name, temp_base,
1285
0
      &errmsg_fmt, &errmsg_arg, PEXECUTE_ONE | PEXECUTE_SEARCH);
1286
0
  free (argv);
1287
0
  free (temp_base);
1288
1289
0
  if (pid == -1)
1290
0
    {
1291
0
      inform ("%s", strerror (errno));
1292
1293
0
      fatal (errmsg_fmt, errmsg_arg);
1294
0
    }
1295
1296
0
  pid = pwait (pid, & wait_status, 0);
1297
1298
0
  if (pid == -1)
1299
0
    {
1300
      /* xgettext:c-format */
1301
0
      fatal (_("wait: %s"), strerror (errno));
1302
0
    }
1303
0
  else if (WIFSIGNALED (wait_status))
1304
0
    {
1305
      /* xgettext:c-format */
1306
0
      fatal (_("subprocess got fatal signal %d"), WTERMSIG (wait_status));
1307
0
    }
1308
0
  else if (WIFEXITED (wait_status))
1309
0
    {
1310
0
      if (WEXITSTATUS (wait_status) != 0)
1311
  /* xgettext:c-format */
1312
0
  non_fatal (_("%s exited with status %d"),
1313
0
       what, WEXITSTATUS (wait_status));
1314
0
    }
1315
0
  else
1316
0
    abort ();
1317
0
}
1318
1319
/* Look for a list of symbols to export in the .drectve section of
1320
   ABFD.  Pass each one to def_exports.  */
1321
1322
static void
1323
scan_drectve_symbols (bfd *abfd)
1324
316
{
1325
  /* Look for .drectve's */
1326
316
  asection *s = bfd_get_section_by_name (abfd, DRECTVE_SECTION_NAME);
1327
316
  if (s == NULL)
1328
316
    return;
1329
1330
0
  bfd_byte *buf;
1331
0
  if (!bfd_malloc_and_get_section (abfd, s, &buf))
1332
0
    return;
1333
1334
  /* xgettext:c-format */
1335
0
  inform (_("Sucking in info from %s section in %s"),
1336
0
    DRECTVE_SECTION_NAME, bfd_get_filename (abfd));
1337
1338
  /* Search for -export: strings. The exported symbols can optionally
1339
     have type tags (eg., -export:foo,data), so handle those as well.
1340
     Currently only data tag is supported.  */
1341
0
  const char *p = (const char *) buf;
1342
0
  const char *e = (const char *) buf + bfd_section_size (s);
1343
0
  while (p < e)
1344
0
    {
1345
0
      if (p[0] == '-'
1346
0
    && startswith (p, "-export:"))
1347
0
  {
1348
0
    const char *name;
1349
0
    char *c;
1350
0
    flagword flags = BSF_FUNCTION;
1351
1352
0
    p += 8;
1353
    /* Do we have a quoted export?  */
1354
0
    if (*p == '"')
1355
0
      {
1356
0
        p++;
1357
0
        name = p;
1358
0
        while (p < e && *p != '"')
1359
0
    ++p;
1360
0
      }
1361
0
    else
1362
0
      {
1363
0
        name = p;
1364
0
        while (p < e && *p != ',' && *p != ' ' && *p != '-')
1365
0
    p++;
1366
0
      }
1367
0
    c = xmemdup (name, p - name, p - name + 1);
1368
    /* Advance over trailing quote.  */
1369
0
    if (p < e && *p == '"')
1370
0
      ++p;
1371
0
    if (p < e && *p == ',')       /* found type tag.  */
1372
0
      {
1373
0
        const char *tag_start = ++p;
1374
0
        while (p < e && *p != ' ' && *p != '-')
1375
0
    p++;
1376
0
        if (startswith (tag_start, "data"))
1377
0
    flags &= ~BSF_FUNCTION;
1378
0
      }
1379
1380
    /* FIXME: The 5th arg is for the `constant' field.
1381
       What should it be?  Not that it matters since it's not
1382
       currently useful.  */
1383
0
    def_exports (c, 0, -1, 0, 0, ! (flags & BSF_FUNCTION), 0, NULL);
1384
1385
0
    if (add_stdcall_alias && strchr (c, '@'))
1386
0
      {
1387
0
        int lead_at = (*c == '@') ;
1388
0
        char *exported_name = xstrdup (c + lead_at);
1389
0
        char *atsym = strchr (exported_name, '@');
1390
0
        if (atsym)
1391
0
    *atsym = '\0';
1392
        /* Note: stdcall alias symbols can never be data.  */
1393
0
        def_exports (exported_name, c, -1, 0, 0, 0, 0, NULL);
1394
0
      }
1395
0
  }
1396
0
      else
1397
0
  p++;
1398
0
    }
1399
0
  free (buf);
1400
0
}
1401
1402
/* Look through the symbols in MINISYMS, and add each one to list of
1403
   symbols to export.  */
1404
1405
static void
1406
scan_filtered_symbols (bfd *abfd, void *minisyms, long symcount,
1407
           unsigned int size)
1408
226
{
1409
226
  asymbol *store;
1410
226
  bfd_byte *from, *fromend;
1411
1412
226
  store = bfd_make_empty_symbol (abfd);
1413
226
  if (store == NULL)
1414
0
    bfd_fatal (bfd_get_filename (abfd));
1415
1416
226
  from = (bfd_byte *) minisyms;
1417
226
  fromend = from + symcount * size;
1418
1.20k
  for (; from < fromend; from += size)
1419
979
    {
1420
979
      asymbol *sym;
1421
979
      const char *symbol_name;
1422
1423
979
      sym = bfd_minisymbol_to_symbol (abfd, false, from, store);
1424
979
      if (sym == NULL)
1425
0
  bfd_fatal (bfd_get_filename (abfd));
1426
1427
979
      symbol_name = bfd_asymbol_name (sym);
1428
979
      if (*symbol_name
1429
629
    && *symbol_name == bfd_get_symbol_leading_char (abfd))
1430
1
  ++symbol_name;
1431
979
      symbol_name = xstrdup (symbol_name);
1432
1433
979
      def_exports (symbol_name , 0, -1, 0, 0,
1434
979
       ! (sym->flags & BSF_FUNCTION), 0, NULL);
1435
1436
979
      if (add_stdcall_alias && strchr (symbol_name, '@'))
1437
0
  {
1438
0
    int lead_at = (*symbol_name == '@');
1439
0
    char *exported_name = xstrdup (symbol_name + lead_at);
1440
0
    char *atsym = strchr (exported_name, '@');
1441
0
    if (atsym)
1442
0
      *atsym = '\0';
1443
    /* Note: stdcall alias symbols can never be data.  */
1444
0
    def_exports (exported_name, symbol_name,
1445
0
           -1, 0, 0, 0, 0, NULL);
1446
0
  }
1447
979
    }
1448
226
}
1449
1450
/* Add a list of symbols to exclude.  */
1451
1452
static void
1453
add_excludes (const char *new_excludes)
1454
0
{
1455
0
  char *local_copy;
1456
0
  char *exclude_string;
1457
1458
0
  local_copy = xstrdup (new_excludes);
1459
1460
0
  exclude_string = strtok (local_copy, ",:");
1461
0
  for (; exclude_string; exclude_string = strtok (NULL, ",:"))
1462
0
    {
1463
0
      struct string_list *new_exclude = xmalloc (sizeof (*new_exclude));
1464
      /* Don't add a leading underscore for fastcall symbols.  */
1465
0
      if (*exclude_string == '@')
1466
0
  new_exclude->string = xstrdup (exclude_string);
1467
0
      else
1468
0
  new_exclude->string = xasprintf ("%s%s", leading_underscore,
1469
0
           exclude_string);
1470
0
      new_exclude->next = excludes;
1471
0
      excludes = new_exclude;
1472
1473
      /* xgettext:c-format */
1474
0
      inform (_("Excluding symbol: %s"), exclude_string);
1475
0
    }
1476
1477
0
  free (local_copy);
1478
0
}
1479
1480
/* See if STRING is on the list of symbols to exclude.  */
1481
1482
static bool
1483
match_exclude (const char *string)
1484
979
{
1485
979
  struct string_list *excl_item;
1486
1487
979
  for (excl_item = excludes; excl_item; excl_item = excl_item->next)
1488
0
    if (strcmp (string, excl_item->string) == 0)
1489
0
      return true;
1490
979
  return false;
1491
979
}
1492
1493
/* Add the default list of symbols to exclude.  */
1494
1495
static void
1496
set_default_excludes (void)
1497
0
{
1498
0
  add_excludes (default_excludes);
1499
0
}
1500
1501
/* Choose which symbols to export.  */
1502
1503
static long
1504
filter_symbols (bfd *abfd, void *minisyms, long symcount, unsigned int size)
1505
226
{
1506
226
  bfd_byte *from, *fromend, *to;
1507
226
  asymbol *store;
1508
1509
226
  store = bfd_make_empty_symbol (abfd);
1510
226
  if (store == NULL)
1511
0
    bfd_fatal (bfd_get_filename (abfd));
1512
1513
226
  from = (bfd_byte *) minisyms;
1514
226
  fromend = from + symcount * size;
1515
226
  to = (bfd_byte *) minisyms;
1516
1517
10.1k
  for (; from < fromend; from += size)
1518
9.92k
    {
1519
9.92k
      int keep = 0;
1520
9.92k
      asymbol *sym;
1521
1522
9.92k
      sym = bfd_minisymbol_to_symbol (abfd, false, (const void *) from, store);
1523
9.92k
      if (sym == NULL)
1524
0
  bfd_fatal (bfd_get_filename (abfd));
1525
1526
      /* Check for external and defined only symbols.  */
1527
9.92k
      keep = (((sym->flags & BSF_GLOBAL) != 0
1528
6.91k
         || (sym->flags & BSF_WEAK) != 0
1529
6.89k
         || bfd_is_com_section (sym->section))
1530
3.11k
        && ! bfd_is_und_section (sym->section));
1531
1532
9.92k
      keep = keep && ! match_exclude (sym->name);
1533
1534
9.92k
      if (keep)
1535
979
  {
1536
979
    memcpy (to, from, size);
1537
979
    to += size;
1538
979
  }
1539
9.92k
    }
1540
1541
226
  return (to - (bfd_byte *) minisyms) / size;
1542
226
}
1543
1544
/* Export all symbols in ABFD, except for ones we were told not to
1545
   export.  */
1546
1547
static void
1548
scan_all_symbols (bfd *abfd)
1549
316
{
1550
316
  long symcount;
1551
316
  void *minisyms;
1552
316
  unsigned int size;
1553
1554
  /* Ignore bfds with an import descriptor table.  We assume that any
1555
     such BFD contains symbols which are exported from another DLL,
1556
     and we don't want to reexport them from here.  */
1557
316
  if (bfd_get_section_by_name (abfd, ".idata$4"))
1558
0
    return;
1559
1560
316
  if (! (bfd_get_file_flags (abfd) & HAS_SYMS))
1561
90
    {
1562
      /* xgettext:c-format */
1563
90
      non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1564
90
      return;
1565
90
    }
1566
1567
226
  symcount = bfd_read_minisymbols (abfd, false, &minisyms, &size);
1568
226
  if (symcount < 0)
1569
0
    bfd_fatal (bfd_get_filename (abfd));
1570
1571
226
  if (symcount == 0)
1572
0
    {
1573
      /* xgettext:c-format */
1574
0
      non_fatal (_("%s: no symbols"), bfd_get_filename (abfd));
1575
0
      return;
1576
0
    }
1577
1578
  /* Discard the symbols we don't want to export.  It's OK to do this
1579
     in place; we'll free the storage anyway.  */
1580
1581
226
  symcount = filter_symbols (abfd, minisyms, symcount, size);
1582
226
  scan_filtered_symbols (abfd, minisyms, symcount, size);
1583
1584
226
  free (minisyms);
1585
226
}
1586
1587
/* Look at the object file to decide which symbols to export.  */
1588
1589
static void
1590
scan_open_obj_file (bfd *abfd)
1591
632
{
1592
632
  if (export_all_symbols)
1593
316
    scan_all_symbols (abfd);
1594
316
  else
1595
316
    scan_drectve_symbols (abfd);
1596
1597
  /* FIXME: we ought to read in and block out the base relocations.  */
1598
1599
  /* xgettext:c-format */
1600
632
  inform (_("Done reading %s"), bfd_get_filename (abfd));
1601
632
}
1602
1603
static void
1604
scan_obj_file (const char *filename)
1605
1.00k
{
1606
1.00k
  bfd * f = bfd_openr (filename, 0);
1607
1608
1.00k
  if (!f)
1609
    /* xgettext:c-format */
1610
0
    fatal (_("Unable to open object file: %s: %s"), filename, bfd_get_errmsg ());
1611
1612
  /* xgettext:c-format */
1613
1.00k
  inform (_("Scanning object file %s"), filename);
1614
1615
1.00k
  if (bfd_check_format (f, bfd_archive))
1616
64
    {
1617
64
      bfd *arfile = bfd_openr_next_archived_file (f, 0);
1618
128
      while (arfile)
1619
64
  {
1620
64
    bfd *next;
1621
64
    if (bfd_check_format (arfile, bfd_object))
1622
36
      scan_open_obj_file (arfile);
1623
64
    next = bfd_openr_next_archived_file (f, arfile);
1624
64
    bfd_close (arfile);
1625
    /* PR 17512: file: 58715298.  */
1626
64
    if (next == arfile)
1627
0
      break;
1628
64
    arfile = next;
1629
64
  }
1630
1631
#ifdef DLLTOOL_MCORE_ELF
1632
      if (mcore_elf_out_file)
1633
  inform (_("Cannot produce mcore-elf dll from archive file: %s"),
1634
    filename);
1635
#endif
1636
64
    }
1637
940
  else if (bfd_check_format (f, bfd_object))
1638
596
    {
1639
596
      scan_open_obj_file (f);
1640
1641
#ifdef DLLTOOL_MCORE_ELF
1642
      if (mcore_elf_out_file)
1643
  mcore_elf_cache_filename (filename);
1644
#endif
1645
596
    }
1646
1647
1.00k
  bfd_close (f);
1648
1.00k
}
1649
1650

1651
static void
1652
dump_def_info (FILE *f)
1653
0
{
1654
0
  int i;
1655
0
  export_type *exp;
1656
0
  fprintf (f, "%s ", ASM_C);
1657
0
  for (i = 0; oav[i]; i++)
1658
0
    fprintf (f, "%s ", oav[i]);
1659
0
  fprintf (f, "\n");
1660
0
  for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
1661
0
    {
1662
0
      fprintf (f, "%s  %d = %s %s @ %d %s%s%s%s%s%s\n",
1663
0
         ASM_C,
1664
0
         i,
1665
0
         exp->name,
1666
0
         exp->internal_name,
1667
0
         exp->ordinal,
1668
0
         exp->noname ? "NONAME " : "",
1669
0
         exp->private ? "PRIVATE " : "",
1670
0
         exp->constant ? "CONSTANT" : "",
1671
0
         exp->data ? "DATA" : "",
1672
0
         exp->its_name ? " ==" : "",
1673
0
         exp->its_name ? exp->its_name : "");
1674
0
    }
1675
0
}
1676
1677
/* Generate the .exp file.  */
1678
1679
static int
1680
sfunc (const void *a, const void *b)
1681
0
{
1682
0
  if (*(const bfd_vma *) a == *(const bfd_vma *) b)
1683
0
    return 0;
1684
1685
0
  return ((*(const bfd_vma *) a > *(const bfd_vma *) b) ? 1 : -1);
1686
0
}
1687
1688
static void
1689
flush_page (FILE *f, bfd_vma *need, bfd_vma page_addr, int on_page)
1690
0
{
1691
0
  int i;
1692
1693
  /* Flush this page.  */
1694
0
  fprintf (f, "\t%s\t0x%08x\t%s Starting RVA for chunk\n",
1695
0
     ASM_LONG,
1696
0
     (int) page_addr,
1697
0
     ASM_C);
1698
0
  fprintf (f, "\t%s\t0x%x\t%s Size of block\n",
1699
0
     ASM_LONG,
1700
0
     (on_page * 2) + (on_page & 1) * 2 + 8,
1701
0
     ASM_C);
1702
1703
0
  for (i = 0; i < on_page; i++)
1704
0
    {
1705
0
      bfd_vma needed = need[i];
1706
1707
0
      if (needed)
1708
0
  {
1709
0
    if (!create_for_pep)
1710
0
      {
1711
        /* Relocation via HIGHLOW.  */
1712
0
        needed = ((needed - page_addr) | 0x3000) & 0xffff;
1713
0
      }
1714
0
    else
1715
0
      {
1716
        /* Relocation via DIR64.  */
1717
0
        needed = ((needed - page_addr) | 0xa000) & 0xffff;
1718
0
      }
1719
0
  }
1720
1721
0
      fprintf (f, "\t%s\t0x%lx\n", ASM_SHORT, (long) needed);
1722
0
    }
1723
1724
  /* And padding */
1725
0
  if (on_page & 1)
1726
0
    fprintf (f, "\t%s\t0x%x\n", ASM_SHORT, 0 | 0x0000);
1727
0
}
1728
1729
static void
1730
gen_def_file (void)
1731
0
{
1732
0
  int i;
1733
0
  export_type *exp;
1734
1735
0
  inform (_("Adding exports to output file"));
1736
1737
0
  fprintf (output_def, ";");
1738
0
  for (i = 0; oav[i]; i++)
1739
0
    fprintf (output_def, " %s", oav[i]);
1740
1741
0
  fprintf (output_def, "\nEXPORTS\n");
1742
1743
0
  for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
1744
0
    {
1745
0
      char *quote = strchr (exp->name, '.') ? "\"" : "";
1746
0
      char *res = cplus_demangle (exp->internal_name, DMGL_ANSI | DMGL_PARAMS);
1747
1748
0
      if (res)
1749
0
  {
1750
0
    fprintf (output_def, ";\t%s\n", res);
1751
0
    free (res);
1752
0
  }
1753
1754
0
      if (strcmp (exp->name, exp->internal_name) == 0)
1755
0
  {
1756
0
    fprintf (output_def, "\t%s%s%s @ %d%s%s%s%s%s\n",
1757
0
       quote,
1758
0
       exp->name,
1759
0
       quote,
1760
0
       exp->ordinal,
1761
0
       exp->noname ? " NONAME" : "",
1762
0
       exp->private ? "PRIVATE " : "",
1763
0
       exp->data ? " DATA" : "",
1764
0
       exp->its_name ? " ==" : "",
1765
0
       exp->its_name ? exp->its_name : "");
1766
0
  }
1767
0
      else
1768
0
  {
1769
0
    char * quote1 = strchr (exp->internal_name, '.') ? "\"" : "";
1770
    /* char *alias =  */
1771
0
    fprintf (output_def, "\t%s%s%s = %s%s%s @ %d%s%s%s%s%s\n",
1772
0
       quote,
1773
0
       exp->name,
1774
0
       quote,
1775
0
       quote1,
1776
0
       exp->internal_name,
1777
0
       quote1,
1778
0
       exp->ordinal,
1779
0
       exp->noname ? " NONAME" : "",
1780
0
       exp->private ? "PRIVATE " : "",
1781
0
       exp->data ? " DATA" : "",
1782
0
       exp->its_name ? " ==" : "",
1783
0
       exp->its_name ? exp->its_name : "");
1784
0
  }
1785
0
    }
1786
1787
0
  inform (_("Added exports to output file"));
1788
0
}
1789
1790
/* generate_idata_ofile generates the portable assembly source code
1791
   for the idata sections.  It appends the source code to the end of
1792
   the file.  */
1793
1794
static void
1795
generate_idata_ofile (FILE *filvar)
1796
0
{
1797
0
  iheadtype *headptr;
1798
0
  ifunctype *funcptr;
1799
0
  int        headindex;
1800
0
  int        funcindex;
1801
0
  int      nheads;
1802
1803
0
  if (import_list == NULL)
1804
0
    return;
1805
1806
0
  fprintf (filvar, "%s Import data sections\n", ASM_C);
1807
0
  fprintf (filvar, "\n\t.section\t.idata$2\n");
1808
0
  fprintf (filvar, "\t%s\tdoi_idata\n", ASM_GLOBAL);
1809
0
  fprintf (filvar, "doi_idata:\n");
1810
1811
0
  nheads = 0;
1812
0
  for (headptr = import_list; headptr != NULL; headptr = headptr->next)
1813
0
    {
1814
0
      fprintf (filvar, "\t%slistone%d%s\t%s %s\n",
1815
0
         ASM_RVA_BEFORE, nheads, ASM_RVA_AFTER,
1816
0
         ASM_C, headptr->dllname);
1817
0
      fprintf (filvar, "\t%s\t0\n", ASM_LONG);
1818
0
      fprintf (filvar, "\t%s\t0\n", ASM_LONG);
1819
0
      fprintf (filvar, "\t%sdllname%d%s\n",
1820
0
         ASM_RVA_BEFORE, nheads, ASM_RVA_AFTER);
1821
0
      fprintf (filvar, "\t%slisttwo%d%s\n\n",
1822
0
         ASM_RVA_BEFORE, nheads, ASM_RVA_AFTER);
1823
0
      nheads++;
1824
0
    }
1825
1826
0
  fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL record at */
1827
0
  fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* end of idata$2 */
1828
0
  fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* section        */
1829
0
  fprintf (filvar, "\t%s\t0\n", ASM_LONG);
1830
0
  fprintf (filvar, "\t%s\t0\n", ASM_LONG);
1831
1832
0
  fprintf (filvar, "\n\t.section\t.idata$4\n");
1833
0
  headindex = 0;
1834
0
  for (headptr = import_list; headptr != NULL; headptr = headptr->next)
1835
0
    {
1836
0
      fprintf (filvar, "listone%d:\n", headindex);
1837
0
      for (funcindex = 0; funcindex < headptr->nfuncs; funcindex++)
1838
0
  {
1839
0
    if (create_for_pep)
1840
0
      fprintf (filvar, "\t%sfuncptr%d_%d%s\n%s\t0\n",
1841
0
         ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER,
1842
0
         ASM_LONG);
1843
0
    else
1844
0
      fprintf (filvar, "\t%sfuncptr%d_%d%s\n",
1845
0
         ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER);
1846
0
  }
1847
0
      if (create_for_pep)
1848
0
  fprintf (filvar, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
1849
0
      else
1850
0
  fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list.  */
1851
0
      headindex++;
1852
0
    }
1853
1854
0
  fprintf (filvar, "\n\t.section\t.idata$5\n");
1855
0
  headindex = 0;
1856
0
  for (headptr = import_list; headptr != NULL; headptr = headptr->next)
1857
0
    {
1858
0
      fprintf (filvar, "listtwo%d:\n", headindex);
1859
0
      for (funcindex = 0; funcindex < headptr->nfuncs; funcindex++)
1860
0
  {
1861
0
    if (create_for_pep)
1862
0
      fprintf (filvar, "\t%sfuncptr%d_%d%s\n%s\t0\n",
1863
0
         ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER,
1864
0
         ASM_LONG);
1865
0
    else
1866
0
      fprintf (filvar, "\t%sfuncptr%d_%d%s\n",
1867
0
         ASM_RVA_BEFORE, headindex, funcindex, ASM_RVA_AFTER);
1868
0
  }
1869
0
      if (create_for_pep)
1870
0
  fprintf (filvar, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
1871
0
      else
1872
0
  fprintf (filvar, "\t%s\t0\n", ASM_LONG); /* NULL terminating list.  */
1873
0
      headindex++;
1874
0
    }
1875
1876
0
  fprintf (filvar, "\n\t.section\t.idata$6\n");
1877
0
  headindex = 0;
1878
0
  for (headptr = import_list; headptr != NULL; headptr = headptr->next)
1879
0
    {
1880
0
      funcindex = 0;
1881
0
      for (funcptr = headptr->funchead; funcptr != NULL;
1882
0
     funcptr = funcptr->next)
1883
0
  {
1884
0
    fprintf (filvar, "funcptr%d_%d:\n", headindex, funcindex);
1885
0
    fprintf (filvar, "\t%s\t%d\n", ASM_SHORT, funcptr->ord & 0xFFFF);
1886
0
    fprintf (filvar, "\t%s\t\"%s\"\n", ASM_TEXT,
1887
0
       funcptr->its_name ? funcptr->its_name : funcptr->name);
1888
0
    fprintf (filvar, "\t%s\t0\n", ASM_BYTE);
1889
0
    funcindex++;
1890
0
  }
1891
0
      headindex++;
1892
0
    }
1893
1894
0
  fprintf (filvar, "\n\t.section\t.idata$7\n");
1895
0
  headindex = 0;
1896
0
  for (headptr = import_list; headptr != NULL; headptr = headptr->next)
1897
0
    {
1898
0
      fprintf (filvar, "dllname%d:\n", headindex);
1899
0
      fprintf (filvar, "\t%s\t\"%s\"\n", ASM_TEXT, headptr->dllname);
1900
0
      fprintf (filvar, "\t%s\t0\n", ASM_BYTE);
1901
0
      headindex++;
1902
0
    }
1903
0
}
1904
1905
/* Assemble the specified file.  */
1906
static void
1907
assemble_file (const char * source, const char * dest)
1908
0
{
1909
0
  char *cmd = xasprintf ("%s %s -o %s %s",
1910
0
       ASM_SWITCHES, as_flags, dest, source);
1911
0
  run (as_name, cmd);
1912
0
  free (cmd);
1913
0
}
1914
1915
static const char * temp_file_to_remove[5];
1916
0
#define TEMP_EXPORT_FILE 0
1917
0
#define TEMP_HEAD_FILE   1
1918
0
#define TEMP_TAIL_FILE   2
1919
0
#define TEMP_HEAD_O_FILE 3
1920
0
#define TEMP_TAIL_O_FILE 4
1921
1922
static void
1923
unlink_temp_files (void)
1924
0
{
1925
0
  unsigned i;
1926
1927
0
  if (dontdeltemps > 0)
1928
0
    return;
1929
1930
0
  for (i = 0; i < ARRAY_SIZE (temp_file_to_remove); i++)
1931
0
    {
1932
0
      if (temp_file_to_remove[i])
1933
0
  {
1934
0
    unlink (temp_file_to_remove[i]);
1935
0
    temp_file_to_remove[i] = NULL;
1936
0
  }
1937
0
    }
1938
0
}
1939
1940
static void
1941
gen_exp_file (void)
1942
0
{
1943
0
  FILE *f;
1944
0
  int i;
1945
0
  export_type *exp;
1946
0
  dlist_type *dl;
1947
1948
  /* xgettext:c-format */
1949
0
  inform (_("Generating export file: %s"), exp_name);
1950
1951
0
  f = fopen (TMP_ASM, FOPEN_WT);
1952
0
  if (!f)
1953
    /* xgettext:c-format */
1954
0
    fatal (_("Unable to open temporary assembler file: %s"), TMP_ASM);
1955
1956
0
  temp_file_to_remove[TEMP_EXPORT_FILE] = TMP_ASM;
1957
1958
  /* xgettext:c-format */
1959
0
  inform (_("Opened temporary file: %s"), TMP_ASM);
1960
1961
0
  dump_def_info (f);
1962
1963
0
  if (d_exports)
1964
0
    {
1965
0
      fprintf (f, "\t.section .edata\n\n");
1966
0
      fprintf (f, "\t%s 0 %s Allways 0\n", ASM_LONG, ASM_C);
1967
0
      fprintf (f, "\t%s 0x%lx %s Time and date\n", ASM_LONG,
1968
0
         (unsigned long) time(0), ASM_C);
1969
0
      fprintf (f, "\t%s 0 %s Major and Minor version\n", ASM_LONG, ASM_C);
1970
0
      fprintf (f, "\t%sname%s %s Ptr to name of dll\n",
1971
0
         ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
1972
0
      fprintf (f, "\t%s %d  %s Starting ordinal of exports\n",
1973
0
         ASM_LONG, d_low_ord, ASM_C);
1974
1975
1976
0
      fprintf (f, "\t%s %d  %s Number of functions\n",
1977
0
         ASM_LONG, d_high_ord - d_low_ord + 1, ASM_C);
1978
0
      fprintf (f, "\t%s named funcs %d, low ord %d, high ord %d\n",
1979
0
         ASM_C, d_named_nfuncs, d_low_ord, d_high_ord);
1980
0
      fprintf (f, "\t%s %d  %s Number of names\n", ASM_LONG,
1981
0
         show_allnames ? d_high_ord - d_low_ord + 1 : d_named_nfuncs,
1982
0
         ASM_C);
1983
0
      fprintf (f, "\t%safuncs%s  %s Address of functions\n",
1984
0
         ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
1985
1986
0
      fprintf (f, "\t%sanames%s %s Address of Name Pointer Table\n",
1987
0
         ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
1988
1989
0
      fprintf (f, "\t%sanords%s %s Address of ordinals\n",
1990
0
         ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
1991
1992
0
      fprintf (f, "name:  %s  \"%s\"\n", ASM_TEXT, dll_name);
1993
1994
1995
0
      fprintf (f, "%s Export address Table\n", ASM_C);
1996
0
      fprintf (f, "\t%s\n", ASM_ALIGN_LONG);
1997
0
      fprintf (f, "afuncs:\n");
1998
0
      i = d_low_ord;
1999
2000
0
      for (exp = d_exports; exp; exp = exp->next)
2001
0
  {
2002
0
    if (exp->ordinal != i)
2003
0
      {
2004
0
        while (i < exp->ordinal)
2005
0
    {
2006
0
      fprintf (f, "\t%s\t0\n", ASM_LONG);
2007
0
      i++;
2008
0
    }
2009
0
      }
2010
2011
0
    if (exp->forward == 0)
2012
0
      {
2013
0
        if (exp->internal_name[0] == '@')
2014
0
    fprintf (f, "\t%s%s%s\t%s %d\n", ASM_RVA_BEFORE,
2015
0
       exp->internal_name, ASM_RVA_AFTER, ASM_C, exp->ordinal);
2016
0
        else
2017
0
    fprintf (f, "\t%s%s%s%s\t%s %d\n", ASM_RVA_BEFORE,
2018
0
       ASM_PREFIX (exp->internal_name),
2019
0
       exp->internal_name, ASM_RVA_AFTER, ASM_C, exp->ordinal);
2020
0
      }
2021
0
    else
2022
0
      fprintf (f, "\t%sf%d%s\t%s %d\n", ASM_RVA_BEFORE,
2023
0
         exp->forward, ASM_RVA_AFTER, ASM_C, exp->ordinal);
2024
0
    i++;
2025
0
  }
2026
2027
0
      fprintf (f, "%s Export Name Pointer Table\n", ASM_C);
2028
0
      fprintf (f, "anames:\n");
2029
2030
0
      for (i = 0; (exp = d_exports_lexically[i]); i++)
2031
0
  {
2032
0
    if (!exp->noname || show_allnames)
2033
0
      fprintf (f, "\t%sn%d%s\n",
2034
0
         ASM_RVA_BEFORE, exp->ordinal, ASM_RVA_AFTER);
2035
0
  }
2036
2037
0
      fprintf (f, "%s Export Ordinal Table\n", ASM_C);
2038
0
      fprintf (f, "anords:\n");
2039
0
      for (i = 0; (exp = d_exports_lexically[i]); i++)
2040
0
  {
2041
0
    if (!exp->noname || show_allnames)
2042
0
      fprintf (f, "\t%s %d\n", ASM_SHORT, exp->ordinal - d_low_ord);
2043
0
  }
2044
2045
0
      fprintf (f, "%s Export Name Table\n", ASM_C);
2046
0
      for (i = 0; (exp = d_exports_lexically[i]); i++)
2047
0
  {
2048
0
    if (!exp->noname || show_allnames)
2049
0
      {
2050
0
        const char *xname = (exp->its_name ? exp->its_name
2051
0
           : xlate (exp->name));
2052
0
        fprintf (f, "n%d: %s  \"%s\"\n",
2053
0
           exp->ordinal, ASM_TEXT, xname);
2054
0
        if (!exp->its_name)
2055
0
    free ((char *) xname);
2056
0
      }
2057
0
    if (exp->forward != 0)
2058
0
      fprintf (f, "f%d: %s  \"%s\"\n",
2059
0
         exp->forward, ASM_TEXT, exp->internal_name);
2060
0
  }
2061
2062
0
      if (a_list)
2063
0
  {
2064
0
    fprintf (f, "\t.section %s\n", DRECTVE_SECTION_NAME);
2065
0
    for (dl = a_list; dl; dl = dl->next)
2066
0
      {
2067
0
        fprintf (f, "\t%s\t\"%s\"\n", ASM_TEXT, dl->text);
2068
0
      }
2069
0
  }
2070
2071
0
      if (d_list)
2072
0
  {
2073
0
    fprintf (f, "\t.section .rdata\n");
2074
0
    for (dl = d_list; dl; dl = dl->next)
2075
0
      {
2076
0
        const char *p;
2077
0
        int l;
2078
2079
        /* We don't output as ascii because there can
2080
     be quote characters in the string.  */
2081
0
        l = 0;
2082
0
        for (p = dl->text; *p; p++)
2083
0
    {
2084
0
      if (l == 0)
2085
0
        fprintf (f, "\t%s\t", ASM_BYTE);
2086
0
      else
2087
0
        fprintf (f, ",");
2088
0
      fprintf (f, "%d", *p);
2089
0
      if (p[1] == 0)
2090
0
        {
2091
0
          fprintf (f, ",0\n");
2092
0
          break;
2093
0
        }
2094
0
      if (++l == 10)
2095
0
        {
2096
0
          fprintf (f, "\n");
2097
0
          l = 0;
2098
0
        }
2099
0
    }
2100
0
      }
2101
0
  }
2102
0
    }
2103
2104
  /* Add to the output file a way of getting to the exported names
2105
     without using the import library.  */
2106
0
  if (add_indirect)
2107
0
    {
2108
0
      fprintf (f, "\t.section\t.rdata\n");
2109
0
      for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
2110
0
  if (!exp->noname || show_allnames)
2111
0
    {
2112
      /* We use a single underscore for MS compatibility, and a
2113
         double underscore for backward compatibility with old
2114
         cygwin releases.  */
2115
0
      if (create_compat_implib)
2116
0
        fprintf (f, "\t%s\t__imp_%s\n", ASM_GLOBAL, exp->name);
2117
0
      fprintf (f, "\t%s\t_imp_%s%s\n", ASM_GLOBAL,
2118
0
         leading_underscore, exp->name);
2119
0
      if (create_compat_implib)
2120
0
        fprintf (f, "__imp_%s:\n", exp->name);
2121
0
      fprintf (f, "_imp_%s%s:\n", leading_underscore, exp->name);
2122
0
      fprintf (f, "\t%s\t%s\n", ASM_LONG, exp->name);
2123
0
    }
2124
0
    }
2125
2126
  /* Dump the reloc section if a base file is provided.  */
2127
0
  if (base_file)
2128
0
    {
2129
0
      bfd_vma addr;
2130
0
      bfd_vma need[COFF_PAGE_SIZE];
2131
0
      bfd_vma page_addr;
2132
0
      bfd_size_type numbytes;
2133
0
      int num_entries;
2134
0
      bfd_vma *copy;
2135
0
      int j;
2136
0
      int on_page;
2137
0
      fprintf (f, "\t.section\t.init\n");
2138
0
      fprintf (f, "lab:\n");
2139
2140
0
      fseek (base_file, 0, SEEK_END);
2141
0
      numbytes = ftell (base_file);
2142
0
      fseek (base_file, 0, SEEK_SET);
2143
0
      copy = xmalloc (numbytes);
2144
0
      if (fread (copy, 1, numbytes, base_file) < numbytes)
2145
0
  fatal (_("failed to read the number of entries from base file"));
2146
0
      num_entries = numbytes / sizeof (bfd_vma);
2147
2148
0
      fprintf (f, "\t.section\t.reloc\n");
2149
0
      if (num_entries)
2150
0
  {
2151
0
    int src;
2152
0
    int dst = 0;
2153
0
    bfd_vma last = (bfd_vma) -1;
2154
0
    qsort (copy, num_entries, sizeof (bfd_vma), sfunc);
2155
    /* Delete duplicates */
2156
0
    for (src = 0; src < num_entries; src++)
2157
0
      {
2158
0
        if (last != copy[src])
2159
0
    last = copy[dst++] = copy[src];
2160
0
      }
2161
0
    num_entries = dst;
2162
0
    addr = copy[0];
2163
0
    page_addr = addr & PAGE_MASK;   /* work out the page addr */
2164
0
    on_page = 0;
2165
0
    for (j = 0; j < num_entries; j++)
2166
0
      {
2167
0
        addr = copy[j];
2168
0
        if ((addr & PAGE_MASK) != page_addr)
2169
0
    {
2170
0
      flush_page (f, need, page_addr, on_page);
2171
0
      on_page = 0;
2172
0
      page_addr = addr & PAGE_MASK;
2173
0
    }
2174
0
        need[on_page++] = addr;
2175
0
      }
2176
0
    flush_page (f, need, page_addr, on_page);
2177
#if 0
2178
    fprintf (f, "\t%s\t0,0\t%s End\n", ASM_LONG, ASM_C);
2179
#endif
2180
0
  }
2181
0
      free (copy);
2182
0
    }
2183
2184
0
  generate_idata_ofile (f);
2185
2186
0
  fclose (f);
2187
2188
  /* Assemble the file.  */
2189
0
  assemble_file (TMP_ASM, exp_name);
2190
2191
0
  if (dontdeltemps == 0)
2192
0
    {
2193
0
      temp_file_to_remove[TEMP_EXPORT_FILE] = NULL;
2194
0
      unlink (TMP_ASM);
2195
0
    }
2196
2197
0
  inform (_("Generated exports file"));
2198
0
}
2199
2200
static char *
2201
xlate (const char *name)
2202
0
{
2203
0
  int lead_at = *name == '@';
2204
0
  int is_stdcall = !lead_at && strchr (name, '@') != NULL;
2205
0
  char *copy;
2206
2207
0
  if (!lead_at && (add_underscore
2208
0
       || (add_stdcall_underscore && is_stdcall)))
2209
0
    copy = xasprintf ("_%s", name);
2210
0
  else
2211
0
    copy = xstrdup (name + (killat ? lead_at : 0));
2212
2213
0
  if (killat)
2214
0
    {
2215
      /* PR 9766: Look for the last @ sign in the name.  */
2216
0
      char *p = strrchr (copy, '@');
2217
0
      if (p && ISDIGIT (p[1]))
2218
0
  *p = 0;
2219
0
    }
2220
0
  return copy;
2221
0
}
2222
2223
typedef struct
2224
{
2225
  int id;
2226
  const char *name;
2227
  int flags;
2228
  int align;
2229
  asection *sec;
2230
  asymbol *sym;
2231
  asymbol **sympp;
2232
  int size;
2233
  unsigned char *data;
2234
} sinfo;
2235
2236
#define INIT_SEC_DATA(id, name, flags, align) \
2237
  { id, name, flags, align, NULL, NULL, NULL, 0, NULL }
2238
2239
0
#define TEXT 0
2240
#define DATA 1
2241
#define BSS 2
2242
0
#define IDATA7 3
2243
0
#define IDATA5 4
2244
0
#define IDATA4 5
2245
0
#define IDATA6 6
2246
2247
0
#define NSECS 7
2248
2249
#define TEXT_SEC_FLAGS   \
2250
  (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY | SEC_HAS_CONTENTS)
2251
#define DATA_SEC_FLAGS   (SEC_ALLOC | SEC_LOAD | SEC_DATA)
2252
#define BSS_SEC_FLAGS     SEC_ALLOC
2253
2254
static sinfo secdata_plain[NSECS] =
2255
{
2256
  INIT_SEC_DATA (TEXT,   ".text",    TEXT_SEC_FLAGS,   2),
2257
  INIT_SEC_DATA (DATA,   ".data",    DATA_SEC_FLAGS,   2),
2258
  INIT_SEC_DATA (BSS,    ".bss",     BSS_SEC_FLAGS,    2),
2259
  INIT_SEC_DATA (IDATA7, ".idata$7", SEC_HAS_CONTENTS, 2),
2260
  INIT_SEC_DATA (IDATA5, ".idata$5", SEC_HAS_CONTENTS, 2),
2261
  INIT_SEC_DATA (IDATA4, ".idata$4", SEC_HAS_CONTENTS, 2),
2262
  INIT_SEC_DATA (IDATA6, ".idata$6", SEC_HAS_CONTENTS, 1)
2263
};
2264
2265
static sinfo secdata_delay[NSECS] =
2266
{
2267
  INIT_SEC_DATA (TEXT,   ".text",    TEXT_SEC_FLAGS,   2),
2268
  INIT_SEC_DATA (DATA,   ".data",    DATA_SEC_FLAGS,   2),
2269
  INIT_SEC_DATA (BSS,    ".bss",     BSS_SEC_FLAGS,    2),
2270
  INIT_SEC_DATA (IDATA7, ".didat$7", SEC_HAS_CONTENTS, 2),
2271
  INIT_SEC_DATA (IDATA5, ".didat$5", SEC_HAS_CONTENTS, 2),
2272
  INIT_SEC_DATA (IDATA4, ".didat$4", SEC_HAS_CONTENTS, 2),
2273
  INIT_SEC_DATA (IDATA6, ".didat$6", SEC_HAS_CONTENTS, 1)
2274
};
2275
2276
/* This is what we're trying to make.  We generate the imp symbols with
2277
   both single and double underscores, for compatibility.
2278
2279
  .text
2280
  .global _GetFileVersionInfoSizeW@8
2281
  .global __imp_GetFileVersionInfoSizeW@8
2282
_GetFileVersionInfoSizeW@8:
2283
  jmp * __imp_GetFileVersionInfoSizeW@8
2284
  .section  .idata$7  # To force loading of head
2285
  .long __version_a_head
2286
# Import Address Table
2287
  .section  .idata$5
2288
__imp_GetFileVersionInfoSizeW@8:
2289
  .rva  ID2
2290
2291
# Import Lookup Table
2292
  .section  .idata$4
2293
  .rva  ID2
2294
# Hint/Name table
2295
  .section  .idata$6
2296
ID2:  .short  2
2297
  .asciz  "GetFileVersionInfoSizeW"  */
2298
2299
static char *
2300
make_label (const char *prefix, const char *name)
2301
0
{
2302
0
  int len = strlen (ASM_PREFIX (name)) + strlen (prefix) + strlen (name);
2303
0
  char *copy = xmalloc (len + 1);
2304
2305
0
  strcpy (copy, ASM_PREFIX (name));
2306
0
  strcat (copy, prefix);
2307
0
  strcat (copy, name);
2308
0
  return copy;
2309
0
}
2310
2311
static char *
2312
make_imp_label (bfd *abfd, const char *prefix, const char *name)
2313
0
{
2314
0
  int len;
2315
0
  char *copy;
2316
2317
0
  if (name[0] == '@')
2318
0
    {
2319
0
      len = strlen (prefix) + strlen (name);
2320
0
      copy = bfd_xalloc (abfd, len + 1);
2321
0
      strcpy (copy, prefix);
2322
0
      strcat (copy, name);
2323
0
    }
2324
0
  else
2325
0
    {
2326
0
      len = strlen (ASM_PREFIX (name)) + strlen (prefix) + strlen (name);
2327
0
      copy = bfd_xalloc (abfd, len + 1);
2328
0
      strcpy (copy, prefix);
2329
0
      strcat (copy, ASM_PREFIX (name));
2330
0
      strcat (copy, name);
2331
0
    }
2332
0
  return copy;
2333
0
}
2334
2335
static bfd *
2336
make_one_lib_file (export_type *exp, int i, int delay)
2337
0
{
2338
0
  sinfo *const secdata = delay ? secdata_delay : secdata_plain;
2339
0
  char *outname = TMP_STUB;
2340
0
  size_t name_len = strlen (outname);
2341
0
  sprintf (outname + name_len - 7, "%05d.o", i);
2342
2343
0
  bfd *abfd = bfd_openw (outname, HOW_BFD_WRITE_TARGET);
2344
0
  if (!abfd)
2345
    /* xgettext:c-format */
2346
0
    fatal (_("bfd_open failed open stub file: %s: %s"),
2347
0
     outname, bfd_get_errmsg ());
2348
2349
  /* xgettext:c-format */
2350
0
  inform (_("Creating stub file: %s"), outname);
2351
2352
0
  bfd_set_format (abfd, bfd_object);
2353
0
  bfd_set_arch_mach (abfd, HOW_BFD_ARCH, 0);
2354
2355
#ifdef DLLTOOL_ARM
2356
  if (machine == MARM_INTERWORK || machine == MTHUMB)
2357
    bfd_set_private_flags (abfd, F_INTERWORK);
2358
#endif
2359
2360
  /* First make symbols for the sections.  */
2361
0
  flagword applicable = bfd_applicable_section_flags (abfd);
2362
0
#ifndef EXTRA
2363
0
#define EXTRA    0
2364
0
#endif
2365
0
  asymbol *ptrs[NSECS + 4 + EXTRA + 1];
2366
0
  int oidx = 0;
2367
0
  for (i = 0; i < NSECS; i++)
2368
0
    {
2369
0
      sinfo *si = secdata + i;
2370
2371
0
      if (si->id != i)
2372
0
  abort ();
2373
0
      si->sec = bfd_make_section_old_way (abfd, si->name);
2374
0
      bfd_set_section_flags (si->sec, si->flags & applicable);
2375
2376
0
      bfd_set_section_alignment (si->sec, si->align);
2377
0
      si->sec->output_section = si->sec;
2378
0
      si->sym = bfd_make_empty_symbol(abfd);
2379
0
      si->sym->name = si->sec->name;
2380
0
      si->sym->section = si->sec;
2381
0
      si->sym->flags = BSF_LOCAL;
2382
0
      si->sym->value = 0;
2383
0
      ptrs[oidx] = si->sym;
2384
0
      si->sympp = ptrs + oidx;
2385
0
      si->size = 0;
2386
0
      si->data = NULL;
2387
2388
0
      oidx++;
2389
0
    }
2390
2391
0
  if (! exp->data)
2392
0
    {
2393
0
      asymbol *exp_label = bfd_make_empty_symbol (abfd);
2394
0
      exp_label->name = make_imp_label (abfd, "", exp->name);
2395
0
      exp_label->section = secdata[TEXT].sec;
2396
0
      exp_label->flags = BSF_GLOBAL;
2397
0
      exp_label->value = 0;
2398
2399
#ifdef DLLTOOL_ARM
2400
      if (machine == MTHUMB)
2401
  bfd_coff_set_symbol_class (abfd, exp_label, C_THUMBEXTFUNC);
2402
#endif
2403
0
      ptrs[oidx++] = exp_label;
2404
0
    }
2405
2406
  /* Generate imp symbols with one underscore for Microsoft
2407
     compatibility, and with two underscores for backward
2408
     compatibility with old versions of cygwin.  */
2409
0
  asymbol *iname = NULL;
2410
0
  if (create_compat_implib)
2411
0
    {
2412
0
      iname = bfd_make_empty_symbol (abfd);
2413
0
      iname->name = make_imp_label (abfd, "___imp", exp->name);
2414
0
      iname->section = secdata[IDATA5].sec;
2415
0
      iname->flags = BSF_GLOBAL;
2416
0
      iname->value = 0;
2417
0
    }
2418
2419
0
  asymbol *iname2 = bfd_make_empty_symbol (abfd);
2420
0
  iname2->name = make_imp_label (abfd, "__imp_", exp->name);
2421
0
  iname2->section = secdata[IDATA5].sec;
2422
0
  iname2->flags = BSF_GLOBAL;
2423
0
  iname2->value = 0;
2424
2425
0
  asymbol *iname_lab = bfd_make_empty_symbol (abfd);
2426
0
  iname_lab->name = head_label;
2427
0
  iname_lab->section = bfd_und_section_ptr;
2428
0
  iname_lab->flags = 0;
2429
0
  iname_lab->value = 0;
2430
2431
0
  asymbol **iname_pp = ptrs + oidx;
2432
0
  if (create_compat_implib)
2433
0
    ptrs[oidx++] = iname;
2434
0
  ptrs[oidx++] = iname2;
2435
2436
0
  asymbol **iname_lab_pp = ptrs + oidx;
2437
0
  ptrs[oidx++] = iname_lab;
2438
2439
0
  ptrs[oidx] = 0;
2440
2441
0
  for (i = 0; i < NSECS; i++)
2442
0
    {
2443
0
      sinfo *si = secdata + i;
2444
0
      asection *sec = si->sec;
2445
0
      arelent *rel, *rel2 = 0, *rel3 = 0;
2446
0
      arelent **rpp;
2447
2448
0
      switch (i)
2449
0
  {
2450
0
  case TEXT:
2451
0
    if (! exp->data)
2452
0
      {
2453
0
        unsigned int rpp_len;
2454
2455
0
        si->size = HOW_JTAB_SIZE;
2456
0
        si->data = bfd_xalloc (abfd, HOW_JTAB_SIZE);
2457
0
        memcpy (si->data, HOW_JTAB, HOW_JTAB_SIZE);
2458
2459
        /* Add the reloc into idata$5.  */
2460
0
        rel = bfd_xalloc (abfd, sizeof (arelent));
2461
2462
0
        rpp_len = delay ? 4 : 2;
2463
2464
0
        if (machine == MAARCH64)
2465
0
    rpp_len++;
2466
2467
0
        rpp = bfd_xalloc (abfd, sizeof (arelent *) * rpp_len);
2468
0
        rpp[0] = rel;
2469
0
        rpp[1] = 0;
2470
2471
0
        rel->address = HOW_JTAB_ROFF;
2472
0
        rel->addend = 0;
2473
2474
0
        if (delay)
2475
0
    {
2476
0
      rel2 = bfd_xalloc (abfd, sizeof (arelent));
2477
0
      rpp[1] = rel2;
2478
0
      rel2->address = HOW_JTAB_ROFF2;
2479
0
      rel2->addend = 0;
2480
0
      rel3 = bfd_xalloc (abfd, sizeof (arelent));
2481
0
      rpp[2] = rel3;
2482
0
      rel3->address = HOW_JTAB_ROFF3;
2483
0
      rel3->addend = 0;
2484
0
      rpp[3] = 0;
2485
0
    }
2486
2487
0
        if (machine == MX86)
2488
0
    {
2489
0
      rel->howto = bfd_reloc_type_lookup (abfd,
2490
0
                  BFD_RELOC_32_PCREL);
2491
0
      rel->sym_ptr_ptr = iname_pp;
2492
0
    }
2493
0
        else if (machine == MAARCH64)
2494
0
    {
2495
0
      arelent *rel_add;
2496
2497
0
      rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_AARCH64_ADR_HI21_NC_PCREL);
2498
0
      rel->sym_ptr_ptr = secdata[IDATA5].sympp;
2499
2500
0
      rel_add = bfd_xalloc (abfd, sizeof (arelent));
2501
0
      rel_add->address = 4;
2502
0
      rel_add->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_AARCH64_ADD_LO12);
2503
0
      rel_add->sym_ptr_ptr = secdata[IDATA5].sympp;
2504
0
      rel_add->addend = 0;
2505
2506
0
      rpp[rpp_len - 2] = rel_add;
2507
0
      rpp[rpp_len - 1] = 0;
2508
0
    }
2509
0
        else
2510
0
    {
2511
0
      rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
2512
0
      rel->sym_ptr_ptr = secdata[IDATA5].sympp;
2513
0
    }
2514
2515
0
        if (delay)
2516
0
    {
2517
0
      if (machine == MX86)
2518
0
        rel2->howto = bfd_reloc_type_lookup (abfd,
2519
0
               BFD_RELOC_32_PCREL);
2520
0
      else
2521
0
        rel2->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
2522
0
      rel2->sym_ptr_ptr = rel->sym_ptr_ptr;
2523
0
      rel3->howto = bfd_reloc_type_lookup (abfd,
2524
0
                   BFD_RELOC_32_PCREL);
2525
0
      rel3->sym_ptr_ptr = iname_lab_pp;
2526
0
    }
2527
2528
0
        sec->orelocation = rpp;
2529
0
        sec->reloc_count = rpp_len - 1;
2530
0
      }
2531
0
    break;
2532
2533
0
  case IDATA5:
2534
0
    if (delay)
2535
0
      {
2536
0
        si->size = create_for_pep ? 8 : 4;
2537
0
        si->data = bfd_xalloc (abfd, si->size);
2538
0
        sec->reloc_count = 1;
2539
0
        memset (si->data, 0, si->size);
2540
        /* Point after jmp [__imp_...] instruction.  */
2541
0
        si->data[0] = 6;
2542
0
        rel = bfd_xalloc (abfd, sizeof (arelent));
2543
0
        rpp = bfd_xalloc (abfd, sizeof (arelent *) * 2);
2544
0
        rpp[0] = rel;
2545
0
        rpp[1] = 0;
2546
0
        rel->address = 0;
2547
0
        rel->addend = 0;
2548
0
        if (create_for_pep)
2549
0
    rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_64);
2550
0
        else
2551
0
    rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_32);
2552
0
        rel->sym_ptr_ptr = secdata[TEXT].sympp;
2553
0
        sec->orelocation = rpp;
2554
0
        break;
2555
0
      }
2556
    /* Fall through.  */
2557
2558
0
  case IDATA4:
2559
    /* An idata$4 or idata$5 is one word long, and has an
2560
       rva to idata$6.  */
2561
2562
0
    if (create_for_pep)
2563
0
      {
2564
0
        si->data = bfd_xalloc (abfd, 8);
2565
0
        si->size = 8;
2566
0
        if (exp->noname)
2567
0
    {
2568
0
      si->data[0] = exp->ordinal ;
2569
0
      si->data[1] = exp->ordinal >> 8;
2570
0
      si->data[2] = exp->ordinal >> 16;
2571
0
      si->data[3] = exp->ordinal >> 24;
2572
0
      si->data[4] = 0;
2573
0
      si->data[5] = 0;
2574
0
      si->data[6] = 0;
2575
0
      si->data[7] = 0x80;
2576
0
    }
2577
0
        else
2578
0
    {
2579
0
      sec->reloc_count = 1;
2580
0
      memset (si->data, 0, si->size);
2581
0
      rel = bfd_xalloc (abfd, sizeof (arelent));
2582
0
      rpp = bfd_xalloc (abfd, sizeof (arelent *) * 2);
2583
0
      rpp[0] = rel;
2584
0
      rpp[1] = 0;
2585
0
      rel->address = 0;
2586
0
      rel->addend = 0;
2587
0
      rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA);
2588
0
      rel->sym_ptr_ptr = secdata[IDATA6].sympp;
2589
0
      sec->orelocation = rpp;
2590
0
    }
2591
0
      }
2592
0
    else
2593
0
      {
2594
0
        si->data = bfd_xalloc (abfd, 4);
2595
0
        si->size = 4;
2596
2597
0
        if (exp->noname)
2598
0
    {
2599
0
      si->data[0] = exp->ordinal ;
2600
0
      si->data[1] = exp->ordinal >> 8;
2601
0
      si->data[2] = exp->ordinal >> 16;
2602
0
      si->data[3] = 0x80;
2603
0
    }
2604
0
        else
2605
0
    {
2606
0
      sec->reloc_count = 1;
2607
0
      memset (si->data, 0, si->size);
2608
0
      rel = bfd_xalloc (abfd, sizeof (arelent));
2609
0
      rpp = bfd_xalloc (abfd, sizeof (arelent *) * 2);
2610
0
      rpp[0] = rel;
2611
0
      rpp[1] = 0;
2612
0
      rel->address = 0;
2613
0
      rel->addend = 0;
2614
0
      rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA);
2615
0
      rel->sym_ptr_ptr = secdata[IDATA6].sympp;
2616
0
      sec->orelocation = rpp;
2617
0
    }
2618
0
      }
2619
0
    break;
2620
2621
0
  case IDATA6:
2622
0
    if (!exp->noname)
2623
0
      {
2624
        /* This used to add 1 to exp->hint.  I don't know
2625
     why it did that, and it does not match what I see
2626
     in programs compiled with the MS tools.  */
2627
0
        int idx = exp->hint;
2628
0
        const char *xname = (exp->its_name ? exp->its_name
2629
0
           : xlate (exp->import_name));
2630
0
        si->size = strlen (xname) + 3;
2631
0
        si->data = bfd_xalloc (abfd, si->size);
2632
0
        si->data[0] = idx & 0xff;
2633
0
        si->data[1] = idx >> 8;
2634
0
        memcpy (si->data + 2, xname, si->size - 2);
2635
0
        if (!exp->its_name)
2636
0
    free ((char *) xname);
2637
0
      }
2638
0
    break;
2639
0
  case IDATA7:
2640
0
    if (delay)
2641
0
      break;
2642
0
    si->size = 4;
2643
0
    si->data = bfd_xalloc (abfd, 4);
2644
0
    memset (si->data, 0, si->size);
2645
0
    rel = bfd_xalloc (abfd, sizeof (arelent));
2646
0
    rpp = bfd_xalloc (abfd, sizeof (arelent *) * 2);
2647
0
    rpp[0] = rel;
2648
0
    rel->address = 0;
2649
0
    rel->addend = 0;
2650
0
    rel->howto = bfd_reloc_type_lookup (abfd, BFD_RELOC_RVA);
2651
0
    rel->sym_ptr_ptr = iname_lab_pp;
2652
0
    sec->orelocation = rpp;
2653
0
    sec->reloc_count = 1;
2654
0
    break;
2655
0
  }
2656
0
    }
2657
2658
0
  {
2659
0
    bfd_vma vma = 0;
2660
    /* Size up all the sections.  */
2661
0
    for (i = 0; i < NSECS; i++)
2662
0
      {
2663
0
  sinfo *si = secdata + i;
2664
2665
0
  bfd_set_section_size (si->sec, si->size);
2666
0
  bfd_set_section_vma (si->sec, vma);
2667
0
      }
2668
0
  }
2669
  /* Write them out.  */
2670
0
  for (i = 0; i < NSECS; i++)
2671
0
    {
2672
0
      sinfo *si = secdata + i;
2673
2674
0
      if (i == IDATA5 && no_idata5)
2675
0
  continue;
2676
2677
0
      if (i == IDATA4 && no_idata4)
2678
0
  continue;
2679
2680
0
      bfd_set_section_contents (abfd, si->sec,
2681
0
        si->data, 0,
2682
0
        si->size);
2683
0
    }
2684
2685
0
  bfd_set_symtab (abfd, ptrs, oidx);
2686
0
  bfd_close (abfd);
2687
0
  abfd = bfd_openr (outname, HOW_BFD_READ_TARGET);
2688
0
  if (!abfd)
2689
    /* xgettext:c-format */
2690
0
    fatal (_("bfd_open failed reopen stub file: %s: %s"),
2691
0
     outname, bfd_get_errmsg ());
2692
2693
0
  return abfd;
2694
0
}
2695
2696
static bfd *
2697
make_head (void)
2698
0
{
2699
0
  FILE *f = fopen (TMP_HEAD_S, FOPEN_WT);
2700
0
  bfd *abfd;
2701
2702
0
  if (f == NULL)
2703
0
    {
2704
0
      fatal (_("failed to open temporary head file: %s"), TMP_HEAD_S);
2705
0
      return NULL;
2706
0
    }
2707
2708
0
  temp_file_to_remove[TEMP_HEAD_FILE] = TMP_HEAD_S;
2709
2710
0
  fprintf (f, "%s IMAGE_IMPORT_DESCRIPTOR\n", ASM_C);
2711
0
  fprintf (f, "\t.section\t.idata$2\n");
2712
2713
0
  fprintf (f, "\t%s\t%s\n", ASM_GLOBAL, head_label);
2714
2715
0
  fprintf (f, "%s:\n", head_label);
2716
2717
0
  fprintf (f, "\t%shname%s\t%sPtr to image import by name list\n",
2718
0
     ASM_RVA_BEFORE, ASM_RVA_AFTER, ASM_C);
2719
2720
0
  fprintf (f, "\t%sthis should be the timestamp, but NT sometimes\n", ASM_C);
2721
0
  fprintf (f, "\t%sdoesn't load DLLs when this is set.\n", ASM_C);
2722
0
  fprintf (f, "\t%s\t0\t%s loaded time\n", ASM_LONG, ASM_C);
2723
0
  fprintf (f, "\t%s\t0\t%s Forwarder chain\n", ASM_LONG, ASM_C);
2724
0
  fprintf (f, "\t%s__%s_iname%s\t%s imported dll's name\n",
2725
0
     ASM_RVA_BEFORE,
2726
0
     imp_name_lab,
2727
0
     ASM_RVA_AFTER,
2728
0
     ASM_C);
2729
0
  fprintf (f, "\t%sfthunk%s\t%s pointer to firstthunk\n",
2730
0
     ASM_RVA_BEFORE,
2731
0
     ASM_RVA_AFTER, ASM_C);
2732
2733
0
  fprintf (f, "%sStuff for compatibility\n", ASM_C);
2734
2735
0
  if (!no_idata5)
2736
0
    {
2737
0
      fprintf (f, "\t.section\t.idata$5\n");
2738
0
      if (use_nul_prefixed_import_tables)
2739
0
  {
2740
0
    if (create_for_pep)
2741
0
      fprintf (f, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
2742
0
    else
2743
0
      fprintf (f, "\t%s\t0\n", ASM_LONG);
2744
0
  }
2745
0
      fprintf (f, "fthunk:\n");
2746
0
    }
2747
2748
0
  if (!no_idata4)
2749
0
    {
2750
0
      fprintf (f, "\t.section\t.idata$4\n");
2751
0
      if (use_nul_prefixed_import_tables)
2752
0
  {
2753
0
    if (create_for_pep)
2754
0
      fprintf (f, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
2755
0
    else
2756
0
      fprintf (f, "\t%s\t0\n", ASM_LONG);
2757
0
  }
2758
0
      fprintf (f, "hname:\n");
2759
0
    }
2760
2761
0
  fclose (f);
2762
2763
0
  assemble_file (TMP_HEAD_S, TMP_HEAD_O);
2764
2765
0
  abfd = bfd_openr (TMP_HEAD_O, HOW_BFD_READ_TARGET);
2766
0
  if (abfd == NULL)
2767
    /* xgettext:c-format */
2768
0
    fatal (_("failed to open temporary head file: %s: %s"),
2769
0
     TMP_HEAD_O, bfd_get_errmsg ());
2770
2771
0
  temp_file_to_remove[TEMP_HEAD_O_FILE] = TMP_HEAD_O;
2772
0
  return abfd;
2773
0
}
2774
2775
static bfd *
2776
make_delay_head (void)
2777
0
{
2778
0
  FILE *f = fopen (TMP_HEAD_S, FOPEN_WT);
2779
0
  bfd *abfd;
2780
2781
0
  if (f == NULL)
2782
0
    {
2783
0
      fatal (_("failed to open temporary head file: %s"), TMP_HEAD_S);
2784
0
      return NULL;
2785
0
    }
2786
2787
0
  temp_file_to_remove[TEMP_HEAD_FILE] = TMP_HEAD_S;
2788
2789
  /* Output the __tailMerge__xxx function */
2790
0
  fprintf (f, "%s Import trampoline\n", ASM_C);
2791
0
  fprintf (f, "\t.section\t.text\n");
2792
0
  fprintf (f, "\t%s\t%s\n", ASM_GLOBAL, head_label);
2793
0
  if (HOW_SEH)
2794
0
    fprintf (f, "\t.seh_proc\t%s\n", head_label);
2795
0
  fprintf (f, "%s:\n", head_label);
2796
0
  fprintf (f, mtable[machine].trampoline, imp_name_lab);
2797
0
  if (HOW_SEH)
2798
0
    fprintf (f, "\t.seh_endproc\n");
2799
2800
  /* Output the delay import descriptor */
2801
0
  fprintf (f, "\n%s DELAY_IMPORT_DESCRIPTOR\n", ASM_C);
2802
0
  fprintf (f, ".section\t.didat$2\n");
2803
0
  fprintf (f, "%s __DELAY_IMPORT_DESCRIPTOR_%s\n", ASM_GLOBAL,imp_name_lab);
2804
0
  fprintf (f, "__DELAY_IMPORT_DESCRIPTOR_%s:\n", imp_name_lab);
2805
0
  fprintf (f, "\t%s 1\t%s grAttrs\n", ASM_LONG, ASM_C);
2806
0
  fprintf (f, "\t%s__%s_iname%s\t%s rvaDLLName\n",
2807
0
     ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C);
2808
0
  fprintf (f, "\t%s__DLL_HANDLE_%s%s\t%s rvaHmod\n",
2809
0
     ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C);
2810
0
  fprintf (f, "\t%s__IAT_%s%s\t%s rvaIAT\n",
2811
0
     ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C);
2812
0
  fprintf (f, "\t%s__INT_%s%s\t%s rvaINT\n",
2813
0
     ASM_RVA_BEFORE, imp_name_lab, ASM_RVA_AFTER, ASM_C);
2814
0
  fprintf (f, "\t%s\t0\t%s rvaBoundIAT\n", ASM_LONG, ASM_C);
2815
0
  fprintf (f, "\t%s\t0\t%s rvaUnloadIAT\n", ASM_LONG, ASM_C);
2816
0
  fprintf (f, "\t%s\t0\t%s dwTimeStamp\n", ASM_LONG, ASM_C);
2817
2818
  /* Output the dll_handle */
2819
0
  fprintf (f, "\n.section .data\n");
2820
0
  fprintf (f, "__DLL_HANDLE_%s:\n", imp_name_lab);
2821
0
  fprintf (f, "\t%s\t0\t%s Handle\n", ASM_LONG, ASM_C);
2822
0
  if (create_for_pep)
2823
0
    fprintf (f, "\t%s\t0\n", ASM_LONG);
2824
0
  fprintf (f, "\n");
2825
2826
0
  fprintf (f, "%sStuff for compatibility\n", ASM_C);
2827
2828
0
  if (!no_idata5)
2829
0
    {
2830
0
      fprintf (f, "\t.section\t.didat$5\n");
2831
0
      if (use_nul_prefixed_import_tables)
2832
0
  {
2833
0
    if (create_for_pep)
2834
0
      fprintf (f, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
2835
0
    else
2836
0
      fprintf (f, "\t%s\t0\n", ASM_LONG);
2837
0
  }
2838
0
      fprintf (f, "__IAT_%s:\n", imp_name_lab);
2839
0
    }
2840
2841
0
  if (!no_idata4)
2842
0
    {
2843
0
      fprintf (f, "\t.section\t.didat$4\n");
2844
0
      if (use_nul_prefixed_import_tables)
2845
0
  {
2846
0
    fprintf (f, "\t%s\t0\n", ASM_LONG);
2847
0
    if (create_for_pep)
2848
0
      fprintf (f, "\t%s\t0\n", ASM_LONG);
2849
0
  }
2850
0
      fprintf (f, "__INT_%s:\n", imp_name_lab);
2851
0
    }
2852
2853
0
  fclose (f);
2854
2855
0
  assemble_file (TMP_HEAD_S, TMP_HEAD_O);
2856
2857
0
  abfd = bfd_openr (TMP_HEAD_O, HOW_BFD_READ_TARGET);
2858
0
  if (abfd == NULL)
2859
    /* xgettext:c-format */
2860
0
    fatal (_("failed to open temporary head file: %s: %s"),
2861
0
     TMP_HEAD_O, bfd_get_errmsg ());
2862
2863
0
  temp_file_to_remove[TEMP_HEAD_O_FILE] = TMP_HEAD_O;
2864
0
  return abfd;
2865
0
}
2866
2867
static bfd *
2868
make_tail (void)
2869
0
{
2870
0
  FILE *f = fopen (TMP_TAIL_S, FOPEN_WT);
2871
0
  bfd *abfd;
2872
2873
0
  if (f == NULL)
2874
0
    {
2875
0
      fatal (_("failed to open temporary tail file: %s"), TMP_TAIL_S);
2876
0
      return NULL;
2877
0
    }
2878
2879
0
  temp_file_to_remove[TEMP_TAIL_FILE] = TMP_TAIL_S;
2880
2881
0
  if (!no_idata4)
2882
0
    {
2883
0
      fprintf (f, "\t.section\t.idata$4\n");
2884
0
      if (create_for_pep)
2885
0
  fprintf (f, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
2886
0
      else
2887
0
  fprintf (f, "\t%s\t0\n", ASM_LONG); /* NULL terminating list.  */
2888
0
    }
2889
2890
0
  if (!no_idata5)
2891
0
    {
2892
0
      fprintf (f, "\t.section\t.idata$5\n");
2893
0
      if (create_for_pep)
2894
0
  fprintf (f, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
2895
0
      else
2896
0
  fprintf (f, "\t%s\t0\n", ASM_LONG); /* NULL terminating list.  */
2897
0
    }
2898
2899
0
  fprintf (f, "\t.section\t.idata$7\n");
2900
0
  fprintf (f, "\t%s\t__%s_iname\n", ASM_GLOBAL, imp_name_lab);
2901
0
  fprintf (f, "__%s_iname:\t%s\t\"%s\"\n",
2902
0
     imp_name_lab, ASM_TEXT, dll_name);
2903
2904
0
  fclose (f);
2905
2906
0
  assemble_file (TMP_TAIL_S, TMP_TAIL_O);
2907
2908
0
  abfd = bfd_openr (TMP_TAIL_O, HOW_BFD_READ_TARGET);
2909
0
  if (abfd == NULL)
2910
    /* xgettext:c-format */
2911
0
    fatal (_("failed to open temporary tail file: %s: %s"),
2912
0
     TMP_TAIL_O, bfd_get_errmsg ());
2913
2914
0
  temp_file_to_remove[TEMP_TAIL_O_FILE] = TMP_TAIL_O;
2915
0
  return abfd;
2916
0
}
2917
2918
static bfd *
2919
make_delay_tail (void)
2920
0
{
2921
0
  FILE *f = fopen (TMP_TAIL_S, FOPEN_WT);
2922
0
  bfd *abfd;
2923
2924
0
  if (f == NULL)
2925
0
    {
2926
0
      fatal (_("failed to open temporary tail file: %s"), TMP_TAIL_S);
2927
0
      return NULL;
2928
0
    }
2929
2930
0
  temp_file_to_remove[TEMP_TAIL_FILE] = TMP_TAIL_S;
2931
2932
0
  if (!no_idata4)
2933
0
    {
2934
0
      fprintf (f, "\t.section\t.didat$4\n");
2935
0
      if (create_for_pep)
2936
0
  fprintf (f, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
2937
0
      else
2938
0
  fprintf (f, "\t%s\t0\n", ASM_LONG); /* NULL terminating list.  */
2939
0
    }
2940
2941
0
  if (!no_idata5)
2942
0
    {
2943
0
      fprintf (f, "\t.section\t.didat$5\n");
2944
0
      if (create_for_pep)
2945
0
  fprintf (f, "\t%s\t0\n\t%s\t0\n", ASM_LONG, ASM_LONG);
2946
0
      else
2947
0
  fprintf (f, "\t%s\t0\n", ASM_LONG); /* NULL terminating list.  */
2948
0
    }
2949
2950
0
  fprintf (f, "\t.section\t.didat$7\n");
2951
0
  fprintf (f, "\t%s\t__%s_iname\n", ASM_GLOBAL, imp_name_lab);
2952
0
  fprintf (f, "__%s_iname:\t%s\t\"%s\"\n",
2953
0
     imp_name_lab, ASM_TEXT, dll_name);
2954
2955
0
  fclose (f);
2956
2957
0
  assemble_file (TMP_TAIL_S, TMP_TAIL_O);
2958
2959
0
  abfd = bfd_openr (TMP_TAIL_O, HOW_BFD_READ_TARGET);
2960
0
  if (abfd == NULL)
2961
    /* xgettext:c-format */
2962
0
    fatal (_("failed to open temporary tail file: %s: %s"),
2963
0
     TMP_TAIL_O, bfd_get_errmsg ());
2964
2965
0
  temp_file_to_remove[TEMP_TAIL_O_FILE] = TMP_TAIL_O;
2966
0
  return abfd;
2967
0
}
2968
2969
static void
2970
gen_lib_file (int delay)
2971
0
{
2972
0
  int i;
2973
0
  export_type *exp;
2974
0
  bfd *ar_head;
2975
0
  bfd *ar_tail;
2976
0
  bfd *outarch;
2977
0
  bfd * head  = 0;
2978
2979
0
  unlink (imp_name);
2980
2981
0
  outarch = bfd_openw (imp_name, HOW_BFD_WRITE_TARGET);
2982
2983
0
  if (!outarch)
2984
    /* xgettext:c-format */
2985
0
    fatal (_("Can't create .lib file: %s: %s"),
2986
0
     imp_name, bfd_get_errmsg ());
2987
2988
  /* xgettext:c-format */
2989
0
  inform (_("Creating library file: %s"), imp_name);
2990
2991
0
  xatexit (unlink_temp_files);
2992
2993
0
  bfd_set_format (outarch, bfd_archive);
2994
0
  outarch->has_armap = 1;
2995
0
  outarch->is_thin_archive = 0;
2996
2997
0
  if (deterministic)
2998
0
    outarch->flags |= BFD_DETERMINISTIC_OUTPUT;
2999
3000
  /* Work out a reasonable size of things to put onto one line.  */
3001
0
  if (delay)
3002
0
    {
3003
0
      ar_head = make_delay_head ();
3004
0
      ar_tail = make_delay_tail();
3005
0
    }
3006
0
  else
3007
0
    {
3008
0
      ar_head = make_head ();
3009
0
      ar_tail = make_tail();
3010
0
    }
3011
3012
0
  if (ar_head == NULL || ar_tail == NULL)
3013
0
    return;
3014
3015
0
  for (i = 0; (exp = d_exports_lexically[i]); i++)
3016
0
    {
3017
0
      bfd *n;
3018
      /* Don't add PRIVATE entries to import lib.  */
3019
0
      if (exp->private)
3020
0
  continue;
3021
0
      n = make_one_lib_file (exp, i, delay);
3022
0
      n->archive_next = head;
3023
0
      head = n;
3024
0
      if (ext_prefix_alias)
3025
0
  {
3026
0
    export_type alias_exp;
3027
3028
0
    assert (i < PREFIX_ALIAS_BASE);
3029
0
    alias_exp.name = make_imp_label (outarch, ext_prefix_alias, exp->name);
3030
0
    alias_exp.internal_name = exp->internal_name;
3031
0
    alias_exp.its_name = exp->its_name;
3032
0
    alias_exp.import_name = exp->name;
3033
0
    alias_exp.ordinal = exp->ordinal;
3034
0
    alias_exp.constant = exp->constant;
3035
0
    alias_exp.noname = exp->noname;
3036
0
    alias_exp.private = exp->private;
3037
0
    alias_exp.data = exp->data;
3038
0
    alias_exp.hint = exp->hint;
3039
0
    alias_exp.forward = exp->forward;
3040
0
    alias_exp.next = exp->next;
3041
0
    n = make_one_lib_file (&alias_exp, i + PREFIX_ALIAS_BASE, delay);
3042
0
    n->archive_next = head;
3043
0
    head = n;
3044
0
  }
3045
0
    }
3046
3047
  /* Now stick them all into the archive.  */
3048
0
  ar_head->archive_next = head;
3049
0
  ar_tail->archive_next = ar_head;
3050
0
  head = ar_tail;
3051
3052
0
  if (! bfd_set_archive_head (outarch, head))
3053
0
    bfd_fatal ("bfd_set_archive_head");
3054
3055
0
  if (! bfd_close (outarch))
3056
0
    bfd_fatal (imp_name);
3057
3058
  /* Delete all the temp files.  */
3059
0
  unlink_temp_files ();
3060
3061
0
  if (dontdeltemps < 2)
3062
0
    {
3063
0
      char *name = TMP_STUB;
3064
0
      size_t name_len = strlen (name);
3065
3066
0
      for (i = 0; (exp = d_exports_lexically[i]); i++)
3067
0
  {
3068
    /* Don't delete non-existent stubs for PRIVATE entries.  */
3069
0
    if (exp->private)
3070
0
      continue;
3071
0
    sprintf (name + name_len - 7, "%05d.o", i);
3072
0
    if (unlink (name) < 0)
3073
      /* xgettext:c-format */
3074
0
      non_fatal (_("cannot delete %s: %s"), name, strerror (errno));
3075
0
    if (ext_prefix_alias)
3076
0
      {
3077
0
        sprintf (name + name_len - 7, "%05d.o", i + PREFIX_ALIAS_BASE);
3078
0
        if (unlink (name) < 0)
3079
    /* xgettext:c-format */
3080
0
    non_fatal (_("cannot delete %s: %s"), name, strerror (errno));
3081
0
      }
3082
0
  }
3083
0
    }
3084
3085
0
  inform (_("Created lib file"));
3086
0
}
3087
3088
/* Append a copy of data (cast to char *) to list.  */
3089
3090
static void
3091
dll_name_list_append (dll_name_list_type * list, bfd_byte * data)
3092
0
{
3093
0
  dll_name_list_node_type * entry;
3094
3095
  /* Error checking.  */
3096
0
  if (! list || ! list->tail)
3097
0
    return;
3098
3099
  /* Allocate new node.  */
3100
0
  entry = ((dll_name_list_node_type *)
3101
0
     xmalloc (sizeof (dll_name_list_node_type)));
3102
3103
  /* Initialize its values.  */
3104
0
  entry->dllname = xstrdup ((char *) data);
3105
0
  entry->next = NULL;
3106
3107
  /* Add to tail, and move tail.  */
3108
0
  list->tail->next = entry;
3109
0
  list->tail = entry;
3110
0
}
3111
3112
/* Count the number of entries in list.  */
3113
3114
static int
3115
dll_name_list_count (dll_name_list_type * list)
3116
0
{
3117
0
  dll_name_list_node_type * p;
3118
0
  int count = 0;
3119
3120
  /* Error checking.  */
3121
0
  if (! list || ! list->head)
3122
0
    return 0;
3123
3124
0
  p = list->head;
3125
3126
0
  while (p && p->next)
3127
0
    {
3128
0
      count++;
3129
0
      p = p->next;
3130
0
    }
3131
0
  return count;
3132
0
}
3133
3134
/* Print each entry in list to stdout.  */
3135
3136
static void
3137
dll_name_list_print (dll_name_list_type * list)
3138
0
{
3139
0
  dll_name_list_node_type * p;
3140
3141
  /* Error checking.  */
3142
0
  if (! list || ! list->head)
3143
0
    return;
3144
3145
0
  p = list->head;
3146
3147
0
  while (p && p->next && p->next->dllname && *p->next->dllname)
3148
0
    {
3149
0
      printf ("%s\n", p->next->dllname);
3150
0
      p = p->next;
3151
0
    }
3152
0
}
3153
3154
/* Free all entries in list, and list itself.  */
3155
3156
static void
3157
dll_name_list_free (dll_name_list_type * list)
3158
0
{
3159
0
  if (list)
3160
0
    {
3161
0
      dll_name_list_free_contents (list->head);
3162
0
      list->head = NULL;
3163
0
      list->tail = NULL;
3164
0
      free (list);
3165
0
    }
3166
0
}
3167
3168
/* Recursive function to free all nodes entry->next->next...
3169
   as well as entry itself.  */
3170
3171
static void
3172
dll_name_list_free_contents (dll_name_list_node_type * entry)
3173
0
{
3174
0
  while (entry)
3175
0
    {
3176
0
      dll_name_list_node_type *next = entry->next;
3177
0
      free (entry->dllname);
3178
0
      free (entry);
3179
0
      entry = next;
3180
0
    }
3181
0
}
3182
3183
/* Allocate and initialize a dll_name_list_type object,
3184
   including its sentinel node.  Caller is responsible
3185
   for calling dll_name_list_free when finished with
3186
   the list.  */
3187
3188
static dll_name_list_type *
3189
dll_name_list_create (void)
3190
0
{
3191
  /* Allocate list.  */
3192
0
  dll_name_list_type * list = xmalloc (sizeof (dll_name_list_type));
3193
3194
  /* Allocate and initialize sentinel node.  */
3195
0
  list->head = xmalloc (sizeof (dll_name_list_node_type));
3196
0
  list->head->dllname = NULL;
3197
0
  list->head->next = NULL;
3198
3199
  /* Bookkeeping for empty list.  */
3200
0
  list->tail = list->head;
3201
3202
0
  return list;
3203
0
}
3204
3205
/* Search the symbol table of the suppled BFD for a symbol whose name matches
3206
   OBJ (where obj is cast to const char *).  If found, set global variable
3207
   identify_member_contains_symname_result TRUE.  It is the caller's
3208
   responsibility to set the result variable FALSE before iterating with
3209
   this function.  */
3210
3211
static void
3212
identify_member_contains_symname (bfd  * abfd,
3213
          bfd  * archive_bfd ATTRIBUTE_UNUSED,
3214
          void * obj)
3215
0
{
3216
0
  long storage_needed;
3217
0
  asymbol ** symbol_table;
3218
0
  long number_of_symbols;
3219
0
  long i;
3220
0
  symname_search_data_type * search_data = (symname_search_data_type *) obj;
3221
3222
  /* If we already found the symbol in a different member,
3223
     short circuit.  */
3224
0
  if (search_data->found)
3225
0
    return;
3226
3227
0
  storage_needed = bfd_get_symtab_upper_bound (abfd);
3228
0
  if (storage_needed <= 0)
3229
0
    return;
3230
3231
0
  symbol_table = xmalloc (storage_needed);
3232
0
  number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
3233
0
  if (number_of_symbols < 0)
3234
0
    {
3235
0
      free (symbol_table);
3236
0
      return;
3237
0
    }
3238
3239
0
  for (i = 0; i < number_of_symbols; i++)
3240
0
    {
3241
0
      if (strncmp (symbol_table[i]->name,
3242
0
       search_data->symname,
3243
0
       strlen (search_data->symname)) == 0)
3244
0
  {
3245
0
    search_data->found = true;
3246
0
    break;
3247
0
  }
3248
0
    }
3249
0
  free (symbol_table);
3250
0
}
3251
3252
/* This is the main implementation for the --identify option.
3253
   Given the name of an import library in identify_imp_name, first
3254
   determine if the import library is a GNU binutils-style one (where
3255
   the DLL name is stored in an .idata$7 section), or if it is a
3256
   MS-style one (where the DLL name, along with much other data, is
3257
   stored in the .idata$6 section).  We determine the style of import
3258
   library by searching for the DLL-structure symbol inserted by MS
3259
   tools: __NULL_IMPORT_DESCRIPTOR.
3260
3261
   Once we know which section to search, evaluate each section for the
3262
   appropriate properties that indicate it may contain the name of the
3263
   associated DLL (this differs depending on the style).  Add the contents
3264
   of all sections which meet the criteria to a linked list of dll names.
3265
3266
   Finally, print them all to stdout. (If --identify-strict, an error is
3267
   reported if more than one match was found).  */
3268
3269
static void
3270
identify_dll_for_implib (void)
3271
0
{
3272
0
  bfd * abfd = NULL;
3273
0
  int count = 0;
3274
0
  identify_data_type identify_data;
3275
0
  symname_search_data_type search_data;
3276
3277
  /* Initialize identify_data.  */
3278
0
  identify_data.list = dll_name_list_create ();
3279
0
  identify_data.ms_style_implib = false;
3280
3281
  /* Initialize search_data.  */
3282
0
  search_data.symname = "__NULL_IMPORT_DESCRIPTOR";
3283
0
  search_data.found = false;
3284
3285
0
  if (bfd_init () != BFD_INIT_MAGIC)
3286
0
    fatal (_("fatal error: libbfd ABI mismatch"));
3287
3288
0
  abfd = bfd_openr (identify_imp_name, 0);
3289
0
  if (abfd == NULL)
3290
    /* xgettext:c-format */
3291
0
    fatal (_("Can't open .lib file: %s: %s"),
3292
0
     identify_imp_name, bfd_get_errmsg ());
3293
3294
0
  if (! bfd_check_format (abfd, bfd_archive))
3295
0
    {
3296
0
      if (! bfd_close (abfd))
3297
0
  bfd_fatal (identify_imp_name);
3298
3299
0
      fatal (_("%s is not a library"), identify_imp_name);
3300
0
    }
3301
3302
  /* Detect if this a Microsoft import library.  */
3303
0
  identify_search_archive (abfd,
3304
0
         identify_member_contains_symname,
3305
0
         (void *) &search_data);
3306
0
  if (search_data.found)
3307
0
    identify_data.ms_style_implib = true;
3308
3309
  /* Rewind the bfd.  */
3310
0
  if (! bfd_close (abfd))
3311
0
    bfd_fatal (identify_imp_name);
3312
0
  abfd = bfd_openr (identify_imp_name, 0);
3313
0
  if (abfd == NULL)
3314
0
    bfd_fatal (identify_imp_name);
3315
3316
0
  if (!bfd_check_format (abfd, bfd_archive))
3317
0
    {
3318
0
      if (!bfd_close (abfd))
3319
0
  bfd_fatal (identify_imp_name);
3320
3321
0
      fatal (_("%s is not a library"), identify_imp_name);
3322
0
    }
3323
3324
  /* Now search for the dll name.  */
3325
0
  identify_search_archive (abfd,
3326
0
         identify_search_member,
3327
0
         (void *) &identify_data);
3328
3329
0
  if (! bfd_close (abfd))
3330
0
    bfd_fatal (identify_imp_name);
3331
3332
0
  count = dll_name_list_count (identify_data.list);
3333
0
  if (count > 0)
3334
0
    {
3335
0
      if (identify_strict && count > 1)
3336
0
  {
3337
0
    dll_name_list_free (identify_data.list);
3338
0
    identify_data.list = NULL;
3339
0
    fatal (_("Import library `%s' specifies two or more dlls"),
3340
0
     identify_imp_name);
3341
0
  }
3342
0
      dll_name_list_print (identify_data.list);
3343
0
      dll_name_list_free (identify_data.list);
3344
0
      identify_data.list = NULL;
3345
0
    }
3346
0
  else
3347
0
    {
3348
0
      dll_name_list_free (identify_data.list);
3349
0
      identify_data.list = NULL;
3350
0
      fatal (_("Unable to determine dll name for `%s' (not an import library?)"),
3351
0
       identify_imp_name);
3352
0
    }
3353
0
}
3354
3355
/* Loop over all members of the archive, applying the supplied function to
3356
   each member that is a bfd_object.  The function will be called as if:
3357
      func (member_bfd, abfd, user_storage)  */
3358
3359
static void
3360
identify_search_archive (bfd * abfd,
3361
       void (* operation) (bfd *, bfd *, void *),
3362
       void * user_storage)
3363
0
{
3364
0
  bfd *last_arfile = NULL;
3365
3366
0
  while (1)
3367
0
    {
3368
0
      bfd *arfile = bfd_openr_next_archived_file (abfd, last_arfile);
3369
0
      if (arfile == NULL
3370
0
    || arfile == last_arfile)
3371
0
  {
3372
0
    if (arfile != NULL)
3373
0
      bfd_set_error (bfd_error_malformed_archive);
3374
0
    if (bfd_get_error () != bfd_error_no_more_archived_files)
3375
0
      bfd_fatal (bfd_get_filename (abfd));
3376
0
    break;
3377
0
  }
3378
3379
0
      if (last_arfile != NULL)
3380
0
  bfd_close (last_arfile);
3381
3382
0
      char **matching;
3383
0
      if (bfd_check_format_matches (arfile, bfd_object, &matching))
3384
0
  (*operation) (arfile, abfd, user_storage);
3385
0
      else
3386
0
  {
3387
0
    bfd_nonfatal (bfd_get_filename (arfile));
3388
0
    free (matching);
3389
0
  }
3390
3391
0
      last_arfile = arfile;
3392
0
    }
3393
3394
0
  if (last_arfile != NULL)
3395
0
    bfd_close (last_arfile);
3396
0
}
3397
3398
/* Call the identify_search_section() function for each section of this
3399
   archive member.  */
3400
3401
static void
3402
identify_search_member (bfd  *abfd,
3403
      bfd  *archive_bfd ATTRIBUTE_UNUSED,
3404
      void *obj)
3405
0
{
3406
0
  bfd_map_over_sections (abfd, identify_search_section, obj);
3407
0
}
3408
3409
/* This predicate returns true if section->name matches the desired value.
3410
   By default, this is .idata$7 (.idata$6 if the import library is
3411
   ms-style).  */
3412
3413
static bool
3414
identify_process_section_p (asection * section, bool ms_style_implib)
3415
0
{
3416
0
  static const char * SECTION_NAME = ".idata$7";
3417
0
  static const char * MS_SECTION_NAME = ".idata$6";
3418
3419
0
  const char * section_name =
3420
0
    (ms_style_implib ? MS_SECTION_NAME : SECTION_NAME);
3421
3422
0
  if (strcmp (section_name, section->name) == 0)
3423
0
    return true;
3424
0
  return false;
3425
0
}
3426
3427
/* If *section has contents and its name is .idata$7 (.idata$6 if
3428
   import lib ms-generated) -- and it satisfies several other constraints
3429
   -- then add the contents of the section to obj->list.  */
3430
3431
static void
3432
identify_search_section (bfd * abfd, asection * section, void * obj)
3433
0
{
3434
0
  bfd_byte *data = 0;
3435
0
  bfd_size_type datasize;
3436
0
  identify_data_type * identify_data = (identify_data_type *)obj;
3437
0
  bool ms_style = identify_data->ms_style_implib;
3438
3439
0
  if ((section->flags & SEC_HAS_CONTENTS) == 0)
3440
0
    return;
3441
3442
0
  if (! identify_process_section_p (section, ms_style))
3443
0
    return;
3444
3445
  /* Binutils import libs seem distinguish the .idata$7 section that contains
3446
     the DLL name from other .idata$7 sections by the absence of the
3447
     SEC_RELOC flag.  */
3448
0
  if (!ms_style && ((section->flags & SEC_RELOC) == SEC_RELOC))
3449
0
    return;
3450
3451
  /* MS import libs seem to distinguish the .idata$6 section
3452
     that contains the DLL name from other .idata$6 sections
3453
     by the presence of the SEC_DATA flag.  */
3454
0
  if (ms_style && ((section->flags & SEC_DATA) == 0))
3455
0
    return;
3456
3457
0
  if ((datasize = bfd_section_size (section)) == 0)
3458
0
    return;
3459
3460
0
  data = (bfd_byte *) xmalloc (datasize + 1);
3461
0
  data[0] = '\0';
3462
3463
0
  bfd_get_section_contents (abfd, section, data, 0, datasize);
3464
0
  data[datasize] = '\0';
3465
3466
  /* Use a heuristic to determine if data is a dll name.
3467
     Possible to defeat this if (a) the library has MANY
3468
     (more than 0x302f) imports, (b) it is an ms-style
3469
     import library, but (c) it is buggy, in that the SEC_DATA
3470
     flag is set on the "wrong" sections.  This heuristic might
3471
     also fail to record a valid dll name if the dllname uses
3472
     a multibyte or unicode character set (is that valid?).
3473
3474
     This heuristic is based on the fact that symbols names in
3475
     the chosen section -- as opposed to the dll name -- begin
3476
     at offset 2 in the data. The first two bytes are a 16bit
3477
     little-endian count, and start at 0x0000. However, the dll
3478
     name begins at offset 0 in the data. We assume that the
3479
     dll name does not contain unprintable characters.   */
3480
0
  if (data[0] != '\0' && ISPRINT (data[0])
3481
0
      && (datasize < 2 || ISPRINT (data[1])))
3482
0
    dll_name_list_append (identify_data->list, data);
3483
3484
0
  free (data);
3485
0
}
3486
3487
/* Run through the information gathered from the .o files and the
3488
   .def file and work out the best stuff.  */
3489
3490
static int
3491
pfunc (const void *a, const void *b)
3492
0
{
3493
0
  export_type *ap = *(export_type **) a;
3494
0
  export_type *bp = *(export_type **) b;
3495
3496
0
  if (ap->ordinal == bp->ordinal)
3497
0
    return 0;
3498
3499
  /* Unset ordinals go to the bottom.  */
3500
0
  if (ap->ordinal == -1)
3501
0
    return 1;
3502
0
  if (bp->ordinal == -1)
3503
0
    return -1;
3504
0
  return (ap->ordinal - bp->ordinal);
3505
0
}
3506
3507
static int
3508
nfunc (const void *a, const void *b)
3509
0
{
3510
0
  export_type *ap = *(export_type **) a;
3511
0
  export_type *bp = *(export_type **) b;
3512
0
  const char *an = ap->name;
3513
0
  const char *bn = bp->name;
3514
0
  if (ap->its_name)
3515
0
    an = ap->its_name;
3516
0
  if (bp->its_name)
3517
0
    an = bp->its_name;
3518
0
  if (killat)
3519
0
    {
3520
0
      an = (an[0] == '@') ? an + 1 : an;
3521
0
      bn = (bn[0] == '@') ? bn + 1 : bn;
3522
0
    }
3523
3524
0
  return (strcmp (an, bn));
3525
0
}
3526
3527
static void
3528
remove_null_names (export_type **ptr)
3529
0
{
3530
0
  int src;
3531
0
  int dst;
3532
3533
0
  for (dst = src = 0; src < d_nfuncs; src++)
3534
0
    {
3535
0
      if (ptr[src])
3536
0
  {
3537
0
    ptr[dst] = ptr[src];
3538
0
    dst++;
3539
0
  }
3540
0
    }
3541
0
  d_nfuncs = dst;
3542
0
}
3543
3544
static void
3545
process_duplicates (export_type **d_export_vec)
3546
0
{
3547
0
  int more = 1;
3548
0
  int i;
3549
3550
0
  while (more)
3551
0
    {
3552
0
      more = 0;
3553
      /* Remove duplicates.  */
3554
0
      qsort (d_export_vec, d_nfuncs, sizeof (export_type *), nfunc);
3555
3556
0
      for (i = 0; i < d_nfuncs - 1; i++)
3557
0
  {
3558
0
    if (strcmp (d_export_vec[i]->name,
3559
0
          d_export_vec[i + 1]->name) == 0)
3560
0
      {
3561
0
        export_type *a = d_export_vec[i];
3562
0
        export_type *b = d_export_vec[i + 1];
3563
3564
0
        more = 1;
3565
3566
        /* xgettext:c-format */
3567
0
        inform (_("Warning, ignoring duplicate EXPORT %s %d,%d"),
3568
0
          a->name, a->ordinal, b->ordinal);
3569
3570
0
        if (a->ordinal != -1
3571
0
      && b->ordinal != -1)
3572
    /* xgettext:c-format */
3573
0
    fatal (_("Error, duplicate EXPORT with ordinals: %s"),
3574
0
           a->name);
3575
3576
        /* Merge attributes.  */
3577
0
        b->ordinal = a->ordinal > 0 ? a->ordinal : b->ordinal;
3578
0
        b->constant |= a->constant;
3579
0
        b->noname |= a->noname;
3580
0
        b->data |= a->data;
3581
0
        d_export_vec[i] = 0;
3582
0
      }
3583
3584
0
    remove_null_names (d_export_vec);
3585
0
  }
3586
0
    }
3587
3588
  /* Count the names.  */
3589
0
  for (i = 0; i < d_nfuncs; i++)
3590
0
    if (!d_export_vec[i]->noname)
3591
0
      d_named_nfuncs++;
3592
0
}
3593
3594
static void
3595
fill_ordinals (export_type **d_export_vec)
3596
0
{
3597
0
  int lowest = -1;
3598
0
  int i;
3599
0
  char *ptr;
3600
0
  int size = 65536;
3601
3602
0
  qsort (d_export_vec, d_nfuncs, sizeof (export_type *), pfunc);
3603
3604
  /* Fill in the unset ordinals with ones from our range.  */
3605
0
  ptr = (char *) xmalloc (size);
3606
3607
0
  memset (ptr, 0, size);
3608
3609
  /* Mark in our large vector all the numbers that are taken.  */
3610
0
  for (i = 0; i < d_nfuncs; i++)
3611
0
    {
3612
0
      if (d_export_vec[i]->ordinal != -1)
3613
0
  {
3614
0
    ptr[d_export_vec[i]->ordinal] = 1;
3615
3616
0
    if (lowest == -1 || d_export_vec[i]->ordinal < lowest)
3617
0
      lowest = d_export_vec[i]->ordinal;
3618
0
  }
3619
0
    }
3620
3621
  /* Start at 1 for compatibility with MS toolchain.  */
3622
0
  if (lowest == -1)
3623
0
    lowest = 1;
3624
3625
  /* Now fill in ordinals where the user wants us to choose.  */
3626
0
  for (i = 0; i < d_nfuncs; i++)
3627
0
    {
3628
0
      if (d_export_vec[i]->ordinal == -1)
3629
0
  {
3630
0
    int j;
3631
3632
    /* First try within or after any user supplied range.  */
3633
0
    for (j = lowest; j < size; j++)
3634
0
      if (ptr[j] == 0)
3635
0
        {
3636
0
    ptr[j] = 1;
3637
0
    d_export_vec[i]->ordinal = j;
3638
0
    goto done;
3639
0
        }
3640
3641
    /* Then try before the range.  */
3642
0
    for (j = lowest; j >0; j--)
3643
0
      if (ptr[j] == 0)
3644
0
        {
3645
0
    ptr[j] = 1;
3646
0
    d_export_vec[i]->ordinal = j;
3647
0
    goto done;
3648
0
        }
3649
0
  done:;
3650
0
  }
3651
0
    }
3652
3653
0
  free (ptr);
3654
3655
  /* And resort.  */
3656
0
  qsort (d_export_vec, d_nfuncs, sizeof (export_type *), pfunc);
3657
3658
  /* Work out the lowest and highest ordinal numbers.  */
3659
0
  if (d_nfuncs)
3660
0
    {
3661
0
      if (d_export_vec[0])
3662
0
  d_low_ord = d_export_vec[0]->ordinal;
3663
0
      if (d_export_vec[d_nfuncs-1])
3664
0
  d_high_ord = d_export_vec[d_nfuncs-1]->ordinal;
3665
0
    }
3666
0
}
3667
3668
static void
3669
mangle_defs (void)
3670
{
3671
  /* First work out the minimum ordinal chosen.  */
3672
  export_type *exp;
3673
3674
  int i;
3675
  int hint = 0;
3676
  export_type **d_export_vec = xmalloc (sizeof (export_type *) * d_nfuncs);
3677
3678
  inform (_("Processing definitions"));
3679
3680
  for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
3681
    d_export_vec[i] = exp;
3682
3683
  process_duplicates (d_export_vec);
3684
  fill_ordinals (d_export_vec);
3685
3686
  /* Put back the list in the new order.  */
3687
  d_exports = 0;
3688
  for (i = d_nfuncs - 1; i >= 0; i--)
3689
    {
3690
      d_export_vec[i]->next = d_exports;
3691
      d_exports = d_export_vec[i];
3692
    }
3693
  free (d_export_vec);
3694
3695
  /* Build list in alpha order.  */
3696
  d_exports_lexically = (export_type **)
3697
    xmalloc (sizeof (export_type *) * (d_nfuncs + 1));
3698
3699
  for (i = 0, exp = d_exports; exp; i++, exp = exp->next)
3700
    d_exports_lexically[i] = exp;
3701
3702
  d_exports_lexically[i] = 0;
3703
3704
  qsort (d_exports_lexically, i, sizeof (export_type *), nfunc);
3705
3706
  /* Fill exp entries with their hint values.  */
3707
  for (i = 0; i < d_nfuncs; i++)
3708
    if (!d_exports_lexically[i]->noname || show_allnames)
3709
      d_exports_lexically[i]->hint = hint++;
3710
3711
  inform (_("Processed definitions"));
3712
}
3713
3714
static void
3715
usage (FILE *file, int status)
3716
0
{
3717
  /* xgetext:c-format */
3718
0
  fprintf (file, _("Usage %s <option(s)> <object-file(s)>\n"), program_name);
3719
  /* xgetext:c-format */
3720
0
  fprintf (file, _("   -m --machine <machine>    Create as DLL for <machine>.  [default: %s]\n"), mname);
3721
0
  fprintf (file, _("        possible <machine>: arm[_interwork], arm64, i386, mcore[-elf]{-le|-be}, thumb\n"));
3722
0
  fprintf (file, _("   -e --output-exp <outname> Generate an export file.\n"));
3723
0
  fprintf (file, _("   -l --output-lib <outname> Generate an interface library.\n"));
3724
0
  fprintf (file, _("   -y --output-delaylib <outname> Create a delay-import library.\n"));
3725
0
  fprintf (file, _("      --deterministic-libraries\n"));
3726
0
  if (DEFAULT_AR_DETERMINISTIC)
3727
0
    fprintf (file, _("                             Use zero for timestamps and uids/gids in output libraries (default)\n"));
3728
0
  else
3729
0
    fprintf (file, _("                             Use zero for timestamps and uids/gids in output libraries\n"));
3730
0
  fprintf (file, _("      --non-deterministic-libraries\n"));
3731
0
  if (DEFAULT_AR_DETERMINISTIC)
3732
0
    fprintf (file, _("                             Use actual timestamps and uids/gids in output libraries\n"));
3733
0
  else
3734
0
    fprintf (file, _("                             Use actual timestamps and uids/gids in output libraries (default)\n"));
3735
0
  fprintf (file, _("   -a --add-indirect         Add dll indirects to export file.\n"));
3736
0
  fprintf (file, _("   -D --dllname <name>       Name of input dll to put into interface lib.\n"));
3737
0
  fprintf (file, _("   -d --input-def <deffile>  Name of .def file to be read in.\n"));
3738
0
  fprintf (file, _("   -z --output-def <deffile> Name of .def file to be created.\n"));
3739
0
  fprintf (file, _("      --export-all-symbols   Export all symbols to .def\n"));
3740
0
  fprintf (file, _("      --no-export-all-symbols  Only export listed symbols\n"));
3741
0
  fprintf (file, _("      --exclude-symbols <list> Don't export <list>\n"));
3742
0
  fprintf (file, _("      --no-default-excludes  Clear default exclude symbols\n"));
3743
0
  fprintf (file, _("   -b --base-file <basefile> Read linker generated base file.\n"));
3744
0
  fprintf (file, _("   -x --no-idata4            Don't generate idata$4 section.\n"));
3745
0
  fprintf (file, _("   -c --no-idata5            Don't generate idata$5 section.\n"));
3746
0
  fprintf (file, _("      --use-nul-prefixed-import-tables Use zero prefixed idata$4 and idata$5.\n"));
3747
0
  fprintf (file, _("   -U --add-underscore       Add underscores to all symbols in interface library.\n"));
3748
0
  fprintf (file, _("      --add-stdcall-underscore Add underscores to stdcall symbols in interface library.\n"));
3749
0
  fprintf (file, _("      --no-leading-underscore All symbols shouldn't be prefixed by an underscore.\n"));
3750
0
  fprintf (file, _("      --leading-underscore   All symbols should be prefixed by an underscore.\n"));
3751
0
  fprintf (file, _("   -k --kill-at              Kill @<n> from exported names.\n"));
3752
0
  fprintf (file, _("   -A --add-stdcall-alias    Add aliases without @<n>.\n"));
3753
0
  fprintf (file, _("   -p --ext-prefix-alias <prefix> Add aliases with <prefix>.\n"));
3754
0
  fprintf (file, _("   -S --as <name>            Use <name> for assembler.\n"));
3755
0
  fprintf (file, _("   -f --as-flags <flags>     Pass <flags> to the assembler.\n"));
3756
0
  fprintf (file, _("   -C --compat-implib        Create backward compatible import library.\n"));
3757
0
  fprintf (file, _("   -n --no-delete            Keep temp files (repeat for extra preservation).\n"));
3758
0
  fprintf (file, _("   -t --temp-prefix <prefix> Use <prefix> to construct temp file names.\n"));
3759
0
  fprintf (file, _("   -I --identify <implib>    Report the name of the DLL associated with <implib>.\n"));
3760
0
  fprintf (file, _("      --identify-strict      Causes --identify to report error when multiple DLLs.\n"));
3761
0
  fprintf (file, _("   -v --verbose              Be verbose.\n"));
3762
0
  fprintf (file, _("   -V --version              Display the program version.\n"));
3763
0
  fprintf (file, _("   -h --help                 Display this information.\n"));
3764
0
  fprintf (file, _("   @<file>                   Read options from <file>.\n"));
3765
#ifdef DLLTOOL_MCORE_ELF
3766
  fprintf (file, _("   -M --mcore-elf <outname>  Process mcore-elf object files into <outname>.\n"));
3767
  fprintf (file, _("   -L --linker <name>        Use <name> as the linker.\n"));
3768
  fprintf (file, _("   -F --linker-flags <flags> Pass <flags> to the linker.\n"));
3769
#endif
3770
0
  if (REPORT_BUGS_TO[0] && status == 0)
3771
0
    fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
3772
0
  exit (status);
3773
0
}
3774
3775
/* 150 isn't special; it's just an arbitrary non-ASCII char value.  */
3776
enum command_line_switch
3777
{
3778
  OPTION_EXPORT_ALL_SYMS = 150,
3779
  OPTION_NO_EXPORT_ALL_SYMS,
3780
  OPTION_EXCLUDE_SYMS,
3781
  OPTION_NO_DEFAULT_EXCLUDES,
3782
  OPTION_ADD_STDCALL_UNDERSCORE,
3783
  OPTION_USE_NUL_PREFIXED_IMPORT_TABLES,
3784
  OPTION_IDENTIFY_STRICT,
3785
  OPTION_NO_LEADING_UNDERSCORE,
3786
  OPTION_LEADING_UNDERSCORE,
3787
  OPTION_DETERMINISTIC_LIBRARIES,
3788
  OPTION_NON_DETERMINISTIC_LIBRARIES
3789
};
3790
3791
static const struct option long_options[] =
3792
{
3793
  {"add-indirect", no_argument, NULL, 'a'},
3794
  {"add-stdcall-alias", no_argument, NULL, 'A'},
3795
  {"add-stdcall-underscore", no_argument, NULL, OPTION_ADD_STDCALL_UNDERSCORE},
3796
  {"add-underscore", no_argument, NULL, 'U'},
3797
  {"as", required_argument, NULL, 'S'},
3798
  {"as-flags", required_argument, NULL, 'f'},
3799
  {"base-file", required_argument, NULL, 'b'},
3800
  {"compat-implib", no_argument, NULL, 'C'},
3801
  {"def", required_argument, NULL, 'd'},     /* For compatibility with older versions.  */
3802
  {"deterministic-libraries", no_argument, NULL, OPTION_DETERMINISTIC_LIBRARIES},
3803
  {"dllname", required_argument, NULL, 'D'},
3804
  {"exclude-symbols", required_argument, NULL, OPTION_EXCLUDE_SYMS},
3805
  {"export-all-symbols", no_argument, NULL, OPTION_EXPORT_ALL_SYMS},
3806
  {"ext-prefix-alias", required_argument, NULL, 'p'},
3807
  {"help", no_argument, NULL, 'h'},
3808
  {"identify", required_argument, NULL, 'I'},
3809
  {"identify-strict", no_argument, NULL, OPTION_IDENTIFY_STRICT},
3810
  {"input-def", required_argument, NULL, 'd'},
3811
  {"kill-at", no_argument, NULL, 'k'},
3812
  {"leading-underscore", no_argument, NULL, OPTION_LEADING_UNDERSCORE},
3813
  {"machine", required_argument, NULL, 'm'},
3814
  {"mcore-elf", required_argument, NULL, 'M'},
3815
  {"no-default-excludes", no_argument, NULL, OPTION_NO_DEFAULT_EXCLUDES},
3816
  {"no-delete", no_argument, NULL, 'n'},
3817
  {"no-export-all-symbols", no_argument, NULL, OPTION_NO_EXPORT_ALL_SYMS},
3818
  {"no-idata4", no_argument, NULL, 'x'},
3819
  {"no-idata5", no_argument, NULL, 'c'},
3820
  {"no-leading-underscore", no_argument, NULL, OPTION_NO_LEADING_UNDERSCORE},
3821
  {"non-deterministic-libraries", no_argument, NULL, OPTION_NON_DETERMINISTIC_LIBRARIES},
3822
  {"output-def", required_argument, NULL, 'z'},
3823
  {"output-delaylib", required_argument, NULL, 'y'},
3824
  {"output-exp", required_argument, NULL, 'e'},
3825
  {"output-lib", required_argument, NULL, 'l'},
3826
  {"temp-prefix", required_argument, NULL, 't'},
3827
  {"use-nul-prefixed-import-tables", no_argument, NULL, OPTION_USE_NUL_PREFIXED_IMPORT_TABLES},
3828
  {"verbose", no_argument, NULL, 'v'},
3829
  {"version", no_argument, NULL, 'V'},
3830
  {NULL,0,NULL,0}
3831
};
3832
3833
int main (int, char **);
3834
3835
int
3836
old_main32 (int ac, char **av);
3837
int old_main32 (int ac, char **av)
3838
0
{
3839
0
  int c;
3840
0
  int i;
3841
0
  char *firstarg = 0;
3842
0
  program_name = av[0];
3843
0
  oav = av;
3844
3845
0
#ifdef HAVE_LC_MESSAGES
3846
0
  setlocale (LC_MESSAGES, "");
3847
0
#endif
3848
0
  setlocale (LC_CTYPE, "");
3849
0
  bindtextdomain (PACKAGE, LOCALEDIR);
3850
0
  textdomain (PACKAGE);
3851
3852
0
  bfd_set_error_program_name (program_name);
3853
0
  expandargv (&ac, &av);
3854
3855
0
  while ((c = getopt_long (ac, av,
3856
#ifdef DLLTOOL_MCORE_ELF
3857
         "m:e:l:aD:d:z:b:xp:cCuUkAS:t:f:nI:vVHhM:L:F:",
3858
#else
3859
0
         "m:e:l:y:aD:d:z:b:xp:cCuUkAS:t:f:nI:vVHh",
3860
0
#endif
3861
0
         long_options, 0))
3862
0
   != EOF)
3863
0
    {
3864
0
      switch (c)
3865
0
  {
3866
0
  case OPTION_EXPORT_ALL_SYMS:
3867
0
    export_all_symbols = true;
3868
0
    break;
3869
0
  case OPTION_NO_EXPORT_ALL_SYMS:
3870
0
    export_all_symbols = false;
3871
0
    break;
3872
0
  case OPTION_EXCLUDE_SYMS:
3873
0
    add_excludes (optarg);
3874
0
    break;
3875
0
  case OPTION_NO_DEFAULT_EXCLUDES:
3876
0
    do_default_excludes = false;
3877
0
    break;
3878
0
  case OPTION_USE_NUL_PREFIXED_IMPORT_TABLES:
3879
0
    use_nul_prefixed_import_tables = true;
3880
0
    break;
3881
0
  case OPTION_ADD_STDCALL_UNDERSCORE:
3882
0
    add_stdcall_underscore = 1;
3883
0
    break;
3884
0
  case OPTION_NO_LEADING_UNDERSCORE:
3885
0
    leading_underscore = "";
3886
0
    break;
3887
0
  case OPTION_LEADING_UNDERSCORE:
3888
0
    leading_underscore = "_";
3889
0
    break;
3890
0
  case OPTION_IDENTIFY_STRICT:
3891
0
    identify_strict = 1;
3892
0
    break;
3893
0
  case 'x':
3894
0
    no_idata4 = 1;
3895
0
    break;
3896
0
  case 'c':
3897
0
    no_idata5 = 1;
3898
0
    break;
3899
0
  case 'S':
3900
0
    as_name = optarg;
3901
0
    break;
3902
0
  case 't':
3903
0
    tmp_prefix = optarg;
3904
0
    break;
3905
0
  case 'f':
3906
0
    as_flags = optarg;
3907
0
    break;
3908
3909
    /* Ignored for compatibility.  */
3910
0
  case 'u':
3911
0
    break;
3912
0
  case 'a':
3913
0
    add_indirect = 1;
3914
0
    break;
3915
0
  case 'z':
3916
0
    output_def = fopen (optarg, FOPEN_WT);
3917
0
    if (!output_def)
3918
      /* xgettext:c-format */
3919
0
      fatal (_("Unable to open def-file: %s"), optarg);
3920
0
    break;
3921
0
  case 'D':
3922
0
    dll_name = (char*) lbasename (optarg);
3923
0
    if (dll_name != optarg)
3924
0
      non_fatal (_("Path components stripped from dllname, '%s'."),
3925
0
           optarg);
3926
0
    break;
3927
0
  case 'l':
3928
0
    imp_name = optarg;
3929
0
    break;
3930
0
  case 'e':
3931
0
    exp_name = optarg;
3932
0
    break;
3933
0
  case 'H':
3934
0
  case 'h':
3935
0
    usage (stdout, 0);
3936
0
    break;
3937
0
  case 'm':
3938
0
    mname = optarg;
3939
0
    break;
3940
0
  case 'I':
3941
0
    identify_imp_name = optarg;
3942
0
    break;
3943
0
  case 'v':
3944
0
    verbose = 1;
3945
0
    break;
3946
0
  case 'V':
3947
0
    print_version (program_name);
3948
0
    break;
3949
0
  case 'U':
3950
0
    add_underscore = 1;
3951
0
    break;
3952
0
  case 'k':
3953
0
    killat = 1;
3954
0
    break;
3955
0
  case 'A':
3956
0
    add_stdcall_alias = 1;
3957
0
    break;
3958
0
  case 'p':
3959
0
    ext_prefix_alias = optarg;
3960
0
    break;
3961
0
  case 'd':
3962
0
    def_file = optarg;
3963
0
    break;
3964
0
  case 'n':
3965
0
    dontdeltemps++;
3966
0
    break;
3967
0
  case 'b':
3968
0
    base_file = fopen (optarg, FOPEN_RB);
3969
3970
0
    if (!base_file)
3971
      /* xgettext:c-format */
3972
0
      fatal (_("Unable to open base-file: %s"), optarg);
3973
3974
0
    break;
3975
#ifdef DLLTOOL_MCORE_ELF
3976
  case 'M':
3977
    mcore_elf_out_file = optarg;
3978
    break;
3979
  case 'L':
3980
    mcore_elf_linker = optarg;
3981
    break;
3982
  case 'F':
3983
    mcore_elf_linker_flags = optarg;
3984
    break;
3985
#endif
3986
0
  case 'C':
3987
0
    create_compat_implib = 1;
3988
0
    break;
3989
0
  case 'y':
3990
0
    delayimp_name = optarg;
3991
0
    break;
3992
0
  case OPTION_DETERMINISTIC_LIBRARIES:
3993
0
    deterministic = true;
3994
0
    break;
3995
0
  case OPTION_NON_DETERMINISTIC_LIBRARIES:
3996
0
    deterministic = false;
3997
0
    break;
3998
0
  default:
3999
0
    usage (stderr, 1);
4000
0
    break;
4001
0
  }
4002
0
    }
4003
4004
0
  for (i = 0; mtable[i].type; i++)
4005
0
    if (strcmp (mtable[i].type, mname) == 0)
4006
0
      break;
4007
4008
0
  if (!mtable[i].type)
4009
    /* xgettext:c-format */
4010
0
    fatal (_("Machine '%s' not supported"), mname);
4011
4012
0
  machine = i;
4013
4014
  /* Check if we generated PE+.  */
4015
0
  create_for_pep = (strcmp (mname, "i386:x86-64") == 0
4016
0
        || strcmp (mname, "arm64") == 0);
4017
4018
  /* Check the default underscore */
4019
0
  if (leading_underscore == NULL)
4020
0
    {
4021
0
      int u;
4022
0
      static char underscore[2];
4023
0
      bfd_get_target_info (mtable[machine].how_bfd_target, NULL,
4024
0
         NULL, &u, NULL);
4025
0
      if (u == -1)
4026
0
  u = 0;
4027
0
      underscore[0] = u;
4028
0
      underscore[1] = 0;
4029
0
      leading_underscore = underscore;
4030
0
    }
4031
4032
0
  if (!dll_name && exp_name)
4033
0
    {
4034
      /* If we are inferring dll_name from exp_name,
4035
   strip off any path components, without emitting
4036
   a warning.  */
4037
0
      const char* exp_basename = lbasename (exp_name);
4038
0
      const int len = strlen (exp_basename) + 5;
4039
0
      dll_name = xmalloc (len);
4040
0
      strcpy (dll_name, exp_basename);
4041
0
      strcat (dll_name, ".dll");
4042
0
      dll_name_set_by_exp_name = 1;
4043
0
    }
4044
4045
0
  if (as_name == NULL)
4046
0
    as_name = deduce_name ("as");
4047
4048
  /* Don't use the default exclude list if we're reading only the
4049
     symbols in the .drectve section.  The default excludes are meant
4050
     to avoid exporting DLL entry point and Cygwin32 impure_ptr.  */
4051
0
  if (! export_all_symbols)
4052
0
    do_default_excludes = false;
4053
4054
0
  if (do_default_excludes)
4055
0
    set_default_excludes ();
4056
4057
0
  if (def_file)
4058
0
    process_def_file (def_file);
4059
4060
0
  while (optind < ac)
4061
0
    {
4062
0
      if (!firstarg)
4063
0
  firstarg = av[optind];
4064
0
      scan_obj_file (av[optind]);
4065
0
      optind++;
4066
0
    }
4067
4068
0
  if (tmp_prefix == NULL)
4069
0
    {
4070
      /* If possible use a deterministic prefix.  */
4071
0
      const char *input = imp_name ? imp_name : delayimp_name;
4072
0
      if (input && strlen (input) + sizeof ("_snnnnn.o") - 1 <= NAME_MAX)
4073
0
  {
4074
0
    tmp_prefix = xasprintf ("%s_", input);
4075
0
    for (i = 0; tmp_prefix[i]; i++)
4076
0
      if (!ISALNUM (tmp_prefix[i]))
4077
0
        tmp_prefix[i] = '_';
4078
0
  }
4079
0
      else
4080
0
  tmp_prefix = prefix_encode ("d", getpid ());
4081
0
    }
4082
4083
0
  mangle_defs ();
4084
4085
0
  if (exp_name)
4086
0
    gen_exp_file ();
4087
4088
0
  if (imp_name)
4089
0
    {
4090
      /* Make imp_name safe for use as a label.  */
4091
0
      char *p;
4092
4093
0
      imp_name_lab = xstrdup (imp_name);
4094
0
      for (p = imp_name_lab; *p; p++)
4095
0
  {
4096
0
    if (!ISALNUM (*p))
4097
0
      *p = '_';
4098
0
  }
4099
0
      head_label = make_label ("_head_", imp_name_lab);
4100
0
      gen_lib_file (0);
4101
0
    }
4102
4103
0
  if (delayimp_name)
4104
0
    {
4105
      /* Make delayimp_name safe for use as a label.  */
4106
0
      char *p;
4107
4108
0
      if (mtable[machine].how_dljtab == 0)
4109
0
  {
4110
0
    inform (_("Warning, machine type (%d) not supported for "
4111
0
        "delayimport."), machine);
4112
0
  }
4113
0
      else
4114
0
  {
4115
0
    killat = 1;
4116
0
    imp_name = delayimp_name;
4117
0
    imp_name_lab = xstrdup (imp_name);
4118
0
    for (p = imp_name_lab; *p; p++)
4119
0
      {
4120
0
        if (!ISALNUM (*p))
4121
0
    *p = '_';
4122
0
      }
4123
0
    head_label = make_label ("__tailMerge_", imp_name_lab);
4124
0
    gen_lib_file (1);
4125
0
  }
4126
0
    }
4127
4128
0
  if (output_def)
4129
0
    gen_def_file ();
4130
4131
0
  if (identify_imp_name)
4132
0
    {
4133
0
      identify_dll_for_implib ();
4134
0
    }
4135
4136
#ifdef DLLTOOL_MCORE_ELF
4137
  if (mcore_elf_out_file)
4138
    mcore_elf_gen_out_file ();
4139
#endif
4140
4141
0
  return 0;
4142
0
}
4143
4144
/* Look for the program formed by concatenating PROG_NAME and the
4145
   string running from PREFIX to END_PREFIX.  If the concatenated
4146
   string contains a '/', try appending EXECUTABLE_SUFFIX if it is
4147
   appropriate.  */
4148
4149
static char *
4150
look_for_prog (const char *prog_name, const char *prefix, int end_prefix)
4151
0
{
4152
0
  struct stat s;
4153
0
  char *cmd;
4154
4155
0
  cmd = xmalloc (strlen (prefix)
4156
0
     + strlen (prog_name)
4157
#ifdef HAVE_EXECUTABLE_SUFFIX
4158
     + strlen (EXECUTABLE_SUFFIX)
4159
#endif
4160
0
     + 10);
4161
0
  memcpy (cmd, prefix, end_prefix);
4162
4163
0
  strcpy (cmd + end_prefix, prog_name);
4164
4165
0
  if (strchr (cmd, '/') != NULL)
4166
0
    {
4167
0
      int found;
4168
4169
0
      found = (stat (cmd, &s) == 0
4170
#ifdef HAVE_EXECUTABLE_SUFFIX
4171
         || stat (strcat (cmd, EXECUTABLE_SUFFIX), &s) == 0
4172
#endif
4173
0
         );
4174
4175
0
      if (! found)
4176
0
  {
4177
    /* xgettext:c-format */
4178
0
    inform (_("Tried file: %s"), cmd);
4179
0
    free (cmd);
4180
0
    return NULL;
4181
0
  }
4182
0
    }
4183
4184
  /* xgettext:c-format */
4185
0
  inform (_("Using file: %s"), cmd);
4186
4187
0
  return cmd;
4188
0
}
4189
4190
/* Deduce the name of the program we are want to invoke.
4191
   PROG_NAME is the basic name of the program we want to run,
4192
   eg "as" or "ld".  The catch is that we might want actually
4193
   run "i386-pe-as".
4194
4195
   If argv[0] contains the full path, then try to find the program
4196
   in the same place, with and then without a target-like prefix.
4197
4198
   Given, argv[0] = /usr/local/bin/i586-cygwin32-dlltool,
4199
   deduce_name("as") uses the following search order:
4200
4201
     /usr/local/bin/i586-cygwin32-as
4202
     /usr/local/bin/as
4203
     as
4204
4205
   If there's an EXECUTABLE_SUFFIX, it'll use that as well; for each
4206
   name, it'll try without and then with EXECUTABLE_SUFFIX.
4207
4208
   Given, argv[0] = i586-cygwin32-dlltool, it will not even try "as"
4209
   as the fallback, but rather return i586-cygwin32-as.
4210
4211
   Oh, and given, argv[0] = dlltool, it'll return "as".
4212
4213
   Returns a dynamically allocated string.  */
4214
4215
static char *
4216
deduce_name (const char *prog_name)
4217
0
{
4218
0
  char *cmd;
4219
0
  char *dash, *slash, *cp;
4220
4221
0
  dash = NULL;
4222
0
  slash = NULL;
4223
0
  for (cp = program_name; *cp != '\0'; ++cp)
4224
0
    {
4225
0
      if (*cp == '-')
4226
0
  dash = cp;
4227
0
      if (
4228
#if defined(__DJGPP__) || defined (__CYGWIN__) || defined(__WIN32__)
4229
    *cp == ':' || *cp == '\\' ||
4230
#endif
4231
0
    *cp == '/')
4232
0
  {
4233
0
    slash = cp;
4234
0
    dash = NULL;
4235
0
  }
4236
0
    }
4237
4238
0
  cmd = NULL;
4239
4240
0
  if (dash != NULL)
4241
0
    {
4242
      /* First, try looking for a prefixed PROG_NAME in the
4243
   PROGRAM_NAME directory, with the same prefix as PROGRAM_NAME.  */
4244
0
      cmd = look_for_prog (prog_name, program_name, dash - program_name + 1);
4245
0
    }
4246
4247
0
  if (slash != NULL && cmd == NULL)
4248
0
    {
4249
      /* Next, try looking for a PROG_NAME in the same directory as
4250
   that of this program.  */
4251
0
      cmd = look_for_prog (prog_name, program_name, slash - program_name + 1);
4252
0
    }
4253
4254
0
  if (cmd == NULL)
4255
0
    {
4256
      /* Just return PROG_NAME as is.  */
4257
0
      cmd = xstrdup (prog_name);
4258
0
    }
4259
4260
0
  return cmd;
4261
0
}
4262
4263
#ifdef DLLTOOL_MCORE_ELF
4264
typedef struct fname_cache
4265
{
4266
  const char *         filename;
4267
  struct fname_cache * next;
4268
}
4269
fname_cache;
4270
4271
static fname_cache fnames;
4272
4273
static void
4274
mcore_elf_cache_filename (const char * filename)
4275
{
4276
  fname_cache * ptr;
4277
4278
  ptr = & fnames;
4279
4280
  while (ptr->next != NULL)
4281
    ptr = ptr->next;
4282
4283
  ptr->filename = filename;
4284
  ptr->next     = (fname_cache *) malloc (sizeof (fname_cache));
4285
  if (ptr->next != NULL)
4286
    ptr->next->next = NULL;
4287
}
4288
4289
#define MCORE_ELF_TMP_OBJ "mcoreelf.o"
4290
#define MCORE_ELF_TMP_EXP "mcoreelf.exp"
4291
#define MCORE_ELF_TMP_LIB "mcoreelf.lib"
4292
4293
static void
4294
mcore_elf_gen_out_file (void)
4295
{
4296
  fname_cache * ptr;
4297
  dyn_string_t ds;
4298
4299
  /* Step one.  Run 'ld -r' on the input object files in order to resolve
4300
     any internal references and to generate a single .exports section.  */
4301
  ptr = & fnames;
4302
4303
  ds = dyn_string_new (100);
4304
  dyn_string_append_cstr (ds, "-r ");
4305
4306
  if (mcore_elf_linker_flags != NULL)
4307
    dyn_string_append_cstr (ds, mcore_elf_linker_flags);
4308
4309
  while (ptr->next != NULL)
4310
    {
4311
      dyn_string_append_cstr (ds, ptr->filename);
4312
      dyn_string_append_cstr (ds, " ");
4313
4314
      ptr = ptr->next;
4315
    }
4316
4317
  dyn_string_append_cstr (ds, "-o ");
4318
  dyn_string_append_cstr (ds, MCORE_ELF_TMP_OBJ);
4319
4320
  if (mcore_elf_linker == NULL)
4321
    mcore_elf_linker = deduce_name ("ld");
4322
4323
  run (mcore_elf_linker, ds->s);
4324
4325
  dyn_string_delete (ds);
4326
4327
  /* Step two. Create a .exp file and a .lib file from the temporary file.
4328
     Do this by recursively invoking dlltool...  */
4329
  ds = dyn_string_new (100);
4330
4331
  dyn_string_append_cstr (ds, "-S ");
4332
  dyn_string_append_cstr (ds, as_name);
4333
4334
  dyn_string_append_cstr (ds, " -e ");
4335
  dyn_string_append_cstr (ds, MCORE_ELF_TMP_EXP);
4336
  dyn_string_append_cstr (ds, " -l ");
4337
  dyn_string_append_cstr (ds, MCORE_ELF_TMP_LIB);
4338
  dyn_string_append_cstr (ds, " " );
4339
  dyn_string_append_cstr (ds, MCORE_ELF_TMP_OBJ);
4340
4341
  if (verbose)
4342
    dyn_string_append_cstr (ds, " -v");
4343
4344
  if (dontdeltemps)
4345
    {
4346
      dyn_string_append_cstr (ds, " -n");
4347
4348
      if (dontdeltemps > 1)
4349
  dyn_string_append_cstr (ds, " -n");
4350
    }
4351
4352
  /* XXX - FIME: ought to check/copy other command line options as well.  */
4353
  run (program_name, ds->s);
4354
4355
  dyn_string_delete (ds);
4356
4357
  /* Step four. Feed the .exp and object files to ld -shared to create the dll.  */
4358
  ds = dyn_string_new (100);
4359
4360
  dyn_string_append_cstr (ds, "-shared ");
4361
4362
  if (mcore_elf_linker_flags)
4363
    dyn_string_append_cstr (ds, mcore_elf_linker_flags);
4364
4365
  dyn_string_append_cstr (ds, " ");
4366
  dyn_string_append_cstr (ds, MCORE_ELF_TMP_EXP);
4367
  dyn_string_append_cstr (ds, " ");
4368
  dyn_string_append_cstr (ds, MCORE_ELF_TMP_OBJ);
4369
  dyn_string_append_cstr (ds, " -o ");
4370
  dyn_string_append_cstr (ds, mcore_elf_out_file);
4371
4372
  run (mcore_elf_linker, ds->s);
4373
4374
  dyn_string_delete (ds);
4375
4376
  if (dontdeltemps == 0)
4377
    unlink (MCORE_ELF_TMP_EXP);
4378
4379
  if (dontdeltemps < 2)
4380
    unlink (MCORE_ELF_TMP_OBJ);
4381
}
4382
#endif /* DLLTOOL_MCORE_ELF */