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
|
/*
* Spec file parser
*
* Copyright 1993 Robert J. Amstadt
* Copyright 1995 Martin von Loewis
* Copyright 1995, 1996, 1997, 2004 Alexandre Julliard
* Copyright 1997 Eric Youngdale
* Copyright 1999 Ulrich Weigand
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include "wine/port.h"
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "build.h"
int current_line = 0;
static char ParseBuffer[512];
static char TokenBuffer[512];
static char *ParseNext = ParseBuffer;
static FILE *input_file;
static const char *separator_chars;
static const char *comment_chars;
/* valid characters in ordinal names */
static const char valid_ordname_chars[] = "/$:-_@?<>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
static const char * const TypeNames[TYPE_NBTYPES] =
{
"variable", /* TYPE_VARIABLE */
"pascal", /* TYPE_PASCAL */
"equate", /* TYPE_ABS */
"stub", /* TYPE_STUB */
"stdcall", /* TYPE_STDCALL */
"cdecl", /* TYPE_CDECL */
"varargs", /* TYPE_VARARGS */
"thiscall", /* TYPE_THISCALL */
"extern" /* TYPE_EXTERN */
};
static const char * const FlagNames[] =
{
"norelay", /* FLAG_NORELAY */
"noname", /* FLAG_NONAME */
"ret16", /* FLAG_RET16 */
"ret64", /* FLAG_RET64 */
"register", /* FLAG_REGISTER */
"private", /* FLAG_PRIVATE */
"ordinal", /* FLAG_ORDINAL */
NULL
};
static const char * const ArgNames[ARG_MAXARG + 1] =
{
"word", /* ARG_WORD */
"s_word", /* ARG_SWORD */
"segptr", /* ARG_SEGPTR */
"segstr", /* ARG_SEGSTR */
"long", /* ARG_LONG */
"ptr", /* ARG_PTR */
"str", /* ARG_STR */
"wstr", /* ARG_WSTR */
"int64", /* ARG_INT64 */
"int128", /* ARG_INT128 */
"float", /* ARG_FLOAT */
"double" /* ARG_DOUBLE */
};
static int IsNumberString(const char *s)
{
while (*s) if (!isdigit(*s++)) return 0;
return 1;
}
static inline int is_token_separator( char ch )
{
return strchr( separator_chars, ch ) != NULL;
}
static inline int is_token_comment( char ch )
{
return strchr( comment_chars, ch ) != NULL;
}
/* get the next line from the input file, or return 0 if at eof */
static int get_next_line(void)
{
ParseNext = ParseBuffer;
current_line++;
return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
}
static const char * GetToken( int allow_eol )
{
char *p;
char *token = TokenBuffer;
for (;;)
{
/* remove initial white space */
p = ParseNext;
while (isspace(*p)) p++;
if (*p == '\\' && p[1] == '\n') /* line continuation */
{
if (!get_next_line())
{
if (!allow_eol) error( "Unexpected end of file\n" );
return NULL;
}
}
else break;
}
if ((*p == '\0') || is_token_comment(*p))
{
if (!allow_eol) error( "Declaration not terminated properly\n" );
return NULL;
}
/*
* Find end of token.
*/
if (is_token_separator(*p))
{
/* a separator is always a complete token */
*token++ = *p++;
}
else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
{
if (*p == '\\') p++;
if (*p) *token++ = *p++;
}
*token = '\0';
ParseNext = p;
return TokenBuffer;
}
static ORDDEF *add_entry_point( DLLSPEC *spec )
{
ORDDEF *ret;
if (spec->nb_entry_points == spec->alloc_entry_points)
{
spec->alloc_entry_points += 128;
spec->entry_points = xrealloc( spec->entry_points,
spec->alloc_entry_points * sizeof(*spec->entry_points) );
}
ret = &spec->entry_points[spec->nb_entry_points++];
memset( ret, 0, sizeof(*ret) );
return ret;
}
/*******************************************************************
* parse_spec_variable
*
* Parse a variable definition in a .spec file.
*/
static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
{
char *endptr;
unsigned int *value_array;
int n_values;
int value_array_size;
const char *token;
if (spec->type == SPEC_WIN32)
{
error( "'variable' not supported in Win32, use 'extern' instead\n" );
return 0;
}
if (!(token = GetToken(0))) return 0;
if (*token != '(')
{
error( "Expected '(' got '%s'\n", token );
return 0;
}
n_values = 0;
value_array_size = 25;
value_array = xmalloc(sizeof(*value_array) * value_array_size);
for (;;)
{
if (!(token = GetToken(0)))
{
free( value_array );
return 0;
}
if (*token == ')')
break;
value_array[n_values++] = strtoul(token, &endptr, 0);
if (n_values == value_array_size)
{
value_array_size += 25;
value_array = xrealloc(value_array,
sizeof(*value_array) * value_array_size);
}
if (endptr == NULL || *endptr != '\0')
{
error( "Expected number value, got '%s'\n", token );
free( value_array );
return 0;
}
}
odp->u.var.n_values = n_values;
odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
return 1;
}
/*******************************************************************
* parse_spec_arguments
*
* Parse the arguments of an entry point.
*/
static int parse_spec_arguments( ORDDEF *odp, DLLSPEC *spec, int optional )
{
const char *token;
unsigned int i, arg;
int is_win32 = (spec->type == SPEC_WIN32) || (odp->flags & FLAG_EXPORT32);
if (!(token = GetToken( optional ))) return optional;
if (*token != '(')
{
error( "Expected '(' got '%s'\n", token );
return 0;
}
odp->u.func.nb_args = 0;
for (i = 0; i < MAX_ARGUMENTS; i++)
{
if (!(token = GetToken(0))) return 0;
if (*token == ')')
break;
for (arg = 0; arg <= ARG_MAXARG; arg++)
if (!strcmp( ArgNames[arg], token )) break;
if (arg > ARG_MAXARG)
{
error( "Unknown argument type '%s'\n", token );
return 0;
}
if (is_win32) switch (arg)
{
case ARG_WORD:
case ARG_SWORD:
case ARG_SEGPTR:
case ARG_SEGSTR:
error( "Argument type '%s' only allowed for Win16\n", token );
return 0;
}
odp->u.func.args[i] = arg;
}
if (*token != ')')
{
error( "Too many arguments\n" );
return 0;
}
odp->u.func.nb_args = i;
if (odp->type == TYPE_THISCALL && (!i || odp->u.func.args[0] != ARG_PTR))
{
error( "First argument of a thiscall function must be a pointer\n" );
return 0;
}
return 1;
}
/*******************************************************************
* parse_spec_export
*
* Parse an exported function definition in a .spec file.
*/
static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
{
const char *token;
int is_win32 = (spec->type == SPEC_WIN32) || (odp->flags & FLAG_EXPORT32);
if (!is_win32 && odp->type == TYPE_STDCALL)
{
error( "'stdcall' not supported for Win16\n" );
return 0;
}
if (!is_win32 && odp->type == TYPE_THISCALL)
{
error( "'thiscall' not supported for Win16\n" );
return 0;
}
if (is_win32 && odp->type == TYPE_PASCAL)
{
error( "'pascal' not supported for Win32\n" );
return 0;
}
if (!parse_spec_arguments( odp, spec, 0 )) return 0;
if (odp->type == TYPE_VARARGS)
odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
if (!(token = GetToken(1)))
{
if (!strcmp( odp->name, "@" ))
{
error( "Missing handler name for anonymous function\n" );
return 0;
}
odp->link_name = xstrdup( odp->name );
}
else
{
odp->link_name = xstrdup( token );
if (strchr( odp->link_name, '.' ))
{
if (!is_win32)
{
error( "Forwarded functions not supported for Win16\n" );
return 0;
}
odp->flags |= FLAG_FORWARD;
}
}
if (target_cpu == CPU_x86 && odp->type == TYPE_THISCALL && !(odp->flags & FLAG_FORWARD))
{
char *link_name = strmake( "__thiscall_%s", odp->link_name );
free( odp->link_name );
odp->link_name = link_name;
}
return 1;
}
/*******************************************************************
* parse_spec_equate
*
* Parse an 'equate' definition in a .spec file.
*/
static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
{
char *endptr;
int value;
const char *token;
if (spec->type == SPEC_WIN32)
{
error( "'equate' not supported for Win32\n" );
return 0;
}
if (!(token = GetToken(0))) return 0;
value = strtol(token, &endptr, 0);
if (endptr == NULL || *endptr != '\0')
{
error( "Expected number value, got '%s'\n", token );
return 0;
}
if (value < -0x8000 || value > 0xffff)
{
error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
value = 0;
}
odp->u.abs.value = value;
return 1;
}
/*******************************************************************
* parse_spec_stub
*
* Parse a 'stub' definition in a .spec file
*/
static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
{
odp->u.func.nb_args = -1;
odp->link_name = xstrdup("");
/* don't bother generating stubs for Winelib */
if (odp->flags & FLAG_CPU_MASK)
odp->flags &= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64) | FLAG_CPU(CPU_ARM) | FLAG_CPU(CPU_ARM64);
else
odp->flags |= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64) | FLAG_CPU(CPU_ARM) | FLAG_CPU(CPU_ARM64);
return parse_spec_arguments( odp, spec, 1 );
}
/*******************************************************************
* parse_spec_extern
*
* Parse an 'extern' definition in a .spec file.
*/
static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
{
const char *token;
if (spec->type == SPEC_WIN16)
{
error( "'extern' not supported for Win16, use 'variable' instead\n" );
return 0;
}
if (!(token = GetToken(1)))
{
if (!strcmp( odp->name, "@" ))
{
error( "Missing handler name for anonymous extern\n" );
return 0;
}
odp->link_name = xstrdup( odp->name );
}
else
{
odp->link_name = xstrdup( token );
if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
}
return 1;
}
/*******************************************************************
* parse_spec_flags
*
* Parse the optional flags for an entry point in a .spec file.
*/
static const char *parse_spec_flags( DLLSPEC *spec, ORDDEF *odp )
{
unsigned int i;
const char *token;
do
{
if (!(token = GetToken(0))) break;
if (!strncmp( token, "arch=", 5))
{
char *args = xstrdup( token + 5 );
char *cpu_name = strtok( args, "," );
while (cpu_name)
{
if (!strcmp( cpu_name, "win32" ))
{
if (spec->type == SPEC_WIN32)
odp->flags |= FLAG_CPU_WIN32;
else
odp->flags |= FLAG_EXPORT32;
}
else if (!strcmp( cpu_name, "win64" ))
odp->flags |= FLAG_CPU_WIN64;
else
{
int cpu = get_cpu_from_name( cpu_name );
if (cpu == -1)
{
error( "Unknown architecture '%s'\n", cpu_name );
return NULL;
}
odp->flags |= FLAG_CPU( cpu );
}
cpu_name = strtok( NULL, "," );
}
free( args );
}
else if (!strcmp( token, "i386" )) /* backwards compatibility */
{
odp->flags |= FLAG_CPU(CPU_x86);
}
else
{
for (i = 0; FlagNames[i]; i++)
if (!strcmp( FlagNames[i], token )) break;
if (!FlagNames[i])
{
error( "Unknown flag '%s'\n", token );
return NULL;
}
switch (1 << i)
{
case FLAG_RET16:
case FLAG_REGISTER:
if (spec->type == SPEC_WIN32)
error( "Flag '%s' is not supported in Win32\n", FlagNames[i] );
break;
case FLAG_RET64:
if (spec->type == SPEC_WIN16)
error( "Flag '%s' is not supported in Win16\n", FlagNames[i] );
break;
}
odp->flags |= 1 << i;
}
token = GetToken(0);
} while (token && *token == '-');
return token;
}
/*******************************************************************
* parse_spec_ordinal
*
* Parse an ordinal definition in a .spec file.
*/
static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
{
const char *token;
size_t len;
ORDDEF *odp = add_entry_point( spec );
if (!(token = GetToken(0))) goto error;
for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
break;
if (odp->type >= TYPE_NBTYPES)
{
error( "Expected type after ordinal, found '%s' instead\n", token );
goto error;
}
if (!(token = GetToken(0))) goto error;
if (*token == '-' && !(token = parse_spec_flags( spec, odp ))) goto error;
if (ordinal == -1 && spec->type != SPEC_WIN32 && !(odp->flags & FLAG_EXPORT32))
{
error( "'@' ordinals not supported for Win16\n" );
goto error;
}
odp->name = xstrdup( token );
odp->lineno = current_line;
odp->ordinal = ordinal;
len = strspn( odp->name, valid_ordname_chars );
if (len < strlen( odp->name ))
{
error( "Character '%c' is not allowed in exported name '%s'\n", odp->name[len], odp->name );
goto error;
}
switch(odp->type)
{
case TYPE_VARIABLE:
if (!parse_spec_variable( odp, spec )) goto error;
break;
case TYPE_PASCAL:
case TYPE_STDCALL:
case TYPE_VARARGS:
case TYPE_CDECL:
case TYPE_THISCALL:
if (!parse_spec_export( odp, spec )) goto error;
break;
case TYPE_ABS:
if (!parse_spec_equate( odp, spec )) goto error;
break;
case TYPE_STUB:
if (!parse_spec_stub( odp, spec )) goto error;
break;
case TYPE_EXTERN:
if (!parse_spec_extern( odp, spec )) goto error;
break;
default:
assert( 0 );
}
if ((odp->flags & FLAG_CPU_MASK) && !(odp->flags & FLAG_CPU(target_cpu)))
{
/* ignore this entry point */
spec->nb_entry_points--;
return 1;
}
if (ordinal != -1)
{
if (!ordinal)
{
error( "Ordinal 0 is not valid\n" );
goto error;
}
if (ordinal >= MAX_ORDINALS)
{
error( "Ordinal number %d too large\n", ordinal );
goto error;
}
if (ordinal > spec->limit) spec->limit = ordinal;
if (ordinal < spec->base) spec->base = ordinal;
odp->ordinal = ordinal;
}
if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
{
if (!strcmp( odp->name, "DllRegisterServer" ) ||
!strcmp( odp->name, "DllUnregisterServer" ) ||
!strcmp( odp->name, "DllMain" ) ||
!strcmp( odp->name, "DllGetClassObject" ) ||
!strcmp( odp->name, "DllGetVersion" ) ||
!strcmp( odp->name, "DllInstall" ) ||
!strcmp( odp->name, "DllCanUnloadNow" ))
{
warning( "Function %s should be marked private\n", odp->name );
if (strcmp( odp->name, odp->link_name ))
warning( "Function %s should not use a different internal name (%s)\n",
odp->name, odp->link_name );
}
}
if (!strcmp( odp->name, "@" ) || odp->flags & (FLAG_NONAME | FLAG_ORDINAL))
{
if (ordinal == -1)
{
if (!strcmp( odp->name, "@" ))
error( "Nameless function needs an explicit ordinal number\n" );
else
error( "Function imported by ordinal needs an explicit ordinal number\n" );
goto error;
}
if (spec->type != SPEC_WIN32)
{
error( "Nameless functions not supported for Win16\n" );
goto error;
}
if (!strcmp( odp->name, "@" ))
{
free( odp->name );
odp->name = NULL;
}
else if (!(odp->flags & FLAG_ORDINAL)) /* -ordinal only affects the import library */
{
odp->export_name = odp->name;
odp->name = NULL;
}
}
return 1;
error:
spec->nb_entry_points--;
free( odp->name );
return 0;
}
static int name_compare( const void *ptr1, const void *ptr2 )
{
const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
const char *name1 = odp1->name ? odp1->name : odp1->export_name;
const char *name2 = odp2->name ? odp2->name : odp2->export_name;
return strcmp( name1, name2 );
}
/*******************************************************************
* assign_names
*
* Build the name array and catch duplicates.
*/
static void assign_names( DLLSPEC *spec )
{
int i, j, nb_exp_names = 0;
ORDDEF **all_names;
spec->nb_names = 0;
for (i = 0; i < spec->nb_entry_points; i++)
if (spec->entry_points[i].name) spec->nb_names++;
else if (spec->entry_points[i].export_name) nb_exp_names++;
if (!spec->nb_names && !nb_exp_names) return;
/* check for duplicates */
all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
for (i = j = 0; i < spec->nb_entry_points; i++)
if (spec->entry_points[i].name || spec->entry_points[i].export_name)
all_names[j++] = &spec->entry_points[i];
qsort( all_names, j, sizeof(all_names[0]), name_compare );
for (i = 0; i < j - 1; i++)
{
const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
if (!strcmp( name1, name2 ) &&
!((all_names[i]->flags ^ all_names[i+1]->flags) & FLAG_EXPORT32))
{
current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
error( "'%s' redefined\n%s:%d: First defined here\n",
name1, input_file_name,
min( all_names[i]->lineno, all_names[i+1]->lineno ) );
}
}
free( all_names );
if (spec->nb_names)
{
spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
for (i = j = 0; i < spec->nb_entry_points; i++)
if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
/* sort the list of names */
qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
}
}
/*******************************************************************
* assign_ordinals
*
* Build the ordinal array.
*/
static void assign_ordinals( DLLSPEC *spec )
{
int i, count, ordinal;
/* start assigning from base, or from 1 if no ordinal defined yet */
spec->base = MAX_ORDINALS;
spec->limit = 0;
for (i = 0; i < spec->nb_entry_points; i++)
{
ordinal = spec->entry_points[i].ordinal;
if (ordinal == -1) continue;
if (ordinal > spec->limit) spec->limit = ordinal;
if (ordinal < spec->base) spec->base = ordinal;
}
if (spec->base == MAX_ORDINALS) spec->base = 1;
if (spec->limit < spec->base) spec->limit = spec->base;
count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
/* fill in all explicitly specified ordinals */
for (i = 0; i < spec->nb_entry_points; i++)
{
ordinal = spec->entry_points[i].ordinal;
if (ordinal == -1) continue;
if (spec->ordinals[ordinal])
{
current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
error( "ordinal %d redefined\n%s:%d: First defined here\n",
ordinal, input_file_name,
min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
}
else spec->ordinals[ordinal] = &spec->entry_points[i];
}
/* now assign ordinals to the rest */
for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
{
if (spec->entry_points[i].ordinal != -1) continue;
while (spec->ordinals[ordinal]) ordinal++;
if (ordinal >= MAX_ORDINALS)
{
current_line = spec->entry_points[i].lineno;
fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
}
spec->entry_points[i].ordinal = ordinal;
spec->ordinals[ordinal] = &spec->entry_points[i];
}
if (ordinal > spec->limit) spec->limit = ordinal;
}
/*******************************************************************
* add_16bit_exports
*
* Add the necessary exports to the 32-bit counterpart of a 16-bit module.
*/
void add_16bit_exports( DLLSPEC *spec32, DLLSPEC *spec16 )
{
int i;
ORDDEF *odp;
spec32->file_name = xstrdup( spec16->file_name );
if (spec16->characteristics & IMAGE_FILE_DLL)
{
spec32->characteristics = IMAGE_FILE_DLL;
spec32->init_func = xstrdup( "__wine_spec_dll_entry" );
}
/* add an export for the NE module */
odp = add_entry_point( spec32 );
odp->type = TYPE_EXTERN;
odp->flags = FLAG_PRIVATE;
odp->name = xstrdup( "__wine_spec_dos_header" );
odp->lineno = 0;
odp->ordinal = 1;
odp->link_name = xstrdup( ".L__wine_spec_dos_header" );
if (spec16->main_module)
{
odp = add_entry_point( spec32 );
odp->type = TYPE_EXTERN;
odp->flags = FLAG_PRIVATE;
odp->name = xstrdup( "__wine_spec_main_module" );
odp->lineno = 0;
odp->ordinal = 2;
odp->link_name = xstrdup( ".L__wine_spec_main_module" );
}
/* add the explicit win32 exports */
for (i = 1; i <= spec16->limit; i++)
{
ORDDEF *odp16 = spec16->ordinals[i];
if (!odp16 || !odp16->name) continue;
if (!(odp16->flags & FLAG_EXPORT32)) continue;
odp = add_entry_point( spec32 );
odp->flags = odp16->flags & ~FLAG_EXPORT32;
odp->type = odp16->type;
odp->name = xstrdup( odp16->name );
odp->lineno = odp16->lineno;
odp->ordinal = -1;
odp->link_name = xstrdup( odp16->link_name );
odp->u.func.nb_args = odp16->u.func.nb_args;
if (odp->u.func.nb_args > 0) memcpy( odp->u.func.args, odp16->u.func.args,
odp->u.func.nb_args * sizeof(odp->u.func.args[0]) );
}
assign_names( spec32 );
assign_ordinals( spec32 );
}
/*******************************************************************
* parse_spec_file
*
* Parse a .spec file.
*/
int parse_spec_file( FILE *file, DLLSPEC *spec )
{
const char *token;
input_file = file;
current_line = 0;
comment_chars = "#;";
separator_chars = "()-";
while (get_next_line())
{
if (!(token = GetToken(1))) continue;
if (strcmp(token, "@") == 0)
{
if (!parse_spec_ordinal( -1, spec )) continue;
}
else if (IsNumberString(token))
{
if (!parse_spec_ordinal( atoi(token), spec )) continue;
}
else
{
error( "Expected ordinal declaration, got '%s'\n", token );
continue;
}
if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
}
current_line = 0; /* no longer parsing the input file */
assign_names( spec );
assign_ordinals( spec );
return !nb_errors;
}
/*******************************************************************
* parse_def_library
*
* Parse a LIBRARY declaration in a .def file.
*/
static int parse_def_library( DLLSPEC *spec )
{
const char *token = GetToken(1);
if (!token) return 1;
if (strcmp( token, "BASE" ))
{
free( spec->file_name );
spec->file_name = xstrdup( token );
if (!(token = GetToken(1))) return 1;
}
if (strcmp( token, "BASE" ))
{
error( "Expected library name or BASE= declaration, got '%s'\n", token );
return 0;
}
if (!(token = GetToken(0))) return 0;
if (strcmp( token, "=" ))
{
error( "Expected '=' after BASE, got '%s'\n", token );
return 0;
}
if (!(token = GetToken(0))) return 0;
/* FIXME: do something with base address */
return 1;
}
/*******************************************************************
* parse_def_stack_heap_size
*
* Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
*/
static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
{
const char *token = GetToken(0);
char *end;
unsigned long size;
if (!token) return 0;
size = strtoul( token, &end, 0 );
if (*end)
{
error( "Invalid number '%s'\n", token );
return 0;
}
if (is_stack) spec->stack_size = size / 1024;
else spec->heap_size = size / 1024;
if (!(token = GetToken(1))) return 1;
if (strcmp( token, "," ))
{
error( "Expected ',' after size, got '%s'\n", token );
return 0;
}
if (!(token = GetToken(0))) return 0;
/* FIXME: do something with reserve size */
return 1;
}
/*******************************************************************
* parse_def_export
*
* Parse an export declaration in a .def file.
*/
static int parse_def_export( char *name, DLLSPEC *spec )
{
int i, args;
const char *token = GetToken(1);
ORDDEF *odp = add_entry_point( spec );
odp->lineno = current_line;
odp->ordinal = -1;
odp->name = name;
args = remove_stdcall_decoration( odp->name );
if (args == -1)
{
odp->type = TYPE_CDECL;
args = 0;
}
else
{
odp->type = TYPE_STDCALL;
args /= get_ptr_size();
if (args >= MAX_ARGUMENTS)
{
error( "Too many arguments in stdcall function '%s'\n", odp->name );
return 0;
}
for (i = 0; i < args; i++) odp->u.func.args[i] = ARG_LONG;
}
odp->u.func.nb_args = args;
/* check for optional internal name */
if (token && !strcmp( token, "=" ))
{
if (!(token = GetToken(0))) goto error;
odp->link_name = xstrdup( token );
remove_stdcall_decoration( odp->link_name );
token = GetToken(1);
}
else
{
odp->link_name = xstrdup( name );
}
/* check for optional ordinal */
if (token && token[0] == '@')
{
int ordinal;
if (!IsNumberString( token+1 ))
{
error( "Expected number after '@', got '%s'\n", token+1 );
goto error;
}
ordinal = atoi( token+1 );
if (!ordinal)
{
error( "Ordinal 0 is not valid\n" );
goto error;
}
if (ordinal >= MAX_ORDINALS)
{
error( "Ordinal number %d too large\n", ordinal );
goto error;
}
odp->ordinal = ordinal;
token = GetToken(1);
}
/* check for other optional keywords */
while (token)
{
if (!strcmp( token, "NONAME" ))
{
if (odp->ordinal == -1)
{
error( "NONAME requires an ordinal\n" );
goto error;
}
odp->export_name = odp->name;
odp->name = NULL;
odp->flags |= FLAG_NONAME;
}
else if (!strcmp( token, "PRIVATE" ))
{
odp->flags |= FLAG_PRIVATE;
}
else if (!strcmp( token, "DATA" ))
{
odp->type = TYPE_EXTERN;
}
else
{
error( "Garbage text '%s' found at end of export declaration\n", token );
goto error;
}
token = GetToken(1);
}
return 1;
error:
spec->nb_entry_points--;
free( odp->name );
return 0;
}
/*******************************************************************
* parse_def_file
*
* Parse a .def file.
*/
int parse_def_file( FILE *file, DLLSPEC *spec )
{
const char *token;
int in_exports = 0;
input_file = file;
current_line = 0;
comment_chars = ";";
separator_chars = ",=";
while (get_next_line())
{
if (!(token = GetToken(1))) continue;
if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
{
if (!parse_def_library( spec )) continue;
goto end_of_line;
}
else if (!strcmp( token, "STACKSIZE" ))
{
if (!parse_def_stack_heap_size( 1, spec )) continue;
goto end_of_line;
}
else if (!strcmp( token, "HEAPSIZE" ))
{
if (!parse_def_stack_heap_size( 0, spec )) continue;
goto end_of_line;
}
else if (!strcmp( token, "EXPORTS" ))
{
in_exports = 1;
if (!(token = GetToken(1))) continue;
}
else if (!strcmp( token, "IMPORTS" ))
{
in_exports = 0;
if (!(token = GetToken(1))) continue;
}
else if (!strcmp( token, "SECTIONS" ))
{
in_exports = 0;
if (!(token = GetToken(1))) continue;
}
if (!in_exports) continue; /* ignore this line */
if (!parse_def_export( xstrdup(token), spec )) continue;
end_of_line:
if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
}
current_line = 0; /* no longer parsing the input file */
assign_names( spec );
assign_ordinals( spec );
return !nb_errors;
}
|