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 /* valid characters in ordinal names */
50 static const char valid_ordname_chars[] = "/$:-_@?abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
52 static const char * const TypeNames[TYPE_NBTYPES] =
54 "variable", /* TYPE_VARIABLE */
55 "pascal", /* TYPE_PASCAL */
56 "equate", /* TYPE_ABS */
57 "stub", /* TYPE_STUB */
58 "stdcall", /* TYPE_STDCALL */
59 "cdecl", /* TYPE_CDECL */
60 "varargs", /* TYPE_VARARGS */
61 "extern" /* TYPE_EXTERN */
64 static const char * const FlagNames[] =
66 "norelay", /* FLAG_NORELAY */
67 "noname", /* FLAG_NONAME */
68 "ret16", /* FLAG_RET16 */
69 "ret64", /* FLAG_RET64 */
70 "register", /* FLAG_REGISTER */
71 "private", /* FLAG_PRIVATE */
72 "ordinal", /* FLAG_ORDINAL */
76 static int IsNumberString(const char *s)
78 while (*s) if (!isdigit(*s++)) return 0;
82 static inline int is_token_separator( char ch )
84 return strchr( separator_chars, ch ) != NULL;
87 static inline int is_token_comment( char ch )
89 return strchr( comment_chars, ch ) != NULL;
92 /* get the next line from the input file, or return 0 if at eof */
93 static int get_next_line(void)
95 ParseNext = ParseBuffer;
97 return (fgets(ParseBuffer, sizeof(ParseBuffer), input_file) != NULL);
100 static const char * GetToken( int allow_eol )
103 char *token = TokenBuffer;
107 /* remove initial white space */
109 while (isspace(*p)) p++;
111 if (*p == '\\' && p[1] == '\n') /* line continuation */
113 if (!get_next_line())
115 if (!allow_eol) error( "Unexpected end of file\n" );
122 if ((*p == '\0') || is_token_comment(*p))
124 if (!allow_eol) error( "Declaration not terminated properly\n" );
131 if (is_token_separator(*p))
133 /* a separator is always a complete token */
136 else while (*p != '\0' && !is_token_separator(*p) && !isspace(*p))
139 if (*p) *token++ = *p++;
147 static ORDDEF *add_entry_point( DLLSPEC *spec )
151 if (spec->nb_entry_points == spec->alloc_entry_points)
153 spec->alloc_entry_points += 128;
154 spec->entry_points = xrealloc( spec->entry_points,
155 spec->alloc_entry_points * sizeof(*spec->entry_points) );
157 ret = &spec->entry_points[spec->nb_entry_points++];
158 memset( ret, 0, sizeof(*ret) );
162 /*******************************************************************
163 * parse_spec_variable
165 * Parse a variable definition in a .spec file.
167 static int parse_spec_variable( ORDDEF *odp, DLLSPEC *spec )
172 int value_array_size;
175 if (spec->type == SPEC_WIN32)
177 error( "'variable' not supported in Win32, use 'extern' instead\n" );
181 if (!(token = GetToken(0))) return 0;
184 error( "Expected '(' got '%s'\n", token );
189 value_array_size = 25;
190 value_array = xmalloc(sizeof(*value_array) * value_array_size);
194 if (!(token = GetToken(0)))
202 value_array[n_values++] = strtol(token, &endptr, 0);
203 if (n_values == value_array_size)
205 value_array_size += 25;
206 value_array = xrealloc(value_array,
207 sizeof(*value_array) * value_array_size);
210 if (endptr == NULL || *endptr != '\0')
212 error( "Expected number value, got '%s'\n", token );
218 odp->u.var.n_values = n_values;
219 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
224 /*******************************************************************
227 * Parse an exported function definition in a .spec file.
229 static int parse_spec_export( ORDDEF *odp, DLLSPEC *spec )
237 if (odp->type == TYPE_STDCALL)
239 error( "'stdcall' not supported for Win16\n" );
244 if (odp->type == TYPE_PASCAL)
246 error( "'pascal' not supported for Win32\n" );
254 if (!(token = GetToken(0))) return 0;
257 error( "Expected '(' got '%s'\n", token );
261 for (i = 0; i < sizeof(odp->u.func.arg_types); i++)
263 if (!(token = GetToken(0))) return 0;
267 if (!strcmp(token, "word"))
268 odp->u.func.arg_types[i] = 'w';
269 else if (!strcmp(token, "s_word"))
270 odp->u.func.arg_types[i] = 's';
271 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
272 odp->u.func.arg_types[i] = 'l';
273 else if (!strcmp(token, "ptr"))
274 odp->u.func.arg_types[i] = 'p';
275 else if (!strcmp(token, "str"))
276 odp->u.func.arg_types[i] = 't';
277 else if (!strcmp(token, "wstr"))
278 odp->u.func.arg_types[i] = 'W';
279 else if (!strcmp(token, "segstr"))
280 odp->u.func.arg_types[i] = 'T';
281 else if (!strcmp(token, "double"))
283 odp->u.func.arg_types[i++] = 'l';
284 if (get_ptr_size() == 4 && i < sizeof(odp->u.func.arg_types))
285 odp->u.func.arg_types[i] = 'l';
289 error( "Unknown argument type '%s'\n", token );
293 if (spec->type == SPEC_WIN32)
295 if (strcmp(token, "long") &&
296 strcmp(token, "ptr") &&
297 strcmp(token, "str") &&
298 strcmp(token, "wstr") &&
299 strcmp(token, "double"))
301 error( "Type '%s' not supported for Win32\n", token );
306 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
308 error( "Too many arguments\n" );
312 odp->u.func.arg_types[i] = '\0';
313 if (odp->type == TYPE_VARARGS)
314 odp->flags |= FLAG_NORELAY; /* no relay debug possible for varags entry point */
316 if (!(token = GetToken(1)))
318 if (!strcmp( odp->name, "@" ))
320 error( "Missing handler name for anonymous function\n" );
323 odp->link_name = xstrdup( odp->name );
327 odp->link_name = xstrdup( token );
328 if (strchr( odp->link_name, '.' ))
330 if (spec->type == SPEC_WIN16)
332 error( "Forwarded functions not supported for Win16\n" );
335 odp->flags |= FLAG_FORWARD;
342 /*******************************************************************
345 * Parse an 'equate' definition in a .spec file.
347 static int parse_spec_equate( ORDDEF *odp, DLLSPEC *spec )
353 if (spec->type == SPEC_WIN32)
355 error( "'equate' not supported for Win32\n" );
358 if (!(token = GetToken(0))) return 0;
359 value = strtol(token, &endptr, 0);
360 if (endptr == NULL || *endptr != '\0')
362 error( "Expected number value, got '%s'\n", token );
365 if (value < -0x8000 || value > 0xffff)
367 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value );
370 odp->u.abs.value = value;
375 /*******************************************************************
378 * Parse a 'stub' definition in a .spec file
380 static int parse_spec_stub( ORDDEF *odp, DLLSPEC *spec )
382 odp->u.func.arg_types[0] = '\0';
383 odp->link_name = xstrdup("");
384 odp->flags |= FLAG_CPU(CPU_x86) | FLAG_CPU(CPU_x86_64); /* don't bother generating stubs for Winelib */
389 /*******************************************************************
392 * Parse an 'extern' definition in a .spec file.
394 static int parse_spec_extern( ORDDEF *odp, DLLSPEC *spec )
398 if (spec->type == SPEC_WIN16)
400 error( "'extern' not supported for Win16, use 'variable' instead\n" );
403 if (!(token = GetToken(1)))
405 if (!strcmp( odp->name, "@" ))
407 error( "Missing handler name for anonymous extern\n" );
410 odp->link_name = xstrdup( odp->name );
414 odp->link_name = xstrdup( token );
415 if (strchr( odp->link_name, '.' )) odp->flags |= FLAG_FORWARD;
421 /*******************************************************************
424 * Parse the optional flags for an entry point in a .spec file.
426 static const char *parse_spec_flags( ORDDEF *odp )
433 if (!(token = GetToken(0))) break;
434 if (!strncmp( token, "arch=", 5))
436 char *args = xstrdup( token + 5 );
437 char *cpu_name = strtok( args, "," );
440 enum target_cpu cpu = get_cpu_from_name( cpu_name );
443 error( "Unknown architecture '%s'\n", cpu_name );
446 odp->flags |= FLAG_CPU( cpu );
447 cpu_name = strtok( NULL, "," );
451 else if (!strcmp( token, "i386" )) /* backwards compatibility */
453 odp->flags |= FLAG_CPU(CPU_x86);
457 for (i = 0; FlagNames[i]; i++)
458 if (!strcmp( FlagNames[i], token )) break;
461 error( "Unknown flag '%s'\n", token );
464 odp->flags |= 1 << i;
467 } while (token && *token == '-');
473 /*******************************************************************
476 * Parse an ordinal definition in a .spec file.
478 static int parse_spec_ordinal( int ordinal, DLLSPEC *spec )
482 ORDDEF *odp = add_entry_point( spec );
484 if (!(token = GetToken(0))) goto error;
486 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
487 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
490 if (odp->type >= TYPE_NBTYPES)
492 error( "Expected type after ordinal, found '%s' instead\n", token );
496 if (!(token = GetToken(0))) goto error;
497 if (*token == '-' && !(token = parse_spec_flags( odp ))) goto error;
499 odp->name = xstrdup( token );
500 odp->lineno = current_line;
501 odp->ordinal = ordinal;
503 len = strspn( odp->name, valid_ordname_chars );
504 if (len < strlen( odp->name ))
506 error( "Character '%c' is not allowed in exported name '%s'\n", odp->name[len], odp->name );
513 if (!parse_spec_variable( odp, spec )) goto error;
519 if (!parse_spec_export( odp, spec )) goto error;
522 if (!parse_spec_equate( odp, spec )) goto error;
525 if (!parse_spec_stub( odp, spec )) goto error;
528 if (!parse_spec_extern( odp, spec )) goto error;
534 if ((odp->flags & FLAG_CPU_MASK) && !(odp->flags & FLAG_CPU(target_cpu)))
536 /* ignore this entry point */
537 spec->nb_entry_points--;
545 error( "Ordinal 0 is not valid\n" );
548 if (ordinal >= MAX_ORDINALS)
550 error( "Ordinal number %d too large\n", ordinal );
553 if (ordinal > spec->limit) spec->limit = ordinal;
554 if (ordinal < spec->base) spec->base = ordinal;
555 odp->ordinal = ordinal;
558 if (odp->type == TYPE_STDCALL && !(odp->flags & FLAG_PRIVATE))
560 if (!strcmp( odp->name, "DllRegisterServer" ) ||
561 !strcmp( odp->name, "DllUnregisterServer" ) ||
562 !strcmp( odp->name, "DllGetClassObject" ) ||
563 !strcmp( odp->name, "DllGetVersion" ) ||
564 !strcmp( odp->name, "DllInstall" ) ||
565 !strcmp( odp->name, "DllCanUnloadNow" ))
567 warning( "Function %s should be marked private\n", odp->name );
568 if (strcmp( odp->name, odp->link_name ))
569 warning( "Function %s should not use a different internal name (%s)\n",
570 odp->name, odp->link_name );
574 if (!strcmp( odp->name, "@" ) || odp->flags & (FLAG_NONAME | FLAG_ORDINAL))
578 if (!strcmp( odp->name, "@" ))
579 error( "Nameless function needs an explicit ordinal number\n" );
581 error( "Function imported by ordinal needs an explicit ordinal number\n" );
584 if (spec->type != SPEC_WIN32)
586 error( "Nameless functions not supported for Win16\n" );
589 if (!strcmp( odp->name, "@" ))
594 else if (!(odp->flags & FLAG_ORDINAL)) /* -ordinal only affects the import library */
596 odp->export_name = odp->name;
603 spec->nb_entry_points--;
609 static int name_compare( const void *ptr1, const void *ptr2 )
611 const ORDDEF *odp1 = *(const ORDDEF * const *)ptr1;
612 const ORDDEF *odp2 = *(const ORDDEF * const *)ptr2;
613 const char *name1 = odp1->name ? odp1->name : odp1->export_name;
614 const char *name2 = odp2->name ? odp2->name : odp2->export_name;
615 return strcmp( name1, name2 );
618 /*******************************************************************
621 * Build the name array and catch duplicates.
623 static void assign_names( DLLSPEC *spec )
625 int i, j, nb_exp_names = 0;
629 for (i = 0; i < spec->nb_entry_points; i++)
630 if (spec->entry_points[i].name) spec->nb_names++;
631 else if (spec->entry_points[i].export_name) nb_exp_names++;
633 if (!spec->nb_names && !nb_exp_names) return;
635 /* check for duplicates */
637 all_names = xmalloc( (spec->nb_names + nb_exp_names) * sizeof(all_names[0]) );
638 for (i = j = 0; i < spec->nb_entry_points; i++)
639 if (spec->entry_points[i].name || spec->entry_points[i].export_name)
640 all_names[j++] = &spec->entry_points[i];
642 qsort( all_names, j, sizeof(all_names[0]), name_compare );
644 for (i = 0; i < j - 1; i++)
646 const char *name1 = all_names[i]->name ? all_names[i]->name : all_names[i]->export_name;
647 const char *name2 = all_names[i+1]->name ? all_names[i+1]->name : all_names[i+1]->export_name;
648 if (!strcmp( name1, name2 ))
650 current_line = max( all_names[i]->lineno, all_names[i+1]->lineno );
651 error( "'%s' redefined\n%s:%d: First defined here\n",
652 name1, input_file_name,
653 min( all_names[i]->lineno, all_names[i+1]->lineno ) );
660 spec->names = xmalloc( spec->nb_names * sizeof(spec->names[0]) );
661 for (i = j = 0; i < spec->nb_entry_points; i++)
662 if (spec->entry_points[i].name) spec->names[j++] = &spec->entry_points[i];
664 /* sort the list of names */
665 qsort( spec->names, spec->nb_names, sizeof(spec->names[0]), name_compare );
669 /*******************************************************************
672 * Build the ordinal array.
674 static void assign_ordinals( DLLSPEC *spec )
676 int i, count, ordinal;
678 /* start assigning from base, or from 1 if no ordinal defined yet */
680 spec->base = MAX_ORDINALS;
682 for (i = 0; i < spec->nb_entry_points; i++)
684 ordinal = spec->entry_points[i].ordinal;
685 if (ordinal == -1) continue;
686 if (ordinal > spec->limit) spec->limit = ordinal;
687 if (ordinal < spec->base) spec->base = ordinal;
689 if (spec->base == MAX_ORDINALS) spec->base = 1;
690 if (spec->limit < spec->base) spec->limit = spec->base;
692 count = max( spec->limit + 1, spec->base + spec->nb_entry_points );
693 spec->ordinals = xmalloc( count * sizeof(spec->ordinals[0]) );
694 memset( spec->ordinals, 0, count * sizeof(spec->ordinals[0]) );
696 /* fill in all explicitly specified ordinals */
697 for (i = 0; i < spec->nb_entry_points; i++)
699 ordinal = spec->entry_points[i].ordinal;
700 if (ordinal == -1) continue;
701 if (spec->ordinals[ordinal])
703 current_line = max( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno );
704 error( "ordinal %d redefined\n%s:%d: First defined here\n",
705 ordinal, input_file_name,
706 min( spec->entry_points[i].lineno, spec->ordinals[ordinal]->lineno ) );
708 else spec->ordinals[ordinal] = &spec->entry_points[i];
711 /* now assign ordinals to the rest */
712 for (i = 0, ordinal = spec->base; i < spec->nb_entry_points; i++)
714 if (spec->entry_points[i].ordinal != -1) continue;
715 while (spec->ordinals[ordinal]) ordinal++;
716 if (ordinal >= MAX_ORDINALS)
718 current_line = spec->entry_points[i].lineno;
719 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS );
721 spec->entry_points[i].ordinal = ordinal;
722 spec->ordinals[ordinal] = &spec->entry_points[i];
724 if (ordinal > spec->limit) spec->limit = ordinal;
728 /*******************************************************************
731 * Add the necessary exports to the 32-bit counterpart of a 16-bit module.
733 void add_16bit_exports( DLLSPEC *spec32, DLLSPEC *spec16 )
737 /* add an export for the NE module */
739 odp = add_entry_point( spec32 );
740 odp->type = TYPE_EXTERN;
741 odp->name = xstrdup( "__wine_spec_dos_header" );
744 odp->link_name = xstrdup( ".L__wine_spec_dos_header" );
746 if (spec16->main_module)
748 odp = add_entry_point( spec32 );
749 odp->type = TYPE_EXTERN;
750 odp->name = xstrdup( "__wine_spec_main_module" );
753 odp->link_name = xstrdup( ".L__wine_spec_main_module" );
756 assign_names( spec32 );
757 assign_ordinals( spec32 );
761 /*******************************************************************
764 * Parse a .spec file.
766 int parse_spec_file( FILE *file, DLLSPEC *spec )
773 comment_chars = "#;";
774 separator_chars = "()-";
776 while (get_next_line())
778 if (!(token = GetToken(1))) continue;
779 if (strcmp(token, "@") == 0)
781 if (spec->type != SPEC_WIN32)
783 error( "'@' ordinals not supported for Win16\n" );
786 if (!parse_spec_ordinal( -1, spec )) continue;
788 else if (IsNumberString(token))
790 if (!parse_spec_ordinal( atoi(token), spec )) continue;
794 error( "Expected ordinal declaration, got '%s'\n", token );
797 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
800 current_line = 0; /* no longer parsing the input file */
801 assign_names( spec );
802 assign_ordinals( spec );
807 /*******************************************************************
810 * Parse a LIBRARY declaration in a .def file.
812 static int parse_def_library( DLLSPEC *spec )
814 const char *token = GetToken(1);
816 if (!token) return 1;
817 if (strcmp( token, "BASE" ))
819 free( spec->file_name );
820 spec->file_name = xstrdup( token );
821 if (!(token = GetToken(1))) return 1;
823 if (strcmp( token, "BASE" ))
825 error( "Expected library name or BASE= declaration, got '%s'\n", token );
828 if (!(token = GetToken(0))) return 0;
829 if (strcmp( token, "=" ))
831 error( "Expected '=' after BASE, got '%s'\n", token );
834 if (!(token = GetToken(0))) return 0;
835 /* FIXME: do something with base address */
841 /*******************************************************************
842 * parse_def_stack_heap_size
844 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
846 static int parse_def_stack_heap_size( int is_stack, DLLSPEC *spec )
848 const char *token = GetToken(0);
852 if (!token) return 0;
853 size = strtoul( token, &end, 0 );
856 error( "Invalid number '%s'\n", token );
859 if (is_stack) spec->stack_size = size / 1024;
860 else spec->heap_size = size / 1024;
861 if (!(token = GetToken(1))) return 1;
862 if (strcmp( token, "," ))
864 error( "Expected ',' after size, got '%s'\n", token );
867 if (!(token = GetToken(0))) return 0;
868 /* FIXME: do something with reserve size */
873 /*******************************************************************
876 * Parse an export declaration in a .def file.
878 static int parse_def_export( char *name, DLLSPEC *spec )
881 const char *token = GetToken(1);
882 ORDDEF *odp = add_entry_point( spec );
884 odp->lineno = current_line;
887 args = remove_stdcall_decoration( odp->name );
888 if (args == -1) odp->type = TYPE_CDECL;
891 odp->type = TYPE_STDCALL;
892 args /= get_ptr_size();
893 if (args >= sizeof(odp->u.func.arg_types))
895 error( "Too many arguments in stdcall function '%s'\n", odp->name );
898 for (i = 0; i < args; i++) odp->u.func.arg_types[i] = 'l';
901 /* check for optional internal name */
903 if (token && !strcmp( token, "=" ))
905 if (!(token = GetToken(0))) goto error;
906 odp->link_name = xstrdup( token );
907 remove_stdcall_decoration( odp->link_name );
912 odp->link_name = xstrdup( name );
915 /* check for optional ordinal */
917 if (token && token[0] == '@')
921 if (!IsNumberString( token+1 ))
923 error( "Expected number after '@', got '%s'\n", token+1 );
926 ordinal = atoi( token+1 );
929 error( "Ordinal 0 is not valid\n" );
932 if (ordinal >= MAX_ORDINALS)
934 error( "Ordinal number %d too large\n", ordinal );
937 odp->ordinal = ordinal;
941 /* check for other optional keywords */
943 if (token && !strcmp( token, "NONAME" ))
945 if (odp->ordinal == -1)
947 error( "NONAME requires an ordinal\n" );
950 odp->export_name = odp->name;
952 odp->flags |= FLAG_NONAME;
955 if (token && !strcmp( token, "PRIVATE" ))
957 odp->flags |= FLAG_PRIVATE;
960 if (token && !strcmp( token, "DATA" ))
962 odp->type = TYPE_EXTERN;
967 error( "Garbage text '%s' found at end of export declaration\n", token );
973 spec->nb_entry_points--;
979 /*******************************************************************
984 int parse_def_file( FILE *file, DLLSPEC *spec )
993 separator_chars = ",=";
995 while (get_next_line())
997 if (!(token = GetToken(1))) continue;
999 if (!strcmp( token, "LIBRARY" ) || !strcmp( token, "NAME" ))
1001 if (!parse_def_library( spec )) continue;
1004 else if (!strcmp( token, "STACKSIZE" ))
1006 if (!parse_def_stack_heap_size( 1, spec )) continue;
1009 else if (!strcmp( token, "HEAPSIZE" ))
1011 if (!parse_def_stack_heap_size( 0, spec )) continue;
1014 else if (!strcmp( token, "EXPORTS" ))
1017 if (!(token = GetToken(1))) continue;
1019 else if (!strcmp( token, "IMPORTS" ))
1022 if (!(token = GetToken(1))) continue;
1024 else if (!strcmp( token, "SECTIONS" ))
1027 if (!(token = GetToken(1))) continue;
1030 if (!in_exports) continue; /* ignore this line */
1031 if (!parse_def_export( xstrdup(token), spec )) continue;
1034 if ((token = GetToken(1))) error( "Syntax error near '%s'\n", token );
1037 current_line = 0; /* no longer parsing the input file */
1038 assign_names( spec );
1039 assign_ordinals( spec );