+
+/* Extract from ABFD the offset of the .text section.
+ This offset is mainly related to the offset within the file.
+ The value was previously expected to be 0x1000 for all files,
+ but some Windows OS core DLLs seem to use 0x10000 section alignement
+ which modified the return value of that function.
+ Still return default 0x1000 value if ABFD is NULL or
+ if '.text' section is not found, but that should not happen... */
+
+#define DEFAULT_COFF_PE_TEXT_SECTION_OFFSET 0x1000
+
+CORE_ADDR
+pe_text_section_offset (struct bfd *abfd)
+
+{
+ unsigned long pe_header_offset, i;
+ unsigned long nsections, secptr;
+ int is_pe64 = 0;
+ int is_pe32 = 0;
+ char const *target;
+
+ if (!abfd)
+ return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
+
+ target = bfd_get_target (abfd);
+
+ is_pe64 = (strcmp (target, "pe-x86-64") == 0
+ || strcmp (target, "pei-x86-64") == 0);
+ is_pe32 = (strcmp (target, "pe-i386") == 0
+ || strcmp (target, "pei-i386") == 0
+ || strcmp (target, "pe-arm-wince-little") == 0
+ || strcmp (target, "pei-arm-wince-little") == 0);
+
+ if (!is_pe32 && !is_pe64)
+ {
+ /* This is not a recognized PE format file. Abort now, because
+ the code is untested on anything else. *FIXME* test on
+ further architectures and loosen or remove this test. */
+ return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
+ }
+
+ /* Get pe_header, optional header and numbers of sections. */
+ pe_header_offset = pe_get32 (abfd, 0x3c);
+ nsections = pe_get16 (abfd, pe_header_offset + 4 + 2);
+ secptr = (pe_header_offset + 4 + 20 +
+ pe_get16 (abfd, pe_header_offset + 4 + 16));
+
+ /* Get the rva and size of the export section. */
+ for (i = 0; i < nsections; i++)
+ {
+ char sname[SCNNMLEN + 1];
+ unsigned long secptr1 = secptr + 40 * i;
+ unsigned long vaddr = pe_get32 (abfd, secptr1 + 12);
+
+ bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET);
+ bfd_bread (sname, (bfd_size_type) SCNNMLEN, abfd);
+ sname[SCNNMLEN] = '\0';
+ if (strcmp (sname, ".text") == 0)
+ return vaddr;
+ }
+
+ return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
+}
+
+/* Implements "show debug coff_pe_read" command. */
+
+static void
+show_debug_coff_pe_read (struct ui_file *file, int from_tty,
+ struct cmd_list_element *c, const char *value)
+{
+ fprintf_filtered (file, _("Coff PE read debugging is %s.\n"), value);
+}
+
+/* Provide a prototype to silence -Wmissing-prototypes. */
+
+void _initialize_coff_pe_read (void);
+
+/* Adds "Set/show debug coff_pe_read" commands. */
+
+void
+_initialize_coff_pe_read (void)
+{
+ add_setshow_zuinteger_cmd ("coff-pe-read", class_maintenance,
+ &debug_coff_pe_read,
+ _("Set coff PE read debugging."),
+ _("Show coff PE read debugging."),
+ _("When set, debugging messages for coff reading "
+ "of exported symbols are displayed."),
+ NULL, show_debug_coff_pe_read,
+ &setdebuglist, &showdebuglist);
+}