From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1698) id 81FBC384AB7E; Fri, 19 Apr 2024 13:15:35 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 81FBC384AB7E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1713532535; bh=IHexanzvw+thkcOzupYNBszXM0Ax3C3/GlU2Bjkzof0=; h=From:To:Subject:Date:From; b=OU2mb0RQyWI3Rvf76RMgXRpHCPkdNRNECIZrYNNm37bgbSA2eGiFtZpllt7LFX3wg H9w9pvFB9jXFXqG8y8gZlA2hOjTJKDIEn2Y2CHSnOIE4y0IcBkNwHAbY01/AaeIEkI Uc1FvJ+ScmZoeOFee3gqfkYC/6hdPkK0d31gfups= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Iain D Sandoe To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-10350] Objective-C, Darwin: Do not overalign CFStrings and Objective-C metadata. X-Act-Checkin: gcc X-Git-Author: Iain Sandoe X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 3588c72322c0f54bf013366350e98d420570d2f5 X-Git-Newrev: 877d87416656cbd8bf21676e6f54865583aa03c8 Message-Id: <20240419131535.81FBC384AB7E@sourceware.org> Date: Fri, 19 Apr 2024 13:15:35 +0000 (GMT) List-Id: https://gcc.gnu.org/g:877d87416656cbd8bf21676e6f54865583aa03c8 commit r12-10350-g877d87416656cbd8bf21676e6f54865583aa03c8 Author: Iain Sandoe Date: Thu Jan 25 20:11:09 2024 +0000 Objective-C, Darwin: Do not overalign CFStrings and Objective-C metadata. We have reports of regressions in both Objective-C and Objective-C++ on Darwin23 (macOS 14). In some cases, these are linker warnings about the alignment of CFString constants; in other cases the built executables crash during runtime initialization. The underlying issue is the same in both cases; since the objects (CFStrings, Objective-C meta-data) are TU- local, we are choosing to increase their alignment for efficiency - to values greater than ABI alignment. However, although these objects are TU-local, they are also visible to the linker (since they are placed in specific named sections). In many cases the metadata can be regarded as tables of data, and thus it is expected that these sections can be concatenated from multiple TUs and the data treated as tabular. In order for this to work the data cannot be allowed to exceed ABI alignment - which leads to the crashes. For GCC-15+ it would be nice to find a more elegant solution to this issue (perhaps by adjusting the concept of binds-locally to exclude specific named sections) - but I do not want to do that in stage 4. The solution here is to force the alignment to be preserved as created by setting DECL_USER_ALIGN on the relevant objects. gcc/ChangeLog: * config/darwin.cc (darwin_build_constant_cfstring): Prevent over- alignment of CFString constants by setting DECL_USER_ALIGN. gcc/objc/ChangeLog: * objc-next-runtime-abi-02.cc (build_v2_address_table): Prevent over-alignment of Objective-C metadata by setting DECL_USER_ALIGN on relevant variables. (build_v2_protocol_list_address_table): Likewise. (generate_v2_protocol_list): Likewise. (generate_v2_meth_descriptor_table): Likewise. (generate_v2_meth_type_list): Likewise. (generate_v2_property_table): Likewise. (generate_v2_dispatch_table): Likewise. (generate_v2_ivars_list): Likewise. (generate_v2_class_structs): Likewise. (build_ehtype): Likewise. * objc-runtime-shared-support.cc (generate_strings): Likewise. Signed-off-by: Iain Sandoe (cherry picked from commit f74f840d35117bcaf995cee99fb2ab30c60f64f3) Diff: --- gcc/config/darwin.cc | 1 + gcc/objc/objc-next-runtime-abi-02.cc | 18 +++++++++++++++--- gcc/objc/objc-runtime-shared-support.cc | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/gcc/config/darwin.cc b/gcc/config/darwin.cc index a11095716d8..129250a48fd 100644 --- a/gcc/config/darwin.cc +++ b/gcc/config/darwin.cc @@ -3859,6 +3859,7 @@ darwin_build_constant_cfstring (tree str) /* global namespace. */ DECL_CONTEXT (var) = NULL_TREE; DECL_INITIAL (var) = constructor; + DECL_USER_ALIGN (var) = 1; lang_hooks.decls.pushdecl (var); rest_of_decl_compilation (var, 1, 0); desc->ccf_str = var; diff --git a/gcc/objc/objc-next-runtime-abi-02.cc b/gcc/objc/objc-next-runtime-abi-02.cc index a9b3e32b92d..1dee05df769 100644 --- a/gcc/objc/objc-next-runtime-abi-02.cc +++ b/gcc/objc/objc-next-runtime-abi-02.cc @@ -2246,6 +2246,7 @@ build_v2_address_table (vec *src, const char *nam, tree attr) DECL_PRESERVE_P (decl) = 1; expr = objc_build_constructor (type, initlist); OBJCMETA (decl, objc_meta, attr); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, expr); } @@ -2320,8 +2321,9 @@ build_v2_protocol_list_address_table (void) decl = create_global_decl (objc_protocol_type, buf, /*is def=*/true); expr = convert (objc_protocol_type, build_fold_addr_expr (ref->refdecl)); OBJCMETA (decl, objc_meta, meta_label_protocollist); - finish_var_decl (decl, expr); DECL_PRESERVE_P (decl) = 1; + DECL_USER_ALIGN (decl) = 1; + finish_var_decl (decl, expr); } /* TODO: delete the vec. */ @@ -2399,6 +2401,7 @@ generate_v2_protocol_list (tree i_or_p, tree klass_ctxt) /* ObjC2 puts all these in the base section. */ OBJCMETA (refs_decl, objc_meta, meta_base); DECL_PRESERVE_P (refs_decl) = 1; + DECL_USER_ALIGN (refs_decl) = 1; finish_var_decl (refs_decl, objc_build_constructor (TREE_TYPE (refs_decl),initlist)); return refs_decl; @@ -2507,6 +2510,7 @@ generate_v2_meth_descriptor_table (tree chain, tree protocol, CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, initlist); /* Get into the right section. */ OBJCMETA (decl, objc_meta, attr); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, objc_build_constructor (method_list_template, v)); return decl; } @@ -2525,13 +2529,14 @@ generate_v2_meth_type_list (vec& all_meths, tree protocol, IDENTIFIER_POINTER (PROTOCOL_NAME (protocol))); tree decl = start_var_decl (list_type, nam); free (nam); - OBJCMETA (decl, objc_meta, meta_base); vec *v = NULL; for (unsigned i = 0; i < size; ++i) CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, add_objc_string (METHOD_ENCODING (all_meths[i]), meth_var_types)); + OBJCMETA (decl, objc_meta, meta_base); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, objc_build_constructor (list_type, v)); return decl; } @@ -2654,6 +2659,7 @@ generate_v2_property_table (tree context, tree klass_ctxt) CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, initlist); OBJCMETA (decl, objc_meta, meta_base); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), inits)); return decl; } @@ -2865,6 +2871,7 @@ generate_v2_dispatch_table (tree chain, const char *name, tree attr) CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, initlist); OBJCMETA (decl, objc_meta, attr); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), v)); return decl; @@ -3162,6 +3169,7 @@ generate_v2_ivars_list (tree chain, const char *name, tree attr, tree templ) build_int_cst (integer_type_node, size)); CONSTRUCTOR_APPEND_ELT (inits, NULL_TREE, initlist); OBJCMETA (decl, objc_meta, attr); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, objc_build_constructor (TREE_TYPE (decl), inits)); generating_instance_variables = 0; return decl; @@ -3429,7 +3437,6 @@ generate_v2_class_structs (struct imp_entry *impent) decl = start_var_decl (objc_v2_class_ro_template, newabi_append_ro (IDENTIFIER_POINTER (DECL_NAME (metaclass_decl)))); - /* TODO: ivarLayout needs t be built. */ initlist = build_v2_class_ro_t_initializer (TREE_TYPE (decl), name_expr, @@ -3439,6 +3446,7 @@ generate_v2_class_structs (struct imp_entry *impent) class_ivars, NULL_TREE); /* The ROs sit in the default const section. */ OBJCMETA (decl, objc_meta, meta_base); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, initlist); /* static struct class_t _OBJC_METACLASS_Foo = { ... }; */ @@ -3450,6 +3458,7 @@ generate_v2_class_structs (struct imp_entry *impent) build_fold_addr_expr (UOBJC_V2_CACHE_decl), build_fold_addr_expr (UOBJC_V2_VTABLE_decl)); /* The class section attributes are set when they are created. */ + DECL_USER_ALIGN (metaclass_decl) = 1; finish_var_decl (metaclass_decl, initlist); impent->meta_decl = metaclass_decl; @@ -3529,6 +3538,7 @@ generate_v2_class_structs (struct imp_entry *impent) inst_ivars, props); /* The ROs sit in the default const section. */ OBJCMETA (decl, objc_meta, meta_base); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, initlist); /* static struct class_t _OBJC_CLASS_Foo = { ... }; */ @@ -3540,6 +3550,7 @@ generate_v2_class_structs (struct imp_entry *impent) build_fold_addr_expr (UOBJC_V2_VTABLE_decl)); /* The class section attributes are set when they are created. */ + DECL_USER_ALIGN (class_decl) = 1; finish_var_decl (class_decl, initlist); impent->class_decl = class_decl; @@ -3714,6 +3725,7 @@ build_ehtype (tree name, const char *eh_name, bool weak) DECL_WEAK (ehtype_decl) = 1; inits = objc2_build_ehtype_initializer (name_expr, class_name_expr); OBJCMETA (ehtype_decl, objc_meta, meta_ehtype); + DECL_USER_ALIGN (ehtype_decl) = 1; finish_var_decl (ehtype_decl, inits); return ehtype_decl; } diff --git a/gcc/objc/objc-runtime-shared-support.cc b/gcc/objc/objc-runtime-shared-support.cc index 75c28fbdc5c..074e418abd6 100644 --- a/gcc/objc/objc-runtime-shared-support.cc +++ b/gcc/objc/objc-runtime-shared-support.cc @@ -684,6 +684,7 @@ generate_strings (void) decl = TREE_PURPOSE (chain); string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1, IDENTIFIER_POINTER (string)); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, string_expr); } @@ -693,6 +694,7 @@ generate_strings (void) decl = TREE_PURPOSE (chain); string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1, IDENTIFIER_POINTER (string)); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, string_expr); } @@ -702,6 +704,7 @@ generate_strings (void) decl = TREE_PURPOSE (chain); string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1, IDENTIFIER_POINTER (string)); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, string_expr); } @@ -711,6 +714,7 @@ generate_strings (void) decl = TREE_PURPOSE (chain); string_expr = my_build_string (IDENTIFIER_LENGTH (string) + 1, IDENTIFIER_POINTER (string)); + DECL_USER_ALIGN (decl) = 1; finish_var_decl (decl, string_expr); } }