srconv.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  1. /* srconv.c -- Sysroff conversion program
  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, MA
  15. 02110-1301, USA. */
  16. /* Written by Steve Chamberlain (sac@cygnus.com)
  17. This program can be used to convert a coff object file
  18. into a Hitachi OM/LM (Sysroff) format.
  19. All debugging information is preserved */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "bucomm.h"
  23. #include "sysroff.h"
  24. #include "coffgrok.h"
  25. #include "libiberty.h"
  26. #include "filenames.h"
  27. #include "getopt.h"
  28. #include "coff/internal.h"
  29. #include "../bfd/libcoff.h"
  30. /*#define FOOP1 1 */
  31. static int addrsize;
  32. static char *toolname;
  33. static char **rnames;
  34. static void walk_tree_symbol
  35. (struct coff_sfile *, struct coff_section *, struct coff_symbol *, int);
  36. static void walk_tree_scope
  37. (struct coff_section *, struct coff_sfile *, struct coff_scope *, int, int);
  38. static int find_base (struct coff_sfile *, struct coff_section *);
  39. static void wr_globals (struct coff_ofile *, struct coff_sfile *, int);
  40. static FILE *file;
  41. static bfd *abfd;
  42. static int debug = 0;
  43. static int quick = 0;
  44. static int noprescan = 0;
  45. static struct coff_ofile *tree;
  46. /* Obsolete ??
  47. static int absolute_p;
  48. */
  49. static int segmented_p;
  50. static int code;
  51. static int ids1[20000];
  52. static int ids2[20000];
  53. static int base1 = 0x18;
  54. static int base2 = 0x2018;
  55. static int
  56. get_member_id (int x)
  57. {
  58. if (ids2[x])
  59. return ids2[x];
  60. ids2[x] = base2++;
  61. return ids2[x];
  62. }
  63. static int
  64. get_ordinary_id (int x)
  65. {
  66. if (ids1[x])
  67. return ids1[x];
  68. ids1[x] = base1++;
  69. return ids1[x];
  70. }
  71. static char *
  72. section_translate (char *n)
  73. {
  74. if (strcmp (n, ".text") == 0)
  75. return "P";
  76. if (strcmp (n, ".data") == 0)
  77. return "D";
  78. if (strcmp (n, ".bss") == 0)
  79. return "B";
  80. return n;
  81. }
  82. #define DATE "940201073000"; /* Just a time on my birthday */
  83. static char *
  84. strip_suffix (const char *name)
  85. {
  86. int i;
  87. char *res;
  88. for (i = 0; name[i] != 0 && name[i] != '.'; i++)
  89. ;
  90. res = (char *) xmalloc (i + 1);
  91. memcpy (res, name, i);
  92. res[i] = 0;
  93. return res;
  94. }
  95. /* IT LEN stuff CS */
  96. static void
  97. checksum (FILE *ffile, unsigned char *ptr, int size, int ccode)
  98. {
  99. int j;
  100. int last;
  101. int sum = 0;
  102. int bytes = size / 8;
  103. last = !(ccode & 0xff00);
  104. if (size & 0x7)
  105. fatal (_("Checksum failure"));
  106. ptr[0] = ccode | (last ? 0x80 : 0);
  107. ptr[1] = bytes + 1;
  108. for (j = 0; j < bytes; j++)
  109. sum += ptr[j];
  110. /* Glue on a checksum too. */
  111. ptr[bytes] = ~sum;
  112. if (fwrite (ptr, bytes + 1, 1, ffile) != 1)
  113. /* FIXME: Return error status. */
  114. fatal (_("Failed to write checksum"));
  115. }
  116. static void
  117. writeINT (int n, unsigned char *ptr, int *idx, int size, FILE *ffile)
  118. {
  119. int byte = *idx / 8;
  120. if (size == -2)
  121. size = addrsize;
  122. else if (size == -1)
  123. size = 0;
  124. if (byte > 240)
  125. {
  126. /* Lets write out that record and do another one. */
  127. checksum (ffile, ptr, *idx, code | 0x1000);
  128. *idx = 16;
  129. byte = *idx / 8;
  130. }
  131. switch (size)
  132. {
  133. case 0:
  134. break;
  135. case 1:
  136. ptr[byte] = n;
  137. break;
  138. case 2:
  139. ptr[byte + 0] = n >> 8;
  140. ptr[byte + 1] = n;
  141. break;
  142. case 4:
  143. ptr[byte + 0] = n >> 24;
  144. ptr[byte + 1] = n >> 16;
  145. ptr[byte + 2] = n >> 8;
  146. ptr[byte + 3] = n >> 0;
  147. break;
  148. default:
  149. fatal (_("Unsupported integer write size: %d"), size);
  150. }
  151. *idx += size * 8;
  152. }
  153. static void
  154. writeBITS (int val, unsigned char *ptr, int *idx, int size)
  155. {
  156. int byte = *idx / 8;
  157. int bit = *idx % 8;
  158. int old;
  159. *idx += size;
  160. old = ptr[byte];
  161. /* Turn off all about to change bits. */
  162. old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
  163. /* Turn on the bits we want. */
  164. old |= (val & ((1 << size) - 1)) << (8 - bit - size);
  165. ptr[byte] = old;
  166. }
  167. static void
  168. writeBARRAY (barray data, unsigned char *ptr, int *idx,
  169. int size ATTRIBUTE_UNUSED, FILE *ffile)
  170. {
  171. int i;
  172. writeINT (data.len, ptr, idx, 1, ffile);
  173. for (i = 0; i < data.len; i++)
  174. writeINT (data.data[i], ptr, idx, 1, ffile);
  175. }
  176. static void
  177. writeCHARS (char *string, unsigned char *ptr, int *idx, int size, FILE *ffile)
  178. {
  179. int i = *idx / 8;
  180. if (i > 240)
  181. {
  182. /* Lets write out that record and do another one. */
  183. checksum (ffile, ptr, *idx, code | 0x1000);
  184. *idx = 16;
  185. i = *idx / 8;
  186. }
  187. if (size == 0)
  188. {
  189. /* Variable length string. */
  190. size = strlen (string);
  191. ptr[i++] = size;
  192. }
  193. /* BUG WAITING TO HAPPEN. */
  194. memcpy (ptr + i, string, size);
  195. i += size;
  196. *idx = i * 8;
  197. }
  198. #define SYSROFF_SWAP_OUT
  199. #include "sysroff.c"
  200. static char *rname_sh[] =
  201. {
  202. "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
  203. };
  204. static char *rname_h8300[] =
  205. {
  206. "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
  207. };
  208. static void
  209. wr_tr (void)
  210. {
  211. /* The TR block is not normal - it doesn't have any contents. */
  212. static char b[] =
  213. {
  214. 0xff, /* IT */
  215. 0x03, /* RL */
  216. 0xfd, /* CS */
  217. };
  218. if (fwrite (b, sizeof (b), 1, file) != 1)
  219. /* FIXME: Return error status. */
  220. fatal (_("Failed to write TR block"));
  221. }
  222. static void
  223. wr_un (struct coff_ofile *ptr, struct coff_sfile *sfile, int first,
  224. int nsecs ATTRIBUTE_UNUSED)
  225. {
  226. struct IT_un un;
  227. struct coff_symbol *s;
  228. un.spare1 = 0;
  229. if (bfd_get_file_flags (abfd) & EXEC_P)
  230. un.format = FORMAT_LM;
  231. else
  232. un.format = FORMAT_OM;
  233. un.spare1 = 0;
  234. /* Don't count the abs section. */
  235. un.nsections = ptr->nsections - 1;
  236. un.nextdefs = 0;
  237. un.nextrefs = 0;
  238. /* Count all the undefined and defined variables with global scope. */
  239. if (first)
  240. {
  241. for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
  242. {
  243. if (s->visible->type == coff_vis_ext_def
  244. || s->visible->type == coff_vis_common)
  245. un.nextdefs++;
  246. if (s->visible->type == coff_vis_ext_ref)
  247. un.nextrefs++;
  248. }
  249. }
  250. un.tool = toolname;
  251. un.tcd = DATE;
  252. un.linker = "L_GX00";
  253. un.lcd = DATE;
  254. un.name = sfile->name;
  255. sysroff_swap_un_out (file, &un);
  256. }
  257. static void
  258. wr_hd (struct coff_ofile *p)
  259. {
  260. struct IT_hd hd;
  261. hd.spare1 = 0;
  262. if (bfd_get_file_flags (abfd) & EXEC_P)
  263. hd.mt = MTYPE_ABS_LM;
  264. else
  265. hd.mt = MTYPE_OMS_OR_LMS;
  266. hd.cd = DATE;
  267. hd.nu = p->nsources; /* Always one unit */
  268. hd.code = 0; /* Always ASCII */
  269. hd.ver = "0200"; /* Version 2.00 */
  270. switch (bfd_get_arch (abfd))
  271. {
  272. case bfd_arch_h8300:
  273. hd.au = 8;
  274. hd.si = 0;
  275. hd.spcsz = 32;
  276. hd.segsz = 0;
  277. hd.segsh = 0;
  278. switch (bfd_get_mach (abfd))
  279. {
  280. case bfd_mach_h8300:
  281. hd.cpu = "H8300";
  282. hd.afl = 2;
  283. addrsize = 2;
  284. toolname = "C_H8/300";
  285. break;
  286. case bfd_mach_h8300h:
  287. hd.cpu = "H8300H";
  288. hd.afl = 4;
  289. addrsize = 4;
  290. toolname = "C_H8/300H";
  291. break;
  292. case bfd_mach_h8300s:
  293. hd.cpu = "H8300S";
  294. hd.afl = 4;
  295. addrsize = 4;
  296. toolname = "C_H8/300S";
  297. break;
  298. default:
  299. fatal (_("Unrecognized H8300 sub-architecture: %ld"),
  300. bfd_get_mach (abfd));
  301. }
  302. rnames = rname_h8300;
  303. break;
  304. case bfd_arch_sh:
  305. hd.au = 8;
  306. hd.si = 0;
  307. hd.afl = 4;
  308. hd.spcsz = 32;
  309. hd.segsz = 0;
  310. hd.segsh = 0;
  311. hd.cpu = "SH";
  312. addrsize = 4;
  313. toolname = "C_SH";
  314. rnames = rname_sh;
  315. break;
  316. default:
  317. fatal (_("Unsupported architecture: %d"), bfd_get_arch (abfd));
  318. }
  319. if (! (bfd_get_file_flags(abfd) & EXEC_P))
  320. {
  321. hd.ep = 0;
  322. }
  323. else
  324. {
  325. hd.ep = 1;
  326. hd.uan = 0;
  327. hd.sa = 0;
  328. hd.sad = 0;
  329. hd.address = bfd_get_start_address (abfd);
  330. }
  331. hd.os = "";
  332. hd.sys = "";
  333. hd.mn = strip_suffix (bfd_get_filename (abfd));
  334. sysroff_swap_hd_out (file, &hd);
  335. }
  336. static void
  337. wr_sh (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_section *sec)
  338. {
  339. struct IT_sh sh;
  340. sh.unit = 0;
  341. sh.section = sec->number;
  342. #ifdef FOOP1
  343. sh.section = 0;
  344. #endif
  345. sysroff_swap_sh_out (file, &sh);
  346. }
  347. static void
  348. wr_ob (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_section *section)
  349. {
  350. bfd_size_type i;
  351. int first = 1;
  352. unsigned char stuff[200];
  353. i = 0;
  354. while (i < bfd_section_size (section->bfd_section))
  355. {
  356. struct IT_ob ob;
  357. int todo = 200; /* Copy in 200 byte lumps. */
  358. ob.spare = 0;
  359. if (i + todo > bfd_section_size (section->bfd_section))
  360. todo = bfd_section_size (section->bfd_section) - i;
  361. if (first)
  362. {
  363. ob.saf = 1;
  364. if (bfd_get_file_flags (abfd) & EXEC_P)
  365. ob.address = section->address;
  366. else
  367. ob.address = 0;
  368. first = 0;
  369. }
  370. else
  371. {
  372. ob.saf = 0;
  373. }
  374. ob.cpf = 0; /* Never compress. */
  375. ob.data.len = todo;
  376. bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
  377. ob.data.data = stuff;
  378. sysroff_swap_ob_out (file, &ob /*, i + todo < section->size */ );
  379. i += todo;
  380. }
  381. /* Now fill the rest with blanks. */
  382. while (i < (bfd_size_type) section->size)
  383. {
  384. struct IT_ob ob;
  385. int todo = 200; /* Copy in 200 byte lumps. */
  386. ob.spare = 0;
  387. if (i + todo > (bfd_size_type) section->size)
  388. todo = section->size - i;
  389. ob.saf = 0;
  390. ob.cpf = 0; /* Never compress. */
  391. ob.data.len = todo;
  392. memset (stuff, 0, todo);
  393. ob.data.data = stuff;
  394. sysroff_swap_ob_out (file, &ob);
  395. i += todo;
  396. }
  397. /* Now fill the rest with blanks. */
  398. }
  399. static void
  400. wr_rl (struct coff_ofile *ptr ATTRIBUTE_UNUSED, struct coff_section *sec)
  401. {
  402. int nr = sec->nrelocs;
  403. int i;
  404. for (i = 0; i < nr; i++)
  405. {
  406. struct coff_reloc *r = sec->relocs + i;
  407. struct coff_symbol *ref;
  408. struct IT_rl rl;
  409. rl.apol = 0;
  410. rl.boundary = 0;
  411. rl.segment = 1;
  412. rl.sign = 0;
  413. rl.check = 0;
  414. rl.addr = r->offset;
  415. rl.bitloc = 0;
  416. rl.flen = 32; /* SH Specific. */
  417. /* What sort of reloc ? Look in the section to find out. */
  418. ref = r->symbol;
  419. if (ref->visible->type == coff_vis_ext_ref)
  420. {
  421. rl.bcount = 4; /* Always 4 for us. */
  422. rl.op = OP_EXT_REF;
  423. rl.symn = ref->er_number;
  424. }
  425. else if (ref->visible->type == coff_vis_common)
  426. {
  427. rl.bcount = 11; /* Always 11 for us. */
  428. rl.op = OP_SEC_REF;
  429. rl.secn = ref->where->section->number;
  430. rl.copcode_is_3 = 3;
  431. rl.alength_is_4 = 4;
  432. rl.addend = ref->where->offset - ref->where->section->address;
  433. rl.aopcode_is_0x20 = 0x20;
  434. }
  435. else
  436. {
  437. rl.bcount = 11; /* Always 11 for us. */
  438. rl.op = OP_SEC_REF;
  439. rl.secn = ref->where->section->number;
  440. rl.copcode_is_3 = 3;
  441. rl.alength_is_4 = 4;
  442. rl.addend = -ref->where->section->address;
  443. rl.aopcode_is_0x20 = 0x20;
  444. }
  445. rl.end = 0xff;
  446. if ( rl.op == OP_SEC_REF
  447. || rl.op == OP_EXT_REF)
  448. sysroff_swap_rl_out (file, &rl);
  449. }
  450. }
  451. static void
  452. wr_object_body (struct coff_ofile *p)
  453. {
  454. int i;
  455. for (i = 1; i < p->nsections; i++)
  456. {
  457. wr_sh (p, p->sections + i);
  458. wr_ob (p, p->sections + i);
  459. wr_rl (p, p->sections + i);
  460. }
  461. }
  462. static void
  463. wr_dps_start (struct coff_sfile *sfile,
  464. struct coff_section *section ATTRIBUTE_UNUSED,
  465. struct coff_scope *scope, int type, int nest)
  466. {
  467. struct IT_dps dps;
  468. dps.end = 0;
  469. dps.opt = 0;
  470. dps.type = type;
  471. if (scope->sec)
  472. {
  473. dps.san = scope->sec->number;
  474. dps.address = scope->offset - find_base (sfile, scope->sec);
  475. dps.block_size = scope->size;
  476. if (debug)
  477. {
  478. printf ("DPS %s %d %x\n",
  479. sfile->name,
  480. nest,
  481. dps.address);
  482. }
  483. }
  484. else
  485. {
  486. dps.san = 0;
  487. dps.address = 0;
  488. dps.block_size = 0;
  489. }
  490. dps.nesting = nest;
  491. dps.neg = 0x1001;
  492. sysroff_swap_dps_out (file, &dps);
  493. }
  494. static void
  495. wr_dps_end (struct coff_section *section ATTRIBUTE_UNUSED,
  496. struct coff_scope *scope ATTRIBUTE_UNUSED, int type)
  497. {
  498. struct IT_dps dps;
  499. dps.end = 1;
  500. dps.type = type;
  501. sysroff_swap_dps_out (file, &dps);
  502. }
  503. static int *
  504. nints (int x)
  505. {
  506. return (int *) (xcalloc (sizeof (int), x));
  507. }
  508. static void
  509. walk_tree_type_1 (struct coff_sfile *sfile, struct coff_symbol *symbol,
  510. struct coff_type *type, int nest)
  511. {
  512. switch (type->type)
  513. {
  514. case coff_secdef_type:
  515. case coff_basic_type:
  516. {
  517. struct IT_dbt dbt;
  518. switch (type->u.basic)
  519. {
  520. case T_NULL:
  521. case T_VOID:
  522. dbt.btype = BTYPE_VOID;
  523. dbt.sign = BTYPE_UNSPEC;
  524. dbt.fptype = FPTYPE_NOTSPEC;
  525. break;
  526. case T_CHAR:
  527. dbt.btype = BTYPE_CHAR;
  528. dbt.sign = BTYPE_UNSPEC;
  529. dbt.fptype = FPTYPE_NOTSPEC;
  530. break;
  531. case T_SHORT:
  532. case T_INT:
  533. case T_LONG:
  534. dbt.btype = BTYPE_INT;
  535. dbt.sign = SIGN_SIGNED;
  536. dbt.fptype = FPTYPE_NOTSPEC;
  537. break;
  538. case T_FLOAT:
  539. dbt.btype = BTYPE_FLOAT;
  540. dbt.fptype = FPTYPE_SINGLE;
  541. break;
  542. case T_DOUBLE:
  543. dbt.btype = BTYPE_FLOAT;
  544. dbt.fptype = FPTYPE_DOUBLE;
  545. break;
  546. case T_LNGDBL:
  547. dbt.btype = BTYPE_FLOAT;
  548. dbt.fptype = FPTYPE_EXTENDED;
  549. break;
  550. case T_UCHAR:
  551. dbt.btype = BTYPE_CHAR;
  552. dbt.sign = SIGN_UNSIGNED;
  553. dbt.fptype = FPTYPE_NOTSPEC;
  554. break;
  555. case T_USHORT:
  556. case T_UINT:
  557. case T_ULONG:
  558. dbt.btype = BTYPE_INT;
  559. dbt.sign = SIGN_UNSIGNED;
  560. dbt.fptype = FPTYPE_NOTSPEC;
  561. break;
  562. }
  563. dbt.bitsize = type->size;
  564. dbt.neg = 0x1001;
  565. sysroff_swap_dbt_out (file, &dbt);
  566. break;
  567. }
  568. case coff_pointer_type:
  569. {
  570. struct IT_dpt dpt;
  571. dpt.dunno = 0;
  572. walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
  573. dpt.neg = 0x1001;
  574. sysroff_swap_dpt_out (file, &dpt);
  575. break;
  576. }
  577. case coff_function_type:
  578. {
  579. struct IT_dfp dfp;
  580. struct coff_symbol *param;
  581. dfp.end = 0;
  582. dfp.spare = 0;
  583. dfp.nparams = type->u.function.parameters->nvars;
  584. dfp.neg = 0x1001;
  585. walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
  586. sysroff_swap_dfp_out (file, &dfp);
  587. for (param = type->u.function.parameters->vars_head;
  588. param;
  589. param = param->next)
  590. walk_tree_symbol (sfile, 0, param, nest);
  591. dfp.end = 1;
  592. sysroff_swap_dfp_out (file, &dfp);
  593. break;
  594. }
  595. case coff_structdef_type:
  596. {
  597. struct IT_dbt dbt;
  598. struct IT_dds dds;
  599. struct coff_symbol *member;
  600. dds.spare = 0;
  601. dbt.btype = BTYPE_STRUCT;
  602. dbt.bitsize = type->size;
  603. dbt.sign = SIGN_UNSPEC;
  604. dbt.fptype = FPTYPE_NOTSPEC;
  605. dbt.sid = get_member_id (type->u.astructdef.idx);
  606. dbt.neg = 0x1001;
  607. sysroff_swap_dbt_out (file, &dbt);
  608. dds.end = 0;
  609. dds.neg = 0x1001;
  610. sysroff_swap_dds_out (file, &dds);
  611. for (member = type->u.astructdef.elements->vars_head;
  612. member;
  613. member = member->next)
  614. walk_tree_symbol (sfile, 0, member, nest + 1);
  615. dds.end = 1;
  616. sysroff_swap_dds_out (file, &dds);
  617. }
  618. break;
  619. case coff_structref_type:
  620. {
  621. struct IT_dbt dbt;
  622. dbt.btype = BTYPE_TAG;
  623. dbt.bitsize = type->size;
  624. dbt.sign = SIGN_UNSPEC;
  625. dbt.fptype = FPTYPE_NOTSPEC;
  626. if (type->u.astructref.ref)
  627. dbt.sid = get_member_id (type->u.astructref.ref->number);
  628. else
  629. dbt.sid = 0;
  630. dbt.neg = 0x1001;
  631. sysroff_swap_dbt_out (file, &dbt);
  632. }
  633. break;
  634. case coff_array_type:
  635. {
  636. struct IT_dar dar;
  637. int j;
  638. int dims = 1; /* Only output one dimension at a time. */
  639. dar.dims = dims;
  640. dar.variable = nints (dims);
  641. dar.subtype = nints (dims);
  642. dar.spare = nints (dims);
  643. dar.max_variable = nints (dims);
  644. dar.maxspare = nints (dims);
  645. dar.max = nints (dims);
  646. dar.min_variable = nints (dims);
  647. dar.min = nints (dims);
  648. dar.minspare = nints (dims);
  649. dar.neg = 0x1001;
  650. dar.length = type->size / type->u.array.dim;
  651. for (j = 0; j < dims; j++)
  652. {
  653. dar.variable[j] = VARIABLE_FIXED;
  654. dar.subtype[j] = SUB_INTEGER;
  655. dar.spare[j] = 0;
  656. dar.max_variable[j] = 0;
  657. dar.max[j] = type->u.array.dim;
  658. dar.min_variable[j] = 0;
  659. dar.min[j] = 1; /* Why isn't this 0 ? */
  660. }
  661. walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
  662. sysroff_swap_dar_out (file, &dar);
  663. }
  664. break;
  665. case coff_enumdef_type:
  666. {
  667. struct IT_dbt dbt;
  668. struct IT_den den;
  669. struct coff_symbol *member;
  670. dbt.btype = BTYPE_ENUM;
  671. dbt.bitsize = type->size;
  672. dbt.sign = SIGN_UNSPEC;
  673. dbt.fptype = FPTYPE_NOTSPEC;
  674. dbt.sid = get_member_id (type->u.aenumdef.idx);
  675. dbt.neg = 0x1001;
  676. sysroff_swap_dbt_out (file, &dbt);
  677. den.end = 0;
  678. den.neg = 0x1001;
  679. den.spare = 0;
  680. sysroff_swap_den_out (file, &den);
  681. for (member = type->u.aenumdef.elements->vars_head;
  682. member;
  683. member = member->next)
  684. walk_tree_symbol (sfile, 0, member, nest + 1);
  685. den.end = 1;
  686. sysroff_swap_den_out (file, &den);
  687. }
  688. break;
  689. case coff_enumref_type:
  690. {
  691. struct IT_dbt dbt;
  692. dbt.btype = BTYPE_TAG;
  693. dbt.bitsize = type->size;
  694. dbt.sign = SIGN_UNSPEC;
  695. dbt.fptype = FPTYPE_NOTSPEC;
  696. dbt.sid = get_member_id (type->u.aenumref.ref->number);
  697. dbt.neg = 0x1001;
  698. sysroff_swap_dbt_out (file, &dbt);
  699. }
  700. break;
  701. default:
  702. fatal (_("Unrecognised type: %d"), type->type);
  703. }
  704. }
  705. /* Obsolete ?
  706. static void
  707. dty_start ()
  708. {
  709. struct IT_dty dty;
  710. dty.end = 0;
  711. dty.neg = 0x1001;
  712. dty.spare = 0;
  713. sysroff_swap_dty_out (file, &dty);
  714. }
  715. static void
  716. dty_stop ()
  717. {
  718. struct IT_dty dty;
  719. dty.end = 0;
  720. dty.neg = 0x1001;
  721. dty.end = 1;
  722. sysroff_swap_dty_out (file, &dty);
  723. }
  724. static void
  725. dump_tree_structure (sfile, symbol, type, nest)
  726. struct coff_sfile *sfile;
  727. struct coff_symbol *symbol;
  728. struct coff_type *type;
  729. int nest;
  730. {
  731. if (symbol->type->type == coff_function_type)
  732. {
  733. }
  734. }
  735. */
  736. static void
  737. walk_tree_type (struct coff_sfile *sfile, struct coff_symbol *symbol,
  738. struct coff_type *type, int nest)
  739. {
  740. struct IT_dty dty;
  741. dty.spare = 0;
  742. dty.end = 0;
  743. dty.neg = 0x1001;
  744. if (symbol->type->type == coff_function_type)
  745. {
  746. sysroff_swap_dty_out (file, &dty);
  747. walk_tree_type_1 (sfile, symbol, type, nest);
  748. dty.end = 1;
  749. sysroff_swap_dty_out (file, &dty);
  750. wr_dps_start (sfile,
  751. symbol->where->section,
  752. symbol->type->u.function.code,
  753. BLOCK_TYPE_FUNCTION, nest);
  754. wr_dps_start (sfile, symbol->where->section,
  755. symbol->type->u.function.code,
  756. BLOCK_TYPE_BLOCK, nest);
  757. walk_tree_scope (symbol->where->section,
  758. sfile,
  759. symbol->type->u.function.code,
  760. nest + 1, BLOCK_TYPE_BLOCK);
  761. wr_dps_end (symbol->where->section,
  762. symbol->type->u.function.code,
  763. BLOCK_TYPE_BLOCK);
  764. wr_dps_end (symbol->where->section,
  765. symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
  766. }
  767. else
  768. {
  769. sysroff_swap_dty_out (file, &dty);
  770. walk_tree_type_1 (sfile, symbol, type, nest);
  771. dty.end = 1;
  772. sysroff_swap_dty_out (file, &dty);
  773. }
  774. }
  775. static void
  776. walk_tree_symbol (struct coff_sfile *sfile, struct coff_section *section ATTRIBUTE_UNUSED, struct coff_symbol *symbol, int nest)
  777. {
  778. struct IT_dsy dsy;
  779. memset (&dsy, 0, sizeof(dsy));
  780. dsy.nesting = nest;
  781. switch (symbol->type->type)
  782. {
  783. case coff_function_type:
  784. dsy.type = STYPE_FUNC;
  785. dsy.assign = 1;
  786. break;
  787. case coff_structref_type:
  788. case coff_pointer_type:
  789. case coff_array_type:
  790. case coff_basic_type:
  791. case coff_enumref_type:
  792. dsy.type = STYPE_VAR;
  793. dsy.assign = 1;
  794. break;
  795. case coff_enumdef_type:
  796. dsy.type = STYPE_TAG;
  797. dsy.assign = 0;
  798. dsy.magic = 2;
  799. break;
  800. case coff_structdef_type:
  801. dsy.type = STYPE_TAG;
  802. dsy.assign = 0;
  803. dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
  804. break;
  805. case coff_secdef_type:
  806. return;
  807. default:
  808. fatal (_("Unrecognised coff symbol type: %d"), symbol->type->type);
  809. }
  810. if (symbol->where->where == coff_where_member_of_struct)
  811. {
  812. dsy.assign = 0;
  813. dsy.type = STYPE_MEMBER;
  814. }
  815. if (symbol->where->where == coff_where_member_of_enum)
  816. {
  817. dsy.type = STYPE_ENUM;
  818. dsy.assign = 0;
  819. dsy.evallen = 4;
  820. dsy.evalue = symbol->where->offset;
  821. }
  822. if (symbol->type->type == coff_structdef_type
  823. || symbol->where->where == coff_where_entag
  824. || symbol->where->where == coff_where_strtag)
  825. {
  826. dsy.snumber = get_member_id (symbol->number);
  827. }
  828. else
  829. {
  830. dsy.snumber = get_ordinary_id (symbol->number);
  831. }
  832. dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
  833. switch (symbol->visible->type)
  834. {
  835. case coff_vis_common:
  836. case coff_vis_ext_def:
  837. dsy.ainfo = AINFO_STATIC_EXT_DEF;
  838. break;
  839. case coff_vis_ext_ref:
  840. dsy.ainfo = AINFO_STATIC_EXT_REF;
  841. break;
  842. case coff_vis_int_def:
  843. dsy.ainfo = AINFO_STATIC_INT;
  844. break;
  845. case coff_vis_auto:
  846. case coff_vis_autoparam:
  847. dsy.ainfo = AINFO_AUTO;
  848. break;
  849. case coff_vis_register:
  850. case coff_vis_regparam:
  851. dsy.ainfo = AINFO_REG;
  852. break;
  853. break;
  854. case coff_vis_tag:
  855. case coff_vis_member_of_struct:
  856. case coff_vis_member_of_enum:
  857. break;
  858. default:
  859. fatal (_("Unrecognised coff symbol visibility: %d"), symbol->visible->type);
  860. }
  861. dsy.dlength = symbol->type->size;
  862. switch (symbol->where->where)
  863. {
  864. case coff_where_memory:
  865. dsy.section = symbol->where->section->number;
  866. #ifdef FOOP
  867. dsy.section = 0;
  868. #endif
  869. break;
  870. case coff_where_member_of_struct:
  871. case coff_where_member_of_enum:
  872. case coff_where_stack:
  873. case coff_where_register:
  874. case coff_where_unknown:
  875. case coff_where_strtag:
  876. case coff_where_entag:
  877. case coff_where_typedef:
  878. break;
  879. default:
  880. fatal (_("Unrecognised coff symbol location: %d"), symbol->where->where);
  881. }
  882. switch (symbol->where->where)
  883. {
  884. case coff_where_memory:
  885. dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
  886. break;
  887. case coff_where_stack:
  888. dsy.address = symbol->where->offset;
  889. break;
  890. case coff_where_member_of_struct:
  891. if (symbol->where->bitsize)
  892. {
  893. int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
  894. dsy.bitunit = 1;
  895. dsy.field_len = symbol->where->bitsize;
  896. dsy.field_off = (bits / 32) * 4;
  897. dsy.field_bitoff = bits % 32;
  898. }
  899. else
  900. {
  901. dsy.bitunit = 0;
  902. dsy.field_len = symbol->type->size;
  903. dsy.field_off = symbol->where->offset;
  904. }
  905. break;
  906. case coff_where_member_of_enum:
  907. /* dsy.bitunit = 0;
  908. dsy.field_len = symbol->type->size;
  909. dsy.field_off = symbol->where->offset; */
  910. break;
  911. case coff_where_register:
  912. case coff_where_unknown:
  913. case coff_where_strtag:
  914. case coff_where_entag:
  915. case coff_where_typedef:
  916. break;
  917. default:
  918. fatal (_("Unrecognised coff symbol location: %d"), symbol->where->where);
  919. }
  920. if (symbol->where->where == coff_where_register)
  921. dsy.reg = rnames[symbol->where->offset];
  922. switch (symbol->visible->type)
  923. {
  924. case coff_vis_common:
  925. /* We do this 'cause common C symbols are treated as extdefs. */
  926. case coff_vis_ext_def:
  927. case coff_vis_ext_ref:
  928. dsy.ename = symbol->name;
  929. break;
  930. case coff_vis_regparam:
  931. case coff_vis_autoparam:
  932. dsy.type = STYPE_PARAMETER;
  933. break;
  934. case coff_vis_int_def:
  935. case coff_vis_auto:
  936. case coff_vis_register:
  937. case coff_vis_tag:
  938. case coff_vis_member_of_struct:
  939. case coff_vis_member_of_enum:
  940. break;
  941. default:
  942. fatal (_("Unrecognised coff symbol visibility: %d"), symbol->visible->type);
  943. }
  944. dsy.sfn = 0;
  945. dsy.sln = 2;
  946. dsy.neg = 0x1001;
  947. sysroff_swap_dsy_out (file, &dsy);
  948. walk_tree_type (sfile, symbol, symbol->type, nest);
  949. }
  950. static void
  951. walk_tree_scope (struct coff_section *section, struct coff_sfile *sfile, struct coff_scope *scope, int nest, int type)
  952. {
  953. struct coff_symbol *vars;
  954. struct coff_scope *child;
  955. if (scope->vars_head
  956. || (scope->list_head && scope->list_head->vars_head))
  957. {
  958. wr_dps_start (sfile, section, scope, type, nest);
  959. if (nest == 0)
  960. wr_globals (tree, sfile, nest + 1);
  961. for (vars = scope->vars_head; vars; vars = vars->next)
  962. walk_tree_symbol (sfile, section, vars, nest);
  963. for (child = scope->list_head; child; child = child->next)
  964. walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
  965. wr_dps_end (section, scope, type);
  966. }
  967. }
  968. static void
  969. walk_tree_sfile (struct coff_section *section, struct coff_sfile *sfile)
  970. {
  971. walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
  972. }
  973. static void
  974. wr_program_structure (struct coff_ofile *p, struct coff_sfile *sfile)
  975. {
  976. if (p->nsections < 4)
  977. return;
  978. walk_tree_sfile (p->sections + 4, sfile);
  979. }
  980. static void
  981. wr_du (struct coff_ofile *p, struct coff_sfile *sfile, int n)
  982. {
  983. struct IT_du du;
  984. int lim;
  985. int i;
  986. int j;
  987. unsigned int *lowest = (unsigned *) nints (p->nsections);
  988. unsigned int *highest = (unsigned *) nints (p->nsections);
  989. du.format = bfd_get_file_flags (abfd) & EXEC_P ? 0 : 1;
  990. du.optimized = 0;
  991. du.stackfrmt = 0;
  992. du.spare = 0;
  993. du.unit = n;
  994. du.sections = p->nsections - 1;
  995. du.san = (int *) xcalloc (sizeof (int), du.sections);
  996. du.address = nints (du.sections);
  997. du.length = nints (du.sections);
  998. for (i = 0; i < du.sections; i++)
  999. {
  1000. lowest[i] = ~0;
  1001. highest[i] = 0;
  1002. }
  1003. lim = du.sections;
  1004. for (j = 0; j < lim; j++)
  1005. {
  1006. int src = j;
  1007. int dst = j;
  1008. du.san[dst] = dst;
  1009. if (sfile->section[src].init)
  1010. {
  1011. du.length[dst]
  1012. = sfile->section[src].high - sfile->section[src].low + 1;
  1013. du.address[dst]
  1014. = sfile->section[src].low;
  1015. }
  1016. else
  1017. {
  1018. du.length[dst] = 0;
  1019. du.address[dst] = 0;
  1020. }
  1021. if (debug)
  1022. {
  1023. if (sfile->section[src].parent)
  1024. {
  1025. printf (" section %6s 0x%08x..0x%08x\n",
  1026. sfile->section[src].parent->name,
  1027. du.address[dst],
  1028. du.address[dst] + du.length[dst] - 1);
  1029. }
  1030. }
  1031. du.sections = dst + 1;
  1032. }
  1033. du.tool = "c_gcc";
  1034. du.date = DATE;
  1035. sysroff_swap_du_out (file, &du);
  1036. }
  1037. static void
  1038. wr_dus (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile)
  1039. {
  1040. struct IT_dus dus;
  1041. dus.efn = 0x1001;
  1042. dus.ns = 1; /* p->nsources; sac 14 jul 94 */
  1043. dus.drb = nints (dus.ns);
  1044. dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
  1045. dus.spare = nints (dus.ns);
  1046. dus.ndir = 0;
  1047. /* Find the filenames. */
  1048. dus.drb[0] = 0;
  1049. dus.fname[0] = sfile->name;
  1050. sysroff_swap_dus_out (file, &dus);
  1051. }
  1052. /* Find the offset of the .text section for this sfile in the
  1053. .text section for the output file. */
  1054. static int
  1055. find_base (struct coff_sfile *sfile, struct coff_section *section)
  1056. {
  1057. return sfile->section[section->number].low;
  1058. }
  1059. static void
  1060. wr_dln (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile,
  1061. int n ATTRIBUTE_UNUSED)
  1062. {
  1063. /* Count up all the linenumbers */
  1064. struct coff_symbol *sy;
  1065. int lc = 0;
  1066. struct IT_dln dln;
  1067. int idx;
  1068. for (sy = sfile->scope->vars_head;
  1069. sy;
  1070. sy = sy->next)
  1071. {
  1072. struct coff_type *t = sy->type;
  1073. if (t->type == coff_function_type)
  1074. {
  1075. struct coff_line *l = t->u.function.lines;
  1076. if (l)
  1077. lc += l->nlines;
  1078. }
  1079. }
  1080. dln.sfn = nints (lc);
  1081. dln.sln = nints (lc);
  1082. dln.cc = nints (lc);
  1083. dln.section = nints (lc);
  1084. dln.from_address = nints (lc);
  1085. dln.to_address = nints (lc);
  1086. dln.neg = 0x1001;
  1087. dln.nln = lc;
  1088. /* Run through once more and fill up the structure */
  1089. idx = 0;
  1090. for (sy = sfile->scope->vars_head;
  1091. sy;
  1092. sy = sy->next)
  1093. {
  1094. if (sy->type->type == coff_function_type)
  1095. {
  1096. int i;
  1097. struct coff_line *l = sy->type->u.function.lines;
  1098. if (l)
  1099. {
  1100. int base = find_base (sfile, sy->where->section);
  1101. for (i = 0; i < l->nlines; i++)
  1102. {
  1103. dln.section[idx] = sy->where->section->number;
  1104. dln.sfn[idx] = 0;
  1105. dln.sln[idx] = l->lines[i];
  1106. dln.from_address[idx] =
  1107. l->addresses[i] + sy->where->section->address - base;
  1108. dln.cc[idx] = 0;
  1109. if (idx)
  1110. dln.to_address[idx - 1] = dln.from_address[idx];
  1111. idx++;
  1112. }
  1113. dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
  1114. }
  1115. }
  1116. }
  1117. if (lc)
  1118. sysroff_swap_dln_out (file, &dln);
  1119. }
  1120. /* Write the global symbols out to the debug info. */
  1121. static void
  1122. wr_globals (struct coff_ofile *p, struct coff_sfile *sfile,
  1123. int n ATTRIBUTE_UNUSED)
  1124. {
  1125. struct coff_symbol *sy;
  1126. for (sy = p->symbol_list_head;
  1127. sy;
  1128. sy = sy->next_in_ofile_list)
  1129. {
  1130. if (sy->visible->type == coff_vis_ext_def
  1131. || sy->visible->type == coff_vis_ext_ref)
  1132. {
  1133. /* Only write out symbols if they belong to
  1134. the current source file. */
  1135. if (sy->sfile == sfile)
  1136. walk_tree_symbol (sfile, 0, sy, 0);
  1137. }
  1138. }
  1139. }
  1140. static void
  1141. wr_debug (struct coff_ofile *p)
  1142. {
  1143. struct coff_sfile *sfile;
  1144. int n = 0;
  1145. for (sfile = p->source_head;
  1146. sfile;
  1147. sfile = sfile->next)
  1148. {
  1149. if (debug)
  1150. printf ("%s\n", sfile->name);
  1151. wr_du (p, sfile, n);
  1152. wr_dus (p, sfile);
  1153. wr_program_structure (p, sfile);
  1154. wr_dln (p, sfile, n);
  1155. n++;
  1156. }
  1157. }
  1158. static void
  1159. wr_cs (void)
  1160. {
  1161. /* It seems that the CS struct is not normal - the size is wrong
  1162. heres one I prepared earlier. */
  1163. static char b[] =
  1164. {
  1165. 0x80, /* IT */
  1166. 0x21, /* RL */
  1167. 0x00, /* number of chars in variable length part */
  1168. 0x80, /* hd */
  1169. 0x00, /* hs */
  1170. 0x80, /* un */
  1171. 0x00, /* us */
  1172. 0x80, /* sc */
  1173. 0x00, /* ss */
  1174. 0x80, /* er */
  1175. 0x80, /* ed */
  1176. 0x80, /* sh */
  1177. 0x80, /* ob */
  1178. 0x80, /* rl */
  1179. 0x80, /* du */
  1180. 0x80, /* dps */
  1181. 0x80, /* dsy */
  1182. 0x80, /* dty */
  1183. 0x80, /* dln */
  1184. 0x80, /* dso */
  1185. 0x80, /* dus */
  1186. 0x00, /* dss */
  1187. 0x80, /* dbt */
  1188. 0x00, /* dpp */
  1189. 0x80, /* dfp */
  1190. 0x80, /* den */
  1191. 0x80, /* dds */
  1192. 0x80, /* dar */
  1193. 0x80, /* dpt */
  1194. 0x00, /* dul */
  1195. 0x00, /* dse */
  1196. 0x00, /* dot */
  1197. 0xDE /* CS */
  1198. };
  1199. if (fwrite (b, sizeof (b), 1, file) != 1)
  1200. /* FIXME: Return error status. */
  1201. fatal (_("Failed to write CS struct"));
  1202. }
  1203. /* Write out the SC records for a unit. Create an SC
  1204. for all the sections which appear in the output file, even
  1205. if there isn't an equivalent one on the input. */
  1206. static int
  1207. wr_sc (struct coff_ofile *ptr, struct coff_sfile *sfile)
  1208. {
  1209. int i;
  1210. int scount = 0;
  1211. /* First work out the total number of sections. */
  1212. int total_sec = ptr->nsections;
  1213. struct myinfo
  1214. {
  1215. struct coff_section *sec;
  1216. struct coff_symbol *symbol;
  1217. };
  1218. struct coff_symbol *symbol;
  1219. struct myinfo *info
  1220. = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
  1221. for (i = 0; i < total_sec; i++)
  1222. {
  1223. info[i].sec = ptr->sections + i;
  1224. info[i].symbol = 0;
  1225. }
  1226. for (symbol = sfile->scope->vars_head;
  1227. symbol;
  1228. symbol = symbol->next)
  1229. {
  1230. if (symbol->type->type == coff_secdef_type)
  1231. {
  1232. for (i = 0; i < total_sec; i++)
  1233. {
  1234. if (symbol->where->section == info[i].sec)
  1235. {
  1236. info[i].symbol = symbol;
  1237. break;
  1238. }
  1239. }
  1240. }
  1241. }
  1242. /* Now output all the section info, and fake up some stuff for sections
  1243. we don't have. */
  1244. for (i = 1; i < total_sec; i++)
  1245. {
  1246. struct IT_sc sc;
  1247. char *name;
  1248. symbol = info[i].symbol;
  1249. sc.spare = 0;
  1250. sc.spare1 = 0;
  1251. if (!symbol)
  1252. {
  1253. /* Don't have a symbol set aside for this section, which means
  1254. that nothing in this file does anything for the section. */
  1255. sc.format = !(bfd_get_file_flags (abfd) & EXEC_P);
  1256. sc.addr = 0;
  1257. sc.length = 0;
  1258. name = info[i].sec->name;
  1259. }
  1260. else
  1261. {
  1262. if (bfd_get_file_flags (abfd) & EXEC_P)
  1263. {
  1264. sc.format = 0;
  1265. sc.addr = symbol->where->offset;
  1266. }
  1267. else
  1268. {
  1269. sc.format = 1;
  1270. sc.addr = 0;
  1271. }
  1272. sc.length = symbol->type->size;
  1273. name = symbol->name;
  1274. }
  1275. sc.align = 4;
  1276. sc.concat = CONCAT_SIMPLE;
  1277. sc.read = 3;
  1278. sc.write = 3;
  1279. sc.exec = 3;
  1280. sc.init = 3;
  1281. sc.mode = 3;
  1282. sc.spare = 0;
  1283. sc.segadd = 0;
  1284. sc.spare1 = 0; /* If not zero, then it doesn't work. */
  1285. sc.name = section_translate (name);
  1286. if (strlen (sc.name) == 1)
  1287. {
  1288. switch (sc.name[0])
  1289. {
  1290. case 'D':
  1291. case 'B':
  1292. sc.contents = CONTENTS_DATA;
  1293. break;
  1294. default:
  1295. sc.contents = CONTENTS_CODE;
  1296. }
  1297. }
  1298. else
  1299. {
  1300. sc.contents = CONTENTS_CODE;
  1301. }
  1302. sysroff_swap_sc_out (file, &sc);
  1303. scount++;
  1304. }
  1305. free (info);
  1306. return scount;
  1307. }
  1308. /* Write out the ER records for a unit. */
  1309. static void
  1310. wr_er (struct coff_ofile *ptr, struct coff_sfile *sfile ATTRIBUTE_UNUSED,
  1311. int first)
  1312. {
  1313. int idx = 0;
  1314. struct coff_symbol *sym;
  1315. if (first)
  1316. {
  1317. for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
  1318. {
  1319. if (sym->visible->type == coff_vis_ext_ref)
  1320. {
  1321. struct IT_er er;
  1322. er.spare = 0;
  1323. er.type = ER_NOTSPEC;
  1324. er.name = sym->name;
  1325. sysroff_swap_er_out (file, &er);
  1326. sym->er_number = idx++;
  1327. }
  1328. }
  1329. }
  1330. }
  1331. /* Write out the ED records for a unit. */
  1332. static void
  1333. wr_ed (struct coff_ofile *ptr, struct coff_sfile *sfile ATTRIBUTE_UNUSED,
  1334. int first)
  1335. {
  1336. struct coff_symbol *s;
  1337. if (first)
  1338. {
  1339. for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
  1340. {
  1341. if (s->visible->type == coff_vis_ext_def
  1342. || s->visible->type == coff_vis_common)
  1343. {
  1344. struct IT_ed ed;
  1345. ed.section = s->where->section->number;
  1346. ed.spare = 0;
  1347. if (s->where->section->data)
  1348. {
  1349. ed.type = ED_TYPE_DATA;
  1350. }
  1351. else if (s->where->section->code & SEC_CODE)
  1352. {
  1353. ed.type = ED_TYPE_ENTRY;
  1354. }
  1355. else
  1356. {
  1357. ed.type = ED_TYPE_NOTSPEC;
  1358. ed.type = ED_TYPE_DATA;
  1359. }
  1360. ed.address = s->where->offset - s->where->section->address;
  1361. ed.name = s->name;
  1362. sysroff_swap_ed_out (file, &ed);
  1363. }
  1364. }
  1365. }
  1366. }
  1367. static void
  1368. wr_unit_info (struct coff_ofile *ptr)
  1369. {
  1370. struct coff_sfile *sfile;
  1371. int first = 1;
  1372. for (sfile = ptr->source_head;
  1373. sfile;
  1374. sfile = sfile->next)
  1375. {
  1376. long p1;
  1377. long p2;
  1378. int nsecs;
  1379. p1 = ftell (file);
  1380. wr_un (ptr, sfile, first, 0);
  1381. nsecs = wr_sc (ptr, sfile);
  1382. p2 = ftell (file);
  1383. fseek (file, p1, SEEK_SET);
  1384. wr_un (ptr, sfile, first, nsecs);
  1385. fseek (file, p2, SEEK_SET);
  1386. wr_er (ptr, sfile, first);
  1387. wr_ed (ptr, sfile, first);
  1388. first = 0;
  1389. }
  1390. }
  1391. static void
  1392. wr_module (struct coff_ofile *p)
  1393. {
  1394. wr_cs ();
  1395. wr_hd (p);
  1396. wr_unit_info (p);
  1397. wr_object_body (p);
  1398. wr_debug (p);
  1399. wr_tr ();
  1400. }
  1401. static int
  1402. align (int x)
  1403. {
  1404. return (x + 3) & ~3;
  1405. }
  1406. /* Find all the common variables and turn them into
  1407. ordinary defs - dunno why, but thats what hitachi does with 'em. */
  1408. static void
  1409. prescan (struct coff_ofile *otree)
  1410. {
  1411. struct coff_symbol *s;
  1412. struct coff_section *common_section;
  1413. if (otree->nsections < 3)
  1414. return;
  1415. /* Find the common section - always section 3. */
  1416. common_section = otree->sections + 3;
  1417. for (s = otree->symbol_list_head;
  1418. s;
  1419. s = s->next_in_ofile_list)
  1420. {
  1421. if (s->visible->type == coff_vis_common)
  1422. {
  1423. struct coff_where *w = s->where;
  1424. /* s->visible->type = coff_vis_ext_def; leave it as common */
  1425. common_section->size = align (common_section->size);
  1426. w->offset = common_section->size + common_section->address;
  1427. w->section = common_section;
  1428. common_section->size += s->type->size;
  1429. common_section->size = align (common_section->size);
  1430. }
  1431. }
  1432. }
  1433. ATTRIBUTE_NORETURN static void
  1434. show_usage (FILE *ffile, int status)
  1435. {
  1436. fprintf (ffile, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
  1437. fprintf (ffile, _("Convert a COFF object file into a SYSROFF object file\n"));
  1438. fprintf (ffile, _(" The options are:\n\
  1439. -q --quick (Obsolete - ignored)\n\
  1440. -n --noprescan Do not perform a scan to convert commons into defs\n\
  1441. -d --debug Display information about what is being done\n\
  1442. @<file> Read options from <file>\n\
  1443. -h --help Display this information\n\
  1444. -v --version Print the program's version number\n"));
  1445. if (REPORT_BUGS_TO[0] && status == 0)
  1446. fprintf (ffile, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  1447. exit (status);
  1448. }
  1449. int
  1450. main (int ac, char **av)
  1451. {
  1452. int opt;
  1453. static struct option long_options[] =
  1454. {
  1455. {"debug", no_argument, 0, 'd'},
  1456. {"quick", no_argument, 0, 'q'},
  1457. {"noprescan", no_argument, 0, 'n'},
  1458. {"help", no_argument, 0, 'h'},
  1459. {"version", no_argument, 0, 'V'},
  1460. {NULL, no_argument, 0, 0}
  1461. };
  1462. char **matching;
  1463. char *input_file;
  1464. char *output_file;
  1465. #ifdef HAVE_LC_MESSAGES
  1466. setlocale (LC_MESSAGES, "");
  1467. #endif
  1468. setlocale (LC_CTYPE, "");
  1469. bindtextdomain (PACKAGE, LOCALEDIR);
  1470. textdomain (PACKAGE);
  1471. program_name = av[0];
  1472. xmalloc_set_program_name (program_name);
  1473. bfd_set_error_program_name (program_name);
  1474. expandargv (&ac, &av);
  1475. while ((opt = getopt_long (ac, av, "dHhVvqn", long_options,
  1476. (int *) NULL))
  1477. != EOF)
  1478. {
  1479. switch (opt)
  1480. {
  1481. case 'q':
  1482. quick = 1;
  1483. break;
  1484. case 'n':
  1485. noprescan = 1;
  1486. break;
  1487. case 'd':
  1488. debug = 1;
  1489. break;
  1490. case 'H':
  1491. case 'h':
  1492. show_usage (stdout, 0);
  1493. /*NOTREACHED */
  1494. case 'v':
  1495. case 'V':
  1496. print_version ("srconv");
  1497. exit (0);
  1498. /*NOTREACHED */
  1499. case 0:
  1500. break;
  1501. default:
  1502. show_usage (stderr, 1);
  1503. /*NOTREACHED */
  1504. }
  1505. }
  1506. /* The input and output files may be named on the command line. */
  1507. output_file = NULL;
  1508. if (optind < ac)
  1509. {
  1510. input_file = av[optind];
  1511. ++optind;
  1512. if (optind < ac)
  1513. {
  1514. output_file = av[optind];
  1515. ++optind;
  1516. if (optind < ac)
  1517. show_usage (stderr, 1);
  1518. if (filename_cmp (input_file, output_file) == 0)
  1519. {
  1520. fatal (_("input and output files must be different"));
  1521. }
  1522. }
  1523. }
  1524. else
  1525. input_file = 0;
  1526. if (!input_file)
  1527. {
  1528. fatal (_("no input file specified"));
  1529. }
  1530. if (!output_file)
  1531. {
  1532. /* Take a .o off the input file and stick on a .obj. If
  1533. it doesn't end in .o, then stick a .obj on anyway */
  1534. int len = strlen (input_file);
  1535. output_file = xmalloc (len + 5);
  1536. strcpy (output_file, input_file);
  1537. if (len > 3
  1538. && output_file[len - 2] == '.'
  1539. && output_file[len - 1] == 'o')
  1540. {
  1541. output_file[len] = 'b';
  1542. output_file[len + 1] = 'j';
  1543. output_file[len + 2] = 0;
  1544. }
  1545. else
  1546. {
  1547. strcat (output_file, ".obj");
  1548. }
  1549. }
  1550. abfd = bfd_openr (input_file, 0);
  1551. if (!abfd)
  1552. bfd_fatal (input_file);
  1553. if (!bfd_check_format_matches (abfd, bfd_object, &matching))
  1554. {
  1555. bfd_nonfatal (input_file);
  1556. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  1557. {
  1558. list_matching_formats (matching);
  1559. free (matching);
  1560. }
  1561. exit (1);
  1562. }
  1563. file = fopen (output_file, FOPEN_WB);
  1564. if (!file)
  1565. fatal (_("unable to open output file %s"), output_file);
  1566. if (debug)
  1567. printf ("ids %d %d\n", base1, base2);
  1568. tree = coff_grok (abfd);
  1569. if (tree)
  1570. {
  1571. if (!noprescan)
  1572. prescan (tree);
  1573. wr_module (tree);
  1574. }
  1575. return 0;
  1576. }