tcccoff.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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 "coff.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. BOOL 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. 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. 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. char s[256];
  454. sprintf(s, "debug info can't find function: %s", name);
  455. error(s);
  456. }
  457. // put a Function Name
  458. csym.n_value = p->st_value; // physical address
  459. csym.n_scnum = CoffTextSectionNo;
  460. csym.n_type = MKTYPE(T_INT, DT_FCN, 0, 0, 0, 0, 0);
  461. csym.n_sclass = C_EXT;
  462. csym.n_numaux = 1;
  463. fwrite(&csym, 18, 1, f);
  464. // now put aux info
  465. auxfunc.tag = 0;
  466. auxfunc.size = EndAddress[k] - p->st_value;
  467. auxfunc.fileptr = LineNoFilePtr[k];
  468. auxfunc.nextsym = n + 6; // tktk
  469. auxfunc.dummy = 0;
  470. fwrite(&auxfunc, 18, 1, f);
  471. // put a .bf
  472. strcpy(csym._n._n_name, ".bf");
  473. csym.n_value = p->st_value; // physical address
  474. csym.n_scnum = CoffTextSectionNo;
  475. csym.n_type = 0;
  476. csym.n_sclass = C_FCN;
  477. csym.n_numaux = 1;
  478. fwrite(&csym, 18, 1, f);
  479. // now put aux info
  480. auxbf.regmask = 0;
  481. auxbf.lineno = 0;
  482. auxbf.nentries = FuncEntries[k];
  483. auxbf.localframe = 0;
  484. auxbf.nextentry = n + 6;
  485. auxbf.dummy = 0;
  486. fwrite(&auxbf, 18, 1, f);
  487. // put a .ef
  488. strcpy(csym._n._n_name, ".ef");
  489. csym.n_value = EndAddress[k]; // physical address
  490. csym.n_scnum = CoffTextSectionNo;
  491. csym.n_type = 0;
  492. csym.n_sclass = C_FCN;
  493. csym.n_numaux = 1;
  494. fwrite(&csym, 18, 1, f);
  495. // now put aux info
  496. auxef.dummy = 0;
  497. auxef.lineno = LastLineNo[k];
  498. auxef.dummy1 = 0;
  499. auxef.dummy2 = 0;
  500. auxef.dummy3 = 0;
  501. auxef.dummy4 = 0;
  502. fwrite(&auxef, 18, 1, f);
  503. n += 6;
  504. } else {
  505. // try an put some type info
  506. if ((p->st_other & VT_BTYPE) == VT_DOUBLE) {
  507. csym.n_type = T_DOUBLE; // int
  508. csym.n_sclass = C_EXT;
  509. } else if ((p->st_other & VT_BTYPE) == VT_FLOAT) {
  510. csym.n_type = T_FLOAT;
  511. csym.n_sclass = C_EXT;
  512. } else if ((p->st_other & VT_BTYPE) == VT_INT) {
  513. csym.n_type = T_INT; // int
  514. csym.n_sclass = C_EXT;
  515. } else if ((p->st_other & VT_BTYPE) == VT_SHORT) {
  516. csym.n_type = T_SHORT;
  517. csym.n_sclass = C_EXT;
  518. } else if ((p->st_other & VT_BTYPE) == VT_BYTE) {
  519. csym.n_type = T_CHAR;
  520. csym.n_sclass = C_EXT;
  521. } else {
  522. csym.n_type = T_INT; // just mark as a label
  523. csym.n_sclass = C_LABEL;
  524. }
  525. csym.n_value = p->st_value;
  526. csym.n_scnum = 2;
  527. csym.n_numaux = 1;
  528. fwrite(&csym, 18, 1, f);
  529. auxfunc.tag = 0;
  530. auxfunc.size = 0x20;
  531. auxfunc.fileptr = 0;
  532. auxfunc.nextsym = 0;
  533. auxfunc.dummy = 0;
  534. fwrite(&auxfunc, 18, 1, f);
  535. n++;
  536. n++;
  537. }
  538. p++;
  539. }
  540. }
  541. if (s1->do_debug) {
  542. // write string table
  543. // first write the size
  544. i = pCoff_str_table - Coff_str_table;
  545. fwrite(&i, 4, 1, f);
  546. // then write the strings
  547. fwrite(Coff_str_table, i, 1, f);
  548. tcc_free(Coff_str_table);
  549. }
  550. return 0;
  551. }
  552. // group the symbols in order of filename, func1, func2, etc
  553. // finally global symbols
  554. void SortSymbolTable(void)
  555. {
  556. int i, j, k, n = 0;
  557. Elf32_Sym *p, *p2, *NewTable;
  558. char *name, *name2;
  559. NewTable = (Elf32_Sym *) tcc_malloc(nb_syms * sizeof(Elf32_Sym));
  560. p = (Elf32_Sym *) symtab_section->data;
  561. // find a file symbol, copy it over
  562. // then scan the whole symbol list and copy any function
  563. // symbols that match the file association
  564. for (i = 0; i < nb_syms; i++) {
  565. if (p->st_info == 4) {
  566. name = (char *) symtab_section->link->data + p->st_name;
  567. // this is a file symbol, copy it over
  568. NewTable[n++] = *p;
  569. p2 = (Elf32_Sym *) symtab_section->data;
  570. for (j = 0; j < nb_syms; j++) {
  571. if (p2->st_info == 0x12) {
  572. // this is a func symbol
  573. name2 =
  574. (char *) symtab_section->link->data + p2->st_name;
  575. // find the function data index
  576. for (k = 0; k < nFuncs; k++) {
  577. if (strcmp(name2, Func[k]) == 0)
  578. break;
  579. }
  580. if (k >= nFuncs) {
  581. char s[256];
  582. sprintf(s,
  583. "debug (sort) info can't find function: %s",
  584. name2);
  585. error(s);
  586. }
  587. if (strcmp(AssociatedFile[k], name) == 0) {
  588. // yes they match copy it over
  589. NewTable[n++] = *p2;
  590. }
  591. }
  592. p2++;
  593. }
  594. }
  595. p++;
  596. }
  597. // now all the filename and func symbols should have been copied over
  598. // copy all the rest over (all except file and funcs)
  599. p = (Elf32_Sym *) symtab_section->data;
  600. for (i = 0; i < nb_syms; i++) {
  601. if (p->st_info != 4 && p->st_info != 0x12) {
  602. NewTable[n++] = *p;
  603. }
  604. p++;
  605. }
  606. if (n != nb_syms)
  607. error("Internal Compiler error, debug info");
  608. // copy it all back
  609. p = (Elf32_Sym *) symtab_section->data;
  610. for (i = 0; i < nb_syms; i++) {
  611. *p++ = NewTable[i];
  612. }
  613. tcc_free(NewTable);
  614. }
  615. int FindCoffSymbolIndex(const char *func_name)
  616. {
  617. int i, n = 0;
  618. Elf32_Sym *p;
  619. char *name;
  620. p = (Elf32_Sym *) symtab_section->data;
  621. for (i = 0; i < nb_syms; i++) {
  622. name = (char *) symtab_section->link->data + p->st_name;
  623. if (p->st_info == 4) {
  624. // put a filename symbol
  625. n++;
  626. } else if (p->st_info == 0x12) {
  627. if (strcmp(func_name, name) == 0)
  628. return n;
  629. n += 6;
  630. // put a Function Name
  631. // now put aux info
  632. // put a .bf
  633. // now put aux info
  634. // put a .ef
  635. // now put aux info
  636. } else {
  637. n += 2;
  638. }
  639. p++;
  640. }
  641. return n; // total number of symbols
  642. }
  643. BOOL OutputTheSection(Section * sect)
  644. {
  645. const char *s = sect->name;
  646. if (!strcmp(s, ".text"))
  647. return true;
  648. else if (!strcmp(s, ".data"))
  649. return true;
  650. else
  651. return 0;
  652. }
  653. short int GetCoffFlags(const char *s)
  654. {
  655. if (!strcmp(s, ".text"))
  656. return STYP_TEXT | STYP_DATA | STYP_ALIGN | 0x400;
  657. else if (!strcmp(s, ".data"))
  658. return STYP_DATA;
  659. else if (!strcmp(s, ".bss"))
  660. return STYP_BSS;
  661. else if (!strcmp(s, ".stack"))
  662. return STYP_BSS | STYP_ALIGN | 0x200;
  663. else if (!strcmp(s, ".cinit"))
  664. return STYP_COPY | STYP_DATA | STYP_ALIGN | 0x200;
  665. else
  666. return 0;
  667. }
  668. Section *FindSection(TCCState * s1, const char *sname)
  669. {
  670. Section *s;
  671. int i;
  672. for (i = 1; i < s1->nb_sections; i++) {
  673. s = s1->sections[i];
  674. if (!strcmp(sname, s->name))
  675. return s;
  676. }
  677. error("could not find section %s", sname);
  678. return 0;
  679. }
  680. int tcc_load_coff(TCCState * s1, int fd)
  681. {
  682. // tktk TokenSym *ts;
  683. FILE *f;
  684. unsigned int str_size;
  685. char *Coff_str_table, *name;
  686. int i, k;
  687. struct syment csym;
  688. char name2[9];
  689. FILHDR file_hdr; /* FILE HEADER STRUCTURE */
  690. f = fdopen(fd, "rb");
  691. if (!f) {
  692. error("Unable to open .out file for input");
  693. }
  694. if (fread(&file_hdr, FILHSZ, 1, f) != 1)
  695. error("error reading .out file for input");
  696. if (fread(&o_filehdr, sizeof(o_filehdr), 1, f) != 1)
  697. error("error reading .out file for input");
  698. // first read the string table
  699. if (fseek(f, file_hdr.f_symptr + file_hdr.f_nsyms * SYMESZ, SEEK_SET))
  700. error("error reading .out file for input");
  701. if (fread(&str_size, sizeof(int), 1, f) != 1)
  702. error("error reading .out file for input");
  703. Coff_str_table = (char *) tcc_malloc(str_size);
  704. if (fread(Coff_str_table, str_size - 4, 1, f) != 1)
  705. error("error reading .out file for input");
  706. // read/process all the symbols
  707. // seek back to symbols
  708. if (fseek(f, file_hdr.f_symptr, SEEK_SET))
  709. error("error reading .out file for input");
  710. for (i = 0; i < file_hdr.f_nsyms; i++) {
  711. if (fread(&csym, SYMESZ, 1, f) != 1)
  712. error("error reading .out file for input");
  713. if (csym._n._n_n._n_zeroes == 0) {
  714. name = Coff_str_table + csym._n._n_n._n_offset - 4;
  715. } else {
  716. name = csym._n._n_name;
  717. if (name[7] != 0) {
  718. for (k = 0; k < 8; k++)
  719. name2[k] = name[k];
  720. name2[8] = 0;
  721. name = name2;
  722. }
  723. }
  724. // if (strcmp("_DAC_Buffer",name)==0) // tktk
  725. // name[0]=0;
  726. 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
  727. (csym.n_type == 0x18 && csym.n_sclass == 0x2) || // pointer to structure
  728. (csym.n_type == 0x7 && csym.n_sclass == 0x2) || // doubles
  729. (csym.n_type == 0x6 && csym.n_sclass == 0x2)) // floats
  730. {
  731. // strip off any leading underscore (except for other main routine)
  732. if (name[0] == '_' && strcmp(name, "_main") != 0)
  733. name++;
  734. tcc_add_symbol(s1, name, (void*)csym.n_value);
  735. }
  736. // skip any aux records
  737. if (csym.n_numaux == 1) {
  738. if (fread(&csym, SYMESZ, 1, f) != 1)
  739. error("error reading .out file for input");
  740. i++;
  741. }
  742. }
  743. return 0;
  744. }