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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 static char ParseBuffer
[512];
40 static char TokenBuffer
[512];
41 static char *ParseNext
= ParseBuffer
;
42 static FILE *input_file
;
44 static const char *separator_chars
;
45 static const char *comment_chars
;
47 static const char * const TypeNames
[TYPE_NBTYPES
] =
49 "variable", /* TYPE_VARIABLE */
50 "pascal", /* TYPE_PASCAL */
51 "equate", /* TYPE_ABS */
52 "stub", /* TYPE_STUB */
53 "stdcall", /* TYPE_STDCALL */
54 "cdecl", /* TYPE_CDECL */
55 "varargs", /* TYPE_VARARGS */
56 "extern" /* TYPE_EXTERN */
59 static const char * const FlagNames
[] =
61 "norelay", /* FLAG_NORELAY */
62 "noname", /* FLAG_NONAME */
63 "ret16", /* FLAG_RET16 */
64 "ret64", /* FLAG_RET64 */
65 "i386", /* FLAG_I386 */
66 "register", /* FLAG_REGISTER */
67 "private", /* FLAG_PRIVATE */
71 static int IsNumberString(const char *s
)
73 while (*s
) if (!isdigit(*s
++)) return 0;
77 inline static int is_token_separator( char ch
)
79 return strchr( separator_chars
, ch
) != NULL
;
82 inline static int is_token_comment( char ch
)
84 return strchr( comment_chars
, ch
) != NULL
;
87 /* get the next line from the input file, or return 0 if at eof */
88 static int get_next_line(void)
90 ParseNext
= ParseBuffer
;
92 return (fgets(ParseBuffer
, sizeof(ParseBuffer
), input_file
) != NULL
);
95 static const char * GetToken( int allow_eol
)
98 char *token
= TokenBuffer
;
102 /* remove initial white space */
104 while (isspace(*p
)) p
++;
106 if (*p
== '\\' && p
[1] == '\n') /* line continuation */
108 if (!get_next_line())
110 if (!allow_eol
) error( "Unexpected end of file\n" );
117 if ((*p
== '\0') || is_token_comment(*p
))
119 if (!allow_eol
) error( "Declaration not terminated properly\n" );
126 if (is_token_separator(*p
))
128 /* a separator is always a complete token */
131 else while (*p
!= '\0' && !is_token_separator(*p
) && !isspace(*p
))
134 if (*p
) *token
++ = *p
++;
142 static ORDDEF
*add_entry_point( DLLSPEC
*spec
)
144 if (spec
->nb_entry_points
== spec
->alloc_entry_points
)
146 spec
->alloc_entry_points
+= 128;
147 spec
->entry_points
= xrealloc( spec
->entry_points
,
148 spec
->alloc_entry_points
* sizeof(*spec
->entry_points
) );
150 return &spec
->entry_points
[spec
->nb_entry_points
++];
153 /*******************************************************************
154 * parse_spec_variable
156 * Parse a variable definition in a .spec file.
158 static int parse_spec_variable( ORDDEF
*odp
, DLLSPEC
*spec
)
163 int value_array_size
;
166 if (spec
->type
== SPEC_WIN32
)
168 error( "'variable' not supported in Win32, use 'extern' instead\n" );
172 if (!(token
= GetToken(0))) return 0;
175 error( "Expected '(' got '%s'\n", token
);
180 value_array_size
= 25;
181 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
185 if (!(token
= GetToken(0)))
193 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
194 if (n_values
== value_array_size
)
196 value_array_size
+= 25;
197 value_array
= xrealloc(value_array
,
198 sizeof(*value_array
) * value_array_size
);
201 if (endptr
== NULL
|| *endptr
!= '\0')
203 error( "Expected number value, got '%s'\n", token
);
209 odp
->u
.var
.n_values
= n_values
;
210 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
215 /*******************************************************************
218 * Parse an exported function definition in a .spec file.
220 static int parse_spec_export( ORDDEF
*odp
, DLLSPEC
*spec
)
228 if (odp
->type
== TYPE_STDCALL
)
230 error( "'stdcall' not supported for Win16\n" );
235 if (odp
->type
== TYPE_PASCAL
)
237 error( "'pascal' not supported for Win32\n" );
245 if (!(token
= GetToken(0))) return 0;
248 error( "Expected '(' got '%s'\n", token
);
252 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
); i
++)
254 if (!(token
= GetToken(0))) return 0;
258 if (!strcmp(token
, "word"))
259 odp
->u
.func
.arg_types
[i
] = 'w';
260 else if (!strcmp(token
, "s_word"))
261 odp
->u
.func
.arg_types
[i
] = 's';
262 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
263 odp
->u
.func
.arg_types
[i
] = 'l';
264 else if (!strcmp(token
, "ptr"))
265 odp
->u
.func
.arg_types
[i
] = 'p';
266 else if (!strcmp(token
, "str"))
267 odp
->u
.func
.arg_types
[i
] = 't';
268 else if (!strcmp(token
, "wstr"))
269 odp
->u
.func
.arg_types
[i
] = 'W';
270 else if (!strcmp(token
, "segstr"))
271 odp
->u
.func
.arg_types
[i
] = 'T';
272 else if (!strcmp(token
, "double"))
274 odp
->u
.func
.arg_types
[i
++] = 'l';
275 if (get_ptr_size() == 4 && i
< sizeof(odp
->u
.func
.arg_types
))
276 odp
->u
.func
.arg_types
[i
] = 'l';
280 error( "Unknown argument type '%s'\n", token
);
284 if (spec
->type
== SPEC_WIN32
)
286 if (strcmp(token
, "long") &&
287 strcmp(token
, "ptr") &&
288 strcmp(token
, "str") &&
289 strcmp(token
, "wstr") &&
290 strcmp(token
, "double"))
292 error( "Type '%s' not supported for Win32\n", token
);
297 if ((*token
!= ')') || (i
>= sizeof(odp
->u
.func
.arg_types
)))
299 error( "Too many arguments\n" );
303 odp
->u
.func
.arg_types
[i
] = '\0';
304 if (odp
->type
== TYPE_VARARGS
)
305 odp
->flags
|= FLAG_NORELAY
; /* no relay debug possible for varags entry point */
307 if (!(token
= GetToken(1)))
309 if (!strcmp( odp
->name
, "@" ))
311 error( "Missing handler name for anonymous function\n" );
314 odp
->link_name
= xstrdup( odp
->name
);
318 odp
->link_name
= xstrdup( token
);
319 if (strchr( odp
->link_name
, '.' ))
321 if (spec
->type
== SPEC_WIN16
)
323 error( "Forwarded functions not supported for Win16\n" );
326 odp
->flags
|= FLAG_FORWARD
;
333 /*******************************************************************
336 * Parse an 'equate' definition in a .spec file.
338 static int parse_spec_equate( ORDDEF
*odp
, DLLSPEC
*spec
)
344 if (spec
->type
== SPEC_WIN32
)
346 error( "'equate' not supported for Win32\n" );
349 if (!(token
= GetToken(0))) return 0;
350 value
= strtol(token
, &endptr
, 0);
351 if (endptr
== NULL
|| *endptr
!= '\0')
353 error( "Expected number value, got '%s'\n", token
);
356 if (value
< -0x8000 || value
> 0xffff)
358 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value
);
361 odp
->u
.abs
.value
= value
;
366 /*******************************************************************
369 * Parse a 'stub' definition in a .spec file
371 static int parse_spec_stub( ORDDEF
*odp
, DLLSPEC
*spec
)
373 odp
->u
.func
.arg_types
[0] = '\0';
374 odp
->link_name
= xstrdup("");
375 odp
->flags
|= FLAG_I386
; /* don't bother generating stubs for Winelib */
380 /*******************************************************************
383 * Parse an 'extern' definition in a .spec file.
385 static int parse_spec_extern( ORDDEF
*odp
, DLLSPEC
*spec
)
389 if (spec
->type
== SPEC_WIN16
)
391 error( "'extern' not supported for Win16, use 'variable' instead\n" );
394 if (!(token
= GetToken(1)))
396 if (!strcmp( odp
->name
, "@" ))
398 error( "Missing handler name for anonymous extern\n" );
401 odp
->link_name
= xstrdup( odp
->name
);
405 odp
->link_name
= xstrdup( token
);
406 if (strchr( odp
->link_name
, '.' )) odp
->flags
|= FLAG_FORWARD
;
412 /*******************************************************************
415 * Parse the optional flags for an entry point in a .spec file.
417 static const char *parse_spec_flags( ORDDEF
*odp
)
424 if (!(token
= GetToken(0))) break;
425 for (i
= 0; FlagNames
[i
]; i
++)
426 if (!strcmp( FlagNames
[i
], token
)) break;
429 error( "Unknown flag '%s'\n", token
);
432 odp
->flags
|= 1 << i
;
434 } while (token
&& *token
== '-');
440 /*******************************************************************
443 * Parse an ordinal definition in a .spec file.
445 static int parse_spec_ordinal( int ordinal
, DLLSPEC
*spec
)
449 ORDDEF
*odp
= add_entry_point( spec
);
450 memset( odp
, 0, sizeof(*odp
) );
452 if (!(token
= GetToken(0))) goto error
;
454 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
455 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
458 if (odp
->type
>= TYPE_NBTYPES
)
460 error( "Expected type after ordinal, found '%s' instead\n", token
);
464 if (!(token
= GetToken(0))) goto error
;
465 if (*token
== '-' && !(token
= parse_spec_flags( odp
))) goto error
;
467 odp
->name
= xstrdup( token
);
468 odp
->lineno
= current_line
;
469 odp
->ordinal
= ordinal
;
474 if (!parse_spec_variable( odp
, spec
)) goto error
;
480 if (!parse_spec_export( odp
, spec
)) goto error
;
483 if (!parse_spec_equate( odp
, spec
)) goto error
;
486 if (!parse_spec_stub( odp
, spec
)) goto error
;
489 if (!parse_spec_extern( odp
, spec
)) goto error
;
495 if ((target_cpu
!= CPU_x86
) && (odp
->flags
& FLAG_I386
))
497 /* ignore this entry point on non-Intel archs */
498 spec
->nb_entry_points
--;
506 error( "Ordinal 0 is not valid\n" );
509 if (ordinal
>= MAX_ORDINALS
)
511 error( "Ordinal number %d too large\n", ordinal
);
514 if (ordinal
> spec
->limit
) spec
->limit
= ordinal
;
515 if (ordinal
< spec
->base
) spec
->base
= ordinal
;
516 odp
->ordinal
= ordinal
;
519 if (odp
->type
== TYPE_STDCALL
&& !(odp
->flags
& FLAG_PRIVATE
))
521 if (!strcmp( odp
->name
, "DllRegisterServer" ) ||
522 !strcmp( odp
->name
, "DllUnregisterServer" ) ||
523 !strcmp( odp
->name
, "DllGetClassObject" ) ||
524 !strcmp( odp
->name
, "DllGetVersion" ) ||
525 !strcmp( odp
->name
, "DllInstall" ) ||
526 !strcmp( odp
->name
, "DllCanUnloadNow" ))
528 warning( "Function %s should be marked private\n", odp
->name
);
529 if (strcmp( odp
->name
, odp
->link_name
))
530 warning( "Function %s should not use a different internal name (%s)\n",
531 odp
->name
, odp
->link_name
);
535 if (!strcmp( odp
->name
, "@" ) || odp
->flags
& FLAG_NONAME
)
539 error( "Nameless function needs an explicit ordinal number\n" );
542 if (spec
->type
!= SPEC_WIN32
)
544 error( "Nameless functions not supported for Win16\n" );
547 if (!strcmp( odp
->name
, "@" )) free( odp
->name
);
548 else odp
->export_name
= odp
->name
;
554 spec
->nb_entry_points
--;
560 static int name_compare( const void *ptr1
, const void *ptr2
)
562 const ORDDEF
*odp1
= *(const ORDDEF
* const *)ptr1
;
563 const ORDDEF
*odp2
= *(const ORDDEF
* const *)ptr2
;
564 const char *name1
= odp1
->name
? odp1
->name
: odp1
->export_name
;
565 const char *name2
= odp2
->name
? odp2
->name
: odp2
->export_name
;
566 return strcmp( name1
, name2
);
569 /*******************************************************************
572 * Build the name array and catch duplicates.
574 static void assign_names( DLLSPEC
*spec
)
576 int i
, j
, nb_exp_names
= 0;
580 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
581 if (spec
->entry_points
[i
].name
) spec
->nb_names
++;
582 else if (spec
->entry_points
[i
].export_name
) nb_exp_names
++;
584 if (!spec
->nb_names
&& !nb_exp_names
) return;
586 /* check for duplicates */
588 all_names
= xmalloc( (spec
->nb_names
+ nb_exp_names
) * sizeof(all_names
[0]) );
589 for (i
= j
= 0; i
< spec
->nb_entry_points
; i
++)
590 if (spec
->entry_points
[i
].name
|| spec
->entry_points
[i
].export_name
)
591 all_names
[j
++] = &spec
->entry_points
[i
];
593 qsort( all_names
, j
, sizeof(all_names
[0]), name_compare
);
595 for (i
= 0; i
< j
- 1; i
++)
597 const char *name1
= all_names
[i
]->name
? all_names
[i
]->name
: all_names
[i
]->export_name
;
598 const char *name2
= all_names
[i
+1]->name
? all_names
[i
+1]->name
: all_names
[i
+1]->export_name
;
599 if (!strcmp( name1
, name2
))
601 current_line
= max( all_names
[i
]->lineno
, all_names
[i
+1]->lineno
);
602 error( "'%s' redefined\n%s:%d: First defined here\n",
603 name1
, input_file_name
,
604 min( all_names
[i
]->lineno
, all_names
[i
+1]->lineno
) );
611 spec
->names
= xmalloc( spec
->nb_names
* sizeof(spec
->names
[0]) );
612 for (i
= j
= 0; i
< spec
->nb_entry_points
; i
++)
613 if (spec
->entry_points
[i
].name
) spec
->names
[j
++] = &spec
->entry_points
[i
];
615 /* sort the list of names */
616 qsort( spec
->names
, spec
->nb_names
, sizeof(spec
->names
[0]), name_compare
);
620 /*******************************************************************
623 * Build the ordinal array.
625 static void assign_ordinals( DLLSPEC
*spec
)
627 int i
, count
, ordinal
;
629 /* start assigning from base, or from 1 if no ordinal defined yet */
631 spec
->base
= MAX_ORDINALS
;
633 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
635 ordinal
= spec
->entry_points
[i
].ordinal
;
636 if (ordinal
== -1) continue;
637 if (ordinal
> spec
->limit
) spec
->limit
= ordinal
;
638 if (ordinal
< spec
->base
) spec
->base
= ordinal
;
640 if (spec
->base
== MAX_ORDINALS
) spec
->base
= 1;
641 if (spec
->limit
< spec
->base
) spec
->limit
= spec
->base
;
643 count
= max( spec
->limit
+ 1, spec
->base
+ spec
->nb_entry_points
);
644 spec
->ordinals
= xmalloc( count
* sizeof(spec
->ordinals
[0]) );
645 memset( spec
->ordinals
, 0, count
* sizeof(spec
->ordinals
[0]) );
647 /* fill in all explicitly specified ordinals */
648 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
650 ordinal
= spec
->entry_points
[i
].ordinal
;
651 if (ordinal
== -1) continue;
652 if (spec
->ordinals
[ordinal
])
654 current_line
= max( spec
->entry_points
[i
].lineno
, spec
->ordinals
[ordinal
]->lineno
);
655 error( "ordinal %d redefined\n%s:%d: First defined here\n",
656 ordinal
, input_file_name
,
657 min( spec
->entry_points
[i
].lineno
, spec
->ordinals
[ordinal
]->lineno
) );
659 else spec
->ordinals
[ordinal
] = &spec
->entry_points
[i
];
662 /* now assign ordinals to the rest */
663 for (i
= 0, ordinal
= spec
->base
; i
< spec
->nb_entry_points
; i
++)
665 if (spec
->entry_points
[i
].ordinal
!= -1) continue;
666 while (spec
->ordinals
[ordinal
]) ordinal
++;
667 if (ordinal
>= MAX_ORDINALS
)
669 current_line
= spec
->entry_points
[i
].lineno
;
670 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS
);
672 spec
->entry_points
[i
].ordinal
= ordinal
;
673 spec
->ordinals
[ordinal
] = &spec
->entry_points
[i
];
675 if (ordinal
> spec
->limit
) spec
->limit
= ordinal
;
679 /*******************************************************************
682 * Parse a .spec file.
684 int parse_spec_file( FILE *file
, DLLSPEC
*spec
)
691 comment_chars
= "#;";
692 separator_chars
= "()-";
694 while (get_next_line())
696 if (!(token
= GetToken(1))) continue;
697 if (strcmp(token
, "@") == 0)
699 if (spec
->type
!= SPEC_WIN32
)
701 error( "'@' ordinals not supported for Win16\n" );
704 if (!parse_spec_ordinal( -1, spec
)) continue;
706 else if (IsNumberString(token
))
708 if (!parse_spec_ordinal( atoi(token
), spec
)) continue;
712 error( "Expected ordinal declaration, got '%s'\n", token
);
715 if ((token
= GetToken(1))) error( "Syntax error near '%s'\n", token
);
718 current_line
= 0; /* no longer parsing the input file */
719 assign_names( spec
);
720 assign_ordinals( spec
);
725 /*******************************************************************
728 * Parse a LIBRARY declaration in a .def file.
730 static int parse_def_library( DLLSPEC
*spec
)
732 const char *token
= GetToken(1);
734 if (!token
) return 1;
735 if (strcmp( token
, "BASE" ))
737 free( spec
->file_name
);
738 spec
->file_name
= xstrdup( token
);
739 if (!(token
= GetToken(1))) return 1;
741 if (strcmp( token
, "BASE" ))
743 error( "Expected library name or BASE= declaration, got '%s'\n", token
);
746 if (!(token
= GetToken(0))) return 0;
747 if (strcmp( token
, "=" ))
749 error( "Expected '=' after BASE, got '%s'\n", token
);
752 if (!(token
= GetToken(0))) return 0;
753 /* FIXME: do something with base address */
759 /*******************************************************************
760 * parse_def_stack_heap_size
762 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
764 static int parse_def_stack_heap_size( int is_stack
, DLLSPEC
*spec
)
766 const char *token
= GetToken(0);
770 if (!token
) return 0;
771 size
= strtoul( token
, &end
, 0 );
774 error( "Invalid number '%s'\n", token
);
777 if (is_stack
) spec
->stack_size
= size
/ 1024;
778 else spec
->heap_size
= size
/ 1024;
779 if (!(token
= GetToken(1))) return 1;
780 if (strcmp( token
, "," ))
782 error( "Expected ',' after size, got '%s'\n", token
);
785 if (!(token
= GetToken(0))) return 0;
786 /* FIXME: do something with reserve size */
791 /*******************************************************************
794 * Parse an export declaration in a .def file.
796 static int parse_def_export( char *name
, DLLSPEC
*spec
)
799 const char *token
= GetToken(1);
801 ORDDEF
*odp
= add_entry_point( spec
);
802 memset( odp
, 0, sizeof(*odp
) );
804 odp
->lineno
= current_line
;
807 args
= remove_stdcall_decoration( odp
->name
);
808 if (args
== -1) odp
->type
= TYPE_CDECL
;
811 odp
->type
= TYPE_STDCALL
;
812 args
/= get_ptr_size();
813 if (args
>= sizeof(odp
->u
.func
.arg_types
))
815 error( "Too many arguments in stdcall function '%s'\n", odp
->name
);
818 for (i
= 0; i
< args
; i
++) odp
->u
.func
.arg_types
[i
] = 'l';
821 /* check for optional internal name */
823 if (token
&& !strcmp( token
, "=" ))
825 if (!(token
= GetToken(0))) goto error
;
826 odp
->link_name
= xstrdup( token
);
827 remove_stdcall_decoration( odp
->link_name
);
832 odp
->link_name
= xstrdup( name
);
835 /* check for optional ordinal */
837 if (token
&& token
[0] == '@')
841 if (!IsNumberString( token
+1 ))
843 error( "Expected number after '@', got '%s'\n", token
+1 );
846 ordinal
= atoi( token
+1 );
849 error( "Ordinal 0 is not valid\n" );
852 if (ordinal
>= MAX_ORDINALS
)
854 error( "Ordinal number %d too large\n", ordinal
);
857 odp
->ordinal
= ordinal
;
861 /* check for other optional keywords */
863 if (token
&& !strcmp( token
, "NONAME" ))
865 if (odp
->ordinal
== -1)
867 error( "NONAME requires an ordinal\n" );
870 odp
->export_name
= odp
->name
;
872 odp
->flags
|= FLAG_NONAME
;
875 if (token
&& !strcmp( token
, "PRIVATE" ))
877 odp
->flags
|= FLAG_PRIVATE
;
880 if (token
&& !strcmp( token
, "DATA" ))
882 odp
->type
= TYPE_EXTERN
;
887 error( "Garbage text '%s' found at end of export declaration\n", token
);
893 spec
->nb_entry_points
--;
899 /*******************************************************************
904 int parse_def_file( FILE *file
, DLLSPEC
*spec
)
913 separator_chars
= ",=";
915 while (get_next_line())
917 if (!(token
= GetToken(1))) continue;
919 if (!strcmp( token
, "LIBRARY" ) || !strcmp( token
, "NAME" ))
921 if (!parse_def_library( spec
)) continue;
924 else if (!strcmp( token
, "STACKSIZE" ))
926 if (!parse_def_stack_heap_size( 1, spec
)) continue;
929 else if (!strcmp( token
, "HEAPSIZE" ))
931 if (!parse_def_stack_heap_size( 0, spec
)) continue;
934 else if (!strcmp( token
, "EXPORTS" ))
937 if (!(token
= GetToken(1))) continue;
939 else if (!strcmp( token
, "IMPORTS" ))
942 if (!(token
= GetToken(1))) continue;
944 else if (!strcmp( token
, "SECTIONS" ))
947 if (!(token
= GetToken(1))) continue;
950 if (!in_exports
) continue; /* ignore this line */
951 if (!parse_def_export( xstrdup(token
), spec
)) continue;
954 if ((token
= GetToken(1))) error( "Syntax error near '%s'\n", token
);
957 current_line
= 0; /* no longer parsing the input file */
958 assign_names( spec
);
959 assign_ordinals( spec
);