1*3d8817e4Smiod /* Routines to help build PEI-format DLLs (Win32 etc)
2*3d8817e4Smiod Copyright 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3*3d8817e4Smiod Free Software Foundation, Inc.
4*3d8817e4Smiod Written by DJ Delorie <dj@cygnus.com>
5*3d8817e4Smiod
6*3d8817e4Smiod This file is part of GLD, the Gnu Linker.
7*3d8817e4Smiod
8*3d8817e4Smiod GLD is free software; you can redistribute it and/or modify
9*3d8817e4Smiod it under the terms of the GNU General Public License as published by
10*3d8817e4Smiod the Free Software Foundation; either version 2, or (at your option)
11*3d8817e4Smiod any later version.
12*3d8817e4Smiod
13*3d8817e4Smiod GLD is distributed in the hope that it will be useful,
14*3d8817e4Smiod but WITHOUT ANY WARRANTY; without even the implied warranty of
15*3d8817e4Smiod MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*3d8817e4Smiod GNU General Public License for more details.
17*3d8817e4Smiod
18*3d8817e4Smiod You should have received a copy of the GNU General Public License
19*3d8817e4Smiod along with GLD; see the file COPYING. If not, write to the Free
20*3d8817e4Smiod Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21*3d8817e4Smiod 02110-1301, USA. */
22*3d8817e4Smiod
23*3d8817e4Smiod #include "bfd.h"
24*3d8817e4Smiod #include "sysdep.h"
25*3d8817e4Smiod #include "bfdlink.h"
26*3d8817e4Smiod #include "libiberty.h"
27*3d8817e4Smiod #include "safe-ctype.h"
28*3d8817e4Smiod
29*3d8817e4Smiod #include <time.h>
30*3d8817e4Smiod
31*3d8817e4Smiod #include "ld.h"
32*3d8817e4Smiod #include "ldexp.h"
33*3d8817e4Smiod #include "ldlang.h"
34*3d8817e4Smiod #include "ldwrite.h"
35*3d8817e4Smiod #include "ldmisc.h"
36*3d8817e4Smiod #include <ldgram.h>
37*3d8817e4Smiod #include "ldmain.h"
38*3d8817e4Smiod #include "ldfile.h"
39*3d8817e4Smiod #include "ldemul.h"
40*3d8817e4Smiod #include "coff/internal.h"
41*3d8817e4Smiod #include "../bfd/libcoff.h"
42*3d8817e4Smiod #include "deffile.h"
43*3d8817e4Smiod #include "pe-dll.h"
44*3d8817e4Smiod
45*3d8817e4Smiod /* This file turns a regular Windows PE image into a DLL. Because of
46*3d8817e4Smiod the complexity of this operation, it has been broken down into a
47*3d8817e4Smiod number of separate modules which are all called by the main function
48*3d8817e4Smiod at the end of this file. This function is not re-entrant and is
49*3d8817e4Smiod normally only called once, so static variables are used to reduce
50*3d8817e4Smiod the number of parameters and return values required.
51*3d8817e4Smiod
52*3d8817e4Smiod See also: ld/emultempl/pe.em. */
53*3d8817e4Smiod
54*3d8817e4Smiod /* Auto-import feature by Paul Sokolovsky
55*3d8817e4Smiod
56*3d8817e4Smiod Quick facts:
57*3d8817e4Smiod
58*3d8817e4Smiod 1. With this feature on, DLL clients can import variables from DLL
59*3d8817e4Smiod without any concern from their side (for example, without any source
60*3d8817e4Smiod code modifications).
61*3d8817e4Smiod
62*3d8817e4Smiod 2. This is done completely in bounds of the PE specification (to be fair,
63*3d8817e4Smiod there's a place where it pokes nose out of, but in practice it works).
64*3d8817e4Smiod So, resulting module can be used with any other PE compiler/linker.
65*3d8817e4Smiod
66*3d8817e4Smiod 3. Auto-import is fully compatible with standard import method and they
67*3d8817e4Smiod can be mixed together.
68*3d8817e4Smiod
69*3d8817e4Smiod 4. Overheads: space: 8 bytes per imported symbol, plus 20 for each
70*3d8817e4Smiod reference to it; load time: negligible; virtual/physical memory: should be
71*3d8817e4Smiod less than effect of DLL relocation, and I sincerely hope it doesn't affect
72*3d8817e4Smiod DLL sharability (too much).
73*3d8817e4Smiod
74*3d8817e4Smiod Idea
75*3d8817e4Smiod
76*3d8817e4Smiod The obvious and only way to get rid of dllimport insanity is to make client
77*3d8817e4Smiod access variable directly in the DLL, bypassing extra dereference. I.e.,
78*3d8817e4Smiod whenever client contains something like
79*3d8817e4Smiod
80*3d8817e4Smiod mov dll_var,%eax,
81*3d8817e4Smiod
82*3d8817e4Smiod address of dll_var in the command should be relocated to point into loaded
83*3d8817e4Smiod DLL. The aim is to make OS loader do so, and than make ld help with that.
84*3d8817e4Smiod Import section of PE made following way: there's a vector of structures
85*3d8817e4Smiod each describing imports from particular DLL. Each such structure points
86*3d8817e4Smiod to two other parallel vectors: one holding imported names, and one which
87*3d8817e4Smiod will hold address of corresponding imported name. So, the solution is
88*3d8817e4Smiod de-vectorize these structures, making import locations be sparse and
89*3d8817e4Smiod pointing directly into code. Before continuing, it is worth a note that,
90*3d8817e4Smiod while authors strives to make PE act ELF-like, there're some other people
91*3d8817e4Smiod make ELF act PE-like: elfvector, ;-) .
92*3d8817e4Smiod
93*3d8817e4Smiod Implementation
94*3d8817e4Smiod
95*3d8817e4Smiod For each reference of data symbol to be imported from DLL (to set of which
96*3d8817e4Smiod belong symbols with name <sym>, if __imp_<sym> is found in implib), the
97*3d8817e4Smiod import fixup entry is generated. That entry is of type
98*3d8817e4Smiod IMAGE_IMPORT_DESCRIPTOR and stored in .idata$3 subsection. Each
99*3d8817e4Smiod fixup entry contains pointer to symbol's address within .text section
100*3d8817e4Smiod (marked with __fuN_<sym> symbol, where N is integer), pointer to DLL name
101*3d8817e4Smiod (so, DLL name is referenced by multiple entries), and pointer to symbol
102*3d8817e4Smiod name thunk. Symbol name thunk is singleton vector (__nm_th_<symbol>)
103*3d8817e4Smiod pointing to IMAGE_IMPORT_BY_NAME structure (__nm_<symbol>) directly
104*3d8817e4Smiod containing imported name. Here comes that "om the edge" problem mentioned
105*3d8817e4Smiod above: PE specification rambles that name vector (OriginalFirstThunk)
106*3d8817e4Smiod should run in parallel with addresses vector (FirstThunk), i.e. that they
107*3d8817e4Smiod should have same number of elements and terminated with zero. We violate
108*3d8817e4Smiod this, since FirstThunk points directly into machine code. But in practice,
109*3d8817e4Smiod OS loader implemented the sane way: it goes thru OriginalFirstThunk and
110*3d8817e4Smiod puts addresses to FirstThunk, not something else. It once again should be
111*3d8817e4Smiod noted that dll and symbol name structures are reused across fixup entries
112*3d8817e4Smiod and should be there anyway to support standard import stuff, so sustained
113*3d8817e4Smiod overhead is 20 bytes per reference. Other question is whether having several
114*3d8817e4Smiod IMAGE_IMPORT_DESCRIPTORS for the same DLL is possible. Answer is yes, it is
115*3d8817e4Smiod done even by native compiler/linker (libth32's functions are in fact reside
116*3d8817e4Smiod in windows9x kernel32.dll, so if you use it, you have two
117*3d8817e4Smiod IMAGE_IMPORT_DESCRIPTORS for kernel32.dll). Yet other question is whether
118*3d8817e4Smiod referencing the same PE structures several times is valid. The answer is why
119*3d8817e4Smiod not, prohibiting that (detecting violation) would require more work on
120*3d8817e4Smiod behalf of loader than not doing it.
121*3d8817e4Smiod
122*3d8817e4Smiod See also: ld/emultempl/pe.em. */
123*3d8817e4Smiod
124*3d8817e4Smiod static void add_bfd_to_link (bfd *, const char *, struct bfd_link_info *);
125*3d8817e4Smiod
126*3d8817e4Smiod /* For emultempl/pe.em. */
127*3d8817e4Smiod
128*3d8817e4Smiod def_file * pe_def_file = 0;
129*3d8817e4Smiod int pe_dll_export_everything = 0;
130*3d8817e4Smiod int pe_dll_do_default_excludes = 1;
131*3d8817e4Smiod int pe_dll_kill_ats = 0;
132*3d8817e4Smiod int pe_dll_stdcall_aliases = 0;
133*3d8817e4Smiod int pe_dll_warn_dup_exports = 0;
134*3d8817e4Smiod int pe_dll_compat_implib = 0;
135*3d8817e4Smiod int pe_dll_extra_pe_debug = 0;
136*3d8817e4Smiod
137*3d8817e4Smiod /* Static variables and types. */
138*3d8817e4Smiod
139*3d8817e4Smiod static bfd_vma image_base;
140*3d8817e4Smiod static bfd *filler_bfd;
141*3d8817e4Smiod static struct bfd_section *edata_s, *reloc_s;
142*3d8817e4Smiod static unsigned char *edata_d, *reloc_d;
143*3d8817e4Smiod static size_t edata_sz, reloc_sz;
144*3d8817e4Smiod static int runtime_pseudo_relocs_created = 0;
145*3d8817e4Smiod
146*3d8817e4Smiod typedef struct
147*3d8817e4Smiod {
148*3d8817e4Smiod char *target_name;
149*3d8817e4Smiod char *object_target;
150*3d8817e4Smiod unsigned int imagebase_reloc;
151*3d8817e4Smiod int pe_arch;
152*3d8817e4Smiod int bfd_arch;
153*3d8817e4Smiod int underscored;
154*3d8817e4Smiod }
155*3d8817e4Smiod pe_details_type;
156*3d8817e4Smiod
157*3d8817e4Smiod typedef struct
158*3d8817e4Smiod {
159*3d8817e4Smiod char *name;
160*3d8817e4Smiod int len;
161*3d8817e4Smiod }
162*3d8817e4Smiod autofilter_entry_type;
163*3d8817e4Smiod
164*3d8817e4Smiod #define PE_ARCH_i386 1
165*3d8817e4Smiod #define PE_ARCH_sh 2
166*3d8817e4Smiod #define PE_ARCH_mips 3
167*3d8817e4Smiod #define PE_ARCH_arm 4
168*3d8817e4Smiod #define PE_ARCH_arm_epoc 5
169*3d8817e4Smiod
170*3d8817e4Smiod static pe_details_type pe_detail_list[] =
171*3d8817e4Smiod {
172*3d8817e4Smiod {
173*3d8817e4Smiod "pei-i386",
174*3d8817e4Smiod "pe-i386",
175*3d8817e4Smiod 7 /* R_IMAGEBASE */,
176*3d8817e4Smiod PE_ARCH_i386,
177*3d8817e4Smiod bfd_arch_i386,
178*3d8817e4Smiod 1
179*3d8817e4Smiod },
180*3d8817e4Smiod {
181*3d8817e4Smiod "pei-shl",
182*3d8817e4Smiod "pe-shl",
183*3d8817e4Smiod 16 /* R_SH_IMAGEBASE */,
184*3d8817e4Smiod PE_ARCH_sh,
185*3d8817e4Smiod bfd_arch_sh,
186*3d8817e4Smiod 1
187*3d8817e4Smiod },
188*3d8817e4Smiod {
189*3d8817e4Smiod "pei-mips",
190*3d8817e4Smiod "pe-mips",
191*3d8817e4Smiod 34 /* MIPS_R_RVA */,
192*3d8817e4Smiod PE_ARCH_mips,
193*3d8817e4Smiod bfd_arch_mips,
194*3d8817e4Smiod 0
195*3d8817e4Smiod },
196*3d8817e4Smiod {
197*3d8817e4Smiod "pei-arm-little",
198*3d8817e4Smiod "pe-arm-little",
199*3d8817e4Smiod 11 /* ARM_RVA32 */,
200*3d8817e4Smiod PE_ARCH_arm,
201*3d8817e4Smiod bfd_arch_arm,
202*3d8817e4Smiod 1
203*3d8817e4Smiod },
204*3d8817e4Smiod {
205*3d8817e4Smiod "epoc-pei-arm-little",
206*3d8817e4Smiod "epoc-pe-arm-little",
207*3d8817e4Smiod 11 /* ARM_RVA32 */,
208*3d8817e4Smiod PE_ARCH_arm_epoc,
209*3d8817e4Smiod bfd_arch_arm,
210*3d8817e4Smiod 0
211*3d8817e4Smiod },
212*3d8817e4Smiod { NULL, NULL, 0, 0, 0, 0 }
213*3d8817e4Smiod };
214*3d8817e4Smiod
215*3d8817e4Smiod static pe_details_type *pe_details;
216*3d8817e4Smiod
217*3d8817e4Smiod static autofilter_entry_type autofilter_symbollist[] =
218*3d8817e4Smiod {
219*3d8817e4Smiod { "DllMain@12", 10 },
220*3d8817e4Smiod { "DllEntryPoint@0", 15 },
221*3d8817e4Smiod { "DllMainCRTStartup@12", 20 },
222*3d8817e4Smiod { "_cygwin_dll_entry@12", 20 },
223*3d8817e4Smiod { "_cygwin_crt0_common@8", 21 },
224*3d8817e4Smiod { "_cygwin_noncygwin_dll_entry@12", 30 },
225*3d8817e4Smiod { "impure_ptr", 10 },
226*3d8817e4Smiod { "_pei386_runtime_relocator", 25 },
227*3d8817e4Smiod { "do_pseudo_reloc", 15 },
228*3d8817e4Smiod { "cygwin_crt0", 11 },
229*3d8817e4Smiod { NULL, 0 }
230*3d8817e4Smiod };
231*3d8817e4Smiod
232*3d8817e4Smiod /* Do not specify library suffix explicitly, to allow for dllized versions. */
233*3d8817e4Smiod static autofilter_entry_type autofilter_liblist[] =
234*3d8817e4Smiod {
235*3d8817e4Smiod { "libcygwin", 9 },
236*3d8817e4Smiod { "libgcc", 6 },
237*3d8817e4Smiod { "libstdc++", 9 },
238*3d8817e4Smiod { "libmingw32", 10 },
239*3d8817e4Smiod { "libmingwex", 10 },
240*3d8817e4Smiod { "libg2c", 6 },
241*3d8817e4Smiod { "libsupc++", 9 },
242*3d8817e4Smiod { "libobjc", 7 },
243*3d8817e4Smiod { "libgcj", 6 },
244*3d8817e4Smiod { NULL, 0 }
245*3d8817e4Smiod };
246*3d8817e4Smiod
247*3d8817e4Smiod static autofilter_entry_type autofilter_objlist[] =
248*3d8817e4Smiod {
249*3d8817e4Smiod { "crt0.o", 6 },
250*3d8817e4Smiod { "crt1.o", 6 },
251*3d8817e4Smiod { "crt2.o", 6 },
252*3d8817e4Smiod { "dllcrt1.o", 9 },
253*3d8817e4Smiod { "dllcrt2.o", 9 },
254*3d8817e4Smiod { "gcrt0.o", 7 },
255*3d8817e4Smiod { "gcrt1.o", 7 },
256*3d8817e4Smiod { "gcrt2.o", 7 },
257*3d8817e4Smiod { "crtbegin.o", 10 },
258*3d8817e4Smiod { "crtend.o", 8 },
259*3d8817e4Smiod { NULL, 0 }
260*3d8817e4Smiod };
261*3d8817e4Smiod
262*3d8817e4Smiod static autofilter_entry_type autofilter_symbolprefixlist[] =
263*3d8817e4Smiod {
264*3d8817e4Smiod /* { "__imp_", 6 }, */
265*3d8817e4Smiod /* Do __imp_ explicitly to save time. */
266*3d8817e4Smiod { "__rtti_", 7 },
267*3d8817e4Smiod /* Don't re-export auto-imported symbols. */
268*3d8817e4Smiod { "_nm_", 4 },
269*3d8817e4Smiod { "__builtin_", 10 },
270*3d8817e4Smiod /* Don't export symbols specifying internal DLL layout. */
271*3d8817e4Smiod { "_head_", 6 },
272*3d8817e4Smiod { "_fmode", 6 },
273*3d8817e4Smiod { "_impure_ptr", 11 },
274*3d8817e4Smiod { "cygwin_attach_dll", 17 },
275*3d8817e4Smiod { "cygwin_premain0", 15 },
276*3d8817e4Smiod { "cygwin_premain1", 15 },
277*3d8817e4Smiod { "cygwin_premain2", 15 },
278*3d8817e4Smiod { "cygwin_premain3", 15 },
279*3d8817e4Smiod { "environ", 7 },
280*3d8817e4Smiod { NULL, 0 }
281*3d8817e4Smiod };
282*3d8817e4Smiod
283*3d8817e4Smiod static autofilter_entry_type autofilter_symbolsuffixlist[] =
284*3d8817e4Smiod {
285*3d8817e4Smiod { "_iname", 6 },
286*3d8817e4Smiod { NULL, 0 }
287*3d8817e4Smiod };
288*3d8817e4Smiod
289*3d8817e4Smiod #define U(str) (pe_details->underscored ? "_" str : str)
290*3d8817e4Smiod
291*3d8817e4Smiod void
pe_dll_id_target(const char * target)292*3d8817e4Smiod pe_dll_id_target (const char *target)
293*3d8817e4Smiod {
294*3d8817e4Smiod int i;
295*3d8817e4Smiod
296*3d8817e4Smiod for (i = 0; pe_detail_list[i].target_name; i++)
297*3d8817e4Smiod if (strcmp (pe_detail_list[i].target_name, target) == 0
298*3d8817e4Smiod || strcmp (pe_detail_list[i].object_target, target) == 0)
299*3d8817e4Smiod {
300*3d8817e4Smiod pe_details = pe_detail_list + i;
301*3d8817e4Smiod return;
302*3d8817e4Smiod }
303*3d8817e4Smiod einfo (_("%XUnsupported PEI architecture: %s\n"), target);
304*3d8817e4Smiod exit (1);
305*3d8817e4Smiod }
306*3d8817e4Smiod
307*3d8817e4Smiod /* Helper functions for qsort. Relocs must be sorted so that we can write
308*3d8817e4Smiod them out by pages. */
309*3d8817e4Smiod
310*3d8817e4Smiod typedef struct
311*3d8817e4Smiod {
312*3d8817e4Smiod bfd_vma vma;
313*3d8817e4Smiod char type;
314*3d8817e4Smiod short extra;
315*3d8817e4Smiod }
316*3d8817e4Smiod reloc_data_type;
317*3d8817e4Smiod
318*3d8817e4Smiod static int
reloc_sort(const void * va,const void * vb)319*3d8817e4Smiod reloc_sort (const void *va, const void *vb)
320*3d8817e4Smiod {
321*3d8817e4Smiod bfd_vma a = ((const reloc_data_type *) va)->vma;
322*3d8817e4Smiod bfd_vma b = ((const reloc_data_type *) vb)->vma;
323*3d8817e4Smiod
324*3d8817e4Smiod return (a > b) ? 1 : ((a < b) ? -1 : 0);
325*3d8817e4Smiod }
326*3d8817e4Smiod
327*3d8817e4Smiod static int
pe_export_sort(const void * va,const void * vb)328*3d8817e4Smiod pe_export_sort (const void *va, const void *vb)
329*3d8817e4Smiod {
330*3d8817e4Smiod const def_file_export *a = va;
331*3d8817e4Smiod const def_file_export *b = vb;
332*3d8817e4Smiod
333*3d8817e4Smiod return strcmp (a->name, b->name);
334*3d8817e4Smiod }
335*3d8817e4Smiod
336*3d8817e4Smiod /* Read and process the .DEF file. */
337*3d8817e4Smiod
338*3d8817e4Smiod /* These correspond to the entries in pe_def_file->exports[]. I use
339*3d8817e4Smiod exported_symbol_sections[i] to tag whether or not the symbol was
340*3d8817e4Smiod defined, since we can't export symbols we don't have. */
341*3d8817e4Smiod
342*3d8817e4Smiod static bfd_vma *exported_symbol_offsets;
343*3d8817e4Smiod static struct bfd_section **exported_symbol_sections;
344*3d8817e4Smiod static int export_table_size;
345*3d8817e4Smiod static int count_exported;
346*3d8817e4Smiod static int count_exported_byname;
347*3d8817e4Smiod static int count_with_ordinals;
348*3d8817e4Smiod static const char *dll_name;
349*3d8817e4Smiod static int min_ordinal, max_ordinal;
350*3d8817e4Smiod static int *exported_symbols;
351*3d8817e4Smiod
352*3d8817e4Smiod typedef struct exclude_list_struct
353*3d8817e4Smiod {
354*3d8817e4Smiod char *string;
355*3d8817e4Smiod struct exclude_list_struct *next;
356*3d8817e4Smiod int type;
357*3d8817e4Smiod }
358*3d8817e4Smiod exclude_list_struct;
359*3d8817e4Smiod
360*3d8817e4Smiod static struct exclude_list_struct *excludes = 0;
361*3d8817e4Smiod
362*3d8817e4Smiod void
pe_dll_add_excludes(const char * new_excludes,const int type)363*3d8817e4Smiod pe_dll_add_excludes (const char *new_excludes, const int type)
364*3d8817e4Smiod {
365*3d8817e4Smiod char *local_copy;
366*3d8817e4Smiod char *exclude_string;
367*3d8817e4Smiod
368*3d8817e4Smiod local_copy = xstrdup (new_excludes);
369*3d8817e4Smiod
370*3d8817e4Smiod exclude_string = strtok (local_copy, ",:");
371*3d8817e4Smiod for (; exclude_string; exclude_string = strtok (NULL, ",:"))
372*3d8817e4Smiod {
373*3d8817e4Smiod struct exclude_list_struct *new_exclude;
374*3d8817e4Smiod
375*3d8817e4Smiod new_exclude = xmalloc (sizeof (struct exclude_list_struct));
376*3d8817e4Smiod new_exclude->string = xmalloc (strlen (exclude_string) + 1);
377*3d8817e4Smiod strcpy (new_exclude->string, exclude_string);
378*3d8817e4Smiod new_exclude->type = type;
379*3d8817e4Smiod new_exclude->next = excludes;
380*3d8817e4Smiod excludes = new_exclude;
381*3d8817e4Smiod }
382*3d8817e4Smiod
383*3d8817e4Smiod free (local_copy);
384*3d8817e4Smiod }
385*3d8817e4Smiod
386*3d8817e4Smiod
387*3d8817e4Smiod /* abfd is a bfd containing n (or NULL)
388*3d8817e4Smiod It can be used for contextual checks. */
389*3d8817e4Smiod
390*3d8817e4Smiod static int
auto_export(bfd * abfd,def_file * d,const char * n)391*3d8817e4Smiod auto_export (bfd *abfd, def_file *d, const char *n)
392*3d8817e4Smiod {
393*3d8817e4Smiod int i;
394*3d8817e4Smiod struct exclude_list_struct *ex;
395*3d8817e4Smiod autofilter_entry_type *afptr;
396*3d8817e4Smiod const char * libname = 0;
397*3d8817e4Smiod if (abfd && abfd->my_archive)
398*3d8817e4Smiod libname = lbasename (abfd->my_archive->filename);
399*3d8817e4Smiod
400*3d8817e4Smiod /* We should not re-export imported stuff. */
401*3d8817e4Smiod if (strncmp (n, "_imp_", 5) == 0)
402*3d8817e4Smiod return 0;
403*3d8817e4Smiod
404*3d8817e4Smiod for (i = 0; i < d->num_exports; i++)
405*3d8817e4Smiod if (strcmp (d->exports[i].name, n) == 0)
406*3d8817e4Smiod return 0;
407*3d8817e4Smiod
408*3d8817e4Smiod if (pe_dll_do_default_excludes)
409*3d8817e4Smiod {
410*3d8817e4Smiod const char * p;
411*3d8817e4Smiod int len;
412*3d8817e4Smiod
413*3d8817e4Smiod if (pe_dll_extra_pe_debug)
414*3d8817e4Smiod printf ("considering exporting: %s, abfd=%p, abfd->my_arc=%p\n",
415*3d8817e4Smiod n, abfd, abfd->my_archive);
416*3d8817e4Smiod
417*3d8817e4Smiod /* First of all, make context checks:
418*3d8817e4Smiod Don't export anything from standard libs. */
419*3d8817e4Smiod if (libname)
420*3d8817e4Smiod {
421*3d8817e4Smiod afptr = autofilter_liblist;
422*3d8817e4Smiod
423*3d8817e4Smiod while (afptr->name)
424*3d8817e4Smiod {
425*3d8817e4Smiod if (strncmp (libname, afptr->name, afptr->len) == 0 )
426*3d8817e4Smiod return 0;
427*3d8817e4Smiod afptr++;
428*3d8817e4Smiod }
429*3d8817e4Smiod }
430*3d8817e4Smiod
431*3d8817e4Smiod /* Next, exclude symbols from certain startup objects. */
432*3d8817e4Smiod
433*3d8817e4Smiod if (abfd && (p = lbasename (abfd->filename)))
434*3d8817e4Smiod {
435*3d8817e4Smiod afptr = autofilter_objlist;
436*3d8817e4Smiod while (afptr->name)
437*3d8817e4Smiod {
438*3d8817e4Smiod if (strcmp (p, afptr->name) == 0)
439*3d8817e4Smiod return 0;
440*3d8817e4Smiod afptr++;
441*3d8817e4Smiod }
442*3d8817e4Smiod }
443*3d8817e4Smiod
444*3d8817e4Smiod /* Don't try to blindly exclude all symbols
445*3d8817e4Smiod that begin with '__'; this was tried and
446*3d8817e4Smiod it is too restrictive. */
447*3d8817e4Smiod
448*3d8817e4Smiod /* Then, exclude specific symbols. */
449*3d8817e4Smiod afptr = autofilter_symbollist;
450*3d8817e4Smiod while (afptr->name)
451*3d8817e4Smiod {
452*3d8817e4Smiod if (strcmp (n, afptr->name) == 0)
453*3d8817e4Smiod return 0;
454*3d8817e4Smiod
455*3d8817e4Smiod afptr++;
456*3d8817e4Smiod }
457*3d8817e4Smiod
458*3d8817e4Smiod /* Next, exclude symbols starting with ... */
459*3d8817e4Smiod afptr = autofilter_symbolprefixlist;
460*3d8817e4Smiod while (afptr->name)
461*3d8817e4Smiod {
462*3d8817e4Smiod if (strncmp (n, afptr->name, afptr->len) == 0)
463*3d8817e4Smiod return 0;
464*3d8817e4Smiod
465*3d8817e4Smiod afptr++;
466*3d8817e4Smiod }
467*3d8817e4Smiod
468*3d8817e4Smiod /* Finally, exclude symbols ending with ... */
469*3d8817e4Smiod len = strlen (n);
470*3d8817e4Smiod afptr = autofilter_symbolsuffixlist;
471*3d8817e4Smiod while (afptr->name)
472*3d8817e4Smiod {
473*3d8817e4Smiod if ((len >= afptr->len)
474*3d8817e4Smiod /* Add 1 to insure match with trailing '\0'. */
475*3d8817e4Smiod && strncmp (n + len - afptr->len, afptr->name,
476*3d8817e4Smiod afptr->len + 1) == 0)
477*3d8817e4Smiod return 0;
478*3d8817e4Smiod
479*3d8817e4Smiod afptr++;
480*3d8817e4Smiod }
481*3d8817e4Smiod }
482*3d8817e4Smiod
483*3d8817e4Smiod for (ex = excludes; ex; ex = ex->next)
484*3d8817e4Smiod {
485*3d8817e4Smiod if (ex->type == 1) /* exclude-libs */
486*3d8817e4Smiod {
487*3d8817e4Smiod if (libname
488*3d8817e4Smiod && ((strcmp (libname, ex->string) == 0)
489*3d8817e4Smiod || (strcasecmp ("ALL", ex->string) == 0)))
490*3d8817e4Smiod return 0;
491*3d8817e4Smiod }
492*3d8817e4Smiod else if (strcmp (n, ex->string) == 0)
493*3d8817e4Smiod return 0;
494*3d8817e4Smiod }
495*3d8817e4Smiod
496*3d8817e4Smiod return 1;
497*3d8817e4Smiod }
498*3d8817e4Smiod
499*3d8817e4Smiod static void
process_def_file(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)500*3d8817e4Smiod process_def_file (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
501*3d8817e4Smiod {
502*3d8817e4Smiod int i, j;
503*3d8817e4Smiod struct bfd_link_hash_entry *blhe;
504*3d8817e4Smiod bfd *b;
505*3d8817e4Smiod struct bfd_section *s;
506*3d8817e4Smiod def_file_export *e = 0;
507*3d8817e4Smiod
508*3d8817e4Smiod if (!pe_def_file)
509*3d8817e4Smiod pe_def_file = def_file_empty ();
510*3d8817e4Smiod
511*3d8817e4Smiod /* First, run around to all the objects looking for the .drectve
512*3d8817e4Smiod sections, and push those into the def file too. */
513*3d8817e4Smiod for (b = info->input_bfds; b; b = b->link_next)
514*3d8817e4Smiod {
515*3d8817e4Smiod s = bfd_get_section_by_name (b, ".drectve");
516*3d8817e4Smiod if (s)
517*3d8817e4Smiod {
518*3d8817e4Smiod long size = s->size;
519*3d8817e4Smiod char *buf = xmalloc (size);
520*3d8817e4Smiod
521*3d8817e4Smiod bfd_get_section_contents (b, s, buf, 0, size);
522*3d8817e4Smiod def_file_add_directive (pe_def_file, buf, size);
523*3d8817e4Smiod free (buf);
524*3d8817e4Smiod }
525*3d8817e4Smiod }
526*3d8817e4Smiod
527*3d8817e4Smiod /* If we are not building a DLL, when there are no exports
528*3d8817e4Smiod we do not build an export table at all. */
529*3d8817e4Smiod if (!pe_dll_export_everything && pe_def_file->num_exports == 0
530*3d8817e4Smiod && info->executable)
531*3d8817e4Smiod return;
532*3d8817e4Smiod
533*3d8817e4Smiod /* Now, maybe export everything else the default way. */
534*3d8817e4Smiod if (pe_dll_export_everything || pe_def_file->num_exports == 0)
535*3d8817e4Smiod {
536*3d8817e4Smiod for (b = info->input_bfds; b; b = b->link_next)
537*3d8817e4Smiod {
538*3d8817e4Smiod asymbol **symbols;
539*3d8817e4Smiod int nsyms, symsize;
540*3d8817e4Smiod
541*3d8817e4Smiod symsize = bfd_get_symtab_upper_bound (b);
542*3d8817e4Smiod symbols = xmalloc (symsize);
543*3d8817e4Smiod nsyms = bfd_canonicalize_symtab (b, symbols);
544*3d8817e4Smiod
545*3d8817e4Smiod for (j = 0; j < nsyms; j++)
546*3d8817e4Smiod {
547*3d8817e4Smiod /* We should export symbols which are either global or not
548*3d8817e4Smiod anything at all. (.bss data is the latter)
549*3d8817e4Smiod We should not export undefined symbols. */
550*3d8817e4Smiod if (symbols[j]->section != &bfd_und_section
551*3d8817e4Smiod && ((symbols[j]->flags & BSF_GLOBAL)
552*3d8817e4Smiod || (symbols[j]->flags == BFD_FORT_COMM_DEFAULT_VALUE)))
553*3d8817e4Smiod {
554*3d8817e4Smiod const char *sn = symbols[j]->name;
555*3d8817e4Smiod
556*3d8817e4Smiod /* We should not re-export imported stuff. */
557*3d8817e4Smiod {
558*3d8817e4Smiod char *name = xmalloc (strlen (sn) + 2 + 6);
559*3d8817e4Smiod sprintf (name, "%s%s", U("_imp_"), sn);
560*3d8817e4Smiod
561*3d8817e4Smiod blhe = bfd_link_hash_lookup (info->hash, name,
562*3d8817e4Smiod FALSE, FALSE, FALSE);
563*3d8817e4Smiod free (name);
564*3d8817e4Smiod
565*3d8817e4Smiod if (blhe && blhe->type == bfd_link_hash_defined)
566*3d8817e4Smiod continue;
567*3d8817e4Smiod }
568*3d8817e4Smiod
569*3d8817e4Smiod if (*sn == '_')
570*3d8817e4Smiod sn++;
571*3d8817e4Smiod
572*3d8817e4Smiod if (auto_export (b, pe_def_file, sn))
573*3d8817e4Smiod {
574*3d8817e4Smiod def_file_export *p;
575*3d8817e4Smiod p=def_file_add_export (pe_def_file, sn, 0, -1);
576*3d8817e4Smiod /* Fill data flag properly, from dlltool.c. */
577*3d8817e4Smiod p->flag_data = !(symbols[j]->flags & BSF_FUNCTION);
578*3d8817e4Smiod }
579*3d8817e4Smiod }
580*3d8817e4Smiod }
581*3d8817e4Smiod }
582*3d8817e4Smiod }
583*3d8817e4Smiod
584*3d8817e4Smiod #undef NE
585*3d8817e4Smiod #define NE pe_def_file->num_exports
586*3d8817e4Smiod
587*3d8817e4Smiod /* Canonicalize the export list. */
588*3d8817e4Smiod if (pe_dll_kill_ats)
589*3d8817e4Smiod {
590*3d8817e4Smiod for (i = 0; i < NE; i++)
591*3d8817e4Smiod {
592*3d8817e4Smiod if (strchr (pe_def_file->exports[i].name, '@'))
593*3d8817e4Smiod {
594*3d8817e4Smiod /* This will preserve internal_name, which may have been
595*3d8817e4Smiod pointing to the same memory as name, or might not
596*3d8817e4Smiod have. */
597*3d8817e4Smiod int lead_at = (*pe_def_file->exports[i].name == '@');
598*3d8817e4Smiod char *tmp = xstrdup (pe_def_file->exports[i].name + lead_at);
599*3d8817e4Smiod char *tmp_at = strchr (tmp, '@');
600*3d8817e4Smiod
601*3d8817e4Smiod if (tmp_at)
602*3d8817e4Smiod *tmp_at = 0;
603*3d8817e4Smiod else
604*3d8817e4Smiod einfo (_("%XCannot export %s: invalid export name\n"),
605*3d8817e4Smiod pe_def_file->exports[i].name);
606*3d8817e4Smiod pe_def_file->exports[i].name = tmp;
607*3d8817e4Smiod }
608*3d8817e4Smiod }
609*3d8817e4Smiod }
610*3d8817e4Smiod
611*3d8817e4Smiod if (pe_dll_stdcall_aliases)
612*3d8817e4Smiod {
613*3d8817e4Smiod for (i = 0; i < NE; i++)
614*3d8817e4Smiod {
615*3d8817e4Smiod if (strchr (pe_def_file->exports[i].name, '@'))
616*3d8817e4Smiod {
617*3d8817e4Smiod int lead_at = (*pe_def_file->exports[i].name == '@');
618*3d8817e4Smiod char *tmp = xstrdup (pe_def_file->exports[i].name + lead_at);
619*3d8817e4Smiod
620*3d8817e4Smiod *(strchr (tmp, '@')) = 0;
621*3d8817e4Smiod if (auto_export (NULL, pe_def_file, tmp))
622*3d8817e4Smiod def_file_add_export (pe_def_file, tmp,
623*3d8817e4Smiod pe_def_file->exports[i].internal_name,
624*3d8817e4Smiod -1);
625*3d8817e4Smiod else
626*3d8817e4Smiod free (tmp);
627*3d8817e4Smiod }
628*3d8817e4Smiod }
629*3d8817e4Smiod }
630*3d8817e4Smiod
631*3d8817e4Smiod /* Convenience, but watch out for it changing. */
632*3d8817e4Smiod e = pe_def_file->exports;
633*3d8817e4Smiod
634*3d8817e4Smiod exported_symbol_offsets = xmalloc (NE * sizeof (bfd_vma));
635*3d8817e4Smiod exported_symbol_sections = xmalloc (NE * sizeof (struct bfd_section *));
636*3d8817e4Smiod
637*3d8817e4Smiod memset (exported_symbol_sections, 0, NE * sizeof (struct bfd_section *));
638*3d8817e4Smiod max_ordinal = 0;
639*3d8817e4Smiod min_ordinal = 65536;
640*3d8817e4Smiod count_exported = 0;
641*3d8817e4Smiod count_exported_byname = 0;
642*3d8817e4Smiod count_with_ordinals = 0;
643*3d8817e4Smiod
644*3d8817e4Smiod qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]),
645*3d8817e4Smiod pe_export_sort);
646*3d8817e4Smiod for (i = 0, j = 0; i < NE; i++)
647*3d8817e4Smiod {
648*3d8817e4Smiod if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0)
649*3d8817e4Smiod {
650*3d8817e4Smiod /* This is a duplicate. */
651*3d8817e4Smiod if (e[j - 1].ordinal != -1
652*3d8817e4Smiod && e[i].ordinal != -1
653*3d8817e4Smiod && e[j - 1].ordinal != e[i].ordinal)
654*3d8817e4Smiod {
655*3d8817e4Smiod if (pe_dll_warn_dup_exports)
656*3d8817e4Smiod /* xgettext:c-format */
657*3d8817e4Smiod einfo (_("%XError, duplicate EXPORT with ordinals: %s (%d vs %d)\n"),
658*3d8817e4Smiod e[j - 1].name, e[j - 1].ordinal, e[i].ordinal);
659*3d8817e4Smiod }
660*3d8817e4Smiod else
661*3d8817e4Smiod {
662*3d8817e4Smiod if (pe_dll_warn_dup_exports)
663*3d8817e4Smiod /* xgettext:c-format */
664*3d8817e4Smiod einfo (_("Warning, duplicate EXPORT: %s\n"),
665*3d8817e4Smiod e[j - 1].name);
666*3d8817e4Smiod }
667*3d8817e4Smiod
668*3d8817e4Smiod if (e[i].ordinal != -1)
669*3d8817e4Smiod e[j - 1].ordinal = e[i].ordinal;
670*3d8817e4Smiod e[j - 1].flag_private |= e[i].flag_private;
671*3d8817e4Smiod e[j - 1].flag_constant |= e[i].flag_constant;
672*3d8817e4Smiod e[j - 1].flag_noname |= e[i].flag_noname;
673*3d8817e4Smiod e[j - 1].flag_data |= e[i].flag_data;
674*3d8817e4Smiod }
675*3d8817e4Smiod else
676*3d8817e4Smiod {
677*3d8817e4Smiod if (i != j)
678*3d8817e4Smiod e[j] = e[i];
679*3d8817e4Smiod j++;
680*3d8817e4Smiod }
681*3d8817e4Smiod }
682*3d8817e4Smiod pe_def_file->num_exports = j; /* == NE */
683*3d8817e4Smiod
684*3d8817e4Smiod for (i = 0; i < NE; i++)
685*3d8817e4Smiod {
686*3d8817e4Smiod char *name;
687*3d8817e4Smiod
688*3d8817e4Smiod /* Check for forward exports */
689*3d8817e4Smiod if (strchr (pe_def_file->exports[i].internal_name, '.'))
690*3d8817e4Smiod {
691*3d8817e4Smiod count_exported++;
692*3d8817e4Smiod if (!pe_def_file->exports[i].flag_noname)
693*3d8817e4Smiod count_exported_byname++;
694*3d8817e4Smiod
695*3d8817e4Smiod pe_def_file->exports[i].flag_forward = 1;
696*3d8817e4Smiod
697*3d8817e4Smiod if (pe_def_file->exports[i].ordinal != -1)
698*3d8817e4Smiod {
699*3d8817e4Smiod if (max_ordinal < pe_def_file->exports[i].ordinal)
700*3d8817e4Smiod max_ordinal = pe_def_file->exports[i].ordinal;
701*3d8817e4Smiod if (min_ordinal > pe_def_file->exports[i].ordinal)
702*3d8817e4Smiod min_ordinal = pe_def_file->exports[i].ordinal;
703*3d8817e4Smiod count_with_ordinals++;
704*3d8817e4Smiod }
705*3d8817e4Smiod
706*3d8817e4Smiod continue;
707*3d8817e4Smiod }
708*3d8817e4Smiod
709*3d8817e4Smiod name = xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
710*3d8817e4Smiod if (pe_details->underscored
711*3d8817e4Smiod && (*pe_def_file->exports[i].internal_name != '@'))
712*3d8817e4Smiod {
713*3d8817e4Smiod *name = '_';
714*3d8817e4Smiod strcpy (name + 1, pe_def_file->exports[i].internal_name);
715*3d8817e4Smiod }
716*3d8817e4Smiod else
717*3d8817e4Smiod strcpy (name, pe_def_file->exports[i].internal_name);
718*3d8817e4Smiod
719*3d8817e4Smiod blhe = bfd_link_hash_lookup (info->hash,
720*3d8817e4Smiod name,
721*3d8817e4Smiod FALSE, FALSE, TRUE);
722*3d8817e4Smiod
723*3d8817e4Smiod if (blhe
724*3d8817e4Smiod && (blhe->type == bfd_link_hash_defined
725*3d8817e4Smiod || (blhe->type == bfd_link_hash_common)))
726*3d8817e4Smiod {
727*3d8817e4Smiod count_exported++;
728*3d8817e4Smiod if (!pe_def_file->exports[i].flag_noname)
729*3d8817e4Smiod count_exported_byname++;
730*3d8817e4Smiod
731*3d8817e4Smiod /* Only fill in the sections. The actual offsets are computed
732*3d8817e4Smiod in fill_exported_offsets() after common symbols are laid
733*3d8817e4Smiod out. */
734*3d8817e4Smiod if (blhe->type == bfd_link_hash_defined)
735*3d8817e4Smiod exported_symbol_sections[i] = blhe->u.def.section;
736*3d8817e4Smiod else
737*3d8817e4Smiod exported_symbol_sections[i] = blhe->u.c.p->section;
738*3d8817e4Smiod
739*3d8817e4Smiod if (pe_def_file->exports[i].ordinal != -1)
740*3d8817e4Smiod {
741*3d8817e4Smiod if (max_ordinal < pe_def_file->exports[i].ordinal)
742*3d8817e4Smiod max_ordinal = pe_def_file->exports[i].ordinal;
743*3d8817e4Smiod if (min_ordinal > pe_def_file->exports[i].ordinal)
744*3d8817e4Smiod min_ordinal = pe_def_file->exports[i].ordinal;
745*3d8817e4Smiod count_with_ordinals++;
746*3d8817e4Smiod }
747*3d8817e4Smiod }
748*3d8817e4Smiod else if (blhe && blhe->type == bfd_link_hash_undefined)
749*3d8817e4Smiod {
750*3d8817e4Smiod /* xgettext:c-format */
751*3d8817e4Smiod einfo (_("%XCannot export %s: symbol not defined\n"),
752*3d8817e4Smiod pe_def_file->exports[i].internal_name);
753*3d8817e4Smiod }
754*3d8817e4Smiod else if (blhe)
755*3d8817e4Smiod {
756*3d8817e4Smiod /* xgettext:c-format */
757*3d8817e4Smiod einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
758*3d8817e4Smiod pe_def_file->exports[i].internal_name,
759*3d8817e4Smiod blhe->type, bfd_link_hash_defined);
760*3d8817e4Smiod }
761*3d8817e4Smiod else
762*3d8817e4Smiod {
763*3d8817e4Smiod /* xgettext:c-format */
764*3d8817e4Smiod einfo (_("%XCannot export %s: symbol not found\n"),
765*3d8817e4Smiod pe_def_file->exports[i].internal_name);
766*3d8817e4Smiod }
767*3d8817e4Smiod free (name);
768*3d8817e4Smiod }
769*3d8817e4Smiod }
770*3d8817e4Smiod
771*3d8817e4Smiod /* Build the bfd that will contain .edata and .reloc sections. */
772*3d8817e4Smiod
773*3d8817e4Smiod static void
build_filler_bfd(int include_edata)774*3d8817e4Smiod build_filler_bfd (int include_edata)
775*3d8817e4Smiod {
776*3d8817e4Smiod lang_input_statement_type *filler_file;
777*3d8817e4Smiod filler_file = lang_add_input_file ("dll stuff",
778*3d8817e4Smiod lang_input_file_is_fake_enum,
779*3d8817e4Smiod NULL);
780*3d8817e4Smiod filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd);
781*3d8817e4Smiod if (filler_bfd == NULL
782*3d8817e4Smiod || !bfd_set_arch_mach (filler_bfd,
783*3d8817e4Smiod bfd_get_arch (output_bfd),
784*3d8817e4Smiod bfd_get_mach (output_bfd)))
785*3d8817e4Smiod {
786*3d8817e4Smiod einfo ("%X%P: can not create BFD: %E\n");
787*3d8817e4Smiod return;
788*3d8817e4Smiod }
789*3d8817e4Smiod
790*3d8817e4Smiod if (include_edata)
791*3d8817e4Smiod {
792*3d8817e4Smiod edata_s = bfd_make_section_old_way (filler_bfd, ".edata");
793*3d8817e4Smiod if (edata_s == NULL
794*3d8817e4Smiod || !bfd_set_section_flags (filler_bfd, edata_s,
795*3d8817e4Smiod (SEC_HAS_CONTENTS
796*3d8817e4Smiod | SEC_ALLOC
797*3d8817e4Smiod | SEC_LOAD
798*3d8817e4Smiod | SEC_KEEP
799*3d8817e4Smiod | SEC_IN_MEMORY)))
800*3d8817e4Smiod {
801*3d8817e4Smiod einfo ("%X%P: can not create .edata section: %E\n");
802*3d8817e4Smiod return;
803*3d8817e4Smiod }
804*3d8817e4Smiod bfd_set_section_size (filler_bfd, edata_s, edata_sz);
805*3d8817e4Smiod }
806*3d8817e4Smiod
807*3d8817e4Smiod reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc");
808*3d8817e4Smiod if (reloc_s == NULL
809*3d8817e4Smiod || !bfd_set_section_flags (filler_bfd, reloc_s,
810*3d8817e4Smiod (SEC_HAS_CONTENTS
811*3d8817e4Smiod | SEC_ALLOC
812*3d8817e4Smiod | SEC_LOAD
813*3d8817e4Smiod | SEC_KEEP
814*3d8817e4Smiod | SEC_IN_MEMORY)))
815*3d8817e4Smiod {
816*3d8817e4Smiod einfo ("%X%P: can not create .reloc section: %E\n");
817*3d8817e4Smiod return;
818*3d8817e4Smiod }
819*3d8817e4Smiod
820*3d8817e4Smiod bfd_set_section_size (filler_bfd, reloc_s, 0);
821*3d8817e4Smiod
822*3d8817e4Smiod ldlang_add_file (filler_file);
823*3d8817e4Smiod }
824*3d8817e4Smiod
825*3d8817e4Smiod /* Gather all the exported symbols and build the .edata section. */
826*3d8817e4Smiod
827*3d8817e4Smiod static void
generate_edata(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED)828*3d8817e4Smiod generate_edata (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
829*3d8817e4Smiod {
830*3d8817e4Smiod int i, next_ordinal;
831*3d8817e4Smiod int name_table_size = 0;
832*3d8817e4Smiod const char *dlnp;
833*3d8817e4Smiod
834*3d8817e4Smiod /* First, we need to know how many exported symbols there are,
835*3d8817e4Smiod and what the range of ordinals is. */
836*3d8817e4Smiod if (pe_def_file->name)
837*3d8817e4Smiod dll_name = pe_def_file->name;
838*3d8817e4Smiod else
839*3d8817e4Smiod {
840*3d8817e4Smiod dll_name = abfd->filename;
841*3d8817e4Smiod
842*3d8817e4Smiod for (dlnp = dll_name; *dlnp; dlnp++)
843*3d8817e4Smiod if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':')
844*3d8817e4Smiod dll_name = dlnp + 1;
845*3d8817e4Smiod }
846*3d8817e4Smiod
847*3d8817e4Smiod if (count_with_ordinals && max_ordinal > count_exported)
848*3d8817e4Smiod {
849*3d8817e4Smiod if (min_ordinal > max_ordinal - count_exported + 1)
850*3d8817e4Smiod min_ordinal = max_ordinal - count_exported + 1;
851*3d8817e4Smiod }
852*3d8817e4Smiod else
853*3d8817e4Smiod {
854*3d8817e4Smiod min_ordinal = 1;
855*3d8817e4Smiod max_ordinal = count_exported;
856*3d8817e4Smiod }
857*3d8817e4Smiod
858*3d8817e4Smiod export_table_size = max_ordinal - min_ordinal + 1;
859*3d8817e4Smiod exported_symbols = xmalloc (export_table_size * sizeof (int));
860*3d8817e4Smiod for (i = 0; i < export_table_size; i++)
861*3d8817e4Smiod exported_symbols[i] = -1;
862*3d8817e4Smiod
863*3d8817e4Smiod /* Now we need to assign ordinals to those that don't have them. */
864*3d8817e4Smiod for (i = 0; i < NE; i++)
865*3d8817e4Smiod {
866*3d8817e4Smiod if (exported_symbol_sections[i] ||
867*3d8817e4Smiod pe_def_file->exports[i].flag_forward)
868*3d8817e4Smiod {
869*3d8817e4Smiod if (pe_def_file->exports[i].ordinal != -1)
870*3d8817e4Smiod {
871*3d8817e4Smiod int ei = pe_def_file->exports[i].ordinal - min_ordinal;
872*3d8817e4Smiod int pi = exported_symbols[ei];
873*3d8817e4Smiod
874*3d8817e4Smiod if (pi != -1)
875*3d8817e4Smiod {
876*3d8817e4Smiod /* xgettext:c-format */
877*3d8817e4Smiod einfo (_("%XError, ordinal used twice: %d (%s vs %s)\n"),
878*3d8817e4Smiod pe_def_file->exports[i].ordinal,
879*3d8817e4Smiod pe_def_file->exports[i].name,
880*3d8817e4Smiod pe_def_file->exports[pi].name);
881*3d8817e4Smiod }
882*3d8817e4Smiod exported_symbols[ei] = i;
883*3d8817e4Smiod }
884*3d8817e4Smiod name_table_size += strlen (pe_def_file->exports[i].name) + 1;
885*3d8817e4Smiod }
886*3d8817e4Smiod
887*3d8817e4Smiod /* Reserve space for the forward name. */
888*3d8817e4Smiod if (pe_def_file->exports[i].flag_forward)
889*3d8817e4Smiod {
890*3d8817e4Smiod name_table_size += strlen (pe_def_file->exports[i].internal_name) + 1;
891*3d8817e4Smiod }
892*3d8817e4Smiod }
893*3d8817e4Smiod
894*3d8817e4Smiod next_ordinal = min_ordinal;
895*3d8817e4Smiod for (i = 0; i < NE; i++)
896*3d8817e4Smiod if ((exported_symbol_sections[i] ||
897*3d8817e4Smiod pe_def_file->exports[i].flag_forward) &&
898*3d8817e4Smiod pe_def_file->exports[i].ordinal == -1)
899*3d8817e4Smiod {
900*3d8817e4Smiod while (exported_symbols[next_ordinal - min_ordinal] != -1)
901*3d8817e4Smiod next_ordinal++;
902*3d8817e4Smiod
903*3d8817e4Smiod exported_symbols[next_ordinal - min_ordinal] = i;
904*3d8817e4Smiod pe_def_file->exports[i].ordinal = next_ordinal;
905*3d8817e4Smiod }
906*3d8817e4Smiod
907*3d8817e4Smiod /* OK, now we can allocate some memory. */
908*3d8817e4Smiod edata_sz = (40 /* directory */
909*3d8817e4Smiod + 4 * export_table_size /* addresses */
910*3d8817e4Smiod + 4 * count_exported_byname /* name ptrs */
911*3d8817e4Smiod + 2 * count_exported_byname /* ordinals */
912*3d8817e4Smiod + name_table_size + strlen (dll_name) + 1);
913*3d8817e4Smiod }
914*3d8817e4Smiod
915*3d8817e4Smiod /* Fill the exported symbol offsets. The preliminary work has already
916*3d8817e4Smiod been done in process_def_file(). */
917*3d8817e4Smiod
918*3d8817e4Smiod static void
fill_exported_offsets(bfd * abfd ATTRIBUTE_UNUSED,struct bfd_link_info * info)919*3d8817e4Smiod fill_exported_offsets (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
920*3d8817e4Smiod {
921*3d8817e4Smiod int i;
922*3d8817e4Smiod struct bfd_link_hash_entry *blhe;
923*3d8817e4Smiod
924*3d8817e4Smiod for (i = 0; i < pe_def_file->num_exports; i++)
925*3d8817e4Smiod {
926*3d8817e4Smiod char *name;
927*3d8817e4Smiod
928*3d8817e4Smiod name = xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2);
929*3d8817e4Smiod if (pe_details->underscored
930*3d8817e4Smiod && *pe_def_file->exports[i].internal_name != '@')
931*3d8817e4Smiod {
932*3d8817e4Smiod *name = '_';
933*3d8817e4Smiod strcpy (name + 1, pe_def_file->exports[i].internal_name);
934*3d8817e4Smiod }
935*3d8817e4Smiod else
936*3d8817e4Smiod strcpy (name, pe_def_file->exports[i].internal_name);
937*3d8817e4Smiod
938*3d8817e4Smiod blhe = bfd_link_hash_lookup (info->hash,
939*3d8817e4Smiod name,
940*3d8817e4Smiod FALSE, FALSE, TRUE);
941*3d8817e4Smiod
942*3d8817e4Smiod if (blhe && blhe->type == bfd_link_hash_defined)
943*3d8817e4Smiod exported_symbol_offsets[i] = blhe->u.def.value;
944*3d8817e4Smiod
945*3d8817e4Smiod free (name);
946*3d8817e4Smiod }
947*3d8817e4Smiod }
948*3d8817e4Smiod
949*3d8817e4Smiod static void
fill_edata(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED)950*3d8817e4Smiod fill_edata (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
951*3d8817e4Smiod {
952*3d8817e4Smiod int s, hint;
953*3d8817e4Smiod unsigned char *edirectory;
954*3d8817e4Smiod unsigned char *eaddresses;
955*3d8817e4Smiod unsigned char *enameptrs;
956*3d8817e4Smiod unsigned char *eordinals;
957*3d8817e4Smiod char *enamestr;
958*3d8817e4Smiod time_t now;
959*3d8817e4Smiod
960*3d8817e4Smiod time (&now);
961*3d8817e4Smiod
962*3d8817e4Smiod edata_d = xmalloc (edata_sz);
963*3d8817e4Smiod
964*3d8817e4Smiod /* Note use of array pointer math here. */
965*3d8817e4Smiod edirectory = edata_d;
966*3d8817e4Smiod eaddresses = edata_d + 40;
967*3d8817e4Smiod enameptrs = eaddresses + 4 * export_table_size;
968*3d8817e4Smiod eordinals = enameptrs + 4 * count_exported_byname;
969*3d8817e4Smiod enamestr = (char *) eordinals + 2 * count_exported_byname;
970*3d8817e4Smiod
971*3d8817e4Smiod #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) \
972*3d8817e4Smiod + edata_s->output_section->vma - image_base)
973*3d8817e4Smiod
974*3d8817e4Smiod memset (edata_d, 0, edata_sz);
975*3d8817e4Smiod bfd_put_32 (abfd, now, edata_d + 4);
976*3d8817e4Smiod if (pe_def_file->version_major != -1)
977*3d8817e4Smiod {
978*3d8817e4Smiod bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8);
979*3d8817e4Smiod bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10);
980*3d8817e4Smiod }
981*3d8817e4Smiod
982*3d8817e4Smiod bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12);
983*3d8817e4Smiod strcpy (enamestr, dll_name);
984*3d8817e4Smiod enamestr += strlen (enamestr) + 1;
985*3d8817e4Smiod bfd_put_32 (abfd, min_ordinal, edata_d + 16);
986*3d8817e4Smiod bfd_put_32 (abfd, export_table_size, edata_d + 20);
987*3d8817e4Smiod bfd_put_32 (abfd, count_exported_byname, edata_d + 24);
988*3d8817e4Smiod bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28);
989*3d8817e4Smiod bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32);
990*3d8817e4Smiod bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36);
991*3d8817e4Smiod
992*3d8817e4Smiod fill_exported_offsets (abfd, info);
993*3d8817e4Smiod
994*3d8817e4Smiod /* Ok, now for the filling in part.
995*3d8817e4Smiod Scan alphabetically - ie the ordering in the exports[] table,
996*3d8817e4Smiod rather than by ordinal - the ordering in the exported_symbol[]
997*3d8817e4Smiod table. See dlltool.c and:
998*3d8817e4Smiod http://sources.redhat.com/ml/binutils/2003-04/msg00379.html
999*3d8817e4Smiod for more information. */
1000*3d8817e4Smiod hint = 0;
1001*3d8817e4Smiod for (s = 0; s < NE; s++)
1002*3d8817e4Smiod {
1003*3d8817e4Smiod struct bfd_section *ssec = exported_symbol_sections[s];
1004*3d8817e4Smiod if (pe_def_file->exports[s].ordinal != -1 &&
1005*3d8817e4Smiod (pe_def_file->exports[s].flag_forward || ssec != NULL))
1006*3d8817e4Smiod {
1007*3d8817e4Smiod int ord = pe_def_file->exports[s].ordinal;
1008*3d8817e4Smiod
1009*3d8817e4Smiod if (pe_def_file->exports[s].flag_forward)
1010*3d8817e4Smiod {
1011*3d8817e4Smiod bfd_put_32 (abfd, ERVA (enamestr),
1012*3d8817e4Smiod eaddresses + 4 * (ord - min_ordinal));
1013*3d8817e4Smiod
1014*3d8817e4Smiod strcpy (enamestr, pe_def_file->exports[s].internal_name);
1015*3d8817e4Smiod enamestr += strlen (pe_def_file->exports[s].internal_name) + 1;
1016*3d8817e4Smiod }
1017*3d8817e4Smiod else
1018*3d8817e4Smiod {
1019*3d8817e4Smiod unsigned long srva = (exported_symbol_offsets[s]
1020*3d8817e4Smiod + ssec->output_section->vma
1021*3d8817e4Smiod + ssec->output_offset);
1022*3d8817e4Smiod
1023*3d8817e4Smiod bfd_put_32 (abfd, srva - image_base,
1024*3d8817e4Smiod eaddresses + 4 * (ord - min_ordinal));
1025*3d8817e4Smiod }
1026*3d8817e4Smiod
1027*3d8817e4Smiod if (!pe_def_file->exports[s].flag_noname)
1028*3d8817e4Smiod {
1029*3d8817e4Smiod char *ename = pe_def_file->exports[s].name;
1030*3d8817e4Smiod
1031*3d8817e4Smiod bfd_put_32 (abfd, ERVA (enamestr), enameptrs);
1032*3d8817e4Smiod enameptrs += 4;
1033*3d8817e4Smiod strcpy (enamestr, ename);
1034*3d8817e4Smiod enamestr += strlen (enamestr) + 1;
1035*3d8817e4Smiod bfd_put_16 (abfd, ord - min_ordinal, eordinals);
1036*3d8817e4Smiod eordinals += 2;
1037*3d8817e4Smiod pe_def_file->exports[s].hint = hint++;
1038*3d8817e4Smiod }
1039*3d8817e4Smiod }
1040*3d8817e4Smiod }
1041*3d8817e4Smiod }
1042*3d8817e4Smiod
1043*3d8817e4Smiod
1044*3d8817e4Smiod static struct bfd_section *current_sec;
1045*3d8817e4Smiod
1046*3d8817e4Smiod void
pe_walk_relocs_of_symbol(struct bfd_link_info * info,const char * name,int (* cb)(arelent *,asection *))1047*3d8817e4Smiod pe_walk_relocs_of_symbol (struct bfd_link_info *info,
1048*3d8817e4Smiod const char *name,
1049*3d8817e4Smiod int (*cb) (arelent *, asection *))
1050*3d8817e4Smiod {
1051*3d8817e4Smiod bfd *b;
1052*3d8817e4Smiod asection *s;
1053*3d8817e4Smiod
1054*3d8817e4Smiod for (b = info->input_bfds; b; b = b->link_next)
1055*3d8817e4Smiod {
1056*3d8817e4Smiod asymbol **symbols;
1057*3d8817e4Smiod int nsyms, symsize;
1058*3d8817e4Smiod
1059*3d8817e4Smiod symsize = bfd_get_symtab_upper_bound (b);
1060*3d8817e4Smiod symbols = xmalloc (symsize);
1061*3d8817e4Smiod nsyms = bfd_canonicalize_symtab (b, symbols);
1062*3d8817e4Smiod
1063*3d8817e4Smiod for (s = b->sections; s; s = s->next)
1064*3d8817e4Smiod {
1065*3d8817e4Smiod arelent **relocs;
1066*3d8817e4Smiod int relsize, nrelocs, i;
1067*3d8817e4Smiod int flags = bfd_get_section_flags (b, s);
1068*3d8817e4Smiod
1069*3d8817e4Smiod /* Skip discarded linkonce sections. */
1070*3d8817e4Smiod if (flags & SEC_LINK_ONCE
1071*3d8817e4Smiod && s->output_section == bfd_abs_section_ptr)
1072*3d8817e4Smiod continue;
1073*3d8817e4Smiod
1074*3d8817e4Smiod current_sec = s;
1075*3d8817e4Smiod
1076*3d8817e4Smiod relsize = bfd_get_reloc_upper_bound (b, s);
1077*3d8817e4Smiod relocs = xmalloc (relsize);
1078*3d8817e4Smiod nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
1079*3d8817e4Smiod
1080*3d8817e4Smiod for (i = 0; i < nrelocs; i++)
1081*3d8817e4Smiod {
1082*3d8817e4Smiod struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
1083*3d8817e4Smiod
1084*3d8817e4Smiod if (!strcmp (name, sym->name))
1085*3d8817e4Smiod cb (relocs[i], s);
1086*3d8817e4Smiod }
1087*3d8817e4Smiod
1088*3d8817e4Smiod free (relocs);
1089*3d8817e4Smiod
1090*3d8817e4Smiod /* Warning: the allocated symbols are remembered in BFD and reused
1091*3d8817e4Smiod later, so don't free them! */
1092*3d8817e4Smiod /* free (symbols); */
1093*3d8817e4Smiod }
1094*3d8817e4Smiod }
1095*3d8817e4Smiod }
1096*3d8817e4Smiod
1097*3d8817e4Smiod /* Gather all the relocations and build the .reloc section. */
1098*3d8817e4Smiod
1099*3d8817e4Smiod static void
generate_reloc(bfd * abfd,struct bfd_link_info * info)1100*3d8817e4Smiod generate_reloc (bfd *abfd, struct bfd_link_info *info)
1101*3d8817e4Smiod {
1102*3d8817e4Smiod
1103*3d8817e4Smiod /* For .reloc stuff. */
1104*3d8817e4Smiod reloc_data_type *reloc_data;
1105*3d8817e4Smiod int total_relocs = 0;
1106*3d8817e4Smiod int i;
1107*3d8817e4Smiod unsigned long sec_page = (unsigned long) -1;
1108*3d8817e4Smiod unsigned long page_ptr, page_count;
1109*3d8817e4Smiod int bi;
1110*3d8817e4Smiod bfd *b;
1111*3d8817e4Smiod struct bfd_section *s;
1112*3d8817e4Smiod
1113*3d8817e4Smiod total_relocs = 0;
1114*3d8817e4Smiod for (b = info->input_bfds; b; b = b->link_next)
1115*3d8817e4Smiod for (s = b->sections; s; s = s->next)
1116*3d8817e4Smiod total_relocs += s->reloc_count;
1117*3d8817e4Smiod
1118*3d8817e4Smiod reloc_data = xmalloc (total_relocs * sizeof (reloc_data_type));
1119*3d8817e4Smiod
1120*3d8817e4Smiod total_relocs = 0;
1121*3d8817e4Smiod bi = 0;
1122*3d8817e4Smiod for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next)
1123*3d8817e4Smiod {
1124*3d8817e4Smiod arelent **relocs;
1125*3d8817e4Smiod int relsize, nrelocs, i;
1126*3d8817e4Smiod
1127*3d8817e4Smiod for (s = b->sections; s; s = s->next)
1128*3d8817e4Smiod {
1129*3d8817e4Smiod unsigned long sec_vma = s->output_section->vma + s->output_offset;
1130*3d8817e4Smiod asymbol **symbols;
1131*3d8817e4Smiod int nsyms, symsize;
1132*3d8817e4Smiod
1133*3d8817e4Smiod /* If it's not loaded, we don't need to relocate it this way. */
1134*3d8817e4Smiod if (!(s->output_section->flags & SEC_LOAD))
1135*3d8817e4Smiod continue;
1136*3d8817e4Smiod
1137*3d8817e4Smiod /* I don't know why there would be a reloc for these, but I've
1138*3d8817e4Smiod seen it happen - DJ */
1139*3d8817e4Smiod if (s->output_section == &bfd_abs_section)
1140*3d8817e4Smiod continue;
1141*3d8817e4Smiod
1142*3d8817e4Smiod if (s->output_section->vma == 0)
1143*3d8817e4Smiod {
1144*3d8817e4Smiod /* Huh? Shouldn't happen, but punt if it does. */
1145*3d8817e4Smiod einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
1146*3d8817e4Smiod s->output_section->name, s->output_section->index,
1147*3d8817e4Smiod s->output_section->flags);
1148*3d8817e4Smiod continue;
1149*3d8817e4Smiod }
1150*3d8817e4Smiod
1151*3d8817e4Smiod symsize = bfd_get_symtab_upper_bound (b);
1152*3d8817e4Smiod symbols = xmalloc (symsize);
1153*3d8817e4Smiod nsyms = bfd_canonicalize_symtab (b, symbols);
1154*3d8817e4Smiod
1155*3d8817e4Smiod relsize = bfd_get_reloc_upper_bound (b, s);
1156*3d8817e4Smiod relocs = xmalloc (relsize);
1157*3d8817e4Smiod nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols);
1158*3d8817e4Smiod
1159*3d8817e4Smiod for (i = 0; i < nrelocs; i++)
1160*3d8817e4Smiod {
1161*3d8817e4Smiod if (pe_dll_extra_pe_debug)
1162*3d8817e4Smiod {
1163*3d8817e4Smiod struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
1164*3d8817e4Smiod printf ("rel: %s\n", sym->name);
1165*3d8817e4Smiod }
1166*3d8817e4Smiod if (!relocs[i]->howto->pc_relative
1167*3d8817e4Smiod && relocs[i]->howto->type != pe_details->imagebase_reloc)
1168*3d8817e4Smiod {
1169*3d8817e4Smiod bfd_vma sym_vma;
1170*3d8817e4Smiod struct bfd_symbol *sym = *relocs[i]->sym_ptr_ptr;
1171*3d8817e4Smiod
1172*3d8817e4Smiod sym_vma = (relocs[i]->addend
1173*3d8817e4Smiod + sym->value
1174*3d8817e4Smiod + sym->section->vma
1175*3d8817e4Smiod + sym->section->output_offset
1176*3d8817e4Smiod + sym->section->output_section->vma);
1177*3d8817e4Smiod reloc_data[total_relocs].vma = sec_vma + relocs[i]->address;
1178*3d8817e4Smiod
1179*3d8817e4Smiod #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
1180*3d8817e4Smiod
1181*3d8817e4Smiod switch BITS_AND_SHIFT (relocs[i]->howto->bitsize,
1182*3d8817e4Smiod relocs[i]->howto->rightshift)
1183*3d8817e4Smiod {
1184*3d8817e4Smiod case BITS_AND_SHIFT (32, 0):
1185*3d8817e4Smiod reloc_data[total_relocs].type = 3;
1186*3d8817e4Smiod total_relocs++;
1187*3d8817e4Smiod break;
1188*3d8817e4Smiod case BITS_AND_SHIFT (16, 0):
1189*3d8817e4Smiod reloc_data[total_relocs].type = 2;
1190*3d8817e4Smiod total_relocs++;
1191*3d8817e4Smiod break;
1192*3d8817e4Smiod case BITS_AND_SHIFT (16, 16):
1193*3d8817e4Smiod reloc_data[total_relocs].type = 4;
1194*3d8817e4Smiod /* FIXME: we can't know the symbol's right value
1195*3d8817e4Smiod yet, but we probably can safely assume that
1196*3d8817e4Smiod CE will relocate us in 64k blocks, so leaving
1197*3d8817e4Smiod it zero is safe. */
1198*3d8817e4Smiod reloc_data[total_relocs].extra = 0;
1199*3d8817e4Smiod total_relocs++;
1200*3d8817e4Smiod break;
1201*3d8817e4Smiod case BITS_AND_SHIFT (26, 2):
1202*3d8817e4Smiod reloc_data[total_relocs].type = 5;
1203*3d8817e4Smiod total_relocs++;
1204*3d8817e4Smiod break;
1205*3d8817e4Smiod case BITS_AND_SHIFT (24, 2):
1206*3d8817e4Smiod /* FIXME: 0 is ARM_26D, it is defined in bfd/coff-arm.c
1207*3d8817e4Smiod Those ARM_xxx definitions should go in proper
1208*3d8817e4Smiod header someday. */
1209*3d8817e4Smiod if (relocs[i]->howto->type == 0
1210*3d8817e4Smiod /* Older GNU linkers used 5 instead of 0 for this reloc. */
1211*3d8817e4Smiod || relocs[i]->howto->type == 5)
1212*3d8817e4Smiod /* This is an ARM_26D reloc, which is an ARM_26 reloc
1213*3d8817e4Smiod that has already been fully processed during a
1214*3d8817e4Smiod previous link stage, so ignore it here. */
1215*3d8817e4Smiod break;
1216*3d8817e4Smiod /* Fall through. */
1217*3d8817e4Smiod default:
1218*3d8817e4Smiod /* xgettext:c-format */
1219*3d8817e4Smiod einfo (_("%XError: %d-bit reloc in dll\n"),
1220*3d8817e4Smiod relocs[i]->howto->bitsize);
1221*3d8817e4Smiod break;
1222*3d8817e4Smiod }
1223*3d8817e4Smiod }
1224*3d8817e4Smiod }
1225*3d8817e4Smiod free (relocs);
1226*3d8817e4Smiod /* Warning: the allocated symbols are remembered in BFD and
1227*3d8817e4Smiod reused later, so don't free them! */
1228*3d8817e4Smiod }
1229*3d8817e4Smiod }
1230*3d8817e4Smiod
1231*3d8817e4Smiod /* At this point, we have total_relocs relocation addresses in
1232*3d8817e4Smiod reloc_addresses, which are all suitable for the .reloc section.
1233*3d8817e4Smiod We must now create the new sections. */
1234*3d8817e4Smiod qsort (reloc_data, total_relocs, sizeof (*reloc_data), reloc_sort);
1235*3d8817e4Smiod
1236*3d8817e4Smiod for (i = 0; i < total_relocs; i++)
1237*3d8817e4Smiod {
1238*3d8817e4Smiod unsigned long this_page = (reloc_data[i].vma >> 12);
1239*3d8817e4Smiod
1240*3d8817e4Smiod if (this_page != sec_page)
1241*3d8817e4Smiod {
1242*3d8817e4Smiod reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */
1243*3d8817e4Smiod reloc_sz += 8;
1244*3d8817e4Smiod sec_page = this_page;
1245*3d8817e4Smiod }
1246*3d8817e4Smiod
1247*3d8817e4Smiod reloc_sz += 2;
1248*3d8817e4Smiod
1249*3d8817e4Smiod if (reloc_data[i].type == 4)
1250*3d8817e4Smiod reloc_sz += 2;
1251*3d8817e4Smiod }
1252*3d8817e4Smiod
1253*3d8817e4Smiod reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */
1254*3d8817e4Smiod reloc_d = xmalloc (reloc_sz);
1255*3d8817e4Smiod sec_page = (unsigned long) -1;
1256*3d8817e4Smiod reloc_sz = 0;
1257*3d8817e4Smiod page_ptr = (unsigned long) -1;
1258*3d8817e4Smiod page_count = 0;
1259*3d8817e4Smiod
1260*3d8817e4Smiod for (i = 0; i < total_relocs; i++)
1261*3d8817e4Smiod {
1262*3d8817e4Smiod unsigned long rva = reloc_data[i].vma - image_base;
1263*3d8817e4Smiod unsigned long this_page = (rva & ~0xfff);
1264*3d8817e4Smiod
1265*3d8817e4Smiod if (this_page != sec_page)
1266*3d8817e4Smiod {
1267*3d8817e4Smiod while (reloc_sz & 3)
1268*3d8817e4Smiod reloc_d[reloc_sz++] = 0;
1269*3d8817e4Smiod
1270*3d8817e4Smiod if (page_ptr != (unsigned long) -1)
1271*3d8817e4Smiod bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
1272*3d8817e4Smiod
1273*3d8817e4Smiod bfd_put_32 (abfd, this_page, reloc_d + reloc_sz);
1274*3d8817e4Smiod page_ptr = reloc_sz;
1275*3d8817e4Smiod reloc_sz += 8;
1276*3d8817e4Smiod sec_page = this_page;
1277*3d8817e4Smiod page_count = 0;
1278*3d8817e4Smiod }
1279*3d8817e4Smiod
1280*3d8817e4Smiod bfd_put_16 (abfd, (rva & 0xfff) + (reloc_data[i].type << 12),
1281*3d8817e4Smiod reloc_d + reloc_sz);
1282*3d8817e4Smiod reloc_sz += 2;
1283*3d8817e4Smiod
1284*3d8817e4Smiod if (reloc_data[i].type == 4)
1285*3d8817e4Smiod {
1286*3d8817e4Smiod bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
1287*3d8817e4Smiod reloc_sz += 2;
1288*3d8817e4Smiod }
1289*3d8817e4Smiod
1290*3d8817e4Smiod page_count++;
1291*3d8817e4Smiod }
1292*3d8817e4Smiod
1293*3d8817e4Smiod while (reloc_sz & 3)
1294*3d8817e4Smiod reloc_d[reloc_sz++] = 0;
1295*3d8817e4Smiod
1296*3d8817e4Smiod if (page_ptr != (unsigned long) -1)
1297*3d8817e4Smiod bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4);
1298*3d8817e4Smiod
1299*3d8817e4Smiod while (reloc_sz < reloc_s->size)
1300*3d8817e4Smiod reloc_d[reloc_sz++] = 0;
1301*3d8817e4Smiod }
1302*3d8817e4Smiod
1303*3d8817e4Smiod /* Given the exiting def_file structure, print out a .DEF file that
1304*3d8817e4Smiod corresponds to it. */
1305*3d8817e4Smiod
1306*3d8817e4Smiod static void
quoteput(char * s,FILE * f,int needs_quotes)1307*3d8817e4Smiod quoteput (char *s, FILE *f, int needs_quotes)
1308*3d8817e4Smiod {
1309*3d8817e4Smiod char *cp;
1310*3d8817e4Smiod
1311*3d8817e4Smiod for (cp = s; *cp; cp++)
1312*3d8817e4Smiod if (*cp == '\''
1313*3d8817e4Smiod || *cp == '"'
1314*3d8817e4Smiod || *cp == '\\'
1315*3d8817e4Smiod || ISSPACE (*cp)
1316*3d8817e4Smiod || *cp == ','
1317*3d8817e4Smiod || *cp == ';')
1318*3d8817e4Smiod needs_quotes = 1;
1319*3d8817e4Smiod
1320*3d8817e4Smiod if (needs_quotes)
1321*3d8817e4Smiod {
1322*3d8817e4Smiod putc ('"', f);
1323*3d8817e4Smiod
1324*3d8817e4Smiod while (*s)
1325*3d8817e4Smiod {
1326*3d8817e4Smiod if (*s == '"' || *s == '\\')
1327*3d8817e4Smiod putc ('\\', f);
1328*3d8817e4Smiod
1329*3d8817e4Smiod putc (*s, f);
1330*3d8817e4Smiod s++;
1331*3d8817e4Smiod }
1332*3d8817e4Smiod
1333*3d8817e4Smiod putc ('"', f);
1334*3d8817e4Smiod }
1335*3d8817e4Smiod else
1336*3d8817e4Smiod fputs (s, f);
1337*3d8817e4Smiod }
1338*3d8817e4Smiod
1339*3d8817e4Smiod void
pe_dll_generate_def_file(const char * pe_out_def_filename)1340*3d8817e4Smiod pe_dll_generate_def_file (const char *pe_out_def_filename)
1341*3d8817e4Smiod {
1342*3d8817e4Smiod int i;
1343*3d8817e4Smiod FILE *out = fopen (pe_out_def_filename, "w");
1344*3d8817e4Smiod
1345*3d8817e4Smiod if (out == NULL)
1346*3d8817e4Smiod /* xgettext:c-format */
1347*3d8817e4Smiod einfo (_("%s: Can't open output def file %s\n"),
1348*3d8817e4Smiod program_name, pe_out_def_filename);
1349*3d8817e4Smiod
1350*3d8817e4Smiod if (pe_def_file)
1351*3d8817e4Smiod {
1352*3d8817e4Smiod if (pe_def_file->name)
1353*3d8817e4Smiod {
1354*3d8817e4Smiod if (pe_def_file->is_dll)
1355*3d8817e4Smiod fprintf (out, "LIBRARY ");
1356*3d8817e4Smiod else
1357*3d8817e4Smiod fprintf (out, "NAME ");
1358*3d8817e4Smiod
1359*3d8817e4Smiod quoteput (pe_def_file->name, out, 1);
1360*3d8817e4Smiod
1361*3d8817e4Smiod if (pe_data (output_bfd)->pe_opthdr.ImageBase)
1362*3d8817e4Smiod fprintf (out, " BASE=0x%lx",
1363*3d8817e4Smiod (unsigned long) pe_data (output_bfd)->pe_opthdr.ImageBase);
1364*3d8817e4Smiod fprintf (out, "\n");
1365*3d8817e4Smiod }
1366*3d8817e4Smiod
1367*3d8817e4Smiod if (pe_def_file->description)
1368*3d8817e4Smiod {
1369*3d8817e4Smiod fprintf (out, "DESCRIPTION ");
1370*3d8817e4Smiod quoteput (pe_def_file->description, out, 1);
1371*3d8817e4Smiod fprintf (out, "\n");
1372*3d8817e4Smiod }
1373*3d8817e4Smiod
1374*3d8817e4Smiod if (pe_def_file->version_minor != -1)
1375*3d8817e4Smiod fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major,
1376*3d8817e4Smiod pe_def_file->version_minor);
1377*3d8817e4Smiod else if (pe_def_file->version_major != -1)
1378*3d8817e4Smiod fprintf (out, "VERSION %d\n", pe_def_file->version_major);
1379*3d8817e4Smiod
1380*3d8817e4Smiod if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1)
1381*3d8817e4Smiod fprintf (out, "\n");
1382*3d8817e4Smiod
1383*3d8817e4Smiod if (pe_def_file->stack_commit != -1)
1384*3d8817e4Smiod fprintf (out, "STACKSIZE 0x%x,0x%x\n",
1385*3d8817e4Smiod pe_def_file->stack_reserve, pe_def_file->stack_commit);
1386*3d8817e4Smiod else if (pe_def_file->stack_reserve != -1)
1387*3d8817e4Smiod fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve);
1388*3d8817e4Smiod
1389*3d8817e4Smiod if (pe_def_file->heap_commit != -1)
1390*3d8817e4Smiod fprintf (out, "HEAPSIZE 0x%x,0x%x\n",
1391*3d8817e4Smiod pe_def_file->heap_reserve, pe_def_file->heap_commit);
1392*3d8817e4Smiod else if (pe_def_file->heap_reserve != -1)
1393*3d8817e4Smiod fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve);
1394*3d8817e4Smiod
1395*3d8817e4Smiod if (pe_def_file->num_section_defs > 0)
1396*3d8817e4Smiod {
1397*3d8817e4Smiod fprintf (out, "\nSECTIONS\n\n");
1398*3d8817e4Smiod
1399*3d8817e4Smiod for (i = 0; i < pe_def_file->num_section_defs; i++)
1400*3d8817e4Smiod {
1401*3d8817e4Smiod fprintf (out, " ");
1402*3d8817e4Smiod quoteput (pe_def_file->section_defs[i].name, out, 0);
1403*3d8817e4Smiod
1404*3d8817e4Smiod if (pe_def_file->section_defs[i].class)
1405*3d8817e4Smiod {
1406*3d8817e4Smiod fprintf (out, " CLASS ");
1407*3d8817e4Smiod quoteput (pe_def_file->section_defs[i].class, out, 0);
1408*3d8817e4Smiod }
1409*3d8817e4Smiod
1410*3d8817e4Smiod if (pe_def_file->section_defs[i].flag_read)
1411*3d8817e4Smiod fprintf (out, " READ");
1412*3d8817e4Smiod
1413*3d8817e4Smiod if (pe_def_file->section_defs[i].flag_write)
1414*3d8817e4Smiod fprintf (out, " WRITE");
1415*3d8817e4Smiod
1416*3d8817e4Smiod if (pe_def_file->section_defs[i].flag_execute)
1417*3d8817e4Smiod fprintf (out, " EXECUTE");
1418*3d8817e4Smiod
1419*3d8817e4Smiod if (pe_def_file->section_defs[i].flag_shared)
1420*3d8817e4Smiod fprintf (out, " SHARED");
1421*3d8817e4Smiod
1422*3d8817e4Smiod fprintf (out, "\n");
1423*3d8817e4Smiod }
1424*3d8817e4Smiod }
1425*3d8817e4Smiod
1426*3d8817e4Smiod if (pe_def_file->num_exports > 0)
1427*3d8817e4Smiod {
1428*3d8817e4Smiod fprintf (out, "EXPORTS\n");
1429*3d8817e4Smiod
1430*3d8817e4Smiod for (i = 0; i < pe_def_file->num_exports; i++)
1431*3d8817e4Smiod {
1432*3d8817e4Smiod def_file_export *e = pe_def_file->exports + i;
1433*3d8817e4Smiod fprintf (out, " ");
1434*3d8817e4Smiod quoteput (e->name, out, 0);
1435*3d8817e4Smiod
1436*3d8817e4Smiod if (e->internal_name && strcmp (e->internal_name, e->name))
1437*3d8817e4Smiod {
1438*3d8817e4Smiod fprintf (out, " = ");
1439*3d8817e4Smiod quoteput (e->internal_name, out, 0);
1440*3d8817e4Smiod }
1441*3d8817e4Smiod
1442*3d8817e4Smiod if (e->ordinal != -1)
1443*3d8817e4Smiod fprintf (out, " @%d", e->ordinal);
1444*3d8817e4Smiod
1445*3d8817e4Smiod if (e->flag_private)
1446*3d8817e4Smiod fprintf (out, " PRIVATE");
1447*3d8817e4Smiod
1448*3d8817e4Smiod if (e->flag_constant)
1449*3d8817e4Smiod fprintf (out, " CONSTANT");
1450*3d8817e4Smiod
1451*3d8817e4Smiod if (e->flag_noname)
1452*3d8817e4Smiod fprintf (out, " NONAME");
1453*3d8817e4Smiod
1454*3d8817e4Smiod if (e->flag_data)
1455*3d8817e4Smiod fprintf (out, " DATA");
1456*3d8817e4Smiod
1457*3d8817e4Smiod fprintf (out, "\n");
1458*3d8817e4Smiod }
1459*3d8817e4Smiod }
1460*3d8817e4Smiod
1461*3d8817e4Smiod if (pe_def_file->num_imports > 0)
1462*3d8817e4Smiod {
1463*3d8817e4Smiod fprintf (out, "\nIMPORTS\n\n");
1464*3d8817e4Smiod
1465*3d8817e4Smiod for (i = 0; i < pe_def_file->num_imports; i++)
1466*3d8817e4Smiod {
1467*3d8817e4Smiod def_file_import *im = pe_def_file->imports + i;
1468*3d8817e4Smiod fprintf (out, " ");
1469*3d8817e4Smiod
1470*3d8817e4Smiod if (im->internal_name
1471*3d8817e4Smiod && (!im->name || strcmp (im->internal_name, im->name)))
1472*3d8817e4Smiod {
1473*3d8817e4Smiod quoteput (im->internal_name, out, 0);
1474*3d8817e4Smiod fprintf (out, " = ");
1475*3d8817e4Smiod }
1476*3d8817e4Smiod
1477*3d8817e4Smiod quoteput (im->module->name, out, 0);
1478*3d8817e4Smiod fprintf (out, ".");
1479*3d8817e4Smiod
1480*3d8817e4Smiod if (im->name)
1481*3d8817e4Smiod quoteput (im->name, out, 0);
1482*3d8817e4Smiod else
1483*3d8817e4Smiod fprintf (out, "%d", im->ordinal);
1484*3d8817e4Smiod
1485*3d8817e4Smiod fprintf (out, "\n");
1486*3d8817e4Smiod }
1487*3d8817e4Smiod }
1488*3d8817e4Smiod }
1489*3d8817e4Smiod else
1490*3d8817e4Smiod fprintf (out, _("; no contents available\n"));
1491*3d8817e4Smiod
1492*3d8817e4Smiod if (fclose (out) == EOF)
1493*3d8817e4Smiod /* xgettext:c-format */
1494*3d8817e4Smiod einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename);
1495*3d8817e4Smiod }
1496*3d8817e4Smiod
1497*3d8817e4Smiod /* Generate the import library. */
1498*3d8817e4Smiod
1499*3d8817e4Smiod static asymbol **symtab;
1500*3d8817e4Smiod static int symptr;
1501*3d8817e4Smiod static int tmp_seq;
1502*3d8817e4Smiod static const char *dll_filename;
1503*3d8817e4Smiod static char *dll_symname;
1504*3d8817e4Smiod
1505*3d8817e4Smiod #define UNDSEC (asection *) &bfd_und_section
1506*3d8817e4Smiod
1507*3d8817e4Smiod static asection *
quick_section(bfd * abfd,const char * name,int flags,int align)1508*3d8817e4Smiod quick_section (bfd *abfd, const char *name, int flags, int align)
1509*3d8817e4Smiod {
1510*3d8817e4Smiod asection *sec;
1511*3d8817e4Smiod asymbol *sym;
1512*3d8817e4Smiod
1513*3d8817e4Smiod sec = bfd_make_section_old_way (abfd, name);
1514*3d8817e4Smiod bfd_set_section_flags (abfd, sec, flags | SEC_ALLOC | SEC_LOAD | SEC_KEEP);
1515*3d8817e4Smiod bfd_set_section_alignment (abfd, sec, align);
1516*3d8817e4Smiod /* Remember to undo this before trying to link internally! */
1517*3d8817e4Smiod sec->output_section = sec;
1518*3d8817e4Smiod
1519*3d8817e4Smiod sym = bfd_make_empty_symbol (abfd);
1520*3d8817e4Smiod symtab[symptr++] = sym;
1521*3d8817e4Smiod sym->name = sec->name;
1522*3d8817e4Smiod sym->section = sec;
1523*3d8817e4Smiod sym->flags = BSF_LOCAL;
1524*3d8817e4Smiod sym->value = 0;
1525*3d8817e4Smiod
1526*3d8817e4Smiod return sec;
1527*3d8817e4Smiod }
1528*3d8817e4Smiod
1529*3d8817e4Smiod static void
quick_symbol(bfd * abfd,const char * n1,const char * n2,const char * n3,asection * sec,int flags,int addr)1530*3d8817e4Smiod quick_symbol (bfd *abfd,
1531*3d8817e4Smiod const char *n1,
1532*3d8817e4Smiod const char *n2,
1533*3d8817e4Smiod const char *n3,
1534*3d8817e4Smiod asection *sec,
1535*3d8817e4Smiod int flags,
1536*3d8817e4Smiod int addr)
1537*3d8817e4Smiod {
1538*3d8817e4Smiod asymbol *sym;
1539*3d8817e4Smiod char *name = xmalloc (strlen (n1) + strlen (n2) + strlen (n3) + 1);
1540*3d8817e4Smiod
1541*3d8817e4Smiod strcpy (name, n1);
1542*3d8817e4Smiod strcat (name, n2);
1543*3d8817e4Smiod strcat (name, n3);
1544*3d8817e4Smiod sym = bfd_make_empty_symbol (abfd);
1545*3d8817e4Smiod sym->name = name;
1546*3d8817e4Smiod sym->section = sec;
1547*3d8817e4Smiod sym->flags = flags;
1548*3d8817e4Smiod sym->value = addr;
1549*3d8817e4Smiod symtab[symptr++] = sym;
1550*3d8817e4Smiod }
1551*3d8817e4Smiod
1552*3d8817e4Smiod static arelent *reltab = 0;
1553*3d8817e4Smiod static int relcount = 0, relsize = 0;
1554*3d8817e4Smiod
1555*3d8817e4Smiod static void
quick_reloc(bfd * abfd,int address,int which_howto,int symidx)1556*3d8817e4Smiod quick_reloc (bfd *abfd, int address, int which_howto, int symidx)
1557*3d8817e4Smiod {
1558*3d8817e4Smiod if (relcount >= relsize - 1)
1559*3d8817e4Smiod {
1560*3d8817e4Smiod relsize += 10;
1561*3d8817e4Smiod if (reltab)
1562*3d8817e4Smiod reltab = xrealloc (reltab, relsize * sizeof (arelent));
1563*3d8817e4Smiod else
1564*3d8817e4Smiod reltab = xmalloc (relsize * sizeof (arelent));
1565*3d8817e4Smiod }
1566*3d8817e4Smiod reltab[relcount].address = address;
1567*3d8817e4Smiod reltab[relcount].addend = 0;
1568*3d8817e4Smiod reltab[relcount].howto = bfd_reloc_type_lookup (abfd, which_howto);
1569*3d8817e4Smiod reltab[relcount].sym_ptr_ptr = symtab + symidx;
1570*3d8817e4Smiod relcount++;
1571*3d8817e4Smiod }
1572*3d8817e4Smiod
1573*3d8817e4Smiod static void
save_relocs(asection * sec)1574*3d8817e4Smiod save_relocs (asection *sec)
1575*3d8817e4Smiod {
1576*3d8817e4Smiod int i;
1577*3d8817e4Smiod
1578*3d8817e4Smiod sec->relocation = reltab;
1579*3d8817e4Smiod sec->reloc_count = relcount;
1580*3d8817e4Smiod sec->orelocation = xmalloc ((relcount + 1) * sizeof (arelent *));
1581*3d8817e4Smiod for (i = 0; i < relcount; i++)
1582*3d8817e4Smiod sec->orelocation[i] = sec->relocation + i;
1583*3d8817e4Smiod sec->orelocation[relcount] = 0;
1584*3d8817e4Smiod sec->flags |= SEC_RELOC;
1585*3d8817e4Smiod reltab = 0;
1586*3d8817e4Smiod relcount = relsize = 0;
1587*3d8817e4Smiod }
1588*3d8817e4Smiod
1589*3d8817e4Smiod /* .section .idata$2
1590*3d8817e4Smiod .global __head_my_dll
1591*3d8817e4Smiod __head_my_dll:
1592*3d8817e4Smiod .rva hname
1593*3d8817e4Smiod .long 0
1594*3d8817e4Smiod .long 0
1595*3d8817e4Smiod .rva __my_dll_iname
1596*3d8817e4Smiod .rva fthunk
1597*3d8817e4Smiod
1598*3d8817e4Smiod .section .idata$5
1599*3d8817e4Smiod .long 0
1600*3d8817e4Smiod fthunk:
1601*3d8817e4Smiod
1602*3d8817e4Smiod .section .idata$4
1603*3d8817e4Smiod .long 0
1604*3d8817e4Smiod hname: */
1605*3d8817e4Smiod
1606*3d8817e4Smiod static bfd *
make_head(bfd * parent)1607*3d8817e4Smiod make_head (bfd *parent)
1608*3d8817e4Smiod {
1609*3d8817e4Smiod asection *id2, *id5, *id4;
1610*3d8817e4Smiod unsigned char *d2, *d5, *d4;
1611*3d8817e4Smiod char *oname;
1612*3d8817e4Smiod bfd *abfd;
1613*3d8817e4Smiod
1614*3d8817e4Smiod oname = xmalloc (20);
1615*3d8817e4Smiod sprintf (oname, "d%06d.o", tmp_seq);
1616*3d8817e4Smiod tmp_seq++;
1617*3d8817e4Smiod
1618*3d8817e4Smiod abfd = bfd_create (oname, parent);
1619*3d8817e4Smiod bfd_find_target (pe_details->object_target, abfd);
1620*3d8817e4Smiod bfd_make_writable (abfd);
1621*3d8817e4Smiod
1622*3d8817e4Smiod bfd_set_format (abfd, bfd_object);
1623*3d8817e4Smiod bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1624*3d8817e4Smiod
1625*3d8817e4Smiod symptr = 0;
1626*3d8817e4Smiod symtab = xmalloc (6 * sizeof (asymbol *));
1627*3d8817e4Smiod id2 = quick_section (abfd, ".idata$2", SEC_HAS_CONTENTS, 2);
1628*3d8817e4Smiod id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1629*3d8817e4Smiod id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1630*3d8817e4Smiod quick_symbol (abfd, U ("_head_"), dll_symname, "", id2, BSF_GLOBAL, 0);
1631*3d8817e4Smiod quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
1632*3d8817e4Smiod
1633*3d8817e4Smiod /* OK, pay attention here. I got confused myself looking back at
1634*3d8817e4Smiod it. We create a four-byte section to mark the beginning of the
1635*3d8817e4Smiod list, and we include an offset of 4 in the section, so that the
1636*3d8817e4Smiod pointer to the list points to the *end* of this section, which is
1637*3d8817e4Smiod the start of the list of sections from other objects. */
1638*3d8817e4Smiod
1639*3d8817e4Smiod bfd_set_section_size (abfd, id2, 20);
1640*3d8817e4Smiod d2 = xmalloc (20);
1641*3d8817e4Smiod id2->contents = d2;
1642*3d8817e4Smiod memset (d2, 0, 20);
1643*3d8817e4Smiod d2[0] = d2[16] = 4; /* Reloc addend. */
1644*3d8817e4Smiod quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1645*3d8817e4Smiod quick_reloc (abfd, 12, BFD_RELOC_RVA, 4);
1646*3d8817e4Smiod quick_reloc (abfd, 16, BFD_RELOC_RVA, 1);
1647*3d8817e4Smiod save_relocs (id2);
1648*3d8817e4Smiod
1649*3d8817e4Smiod bfd_set_section_size (abfd, id5, 4);
1650*3d8817e4Smiod d5 = xmalloc (4);
1651*3d8817e4Smiod id5->contents = d5;
1652*3d8817e4Smiod memset (d5, 0, 4);
1653*3d8817e4Smiod
1654*3d8817e4Smiod bfd_set_section_size (abfd, id4, 4);
1655*3d8817e4Smiod d4 = xmalloc (4);
1656*3d8817e4Smiod id4->contents = d4;
1657*3d8817e4Smiod memset (d4, 0, 4);
1658*3d8817e4Smiod
1659*3d8817e4Smiod bfd_set_symtab (abfd, symtab, symptr);
1660*3d8817e4Smiod
1661*3d8817e4Smiod bfd_set_section_contents (abfd, id2, d2, 0, 20);
1662*3d8817e4Smiod bfd_set_section_contents (abfd, id5, d5, 0, 4);
1663*3d8817e4Smiod bfd_set_section_contents (abfd, id4, d4, 0, 4);
1664*3d8817e4Smiod
1665*3d8817e4Smiod bfd_make_readable (abfd);
1666*3d8817e4Smiod return abfd;
1667*3d8817e4Smiod }
1668*3d8817e4Smiod
1669*3d8817e4Smiod /* .section .idata$4
1670*3d8817e4Smiod .long 0
1671*3d8817e4Smiod .section .idata$5
1672*3d8817e4Smiod .long 0
1673*3d8817e4Smiod .section idata$7
1674*3d8817e4Smiod .global __my_dll_iname
1675*3d8817e4Smiod __my_dll_iname:
1676*3d8817e4Smiod .asciz "my.dll" */
1677*3d8817e4Smiod
1678*3d8817e4Smiod static bfd *
make_tail(bfd * parent)1679*3d8817e4Smiod make_tail (bfd *parent)
1680*3d8817e4Smiod {
1681*3d8817e4Smiod asection *id4, *id5, *id7;
1682*3d8817e4Smiod unsigned char *d4, *d5, *d7;
1683*3d8817e4Smiod int len;
1684*3d8817e4Smiod char *oname;
1685*3d8817e4Smiod bfd *abfd;
1686*3d8817e4Smiod
1687*3d8817e4Smiod oname = xmalloc (20);
1688*3d8817e4Smiod sprintf (oname, "d%06d.o", tmp_seq);
1689*3d8817e4Smiod tmp_seq++;
1690*3d8817e4Smiod
1691*3d8817e4Smiod abfd = bfd_create (oname, parent);
1692*3d8817e4Smiod bfd_find_target (pe_details->object_target, abfd);
1693*3d8817e4Smiod bfd_make_writable (abfd);
1694*3d8817e4Smiod
1695*3d8817e4Smiod bfd_set_format (abfd, bfd_object);
1696*3d8817e4Smiod bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1697*3d8817e4Smiod
1698*3d8817e4Smiod symptr = 0;
1699*3d8817e4Smiod symtab = xmalloc (5 * sizeof (asymbol *));
1700*3d8817e4Smiod id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1701*3d8817e4Smiod id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1702*3d8817e4Smiod id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1703*3d8817e4Smiod quick_symbol (abfd, U (""), dll_symname, "_iname", id7, BSF_GLOBAL, 0);
1704*3d8817e4Smiod
1705*3d8817e4Smiod bfd_set_section_size (abfd, id4, 4);
1706*3d8817e4Smiod d4 = xmalloc (4);
1707*3d8817e4Smiod id4->contents = d4;
1708*3d8817e4Smiod memset (d4, 0, 4);
1709*3d8817e4Smiod
1710*3d8817e4Smiod bfd_set_section_size (abfd, id5, 4);
1711*3d8817e4Smiod d5 = xmalloc (4);
1712*3d8817e4Smiod id5->contents = d5;
1713*3d8817e4Smiod memset (d5, 0, 4);
1714*3d8817e4Smiod
1715*3d8817e4Smiod len = strlen (dll_filename) + 1;
1716*3d8817e4Smiod if (len & 1)
1717*3d8817e4Smiod len++;
1718*3d8817e4Smiod bfd_set_section_size (abfd, id7, len);
1719*3d8817e4Smiod d7 = xmalloc (len);
1720*3d8817e4Smiod id7->contents = d7;
1721*3d8817e4Smiod strcpy ((char *) d7, dll_filename);
1722*3d8817e4Smiod
1723*3d8817e4Smiod bfd_set_symtab (abfd, symtab, symptr);
1724*3d8817e4Smiod
1725*3d8817e4Smiod bfd_set_section_contents (abfd, id4, d4, 0, 4);
1726*3d8817e4Smiod bfd_set_section_contents (abfd, id5, d5, 0, 4);
1727*3d8817e4Smiod bfd_set_section_contents (abfd, id7, d7, 0, len);
1728*3d8817e4Smiod
1729*3d8817e4Smiod bfd_make_readable (abfd);
1730*3d8817e4Smiod return abfd;
1731*3d8817e4Smiod }
1732*3d8817e4Smiod
1733*3d8817e4Smiod /* .text
1734*3d8817e4Smiod .global _function
1735*3d8817e4Smiod .global ___imp_function
1736*3d8817e4Smiod .global __imp__function
1737*3d8817e4Smiod _function:
1738*3d8817e4Smiod jmp *__imp__function:
1739*3d8817e4Smiod
1740*3d8817e4Smiod .section idata$7
1741*3d8817e4Smiod .long __head_my_dll
1742*3d8817e4Smiod
1743*3d8817e4Smiod .section .idata$5
1744*3d8817e4Smiod ___imp_function:
1745*3d8817e4Smiod __imp__function:
1746*3d8817e4Smiod iat?
1747*3d8817e4Smiod .section .idata$4
1748*3d8817e4Smiod iat?
1749*3d8817e4Smiod .section .idata$6
1750*3d8817e4Smiod ID<ordinal>:
1751*3d8817e4Smiod .short <hint>
1752*3d8817e4Smiod .asciz "function" xlate? (add underscore, kill at) */
1753*3d8817e4Smiod
1754*3d8817e4Smiod static unsigned char jmp_ix86_bytes[] =
1755*3d8817e4Smiod {
1756*3d8817e4Smiod 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
1757*3d8817e4Smiod };
1758*3d8817e4Smiod
1759*3d8817e4Smiod /* _function:
1760*3d8817e4Smiod mov.l ip+8,r0
1761*3d8817e4Smiod mov.l @r0,r0
1762*3d8817e4Smiod jmp @r0
1763*3d8817e4Smiod nop
1764*3d8817e4Smiod .dw __imp_function */
1765*3d8817e4Smiod
1766*3d8817e4Smiod static unsigned char jmp_sh_bytes[] =
1767*3d8817e4Smiod {
1768*3d8817e4Smiod 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
1769*3d8817e4Smiod };
1770*3d8817e4Smiod
1771*3d8817e4Smiod /* _function:
1772*3d8817e4Smiod lui $t0,<high:__imp_function>
1773*3d8817e4Smiod lw $t0,<low:__imp_function>
1774*3d8817e4Smiod jr $t0
1775*3d8817e4Smiod nop */
1776*3d8817e4Smiod
1777*3d8817e4Smiod static unsigned char jmp_mips_bytes[] =
1778*3d8817e4Smiod {
1779*3d8817e4Smiod 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
1780*3d8817e4Smiod 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
1781*3d8817e4Smiod };
1782*3d8817e4Smiod
1783*3d8817e4Smiod static bfd *
make_one(def_file_export * exp,bfd * parent)1784*3d8817e4Smiod make_one (def_file_export *exp, bfd *parent)
1785*3d8817e4Smiod {
1786*3d8817e4Smiod asection *tx, *id7, *id5, *id4, *id6;
1787*3d8817e4Smiod unsigned char *td = NULL, *d7, *d5, *d4, *d6 = NULL;
1788*3d8817e4Smiod int len;
1789*3d8817e4Smiod char *oname;
1790*3d8817e4Smiod bfd *abfd;
1791*3d8817e4Smiod unsigned char *jmp_bytes = NULL;
1792*3d8817e4Smiod int jmp_byte_count = 0;
1793*3d8817e4Smiod
1794*3d8817e4Smiod switch (pe_details->pe_arch)
1795*3d8817e4Smiod {
1796*3d8817e4Smiod case PE_ARCH_i386:
1797*3d8817e4Smiod jmp_bytes = jmp_ix86_bytes;
1798*3d8817e4Smiod jmp_byte_count = sizeof (jmp_ix86_bytes);
1799*3d8817e4Smiod break;
1800*3d8817e4Smiod case PE_ARCH_sh:
1801*3d8817e4Smiod jmp_bytes = jmp_sh_bytes;
1802*3d8817e4Smiod jmp_byte_count = sizeof (jmp_sh_bytes);
1803*3d8817e4Smiod break;
1804*3d8817e4Smiod case PE_ARCH_mips:
1805*3d8817e4Smiod jmp_bytes = jmp_mips_bytes;
1806*3d8817e4Smiod jmp_byte_count = sizeof (jmp_mips_bytes);
1807*3d8817e4Smiod break;
1808*3d8817e4Smiod default:
1809*3d8817e4Smiod abort ();
1810*3d8817e4Smiod }
1811*3d8817e4Smiod
1812*3d8817e4Smiod oname = xmalloc (20);
1813*3d8817e4Smiod sprintf (oname, "d%06d.o", tmp_seq);
1814*3d8817e4Smiod tmp_seq++;
1815*3d8817e4Smiod
1816*3d8817e4Smiod abfd = bfd_create (oname, parent);
1817*3d8817e4Smiod bfd_find_target (pe_details->object_target, abfd);
1818*3d8817e4Smiod bfd_make_writable (abfd);
1819*3d8817e4Smiod
1820*3d8817e4Smiod bfd_set_format (abfd, bfd_object);
1821*3d8817e4Smiod bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1822*3d8817e4Smiod
1823*3d8817e4Smiod symptr = 0;
1824*3d8817e4Smiod symtab = xmalloc (11 * sizeof (asymbol *));
1825*3d8817e4Smiod tx = quick_section (abfd, ".text", SEC_CODE|SEC_HAS_CONTENTS, 2);
1826*3d8817e4Smiod id7 = quick_section (abfd, ".idata$7", SEC_HAS_CONTENTS, 2);
1827*3d8817e4Smiod id5 = quick_section (abfd, ".idata$5", SEC_HAS_CONTENTS, 2);
1828*3d8817e4Smiod id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1829*3d8817e4Smiod id6 = quick_section (abfd, ".idata$6", SEC_HAS_CONTENTS, 2);
1830*3d8817e4Smiod
1831*3d8817e4Smiod if (*exp->internal_name == '@')
1832*3d8817e4Smiod {
1833*3d8817e4Smiod quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
1834*3d8817e4Smiod BSF_GLOBAL, 0);
1835*3d8817e4Smiod if (! exp->flag_data)
1836*3d8817e4Smiod quick_symbol (abfd, "", exp->internal_name, "", tx, BSF_GLOBAL, 0);
1837*3d8817e4Smiod quick_symbol (abfd, U ("_imp_"), exp->internal_name, "", id5,
1838*3d8817e4Smiod BSF_GLOBAL, 0);
1839*3d8817e4Smiod /* Fastcall applies only to functions,
1840*3d8817e4Smiod so no need for auto-import symbol. */
1841*3d8817e4Smiod }
1842*3d8817e4Smiod else
1843*3d8817e4Smiod {
1844*3d8817e4Smiod quick_symbol (abfd, U ("_head_"), dll_symname, "", UNDSEC,
1845*3d8817e4Smiod BSF_GLOBAL, 0);
1846*3d8817e4Smiod if (! exp->flag_data)
1847*3d8817e4Smiod quick_symbol (abfd, U (""), exp->internal_name, "", tx,
1848*3d8817e4Smiod BSF_GLOBAL, 0);
1849*3d8817e4Smiod quick_symbol (abfd, U ("_imp__"), exp->internal_name, "", id5,
1850*3d8817e4Smiod BSF_GLOBAL, 0);
1851*3d8817e4Smiod /* Symbol to reference ord/name of imported
1852*3d8817e4Smiod data symbol, used to implement auto-import. */
1853*3d8817e4Smiod if (exp->flag_data)
1854*3d8817e4Smiod quick_symbol (abfd, U("_nm__"), exp->internal_name, "", id6,
1855*3d8817e4Smiod BSF_GLOBAL,0);
1856*3d8817e4Smiod }
1857*3d8817e4Smiod if (pe_dll_compat_implib)
1858*3d8817e4Smiod quick_symbol (abfd, U ("__imp_"), exp->internal_name, "", id5,
1859*3d8817e4Smiod BSF_GLOBAL, 0);
1860*3d8817e4Smiod
1861*3d8817e4Smiod if (! exp->flag_data)
1862*3d8817e4Smiod {
1863*3d8817e4Smiod bfd_set_section_size (abfd, tx, jmp_byte_count);
1864*3d8817e4Smiod td = xmalloc (jmp_byte_count);
1865*3d8817e4Smiod tx->contents = td;
1866*3d8817e4Smiod memcpy (td, jmp_bytes, jmp_byte_count);
1867*3d8817e4Smiod
1868*3d8817e4Smiod switch (pe_details->pe_arch)
1869*3d8817e4Smiod {
1870*3d8817e4Smiod case PE_ARCH_i386:
1871*3d8817e4Smiod quick_reloc (abfd, 2, BFD_RELOC_32, 2);
1872*3d8817e4Smiod break;
1873*3d8817e4Smiod case PE_ARCH_sh:
1874*3d8817e4Smiod quick_reloc (abfd, 8, BFD_RELOC_32, 2);
1875*3d8817e4Smiod break;
1876*3d8817e4Smiod case PE_ARCH_mips:
1877*3d8817e4Smiod quick_reloc (abfd, 0, BFD_RELOC_HI16_S, 2);
1878*3d8817e4Smiod quick_reloc (abfd, 0, BFD_RELOC_LO16, 0); /* MIPS_R_PAIR */
1879*3d8817e4Smiod quick_reloc (abfd, 4, BFD_RELOC_LO16, 2);
1880*3d8817e4Smiod break;
1881*3d8817e4Smiod default:
1882*3d8817e4Smiod abort ();
1883*3d8817e4Smiod }
1884*3d8817e4Smiod save_relocs (tx);
1885*3d8817e4Smiod }
1886*3d8817e4Smiod
1887*3d8817e4Smiod bfd_set_section_size (abfd, id7, 4);
1888*3d8817e4Smiod d7 = xmalloc (4);
1889*3d8817e4Smiod id7->contents = d7;
1890*3d8817e4Smiod memset (d7, 0, 4);
1891*3d8817e4Smiod quick_reloc (abfd, 0, BFD_RELOC_RVA, 5);
1892*3d8817e4Smiod save_relocs (id7);
1893*3d8817e4Smiod
1894*3d8817e4Smiod bfd_set_section_size (abfd, id5, 4);
1895*3d8817e4Smiod d5 = xmalloc (4);
1896*3d8817e4Smiod id5->contents = d5;
1897*3d8817e4Smiod memset (d5, 0, 4);
1898*3d8817e4Smiod
1899*3d8817e4Smiod if (exp->flag_noname)
1900*3d8817e4Smiod {
1901*3d8817e4Smiod d5[0] = exp->ordinal;
1902*3d8817e4Smiod d5[1] = exp->ordinal >> 8;
1903*3d8817e4Smiod d5[3] = 0x80;
1904*3d8817e4Smiod }
1905*3d8817e4Smiod else
1906*3d8817e4Smiod {
1907*3d8817e4Smiod quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1908*3d8817e4Smiod save_relocs (id5);
1909*3d8817e4Smiod }
1910*3d8817e4Smiod
1911*3d8817e4Smiod bfd_set_section_size (abfd, id4, 4);
1912*3d8817e4Smiod d4 = xmalloc (4);
1913*3d8817e4Smiod id4->contents = d4;
1914*3d8817e4Smiod memset (d4, 0, 4);
1915*3d8817e4Smiod
1916*3d8817e4Smiod if (exp->flag_noname)
1917*3d8817e4Smiod {
1918*3d8817e4Smiod d4[0] = exp->ordinal;
1919*3d8817e4Smiod d4[1] = exp->ordinal >> 8;
1920*3d8817e4Smiod d4[3] = 0x80;
1921*3d8817e4Smiod }
1922*3d8817e4Smiod else
1923*3d8817e4Smiod {
1924*3d8817e4Smiod quick_reloc (abfd, 0, BFD_RELOC_RVA, 4);
1925*3d8817e4Smiod save_relocs (id4);
1926*3d8817e4Smiod }
1927*3d8817e4Smiod
1928*3d8817e4Smiod if (exp->flag_noname)
1929*3d8817e4Smiod {
1930*3d8817e4Smiod len = 0;
1931*3d8817e4Smiod bfd_set_section_size (abfd, id6, 0);
1932*3d8817e4Smiod }
1933*3d8817e4Smiod else
1934*3d8817e4Smiod {
1935*3d8817e4Smiod len = strlen (exp->name) + 3;
1936*3d8817e4Smiod if (len & 1)
1937*3d8817e4Smiod len++;
1938*3d8817e4Smiod bfd_set_section_size (abfd, id6, len);
1939*3d8817e4Smiod d6 = xmalloc (len);
1940*3d8817e4Smiod id6->contents = d6;
1941*3d8817e4Smiod memset (d6, 0, len);
1942*3d8817e4Smiod d6[0] = exp->hint & 0xff;
1943*3d8817e4Smiod d6[1] = exp->hint >> 8;
1944*3d8817e4Smiod strcpy ((char *) d6 + 2, exp->name);
1945*3d8817e4Smiod }
1946*3d8817e4Smiod
1947*3d8817e4Smiod bfd_set_symtab (abfd, symtab, symptr);
1948*3d8817e4Smiod
1949*3d8817e4Smiod bfd_set_section_contents (abfd, tx, td, 0, jmp_byte_count);
1950*3d8817e4Smiod bfd_set_section_contents (abfd, id7, d7, 0, 4);
1951*3d8817e4Smiod bfd_set_section_contents (abfd, id5, d5, 0, 4);
1952*3d8817e4Smiod bfd_set_section_contents (abfd, id4, d4, 0, 4);
1953*3d8817e4Smiod if (!exp->flag_noname)
1954*3d8817e4Smiod bfd_set_section_contents (abfd, id6, d6, 0, len);
1955*3d8817e4Smiod
1956*3d8817e4Smiod bfd_make_readable (abfd);
1957*3d8817e4Smiod return abfd;
1958*3d8817e4Smiod }
1959*3d8817e4Smiod
1960*3d8817e4Smiod static bfd *
make_singleton_name_thunk(const char * import,bfd * parent)1961*3d8817e4Smiod make_singleton_name_thunk (const char *import, bfd *parent)
1962*3d8817e4Smiod {
1963*3d8817e4Smiod /* Name thunks go to idata$4. */
1964*3d8817e4Smiod asection *id4;
1965*3d8817e4Smiod unsigned char *d4;
1966*3d8817e4Smiod char *oname;
1967*3d8817e4Smiod bfd *abfd;
1968*3d8817e4Smiod
1969*3d8817e4Smiod oname = xmalloc (20);
1970*3d8817e4Smiod sprintf (oname, "nmth%06d.o", tmp_seq);
1971*3d8817e4Smiod tmp_seq++;
1972*3d8817e4Smiod
1973*3d8817e4Smiod abfd = bfd_create (oname, parent);
1974*3d8817e4Smiod bfd_find_target (pe_details->object_target, abfd);
1975*3d8817e4Smiod bfd_make_writable (abfd);
1976*3d8817e4Smiod
1977*3d8817e4Smiod bfd_set_format (abfd, bfd_object);
1978*3d8817e4Smiod bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
1979*3d8817e4Smiod
1980*3d8817e4Smiod symptr = 0;
1981*3d8817e4Smiod symtab = xmalloc (3 * sizeof (asymbol *));
1982*3d8817e4Smiod id4 = quick_section (abfd, ".idata$4", SEC_HAS_CONTENTS, 2);
1983*3d8817e4Smiod quick_symbol (abfd, U ("_nm_thnk_"), import, "", id4, BSF_GLOBAL, 0);
1984*3d8817e4Smiod quick_symbol (abfd, U ("_nm_"), import, "", UNDSEC, BSF_GLOBAL, 0);
1985*3d8817e4Smiod
1986*3d8817e4Smiod bfd_set_section_size (abfd, id4, 8);
1987*3d8817e4Smiod d4 = xmalloc (4);
1988*3d8817e4Smiod id4->contents = d4;
1989*3d8817e4Smiod memset (d4, 0, 8);
1990*3d8817e4Smiod quick_reloc (abfd, 0, BFD_RELOC_RVA, 2);
1991*3d8817e4Smiod save_relocs (id4);
1992*3d8817e4Smiod
1993*3d8817e4Smiod bfd_set_symtab (abfd, symtab, symptr);
1994*3d8817e4Smiod
1995*3d8817e4Smiod bfd_set_section_contents (abfd, id4, d4, 0, 8);
1996*3d8817e4Smiod
1997*3d8817e4Smiod bfd_make_readable (abfd);
1998*3d8817e4Smiod return abfd;
1999*3d8817e4Smiod }
2000*3d8817e4Smiod
2001*3d8817e4Smiod static char *
make_import_fixup_mark(arelent * rel)2002*3d8817e4Smiod make_import_fixup_mark (arelent *rel)
2003*3d8817e4Smiod {
2004*3d8817e4Smiod /* We convert reloc to symbol, for later reference. */
2005*3d8817e4Smiod static int counter;
2006*3d8817e4Smiod static char *fixup_name = NULL;
2007*3d8817e4Smiod static size_t buffer_len = 0;
2008*3d8817e4Smiod
2009*3d8817e4Smiod struct bfd_symbol *sym = *rel->sym_ptr_ptr;
2010*3d8817e4Smiod
2011*3d8817e4Smiod bfd *abfd = bfd_asymbol_bfd (sym);
2012*3d8817e4Smiod struct bfd_link_hash_entry *bh;
2013*3d8817e4Smiod
2014*3d8817e4Smiod if (!fixup_name)
2015*3d8817e4Smiod {
2016*3d8817e4Smiod fixup_name = xmalloc (384);
2017*3d8817e4Smiod buffer_len = 384;
2018*3d8817e4Smiod }
2019*3d8817e4Smiod
2020*3d8817e4Smiod if (strlen (sym->name) + 25 > buffer_len)
2021*3d8817e4Smiod /* Assume 25 chars for "__fu" + counter + "_". If counter is
2022*3d8817e4Smiod bigger than 20 digits long, we've got worse problems than
2023*3d8817e4Smiod overflowing this buffer... */
2024*3d8817e4Smiod {
2025*3d8817e4Smiod free (fixup_name);
2026*3d8817e4Smiod /* New buffer size is length of symbol, plus 25, but
2027*3d8817e4Smiod then rounded up to the nearest multiple of 128. */
2028*3d8817e4Smiod buffer_len = ((strlen (sym->name) + 25) + 127) & ~127;
2029*3d8817e4Smiod fixup_name = xmalloc (buffer_len);
2030*3d8817e4Smiod }
2031*3d8817e4Smiod
2032*3d8817e4Smiod sprintf (fixup_name, "__fu%d_%s", counter++, sym->name);
2033*3d8817e4Smiod
2034*3d8817e4Smiod bh = NULL;
2035*3d8817e4Smiod bfd_coff_link_add_one_symbol (&link_info, abfd, fixup_name, BSF_GLOBAL,
2036*3d8817e4Smiod current_sec, /* sym->section, */
2037*3d8817e4Smiod rel->address, NULL, TRUE, FALSE, &bh);
2038*3d8817e4Smiod
2039*3d8817e4Smiod if (0)
2040*3d8817e4Smiod {
2041*3d8817e4Smiod struct coff_link_hash_entry *myh;
2042*3d8817e4Smiod
2043*3d8817e4Smiod myh = (struct coff_link_hash_entry *) bh;
2044*3d8817e4Smiod printf ("type:%d\n", myh->type);
2045*3d8817e4Smiod printf ("%s\n", myh->root.u.def.section->name);
2046*3d8817e4Smiod }
2047*3d8817e4Smiod
2048*3d8817e4Smiod return fixup_name;
2049*3d8817e4Smiod }
2050*3d8817e4Smiod
2051*3d8817e4Smiod /* .section .idata$3
2052*3d8817e4Smiod .rva __nm_thnk_SYM (singleton thunk with name of func)
2053*3d8817e4Smiod .long 0
2054*3d8817e4Smiod .long 0
2055*3d8817e4Smiod .rva __my_dll_iname (name of dll)
2056*3d8817e4Smiod .rva __fuNN_SYM (pointer to reference (address) in text) */
2057*3d8817e4Smiod
2058*3d8817e4Smiod static bfd *
make_import_fixup_entry(const char * name,const char * fixup_name,const char * dll_symname,bfd * parent)2059*3d8817e4Smiod make_import_fixup_entry (const char *name,
2060*3d8817e4Smiod const char *fixup_name,
2061*3d8817e4Smiod const char *dll_symname,
2062*3d8817e4Smiod bfd *parent)
2063*3d8817e4Smiod {
2064*3d8817e4Smiod asection *id3;
2065*3d8817e4Smiod unsigned char *d3;
2066*3d8817e4Smiod char *oname;
2067*3d8817e4Smiod bfd *abfd;
2068*3d8817e4Smiod
2069*3d8817e4Smiod oname = xmalloc (20);
2070*3d8817e4Smiod sprintf (oname, "fu%06d.o", tmp_seq);
2071*3d8817e4Smiod tmp_seq++;
2072*3d8817e4Smiod
2073*3d8817e4Smiod abfd = bfd_create (oname, parent);
2074*3d8817e4Smiod bfd_find_target (pe_details->object_target, abfd);
2075*3d8817e4Smiod bfd_make_writable (abfd);
2076*3d8817e4Smiod
2077*3d8817e4Smiod bfd_set_format (abfd, bfd_object);
2078*3d8817e4Smiod bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
2079*3d8817e4Smiod
2080*3d8817e4Smiod symptr = 0;
2081*3d8817e4Smiod symtab = xmalloc (6 * sizeof (asymbol *));
2082*3d8817e4Smiod id3 = quick_section (abfd, ".idata$3", SEC_HAS_CONTENTS, 2);
2083*3d8817e4Smiod
2084*3d8817e4Smiod quick_symbol (abfd, U ("_nm_thnk_"), name, "", UNDSEC, BSF_GLOBAL, 0);
2085*3d8817e4Smiod quick_symbol (abfd, U (""), dll_symname, "_iname", UNDSEC, BSF_GLOBAL, 0);
2086*3d8817e4Smiod quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
2087*3d8817e4Smiod
2088*3d8817e4Smiod bfd_set_section_size (abfd, id3, 20);
2089*3d8817e4Smiod d3 = xmalloc (20);
2090*3d8817e4Smiod id3->contents = d3;
2091*3d8817e4Smiod memset (d3, 0, 20);
2092*3d8817e4Smiod
2093*3d8817e4Smiod quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
2094*3d8817e4Smiod quick_reloc (abfd, 12, BFD_RELOC_RVA, 2);
2095*3d8817e4Smiod quick_reloc (abfd, 16, BFD_RELOC_RVA, 3);
2096*3d8817e4Smiod save_relocs (id3);
2097*3d8817e4Smiod
2098*3d8817e4Smiod bfd_set_symtab (abfd, symtab, symptr);
2099*3d8817e4Smiod
2100*3d8817e4Smiod bfd_set_section_contents (abfd, id3, d3, 0, 20);
2101*3d8817e4Smiod
2102*3d8817e4Smiod bfd_make_readable (abfd);
2103*3d8817e4Smiod return abfd;
2104*3d8817e4Smiod }
2105*3d8817e4Smiod
2106*3d8817e4Smiod /* .section .rdata_runtime_pseudo_reloc
2107*3d8817e4Smiod .long addend
2108*3d8817e4Smiod .rva __fuNN_SYM (pointer to reference (address) in text) */
2109*3d8817e4Smiod
2110*3d8817e4Smiod static bfd *
make_runtime_pseudo_reloc(const char * name ATTRIBUTE_UNUSED,const char * fixup_name,int addend,bfd * parent)2111*3d8817e4Smiod make_runtime_pseudo_reloc (const char *name ATTRIBUTE_UNUSED,
2112*3d8817e4Smiod const char *fixup_name,
2113*3d8817e4Smiod int addend,
2114*3d8817e4Smiod bfd *parent)
2115*3d8817e4Smiod {
2116*3d8817e4Smiod asection *rt_rel;
2117*3d8817e4Smiod unsigned char *rt_rel_d;
2118*3d8817e4Smiod char *oname;
2119*3d8817e4Smiod bfd *abfd;
2120*3d8817e4Smiod
2121*3d8817e4Smiod oname = xmalloc (20);
2122*3d8817e4Smiod sprintf (oname, "rtr%06d.o", tmp_seq);
2123*3d8817e4Smiod tmp_seq++;
2124*3d8817e4Smiod
2125*3d8817e4Smiod abfd = bfd_create (oname, parent);
2126*3d8817e4Smiod bfd_find_target (pe_details->object_target, abfd);
2127*3d8817e4Smiod bfd_make_writable (abfd);
2128*3d8817e4Smiod
2129*3d8817e4Smiod bfd_set_format (abfd, bfd_object);
2130*3d8817e4Smiod bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
2131*3d8817e4Smiod
2132*3d8817e4Smiod symptr = 0;
2133*3d8817e4Smiod symtab = xmalloc (2 * sizeof (asymbol *));
2134*3d8817e4Smiod rt_rel = quick_section (abfd, ".rdata_runtime_pseudo_reloc",
2135*3d8817e4Smiod SEC_HAS_CONTENTS, 2);
2136*3d8817e4Smiod
2137*3d8817e4Smiod quick_symbol (abfd, "", fixup_name, "", UNDSEC, BSF_GLOBAL, 0);
2138*3d8817e4Smiod
2139*3d8817e4Smiod bfd_set_section_size (abfd, rt_rel, 8);
2140*3d8817e4Smiod rt_rel_d = xmalloc (8);
2141*3d8817e4Smiod rt_rel->contents = rt_rel_d;
2142*3d8817e4Smiod memset (rt_rel_d, 0, 8);
2143*3d8817e4Smiod bfd_put_32 (abfd, addend, rt_rel_d);
2144*3d8817e4Smiod
2145*3d8817e4Smiod quick_reloc (abfd, 4, BFD_RELOC_RVA, 1);
2146*3d8817e4Smiod save_relocs (rt_rel);
2147*3d8817e4Smiod
2148*3d8817e4Smiod bfd_set_symtab (abfd, symtab, symptr);
2149*3d8817e4Smiod
2150*3d8817e4Smiod bfd_set_section_contents (abfd, rt_rel, rt_rel_d, 0, 8);
2151*3d8817e4Smiod
2152*3d8817e4Smiod bfd_make_readable (abfd);
2153*3d8817e4Smiod return abfd;
2154*3d8817e4Smiod }
2155*3d8817e4Smiod
2156*3d8817e4Smiod /* .section .rdata
2157*3d8817e4Smiod .rva __pei386_runtime_relocator */
2158*3d8817e4Smiod
2159*3d8817e4Smiod static bfd *
pe_create_runtime_relocator_reference(bfd * parent)2160*3d8817e4Smiod pe_create_runtime_relocator_reference (bfd *parent)
2161*3d8817e4Smiod {
2162*3d8817e4Smiod asection *extern_rt_rel;
2163*3d8817e4Smiod unsigned char *extern_rt_rel_d;
2164*3d8817e4Smiod char *oname;
2165*3d8817e4Smiod bfd *abfd;
2166*3d8817e4Smiod
2167*3d8817e4Smiod oname = xmalloc (20);
2168*3d8817e4Smiod sprintf (oname, "ertr%06d.o", tmp_seq);
2169*3d8817e4Smiod tmp_seq++;
2170*3d8817e4Smiod
2171*3d8817e4Smiod abfd = bfd_create (oname, parent);
2172*3d8817e4Smiod bfd_find_target (pe_details->object_target, abfd);
2173*3d8817e4Smiod bfd_make_writable (abfd);
2174*3d8817e4Smiod
2175*3d8817e4Smiod bfd_set_format (abfd, bfd_object);
2176*3d8817e4Smiod bfd_set_arch_mach (abfd, pe_details->bfd_arch, 0);
2177*3d8817e4Smiod
2178*3d8817e4Smiod symptr = 0;
2179*3d8817e4Smiod symtab = xmalloc (2 * sizeof (asymbol *));
2180*3d8817e4Smiod extern_rt_rel = quick_section (abfd, ".rdata", SEC_HAS_CONTENTS, 2);
2181*3d8817e4Smiod
2182*3d8817e4Smiod quick_symbol (abfd, "", "__pei386_runtime_relocator", "", UNDSEC,
2183*3d8817e4Smiod BSF_NO_FLAGS, 0);
2184*3d8817e4Smiod
2185*3d8817e4Smiod bfd_set_section_size (abfd, extern_rt_rel, 4);
2186*3d8817e4Smiod extern_rt_rel_d = xmalloc (4);
2187*3d8817e4Smiod extern_rt_rel->contents = extern_rt_rel_d;
2188*3d8817e4Smiod
2189*3d8817e4Smiod quick_reloc (abfd, 0, BFD_RELOC_RVA, 1);
2190*3d8817e4Smiod save_relocs (extern_rt_rel);
2191*3d8817e4Smiod
2192*3d8817e4Smiod bfd_set_symtab (abfd, symtab, symptr);
2193*3d8817e4Smiod
2194*3d8817e4Smiod bfd_set_section_contents (abfd, extern_rt_rel, extern_rt_rel_d, 0, 4);
2195*3d8817e4Smiod
2196*3d8817e4Smiod bfd_make_readable (abfd);
2197*3d8817e4Smiod return abfd;
2198*3d8817e4Smiod }
2199*3d8817e4Smiod
2200*3d8817e4Smiod void
pe_create_import_fixup(arelent * rel,asection * s,int addend)2201*3d8817e4Smiod pe_create_import_fixup (arelent *rel, asection *s, int addend)
2202*3d8817e4Smiod {
2203*3d8817e4Smiod char buf[300];
2204*3d8817e4Smiod struct bfd_symbol *sym = *rel->sym_ptr_ptr;
2205*3d8817e4Smiod struct bfd_link_hash_entry *name_thunk_sym;
2206*3d8817e4Smiod const char *name = sym->name;
2207*3d8817e4Smiod char *fixup_name = make_import_fixup_mark (rel);
2208*3d8817e4Smiod bfd *b;
2209*3d8817e4Smiod
2210*3d8817e4Smiod sprintf (buf, U ("_nm_thnk_%s"), name);
2211*3d8817e4Smiod
2212*3d8817e4Smiod name_thunk_sym = bfd_link_hash_lookup (link_info.hash, buf, 0, 0, 1);
2213*3d8817e4Smiod
2214*3d8817e4Smiod if (!name_thunk_sym || name_thunk_sym->type != bfd_link_hash_defined)
2215*3d8817e4Smiod {
2216*3d8817e4Smiod bfd *b = make_singleton_name_thunk (name, output_bfd);
2217*3d8817e4Smiod add_bfd_to_link (b, b->filename, &link_info);
2218*3d8817e4Smiod
2219*3d8817e4Smiod /* If we ever use autoimport, we have to cast text section writable. */
2220*3d8817e4Smiod config.text_read_only = FALSE;
2221*3d8817e4Smiod output_bfd->flags &= ~WP_TEXT;
2222*3d8817e4Smiod }
2223*3d8817e4Smiod
2224*3d8817e4Smiod if (addend == 0 || link_info.pei386_runtime_pseudo_reloc)
2225*3d8817e4Smiod {
2226*3d8817e4Smiod extern char * pe_data_import_dll;
2227*3d8817e4Smiod char * dll_symname = pe_data_import_dll ? pe_data_import_dll : "unknown";
2228*3d8817e4Smiod
2229*3d8817e4Smiod b = make_import_fixup_entry (name, fixup_name, dll_symname, output_bfd);
2230*3d8817e4Smiod add_bfd_to_link (b, b->filename, &link_info);
2231*3d8817e4Smiod }
2232*3d8817e4Smiod
2233*3d8817e4Smiod if (addend != 0)
2234*3d8817e4Smiod {
2235*3d8817e4Smiod if (link_info.pei386_runtime_pseudo_reloc)
2236*3d8817e4Smiod {
2237*3d8817e4Smiod if (pe_dll_extra_pe_debug)
2238*3d8817e4Smiod printf ("creating runtime pseudo-reloc entry for %s (addend=%d)\n",
2239*3d8817e4Smiod fixup_name, addend);
2240*3d8817e4Smiod b = make_runtime_pseudo_reloc (name, fixup_name, addend, output_bfd);
2241*3d8817e4Smiod add_bfd_to_link (b, b->filename, &link_info);
2242*3d8817e4Smiod
2243*3d8817e4Smiod if (runtime_pseudo_relocs_created == 0)
2244*3d8817e4Smiod {
2245*3d8817e4Smiod b = pe_create_runtime_relocator_reference (output_bfd);
2246*3d8817e4Smiod add_bfd_to_link (b, b->filename, &link_info);
2247*3d8817e4Smiod }
2248*3d8817e4Smiod runtime_pseudo_relocs_created++;
2249*3d8817e4Smiod }
2250*3d8817e4Smiod else
2251*3d8817e4Smiod {
2252*3d8817e4Smiod einfo (_("%C: variable '%T' can't be auto-imported. Please read the documentation for ld's --enable-auto-import for details.\n"),
2253*3d8817e4Smiod s->owner, s, rel->address, sym->name);
2254*3d8817e4Smiod einfo ("%X");
2255*3d8817e4Smiod }
2256*3d8817e4Smiod }
2257*3d8817e4Smiod }
2258*3d8817e4Smiod
2259*3d8817e4Smiod
2260*3d8817e4Smiod void
pe_dll_generate_implib(def_file * def,const char * impfilename)2261*3d8817e4Smiod pe_dll_generate_implib (def_file *def, const char *impfilename)
2262*3d8817e4Smiod {
2263*3d8817e4Smiod int i;
2264*3d8817e4Smiod bfd *ar_head;
2265*3d8817e4Smiod bfd *ar_tail;
2266*3d8817e4Smiod bfd *outarch;
2267*3d8817e4Smiod bfd *head = 0;
2268*3d8817e4Smiod
2269*3d8817e4Smiod dll_filename = (def->name) ? def->name : dll_name;
2270*3d8817e4Smiod dll_symname = xstrdup (dll_filename);
2271*3d8817e4Smiod for (i = 0; dll_symname[i]; i++)
2272*3d8817e4Smiod if (!ISALNUM (dll_symname[i]))
2273*3d8817e4Smiod dll_symname[i] = '_';
2274*3d8817e4Smiod
2275*3d8817e4Smiod unlink_if_ordinary (impfilename);
2276*3d8817e4Smiod
2277*3d8817e4Smiod outarch = bfd_openw (impfilename, 0);
2278*3d8817e4Smiod
2279*3d8817e4Smiod if (!outarch)
2280*3d8817e4Smiod {
2281*3d8817e4Smiod /* xgettext:c-format */
2282*3d8817e4Smiod einfo (_("%XCan't open .lib file: %s\n"), impfilename);
2283*3d8817e4Smiod return;
2284*3d8817e4Smiod }
2285*3d8817e4Smiod
2286*3d8817e4Smiod /* xgettext:c-format */
2287*3d8817e4Smiod info_msg (_("Creating library file: %s\n"), impfilename);
2288*3d8817e4Smiod
2289*3d8817e4Smiod bfd_set_format (outarch, bfd_archive);
2290*3d8817e4Smiod outarch->has_armap = 1;
2291*3d8817e4Smiod
2292*3d8817e4Smiod /* Work out a reasonable size of things to put onto one line. */
2293*3d8817e4Smiod ar_head = make_head (outarch);
2294*3d8817e4Smiod
2295*3d8817e4Smiod for (i = 0; i < def->num_exports; i++)
2296*3d8817e4Smiod {
2297*3d8817e4Smiod /* The import library doesn't know about the internal name. */
2298*3d8817e4Smiod char *internal = def->exports[i].internal_name;
2299*3d8817e4Smiod bfd *n;
2300*3d8817e4Smiod
2301*3d8817e4Smiod /* Don't add PRIVATE entries to import lib. */
2302*3d8817e4Smiod if (pe_def_file->exports[i].flag_private)
2303*3d8817e4Smiod continue;
2304*3d8817e4Smiod def->exports[i].internal_name = def->exports[i].name;
2305*3d8817e4Smiod n = make_one (def->exports + i, outarch);
2306*3d8817e4Smiod n->next = head;
2307*3d8817e4Smiod head = n;
2308*3d8817e4Smiod def->exports[i].internal_name = internal;
2309*3d8817e4Smiod }
2310*3d8817e4Smiod
2311*3d8817e4Smiod ar_tail = make_tail (outarch);
2312*3d8817e4Smiod
2313*3d8817e4Smiod if (ar_head == NULL || ar_tail == NULL)
2314*3d8817e4Smiod return;
2315*3d8817e4Smiod
2316*3d8817e4Smiod /* Now stick them all into the archive. */
2317*3d8817e4Smiod ar_head->next = head;
2318*3d8817e4Smiod ar_tail->next = ar_head;
2319*3d8817e4Smiod head = ar_tail;
2320*3d8817e4Smiod
2321*3d8817e4Smiod if (! bfd_set_archive_head (outarch, head))
2322*3d8817e4Smiod einfo ("%Xbfd_set_archive_head: %E\n");
2323*3d8817e4Smiod
2324*3d8817e4Smiod if (! bfd_close (outarch))
2325*3d8817e4Smiod einfo ("%Xbfd_close %s: %E\n", impfilename);
2326*3d8817e4Smiod
2327*3d8817e4Smiod while (head != NULL)
2328*3d8817e4Smiod {
2329*3d8817e4Smiod bfd *n = head->next;
2330*3d8817e4Smiod bfd_close (head);
2331*3d8817e4Smiod head = n;
2332*3d8817e4Smiod }
2333*3d8817e4Smiod }
2334*3d8817e4Smiod
2335*3d8817e4Smiod static void
add_bfd_to_link(bfd * abfd,const char * name,struct bfd_link_info * link_info)2336*3d8817e4Smiod add_bfd_to_link (bfd *abfd, const char *name, struct bfd_link_info *link_info)
2337*3d8817e4Smiod {
2338*3d8817e4Smiod lang_input_statement_type *fake_file;
2339*3d8817e4Smiod
2340*3d8817e4Smiod fake_file = lang_add_input_file (name,
2341*3d8817e4Smiod lang_input_file_is_fake_enum,
2342*3d8817e4Smiod NULL);
2343*3d8817e4Smiod fake_file->the_bfd = abfd;
2344*3d8817e4Smiod ldlang_add_file (fake_file);
2345*3d8817e4Smiod
2346*3d8817e4Smiod if (!bfd_link_add_symbols (abfd, link_info))
2347*3d8817e4Smiod einfo ("%Xaddsym %s: %E\n", name);
2348*3d8817e4Smiod }
2349*3d8817e4Smiod
2350*3d8817e4Smiod void
pe_process_import_defs(bfd * output_bfd,struct bfd_link_info * link_info)2351*3d8817e4Smiod pe_process_import_defs (bfd *output_bfd, struct bfd_link_info *link_info)
2352*3d8817e4Smiod {
2353*3d8817e4Smiod def_file_module *module;
2354*3d8817e4Smiod
2355*3d8817e4Smiod pe_dll_id_target (bfd_get_target (output_bfd));
2356*3d8817e4Smiod
2357*3d8817e4Smiod if (!pe_def_file)
2358*3d8817e4Smiod return;
2359*3d8817e4Smiod
2360*3d8817e4Smiod for (module = pe_def_file->modules; module; module = module->next)
2361*3d8817e4Smiod {
2362*3d8817e4Smiod int i, do_this_dll;
2363*3d8817e4Smiod
2364*3d8817e4Smiod dll_filename = module->name;
2365*3d8817e4Smiod dll_symname = xstrdup (module->name);
2366*3d8817e4Smiod for (i = 0; dll_symname[i]; i++)
2367*3d8817e4Smiod if (!ISALNUM (dll_symname[i]))
2368*3d8817e4Smiod dll_symname[i] = '_';
2369*3d8817e4Smiod
2370*3d8817e4Smiod do_this_dll = 0;
2371*3d8817e4Smiod
2372*3d8817e4Smiod for (i = 0; i < pe_def_file->num_imports; i++)
2373*3d8817e4Smiod if (pe_def_file->imports[i].module == module)
2374*3d8817e4Smiod {
2375*3d8817e4Smiod def_file_export exp;
2376*3d8817e4Smiod struct bfd_link_hash_entry *blhe;
2377*3d8817e4Smiod int lead_at = (*pe_def_file->imports[i].internal_name == '@');
2378*3d8817e4Smiod /* See if we need this import. */
2379*3d8817e4Smiod size_t len = strlen (pe_def_file->imports[i].internal_name);
2380*3d8817e4Smiod char *name = xmalloc (len + 2 + 6);
2381*3d8817e4Smiod
2382*3d8817e4Smiod if (lead_at)
2383*3d8817e4Smiod sprintf (name, "%s%s", "",
2384*3d8817e4Smiod pe_def_file->imports[i].internal_name);
2385*3d8817e4Smiod else
2386*3d8817e4Smiod sprintf (name, "%s%s",U (""),
2387*3d8817e4Smiod pe_def_file->imports[i].internal_name);
2388*3d8817e4Smiod
2389*3d8817e4Smiod blhe = bfd_link_hash_lookup (link_info->hash, name,
2390*3d8817e4Smiod FALSE, FALSE, FALSE);
2391*3d8817e4Smiod
2392*3d8817e4Smiod if (!blhe || (blhe && blhe->type != bfd_link_hash_undefined))
2393*3d8817e4Smiod {
2394*3d8817e4Smiod if (lead_at)
2395*3d8817e4Smiod sprintf (name, "%s%s", U ("_imp_"),
2396*3d8817e4Smiod pe_def_file->imports[i].internal_name);
2397*3d8817e4Smiod else
2398*3d8817e4Smiod sprintf (name, "%s%s", U ("_imp__"),
2399*3d8817e4Smiod pe_def_file->imports[i].internal_name);
2400*3d8817e4Smiod
2401*3d8817e4Smiod blhe = bfd_link_hash_lookup (link_info->hash, name,
2402*3d8817e4Smiod FALSE, FALSE, FALSE);
2403*3d8817e4Smiod }
2404*3d8817e4Smiod free (name);
2405*3d8817e4Smiod
2406*3d8817e4Smiod if (blhe && blhe->type == bfd_link_hash_undefined)
2407*3d8817e4Smiod {
2408*3d8817e4Smiod bfd *one;
2409*3d8817e4Smiod /* We do. */
2410*3d8817e4Smiod if (!do_this_dll)
2411*3d8817e4Smiod {
2412*3d8817e4Smiod bfd *ar_head = make_head (output_bfd);
2413*3d8817e4Smiod add_bfd_to_link (ar_head, ar_head->filename, link_info);
2414*3d8817e4Smiod do_this_dll = 1;
2415*3d8817e4Smiod }
2416*3d8817e4Smiod exp.internal_name = pe_def_file->imports[i].internal_name;
2417*3d8817e4Smiod exp.name = pe_def_file->imports[i].name;
2418*3d8817e4Smiod exp.ordinal = pe_def_file->imports[i].ordinal;
2419*3d8817e4Smiod exp.hint = exp.ordinal >= 0 ? exp.ordinal : 0;
2420*3d8817e4Smiod exp.flag_private = 0;
2421*3d8817e4Smiod exp.flag_constant = 0;
2422*3d8817e4Smiod exp.flag_data = pe_def_file->imports[i].data;
2423*3d8817e4Smiod exp.flag_noname = exp.name ? 0 : 1;
2424*3d8817e4Smiod one = make_one (&exp, output_bfd);
2425*3d8817e4Smiod add_bfd_to_link (one, one->filename, link_info);
2426*3d8817e4Smiod }
2427*3d8817e4Smiod }
2428*3d8817e4Smiod if (do_this_dll)
2429*3d8817e4Smiod {
2430*3d8817e4Smiod bfd *ar_tail = make_tail (output_bfd);
2431*3d8817e4Smiod add_bfd_to_link (ar_tail, ar_tail->filename, link_info);
2432*3d8817e4Smiod }
2433*3d8817e4Smiod
2434*3d8817e4Smiod free (dll_symname);
2435*3d8817e4Smiod }
2436*3d8817e4Smiod }
2437*3d8817e4Smiod
2438*3d8817e4Smiod /* We were handed a *.DLL file. Parse it and turn it into a set of
2439*3d8817e4Smiod IMPORTS directives in the def file. Return TRUE if the file was
2440*3d8817e4Smiod handled, FALSE if not. */
2441*3d8817e4Smiod
2442*3d8817e4Smiod static unsigned int
pe_get16(bfd * abfd,int where)2443*3d8817e4Smiod pe_get16 (bfd *abfd, int where)
2444*3d8817e4Smiod {
2445*3d8817e4Smiod unsigned char b[2];
2446*3d8817e4Smiod
2447*3d8817e4Smiod bfd_seek (abfd, (file_ptr) where, SEEK_SET);
2448*3d8817e4Smiod bfd_bread (b, (bfd_size_type) 2, abfd);
2449*3d8817e4Smiod return b[0] + (b[1] << 8);
2450*3d8817e4Smiod }
2451*3d8817e4Smiod
2452*3d8817e4Smiod static unsigned int
pe_get32(bfd * abfd,int where)2453*3d8817e4Smiod pe_get32 (bfd *abfd, int where)
2454*3d8817e4Smiod {
2455*3d8817e4Smiod unsigned char b[4];
2456*3d8817e4Smiod
2457*3d8817e4Smiod bfd_seek (abfd, (file_ptr) where, SEEK_SET);
2458*3d8817e4Smiod bfd_bread (b, (bfd_size_type) 4, abfd);
2459*3d8817e4Smiod return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
2460*3d8817e4Smiod }
2461*3d8817e4Smiod
2462*3d8817e4Smiod static unsigned int
pe_as32(void * ptr)2463*3d8817e4Smiod pe_as32 (void *ptr)
2464*3d8817e4Smiod {
2465*3d8817e4Smiod unsigned char *b = ptr;
2466*3d8817e4Smiod
2467*3d8817e4Smiod return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
2468*3d8817e4Smiod }
2469*3d8817e4Smiod
2470*3d8817e4Smiod bfd_boolean
pe_implied_import_dll(const char * filename)2471*3d8817e4Smiod pe_implied_import_dll (const char *filename)
2472*3d8817e4Smiod {
2473*3d8817e4Smiod bfd *dll;
2474*3d8817e4Smiod unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
2475*3d8817e4Smiod unsigned long export_rva, export_size, nsections, secptr, expptr;
2476*3d8817e4Smiod unsigned long exp_funcbase;
2477*3d8817e4Smiod unsigned char *expdata;
2478*3d8817e4Smiod char *erva;
2479*3d8817e4Smiod unsigned long name_rvas, ordinals, nexp, ordbase;
2480*3d8817e4Smiod const char *dll_name;
2481*3d8817e4Smiod /* Initialization with start > end guarantees that is_data
2482*3d8817e4Smiod will not be set by mistake, and avoids compiler warning. */
2483*3d8817e4Smiod unsigned long data_start = 1;
2484*3d8817e4Smiod unsigned long data_end = 0;
2485*3d8817e4Smiod unsigned long rdata_start = 1;
2486*3d8817e4Smiod unsigned long rdata_end = 0;
2487*3d8817e4Smiod unsigned long bss_start = 1;
2488*3d8817e4Smiod unsigned long bss_end = 0;
2489*3d8817e4Smiod
2490*3d8817e4Smiod /* No, I can't use bfd here. kernel32.dll puts its export table in
2491*3d8817e4Smiod the middle of the .rdata section. */
2492*3d8817e4Smiod dll = bfd_openr (filename, pe_details->target_name);
2493*3d8817e4Smiod if (!dll)
2494*3d8817e4Smiod {
2495*3d8817e4Smiod einfo ("%Xopen %s: %E\n", filename);
2496*3d8817e4Smiod return FALSE;
2497*3d8817e4Smiod }
2498*3d8817e4Smiod
2499*3d8817e4Smiod /* PEI dlls seem to be bfd_objects. */
2500*3d8817e4Smiod if (!bfd_check_format (dll, bfd_object))
2501*3d8817e4Smiod {
2502*3d8817e4Smiod einfo ("%X%s: this doesn't appear to be a DLL\n", filename);
2503*3d8817e4Smiod return FALSE;
2504*3d8817e4Smiod }
2505*3d8817e4Smiod
2506*3d8817e4Smiod /* Get pe_header, optional header and numbers of export entries. */
2507*3d8817e4Smiod pe_header_offset = pe_get32 (dll, 0x3c);
2508*3d8817e4Smiod opthdr_ofs = pe_header_offset + 4 + 20;
2509*3d8817e4Smiod num_entries = pe_get32 (dll, opthdr_ofs + 92);
2510*3d8817e4Smiod
2511*3d8817e4Smiod if (num_entries < 1) /* No exports. */
2512*3d8817e4Smiod return FALSE;
2513*3d8817e4Smiod
2514*3d8817e4Smiod export_rva = pe_get32 (dll, opthdr_ofs + 96);
2515*3d8817e4Smiod export_size = pe_get32 (dll, opthdr_ofs + 100);
2516*3d8817e4Smiod nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
2517*3d8817e4Smiod secptr = (pe_header_offset + 4 + 20 +
2518*3d8817e4Smiod pe_get16 (dll, pe_header_offset + 4 + 16));
2519*3d8817e4Smiod expptr = 0;
2520*3d8817e4Smiod
2521*3d8817e4Smiod /* Get the rva and size of the export section. */
2522*3d8817e4Smiod for (i = 0; i < nsections; i++)
2523*3d8817e4Smiod {
2524*3d8817e4Smiod char sname[8];
2525*3d8817e4Smiod unsigned long secptr1 = secptr + 40 * i;
2526*3d8817e4Smiod unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
2527*3d8817e4Smiod unsigned long vsize = pe_get32 (dll, secptr1 + 16);
2528*3d8817e4Smiod unsigned long fptr = pe_get32 (dll, secptr1 + 20);
2529*3d8817e4Smiod
2530*3d8817e4Smiod bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
2531*3d8817e4Smiod bfd_bread (sname, (bfd_size_type) 8, dll);
2532*3d8817e4Smiod
2533*3d8817e4Smiod if (vaddr <= export_rva && vaddr + vsize > export_rva)
2534*3d8817e4Smiod {
2535*3d8817e4Smiod expptr = fptr + (export_rva - vaddr);
2536*3d8817e4Smiod if (export_rva + export_size > vaddr + vsize)
2537*3d8817e4Smiod export_size = vsize - (export_rva - vaddr);
2538*3d8817e4Smiod break;
2539*3d8817e4Smiod }
2540*3d8817e4Smiod }
2541*3d8817e4Smiod
2542*3d8817e4Smiod /* Scan sections and store the base and size of the
2543*3d8817e4Smiod data and bss segments in data/base_start/end. */
2544*3d8817e4Smiod for (i = 0; i < nsections; i++)
2545*3d8817e4Smiod {
2546*3d8817e4Smiod unsigned long secptr1 = secptr + 40 * i;
2547*3d8817e4Smiod unsigned long vsize = pe_get32 (dll, secptr1 + 8);
2548*3d8817e4Smiod unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
2549*3d8817e4Smiod unsigned long flags = pe_get32 (dll, secptr1 + 36);
2550*3d8817e4Smiod char sec_name[9];
2551*3d8817e4Smiod
2552*3d8817e4Smiod sec_name[8] = '\0';
2553*3d8817e4Smiod bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
2554*3d8817e4Smiod bfd_bread (sec_name, (bfd_size_type) 8, dll);
2555*3d8817e4Smiod
2556*3d8817e4Smiod if (strcmp(sec_name,".data") == 0)
2557*3d8817e4Smiod {
2558*3d8817e4Smiod data_start = vaddr;
2559*3d8817e4Smiod data_end = vaddr + vsize;
2560*3d8817e4Smiod
2561*3d8817e4Smiod if (pe_dll_extra_pe_debug)
2562*3d8817e4Smiod printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
2563*3d8817e4Smiod __FUNCTION__, sec_name, vaddr, vaddr + vsize, flags);
2564*3d8817e4Smiod }
2565*3d8817e4Smiod else if (strcmp(sec_name,".rdata") == 0)
2566*3d8817e4Smiod {
2567*3d8817e4Smiod rdata_start = vaddr;
2568*3d8817e4Smiod rdata_end = vaddr + vsize;
2569*3d8817e4Smiod
2570*3d8817e4Smiod if (pe_dll_extra_pe_debug)
2571*3d8817e4Smiod printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
2572*3d8817e4Smiod __FUNCTION__, sec_name, vaddr, vaddr + vsize, flags);
2573*3d8817e4Smiod }
2574*3d8817e4Smiod else if (strcmp (sec_name,".bss") == 0)
2575*3d8817e4Smiod {
2576*3d8817e4Smiod bss_start = vaddr;
2577*3d8817e4Smiod bss_end = vaddr + vsize;
2578*3d8817e4Smiod
2579*3d8817e4Smiod if (pe_dll_extra_pe_debug)
2580*3d8817e4Smiod printf ("%s %s: 0x%08lx-0x%08lx (0x%08lx)\n",
2581*3d8817e4Smiod __FUNCTION__, sec_name, vaddr, vaddr + vsize, flags);
2582*3d8817e4Smiod }
2583*3d8817e4Smiod }
2584*3d8817e4Smiod
2585*3d8817e4Smiod expdata = xmalloc (export_size);
2586*3d8817e4Smiod bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
2587*3d8817e4Smiod bfd_bread (expdata, (bfd_size_type) export_size, dll);
2588*3d8817e4Smiod erva = (char *) expdata - export_rva;
2589*3d8817e4Smiod
2590*3d8817e4Smiod if (pe_def_file == 0)
2591*3d8817e4Smiod pe_def_file = def_file_empty ();
2592*3d8817e4Smiod
2593*3d8817e4Smiod nexp = pe_as32 (expdata + 24);
2594*3d8817e4Smiod name_rvas = pe_as32 (expdata + 32);
2595*3d8817e4Smiod ordinals = pe_as32 (expdata + 36);
2596*3d8817e4Smiod ordbase = pe_as32 (expdata + 16);
2597*3d8817e4Smiod exp_funcbase = pe_as32 (expdata + 28);
2598*3d8817e4Smiod
2599*3d8817e4Smiod /* Use internal dll name instead of filename
2600*3d8817e4Smiod to enable symbolic dll linking. */
2601*3d8817e4Smiod dll_name = erva + pe_as32 (expdata + 12);
2602*3d8817e4Smiod
2603*3d8817e4Smiod /* Check to see if the dll has already been added to
2604*3d8817e4Smiod the definition list and if so return without error.
2605*3d8817e4Smiod This avoids multiple symbol definitions. */
2606*3d8817e4Smiod if (def_get_module (pe_def_file, dll_name))
2607*3d8817e4Smiod {
2608*3d8817e4Smiod if (pe_dll_extra_pe_debug)
2609*3d8817e4Smiod printf ("%s is already loaded\n", dll_name);
2610*3d8817e4Smiod return TRUE;
2611*3d8817e4Smiod }
2612*3d8817e4Smiod
2613*3d8817e4Smiod /* Iterate through the list of symbols. */
2614*3d8817e4Smiod for (i = 0; i < nexp; i++)
2615*3d8817e4Smiod {
2616*3d8817e4Smiod /* Pointer to the names vector. */
2617*3d8817e4Smiod unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
2618*3d8817e4Smiod def_file_import *imp;
2619*3d8817e4Smiod /* Pointer to the function address vector. */
2620*3d8817e4Smiod unsigned long func_rva = pe_as32 (erva + exp_funcbase + i * 4);
2621*3d8817e4Smiod int is_data = 0;
2622*3d8817e4Smiod
2623*3d8817e4Smiod /* Skip unwanted symbols, which are
2624*3d8817e4Smiod exported in buggy auto-import releases. */
2625*3d8817e4Smiod if (strncmp (erva + name_rva, "_nm_", 4) != 0)
2626*3d8817e4Smiod {
2627*3d8817e4Smiod /* is_data is true if the address is in the data, rdata or bss
2628*3d8817e4Smiod segment. */
2629*3d8817e4Smiod is_data =
2630*3d8817e4Smiod (func_rva >= data_start && func_rva < data_end)
2631*3d8817e4Smiod || (func_rva >= rdata_start && func_rva < rdata_end)
2632*3d8817e4Smiod || (func_rva >= bss_start && func_rva < bss_end);
2633*3d8817e4Smiod
2634*3d8817e4Smiod imp = def_file_add_import (pe_def_file, erva + name_rva,
2635*3d8817e4Smiod dll_name, i, 0);
2636*3d8817e4Smiod /* Mark symbol type. */
2637*3d8817e4Smiod imp->data = is_data;
2638*3d8817e4Smiod
2639*3d8817e4Smiod if (pe_dll_extra_pe_debug)
2640*3d8817e4Smiod printf ("%s dll-name: %s sym: %s addr: 0x%lx %s\n",
2641*3d8817e4Smiod __FUNCTION__, dll_name, erva + name_rva,
2642*3d8817e4Smiod func_rva, is_data ? "(data)" : "");
2643*3d8817e4Smiod }
2644*3d8817e4Smiod }
2645*3d8817e4Smiod
2646*3d8817e4Smiod return TRUE;
2647*3d8817e4Smiod }
2648*3d8817e4Smiod
2649*3d8817e4Smiod /* These are the main functions, called from the emulation. The first
2650*3d8817e4Smiod is called after the bfds are read, so we can guess at how much space
2651*3d8817e4Smiod we need. The second is called after everything is placed, so we
2652*3d8817e4Smiod can put the right values in place. */
2653*3d8817e4Smiod
2654*3d8817e4Smiod void
pe_dll_build_sections(bfd * abfd,struct bfd_link_info * info)2655*3d8817e4Smiod pe_dll_build_sections (bfd *abfd, struct bfd_link_info *info)
2656*3d8817e4Smiod {
2657*3d8817e4Smiod pe_dll_id_target (bfd_get_target (abfd));
2658*3d8817e4Smiod process_def_file (abfd, info);
2659*3d8817e4Smiod
2660*3d8817e4Smiod if (pe_def_file->num_exports == 0 && !info->shared)
2661*3d8817e4Smiod return;
2662*3d8817e4Smiod
2663*3d8817e4Smiod generate_edata (abfd, info);
2664*3d8817e4Smiod build_filler_bfd (1);
2665*3d8817e4Smiod }
2666*3d8817e4Smiod
2667*3d8817e4Smiod void
pe_exe_build_sections(bfd * abfd,struct bfd_link_info * info ATTRIBUTE_UNUSED)2668*3d8817e4Smiod pe_exe_build_sections (bfd *abfd, struct bfd_link_info *info ATTRIBUTE_UNUSED)
2669*3d8817e4Smiod {
2670*3d8817e4Smiod pe_dll_id_target (bfd_get_target (abfd));
2671*3d8817e4Smiod build_filler_bfd (0);
2672*3d8817e4Smiod }
2673*3d8817e4Smiod
2674*3d8817e4Smiod void
pe_dll_fill_sections(bfd * abfd,struct bfd_link_info * info)2675*3d8817e4Smiod pe_dll_fill_sections (bfd *abfd, struct bfd_link_info *info)
2676*3d8817e4Smiod {
2677*3d8817e4Smiod pe_dll_id_target (bfd_get_target (abfd));
2678*3d8817e4Smiod image_base = pe_data (abfd)->pe_opthdr.ImageBase;
2679*3d8817e4Smiod
2680*3d8817e4Smiod generate_reloc (abfd, info);
2681*3d8817e4Smiod if (reloc_sz > 0)
2682*3d8817e4Smiod {
2683*3d8817e4Smiod bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
2684*3d8817e4Smiod
2685*3d8817e4Smiod /* Resize the sections. */
2686*3d8817e4Smiod lang_reset_memory_regions ();
2687*3d8817e4Smiod lang_size_sections (NULL, TRUE);
2688*3d8817e4Smiod
2689*3d8817e4Smiod /* Redo special stuff. */
2690*3d8817e4Smiod ldemul_after_allocation ();
2691*3d8817e4Smiod
2692*3d8817e4Smiod /* Do the assignments again. */
2693*3d8817e4Smiod lang_do_assignments ();
2694*3d8817e4Smiod }
2695*3d8817e4Smiod
2696*3d8817e4Smiod fill_edata (abfd, info);
2697*3d8817e4Smiod
2698*3d8817e4Smiod if (info->shared && !info->pie)
2699*3d8817e4Smiod pe_data (abfd)->dll = 1;
2700*3d8817e4Smiod
2701*3d8817e4Smiod edata_s->contents = edata_d;
2702*3d8817e4Smiod reloc_s->contents = reloc_d;
2703*3d8817e4Smiod }
2704*3d8817e4Smiod
2705*3d8817e4Smiod void
pe_exe_fill_sections(bfd * abfd,struct bfd_link_info * info)2706*3d8817e4Smiod pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
2707*3d8817e4Smiod {
2708*3d8817e4Smiod pe_dll_id_target (bfd_get_target (abfd));
2709*3d8817e4Smiod image_base = pe_data (abfd)->pe_opthdr.ImageBase;
2710*3d8817e4Smiod
2711*3d8817e4Smiod generate_reloc (abfd, info);
2712*3d8817e4Smiod if (reloc_sz > 0)
2713*3d8817e4Smiod {
2714*3d8817e4Smiod bfd_set_section_size (filler_bfd, reloc_s, reloc_sz);
2715*3d8817e4Smiod
2716*3d8817e4Smiod /* Resize the sections. */
2717*3d8817e4Smiod lang_reset_memory_regions ();
2718*3d8817e4Smiod lang_size_sections (NULL, TRUE);
2719*3d8817e4Smiod
2720*3d8817e4Smiod /* Redo special stuff. */
2721*3d8817e4Smiod ldemul_after_allocation ();
2722*3d8817e4Smiod
2723*3d8817e4Smiod /* Do the assignments again. */
2724*3d8817e4Smiod lang_do_assignments ();
2725*3d8817e4Smiod }
2726*3d8817e4Smiod reloc_s->contents = reloc_d;
2727*3d8817e4Smiod }
2728