2006-10-16 Mike Stump * config/rs6000/darwin.h (SUBTARGET_OVERRIDE_OPTIONS): Add -mkernel support. (C_COMMON_OVERRIDE_OPTIONS): Add -mkernel support. Add SUBTARGET_C_COMMON_OVERRIDE_OPTIONS callout. (CC1_SPEC): Don't turn on -fPIC when -mkernel is given. (OS_MISSING_ALTIVEC): Add. * config/i386/i386.c (override_options): Add SUBSUBTARGET_OVERRIDE_OPTIONS callout. * config/i386/darwin.h (CC1_SPEC): Don't turn on -fPIC when -mkernel, -static or -mdynamic-no-pic is given. (C_COMMON_OVERRIDE_OPTIONS): Add. * config/darwin.opt (fapple-kext): Add. (mkernel): Add. * config/darwin.h (TARGET_OPTION_TRANSLATE_TABLE): Add -fapple-kext, -findirect-virtual-calls, -fterminated-vtables and -mkernel support. (SUBSUBTARGET_OVERRIDE_OPTIONS): Add. (SUBTARGET_C_COMMON_OVERRIDE_OPTIONS): Add. (CPP_SPEC): Move defines for __DYNAMIC__ and __STATIC__ from here... (SUBTARGET_ATTRIBUTE_TABLE): Add apple_kext_compatibility. (TARGET_CXX_CDTOR_RETURNS_THIS): Add. (flag_mkernel): Add. (flag_apple_kext): Add. (TARGET_KEXTABI): Add. * config/darwin.c (darwin_handle_kext_attribute): Add. (DARWIN_VTABLE_P): Add. (darwin_binds_local_p): Add partial support for rebinding vtables in kexts. (darwin_kextabi_p): Add. (darwin_override_options): Add. * config/darwin-protos.h (darwin_handle_kext_attribute): Add. (darwin_kextabi_p): Add. (darwin_override_options): Add. * config/darwin-c.c (darwin_cpp_builtins): ... move defines for __DYNAMIC__ and __STATIC__ here. Doing diffs in .: --- ./config/darwin-c.c.~1~ 2006-07-05 19:26:44.000000000 -0700 +++ ./config/darwin-c.c 2006-10-12 13:03:42.000000000 -0700 @@ -619,4 +619,9 @@ darwin_cpp_builtins (cpp_reader *pfile) if (darwin_macosx_version_min) builtin_define_with_value ("__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__", version_as_macro(), false); + + if (flag_pic || MACHO_DYNAMIC_NO_PIC_P) + builtin_define ("__DYNAMIC__"); + else + builtin_define ("__STATIC__"); } --- ./config/darwin-protos.h.~1~ 2006-07-05 19:26:44.000000000 -0700 +++ ./config/darwin-protos.h 2006-10-12 14:49:02.000000000 -0700 @@ -73,6 +73,7 @@ extern void darwin_file_end (void); extern void darwin_mark_decl_preserved (const char *); +extern tree darwin_handle_kext_attribute (tree *, tree, tree, int, bool *); extern tree darwin_handle_weak_import_attribute (tree *node, tree name, tree args, int flags, bool * no_add_attrs); @@ -86,3 +87,5 @@ extern void darwin_asm_output_dwarf_offs extern bool darwin_binds_local_p (tree); extern void darwin_cpp_builtins (struct cpp_reader *); extern void darwin_asm_output_anchor (rtx symbol); +extern bool darwin_kextabi_p (void); +extern void darwin_override_options (void); --- ./config/darwin.c.~1~ 2006-09-19 16:18:27.000000000 -0700 +++ ./config/darwin.c 2006-10-16 11:29:41.000000000 -0700 @@ -1303,6 +1303,42 @@ darwin_unique_section (tree decl ATTRIBU /* Darwin does not use unique sections. */ } +/* Handle __attribute__ ((apple_kext_compatibility)). + This only applies to darwin kexts for 2.95 compatibility -- it shrinks the + vtable for classes with this attribute (and their descendants) by not + outputting the new 3.0 nondeleting destructor. This means that such + objects CANNOT be allocated on the stack or as globals UNLESS they have + a completely empty `operator delete'. + Luckily, this fits in with the Darwin kext model. + + This attribute also disables gcc3's potential overlaying of derived + class data members on the padding at the end of the base class. */ + +tree +darwin_handle_kext_attribute (tree *node, tree name, + tree args ATTRIBUTE_UNUSED, + int flags ATTRIBUTE_UNUSED, + bool *no_add_attrs) +{ + /* APPLE KEXT stuff -- only applies with pure static C++ code. */ + if (! TARGET_KEXTABI) + { + warning (0, "%<%s%> 2.95 vtable-compatability attribute applies " + "only when compiling a kext", IDENTIFIER_POINTER (name)); + + *no_add_attrs = true; + } + else if (TREE_CODE (*node) != RECORD_TYPE) + { + warning (0, "%<%s%> 2.95 vtable-compatability attribute applies " + "only to C++ classes", IDENTIFIER_POINTER (name)); + + *no_add_attrs = true; + } + + return NULL_TREE; +} + /* Handle a "weak_import" attribute; arguments as in struct attribute_spec.handler. */ @@ -1526,13 +1562,17 @@ darwin_file_end (void) fprintf (asm_out_file, "\t.subsections_via_symbols\n"); } +/* TODO: Add a language hook for identifying if a decl is a vtable. */ +#define DARWIN_VTABLE_P(DECL) 0 + /* Cross-module name binding. Darwin does not support overriding - functions at dynamic-link time. */ + functions at dynamic-link time, except for vtables in kexts. */ bool darwin_binds_local_p (tree decl) { - return default_binds_local_p_1 (decl, 0); + return default_binds_local_p_1 (decl, + TARGET_KEXTABI && DARWIN_VTABLE_P (decl)); } #if 0 @@ -1562,4 +1602,34 @@ darwin_set_default_type_attributes (tree TYPE_ATTRIBUTES (type)); } +/* True, iff we're generating code for loadable kernel extentions. */ + +bool +darwin_kextabi_p (void) { + return flag_apple_kext; +} + +void +darwin_override_options (void) +{ + if (flag_apple_kext && strcmp (lang_hooks.name, "GNU C++") != 0) + { + warning (0, "command line option %<-fapple-kext%> is only valid for C++"); + flag_apple_kext = 0; + } + if (flag_mkernel || flag_apple_kext) + { + /* -mkernel implies -fapple-kext for C++ */ + if (strcmp (lang_hooks.name, "GNU C++") == 0) + flag_apple_kext = 1; + + flag_no_common = 1; + + /* No EH in kexts. */ + flag_exceptions = 0; + /* No -fnon-call-exceptions data in kexts. */ + flag_non_call_exceptions = 0; + } +} + #include "gt-darwin.h" --- ./config/darwin.h.~1~ 2006-10-11 19:28:12.000000000 -0700 +++ ./config/darwin.h 2006-10-13 15:45:04.000000000 -0700 @@ -127,14 +127,18 @@ extern GTY(()) int darwin_ms_struct; { "-segs_read_write_addr", "-Zsegs_read_write_addr" }, \ { "-seg_addr_table", "-Zseg_addr_table" }, \ { "-seg_addr_table_filename", "-Zfn_seg_addr_table_filename" }, \ + { "-fapple-kext", "-fapple-kext -static -Wa,-static" }, \ { "-filelist", "-Xlinker -filelist -Xlinker" }, \ - { "-framework", "-Xlinker -framework -Xlinker" }, \ + { "-findirect-virtual-calls", "-fapple-kext" }, \ { "-flat_namespace", "-Zflat_namespace" }, \ { "-force_cpusubtype_ALL", "-Zforce_cpusubtype_ALL" }, \ { "-force_flat_namespace", "-Zforce_flat_namespace" }, \ + { "-framework", "-Xlinker -framework -Xlinker" }, \ + { "-fterminated-vtables", "-fapple-kext" }, \ { "-image_base", "-Zimage_base" }, \ { "-init", "-Zinit" }, \ { "-install_name", "-Zinstall_name" }, \ + { "-mkernel", "-mkernel -static -Wa,-static" }, \ { "-multiply_defined_unused", "-Zmultiplydefinedunused" }, \ { "-multiply_defined", "-Zmultiply_defined" }, \ { "-multi_module", "-Zmulti_module" }, \ @@ -143,6 +147,11 @@ extern GTY(()) int darwin_ms_struct; { "-unexported_symbols_list", "-Zunexported_symbols_list" }, \ SUBTARGET_OPTION_TRANSLATE_TABLE +#define SUBSUBTARGET_OVERRIDE_OPTIONS \ + do { \ + darwin_override_options (); \ + } while (0) + /* These compiler options take n arguments. */ #undef WORD_SWITCH_TAKES_ARG @@ -188,11 +197,24 @@ extern GTY(()) int darwin_ms_struct; !strcmp (STR, "dylinker_install_name") ? 1 : \ 0) +#define SUBTARGET_C_COMMON_OVERRIDE_OPTIONS do { \ + if (flag_mkernel || flag_apple_kext) \ + { \ + if (flag_use_cxa_atexit == 2) \ + flag_use_cxa_atexit = 0; \ + /* kexts should always be built without the coalesced sections \ + because the kernel loader doesn't grok such sections. */ \ + flag_weak = 0; \ + /* No RTTI in kexts. */ \ + flag_rtti = 0; \ + } \ + } while (0) + /* Machine dependent cpp options. Don't add more options here, add them to darwin_cpp_builtins in darwin-c.c. */ #undef CPP_SPEC -#define CPP_SPEC "%{static:%{!dynamic:-D__STATIC__}}%{!static:-D__DYNAMIC__}" +#define CPP_SPEC "" /* This is mostly a clone of the standard LINK_COMMAND_SPEC, plus precomp, libtool, and fat build additions. Also we @@ -693,6 +715,8 @@ extern GTY(()) section * darwin_sections /* Extra attributes for Darwin. */ #define SUBTARGET_ATTRIBUTE_TABLE \ /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */ \ + { "apple_kext_compatibility", 0, 0, false, true, false, \ + darwin_handle_kext_attribute }, \ { "weak_import", 0, 0, true, false, false, \ darwin_handle_weak_import_attribute } @@ -924,4 +948,11 @@ __enable_execute_stack (void *addr) (void) mprotect (page, end - page, 7); \ } +/* For Apple KEXTs, we make the constructors return this to match gcc + 2.95. */ +#define TARGET_CXX_CDTOR_RETURNS_THIS (darwin_kextabi_p) +extern int flag_mkernel; +extern int flag_apple_kext; +#define TARGET_KEXTABI flag_apple_kext + #endif /* CONFIG_DARWIN_H */ --- ./config/darwin.opt.~1~ 2006-07-05 19:26:44.000000000 -0700 +++ ./config/darwin.opt 2006-10-12 13:02:03.000000000 -0700 @@ -30,3 +30,11 @@ The earliest MacOS X version on which th mone-byte-bool Target RejectNegative Report Var(darwin_one_byte_bool) Set sizeof(bool) to 1 + +fapple-kext +Target Report Var(flag_apple_kext) +Generate code for darwin loadable kernel extentions + +mkernel +Target Report Var(flag_mkernel) +Generate code for the kernel or loadable kernel extentions --- ./config/i386/darwin.h.~1~ 2006-10-11 19:28:10.000000000 -0700 +++ ./config/i386/darwin.h 2006-10-16 11:07:18.000000000 -0700 @@ -70,7 +70,7 @@ Boston, MA 02110-1301, USA. */ the kernel or some such. */ #undef CC1_SPEC -#define CC1_SPEC "%{!static:-fPIC}\ +#define CC1_SPEC "%{!mkernel:%{!static:%{!mdynamic-no-pic:-fPIC}}} \ %{g: %{!fno-eliminate-unused-debug-symbols: -feliminate-unused-debug-symbols }}" #undef ASM_SPEC @@ -182,6 +182,11 @@ extern void darwin_x86_file_end (void); else fprintf (FILE, "\tcall mcount\n"); \ } while (0) +#define C_COMMON_OVERRIDE_OPTIONS \ + do { \ + SUBTARGET_C_COMMON_OVERRIDE_OPTIONS; \ + } while (0) + /* Darwin on x86_64 uses dwarf-2 by default. */ #undef PREFERRED_DEBUGGING_TYPE #define PREFERRED_DEBUGGING_TYPE (TARGET_64BIT ? DWARF2_DEBUG : DBX_DEBUG) --- ./config/i386/i386.c.~1~ 2006-10-11 19:28:10.000000000 -0700 +++ ./config/i386/i386.c 2006-10-13 15:57:10.000000000 -0700 @@ -1537,6 +1537,10 @@ override_options (void) SUBTARGET_OVERRIDE_OPTIONS; #endif +#ifdef SUBSUBTARGET_OVERRIDE_OPTIONS + SUBSUBTARGET_OVERRIDE_OPTIONS; +#endif + /* -fPIC is the default for x86_64. */ if (TARGET_MACHO && TARGET_64BIT) flag_pic = 2; --- ./config/rs6000/darwin.h.~1~ 2006-10-11 19:28:12.000000000 -0700 +++ ./config/rs6000/darwin.h 2006-10-16 11:06:45.000000000 -0700 @@ -91,6 +91,11 @@ do { \ target_flags |= MASK_POWERPC64; \ warning (0, "-m64 requires PowerPC64 architecture, enabling"); \ } \ + if (flag_mkernel) \ + { \ + rs6000_default_long_calls = 1; \ + target_flags |= MASK_SOFT_FLOAT; \ + } \ } while(0) #define C_COMMON_OVERRIDE_OPTIONS do { \ @@ -100,6 +105,9 @@ do { \ || strverscmp (darwin_macosx_version_min, "10.4.6") < 0) \ && flag_use_cxa_get_exception_ptr == 2) \ flag_use_cxa_get_exception_ptr = 0; \ + if (flag_mkernel) \ + flag_no_builtin = 1; \ + SUBTARGET_C_COMMON_OVERRIDE_OPTIONS; \ } while (0) /* Darwin has 128-bit long double support in libc in 10.4 and later. @@ -114,9 +122,9 @@ do { \ the kernel or some such. */ #define CC1_SPEC "\ -%{g: %{!fno-eliminate-unused-debug-symbols: -feliminate-unused-debug-symbols }} \ -%{static: %{Zdynamic: %e conflicting code gen style switches are used}}\ -%{!static:%{!mdynamic-no-pic:-fPIC}}" + %{g: %{!fno-eliminate-unused-debug-symbols: -feliminate-unused-debug-symbols }} \ + %{static: %{Zdynamic: %e conflicting code gen style switches are used}}\ + %{!mkernel:%{!static:%{!mdynamic-no-pic:-fPIC}}}" #define DARWIN_SUBARCH_SPEC " \ %{m64: ppc64} \ @@ -440,3 +448,7 @@ do { \ (TARGET_64BIT \ || (darwin_macosx_version_min \ && strverscmp (darwin_macosx_version_min, "10.3") >= 0)) + +/* When generating kernel code or kexts, we don't use Altivec by + default, as kernel code doesn't save/restore those registers. */ +#define OS_MISSING_ALTIVEC (flag_mkernel || flag_apple_kext) --------------