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