coffgrok.c 22 KB

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