PageRenderTime 80ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/mingw-w64-v2.0.999/binutils/src/binutils/dlltool.c

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