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
38 static char ParseBuffer
[512];
39 static char TokenBuffer
[512];
40 static char *ParseNext
= ParseBuffer
;
41 static FILE *input_file
;
43 static const char *separator_chars
;
44 static const char *comment_chars
;
46 /* valid characters in ordinal names */
47 static const char valid_ordname_chars
[] = "/$:-_@?<>abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
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 "register", /* FLAG_REGISTER */
68 "private", /* FLAG_PRIVATE */
69 "ordinal", /* FLAG_ORDINAL */
70 "thiscall", /* FLAG_THISCALL */
71 "fastcall", /* FLAG_FASTCALL */
72 "syscall", /* FLAG_SYSCALL */
73 "import", /* FLAG_IMPORT */
77 static const char * const ArgNames
[ARG_MAXARG
+ 1] =
79 "word", /* ARG_WORD */
80 "s_word", /* ARG_SWORD */
81 "segptr", /* ARG_SEGPTR */
82 "segstr", /* ARG_SEGSTR */
83 "long", /* ARG_LONG */
86 "wstr", /* ARG_WSTR */
87 "int64", /* ARG_INT64 */
88 "int128", /* ARG_INT128 */
89 "float", /* ARG_FLOAT */
90 "double" /* ARG_DOUBLE */
93 static int IsNumberString(const char *s
)
95 while (*s
) if (!isdigit(*s
++)) return 0;
99 static inline int is_token_separator( char ch
)
101 return strchr( separator_chars
, ch
) != NULL
;
104 static inline int is_token_comment( char ch
)
106 return strchr( comment_chars
, ch
) != NULL
;
109 /* get the next line from the input file, or return 0 if at eof */
110 static int get_next_line(void)
112 ParseNext
= ParseBuffer
;
114 return (fgets(ParseBuffer
, sizeof(ParseBuffer
), input_file
) != NULL
);
117 static const char * GetToken( int allow_eol
)
120 char *token
= TokenBuffer
;
124 /* remove initial white space */
126 while (isspace(*p
)) p
++;
128 if (*p
== '\\' && p
[1] == '\n') /* line continuation */
130 if (!get_next_line())
132 if (!allow_eol
) error( "Unexpected end of file\n" );
139 if ((*p
== '\0') || is_token_comment(*p
))
141 if (!allow_eol
) error( "Declaration not terminated properly\n" );
148 if (is_token_separator(*p
))
150 /* a separator is always a complete token */
153 else while (*p
!= '\0' && !is_token_separator(*p
) && !isspace(*p
))
156 if (*p
) *token
++ = *p
++;
164 static ORDDEF
*add_entry_point( DLLSPEC
*spec
)
168 if (spec
->nb_entry_points
== spec
->alloc_entry_points
)
170 spec
->alloc_entry_points
+= 128;
171 spec
->entry_points
= xrealloc( spec
->entry_points
,
172 spec
->alloc_entry_points
* sizeof(*spec
->entry_points
) );
174 ret
= &spec
->entry_points
[spec
->nb_entry_points
++];
175 memset( ret
, 0, sizeof(*ret
) );
179 /*******************************************************************
180 * parse_spec_variable
182 * Parse a variable definition in a .spec file.
184 static int parse_spec_variable( ORDDEF
*odp
, DLLSPEC
*spec
)
187 unsigned int *value_array
;
189 int value_array_size
;
192 if (spec
->type
== SPEC_WIN32
)
194 error( "'variable' not supported in Win32, use 'extern' instead\n" );
198 if (!(token
= GetToken(0))) return 0;
201 error( "Expected '(' got '%s'\n", token
);
206 value_array_size
= 25;
207 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
211 if (!(token
= GetToken(0)))
219 value_array
[n_values
++] = strtoul(token
, &endptr
, 0);
220 if (n_values
== value_array_size
)
222 value_array_size
+= 25;
223 value_array
= xrealloc(value_array
,
224 sizeof(*value_array
) * value_array_size
);
227 if (endptr
== NULL
|| *endptr
!= '\0')
229 error( "Expected number value, got '%s'\n", token
);
235 odp
->u
.var
.n_values
= n_values
;
236 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
241 /*******************************************************************
242 * parse_spec_arguments
244 * Parse the arguments of an entry point.
246 static int parse_spec_arguments( ORDDEF
*odp
, DLLSPEC
*spec
, int optional
)
250 int is_win32
= (spec
->type
== SPEC_WIN32
) || (odp
->flags
& FLAG_EXPORT32
);
252 if (!(token
= GetToken( optional
))) return optional
;
255 error( "Expected '(' got '%s'\n", token
);
259 odp
->u
.func
.nb_args
= 0;
260 for (i
= 0; i
< MAX_ARGUMENTS
; i
++)
262 if (!(token
= GetToken(0))) return 0;
266 for (arg
= 0; arg
<= ARG_MAXARG
; arg
++)
267 if (!strcmp( ArgNames
[arg
], token
)) break;
269 if (arg
> ARG_MAXARG
)
271 error( "Unknown argument type '%s'\n", token
);
280 if (!is_win32
) break;
281 error( "Argument type '%s' only allowed for Win16\n", token
);
285 if (!(odp
->flags
& FLAG_SYSCALL
)) break;
286 error( "Argument type '%s' not allowed for syscall function\n", token
);
289 odp
->u
.func
.args
[i
] = arg
;
293 error( "Too many arguments\n" );
297 odp
->u
.func
.nb_args
= i
;
298 if (odp
->flags
& FLAG_THISCALL
)
300 if (odp
->type
!= TYPE_STDCALL
)
302 error( "A thiscall function must use the stdcall convention\n" );
305 if (!i
|| odp
->u
.func
.args
[0] != ARG_PTR
)
307 error( "First argument of a thiscall function must be a pointer\n" );
311 if (odp
->flags
& FLAG_FASTCALL
)
313 if (odp
->type
!= TYPE_STDCALL
)
315 error( "A fastcall function must use the stdcall convention\n" );
318 if ((i
&& odp
->u
.func
.args
[0] != ARG_PTR
&& odp
->u
.func
.args
[0] != ARG_LONG
) ||
319 (i
> 1 && odp
->u
.func
.args
[1] != ARG_PTR
&& odp
->u
.func
.args
[1] != ARG_LONG
))
320 odp
->flags
|= FLAG_NORELAY
; /* no relay debug possible for non-standard fastcall args */
322 if (odp
->flags
& FLAG_SYSCALL
)
324 if (odp
->type
!= TYPE_STDCALL
)
326 error( "A syscall function must use the stdcall convention\n" );
334 /*******************************************************************
337 * Parse an exported function definition in a .spec file.
339 static int parse_spec_export( ORDDEF
*odp
, DLLSPEC
*spec
)
342 int is_win32
= (spec
->type
== SPEC_WIN32
) || (odp
->flags
& FLAG_EXPORT32
);
344 if (!is_win32
&& odp
->type
== TYPE_STDCALL
)
346 error( "'stdcall' not supported for Win16\n" );
349 if (is_win32
&& odp
->type
== TYPE_PASCAL
)
351 error( "'pascal' not supported for Win32\n" );
355 if (!parse_spec_arguments( odp
, spec
, 0 )) return 0;
357 if (odp
->type
== TYPE_VARARGS
)
358 odp
->flags
|= FLAG_NORELAY
; /* no relay debug possible for varags entry point */
360 if (target
.cpu
!= CPU_i386
)
361 odp
->flags
&= ~(FLAG_THISCALL
| FLAG_FASTCALL
);
363 if (!(token
= GetToken(1)))
365 if (!strcmp( odp
->name
, "@" ))
367 error( "Missing handler name for anonymous function\n" );
370 odp
->link_name
= xstrdup( odp
->name
);
374 odp
->link_name
= xstrdup( token
);
375 if (strchr( odp
->link_name
, '.' ))
379 error( "Forwarded functions not supported for Win16\n" );
382 odp
->flags
|= FLAG_FORWARD
;
389 /*******************************************************************
392 * Parse an 'equate' definition in a .spec file.
394 static int parse_spec_equate( ORDDEF
*odp
, DLLSPEC
*spec
)
400 if (spec
->type
== SPEC_WIN32
)
402 error( "'equate' not supported for Win32\n" );
405 if (!(token
= GetToken(0))) return 0;
406 value
= strtol(token
, &endptr
, 0);
407 if (endptr
== NULL
|| *endptr
!= '\0')
409 error( "Expected number value, got '%s'\n", token
);
412 if (value
< -0x8000 || value
> 0xffff)
414 error( "Value %d for absolute symbol doesn't fit in 16 bits\n", value
);
417 odp
->u
.abs
.value
= value
;
422 /*******************************************************************
425 * Parse a 'stub' definition in a .spec file
427 static int parse_spec_stub( ORDDEF
*odp
, DLLSPEC
*spec
)
429 odp
->u
.func
.nb_args
= -1;
430 odp
->link_name
= xstrdup("");
432 return parse_spec_arguments( odp
, spec
, 1 );
436 /*******************************************************************
439 * Parse an 'extern' definition in a .spec file.
441 static int parse_spec_extern( ORDDEF
*odp
, DLLSPEC
*spec
)
445 if (spec
->type
== SPEC_WIN16
)
447 error( "'extern' not supported for Win16, use 'variable' instead\n" );
450 if (!(token
= GetToken(1)))
452 if (!strcmp( odp
->name
, "@" ))
454 error( "Missing handler name for anonymous extern\n" );
457 odp
->link_name
= xstrdup( odp
->name
);
461 odp
->link_name
= xstrdup( token
);
462 if (strchr( odp
->link_name
, '.' )) odp
->flags
|= FLAG_FORWARD
;
468 /*******************************************************************
471 * Parse the optional flags for an entry point in a .spec file.
473 static const char *parse_spec_flags( DLLSPEC
*spec
, ORDDEF
*odp
, const char *token
)
475 unsigned int i
, cpu_mask
= 0;
480 if (!strncmp( token
, "arch=", 5))
482 char *args
= xstrdup( token
+ 5 );
483 char *cpu_name
= strtok( args
, "," );
486 if (!strcmp( cpu_name
, "win32" ))
488 if (spec
->type
== SPEC_WIN32
)
489 odp
->flags
|= FLAG_CPU_WIN32
;
491 odp
->flags
|= FLAG_EXPORT32
;
493 else if (!strcmp( cpu_name
, "win64" ))
494 odp
->flags
|= FLAG_CPU_WIN64
;
497 int cpu
= get_cpu_from_name( cpu_name
+ (cpu_name
[0] == '!') );
500 error( "Unknown architecture '%s'\n", cpu_name
);
503 if (cpu_name
[0] == '!') cpu_mask
|= FLAG_CPU( cpu
);
504 else odp
->flags
|= FLAG_CPU( cpu
);
506 cpu_name
= strtok( NULL
, "," );
510 else if (!strcmp( token
, "i386" )) /* backwards compatibility */
512 odp
->flags
|= FLAG_CPU(CPU_i386
);
516 for (i
= 0; FlagNames
[i
]; i
++)
517 if (!strcmp( FlagNames
[i
], token
)) break;
520 error( "Unknown flag '%s'\n", token
);
527 if (spec
->type
== SPEC_WIN32
)
528 error( "Flag '%s' is not supported in Win32\n", FlagNames
[i
] );
533 if (spec
->type
== SPEC_WIN16
)
534 error( "Flag '%s' is not supported in Win16\n", FlagNames
[i
] );
537 odp
->flags
|= 1 << i
;
540 } while (token
&& *token
== '-');
542 if (cpu_mask
) odp
->flags
|= FLAG_CPU_MASK
& ~cpu_mask
;
547 /*******************************************************************
550 * Parse an ordinal definition in a .spec file.
552 static int parse_spec_ordinal( int ordinal
, DLLSPEC
*spec
)
556 ORDDEF
*odp
= add_entry_point( spec
);
558 if (!(token
= GetToken(0))) goto error
;
560 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
561 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
564 if (odp
->type
>= TYPE_NBTYPES
)
566 if (!strcmp( token
, "thiscall" )) /* for backwards compatibility */
568 odp
->type
= TYPE_STDCALL
;
569 odp
->flags
|= FLAG_THISCALL
;
573 error( "Expected type after ordinal, found '%s' instead\n", token
);
578 if (!(token
= GetToken(0))) goto error
;
579 if (*token
== '-' && !(token
= parse_spec_flags( spec
, odp
, token
))) goto error
;
581 if (ordinal
== -1 && spec
->type
!= SPEC_WIN32
&& !(odp
->flags
& FLAG_EXPORT32
))
583 error( "'@' ordinals not supported for Win16\n" );
587 odp
->name
= xstrdup( token
);
588 odp
->lineno
= current_line
;
589 odp
->ordinal
= ordinal
;
591 len
= strspn( odp
->name
, valid_ordname_chars
);
592 if (len
< strlen( odp
->name
))
594 error( "Character '%c' is not allowed in exported name '%s'\n", odp
->name
[len
], odp
->name
);
601 if (!parse_spec_variable( odp
, spec
)) goto error
;
607 if (!parse_spec_export( odp
, spec
)) goto error
;
610 if (!parse_spec_equate( odp
, spec
)) goto error
;
613 if (!parse_spec_stub( odp
, spec
)) goto error
;
616 if (!parse_spec_extern( odp
, spec
)) goto error
;
622 if ((odp
->flags
& FLAG_CPU_MASK
) && !(odp
->flags
& FLAG_CPU(target
.cpu
)))
624 /* ignore this entry point */
625 spec
->nb_entry_points
--;
629 if (data_only
&& !(odp
->flags
& FLAG_FORWARD
))
631 error( "Only forwarded entry points are allowed in data-only mode\n" );
639 error( "Ordinal 0 is not valid\n" );
642 if (ordinal
>= MAX_ORDINALS
)
644 error( "Ordinal number %d too large\n", ordinal
);
647 if (ordinal
> spec
->limit
) spec
->limit
= ordinal
;
648 if (ordinal
< spec
->base
) spec
->base
= ordinal
;
649 odp
->ordinal
= ordinal
;
652 if (odp
->type
== TYPE_STDCALL
&& !(odp
->flags
& FLAG_PRIVATE
))
654 if (!strcmp( odp
->name
, "DllRegisterServer" ) ||
655 !strcmp( odp
->name
, "DllUnregisterServer" ) ||
656 !strcmp( odp
->name
, "DllMain" ) ||
657 !strcmp( odp
->name
, "DllGetClassObject" ) ||
658 !strcmp( odp
->name
, "DllGetVersion" ) ||
659 !strcmp( odp
->name
, "DllInstall" ) ||
660 !strcmp( odp
->name
, "DllCanUnloadNow" ))
662 warning( "Function %s should be marked private\n", odp
->name
);
663 if (strcmp( odp
->name
, odp
->link_name
))
664 warning( "Function %s should not use a different internal name (%s)\n",
665 odp
->name
, odp
->link_name
);
669 if (!strcmp( odp
->name
, "@" ) || odp
->flags
& (FLAG_NONAME
| FLAG_ORDINAL
))
673 if (!strcmp( odp
->name
, "@" ))
674 error( "Nameless function needs an explicit ordinal number\n" );
676 error( "Function imported by ordinal needs an explicit ordinal number\n" );
679 if (spec
->type
!= SPEC_WIN32
)
681 error( "Nameless functions not supported for Win16\n" );
684 if (!strcmp( odp
->name
, "@" ))
689 else if (!(odp
->flags
& FLAG_ORDINAL
)) /* -ordinal only affects the import library */
691 odp
->export_name
= odp
->name
;
698 spec
->nb_entry_points
--;
704 static unsigned int apiset_hash_len( const char *str
)
706 return strrchr( str
, '-' ) - str
;
709 static unsigned int apiset_hash( const char *str
)
711 unsigned int ret
= 0, len
= apiset_hash_len( str
);
712 while (len
--) ret
= ret
* apiset_hash_factor
+ *str
++;
716 static unsigned int apiset_add_str( struct apiset
*apiset
, const char *str
, unsigned int len
)
720 if (!apiset
->strings
|| !(ret
= strstr( apiset
->strings
, str
)))
722 if (apiset
->str_pos
+ len
>= apiset
->str_size
)
724 apiset
->str_size
= max( apiset
->str_size
* 2, 1024 );
725 apiset
->strings
= xrealloc( apiset
->strings
, apiset
->str_size
);
727 ret
= apiset
->strings
+ apiset
->str_pos
;
728 memcpy( ret
, str
, len
);
730 apiset
->str_pos
+= len
;
732 return ret
- apiset
->strings
;
735 static void add_apiset( struct apiset
*apiset
, const char *api
)
737 struct apiset_entry
*entry
;
739 if (apiset
->count
== apiset
->size
)
741 apiset
->size
= max( apiset
->size
* 2, 64 );
742 apiset
->entries
= xrealloc( apiset
->entries
, apiset
->size
* sizeof(*apiset
->entries
) );
744 entry
= &apiset
->entries
[apiset
->count
++];
745 entry
->name_len
= strlen( api
);
746 entry
->name_off
= apiset_add_str( apiset
, api
, entry
->name_len
);
747 entry
->hash
= apiset_hash( api
);
748 entry
->hash_len
= apiset_hash_len( api
);
749 entry
->val_count
= 0;
752 static void add_apiset_value( struct apiset
*apiset
, const char *value
)
754 struct apiset_entry
*entry
= &apiset
->entries
[apiset
->count
- 1];
756 if (entry
->val_count
< ARRAY_SIZE(entry
->values
) - 1)
758 struct apiset_value
*val
= &entry
->values
[entry
->val_count
++];
759 char *sep
= strchr( value
, ':' );
763 val
->name_len
= sep
- value
;
764 val
->name_off
= apiset_add_str( apiset
, value
, val
->name_len
);
765 val
->val_len
= strlen( sep
+ 1 );
766 val
->val_off
= apiset_add_str( apiset
, sep
+ 1, val
->val_len
);
770 val
->name_len
= val
->name_off
= 0;
771 val
->val_len
= strlen( value
);
772 val
->val_off
= apiset_add_str( apiset
, value
, val
->val_len
);
775 else error( "Too many values for api '%.*s'\n", entry
->name_len
, apiset
->strings
+ entry
->name_off
);
778 /*******************************************************************
781 static int parse_spec_apiset( DLLSPEC
*spec
)
783 struct apiset_entry
*entry
;
785 unsigned int i
, hash
;
789 error( "Apiset definitions are only allowed in data-only mode\n" );
793 if (!(token
= GetToken(0))) return 0;
795 if (!strncmp( token
, "api-", 4 ) && !strncmp( token
, "ext-", 4 ))
797 error( "Unrecognized API set name '%s'\n", token
);
801 hash
= apiset_hash( token
);
802 for (i
= 0, entry
= spec
->apiset
.entries
; i
< spec
->apiset
.count
; i
++, entry
++)
804 if (entry
->name_len
== strlen( token
) &&
805 !strncmp( spec
->apiset
.strings
+ entry
->name_off
, token
, entry
->name_len
))
807 error( "Duplicate API set '%s'\n", token
);
810 if (entry
->hash
== hash
)
812 error( "Duplicate hash code '%.*s' and '%s'\n",
813 entry
->name_len
, spec
->apiset
.strings
+ entry
->name_off
, token
);
817 add_apiset( &spec
->apiset
, token
);
819 if (!(token
= GetToken(0)) || strcmp( token
, "=" ))
821 error( "Syntax error near '%s'\n", token
);
825 while ((token
= GetToken(1))) add_apiset_value( &spec
->apiset
, token
);
830 static int name_compare( const void *ptr1
, const void *ptr2
)
832 const ORDDEF
*odp1
= *(const ORDDEF
* const *)ptr1
;
833 const ORDDEF
*odp2
= *(const ORDDEF
* const *)ptr2
;
834 const char *name1
= odp1
->name
? odp1
->name
: odp1
->export_name
;
835 const char *name2
= odp2
->name
? odp2
->name
: odp2
->export_name
;
836 return strcmp( name1
, name2
);
839 /*******************************************************************
842 * Build the name array and catch duplicates.
844 static void assign_names( DLLSPEC
*spec
)
846 int i
, j
, nb_exp_names
= 0;
850 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
851 if (spec
->entry_points
[i
].name
) spec
->nb_names
++;
852 else if (spec
->entry_points
[i
].export_name
) nb_exp_names
++;
854 if (!spec
->nb_names
&& !nb_exp_names
) return;
856 /* check for duplicates */
858 all_names
= xmalloc( (spec
->nb_names
+ nb_exp_names
) * sizeof(all_names
[0]) );
859 for (i
= j
= 0; i
< spec
->nb_entry_points
; i
++)
860 if (spec
->entry_points
[i
].name
|| spec
->entry_points
[i
].export_name
)
861 all_names
[j
++] = &spec
->entry_points
[i
];
863 qsort( all_names
, j
, sizeof(all_names
[0]), name_compare
);
865 for (i
= 0; i
< j
- 1; i
++)
867 const char *name1
= all_names
[i
]->name
? all_names
[i
]->name
: all_names
[i
]->export_name
;
868 const char *name2
= all_names
[i
+1]->name
? all_names
[i
+1]->name
: all_names
[i
+1]->export_name
;
869 if (!strcmp( name1
, name2
) &&
870 !((all_names
[i
]->flags
^ all_names
[i
+1]->flags
) & FLAG_EXPORT32
))
872 current_line
= max( all_names
[i
]->lineno
, all_names
[i
+1]->lineno
);
873 error( "'%s' redefined\n%s:%d: First defined here\n",
874 name1
, input_file_name
,
875 min( all_names
[i
]->lineno
, all_names
[i
+1]->lineno
) );
882 spec
->names
= xmalloc( spec
->nb_names
* sizeof(spec
->names
[0]) );
883 for (i
= j
= 0; i
< spec
->nb_entry_points
; i
++)
884 if (spec
->entry_points
[i
].name
) spec
->names
[j
++] = &spec
->entry_points
[i
];
886 /* sort the list of names */
887 qsort( spec
->names
, spec
->nb_names
, sizeof(spec
->names
[0]), name_compare
);
888 for (i
= 0; i
< spec
->nb_names
; i
++) spec
->names
[i
]->hint
= i
;
892 /*******************************************************************
895 * Build the ordinal array.
897 static void assign_ordinals( DLLSPEC
*spec
)
899 int i
, count
, ordinal
;
901 /* start assigning from base, or from 1 if no ordinal defined yet */
903 spec
->base
= MAX_ORDINALS
;
905 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
907 ordinal
= spec
->entry_points
[i
].ordinal
;
908 if (ordinal
== -1) continue;
909 if (ordinal
> spec
->limit
) spec
->limit
= ordinal
;
910 if (ordinal
< spec
->base
) spec
->base
= ordinal
;
912 if (spec
->base
== MAX_ORDINALS
) spec
->base
= 1;
913 if (spec
->limit
< spec
->base
) spec
->limit
= spec
->base
;
915 count
= max( spec
->limit
+ 1, spec
->base
+ spec
->nb_entry_points
);
916 spec
->ordinals
= xmalloc( count
* sizeof(spec
->ordinals
[0]) );
917 memset( spec
->ordinals
, 0, count
* sizeof(spec
->ordinals
[0]) );
919 /* fill in all explicitly specified ordinals */
920 for (i
= 0; i
< spec
->nb_entry_points
; i
++)
922 ordinal
= spec
->entry_points
[i
].ordinal
;
923 if (ordinal
== -1) continue;
924 if (spec
->ordinals
[ordinal
])
926 current_line
= max( spec
->entry_points
[i
].lineno
, spec
->ordinals
[ordinal
]->lineno
);
927 error( "ordinal %d redefined\n%s:%d: First defined here\n",
928 ordinal
, input_file_name
,
929 min( spec
->entry_points
[i
].lineno
, spec
->ordinals
[ordinal
]->lineno
) );
931 else spec
->ordinals
[ordinal
] = &spec
->entry_points
[i
];
934 /* now assign ordinals to the rest */
935 for (i
= 0, ordinal
= spec
->base
; i
< spec
->nb_entry_points
; i
++)
937 if (spec
->entry_points
[i
].ordinal
!= -1) continue;
938 while (spec
->ordinals
[ordinal
]) ordinal
++;
939 if (ordinal
>= MAX_ORDINALS
)
941 current_line
= spec
->entry_points
[i
].lineno
;
942 fatal_error( "Too many functions defined (max %d)\n", MAX_ORDINALS
);
944 spec
->entry_points
[i
].ordinal
= ordinal
;
945 spec
->ordinals
[ordinal
] = &spec
->entry_points
[i
];
947 if (ordinal
> spec
->limit
) spec
->limit
= ordinal
;
951 /*******************************************************************
954 * Add the necessary exports to the 32-bit counterpart of a 16-bit module.
956 void add_16bit_exports( DLLSPEC
*spec32
, DLLSPEC
*spec16
)
961 spec32
->file_name
= xstrdup( spec16
->file_name
);
962 spec32
->characteristics
= IMAGE_FILE_DLL
;
963 spec32
->init_func
= xstrdup( "DllMain" );
965 /* add an export for the NE module */
967 odp
= add_entry_point( spec32
);
968 odp
->type
= TYPE_EXTERN
;
969 odp
->flags
= FLAG_PRIVATE
;
970 odp
->name
= xstrdup( "__wine_spec_dos_header" );
973 odp
->link_name
= xstrdup( ".L__wine_spec_dos_header" );
975 if (spec16
->main_module
)
977 odp
= add_entry_point( spec32
);
978 odp
->type
= TYPE_EXTERN
;
979 odp
->flags
= FLAG_PRIVATE
;
980 odp
->name
= xstrdup( "__wine_spec_main_module" );
983 odp
->link_name
= xstrdup( ".L__wine_spec_main_module" );
986 /* add the explicit win32 exports */
988 for (i
= 1; i
<= spec16
->limit
; i
++)
990 ORDDEF
*odp16
= spec16
->ordinals
[i
];
992 if (!odp16
|| !odp16
->name
) continue;
993 if (!(odp16
->flags
& FLAG_EXPORT32
)) continue;
995 odp
= add_entry_point( spec32
);
996 odp
->flags
= odp16
->flags
& ~FLAG_EXPORT32
;
997 odp
->type
= odp16
->type
;
998 odp
->name
= xstrdup( odp16
->name
);
999 odp
->lineno
= odp16
->lineno
;
1001 odp
->link_name
= xstrdup( odp16
->link_name
);
1002 odp
->u
.func
.nb_args
= odp16
->u
.func
.nb_args
;
1003 if (odp
->u
.func
.nb_args
> 0) memcpy( odp
->u
.func
.args
, odp16
->u
.func
.args
,
1004 odp
->u
.func
.nb_args
* sizeof(odp
->u
.func
.args
[0]) );
1007 assign_names( spec32
);
1008 assign_ordinals( spec32
);
1012 /*******************************************************************
1015 * Parse a .spec file.
1017 int parse_spec_file( FILE *file
, DLLSPEC
*spec
)
1024 comment_chars
= "#;";
1025 separator_chars
= "()";
1027 while (get_next_line())
1029 if (!(token
= GetToken(1))) continue;
1030 if (strcmp(token
, "@") == 0)
1032 if (!parse_spec_ordinal( -1, spec
)) continue;
1034 else if (IsNumberString(token
))
1036 if (!parse_spec_ordinal( atoi(token
), spec
)) continue;
1038 else if (strcmp(token
, "apiset") == 0)
1040 if (!parse_spec_apiset( spec
)) continue;
1044 error( "Expected ordinal declaration, got '%s'\n", token
);
1047 if ((token
= GetToken(1))) error( "Syntax error near '%s'\n", token
);
1050 current_line
= 0; /* no longer parsing the input file */
1051 assign_names( spec
);
1052 assign_ordinals( spec
);
1057 /*******************************************************************
1060 * Parse a LIBRARY declaration in a .def file.
1062 static int parse_def_library( DLLSPEC
*spec
)
1064 const char *token
= GetToken(1);
1066 if (!token
) return 1;
1067 if (strcmp( token
, "BASE" ))
1069 free( spec
->file_name
);
1070 spec
->file_name
= xstrdup( token
);
1071 if (!(token
= GetToken(1))) return 1;
1073 if (strcmp( token
, "BASE" ))
1075 error( "Expected library name or BASE= declaration, got '%s'\n", token
);
1078 if (!(token
= GetToken(0))) return 0;
1079 if (strcmp( token
, "=" ))
1081 error( "Expected '=' after BASE, got '%s'\n", token
);
1084 if (!(token
= GetToken(0))) return 0;
1085 /* FIXME: do something with base address */
1091 /*******************************************************************
1092 * parse_def_stack_heap_size
1094 * Parse a STACKSIZE or HEAPSIZE declaration in a .def file.
1096 static int parse_def_stack_heap_size( int is_stack
, DLLSPEC
*spec
)
1098 const char *token
= GetToken(0);
1102 if (!token
) return 0;
1103 size
= strtoul( token
, &end
, 0 );
1106 error( "Invalid number '%s'\n", token
);
1109 if (is_stack
) spec
->stack_size
= size
/ 1024;
1110 else spec
->heap_size
= size
/ 1024;
1111 if (!(token
= GetToken(1))) return 1;
1112 if (strcmp( token
, "," ))
1114 error( "Expected ',' after size, got '%s'\n", token
);
1117 if (!(token
= GetToken(0))) return 0;
1118 /* FIXME: do something with reserve size */
1123 /*******************************************************************
1126 * Parse an export declaration in a .def file.
1128 static int parse_def_export( char *name
, DLLSPEC
*spec
)
1131 const char *token
= GetToken(1);
1132 ORDDEF
*odp
= add_entry_point( spec
);
1134 odp
->lineno
= current_line
;
1137 args
= remove_stdcall_decoration( odp
->name
);
1140 odp
->type
= TYPE_CDECL
;
1145 odp
->type
= TYPE_STDCALL
;
1146 args
/= get_ptr_size();
1147 if (args
>= MAX_ARGUMENTS
)
1149 error( "Too many arguments in stdcall function '%s'\n", odp
->name
);
1152 for (i
= 0; i
< args
; i
++) odp
->u
.func
.args
[i
] = ARG_LONG
;
1154 odp
->u
.func
.nb_args
= args
;
1156 /* check for optional internal name */
1158 if (token
&& !strcmp( token
, "=" ))
1160 if (!(token
= GetToken(0))) goto error
;
1161 odp
->link_name
= xstrdup( token
);
1162 remove_stdcall_decoration( odp
->link_name
);
1163 token
= GetToken(1);
1167 odp
->link_name
= xstrdup( name
);
1170 /* check for optional ordinal */
1172 if (token
&& token
[0] == '@')
1176 if (!IsNumberString( token
+1 ))
1178 error( "Expected number after '@', got '%s'\n", token
+1 );
1181 ordinal
= atoi( token
+1 );
1184 error( "Ordinal 0 is not valid\n" );
1187 if (ordinal
>= MAX_ORDINALS
)
1189 error( "Ordinal number %d too large\n", ordinal
);
1192 odp
->ordinal
= ordinal
;
1193 token
= GetToken(1);
1196 /* check for other optional keywords */
1200 if (!strcmp( token
, "NONAME" ))
1202 if (odp
->ordinal
== -1)
1204 error( "NONAME requires an ordinal\n" );
1207 odp
->export_name
= odp
->name
;
1209 odp
->flags
|= FLAG_NONAME
;
1211 else if (!strcmp( token
, "PRIVATE" ))
1213 odp
->flags
|= FLAG_PRIVATE
;
1215 else if (!strcmp( token
, "DATA" ))
1217 odp
->type
= TYPE_EXTERN
;
1221 error( "Garbage text '%s' found at end of export declaration\n", token
);
1224 token
= GetToken(1);
1229 spec
->nb_entry_points
--;
1235 /*******************************************************************
1238 * Parse a .def file.
1240 int parse_def_file( FILE *file
, DLLSPEC
*spec
)
1248 comment_chars
= ";";
1249 separator_chars
= ",=";
1251 while (get_next_line())
1253 if (!(token
= GetToken(1))) continue;
1255 if (!strcmp( token
, "LIBRARY" ) || !strcmp( token
, "NAME" ))
1257 if (!parse_def_library( spec
)) continue;
1260 else if (!strcmp( token
, "STACKSIZE" ))
1262 if (!parse_def_stack_heap_size( 1, spec
)) continue;
1265 else if (!strcmp( token
, "HEAPSIZE" ))
1267 if (!parse_def_stack_heap_size( 0, spec
)) continue;
1270 else if (!strcmp( token
, "EXPORTS" ))
1273 if (!(token
= GetToken(1))) continue;
1275 else if (!strcmp( token
, "IMPORTS" ))
1278 if (!(token
= GetToken(1))) continue;
1280 else if (!strcmp( token
, "SECTIONS" ))
1283 if (!(token
= GetToken(1))) continue;
1286 if (!in_exports
) continue; /* ignore this line */
1287 if (!parse_def_export( xstrdup(token
), spec
)) continue;
1290 if ((token
= GetToken(1))) error( "Syntax error near '%s'\n", token
);
1293 current_line
= 0; /* no longer parsing the input file */
1294 assign_names( spec
);
1295 assign_ordinals( spec
);