4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1995, 1996, 1997, 2004 Alexandre Julliard
7 * Copyright 1997 Eric Youngdale
8 * Copyright 1999 Ulrich Weigand
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
41 static char ParseBuffer[512];
42 static char TokenBuffer[512];
43 static char *ParseNext = ParseBuffer;
44 static FILE *input_file;
46 static const char *separator_chars;
47 static const char *comment_chars;
49 static const char * const TypeNames[TYPE_NBTYPES] =
51 "variable", /* TYPE_VARIABLE */
52 "pascal", /* TYPE_PASCAL */
53 "equate", /* TYPE_ABS */
54 "stub", /* TYPE_STUB */
55 "stdcall", /* TYPE_STDCALL */
56 "cdecl", /* TYPE_CDECL */
57 "varargs", /* TYPE_VARARGS */
58 "extern" /* TYPE_EXTERN */
61 static const char * const FlagNames[] =
63 "norelay", /* FLAG_NORELAY */
64 "noname", /* FLAG_NONAME */
65 "ret16", /* FLAG_RET16 */
66 "ret64", /* FLAG_RET64 */
67 "i386", /* FLAG_I386 */
68 "register", /* FLAG_REGISTER */
69 "private", /* FLAG_PRIVATE */
73 static int IsNumberString(const char *s)
75 while (*s) if (!isdigit(*s++)) return 0;
79 inline static int is_token_separator( char ch )
81 return strchr( separator_chars, ch ) != NULL;
84 inline static int is_token_comment( char ch )
86 return strchr( comment_chars, ch ) != NULL;
89 /* get the next line from the input file, or return 0 if at eof */
90 static int get_next_line(void)
92 ParseNext = ParseBuffer;
94 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
97 static const char * GetToken( int allow_eol )
100 char *token = TokenBuffer;
104 /* remove initial white space */
106 while (isspace(*p)) p++;
108 if (*p == '\\' && p[1] == '\n') /* line continuation */
110 if (!get_next_line())
112 if (!allow_eol) error( "Unexpected end of file\n" );
119 if ((*p == '\0') || is_token_comment(*p))
121 if (!allow_eol) error( "Declaration not terminated properly\n" );
128 if (is_token_separator(*p))
130 /* a separator is always a complete token */
133 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
136 if (*p) *token++ = *p++;
144 static ORDDEF *add_entry_point( DLLSPEC *spec )
146 if (spec->nb_entry_points == spec->alloc_entry_points)
148 spec->alloc_entry_points += 128;
149 spec->entry_points = xrealloc( spec->entry_points,
150 spec->alloc_entry_points * sizeof(*spec->entry_points) );
152 return &spec->entry_points[spec->nb_entry_points++];
155 /*******************************************************************
156 * parse_spec_variable
158 * Parse a variable definition in a .spec file.
160 static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
165 int value_array_size;
168 if (spec->type == SPEC_WIN32)
170 error( "'variable' not supported in Win32, use 'extern' instead\n" );
174 if (!(token = GetToken(0))) return 0;
177 error( "Expected '(' got '%s'\n", token );
182 value_array_size = 25;
183 value_array = xmalloc(sizeof(*value_array) * value_array_size);
187 if (!(token = GetToken(0)))
195 value_array[n_values++] = strtol(token, &endptr, 0);
196 if (n_values == value_array_size)
198 value_array_size += 25;
199 value_array = xrealloc(value_array,
200 sizeof(*value_array) * value_array_size);
203 if (endptr == NULL || *endptr != '\0')
205 error( "Expected number value, got '%s'\n", token );
211 odp->u.var.n_values = n_values;
212 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
217 /*******************************************************************
220 * Parse an exported function definition in a .spec file.
222 static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
230 if (odp->type == TYPE_STDCALL)
232 error( "'stdcall' not supported for Win16\n" );
237 if (odp->type == TYPE_PASCAL)
239 error( "'pascal' not supported for Win32\n" );
247 if (!(token = GetToken(0))) return 0;
250 error( "Expected '(' got '%s'\n", token );
254 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
256 if (!(token = GetToken(0))) return 0;
260 if (!strcmp(token, "word"))
261 odp->u.func.arg_types[i] = 'w';
262 else if (!strcmp(token, "s_word"))
263 odp->u.func.arg_types[i] = 's';
264 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
265 odp->u.func.arg_types[i] = 'l';
266 else if (!strcmp(token, "ptr"))
267 odp->u.func.arg_types[i] = 'p';
268 else if (!strcmp(token, "str"))
269 odp->u.func.arg_types[i] = 't';
270 else if (!strcmp(token, "wstr"))
271 odp->u.func.arg_types[i] = 'W';
272 else if (!strcmp(token, "segstr"))
273 odp->u.func.arg_types[i] = 'T';
274 else if (!strcmp(token, "double"))
276 odp->u.func.arg_types[i++] = 'l';
277 if (get_ptr_size() == 4 && i < sizeof(odp->u.func.arg_types))
278 odp->u.func.arg_types[i] = 'l';
282 error( "Unknown argument type '%s'\n", token );
286 if (spec->type == SPEC_WIN32)
288 if (strcmp(token, "long") &&
289 strcmp(token, "ptr") &&
290 strcmp(token, "str") &&
291 strcmp(token, "wstr") &&
292 strcmp(token, "double"))
294 error( "Type '%s' not supported for Win32\n", token );
299 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
301 error( "Too many arguments\n" );
305 odp->u.func.arg_types[i] = '\0';
306 if (odp->type == TYPE_VARARGS)
307 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
309 if (!(token = GetToken(1)))
311 if (!strcmp( odp->name, "@" ))
313 error( "Missing handler name for anonymous function\n" );
316 odp->link_name = xstrdup( odp->name );
320 odp->link_name = xstrdup( token );
321 if (strchr( odp->link_name, '.' ))
323 if (spec->type == SPEC_WIN16)
325 error( "Forwarded functions not supported for Win16\n" );
328 odp->flags |= FLAG_FORWARD;
335 /*******************************************************************
338 * Parse an 'equate' definition in a .spec file.
340 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
346 if (spec->type == SPEC_WIN32)
348 error( "'equate' not supported for Win32\n" );
351 if (!(token = GetToken(0))) return 0;
352 value = strtol(token, &endptr, 0);
353 if (endptr == NULL || *endptr != '\0')
355 error( "Expected number value, got '%s'\n", token );
358 if (value < -0x8000 || value > 0xffff)
360 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
363 odp->u.abs.value = value;
368 /*******************************************************************
371 * Parse a 'stub' definition in a .spec file
373 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
375 odp->u.func.arg_types[0] = '\0';
376 odp->link_name = xstrdup("");
377 odp->flags |= FLAG_I386; /* don't bother generating stubs for Winelib */
382 /*******************************************************************
385 * Parse an 'extern' definition in a .spec file.
387 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
391 if (spec->type == SPEC_WIN16)
393 error( "'extern' not supported for Win16, use 'variable' instead\n" );
396 if (!(token = GetToken(1)))
398 if (!strcmp( odp->name, "@" ))
400 error( "Missing handler name for anonymous extern\n" );
403 odp->link_name = xstrdup( odp->name );
407 odp->link_name = xstrdup( token );
408 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
414 /*******************************************************************
417 * Parse the optional flags for an entry point in a .spec file.
419 static const char *parse_spec_flags( ORDDEF *odp )
426 if (!(token = GetToken(0))) break;
427 for (i = 0; FlagNames[i]; i++)
428 if (!strcmp( FlagNames[i], token )) break;
431 error( "Unknown flag '%s'\n", token );
434 odp->flags |= 1 << i;
436 } while (token && *token == '-');
442 /*******************************************************************
445 * Parse an ordinal definition in a .spec file.
447 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
451 ORDDEF *odp = add_entry_point( spec );
452 memset( odp, 0, sizeof(*odp) );
454 if (!(token = GetToken(0))) goto error;
456 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
457 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
460 if (odp->type >= TYPE_NBTYPES)
462 error( "Expected type after ordinal, found '%s' instead\n", token );
466 if (!(token = GetToken(0))) goto error;
467 if (*token == '-' && !(token = parse_spec_flags( odp ))) goto error;
469 odp->name = xstrdup( token );
470 odp->lineno = current_line;
471 odp->ordinal = ordinal;
476 if (!parse_spec_variable( odp, spec )) goto error;
482 if (!parse_spec_export( odp, spec )) goto error;
485 if (!parse_spec_equate( odp, spec )) goto error;
488 if (!parse_spec_stub( odp, spec )) goto error;
491 if (!parse_spec_extern( odp, spec )) goto error;
497 if ((target_cpu != CPU_x86) && (odp->flags & FLAG_I386))
499 /* ignore this entry point on non-Intel archs */
500 spec->nb_entry_points--;
508 error( "Ordinal 0 is not valid\n" );
511 if (ordinal >= MAX_ORDINALS)
513 error( "Ordinal number %d too large\n", ordinal );
516 if (ordinal > spec->limit) spec->limit = ordinal;
517 if (ordinal < spec->base) spec->base = ordinal;
518 odp->ordinal = ordinal;
521 if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
523 if (!strcmp( odp->name, "DllRegisterServer" ) ||
524 !strcmp( odp->name, "DllUnregisterServer" ) ||
525 !strcmp( odp->name, "DllGetClassObject" ) ||
526 !strcmp( odp->name, "DllGetVersion" ) ||
527 !strcmp( odp->name, "DllInstall" ) ||
528 !strcmp( odp->name, "DllCanUnloadNow" ))
530 warning( "Function %s should be marked private\n", odp->name );
531 if (strcmp( odp->name, odp->link_name ))
532 warning( "Function %s should not use a different internal name (%s)\n",
533 odp->name, odp->link_name );
537 if (!strcmp( odp->name, "@" ) || odp->flags & FLAG_NONAME)
541 error( "Nameless function needs an explicit ordinal number\n" );
544 if (spec->type != SPEC_WIN32)
546 error( "Nameless functions not supported for Win16\n" );
549 if (!strcmp( odp->name, "@" )) free( odp->name );
550 else odp->export_name = odp->name;
556 spec->nb_entry_points--;
562 static int name_compare( const void *ptr1, const void *ptr2 )
564 const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
565 const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
566 const char *name1 = odp1->name ? odp1->name : odp1->export_name;
567 const char *name2 = odp2->name ? odp2->name : odp2->export_name;
568 return strcmp( name1, name2 );
571 /*******************************************************************
574 * Build the name array and catch duplicates.
576 static void assign_names( DLLSPEC *spec )
578 int i, j, nb_exp_names = 0;
582 for (i = 0; i < spec->nb_entry_points; i++)
583 if (spec->entry_points[i].name) spec->nb_names++;
584 else if (spec->entry_points[i].export_name) nb_exp_names++;
586 if (!spec->nb_names && !nb_exp_names) return;
588 /* check for duplicates */
590 all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
591 for (i = j = 0; i < spec->nb_entry_points; i++)
592 if (spec->entry_points[i].name || spec->entry_points[i].export_name)
593 all_names[j++] = &spec->entry_points[i];
595 qsort( all_names, j, sizeof(all_names[0]), name_compare );
597 for (i = 0; i < j - 1; i++)
599 const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
600 const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
601 if (!strcmp( name1, name2 ))
603 current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
604 error( "'%s' redefined\n%s:%d: First defined here\n",
605 name1, input_file_name,
606 min( all_names[i]->lineno, all_names[i+1]->lineno ) );
613 spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
614 for (i = j = 0; i < spec->nb_entry_points; i++)
615 if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
617 /* sort the list of names */
618 qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
622 /*******************************************************************
625 * Build the ordinal array.
627 static void assign_ordinals( DLLSPEC *spec )
629 int i, count, ordinal;
631 /* start assigning from base, or from 1 if no ordinal defined yet */
633 spec->base = MAX_ORDINALS;
635 for (i = 0; i < spec->nb_entry_points; i++)
637 ordinal = spec->entry_points[i].ordinal;
638 if (ordinal == -1) continue;
639 if (ordinal > spec->limit) spec->limit = ordinal;
640 if (ordinal < spec->base) spec->base = ordinal;
642 if (spec->base == MAX_ORDINALS) spec->base = 1;
643 if (spec->limit < spec->base) spec->limit = spec->base;
645 count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
646 spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
647 memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
649 /* fill in all explicitly specified ordinals */
650 for (i = 0; i < spec->nb_entry_points; i++)
652 ordinal = spec->entry_points[i].ordinal;
653 if (ordinal == -1) continue;
654 if (spec->ordinals[ordinal])
656 current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
657 error( "ordinal %d redefined\n%s:%d: First defined here\n",
658 ordinal, input_file_name,
659 min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
661 else spec->ordinals[ordinal] = &spec->entry_points[i];
664 /* now assign ordinals to the rest */
665 for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
667 if (spec->entry_points[i].ordinal != -1) continue;
668 while (spec->ordinals[ordinal]) ordinal++;
669 if (ordinal >= MAX_ORDINALS)
671 current_line = spec->entry_points[i].lineno;
672 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
674 spec->entry_points[i].ordinal = ordinal;
675 spec->ordinals[ordinal] = &spec->entry_points[i];
677 if (ordinal > spec->limit) spec->limit = ordinal;
681 /*******************************************************************
684 * Parse a .spec file.
686 int parse_spec_file( FILE *file, DLLSPEC *spec )
693 comment_chars = "#;";
694 separator_chars = "()-";
696 while (get_next_line())
698 if (!(token = GetToken(1))) continue;
699 if (strcmp(token, "@") == 0)
701 if (spec->type != SPEC_WIN32)
703 error( "'@' ordinals not supported for Win16\n" );
706 if (!parse_spec_ordinal( -1, spec )) continue;
708 else if (IsNumberString(token))
710 if (!parse_spec_ordinal( atoi(token), spec )) continue;
714 error( "Expected ordinal declaration, got '%s'\n", token );
717 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
720 current_line = 0; /* no longer parsing the input file */
721 assign_names( spec );
722 assign_ordinals( spec );
727 /*******************************************************************
730 * Parse a LIBRARY declaration in a .def file.
732 static int parse_def_library( DLLSPEC *spec )
734 const char *token = GetToken(1);
736 if (!token) return 1;
737 if (strcmp( token, "BASE" ))
739 free( spec->file_name );
740 spec->file_name = xstrdup( token );
741 if (!(token = GetToken(1))) return 1;
743 if (strcmp( token, "BASE" ))
745 error( "Expected library name or BASE= declaration, got '%s'\n", token );
748 if (!(token = GetToken(0))) return 0;
749 if (strcmp( token, "=" ))
751 error( "Expected '=' after BASE, got '%s'\n", token );
754 if (!(token = GetToken(0))) return 0;
755 /* FIXME: do something with base address */
761 /*******************************************************************
762 * parse_def_stack_heap_size
764 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
766 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
768 const char *token = GetToken(0);
772 if (!token) return 0;
773 size = strtoul( token, &end, 0 );
776 error( "Invalid number '%s'\n", token );
779 if (is_stack) spec->stack_size = size / 1024;
780 else spec->heap_size = size / 1024;
781 if (!(token = GetToken(1))) return 1;
782 if (strcmp( token, "," ))
784 error( "Expected ',' after size, got '%s'\n", token );
787 if (!(token = GetToken(0))) return 0;
788 /* FIXME: do something with reserve size */
793 /*******************************************************************
796 * Parse an export declaration in a .def file.
798 static int parse_def_export( char *name, DLLSPEC *spec )
801 const char *token = GetToken(1);
803 ORDDEF *odp = add_entry_point( spec );
804 memset( odp, 0, sizeof(*odp) );
806 odp->lineno = current_line;
809 args = remove_stdcall_decoration( odp->name );
810 if (args == -1) odp->type = TYPE_CDECL;
813 odp->type = TYPE_STDCALL;
814 args /= get_ptr_size();
815 if (args >= sizeof(odp->u.func.arg_types))
817 error( "Too many arguments in stdcall function '%s'\n", odp->name );
820 for (i = 0; i < args; i++) odp->u.func.arg_types[i] = 'l';
823 /* check for optional internal name */
825 if (token && !strcmp( token, "=" ))
827 if (!(token = GetToken(0))) goto error;
828 odp->link_name = xstrdup( token );
829 remove_stdcall_decoration( odp->link_name );
834 odp->link_name = xstrdup( name );
837 /* check for optional ordinal */
839 if (token && token[0] == '@')
843 if (!IsNumberString( token+1 ))
845 error( "Expected number after '@', got '%s'\n", token+1 );
848 ordinal = atoi( token+1 );
851 error( "Ordinal 0 is not valid\n" );
854 if (ordinal >= MAX_ORDINALS)
856 error( "Ordinal number %d too large\n", ordinal );
859 odp->ordinal = ordinal;
863 /* check for other optional keywords */
865 if (token && !strcmp( token, "NONAME" ))
867 if (odp->ordinal == -1)
869 error( "NONAME requires an ordinal\n" );
872 odp->export_name = odp->name;
874 odp->flags |= FLAG_NONAME;
877 if (token && !strcmp( token, "PRIVATE" ))
879 odp->flags |= FLAG_PRIVATE;
882 if (token && !strcmp( token, "DATA" ))
884 odp->type = TYPE_EXTERN;
889 error( "Garbage text '%s' found at end of export declaration\n", token );
895 spec->nb_entry_points--;
901 /*******************************************************************
906 int parse_def_file( FILE *file, DLLSPEC *spec )
915 separator_chars = ",=";
917 while (get_next_line())
919 if (!(token = GetToken(1))) continue;
921 if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
923 if (!parse_def_library( spec )) continue;
926 else if (!strcmp( token, "STACKSIZE" ))
928 if (!parse_def_stack_heap_size( 1, spec )) continue;
931 else if (!strcmp( token, "HEAPSIZE" ))
933 if (!parse_def_stack_heap_size( 0, spec )) continue;
936 else if (!strcmp( token, "EXPORTS" ))
939 if (!(token = GetToken(1))) continue;
941 else if (!strcmp( token, "IMPORTS" ))
944 if (!(token = GetToken(1))) continue;
946 else if (!strcmp( token, "SECTIONS" ))
949 if (!(token = GetToken(1))) continue;
952 if (!in_exports) continue; /* ignore this line */
953 if (!parse_def_export( xstrdup(token), spec )) continue;
956 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
959 current_line = 0; /* no longer parsing the input file */
960 assign_names( spec );
961 assign_ordinals( spec );