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