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