]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/coff-pe-read.c
07cd216b4b215de7c9df9d65af94250b333d7b0e
[thirdparty/binutils-gdb.git] / gdb / coff-pe-read.c
1 /* Read the export table symbols from a portable executable and
2 convert to internal format, for GDB. Used as a last resort if no
3 debugging symbols recognized.
4
5 Copyright (C) 2003-2023 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21
22 Contributed by Raoul M. Gough (RaoulGough@yahoo.co.uk). */
23
24 #include "defs.h"
25
26 #include "coff-pe-read.h"
27
28 #include "bfd.h"
29 #include "gdbtypes.h"
30
31 #include "command.h"
32 #include "gdbcmd.h"
33 #include "symtab.h"
34 #include "symfile.h"
35 #include "objfiles.h"
36 #include "gdbsupport/common-utils.h"
37 #include "coff/internal.h"
38
39 #include <ctype.h>
40
41 /* Internal section information */
42
43 /* Coff PE read debugging flag:
44 default value is 0,
45 value 1 outputs problems encountered while parsing PE file,
46 value above 1 also lists all generated minimal symbols. */
47 static unsigned int debug_coff_pe_read;
48
49 struct read_pe_section_data
50 {
51 CORE_ADDR vma_offset; /* Offset to loaded address of section. */
52 unsigned long rva_start; /* Start offset within the pe. */
53 unsigned long rva_end; /* End offset within the pe. */
54 enum minimal_symbol_type ms_type; /* Type to assign symbols in
55 section. */
56 unsigned int index; /* BFD section number. */
57 std::string section_name; /* Recorded section name. */
58 };
59
60 #define IMAGE_SCN_CNT_CODE 0x20
61 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x40
62 #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x80
63 #define PE_SECTION_INDEX_TEXT 0
64 #define PE_SECTION_INDEX_DATA 1
65 #define PE_SECTION_INDEX_BSS 2
66 #define PE_SECTION_TABLE_SIZE 3
67 #define PE_SECTION_INDEX_INVALID -1
68 \f
69 /* Get the index of the named section in our own array, which contains
70 text, data and bss in that order. Return PE_SECTION_INDEX_INVALID
71 if passed an unrecognised section name. */
72
73 static int
74 read_pe_section_index (const char *section_name)
75 {
76 if (strcmp (section_name, ".text") == 0)
77 {
78 return PE_SECTION_INDEX_TEXT;
79 }
80
81 else if (strcmp (section_name, ".data") == 0)
82 {
83 return PE_SECTION_INDEX_DATA;
84 }
85
86 else if (strcmp (section_name, ".bss") == 0)
87 {
88 return PE_SECTION_INDEX_BSS;
89 }
90
91 else
92 {
93 return PE_SECTION_INDEX_INVALID;
94 }
95 }
96
97 /* Get the index of the named section in our own full array.
98 text, data and bss in that order. Return PE_SECTION_INDEX_INVALID
99 if passed an unrecognised section name. */
100
101 static int
102 get_pe_section_index (const char *section_name,
103 const std::vector<read_pe_section_data> &sections)
104 {
105 for (int i = 0; i < sections.size (); i++)
106 if (sections[i].section_name == section_name)
107 return i;
108 return PE_SECTION_INDEX_INVALID;
109 }
110
111 \f
112 /* Create a minimal symbol entry for an exported symbol.
113 SYM_NAME contains the exported name or NULL if exported by ordinal,
114 FUNC_RVA contains the Relative Virtual Address of the symbol,
115 ORDINAL is the ordinal index value of the symbol,
116 SECTION_DATA contains information about the section in which the
117 symbol is declared,
118 DLL_NAME is the internal name of the DLL file,
119 OBJFILE is the objfile struct of DLL_NAME. */
120
121 static void
122 add_pe_exported_sym (minimal_symbol_reader &reader,
123 const char *sym_name,
124 unsigned long func_rva,
125 int ordinal,
126 const struct read_pe_section_data *section_data,
127 const char *dll_name, struct objfile *objfile)
128 {
129 /* Add the stored offset to get the loaded address of the symbol. */
130 unrelocated_addr vma = unrelocated_addr (func_rva
131 + section_data->vma_offset);
132
133 /* Generate a (hopefully unique) qualified name using the first part
134 of the dll name, e.g. KERNEL32!AddAtomA. This matches the style
135 used by windbg from the "Microsoft Debugging Tools for Windows". */
136
137 std::string bare_name;
138 if (sym_name == NULL || *sym_name == '\0')
139 bare_name = string_printf ("#%d", ordinal);
140 else
141 bare_name = sym_name;
142
143 std::string qualified_name
144 = string_printf ("%s!%s", dll_name, bare_name.c_str ());
145
146 if ((section_data->ms_type == mst_unknown) && debug_coff_pe_read)
147 gdb_printf (gdb_stdlog , _("Unknown section type for \"%s\""
148 " for entry \"%s\" in dll \"%s\"\n"),
149 section_data->section_name.c_str (), sym_name,
150 dll_name);
151
152 reader.record_with_info (qualified_name.c_str (), vma, section_data->ms_type,
153 section_data->index);
154
155 /* Enter the plain name as well, which might not be unique. */
156 reader.record_with_info (bare_name.c_str (), vma, section_data->ms_type,
157 section_data->index);
158 if (debug_coff_pe_read > 1)
159 gdb_printf (gdb_stdlog, _("Adding exported symbol \"%s\""
160 " in dll \"%s\"\n"), sym_name, dll_name);
161 }
162
163 /* Create a minimal symbol entry for an exported forward symbol.
164 Return 1 if the forwarded function was found 0 otherwise.
165 SYM_NAME contains the exported name or NULL if exported by ordinal,
166 FORWARD_DLL_NAME is the name of the DLL in which the target symobl resides,
167 FORWARD_FUNC_NAME is the name of the target symbol in that DLL,
168 ORDINAL is the ordinal index value of the symbol,
169 DLL_NAME is the internal name of the DLL file,
170 OBJFILE is the objfile struct of DLL_NAME. */
171
172 static int
173 add_pe_forwarded_sym (minimal_symbol_reader &reader,
174 const char *sym_name, const char *forward_dll_name,
175 const char *forward_func_name, int ordinal,
176 const char *dll_name, struct objfile *objfile)
177 {
178 struct bound_minimal_symbol msymbol;
179 enum minimal_symbol_type msymtype;
180 int forward_dll_name_len = strlen (forward_dll_name);
181 short section;
182
183 std::string forward_qualified_name = string_printf ("%s!%s",
184 forward_dll_name,
185 forward_func_name);
186
187 msymbol = lookup_bound_minimal_symbol (forward_qualified_name.c_str ());
188
189 if (!msymbol.minsym)
190 {
191 int i;
192
193 for (i = 0; i < forward_dll_name_len; i++)
194 forward_qualified_name[i] = tolower (forward_qualified_name[i]);
195 msymbol = lookup_bound_minimal_symbol (forward_qualified_name.c_str ());
196 }
197
198 if (!msymbol.minsym)
199 {
200 if (debug_coff_pe_read)
201 gdb_printf (gdb_stdlog, _("Unable to find function \"%s\" in"
202 " dll \"%s\", forward of \"%s\" in dll \"%s\"\n"),
203 forward_func_name, forward_dll_name, sym_name,
204 dll_name);
205 return 0;
206 }
207
208 if (debug_coff_pe_read > 1)
209 gdb_printf (gdb_stdlog, _("Adding forwarded exported symbol"
210 " \"%s\" in dll \"%s\", pointing to \"%s\"\n"),
211 sym_name, dll_name, forward_qualified_name.c_str ());
212
213 unrelocated_addr vma = msymbol.minsym->value_raw_address ();
214 msymtype = msymbol.minsym->type ();
215 section = msymbol.minsym->section_index ();
216
217 /* Generate a (hopefully unique) qualified name using the first part
218 of the dll name, e.g. KERNEL32!AddAtomA. This matches the style
219 used by windbg from the "Microsoft Debugging Tools for Windows". */
220
221 std::string bare_name;
222 if (sym_name == NULL || *sym_name == '\0')
223 bare_name = string_printf ("#%d", ordinal);
224 else
225 bare_name = sym_name;
226
227 std::string qualified_name
228 = string_printf ("%s!%s", dll_name, bare_name.c_str ());
229
230 /* Note that this code makes a minimal symbol whose value may point
231 outside of any section in this objfile. These symbols can't
232 really be relocated properly, but nevertheless we make a stab at
233 it, choosing an approach consistent with the history of this
234 code. */
235
236 reader.record_with_info (qualified_name.c_str (), vma, msymtype, section);
237
238 /* Enter the plain name as well, which might not be unique. */
239 reader.record_with_info (bare_name.c_str(), vma, msymtype, section);
240
241 return 1;
242 }
243
244 /* Truncate a dll_name at the last dot character. */
245
246 static void
247 read_pe_truncate_name (char *dll_name)
248 {
249 char *last_point = strrchr (dll_name, '.');
250
251 if (last_point != NULL)
252 *last_point = '\0';
253 }
254 \f
255 /* Low-level support functions, direct from the ld module pe-dll.c. */
256 static unsigned int
257 pe_get16 (bfd *abfd, int where)
258 {
259 unsigned char b[2];
260
261 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
262 bfd_bread (b, (bfd_size_type) 2, abfd);
263 return b[0] + (b[1] << 8);
264 }
265
266 static unsigned int
267 pe_get32 (bfd *abfd, int where)
268 {
269 unsigned char b[4];
270
271 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
272 bfd_bread (b, (bfd_size_type) 4, abfd);
273 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
274 }
275
276 static unsigned int
277 pe_as16 (void *ptr)
278 {
279 unsigned char *b = (unsigned char *) ptr;
280
281 return b[0] + (b[1] << 8);
282 }
283
284 static unsigned int
285 pe_as32 (void *ptr)
286 {
287 unsigned char *b = (unsigned char *) ptr;
288
289 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
290 }
291 \f
292 /* Read the (non-debug) export symbol table from a portable
293 executable. Code originally lifted from the ld function
294 pe_implied_import_dll in pe-dll.c. */
295
296 void
297 read_pe_exported_syms (minimal_symbol_reader &reader,
298 struct objfile *objfile)
299 {
300 bfd *dll = objfile->obfd.get ();
301 unsigned long nbnormal, nbforward;
302 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
303 unsigned long export_opthdrrva, export_opthdrsize;
304 unsigned long export_rva, export_size, nsections, secptr, expptr;
305 unsigned long exp_funcbase;
306 unsigned char *expdata, *erva;
307 unsigned long name_rvas, ordinals, nexp, ordbase;
308 int otherix = PE_SECTION_TABLE_SIZE;
309 int is_pe64 = 0;
310 int is_pe32 = 0;
311
312 char const *target = bfd_get_target (objfile->obfd.get ());
313
314 std::vector<struct read_pe_section_data> section_data
315 (PE_SECTION_TABLE_SIZE);
316
317 for (i=0; i < PE_SECTION_TABLE_SIZE; i++)
318 {
319 section_data[i].vma_offset = 0;
320 section_data[i].rva_start = 1;
321 section_data[i].rva_end = 0;
322 };
323 section_data[PE_SECTION_INDEX_TEXT].ms_type = mst_text;
324 section_data[PE_SECTION_INDEX_TEXT].section_name = ".text";
325 section_data[PE_SECTION_INDEX_DATA].ms_type = mst_data;
326 section_data[PE_SECTION_INDEX_DATA].section_name = ".data";
327 section_data[PE_SECTION_INDEX_BSS].ms_type = mst_bss;
328 section_data[PE_SECTION_INDEX_BSS].section_name = ".bss";
329
330 is_pe64 = (strcmp (target, "pe-x86-64") == 0
331 || strcmp (target, "pei-x86-64") == 0
332 || strcmp (target, "pe-aarch64") == 0
333 || strcmp (target, "pei-aarch64") == 0);
334 is_pe32 = (strcmp (target, "pe-i386") == 0
335 || strcmp (target, "pei-i386") == 0
336 || strcmp (target, "pe-arm-wince-little") == 0
337 || strcmp (target, "pei-arm-wince-little") == 0);
338 if (!is_pe32 && !is_pe64)
339 {
340 /* This is not a recognized PE format file. Abort now, because
341 the code is untested on anything else. *FIXME* test on
342 further architectures and loosen or remove this test. */
343 return;
344 }
345
346 /* Get pe_header, optional header and numbers of export entries. */
347 pe_header_offset = pe_get32 (dll, 0x3c);
348 opthdr_ofs = pe_header_offset + 4 + 20;
349 if (is_pe64)
350 num_entries = pe_get32 (dll, opthdr_ofs + 108);
351 else
352 num_entries = pe_get32 (dll, opthdr_ofs + 92);
353
354 if (num_entries < 1) /* No exports. */
355 return;
356 if (is_pe64)
357 {
358 export_opthdrrva = pe_get32 (dll, opthdr_ofs + 112);
359 export_opthdrsize = pe_get32 (dll, opthdr_ofs + 116);
360 }
361 else
362 {
363 export_opthdrrva = pe_get32 (dll, opthdr_ofs + 96);
364 export_opthdrsize = pe_get32 (dll, opthdr_ofs + 100);
365 }
366 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
367 secptr = (pe_header_offset + 4 + 20 +
368 pe_get16 (dll, pe_header_offset + 4 + 16));
369 expptr = 0;
370 export_size = 0;
371
372 /* Get the rva and size of the export section. */
373 for (i = 0; i < nsections; i++)
374 {
375 char sname[8];
376 unsigned long secptr1 = secptr + 40 * i;
377 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
378 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
379 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
380
381 bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
382 bfd_bread (sname, (bfd_size_type) sizeof (sname), dll);
383
384 if ((strcmp (sname, ".edata") == 0)
385 || (vaddr <= export_opthdrrva && export_opthdrrva < vaddr + vsize))
386 {
387 if (strcmp (sname, ".edata") != 0)
388 {
389 if (debug_coff_pe_read)
390 gdb_printf (gdb_stdlog, _("Export RVA for dll "
391 "\"%s\" is in section \"%s\"\n"),
392 bfd_get_filename (dll), sname);
393 }
394 else if (export_opthdrrva != vaddr && debug_coff_pe_read)
395 gdb_printf (gdb_stdlog, _("Wrong value of export RVA"
396 " for dll \"%s\": 0x%lx instead of 0x%lx\n"),
397 bfd_get_filename (dll), export_opthdrrva, vaddr);
398 expptr = fptr + (export_opthdrrva - vaddr);
399 break;
400 }
401 }
402
403 if (expptr == 0)
404 {
405 /* no section contains export table rva */
406 return;
407 }
408
409 export_rva = export_opthdrrva;
410 export_size = export_opthdrsize;
411
412 if (export_size == 0)
413 {
414 /* Empty export table. */
415 return;
416 }
417
418 /* Scan sections and store the base and size of the relevant
419 sections. */
420 for (i = 0; i < nsections; i++)
421 {
422 unsigned long secptr1 = secptr + 40 * i;
423 unsigned long vsize = pe_get32 (dll, secptr1 + 8);
424 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
425 unsigned long characteristics = pe_get32 (dll, secptr1 + 36);
426 char sec_name[SCNNMLEN + 1];
427 int sectix;
428 unsigned int bfd_section_index;
429 asection *section;
430
431 bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
432 bfd_bread (sec_name, (bfd_size_type) SCNNMLEN, dll);
433 sec_name[SCNNMLEN] = '\0';
434
435 sectix = read_pe_section_index (sec_name);
436 section = bfd_get_section_by_name (dll, sec_name);
437 if (section)
438 bfd_section_index = section->index;
439 else
440 bfd_section_index = -1;
441
442 if (sectix != PE_SECTION_INDEX_INVALID)
443 {
444 section_data[sectix].rva_start = vaddr;
445 section_data[sectix].rva_end = vaddr + vsize;
446 section_data[sectix].index = bfd_section_index;
447 }
448 else
449 {
450 section_data.resize (otherix + 1);
451 section_data[otherix].section_name = sec_name;
452 section_data[otherix].rva_start = vaddr;
453 section_data[otherix].rva_end = vaddr + vsize;
454 section_data[otherix].vma_offset = 0;
455 section_data[otherix].index = bfd_section_index;
456 if (characteristics & IMAGE_SCN_CNT_CODE)
457 section_data[otherix].ms_type = mst_text;
458 else if (characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
459 section_data[otherix].ms_type = mst_data;
460 else if (characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA)
461 section_data[otherix].ms_type = mst_bss;
462 else
463 section_data[otherix].ms_type = mst_unknown;
464 otherix++;
465 }
466 }
467
468 gdb::def_vector<unsigned char> expdata_storage (export_size);
469 expdata = expdata_storage.data ();
470
471 bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
472 bfd_bread (expdata, (bfd_size_type) export_size, dll);
473 erva = expdata - export_rva;
474
475 nexp = pe_as32 (expdata + 24);
476 name_rvas = pe_as32 (expdata + 32);
477 ordinals = pe_as32 (expdata + 36);
478 ordbase = pe_as32 (expdata + 16);
479 exp_funcbase = pe_as32 (expdata + 28);
480
481 /* Use internal dll name instead of full pathname. */
482 char *dll_name = (char *) (pe_as32 (expdata + 12) + erva);
483
484 for (asection *sectp : gdb_bfd_sections (dll))
485 {
486 int sectix = get_pe_section_index (sectp->name, section_data);
487 if (sectix != PE_SECTION_INDEX_INVALID)
488 {
489 /* Data within the section start at rva_start in the pe and at
490 bfd_get_section_vma() within memory. Store the offset. */
491 section_data[sectix].vma_offset
492 = bfd_section_vma (sectp) - section_data[sectix].rva_start;
493 }
494 }
495
496 /* Truncate name at first dot. Should maybe also convert to all
497 lower case for convenience on Windows. */
498 read_pe_truncate_name (dll_name);
499
500 if (debug_coff_pe_read)
501 gdb_printf (gdb_stdlog, _("DLL \"%s\" has %ld export entries,"
502 " base=%ld\n"), dll_name, nexp, ordbase);
503 nbforward = 0;
504 nbnormal = 0;
505 /* Iterate through the list of symbols. */
506 for (i = 0; i < nexp; i++)
507 {
508 /* Pointer to the names vector. */
509 unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
510 /* Retrieve ordinal value. */
511
512 unsigned long ordinal = pe_as16 (erva + ordinals + i * 2);
513
514
515 /* Pointer to the function address vector. */
516 /* This is relative to ordinal value. */
517 unsigned long func_rva = pe_as32 (erva + exp_funcbase +
518 ordinal * 4);
519
520 /* Find this symbol's section in our own array. */
521 int sectix = 0;
522 int section_found = 0;
523
524 /* First handle forward cases. */
525 if (func_rva >= export_rva && func_rva < export_rva + export_size)
526 {
527 const char *forward_name = (const char *) (erva + func_rva);
528 const char *funcname = (const char *) (erva + name_rva);
529 const char *forward_dll_name = forward_name;
530 const char *forward_func_name = forward_name;
531 const char *sep = strrchr (forward_name, '.');
532
533 std::string name_storage;
534 if (sep != nullptr)
535 {
536 int len = (int) (sep - forward_name);
537
538 name_storage = std::string (forward_name, len);
539 forward_dll_name = name_storage.c_str ();
540 forward_func_name = sep + 1;
541 }
542 if (add_pe_forwarded_sym (reader, funcname, forward_dll_name,
543 forward_func_name, ordinal,
544 dll_name, objfile) != 0)
545 ++nbforward;
546 continue;
547 }
548
549 for (sectix = 0; sectix < otherix; ++sectix)
550 {
551 if ((func_rva >= section_data[sectix].rva_start)
552 && (func_rva < section_data[sectix].rva_end))
553 {
554 const char *sym_name = (const char *) (erva + name_rva);
555
556 section_found = 1;
557 add_pe_exported_sym (reader, sym_name, func_rva, ordinal,
558 &section_data[sectix], dll_name, objfile);
559 ++nbnormal;
560 break;
561 }
562 }
563 if (!section_found)
564 {
565 const char *funcname = (const char *) (erva + name_rva);
566
567 if (name_rva == 0)
568 {
569 add_pe_exported_sym (reader, NULL, func_rva, ordinal,
570 &section_data[0], dll_name, objfile);
571 ++nbnormal;
572 }
573 else if (debug_coff_pe_read)
574 gdb_printf (gdb_stdlog, _("Export name \"%s\" ord. %lu,"
575 " RVA 0x%lx in dll \"%s\" not handled\n"),
576 funcname, ordinal, func_rva, dll_name);
577 }
578 }
579
580 if (debug_coff_pe_read)
581 gdb_printf (gdb_stdlog, _("Finished reading \"%s\", exports %ld,"
582 " forwards %ld, total %ld/%ld.\n"), dll_name, nbnormal,
583 nbforward, nbnormal + nbforward, nexp);
584 }
585
586 /* Extract from ABFD the offset of the .text section.
587 This offset is mainly related to the offset within the file.
588 The value was previously expected to be 0x1000 for all files,
589 but some Windows OS core DLLs seem to use 0x10000 section alignment
590 which modified the return value of that function.
591 Still return default 0x1000 value if ABFD is NULL or
592 if '.text' section is not found, but that should not happen... */
593
594 #define DEFAULT_COFF_PE_TEXT_SECTION_OFFSET 0x1000
595
596 CORE_ADDR
597 pe_text_section_offset (struct bfd *abfd)
598
599 {
600 unsigned long pe_header_offset, i;
601 unsigned long nsections, secptr;
602 int is_pe64 = 0;
603 int is_pe32 = 0;
604 char const *target;
605
606 if (!abfd)
607 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
608
609 target = bfd_get_target (abfd);
610
611 is_pe64 = (strcmp (target, "pe-x86-64") == 0
612 || strcmp (target, "pei-x86-64") == 0
613 || strcmp (target, "pe-aarch64") == 0
614 || strcmp (target, "pei-aarch64") == 0);
615 is_pe32 = (strcmp (target, "pe-i386") == 0
616 || strcmp (target, "pei-i386") == 0
617 || strcmp (target, "pe-arm-wince-little") == 0
618 || strcmp (target, "pei-arm-wince-little") == 0);
619
620 if (!is_pe32 && !is_pe64)
621 {
622 /* This is not a recognized PE format file. Abort now, because
623 the code is untested on anything else. *FIXME* test on
624 further architectures and loosen or remove this test. */
625 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
626 }
627
628 /* Get pe_header, optional header and numbers of sections. */
629 pe_header_offset = pe_get32 (abfd, 0x3c);
630 nsections = pe_get16 (abfd, pe_header_offset + 4 + 2);
631 secptr = (pe_header_offset + 4 + 20 +
632 pe_get16 (abfd, pe_header_offset + 4 + 16));
633
634 /* Get the rva and size of the export section. */
635 for (i = 0; i < nsections; i++)
636 {
637 char sname[SCNNMLEN + 1];
638 unsigned long secptr1 = secptr + 40 * i;
639 unsigned long vaddr = pe_get32 (abfd, secptr1 + 12);
640
641 bfd_seek (abfd, (file_ptr) secptr1, SEEK_SET);
642 bfd_bread (sname, (bfd_size_type) SCNNMLEN, abfd);
643 sname[SCNNMLEN] = '\0';
644 if (strcmp (sname, ".text") == 0)
645 return vaddr;
646 }
647
648 return DEFAULT_COFF_PE_TEXT_SECTION_OFFSET;
649 }
650
651 /* Implements "show debug coff_pe_read" command. */
652
653 static void
654 show_debug_coff_pe_read (struct ui_file *file, int from_tty,
655 struct cmd_list_element *c, const char *value)
656 {
657 gdb_printf (file, _("Coff PE read debugging is %s.\n"), value);
658 }
659
660 /* Adds "Set/show debug coff_pe_read" commands. */
661
662 void _initialize_coff_pe_read ();
663 void
664 _initialize_coff_pe_read ()
665 {
666 add_setshow_zuinteger_cmd ("coff-pe-read", class_maintenance,
667 &debug_coff_pe_read,
668 _("Set coff PE read debugging."),
669 _("Show coff PE read debugging."),
670 _("When set, debugging messages for coff reading "
671 "of exported symbols are displayed."),
672 NULL, show_debug_coff_pe_read,
673 &setdebuglist, &showdebuglist);
674 }