1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
|
/***********************************************************************
*
* avra - Assembler for the Atmel AVR microcontroller series
*
* Copyright (C) 1998-2004 Jon Anders Haugum, Tobias Weber
*
* coff.c - Common Object File Format (COFF) support
*
* This file was developed for the avra assembler in order to produce COFF output files
* for use with the Atmel AVR Studio. The Lean C Compiler (LCC) debugging stabs
* output was used as input to the assembler. The information used to develop this file
* was obtained from various sources on the Internet, most notably, the Free Software Foundation,
* The "stabs" debug format, ??? Chapter 7: Common Object File Format (COFF),
*
* This software has absolutely no warrantee! The money you paid for this will be
* promptly refunded if not fully satisfied.
*
* Beta release 1/20/2000 by Bob Harris
*
* This software has not been fully tested and probably has a few software deficiencies.
* Some software support may be possible by sending a problem description report to
* rth@mclean.sparta.com
*
* Made the recommended change in write_coff_program().
* Fixed an obvious typo in SkipPastDigits(). The if() statement was terminated
* with a semicolon, which terminated the if(); early. JEG 4-01-03
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "misc.h"
#include "avra.h"
#include "args.h"
#include "coff.h"
#include "device.h" /* device flash and eeprom size */
struct FundamentalType {
char const *pString;
int Type;
int Size;
};
struct FundamentalType FundamentalTypes[] = {
{"null", T_NULL, 0},
{"void", T_VOID, 0},
{"char", T_CHAR, 1},
{"short", T_SHORT, 1},
{"int", T_INT, 1},
{"long", T_LONG, 2},
{"float", T_FLOAT, 4},
{"double", T_DOUBLE, 4},
{"struct", T_STRUCT, 0},
{"union", T_UNION, 0},
{"enum", T_ENUM, 0},
{"moe", T_MOE, 0},
{"unsigned char", T_UCHAR, 1},
{"unsigned short", T_USHORT, 1},
{"unsigned int", T_UINT, 1},
{"unsigned long", T_ULONG, 2},
{"long double", T_LNGDBL, 2},
{"long long int", T_LONG, 2},
{"long int", T_LONG, 2},
{"unsigned long long", T_ULONG, 2},
{"signed char", T_CHAR, 1},
{0, 0}
};
struct coff_info *ci;
/****************************************************************************************/
FILE *open_coff_file(struct prog_info *pi, char *filename){
int ok /*, i*/;
FILE *fp;
//unsigned long *pu4;
char*p;
ci = calloc( 1, sizeof(struct coff_info) );
if ( !ci )
return( 0 );
ok = True;
/* default values */
ci->CurrentFileNumber = 0;
ci->pRomMemory = 0;
ci->pEEPRomMemory = 0;
ci->MaxRomAddress = 0;
ci->MaxEepromAddress = 0;
ci->NeedLineNumberFixup = 0;
ci->GlobalStartAddress = -1;
ci->GlobalEndAddress = 0;
/* Linked lists start out at zero */
InitializeList( &ci->ListOfSectionHeaders );
InitializeList( &ci->ListOfRawData );
InitializeList( &ci->ListOfRelocations );
InitializeList( &ci->ListOfLineNumbers );
InitializeList( &ci->ListOfSymbols );
InitializeList( &ci->ListOfGlobals );
InitializeList( &ci->ListOfSpecials );
InitializeList( &ci->ListOfUndefined );
InitializeList( &ci->ListOfStrings );
InitializeList( &ci->ListOfTypes );
InitializeList( &ci->ListOfSplitLines );
/* add two default sections to SectionHeaders */
if ( !AllocateListObject( &ci->ListOfSectionHeaders, sizeof(struct external_scnhdr) ) ||
!AllocateListObject( &ci->ListOfSectionHeaders, sizeof(struct external_scnhdr) ) ) {
fprintf(stderr, "\nOut of memory allocating section headers!");
return( 0 );
}
/* add to string table */
p = (char *)AllocateListObject( &ci->ListOfStrings, 4 );
if ( !p ) {
fprintf(stderr, "\nOut of memory allocating string table space!");
return( 0 );
}
/* Allocate space for binary output into ROM, and EEPROM memory buffers for COFF output */
/* ASSUMES ci->device is accurate */
if ( (ci->pRomMemory = AllocateListObject( &ci->ListOfRawData, pi->device->flash_size * 2 ) ) != 0) {
if ( (ci->pEEPRomMemory = AllocateListObject( &ci->ListOfRawData, pi->device->eeprom_size )) != 0) {
ok = True; /* only true if both buffers are properly allocated */
/* now fill them with 0xff's to simulate flash erasure */
memset( (void *)ci->pRomMemory, 0xff, pi->device->flash_size * 2 );
memset( ( void *)ci->pEEPRomMemory, 0xff, pi->device->eeprom_size );
}
}
if ( ok != True )
return( 0 );
fp = fopen(filename,"wb");
if ( fp == NULL ) {
fprintf(stderr,"Error: cannot write coff file\n");
return( fp );
}
/* simulate void type .stabs void:t15=r1;*/
stab_add_local_type( "void", "15=r1;0;0;" );
return( fp );
}
/****************************************************************************************/
void write_coff_file(struct prog_info *pi){
//FILE *fp;
//struct label *label;
char /* File[256],*/*p;
struct external_scnhdr *pSectionHdr;
struct syment *pEntry;
union auxent *pAux;
unsigned long *plong;
int NumberOfSymbols, SymbolIndex, LastFileIndex, LastFunctionIndex, LastFunctionAddress;
LISTNODE *pNode;
int LinesOffset, SymbolsOffset, StringsOffset, RawOffset;
struct lineno *pLine;
/* add two special sections */
/* one for .text */
if ( ( pEntry = (struct syment*)AllocateTwoListObjects( &ci->ListOfSpecials, sizeof(struct syment) * 2 ) ) == 0 ) {
fprintf(stderr, "\nOut of memory allocating special headers for .text!");
return;
}
memset( pEntry->n_name, 0, 8 );
strcpy( pEntry->n_name, ".text" );
pEntry->n_value = 0;
pEntry->n_scnum = 1;
pEntry->n_type = 0;
pEntry->n_sclass = C_STAT;
pEntry->n_numaux = 1;
pEntry++;
pAux = (union auxent *)pEntry;
pAux->x_scn.x_scnlen = ci->MaxRomAddress + 2;
pAux->x_scn.x_nreloc = 0;
pAux->x_scn.x_nlinno = ci->ListOfLineNumbers.TotalItems;
/* one for .bss */
if ( ( pEntry = (struct syment*)AllocateTwoListObjects( &ci->ListOfSpecials, sizeof(struct syment) * 2 ) ) == 0 ) {
fprintf(stderr, "\nOut of memory allocating special header for .bss!");
return;
}
memset( pEntry->n_name, 0, 8 );
strcpy( pEntry->n_name, ".bss" );
if ( ci->GlobalStartAddress == -1 ) {
ci->GlobalEndAddress = ci->GlobalStartAddress = 0x60;
}
pEntry->n_value = ci->GlobalStartAddress;
pEntry->n_scnum = 2;
pEntry->n_type = 0;
pEntry->n_sclass = C_STAT;
pEntry->n_numaux = 1;
pEntry++;
pAux = (union auxent *)pEntry;
pAux->x_scn.x_scnlen = 0; /* we don't store any data here */
pAux->x_scn.x_nreloc = 0;
pAux->x_scn.x_nlinno = 0;
/* one more for .data - eeprom ??? */
/* Calculate common offsets into the file */
RawOffset = sizeof(struct external_filehdr) + ci->ListOfSectionHeaders.TotalBytes;
LinesOffset = RawOffset + ci->MaxRomAddress + 2; /* ignore eeprom for now */
SymbolsOffset = LinesOffset + ci->ListOfLineNumbers.TotalBytes;
StringsOffset = SymbolsOffset + ci->ListOfSymbols.TotalBytes + ci->ListOfSpecials.TotalBytes + ci->ListOfGlobals.TotalBytes;
/* Clean up loose ends in string table */
if ( !(plong = (unsigned long *)FindFirstListObject(&ci->ListOfStrings)) ) {
fprintf(stderr,"\nInternal error in string table!");
return;
}
*plong = ci->ListOfStrings.TotalBytes; /* Size of string table */
/* Clean up loose ends in symbol table */
/* symbol table - Filename value - index to next .file or global symbol */
/* The value of that symbol equals the symbol table entry index of the next .file symbol or .global */
LastFunctionAddress = ci->MaxRomAddress;
NumberOfSymbols = ci->ListOfSymbols.TotalItems + ci->ListOfSpecials.TotalItems + ci->ListOfGlobals.TotalItems;
SymbolIndex = LastFileIndex = NumberOfSymbols;
LastFunctionIndex = 0; /* set to zero on last function */
for ( pEntry = (struct syment *)FindLastListObject(&ci->ListOfSymbols);
pEntry != 0;
pEntry = (struct syment *)FindNextLastListObject(&ci->ListOfSymbols) ) {
/* Search for .file entries designated by C_FILE */
if ( pEntry->n_sclass == C_FILE ) {
pEntry->n_value = LastFileIndex;
LastFileIndex = SymbolIndex; /* save current index */
}
/* Search for Function entries C_EXT */
else if ( pEntry->n_sclass == C_EXT ) {
pEntry++;
pAux = (union auxent *)pEntry;
pAux->x_sym.x_misc.x_fsize = LastFunctionAddress - pEntry->n_value; /* function updated size */
pAux->x_sym.x_fcnary.x_fcn.x_lnnoptr += LinesOffset;
LastFunctionAddress = pEntry->n_value;
pAux->x_sym.x_fcnary.x_fcn.x_endndx = LastFunctionIndex; /* point to next function index */
pAux->x_sym.x_tvndx = 0; /* ??? */
LastFunctionIndex = SymbolIndex;
} else if ( (pEntry->n_sclass == C_FCN ) || ( pEntry->n_sclass == C_BLOCK) ) {
if ( pEntry->n_name[1] == 'b' ) {
/* .bf and .bb */
pEntry++;
pAux = (union auxent *)pEntry;
pAux->x_sym.x_fcnary.x_fcn.x_endndx = LastFunctionIndex;
}
}
/* else do nothing */
/* update current symbol index */
pNode = GetCurrentNode( &ci->ListOfSymbols );
SymbolIndex -= ( pNode->Size / sizeof(struct syment) );
}
// File Header
ci->FileHeader.f_magic = MAGIC_NUMBER_AVR;
ci->FileHeader.f_nscns = 2;
ci->FileHeader.f_timdat = time( (time_t *)&ci->FileHeader.f_timdat);
ci->FileHeader.f_symptr = SymbolsOffset;
ci->FileHeader.f_nsyms = NumberOfSymbols;
ci->FileHeader.f_opthdr = 0;
ci->FileHeader.f_flags = 0xff; /*F_RELFLG;*/ /* No relocation information available */
/* write it out */
if ( fwrite(&ci->FileHeader, 1, sizeof(struct external_filehdr), pi->coff_file ) != sizeof(struct external_filehdr) ) {
fprintf(stderr,"\nFile error writing header ...(disk full?)");
return;
}
// Optional Information
// Section 1 Header
pSectionHdr = (struct external_scnhdr *)FindFirstListObject(&ci->ListOfSectionHeaders);
if ( !pSectionHdr ) {
fprintf(stderr, "\nInternal Coff error - cannot find section header .text!");
return;
}
memset( &pSectionHdr->s_name[0], 0, sizeof(struct external_scnhdr) );
strcpy( &pSectionHdr->s_name[0], ".text");
pSectionHdr->s_paddr = 0;
pSectionHdr->s_vaddr = 0;
pSectionHdr->s_size = ci->MaxRomAddress + 2; /* remember the last instruction */
pSectionHdr->s_scnptr = RawOffset;
pSectionHdr->s_relptr = 0;
pSectionHdr->s_lnnoptr = LinesOffset;
pSectionHdr->s_nreloc = 0;
pSectionHdr->s_nlnno = ci->ListOfLineNumbers.TotalBytes/sizeof(struct lineno);
pSectionHdr->s_flags = STYP_TEXT;
/* write it out */
if ( fwrite(&pSectionHdr->s_name[0], 1, sizeof(struct external_scnhdr), pi->coff_file ) != sizeof(struct external_scnhdr) ) {
fprintf(stderr,"\nFile error writing section header ...(disk full?)");
return;
}
// Section 2 Header
pSectionHdr = (struct external_scnhdr *)FindNextListObject(&ci->ListOfSectionHeaders);
if ( !pSectionHdr ) {
fprintf(stderr, "\nInternal Coff error - cannot find section header .bss!");
return;
}
memset( &pSectionHdr->s_name[0], 0, sizeof(struct external_scnhdr) );
strcpy( &pSectionHdr->s_name[0], ".bss");
/* later expansion */
pSectionHdr->s_paddr = ci->GlobalStartAddress;
pSectionHdr->s_vaddr = ci->GlobalStartAddress;
pSectionHdr->s_flags = STYP_DATA; /* seems it should be STYP_BSS */
/* write it out */
if ( fwrite(&pSectionHdr->s_name[0], 1, sizeof(struct external_scnhdr), pi->coff_file ) != sizeof(struct external_scnhdr) ) {
fprintf(stderr,"\nFile error writing section header ...(disk full?)");
return;
}
/* Section N Header - .data or eeprom */
// Raw Data for Section 1
if ( (p = (unsigned char *)FindFirstListObject(&ci->ListOfRawData) ) == 0 ) {
fprintf(stderr,"\nInternal error - unable to find binary data!");
return;
}
/* write it out */
if ( fwrite( p, 1, ci->MaxRomAddress + 2, pi->coff_file ) != (size_t)(ci->MaxRomAddress + 2) ) {
fprintf(stderr,"\nFile error writing raw .text data ...(disk full?)");
return;
}
// Raw data for section n
// Relocation Info for section 1
// Relocation info for section n
// Line numbers for section 1
for ( pLine = (struct lineno *)FindFirstListObject( &ci->ListOfLineNumbers );
pLine != 0;
pLine = (struct lineno *)FindNextListObject( &ci->ListOfLineNumbers ) ) {
pNode = GetCurrentNode( &ci->ListOfLineNumbers );
/* write it out */
if ( fwrite( pLine, 1, pNode->Size, pi->coff_file ) != pNode->Size ) {
fprintf(stderr,"\nFile error writing line numbers ...(disk full?)");
return;
}
}
// Line numbers for section n
// Symbol table
for ( pEntry = (struct syment *)FindFirstListObject( &ci->ListOfSymbols );
pEntry != 0;
pEntry = (struct syment *)FindNextListObject( &ci->ListOfSymbols ) ) {
pNode = GetCurrentNode( &ci->ListOfSymbols );
/* write it out */
if ( fwrite( pEntry, 1, pNode->Size, pi->coff_file ) != pNode->Size ) {
fprintf(stderr,"\nFile error writing symbol table ...(disk full?)");
return;
}
}
// Symbol table of Globals
for ( pEntry = (struct syment *)FindFirstListObject( &ci->ListOfGlobals );
pEntry != 0;
pEntry = (struct syment *)FindNextListObject( &ci->ListOfGlobals ) ) {
pNode = GetCurrentNode( &ci->ListOfGlobals );
/* write it out */
if ( fwrite( pEntry, 1, pNode->Size, pi->coff_file ) != pNode->Size ) {
fprintf(stderr,"\nFile error writing global symbols ...(disk full?)");
return;
}
}
/* Specials .text, .bss, .data */
for ( pEntry = (struct syment *)FindFirstListObject( &ci->ListOfSpecials );
pEntry != 0;
pEntry = (struct syment *)FindNextListObject( &ci->ListOfSpecials ) ) {
pNode = GetCurrentNode( &ci->ListOfSpecials );
/* write it out */
if ( fwrite( pEntry, 1, pNode->Size, pi->coff_file ) != pNode->Size ) {
fprintf(stderr,"\nFile error writing special symbols ...(disk full?)");
return;
}
}
// String Table
for ( p = (char *)FindFirstListObject( &ci->ListOfStrings );
p != 0;
p = (char *)FindNextListObject( &ci->ListOfStrings ) ) {
pNode = GetCurrentNode( &ci->ListOfStrings );
/* write it out */
if ( fwrite( p, 1, pNode->Size, pi->coff_file ) != pNode->Size ) {
fprintf(stderr,"\nFile error writing strings data ...(disk full?)");
return;
}
}
return;
}
/****************************************************************************************/
void write_coff_eeprom( struct prog_info *pi, int address, unsigned char data){
if ( !GET_ARG(pi->args, ARG_COFF) )
return;
/* Coff output keeps track of binary data in memory buffers */
if ( ci->pEEPRomMemory ) {
if ( address <= pi->device->eeprom_size ) {
*(ci->pEEPRomMemory + address) = data;
if ( address >= ci->MaxEepromAddress )
ci->MaxEepromAddress = address; /* keep high water mark */
} else {
pi->error_count++;
fprintf(stderr, "Error: EEPROM address %d exceeds max range %d", address, pi->device->eeprom_size );
}
}
}
/****************************************************************************************/
void write_coff_program( struct prog_info *pi, int address, unsigned int data){
unsigned char *pByte;
if ( !GET_ARG(pi->args, ARG_COFF) )
return;
/* Coff output keeps track of binary data in memory buffers, address is in bytes not words */
if ( ci->pRomMemory ) {
/* JEG if ( address <= pi->device->flash_size ) { */ /* JEG 4-23-03 */
if ( address <= pi->device->flash_size*2 ) {
pByte = (unsigned char *)(ci->pRomMemory + address); /* point to low byte in memory */
*pByte++ = (data & 0xff); /* low byte */
*pByte = ((data >> 8) & 0xff); /* high byte */
if ( address >= ci->MaxRomAddress )
ci->MaxRomAddress = address; /* keep high water mark */
} else {
pi->error_count++;
/* JEG fprintf(stderr, "Error: FLASH address %d exceeds max range %d", address, pi->device->flash_size ); */
fprintf(stderr, "Error: FLASH address %d exceeds max range %d", address, pi->device->flash_size*2 );
}
}
}
/****************************************************************************************/
void close_coff_file(struct prog_info *pi, FILE *fp){
/* close the output file */
fclose( fp );
pi->coff_file = 0;
/* free all the internal memory buffers used by ci */
FreeList( &ci->ListOfSectionHeaders );
FreeList( &ci->ListOfRawData );
FreeList( &ci->ListOfRelocations );
FreeList( &ci->ListOfLineNumbers );
FreeList( &ci->ListOfSymbols );
FreeList( &ci->ListOfGlobals );
FreeList( &ci->ListOfUndefined );
FreeList( &ci->ListOfStrings );
FreeList( &ci->ListOfTypes );
FreeList( &ci->ListOfSplitLines );
/* now free ci */
free( ci );
ci = 0;
}
/****************************************************************************************/
int parse_stabs( struct prog_info *pi, char *p, int pass ){
int ok = True;
int TypeCode, n;
char *pString, *p2, *p3, *p4, *p5, *pType, *pEnd, *pp, *pJoined;
if ( !GET_ARG(pi->args, ARG_COFF) || ( pass == PASS_1 ) )
return(True);
/* stabs debugging information is in the form:
.stabs "symbolic info string", HexorDecimalTypecode, parm3, parm4, parm5
parm1, parm2, parm3 depend on typecode
N_LSYM 0x80 local sym: name,,0,type,offset
N_OPT 0x3c compiler options
N_SO 0x64 source file name: name,,0,0,address
N_SOL 0x84 #included file name: name,,0,0,address
N_FUN 0x24 procedure: name,,0,linenumber,address
N_GSYM 0x20 global symbol: name,,0,type,0
N_LCSYM 0x28 .lcomm symbol: name,,0,type,address
N_STSYM 0x26 static symbol: name,,0,type,address
N_RSYM 0x40 register sym: name,,0,type,register
N_PSYM 0xa0 parameter: name,,0,type,offset
*/
/* Look for multiple commands per line */
/* .stabs "linktag:T19=s46next:20=*19,0,16;last:20,16,16;a:21=ar1;0;2;22=ar1;0;3;1,32,96;\\",128,0,0,0 */
/* .stabs "b:23=ar1;0;4;24=ar1;0;5;2,128,240;;",128,0,0,0 */
/* Look for continuation lines per line */
/* Get String information as a token */
/* Parse the tokens in the stabn line buffer */
pString = get_next_token(p, TERM_DOUBLEQUOTE ); /* zap first doublequote */
p2 = get_next_token(pString, TERM_DOUBLEQUOTE ); /* zap last doublequote */
p2 = get_next_token(p2, TERM_COMMA ); /* zap comma */
p3 = get_next_token(p2, TERM_COMMA );
p4 = get_next_token(p3, TERM_COMMA );
p5 = get_next_token(p4, TERM_COMMA );
pEnd = get_next_token(p5, TERM_END ); /* zap CR LF, make ASCIIZ */
if ( !pString || !p2 || !p3 || !p4 || !p5 )
return( False );
/* Check for split lines */
n = strlen( pString );
if ( ( pString[n - 1] == '\\' ) && (pString[n - 2] == '\\') ) {
/* We have a continuation string here */
pString[n - 2] = 0;
n -= 2;
if ( !(pp = (char *)AllocateListObject( &ci->ListOfSplitLines, n + 1 )) ) {
fprintf(stderr, "\nOut of memory allocating continuation line!");
return( False );
}
strcpy( pp, pString ); /* loose the continuation characters */
return(True);
}
if ( ci->ListOfSplitLines.TotalItems > 0 ) {
/* Join lines together and process */
if ( !(pJoined = calloc( 1, n + ci->ListOfSplitLines.TotalBytes ) ) ) {
fprintf(stderr, "\nOut of memory joining continuation lines!");
return( False );
}
for ( pp = (char *)FindFirstListObject( &ci->ListOfSplitLines );
pp != 0;
pp = (char *)FindNextListObject( &ci->ListOfSplitLines ) ) {
strcat( pJoined, pp ); /* connect the lines */
}
strcat( pJoined, pString );
FreeList( &ci->ListOfSplitLines );
if ( !AddListObject( &ci->ListOfSplitLines, pJoined, n + ci->ListOfSplitLines.TotalBytes ) ) {
fprintf(stderr, "\nUnable to add joined continuation line");
return( False );
}
pString = pJoined;
}
if ( *p2 == '0' )
TypeCode = atox(p2); /* presume to be hex 0x */
else
TypeCode = atoi(p2);
switch ( TypeCode ) {
case N_OPT: /* compiler options */
break; /* nothing used here */
case N_SO: /* source file name: name,,0,0,address */
ok = stab_add_filename( pString, p5 );
break;
case N_GSYM: /* global symbol: name,,0,type,0 */
pType = get_next_token(pString, TERM_COLON ); /* separate at colon */
ok = stab_add_global( pi, pString, pType );
break;
case N_FUN: /* procedure: name,,0,linenumber,address */
ok = stab_add_function( pi, pString, p5 );
break;
case N_LSYM: /* local sym: name,,0,type,offset */
/* pString, p2 = TypeCode, p3 = 0, p4 = 0, p5 = offset */
pType = get_next_token(pString, TERM_COLON ); /* pType = symbol descriptor (character after the colon) */
if ( *pType == 't')
ok = stab_add_local_type( pString, ++pType );
else if (*pType == 'T')
ok = stab_add_tag_type( pString, ++pType );
else
ok = stab_add_local( pi, pString, pType, p5 );
break;
case N_RSYM: /* Symbol:[P|r]type,0,size,register */
pType = get_next_token(pString, TERM_COLON ); /* separate at colon */
ok = stab_add_local_register( pi, pString, pType, p5 );
break;
case N_LCSYM: /* .lcomm symbol: name,,0,type,address */
ok = True;
break; /* ignore constants */
case N_STSYM: /* static symbol: name,,0,type,address */
pType = get_next_token(pString, TERM_COLON ); /* separate at colon */
ok = stab_add_static_symbol( pi, pString, pType, p5 );
break;
case N_PSYM: /* parameter: name,,0,type,offset */
pType = get_next_token(pString, TERM_COLON ); /* separate at colon */
ok = stab_add_parameter_symbol( pi, pString, pType, p5 );
break;
case N_SOL: /* #included file name: name,,0,0,address */
ok = True;
break; /* ignore include files */
default:
ok = False;
}
if ( ci->ListOfSplitLines.TotalItems > 0 )
FreeList( &ci->ListOfSplitLines );
return( ok );
}
/****************************************************************************************/
int parse_stabn( struct prog_info *pi, char *p, int pass ){
int ok = True;
int TypeCode /* , LineNumber */, Level;
char *p1, *p2, *p3, *p4, *pLabel, *pFunction, *pEnd;
/* stabn debugging information is in the form:
.stabn TypeCode, 0, parm1, parm2
parm1 is level
parm2 is Label-Function
compiler currently produces the following TypeCodes:
N_LBRAC 0xc0 left bracket: 0,,0,nesting level,address
N_RBRAC 0xe0 right bracket: 0,,0,nesting level,address
N_SLINE 0x44 src line: 0,,0,linenumber,address
*/
if ( !GET_ARG(pi->args, ARG_COFF) || ( pass == PASS_1 ) )
return(True);
/* Parse the tokens in the stabn line buffer */
p1 = get_next_token(p, TERM_SPACE );
p2 = get_next_token(p1, TERM_COMMA );
p3 = get_next_token(p2, TERM_COMMA );
p4 = get_next_token(p3, TERM_COMMA );
pEnd = get_next_token(p4, TERM_END ); /* zap CR LF, make ASCIIZ */
if ( !p1 || !p2 || !p3 || !p4 )
return( False );
/* first convert TypeCode to binary */
if ( *p1 == '0' )
TypeCode = atox(p1); /* presume to be hex 0x */
else
TypeCode = atoi(p1);
Level = atoi(p3); /* line number or level */
pLabel = p4; /* Assembly label */
pFunction = get_next_token( pLabel, TERM_DASH ); /* Function */
switch ( TypeCode ) {
case N_SLINE: /* src line: 0,,0,linenumber,address */
ok = stab_add_lineno( pi, Level, pLabel, pFunction );
break;
case N_LBRAC: /* left bracket: 0,,0,nesting level,address */
ok = stab_add_lbracket( pi, Level, pLabel, pFunction );
break;
case N_RBRAC: /* right bracket: 0,,0,nesting level,address */
ok = stab_add_rbracket( pi, Level, pLabel, pFunction );
break;
default:
fprintf(stderr, "\nUnknown .stabn TypeCode = 0x%x", TypeCode );
ok = False;
}
return( ok );
}
/****************************************************************************************/
int stab_add_lineno( struct prog_info *pi, int LineNumber, char *pLabel, char *pFunction ){
int Address;
struct lineno *pln;
struct syment *pEntry;
union auxent *pAux;
/* Allocate LineNumber Table entry and fill it in */
pln = (struct lineno *)AllocateListObject(&ci->ListOfLineNumbers, sizeof(struct lineno) );
if ( !pln ) {
fprintf(stderr, "\nOut of memory allocating lineno table for function %s", pFunction );
return( False );
}
/* set value field to be address of label in bytes */
if ( !get_symbol(pi, pLabel, &Address) ) {
fprintf(stderr, "\nUnable to locate label %s", pLabel );
return( False );
}
pln->l_addr.l_paddr = Address * 2; /* need byte quanities */
/* Line number is relative to beginning of function, starts at 1 */
if ( ci->FunctionStartLine == 0 ) {
/* This line number is that same as the function start */
ci->FunctionStartLine = LineNumber;
}
pln->l_lnno = LineNumber - ci->FunctionStartLine + 1;
ci->CurrentSourceLine = LineNumber; /* keep track of source line for .eb .ef arrays */
if ( ci->NeedLineNumberFixup ) {
/* need to go into symbol table and fix last NeedLineNumberFixup entries */
for ( pEntry = (struct syment *)FindLastListObject(&ci->ListOfSymbols);
(pEntry != 0) && ( ci->NeedLineNumberFixup != 0);
pEntry = (struct syment *)FindNextLastListObject(&ci->ListOfSymbols) ) {
/* Fix up line number entries */
if ( (pEntry->n_sclass == C_FCN ) || ( pEntry->n_sclass == C_BLOCK ) || ( pEntry->n_sclass == C_EXT) ) {
pEntry++;
pAux = (union auxent *)pEntry;
pAux->x_sym.x_misc.x_lnsz.x_lnno = LineNumber;
ci->NeedLineNumberFixup--;
}
}
}
return(True);
}
/****************************************************************************************/
int stab_add_lbracket( struct prog_info *pi, int Level, char *pLabel, char *pFunction ){
int Address;
struct syment *pEntry;
union auxent *pAux;
//char *p;
//struct lineno *pln;
if ( !get_symbol(pi, pLabel, &Address) ) {
fprintf(stderr, "\nUnable to locate label %s", pLabel );
return( False );
}
/* Now create a .bb symbol table entry and aux entry too */
pEntry = (struct syment *)AllocateTwoListObjects( &ci->ListOfSymbols, sizeof(struct syment) * 2 );
if ( !pEntry ) {
fprintf(stderr, "\nOut of memory allocating symbol table entry for .bb %s", pLabel );
return( False );
}
/* n_name */
memset( pEntry->n_name, 0, 8 );
strcpy( pEntry->n_name, ".bb" );
pEntry->n_value = Address * 2; /* bytes not words */
pEntry->n_scnum = 1; /* .text */
pEntry->n_type = 0;
pEntry->n_sclass = C_BLOCK;
pEntry->n_numaux = 1;
pEntry++; /* point to aux entry */
pAux = (union auxent *)pEntry;
pAux->x_sym.x_misc.x_lnsz.x_lnno = 0; /* UNKNOWN - post process */
pAux->x_sym.x_misc.x_lnsz.x_size = 0; /* UNKNOWN - post process */
ci->NeedLineNumberFixup++; /* once for .bb block */
return(True);
}
/****************************************************************************************/
int stab_add_rbracket( struct prog_info *pi, int Level, char *pLabel, char *pFunction ){
int Address;
struct syment *pEntry;
union auxent *pAux;
//char *p;
//struct lineno *pln;
if ( !get_symbol(pi, pLabel, &Address) ) {
fprintf(stderr, "\nUnable to locate label %s", pLabel );
return( False );
}
/* Now create a .eb symbol table entry */
pEntry = (struct syment *)AllocateTwoListObjects( &ci->ListOfSymbols, sizeof(struct syment) * 2 );
if ( !pEntry ) {
fprintf(stderr, "\nOut of memory allocating symbol table entry for .eb %s", pLabel );
return( False );
}
/* n_name */
memset( pEntry->n_name, 0, 8 );
strcpy( pEntry->n_name, ".eb" );
pEntry->n_sclass = C_BLOCK;
pEntry->n_value = Address * 2; /* bytes not words */
pEntry->n_scnum = 1; /* .text */
pEntry->n_type = 0;
pEntry->n_numaux = 1;
pEntry++; /* point to aux entry */
pAux = (union auxent *)pEntry;
pAux->x_sym.x_misc.x_lnsz.x_lnno = ci->CurrentSourceLine;
/* create an .ef if at level 0 */
if ( Level == 0 ) {
/* Now create a .ef symbol table entry */
pEntry = (struct syment *)AllocateTwoListObjects( &ci->ListOfSymbols, sizeof(struct syment) * 2 );
if ( !pEntry ) {
fprintf(stderr, "\nOut of memory allocating symbol table entry for .ef %s", pLabel );
return( False );
}
/* n_name */
memset( pEntry->n_name, 0, 8 );
strcpy( pEntry->n_name, ".ef" );
pEntry->n_sclass = C_FCN;
pEntry->n_value = Address * 2; /* bytes not words */
pEntry->n_scnum = 1; /* .text */
pEntry->n_type = 0;
pEntry->n_numaux = 1;
pEntry++; /* point to aux entry */
pAux = (union auxent *)pEntry;
pAux->x_sym.x_misc.x_lnsz.x_lnno = ci->CurrentSourceLine;
}
return(True);
}
/****************************************************************************************/
int stab_add_filename( char *pName, char *pLabel ){
int ok, n;
struct syment *pEntry;
union auxent *pAux;
char *p;
/* if( pLabel == "Ltext0" ) then beginning of .text, pName = cwd, next pName = file */
/* if( pLabel == "Letext" ) then end of .text , pName == NULL */
/* we only need the one not ending in Slash */
ok = True;
n = strlen(pName);
if ( n > 0 ) {
if ( ( pName[ n - 1] == '\\') || (pName[ n - 1] == '/') )
return(True); /* ignore */
} else
return(True);
/* allocate entry in symbol table list */
pEntry = (struct syment *)AllocateTwoListObjects(
&ci->ListOfSymbols, sizeof(struct syment) * 2 ); /* aux entry too */
if ( !pEntry ) {
fprintf(stderr, "\nOut of memory allocating symbol table entry for global %s", pName );
return( False );
}
/* n_name */
memset( pEntry->n_name, 0, 8 );
strcpy( pEntry->n_name, ".file" );
/* n_value is determined after processing done UNKNOWN - post process */
/* The value of that symbol equals the symbol table entry index of the next .file symbol or .global */
/* post process */
pEntry->n_scnum = N_DEBUG;
pEntry->n_sclass = C_FILE;
pEntry->n_numaux = 1;
pEntry++; /* point to aux entry */
pAux = (union auxent *)pEntry;
/* Add Label name to symbol table */
if ( n <= FILNMLEN ) {
/* inline filename */
memset( pAux->x_file.x_fname, 0, FILNMLEN );
strncpy( pAux->x_file.x_fname, pName, n ); /* might not be zero terminated */
} else {
pAux->x_file.x_n.x_zeroes = 0; /* symbol name is in string table */
pAux->x_file.x_n.x_offset = ci->ListOfStrings.TotalBytes;
/* add to string table */
p = (char *)AllocateListObject( &ci->ListOfStrings, n + 1 );
if ( !p ) {
fprintf(stderr, "\nOut of memory allocating string table space!");
return( False );
}
strcpy( p, pName );
}
return( ok );
}
/****************************************************************************************/
int stab_add_function( struct prog_info *pi, char *pName, char *pLabel ){
int n, Address;
unsigned short CoffType, Type;
struct syment *pEntry;
char *pType;
struct lineno *pln;
union auxent *pAux;
int SymbolIndex;
pType = get_next_token(pName, TERM_COLON ); /* pType = symbol descriptor (character after the colon) */
Type = atoi(pType + 1); /* skip past F, predefined variable type */
if ( (CoffType = GetCoffType( Type )) == 0 ) {
fprintf(stderr, "\nUnrecognized return type found for function %s = %d", pName, Type );
return(False);
}
/* Get Current Symbol Index, Allocate Symbol Table entry and fill it in */
SymbolIndex = ci->ListOfSymbols.TotalItems;
pEntry = (struct syment *)AllocateTwoListObjects( &ci->ListOfSymbols, sizeof(struct syment) * 2 );
if ( !pEntry ) {
fprintf(stderr, "\nOut of memory allocating symbol table entry for function %s", pName );
return( False );
}
if ( (n = AddNameToEntry( pName, pEntry )) == 0 ) {
fprintf(stderr,"\nOut of memory adding local %s to string table", pName );
}
if ( !get_symbol(pi, pLabel, &Address) ) {
fprintf(stderr, "\nUnable to locate function %s", pName );
return( False );
}
pEntry->n_value = Address * 2; /* convert words to bytes */
pEntry->n_scnum = 2; /* .bss */
if ( (CoffType = GetCoffType( Type )) == 0 ) {
fprintf(stderr, "\nUnrecognized type found for function %s = %d", pName, Type );
return(False);
}
pEntry->n_type = (unsigned short)(CoffType | (DT_FCN << 4));
pEntry->n_sclass = C_EXT;
pEntry->n_numaux = 1;
pEntry++; /* point to aux entry */
pAux = (union auxent *)pEntry;
pAux->x_sym.x_tagndx = SymbolIndex + 1; /* point to the .bf entry index */
// wrong!
// pAux->x_sym.x_misc.x_lnsz.x_lnno = ci->ListOfLineNumbers.TotalBytes; /* Relative Fixup point to where line numbers start */
// pAux->x_sym.x_misc.x_lnsz.x_size = 0; /* UNKNOWN till next function called */
pAux->x_sym.x_misc.x_fsize = 0; /* unknown till end */
pAux->x_sym.x_fcnary.x_fcn.x_lnnoptr = ci->ListOfLineNumbers.TotalBytes; /* relative offset to line number entry */
pAux->x_sym.x_fcnary.x_fcn.x_endndx = 0; /* index to next entry */
/* Now add function entry into the line number table */
/* Allocate Symbol Table entry and fill it in */
pln = (struct lineno *)AllocateListObject(&ci->ListOfLineNumbers, sizeof(struct lineno) );
if ( !pln ) {
fprintf(stderr, "\nOut of memory allocating lineno table for function %s", pName );
return( False );
}
pln->l_lnno = 0;
pln->l_addr.l_symndx = SymbolIndex;
/* Initialize the FunctionStartLine from the beginning of the function */
ci->FunctionStartLine = 0;
/* Allocate Symbol Table entry and fill it in */
pEntry = (struct syment *)AllocateTwoListObjects( &ci->ListOfSymbols, sizeof(struct syment) * 2 );
if ( !pEntry ) {
fprintf(stderr, "\nOut of memory allocating symbol table entry .bf for function %s", pName );
return( False );
}
memset( pEntry->n_name, 0, 8 );
strcpy( pEntry->n_name, ".bf" );
pEntry->n_value = Address * 2; /* bytes not words */
pEntry->n_scnum = 1; /* .text */
pEntry->n_type = 0;
pEntry->n_sclass = C_FCN;
pEntry->n_numaux = 1;
pEntry++; /* point to aux entry */
pAux = (union auxent *)pEntry;
pAux->x_sym.x_misc.x_lnsz.x_lnno = 0; /* UNKNOWN - post process */
pAux->x_sym.x_misc.x_lnsz.x_size = 0; /* UNKNOWN - post process */
ci->NeedLineNumberFixup++; /* once for function C_EXT symbol */
ci->NeedLineNumberFixup++; /* once for .bf block */
return( True );
}
/****************************************************************************************/
int stab_add_global( struct prog_info *pi, char *pName, char *pType ){
int n, Address, IsArray, SymbolIndex;
unsigned short CoffType, Type;
struct syment *pEntry;
char *p;
STABCOFFMAP *pMap;
n = strlen( pName ); /* see if it's 8 bytes or less */
Type = atoi(pType + 1); /* skip past G, predefined variable type */
if ( (CoffType = GetCoffType( Type )) == 0 ) {
fprintf(stderr, "\nUnrecognized type found for global %s = %d", pName, Type );
return(False);
}
pMap = (STABCOFFMAP *)GetCurrentListObject( &ci->ListOfTypes );
SymbolIndex = ci->ListOfSymbols.TotalItems;
/* Allocate Symbol Table entry and fill it in, Auxiliary table if its an array */
if ( IsTypeArray( CoffType ) == True ) {
IsArray = True;
pEntry = (struct syment *)AllocateTwoListObjects( &ci->ListOfGlobals, sizeof(struct syment) * 2 );
} else {
IsArray = False;
pEntry = (struct syment *)AllocateListObject( &ci->ListOfGlobals, sizeof(struct syment) );
}
if ( (n = AddNameToEntry( pName, pEntry )) == 0 ) {
fprintf(stderr,"\nOut of memory adding local %s to string table", pName );
}
/* set value field to be address of label in bytes */
/* add underscore to lookup label */
if ( (p = calloc( 1, n + 2)) == 0 ) {
fprintf(stderr,"\nOut of memory adding global %s", pName );
return(False);
}
*p = '_';
strcpy( p + 1, pName );
if ( !get_symbol(pi, p, &Address) ) {
fprintf(stderr, "\nUnable to locate global %s", p );
free( p );
return( False );
}
free( p );
pEntry->n_value = Address; /* already in bytes */
if ( ci->GlobalStartAddress == -1 ) {
ci->GlobalStartAddress = Address;
}
if ( Address < ci->GlobalStartAddress )
ci->GlobalStartAddress = Address;
if ( Address > ci->GlobalEndAddress )
ci->GlobalEndAddress = Address;
pEntry->n_scnum = 2; /* .bss */
pEntry->n_type = CoffType;
pEntry->n_sclass = C_STAT;
if ( IsArray == False )
pEntry->n_numaux = 0;
else {
pEntry->n_numaux = 1;
pEntry++;
AddArrayAuxInfo( (union auxent *)pEntry, (unsigned short)SymbolIndex, pMap );
}
return( True );
}
/****************************************************************************************/
int stab_add_local( struct prog_info *pi, char *pName, char *pType, char *pOffset ){
int n, Offset, IsArray;
unsigned short CoffType, Type, SymbolIndex;
struct syment *pEntry;
STABCOFFMAP *pMap;
n = strlen( pName ); /* see if it's 8 bytes or less */
Type = atoi(pType); /* predefined variable type */
Offset = atoi(pOffset); /* offset in stack frame */
if ( (CoffType = GetCoffType( Type )) == 0 ) {
fprintf(stderr, "\nUnrecognized type found for local %s = %d", pName, Type );
return(False);
}
pMap = (STABCOFFMAP *)GetCurrentListObject( &ci->ListOfTypes );
SymbolIndex = ci->ListOfSymbols.TotalItems;
/* Allocate Symbol Table entry and fill it in, Auxiliary table if its an array */
if ( IsTypeArray( CoffType ) == True ) {
IsArray = True;
pEntry = (struct syment *)AllocateTwoListObjects( &ci->ListOfGlobals, sizeof(struct syment) * 2 );
} else {
IsArray = False;
pEntry = (struct syment *)AllocateListObject( &ci->ListOfSymbols, sizeof(struct syment) );
}
if ( (n = AddNameToEntry( pName, pEntry )) == 0 ) {
fprintf(stderr,"\nOut of memory adding local %s to string table", pName );
}
pEntry->n_type = CoffType;
pEntry->n_sclass = C_AUTO;
pEntry->n_scnum = N_ABS;
pEntry->n_value = Offset + 1; /* Silly avr studio is set in its ways */
if ( IsArray == False )
pEntry->n_numaux = 0;
else {
pEntry->n_numaux = 1;
pEntry++;
AddArrayAuxInfo( (union auxent *)pEntry, SymbolIndex, pMap );
}
return( True );
}
/****************************************************************************************/
int stab_add_parameter_symbol( struct prog_info *pi, char *pName, char *pType, char *pOffset ){
int n, Offset;
unsigned short CoffType, Type;
struct syment *pEntry;
n = strlen( pName ); /* see if it's 8 bytes or less */
Type = atoi(pType); /* predefined variable type */
Offset = atoi(pOffset); /* offset in stack frame */
if ( (CoffType = GetCoffType( Type )) == 0 ) {
fprintf(stderr, "\nUnrecognized type found for %s = %d", pName, Type );
return(False);
}
/* Allocate Symbol Table entry and fill it in */
pEntry = (struct syment *)AllocateListObject( &ci->ListOfSymbols, sizeof(struct syment) );
if ( (n = AddNameToEntry( pName, pEntry )) == 0 ) {
fprintf(stderr,"\nOut of memory adding local %s to string table", pName );
}
pEntry->n_type = CoffType;
pEntry->n_sclass = C_ARG;
pEntry->n_scnum = N_ABS;
pEntry->n_value = Offset;
pEntry->n_numaux = 0;
return( True );
}
/****************************************************************************************/
int stab_add_static_symbol( struct prog_info *pi, char *pName, char *pType, char *pLabel ){
int n, Address;
unsigned short CoffType, Type;
struct syment *pEntry;
n = strlen( pName ); /* see if it's 8 bytes or less */
Type = atoi(pType + 1); /* skip past S, predefined variable type */
if ( (CoffType = GetCoffType( Type )) == 0 ) {
fprintf(stderr, "\nUnrecognized type found for %s = %d", pName, Type );
return(False);
}
/* Allocate Symbol Table entry and fill it in */
pEntry = (struct syment *)AllocateListObject( &ci->ListOfSymbols, sizeof(struct syment) );
if ( (n = AddNameToEntry( pName, pEntry )) == 0 ) {
fprintf(stderr,"\nOut of memory adding local %s to string table", pName );
}
pEntry->n_type = CoffType;
pEntry->n_sclass = C_STAT;
pEntry->n_scnum = N_ABS;
if ( !get_symbol(pi, pLabel, &Address) ) {
fprintf(stderr, "\nUnable to locate label %s", pLabel );
return( False );
}
pEntry->n_value = Address * 2; /* find address of variable in bytes */
pEntry->n_numaux = 0;
return( True );
}
/****************************************************************************************/
int stab_add_local_register( struct prog_info *pi, char *pName, char *pType, char *pRegister ){
int n, Register, Size;
unsigned short CoffType, Type;
struct syment *pEntry;
n = strlen( pName ); /* see if it's 8 bytes or less */
Type = (unsigned short)atoi(pType + 1); /* skip past P, predefined variable type */
Register = atoi(pRegister); /* offset in stack frame */
if ( (CoffType = GetCoffType( Type )) == 0 ) {
fprintf(stderr, "\nUnrecognized type found for %s = %d", pName, Type );
return(False);
}
Size = GetCoffTypeSize( Type ); /* Silly requirement for avr studio */
/* Allocate Symbol Table entry and fill it in */
pEntry = (struct syment *)AllocateListObject( &ci->ListOfSymbols, sizeof(struct syment) );
if ( (n = AddNameToEntry( pName, pEntry )) == 0 ) {
fprintf(stderr,"\nOut of memory adding local %s to string table", pName );
return(False);
}
pEntry->n_type = CoffType;
// if( (*pType == 'r') || (*pType == 'R') )
// pEntry->n_sclass = C_REG;
// else if( (*pType == 'p') || (*pType == 'P') )
pEntry->n_sclass = C_REGPARM; /* Silly avr studio only accepts this for registers */
// else{
// fprintf(stderr,"\nUnknown register type -> %s", pType );
// return(False);
// }
pEntry->n_scnum = N_ABS;
pEntry->n_numaux = 0;
if ( Size == 1 )
pEntry->n_value = 0xffffff00 | Register; /* Silly requirement for avr studio */
else if ( Size == 2 )
pEntry->n_value = 0xffff0000 | ((Register + 1) << 8) | Register; /* Silly requirement for avr studio */
else if ( Size == 4 )
pEntry->n_value = ((Register + 3) << 24) | ((Register + 3) << 16) | ((Register + 1) << 8) | Register; /* Silly requirement for avr studio */
else {
fprintf(stderr,"\nUnknown register size (%d) and coff type (%d)", Size, CoffType );
return(False);
}
return( True );
}
/****************************************************************************************/
int stab_add_local_type( char *pName, char *pType ){
char *p;
unsigned short StabType;
/* .stabs "int:t1=r1;-128;127;",128,0,0,0 */
/* .stabs ":t20=ar1;0;1;21=ar1;0;1;2",128,0,0,0 */
/* pType-----^ */
/* Stab Type - convert to Coff type at end (after inline assignments */
if ( GetStabType( pType, &StabType, &p ) != True ) {
fprintf(stderr,"\nInvalid tag type found in structure item -> %s", p);
return(False);
}
return(True);
}
/****************************************************************************************/
int GetStructUnionTagItem( char *p, char **pEnd, char **pName, unsigned short *pType, unsigned short *pBitOffset, unsigned short *pBitSize) {
unsigned short StabType;
/* Structure or Union Tag Item consists of -> name:type,bitoffset,bitsize; */
/* name */
*pName = p;
while ( *p && (*p != ':') ) p++; // locate colon
if ( *p != ':' ) {
fprintf(stderr,"\nNo colon found in structure item -> %s", *pName);
return(False);
}
*p++ = 0; // Asciiz
/* Stab Type - convert to Coff type at end (after inline assignments */
if ( GetStabType( p, &StabType, &p ) != True ) {
fprintf(stderr,"\nInvalid tag type found in structure item -> %s", p);
return(False);
}
/* BitSize */
if ( *p != ',' ) {
fprintf(stderr,"\nNo Bit size found in structure item -> %s", p );
return(False);
}
*pBitOffset = (unsigned short)atoi( ++p );
while ( *p && (*p >= '0') && (*p <= '9') ) p++; // locate end of digits
/* BitOffset */
if ( *p != ',' ) {
fprintf(stderr,"\nNo Bit offset found in structure item -> %s", p );
return(False);
}
*pBitSize = (unsigned short)atoi( ++p );
while ( *p && (*p >= '0') && (*p <= '9') ) p++; // locate end of digits
/* Now convert stab type to COFF */
if ( (*pType = GetCoffType( (unsigned short)StabType)) == 0 ) {
fprintf(stderr,"\nNo COFF type found for stab type %d", StabType );
return( False);
}
if ( *++p == ';' ) /* Now eat last semicolon(s) */
p++;
*pEnd = p;
return( True );
}
/****************************************************************************************/
int GetEnumTagItem( char *p, char **pEnd, char **pEnumName, int *pEnumValue ) {
/* Enum Tag Item consists of -> member1:value,member2:value2,; */
*pEnumName = p;
while ( *p && (*p != ':') ) p++; // locate colon
if ( *p != ':' ) {
fprintf(stderr,"\nNo colon found in enum item -> %s", *pEnumName);
return(False);
}
*p++ = 0; // Asciiz
*pEnumValue = atoi(p);
while ( *p && (*p >= '0') && (*p <= '9') ) p++; // locate end of digits
if ( *p != ',' ) {
fprintf(stderr,"\nNo comma found after enum value -> %s", p );
return(False);
}
if ( *++p ==';' )
p++; /* eat last semicolon */
*pEnd = p;
return( True );
}
/****************************************************************************************/
int GetArrayType( char *p, char **pEnd, STABCOFFMAP *pMap, unsigned short *DerivedBits, int ExtraLevels ){
int MinIndex, MaxIndex, Result, Size, i;
char *pMinIndex, *pMaxIndex, *pType;
unsigned short Type;
Result = True;
pMinIndex = pMaxIndex = pType = 0;
while ( *p && (*p != ';') ) p++; /* find min index */
pMinIndex = ++p;
while ( *p && (*p != ';') ) p++; /* find max index */
pMaxIndex = ++p;
while ( *p && (*p != ';') ) p++; /* find type index */
pType = ++p;
/* bump the pointers to the digits */
if ( !isdigit(*pMinIndex) )
Result = False;
if ( !isdigit(*pMaxIndex) )
Result = False;
if ( !isdigit(*pType) )
Result = False;
/* Is syntax ok ? */
if ( Result != True ) {
fprintf(stderr,"\nSyntax error on array parameters %s%s%s", pMinIndex, pMaxIndex, pType );
return(False);
}
MinIndex = atoi(pMinIndex);
MaxIndex = atoi(pMaxIndex);
if ( GetStabType( p, &Type, &p ) != True )
return(False);
if ( !SetupDefinedType( Type, pMap, DerivedBits, ExtraLevels ) )
return( False );
/* Now update the size based on the indicies */
Size = (MaxIndex - MinIndex) + 1;
pMap->ByteSize *= Size;
pMap->Line = ci->CurrentSourceLine;
/* add the dimension information */
for ( i = 5; i >= 0; i-- ) {
if ( pMap->Dimensions[i] != 0 ) {
i++;
pMap->Dimensions[i] = Size;
break;
}
}
*pEnd = p;
return(True);
}
/****************************************************************************************/
int GetStabType( char *p, unsigned short *pType, char **pEnd ) {
STABCOFFMAP *pMap;
int extra, ok;
unsigned short derivedbits[6];
unsigned short LStabType, RStabType;
char *pHigh, *pLow;
LStabType = atoi( p );
while ( *p && (*p >= '0') && (*p <= '9') ) p++; // locate end of digits
*pType = LStabType;
if ( GetCoffType( LStabType ) != 0 ) {
*pEnd = p;
return(True);
}
if ( *p != '=' ) {
fprintf(stderr, "\nSyntax error in type assignment -> %s", p );
return(False);
}
p++;
/* Allocate space for new internal type */
if ( !(pMap = (STABCOFFMAP *)AllocateListObject(&ci->ListOfTypes, sizeof(STABCOFFMAP)) ) ) {
fprintf(stderr, "\nOut of memory allocating type info!");
return(False);
}
pMap->StabType = LStabType;
/* process items to right of equals */
for ( extra = 0; extra < 6; extra++ ) {
if ( isdigit( *p ) ) {
/* Finally found base type, try to terminate loop */
GetStabType( p, &RStabType, &p );
// RStabType = atoi( p );
while ( *p && (*p >= '0') && (*p <= '9') ) p++; // locate end of digits
if ( SetupDefinedType( RStabType, pMap, &derivedbits[0], extra ) != True )
return( False );
break;
} else if ( *p == 'a' ) {
derivedbits[extra] = DT_ARY;
p++;
/* Calculate size */
/* Since type assignment will be made we need to set extra bits here */
extra++;
/* =ar1;MinIndex;MaxIndex;BaseType */
if ( GetArrayType( p, &p, pMap, &derivedbits[0], extra ) != True )
return(False);
break;
} else if ( *p == 'f' ) {
derivedbits[extra] = DT_FCN;
p++;
} else if ( *p == '*' ) {
derivedbits[extra] = DT_PTR;
p++;
} else if ( *p == 'r' ) {
// if( LStabType < 15 )
// ok = GetInternalType( pString, pMap ); /* internal types not yet installed */
// else
while ( *p && (*p != ';' ) ) p++;
pLow = p++;
while ( *p && (*p != ';' ) ) p++;
pHigh = p++;
ok = GetSubRangeType( LStabType, pMap, pLow, pHigh );
if ( ok != True )
return(False);
while ( *p && (*p != ';' ) ) p++; /* find end of range */
p++;
break;
} else {
fprintf(stderr, "\nUnrecognized Type modifier %c!", *p );
return(False);
}
}
*pEnd = p; /* Update return pointer */
return(True);
}
/****************************************************************************************/
int stab_add_tag_type( char *pName, char *pString ){
int SymbolIndex, StabType, TotalSize, n, EnumValue;
unsigned short TagType, ItemType, BitOffset, BitSize;
char *p;
struct syment* pEntry;
union auxent *pAux;
STABCOFFMAP *pMap;
/* We arrived here due to :T defining either a structure, union or enumeration */
/* store the basic type as for internals and emit coff structures for debugging */
/* .stabs "stag:T17=s2i:1,0,8;c:2,8,8;;",128,0,0,0 */
/* .stabs "2:T18=u2a:2,0,8;b:1,0,8;c:6,0,16;;",128,0,0,0 */
/* .stabs "1:T19=eenum1:1,enum2:2,enum3:3,;",128,0,0,0 */
/* we don't care about the name */
/* check for bogus errors */
if ( !pName || !pString ) {
fprintf(stderr,"\nInvalid .stabs type format - no information!");
return(False);
}
p = pString;
/* Stab Type - convert to Coff type at end (after inline assignments */
if ( (StabType = (unsigned short)atoi(p)) == 0 ) {
fprintf(stderr,"\nInvalid .stabs type format - no information! - > %s", p );
return(False);
}
while ( *p && (*p >= '0') && (*p <= '9') ) p++; // locate end of digits
if ( *p != '=' ) {
fprintf(stderr,"\nInvalid .stabs type format - no equals - > %s", p );
return(False);
}
SymbolIndex = ci->ListOfSymbols.TotalItems;
if ( ( pEntry = (struct syment*)AllocateTwoListObjects( &ci->ListOfGlobals, sizeof(struct syment) * 2 ) ) == 0 ) {
fprintf(stderr, "\nOut of memory allocating symbol tag entries");
return(False);
}
/* Prepare Tag Header */
if ( (n = AddNameToEntry( pName, pEntry )) == 0 ) {
fprintf(stderr,"\nOut of memory adding local %s to string table", pString );
return(False);
}
if ( !(pMap = (STABCOFFMAP *)AllocateListObject(&ci->ListOfTypes, sizeof(STABCOFFMAP)) ) ) {
fprintf(stderr, "\nOut of memory allocating type info!");
return(False);
}
pMap->StabType = StabType;
pEntry->n_value = 0;
pEntry->n_scnum = N_DEBUG;
pEntry->n_numaux = 1;
if ( *++p == 's' ) {
TagType = pEntry->n_type = pMap->CoffType = T_STRUCT;
pEntry->n_sclass = C_STRTAG;
TotalSize = (unsigned short)atoi(++p);
} else if ( *p == 'u' ) {
TagType = pEntry->n_type = pMap->CoffType = T_UNION;
pEntry->n_sclass = C_UNTAG;
TotalSize = (unsigned short)atoi(++p);
} else if ( *p == 'e' ) {
TagType = pEntry->n_type = pMap->CoffType = T_ENUM;
pEntry->n_sclass = C_ENTAG;
TotalSize = FundamentalTypes[T_INT].Size; /* use size of int for enums */
} else {
fprintf(stderr,"\nUnknown tag type -> %s", p );
return(False);
}
while ( *p && (*p >= '0') && (*p <= '9') ) p++; // locate end of digits
pEntry++; /* point to aux entry */
pAux = (union auxent *)pEntry;
pAux->x_sym.x_tagndx = SymbolIndex;
pAux->x_sym.x_misc.x_lnsz.x_size = TotalSize;
/* update our local knowledge of tag type */
pMap->CoffType = TagType;
pMap->ByteSize = TotalSize;
pMap->Line = ci->CurrentSourceLine;
/* Process the items until the end of the line */
while ( *pName ) {
if ( ( pEntry = (struct syment*)AllocateTwoListObjects( &ci->ListOfGlobals, sizeof(struct syment) * 2 ) ) == 0 ) {
fprintf(stderr, "\nOut of memory allocating symbol tag member entries");
return(False);
}
if ( TagType == T_STRUCT ) {
if ( GetStructUnionTagItem( p, &p, &pName, &ItemType, &BitOffset, &BitSize) != True ) {
return(False);
}
pEntry->n_value = BitOffset/8;
pEntry->n_type = ItemType;
pEntry->n_sclass = C_MOS;
} else if ( TagType == T_UNION ) {
if ( GetStructUnionTagItem( p, &p, &pName, &ItemType, &BitOffset, &BitSize) != True ) {
return(False);
}
pEntry->n_value = BitOffset/8;
pEntry->n_type = ItemType;
pEntry->n_sclass = C_MOU;
} else { /* T_ENUM */
if ( GetEnumTagItem( p, &p, &pName, &EnumValue ) != True ) {
return(False);
}
pEntry->n_value = EnumValue;
pEntry->n_type = TotalSize;
pEntry->n_sclass = C_MOE;
}
/* Prepare Common Tag Header items */
if ( (n = AddNameToEntry( pName, pEntry )) == 0 ) {
fprintf(stderr,"\nOut of memory adding local %s to string table", pString );
return(False);
}
pEntry->n_scnum = N_ABS;
pEntry->n_numaux = 1;
pEntry++; /* point to aux entry */
pAux = (union auxent *)pEntry;
pAux->x_sym.x_tagndx = SymbolIndex;
pAux->x_sym.x_misc.x_lnsz.x_size = TotalSize;
pName = p;
}
/* End of Structures/Unions/Enumberations */
if ( ( pEntry = (struct syment*)AllocateTwoListObjects( &ci->ListOfGlobals, sizeof(struct syment) * 2 ) ) == 0 ) {
fprintf(stderr, "\nOut of memory allocating special headers for structure!");
return(False);
}
strcpy( pEntry->n_name, ".eos" );
pEntry->n_value = TotalSize;
pEntry->n_scnum = N_ABS;
pEntry->n_type = 0;
pEntry->n_sclass = C_EOS;
pEntry->n_numaux = 1;
pEntry++; /* point to aux entry */
pAux = (union auxent *)pEntry;
pAux->x_sym.x_tagndx = SymbolIndex; /* point to the .bf entry index */
pAux->x_sym.x_misc.x_lnsz.x_size = TotalSize;
return(True);
}
/****************************************************************************************/
int SetupDefinedType( unsigned short Type, STABCOFFMAP *pMap, unsigned short *DerivedBits, int ExtraLevels ){
int i, Dlimit, Dstart;
unsigned short StabType;
StabType = pMap->StabType; /* save the new type we found earlier */
if ( CopyStabCoffMap( Type, pMap ) != True ) {
fprintf(stderr, "\nCould not find defined type %d", Type );
return(False);
}
pMap->StabType = StabType; /* save the new type we found earlier */
/* Determine existing derived types for base class */
for ( i = 0; i < 6; i++ ) {
if ( (pMap->CoffType & ( 3 << (4 + i + i))) == 0 )
break;
}
Dstart = i;
Dlimit = i + ExtraLevels;
if ( (Dlimit) >= 6 ) {
fprintf(stderr, "\nStab Type %d has too many derived (%d) types!", pMap->StabType, Dlimit );
return(False);
}
/* Add the new derived levels */
for ( ; i < Dlimit; i++ ) {
pMap->CoffType |= ( ( DerivedBits[i - Dstart] & 3) << (4 + i + i) ); /* add in the derived bits */
}
return(True);
}
/****************************************************************************************/
int GetArrayDefinitions( STABCOFFMAP *pMap , char *pMinIndex, char *pMaxIndex, char *pType, unsigned short *DerivedBits, int ExtraLevels ){
int MinIndex, MaxIndex, Result, Size, i;
unsigned short Type;
Result = True;
if ( (*pMinIndex != ';') || (*pMaxIndex != ';') || (*pType != ';') )
Result = False;
/* bump the pointers to the digits */
pMinIndex++;
if ( !isdigit(*pMinIndex) )
Result = False;
pMaxIndex++;
if ( !isdigit(*pMaxIndex) )
Result = False;
pType++;
if ( !isdigit(*pType) )
Result = False;
/* Is syntax ok ? */
if ( Result != True ) {
fprintf(stderr,"\nSyntax error on array parameters %s%s%s", pMinIndex, pMaxIndex, pType );
return(False);
}
MinIndex = atoi(pMinIndex);
MaxIndex = atoi(pMaxIndex);
Type = (unsigned short)atoi(pType);
if ( SetupDefinedType( Type, pMap, DerivedBits, ExtraLevels ) != True )
return( False );
/* Now update the size based on the indicies */
Size = (MaxIndex - MinIndex) + 1;
pMap->ByteSize *= Size;
pMap->Line = ci->CurrentSourceLine;
/* add the dimension information */
for ( i = 5; i >= 0; i-- ) {
if ( pMap->Dimensions[i] != 0 ) {
i++;
pMap->Dimensions[i] = Size;
break;
}
}
return(True);
}
/****************************************************************************************/
int GetInternalType( char *pName, STABCOFFMAP *pMap ){
int n, found, i;
if ( !pName ) {
return(False);
}
found = False;
n = strlen(pName);
/* Find out if it is a local type */
for (i = 0; FundamentalTypes[i].pString != 0; i++) {
if ( !strncmp(pName, FundamentalTypes[i].pString, n) ) {
/* found an internal type */
pMap->CoffType = FundamentalTypes[i].Type;
pMap->ByteSize = FundamentalTypes[i].Size;
found = True;
}
}
return(found);
}
/****************************************************************************************/
int GetSubRangeType( unsigned short Type, STABCOFFMAP *pMap , char *pLow, char *pHigh ){
int Result, i;
long High, Low;
unsigned long Test;
Result = True;
if ( (*pLow != ';') || (*pHigh != ';') || (Type <= 0) )
Result = False;
/* Is syntax ok ? */
if ( Result != True ) {
fprintf(stderr,"\nSyntax error on sub range parameters!" );
return(False);
}
Low = atol(++pLow);
High = atol(++pHigh);
/* Special handling of type void */
if ( (Low == 0) && (High == 0) ) {
/* Declare type void */
pMap->ByteSize =0;
pMap->CoffType = T_VOID;
pMap->Line = ci->CurrentSourceLine;
return(True);
}
if ( (pMap->CoffType = GetCoffType( Type )) != 0 ) {
pMap->ByteSize = GetCoffTypeSize( Type );
} else {
/* Try to base everything off integer */
pMap->ByteSize = FundamentalTypes[T_INT].Size;
}
/* Now calculate the byte size */
if ( High == 0 ) {
pMap->ByteSize = (unsigned short)Low; /* floating point */
} else {
if ( Low == 0 ) {
/* Unsigned */
Test = (unsigned long)High;
} else if ( Low < 0 ) {
/* signed */
Test = (unsigned long)High << 1;
} else {
if ( Low <= High )
Test = (unsigned long)High;
else
Test = (unsigned long)Low;
}
if ( pMap->ByteSize == 0 ) {
fprintf(stderr,"\nType Range Error 1, need previous type %d size!", pMap->CoffType );
return(False);
}
for ( i = 0; i < sizeof(unsigned long); i++ ) {
if ( !(Test & (0xff << (i * 8))) )
break;
}
pMap->ByteSize = i;
}
/* Now determine the best fit based on byte size, compare against IAR Compiler */
if ( pMap->ByteSize == 1 ) {
if ( Low < 0 )
pMap->CoffType = T_CHAR;
else
pMap->CoffType = T_UCHAR;
} else if ( pMap->ByteSize == 2 ) {
if ( Low < 0 )
pMap->CoffType = T_INT;
else
pMap->CoffType = T_UINT;
} else if ( pMap->ByteSize == 4 ) {
if ( Low == 0 )
pMap->CoffType = T_FLOAT;
if ( Low < 0 )
pMap->CoffType = T_LONG;
else
pMap->CoffType = T_ULONG;
} else {
fprintf(stderr,"\nGetSubRangeType failure - byte size %d", pMap->ByteSize );
return(False);
}
return(True);
}
/****************************************************************************************/
int CopyStabCoffMap( unsigned short StabType, STABCOFFMAP *pMap ){
STABCOFFMAP *p;
for ( p = FindFirstListObject( &ci->ListOfTypes ); p != 0; p = FindNextListObject( &ci->ListOfTypes) ) {
if ( p->StabType == StabType ) {
memcpy( pMap, p, sizeof(STABCOFFMAP) );
return(True);
}
}
return( False ); /* Nothing found */
}
/****************************************************************************************/
unsigned short GetCoffType( unsigned short StabType ){
STABCOFFMAP *p;
for ( p = FindFirstListObject( &ci->ListOfTypes ); p != 0; p = FindNextListObject( &ci->ListOfTypes) ) {
if ( p->StabType == StabType )
return( p->CoffType );
}
return( 0 ); /* Nothing found */
}
/****************************************************************************************/
unsigned short GetCoffTypeSize( unsigned short StabType ){
STABCOFFMAP *p;
for ( p = FindFirstListObject( &ci->ListOfTypes ); p != 0; p = FindNextListObject( &ci->ListOfTypes) ) {
if ( p->StabType == StabType )
return( p->ByteSize );
}
return( 0 ); /* Nothing found */
}
/****************************************************************************************/
int GetDigitLength( char *p ){
int i;
if ( p == 0 )
return(0);
for ( i = 0; (*p != 0) && ( *p >= '0' ) && ( *p <= '9' ); i++ );
return( i );
}
/****************************************************************************************/
int GetStringDelimiters( char *pString, char **pTokens, int MaxTokens ){
int i;
char *p;
p = pString;
if ( !p )
return( 0 );
for ( i = 0; i < MaxTokens; i++ ) {
while ( True ) {
if ( (*p == ':') || (*p == ';') || (*p == '=') || (*p == ',') || (*p == '"') || (*p == 0 ) ) {
*(pTokens + i) = p; /* Remember this location */
p++;
if ( *p == 0 )
return( i );
break;
}
p++;
}
}
return( i );
}
/****************************************************************************************/
int IsTypeArray( unsigned short CoffType ){
int Result;
Result = False;
if ( (CoffType & (DT_ARY << 4 )) == (DT_ARY << 4 ) )
Result = True;
if ( (CoffType & (DT_ARY << 6 )) == (DT_ARY << 6 ) )
Result = True;
if ( (CoffType & (DT_ARY << 8 )) == (DT_ARY << 8 ) )
Result = True;
if ( (CoffType & (DT_ARY << 10 )) == (DT_ARY << 10 ) )
Result = True;
if ( (CoffType & (DT_ARY << 12 )) == (DT_ARY << 12 ) )
Result = True;
if ( (CoffType & (DT_ARY << 14 )) == (DT_ARY << 14 ) )
Result = True;
return(Result);
}
/****************************************************************************************/
void AddArrayAuxInfo( union auxent *pAux, unsigned short SymbolIndex, STABCOFFMAP *pMap ){
int i;
pAux->x_sym.x_tagndx = SymbolIndex; /* point to the .bf entry index */
pAux->x_sym.x_misc.x_lnsz.x_lnno = pMap->Line;
pAux->x_sym.x_misc.x_lnsz.x_size = pMap->ByteSize;
for ( i = 0; i < 4; i++ )
pAux->x_sym.x_fcnary.x_ary.x_dimen[i] = pMap->Dimensions[i];
}
/****************************************************************************************/
int AddNameToEntry( char *pName, struct syment *pEntry ) {
int n;
char *p;
n = strlen( pName ); /* see if it's 8 bytes or less */
if ( n <= 8 ) {
strncpy( pEntry->n_name, pName, 8 );
} else {
/* point to current offset in string table */
pEntry->n_offset = ci->ListOfStrings.TotalBytes;
/* Allocate string table entry */
if ( (p = (char *)AllocateListObject( &ci->ListOfStrings, n + 1 )) == 0 ) {
return(0);
}
strcpy( p, pName );
}
return(n); /* return size of string */
}
/****************************************************************************************/
char *SkipPastDigits( char *p ){
if ( !p )
return(p);
/* if ( *p == 0 ); */ /* JEG 5-01-03 */
if ( *p == 0 )
return(p); /* This line s/b indented JEG */
for ( p--; (*p >= '0') && (*p <= '9') && (*p != 0); p-- );
return(p);
}
/****************************************************************************************/
/****************************************************************************************/
/****************************************************************************************/
/* List management routines */
/****************************************************************************************/
/****************************************************************************************/
/****************************************************************************************/
/****************************************************************************************/
void InitializeList( LISTNODEHEAD *pHead ){
pHead->Node.Next = &pHead->Node;
pHead->Node.Last = &pHead->Node;
pHead->TotalBytes = 0;
pHead->TotalItems = 0;
pHead->current = &pHead->Node;
return;
}
/****************************************************************************************/
void *AllocateTwoListObjects( LISTNODEHEAD *pHead, int size ){
void *p;
if ( (p = AllocateListObject( pHead, size ) ) )
pHead->TotalItems++; /* already incremented once in addtolist */
return( p );
}
/****************************************************************************************/
void *AllocateListObject( LISTNODEHEAD *pHead, int size ){
void *pObject;
LISTNODE *pNode;
if ( (pObject = calloc( 1, size )) != 0 ) {
if ( !(pNode = AddListObject( pHead, pObject, size )) ) {
free( pObject );
pObject = 0;
}
}
return( pObject );
}
/****************************************************************************************/
LISTNODE *AddListObject(LISTNODEHEAD *pHead, void *pObject, int size ){
LISTNODE *pNode;
if ( (pNode = calloc( 1, sizeof(LISTNODE) )) != 0 ) {
pNode->pObject = pObject;
pNode->Size = size;
pNode->FileNumber = ci->CurrentFileNumber;
AddNodeToList( pHead, pNode );
}
return( pNode );
}
/****************************************************************************************/
LISTNODE *AllocateListNode( void *pObject, int size ){
LISTNODE *pNew;
if ( (pNew = calloc( 1, sizeof( LISTNODE ) ) ) != 0 ) {
/* Then we initialize the node */
pNew->pObject = pObject;
pNew->Size = size;
pNew->FileNumber = ci->CurrentFileNumber;
}
return(pNew);
}
/****************************************************************************************/
void AddNodeToList( LISTNODEHEAD *pHead, LISTNODE *pNode ){
LISTNODE *p;
p = &pHead->Node;
pNode->Next = p->Last->Next;
p->Last->Next = pNode;
pNode->Last = p->Last;
p->Last = pNode;
/* and update current size of data contained in the list */
pHead->TotalBytes += pNode->Size;
pHead->TotalItems++;
}
/****************************************************************************************/
void RemoveNodeFromList( LISTNODEHEAD *pHead, LISTNODE *pNode ){
pNode->Last->Next = pNode->Next;
pNode->Next->Last = pNode->Last;
pHead->TotalBytes -= pNode->Size;
pHead->TotalItems--;
}
/****************************************************************************************/
void *FindFirstListObject( LISTNODEHEAD *pHead ){
if ( pHead->Node.Next == &pHead->Node )
return(0); /* Nothing in list */
pHead->current = pHead->Node.Next;
return( pHead->current->pObject );
}
/****************************************************************************************/
void *FindNextListObject( LISTNODEHEAD *pHead ){
if ( pHead->current->Next == &pHead->Node )
return( 0 );
pHead->current = pHead->current->Next;
return( pHead->current->pObject );
}
/****************************************************************************************/
LISTNODE *GetCurrentNode( LISTNODEHEAD *pHead ){
return( pHead->current );
}
/****************************************************************************************/
void *GetCurrentListObject( LISTNODEHEAD *pHead ){
return( pHead->current->pObject );
}
/****************************************************************************************/
void *FindLastListObject( LISTNODEHEAD *pHead ){
if ( pHead->Node.Last == &pHead->Node )
return(0); /* Nothing in list */
pHead->current = pHead->Node.Last;
return( pHead->current->pObject );
}
/****************************************************************************************/
void *FindNextLastListObject( LISTNODEHEAD *pHead ){
if ( pHead->current->Last == &pHead->Node )
return( 0 );
pHead->current = pHead->current->Last;
return( pHead->current->pObject );
}
/****************************************************************************************/
void FreeList( LISTNODEHEAD *pHead ){
LISTNODE *pNode;
for ( pNode = pHead->Node.Last; pNode->Next != &pHead->Node; pNode = pHead->Node.Last ) {
RemoveNodeFromList( pHead, pNode );
free( pNode->pObject );
free( pNode );
}
pHead->TotalBytes = 0;
pHead->TotalItems = 0;
pHead->current = &pHead->Node;
}
/****************************************************************************************/
|