diff options
author | Evgeny Karpov <evgeny.karpov@microsoft.com> | 2024-08-14 17:56:38 +0200 |
---|---|---|
committer | Evgeny Karpov <eukarpov@gmail.com> | 2024-11-19 14:27:32 +0100 |
commit | 5181d982c34a38a552f6d4d19adb039171893ad7 (patch) | |
tree | 44af355d8731dfc98acaaa2ecffb2a3aafe4fa1e /gcc | |
parent | 3f28c4f1df9f898f70acb244dc71f5b2cc57471b (diff) | |
download | gcc-5181d982c34a38a552f6d4d19adb039171893ad7.zip gcc-5181d982c34a38a552f6d4d19adb039171893ad7.tar.gz gcc-5181d982c34a38a552f6d4d19adb039171893ad7.tar.bz2 |
Support weak references
The patch adds support for weak references. The original MinGW
implementation targets ix86, which handles weak symbols differently
compared to AArch64. In AArch64, the weak symbols are replaced by
other symbols which reference the original weak symbols, and the
compiler does not track the original symbol names.
This patch resolves this and declares the original symbols.
Here is an explanation of why this change is needed and what the
difference is between x86_64-w64-mingw32 and aarch64-w64-mingw32.
The way x86_64 calls a weak function:
call weak_fn2
GCC emits the call and creates the required definitions at the end
of the assembly:
.weak weak_fn2
.def weak_fn2; .scl 2; .type 32; .endef
This is different from aarch64:
weak_fn2 will be legitimized and replaced by .refptr.weak_fn2,
and there will be no other references to weak_fn2 in the code.
adrp x0, .refptr.weak_fn2
add x0, x0, :lo12:.refptr.weak_fn2
ldr x0, [x0]
blr x0
GCC does not emit the required definitions at the end of the assembly,
and weak_fn2 is tracked only by the mingw stub sybmol.
Without the change, the stub definition will emit:
.section .rdata$.refptr.weak_fn2, "dr"
.globl .refptr.weak_fn2
.linkonce discard
.refptr.weak_fn2:
.quad weak_fn2
which is not enough. This fix will emit the required definitions:
.weak weak_fn2
.def weak_fn2; .scl 2; .type 32; .endef
.section .rdata$.refptr.weak_fn2, "dr"
.globl .refptr.weak_fn2
.linkonce discard
.refptr.weak_fn2:
.quad weak_fn2
This is the first commit in the third patch series with SMALL code
model fixes, optimization fixes, LTO, and minimal C++ enablement.
Prepared, refactored and validated by
Radek Barton <radek.barton@microsoft.com> and
Evgeny Karpov <evgeny.karpov@microsoft.com>
Contributor: Zac Walker <zacwalker@microsoft.com>
gcc/ChangeLog:
* config/aarch64/cygming.h (SUB_TARGET_RECORD_STUB): Request
declaration for weak symbols.
(PE_COFF_LEGITIMIZE_EXTERN_DECL): Legitimize external
declaration for weak symbols.
* config/i386/cygming.h (SUB_TARGET_RECORD_STUB): Update
declarations in ix86 with the same functionality.
(PE_COFF_LEGITIMIZE_EXTERN_DECL): Likewise.
* config/mingw/winnt-dll.cc (legitimize_pe_coff_symbol):
Support declaration for weak symbols if requested.
* config/mingw/winnt.cc (struct stub_list): Likewise.
(mingw_pe_record_stub): Likewise.
(mingw_pe_file_end): Likewise.
* config/mingw/winnt.h (mingw_pe_record_stub): Likewise.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/config/aarch64/cygming.h | 6 | ||||
-rw-r--r-- | gcc/config/i386/cygming.h | 4 | ||||
-rw-r--r-- | gcc/config/mingw/winnt-dll.cc | 4 | ||||
-rw-r--r-- | gcc/config/mingw/winnt.cc | 13 | ||||
-rw-r--r-- | gcc/config/mingw/winnt.h | 2 |
5 files changed, 21 insertions, 8 deletions
diff --git a/gcc/config/aarch64/cygming.h b/gcc/config/aarch64/cygming.h index 9ce140a..bd60780 100644 --- a/gcc/config/aarch64/cygming.h +++ b/gcc/config/aarch64/cygming.h @@ -171,7 +171,8 @@ still needed for compilation. */ mingw_handle_selectany_attribute, NULL } #undef SUB_TARGET_RECORD_STUB -#define SUB_TARGET_RECORD_STUB mingw_pe_record_stub +#define SUB_TARGET_RECORD_STUB(NAME, DECL) mingw_pe_record_stub((NAME), \ + DECL_WEAK ((DECL))) #define SUPPORTS_ONE_ONLY 1 @@ -186,7 +187,8 @@ still needed for compilation. */ #undef GOT_ALIAS_SET #define GOT_ALIAS_SET mingw_GOT_alias_set () -#define PE_COFF_LEGITIMIZE_EXTERN_DECL 1 +#define PE_COFF_LEGITIMIZE_EXTERN_DECL(RTX) \ + (GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_WEAK (RTX)) #define HAVE_64BIT_POINTERS 1 diff --git a/gcc/config/i386/cygming.h b/gcc/config/i386/cygming.h index bd1259f..67bfb8d 100644 --- a/gcc/config/i386/cygming.h +++ b/gcc/config/i386/cygming.h @@ -461,7 +461,7 @@ do { \ #define TARGET_ASM_ASSEMBLE_VISIBILITY i386_pe_assemble_visibility #undef SUB_TARGET_RECORD_STUB -#define SUB_TARGET_RECORD_STUB mingw_pe_record_stub +#define SUB_TARGET_RECORD_STUB(NAME, DECL) mingw_pe_record_stub((NAME), 0) /* Static stack checking is supported by means of probes. */ #define STACK_CHECK_STATIC_BUILTIN 1 @@ -470,7 +470,7 @@ do { \ # define HAVE_GAS_ALIGNED_COMM 0 #endif -#define PE_COFF_LEGITIMIZE_EXTERN_DECL \ +#define PE_COFF_LEGITIMIZE_EXTERN_DECL(RTX) \ (ix86_cmodel == CM_LARGE_PIC || ix86_cmodel == CM_MEDIUM_PIC) #define HAVE_64BIT_POINTERS TARGET_64BIT_DEFAULT diff --git a/gcc/config/mingw/winnt-dll.cc b/gcc/config/mingw/winnt-dll.cc index f74495b..eb7cff7 100644 --- a/gcc/config/mingw/winnt-dll.cc +++ b/gcc/config/mingw/winnt-dll.cc @@ -134,7 +134,7 @@ get_dllimport_decl (tree decl, bool beimport) { SYMBOL_REF_FLAGS (rtl) |= SYMBOL_FLAG_EXTERNAL; #ifdef SUB_TARGET_RECORD_STUB - SUB_TARGET_RECORD_STUB (name); + SUB_TARGET_RECORD_STUB (name, decl); #endif } @@ -206,7 +206,7 @@ legitimize_pe_coff_symbol (rtx addr, bool inreg) } } - if (!PE_COFF_LEGITIMIZE_EXTERN_DECL) + if (!PE_COFF_LEGITIMIZE_EXTERN_DECL (addr)) return NULL_RTX; if (GET_CODE (addr) == SYMBOL_REF diff --git a/gcc/config/mingw/winnt.cc b/gcc/config/mingw/winnt.cc index 9d433da..61e360b 100644 --- a/gcc/config/mingw/winnt.cc +++ b/gcc/config/mingw/winnt.cc @@ -636,6 +636,7 @@ struct GTY(()) stub_list { struct stub_list *next; const char *name; + bool is_weak_decl_needed; }; static GTY(()) struct export_list *export_head; @@ -673,7 +674,7 @@ mingw_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data) } void -mingw_pe_record_stub (const char *name) +mingw_pe_record_stub (const char *name, bool is_weak_decl_needed) { struct stub_list *p; @@ -692,6 +693,7 @@ mingw_pe_record_stub (const char *name) p = ggc_alloc<stub_list> (); p->next = stub_head; p->name = name; + p->is_weak_decl_needed = is_weak_decl_needed; stub_head = p; } @@ -808,6 +810,15 @@ mingw_pe_file_end (void) if (!startswith (name, "refptr.")) continue; name += 7; + + if (q->is_weak_decl_needed) + { +#ifdef ASM_WEAKEN_LABEL + ASM_WEAKEN_LABEL (asm_out_file, name); +#endif + mingw_pe_declare_function_type (asm_out_file, name, 1); + } + fprintf (asm_out_file, "\t.section\t.rdata$%s, \"dr\"\n" "\t.globl\t%s\n" "\t.linkonce\tdiscard\n", oname, oname); diff --git a/gcc/config/mingw/winnt.h b/gcc/config/mingw/winnt.h index 97fefbc..a21a36b 100644 --- a/gcc/config/mingw/winnt.h +++ b/gcc/config/mingw/winnt.h @@ -28,7 +28,7 @@ extern void mingw_pe_declare_function_type (FILE *file, const char *name, extern void mingw_pe_encode_section_info (tree, rtx, int); extern void mingw_pe_file_end (void); extern void mingw_pe_maybe_record_exported_symbol (tree, const char *, int); -extern void mingw_pe_record_stub (const char *); +extern void mingw_pe_record_stub (const char *, bool); extern unsigned int mingw_pe_section_type_flags (tree, const char *, int); extern void mingw_pe_unique_section (tree, int); extern bool mingw_pe_valid_dllimport_attribute_p (const_tree); |