PR 14072
[external/binutils.git] / bfd / xsym.c
1 /* xSYM symbol-file support for BFD.
2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3    2009, 2010, 2011, 2012  Free Software Foundation, Inc.
4
5    This file is part of BFD, the Binary File Descriptor library.
6
7    This program 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 3 of the License, or
10    (at your option) any later version.
11
12    This program 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.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20    MA 02110-1301, USA.  */
21
22 /* xSYM is the debugging format used by CodeWarrior on Mac OS classic.  */
23
24 #include "sysdep.h"
25 #include "alloca-conf.h"
26 #include "xsym.h"
27 #include "bfd.h"
28 #include "libbfd.h"
29
30 #define bfd_sym_close_and_cleanup                   _bfd_generic_close_and_cleanup
31 #define bfd_sym_bfd_free_cached_info                _bfd_generic_bfd_free_cached_info
32 #define bfd_sym_new_section_hook                    _bfd_generic_new_section_hook
33 #define bfd_sym_bfd_is_local_label_name             bfd_generic_is_local_label_name
34 #define bfd_sym_bfd_is_target_special_symbol       ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
35 #define bfd_sym_get_lineno                          _bfd_nosymbols_get_lineno
36 #define bfd_sym_find_nearest_line                   _bfd_nosymbols_find_nearest_line
37 #define bfd_sym_find_inliner_info                   _bfd_nosymbols_find_inliner_info
38 #define bfd_sym_bfd_make_debug_symbol               _bfd_nosymbols_bfd_make_debug_symbol
39 #define bfd_sym_read_minisymbols                    _bfd_generic_read_minisymbols
40 #define bfd_sym_minisymbol_to_symbol                _bfd_generic_minisymbol_to_symbol
41 #define bfd_sym_set_arch_mach                       _bfd_generic_set_arch_mach
42 #define bfd_sym_get_section_contents                _bfd_generic_get_section_contents
43 #define bfd_sym_set_section_contents                _bfd_generic_set_section_contents
44 #define bfd_sym_bfd_get_relocated_section_contents  bfd_generic_get_relocated_section_contents
45 #define bfd_sym_bfd_relax_section                   bfd_generic_relax_section
46 #define bfd_sym_bfd_gc_sections                     bfd_generic_gc_sections
47 #define bfd_sym_bfd_lookup_section_flags            bfd_generic_lookup_section_flags
48 #define bfd_sym_bfd_merge_sections                  bfd_generic_merge_sections
49 #define bfd_sym_bfd_is_group_section                bfd_generic_is_group_section
50 #define bfd_sym_bfd_discard_group                   bfd_generic_discard_group
51 #define bfd_sym_section_already_linked              _bfd_generic_section_already_linked
52 #define bfd_sym_bfd_define_common_symbol            bfd_generic_define_common_symbol
53 #define bfd_sym_bfd_link_hash_table_create          _bfd_generic_link_hash_table_create
54 #define bfd_sym_bfd_link_hash_table_free            _bfd_generic_link_hash_table_free
55 #define bfd_sym_bfd_link_add_symbols                _bfd_generic_link_add_symbols
56 #define bfd_sym_bfd_link_just_syms                  _bfd_generic_link_just_syms
57 #define bfd_sym_bfd_copy_link_hash_symbol_type \
58   _bfd_generic_copy_link_hash_symbol_type
59 #define bfd_sym_bfd_final_link                      _bfd_generic_final_link
60 #define bfd_sym_bfd_link_split_section              _bfd_generic_link_split_section
61 #define bfd_sym_get_section_contents_in_window      _bfd_generic_get_section_contents_in_window
62
63 extern const bfd_target sym_vec;
64
65 static int
66 pstrcmp (const char *as, const char *bs)
67 {
68   const unsigned char *a = (const unsigned char *) as;
69   const unsigned char *b = (const unsigned char *) bs;
70   unsigned char clen;
71   int ret;
72
73   clen = (a[0] > b[0]) ? b[0] : a[0];
74   ret = memcmp (a + 1, b + 1, clen);
75   if (ret != 0)
76     return ret;
77
78   if (a[0] == b[0])
79     return 0;
80   else if (a[0] < b[0])
81     return -1;
82   else
83     return 1;
84 }
85
86 static unsigned long
87 compute_offset (unsigned long first_page,
88                 unsigned long page_size,
89                 unsigned long entry_size,
90                 unsigned long sym_index)
91 {
92   unsigned long entries_per_page = page_size / entry_size;
93   unsigned long page_number = first_page + (sym_index / entries_per_page);
94   unsigned long page_offset = (sym_index % entries_per_page) * entry_size;
95
96   return (page_number * page_size) + page_offset;
97 }
98
99 bfd_boolean
100 bfd_sym_mkobject (bfd *abfd ATTRIBUTE_UNUSED)
101 {
102   return 1;
103 }
104
105 void
106 bfd_sym_print_symbol (bfd *abfd ATTRIBUTE_UNUSED,
107                       void * afile ATTRIBUTE_UNUSED,
108                       asymbol *symbol ATTRIBUTE_UNUSED,
109                       bfd_print_symbol_type how ATTRIBUTE_UNUSED)
110 {
111   return;
112 }
113
114 bfd_boolean
115 bfd_sym_valid (bfd *abfd)
116 {
117   if (abfd == NULL || abfd->xvec == NULL)
118     return 0;
119
120   return abfd->xvec == &sym_vec;
121 }
122
123 unsigned char *
124 bfd_sym_read_name_table (bfd *abfd, bfd_sym_header_block *dshb)
125 {
126   unsigned char *rstr;
127   long ret;
128   size_t table_size = dshb->dshb_nte.dti_page_count * dshb->dshb_page_size;
129   size_t table_offset = dshb->dshb_nte.dti_first_page * dshb->dshb_page_size;
130
131   rstr = bfd_alloc (abfd, table_size);
132   if (rstr == NULL)
133     return rstr;
134
135   bfd_seek (abfd, table_offset, SEEK_SET);
136   ret = bfd_bread (rstr, table_size, abfd);
137   if (ret < 0 || (unsigned long) ret != table_size)
138     {
139       bfd_release (abfd, rstr);
140       return NULL;
141     }
142
143   return rstr;
144 }
145
146 void
147 bfd_sym_parse_file_reference_v32 (unsigned char *buf,
148                                   size_t len,
149                                   bfd_sym_file_reference *entry)
150 {
151   BFD_ASSERT (len == 6);
152
153   entry->fref_frte_index = bfd_getb16 (buf);
154   entry->fref_offset = bfd_getb32 (buf + 2);
155 }
156
157 void
158 bfd_sym_parse_disk_table_v32 (unsigned char *buf,
159                               size_t len,
160                               bfd_sym_table_info *table)
161 {
162   BFD_ASSERT (len == 8);
163
164   table->dti_first_page = bfd_getb16 (buf);
165   table->dti_page_count = bfd_getb16 (buf + 2);
166   table->dti_object_count = bfd_getb32 (buf + 4);
167 }
168
169 void
170 bfd_sym_parse_header_v32 (unsigned char *buf,
171                           size_t len,
172                           bfd_sym_header_block *header)
173 {
174   BFD_ASSERT (len == 154);
175
176   memcpy (header->dshb_id, buf, 32);
177   header->dshb_page_size = bfd_getb16 (buf + 32);
178   header->dshb_hash_page = bfd_getb16 (buf + 34);
179   header->dshb_root_mte = bfd_getb16 (buf + 36);
180   header->dshb_mod_date = bfd_getb32 (buf + 38);
181
182   bfd_sym_parse_disk_table_v32 (buf + 42, 8, &header->dshb_frte);
183   bfd_sym_parse_disk_table_v32 (buf + 50, 8, &header->dshb_rte);
184   bfd_sym_parse_disk_table_v32 (buf + 58, 8, &header->dshb_mte);
185   bfd_sym_parse_disk_table_v32 (buf + 66, 8, &header->dshb_cmte);
186   bfd_sym_parse_disk_table_v32 (buf + 74, 8, &header->dshb_cvte);
187   bfd_sym_parse_disk_table_v32 (buf + 82, 8, &header->dshb_csnte);
188   bfd_sym_parse_disk_table_v32 (buf + 90, 8, &header->dshb_clte);
189   bfd_sym_parse_disk_table_v32 (buf + 98, 8, &header->dshb_ctte);
190   bfd_sym_parse_disk_table_v32 (buf + 106, 8, &header->dshb_tte);
191   bfd_sym_parse_disk_table_v32 (buf + 114, 8, &header->dshb_nte);
192   bfd_sym_parse_disk_table_v32 (buf + 122, 8, &header->dshb_tinfo);
193   bfd_sym_parse_disk_table_v32 (buf + 130, 8, &header->dshb_fite);
194   bfd_sym_parse_disk_table_v32 (buf + 138, 8, &header->dshb_const);
195
196   memcpy (&header->dshb_file_creator, buf + 146, 4);
197   memcpy (&header->dshb_file_type, buf + 150, 4);
198 }
199
200 int
201 bfd_sym_read_header_v32 (bfd *abfd, bfd_sym_header_block *header)
202 {
203   unsigned char buf[154];
204   long ret;
205
206   ret = bfd_bread (buf, 154, abfd);
207   if (ret != 154)
208     return -1;
209
210   bfd_sym_parse_header_v32 (buf, 154, header);
211
212   return 0;
213 }
214
215 int
216 bfd_sym_read_header_v34 (bfd *abfd ATTRIBUTE_UNUSED,
217                          bfd_sym_header_block *header ATTRIBUTE_UNUSED)
218 {
219   abort ();
220 }
221
222 int
223 bfd_sym_read_header (bfd *abfd,
224                      bfd_sym_header_block *header,
225                      bfd_sym_version version)
226 {
227   switch (version)
228     {
229     case BFD_SYM_VERSION_3_5:
230     case BFD_SYM_VERSION_3_4:
231       return bfd_sym_read_header_v34 (abfd, header);
232     case BFD_SYM_VERSION_3_3:
233     case BFD_SYM_VERSION_3_2:
234       return bfd_sym_read_header_v32 (abfd, header);
235     case BFD_SYM_VERSION_3_1:
236     default:
237       return 0;
238     }
239 }
240
241 int
242 bfd_sym_read_version (bfd *abfd, bfd_sym_version *version)
243 {
244   char version_string[32];
245   long ret;
246
247   ret = bfd_bread (version_string, sizeof (version_string), abfd);
248   if (ret != sizeof (version_string))
249     return -1;
250
251   if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_1) == 0)
252     *version = BFD_SYM_VERSION_3_1;
253   else if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_2) == 0)
254     *version = BFD_SYM_VERSION_3_2;
255   else if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_3) == 0)
256     *version = BFD_SYM_VERSION_3_3;
257   else if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_4) == 0)
258     *version = BFD_SYM_VERSION_3_4;
259   else if (pstrcmp (version_string, BFD_SYM_VERSION_STR_3_5) == 0)
260     *version = BFD_SYM_VERSION_3_5;
261   else
262     return -1;
263
264   return 0;
265 }
266
267 void
268 bfd_sym_display_table_summary (FILE *f,
269                                bfd_sym_table_info *dti,
270                                const char *name)
271 {
272   fprintf (f, "%-6s %13ld %13ld %13ld\n",
273            name,
274            dti->dti_first_page,
275            dti->dti_page_count,
276            dti->dti_object_count);
277 }
278
279 void
280 bfd_sym_display_header (FILE *f, bfd_sym_header_block *dshb)
281 {
282   fprintf (f, "            Version: %.*s\n", dshb->dshb_id[0], dshb->dshb_id + 1);
283   fprintf (f, "          Page Size: 0x%x\n", dshb->dshb_page_size);
284   fprintf (f, "          Hash Page: %lu\n", dshb->dshb_hash_page);
285   fprintf (f, "           Root MTE: %lu\n", dshb->dshb_root_mte);
286   fprintf (f, "  Modification Date: ");
287   fprintf (f, "[unimplemented]");
288   fprintf (f, " (0x%lx)\n", dshb->dshb_mod_date);
289
290   fprintf (f, "       File Creator:  %.4s  Type: %.4s\n\n",
291            dshb->dshb_file_creator, dshb->dshb_file_type);
292
293   fprintf (f, "Table Name   First Page    Page Count   Object Count\n");
294   fprintf (f, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
295
296   bfd_sym_display_table_summary (f, &dshb->dshb_nte, "NTE");
297   bfd_sym_display_table_summary (f, &dshb->dshb_rte, "RTE");
298   bfd_sym_display_table_summary (f, &dshb->dshb_mte, "MTE");
299   bfd_sym_display_table_summary (f, &dshb->dshb_frte, "FRTE");
300   bfd_sym_display_table_summary (f, &dshb->dshb_cmte, "CMTE");
301   bfd_sym_display_table_summary (f, &dshb->dshb_cvte, "CVTE");
302   bfd_sym_display_table_summary (f, &dshb->dshb_csnte, "CSNTE");
303   bfd_sym_display_table_summary (f, &dshb->dshb_clte, "CLTE");
304   bfd_sym_display_table_summary (f, &dshb->dshb_ctte, "CTTE");
305   bfd_sym_display_table_summary (f, &dshb->dshb_tte, "TTE");
306   bfd_sym_display_table_summary (f, &dshb->dshb_tinfo, "TINFO");
307   bfd_sym_display_table_summary (f, &dshb->dshb_fite, "FITE");
308   bfd_sym_display_table_summary (f, &dshb->dshb_const, "CONST");
309
310   fprintf (f, "\n");
311 }
312
313 void
314 bfd_sym_parse_resources_table_entry_v32 (unsigned char *buf,
315                                          size_t len,
316                                          bfd_sym_resources_table_entry *entry)
317 {
318   BFD_ASSERT (len == 18);
319
320   memcpy (&entry->rte_res_type, buf, 4);
321   entry->rte_res_number = bfd_getb16 (buf + 4);
322   entry->rte_nte_index = bfd_getb32 (buf + 6);
323   entry->rte_mte_first = bfd_getb16 (buf + 10);
324   entry->rte_mte_last = bfd_getb16 (buf + 12);
325   entry->rte_res_size = bfd_getb32 (buf + 14);
326 }
327
328 void
329 bfd_sym_parse_modules_table_entry_v33 (unsigned char *buf,
330                                        size_t len,
331                                        bfd_sym_modules_table_entry *entry)
332 {
333   BFD_ASSERT (len == 46);
334
335   entry->mte_rte_index = bfd_getb16 (buf);
336   entry->mte_res_offset = bfd_getb32 (buf + 2);
337   entry->mte_size = bfd_getb32 (buf + 6);
338   entry->mte_kind = buf[10];
339   entry->mte_scope = buf[11];
340   entry->mte_parent = bfd_getb16 (buf + 12);
341   bfd_sym_parse_file_reference_v32 (buf + 14, 6, &entry->mte_imp_fref);
342   entry->mte_imp_end = bfd_getb32 (buf + 20);
343   entry->mte_nte_index = bfd_getb32 (buf + 24);
344   entry->mte_cmte_index = bfd_getb16 (buf + 28);
345   entry->mte_cvte_index = bfd_getb32 (buf + 30);
346   entry->mte_clte_index = bfd_getb16 (buf + 34);
347   entry->mte_ctte_index = bfd_getb16 (buf + 36);
348   entry->mte_csnte_idx_1 = bfd_getb32 (buf + 38);
349   entry->mte_csnte_idx_2 = bfd_getb32 (buf + 42);
350 }
351
352 void
353 bfd_sym_parse_file_references_table_entry_v32 (unsigned char *buf,
354                                                size_t len,
355                                                bfd_sym_file_references_table_entry *entry)
356 {
357   unsigned int type;
358
359   BFD_ASSERT (len == 10);
360
361   memset (entry, 0, sizeof (bfd_sym_file_references_table_entry));
362   type = bfd_getb16 (buf);
363
364   switch (type)
365     {
366     case BFD_SYM_END_OF_LIST_3_2:
367       entry->generic.type = BFD_SYM_END_OF_LIST;
368       break;
369
370     case BFD_SYM_FILE_NAME_INDEX_3_2:
371       entry->filename.type = BFD_SYM_FILE_NAME_INDEX;
372       entry->filename.nte_index = bfd_getb32 (buf + 2);
373       entry->filename.mod_date = bfd_getb32 (buf + 6);
374       break;
375
376     default:
377       entry->entry.mte_index = type;
378       entry->entry.file_offset = bfd_getb32 (buf + 2);
379     }
380 }
381
382 void
383 bfd_sym_parse_contained_modules_table_entry_v32 (unsigned char *buf,
384                                                  size_t len,
385                                                  bfd_sym_contained_modules_table_entry *entry)
386 {
387   unsigned int type;
388
389   BFD_ASSERT (len == 6);
390
391   memset (entry, 0, sizeof (bfd_sym_contained_modules_table_entry));
392   type = bfd_getb16 (buf);
393
394   switch (type)
395     {
396     case BFD_SYM_END_OF_LIST_3_2:
397       entry->generic.type = BFD_SYM_END_OF_LIST;
398       break;
399
400     default:
401       entry->entry.mte_index = type;
402       entry->entry.nte_index = bfd_getb32 (buf + 2);
403       break;
404     }
405 }
406
407 void
408 bfd_sym_parse_contained_variables_table_entry_v32 (unsigned char *buf,
409                                                    size_t len,
410                                                    bfd_sym_contained_variables_table_entry *entry)
411 {
412   unsigned int type;
413
414   BFD_ASSERT (len == 26);
415
416   memset (entry, 0, sizeof (bfd_sym_contained_variables_table_entry));
417   type = bfd_getb16 (buf);
418
419   switch (type)
420     {
421     case BFD_SYM_END_OF_LIST_3_2:
422       entry->generic.type = BFD_SYM_END_OF_LIST;
423       break;
424
425     case BFD_SYM_SOURCE_FILE_CHANGE_3_2:
426       entry->file.type = BFD_SYM_SOURCE_FILE_CHANGE;
427       bfd_sym_parse_file_reference_v32 (buf + 2, 6, &entry->file.fref);
428       break;
429
430     default:
431       entry->entry.tte_index = type;
432       entry->entry.nte_index = bfd_getb32 (buf + 2);
433       entry->entry.file_delta = bfd_getb16 (buf + 6);
434       entry->entry.scope = buf[8];
435       entry->entry.la_size = buf[9];
436
437       if (entry->entry.la_size == BFD_SYM_CVTE_SCA)
438         {
439           entry->entry.address.scstruct.sca_kind = buf[10];
440           entry->entry.address.scstruct.sca_class = buf[11];
441           entry->entry.address.scstruct.sca_offset = bfd_getb32 (buf + 12);
442         }
443       else if (entry->entry.la_size <= BFD_SYM_CVTE_SCA)
444         {
445 #if BFD_SYM_CVTE_SCA > 0
446           memcpy (&entry->entry.address.lastruct.la, buf + 10,
447                   BFD_SYM_CVTE_SCA);
448 #endif
449           entry->entry.address.lastruct.la_kind = buf[23];
450         }
451       else if (entry->entry.la_size == BFD_SYM_CVTE_BIG_LA)
452         {
453           entry->entry.address.biglastruct.big_la = bfd_getb32 (buf + 10);
454           entry->entry.address.biglastruct.big_la_kind = buf[12];
455         }
456     }
457 }
458
459 void
460 bfd_sym_parse_contained_statements_table_entry_v32 (unsigned char *buf,
461                                                     size_t len,
462                                                     bfd_sym_contained_statements_table_entry *entry)
463 {
464   unsigned int type;
465
466   BFD_ASSERT (len == 8);
467
468   memset (entry, 0, sizeof (bfd_sym_contained_statements_table_entry));
469   type = bfd_getb16 (buf);
470
471   switch (type)
472     {
473     case BFD_SYM_END_OF_LIST_3_2:
474       entry->generic.type = BFD_SYM_END_OF_LIST;
475       break;
476
477     case BFD_SYM_SOURCE_FILE_CHANGE_3_2:
478       entry->file.type = BFD_SYM_SOURCE_FILE_CHANGE;
479       bfd_sym_parse_file_reference_v32 (buf + 2, 6, &entry->file.fref);
480       break;
481
482     default:
483       entry->entry.mte_index = type;
484       entry->entry.mte_offset = bfd_getb16 (buf + 2);
485       entry->entry.file_delta = bfd_getb32 (buf + 4);
486       break;
487     }
488 }
489
490 void
491 bfd_sym_parse_contained_labels_table_entry_v32 (unsigned char *buf,
492                                                 size_t len,
493                                                 bfd_sym_contained_labels_table_entry *entry)
494 {
495   unsigned int type;
496
497   BFD_ASSERT (len == 12);
498
499   memset (entry, 0, sizeof (bfd_sym_contained_labels_table_entry));
500   type = bfd_getb16 (buf);
501
502   switch (type)
503     {
504     case BFD_SYM_END_OF_LIST_3_2:
505       entry->generic.type = BFD_SYM_END_OF_LIST;
506       break;
507
508     case BFD_SYM_SOURCE_FILE_CHANGE_3_2:
509       entry->file.type = BFD_SYM_SOURCE_FILE_CHANGE;
510       bfd_sym_parse_file_reference_v32 (buf + 2, 6, &entry->file.fref);
511       break;
512
513     default:
514       entry->entry.mte_index = type;
515       entry->entry.mte_offset = bfd_getb16 (buf + 2);
516       entry->entry.nte_index = bfd_getb32 (buf + 4);
517       entry->entry.file_delta = bfd_getb16 (buf + 8);
518       entry->entry.scope = bfd_getb16 (buf + 10);
519       break;
520     }
521 }
522
523 void
524 bfd_sym_parse_type_table_entry_v32 (unsigned char *buf,
525                                     size_t len,
526                                     bfd_sym_type_table_entry *entry)
527 {
528   BFD_ASSERT (len == 4);
529
530   *entry = bfd_getb32 (buf);
531 }
532
533 int
534 bfd_sym_fetch_resources_table_entry (bfd *abfd,
535                                      bfd_sym_resources_table_entry *entry,
536                                      unsigned long sym_index)
537 {
538   void (*parser) (unsigned char *, size_t, bfd_sym_resources_table_entry *);
539   unsigned long offset;
540   unsigned long entry_size;
541   unsigned char buf[18];
542   bfd_sym_data_struct *sdata = NULL;
543
544   parser = NULL;
545   BFD_ASSERT (bfd_sym_valid (abfd));
546   sdata = abfd->tdata.sym_data;
547
548   if (sym_index == 0)
549     return -1;
550
551   switch (sdata->version)
552     {
553     case BFD_SYM_VERSION_3_5:
554     case BFD_SYM_VERSION_3_4:
555       return -1;
556
557     case BFD_SYM_VERSION_3_3:
558     case BFD_SYM_VERSION_3_2:
559       entry_size = 18;
560       parser = bfd_sym_parse_resources_table_entry_v32;
561       break;
562
563     case BFD_SYM_VERSION_3_1:
564     default:
565       return -1;
566     }
567   if (parser == NULL)
568     return -1;
569
570   offset = compute_offset (sdata->header.dshb_rte.dti_first_page,
571                            sdata->header.dshb_page_size,
572                            entry_size, sym_index);
573
574   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
575     return -1;
576   if (bfd_bread (buf, entry_size, abfd) != entry_size)
577     return -1;
578
579   (*parser) (buf, entry_size, entry);
580
581   return 0;
582 }
583
584 int
585 bfd_sym_fetch_modules_table_entry (bfd *abfd,
586                                    bfd_sym_modules_table_entry *entry,
587                                    unsigned long sym_index)
588 {
589   void (*parser) (unsigned char *, size_t, bfd_sym_modules_table_entry *);
590   unsigned long offset;
591   unsigned long entry_size;
592   unsigned char buf[46];
593   bfd_sym_data_struct *sdata = NULL;
594
595   parser = NULL;
596   BFD_ASSERT (bfd_sym_valid (abfd));
597   sdata = abfd->tdata.sym_data;
598
599   if (sym_index == 0)
600     return -1;
601
602   switch (sdata->version)
603     {
604     case BFD_SYM_VERSION_3_5:
605     case BFD_SYM_VERSION_3_4:
606       return -1;
607
608     case BFD_SYM_VERSION_3_3:
609       entry_size = 46;
610       parser = bfd_sym_parse_modules_table_entry_v33;
611       break;
612
613     case BFD_SYM_VERSION_3_2:
614     case BFD_SYM_VERSION_3_1:
615     default:
616       return -1;
617     }
618   if (parser == NULL)
619     return -1;
620
621   offset = compute_offset (sdata->header.dshb_mte.dti_first_page,
622                            sdata->header.dshb_page_size,
623                            entry_size, sym_index);
624
625   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
626     return -1;
627   if (bfd_bread (buf, entry_size, abfd) != entry_size)
628     return -1;
629
630   (*parser) (buf, entry_size, entry);
631
632   return 0;
633 }
634
635 int
636 bfd_sym_fetch_file_references_table_entry (bfd *abfd,
637                                            bfd_sym_file_references_table_entry *entry,
638                                            unsigned long sym_index)
639 {
640   void (*parser) (unsigned char *, size_t, bfd_sym_file_references_table_entry *);
641   unsigned long offset;
642   unsigned long entry_size = 0;
643   unsigned char buf[8];
644   bfd_sym_data_struct *sdata = NULL;
645
646   parser = NULL;
647   BFD_ASSERT (bfd_sym_valid (abfd));
648   sdata = abfd->tdata.sym_data;
649
650   if (sym_index == 0)
651     return -1;
652
653   switch (sdata->version)
654     {
655     case BFD_SYM_VERSION_3_3:
656     case BFD_SYM_VERSION_3_2:
657       entry_size = 10;
658       parser = bfd_sym_parse_file_references_table_entry_v32;
659       break;
660
661     case BFD_SYM_VERSION_3_5:
662     case BFD_SYM_VERSION_3_4:
663     case BFD_SYM_VERSION_3_1:
664     default:
665       break;
666     }
667
668   if (parser == NULL)
669     return -1;
670
671   offset = compute_offset (sdata->header.dshb_frte.dti_first_page,
672                            sdata->header.dshb_page_size,
673                            entry_size, sym_index);
674
675   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
676     return -1;
677   if (bfd_bread (buf, entry_size, abfd) != entry_size)
678     return -1;
679
680   (*parser) (buf, entry_size, entry);
681
682   return 0;
683 }
684
685 int
686 bfd_sym_fetch_contained_modules_table_entry (bfd *abfd,
687                                              bfd_sym_contained_modules_table_entry *entry,
688                                              unsigned long sym_index)
689 {
690   void (*parser) (unsigned char *, size_t, bfd_sym_contained_modules_table_entry *);
691   unsigned long offset;
692   unsigned long entry_size = 0;
693   unsigned char buf[6];
694   bfd_sym_data_struct *sdata = NULL;
695
696   parser = NULL;
697   BFD_ASSERT (bfd_sym_valid (abfd));
698   sdata = abfd->tdata.sym_data;
699
700   if (sym_index == 0)
701     return -1;
702
703   switch (sdata->version)
704     {
705     case BFD_SYM_VERSION_3_3:
706     case BFD_SYM_VERSION_3_2:
707       entry_size = 6;
708       parser = bfd_sym_parse_contained_modules_table_entry_v32;
709       break;
710
711     case BFD_SYM_VERSION_3_5:
712     case BFD_SYM_VERSION_3_4:
713     case BFD_SYM_VERSION_3_1:
714     default:
715       break;
716     }
717
718   if (parser == NULL)
719     return -1;
720
721   offset = compute_offset (sdata->header.dshb_cmte.dti_first_page,
722                            sdata->header.dshb_page_size,
723                            entry_size, sym_index);
724
725   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
726     return -1;
727   if (bfd_bread (buf, entry_size, abfd) != entry_size)
728     return -1;
729
730   (*parser) (buf, entry_size, entry);
731
732   return 0;
733 }
734
735 int
736 bfd_sym_fetch_contained_variables_table_entry (bfd *abfd,
737                                                bfd_sym_contained_variables_table_entry *entry,
738                                                unsigned long sym_index)
739 {
740   void (*parser) (unsigned char *, size_t, bfd_sym_contained_variables_table_entry *);
741   unsigned long offset;
742   unsigned long entry_size = 0;
743   unsigned char buf[26];
744   bfd_sym_data_struct *sdata = NULL;
745
746   parser = NULL;
747   BFD_ASSERT (bfd_sym_valid (abfd));
748   sdata = abfd->tdata.sym_data;
749
750   if (sym_index == 0)
751     return -1;
752
753   switch (sdata->version)
754     {
755     case BFD_SYM_VERSION_3_3:
756     case BFD_SYM_VERSION_3_2:
757       entry_size = 26;
758       parser = bfd_sym_parse_contained_variables_table_entry_v32;
759       break;
760
761     case BFD_SYM_VERSION_3_5:
762     case BFD_SYM_VERSION_3_4:
763     case BFD_SYM_VERSION_3_1:
764     default:
765       break;
766     }
767
768   if (parser == NULL)
769     return -1;
770
771   offset = compute_offset (sdata->header.dshb_cvte.dti_first_page,
772                            sdata->header.dshb_page_size,
773                            entry_size, sym_index);
774
775   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
776     return -1;
777   if (bfd_bread (buf, entry_size, abfd) != entry_size)
778     return -1;
779
780   (*parser) (buf, entry_size, entry);
781
782   return 0;
783 }
784
785 int
786 bfd_sym_fetch_contained_statements_table_entry (bfd *abfd,
787                                                 bfd_sym_contained_statements_table_entry *entry,
788                                                 unsigned long sym_index)
789 {
790   void (*parser) (unsigned char *, size_t, bfd_sym_contained_statements_table_entry *);
791   unsigned long offset;
792   unsigned long entry_size = 0;
793   unsigned char buf[8];
794   bfd_sym_data_struct *sdata = NULL;
795
796   parser = NULL;
797   BFD_ASSERT (bfd_sym_valid (abfd));
798   sdata = abfd->tdata.sym_data;
799
800   if (sym_index == 0)
801     return -1;
802
803   switch (sdata->version)
804     {
805     case BFD_SYM_VERSION_3_3:
806     case BFD_SYM_VERSION_3_2:
807       entry_size = 8;
808       parser = bfd_sym_parse_contained_statements_table_entry_v32;
809       break;
810
811     case BFD_SYM_VERSION_3_5:
812     case BFD_SYM_VERSION_3_4:
813     case BFD_SYM_VERSION_3_1:
814     default:
815       break;
816     }
817
818   if (parser == NULL)
819     return -1;
820
821   offset = compute_offset (sdata->header.dshb_csnte.dti_first_page,
822                            sdata->header.dshb_page_size,
823                            entry_size, sym_index);
824
825   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
826     return -1;
827   if (bfd_bread (buf, entry_size, abfd) != entry_size)
828     return -1;
829
830   (*parser) (buf, entry_size, entry);
831
832   return 0;
833 }
834
835 int
836 bfd_sym_fetch_contained_labels_table_entry (bfd *abfd,
837                                             bfd_sym_contained_labels_table_entry *entry,
838                                             unsigned long sym_index)
839 {
840   void (*parser) (unsigned char *, size_t, bfd_sym_contained_labels_table_entry *);
841   unsigned long offset;
842   unsigned long entry_size = 0;
843   unsigned char buf[12];
844   bfd_sym_data_struct *sdata = NULL;
845
846   parser = NULL;
847   BFD_ASSERT (bfd_sym_valid (abfd));
848   sdata = abfd->tdata.sym_data;
849
850   if (sym_index == 0)
851     return -1;
852
853   switch (sdata->version)
854     {
855     case BFD_SYM_VERSION_3_3:
856     case BFD_SYM_VERSION_3_2:
857       entry_size = 12;
858       parser = bfd_sym_parse_contained_labels_table_entry_v32;
859       break;
860
861     case BFD_SYM_VERSION_3_5:
862     case BFD_SYM_VERSION_3_4:
863     case BFD_SYM_VERSION_3_1:
864     default:
865       break;
866     }
867
868   if (parser == NULL)
869     return -1;
870
871   offset = compute_offset (sdata->header.dshb_clte.dti_first_page,
872                            sdata->header.dshb_page_size,
873                            entry_size, sym_index);
874
875   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
876     return -1;
877   if (bfd_bread (buf, entry_size, abfd) != entry_size)
878     return -1;
879
880   (*parser) (buf, entry_size, entry);
881
882   return 0;
883 }
884
885 int
886 bfd_sym_fetch_contained_types_table_entry (bfd *abfd,
887                                            bfd_sym_contained_types_table_entry *entry,
888                                            unsigned long sym_index)
889 {
890   void (*parser) (unsigned char *, size_t, bfd_sym_contained_types_table_entry *);
891   unsigned long offset;
892   unsigned long entry_size = 0;
893   unsigned char buf[0];
894   bfd_sym_data_struct *sdata = NULL;
895
896   parser = NULL;
897   BFD_ASSERT (bfd_sym_valid (abfd));
898   sdata = abfd->tdata.sym_data;
899
900   if (sym_index == 0)
901     return -1;
902
903   switch (sdata->version)
904     {
905     case BFD_SYM_VERSION_3_3:
906     case BFD_SYM_VERSION_3_2:
907       entry_size = 0;
908       parser = NULL;
909       break;
910
911     case BFD_SYM_VERSION_3_5:
912     case BFD_SYM_VERSION_3_4:
913     case BFD_SYM_VERSION_3_1:
914     default:
915       break;
916     }
917
918   if (parser == NULL)
919     return -1;
920
921   offset = compute_offset (sdata->header.dshb_ctte.dti_first_page,
922                            sdata->header.dshb_page_size,
923                            entry_size, sym_index);
924
925   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
926     return -1;
927   if (bfd_bread (buf, entry_size, abfd) != entry_size)
928     return -1;
929
930   (*parser) (buf, entry_size, entry);
931
932   return 0;
933 }
934
935 int
936 bfd_sym_fetch_file_references_index_table_entry (bfd *abfd,
937                                                  bfd_sym_file_references_index_table_entry *entry,
938                                                  unsigned long sym_index)
939 {
940   void (*parser) (unsigned char *, size_t, bfd_sym_file_references_index_table_entry *);
941   unsigned long offset;
942   unsigned long entry_size = 0;
943   unsigned char buf[0];
944   bfd_sym_data_struct *sdata = NULL;
945
946   parser = NULL;
947   BFD_ASSERT (bfd_sym_valid (abfd));
948   sdata = abfd->tdata.sym_data;
949
950   if (sym_index == 0)
951     return -1;
952
953   switch (sdata->version)
954     {
955     case BFD_SYM_VERSION_3_3:
956     case BFD_SYM_VERSION_3_2:
957       entry_size = 0;
958       parser = NULL;
959       break;
960
961     case BFD_SYM_VERSION_3_5:
962     case BFD_SYM_VERSION_3_4:
963     case BFD_SYM_VERSION_3_1:
964     default:
965       break;
966     }
967
968   if (parser == NULL)
969     return -1;
970
971   offset = compute_offset (sdata->header.dshb_fite.dti_first_page,
972                            sdata->header.dshb_page_size,
973                            entry_size, sym_index);
974
975   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
976     return -1;
977   if (bfd_bread (buf, entry_size, abfd) != entry_size)
978     return -1;
979
980   (*parser) (buf, entry_size, entry);
981
982   return 0;
983 }
984
985 int
986 bfd_sym_fetch_constant_pool_entry (bfd *abfd,
987                                    bfd_sym_constant_pool_entry *entry,
988                                    unsigned long sym_index)
989 {
990   void (*parser) (unsigned char *, size_t, bfd_sym_constant_pool_entry *);
991   unsigned long offset;
992   unsigned long entry_size = 0;
993   unsigned char buf[0];
994   bfd_sym_data_struct *sdata = NULL;
995
996   parser = NULL;
997   BFD_ASSERT (bfd_sym_valid (abfd));
998   sdata = abfd->tdata.sym_data;
999
1000   if (sym_index == 0)
1001     return -1;
1002
1003   switch (sdata->version)
1004     {
1005     case BFD_SYM_VERSION_3_3:
1006     case BFD_SYM_VERSION_3_2:
1007       entry_size = 0;
1008       parser = NULL;
1009       break;
1010
1011     case BFD_SYM_VERSION_3_5:
1012     case BFD_SYM_VERSION_3_4:
1013     case BFD_SYM_VERSION_3_1:
1014     default:
1015       break;
1016     }
1017
1018   if (parser == NULL)
1019     return -1;
1020
1021   offset = compute_offset (sdata->header.dshb_fite.dti_first_page,
1022                            sdata->header.dshb_page_size,
1023                            entry_size, sym_index);
1024
1025   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
1026     return -1;
1027   if (bfd_bread (buf, entry_size, abfd) != entry_size)
1028     return -1;
1029
1030   (*parser) (buf, entry_size, entry);
1031
1032   return 0;
1033 }
1034
1035 int
1036 bfd_sym_fetch_type_table_entry (bfd *abfd,
1037                                 bfd_sym_type_table_entry *entry,
1038                                 unsigned long sym_index)
1039 {
1040   void (*parser) (unsigned char *, size_t, bfd_sym_type_table_entry *);
1041   unsigned long offset;
1042   unsigned long entry_size = 0;
1043   unsigned char buf[4];
1044   bfd_sym_data_struct *sdata = NULL;
1045
1046   parser = NULL;
1047   BFD_ASSERT (bfd_sym_valid (abfd));
1048   sdata = abfd->tdata.sym_data;
1049
1050   switch (sdata->version)
1051     {
1052     case BFD_SYM_VERSION_3_3:
1053     case BFD_SYM_VERSION_3_2:
1054       entry_size = 4;
1055       parser = bfd_sym_parse_type_table_entry_v32;
1056       break;
1057
1058     case BFD_SYM_VERSION_3_5:
1059     case BFD_SYM_VERSION_3_4:
1060     case BFD_SYM_VERSION_3_1:
1061     default:
1062       break;
1063     }
1064
1065   if (parser == NULL)
1066     return -1;
1067
1068   offset = compute_offset (sdata->header.dshb_tte.dti_first_page,
1069                            sdata->header.dshb_page_size,
1070                            entry_size, sym_index);
1071
1072   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
1073     return -1;
1074   if (bfd_bread (buf, entry_size, abfd) != entry_size)
1075     return -1;
1076
1077   (*parser) (buf, entry_size, entry);
1078
1079   return 0;
1080 }
1081
1082 int
1083 bfd_sym_fetch_type_information_table_entry (bfd *abfd,
1084                                             bfd_sym_type_information_table_entry *entry,
1085                                             unsigned long offset)
1086 {
1087   unsigned char buf[4];
1088
1089   BFD_ASSERT (bfd_sym_valid (abfd));
1090
1091   if (offset == 0)
1092     return -1;
1093
1094   if (bfd_seek (abfd, offset, SEEK_SET) < 0)
1095     return -1;
1096
1097   if (bfd_bread (buf, 4, abfd) != 4)
1098     return -1;
1099   entry->nte_index = bfd_getb32 (buf);
1100
1101   if (bfd_bread (buf, 2, abfd) != 2)
1102     return -1;
1103   entry->physical_size = bfd_getb16 (buf);
1104
1105   if (entry->physical_size & 0x8000)
1106     {
1107       if (bfd_bread (buf, 4, abfd) != 4)
1108         return -1;
1109       entry->physical_size &= 0x7fff;
1110       entry->logical_size = bfd_getb32 (buf);
1111       entry->offset = offset + 10;
1112     }
1113   else
1114     {
1115       if (bfd_bread (buf, 2, abfd) != 2)
1116         return -1;
1117       entry->physical_size &= 0x7fff;
1118       entry->logical_size = bfd_getb16 (buf);
1119       entry->offset = offset + 8;
1120     }
1121
1122   return 0;
1123 }
1124
1125 int
1126 bfd_sym_fetch_type_table_information (bfd *abfd,
1127                                       bfd_sym_type_information_table_entry *entry,
1128                                       unsigned long sym_index)
1129 {
1130   bfd_sym_type_table_entry tindex;
1131   bfd_sym_data_struct *sdata = NULL;
1132
1133   BFD_ASSERT (bfd_sym_valid (abfd));
1134   sdata = abfd->tdata.sym_data;
1135
1136   if (sdata->header.dshb_tte.dti_object_count <= 99)
1137     return -1;
1138   if (sym_index < 100)
1139     return -1;
1140
1141   if (bfd_sym_fetch_type_table_entry (abfd, &tindex, sym_index - 100) < 0)
1142     return -1;
1143   if (bfd_sym_fetch_type_information_table_entry (abfd, entry, tindex) < 0)
1144     return -1;
1145
1146   return 0;
1147 }
1148
1149 const unsigned char *
1150 bfd_sym_symbol_name (bfd *abfd, unsigned long sym_index)
1151 {
1152   bfd_sym_data_struct *sdata = NULL;
1153
1154   BFD_ASSERT (bfd_sym_valid (abfd));
1155   sdata = abfd->tdata.sym_data;
1156
1157   if (sym_index == 0)
1158     return (const unsigned char *) "";
1159
1160   sym_index *= 2;
1161   if ((sym_index / sdata->header.dshb_page_size)
1162       > sdata->header.dshb_nte.dti_page_count)
1163     return (const unsigned char *) "\09[INVALID]";
1164
1165   return (const unsigned char *) sdata->name_table + sym_index;
1166 }
1167
1168 const unsigned char *
1169 bfd_sym_module_name (bfd *abfd, unsigned long sym_index)
1170 {
1171   bfd_sym_modules_table_entry entry;
1172
1173   if (bfd_sym_fetch_modules_table_entry (abfd, &entry, sym_index) < 0)
1174     return (const unsigned char *) "\09[INVALID]";
1175
1176   return bfd_sym_symbol_name (abfd, entry.mte_nte_index);
1177 }
1178
1179 const char *
1180 bfd_sym_unparse_storage_kind (enum bfd_sym_storage_kind kind)
1181 {
1182   switch (kind)
1183     {
1184     case BFD_SYM_STORAGE_KIND_LOCAL: return "LOCAL";
1185     case BFD_SYM_STORAGE_KIND_VALUE: return "VALUE";
1186     case BFD_SYM_STORAGE_KIND_REFERENCE: return "REFERENCE";
1187     case BFD_SYM_STORAGE_KIND_WITH: return "WITH";
1188     default: return "[UNKNOWN]";
1189     }
1190 }
1191
1192 const char *
1193 bfd_sym_unparse_storage_class (enum bfd_sym_storage_class kind)
1194 {
1195   switch (kind)
1196     {
1197     case BFD_SYM_STORAGE_CLASS_REGISTER: return "REGISTER";
1198     case BFD_SYM_STORAGE_CLASS_GLOBAL: return "GLOBAL";
1199     case BFD_SYM_STORAGE_CLASS_FRAME_RELATIVE: return "FRAME_RELATIVE";
1200     case BFD_SYM_STORAGE_CLASS_STACK_RELATIVE: return "STACK_RELATIVE";
1201     case BFD_SYM_STORAGE_CLASS_ABSOLUTE: return "ABSOLUTE";
1202     case BFD_SYM_STORAGE_CLASS_CONSTANT: return "CONSTANT";
1203     case BFD_SYM_STORAGE_CLASS_RESOURCE: return "RESOURCE";
1204     case BFD_SYM_STORAGE_CLASS_BIGCONSTANT: return "BIGCONSTANT";
1205     default: return "[UNKNOWN]";
1206     }
1207 }
1208
1209 const char *
1210 bfd_sym_unparse_module_kind (enum bfd_sym_module_kind kind)
1211 {
1212   switch (kind)
1213     {
1214     case BFD_SYM_MODULE_KIND_NONE: return "NONE";
1215     case BFD_SYM_MODULE_KIND_PROGRAM: return "PROGRAM";
1216     case BFD_SYM_MODULE_KIND_UNIT: return "UNIT";
1217     case BFD_SYM_MODULE_KIND_PROCEDURE: return "PROCEDURE";
1218     case BFD_SYM_MODULE_KIND_FUNCTION: return "FUNCTION";
1219     case BFD_SYM_MODULE_KIND_DATA: return "DATA";
1220     case BFD_SYM_MODULE_KIND_BLOCK: return "BLOCK";
1221     default: return "[UNKNOWN]";
1222     }
1223 }
1224
1225 const char *
1226 bfd_sym_unparse_symbol_scope (enum bfd_sym_symbol_scope scope)
1227 {
1228   switch (scope)
1229     {
1230     case BFD_SYM_SYMBOL_SCOPE_LOCAL: return "LOCAL";
1231     case BFD_SYM_SYMBOL_SCOPE_GLOBAL: return "GLOBAL";
1232     default:
1233       return "[UNKNOWN]";
1234     }
1235 }
1236
1237 void
1238 bfd_sym_print_file_reference (bfd *abfd,
1239                               FILE *f,
1240                               bfd_sym_file_reference *entry)
1241 {
1242   bfd_sym_file_references_table_entry frtentry;
1243   int ret;
1244
1245   ret = bfd_sym_fetch_file_references_table_entry (abfd, &frtentry,
1246                                                    entry->fref_frte_index);
1247   fprintf (f, "FILE ");
1248
1249   if ((ret < 0) || (frtentry.generic.type != BFD_SYM_FILE_NAME_INDEX))
1250     fprintf (f, "[INVALID]");
1251   else
1252     fprintf (f, "\"%.*s\"",
1253              bfd_sym_symbol_name (abfd, frtentry.filename.nte_index)[0],
1254              &bfd_sym_symbol_name (abfd, frtentry.filename.nte_index)[1]);
1255
1256   fprintf (f, " (FRTE %lu)", entry->fref_frte_index);
1257 }
1258
1259 void
1260 bfd_sym_print_resources_table_entry (bfd *abfd,
1261                                      FILE *f,
1262                                      bfd_sym_resources_table_entry *entry)
1263 {
1264   fprintf (f, " \"%.*s\" (NTE %lu), type \"%.4s\", num %u, size %lu, MTE %lu -- %lu",
1265            bfd_sym_symbol_name (abfd, entry->rte_nte_index)[0],
1266            &bfd_sym_symbol_name (abfd, entry->rte_nte_index)[1],
1267            entry->rte_nte_index, entry->rte_res_type, entry->rte_res_number,
1268            entry->rte_res_size, entry->rte_mte_first, entry->rte_mte_last);
1269 }
1270
1271 void
1272 bfd_sym_print_modules_table_entry (bfd *abfd,
1273                                    FILE *f,
1274                                    bfd_sym_modules_table_entry *entry)
1275 {
1276   fprintf (f, "\"%.*s\" (NTE %lu)",
1277            bfd_sym_symbol_name (abfd, entry->mte_nte_index)[0],
1278            &bfd_sym_symbol_name (abfd, entry->mte_nte_index)[1],
1279            entry->mte_nte_index);
1280
1281   fprintf (f, "\n            ");
1282
1283   bfd_sym_print_file_reference (abfd, f, &entry->mte_imp_fref);
1284   fprintf (f, " range %lu -- %lu",
1285            entry->mte_imp_fref.fref_offset, entry->mte_imp_end);
1286
1287   fprintf (f, "\n            ");
1288
1289   fprintf (f, "kind %s", bfd_sym_unparse_module_kind (entry->mte_kind));
1290   fprintf (f, ", scope %s", bfd_sym_unparse_symbol_scope (entry->mte_scope));
1291
1292   fprintf (f, ", RTE %lu, offset %lu, size %lu",
1293            entry->mte_rte_index, entry->mte_res_offset, entry->mte_size);
1294
1295   fprintf (f, "\n            ");
1296
1297   fprintf (f, "CMTE %lu, CVTE %lu, CLTE %lu, CTTE %lu, CSNTE1 %lu, CSNTE2 %lu",
1298            entry->mte_cmte_index, entry->mte_cvte_index,
1299            entry->mte_clte_index, entry->mte_ctte_index,
1300            entry->mte_csnte_idx_1, entry->mte_csnte_idx_2);
1301
1302   if (entry->mte_parent != 0)
1303     fprintf (f, ", parent %lu", entry->mte_parent);
1304   else
1305     fprintf (f, ", no parent");
1306
1307   if (entry->mte_cmte_index != 0)
1308     fprintf (f, ", child %lu", entry->mte_cmte_index);
1309   else
1310     fprintf (f, ", no child");
1311 }
1312
1313 void
1314 bfd_sym_print_file_references_table_entry (bfd *abfd,
1315                                            FILE *f,
1316                                            bfd_sym_file_references_table_entry *entry)
1317 {
1318   switch (entry->generic.type)
1319     {
1320     case BFD_SYM_FILE_NAME_INDEX:
1321       fprintf (f, "FILE \"%.*s\" (NTE %lu), modtime ",
1322                bfd_sym_symbol_name (abfd, entry->filename.nte_index)[0],
1323                &bfd_sym_symbol_name (abfd, entry->filename.nte_index)[1],
1324                entry->filename.nte_index);
1325
1326       fprintf (f, "[UNIMPLEMENTED]");
1327       /* printModDate (entry->filename.mod_date); */
1328       fprintf (f, " (0x%lx)", entry->filename.mod_date);
1329       break;
1330
1331     case BFD_SYM_END_OF_LIST:
1332       fprintf (f, "END");
1333       break;
1334
1335     default:
1336       fprintf (f, "\"%.*s\" (MTE %lu), offset %lu",
1337                bfd_sym_module_name (abfd, entry->entry.mte_index)[0],
1338                &bfd_sym_module_name (abfd, entry->entry.mte_index)[1],
1339                entry->entry.mte_index,
1340                entry->entry.file_offset);
1341       break;
1342     }
1343 }
1344
1345 void
1346 bfd_sym_print_contained_modules_table_entry (bfd *abfd,
1347                                              FILE *f,
1348                                              bfd_sym_contained_modules_table_entry *entry)
1349 {
1350   switch (entry->generic.type)
1351     {
1352     case BFD_SYM_END_OF_LIST:
1353       fprintf (f, "END");
1354       break;
1355
1356     default:
1357       fprintf (f, "\"%.*s\" (MTE %lu, NTE %lu)",
1358                bfd_sym_module_name (abfd, entry->entry.mte_index)[0],
1359                &bfd_sym_module_name (abfd, entry->entry.mte_index)[1],
1360                entry->entry.mte_index,
1361                entry->entry.nte_index);
1362       break;
1363     }
1364 }
1365
1366 void
1367 bfd_sym_print_contained_variables_table_entry (bfd *abfd,
1368                                                FILE *f,
1369                                                bfd_sym_contained_variables_table_entry *entry)
1370 {
1371   if (entry->generic.type == BFD_SYM_END_OF_LIST)
1372     {
1373       fprintf (f, "END");
1374       return;
1375     }
1376
1377   if (entry->generic.type == BFD_SYM_SOURCE_FILE_CHANGE)
1378     {
1379       bfd_sym_print_file_reference (abfd, f, &entry->file.fref);
1380       fprintf (f, " offset %lu", entry->file.fref.fref_offset);
1381       return;
1382     }
1383
1384   fprintf (f, "\"%.*s\" (NTE %lu)",
1385            bfd_sym_symbol_name (abfd, entry->entry.nte_index)[0],
1386            &bfd_sym_symbol_name (abfd, entry->entry.nte_index)[1],
1387            entry->entry.nte_index);
1388
1389   fprintf (f, ", TTE %lu", entry->entry.tte_index);
1390   fprintf (f, ", offset %lu", entry->entry.file_delta);
1391   fprintf (f, ", scope %s", bfd_sym_unparse_symbol_scope (entry->entry.scope));
1392
1393   if (entry->entry.la_size == BFD_SYM_CVTE_SCA)
1394     fprintf (f, ", latype %s, laclass %s, laoffset %lu",
1395              bfd_sym_unparse_storage_kind (entry->entry.address.scstruct.sca_kind),
1396              bfd_sym_unparse_storage_class (entry->entry.address.scstruct.sca_class),
1397              entry->entry.address.scstruct.sca_offset);
1398   else if (entry->entry.la_size <= BFD_SYM_CVTE_LA_MAX_SIZE)
1399     {
1400       unsigned long i;
1401
1402       fprintf (f, ", la [");
1403       for (i = 0; i < entry->entry.la_size; i++)
1404         fprintf (f, "0x%02x ", entry->entry.address.lastruct.la[i]);
1405       fprintf (f, "]");
1406     }
1407   else if (entry->entry.la_size == BFD_SYM_CVTE_BIG_LA)
1408     fprintf (f, ", bigla %lu, biglakind %u",
1409              entry->entry.address.biglastruct.big_la,
1410              entry->entry.address.biglastruct.big_la_kind);
1411
1412   else
1413     fprintf (f, ", la [INVALID]");
1414 }
1415
1416 void
1417 bfd_sym_print_contained_statements_table_entry (bfd *abfd,
1418                                                 FILE *f,
1419                                                 bfd_sym_contained_statements_table_entry *entry)
1420 {
1421   if (entry->generic.type == BFD_SYM_END_OF_LIST)
1422     {
1423       fprintf (f, "END");
1424       return;
1425     }
1426
1427   if (entry->generic.type == BFD_SYM_SOURCE_FILE_CHANGE)
1428     {
1429       bfd_sym_print_file_reference (abfd, f, &entry->file.fref);
1430       fprintf (f, " offset %lu", entry->file.fref.fref_offset);
1431       return;
1432     }
1433
1434   fprintf (f, "\"%.*s\" (MTE %lu), offset %lu, delta %lu",
1435            bfd_sym_module_name (abfd, entry->entry.mte_index)[0],
1436            &bfd_sym_module_name (abfd, entry->entry.mte_index)[1],
1437            entry->entry.mte_index,
1438            entry->entry.mte_offset,
1439            entry->entry.file_delta);
1440 }
1441
1442 void
1443 bfd_sym_print_contained_labels_table_entry (bfd *abfd,
1444                                             FILE *f,
1445                                             bfd_sym_contained_labels_table_entry *entry)
1446 {
1447   if (entry->generic.type == BFD_SYM_END_OF_LIST)
1448     {
1449       fprintf (f, "END");
1450       return;
1451     }
1452
1453   if (entry->generic.type == BFD_SYM_SOURCE_FILE_CHANGE)
1454     {
1455       bfd_sym_print_file_reference (abfd, f, &entry->file.fref);
1456       fprintf (f, " offset %lu", entry->file.fref.fref_offset);
1457       return;
1458     }
1459
1460   fprintf (f, "\"%.*s\" (MTE %lu), offset %lu, delta %lu, scope %s",
1461            bfd_sym_module_name (abfd, entry->entry.mte_index)[0],
1462            &bfd_sym_module_name (abfd, entry->entry.mte_index)[1],
1463            entry->entry.mte_index,
1464            entry->entry.mte_offset,
1465            entry->entry.file_delta,
1466            bfd_sym_unparse_symbol_scope (entry->entry.scope));
1467 }
1468
1469 void
1470 bfd_sym_print_contained_types_table_entry (bfd *abfd ATTRIBUTE_UNUSED,
1471                                            FILE *f,
1472                                            bfd_sym_contained_types_table_entry *entry ATTRIBUTE_UNUSED)
1473 {
1474   fprintf (f, "[UNIMPLEMENTED]");
1475 }
1476
1477 const char *
1478 bfd_sym_type_operator_name (unsigned char num)
1479 {
1480   switch (num)
1481     {
1482     case 1: return "TTE";
1483     case 2: return "PointerTo";
1484     case 3: return "ScalarOf";
1485     case 4: return "ConstantOf";
1486     case 5: return "EnumerationOf";
1487     case 6: return "VectorOf";
1488     case 7: return "RecordOf";
1489     case 8: return "UnionOf";
1490     case 9: return "SubRangeOf";
1491     case 10: return "SetOf";
1492     case 11: return "NamedTypeOf";
1493     case 12: return "ProcOf";
1494     case 13: return "ValueOf";
1495     case 14: return "ArrayOf";
1496     default: return "[UNKNOWN OPERATOR]";
1497     }
1498 }
1499
1500 const char *
1501 bfd_sym_type_basic_name (unsigned char num)
1502 {
1503   switch (num)
1504     {
1505     case 0: return "void";
1506     case 1: return "pascal string";
1507     case 2: return "unsigned long";
1508     case 3: return "signed long";
1509     case 4: return "extended (10 bytes)";
1510     case 5: return "pascal boolean (1 byte)";
1511     case 6: return "unsigned byte";
1512     case 7: return "signed byte";
1513     case 8: return "character (1 byte)";
1514     case 9: return "wide character (2 bytes)";
1515     case 10: return "unsigned short";
1516     case 11: return "signed short";
1517     case 12: return "singled";
1518     case 13: return "double";
1519     case 14: return "extended (12 bytes)";
1520     case 15: return "computational (8 bytes)";
1521     case 16: return "c string";
1522     case 17: return "as-is string";
1523     default: return "[UNKNOWN BASIC TYPE]";
1524     }
1525 }
1526
1527 int
1528 bfd_sym_fetch_long (unsigned char *buf,
1529                     unsigned long len,
1530                     unsigned long offset,
1531                     unsigned long *offsetptr,
1532                     long *value)
1533 {
1534   int ret;
1535
1536   if (offset >= len)
1537     {
1538       *value = 0;
1539       offset += 0;
1540       ret = -1;
1541     }
1542   else if (! (buf[offset] & 0x80))
1543     {
1544       *value = buf[offset];
1545       offset += 1;
1546       ret = 0;
1547     }
1548   else if (buf[offset] == 0xc0)
1549     {
1550       if ((offset + 5) > len)
1551         {
1552           *value = 0;
1553           offset = len;
1554           ret = -1;
1555         }
1556       else
1557         {
1558           *value = bfd_getb32 (buf + offset + 1);
1559           offset += 5;
1560           ret = 0;
1561         }
1562     }
1563   else if ((buf[offset] & 0xc0) == 0xc0)
1564     {
1565       *value =  -(buf[offset] & 0x3f);
1566       offset += 1;
1567       ret = 0;
1568     }
1569   else if ((buf[offset] & 0xc0) == 0x80)
1570     {
1571       if ((offset + 2) > len)
1572         {
1573           *value = 0;
1574           offset = len;
1575           ret = -1;
1576         }
1577       else
1578         {
1579           *value = bfd_getb16 (buf + offset) & 0x3fff;
1580           offset += 2;
1581           ret = 0;
1582         }
1583     }
1584   else
1585     abort ();
1586
1587   if (offsetptr != NULL)
1588     *offsetptr = offset;
1589
1590   return ret;
1591 }
1592
1593 void
1594 bfd_sym_print_type_information (bfd *abfd,
1595                                 FILE *f,
1596                                 unsigned char *buf,
1597                                 unsigned long len,
1598                                 unsigned long offset,
1599                                 unsigned long *offsetptr)
1600 {
1601   unsigned int type;
1602
1603   if (offset >= len)
1604     {
1605       fprintf (f, "[NULL]");
1606
1607       if (offsetptr != NULL)
1608         *offsetptr = offset;
1609       return;
1610   }
1611
1612   type = buf[offset];
1613   offset++;
1614
1615   if (! (type & 0x80))
1616     {
1617       fprintf (f, "[%s] (0x%x)", bfd_sym_type_basic_name (type & 0x7f), type);
1618
1619       if (offsetptr != NULL)
1620         *offsetptr = offset;
1621       return;
1622     }
1623
1624   if (type & 0x40)
1625     fprintf (f, "[packed ");
1626   else
1627     fprintf (f, "[");
1628
1629   switch (type & 0x3f)
1630     {
1631     case 1:
1632       {
1633         long value;
1634         bfd_sym_type_information_table_entry tinfo;
1635
1636         bfd_sym_fetch_long (buf, len, offset, &offset, &value);
1637         if (value <= 0)
1638           fprintf (f, "[INVALID]");
1639         else
1640           {
1641             if (bfd_sym_fetch_type_table_information (abfd, &tinfo, value) < 0)
1642               fprintf (f, "[INVALID]");
1643             else
1644               fprintf (f, "\"%.*s\"",
1645                        bfd_sym_symbol_name (abfd, tinfo.nte_index)[0],
1646                        &bfd_sym_symbol_name (abfd, tinfo.nte_index)[1]);
1647           }
1648         fprintf (f, " (TTE %lu)", (unsigned long) value);
1649         break;
1650       }
1651
1652     case 2:
1653       fprintf (f, "pointer (0x%x) to ", type);
1654       bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1655       break;
1656
1657     case 3:
1658       {
1659         long value;
1660
1661         fprintf (f, "scalar (0x%x) of ", type);
1662         bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1663         bfd_sym_fetch_long (buf, len, offset, &offset, &value);
1664         fprintf (f, " (%lu)", (unsigned long) value);
1665         break;
1666       }
1667
1668     case 5:
1669       {
1670         long lower, upper, nelem;
1671         int i;
1672
1673         fprintf (f, "enumeration (0x%x) of ", type);
1674         bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1675         bfd_sym_fetch_long (buf, len, offset, &offset, &lower);
1676         bfd_sym_fetch_long (buf, len, offset, &offset, &upper);
1677         bfd_sym_fetch_long (buf, len, offset, &offset, &nelem);
1678         fprintf (f, " from %lu to %lu with %lu elements: ",
1679                  (unsigned long) lower, (unsigned long) upper,
1680                  (unsigned long) nelem);
1681
1682         for (i = 0; i < nelem; i++)
1683           {
1684             fprintf (f, "\n                    ");
1685             bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1686           }
1687         break;
1688       }
1689
1690     case 6:
1691       fprintf (f, "vector (0x%x)", type);
1692       fprintf (f, "\n                index ");
1693       bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1694       fprintf (f, "\n                target ");
1695       bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1696       break;
1697
1698     case 7:
1699     case 8:
1700       {
1701         long nrec, eloff, i;
1702
1703         if ((type & 0x3f) == 7)
1704           fprintf (f, "record (0x%x) of ", type);
1705         else
1706           fprintf (f, "union (0x%x) of ", type);
1707
1708         bfd_sym_fetch_long (buf, len, offset, &offset, &nrec);
1709         fprintf (f, "%lu elements: ", (unsigned long) nrec);
1710
1711         for (i = 0; i < nrec; i++)
1712           {
1713             bfd_sym_fetch_long (buf, len, offset, &offset, &eloff);
1714             fprintf (f, "\n                ");
1715             fprintf (f, "offset %lu: ", (unsigned long) eloff);
1716             bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1717           }
1718         break;
1719       }
1720
1721     case 9:
1722       fprintf (f, "subrange (0x%x) of ", type);
1723       bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1724       fprintf (f, " lower ");
1725       bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1726       fprintf (f, " upper ");
1727       bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1728       break;
1729
1730   case 11:
1731     {
1732       long value;
1733
1734       fprintf (f, "named type (0x%x) ", type);
1735       bfd_sym_fetch_long (buf, len, offset, &offset, &value);
1736       if (value <= 0)
1737         fprintf (f, "[INVALID]");
1738       else
1739         fprintf (f, "\"%.*s\"",
1740                  bfd_sym_symbol_name (abfd, value)[0],
1741                  &bfd_sym_symbol_name (abfd, value)[1]);
1742
1743       fprintf (f, " (NTE %lu) with type ", (unsigned long) value);
1744       bfd_sym_print_type_information (abfd, f, buf, len, offset, &offset);
1745       break;
1746     }
1747
1748   default:
1749     fprintf (f, "%s (0x%x)", bfd_sym_type_operator_name (type), type);
1750     break;
1751     }
1752
1753   if (type == (0x40 | 0x6))
1754     {
1755       /* Vector.  */
1756       long n, width, m;
1757       long l;
1758       long i;
1759
1760       bfd_sym_fetch_long (buf, len, offset, &offset, &n);
1761       bfd_sym_fetch_long (buf, len, offset, &offset, &width);
1762       bfd_sym_fetch_long (buf, len, offset, &offset, &m);
1763       /* fprintf (f, "\n                "); */
1764       fprintf (f, " N %ld, width %ld, M %ld, ", n, width, m);
1765       for (i = 0; i < m; i++)
1766         {
1767           bfd_sym_fetch_long (buf, len, offset, &offset, &l);
1768           if (i != 0)
1769             fprintf (f, " ");
1770           fprintf (f, "%ld", l);
1771         }
1772     }
1773   else  if (type & 0x40)
1774     {
1775       /* Other packed type.  */
1776       long msb, lsb;
1777
1778       bfd_sym_fetch_long (buf, len, offset, &offset, &msb);
1779       bfd_sym_fetch_long (buf, len, offset, &offset, &lsb);
1780       /* fprintf (f, "\n                "); */
1781       fprintf (f, " msb %ld, lsb %ld", msb, lsb);
1782     }
1783
1784   fprintf (f, "]");
1785
1786   if (offsetptr != NULL)
1787     *offsetptr = offset;
1788 }
1789
1790 void
1791 bfd_sym_print_type_information_table_entry (bfd *abfd,
1792                                             FILE *f,
1793                                             bfd_sym_type_information_table_entry *entry)
1794 {
1795   unsigned char *buf;
1796   unsigned long offset;
1797   unsigned int i;
1798
1799   fprintf (f, "\"%.*s\" (NTE %lu), %lu bytes at %lu, logical size %lu",
1800            bfd_sym_symbol_name (abfd, entry->nte_index)[0],
1801            &bfd_sym_symbol_name (abfd, entry->nte_index)[1],
1802            entry->nte_index,
1803            entry->physical_size, entry->offset, entry->logical_size);
1804
1805   fprintf (f, "\n            ");
1806
1807   buf = alloca (entry->physical_size);
1808   if (buf == NULL)
1809     {
1810       fprintf (f, "[ERROR]\n");
1811       return;
1812     }
1813   if (bfd_seek (abfd, entry->offset, SEEK_SET) < 0)
1814     {
1815       fprintf (f, "[ERROR]\n");
1816       return;
1817     }
1818   if (bfd_bread (buf, entry->physical_size, abfd) != entry->physical_size)
1819     {
1820       fprintf (f, "[ERROR]\n");
1821       return;
1822     }
1823
1824   fprintf (f, "[");
1825   for (i = 0; i < entry->physical_size; i++)
1826     {
1827       if (i == 0)
1828         fprintf (f, "0x%02x", buf[i]);
1829       else
1830         fprintf (f, " 0x%02x", buf[i]);
1831     }
1832
1833   fprintf (f, "]");
1834   fprintf (f, "\n            ");
1835
1836   bfd_sym_print_type_information (abfd, f, buf, entry->physical_size, 0, &offset);
1837
1838   if (offset != entry->physical_size)
1839     fprintf (f, "\n            [parser used %lu bytes instead of %lu]", offset, entry->physical_size);
1840 }
1841
1842 void
1843 bfd_sym_print_file_references_index_table_entry (bfd *abfd ATTRIBUTE_UNUSED,
1844                                                  FILE *f,
1845                                                  bfd_sym_file_references_index_table_entry *entry ATTRIBUTE_UNUSED)
1846 {
1847   fprintf (f, "[UNIMPLEMENTED]");
1848 }
1849
1850 void
1851 bfd_sym_print_constant_pool_entry (bfd *abfd ATTRIBUTE_UNUSED,
1852                                    FILE *f,
1853                                    bfd_sym_constant_pool_entry *entry ATTRIBUTE_UNUSED)
1854 {
1855   fprintf (f, "[UNIMPLEMENTED]");
1856 }
1857
1858 unsigned char *
1859 bfd_sym_display_name_table_entry (bfd *abfd,
1860                                   FILE *f,
1861                                   unsigned char *entry)
1862 {
1863   unsigned long sym_index;
1864   unsigned long offset;
1865   bfd_sym_data_struct *sdata = NULL;
1866
1867   BFD_ASSERT (bfd_sym_valid (abfd));
1868   sdata = abfd->tdata.sym_data;
1869   sym_index = (entry - sdata->name_table) / 2;
1870
1871   if (sdata->version >= BFD_SYM_VERSION_3_4 && entry[0] == 255 && entry[1] == 0)
1872     {
1873       unsigned short length = bfd_getb16 (entry + 2);
1874       fprintf (f, "[%8lu] \"%.*s\"\n", sym_index, length, entry + 4);
1875       offset = 2 + length + 1;
1876     }
1877   else
1878     {
1879       if (! (entry[0] == 0 || (entry[0] == 1 && entry[1] == '\0')))
1880         fprintf (f, "[%8lu] \"%.*s\"\n", sym_index, entry[0], entry + 1);
1881
1882       if (sdata->version >= BFD_SYM_VERSION_3_4)
1883         offset = entry[0] + 2;
1884       else
1885         offset = entry[0] + 1;
1886     }
1887
1888   return (entry + offset + (offset % 2));
1889 }
1890
1891 void
1892 bfd_sym_display_name_table (bfd *abfd, FILE *f)
1893 {
1894   unsigned long name_table_len;
1895   unsigned char *name_table, *name_table_end, *cur;
1896   bfd_sym_data_struct *sdata = NULL;
1897
1898   BFD_ASSERT (bfd_sym_valid (abfd));
1899   sdata = abfd->tdata.sym_data;
1900
1901   name_table_len = sdata->header.dshb_nte.dti_page_count * sdata->header.dshb_page_size;
1902   name_table = sdata->name_table;
1903   name_table_end = name_table + name_table_len;
1904
1905   fprintf (f, "name table (NTE) contains %lu bytes:\n\n", name_table_len);
1906
1907   cur = name_table;
1908   for (;;)
1909     {
1910       cur = bfd_sym_display_name_table_entry (abfd, f, cur);
1911       if (cur >= name_table_end)
1912         break;
1913     }
1914 }
1915
1916 void
1917 bfd_sym_display_resources_table (bfd *abfd, FILE *f)
1918 {
1919   unsigned long i;
1920   bfd_sym_resources_table_entry entry;
1921   bfd_sym_data_struct *sdata = NULL;
1922
1923   BFD_ASSERT (bfd_sym_valid (abfd));
1924   sdata = abfd->tdata.sym_data;
1925
1926   fprintf (f, "resource table (RTE) contains %lu objects:\n\n",
1927            sdata->header.dshb_rte.dti_object_count);
1928
1929   for (i = 1; i <= sdata->header.dshb_rte.dti_object_count; i++)
1930     {
1931       if (bfd_sym_fetch_resources_table_entry (abfd, &entry, i) < 0)
1932         fprintf (f, " [%8lu] [INVALID]\n", i);
1933       else
1934         {
1935           fprintf (f, " [%8lu] ", i);
1936           bfd_sym_print_resources_table_entry (abfd, f, &entry);
1937           fprintf (f, "\n");
1938         }
1939     }
1940 }
1941
1942 void
1943 bfd_sym_display_modules_table (bfd *abfd, FILE *f)
1944 {
1945   unsigned long i;
1946   bfd_sym_modules_table_entry entry;
1947   bfd_sym_data_struct *sdata = NULL;
1948
1949   BFD_ASSERT (bfd_sym_valid (abfd));
1950   sdata = abfd->tdata.sym_data;
1951
1952   fprintf (f, "module table (MTE) contains %lu objects:\n\n",
1953            sdata->header.dshb_mte.dti_object_count);
1954
1955   for (i = 1; i <= sdata->header.dshb_mte.dti_object_count; i++)
1956     {
1957       if (bfd_sym_fetch_modules_table_entry (abfd, &entry, i) < 0)
1958         fprintf (f, " [%8lu] [INVALID]\n", i);
1959       else
1960         {
1961           fprintf (f, " [%8lu] ", i);
1962           bfd_sym_print_modules_table_entry (abfd, f, &entry);
1963           fprintf (f, "\n");
1964         }
1965     }
1966 }
1967
1968 void
1969 bfd_sym_display_file_references_table (bfd *abfd, FILE *f)
1970 {
1971   unsigned long i;
1972   bfd_sym_file_references_table_entry entry;
1973   bfd_sym_data_struct *sdata = NULL;
1974
1975   BFD_ASSERT (bfd_sym_valid (abfd));
1976   sdata = abfd->tdata.sym_data;
1977
1978   fprintf (f, "file reference table (FRTE) contains %lu objects:\n\n",
1979            sdata->header.dshb_frte.dti_object_count);
1980
1981   for (i = 1; i <= sdata->header.dshb_frte.dti_object_count; i++)
1982     {
1983       if (bfd_sym_fetch_file_references_table_entry (abfd, &entry, i) < 0)
1984         fprintf (f, " [%8lu] [INVALID]\n", i);
1985       else
1986         {
1987           fprintf (f, " [%8lu] ", i);
1988           bfd_sym_print_file_references_table_entry (abfd, f, &entry);
1989           fprintf (f, "\n");
1990         }
1991     }
1992 }
1993
1994 void
1995 bfd_sym_display_contained_modules_table (bfd *abfd, FILE *f)
1996 {
1997   unsigned long i;
1998   bfd_sym_contained_modules_table_entry entry;
1999   bfd_sym_data_struct *sdata = NULL;
2000
2001   BFD_ASSERT (bfd_sym_valid (abfd));
2002   sdata = abfd->tdata.sym_data;
2003
2004   fprintf (f, "contained modules table (CMTE) contains %lu objects:\n\n",
2005            sdata->header.dshb_cmte.dti_object_count);
2006
2007   for (i = 1; i <= sdata->header.dshb_cmte.dti_object_count; i++)
2008     {
2009       if (bfd_sym_fetch_contained_modules_table_entry (abfd, &entry, i) < 0)
2010         fprintf (f, " [%8lu] [INVALID]\n", i);
2011       else
2012         {
2013           fprintf (f, " [%8lu] ", i);
2014           bfd_sym_print_contained_modules_table_entry (abfd, f, &entry);
2015           fprintf (f, "\n");
2016         }
2017     }
2018 }
2019
2020 void
2021 bfd_sym_display_contained_variables_table (bfd *abfd, FILE *f)
2022 {
2023   unsigned long i;
2024   bfd_sym_contained_variables_table_entry entry;
2025   bfd_sym_data_struct *sdata = NULL;
2026
2027   BFD_ASSERT (bfd_sym_valid (abfd));
2028   sdata = abfd->tdata.sym_data;
2029
2030   fprintf (f, "contained variables table (CVTE) contains %lu objects:\n\n",
2031            sdata->header.dshb_cvte.dti_object_count);
2032
2033   for (i = 1; i <= sdata->header.dshb_cvte.dti_object_count; i++)
2034     {
2035       if (bfd_sym_fetch_contained_variables_table_entry (abfd, &entry, i) < 0)
2036         fprintf (f, " [%8lu] [INVALID]\n", i);
2037       else
2038         {
2039           fprintf (f, " [%8lu] ", i);
2040           bfd_sym_print_contained_variables_table_entry (abfd, f, &entry);
2041           fprintf (f, "\n");
2042         }
2043     }
2044
2045   fprintf (f, "\n");
2046 }
2047
2048 void
2049 bfd_sym_display_contained_statements_table (bfd *abfd, FILE *f)
2050 {
2051   unsigned long i;
2052   bfd_sym_contained_statements_table_entry entry;
2053   bfd_sym_data_struct *sdata = NULL;
2054
2055   BFD_ASSERT (bfd_sym_valid (abfd));
2056   sdata = abfd->tdata.sym_data;
2057
2058   fprintf (f, "contained statements table (CSNTE) contains %lu objects:\n\n",
2059            sdata->header.dshb_csnte.dti_object_count);
2060
2061   for (i = 1; i <= sdata->header.dshb_csnte.dti_object_count; i++)
2062     {
2063       if (bfd_sym_fetch_contained_statements_table_entry (abfd, &entry, i) < 0)
2064         fprintf (f, " [%8lu] [INVALID]\n", i);
2065       else
2066         {
2067           fprintf (f, " [%8lu] ", i);
2068           bfd_sym_print_contained_statements_table_entry (abfd, f, &entry);
2069           fprintf (f, "\n");
2070         }
2071     }
2072 }
2073
2074 void
2075 bfd_sym_display_contained_labels_table (bfd *abfd, FILE *f)
2076 {
2077   unsigned long i;
2078   bfd_sym_contained_labels_table_entry entry;
2079   bfd_sym_data_struct *sdata = NULL;
2080
2081   BFD_ASSERT (bfd_sym_valid (abfd));
2082   sdata = abfd->tdata.sym_data;
2083
2084   fprintf (f, "contained labels table (CLTE) contains %lu objects:\n\n",
2085            sdata->header.dshb_clte.dti_object_count);
2086
2087   for (i = 1; i <= sdata->header.dshb_clte.dti_object_count; i++)
2088     {
2089       if (bfd_sym_fetch_contained_labels_table_entry (abfd, &entry, i) < 0)
2090         fprintf (f, " [%8lu] [INVALID]\n", i);
2091       else
2092         {
2093           fprintf (f, " [%8lu] ", i);
2094           bfd_sym_print_contained_labels_table_entry (abfd, f, &entry);
2095           fprintf (f, "\n");
2096         }
2097     }
2098 }
2099
2100 void
2101 bfd_sym_display_contained_types_table (bfd *abfd, FILE *f)
2102 {
2103   unsigned long i;
2104   bfd_sym_contained_types_table_entry entry;
2105   bfd_sym_data_struct *sdata = NULL;
2106
2107   BFD_ASSERT (bfd_sym_valid (abfd));
2108   sdata = abfd->tdata.sym_data;
2109
2110   fprintf (f, "contained types table (CTTE) contains %lu objects:\n\n",
2111            sdata->header.dshb_ctte.dti_object_count);
2112
2113   for (i = 1; i <= sdata->header.dshb_ctte.dti_object_count; i++)
2114     {
2115       if (bfd_sym_fetch_contained_types_table_entry (abfd, &entry, i) < 0)
2116         fprintf (f, " [%8lu] [INVALID]\n", i);
2117       else
2118         {
2119           fprintf (f, " [%8lu] ", i);
2120           bfd_sym_print_contained_types_table_entry (abfd, f, &entry);
2121           fprintf (f, "\n");
2122         }
2123     }
2124 }
2125
2126 void
2127 bfd_sym_display_file_references_index_table (bfd *abfd, FILE *f)
2128 {
2129   unsigned long i;
2130   bfd_sym_file_references_index_table_entry entry;
2131   bfd_sym_data_struct *sdata = NULL;
2132
2133   BFD_ASSERT (bfd_sym_valid (abfd));
2134   sdata = abfd->tdata.sym_data;
2135
2136   fprintf (f, "file references index table (FITE) contains %lu objects:\n\n",
2137            sdata->header.dshb_fite.dti_object_count);
2138
2139   for (i = 1; i <= sdata->header.dshb_fite.dti_object_count; i++)
2140     {
2141       if (bfd_sym_fetch_file_references_index_table_entry (abfd, &entry, i) < 0)
2142         fprintf (f, " [%8lu] [INVALID]\n", i);
2143       else
2144         {
2145           fprintf (f, " [%8lu] ", i);
2146           bfd_sym_print_file_references_index_table_entry (abfd, f, &entry);
2147           fprintf (f, "\n");
2148         }
2149     }
2150 }
2151
2152 void
2153 bfd_sym_display_constant_pool (bfd *abfd, FILE *f)
2154 {
2155   unsigned long i;
2156   bfd_sym_constant_pool_entry entry;
2157   bfd_sym_data_struct *sdata = NULL;
2158
2159   BFD_ASSERT (bfd_sym_valid (abfd));
2160   sdata = abfd->tdata.sym_data;
2161
2162   fprintf (f, "constant pool (CONST) contains %lu objects:\n\n",
2163            sdata->header.dshb_const.dti_object_count);
2164
2165   for (i = 1; i <= sdata->header.dshb_const.dti_object_count; i++)
2166     {
2167       if (bfd_sym_fetch_constant_pool_entry (abfd, &entry, i) < 0)
2168         fprintf (f, " [%8lu] [INVALID]\n", i);
2169       else
2170         {
2171           fprintf (f, " [%8lu] ", i);
2172           bfd_sym_print_constant_pool_entry (abfd, f, &entry);
2173           fprintf (f, "\n");
2174         }
2175     }
2176 }
2177
2178 void
2179 bfd_sym_display_type_information_table (bfd *abfd, FILE *f)
2180 {
2181   unsigned long i;
2182   bfd_sym_type_table_entry sym_index;
2183   bfd_sym_type_information_table_entry entry;
2184   bfd_sym_data_struct *sdata = NULL;
2185
2186   BFD_ASSERT (bfd_sym_valid (abfd));
2187   sdata = abfd->tdata.sym_data;
2188
2189   if (sdata->header.dshb_tte.dti_object_count > 99)
2190     fprintf (f, "type table (TINFO) contains %lu objects:\n\n",
2191              sdata->header.dshb_tte.dti_object_count - 99);
2192   else
2193     {
2194       fprintf (f, "type table (TINFO) contains [INVALID] objects:\n\n");
2195       return;
2196     }
2197
2198   for (i = 100; i <= sdata->header.dshb_tte.dti_object_count; i++)
2199     {
2200       if (bfd_sym_fetch_type_table_entry (abfd, &sym_index, i - 100) < 0)
2201         fprintf (f, " [%8lu] [INVALID]\n", i);
2202       else
2203         {
2204           fprintf (f, " [%8lu] (TINFO %lu) ", i, sym_index);
2205
2206           if (bfd_sym_fetch_type_information_table_entry (abfd, &entry, sym_index) < 0)
2207             fprintf (f, "[INVALID]");
2208           else
2209             bfd_sym_print_type_information_table_entry (abfd, f, &entry);
2210
2211           fprintf (f, "\n");
2212         }
2213     }
2214 }
2215
2216 int
2217 bfd_sym_scan (bfd *abfd, bfd_sym_version version, bfd_sym_data_struct *mdata)
2218 {
2219   asection *bfdsec;
2220   const char *name = "symbols";
2221
2222   mdata->name_table = 0;
2223   mdata->sbfd = abfd;
2224   mdata->version = version;
2225
2226   bfd_seek (abfd, 0, SEEK_SET);
2227   if (bfd_sym_read_header (abfd, &mdata->header, mdata->version) != 0)
2228     return -1;
2229
2230   mdata->name_table = bfd_sym_read_name_table (abfd, &mdata->header);
2231   if (mdata->name_table == NULL)
2232     return -1;
2233
2234   bfdsec = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS);
2235   if (bfdsec == NULL)
2236     return -1;
2237
2238   bfdsec->vma = 0;
2239   bfdsec->lma = 0;
2240   bfdsec->size = 0;
2241   bfdsec->filepos = 0;
2242   bfdsec->alignment_power = 0;
2243
2244   abfd->tdata.sym_data = mdata;
2245
2246   return 0;
2247 }
2248
2249 const bfd_target *
2250 bfd_sym_object_p (bfd *abfd)
2251 {
2252   struct bfd_preserve preserve;
2253   bfd_sym_version version = -1;
2254
2255   preserve.marker = NULL;
2256   bfd_seek (abfd, 0, SEEK_SET);
2257   if (bfd_sym_read_version (abfd, &version) != 0)
2258     goto wrong;
2259
2260   preserve.marker = bfd_alloc (abfd, sizeof (bfd_sym_data_struct));
2261   if (preserve.marker == NULL
2262       || ! bfd_preserve_save (abfd, &preserve))
2263     goto fail;
2264
2265   if (bfd_sym_scan (abfd, version,
2266                     (bfd_sym_data_struct *) preserve.marker) != 0)
2267     goto wrong;
2268
2269   bfd_preserve_finish (abfd, &preserve);
2270   return abfd->xvec;
2271
2272  wrong:
2273   bfd_set_error (bfd_error_wrong_format);
2274
2275  fail:
2276   if (preserve.marker != NULL)
2277     bfd_preserve_restore (abfd, &preserve);
2278   return NULL;
2279 }
2280
2281 #define bfd_sym_make_empty_symbol _bfd_generic_make_empty_symbol
2282
2283 void
2284 bfd_sym_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, asymbol *symbol, symbol_info *ret)
2285 {
2286   bfd_symbol_info (symbol, ret);
2287 }
2288
2289 long
2290 bfd_sym_get_symtab_upper_bound (bfd *abfd ATTRIBUTE_UNUSED)
2291 {
2292   return 0;
2293 }
2294
2295 long
2296 bfd_sym_canonicalize_symtab (bfd *abfd ATTRIBUTE_UNUSED, asymbol **sym ATTRIBUTE_UNUSED)
2297 {
2298   return 0;
2299 }
2300
2301 int
2302 bfd_sym_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
2303                         struct bfd_link_info *info ATTRIBUTE_UNUSED)
2304 {
2305   return 0;
2306 }
2307
2308 const bfd_target sym_vec =
2309 {
2310   "sym",                        /* Name.  */
2311   bfd_target_sym_flavour,       /* Flavour.  */
2312   BFD_ENDIAN_BIG,               /* Byteorder.  */
2313   BFD_ENDIAN_BIG,               /* Header byteorder.  */
2314   (HAS_RELOC | EXEC_P |         /* Object flags.  */
2315    HAS_LINENO | HAS_DEBUG |
2316    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
2317   (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | SEC_DATA
2318    | SEC_ROM | SEC_HAS_CONTENTS), /* Section_flags.  */
2319   0,                            /* Symbol_leading_char.  */
2320   ' ',                          /* AR_pad_char.  */
2321   16,                           /* AR_max_namelen.  */
2322   0,                            /* match priority.  */
2323   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2324   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2325   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* Data.  */
2326   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
2327   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
2328   bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* Hdrs.  */
2329   {                             /* bfd_check_format.  */
2330     _bfd_dummy_target,
2331     bfd_sym_object_p,           /* bfd_check_format.  */
2332     _bfd_dummy_target,
2333     _bfd_dummy_target,
2334   },
2335   {                             /* bfd_set_format.  */
2336     bfd_false,
2337     bfd_sym_mkobject,
2338     bfd_false,
2339     bfd_false,
2340   },
2341   {                             /* bfd_write_contents.  */
2342     bfd_false,
2343     bfd_true,
2344     bfd_false,
2345     bfd_false,
2346   },
2347
2348   BFD_JUMP_TABLE_GENERIC (bfd_sym),
2349   BFD_JUMP_TABLE_COPY (_bfd_generic),
2350   BFD_JUMP_TABLE_CORE (_bfd_nocore),
2351   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
2352   BFD_JUMP_TABLE_SYMBOLS (bfd_sym),
2353   BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
2354   BFD_JUMP_TABLE_WRITE (bfd_sym),
2355   BFD_JUMP_TABLE_LINK (bfd_sym),
2356   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
2357
2358   NULL,
2359
2360   NULL
2361 };