Import a stripped gdb-6.7.1
[dragonfly.git] / contrib / gdb-6 / gdb / coff-pe-read.c
blob8087430b3ea69336622268aee6c46dd0bcb6128b
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.
5 Copyright (C) 2003, 2007 Free Software Foundation, Inc.
7 This file is part of GDB.
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.
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.
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/>.
22 Contributed by Raoul M. Gough (RaoulGough@yahoo.co.uk). */
24 #include "coff-pe-read.h"
26 #include "bfd.h"
28 #include "defs.h"
29 #include "gdbtypes.h"
31 #include "symtab.h"
32 #include "symfile.h"
33 #include "objfiles.h"
35 /* Internal section information */
37 struct read_pe_section_data
39 CORE_ADDR vma_offset; /* Offset to loaded address of section. */
40 unsigned long rva_start; /* Start offset within the pe. */
41 unsigned long rva_end; /* End offset within the pe. */
42 enum minimal_symbol_type ms_type; /* Type to assign symbols in section. */
45 #define PE_SECTION_INDEX_TEXT 0
46 #define PE_SECTION_INDEX_DATA 1
47 #define PE_SECTION_INDEX_BSS 2
48 #define PE_SECTION_TABLE_SIZE 3
49 #define PE_SECTION_INDEX_INVALID -1
51 /* Get the index of the named section in our own array, which contains
52 text, data and bss in that order. Return PE_SECTION_INDEX_INVALID
53 if passed an unrecognised section name. */
55 static int
56 read_pe_section_index (const char *section_name)
58 if (strcmp (section_name, ".text") == 0)
60 return PE_SECTION_INDEX_TEXT;
63 else if (strcmp (section_name, ".data") == 0)
65 return PE_SECTION_INDEX_DATA;
68 else if (strcmp (section_name, ".bss") == 0)
70 return PE_SECTION_INDEX_BSS;
73 else
75 return PE_SECTION_INDEX_INVALID;
79 /* Record the virtual memory address of a section. */
81 static void
82 get_section_vmas (bfd *abfd, asection *sectp, void *context)
84 struct read_pe_section_data *sections = context;
85 int sectix = read_pe_section_index (sectp->name);
87 if (sectix != PE_SECTION_INDEX_INVALID)
89 /* Data within the section start at rva_start in the pe and at
90 bfd_get_section_vma() within memory. Store the offset. */
92 sections[sectix].vma_offset
93 = bfd_get_section_vma (abfd, sectp) - sections[sectix].rva_start;
97 /* Create a minimal symbol entry for an exported symbol. */
99 static void
100 add_pe_exported_sym (char *sym_name,
101 unsigned long func_rva,
102 const struct read_pe_section_data *section_data,
103 const char *dll_name, struct objfile *objfile)
105 /* Add the stored offset to get the loaded address of the symbol. */
107 CORE_ADDR vma = func_rva + section_data->vma_offset;
109 char *qualified_name = 0;
110 int dll_name_len = strlen (dll_name);
111 int count;
113 /* Generate a (hopefully unique) qualified name using the first part
114 of the dll name, e.g. KERNEL32!AddAtomA. This matches the style
115 used by windbg from the "Microsoft Debugging Tools for Windows". */
117 qualified_name = xmalloc (dll_name_len + strlen (sym_name) + 2);
119 strncpy (qualified_name, dll_name, dll_name_len);
120 qualified_name[dll_name_len] = '!';
121 strcpy (qualified_name + dll_name_len + 1, sym_name);
123 prim_record_minimal_symbol (qualified_name,
124 vma, section_data->ms_type, objfile);
126 xfree (qualified_name);
128 /* Enter the plain name as well, which might not be unique. */
129 prim_record_minimal_symbol (sym_name, vma, section_data->ms_type, objfile);
132 /* Truncate a dll_name at the first dot character. */
134 static void
135 read_pe_truncate_name (char *dll_name)
137 while (*dll_name)
139 if ((*dll_name) == '.')
141 *dll_name = '\0'; /* truncates and causes loop exit. */
144 else
146 ++dll_name;
151 /* Low-level support functions, direct from the ld module pe-dll.c. */
152 static unsigned int
153 pe_get16 (bfd *abfd, int where)
155 unsigned char b[2];
157 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
158 bfd_bread (b, (bfd_size_type) 2, abfd);
159 return b[0] + (b[1] << 8);
162 static unsigned int
163 pe_get32 (bfd *abfd, int where)
165 unsigned char b[4];
167 bfd_seek (abfd, (file_ptr) where, SEEK_SET);
168 bfd_bread (b, (bfd_size_type) 4, abfd);
169 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
172 static unsigned int
173 pe_as32 (void *ptr)
175 unsigned char *b = ptr;
177 return b[0] + (b[1] << 8) + (b[2] << 16) + (b[3] << 24);
180 /* Read the (non-debug) export symbol table from a portable
181 executable. Code originally lifted from the ld function
182 pe_implied_import_dll in pe-dll.c. */
184 void
185 read_pe_exported_syms (struct objfile *objfile)
187 bfd *dll = objfile->obfd;
188 unsigned long pe_header_offset, opthdr_ofs, num_entries, i;
189 unsigned long export_rva, export_size, nsections, secptr, expptr;
190 unsigned long exp_funcbase;
191 unsigned char *expdata, *erva;
192 unsigned long name_rvas, ordinals, nexp, ordbase;
193 char *dll_name;
195 /* Array elements are for text, data and bss in that order
196 Initialization with start_rva > end_rva guarantees that
197 unused sections won't be matched. */
198 struct read_pe_section_data section_data[PE_SECTION_TABLE_SIZE]
199 = { {0, 1, 0, mst_text},
200 {0, 1, 0, mst_data},
201 {0, 1, 0, mst_bss}
204 struct cleanup *back_to = 0;
206 char const *target = bfd_get_target (objfile->obfd);
208 if ((strcmp (target, "pe-i386") != 0) && (strcmp (target, "pei-i386") != 0))
210 /* This is not an i386 format file. Abort now, because the code
211 is untested on anything else. *FIXME* test on further
212 architectures and loosen or remove this test. */
213 return;
216 /* Get pe_header, optional header and numbers of export entries. */
217 pe_header_offset = pe_get32 (dll, 0x3c);
218 opthdr_ofs = pe_header_offset + 4 + 20;
219 num_entries = pe_get32 (dll, opthdr_ofs + 92);
221 if (num_entries < 1) /* No exports. */
223 return;
226 export_rva = pe_get32 (dll, opthdr_ofs + 96);
227 export_size = pe_get32 (dll, opthdr_ofs + 100);
228 nsections = pe_get16 (dll, pe_header_offset + 4 + 2);
229 secptr = (pe_header_offset + 4 + 20 +
230 pe_get16 (dll, pe_header_offset + 4 + 16));
231 expptr = 0;
233 /* Get the rva and size of the export section. */
234 for (i = 0; i < nsections; i++)
236 char sname[8];
237 unsigned long secptr1 = secptr + 40 * i;
238 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
239 unsigned long vsize = pe_get32 (dll, secptr1 + 16);
240 unsigned long fptr = pe_get32 (dll, secptr1 + 20);
242 bfd_seek (dll, (file_ptr) secptr1, SEEK_SET);
243 bfd_bread (sname, (bfd_size_type) 8, dll);
245 if (vaddr <= export_rva && vaddr + vsize > export_rva)
247 expptr = fptr + (export_rva - vaddr);
248 if (export_rva + export_size > vaddr + vsize)
249 export_size = vsize - (export_rva - vaddr);
250 break;
254 if (export_size == 0)
256 /* Empty export table. */
257 return;
260 /* Scan sections and store the base and size of the relevant sections. */
261 for (i = 0; i < nsections; i++)
263 unsigned long secptr1 = secptr + 40 * i;
264 unsigned long vsize = pe_get32 (dll, secptr1 + 8);
265 unsigned long vaddr = pe_get32 (dll, secptr1 + 12);
266 unsigned long flags = pe_get32 (dll, secptr1 + 36);
267 char sec_name[9];
268 int sectix;
270 sec_name[8] = '\0';
271 bfd_seek (dll, (file_ptr) secptr1 + 0, SEEK_SET);
272 bfd_bread (sec_name, (bfd_size_type) 8, dll);
274 sectix = read_pe_section_index (sec_name);
276 if (sectix != PE_SECTION_INDEX_INVALID)
278 section_data[sectix].rva_start = vaddr;
279 section_data[sectix].rva_end = vaddr + vsize;
283 expdata = (unsigned char *) xmalloc (export_size);
284 back_to = make_cleanup (xfree, expdata);
286 bfd_seek (dll, (file_ptr) expptr, SEEK_SET);
287 bfd_bread (expdata, (bfd_size_type) export_size, dll);
288 erva = expdata - export_rva;
290 nexp = pe_as32 (expdata + 24);
291 name_rvas = pe_as32 (expdata + 32);
292 ordinals = pe_as32 (expdata + 36);
293 ordbase = pe_as32 (expdata + 16);
294 exp_funcbase = pe_as32 (expdata + 28);
296 /* Use internal dll name instead of full pathname. */
297 dll_name = pe_as32 (expdata + 12) + erva;
299 bfd_map_over_sections (dll, get_section_vmas, section_data);
301 /* Adjust the vma_offsets in case this PE got relocated. This
302 assumes that *all* sections share the same relocation offset
303 as the text section. */
304 for (i = 0; i < PE_SECTION_TABLE_SIZE; i++)
306 section_data[i].vma_offset
307 += ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
310 /* Truncate name at first dot. Should maybe also convert to all
311 lower case for convenience on Windows. */
312 read_pe_truncate_name (dll_name);
314 /* Iterate through the list of symbols. */
315 for (i = 0; i < nexp; i++)
317 /* Pointer to the names vector. */
318 unsigned long name_rva = pe_as32 (erva + name_rvas + i * 4);
320 /* Pointer to the function address vector. */
321 unsigned long func_rva = pe_as32 (erva + exp_funcbase + i * 4);
323 /* Find this symbol's section in our own array. */
324 int sectix = 0;
326 for (sectix = 0; sectix < PE_SECTION_TABLE_SIZE; ++sectix)
328 if ((func_rva >= section_data[sectix].rva_start)
329 && (func_rva < section_data[sectix].rva_end))
331 add_pe_exported_sym (erva + name_rva,
332 func_rva,
333 section_data + sectix, dll_name, objfile);
334 break;
339 /* discard expdata. */
340 do_cleanups (back_to);