Commit | Line | Data |
---|---|---|
eb8061bf DD |
1 | /* Routines to help build PEI-format DLLs (Win32 etc) |
2 | Copyright (C) 1998 Free Software Foundation, Inc. | |
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 "ld.h" | |
28 | #include "ldexp.h" | |
29 | #include "ldlang.h" | |
30 | #include "ldwrite.h" | |
31 | #include "ldmisc.h" | |
32 | #include "ldgram.h" | |
33 | #include "ldmain.h" | |
34 | #include "coff/internal.h" | |
35 | #include "../bfd/libcoff.h" | |
36 | #include "deffile.h" | |
37 | ||
38 | /************************************************************************ | |
39 | ||
40 | This file turns a regular Windows PE image into a DLL. Because of | |
41 | the complexity of this operation, it has been broken down into a | |
42 | number of separate modules which are all called by the main function | |
43 | at the end of this file. This function is not re-entrant and is | |
44 | normally only called once, so static variables are used to reduce | |
45 | the number of parameters and return values required. | |
46 | ||
47 | See also: ld/emultempl/pe.em | |
48 | ||
49 | ************************************************************************/ | |
50 | ||
51 | /* from emultempl/pe.em */ | |
52 | ||
53 | extern def_file *pe_def_file; | |
54 | extern int pe_dll_export_everything; | |
55 | extern int pe_dll_do_default_excludes; | |
56 | extern int pe_dll_kill_ats; | |
57 | extern int pe_dll_stdcall_aliases; | |
58 | ||
59 | /************************************************************************ | |
60 | ||
61 | static variables and types | |
62 | ||
63 | ************************************************************************/ | |
64 | ||
65 | static bfd_vma image_base; | |
66 | ||
67 | static bfd *filler_bfd; | |
68 | static struct sec *edata_s, *reloc_s; | |
69 | static unsigned char *edata_d, *reloc_d; | |
70 | static int edata_sz, reloc_sz; | |
71 | ||
72 | /************************************************************************ | |
73 | ||
74 | Helper functions for qsort. Relocs must be sorted so that we can write | |
75 | them out by pages. | |
76 | ||
77 | ************************************************************************/ | |
78 | ||
79 | static int | |
80 | reloc_sort (va, vb) | |
81 | const void *va, *vb; | |
82 | { | |
83 | unsigned long a = *(unsigned long *) va; | |
84 | unsigned long b = *(unsigned long *) vb; | |
85 | return a - b; | |
86 | } | |
87 | ||
88 | static int | |
89 | pe_export_sort (va, vb) | |
90 | const void *va, *vb; | |
91 | { | |
92 | def_file_export *a = (def_file_export *) va; | |
93 | def_file_export *b = (def_file_export *) vb; | |
94 | return strcmp (a->name, b->name); | |
95 | } | |
96 | ||
97 | /************************************************************************ | |
98 | ||
99 | Read and process the .DEF file | |
100 | ||
101 | ************************************************************************/ | |
102 | ||
103 | /* These correspond to the entries in pe_def_file->exports[]. I use | |
104 | exported_symbol_sections[i] to tag whether or not the symbol was | |
105 | defined, since we can't export symbols we don't have. */ | |
106 | ||
107 | static bfd_vma *exported_symbol_offsets; | |
108 | static struct sec **exported_symbol_sections; | |
109 | ||
110 | static int export_table_size; | |
111 | static int count_exported; | |
112 | static int count_exported_byname; | |
113 | static int count_with_ordinals; | |
114 | static const char *dll_name; | |
115 | static int min_ordinal, max_ordinal; | |
116 | static int *exported_symbols; | |
117 | ||
118 | typedef struct exclude_list_struct | |
119 | { | |
120 | char *string; | |
121 | struct exclude_list_struct *next; | |
122 | } | |
123 | exclude_list_struct; | |
124 | static struct exclude_list_struct *excludes = 0; | |
125 | ||
126 | void | |
127 | pe_dll_add_excludes (new_excludes) | |
128 | const char *new_excludes; | |
129 | { | |
130 | char *local_copy; | |
131 | char *exclude_string; | |
132 | ||
133 | local_copy = xstrdup (new_excludes); | |
134 | ||
135 | exclude_string = strtok (local_copy, ",:"); | |
136 | for (; exclude_string; exclude_string = strtok (NULL, ",:")) | |
137 | { | |
138 | struct exclude_list_struct *new_exclude; | |
139 | ||
140 | new_exclude = ((struct exclude_list_struct *) | |
141 | xmalloc (sizeof (struct exclude_list_struct))); | |
142 | new_exclude->string = (char *) xmalloc (strlen (exclude_string) + 1); | |
143 | strcpy (new_exclude->string, exclude_string); | |
144 | new_exclude->next = excludes; | |
145 | excludes = new_exclude; | |
146 | } | |
147 | ||
148 | free (local_copy); | |
149 | } | |
150 | ||
151 | static int | |
152 | auto_export (d, n) | |
153 | def_file *d; | |
154 | const char *n; | |
155 | { | |
156 | int i; | |
157 | struct exclude_list_struct *ex; | |
158 | for (i = 0; i < d->num_exports; i++) | |
159 | if (strcmp (d->exports[i].name, n) == 0) | |
160 | return 0; | |
161 | if (pe_dll_do_default_excludes) | |
162 | { | |
163 | if (strcmp (n, "DllMain@12") == 0) | |
164 | return 0; | |
165 | if (strcmp (n, "DllEntryPoint@0") == 0) | |
166 | return 0; | |
167 | if (strcmp (n, "impure_ptr") == 0) | |
168 | return 0; | |
169 | } | |
170 | for (ex = excludes; ex; ex = ex->next) | |
171 | if (strcmp (n, ex->string) == 0) | |
172 | return 0; | |
173 | return 1; | |
174 | } | |
175 | ||
176 | static void | |
177 | process_def_file (abfd, info) | |
178 | bfd *abfd; | |
179 | struct bfd_link_info *info; | |
180 | { | |
181 | int i, j; | |
182 | struct bfd_link_hash_entry *blhe; | |
183 | bfd *b; | |
184 | struct sec *s; | |
91c7ad9a | 185 | def_file_export *e=0; |
eb8061bf DD |
186 | |
187 | if (!pe_def_file) | |
188 | pe_def_file = def_file_empty (); | |
189 | ||
190 | /* First, run around to all the objects looking for the .drectve | |
191 | sections, and push those into the def file too */ | |
192 | ||
193 | for (b = info->input_bfds; b; b = b->link_next) | |
194 | { | |
195 | s = bfd_get_section_by_name (b, ".drectve"); | |
196 | if (s) | |
197 | { | |
198 | int size = bfd_get_section_size_before_reloc (s); | |
199 | char *buf = xmalloc (size); | |
200 | bfd_get_section_contents (b, s, buf, 0, size); | |
201 | def_file_add_directive (pe_def_file, buf, size); | |
202 | free (buf); | |
203 | } | |
204 | } | |
205 | ||
206 | /* Now, maybe export everything else the default way */ | |
207 | ||
69c4fd86 | 208 | if (pe_dll_export_everything || pe_def_file->num_exports == 0) |
eb8061bf DD |
209 | { |
210 | for (b = info->input_bfds; b; b = b->link_next) | |
211 | { | |
212 | asymbol **symbols; | |
213 | int nsyms, symsize; | |
214 | ||
215 | symsize = bfd_get_symtab_upper_bound (b); | |
216 | symbols = (asymbol **) xmalloc (symsize); | |
217 | nsyms = bfd_canonicalize_symtab (b, symbols); | |
218 | ||
219 | for (j = 0; j < nsyms; j++) | |
220 | { | |
221 | if ((symbols[j]->flags & (BSF_FUNCTION | BSF_GLOBAL)) | |
222 | == (BSF_FUNCTION | BSF_GLOBAL)) | |
223 | { | |
224 | const char *sn = symbols[j]->name; | |
225 | if (*sn == '_') | |
226 | sn++; | |
227 | if (auto_export (pe_def_file, sn)) | |
228 | def_file_add_export (pe_def_file, sn, 0, -1); | |
229 | } | |
230 | } | |
231 | } | |
232 | } | |
233 | ||
234 | #undef NE | |
235 | #define NE pe_def_file->num_exports | |
eb8061bf DD |
236 | |
237 | /* Canonicalize the export list */ | |
238 | ||
239 | if (pe_dll_kill_ats) | |
240 | { | |
241 | for (i = 0; i < NE; i++) | |
242 | { | |
91c7ad9a | 243 | if (strchr (pe_def_file->exports[i].name, '@')) |
eb8061bf DD |
244 | { |
245 | /* This will preserve internal_name, which may have been pointing | |
246 | to the same memory as name, or might not have */ | |
91c7ad9a | 247 | char *tmp = xstrdup (pe_def_file->exports[i].name); |
eb8061bf | 248 | *(strchr (tmp, '@')) = 0; |
91c7ad9a | 249 | pe_def_file->exports[i].name = tmp; |
eb8061bf DD |
250 | } |
251 | } | |
252 | } | |
253 | ||
254 | if (pe_dll_stdcall_aliases) | |
255 | { | |
256 | for (i = 0; i < NE; i++) | |
257 | { | |
91c7ad9a | 258 | if (strchr (pe_def_file->exports[i].name, '@')) |
eb8061bf | 259 | { |
91c7ad9a | 260 | char *tmp = xstrdup (pe_def_file->exports[i].name); |
eb8061bf DD |
261 | *(strchr (tmp, '@')) = 0; |
262 | if (auto_export (pe_def_file, tmp)) | |
91c7ad9a DD |
263 | def_file_add_export (pe_def_file, tmp, |
264 | pe_def_file->exports[i].internal_name, -1); | |
eb8061bf DD |
265 | else |
266 | free (tmp); | |
267 | } | |
268 | } | |
269 | } | |
270 | ||
91c7ad9a DD |
271 | e = pe_def_file->exports; /* convenience */ |
272 | ||
eb8061bf DD |
273 | exported_symbol_offsets = (bfd_vma *) xmalloc (NE * sizeof (bfd_vma)); |
274 | exported_symbol_sections = (struct sec **) xmalloc (NE * sizeof (struct sec *)); | |
275 | ||
276 | memset (exported_symbol_sections, 0, NE * sizeof (struct sec *)); | |
277 | max_ordinal = 0; | |
278 | min_ordinal = 65536; | |
279 | count_exported = 0; | |
280 | count_exported_byname = 0; | |
281 | count_with_ordinals = 0; | |
282 | ||
283 | qsort (pe_def_file->exports, NE, sizeof (pe_def_file->exports[0]), pe_export_sort); | |
284 | for (i = 0, j = 0; i < NE; i++) | |
285 | { | |
286 | if (i > 0 && strcmp (e[i].name, e[i - 1].name) == 0) | |
287 | { | |
288 | /* This is a duplicate */ | |
289 | if (e[j - 1].ordinal != -1 | |
290 | && e[i].ordinal != -1 | |
291 | && e[j - 1].ordinal != e[i].ordinal) | |
292 | { | |
293 | /* xgettext:c-format */ | |
294 | einfo (_("%XError, duplicate EXPORT with oridinals: %s (%d vs %d)\n"), | |
295 | e[j - 1].name, e[j - 1].ordinal, e[i].ordinal); | |
296 | } | |
297 | else | |
298 | { | |
299 | /* xgettext:c-format */ | |
300 | einfo (_("Warning, duplicate EXPORT: %s\n"), | |
301 | e[j - 1].name); | |
302 | } | |
303 | if (e[i].ordinal) | |
304 | e[j - 1].ordinal = e[i].ordinal; | |
305 | e[j - 1].flag_private |= e[i].flag_private; | |
306 | e[j - 1].flag_constant |= e[i].flag_constant; | |
307 | e[j - 1].flag_noname |= e[i].flag_noname; | |
308 | e[j - 1].flag_data |= e[i].flag_data; | |
309 | } | |
310 | else | |
311 | { | |
312 | if (i != j) | |
313 | e[j] = e[i]; | |
314 | j++; | |
315 | } | |
316 | } | |
317 | pe_def_file->num_exports = j; /* == NE */ | |
318 | ||
319 | for (i = 0; i < NE; i++) | |
320 | { | |
321 | char *name = (char *) xmalloc (strlen (pe_def_file->exports[i].internal_name) + 2); | |
322 | *name = '_'; | |
323 | strcpy (name + 1, pe_def_file->exports[i].internal_name); | |
324 | ||
325 | blhe = bfd_link_hash_lookup (info->hash, | |
326 | name, | |
327 | false, false, true); | |
328 | ||
329 | if (blhe && (blhe->type == bfd_link_hash_defined)) | |
330 | { | |
331 | count_exported++; | |
332 | if (!pe_def_file->exports[i].flag_noname) | |
333 | count_exported_byname++; | |
334 | exported_symbol_offsets[i] = blhe->u.def.value; | |
335 | exported_symbol_sections[i] = blhe->u.def.section; | |
336 | if (pe_def_file->exports[i].ordinal != -1) | |
337 | { | |
338 | if (max_ordinal < pe_def_file->exports[i].ordinal) | |
339 | max_ordinal = pe_def_file->exports[i].ordinal; | |
340 | if (min_ordinal > pe_def_file->exports[i].ordinal) | |
341 | min_ordinal = pe_def_file->exports[i].ordinal; | |
342 | count_with_ordinals++; | |
343 | } | |
344 | } | |
345 | else if (blhe) | |
346 | { | |
347 | /* xgettext:c-format */ | |
348 | einfo (_("%XCannot export %s: symbol wrong type\n"), | |
349 | pe_def_file->exports[i].internal_name); | |
350 | } | |
351 | else | |
352 | { | |
353 | /* xgettext:c-format */ | |
354 | einfo (_("%XCannot export %s: symbol not found\n"), | |
355 | pe_def_file->exports[i].internal_name); | |
356 | } | |
357 | free(name); | |
358 | } | |
359 | ||
360 | #if 0 | |
361 | /* For now, just export all global functions. Read DEF files later */ | |
362 | for (i = 0; i < num_input_bfds; i++) | |
363 | { | |
364 | for (j = 0; j < symtab[i].nsyms; j++) | |
365 | { | |
366 | if ((symtab[i].symbols[j]->flags & (BSF_FUNCTION | BSF_GLOBAL)) | |
367 | == (BSF_FUNCTION | BSF_GLOBAL)) | |
368 | symtab[i].exported[j] = 1; | |
369 | } | |
370 | } | |
371 | #endif | |
372 | } | |
373 | ||
374 | /************************************************************************ | |
375 | ||
376 | Build the bfd that will contain .edata and .reloc sections | |
377 | ||
378 | ************************************************************************/ | |
379 | ||
380 | static void | |
381 | build_filler_bfd () | |
382 | { | |
383 | static lang_input_statement_type *filler_file; | |
384 | filler_file = lang_add_input_file ("dll stuff", | |
385 | lang_input_file_is_fake_enum, | |
386 | NULL); | |
387 | filler_file->the_bfd = filler_bfd = bfd_create ("dll stuff", output_bfd); | |
388 | if (filler_bfd == NULL | |
389 | || !bfd_set_arch_mach (filler_bfd, | |
390 | bfd_get_arch (output_bfd), | |
391 | bfd_get_mach (output_bfd))) | |
392 | { | |
393 | einfo ("%X%P: can not create BFD %E\n"); | |
394 | return; | |
395 | } | |
396 | ||
397 | edata_s = bfd_make_section_old_way (filler_bfd, ".edata"); | |
398 | if (edata_s == NULL | |
399 | || !bfd_set_section_flags (filler_bfd, edata_s, | |
400 | (SEC_HAS_CONTENTS | |
401 | | SEC_ALLOC | |
402 | | SEC_LOAD | |
403 | | SEC_KEEP | |
404 | | SEC_IN_MEMORY))) | |
405 | { | |
406 | einfo ("%X%P: can not create .edata section: %E\n"); | |
407 | return; | |
408 | } | |
409 | bfd_set_section_size (filler_bfd, edata_s, edata_sz); | |
410 | ||
411 | reloc_s = bfd_make_section_old_way (filler_bfd, ".reloc"); | |
412 | if (reloc_s == NULL | |
413 | || !bfd_set_section_flags (filler_bfd, reloc_s, | |
414 | (SEC_HAS_CONTENTS | |
415 | | SEC_ALLOC | |
416 | | SEC_LOAD | |
417 | | SEC_KEEP | |
418 | | SEC_IN_MEMORY))) | |
419 | { | |
420 | einfo ("%X%P: can not create .reloc section: %E\n"); | |
421 | return; | |
422 | } | |
423 | bfd_set_section_size (filler_bfd, reloc_s, 0); | |
424 | ||
425 | ldlang_add_file (filler_file); | |
426 | } | |
427 | ||
428 | /************************************************************************ | |
429 | ||
430 | Gather all the exported symbols and build the .edata section | |
431 | ||
432 | ************************************************************************/ | |
433 | ||
434 | static void | |
435 | generate_edata (abfd, info) | |
436 | bfd *abfd; | |
437 | struct bfd_link_info *info; | |
438 | { | |
439 | int i, j, next_ordinal; | |
440 | int name_table_size = 0; | |
441 | const char *dlnp; | |
442 | ||
443 | /* First, we need to know how many exported symbols there are, | |
444 | and what the range of ordinals is. */ | |
445 | ||
446 | if (pe_def_file->name) | |
447 | { | |
448 | dll_name = pe_def_file->name; | |
449 | } | |
450 | else | |
451 | { | |
452 | dll_name = abfd->filename; | |
453 | for (dlnp = dll_name; *dlnp; dlnp++) | |
454 | { | |
455 | if (*dlnp == '\\' || *dlnp == '/' || *dlnp == ':') | |
456 | dll_name = dlnp + 1; | |
457 | } | |
458 | } | |
459 | ||
460 | if (count_with_ordinals && max_ordinal > count_exported) | |
461 | { | |
462 | if (min_ordinal > max_ordinal - count_exported + 1) | |
463 | min_ordinal = max_ordinal - count_exported + 1; | |
464 | } | |
465 | else | |
466 | { | |
467 | min_ordinal = 1; | |
468 | max_ordinal = count_exported; | |
469 | } | |
470 | export_table_size = max_ordinal - min_ordinal + 1; | |
471 | ||
472 | exported_symbols = (int *) xmalloc (export_table_size * sizeof (int)); | |
473 | for (i = 0; i < export_table_size; i++) | |
474 | exported_symbols[i] = -1; | |
475 | ||
476 | /* Now we need to assign ordinals to those that don't have them */ | |
477 | for (i = 0; i < NE; i++) | |
478 | { | |
479 | if (exported_symbol_sections[i]) | |
480 | { | |
481 | if (pe_def_file->exports[i].ordinal != -1) | |
482 | { | |
483 | int ei = pe_def_file->exports[i].ordinal - min_ordinal; | |
484 | int pi = exported_symbols[ei]; | |
485 | if (pi != -1) | |
486 | { | |
487 | /* xgettext:c-format */ | |
488 | einfo (_("%XError, oridinal used twice: %d (%s vs %s)\n"), | |
489 | pe_def_file->exports[i].ordinal, | |
490 | pe_def_file->exports[i].name, | |
491 | pe_def_file->exports[pi].name); | |
492 | } | |
493 | exported_symbols[ei] = i; | |
494 | } | |
495 | name_table_size += strlen (pe_def_file->exports[i].name) + 1; | |
496 | } | |
497 | } | |
498 | ||
499 | next_ordinal = min_ordinal; | |
500 | for (i = 0; i < NE; i++) | |
501 | if (exported_symbol_sections[i]) | |
502 | if (pe_def_file->exports[i].ordinal == -1) | |
503 | { | |
504 | while (exported_symbols[next_ordinal - min_ordinal] != -1) | |
505 | next_ordinal++; | |
506 | exported_symbols[next_ordinal - min_ordinal] = i; | |
507 | pe_def_file->exports[i].ordinal = next_ordinal; | |
508 | } | |
509 | ||
510 | /* OK, now we can allocate some memory */ | |
511 | ||
512 | edata_sz = (40 /* directory */ | |
513 | + 4 * export_table_size /* addresses */ | |
514 | + 4 * count_exported_byname /* name ptrs */ | |
515 | + 2 * count_exported_byname /* ordinals */ | |
516 | + name_table_size + strlen (dll_name) + 1); | |
517 | } | |
518 | ||
519 | static void | |
520 | fill_edata (abfd, info) | |
521 | bfd *abfd; | |
522 | struct bfd_link_info *info; | |
523 | { | |
524 | int i; | |
525 | unsigned char *edirectory; | |
526 | unsigned long *eaddresses; | |
527 | unsigned long *enameptrs; | |
528 | unsigned short *eordinals; | |
529 | unsigned char *enamestr; | |
530 | ||
531 | edata_d = (unsigned char *) xmalloc (edata_sz); | |
532 | ||
533 | /* Note use of array pointer math here */ | |
534 | edirectory = edata_d; | |
535 | eaddresses = (unsigned long *) (edata_d + 40); | |
536 | enameptrs = eaddresses + export_table_size; | |
537 | eordinals = (unsigned short *) (enameptrs + count_exported_byname); | |
538 | enamestr = (char *) (eordinals + count_exported_byname); | |
539 | ||
540 | #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base) | |
541 | ||
542 | memset (edata_d, 0, 40); | |
543 | if (pe_def_file->version_major != -1) | |
544 | { | |
545 | bfd_put_16 (abfd, pe_def_file->version_major, edata_d + 8); | |
546 | bfd_put_16 (abfd, pe_def_file->version_minor, edata_d + 10); | |
547 | } | |
548 | bfd_put_32 (abfd, ERVA (enamestr), edata_d + 12); | |
549 | strcpy (enamestr, dll_name); | |
550 | enamestr += strlen (enamestr) + 1; | |
551 | bfd_put_32 (abfd, min_ordinal, edata_d + 16); | |
552 | bfd_put_32 (abfd, export_table_size, edata_d + 20); | |
553 | bfd_put_32 (abfd, count_exported_byname, edata_d + 24); | |
554 | bfd_put_32 (abfd, ERVA (eaddresses), edata_d + 28); | |
555 | bfd_put_32 (abfd, ERVA (enameptrs), edata_d + 32); | |
556 | bfd_put_32 (abfd, ERVA (eordinals), edata_d + 36); | |
557 | ||
558 | /* Ok, now for the filling in part */ | |
559 | for (i = 0; i < export_table_size; i++) | |
560 | { | |
561 | int s = exported_symbols[i]; | |
562 | if (s != -1) | |
563 | { | |
564 | struct sec *ssec = exported_symbol_sections[s]; | |
565 | unsigned long srva = (exported_symbol_offsets[s] | |
566 | + ssec->output_section->vma | |
567 | + ssec->output_offset); | |
568 | ||
569 | bfd_put_32 (abfd, srva - image_base, (void *) (eaddresses + i)); | |
570 | if (!pe_def_file->exports[s].flag_noname) | |
571 | { | |
572 | char *ename = pe_def_file->exports[s].name; | |
573 | bfd_put_32 (abfd, ERVA (enamestr), (void *) enameptrs); | |
574 | strcpy (enamestr, ename[0] == '_' ? ename + 1 : ename); | |
575 | enamestr += strlen (enamestr) + 1; | |
576 | bfd_put_16 (abfd, i, (void *) eordinals); | |
577 | enameptrs++; | |
578 | } | |
579 | eordinals++; | |
580 | } | |
581 | } | |
582 | } | |
583 | ||
584 | /************************************************************************ | |
585 | ||
586 | Gather all the relocations and build the .reloc section | |
587 | ||
588 | ************************************************************************/ | |
589 | ||
590 | static void | |
591 | generate_reloc (abfd, info) | |
592 | bfd *abfd; | |
593 | struct bfd_link_info *info; | |
594 | { | |
595 | ||
596 | /* for .reloc stuff */ | |
597 | bfd_vma *reloc_addresses; | |
598 | int total_relocs = 0; | |
599 | int i, j; | |
600 | unsigned long sec_page = (unsigned long) (-1); | |
601 | unsigned long page_ptr, page_count; | |
602 | int bi; | |
603 | bfd *b; | |
604 | struct sec *s; | |
605 | ||
606 | total_relocs = 0; | |
607 | for (b = info->input_bfds; b; b = b->link_next) | |
608 | for (s = b->sections; s; s = s->next) | |
609 | total_relocs += s->reloc_count; | |
610 | ||
611 | reloc_addresses = (unsigned long *) xmalloc (total_relocs * sizeof (unsigned long)); | |
612 | ||
613 | total_relocs = 0; | |
614 | bi = 0; | |
615 | for (bi = 0, b = info->input_bfds; b; bi++, b = b->link_next) | |
616 | { | |
617 | arelent **relocs; | |
618 | int relsize, nrelocs, i; | |
619 | ||
620 | for (s = b->sections; s; s = s->next) | |
621 | { | |
622 | unsigned long sec_vma = s->output_section->vma + s->output_offset; | |
623 | asymbol **symbols; | |
624 | int nsyms, symsize; | |
625 | ||
626 | symsize = bfd_get_symtab_upper_bound (b); | |
627 | symbols = (asymbol **) xmalloc (symsize); | |
628 | nsyms = bfd_canonicalize_symtab (b, symbols); | |
629 | ||
630 | relsize = bfd_get_reloc_upper_bound (b, s); | |
631 | relocs = (arelent **) xmalloc ((size_t) relsize); | |
632 | nrelocs = bfd_canonicalize_reloc (b, s, relocs, symbols); | |
633 | ||
634 | for (i = 0; i < nrelocs; i++) | |
635 | { | |
636 | if (!relocs[i]->howto->pc_relative) | |
637 | { | |
638 | switch (relocs[i]->howto->bitsize) | |
639 | { | |
640 | case 32: | |
641 | reloc_addresses[total_relocs++] = sec_vma + relocs[i]->address; | |
642 | break; | |
643 | default: | |
644 | /* xgettext:c-format */ | |
645 | einfo (_("%XError: %d-bit reloc in dll\n"), | |
646 | relocs[i]->howto->bitsize); | |
647 | break; | |
648 | } | |
649 | } | |
650 | } | |
651 | free (relocs); | |
652 | /* Warning: the allocated symbols are remembered in BFD and reused | |
653 | later, so don't free them! */ | |
654 | /* free(symbols); */ | |
655 | } | |
656 | } | |
657 | ||
658 | /* At this point, we have total_relocs relocation addresses in | |
659 | reloc_addresses, which are all suitable for the .reloc section. | |
660 | We must now create the new sections. */ | |
661 | ||
662 | qsort (reloc_addresses, total_relocs, sizeof (unsigned long), reloc_sort); | |
663 | ||
664 | for (i = 0; i < total_relocs; i++) | |
665 | { | |
666 | unsigned long this_page = (reloc_addresses[i] >> 12); | |
667 | if (this_page != sec_page) | |
668 | { | |
669 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */ | |
670 | reloc_sz += 8; | |
671 | sec_page = this_page; | |
672 | } | |
673 | reloc_sz += 2; | |
674 | } | |
675 | reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align */ | |
676 | ||
677 | reloc_d = (unsigned char *) xmalloc (reloc_sz); | |
678 | ||
679 | sec_page = (unsigned long) (-1); | |
680 | reloc_sz = 0; | |
681 | page_ptr = (unsigned long) (-1); | |
682 | page_count = 0; | |
683 | for (i = 0; i < total_relocs; i++) | |
684 | { | |
685 | unsigned long rva = reloc_addresses[i] - image_base; | |
686 | unsigned long this_page = (rva & ~0xfff); | |
687 | if (this_page != sec_page) | |
688 | { | |
689 | while (reloc_sz & 3) | |
690 | reloc_d[reloc_sz++] = 0; | |
691 | if (page_ptr != (unsigned long) (-1)) | |
692 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4); | |
693 | bfd_put_32 (abfd, this_page, reloc_d + reloc_sz); | |
694 | page_ptr = reloc_sz; | |
695 | reloc_sz += 8; | |
696 | sec_page = this_page; | |
697 | page_count = 0; | |
698 | } | |
699 | bfd_put_16 (abfd, (rva & 0xfff) + 0x3000, reloc_d + reloc_sz); | |
700 | reloc_sz += 2; | |
701 | page_count++; | |
702 | } | |
703 | while (reloc_sz & 3) | |
704 | reloc_d[reloc_sz++] = 0; | |
705 | if (page_ptr != (unsigned long) (-1)) | |
706 | bfd_put_32 (abfd, reloc_sz - page_ptr, reloc_d + page_ptr + 4); | |
707 | while (reloc_sz < reloc_s->_raw_size) | |
708 | reloc_d[reloc_sz++] = 0; | |
709 | } | |
710 | ||
711 | /************************************************************************ | |
712 | ||
713 | Given the exiting def_file structure, print out a .DEF file that | |
714 | corresponds to it. | |
715 | ||
716 | ************************************************************************/ | |
717 | ||
718 | static void | |
719 | quoteput (s, f, needs_quotes) | |
720 | char *s; | |
721 | FILE * f; | |
722 | int needs_quotes; | |
723 | { | |
724 | char *cp; | |
725 | for (cp = s; *cp; cp++) | |
726 | if (*cp == '\'' || *cp == '"' || *cp == '\\' || isspace (*cp) || *cp == ',' | |
727 | || *cp == ';') | |
728 | needs_quotes = 1; | |
729 | if (needs_quotes) | |
730 | { | |
731 | putc ('"', f); | |
732 | while (*s) | |
733 | { | |
734 | if (*s == '"' || *s == '\\') | |
735 | putc ('\\', f); | |
736 | putc (*s, f); | |
737 | s++; | |
738 | } | |
739 | putc ('"', f); | |
740 | } | |
741 | else | |
742 | fputs (s, f); | |
743 | } | |
744 | ||
745 | void | |
746 | pe_dll_generate_def_file (pe_out_def_filename) | |
747 | char *pe_out_def_filename; | |
748 | { | |
749 | int i; | |
750 | FILE *out = fopen (pe_out_def_filename, "w"); | |
751 | if (out == NULL) | |
752 | { | |
753 | /* xgettext:c-format */ | |
754 | einfo (_("%s: Can't open output def file %s\n"), | |
755 | program_name, pe_out_def_filename); | |
756 | } | |
757 | ||
758 | if (pe_def_file->name) | |
759 | { | |
760 | if (pe_def_file->is_dll) | |
761 | fprintf (out, "LIBRARY "); | |
762 | else | |
763 | fprintf (out, "NAME "); | |
764 | quoteput (pe_def_file->name, out, 1); | |
765 | if (pe_def_file->base_address != (bfd_vma) (-1)) | |
766 | fprintf (out, " BASE=0x%x", pe_def_file->base_address); | |
767 | fprintf (out, "\n"); | |
768 | } | |
769 | ||
770 | if (pe_def_file->description) | |
771 | { | |
772 | fprintf (out, "DESCRIPTION "); | |
773 | quoteput (pe_def_file->description, out, 1); | |
774 | fprintf (out, "\n"); | |
775 | } | |
776 | ||
777 | if (pe_def_file->version_minor) | |
778 | fprintf (out, "VERSION %d.%d\n", pe_def_file->version_major, | |
779 | pe_def_file->version_minor); | |
780 | else | |
781 | fprintf (out, "VERSION %d\n", pe_def_file->version_major); | |
782 | ||
783 | if (pe_def_file->stack_reserve != -1 || pe_def_file->heap_reserve != -1) | |
784 | fprintf (out, "\n"); | |
785 | ||
786 | if (pe_def_file->stack_commit != -1) | |
787 | fprintf (out, "STACKSIZE 0x%x,0x%x\n", | |
788 | pe_def_file->stack_reserve, pe_def_file->stack_commit); | |
789 | else if (pe_def_file->stack_reserve != -1) | |
790 | fprintf (out, "STACKSIZE 0x%x\n", pe_def_file->stack_reserve); | |
791 | if (pe_def_file->heap_commit != -1) | |
792 | fprintf (out, "HEAPSIZE 0x%x,0x%x\n", | |
793 | pe_def_file->heap_reserve, pe_def_file->heap_commit); | |
794 | else if (pe_def_file->heap_reserve != -1) | |
795 | fprintf (out, "HEAPSIZE 0x%x\n", pe_def_file->heap_reserve); | |
796 | ||
797 | if (pe_def_file->num_section_defs > 0) | |
798 | { | |
799 | fprintf (out, "\nSECTIONS\n\n"); | |
800 | for (i = 0; i < pe_def_file->num_section_defs; i++) | |
801 | { | |
802 | fprintf (out, " "); | |
803 | quoteput (pe_def_file->section_defs[i].name, out, 0); | |
804 | if (pe_def_file->section_defs[i].class) | |
805 | { | |
806 | fprintf (out, " CLASS "); | |
807 | quoteput (pe_def_file->section_defs[i].class, out, 0); | |
808 | } | |
809 | if (pe_def_file->section_defs[i].flag_read) | |
810 | fprintf (out, " READ"); | |
811 | if (pe_def_file->section_defs[i].flag_write) | |
812 | fprintf (out, " WRITE"); | |
813 | if (pe_def_file->section_defs[i].flag_execute) | |
814 | fprintf (out, " EXECUTE"); | |
815 | if (pe_def_file->section_defs[i].flag_shared) | |
816 | fprintf (out, " SHARED"); | |
817 | fprintf (out, "\n"); | |
818 | } | |
819 | } | |
820 | ||
821 | if (pe_def_file->num_exports > 0) | |
822 | { | |
823 | fprintf (out, "\nEXPORTS\n\n"); | |
824 | for (i = 0; i < pe_def_file->num_exports; i++) | |
825 | { | |
826 | def_file_export *e = pe_def_file->exports + i; | |
827 | fprintf (out, " "); | |
828 | quoteput (e->name, out, 0); | |
829 | if (e->internal_name && strcmp (e->internal_name, e->name)) | |
830 | { | |
831 | fprintf (out, " = "); | |
832 | quoteput (e->internal_name, out, 0); | |
833 | } | |
834 | if (e->ordinal != -1) | |
835 | fprintf (out, " @%d", e->ordinal); | |
836 | if (e->flag_private) | |
837 | fprintf (out, " PRIVATE"); | |
838 | if (e->flag_constant) | |
839 | fprintf (out, " CONSTANT"); | |
840 | if (e->flag_noname) | |
841 | fprintf (out, " NONAME"); | |
842 | if (e->flag_data) | |
843 | fprintf (out, " DATA"); | |
844 | ||
845 | fprintf (out, "\n"); | |
846 | } | |
847 | } | |
848 | ||
849 | if (pe_def_file->num_imports > 0) | |
850 | { | |
851 | fprintf (out, "\nIMPORTS\n\n"); | |
852 | for (i = 0; i < pe_def_file->num_imports; i++) | |
853 | { | |
854 | def_file_import *im = pe_def_file->imports + i; | |
855 | fprintf (out, " "); | |
856 | if (im->internal_name | |
857 | && (!im->name || strcmp (im->internal_name, im->name))) | |
858 | { | |
859 | quoteput (im->internal_name, out, 0); | |
860 | fprintf (out, " = "); | |
861 | } | |
862 | quoteput (im->module, out, 0); | |
863 | fprintf (out, "."); | |
864 | if (im->name) | |
865 | quoteput (im->name, out, 0); | |
866 | else | |
867 | fprintf (out, "%d", im->ordinal); | |
868 | fprintf (out, "\n"); | |
869 | } | |
870 | } | |
871 | ||
872 | if (fclose (out) == EOF) | |
873 | { | |
874 | /* xgettext:c-format */ | |
875 | einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename); | |
876 | } | |
877 | } | |
878 | ||
879 | /************************************************************************ | |
880 | ||
881 | These are the main functions, called from the emulation. The first | |
882 | is called after the bfds are read, so we can guess at how much space | |
883 | we need. The second is called after everything is placed, so we | |
884 | can put the right values in place. | |
885 | ||
886 | ************************************************************************/ | |
887 | ||
888 | void | |
889 | pe_dll_build_sections (abfd, info) | |
890 | bfd *abfd; | |
891 | struct bfd_link_info *info; | |
892 | { | |
893 | process_def_file (abfd, info); | |
894 | ||
895 | generate_edata (abfd, info); | |
896 | build_filler_bfd (); | |
897 | } | |
898 | ||
899 | void | |
900 | pe_dll_fill_sections (abfd, info) | |
901 | bfd *abfd; | |
902 | struct bfd_link_info *info; | |
903 | { | |
904 | image_base = pe_data (abfd)->pe_opthdr.ImageBase; | |
905 | ||
906 | generate_reloc (abfd, info); | |
907 | if (reloc_sz > 0) | |
908 | { | |
909 | bfd_set_section_size (filler_bfd, reloc_s, reloc_sz); | |
910 | ||
911 | /* Resize the sections. */ | |
912 | lang_size_sections (stat_ptr->head, abs_output_section, | |
913 | &stat_ptr->head, 0, (bfd_vma) 0, false); | |
914 | ||
915 | /* Redo special stuff. */ | |
916 | ldemul_after_allocation (); | |
917 | ||
918 | /* Do the assignments again. */ | |
919 | lang_do_assignments (stat_ptr->head, | |
920 | abs_output_section, | |
921 | (fill_type) 0, (bfd_vma) 0); | |
922 | } | |
923 | ||
924 | fill_edata (abfd, info); | |
925 | ||
926 | pe_data (abfd)->dll = 1; | |
927 | ||
928 | edata_s->contents = edata_d; | |
929 | reloc_s->contents = reloc_d; | |
930 | } |