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