winnt.c revision 117395
1/* Subroutines for insn-output.c for Windows NT. 2 Contributed by Douglas Rupp (drupp@cs.washington.edu) 3 Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002 4 Free Software Foundation, Inc. 5 6This file is part of GNU CC. 7 8GNU CC is free software; you can redistribute it and/or modify 9it under the terms of the GNU General Public License as published by 10the Free Software Foundation; either version 2, or (at your option) 11any later version. 12 13GNU CC is distributed in the hope that it will be useful, 14but WITHOUT ANY WARRANTY; without even the implied warranty of 15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16GNU General Public License for more details. 17 18You should have received a copy of the GNU General Public License 19along with GNU CC; see the file COPYING. If not, write to 20the Free Software Foundation, 59 Temple Place - Suite 330, 21Boston, MA 02111-1307, USA. */ 22 23#include "config.h" 24#include "system.h" 25#include "rtl.h" 26#include "regs.h" 27#include "hard-reg-set.h" 28#include "output.h" 29#include "tree.h" 30#include "flags.h" 31#include "tm_p.h" 32#include "toplev.h" 33#include "hashtab.h" 34#include "ggc.h" 35 36/* i386/PE specific attribute support. 37 38 i386/PE has two new attributes: 39 dllexport - for exporting a function/variable that will live in a dll 40 dllimport - for importing a function/variable from a dll 41 42 Microsoft allows multiple declspecs in one __declspec, separating 43 them with spaces. We do NOT support this. Instead, use __declspec 44 multiple times. 45*/ 46 47static tree associated_type PARAMS ((tree)); 48const char * gen_stdcall_suffix PARAMS ((tree)); 49int i386_pe_dllexport_p PARAMS ((tree)); 50int i386_pe_dllimport_p PARAMS ((tree)); 51void i386_pe_mark_dllexport PARAMS ((tree)); 52void i386_pe_mark_dllimport PARAMS ((tree)); 53 54/* Handle a "dllimport" or "dllexport" attribute; 55 arguments as in struct attribute_spec.handler. */ 56tree 57ix86_handle_dll_attribute (node, name, args, flags, no_add_attrs) 58 tree *node; 59 tree name; 60 tree args; 61 int flags; 62 bool *no_add_attrs; 63{ 64 /* These attributes may apply to structure and union types being created, 65 but otherwise should pass to the declaration involved. */ 66 if (!DECL_P (*node)) 67 { 68 if (flags & ((int) ATTR_FLAG_DECL_NEXT | (int) ATTR_FLAG_FUNCTION_NEXT 69 | (int) ATTR_FLAG_ARRAY_NEXT)) 70 { 71 *no_add_attrs = true; 72 return tree_cons (name, args, NULL_TREE); 73 } 74 if (TREE_CODE (*node) != RECORD_TYPE && TREE_CODE (*node) != UNION_TYPE) 75 { 76 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name)); 77 *no_add_attrs = true; 78 } 79 } 80 81 /* `extern' needn't be specified with dllimport. 82 Specify `extern' now and hope for the best. Sigh. */ 83 else if (TREE_CODE (*node) == VAR_DECL 84 && is_attribute_p ("dllimport", name)) 85 { 86 DECL_EXTERNAL (*node) = 1; 87 TREE_PUBLIC (*node) = 1; 88 } 89 90 return NULL_TREE; 91} 92 93/* Handle a "shared" attribute; 94 arguments as in struct attribute_spec.handler. */ 95tree 96ix86_handle_shared_attribute (node, name, args, flags, no_add_attrs) 97 tree *node; 98 tree name; 99 tree args ATTRIBUTE_UNUSED; 100 int flags ATTRIBUTE_UNUSED; 101 bool *no_add_attrs; 102{ 103 if (TREE_CODE (*node) != VAR_DECL) 104 { 105 warning ("`%s' attribute only applies to variables", 106 IDENTIFIER_POINTER (name)); 107 *no_add_attrs = true; 108 } 109 110 return NULL_TREE; 111} 112 113/* Return the type that we should use to determine if DECL is 114 imported or exported. */ 115 116static tree 117associated_type (decl) 118 tree decl; 119{ 120 tree t = NULL_TREE; 121 122 /* In the C++ frontend, DECL_CONTEXT for a method doesn't actually refer 123 to the containing class. So we look at the 'this' arg. */ 124 if (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE) 125 { 126 /* Artificial methods are not affected by the import/export status of 127 their class unless they are virtual. */ 128 if (! DECL_ARTIFICIAL (decl) || DECL_VINDEX (decl)) 129 t = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))); 130 } 131 else if (DECL_CONTEXT (decl) 132 && TREE_CODE_CLASS (TREE_CODE (DECL_CONTEXT (decl))) == 't') 133 t = DECL_CONTEXT (decl); 134 135 return t; 136} 137 138/* Return nonzero if DECL is a dllexport'd object. */ 139 140int 141i386_pe_dllexport_p (decl) 142 tree decl; 143{ 144 tree exp; 145 146 if (TREE_CODE (decl) != VAR_DECL 147 && TREE_CODE (decl) != FUNCTION_DECL) 148 return 0; 149 exp = lookup_attribute ("dllexport", DECL_ATTRIBUTES (decl)); 150 if (exp) 151 return 1; 152 153 /* Class members get the dllexport status of their class. */ 154 if (associated_type (decl)) 155 { 156 exp = lookup_attribute ("dllexport", 157 TYPE_ATTRIBUTES (associated_type (decl))); 158 if (exp) 159 return 1; 160 } 161 162 return 0; 163} 164 165/* Return nonzero if DECL is a dllimport'd object. */ 166 167int 168i386_pe_dllimport_p (decl) 169 tree decl; 170{ 171 tree imp; 172 173 if (TREE_CODE (decl) == FUNCTION_DECL 174 && TARGET_NOP_FUN_DLLIMPORT) 175 return 0; 176 177 if (TREE_CODE (decl) != VAR_DECL 178 && TREE_CODE (decl) != FUNCTION_DECL) 179 return 0; 180 imp = lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl)); 181 if (imp) 182 return 1; 183 184 /* Class members get the dllimport status of their class. */ 185 if (associated_type (decl)) 186 { 187 imp = lookup_attribute ("dllimport", 188 TYPE_ATTRIBUTES (associated_type (decl))); 189 if (imp) 190 return 1; 191 } 192 193 return 0; 194} 195 196/* Return nonzero if SYMBOL is marked as being dllexport'd. */ 197 198int 199i386_pe_dllexport_name_p (symbol) 200 const char *symbol; 201{ 202 return symbol[0] == DLL_IMPORT_EXPORT_PREFIX 203 && symbol[1] == 'e' && symbol[2] == '.'; 204} 205 206/* Return nonzero if SYMBOL is marked as being dllimport'd. */ 207 208int 209i386_pe_dllimport_name_p (symbol) 210 const char *symbol; 211{ 212 return symbol[0] == DLL_IMPORT_EXPORT_PREFIX 213 && symbol[1] == 'i' && symbol[2] == '.'; 214} 215 216/* Mark a DECL as being dllexport'd. 217 Note that we override the previous setting (eg: dllimport). */ 218 219void 220i386_pe_mark_dllexport (decl) 221 tree decl; 222{ 223 const char *oldname; 224 char *newname; 225 rtx rtlname; 226 tree idp; 227 228 rtlname = XEXP (DECL_RTL (decl), 0); 229 if (GET_CODE (rtlname) == SYMBOL_REF) 230 oldname = XSTR (rtlname, 0); 231 else if (GET_CODE (rtlname) == MEM 232 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF) 233 oldname = XSTR (XEXP (rtlname, 0), 0); 234 else 235 abort (); 236 if (i386_pe_dllimport_name_p (oldname)) 237 oldname += 9; 238 else if (i386_pe_dllexport_name_p (oldname)) 239 return; /* already done */ 240 241 newname = alloca (strlen (oldname) + 4); 242 sprintf (newname, "%ce.%s", DLL_IMPORT_EXPORT_PREFIX, oldname); 243 244 /* We pass newname through get_identifier to ensure it has a unique 245 address. RTL processing can sometimes peek inside the symbol ref 246 and compare the string's addresses to see if two symbols are 247 identical. */ 248 idp = get_identifier (newname); 249 250 XEXP (DECL_RTL (decl), 0) = 251 gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp)); 252} 253 254/* Mark a DECL as being dllimport'd. */ 255 256void 257i386_pe_mark_dllimport (decl) 258 tree decl; 259{ 260 const char *oldname; 261 char *newname; 262 tree idp; 263 rtx rtlname, newrtl; 264 265 rtlname = XEXP (DECL_RTL (decl), 0); 266 if (GET_CODE (rtlname) == SYMBOL_REF) 267 oldname = XSTR (rtlname, 0); 268 else if (GET_CODE (rtlname) == MEM 269 && GET_CODE (XEXP (rtlname, 0)) == SYMBOL_REF) 270 oldname = XSTR (XEXP (rtlname, 0), 0); 271 else 272 abort (); 273 if (i386_pe_dllexport_name_p (oldname)) 274 { 275 error ("`%s' declared as both exported to and imported from a DLL", 276 IDENTIFIER_POINTER (DECL_NAME (decl))); 277 return; 278 } 279 else if (i386_pe_dllimport_name_p (oldname)) 280 { 281 /* Already done, but force correct linkage since the redeclaration 282 might have omitted explicit extern. Sigh. */ 283 if (TREE_CODE (decl) == VAR_DECL 284 /* ??? Is this test for vtables needed? */ 285 && !DECL_VIRTUAL_P (decl)) 286 { 287 DECL_EXTERNAL (decl) = 1; 288 TREE_PUBLIC (decl) = 1; 289 } 290 return; 291 } 292 293 /* ??? One can well ask why we're making these checks here, 294 and that would be a good question. */ 295 296 /* Imported variables can't be initialized. Note that C++ classes 297 are marked initial, so we need to check. */ 298 if (TREE_CODE (decl) == VAR_DECL 299 && !DECL_VIRTUAL_P (decl) 300 && (DECL_INITIAL (decl) 301 && ! TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))) 302 { 303 error_with_decl (decl, "initialized variable `%s' is marked dllimport"); 304 return; 305 } 306 /* Nor can they be static. */ 307 if (TREE_CODE (decl) == VAR_DECL 308 /* ??? Is this test for vtables needed? */ 309 && !DECL_VIRTUAL_P (decl) 310 && 0 /*???*/) 311 { 312 error_with_decl (decl, "static variable `%s' is marked dllimport"); 313 return; 314 } 315 316 newname = alloca (strlen (oldname) + 11); 317 sprintf (newname, "%ci._imp__%s", DLL_IMPORT_EXPORT_PREFIX, oldname); 318 319 /* We pass newname through get_identifier to ensure it has a unique 320 address. RTL processing can sometimes peek inside the symbol ref 321 and compare the string's addresses to see if two symbols are 322 identical. */ 323 idp = get_identifier (newname); 324 325 newrtl = gen_rtx (MEM, Pmode, 326 gen_rtx (SYMBOL_REF, Pmode, 327 IDENTIFIER_POINTER (idp))); 328 XEXP (DECL_RTL (decl), 0) = newrtl; 329 330 /* Can't treat a pointer to this as a constant address */ 331 DECL_NON_ADDR_CONST_P (decl) = 1; 332} 333 334/* Return string which is the former assembler name modified with a 335 suffix consisting of an atsign (@) followed by the number of bytes of 336 arguments */ 337 338const char * 339gen_stdcall_suffix (decl) 340 tree decl; 341{ 342 int total = 0; 343 /* ??? This probably should use XSTR (XEXP (DECL_RTL (decl), 0), 0) instead 344 of DECL_ASSEMBLER_NAME. */ 345 const char *asmname = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); 346 char *newsym; 347 348 if (TYPE_ARG_TYPES (TREE_TYPE (decl))) 349 if (TREE_VALUE (tree_last (TYPE_ARG_TYPES (TREE_TYPE (decl)))) 350 == void_type_node) 351 { 352 tree formal_type = TYPE_ARG_TYPES (TREE_TYPE (decl)); 353 354 while (TREE_VALUE (formal_type) != void_type_node) 355 { 356 int parm_size 357 = TREE_INT_CST_LOW (TYPE_SIZE (TREE_VALUE (formal_type))); 358 /* Must round up to include padding. This is done the same 359 way as in store_one_arg. */ 360 parm_size = ((parm_size + PARM_BOUNDARY - 1) 361 / PARM_BOUNDARY * PARM_BOUNDARY); 362 total += parm_size; 363 formal_type = TREE_CHAIN (formal_type); 364 } 365 } 366 367 newsym = xmalloc (strlen (asmname) + 10); 368 sprintf (newsym, "%s@%d", asmname, total/BITS_PER_UNIT); 369 return IDENTIFIER_POINTER (get_identifier (newsym)); 370} 371 372void 373i386_pe_encode_section_info (decl, first) 374 tree decl; 375 int first; 376{ 377 if (!first) 378 return; 379 380 /* This bit is copied from i386.h. */ 381 if (optimize > 0 && TREE_CONSTANT (decl) 382 && (!flag_writable_strings || TREE_CODE (decl) != STRING_CST)) 383 { 384 rtx rtl = (TREE_CODE_CLASS (TREE_CODE (decl)) != 'd' 385 ? TREE_CST_RTL (decl) : DECL_RTL (decl)); 386 SYMBOL_REF_FLAG (XEXP (rtl, 0)) = 1; 387 } 388 389 if (TREE_CODE (decl) == FUNCTION_DECL) 390 if (lookup_attribute ("stdcall", 391 TYPE_ATTRIBUTES (TREE_TYPE (decl)))) 392 XEXP (DECL_RTL (decl), 0) = 393 gen_rtx (SYMBOL_REF, Pmode, gen_stdcall_suffix (decl)); 394 395 /* Mark the decl so we can tell from the rtl whether the object is 396 dllexport'd or dllimport'd. */ 397 398 if (i386_pe_dllexport_p (decl)) 399 i386_pe_mark_dllexport (decl); 400 else if (i386_pe_dllimport_p (decl)) 401 i386_pe_mark_dllimport (decl); 402 /* It might be that DECL has already been marked as dllimport, but a 403 subsequent definition nullified that. The attribute is gone but 404 DECL_RTL still has (DLL_IMPORT_EXPORT_PREFIX)i._imp__foo. We need 405 to remove that. Ditto for the DECL_NON_ADDR_CONST_P flag. */ 406 else if ((TREE_CODE (decl) == FUNCTION_DECL 407 || TREE_CODE (decl) == VAR_DECL) 408 && DECL_RTL (decl) != NULL_RTX 409 && GET_CODE (DECL_RTL (decl)) == MEM 410 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == MEM 411 && GET_CODE (XEXP (XEXP (DECL_RTL (decl), 0), 0)) == SYMBOL_REF 412 && i386_pe_dllimport_name_p (XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0))) 413 { 414 const char *oldname = XSTR (XEXP (XEXP (DECL_RTL (decl), 0), 0), 0); 415 tree idp = get_identifier (oldname + 9); 416 rtx newrtl = gen_rtx (SYMBOL_REF, Pmode, IDENTIFIER_POINTER (idp)); 417 418 XEXP (DECL_RTL (decl), 0) = newrtl; 419 420 DECL_NON_ADDR_CONST_P (decl) = 0; 421 422 /* We previously set TREE_PUBLIC and DECL_EXTERNAL. 423 We leave these alone for now. */ 424 } 425} 426 427/* Strip only the leading encoding, leaving the stdcall suffix. */ 428 429const char * 430i386_pe_strip_name_encoding (str) 431 const char *str; 432{ 433 if (*str == DLL_IMPORT_EXPORT_PREFIX) 434 str += 3; 435 if (*str == '*') 436 str += 1; 437 return str; 438} 439 440/* Also strip the stdcall suffix. */ 441 442const char * 443i386_pe_strip_name_encoding_full (str) 444 const char *str; 445{ 446 const char *p; 447 const char *name = i386_pe_strip_name_encoding (str); 448 449 p = strchr (name, '@'); 450 if (p) 451 return ggc_alloc_string (name, p - name); 452 453 return name; 454} 455 456void 457i386_pe_unique_section (decl, reloc) 458 tree decl; 459 int reloc; 460{ 461 int len; 462 const char *name, *prefix; 463 char *string; 464 465 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)); 466 name = i386_pe_strip_name_encoding_full (name); 467 468 /* The object is put in, for example, section .text$foo. 469 The linker will then ultimately place them in .text 470 (everything from the $ on is stripped). Don't put 471 read-only data in .rdata section to avoid a PE linker 472 bug when .rdata$* grouped sections are used in code 473 without a .rdata section. */ 474 if (TREE_CODE (decl) == FUNCTION_DECL) 475 prefix = ".text$"; 476 else if (decl_readonly_section (decl, reloc)) 477 prefix = ".rdata$"; 478 else 479 prefix = ".data$"; 480 len = strlen (name) + strlen (prefix); 481 string = alloca (len + 1); 482 sprintf (string, "%s%s", prefix, name); 483 484 DECL_SECTION_NAME (decl) = build_string (len, string); 485} 486 487/* Select a set of attributes for section NAME based on the properties 488 of DECL and whether or not RELOC indicates that DECL's initializer 489 might contain runtime relocations. 490 491 We make the section read-only and executable for a function decl, 492 read-only for a const data decl, and writable for a non-const data decl. 493 494 If the section has already been defined, to not allow it to have 495 different attributes, as (1) this is ambiguous since we're not seeing 496 all the declarations up front and (2) some assemblers (e.g. SVR4) 497 do not recoginize section redefinitions. */ 498/* ??? This differs from the "standard" PE implementation in that we 499 handle the SHARED variable attribute. Should this be done for all 500 PE targets? */ 501 502#define SECTION_PE_SHARED SECTION_MACH_DEP 503 504unsigned int 505i386_pe_section_type_flags (decl, name, reloc) 506 tree decl; 507 const char *name; 508 int reloc; 509{ 510 static htab_t htab; 511 unsigned int flags; 512 unsigned int **slot; 513 514 /* The names we put in the hashtable will always be the unique 515 versions gived to us by the stringtable, so we can just use 516 their addresses as the keys. */ 517 if (!htab) 518 htab = htab_create (31, htab_hash_pointer, htab_eq_pointer, NULL); 519 520 if (decl && TREE_CODE (decl) == FUNCTION_DECL) 521 flags = SECTION_CODE; 522 else if (decl && decl_readonly_section (decl, reloc)) 523 flags = 0; 524 else 525 { 526 flags = SECTION_WRITE; 527 528 if (decl && TREE_CODE (decl) == VAR_DECL 529 && lookup_attribute ("shared", DECL_ATTRIBUTES (decl))) 530 flags |= SECTION_PE_SHARED; 531 } 532 533 if (decl && DECL_ONE_ONLY (decl)) 534 flags |= SECTION_LINKONCE; 535 536 /* See if we already have an entry for this section. */ 537 slot = (unsigned int **) htab_find_slot (htab, name, INSERT); 538 if (!*slot) 539 { 540 *slot = (unsigned int *) xmalloc (sizeof (unsigned int)); 541 **slot = flags; 542 } 543 else 544 { 545 if (decl && **slot != flags) 546 error_with_decl (decl, "%s causes a section type conflict"); 547 } 548 549 return flags; 550} 551 552void 553i386_pe_asm_named_section (name, flags) 554 const char *name; 555 unsigned int flags; 556{ 557 char flagchars[8], *f = flagchars; 558 559 if (flags & SECTION_CODE) 560 *f++ = 'x'; 561 if (flags & SECTION_WRITE) 562 *f++ = 'w'; 563 if (flags & SECTION_PE_SHARED) 564 *f++ = 's'; 565 *f = '\0'; 566 567 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars); 568 569 if (flags & SECTION_LINKONCE) 570 { 571 /* Functions may have been compiled at various levels of 572 optimization so we can't use `same_size' here. 573 Instead, have the linker pick one. */ 574 fprintf (asm_out_file, "\t.linkonce %s\n", 575 (flags & SECTION_CODE ? "discard" : "same_size")); 576 } 577} 578 579/* The Microsoft linker requires that every function be marked as 580 DT_FCN. When using gas on cygwin, we must emit appropriate .type 581 directives. */ 582 583#include "gsyms.h" 584 585/* Mark a function appropriately. This should only be called for 586 functions for which we are not emitting COFF debugging information. 587 FILE is the assembler output file, NAME is the name of the 588 function, and PUBLIC is nonzero if the function is globally 589 visible. */ 590 591void 592i386_pe_declare_function_type (file, name, public) 593 FILE *file; 594 const char *name; 595 int public; 596{ 597 fprintf (file, "\t.def\t"); 598 assemble_name (file, name); 599 fprintf (file, ";\t.scl\t%d;\t.type\t%d;\t.endef\n", 600 public ? (int) C_EXT : (int) C_STAT, 601 (int) DT_FCN << N_BTSHFT); 602} 603 604/* Keep a list of external functions. */ 605 606struct extern_list 607{ 608 struct extern_list *next; 609 const char *name; 610}; 611 612static struct extern_list *extern_head; 613 614/* Assemble an external function reference. We need to keep a list of 615 these, so that we can output the function types at the end of the 616 assembly. We can't output the types now, because we might see a 617 definition of the function later on and emit debugging information 618 for it then. */ 619 620void 621i386_pe_record_external_function (name) 622 const char *name; 623{ 624 struct extern_list *p; 625 626 p = (struct extern_list *) xmalloc (sizeof *p); 627 p->next = extern_head; 628 p->name = name; 629 extern_head = p; 630} 631 632/* Keep a list of exported symbols. */ 633 634struct export_list 635{ 636 struct export_list *next; 637 const char *name; 638 int is_data; /* used to type tag exported symbols. */ 639}; 640 641static struct export_list *export_head; 642 643/* Assemble an export symbol entry. We need to keep a list of 644 these, so that we can output the export list at the end of the 645 assembly. We used to output these export symbols in each function, 646 but that causes problems with GNU ld when the sections are 647 linkonce. */ 648 649void 650i386_pe_record_exported_symbol (name, is_data) 651 const char *name; 652 int is_data; 653{ 654 struct export_list *p; 655 656 p = (struct export_list *) xmalloc (sizeof *p); 657 p->next = export_head; 658 p->name = name; 659 p->is_data = is_data; 660 export_head = p; 661} 662 663/* This is called at the end of assembly. For each external function 664 which has not been defined, we output a declaration now. We also 665 output the .drectve section. */ 666 667void 668i386_pe_asm_file_end (file) 669 FILE *file; 670{ 671 struct extern_list *p; 672 673 ix86_asm_file_end (file); 674 675 for (p = extern_head; p != NULL; p = p->next) 676 { 677 tree decl; 678 679 decl = get_identifier (p->name); 680 681 /* Positively ensure only one declaration for any given symbol. */ 682 if (! TREE_ASM_WRITTEN (decl) && TREE_SYMBOL_REFERENCED (decl)) 683 { 684 TREE_ASM_WRITTEN (decl) = 1; 685 i386_pe_declare_function_type (file, p->name, TREE_PUBLIC (decl)); 686 } 687 } 688 689 if (export_head) 690 { 691 struct export_list *q; 692 drectve_section (); 693 for (q = export_head; q != NULL; q = q->next) 694 { 695 fprintf (file, "\t.ascii \" -export:%s%s\"\n", 696 i386_pe_strip_name_encoding (q->name), 697 (q->is_data) ? ",data" : ""); 698 } 699 } 700} 701 702