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
|
%{ /* deffilep.y - parser for .def files */
/* Copyright (C) 1995-2020 Free Software Foundation, Inc.
This file is part of GNU Binutils.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
MA 02110-1301, USA. */
#include "sysdep.h"
#include "libiberty.h"
#include "safe-ctype.h"
#include "bfd.h"
#include "bfdlink.h"
#include "ld.h"
#include "ldmisc.h"
#include "deffile.h"
#define TRACE 0
#define ROUND_UP(a, b) (((a)+((b)-1))&~((b)-1))
/* Remap normal yacc parser interface names (yyparse, yylex, yyerror, etc),
as well as gratuitiously global symbol names, so we can have multiple
yacc generated parsers in ld. Note that these are only the variables
produced by yacc. If other parser generators (bison, byacc, etc) produce
additional global names that conflict at link time, then those parser
generators need to be fixed instead of adding those names to this list. */
#define yymaxdepth def_maxdepth
#define yyparse def_parse
#define yylex def_lex
#define yyerror def_error
#define yylval def_lval
#define yychar def_char
#define yydebug def_debug
#define yypact def_pact
#define yyr1 def_r1
#define yyr2 def_r2
#define yydef def_def
#define yychk def_chk
#define yypgo def_pgo
#define yyact def_act
#define yyexca def_exca
#define yyerrflag def_errflag
#define yynerrs def_nerrs
#define yyps def_ps
#define yypv def_pv
#define yys def_s
#define yy_yys def_yys
#define yystate def_state
#define yytmp def_tmp
#define yyv def_v
#define yy_yyv def_yyv
#define yyval def_val
#define yylloc def_lloc
#define yyreds def_reds /* With YYDEBUG defined. */
#define yytoks def_toks /* With YYDEBUG defined. */
#define yylhs def_yylhs
#define yylen def_yylen
#define yydefred def_yydefred
#define yydgoto def_yydgoto
#define yysindex def_yysindex
#define yyrindex def_yyrindex
#define yygindex def_yygindex
#define yytable def_yytable
#define yycheck def_yycheck
typedef struct def_pool_str {
struct def_pool_str *next;
char data[1];
} def_pool_str;
static def_pool_str *pool_strs = NULL;
static char *def_pool_alloc (size_t sz);
static char *def_pool_strdup (const char *str);
static void def_pool_free (void);
static void def_description (const char *);
static void def_exports (const char *, const char *, int, int, const char *);
static void def_heapsize (int, int);
static void def_import (const char *, const char *, const char *, const char *,
int, const char *);
static void def_image_name (const char *, bfd_vma, int);
static void def_section (const char *, int);
static void def_section_alt (const char *, const char *);
static void def_stacksize (int, int);
static void def_version (int, int);
static void def_directive (char *);
static void def_aligncomm (char *str, int align);
static int def_parse (void);
static int def_error (const char *);
static int def_lex (void);
static int lex_forced_token = 0;
static const char *lex_parse_string = 0;
static const char *lex_parse_string_end = 0;
%}
%union {
char *id;
const char *id_const;
int number;
bfd_vma vma;
char *digits;
};
%token NAME LIBRARY DESCRIPTION STACKSIZE_K HEAPSIZE CODE DATAU DATAL
%token SECTIONS EXPORTS IMPORTS VERSIONK BASE CONSTANTU CONSTANTL
%token PRIVATEU PRIVATEL ALIGNCOMM
%token READ WRITE EXECUTE SHARED NONAMEU NONAMEL DIRECTIVE EQUAL
%token <id> ID
%token <digits> DIGITS
%type <number> NUMBER
%type <vma> VMA opt_base
%type <digits> opt_digits
%type <number> opt_ordinal
%type <number> attr attr_list opt_number exp_opt_list exp_opt
%type <id> opt_name opt_name2 opt_equal_name anylang_id opt_id
%type <id> opt_equalequal_name
%type <id_const> keyword_as_name
%%
start: start command
| command
;
command:
NAME opt_name opt_base { def_image_name ($2, $3, 0); }
| LIBRARY opt_name opt_base { def_image_name ($2, $3, 1); }
| DESCRIPTION ID { def_description ($2);}
| STACKSIZE_K NUMBER opt_number { def_stacksize ($2, $3);}
| HEAPSIZE NUMBER opt_number { def_heapsize ($2, $3);}
| CODE attr_list { def_section ("CODE", $2);}
| DATAU attr_list { def_section ("DATA", $2);}
| SECTIONS seclist
| EXPORTS explist
| IMPORTS implist
| VERSIONK NUMBER { def_version ($2, 0);}
| VERSIONK NUMBER '.' NUMBER { def_version ($2, $4);}
| DIRECTIVE ID { def_directive ($2);}
| ALIGNCOMM anylang_id ',' NUMBER { def_aligncomm ($2, $4);}
;
explist:
/* EMPTY */
| expline
| explist expline
;
expline:
/* The opt_comma is necessary to support both the usual
DEF file syntax as well as .drectve syntax which
mandates <expsym>,<expoptlist>. */
opt_name2 opt_equal_name opt_ordinal opt_comma exp_opt_list opt_comma opt_equalequal_name
{ def_exports ($1, $2, $3, $5, $7); }
;
exp_opt_list:
/* The opt_comma is necessary to support both the usual
DEF file syntax as well as .drectve syntax which
allows for comma separated opt list. */
exp_opt opt_comma exp_opt_list { $$ = $1 | $3; }
| { $$ = 0; }
;
exp_opt:
NONAMEU { $$ = 1; }
| NONAMEL { $$ = 1; }
| CONSTANTU { $$ = 2; }
| CONSTANTL { $$ = 2; }
| DATAU { $$ = 4; }
| DATAL { $$ = 4; }
| PRIVATEU { $$ = 8; }
| PRIVATEL { $$ = 8; }
;
implist:
implist impline
| impline
;
impline:
ID '=' ID '.' ID '.' ID opt_equalequal_name
{ def_import ($1, $3, $5, $7, -1, $8); }
| ID '=' ID '.' ID '.' NUMBER opt_equalequal_name
{ def_import ($1, $3, $5, 0, $7, $8); }
| ID '=' ID '.' ID opt_equalequal_name
{ def_import ($1, $3, 0, $5, -1, $6); }
| ID '=' ID '.' NUMBER opt_equalequal_name
{ def_import ($1, $3, 0, 0, $5, $6); }
| ID '.' ID '.' ID opt_equalequal_name
{ def_import( 0, $1, $3, $5, -1, $6); }
| ID '.' ID opt_equalequal_name
{ def_import ( 0, $1, 0, $3, -1, $4); }
;
seclist:
seclist secline
| secline
;
secline:
ID attr_list { def_section ($1, $2);}
| ID ID { def_section_alt ($1, $2);}
;
attr_list:
attr_list opt_comma attr { $$ = $1 | $3; }
| attr { $$ = $1; }
;
opt_comma:
','
|
;
opt_number: ',' NUMBER { $$=$2;}
| { $$=-1;}
;
attr:
READ { $$ = 1;}
| WRITE { $$ = 2;}
| EXECUTE { $$=4;}
| SHARED { $$=8;}
;
keyword_as_name: BASE { $$ = "BASE"; }
| CODE { $$ = "CODE"; }
| CONSTANTU { $$ = "CONSTANT"; }
| CONSTANTL { $$ = "constant"; }
| DATAU { $$ = "DATA"; }
| DATAL { $$ = "data"; }
| DESCRIPTION { $$ = "DESCRIPTION"; }
| DIRECTIVE { $$ = "DIRECTIVE"; }
| EXECUTE { $$ = "EXECUTE"; }
| EXPORTS { $$ = "EXPORTS"; }
| HEAPSIZE { $$ = "HEAPSIZE"; }
| IMPORTS { $$ = "IMPORTS"; }
/* Disable LIBRARY keyword as valid symbol-name. This is necessary
for libtool, which places this command after EXPORTS command.
This behavior is illegal by specification, but sadly required by
by compatibility reasons.
See PR binutils/13710
| LIBRARY { $$ = "LIBRARY"; } */
| NAME { $$ = "NAME"; }
| NONAMEU { $$ = "NONAME"; }
| NONAMEL { $$ = "noname"; }
| PRIVATEU { $$ = "PRIVATE"; }
| PRIVATEL { $$ = "private"; }
| READ { $$ = "READ"; }
| SHARED { $$ = "SHARED"; }
| STACKSIZE_K { $$ = "STACKSIZE"; }
| VERSIONK { $$ = "VERSION"; }
| WRITE { $$ = "WRITE"; }
;
opt_name2: ID { $$ = $1; }
| '.' keyword_as_name
{
char *name = xmalloc (strlen ($2) + 2);
sprintf (name, ".%s", $2);
$$ = name;
}
| '.' opt_name2
{
char *name = def_pool_alloc (strlen ($2) + 2);
sprintf (name, ".%s", $2);
$$ = name;
}
| keyword_as_name '.' opt_name2
{
char *name = def_pool_alloc (strlen ($1) + 1 + strlen ($3) + 1);
sprintf (name, "%s.%s", $1, $3);
$$ = name;
}
| ID '.' opt_name2
{
char *name = def_pool_alloc (strlen ($1) + 1 + strlen ($3) + 1);
sprintf (name, "%s.%s", $1, $3);
$$ = name;
}
;
opt_name: opt_name2 { $$ = $1; }
| { $$ = ""; }
;
opt_equalequal_name: EQUAL ID { $$ = $2; }
| { $$ = 0; }
;
opt_ordinal:
'@' NUMBER { $$ = $2;}
| { $$ = -1;}
;
opt_equal_name:
'=' opt_name2 { $$ = $2; }
| { $$ = 0; }
;
opt_base: BASE '=' VMA { $$ = $3;}
| { $$ = (bfd_vma) -1;}
;
anylang_id: ID { $$ = $1; }
| '.' ID
{
char *id = def_pool_alloc (strlen ($2) + 2);
sprintf (id, ".%s", $2);
$$ = id;
}
| anylang_id '.' opt_digits opt_id
{
char *id = def_pool_alloc (strlen ($1) + 1 + strlen ($3) + strlen ($4) + 1);
sprintf (id, "%s.%s%s", $1, $3, $4);
$$ = id;
}
;
opt_digits: DIGITS { $$ = $1; }
| { $$ = ""; }
;
opt_id: ID { $$ = $1; }
| { $$ = ""; }
;
NUMBER: DIGITS { $$ = strtoul ($1, 0, 0); }
;
VMA: DIGITS { $$ = (bfd_vma) strtoull ($1, 0, 0); }
%%
/*****************************************************************************
API
*****************************************************************************/
static FILE *the_file;
static const char *def_filename;
static int linenumber;
static def_file *def;
static int saw_newline;
struct directive
{
struct directive *next;
char *name;
int len;
};
static struct directive *directives = 0;
def_file *
def_file_empty (void)
{
def_file *rv = xmalloc (sizeof (def_file));
memset (rv, 0, sizeof (def_file));
rv->is_dll = -1;
rv->base_address = (bfd_vma) -1;
rv->stack_reserve = rv->stack_commit = -1;
rv->heap_reserve = rv->heap_commit = -1;
rv->version_major = rv->version_minor = -1;
return rv;
}
def_file *
def_file_parse (const char *filename, def_file *add_to)
{
struct directive *d;
the_file = fopen (filename, "r");
def_filename = filename;
linenumber = 1;
if (!the_file)
{
perror (filename);
return 0;
}
if (add_to)
{
def = add_to;
}
else
{
def = def_file_empty ();
}
saw_newline = 1;
if (def_parse ())
{
def_file_free (def);
fclose (the_file);
def_pool_free ();
return 0;
}
fclose (the_file);
while ((d = directives) != NULL)
{
#if TRACE
printf ("Adding directive %08x `%s'\n", d->name, d->name);
#endif
def_file_add_directive (def, d->name, d->len);
directives = d->next;
free (d->name);
free (d);
}
def_pool_free ();
return def;
}
void
def_file_free (def_file *fdef)
{
int i;
if (!fdef)
return;
free (fdef->name);
free (fdef->description);
if (fdef->section_defs)
{
for (i = 0; i < fdef->num_section_defs; i++)
{
free (fdef->section_defs[i].name);
free (fdef->section_defs[i].class);
}
free (fdef->section_defs);
}
if (fdef->exports)
{
for (i = 0; i < fdef->num_exports; i++)
{
if (fdef->exports[i].internal_name != fdef->exports[i].name)
free (fdef->exports[i].internal_name);
free (fdef->exports[i].name);
free (fdef->exports[i].its_name);
}
free (fdef->exports);
}
if (fdef->imports)
{
for (i = 0; i < fdef->num_imports; i++)
{
if (fdef->imports[i].internal_name != fdef->imports[i].name)
free (fdef->imports[i].internal_name);
free (fdef->imports[i].name);
free (fdef->imports[i].its_name);
}
free (fdef->imports);
}
while (fdef->modules)
{
def_file_module *m = fdef->modules;
fdef->modules = fdef->modules->next;
free (m);
}
while (fdef->aligncomms)
{
def_file_aligncomm *c = fdef->aligncomms;
fdef->aligncomms = fdef->aligncomms->next;
free (c->symbol_name);
free (c);
}
free (fdef);
}
#ifdef DEF_FILE_PRINT
void
def_file_print (FILE *file, def_file *fdef)
{
int i;
fprintf (file, ">>>> def_file at 0x%08x\n", fdef);
if (fdef->name)
fprintf (file, " name: %s\n", fdef->name ? fdef->name : "(unspecified)");
if (fdef->is_dll != -1)
fprintf (file, " is dll: %s\n", fdef->is_dll ? "yes" : "no");
if (fdef->base_address != (bfd_vma) -1)
{
fprintf (file, " base address: 0x");
fprintf_vma (file, fdef->base_address);
fprintf (file, "\n");
}
if (fdef->description)
fprintf (file, " description: `%s'\n", fdef->description);
if (fdef->stack_reserve != -1)
fprintf (file, " stack reserve: 0x%08x\n", fdef->stack_reserve);
if (fdef->stack_commit != -1)
fprintf (file, " stack commit: 0x%08x\n", fdef->stack_commit);
if (fdef->heap_reserve != -1)
fprintf (file, " heap reserve: 0x%08x\n", fdef->heap_reserve);
if (fdef->heap_commit != -1)
fprintf (file, " heap commit: 0x%08x\n", fdef->heap_commit);
if (fdef->num_section_defs > 0)
{
fprintf (file, " section defs:\n");
for (i = 0; i < fdef->num_section_defs; i++)
{
fprintf (file, " name: `%s', class: `%s', flags:",
fdef->section_defs[i].name, fdef->section_defs[i].class);
if (fdef->section_defs[i].flag_read)
fprintf (file, " R");
if (fdef->section_defs[i].flag_write)
fprintf (file, " W");
if (fdef->section_defs[i].flag_execute)
fprintf (file, " X");
if (fdef->section_defs[i].flag_shared)
fprintf (file, " S");
fprintf (file, "\n");
}
}
if (fdef->num_exports > 0)
{
fprintf (file, " exports:\n");
for (i = 0; i < fdef->num_exports; i++)
{
fprintf (file, " name: `%s', int: `%s', ordinal: %d, flags:",
fdef->exports[i].name, fdef->exports[i].internal_name,
fdef->exports[i].ordinal);
if (fdef->exports[i].flag_private)
fprintf (file, " P");
if (fdef->exports[i].flag_constant)
fprintf (file, " C");
if (fdef->exports[i].flag_noname)
fprintf (file, " N");
if (fdef->exports[i].flag_data)
fprintf (file, " D");
fprintf (file, "\n");
}
}
if (fdef->num_imports > 0)
{
fprintf (file, " imports:\n");
for (i = 0; i < fdef->num_imports; i++)
{
fprintf (file, " int: %s, from: `%s', name: `%s', ordinal: %d\n",
fdef->imports[i].internal_name,
fdef->imports[i].module,
fdef->imports[i].name,
fdef->imports[i].ordinal);
}
}
if (fdef->version_major != -1)
fprintf (file, " version: %d.%d\n", fdef->version_major, fdef->version_minor);
fprintf (file, "<<<< def_file at 0x%08x\n", fdef);
}
#endif
/* Helper routine to check for identity of string pointers,
which might be NULL. */
static int
are_names_equal (const char *s1, const char *s2)
{
if (!s1 && !s2)
return 0;
if (!s1 || !s2)
return (!s1 ? -1 : 1);
return strcmp (s1, s2);
}
static int
cmp_export_elem (const def_file_export *e, const char *ex_name,
const char *in_name, const char *its_name,
int ord)
{
int r;
if ((r = are_names_equal (ex_name, e->name)) != 0)
return r;
if ((r = are_names_equal (in_name, e->internal_name)) != 0)
return r;
if ((r = are_names_equal (its_name, e->its_name)) != 0)
return r;
return (ord - e->ordinal);
}
/* Search the position of the identical element, or returns the position
of the next higher element. If last valid element is smaller, then MAX
is returned. */
static int
find_export_in_list (def_file_export *b, int max,
const char *ex_name, const char *in_name,
const char *its_name, int ord, int *is_ident)
{
int e, l, r, p;
*is_ident = 0;
if (!max)
return 0;
if ((e = cmp_export_elem (b, ex_name, in_name, its_name, ord)) <= 0)
{
if (!e)
*is_ident = 1;
return 0;
}
if (max == 1)
return 1;
if ((e = cmp_export_elem (b + (max - 1), ex_name, in_name, its_name, ord)) > 0)
return max;
else if (!e || max == 2)
{
if (!e)
*is_ident = 1;
return max - 1;
}
l = 0; r = max - 1;
while (l < r)
{
p = (l + r) / 2;
e = cmp_export_elem (b + p, ex_name, in_name, its_name, ord);
if (!e)
{
*is_ident = 1;
return p;
}
else if (e < 0)
r = p - 1;
else if (e > 0)
l = p + 1;
}
if ((e = cmp_export_elem (b + l, ex_name, in_name, its_name, ord)) > 0)
++l;
else if (!e)
*is_ident = 1;
return l;
}
def_file_export *
def_file_add_export (def_file *fdef,
const char *external_name,
const char *internal_name,
int ordinal,
const char *its_name,
int *is_dup)
{
def_file_export *e;
int pos;
int max_exports = ROUND_UP(fdef->num_exports, 32);
if (internal_name && !external_name)
external_name = internal_name;
if (external_name && !internal_name)
internal_name = external_name;
/* We need to avoid duplicates. */
*is_dup = 0;
pos = find_export_in_list (fdef->exports, fdef->num_exports,
external_name, internal_name,
its_name, ordinal, is_dup);
if (*is_dup != 0)
return (fdef->exports + pos);
if (fdef->num_exports >= max_exports)
{
max_exports = ROUND_UP(fdef->num_exports + 1, 32);
if (fdef->exports)
fdef->exports = xrealloc (fdef->exports,
max_exports * sizeof (def_file_export));
else
fdef->exports = xmalloc (max_exports * sizeof (def_file_export));
}
e = fdef->exports + pos;
if (pos != fdef->num_exports)
memmove (&e[1], e, (sizeof (def_file_export) * (fdef->num_exports - pos)));
memset (e, 0, sizeof (def_file_export));
e->name = xstrdup (external_name);
e->internal_name = xstrdup (internal_name);
e->its_name = (its_name ? xstrdup (its_name) : NULL);
e->ordinal = ordinal;
fdef->num_exports++;
return e;
}
def_file_module *
def_get_module (def_file *fdef, const char *name)
{
def_file_module *s;
for (s = fdef->modules; s; s = s->next)
if (strcmp (s->name, name) == 0)
return s;
return NULL;
}
static def_file_module *
def_stash_module (def_file *fdef, const char *name)
{
def_file_module *s;
if ((s = def_get_module (fdef, name)) != NULL)
return s;
s = xmalloc (sizeof (def_file_module) + strlen (name));
s->next = fdef->modules;
fdef->modules = s;
s->user_data = 0;
strcpy (s->name, name);
return s;
}
static int
cmp_import_elem (const def_file_import *e, const char *ex_name,
const char *in_name, const char *module,
int ord)
{
int r;
if ((r = are_names_equal (module, (e->module ? e->module->name : NULL))))
return r;
if ((r = are_names_equal (ex_name, e->name)) != 0)
return r;
if ((r = are_names_equal (in_name, e->internal_name)) != 0)
return r;
if (ord != e->ordinal)
return (ord < e->ordinal ? -1 : 1);
return 0;
}
/* Search the position of the identical element, or returns the position
of the next higher element. If last valid element is smaller, then MAX
is returned. */
static int
find_import_in_list (def_file_import *b, int max,
const char *ex_name, const char *in_name,
const char *module, int ord, int *is_ident)
{
int e, l, r, p;
*is_ident = 0;
if (!max)
return 0;
if ((e = cmp_import_elem (b, ex_name, in_name, module, ord)) <= 0)
{
if (!e)
*is_ident = 1;
return 0;
}
if (max == 1)
return 1;
if ((e = cmp_import_elem (b + (max - 1), ex_name, in_name, module, ord)) > 0)
return max;
else if (!e || max == 2)
{
if (!e)
*is_ident = 1;
return max - 1;
}
l = 0; r = max - 1;
while (l < r)
{
p = (l + r) / 2;
e = cmp_import_elem (b + p, ex_name, in_name, module, ord);
if (!e)
{
*is_ident = 1;
return p;
}
else if (e < 0)
r = p - 1;
else if (e > 0)
l = p + 1;
}
if ((e = cmp_import_elem (b + l, ex_name, in_name, module, ord)) > 0)
++l;
else if (!e)
*is_ident = 1;
return l;
}
static void
fill_in_import (def_file_import *i,
const char *name,
def_file_module *module,
int ordinal,
const char *internal_name,
const char *its_name)
{
memset (i, 0, sizeof (def_file_import));
if (name)
i->name = xstrdup (name);
i->module = module;
i->ordinal = ordinal;
if (internal_name)
i->internal_name = xstrdup (internal_name);
else
i->internal_name = i->name;
i->its_name = (its_name ? xstrdup (its_name) : NULL);
}
def_file_import *
def_file_add_import (def_file *fdef,
const char *name,
const char *module,
int ordinal,
const char *internal_name,
const char *its_name,
int *is_dup)
{
def_file_import *i;
int pos;
int max_imports = ROUND_UP (fdef->num_imports, 16);
/* We need to avoid here duplicates. */
*is_dup = 0;
pos = find_import_in_list (fdef->imports, fdef->num_imports,
name,
(!internal_name ? name : internal_name),
module, ordinal, is_dup);
if (*is_dup != 0)
return fdef->imports + pos;
if (fdef->num_imports >= max_imports)
{
max_imports = ROUND_UP (fdef->num_imports+1, 16);
if (fdef->imports)
fdef->imports = xrealloc (fdef->imports,
max_imports * sizeof (def_file_import));
else
fdef->imports = xmalloc (max_imports * sizeof (def_file_import));
}
i = fdef->imports + pos;
if (pos != fdef->num_imports)
memmove (i + 1, i, sizeof (def_file_import) * (fdef->num_imports - pos));
fill_in_import (i, name, def_stash_module (fdef, module), ordinal,
internal_name, its_name);
fdef->num_imports++;
return i;
}
int
def_file_add_import_from (def_file *fdef,
int num_imports,
const char *name,
const char *module,
int ordinal,
const char *internal_name,
const char *its_name ATTRIBUTE_UNUSED)
{
def_file_import *i;
int is_dup;
int pos;
int max_imports = ROUND_UP (fdef->num_imports, 16);
/* We need to avoid here duplicates. */
is_dup = 0;
pos = find_import_in_list (fdef->imports, fdef->num_imports,
name, internal_name ? internal_name : name,
module, ordinal, &is_dup);
if (is_dup != 0)
return -1;
if (fdef->imports && pos != fdef->num_imports)
{
i = fdef->imports + pos;
if (i->module && strcmp (i->module->name, module) == 0)
return -1;
}
if (fdef->num_imports + num_imports - 1 >= max_imports)
{
max_imports = ROUND_UP (fdef->num_imports + num_imports, 16);
if (fdef->imports)
fdef->imports = xrealloc (fdef->imports,
max_imports * sizeof (def_file_import));
else
fdef->imports = xmalloc (max_imports * sizeof (def_file_import));
}
i = fdef->imports + pos;
if (pos != fdef->num_imports)
memmove (i + num_imports, i,
sizeof (def_file_import) * (fdef->num_imports - pos));
return pos;
}
def_file_import *
def_file_add_import_at (def_file *fdef,
int pos,
const char *name,
const char *module,
int ordinal,
const char *internal_name,
const char *its_name)
{
def_file_import *i = fdef->imports + pos;
fill_in_import (i, name, def_stash_module (fdef, module), ordinal,
internal_name, its_name);
fdef->num_imports++;
return i;
}
struct
{
char *param;
int token;
}
diropts[] =
{
{ "-heap", HEAPSIZE },
{ "-stack", STACKSIZE_K },
{ "-attr", SECTIONS },
{ "-export", EXPORTS },
{ "-aligncomm", ALIGNCOMM },
{ 0, 0 }
};
void
def_file_add_directive (def_file *my_def, const char *param, int len)
{
def_file *save_def = def;
const char *pend = param + len;
char * tend = (char *) param;
int i;
def = my_def;
while (param < pend)
{
while (param < pend
&& (ISSPACE (*param) || *param == '\n' || *param == 0))
param++;
if (param == pend)
break;
/* Scan forward until we encounter any of:
- the end of the buffer
- the start of a new option
- a newline separating options
- a NUL separating options. */
for (tend = (char *) (param + 1);
(tend < pend
&& !(ISSPACE (tend[-1]) && *tend == '-')
&& *tend != '\n' && *tend != 0);
tend++)
;
for (i = 0; diropts[i].param; i++)
{
len = strlen (diropts[i].param);
if (tend - param >= len
&& strncmp (param, diropts[i].param, len) == 0
&& (param[len] == ':' || param[len] == ' '))
{
lex_parse_string_end = tend;
lex_parse_string = param + len + 1;
lex_forced_token = diropts[i].token;
saw_newline = 0;
if (def_parse ())
continue;
break;
}
}
if (!diropts[i].param)
{
if (tend < pend)
{
char saved;
saved = * tend;
* tend = 0;
/* xgettext:c-format */
einfo (_("Warning: .drectve `%s' unrecognized\n"), param);
* tend = saved;
}
else
{
einfo (_("Warning: corrupt .drectve at end of def file\n"));
}
}
lex_parse_string = 0;
param = tend;
}
def = save_def;
def_pool_free ();
}
/* Parser Callbacks. */
static void
def_image_name (const char *name, bfd_vma base, int is_dll)
{
/* If a LIBRARY or NAME statement is specified without a name, there is nothing
to do here. We retain the output filename specified on command line. */
if (*name)
{
const char* image_name = lbasename (name);
if (image_name != name)
einfo ("%s:%d: Warning: path components stripped from %s, '%s'\n",
def_filename, linenumber, is_dll ? "LIBRARY" : "NAME",
name);
free (def->name);
/* Append the default suffix, if none specified. */
if (strchr (image_name, '.') == 0)
{
const char * suffix = is_dll ? ".dll" : ".exe";
def->name = xmalloc (strlen (image_name) + strlen (suffix) + 1);
sprintf (def->name, "%s%s", image_name, suffix);
}
else
def->name = xstrdup (image_name);
}
/* Honor a BASE address statement, even if LIBRARY string is empty. */
def->base_address = base;
def->is_dll = is_dll;
}
static void
def_description (const char *text)
{
int len = def->description ? strlen (def->description) : 0;
len += strlen (text) + 1;
if (def->description)
{
def->description = xrealloc (def->description, len);
strcat (def->description, text);
}
else
{
def->description = xmalloc (len);
strcpy (def->description, text);
}
}
static void
def_stacksize (int reserve, int commit)
{
def->stack_reserve = reserve;
def->stack_commit = commit;
}
static void
def_heapsize (int reserve, int commit)
{
def->heap_reserve = reserve;
def->heap_commit = commit;
}
static void
def_section (const char *name, int attr)
{
def_file_section *s;
int max_sections = ROUND_UP (def->num_section_defs, 4);
if (def->num_section_defs >= max_sections)
{
max_sections = ROUND_UP (def->num_section_defs+1, 4);
if (def->section_defs)
def->section_defs = xrealloc (def->section_defs,
max_sections * sizeof (def_file_import));
else
def->section_defs = xmalloc (max_sections * sizeof (def_file_import));
}
s = def->section_defs + def->num_section_defs;
memset (s, 0, sizeof (def_file_section));
s->name = xstrdup (name);
if (attr & 1)
s->flag_read = 1;
if (attr & 2)
s->flag_write = 1;
if (attr & 4)
s->flag_execute = 1;
if (attr & 8)
s->flag_shared = 1;
def->num_section_defs++;
}
static void
def_section_alt (const char *name, const char *attr)
{
int aval = 0;
for (; *attr; attr++)
{
switch (*attr)
{
case 'R':
case 'r':
aval |= 1;
break;
case 'W':
case 'w':
aval |= 2;
break;
case 'X':
case 'x':
aval |= 4;
break;
case 'S':
case 's':
aval |= 8;
break;
}
}
def_section (name, aval);
}
static void
def_exports (const char *external_name,
const char *internal_name,
int ordinal,
int flags,
const char *its_name)
{
def_file_export *dfe;
int is_dup = 0;
if (!internal_name && external_name)
internal_name = external_name;
#if TRACE
printf ("def_exports, ext=%s int=%s\n", external_name, internal_name);
#endif
dfe = def_file_add_export (def, external_name, internal_name, ordinal,
its_name, &is_dup);
/* We might check here for flag redefinition and warn. For now we
ignore duplicates silently. */
if (is_dup)
return;
if (flags & 1)
dfe->flag_noname = 1;
if (flags & 2)
dfe->flag_constant = 1;
if (flags & 4)
dfe->flag_data = 1;
if (flags & 8)
dfe->flag_private = 1;
}
static void
def_import (const char *internal_name,
const char *module,
const char *dllext,
const char *name,
int ordinal,
const char *its_name)
{
char *buf = 0;
const char *ext = dllext ? dllext : "dll";
int is_dup = 0;
buf = xmalloc (strlen (module) + strlen (ext) + 2);
sprintf (buf, "%s.%s", module, ext);
module = buf;
def_file_add_import (def, name, module, ordinal, internal_name, its_name,
&is_dup);
free (buf);
}
static void
def_version (int major, int minor)
{
def->version_major = major;
def->version_minor = minor;
}
static void
def_directive (char *str)
{
struct directive *d = xmalloc (sizeof (struct directive));
d->next = directives;
directives = d;
d->name = xstrdup (str);
d->len = strlen (str);
}
static void
def_aligncomm (char *str, int align)
{
def_file_aligncomm *c, *p;
p = NULL;
c = def->aligncomms;
while (c != NULL)
{
int e = strcmp (c->symbol_name, str);
if (!e)
{
/* Not sure if we want to allow here duplicates with
different alignments, but for now we keep them. */
e = (int) c->alignment - align;
if (!e)
return;
}
if (e > 0)
break;
c = (p = c)->next;
}
c = xmalloc (sizeof (def_file_aligncomm));
c->symbol_name = xstrdup (str);
c->alignment = (unsigned int) align;
if (!p)
{
c->next = def->aligncomms;
def->aligncomms = c;
}
else
{
c->next = p->next;
p->next = c;
}
}
static int
def_error (const char *err)
{
einfo ("%P: %s:%d: %s\n",
def_filename ? def_filename : "<unknown-file>", linenumber, err);
return 0;
}
/* Lexical Scanner. */
#undef TRACE
#define TRACE 0
/* Never freed, but always reused as needed, so no real leak. */
static char *buffer = 0;
static int buflen = 0;
static int bufptr = 0;
static void
put_buf (char c)
{
if (bufptr == buflen)
{
buflen += 50; /* overly reasonable, eh? */
if (buffer)
buffer = xrealloc (buffer, buflen + 1);
else
buffer = xmalloc (buflen + 1);
}
buffer[bufptr++] = c;
buffer[bufptr] = 0; /* not optimal, but very convenient. */
}
static struct
{
char *name;
int token;
}
tokens[] =
{
{ "BASE", BASE },
{ "CODE", CODE },
{ "CONSTANT", CONSTANTU },
{ "constant", CONSTANTL },
{ "DATA", DATAU },
{ "data", DATAL },
{ "DESCRIPTION", DESCRIPTION },
{ "DIRECTIVE", DIRECTIVE },
{ "EXECUTE", EXECUTE },
{ "EXPORTS", EXPORTS },
{ "HEAPSIZE", HEAPSIZE },
{ "IMPORTS", IMPORTS },
{ "LIBRARY", LIBRARY },
{ "NAME", NAME },
{ "NONAME", NONAMEU },
{ "noname", NONAMEL },
{ "PRIVATE", PRIVATEU },
{ "private", PRIVATEL },
{ "READ", READ },
{ "SECTIONS", SECTIONS },
{ "SEGMENTS", SECTIONS },
{ "SHARED", SHARED },
{ "STACKSIZE", STACKSIZE_K },
{ "VERSION", VERSIONK },
{ "WRITE", WRITE },
{ 0, 0 }
};
static int
def_getc (void)
{
int rv;
if (lex_parse_string)
{
if (lex_parse_string >= lex_parse_string_end)
rv = EOF;
else
rv = *lex_parse_string++;
}
else
{
rv = fgetc (the_file);
}
if (rv == '\n')
saw_newline = 1;
return rv;
}
static int
def_ungetc (int c)
{
if (lex_parse_string)
{
lex_parse_string--;
return c;
}
else
return ungetc (c, the_file);
}
static int
def_lex (void)
{
int c, i, q;
if (lex_forced_token)
{
i = lex_forced_token;
lex_forced_token = 0;
#if TRACE
printf ("lex: forcing token %d\n", i);
#endif
return i;
}
c = def_getc ();
/* Trim leading whitespace. */
while (c != EOF && (c == ' ' || c == '\t') && saw_newline)
c = def_getc ();
if (c == EOF)
{
#if TRACE
printf ("lex: EOF\n");
#endif
return 0;
}
if (saw_newline && c == ';')
{
do
{
c = def_getc ();
}
while (c != EOF && c != '\n');
if (c == '\n')
return def_lex ();
return 0;
}
/* Must be something else. */
saw_newline = 0;
if (ISDIGIT (c))
{
bufptr = 0;
while (c != EOF && (ISXDIGIT (c) || (c == 'x')))
{
put_buf (c);
c = def_getc ();
}
if (c != EOF)
def_ungetc (c);
yylval.digits = def_pool_strdup (buffer);
#if TRACE
printf ("lex: `%s' returns DIGITS\n", buffer);
#endif
return DIGITS;
}
if (ISALPHA (c) || strchr ("$:-_?@", c))
{
bufptr = 0;
q = c;
put_buf (c);
c = def_getc ();
if (q == '@')
{
if (ISBLANK (c) ) /* '@' followed by whitespace. */
return (q);
else if (ISDIGIT (c)) /* '@' followed by digit. */
{
def_ungetc (c);
return (q);
}
#if TRACE
printf ("lex: @ returns itself\n");
#endif
}
while (c != EOF && (ISALNUM (c) || strchr ("$:-_?/@<>", c)))
{
put_buf (c);
c = def_getc ();
}
if (c != EOF)
def_ungetc (c);
if (ISALPHA (q)) /* Check for tokens. */
{
for (i = 0; tokens[i].name; i++)
if (strcmp (tokens[i].name, buffer) == 0)
{
#if TRACE
printf ("lex: `%s' is a string token\n", buffer);
#endif
return tokens[i].token;
}
}
#if TRACE
printf ("lex: `%s' returns ID\n", buffer);
#endif
yylval.id = def_pool_strdup (buffer);
return ID;
}
if (c == '\'' || c == '"')
{
q = c;
c = def_getc ();
bufptr = 0;
while (c != EOF && c != q)
{
put_buf (c);
c = def_getc ();
}
yylval.id = def_pool_strdup (buffer);
#if TRACE
printf ("lex: `%s' returns ID\n", buffer);
#endif
return ID;
}
if ( c == '=')
{
c = def_getc ();
if (c == '=')
{
#if TRACE
printf ("lex: `==' returns EQUAL\n");
#endif
return EQUAL;
}
def_ungetc (c);
#if TRACE
printf ("lex: `=' returns itself\n");
#endif
return '=';
}
if (c == '.' || c == ',')
{
#if TRACE
printf ("lex: `%c' returns itself\n", c);
#endif
return c;
}
if (c == '\n')
{
linenumber++;
saw_newline = 1;
}
/*printf ("lex: 0x%02x ignored\n", c); */
return def_lex ();
}
static char *
def_pool_alloc (size_t sz)
{
def_pool_str *e;
e = (def_pool_str *) xmalloc (sizeof (def_pool_str) + sz);
e->next = pool_strs;
pool_strs = e;
return e->data;
}
static char *
def_pool_strdup (const char *str)
{
char *s;
size_t len;
if (!str)
return NULL;
len = strlen (str) + 1;
s = def_pool_alloc (len);
memcpy (s, str, len);
return s;
}
static void
def_pool_free (void)
{
def_pool_str *p;
while ((p = pool_strs) != NULL)
{
pool_strs = p->next;
free (p);
}
}
|