]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - binutils/coffgrok.c
Update year range in copyright notice of binutils files
[thirdparty/binutils-gdb.git] / binutils / coffgrok.c
CommitLineData
252b5132 1/* coffgrok.c
d87bef3a 2 Copyright (C) 1994-2023 Free Software Foundation, Inc.
252b5132 3
32866df7 4 This file is part of GNU Binutils.
252b5132 5
32866df7
NC
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.
252b5132 10
32866df7
NC
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. */
252b5132 20
252b5132
RH
21
22/* Written by Steve Chamberlain (sac@cygnus.com)
23
24 This module reads a coff file and builds a really simple type tree
25 which can be read by other programs. The first application is a
32866df7 26 coff->sysroff converter. It can be tested with coffdump.c. */
252b5132 27
3db64b00 28#include "sysdep.h"
e9792343
AM
29#include "bfd.h"
30#include "libiberty.h"
252b5132
RH
31#include "coff/internal.h"
32#include "../bfd/libcoff.h"
3db64b00 33#include "bucomm.h"
252b5132 34#include "coffgrok.h"
3db64b00 35
85880250
NC
36static int lofile = 1;
37
38static struct coff_scope * top_scope;
39static struct coff_scope * file_scope;
40static struct coff_ofile * ofile;
41static struct coff_symbol * last_function_symbol;
42static struct coff_type * last_function_type;
43static struct coff_type * last_struct;
44static struct coff_type * last_enum;
45static struct coff_sfile * cur_sfile;
46static struct coff_symbol ** tindex;
47static asymbol ** syms;
48static long symcount;
49static struct coff_ptr_struct * rawsyms;
50static unsigned int rawcount;
51static bfd * abfd;
252b5132
RH
52
53#define N(x) ((x)->_n._n_nptr[1])
54
2da42df6
AJ
55#define PTR_SIZE 4
56#define SHORT_SIZE 2
57#define INT_SIZE 4
58#define LONG_SIZE 4
59#define FLOAT_SIZE 4
60#define DOUBLE_SIZE 8
252b5132
RH
61
62#define INDEXOF(p) ((struct coff_ptr_struct *)(p)-(rawsyms))
63
c32144ff 64
252b5132 65static struct coff_scope *
2da42df6 66empty_scope (void)
252b5132 67{
85880250 68 return (struct coff_scope *) (xcalloc (sizeof (struct coff_scope), 1));
252b5132
RH
69}
70
71static struct coff_symbol *
2da42df6 72empty_symbol (void)
252b5132
RH
73{
74 return (struct coff_symbol *) (xcalloc (sizeof (struct coff_symbol), 1));
75}
76
252b5132 77static void
91d6fa6a 78push_scope (int slink)
252b5132
RH
79{
80 struct coff_scope *n = empty_scope ();
91d6fa6a
NC
81
82 if (slink)
252b5132
RH
83 {
84 if (top_scope)
85 {
86 if (top_scope->list_tail)
87 {
88 top_scope->list_tail->next = n;
89 }
90 else
91 {
92 top_scope->list_head = n;
93 }
94 top_scope->list_tail = n;
95 }
96 }
97 n->parent = top_scope;
98
99 top_scope = n;
100}
101
102static void
2da42df6 103pop_scope (void)
252b5132 104{
85880250
NC
105 /* PR 17512: file: 809933ac. */
106 if (top_scope == NULL)
107 fatal (_("Out of context scope change encountered"));
252b5132
RH
108 top_scope = top_scope->parent;
109}
110
111static void
2da42df6 112do_sections_p1 (struct coff_ofile *head)
252b5132
RH
113{
114 asection *section;
115 int idx;
116 struct coff_section *all = (struct coff_section *) (xcalloc (abfd->section_count + 1,
117 sizeof (struct coff_section)));
118 head->nsections = abfd->section_count + 1;
119 head->sections = all;
120
121 for (idx = 0, section = abfd->sections; section; section = section->next, idx++)
122 {
123 long relsize;
85880250 124 unsigned int i = section->target_index;
252b5132
RH
125 arelent **relpp;
126 long relcount;
127
85880250
NC
128 /* PR 17512: file: 2d6effca. */
129 if (i > abfd->section_count)
130 fatal (_("Invalid section target index: %u"), i);
131
252b5132
RH
132 relsize = bfd_get_reloc_upper_bound (abfd, section);
133 if (relsize < 0)
134 bfd_fatal (bfd_get_filename (abfd));
135 if (relsize == 0)
f462a9ea 136 continue;
252b5132
RH
137 relpp = (arelent **) xmalloc (relsize);
138 relcount = bfd_canonicalize_reloc (abfd, section, relpp, syms);
139 if (relcount < 0)
140 bfd_fatal (bfd_get_filename (abfd));
141
142 head->sections[i].name = (char *) (section->name);
143 head->sections[i].code = section->flags & SEC_CODE;
144 head->sections[i].data = section->flags & SEC_DATA;
145 if (strcmp (section->name, ".bss") == 0)
146 head->sections[i].data = 1;
147 head->sections[i].address = section->lma;
fd361982 148 head->sections[i].size = bfd_section_size (section);
252b5132
RH
149 head->sections[i].number = idx;
150 head->sections[i].nrelocs = section->reloc_count;
151 head->sections[i].relocs =
152 (struct coff_reloc *) (xcalloc (section->reloc_count,
153 sizeof (struct coff_reloc)));
154 head->sections[i].bfd_section = section;
155 }
156 head->sections[0].name = "ABSOLUTE";
157 head->sections[0].code = 0;
158 head->sections[0].data = 0;
159 head->sections[0].address = 0;
160 head->sections[0].size = 0;
161 head->sections[0].number = 0;
162}
163
164static void
2da42df6 165do_sections_p2 (struct coff_ofile *head)
252b5132
RH
166{
167 asection *section;
85880250 168
252b5132
RH
169 for (section = abfd->sections; section; section = section->next)
170 {
171 unsigned int j;
172
85880250
NC
173 /* PR 17512: file: 7c1a36e8.
174 A corrupt COFF binary might have a reloc count but no relocs.
175 Handle this here. */
176 if (section->relocation == NULL)
177 continue;
178
252b5132
RH
179 for (j = 0; j < section->reloc_count; j++)
180 {
85880250 181 unsigned int idx;
252b5132 182 int i = section->target_index;
85880250 183 struct coff_reloc *r;
252b5132 184 arelent *sr = section->relocation + j;
85880250
NC
185
186 if (i > head->nsections)
187 fatal (_("Invalid section target index: %d"), i);
188 /* PR 17512: file: db850ff4. */
189 if (j >= head->sections[i].nrelocs)
190 fatal (_("Target section has insufficient relocs"));
191 r = head->sections[i].relocs + j;
252b5132
RH
192 r->offset = sr->address;
193 r->addend = sr->addend;
194 idx = ((coff_symbol_type *) (sr->sym_ptr_ptr[0]))->native - rawsyms;
85880250
NC
195 if (idx >= rawcount)
196 {
197 if (rawcount == 0)
198 fatal (_("Symbol index %u encountered when there are no symbols"), idx);
199 non_fatal (_("Invalid symbol index %u encountered"), idx);
200 idx = 0;
201 }
252b5132
RH
202 r->symbol = tindex[idx];
203 }
204 }
205}
206
207static struct coff_where *
85880250 208do_where (unsigned int i)
252b5132 209{
85880250 210 struct internal_syment *sym;
252b5132
RH
211 struct coff_where *where =
212 (struct coff_where *) (xmalloc (sizeof (struct coff_where)));
85880250
NC
213
214 if (i >= rawcount)
215 fatal ("Invalid symbol index: %d\n", i);
216
217 sym = &rawsyms[i].u.syment;
252b5132
RH
218 where->offset = sym->n_value;
219
220 if (sym->n_scnum == -1)
221 sym->n_scnum = 0;
222
223 switch (sym->n_sclass)
224 {
225 case C_FIELD:
226 where->where = coff_where_member_of_struct;
227 where->offset = sym->n_value / 8;
228 where->bitoffset = sym->n_value % 8;
229 where->bitsize = rawsyms[i + 1].u.auxent.x_sym.x_misc.x_lnsz.x_size;
230 break;
231 case C_MOE:
232 where->where = coff_where_member_of_enum;
233 break;
234 case C_MOS:
235 case C_MOU:
236 where->where = coff_where_member_of_struct;
237 break;
238 case C_AUTO:
239 case C_ARG:
240 where->where = coff_where_stack;
241 break;
242 case C_EXT:
243 case C_STAT:
244 case C_EXTDEF:
245 case C_LABEL:
246 where->where = coff_where_memory;
85880250
NC
247 /* PR 17512: file: 07a37c40. */
248 /* PR 17512: file: 0c2eb101. */
249 if (sym->n_scnum >= ofile->nsections || sym->n_scnum < 0)
250 {
251 non_fatal (_("Invalid section number (%d) encountered"),
252 sym->n_scnum);
253 where->section = ofile->sections;
254 }
255 else
256 where->section = &ofile->sections[sym->n_scnum];
252b5132
RH
257 break;
258 case C_REG:
259 case C_REGPARM:
260 where->where = coff_where_register;
261 break;
262 case C_ENTAG:
263 where->where = coff_where_entag;
264 break;
265 case C_STRTAG:
266 case C_UNTAG:
267 where->where = coff_where_strtag;
268 break;
269 case C_TPDEF:
270 where->where = coff_where_typedef;
271 break;
272 default:
85880250 273 fatal (_("Unrecognized symbol class: %d"), sym->n_sclass);
252b5132
RH
274 break;
275 }
276 return where;
277}
278
85880250 279static struct coff_line *
2da42df6 280do_lines (int i, char *name ATTRIBUTE_UNUSED)
252b5132
RH
281{
282 struct coff_line *res = (struct coff_line *) xcalloc (sizeof (struct coff_line), 1);
283 asection *s;
284 unsigned int l;
285
85880250 286 /* Find out if this function has any line numbers in the table. */
252b5132
RH
287 for (s = abfd->sections; s; s = s->next)
288 {
85880250
NC
289 /* PR 17512: file: 07a37c40.
290 A corrupt COFF binary can have a linenumber count in the header
291 but no line number table. This should be reported elsewhere, but
292 do not rely upon this. */
293 if (s->lineno == NULL)
294 continue;
295
252b5132
RH
296 for (l = 0; l < s->lineno_count; l++)
297 {
298 if (s->lineno[l].line_number == 0)
299 {
300 if (rawsyms + i == ((coff_symbol_type *) (&(s->lineno[l].u.sym[0])))->native)
301 {
85880250 302 /* These lines are for this function - so count them and stick them on. */
252b5132
RH
303 int c = 0;
304 /* Find the linenumber of the top of the function, since coff linenumbers
f462a9ea 305 are relative to the start of the function. */
252b5132
RH
306 int start_line = rawsyms[i + 3].u.auxent.x_sym.x_misc.x_lnsz.x_lnno;
307
308 l++;
85880250
NC
309 for (c = 0;
310 /* PR 17512: file: c2825452. */
311 l + c + 1 < s->lineno_count
312 && s->lineno[l + c + 1].line_number;
313 c++)
252b5132
RH
314 ;
315
85880250 316 /* Add two extra records, one for the prologue and one for the epilogue. */
252b5132
RH
317 c += 1;
318 res->nlines = c;
319 res->lines = (int *) (xcalloc (sizeof (int), c));
320 res->addresses = (int *) (xcalloc (sizeof (int), c));
321 res->lines[0] = start_line;
322 res->addresses[0] = rawsyms[i].u.syment.n_value - s->vma;
85880250
NC
323 for (c = 0;
324 /* PR 17512: file: c2825452. */
325 l + c + 1 < s->lineno_count
326 && s->lineno[l + c + 1].line_number;
327 c++)
252b5132
RH
328 {
329 res->lines[c + 1] = s->lineno[l + c].line_number + start_line - 1;
330 res->addresses[c + 1] = s->lineno[l + c].u.offset;
331 }
332 return res;
333 }
334 }
335 }
336 }
337 return res;
338}
339
85880250
NC
340static struct coff_type *
341do_type (unsigned int i)
252b5132 342{
85880250
NC
343 struct internal_syment *sym;
344 union internal_auxent *aux;
345 struct coff_type *res = (struct coff_type *) xmalloc (sizeof (struct coff_type));
346 int type;
252b5132
RH
347 int which_dt = 0;
348 int dimind = 0;
349
85880250
NC
350 if (i >= rawcount)
351 fatal (_("Type entry %u does not have enough symbolic information"), i);
352
353 if (!rawsyms[i].is_sym)
354 fatal (_("Type entry %u does not refer to a symbol"), i);
355 sym = &rawsyms[i].u.syment;
356
357 if (sym->n_numaux == 0 || i >= rawcount -1 || rawsyms[i + 1].is_sym)
358 aux = NULL;
359 else
360 aux = &rawsyms[i + 1].u.auxent;
361
362 type = sym->n_type;
363
252b5132
RH
364 res->type = coff_basic_type;
365 res->u.basic = type & 0xf;
366
367 switch (type & 0xf)
368 {
369 case T_NULL:
370 case T_VOID:
371 if (sym->n_numaux && sym->n_sclass == C_STAT)
372 {
85880250 373 /* This is probably a section definition. */
252b5132 374 res->type = coff_secdef_type;
85880250
NC
375 if (aux == NULL)
376 fatal (_("Section definition needs a section length"));
252b5132 377 res->size = aux->x_scn.x_scnlen;
85880250
NC
378
379 /* PR 17512: file: 081c955d.
380 Fill in the asecdef structure as well. */
381 res->u.asecdef.address = 0;
382 res->u.asecdef.size = 0;
252b5132
RH
383 }
384 else
385 {
386 if (type == 0)
387 {
85880250 388 /* Don't know what this is, let's make it a simple int. */
252b5132
RH
389 res->size = INT_SIZE;
390 res->u.basic = T_UINT;
391 }
392 else
393 {
85880250 394 /* Else it could be a function or pointer to void. */
252b5132
RH
395 res->size = 0;
396 }
397 }
398 break;
399
252b5132
RH
400 case T_UCHAR:
401 case T_CHAR:
402 res->size = 1;
403 break;
404 case T_USHORT:
405 case T_SHORT:
406 res->size = SHORT_SIZE;
407 break;
408 case T_UINT:
409 case T_INT:
410 res->size = INT_SIZE;
411 break;
412 case T_ULONG:
413 case T_LONG:
414 res->size = LONG_SIZE;
415 break;
416 case T_FLOAT:
417 res->size = FLOAT_SIZE;
418 break;
419 case T_DOUBLE:
420 res->size = DOUBLE_SIZE;
421 break;
422 case T_STRUCT:
423 case T_UNION:
424 if (sym->n_numaux)
425 {
85880250 426 if (aux == NULL)
f9a6a8f0 427 fatal (_("Aggregate definition needs auxiliary information"));
85880250 428
252b5132
RH
429 if (aux->x_sym.x_tagndx.p)
430 {
d93c7787
NC
431 unsigned int idx;
432
433 /* PR 17512: file: e72f3988. */
434 if (aux->x_sym.x_tagndx.l < 0 || aux->x_sym.x_tagndx.p < rawsyms)
435 {
436 non_fatal (_("Invalid tag index %#lx encountered"), aux->x_sym.x_tagndx.l);
437 idx = 0;
438 }
439 else
440 idx = INDEXOF (aux->x_sym.x_tagndx.p);
85880250
NC
441
442 if (idx >= rawcount)
443 {
444 if (rawcount == 0)
445 fatal (_("Symbol index %u encountered when there are no symbols"), idx);
446 non_fatal (_("Invalid symbol index %u encountered"), idx);
447 idx = 0;
448 }
449
450 /* Referring to a struct defined elsewhere. */
252b5132 451 res->type = coff_structref_type;
85880250 452 res->u.astructref.ref = tindex[idx];
252b5132
RH
453 res->size = res->u.astructref.ref ?
454 res->u.astructref.ref->type->size : 0;
455 }
456 else
457 {
85880250 458 /* A definition of a struct. */
252b5132
RH
459 last_struct = res;
460 res->type = coff_structdef_type;
461 res->u.astructdef.elements = empty_scope ();
462 res->u.astructdef.idx = 0;
463 res->u.astructdef.isstruct = (type & 0xf) == T_STRUCT;
464 res->size = aux->x_sym.x_misc.x_lnsz.x_size;
465 }
466 }
467 else
468 {
85880250 469 /* No auxents - it's anonymous. */
252b5132
RH
470 res->type = coff_structref_type;
471 res->u.astructref.ref = 0;
472 res->size = 0;
473 }
474 break;
475 case T_ENUM:
85880250 476 if (aux == NULL)
f9a6a8f0 477 fatal (_("Enum definition needs auxiliary information"));
252b5132
RH
478 if (aux->x_sym.x_tagndx.p)
479 {
85880250
NC
480 unsigned int idx = INDEXOF (aux->x_sym.x_tagndx.p);
481
482 /* PR 17512: file: 1ef037c7. */
483 if (idx >= rawcount)
484 fatal (_("Invalid enum symbol index %u encountered"), idx);
485 /* Referring to a enum defined elsewhere. */
252b5132 486 res->type = coff_enumref_type;
85880250 487 res->u.aenumref.ref = tindex[idx];
5b7d6237
NC
488 /* PR 17512: file: b85b67e8. */
489 if (res->u.aenumref.ref)
490 res->size = res->u.aenumref.ref->type->size;
491 else
492 res->size = 0;
252b5132
RH
493 }
494 else
495 {
85880250 496 /* A definition of an enum. */
252b5132
RH
497 last_enum = res;
498 res->type = coff_enumdef_type;
499 res->u.aenumdef.elements = empty_scope ();
500 res->size = aux->x_sym.x_misc.x_lnsz.x_size;
501 }
502 break;
503 case T_MOE:
504 break;
505 }
506
507 for (which_dt = 5; which_dt >= 0; which_dt--)
508 {
509 switch ((type >> ((which_dt * 2) + 4)) & 0x3)
510 {
511 case 0:
512 break;
513 case DT_ARY:
514 {
515 struct coff_type *ptr = ((struct coff_type *)
516 xmalloc (sizeof (struct coff_type)));
85880250
NC
517 int els;
518
519 if (aux == NULL)
f9a6a8f0 520 fatal (_("Array definition needs auxiliary information"));
85880250
NC
521 els = (dimind < DIMNUM
522 ? aux->x_sym.x_fcnary.x_ary.x_dimen[dimind]
523 : 0);
524
252b5132
RH
525 ++dimind;
526 ptr->type = coff_array_type;
d93c7787
NC
527 /* PR 17512: file: ae1971e2.
528 Check for integer overflow. */
529 {
530 long long a, z;
531 a = els;
532 z = res->size;
533 a *= z;
534 ptr->size = (int) a;
535 if (ptr->size != a)
536 non_fatal (_("Out of range sum for els (%#x) * size (%#x)"), els, res->size);
537 }
252b5132
RH
538 ptr->u.array.dim = els;
539 ptr->u.array.array_of = res;
540 res = ptr;
541 break;
542 }
543 case DT_PTR:
544 {
545 struct coff_type *ptr =
546 (struct coff_type *) xmalloc (sizeof (struct coff_type));
85880250 547
252b5132
RH
548 ptr->size = PTR_SIZE;
549 ptr->type = coff_pointer_type;
550 ptr->u.pointer.points_to = res;
551 res = ptr;
552 break;
553 }
554 case DT_FCN:
555 {
556 struct coff_type *ptr
557 = (struct coff_type *) xmalloc (sizeof (struct coff_type));
85880250 558
252b5132
RH
559 ptr->size = 0;
560 ptr->type = coff_function_type;
561 ptr->u.function.function_returns = res;
562 ptr->u.function.parameters = empty_scope ();
85880250 563 ptr->u.function.lines = do_lines (i, N(sym));
252b5132
RH
564 ptr->u.function.code = 0;
565 last_function_type = ptr;
566 res = ptr;
567 break;
568 }
569 }
570 }
571 return res;
572}
573
574static struct coff_visible *
2da42df6 575do_visible (int i)
252b5132
RH
576{
577 struct internal_syment *sym = &rawsyms[i].u.syment;
578 struct coff_visible *visible =
579 (struct coff_visible *) (xmalloc (sizeof (struct coff_visible)));
580 enum coff_vis_type t;
85880250 581
252b5132
RH
582 switch (sym->n_sclass)
583 {
584 case C_MOS:
585 case C_MOU:
586 case C_FIELD:
587 t = coff_vis_member_of_struct;
588 break;
589 case C_MOE:
590 t = coff_vis_member_of_enum;
591 break;
252b5132
RH
592 case C_REGPARM:
593 t = coff_vis_regparam;
594 break;
252b5132
RH
595 case C_REG:
596 t = coff_vis_register;
597 break;
598 case C_STRTAG:
599 case C_UNTAG:
600 case C_ENTAG:
601 case C_TPDEF:
602 t = coff_vis_tag;
603 break;
604 case C_AUTOARG:
605 case C_ARG:
606 t = coff_vis_autoparam;
607 break;
608 case C_AUTO:
252b5132
RH
609 t = coff_vis_auto;
610 break;
611 case C_LABEL:
612 case C_STAT:
613 t = coff_vis_int_def;
614 break;
615 case C_EXT:
616 if (sym->n_scnum == N_UNDEF)
617 {
618 if (sym->n_value)
619 t = coff_vis_common;
620 else
621 t = coff_vis_ext_ref;
622 }
623 else
624 t = coff_vis_ext_def;
625 break;
626 default:
85880250 627 fatal (_("Unrecognised symbol class: %d"), sym->n_sclass);
252b5132 628 break;
252b5132
RH
629 }
630 visible->type = t;
631 return visible;
632}
633
85880250
NC
634/* Define a symbol and attach to block B. */
635
252b5132 636static int
85880250 637do_define (unsigned int i, struct coff_scope *b)
252b5132
RH
638{
639 static int symbol_index;
85880250 640 struct internal_syment *sym;
252b5132
RH
641 struct coff_symbol *s = empty_symbol ();
642
85880250
NC
643 if (b == NULL)
644 fatal (_("ICE: do_define called without a block"));
645 if (i >= rawcount)
646 fatal (_("Out of range symbol index: %u"), i);
647
648 sym = &rawsyms[i].u.syment;
252b5132 649 s->number = ++symbol_index;
85880250 650 s->name = N(sym);
252b5132 651 s->sfile = cur_sfile;
85880250 652 /* Glue onto the ofile list. */
252b5132
RH
653 if (lofile >= 0)
654 {
655 if (ofile->symbol_list_tail)
656 ofile->symbol_list_tail->next_in_ofile_list = s;
657 else
658 ofile->symbol_list_head = s;
659 ofile->symbol_list_tail = s;
85880250 660 /* And the block list. */
252b5132
RH
661 }
662 if (b->vars_tail)
663 b->vars_tail->next = s;
664 else
665 b->vars_head = s;
666
667 b->vars_tail = s;
668 b->nvars++;
669 s->type = do_type (i);
670 s->where = do_where (i);
671 s->visible = do_visible (i);
672
673 tindex[i] = s;
674
85880250 675 /* We remember the lowest address in each section for each source file. */
252b5132
RH
676 if (s->where->where == coff_where_memory
677 && s->type->type == coff_secdef_type)
678 {
85880250 679 struct coff_isection *is;
252b5132 680
85880250
NC
681 /* PR 17512: file: 4676c97f. */
682 if (cur_sfile == NULL)
683 non_fatal (_("Section referenced before any file is defined"));
684 else
252b5132 685 {
85880250 686 is = cur_sfile->section + s->where->section->number;
252b5132 687
85880250
NC
688 if (!is->init)
689 {
690 is->low = s->where->offset;
d93c7787
NC
691 /* PR 17512: file: 37e7a80d.
692 Check for integer overflow computing low + size. */
693 {
694 long long a, z;
695
696 a = s->where->offset;
697 z = s->type->size;
698 a += z;
699 is->high = (int) a;
700 if (a != is->high)
701 non_fatal (_("Out of range sum for offset (%#x) + size (%#x)"),
702 is->low, s->type->size);
703 }
f641dd96 704 /* PR 17512: file: 37e7a80d. */
951eaaec 705 if (is->high < s->where->offset)
f641dd96 706 fatal (_("Out of range type size: %u"), s->type->size);
85880250
NC
707 is->init = 1;
708 is->parent = s->where->section;
709 }
710 }
252b5132
RH
711 }
712
713 if (s->type->type == coff_function_type)
714 last_function_symbol = s;
715
716 return i + sym->n_numaux + 1;
717}
718
85880250 719static struct coff_ofile *
2da42df6 720doit (void)
252b5132 721{
85880250 722 unsigned int i;
015dc7e1 723 bool infile = false;
252b5132 724 struct coff_ofile *head =
f462a9ea 725 (struct coff_ofile *) xmalloc (sizeof (struct coff_ofile));
85880250 726
252b5132
RH
727 ofile = head;
728 head->source_head = 0;
729 head->source_tail = 0;
730 head->nsources = 0;
731 head->symbol_list_tail = 0;
732 head->symbol_list_head = 0;
733 do_sections_p1 (head);
734 push_scope (1);
735
736 for (i = 0; i < rawcount;)
737 {
738 struct internal_syment *sym = &rawsyms[i].u.syment;
85880250 739
252b5132
RH
740 switch (sym->n_sclass)
741 {
742 case C_FILE:
743 {
85880250 744 /* New source file announced. */
252b5132
RH
745 struct coff_sfile *n =
746 (struct coff_sfile *) xmalloc (sizeof (struct coff_sfile));
85880250 747
252b5132
RH
748 n->section = (struct coff_isection *) xcalloc (sizeof (struct coff_isection), abfd->section_count + 1);
749 cur_sfile = n;
85880250 750 n->name = N(sym);
252b5132
RH
751 n->next = 0;
752
753 if (infile)
85880250
NC
754 pop_scope ();
755 else
015dc7e1 756 infile = true;
85880250 757
252b5132
RH
758 push_scope (1);
759 file_scope = n->scope = top_scope;
760
761 if (head->source_tail)
762 head->source_tail->next = n;
763 else
764 head->source_head = n;
765 head->source_tail = n;
766 head->nsources++;
767 i += sym->n_numaux + 1;
768 }
769 break;
770 case C_FCN:
771 {
85880250
NC
772 char *name = N(sym);
773
252b5132
RH
774 if (name[1] == 'b')
775 {
85880250 776 /* Function start. */
252b5132 777 push_scope (0);
85880250
NC
778 /* PR 17512: file: 0ef7fbaf. */
779 if (last_function_type)
780 last_function_type->u.function.code = top_scope;
5b7d6237
NC
781 /* PR 17512: file: 22908266. */
782 if (sym->n_scnum < ofile->nsections && sym->n_scnum >= 0)
783 top_scope->sec = ofile->sections + sym->n_scnum;
784 else
785 top_scope->sec = NULL;
252b5132
RH
786 top_scope->offset = sym->n_value;
787 }
788 else
789 {
85880250
NC
790 /* PR 17512: file: e92e42e1. */
791 if (top_scope == NULL)
792 fatal (_("Function start encountered without a top level scope."));
252b5132
RH
793 top_scope->size = sym->n_value - top_scope->offset + 1;
794 pop_scope ();
252b5132
RH
795 }
796 i += sym->n_numaux + 1;
797 }
798 break;
799
800 case C_BLOCK:
801 {
85880250
NC
802 char *name = N(sym);
803
252b5132
RH
804 if (name[1] == 'b')
805 {
85880250 806 /* Block start. */
252b5132 807 push_scope (1);
5b7d6237
NC
808 /* PR 17512: file: af7e8e83. */
809 if (sym->n_scnum < ofile->nsections && sym->n_scnum >= 0)
810 top_scope->sec = ofile->sections + sym->n_scnum;
811 else
812 top_scope->sec = NULL;
252b5132 813 top_scope->offset = sym->n_value;
252b5132
RH
814 }
815 else
816 {
85880250
NC
817 if (top_scope == NULL)
818 fatal (_("Block start encountered without a scope for it."));
252b5132
RH
819 top_scope->size = sym->n_value - top_scope->offset + 1;
820 pop_scope ();
821 }
822 i += sym->n_numaux + 1;
823 }
824 break;
825 case C_REGPARM:
826 case C_ARG:
85880250
NC
827 if (last_function_symbol == NULL)
828 fatal (_("Function arguments encountered without a function definition"));
252b5132
RH
829 i = do_define (i, last_function_symbol->type->u.function.parameters);
830 break;
831 case C_MOS:
832 case C_MOU:
833 case C_FIELD:
85880250
NC
834 /* PR 17512: file: 43ab21f4. */
835 if (last_struct == NULL)
836 fatal (_("Structure element encountered without a structure definition"));
252b5132
RH
837 i = do_define (i, last_struct->u.astructdef.elements);
838 break;
839 case C_MOE:
85880250
NC
840 if (last_enum == NULL)
841 fatal (_("Enum element encountered without an enum definition"));
252b5132
RH
842 i = do_define (i, last_enum->u.aenumdef.elements);
843 break;
844 case C_STRTAG:
845 case C_ENTAG:
846 case C_UNTAG:
85880250
NC
847 /* Various definition. */
848 if (top_scope == NULL)
de194d85 849 fatal (_("Aggregate definition encountered without a scope"));
252b5132
RH
850 i = do_define (i, top_scope);
851 break;
852 case C_EXT:
853 case C_LABEL:
85880250 854 if (file_scope == NULL)
de194d85 855 fatal (_("Label definition encountered without a file scope"));
252b5132
RH
856 i = do_define (i, file_scope);
857 break;
858 case C_STAT:
859 case C_TPDEF:
860 case C_AUTO:
861 case C_REG:
85880250 862 if (top_scope == NULL)
de194d85 863 fatal (_("Variable definition encountered without a scope"));
252b5132
RH
864 i = do_define (i, top_scope);
865 break;
252b5132
RH
866 case C_EOS:
867 i += sym->n_numaux + 1;
868 break;
85880250
NC
869 default:
870 fatal (_("Unrecognised symbol class: %d"), sym->n_sclass);
252b5132
RH
871 }
872 }
873 do_sections_p2 (head);
874 return head;
875}
876
877struct coff_ofile *
2da42df6 878coff_grok (bfd *inabfd)
252b5132
RH
879{
880 long storage;
881 struct coff_ofile *p;
882 abfd = inabfd;
85880250
NC
883
884 if (! bfd_family_coff (abfd))
885 {
886 non_fatal (_("%s: is not a COFF format file"), bfd_get_filename (abfd));
887 return NULL;
888 }
3aade688 889
252b5132
RH
890 storage = bfd_get_symtab_upper_bound (abfd);
891
892 if (storage < 0)
c177f377 893 bfd_fatal (bfd_get_filename (abfd));
252b5132
RH
894
895 syms = (asymbol **) xmalloc (storage);
896 symcount = bfd_canonicalize_symtab (abfd, syms);
897 if (symcount < 0)
c177f377 898 bfd_fatal (bfd_get_filename (abfd));
252b5132 899 rawsyms = obj_raw_syments (abfd);
5bb3703f 900 rawcount = obj_raw_syment_count (abfd);
252b5132
RH
901 tindex = (struct coff_symbol **) (xcalloc (sizeof (struct coff_symbol *), rawcount));
902
903 p = doit ();
904 return p;
905}