srconv.c 41 KB

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