/******************************************************************* * output_entries * * Output entries for individual symbols in the entry table. */ static void output_entries( DLLSPEC *spec, int first, int count ) { int i; for (i = 0; i < count; i++) { ORDDEF *odp = spec->ordinals[first + i]; output( "\t.byte 0x03\n" ); /* flags: exported & public data */ switch (odp->type) { case TYPE_CDECL: case TYPE_PASCAL: case TYPE_VARARGS: case TYPE_STUB: output( "\t.short .L__wine_%s_%u-.L__wine_spec_code_segment\n", make_c_identifier(spec->dll_name), first + i ); break; case TYPE_VARIABLE: output( "\t.short .L__wine_%s_%u-.L__wine_spec_data_segment\n", make_c_identifier(spec->dll_name), first + i ); break; case TYPE_ABS: output( "\t.short 0x%04x /* %s */\n", odp->u.abs.value, odp->name ); break; default: assert(0); } } }
/******************************************************************* * get_stub_name * * Generate an internal name for a stub entry point. */ const char *get_stub_name( const ORDDEF *odp, const DLLSPEC *spec ) { static char buffer[256]; if (odp->name || odp->export_name) { char *p; sprintf( buffer, "__wine_stub_%s", odp->name ? odp->name : odp->export_name ); /* make sure name is a legal C identifier */ for (p = buffer; *p; p++) if (!isalnum(*p) && *p != '_') break; if (!*p) return buffer; } sprintf( buffer, "__wine_stub_%s_%d", make_c_identifier(spec->file_name), odp->ordinal ); return buffer; }
/* set the dll name from the file name */ static void init_dll_name( DLLSPEC *spec ) { if (!spec->file_name && output_file_name) { char *p; spec->file_name = xstrdup( output_file_name ); if ((p = strrchr( spec->file_name, '.' ))) *p = 0; } if (!spec->dll_name && spec->file_name) /* set default name from file name */ { char *p; spec->dll_name = xstrdup( spec->file_name ); if ((p = strrchr( spec->dll_name, '.' ))) *p = 0; } spec->c_name = make_c_identifier( spec->dll_name ); }
/* output the import table of a Win32 module */ static void output_immediate_imports(void) { int i, j; const char *dll_name; if (nb_imports == nb_delayed) return; /* no immediate imports */ /* main import header */ output( "\n/* import table */\n" ); output( "\n\t.data\n" ); output( "\t.align %d\n", get_alignment(4) ); output( ".L__wine_spec_imports:\n" ); /* list of dlls */ for (i = j = 0; i < nb_imports; i++) { if (dll_imports[i]->delay) continue; dll_name = make_c_identifier( dll_imports[i]->spec->file_name ); output( "\t.long .L__wine_spec_import_data_names+%d-.L__wine_spec_rva_base\n", /* OriginalFirstThunk */ j * get_ptr_size() ); output( "\t.long 0\n" ); /* TimeDateStamp */ output( "\t.long 0\n" ); /* ForwarderChain */ output( "\t.long .L__wine_spec_import_name_%s-.L__wine_spec_rva_base\n", /* Name */ dll_name ); output( "\t.long .L__wine_spec_import_data_ptrs+%d-.L__wine_spec_rva_base\n", /* FirstThunk */ j * get_ptr_size() ); j += dll_imports[i]->nb_imports + 1; } output( "\t.long 0\n" ); /* OriginalFirstThunk */ output( "\t.long 0\n" ); /* TimeDateStamp */ output( "\t.long 0\n" ); /* ForwarderChain */ output( "\t.long 0\n" ); /* Name */ output( "\t.long 0\n" ); /* FirstThunk */ output( "\n\t.align %d\n", get_alignment(get_ptr_size()) ); output( ".L__wine_spec_import_data_names:\n" ); for (i = 0; i < nb_imports; i++) { if (dll_imports[i]->delay) continue; dll_name = make_c_identifier( dll_imports[i]->spec->file_name ); for (j = 0; j < dll_imports[i]->nb_imports; j++) { ORDDEF *odp = dll_imports[i]->imports[j]; if (!(odp->flags & FLAG_NONAME)) output( "\t%s .L__wine_spec_import_data_%s_%s-.L__wine_spec_rva_base\n", get_asm_ptr_keyword(), dll_name, odp->name ); else { if (get_ptr_size() == 8) output( "\t.quad 0x800000000000%04x\n", odp->ordinal ); else output( "\t.long 0x8000%04x\n", odp->ordinal ); } } output( "\t%s 0\n", get_asm_ptr_keyword() ); } output( ".L__wine_spec_import_data_ptrs:\n" ); for (i = 0; i < nb_imports; i++) { if (dll_imports[i]->delay) continue; for (j = 0; j < dll_imports[i]->nb_imports; j++) output( "\t%s 0\n", get_asm_ptr_keyword() ); output( "\t%s 0\n", get_asm_ptr_keyword() ); } output( ".L__wine_spec_imports_end:\n" ); for (i = 0; i < nb_imports; i++) { if (dll_imports[i]->delay) continue; dll_name = make_c_identifier( dll_imports[i]->spec->file_name ); for (j = 0; j < dll_imports[i]->nb_imports; j++) { ORDDEF *odp = dll_imports[i]->imports[j]; if (!(odp->flags & FLAG_NONAME)) { output( "\t.align %d\n", get_alignment(2) ); output( ".L__wine_spec_import_data_%s_%s:\n", dll_name, odp->name ); output( "\t%s %d\n", get_asm_short_keyword(), odp->ordinal ); output( "\t%s \"%s\"\n", get_asm_string_keyword(), odp->name ); } } } for (i = 0; i < nb_imports; i++) { if (dll_imports[i]->delay) continue; dll_name = make_c_identifier( dll_imports[i]->spec->file_name ); output( ".L__wine_spec_import_name_%s:\n\t%s \"%s\"\n", dll_name, get_asm_string_keyword(), dll_imports[i]->spec->file_name ); } }
/******************************************************************* * output_module16 * * Output code for a 16-bit module. */ static void output_module16( DLLSPEC *spec ) { ORDDEF **typelist; ORDDEF *entry_point = NULL; int i, j, nb_funcs; /* store the main entry point as ordinal 0 */ if (!spec->ordinals) { assert(spec->limit == 0); spec->ordinals = xmalloc( sizeof(spec->ordinals[0]) ); spec->ordinals[0] = NULL; } if (spec->init_func && !(spec->characteristics & IMAGE_FILE_DLL)) { entry_point = xmalloc( sizeof(*entry_point) ); entry_point->type = TYPE_PASCAL; entry_point->ordinal = 0; entry_point->lineno = 0; entry_point->flags = FLAG_REGISTER; entry_point->name = NULL; entry_point->link_name = xstrdup( spec->init_func ); entry_point->export_name = NULL; entry_point->u.func.nb_args = 0; assert( !spec->ordinals[0] ); spec->ordinals[0] = entry_point; } /* Build sorted list of all argument types, without duplicates */ typelist = xmalloc( (spec->limit + 1) * sizeof(*typelist) ); for (i = nb_funcs = 0; i <= spec->limit; i++) { ORDDEF *odp = spec->ordinals[i]; if (!odp) continue; if (is_function( odp )) typelist[nb_funcs++] = odp; } nb_funcs = sort_func_list( typelist, nb_funcs, callfrom16_type_compare ); /* Output the module structure */ output( "\n/* module data */\n\n" ); output( "\t.data\n" ); output( "\t.align %d\n", get_alignment(4) ); output("\t.globl __wine_spec_dos_header\n"); output("__wine_spec_dos_header:\n"); output("\t.globl _wine_spec_dos_header\n"); output("_wine_spec_dos_header:\n"); output( ".L__wine_spec_dos_header:\n" ); output( "\t.short 0x5a4d\n" ); /* e_magic */ output( "\t.short 0\n" ); /* e_cblp */ output( "\t.short 0\n" ); /* e_cp */ output( "\t.short 0\n" ); /* e_crlc */ output( "\t.short 0\n" ); /* e_cparhdr */ output( "\t.short 0\n" ); /* e_minalloc */ output( "\t.short 0\n" ); /* e_maxalloc */ output( "\t.short 0\n" ); /* e_ss */ output( "\t.short 0\n" ); /* e_sp */ output( "\t.short 0\n" ); /* e_csum */ output( "\t.short 0\n" ); /* e_ip */ output( "\t.short 0\n" ); /* e_cs */ output( "\t.short 0\n" ); /* e_lfarlc */ output( "\t.short 0\n" ); /* e_ovno */ output( "\t.short 0,0,0,0\n" ); /* e_res */ output( "\t.short 0\n" ); /* e_oemid */ output( "\t.short 0\n" ); /* e_oeminfo */ output( "\t.short 0,0,0,0,0,0,0,0,0,0\n" ); /* e_res2 */ output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */ output( ".L__wine_spec_ne_header:\n" ); output( "\t.short 0x454e\n" ); /* ne_magic */ output( "\t.byte 0\n" ); /* ne_ver */ output( "\t.byte 0\n" ); /* ne_rev */ output( "\t.short .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n" );/* ne_enttab */ output( "\t.short .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n" );/* ne_cbenttab */ output( "\t.long 0\n" ); /* ne_crc */ output( "\t.short 0x%04x\n", NE_FFLAGS_SINGLEDATA | /* ne_flags */ ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) ); output( "\t.short 2\n" ); /* ne_autodata */ output( "\t.short %u\n", spec->heap_size ); /* ne_heap */ output( "\t.short 0\n" ); /* ne_stack */ if (!entry_point) output( "\t.long 0\n" ); /* ne_csip */ else output( "\t.short .L__wine_%s_0-.L__wine_spec_code_segment,1\n", make_c_identifier(spec->dll_name) ); output( "\t.short 0,2\n" ); /* ne_sssp */ output( "\t.short 2\n" ); /* ne_cseg */ output( "\t.short 0\n" ); /* ne_cmod */ output( "\t.short 0\n" ); /* ne_cbnrestab */ output( "\t.short .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n" );/* ne_segtab */ output( "\t.short .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n" ); /* ne_rsrctab */ output( "\t.short .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n" ); /* ne_restab */ output( "\t.short .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n" ); /* ne_modtab */ output( "\t.short .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n" ); /* ne_imptab */ output( "\t.long 0\n" ); /* ne_nrestab */ output( "\t.short 0\n" ); /* ne_cmovent */ output( "\t.short 0\n" ); /* ne_align */ output( "\t.short 0\n" ); /* ne_cres */ output( "\t.byte 0x02\n" ); /* ne_exetyp = NE_OSFLAGS_WINDOWS */ output( "\t.byte 0x08\n" ); /* ne_flagsothers = NE_AFLAGS_FASTLOAD */ output( "\t.short 0\n" ); /* ne_pretthunks */ output( "\t.short 0\n" ); /* ne_psegrefbytes */ output( "\t.short 0\n" ); /* ne_swaparea */ output( "\t.short 0\n" ); /* ne_expver */ /* segment table */ output( "\n.L__wine_spec_ne_segtab:\n" ); /* code segment entry */ output( "\t.short .L__wine_spec_code_segment-.L__wine_spec_dos_header\n" ); /* filepos */ output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* size */ output( "\t.short 0x2000\n" ); /* flags = NE_SEGFLAGS_32BIT */ output( "\t.short .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n" ); /* minsize */ /* data segment entry */ output( "\t.short .L__wine_spec_data_segment-.L__wine_spec_dos_header\n" ); /* filepos */ output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* size */ output( "\t.short 0x0001\n" ); /* flags = NE_SEGFLAGS_DATA */ output( "\t.short .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n" ); /* minsize */ /* resource directory */ output_res16_directory( spec ); /* resident names table */ output( "\n\t.align %d\n", get_alignment(2) ); output( ".L__wine_spec_ne_restab:\n" ); output_resident_name( spec->dll_name, 0 ); for (i = 1; i <= spec->limit; i++) { ORDDEF *odp = spec->ordinals[i]; if (!odp || !odp->name[0]) continue; if (odp->flags & FLAG_EXPORT32) continue; output_resident_name( odp->name, i ); } output( "\t.byte 0\n" ); /* imported names table */ output( "\n\t.align %d\n", get_alignment(2) ); output( ".L__wine_spec_ne_modtab:\n" ); output( ".L__wine_spec_ne_imptab:\n" ); output( "\t.byte 0,0\n" ); /* entry table */ output( "\n.L__wine_spec_ne_enttab:\n" ); output_entry_table( spec ); output( ".L__wine_spec_ne_enttab_end:\n" ); /* code segment */ output( "\n\t.align %d\n", get_alignment(2) ); output( ".L__wine_spec_code_segment:\n" ); for ( i = 0; i < nb_funcs; i++ ) { unsigned int arg_types[2]; int nop_words, pos, argsize = 0; if ( typelist[i]->type == TYPE_PASCAL ) argsize = get_function_argsize( typelist[i] ); /* build the arg types bit fields */ arg_types[0] = arg_types[1] = 0; for (j = pos = 0; j < typelist[i]->u.func.nb_args && pos < 20; j++, pos++) { int type = 0; switch (typelist[i]->u.func.args[j]) { case ARG_WORD: type = ARG16_WORD; break; case ARG_SWORD: type = ARG16_SWORD; break; case ARG_SEGPTR: type = ARG16_LONG; break; case ARG_SEGSTR: type = ARG16_SEGSTR; break; case ARG_LONG: type = ARG16_LONG; break; case ARG_PTR: type = ARG16_PTR; break; case ARG_STR: type = ARG16_STR; break; case ARG_WSTR: type = ARG16_PTR; break; case ARG_FLOAT: type = ARG16_LONG; break; case ARG_INT128: type = ARG16_PTR; break; case ARG_INT64: case ARG_DOUBLE: type = ARG16_LONG; arg_types[pos / 10] |= type << (3 * (pos % 10)); pos++; break; } if (pos < 20) arg_types[pos / 10] |= type << (3 * (pos % 10)); } if (typelist[i]->type == TYPE_VARARGS && pos < 20) arg_types[pos / 10] |= ARG16_VARARG << (3 * (pos % 10)); output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist[i]) ); output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist[i]) ); output( "\tlcall $0,$0\n" ); if (typelist[i]->flags & FLAG_REGISTER) { nop_words = 4; } else if (typelist[i]->flags & FLAG_RET16) { output( "\torw %%ax,%%ax\n" ); output( "\tnop\n" ); /* so that the lretw is aligned */ nop_words = 2; } else { output( "\tshld $16,%%eax,%%edx\n" ); output( "\torl %%eax,%%eax\n" ); nop_words = 1; } if (argsize) { output( "\tlretw $%u\n", argsize ); nop_words--; } else output( "\tlretw\n" ); if (nop_words) output( "\t%s\n", nop_sequence[nop_words-1] ); /* the movl is here so that the code contains only valid instructions, */ /* it's never actually executed, we only care about the arg_types[] values */ output( "\t.short 0x86c7\n" ); output( "\t.long 0x%08x,0x%08x\n", arg_types[0], arg_types[1] ); } for (i = 0; i <= spec->limit; i++) { ORDDEF *odp = spec->ordinals[i]; if (!odp || !is_function( odp )) continue; output( ".L__wine_%s_%u:\n", make_c_identifier(spec->dll_name), i ); output( "\tpushw %%bp\n" ); output( "\tpushl $%s\n", asm_name_stdcall16( odp->type == TYPE_STUB ? get_stub_name( odp, spec ) : odp->link_name , odp)); output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp ) ); } output( ".L__wine_spec_code_segment_end:\n" ); /* data segment */ output( "\n.L__wine_spec_data_segment:\n" ); output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */ for (i = 0; i <= spec->limit; i++) { ORDDEF *odp = spec->ordinals[i]; if (!odp || odp->type != TYPE_VARIABLE) continue; output( ".L__wine_%s_%u:\n", make_c_identifier(spec->dll_name), i ); output( "\t.long " ); for (j = 0; j < odp->u.var.n_values-1; j++) output( "0x%08x,", odp->u.var.values[j] ); output( "0x%08x\n", odp->u.var.values[j] ); } output( ".L__wine_spec_data_segment_end:\n" ); /* resource data */ if (spec->nb_resources) { output( "\n.L__wine_spec_resource_data:\n" ); output_res16_data( spec ); } output( "\t.byte 0\n" ); /* make sure the last symbol points to something */ /* relay functions */ nb_funcs = sort_func_list( typelist, nb_funcs, relay_type_compare ); if (nb_funcs) { output( "\n/* relay functions */\n\n" ); output( "\t.text\n" ); for ( i = 0; i < nb_funcs; i++ ) output_call16_function( typelist[i] ); output( "\t.data\n" ); output( "wine_ldt_copy_ptr:\n" ); output( "\t.long %s\n", asm_name("_imp__wine_ldt_copy") ); } free( typelist ); }