tcccoff.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*
  2. * COFF file handling for TCC
  3. *
  4. * Copyright (c) 2003, 2004 TK
  5. * Copyright (c) 2004 Fabrice Bellard
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include "tcc.h"
  22. #define MAXNSCNS 255 /* MAXIMUM NUMBER OF SECTIONS */
  23. #define MAX_STR_TABLE 1000000
  24. AOUTHDR o_filehdr; /* OPTIONAL (A.OUT) FILE HEADER */
  25. SCNHDR section_header[MAXNSCNS];
  26. #define MAX_FUNCS 1000
  27. #define MAX_FUNC_NAME_LENGTH 128
  28. int nFuncs;
  29. char Func[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
  30. char AssociatedFile[MAX_FUNCS][MAX_FUNC_NAME_LENGTH];
  31. int LineNoFilePtr[MAX_FUNCS];
  32. int EndAddress[MAX_FUNCS];
  33. int LastLineNo[MAX_FUNCS];
  34. int FuncEntries[MAX_FUNCS];
  35. int OutputTheSection(Section * sect);
  36. short int GetCoffFlags(const char *s);
  37. void SortSymbolTable(void);
  38. Section *FindSection(TCCState * s1, const char *sname);
  39. int C67_main_entry_point;
  40. int FindCoffSymbolIndex(const char *func_name);
  41. int nb_syms;
  42. typedef struct {
  43. long tag;
  44. long size;
  45. long fileptr;
  46. long nextsym;
  47. short int dummy;
  48. } AUXFUNC;
  49. typedef struct {
  50. long regmask;
  51. unsigned short lineno;
  52. unsigned short nentries;
  53. int localframe;
  54. int nextentry;
  55. short int dummy;
  56. } AUXBF;
  57. typedef struct {
  58. long dummy;
  59. unsigned short lineno;
  60. unsigned short dummy1;
  61. int dummy2;
  62. int dummy3;
  63. unsigned short dummy4;
  64. } AUXEF;
  65. ST_FUNC int tcc_output_coff(TCCState *s1, FILE *f)
  66. {
  67. Section *tcc_sect;
  68. SCNHDR *coff_sec;
  69. int file_pointer;
  70. char *Coff_str_table, *pCoff_str_table;
  71. int CoffTextSectionNo, coff_nb_syms;
  72. FILHDR file_hdr; /* FILE HEADER STRUCTURE */
  73. Section *stext, *sdata, *sbss;
  74. int i, NSectionsToOutput = 0;
  75. Coff_str_table = pCoff_str_table = NULL;
  76. stext = FindSection(s1, ".text");
  77. sdata = FindSection(s1, ".data");
  78. sbss = FindSection(s1, ".bss");
  79. nb_syms = symtab_section->data_offset / sizeof(Elf32_Sym);
  80. coff_nb_syms = FindCoffSymbolIndex("XXXXXXXXXX1");
  81. file_hdr.f_magic = COFF_C67_MAGIC; /* magic number */
  82. file_hdr.f_timdat = 0; /* time & date stamp */
  83. file_hdr.f_opthdr = sizeof(AOUTHDR); /* sizeof(optional hdr) */
  84. file_hdr.f_flags = 0x1143; /* flags (copied from what code composer does) */
  85. file_hdr.f_TargetID = 0x99; /* for C6x = 0x0099 */
  86. o_filehdr.magic = 0x0108; /* see magic.h */
  87. o_filehdr.vstamp = 0x0190; /* version stamp */
  88. o_filehdr.tsize = stext->data_offset; /* text size in bytes, padded to FW bdry */
  89. o_filehdr.dsize = sdata->data_offset; /* initialized data " " */
  90. o_filehdr.bsize = sbss->data_offset; /* uninitialized data " " */
  91. o_filehdr.entrypt = C67_main_entry_point; /* entry pt. */
  92. o_filehdr.text_start = stext->sh_addr; /* base of text used for this file */
  93. o_filehdr.data_start = sdata->sh_addr; /* base of data used for this file */
  94. // create all the section headers
  95. file_pointer = FILHSZ + sizeof(AOUTHDR);
  96. CoffTextSectionNo = -1;
  97. for (i = 1; i < s1->nb_sections; i++) {
  98. coff_sec = &section_header[i];
  99. tcc_sect = s1->sections[i];
  100. if (OutputTheSection(tcc_sect)) {
  101. NSectionsToOutput++;
  102. if (CoffTextSectionNo == -1 && tcc_sect == stext)
  103. CoffTextSectionNo = NSectionsToOutput; // rem which coff sect number the .text sect is
  104. strcpy(coff_sec->s_name, tcc_sect->name); /* section name */
  105. coff_sec->s_paddr = tcc_sect->sh_addr; /* physical address */
  106. coff_sec->s_vaddr = tcc_sect->sh_addr; /* virtual address */
  107. coff_sec->s_size = tcc_sect->data_offset; /* section size */
  108. coff_sec->s_scnptr = 0; /* file ptr to raw data for section */
  109. coff_sec->s_relptr = 0; /* file ptr to relocation */
  110. coff_sec->s_lnnoptr = 0; /* file ptr to line numbers */
  111. coff_sec->s_nreloc = 0; /* number of relocation entries */
  112. coff_sec->s_flags = GetCoffFlags(coff_sec->s_name); /* flags */
  113. coff_sec->s_reserved = 0; /* reserved byte */
  114. coff_sec->s_page = 0; /* memory page id */
  115. file_pointer += sizeof(SCNHDR);
  116. }
  117. }
  118. file_hdr.f_nscns = NSectionsToOutput; /* number of sections */
  119. // now loop through and determine file pointer locations
  120. // for the raw data
  121. for (i = 1; i < s1->nb_sections; i++) {
  122. coff_sec = &section_header[i];
  123. tcc_sect = s1->sections[i];
  124. if (OutputTheSection(tcc_sect)) {
  125. // put raw data
  126. coff_sec->s_scnptr = file_pointer; /* file ptr to raw data for section */
  127. file_pointer += coff_sec->s_size;
  128. }
  129. }
  130. // now loop through and determine file pointer locations
  131. // for the relocation data
  132. for (i = 1; i < s1->nb_sections; i++) {
  133. coff_sec = &section_header[i];
  134. tcc_sect = s1->sections[i];
  135. if (OutputTheSection(tcc_sect)) {
  136. // put relocations data
  137. if (coff_sec->s_nreloc > 0) {
  138. coff_sec->s_relptr = file_pointer; /* file ptr to relocation */
  139. file_pointer += coff_sec->s_nreloc * sizeof(struct reloc);
  140. }
  141. }
  142. }
  143. // now loop through and determine file pointer locations
  144. // for the line number data
  145. for (i = 1; i < s1->nb_sections; i++) {
  146. coff_sec = &section_header[i];
  147. tcc_sect = s1->sections[i];
  148. coff_sec->s_nlnno = 0;
  149. coff_sec->s_lnnoptr = 0;
  150. if (s1->do_debug && tcc_sect == stext) {
  151. // count how many line nos data
  152. // also find association between source file name and function
  153. // so we can sort the symbol table
  154. Stab_Sym *sym, *sym_end;
  155. char func_name[MAX_FUNC_NAME_LENGTH],
  156. last_func_name[MAX_FUNC_NAME_LENGTH];
  157. unsigned long func_addr, last_pc, pc;
  158. const char *incl_files[INCLUDE_STACK_SIZE];
  159. int incl_index, len, last_line_num;
  160. const char *str, *p;
  161. coff_sec->s_lnnoptr = file_pointer; /* file ptr to linno */
  162. func_name[0] = '\0';
  163. func_addr = 0;
  164. incl_index = 0;
  165. last_func_name[0] = '\0';
  166. last_pc = 0xffffffff;
  167. last_line_num = 1;
  168. sym = (Stab_Sym *) stab_section->data + 1;
  169. sym_end =
  170. (Stab_Sym *) (stab_section->data +
  171. stab_section->data_offset);
  172. nFuncs = 0;
  173. while (sym < sym_end) {
  174. switch (sym->n_type) {
  175. /* function start or end */
  176. case N_FUN:
  177. if (sym->n_strx == 0) {
  178. // end of function
  179. coff_sec->s_nlnno++;
  180. file_pointer += LINESZ;
  181. pc = sym->n_value + func_addr;
  182. func_name[0] = '\0';
  183. func_addr = 0;
  184. EndAddress[nFuncs] = pc;
  185. FuncEntries[nFuncs] =
  186. (file_pointer -
  187. LineNoFilePtr[nFuncs]) / LINESZ - 1;
  188. LastLineNo[nFuncs++] = last_line_num + 1;
  189. } else {
  190. // beginning of function
  191. LineNoFilePtr[nFuncs] = file_pointer;
  192. coff_sec->s_nlnno++;
  193. file_pointer += LINESZ;
  194. str =
  195. (const char *) stabstr_section->data +
  196. sym->n_strx;
  197. p = strchr(str, ':');
  198. if (!p) {
  199. pstrcpy(func_name, sizeof(func_name), str);
  200. pstrcpy(Func[nFuncs], sizeof(func_name), str);
  201. } else {
  202. len = p - str;
  203. if (len > sizeof(func_name) - 1)
  204. len = sizeof(func_name) - 1;
  205. memcpy(func_name, str, len);
  206. memcpy(Func[nFuncs], str, len);
  207. func_name[len] = '\0';
  208. }
  209. // save the file that it came in so we can sort later
  210. pstrcpy(AssociatedFile[nFuncs], sizeof(func_name),
  211. incl_files[incl_index - 1]);
  212. func_addr = sym->n_value;
  213. }
  214. break;
  215. /* line number info */
  216. case N_SLINE:
  217. pc = sym->n_value + func_addr;
  218. last_pc = pc;
  219. last_line_num = sym->n_desc;
  220. /* XXX: slow! */
  221. strcpy(last_func_name, func_name);
  222. coff_sec->s_nlnno++;
  223. file_pointer += LINESZ;
  224. break;
  225. /* include files */
  226. case N_BINCL:
  227. str =
  228. (const char *) stabstr_section->data + sym->n_strx;
  229. add_incl:
  230. if (incl_index < INCLUDE_STACK_SIZE) {
  231. incl_files[incl_index++] = str;
  232. }
  233. break;
  234. case N_EINCL:
  235. if (incl_index > 1)
  236. incl_index--;
  237. break;
  238. case N_SO:
  239. if (sym->n_strx == 0) {
  240. incl_index = 0; /* end of translation unit */
  241. } else {
  242. str =
  243. (const char *) stabstr_section->data +
  244. sym->n_strx;
  245. /* do not add path */
  246. len = strlen(str);
  247. if (len > 0 && str[len - 1] != '/')
  248. goto add_incl;
  249. }
  250. break;
  251. }
  252. sym++;
  253. }
  254. }
  255. }
  256. file_hdr.f_symptr = file_pointer; /* file pointer to symtab */
  257. if (s1->do_debug)
  258. file_hdr.f_nsyms = coff_nb_syms; /* number of symtab entries */
  259. else
  260. file_hdr.f_nsyms = 0;
  261. file_pointer += file_hdr.f_nsyms * SYMNMLEN;
  262. // OK now we are all set to write the file
  263. fwrite(&file_hdr, FILHSZ, 1, f);
  264. fwrite(&o_filehdr, sizeof(o_filehdr), 1, f);
  265. // write section headers
  266. for (i = 1; i < s1->nb_sections; i++) {
  267. coff_sec = &section_header[i];
  268. tcc_sect = s1->sections[i];
  269. if (OutputTheSection(tcc_sect)) {
  270. fwrite(coff_sec, sizeof(SCNHDR), 1, f);
  271. }
  272. }
  273. // write raw data
  274. for (i = 1; i < s1->nb_sections; i++) {
  275. coff_sec = &section_header[i];
  276. tcc_sect = s1->sections[i];
  277. if (OutputTheSection(tcc_sect)) {
  278. fwrite(tcc_sect->data, tcc_sect->data_offset, 1, f);
  279. }
  280. }
  281. // write relocation data
  282. for (i = 1; i < s1->nb_sections; i++) {
  283. coff_sec = &section_header[i];
  284. tcc_sect = s1->sections[i];
  285. if (OutputTheSection(tcc_sect)) {
  286. // put relocations data
  287. if (coff_sec->s_nreloc > 0) {
  288. fwrite(tcc_sect->reloc,
  289. coff_sec->s_nreloc * sizeof(struct reloc), 1, f);
  290. }
  291. }
  292. }
  293. // group the symbols in order of filename, func1, func2, etc
  294. // finally global symbols
  295. if (s1->do_debug)
  296. SortSymbolTable();
  297. // write line no data
  298. for (i = 1; i < s1->nb_sections; i++) {
  299. coff_sec = &section_header[i];
  300. tcc_sect = s1->sections[i];
  301. if (s1->do_debug && tcc_sect == stext) {
  302. // count how many line nos data
  303. Stab_Sym *sym, *sym_end;
  304. char func_name[128], last_func_name[128];
  305. unsigned long func_addr, last_pc, pc;
  306. const char *incl_files[INCLUDE_STACK_SIZE];
  307. int incl_index, len, last_line_num;
  308. const char *str, *p;
  309. LINENO CoffLineNo;
  310. func_name[0] = '\0';
  311. func_addr = 0;
  312. incl_index = 0;
  313. last_func_name[0] = '\0';
  314. last_pc = 0;
  315. last_line_num = 1;
  316. sym = (Stab_Sym *) stab_section->data + 1;
  317. sym_end =
  318. (Stab_Sym *) (stab_section->data +
  319. stab_section->data_offset);
  320. while (sym < sym_end) {
  321. switch (sym->n_type) {
  322. /* function start or end */
  323. case N_FUN:
  324. if (sym->n_strx == 0) {
  325. // end of function
  326. CoffLineNo.l_addr.l_paddr = last_pc;
  327. CoffLineNo.l_lnno = last_line_num + 1;
  328. fwrite(&CoffLineNo, 6, 1, f);
  329. pc = sym->n_value + func_addr;
  330. func_name[0] = '\0';
  331. func_addr = 0;
  332. } else {
  333. // beginning of function
  334. str =
  335. (const char *) stabstr_section->data +
  336. sym->n_strx;
  337. p = strchr(str, ':');
  338. if (!p) {
  339. pstrcpy(func_name, sizeof(func_name), str);
  340. } else {
  341. len = p - str;
  342. if (len > sizeof(func_name) - 1)
  343. len = sizeof(func_name) - 1;
  344. memcpy(func_name, str, len);
  345. func_name[len] = '\0';
  346. }
  347. func_addr = sym->n_value;
  348. last_pc = func_addr;
  349. last_line_num = -1;
  350. // output a function begin
  351. CoffLineNo.l_addr.l_symndx =
  352. FindCoffSymbolIndex(func_name);
  353. CoffLineNo.l_lnno = 0;
  354. fwrite(&CoffLineNo, 6, 1, f);
  355. }
  356. break;
  357. /* line number info */
  358. case N_SLINE:
  359. pc = sym->n_value + func_addr;
  360. /* XXX: slow! */
  361. strcpy(last_func_name, func_name);
  362. // output a line reference
  363. CoffLineNo.l_addr.l_paddr = last_pc;
  364. if (last_line_num == -1) {
  365. CoffLineNo.l_lnno = sym->n_desc;
  366. } else {
  367. CoffLineNo.l_lnno = last_line_num + 1;
  368. }
  369. fwrite(&CoffLineNo, 6, 1, f);
  370. last_pc = pc;
  371. last_line_num = sym->n_desc;
  372. break;
  373. /* include files */
  374. case N_BINCL:
  375. str =
  376. (const char *) stabstr_section->data + sym->n_strx;
  377. add_incl2:
  378. if (incl_index < INCLUDE_STACK_SIZE) {
  379. incl_files[incl_index++] = str;
  380. }
  381. break;
  382. case N_EINCL:
  383. if (incl_index > 1)
  384. incl_index--;
  385. break;
  386. case N_SO:
  387. if (sym->n_strx == 0) {
  388. incl_index = 0; /* end of translation unit */
  389. } else {
  390. str =
  391. (const char *) stabstr_section->data +
  392. sym->n_strx;
  393. /* do not add path */
  394. len = strlen(str);
  395. if (len > 0 && str[len - 1] != '/')
  396. goto add_incl2;
  397. }
  398. break;
  399. }
  400. sym++;
  401. }
  402. }
  403. }
  404. // write symbol table
  405. if (s1->do_debug) {
  406. int k;
  407. struct syment csym;
  408. AUXFUNC auxfunc;
  409. AUXBF auxbf;
  410. AUXEF auxef;
  411. int i;
  412. Elf32_Sym *p;
  413. const char *name;
  414. int nstr;
  415. int n = 0;
  416. Coff_str_table = (char *) tcc_malloc(MAX_STR_TABLE);
  417. pCoff_str_table = Coff_str_table;
  418. nstr = 0;
  419. p = (Elf32_Sym *) symtab_section->data;
  420. for (i = 0; i < nb_syms; i++) {
  421. name = symtab_section->link->data + p->st_name;
  422. for (k = 0; k < 8; k++)
  423. csym._n._n_name[k] = 0;
  424. if (strlen(name) <= 8) {
  425. strcpy(csym._n._n_name, name);
  426. } else {
  427. if (pCoff_str_table - Coff_str_table + strlen(name) >
  428. MAX_STR_TABLE - 1)
  429. tcc_error("String table too large");
  430. csym._n._n_n._n_zeroes = 0;
  431. csym._n._n_n._n_offset =
  432. pCoff_str_table - Coff_str_table + 4;
  433. strcpy(pCoff_str_table, name);
  434. pCoff_str_table += strlen(name) + 1; // skip over null
  435. nstr++;
  436. }
  437. if (p->st_info == 4) {
  438. // put a filename symbol
  439. csym.n_value = 33; // ?????
  440. csym.n_scnum = N_DEBUG;
  441. csym.n_type = 0;
  442. csym.n_sclass = C_FILE;
  443. csym.n_numaux = 0;
  444. fwrite(&csym, 18, 1, f);
  445. n++;
  446. } else if (p->st_info == 0x12) {
  447. // find the function data
  448. for (k = 0; k < nFuncs; k++) {
  449. if (strcmp(name, Func[k]) == 0)
  450. break;
  451. }
  452. if (k >= nFuncs) {
  453. tcc_error("debug info can't find function: %s", name);
  454. }
  455. // put a Function Name
  456. csym.n_value = p->st_value; // physical address
  457. csym.n_scnum = CoffTextSectionNo;
  458. csym.n_type = MKTYPE(T_INT, DT_FCN, 0, 0, 0, 0, 0);
  459. csym.n_sclass = C_EXT;
  460. csym.n_numaux = 1;
  461. fwrite(&csym, 18, 1, f);
  462. // now put aux info
  463. auxfunc.tag = 0;
  464. auxfunc.size = EndAddress[k] - p->st_value;
  465. auxfunc.fileptr = LineNoFilePtr[k];
  466. auxfunc.nextsym = n + 6; // tktk
  467. auxfunc.dummy = 0;
  468. fwrite(&auxfunc, 18, 1, f);
  469. // put a .bf
  470. strcpy(csym._n._n_name, ".bf");
  471. csym.n_value = p->st_value; // physical address
  472. csym.n_scnum = CoffTextSectionNo;
  473. csym.n_type = 0;
  474. csym.n_sclass = C_FCN;
  475. csym.n_numaux = 1;
  476. fwrite(&csym, 18, 1, f);
  477. // now put aux info
  478. auxbf.regmask = 0;
  479. auxbf.lineno = 0;
  480. auxbf.nentries = FuncEntries[k];
  481. auxbf.localframe = 0;
  482. auxbf.nextentry = n + 6;
  483. auxbf.dummy = 0;
  484. fwrite(&auxbf, 18, 1, f);
  485. // put a .ef
  486. strcpy(csym._n._n_name, ".ef");
  487. csym.n_value = EndAddress[k]; // physical address
  488. csym.n_scnum = CoffTextSectionNo;
  489. csym.n_type = 0;
  490. csym.n_sclass = C_FCN;
  491. csym.n_numaux = 1;
  492. fwrite(&csym, 18, 1, f);
  493. // now put aux info
  494. auxef.dummy = 0;
  495. auxef.lineno = LastLineNo[k];
  496. auxef.dummy1 = 0;
  497. auxef.dummy2 = 0;
  498. auxef.dummy3 = 0;
  499. auxef.dummy4 = 0;
  500. fwrite(&auxef, 18, 1, f);
  501. n += 6;
  502. } else {
  503. // try an put some type info
  504. if ((p->st_other & VT_BTYPE) == VT_DOUBLE) {
  505. csym.n_type = T_DOUBLE; // int
  506. csym.n_sclass = C_EXT;
  507. } else if ((p->st_other & VT_BTYPE) == VT_FLOAT) {
  508. csym.n_type = T_FLOAT;
  509. csym.n_sclass = C_EXT;
  510. } else if ((p->st_other & VT_BTYPE) == VT_INT) {
  511. csym.n_type = T_INT; // int
  512. csym.n_sclass = C_EXT;
  513. } else if ((p->st_other & VT_BTYPE) == VT_SHORT) {
  514. csym.n_type = T_SHORT;
  515. csym.n_sclass = C_EXT;
  516. } else if ((p->st_other & VT_BTYPE) == VT_BYTE) {
  517. csym.n_type = T_CHAR;
  518. csym.n_sclass = C_EXT;
  519. } else {
  520. csym.n_type = T_INT; // just mark as a label
  521. csym.n_sclass = C_LABEL;
  522. }
  523. csym.n_value = p->st_value;
  524. csym.n_scnum = 2;
  525. csym.n_numaux = 1;
  526. fwrite(&csym, 18, 1, f);
  527. auxfunc.tag = 0;
  528. auxfunc.size = 0x20;
  529. auxfunc.fileptr = 0;
  530. auxfunc.nextsym = 0;
  531. auxfunc.dummy = 0;
  532. fwrite(&auxfunc, 18, 1, f);
  533. n++;
  534. n++;
  535. }
  536. p++;
  537. }
  538. }
  539. if (s1->do_debug) {
  540. // write string table
  541. // first write the size
  542. i = pCoff_str_table - Coff_str_table;
  543. fwrite(&i, 4, 1, f);
  544. // then write the strings
  545. fwrite(Coff_str_table, i, 1, f);
  546. tcc_free(Coff_str_table);
  547. }
  548. return 0;
  549. }
  550. // group the symbols in order of filename, func1, func2, etc
  551. // finally global symbols
  552. void SortSymbolTable(void)
  553. {
  554. int i, j, k, n = 0;
  555. Elf32_Sym *p, *p2, *NewTable;
  556. char *name, *name2;
  557. NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym));
  558. p = (Elf32_Sym *) symtab_section->data;
  559. // find a file symbol, copy it over
  560. // then scan the whole symbol list and copy any function
  561. // symbols that match the file association
  562. for (i = 0; i < nb_syms; i++) {
  563. if (p->st_info == 4) {
  564. name = (char *) symtab_section->link->data + p->st_name;
  565. // this is a file symbol, copy it over
  566. NewTable[n++] = *p;
  567. p2 = (Elf32_Sym *) symtab_section->data;
  568. for (j = 0; j < nb_syms; j++) {
  569. if (p2->st_info == 0x12) {
  570. // this is a func symbol
  571. name2 =
  572. (char *) symtab_section->link->data + p2->st_name;
  573. // find the function data index
  574. for (k = 0; k < nFuncs; k++) {
  575. if (strcmp(name2, Func[k]) == 0)
  576. break;
  577. }
  578. if (k >= nFuncs) {
  579. tcc_error("debug (sort) info can't find function: %s", name2);
  580. }
  581. if (strcmp(AssociatedFile[k], name) == 0) {
  582. // yes they match copy it over
  583. NewTable[n++] = *p2;
  584. }
  585. }
  586. p2++;
  587. }
  588. }
  589. p++;
  590. }
  591. // now all the filename and func symbols should have been copied over
  592. // copy all the rest over (all except file and funcs)
  593. p = (Elf32_Sym *) symtab_section->data;
  594. for (i = 0; i < nb_syms; i++) {
  595. if (p->st_info != 4 && p->st_info != 0x12) {
  596. NewTable[n++] = *p;
  597. }
  598. p++;
  599. }
  600. if (n != nb_syms)
  601. tcc_error("Internal Compiler error, debug info");
  602. // copy it all back
  603. p = (Elf32_Sym *) symtab_section->data;
  604. for (i = 0; i < nb_syms; i++) {
  605. *p++ = NewTable[i];
  606. }
  607. tcc_free(NewTable);
  608. }
  609. int FindCoffSymbolIndex(const char *func_name)
  610. {
  611. int i, n = 0;
  612. Elf32_Sym *p;
  613. char *name;
  614. p = (Elf32_Sym *) symtab_section->data;
  615. for (i = 0; i < nb_syms; i++) {
  616. name = (char *) symtab_section->link->data + p->st_name;
  617. if (p->st_info == 4) {
  618. // put a filename symbol
  619. n++;
  620. } else if (p->st_info == 0x12) {
  621. if (strcmp(func_name, name) == 0)
  622. return n;
  623. n += 6;
  624. // put a Function Name
  625. // now put aux info
  626. // put a .bf
  627. // now put aux info
  628. // put a .ef
  629. // now put aux info
  630. } else {
  631. n += 2;
  632. }
  633. p++;
  634. }
  635. return n; // total number of symbols
  636. }
  637. int OutputTheSection(Section * sect)
  638. {
  639. const char *s = sect->name;
  640. if (!strcmp(s, ".text"))
  641. return 1;
  642. else if (!strcmp(s, ".data"))
  643. return 1;
  644. else
  645. return 0;
  646. }
  647. short int GetCoffFlags(const char *s)
  648. {
  649. if (!strcmp(s, ".text"))
  650. return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400;
  651. else if (!strcmp(s, ".data"))
  652. return STYP_DATA;
  653. else if (!strcmp(s, ".bss"))
  654. return STYP_BSS;
  655. else if (!strcmp(s, ".stack"))
  656. return STYP_BSS | STYP_ALIGN | 0x200;
  657. else if (!strcmp(s, ".cinit"))
  658. return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200;
  659. else
  660. return 0;
  661. }
  662. Section *FindSection(TCCState * s1, const char *sname)
  663. {
  664. Section *s;
  665. int i;
  666. for (i = 1; i < s1->nb_sections; i++) {
  667. s = s1->sections[i];
  668. if (!strcmp(sname, s->name))
  669. return s;
  670. }
  671. tcc_error("could not find section %s", sname);
  672. return 0;
  673. }
  674. ST_FUNC int tcc_load_coff(TCCState * s1, int fd)
  675. {
  676. // tktk TokenSym *ts;
  677. FILE *f;
  678. unsigned int str_size;
  679. char *Coff_str_table, *name;
  680. int i, k;
  681. struct syment csym;
  682. char name2[9];
  683. FILHDR file_hdr; /* FILE HEADER STRUCTURE */
  684. f = fdopen(fd, "rb");
  685. if (!f) {
  686. tcc_error("Unable to open .out file for input");
  687. }
  688. if (fread(&file_hdr, FILHSZ, 1, f) != 1)
  689. tcc_error("error reading .out file for input");
  690. if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
  691. tcc_error("error reading .out file for input");
  692. // first read the string table
  693. if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
  694. tcc_error("error reading .out file for input");
  695. if (fread(&str_size, sizeof(int), 1, f) != 1)
  696. tcc_error("error reading .out file for input");
  697. Coff_str_table = (char *) tcc_malloc(str_size);
  698. if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
  699. tcc_error("error reading .out file for input");
  700. // read/process all the symbols
  701. // seek back to symbols
  702. if (fseek(f, file_hdr.f_symptr, SEEK_SET))
  703. tcc_error("error reading .out file for input");
  704. for (i = 0; i < file_hdr.f_nsyms; i++) {
  705. if (fread(&csym, SYMESZ, 1, f) != 1)
  706. tcc_error("error reading .out file for input");
  707. if (csym._n._n_n._n_zeroes == 0) {
  708. name = Coff_str_table + csym._n._n_n._n_offset - 4;
  709. } else {
  710. name = csym._n._n_name;
  711. if (name[7] != 0) {
  712. for (k = 0; k < 8; k++)
  713. name2[k] = name[k];
  714. name2[8] = 0;
  715. name = name2;
  716. }
  717. }
  718. // if (strcmp("_DAC_Buffer",name)==0) // tktk
  719. // name[0]=0;
  720. if (((csym.n_type & 0x30) == 0x20 && csym.n_sclass == 0x2) || ((csym.n_type & 0x30) == 0x30 && csym.n_sclass == 0x2) || (csym.n_type == 0x4 && csym.n_sclass == 0x2) || (csym.n_type == 0x8 && csym.n_sclass == 0x2) || // structures
  721. (csym.n_type == 0x18 && csym.n_sclass == 0x2) || // pointer to structure
  722. (csym.n_type == 0x7 && csym.n_sclass == 0x2) || // doubles
  723. (csym.n_type == 0x6 && csym.n_sclass == 0x2)) // floats
  724. {
  725. // strip off any leading underscore (except for other main routine)
  726. if (name[0] == '_' && strcmp(name, "_main") != 0)
  727. name++;
  728. tcc_add_symbol(s1, name, (void*)(uintptr_t)csym.n_value);
  729. }
  730. // skip any aux records
  731. if (csym.n_numaux == 1) {
  732. if (fread(&csym, SYMESZ, 1, f) != 1)
  733. tcc_error("error reading .out file for input");
  734. i++;
  735. }
  736. }
  737. return 0;
  738. }