1 /* Routines to help build PEI-format DLLs (Win32 etc)
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 Written by DJ Delorie <dj@cygnus.com>
5 This file is part of GLD, the Gnu Linker.
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)
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.
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
25 #include "libiberty.h"
38 #include "coff/internal.h"
39 #include "../bfd/libcoff.h"
43 /************************************************************************
45 This file turns a regular Windows PE image into a DLL. Because of
46 the complexity of this operation, it has been broken down into a
47 number of separate modules which are all called by the main function
48 at the end of this file. This function is not re-entrant and is
49 normally only called once, so static variables are used to reduce
50 the number of parameters and return values required.
52 See also: ld/emultempl/pe.em
54 ************************************************************************/
56 /* for emultempl/pe.em */
58 def_file
*pe_def_file
= 0;
59 int pe_dll_export_everything
= 0;
60 int pe_dll_do_default_excludes
= 1;
61 int pe_dll_kill_ats
= 0;
62 int pe_dll_stdcall_aliases
= 0;
63 int pe_dll_warn_dup_exports
= 0;
64 int pe_dll_compat_implib
= 0;
66 /************************************************************************
68 static variables and types
70 ************************************************************************/
72 static bfd_vma image_base
;
74 static bfd
*filler_bfd
;
75 static struct sec
*edata_s
, *reloc_s
;
76 static unsigned char *edata_d
, *reloc_d
;
77 static size_t edata_sz
, reloc_sz
;
82 unsigned int imagebase_reloc
;
88 #define PE_ARCH_i386 1
90 #define PE_ARCH_mips 3
93 static pe_details_type pe_detail_list
[] = {
105 16 /* R_SH_IMAGEBASE */,
126 { NULL
, NULL
, 0, 0, 0, 0 }
129 static pe_details_type
*pe_details
;
131 #define U(str) (pe_details->underscored ? "_" str : str)
134 pe_dll_id_target (target
)
138 for (i
=0; pe_detail_list
[i
].target_name
; i
++)
139 if (strcmp (pe_detail_list
[i
].target_name
, target
) == 0)
141 pe_details
= pe_detail_list
+i
;
144 einfo (_("%XUnsupported PEI architecture: %s\n"), target
);
148 /************************************************************************
150 Helper functions for qsort. Relocs must be sorted so that we can write
153 ************************************************************************/
165 bfd_vma a
= ((reloc_data_type
*) va
)->vma
;
166 bfd_vma b
= ((reloc_data_type
*) vb
)->vma
;
167 return (a
> b
) ? 1 : ((a
< b
) ? -1 : 0);
171 pe_export_sort (va
, vb
)
174 def_file_export
*a
= (def_file_export
*) va
;
175 def_file_export
*b
= (def_file_export
*) vb
;
176 return strcmp (a
->name
, b
->name
);
179 /************************************************************************
181 Read and process the .DEF file
183 ************************************************************************/
185 /* These correspond to the entries in pe_def_file->exports[]. I use
186 exported_symbol_sections[i] to tag whether or not the symbol was
187 defined, since we can't export symbols we don't have. */
189 static bfd_vma
*exported_symbol_offsets
;
190 static struct sec
**exported_symbol_sections
;
192 static int export_table_size
;
193 static int count_exported
;
194 static int count_exported_byname
;
195 static int count_with_ordinals
;
196 static const char *dll_name
;
197 static int min_ordinal
, max_ordinal
;
198 static int *exported_symbols
;
200 typedef struct exclude_list_struct
203 struct exclude_list_struct
*next
;
206 static struct exclude_list_struct
*excludes
= 0;
209 pe_dll_add_excludes (new_excludes
)
210 const char *new_excludes
;
213 char *exclude_string
;
215 local_copy
= xstrdup (new_excludes
);
217 exclude_string
= strtok (local_copy
, ",:");
218 for (; exclude_string
; exclude_string
= strtok (NULL
, ",:"))
220 struct exclude_list_struct
*new_exclude
;
222 new_exclude
= ((struct exclude_list_struct
*)
223 xmalloc (sizeof (struct exclude_list_struct
)));
224 new_exclude
->string
= (char *) xmalloc (strlen (exclude_string
) + 1);
225 strcpy (new_exclude
->string
, exclude_string
);
226 new_exclude
->next
= excludes
;
227 excludes
= new_exclude
;
239 struct exclude_list_struct
*ex
;
240 for (i
= 0; i
< d
->num_exports
; i
++)
241 if (strcmp (d
->exports
[i
].name
, n
) == 0)
243 if (pe_dll_do_default_excludes
)
245 if (strcmp (n
, "DllMain@12") == 0)
247 if (strcmp (n
, "DllEntryPoint@0") == 0)
249 if (strcmp (n
, "impure_ptr") == 0)
252 for (ex
= excludes
; ex
; ex
= ex
->next
)
253 if (strcmp (n
, ex
->string
) == 0)
259 process_def_file (abfd
, info
)
260 bfd
*abfd ATTRIBUTE_UNUSED
;
261 struct bfd_link_info
*info
;
264 struct bfd_link_hash_entry
*blhe
;
267 def_file_export
*e
=0;
270 pe_def_file
= def_file_empty ();
272 /* First, run around to all the objects looking for the .drectve
273 sections, and push those into the def file too */
275 for (b
= info
->input_bfds
; b
; b
= b
->link_next
)
277 s
= bfd_get_section_by_name (b
, ".drectve");
280 int size
= bfd_get_section_size_before_reloc (s
);
281 char *buf
= xmalloc (size
);
282 bfd_get_section_contents (b
, s
, buf
, 0, size
);
283 def_file_add_directive (pe_def_file
, buf
, size
);
288 /* Now, maybe export everything else the default way */
290 if (pe_dll_export_everything
|| pe_def_file
->num_exports
== 0)
292 for (b
= info
->input_bfds
; b
; b
= b
->link_next
)
297 symsize
= bfd_get_symtab_upper_bound (b
);
298 symbols
= (asymbol
**) xmalloc (symsize
);
299 nsyms
= bfd_canonicalize_symtab (b
, symbols
);
301 for (j
= 0; j
< nsyms
; j
++)
303 if ((symbols
[j
]->flags
& (BSF_FUNCTION
| BSF_GLOBAL
))
304 == (BSF_FUNCTION
| BSF_GLOBAL
))
306 const char *sn
= symbols
[j
]->name
;
309 if (auto_export (pe_def_file
, sn
))
310 def_file_add_export (pe_def_file
, sn
, 0, -1);
317 #define NE pe_def_file->num_exports
319 /* Canonicalize the export list */
323 for (i
= 0; i
< NE
; i
++)
325 if (strchr (pe_def_file
->exports
[i
].name
, '@'))
327 /* This will preserve internal_name, which may have been pointing
328 to the same memory as name, or might not have */
329 char *tmp
= xstrdup (pe_def_file
->exports
[i
].name
);
330 *(strchr (tmp
, '@')) = 0;
331 pe_def_file
->exports
[i
].name
= tmp
;
336 if (pe_dll_stdcall_aliases
)
338 for (i
= 0; i
< NE
; i
++)
340 if (strchr (pe_def_file
->exports
[i
].name
, '@'))
342 char *tmp
= xstrdup (pe_def_file
->exports
[i
].name
);
343 *(strchr (tmp
, '@')) = 0;
344 if (auto_export (pe_def_file
, tmp
))
345 def_file_add_export (pe_def_file
, tmp
,
346 pe_def_file
->exports
[i
].internal_name
, -1);
353 e
= pe_def_file
->exports
; /* convenience, but watch out for it changing */
355 exported_symbol_offsets
= (bfd_vma
*) xmalloc (NE
* sizeof (bfd_vma
));
356 exported_symbol_sections
= (struct sec
**) xmalloc (NE
* sizeof (struct sec
*));
358 memset (exported_symbol_sections
, 0, NE
* sizeof (struct sec
*));
362 count_exported_byname
= 0;
363 count_with_ordinals
= 0;
365 qsort (pe_def_file
->exports
, NE
, sizeof (pe_def_file
->exports
[0]), pe_export_sort
);
366 for (i
= 0, j
= 0; i
< NE
; i
++)
368 if (i
> 0 && strcmp (e
[i
].name
, e
[i
- 1].name
) == 0)
370 /* This is a duplicate. */
371 if (e
[j
- 1].ordinal
!= -1
372 && e
[i
].ordinal
!= -1
373 && e
[j
- 1].ordinal
!= e
[i
].ordinal
)
375 if (pe_dll_warn_dup_exports
)
376 /* xgettext:c-format */
377 einfo (_("%XError, duplicate EXPORT with oridinals: %s (%d vs %d)\n"),
378 e
[j
- 1].name
, e
[j
- 1].ordinal
, e
[i
].ordinal
);
382 if (pe_dll_warn_dup_exports
)
383 /* xgettext:c-format */
384 einfo (_("Warning, duplicate EXPORT: %s\n"),
388 e
[j
- 1].ordinal
= e
[i
].ordinal
;
389 e
[j
- 1].flag_private
|= e
[i
].flag_private
;
390 e
[j
- 1].flag_constant
|= e
[i
].flag_constant
;
391 e
[j
- 1].flag_noname
|= e
[i
].flag_noname
;
392 e
[j
- 1].flag_data
|= e
[i
].flag_data
;
401 pe_def_file
->num_exports
= j
; /* == NE */
403 for (i
= 0; i
< NE
; i
++)
405 char *name
= (char *) xmalloc (strlen (pe_def_file
->exports
[i
].internal_name
) + 2);
406 if (pe_details
->underscored
)
409 strcpy (name
+ 1, pe_def_file
->exports
[i
].internal_name
);
412 strcpy (name
, pe_def_file
->exports
[i
].internal_name
);
414 blhe
= bfd_link_hash_lookup (info
->hash
,
419 && (blhe
->type
== bfd_link_hash_defined
420 || (blhe
->type
== bfd_link_hash_common
)))
423 if (!pe_def_file
->exports
[i
].flag_noname
)
424 count_exported_byname
++;
426 /* Only fill in the sections. The actual offsets are computed
427 in fill_exported_offsets() after common symbols are laid
429 if (blhe
->type
== bfd_link_hash_defined
)
430 exported_symbol_sections
[i
] = blhe
->u
.def
.section
;
432 exported_symbol_sections
[i
] = blhe
->u
.c
.p
->section
;
434 if (pe_def_file
->exports
[i
].ordinal
!= -1)
436 if (max_ordinal
< pe_def_file
->exports
[i
].ordinal
)
437 max_ordinal
= pe_def_file
->exports
[i
].ordinal
;
438 if (min_ordinal
> pe_def_file
->exports
[i
].ordinal
)
439 min_ordinal
= pe_def_file
->exports
[i
].ordinal
;
440 count_with_ordinals
++;
443 else if (blhe
&& blhe
->type
== bfd_link_hash_undefined
)
445 /* xgettext:c-format */
446 einfo (_("%XCannot export %s: symbol not defined\n"),
447 pe_def_file
->exports
[i
].internal_name
);
451 /* xgettext:c-format */
452 einfo (_("%XCannot export %s: symbol wrong type (%d vs %d)\n"),
453 pe_def_file
->exports
[i
].internal_name
,
454 blhe
->type
, bfd_link_hash_defined
);
458 /* xgettext:c-format */
459 einfo (_("%XCannot export %s: symbol not found\n"),
460 pe_def_file
->exports
[i
].internal_name
);
466 /************************************************************************
468 Build the bfd that will contain .edata and .reloc sections
470 ************************************************************************/
473 build_filler_bfd (include_edata
)
476 lang_input_statement_type
*filler_file
;
477 filler_file
= lang_add_input_file ("dll stuff",
478 lang_input_file_is_fake_enum
,
480 filler_file
->the_bfd
= filler_bfd
= bfd_create ("dll stuff", output_bfd
);
481 if (filler_bfd
== NULL
482 || !bfd_set_arch_mach (filler_bfd
,
483 bfd_get_arch (output_bfd
),
484 bfd_get_mach (output_bfd
)))
486 einfo ("%X%P: can not create BFD %E\n");
492 edata_s
= bfd_make_section_old_way (filler_bfd
, ".edata");
494 || !bfd_set_section_flags (filler_bfd
, edata_s
,
501 einfo ("%X%P: can not create .edata section: %E\n");
504 bfd_set_section_size (filler_bfd
, edata_s
, edata_sz
);
507 reloc_s
= bfd_make_section_old_way (filler_bfd
, ".reloc");
509 || !bfd_set_section_flags (filler_bfd
, reloc_s
,
516 einfo ("%X%P: can not create .reloc section: %E\n");
519 bfd_set_section_size (filler_bfd
, reloc_s
, 0);
521 ldlang_add_file (filler_file
);
524 /************************************************************************
526 Gather all the exported symbols and build the .edata section
528 ************************************************************************/
531 generate_edata (abfd
, info
)
533 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
536 int name_table_size
= 0;
539 /* First, we need to know how many exported symbols there are,
540 and what the range of ordinals is. */
542 if (pe_def_file
->name
)
544 dll_name
= pe_def_file
->name
;
548 dll_name
= abfd
->filename
;
549 for (dlnp
= dll_name
; *dlnp
; dlnp
++)
551 if (*dlnp
== '\\' || *dlnp
== '/' || *dlnp
== ':')
556 if (count_with_ordinals
&& max_ordinal
> count_exported
)
558 if (min_ordinal
> max_ordinal
- count_exported
+ 1)
559 min_ordinal
= max_ordinal
- count_exported
+ 1;
564 max_ordinal
= count_exported
;
566 export_table_size
= max_ordinal
- min_ordinal
+ 1;
568 exported_symbols
= (int *) xmalloc (export_table_size
* sizeof (int));
569 for (i
= 0; i
< export_table_size
; i
++)
570 exported_symbols
[i
] = -1;
572 /* Now we need to assign ordinals to those that don't have them */
573 for (i
= 0; i
< NE
; i
++)
575 if (exported_symbol_sections
[i
])
577 if (pe_def_file
->exports
[i
].ordinal
!= -1)
579 int ei
= pe_def_file
->exports
[i
].ordinal
- min_ordinal
;
580 int pi
= exported_symbols
[ei
];
583 /* xgettext:c-format */
584 einfo (_("%XError, oridinal used twice: %d (%s vs %s)\n"),
585 pe_def_file
->exports
[i
].ordinal
,
586 pe_def_file
->exports
[i
].name
,
587 pe_def_file
->exports
[pi
].name
);
589 exported_symbols
[ei
] = i
;
591 name_table_size
+= strlen (pe_def_file
->exports
[i
].name
) + 1;
595 next_ordinal
= min_ordinal
;
596 for (i
= 0; i
< NE
; i
++)
597 if (exported_symbol_sections
[i
])
598 if (pe_def_file
->exports
[i
].ordinal
== -1)
600 while (exported_symbols
[next_ordinal
- min_ordinal
] != -1)
602 exported_symbols
[next_ordinal
- min_ordinal
] = i
;
603 pe_def_file
->exports
[i
].ordinal
= next_ordinal
;
606 /* OK, now we can allocate some memory */
608 edata_sz
= (40 /* directory */
609 + 4 * export_table_size
/* addresses */
610 + 4 * count_exported_byname
/* name ptrs */
611 + 2 * count_exported_byname
/* ordinals */
612 + name_table_size
+ strlen (dll_name
) + 1);
615 /* Fill the exported symbol offsets. The preliminary work has already
616 been done in process_def_file(). */
619 fill_exported_offsets (abfd
, info
)
620 bfd
*abfd ATTRIBUTE_UNUSED
;
621 struct bfd_link_info
*info
;
624 struct bfd_link_hash_entry
*blhe
;
626 for (i
= 0; i
< pe_def_file
->num_exports
; i
++)
628 char *name
= (char *) xmalloc (strlen (pe_def_file
->exports
[i
].internal_name
) + 2);
629 if (pe_details
->underscored
)
632 strcpy (name
+ 1, pe_def_file
->exports
[i
].internal_name
);
635 strcpy (name
, pe_def_file
->exports
[i
].internal_name
);
637 blhe
= bfd_link_hash_lookup (info
->hash
,
641 if (blhe
&& (blhe
->type
== bfd_link_hash_defined
))
643 exported_symbol_offsets
[i
] = blhe
->u
.def
.value
;
650 fill_edata (abfd
, info
)
652 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
655 unsigned char *edirectory
;
656 unsigned long *eaddresses
;
657 unsigned long *enameptrs
;
658 unsigned short *eordinals
;
659 unsigned char *enamestr
;
664 edata_d
= (unsigned char *) xmalloc (edata_sz
);
666 /* Note use of array pointer math here */
667 edirectory
= edata_d
;
668 eaddresses
= (unsigned long *) (edata_d
+ 40);
669 enameptrs
= eaddresses
+ export_table_size
;
670 eordinals
= (unsigned short *) (enameptrs
+ count_exported_byname
);
671 enamestr
= (char *) (eordinals
+ count_exported_byname
);
673 #define ERVA(ptr) (((unsigned char *)(ptr) - edata_d) + edata_s->output_section->vma - image_base)
675 memset (edata_d
, 0, 40);
676 bfd_put_32 (abfd
, now
, edata_d
+ 4);
677 if (pe_def_file
->version_major
!= -1)
679 bfd_put_16 (abfd
, pe_def_file
->version_major
, edata_d
+ 8);
680 bfd_put_16 (abfd
, pe_def_file
->version_minor
, edata_d
+ 10);
682 bfd_put_32 (abfd
, ERVA (enamestr
), edata_d
+ 12);
683 strcpy (enamestr
, dll_name
);
684 enamestr
+= strlen (enamestr
) + 1;
685 bfd_put_32 (abfd
, min_ordinal
, edata_d
+ 16);
686 bfd_put_32 (abfd
, export_table_size
, edata_d
+ 20);
687 bfd_put_32 (abfd
, count_exported_byname
, edata_d
+ 24);
688 bfd_put_32 (abfd
, ERVA (eaddresses
), edata_d
+ 28);
689 bfd_put_32 (abfd
, ERVA (enameptrs
), edata_d
+ 32);
690 bfd_put_32 (abfd
, ERVA (eordinals
), edata_d
+ 36);
692 fill_exported_offsets (abfd
, info
);
694 /* Ok, now for the filling in part */
696 for (i
= 0; i
< export_table_size
; i
++)
698 int s
= exported_symbols
[i
];
701 struct sec
*ssec
= exported_symbol_sections
[s
];
702 unsigned long srva
= (exported_symbol_offsets
[s
]
703 + ssec
->output_section
->vma
704 + ssec
->output_offset
);
706 bfd_put_32 (abfd
, srva
- image_base
, (void *) (eaddresses
+ i
));
707 if (!pe_def_file
->exports
[s
].flag_noname
)
709 char *ename
= pe_def_file
->exports
[s
].name
;
710 bfd_put_32 (abfd
, ERVA (enamestr
), (void *) enameptrs
);
711 strcpy (enamestr
, ename
);
712 enamestr
+= strlen (enamestr
) + 1;
713 bfd_put_16 (abfd
, i
, (void *) eordinals
);
715 pe_def_file
->exports
[s
].hint
= hint
++;
722 /************************************************************************
724 Gather all the relocations and build the .reloc section
726 ************************************************************************/
729 generate_reloc (abfd
, info
)
731 struct bfd_link_info
*info
;
734 /* for .reloc stuff */
735 reloc_data_type
*reloc_data
;
736 int total_relocs
= 0;
738 unsigned long sec_page
= (unsigned long) (-1);
739 unsigned long page_ptr
, page_count
;
745 for (b
= info
->input_bfds
; b
; b
= b
->link_next
)
746 for (s
= b
->sections
; s
; s
= s
->next
)
747 total_relocs
+= s
->reloc_count
;
749 reloc_data
= (reloc_data_type
*) xmalloc (total_relocs
* sizeof (reloc_data_type
));
753 for (bi
= 0, b
= info
->input_bfds
; b
; bi
++, b
= b
->link_next
)
756 int relsize
, nrelocs
, i
;
758 for (s
= b
->sections
; s
; s
= s
->next
)
760 unsigned long sec_vma
= s
->output_section
->vma
+ s
->output_offset
;
764 /* if it's not loaded, we don't need to relocate it this way */
765 if (!(s
->output_section
->flags
& SEC_LOAD
))
768 /* I don't know why there would be a reloc for these, but I've
769 seen it happen - DJ */
770 if (s
->output_section
== &bfd_abs_section
)
773 if (s
->output_section
->vma
== 0)
775 /* Huh? Shouldn't happen, but punt if it does */
776 einfo ("DJ: zero vma section reloc detected: `%s' #%d f=%d\n",
777 s
->output_section
->name
, s
->output_section
->index
,
778 s
->output_section
->flags
);
782 symsize
= bfd_get_symtab_upper_bound (b
);
783 symbols
= (asymbol
**) xmalloc (symsize
);
784 nsyms
= bfd_canonicalize_symtab (b
, symbols
);
786 relsize
= bfd_get_reloc_upper_bound (b
, s
);
787 relocs
= (arelent
**) xmalloc ((size_t) relsize
);
788 nrelocs
= bfd_canonicalize_reloc (b
, s
, relocs
, symbols
);
790 for (i
= 0; i
< nrelocs
; i
++)
792 if (!relocs
[i
]->howto
->pc_relative
793 && relocs
[i
]->howto
->type
!= pe_details
->imagebase_reloc
)
796 struct symbol_cache_entry
*sym
= *relocs
[i
]->sym_ptr_ptr
;
797 sym_vma
= (relocs
[i
]->addend
800 + sym
->section
->output_offset
801 + sym
->section
->output_section
->vma
);
802 reloc_data
[total_relocs
].vma
= sec_vma
+ relocs
[i
]->address
;
804 #define BITS_AND_SHIFT(bits, shift) (bits * 1000 | shift)
806 switch BITS_AND_SHIFT (relocs
[i
]->howto
->bitsize
,
807 relocs
[i
]->howto
->rightshift
)
809 case BITS_AND_SHIFT (32, 0):
810 reloc_data
[total_relocs
].type
= 3;
813 case BITS_AND_SHIFT (16, 0):
814 reloc_data
[total_relocs
].type
= 2;
817 case BITS_AND_SHIFT (16, 16):
818 reloc_data
[total_relocs
].type
= 4;
819 /* FIXME: we can't know the symbol's right value yet,
820 but we probably can safely assume that CE will relocate
821 us in 64k blocks, so leaving it zero is safe. */
822 reloc_data
[total_relocs
].extra
= 0;
825 case BITS_AND_SHIFT (26, 2):
826 reloc_data
[total_relocs
].type
= 5;
830 /* xgettext:c-format */
831 einfo (_("%XError: %d-bit reloc in dll\n"),
832 relocs
[i
]->howto
->bitsize
);
838 /* Warning: the allocated symbols are remembered in BFD and reused
839 later, so don't free them! */
840 /* free (symbols); */
844 /* At this point, we have total_relocs relocation addresses in
845 reloc_addresses, which are all suitable for the .reloc section.
846 We must now create the new sections. */
848 qsort (reloc_data
, total_relocs
, sizeof (*reloc_data
), reloc_sort
);
850 for (i
= 0; i
< total_relocs
; i
++)
852 unsigned long this_page
= (reloc_data
[i
].vma
>> 12);
854 if (this_page
!= sec_page
)
856 reloc_sz
= (reloc_sz
+ 3) & ~3; /* 4-byte align */
858 sec_page
= this_page
;
863 if (reloc_data
[i
].type
== 4)
866 reloc_sz
= (reloc_sz
+ 3) & ~3; /* 4-byte align */
868 reloc_d
= (unsigned char *) xmalloc (reloc_sz
);
870 sec_page
= (unsigned long) (-1);
872 page_ptr
= (unsigned long) (-1);
874 for (i
= 0; i
< total_relocs
; i
++)
876 unsigned long rva
= reloc_data
[i
].vma
- image_base
;
877 unsigned long this_page
= (rva
& ~0xfff);
878 if (this_page
!= sec_page
)
881 reloc_d
[reloc_sz
++] = 0;
882 if (page_ptr
!= (unsigned long) (-1))
883 bfd_put_32 (abfd
, reloc_sz
- page_ptr
, reloc_d
+ page_ptr
+ 4);
884 bfd_put_32 (abfd
, this_page
, reloc_d
+ reloc_sz
);
887 sec_page
= this_page
;
890 bfd_put_16 (abfd
, (rva
& 0xfff) + (reloc_data
[i
].type
<<12),
893 if (reloc_data
[i
].type
== 4)
895 bfd_put_16 (abfd
, reloc_data
[i
].extra
, reloc_d
+ reloc_sz
);
901 reloc_d
[reloc_sz
++] = 0;
902 if (page_ptr
!= (unsigned long) (-1))
903 bfd_put_32 (abfd
, reloc_sz
- page_ptr
, reloc_d
+ page_ptr
+ 4);
904 while (reloc_sz
< reloc_s
->_raw_size
)
905 reloc_d
[reloc_sz
++] = 0;
908 /************************************************************************
910 Given the exiting def_file structure, print out a .DEF file that
913 ************************************************************************/
916 quoteput (s
, f
, needs_quotes
)
922 for (cp
= s
; *cp
; cp
++)
926 || isspace ((unsigned char) *cp
)
935 if (*s
== '"' || *s
== '\\')
947 pe_dll_generate_def_file (pe_out_def_filename
)
948 const char *pe_out_def_filename
;
951 FILE *out
= fopen (pe_out_def_filename
, "w");
954 /* xgettext:c-format */
955 einfo (_("%s: Can't open output def file %s\n"),
956 program_name
, pe_out_def_filename
);
961 if (pe_def_file
->name
)
963 if (pe_def_file
->is_dll
)
964 fprintf (out
, "LIBRARY ");
966 fprintf (out
, "NAME ");
967 quoteput (pe_def_file
->name
, out
, 1);
968 if (pe_data (output_bfd
)->pe_opthdr
.ImageBase
)
969 fprintf (out
, " BASE=0x%lx",
970 (unsigned long) pe_data (output_bfd
)->pe_opthdr
.ImageBase
);
974 if (pe_def_file
->description
)
976 fprintf (out
, "DESCRIPTION ");
977 quoteput (pe_def_file
->description
, out
, 1);
981 if (pe_def_file
->version_minor
!= -1)
982 fprintf (out
, "VERSION %d.%d\n", pe_def_file
->version_major
,
983 pe_def_file
->version_minor
);
984 else if (pe_def_file
->version_major
!= -1)
985 fprintf (out
, "VERSION %d\n", pe_def_file
->version_major
);
987 if (pe_def_file
->stack_reserve
!= -1 || pe_def_file
->heap_reserve
!= -1)
990 if (pe_def_file
->stack_commit
!= -1)
991 fprintf (out
, "STACKSIZE 0x%x,0x%x\n",
992 pe_def_file
->stack_reserve
, pe_def_file
->stack_commit
);
993 else if (pe_def_file
->stack_reserve
!= -1)
994 fprintf (out
, "STACKSIZE 0x%x\n", pe_def_file
->stack_reserve
);
995 if (pe_def_file
->heap_commit
!= -1)
996 fprintf (out
, "HEAPSIZE 0x%x,0x%x\n",
997 pe_def_file
->heap_reserve
, pe_def_file
->heap_commit
);
998 else if (pe_def_file
->heap_reserve
!= -1)
999 fprintf (out
, "HEAPSIZE 0x%x\n", pe_def_file
->heap_reserve
);
1001 if (pe_def_file
->num_section_defs
> 0)
1003 fprintf (out
, "\nSECTIONS\n\n");
1004 for (i
= 0; i
< pe_def_file
->num_section_defs
; i
++)
1007 quoteput (pe_def_file
->section_defs
[i
].name
, out
, 0);
1008 if (pe_def_file
->section_defs
[i
].class)
1010 fprintf (out
, " CLASS ");
1011 quoteput (pe_def_file
->section_defs
[i
].class, out
, 0);
1013 if (pe_def_file
->section_defs
[i
].flag_read
)
1014 fprintf (out
, " READ");
1015 if (pe_def_file
->section_defs
[i
].flag_write
)
1016 fprintf (out
, " WRITE");
1017 if (pe_def_file
->section_defs
[i
].flag_execute
)
1018 fprintf (out
, " EXECUTE");
1019 if (pe_def_file
->section_defs
[i
].flag_shared
)
1020 fprintf (out
, " SHARED");
1021 fprintf (out
, "\n");
1025 if (pe_def_file
->num_exports
> 0)
1027 fprintf (out
, "\nEXPORTS\n\n");
1028 for (i
= 0; i
< pe_def_file
->num_exports
; i
++)
1030 def_file_export
*e
= pe_def_file
->exports
+ i
;
1032 quoteput (e
->name
, out
, 0);
1033 if (e
->internal_name
&& strcmp (e
->internal_name
, e
->name
))
1035 fprintf (out
, " = ");
1036 quoteput (e
->internal_name
, out
, 0);
1038 if (e
->ordinal
!= -1)
1039 fprintf (out
, " @%d", e
->ordinal
);
1040 if (e
->flag_private
)
1041 fprintf (out
, " PRIVATE");
1042 if (e
->flag_constant
)
1043 fprintf (out
, " CONSTANT");
1045 fprintf (out
, " NONAME");
1047 fprintf (out
, " DATA");
1049 fprintf (out
, "\n");
1053 if (pe_def_file
->num_imports
> 0)
1055 fprintf (out
, "\nIMPORTS\n\n");
1056 for (i
= 0; i
< pe_def_file
->num_imports
; i
++)
1058 def_file_import
*im
= pe_def_file
->imports
+ i
;
1060 if (im
->internal_name
1061 && (!im
->name
|| strcmp (im
->internal_name
, im
->name
)))
1063 quoteput (im
->internal_name
, out
, 0);
1064 fprintf (out
, " = ");
1066 quoteput (im
->module
->name
, out
, 0);
1069 quoteput (im
->name
, out
, 0);
1071 fprintf (out
, "%d", im
->ordinal
);
1072 fprintf (out
, "\n");
1077 fprintf (out
, _("; no contents available\n"));
1079 if (fclose (out
) == EOF
)
1081 /* xgettext:c-format */
1082 einfo (_("%P: Error closing file `%s'\n"), pe_out_def_filename
);
1086 /************************************************************************
1088 Generate the import library
1090 ************************************************************************/
1092 static asymbol
**symtab
;
1095 static const char *dll_filename
;
1096 static char *dll_symname
;
1098 #define UNDSEC (asection *) &bfd_und_section
1101 quick_section(abfd
, name
, flags
, align
)
1110 sec
= bfd_make_section_old_way (abfd
, name
);
1111 bfd_set_section_flags (abfd
, sec
, flags
1116 bfd_set_section_alignment (abfd
, sec
, align
);
1117 /* remember to undo this before trying to link internally! */
1118 sec
->output_section
= sec
;
1120 sym
= bfd_make_empty_symbol (abfd
);
1121 symtab
[symptr
++] = sym
;
1122 sym
->name
= sec
->name
;
1124 sym
->flags
= BSF_LOCAL
;
1131 quick_symbol (abfd
, n1
, n2
, n3
, sec
, flags
, addr
)
1141 char *name
= (char *) xmalloc (strlen (n1
) + strlen (n2
) + strlen (n3
) + 1);
1145 sym
= bfd_make_empty_symbol (abfd
);
1150 symtab
[symptr
++] = sym
;
1153 static arelent
*reltab
= 0;
1154 static int relcount
= 0, relsize
= 0;
1157 quick_reloc (abfd
, address
, which_howto
, symidx
)
1163 if (relcount
>= (relsize
-1))
1167 reltab
= (arelent
*) xrealloc (reltab
, relsize
* sizeof (arelent
));
1169 reltab
= (arelent
*) xmalloc (relsize
* sizeof (arelent
));
1171 reltab
[relcount
].address
= address
;
1172 reltab
[relcount
].addend
= 0;
1173 reltab
[relcount
].howto
= bfd_reloc_type_lookup (abfd
, which_howto
);
1174 reltab
[relcount
].sym_ptr_ptr
= symtab
+ symidx
;
1179 save_relocs (asection
*sec
)
1182 sec
->relocation
= reltab
;
1183 sec
->reloc_count
= relcount
;
1184 sec
->orelocation
= (arelent
**) xmalloc ((relcount
+1) * sizeof (arelent
*));
1185 for (i
=0; i
<relcount
; i
++)
1186 sec
->orelocation
[i
] = sec
->relocation
+ i
;
1187 sec
->orelocation
[relcount
] = 0;
1188 sec
->flags
|= SEC_RELOC
;
1190 relcount
= relsize
= 0;
1195 * .global __head_my_dll
1200 * .rva __my_dll_iname
1216 asection
*id2
, *id5
, *id4
;
1217 unsigned char *d2
, *d5
, *d4
;
1221 oname
= (char *) xmalloc (20);
1222 sprintf (oname
, "d%06d.o", tmp_seq
);
1225 abfd
= bfd_create (oname
, parent
);
1226 bfd_find_target (pe_details
->object_target
, abfd
);
1227 bfd_make_writable (abfd
);
1229 bfd_set_format (abfd
, bfd_object
);
1230 bfd_set_arch_mach (abfd
, pe_details
->bfd_arch
, 0);
1233 symtab
= (asymbol
**) xmalloc (6 * sizeof (asymbol
*));
1234 id2
= quick_section (abfd
, ".idata$2", SEC_HAS_CONTENTS
, 2);
1235 id5
= quick_section (abfd
, ".idata$5", SEC_HAS_CONTENTS
, 2);
1236 id4
= quick_section (abfd
, ".idata$4", SEC_HAS_CONTENTS
, 2);
1237 quick_symbol (abfd
, U("_head_"), dll_symname
, "", id2
, BSF_GLOBAL
, 0);
1238 quick_symbol (abfd
, U(""), dll_symname
, "_iname", UNDSEC
, BSF_GLOBAL
, 0);
1240 /* OK, pay attention here. I got confused myself looking back at
1241 it. We create a four-byte section to mark the beginning of the
1242 list, and we include an offset of 4 in the section, so that the
1243 pointer to the list points to the *end* of this section, which is
1244 the start of the list of sections from other objects. */
1246 bfd_set_section_size (abfd
, id2
, 20);
1247 d2
= (unsigned char *) xmalloc (20);
1250 d2
[0] = d2
[16] = 4; /* reloc addend */
1251 quick_reloc (abfd
, 0, BFD_RELOC_RVA
, 2);
1252 quick_reloc (abfd
, 12, BFD_RELOC_RVA
, 4);
1253 quick_reloc (abfd
, 16, BFD_RELOC_RVA
, 1);
1256 bfd_set_section_size (abfd
, id5
, 4);
1257 d5
= (unsigned char *) xmalloc (4);
1261 bfd_set_section_size (abfd
, id4
, 4);
1262 d4
= (unsigned char *) xmalloc (4);
1266 bfd_set_symtab (abfd
, symtab
, symptr
);
1268 bfd_set_section_contents (abfd
, id2
, d2
, 0, 20);
1269 bfd_set_section_contents (abfd
, id5
, d5
, 0, 4);
1270 bfd_set_section_contents (abfd
, id4
, d4
, 0, 4);
1272 bfd_make_readable (abfd
);
1282 * .global __my_dll_iname
1291 asection
*id4
, *id5
, *id7
;
1292 unsigned char *d4
, *d5
, *d7
;
1297 oname
= (char *) xmalloc (20);
1298 sprintf (oname
, "d%06d.o", tmp_seq
);
1301 abfd
= bfd_create (oname
, parent
);
1302 bfd_find_target (pe_details
->object_target
, abfd
);
1303 bfd_make_writable (abfd
);
1305 bfd_set_format (abfd
, bfd_object
);
1306 bfd_set_arch_mach (abfd
, pe_details
->bfd_arch
, 0);
1309 symtab
= (asymbol
**) xmalloc (5 * sizeof (asymbol
*));
1310 id4
= quick_section (abfd
, ".idata$4", SEC_HAS_CONTENTS
, 2);
1311 id5
= quick_section (abfd
, ".idata$5", SEC_HAS_CONTENTS
, 2);
1312 id7
= quick_section (abfd
, ".idata$7", SEC_HAS_CONTENTS
, 2);
1313 quick_symbol (abfd
, U(""), dll_symname
, "_iname", id7
, BSF_GLOBAL
, 0);
1315 bfd_set_section_size (abfd
, id4
, 4);
1316 d4
= (unsigned char *) xmalloc (4);
1320 bfd_set_section_size (abfd
, id5
, 4);
1321 d5
= (unsigned char *) xmalloc (4);
1325 len
= strlen (dll_filename
)+1;
1328 bfd_set_section_size (abfd
, id7
, len
);
1329 d7
= (unsigned char *) xmalloc (len
);
1331 strcpy (d7
, dll_filename
);
1333 bfd_set_symtab (abfd
, symtab
, symptr
);
1335 bfd_set_section_contents (abfd
, id4
, d4
, 0, 4);
1336 bfd_set_section_contents (abfd
, id5
, d5
, 0, 4);
1337 bfd_set_section_contents (abfd
, id7
, d7
, 0, len
);
1339 bfd_make_readable (abfd
);
1346 * .global ___imp_function
1347 * .global __imp__function
1349 * jmp *__imp__function:
1352 * .long __head_my_dll
1363 * .asciz "function" xlate? (add underscore, kill at)
1366 static unsigned char jmp_ix86_bytes
[] = {
1367 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 0x90, 0x90
1376 * .dw __imp_function
1379 static unsigned char jmp_sh_bytes
[] = {
1380 0x01, 0xd0, 0x02, 0x60, 0x2b, 0x40, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00
1385 * lui $t0,<high:__imp_function>
1386 * lw $t0,<low:__imp_function>
1391 static unsigned char jmp_mips_bytes
[] = {
1392 0x00, 0x00, 0x08, 0x3c, 0x00, 0x00, 0x08, 0x8d,
1393 0x08, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00
1397 make_one (exp
, parent
)
1398 def_file_export
*exp
;
1401 asection
*tx
, *id7
, *id5
, *id4
, *id6
;
1402 unsigned char *td
, *d7
, *d5
, *d4
, *d6
= NULL
;
1406 unsigned char *jmp_bytes
= NULL
;
1407 int jmp_byte_count
= 0;
1409 switch (pe_details
->pe_arch
)
1412 jmp_bytes
= jmp_ix86_bytes
;
1413 jmp_byte_count
= sizeof (jmp_ix86_bytes
);
1416 jmp_bytes
= jmp_sh_bytes
;
1417 jmp_byte_count
= sizeof (jmp_sh_bytes
);
1420 jmp_bytes
= jmp_mips_bytes
;
1421 jmp_byte_count
= sizeof (jmp_mips_bytes
);
1425 oname
= (char *) xmalloc (20);
1426 sprintf (oname
, "d%06d.o", tmp_seq
);
1429 abfd
= bfd_create (oname
, parent
);
1430 bfd_find_target (pe_details
->object_target
, abfd
);
1431 bfd_make_writable (abfd
);
1433 bfd_set_format (abfd
, bfd_object
);
1434 bfd_set_arch_mach (abfd
, pe_details
->bfd_arch
, 0);
1437 symtab
= (asymbol
**) xmalloc (10 * sizeof (asymbol
*));
1438 tx
= quick_section (abfd
, ".text", SEC_CODE
|SEC_HAS_CONTENTS
, 2);
1439 id7
= quick_section (abfd
, ".idata$7", SEC_HAS_CONTENTS
, 2);
1440 id5
= quick_section (abfd
, ".idata$5", SEC_HAS_CONTENTS
, 2);
1441 id4
= quick_section (abfd
, ".idata$4", SEC_HAS_CONTENTS
, 2);
1442 id6
= quick_section (abfd
, ".idata$6", SEC_HAS_CONTENTS
, 2);
1443 if (! exp
->flag_data
)
1444 quick_symbol (abfd
, U(""), exp
->internal_name
, "", tx
, BSF_GLOBAL
, 0);
1445 quick_symbol (abfd
, U("_head_"), dll_symname
, "", UNDSEC
, BSF_GLOBAL
, 0);
1446 quick_symbol (abfd
, U("__imp_"), exp
->internal_name
, "", id5
, BSF_GLOBAL
, 0);
1447 if (pe_dll_compat_implib
)
1448 quick_symbol (abfd
, U("__imp_"), exp
->internal_name
, "",
1449 id5
, BSF_GLOBAL
, 0);
1451 bfd_set_section_size (abfd
, tx
, jmp_byte_count
);
1452 td
= (unsigned char *) xmalloc (jmp_byte_count
);
1454 memcpy (td
, jmp_bytes
, jmp_byte_count
);
1455 switch (pe_details
->pe_arch
)
1458 quick_reloc (abfd
, 2, BFD_RELOC_32
, 2);
1461 quick_reloc (abfd
, 8, BFD_RELOC_32
, 2);
1464 quick_reloc (abfd
, 0, BFD_RELOC_HI16_S
, 2);
1465 quick_reloc (abfd
, 0, BFD_RELOC_LO16
, 0); /* MIPS_R_PAIR */
1466 quick_reloc (abfd
, 4, BFD_RELOC_LO16
, 2);
1471 bfd_set_section_size (abfd
, id7
, 4);
1472 d7
= (unsigned char *) xmalloc (4);
1475 quick_reloc (abfd
, 0, BFD_RELOC_RVA
, 6);
1478 bfd_set_section_size (abfd
, id5
, 4);
1479 d5
= (unsigned char *) xmalloc (4);
1482 if (exp
->flag_noname
)
1484 d5
[0] = exp
->ordinal
;
1485 d5
[1] = exp
->ordinal
>> 8;
1490 quick_reloc (abfd
, 0, BFD_RELOC_RVA
, 4);
1494 bfd_set_section_size (abfd
, id4
, 4);
1495 d4
= (unsigned char *) xmalloc (4);
1498 if (exp
->flag_noname
)
1500 d5
[0] = exp
->ordinal
;
1501 d5
[1] = exp
->ordinal
>> 8;
1506 quick_reloc (abfd
, 0, BFD_RELOC_RVA
, 4);
1510 if (exp
->flag_noname
)
1513 bfd_set_section_size (abfd
, id6
, 0);
1517 len
= strlen (exp
->name
) + 3;
1520 bfd_set_section_size (abfd
, id6
, len
);
1521 d6
= (unsigned char *) xmalloc (len
);
1523 memset (d6
, 0, len
);
1524 d6
[0] = exp
->hint
& 0xff;
1525 d6
[1] = exp
->hint
>> 8;
1526 strcpy (d6
+2, exp
->name
);
1529 bfd_set_symtab (abfd
, symtab
, symptr
);
1531 bfd_set_section_contents (abfd
, tx
, td
, 0, jmp_byte_count
);
1532 bfd_set_section_contents (abfd
, id7
, d7
, 0, 4);
1533 bfd_set_section_contents (abfd
, id5
, d5
, 0, 4);
1534 bfd_set_section_contents (abfd
, id4
, d4
, 0, 4);
1535 if (!exp
->flag_noname
)
1536 bfd_set_section_contents (abfd
, id6
, d6
, 0, len
);
1538 bfd_make_readable (abfd
);
1543 pe_dll_generate_implib (def
, impfilename
)
1545 const char *impfilename
;
1553 dll_filename
= (def
->name
) ? def
->name
: dll_name
;
1554 dll_symname
= xstrdup (dll_filename
);
1555 for (i
=0; dll_symname
[i
]; i
++)
1556 if (!isalnum ((unsigned char) dll_symname
[i
]))
1557 dll_symname
[i
] = '_';
1559 unlink (impfilename
);
1561 outarch
= bfd_openw (impfilename
, 0);
1565 /* xgettext:c-format */
1566 einfo (_("%XCan't open .lib file: %s\n"), impfilename
);
1570 /* xgettext:c-format */
1571 einfo (_("Creating library file: %s\n"), impfilename
);
1573 bfd_set_format (outarch
, bfd_archive
);
1574 outarch
->has_armap
= 1;
1576 /* Work out a reasonable size of things to put onto one line. */
1578 ar_head
= make_head (outarch
);
1580 for (i
= 0; i
<def
->num_exports
; i
++)
1582 /* The import library doesn't know about the internal name */
1583 char *internal
= def
->exports
[i
].internal_name
;
1585 def
->exports
[i
].internal_name
= def
->exports
[i
].name
;
1586 n
= make_one (def
->exports
+i
, outarch
);
1589 def
->exports
[i
].internal_name
= internal
;
1592 ar_tail
= make_tail (outarch
);
1594 if (ar_head
== NULL
|| ar_tail
== NULL
)
1597 /* Now stick them all into the archive */
1599 ar_head
->next
= head
;
1600 ar_tail
->next
= ar_head
;
1603 if (! bfd_set_archive_head (outarch
, head
))
1604 einfo ("%Xbfd_set_archive_head: %s\n", bfd_errmsg (bfd_get_error ()));
1606 if (! bfd_close (outarch
))
1607 einfo ("%Xbfd_close %s: %s\n", impfilename
, bfd_errmsg (bfd_get_error ()));
1609 while (head
!= NULL
)
1611 bfd
*n
= head
->next
;
1618 add_bfd_to_link (abfd
, name
, link_info
)
1621 struct bfd_link_info
*link_info
;
1623 lang_input_statement_type
*fake_file
;
1624 fake_file
= lang_add_input_file (name
,
1625 lang_input_file_is_fake_enum
,
1627 fake_file
->the_bfd
= abfd
;
1628 ldlang_add_file (fake_file
);
1629 if (!bfd_link_add_symbols (abfd
, link_info
))
1630 einfo ("%Xaddsym %s: %s\n", name
, bfd_errmsg (bfd_get_error ()));
1634 pe_process_import_defs (output_bfd
, link_info
)
1636 struct bfd_link_info
*link_info
;
1638 def_file_module
*module
;
1639 pe_dll_id_target(bfd_get_target (output_bfd
));
1644 for (module
= pe_def_file
->modules
; module
; module
= module
->next
)
1648 dll_filename
= module
->name
;
1649 dll_symname
= xstrdup (module
->name
);
1650 for (i
=0; dll_symname
[i
]; i
++)
1651 if (!isalnum (dll_symname
[i
]))
1652 dll_symname
[i
] = '_';
1656 for (i
=0; i
<pe_def_file
->num_imports
; i
++)
1657 if (pe_def_file
->imports
[i
].module
== module
)
1659 def_file_export exp
;
1660 struct bfd_link_hash_entry
*blhe
;
1662 /* see if we need this import */
1663 char *name
= (char *) xmalloc (strlen (pe_def_file
->imports
[i
].internal_name
) + 2);
1664 sprintf (name
, "%s%s", U(""), pe_def_file
->imports
[i
].internal_name
);
1665 blhe
= bfd_link_hash_lookup (link_info
->hash
, name
,
1666 false, false, false);
1668 if (blhe
&& blhe
->type
== bfd_link_hash_undefined
)
1674 bfd
*ar_head
= make_head (output_bfd
);
1675 add_bfd_to_link (ar_head
, ar_head
->filename
, link_info
);
1678 exp
.internal_name
= pe_def_file
->imports
[i
].internal_name
;
1679 exp
.name
= pe_def_file
->imports
[i
].name
;
1680 exp
.ordinal
= pe_def_file
->imports
[i
].ordinal
;
1681 exp
.hint
= exp
.ordinal
>= 0 ? exp
.ordinal
: 0;
1682 exp
.flag_private
= 0;
1683 exp
.flag_constant
= 0;
1685 exp
.flag_noname
= exp
.name
? 0 : 1;
1686 one
= make_one (&exp
, output_bfd
);
1687 add_bfd_to_link (one
, one
->filename
, link_info
);
1692 bfd
*ar_tail
= make_tail (output_bfd
);
1693 add_bfd_to_link (ar_tail
, ar_tail
->filename
, link_info
);
1700 /************************************************************************
1702 We were handed a *.DLL file. Parse it and turn it into a set of
1703 IMPORTS directives in the def file. Return true if the file was
1704 handled, false if not.
1706 ************************************************************************/
1709 pe_get16 (abfd
, where
)
1714 bfd_seek (abfd
, where
, SEEK_SET
);
1715 bfd_read (b
, 1, 2, abfd
);
1716 return b
[0] + (b
[1]<<8);
1720 pe_get32 (abfd
, where
)
1725 bfd_seek (abfd
, where
, SEEK_SET
);
1726 bfd_read (b
, 1, 4, abfd
);
1727 return b
[0] + (b
[1]<<8) + (b
[2]<<16) + (b
[3]<<24);
1730 #if 0 /* This is not currently used. */
1736 unsigned char *b
= ptr
;
1737 return b
[0] + (b
[1]<<8);
1746 unsigned char *b
= ptr
;
1747 return b
[0] + (b
[1]<<8) + (b
[2]<<16) + (b
[3]<<24);
1751 pe_implied_import_dll (filename
)
1752 const char *filename
;
1755 unsigned long pe_header_offset
, opthdr_ofs
, num_entries
, i
;
1756 unsigned long export_rva
, export_size
, nsections
, secptr
, expptr
;
1757 unsigned char *expdata
, *erva
;
1758 unsigned long name_rvas
, ordinals
, nexp
, ordbase
;
1759 const char *dll_name
;
1761 /* No, I can't use bfd here. kernel32.dll puts its export table in
1762 the middle of the .rdata section. */
1764 dll
= bfd_openr (filename
, pe_details
->target_name
);
1767 einfo ("%Xopen %s: %s\n", filename
, bfd_errmsg (bfd_get_error ()));
1770 /* PEI dlls seem to be bfd_objects */
1771 if (!bfd_check_format (dll
, bfd_object
))
1773 einfo ("%X%s: this doesn't appear to be a DLL\n", filename
);
1777 dll_name
= filename
;
1778 for (i
=0; filename
[i
]; i
++)
1779 if (filename
[i
] == '/' || filename
[i
] == '\\' || filename
[i
] == ':')
1780 dll_name
= filename
+ i
+ 1;
1782 pe_header_offset
= pe_get32 (dll
, 0x3c);
1783 opthdr_ofs
= pe_header_offset
+ 4 + 20;
1784 num_entries
= pe_get32 (dll
, opthdr_ofs
+ 92);
1785 if (num_entries
< 1) /* no exports */
1787 export_rva
= pe_get32 (dll
, opthdr_ofs
+ 96);
1788 export_size
= pe_get32 (dll
, opthdr_ofs
+ 100);
1789 nsections
= pe_get16 (dll
, pe_header_offset
+ 4 + 2);
1790 secptr
= (pe_header_offset
+ 4 + 20 +
1791 pe_get16 (dll
, pe_header_offset
+ 4 + 16));
1793 for (i
=0; i
<nsections
; i
++)
1796 unsigned long secptr1
= secptr
+ 40 * i
;
1797 unsigned long vaddr
= pe_get32 (dll
, secptr1
+ 12);
1798 unsigned long vsize
= pe_get32 (dll
, secptr1
+ 16);
1799 unsigned long fptr
= pe_get32 (dll
, secptr1
+ 20);
1800 bfd_seek(dll
, secptr1
, SEEK_SET
);
1801 bfd_read(sname
, 1, 8, dll
);
1802 if (vaddr
<= export_rva
&& vaddr
+vsize
> export_rva
)
1804 expptr
= fptr
+ (export_rva
- vaddr
);
1805 if (export_rva
+ export_size
> vaddr
+ vsize
)
1806 export_size
= vsize
- (export_rva
- vaddr
);
1811 expdata
= (unsigned char *) xmalloc (export_size
);
1812 bfd_seek (dll
, expptr
, SEEK_SET
);
1813 bfd_read (expdata
, 1, export_size
, dll
);
1814 erva
= expdata
- export_rva
;
1816 if (pe_def_file
== 0)
1817 pe_def_file
= def_file_empty();
1819 nexp
= pe_as32 (expdata
+24);
1820 name_rvas
= pe_as32 (expdata
+32);
1821 ordinals
= pe_as32 (expdata
+36);
1822 ordbase
= pe_as32 (expdata
+16);
1823 for (i
=0; i
<nexp
; i
++)
1825 unsigned long name_rva
= pe_as32 (erva
+name_rvas
+i
*4);
1826 def_file_import
*imp
;
1827 imp
= def_file_add_import (pe_def_file
, erva
+name_rva
, dll_name
,
1834 /************************************************************************
1836 These are the main functions, called from the emulation. The first
1837 is called after the bfds are read, so we can guess at how much space
1838 we need. The second is called after everything is placed, so we
1839 can put the right values in place.
1841 ************************************************************************/
1844 pe_dll_build_sections (abfd
, info
)
1846 struct bfd_link_info
*info
;
1848 pe_dll_id_target (bfd_get_target (abfd
));
1849 process_def_file (abfd
, info
);
1851 generate_edata (abfd
, info
);
1852 build_filler_bfd (1);
1856 pe_exe_build_sections (abfd
, info
)
1858 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1860 pe_dll_id_target (bfd_get_target (abfd
));
1861 build_filler_bfd (0);
1865 pe_dll_fill_sections (abfd
, info
)
1867 struct bfd_link_info
*info
;
1869 pe_dll_id_target (bfd_get_target (abfd
));
1870 image_base
= pe_data (abfd
)->pe_opthdr
.ImageBase
;
1872 generate_reloc (abfd
, info
);
1875 bfd_set_section_size (filler_bfd
, reloc_s
, reloc_sz
);
1877 /* Resize the sections. */
1878 lang_size_sections (stat_ptr
->head
, abs_output_section
,
1879 &stat_ptr
->head
, 0, (bfd_vma
) 0, false);
1881 /* Redo special stuff. */
1882 ldemul_after_allocation ();
1884 /* Do the assignments again. */
1885 lang_do_assignments (stat_ptr
->head
,
1887 (fill_type
) 0, (bfd_vma
) 0);
1890 fill_edata (abfd
, info
);
1892 pe_data (abfd
)->dll
= 1;
1894 edata_s
->contents
= edata_d
;
1895 reloc_s
->contents
= reloc_d
;
1899 pe_exe_fill_sections (abfd
, info
)
1901 struct bfd_link_info
*info
;
1903 pe_dll_id_target (bfd_get_target (abfd
));
1904 image_base
= pe_data (abfd
)->pe_opthdr
.ImageBase
;
1906 generate_reloc (abfd
, info
);
1909 bfd_set_section_size (filler_bfd
, reloc_s
, reloc_sz
);
1911 /* Resize the sections. */
1912 lang_size_sections (stat_ptr
->head
, abs_output_section
,
1913 &stat_ptr
->head
, 0, (bfd_vma
) 0, false);
1915 /* Redo special stuff. */
1916 ldemul_after_allocation ();
1918 /* Do the assignments again. */
1919 lang_do_assignments (stat_ptr
->head
,
1921 (fill_type
) 0, (bfd_vma
) 0);
1923 reloc_s
->contents
= reloc_d
;