Skip to content

Commit 894fe48

Browse files
eukarpovgithub-actions
authored andcommitted
Support weak references (#28)
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 in all places, leading to missing declarations.
1 parent b674118 commit 894fe48

File tree

5 files changed

+20
-8
lines changed

5 files changed

+20
-8
lines changed

gcc/config/aarch64/cygming.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ extern void i386_pe_record_external_function (tree, const char *);
279279
mingw_handle_selectany_attribute, NULL }
280280

281281
#undef SUB_TARGET_RECORD_STUB
282-
#define SUB_TARGET_RECORD_STUB mingw_pe_record_stub
282+
#define SUB_TARGET_RECORD_STUB(NAME, DECL) mingw_pe_record_stub((NAME), DECL_WEAK((DECL)))
283283

284284
#define SUPPORTS_ONE_ONLY 1
285285

@@ -314,7 +314,8 @@ do { \
314314
#undef GOT_ALIAS_SET
315315
#define GOT_ALIAS_SET mingw_GOT_alias_set ()
316316

317-
#define PE_COFF_LEGITIMIZE_EXTERN_DECL 0
317+
#define PE_COFF_LEGITIMIZE_EXTERN_DECL(RTX) \
318+
(GET_CODE (RTX) == SYMBOL_REF && SYMBOL_REF_WEAK(RTX))
318319

319320
#define HAVE_64BIT_POINTERS 1
320321

gcc/config/i386/cygming.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ do { \
461461
#define TARGET_ASM_ASSEMBLE_VISIBILITY i386_pe_assemble_visibility
462462

463463
#undef SUB_TARGET_RECORD_STUB
464-
#define SUB_TARGET_RECORD_STUB mingw_pe_record_stub
464+
#define SUB_TARGET_RECORD_STUB(NAME, DECL) mingw_pe_record_stub((NAME), 0)
465465

466466
/* Static stack checking is supported by means of probes. */
467467
#define STACK_CHECK_STATIC_BUILTIN 1
@@ -470,7 +470,7 @@ do { \
470470
# define HAVE_GAS_ALIGNED_COMM 0
471471
#endif
472472

473-
#define PE_COFF_LEGITIMIZE_EXTERN_DECL \
473+
#define PE_COFF_LEGITIMIZE_EXTERN_DECL(RTX) \
474474
(ix86_cmodel == CM_LARGE_PIC || ix86_cmodel == CM_MEDIUM_PIC)
475475

476476
#define HAVE_64BIT_POINTERS TARGET_64BIT_DEFAULT

gcc/config/mingw/winnt-dll.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ get_dllimport_decl (tree decl, bool beimport)
134134
{
135135
SYMBOL_REF_FLAGS (rtl) |= SYMBOL_FLAG_EXTERNAL;
136136
#ifdef SUB_TARGET_RECORD_STUB
137-
SUB_TARGET_RECORD_STUB (name);
137+
SUB_TARGET_RECORD_STUB (name, decl);
138138
#endif
139139
}
140140

@@ -206,7 +206,7 @@ legitimize_pe_coff_symbol (rtx addr, bool inreg)
206206
}
207207
}
208208

209-
if (!PE_COFF_LEGITIMIZE_EXTERN_DECL)
209+
if (!PE_COFF_LEGITIMIZE_EXTERN_DECL(addr))
210210
return NULL_RTX;
211211

212212
if (GET_CODE (addr) == SYMBOL_REF

gcc/config/mingw/winnt.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ struct GTY(()) stub_list
635635
{
636636
struct stub_list *next;
637637
const char *name;
638+
bool is_weak_decl_needed;
638639
};
639640

640641
static GTY(()) struct export_list *export_head;
@@ -672,7 +673,7 @@ mingw_pe_maybe_record_exported_symbol (tree decl, const char *name, int is_data)
672673
}
673674

674675
void
675-
mingw_pe_record_stub (const char *name)
676+
mingw_pe_record_stub (const char *name, bool is_weak_decl_needed)
676677
{
677678
struct stub_list *p;
678679

@@ -691,6 +692,7 @@ mingw_pe_record_stub (const char *name)
691692
p = ggc_alloc<stub_list> ();
692693
p->next = stub_head;
693694
p->name = name;
695+
p->is_weak_decl_needed = is_weak_decl_needed;
694696
stub_head = p;
695697
}
696698

@@ -807,6 +809,15 @@ mingw_pe_file_end (void)
807809
if (!startswith (name, "refptr."))
808810
continue;
809811
name += 7;
812+
813+
if (q->is_weak_decl_needed)
814+
{
815+
#ifdef ASM_WEAKEN_LABEL
816+
ASM_WEAKEN_LABEL (asm_out_file, name);
817+
#endif
818+
mingw_pe_declare_function_type(asm_out_file, name, 1);
819+
}
820+
810821
fprintf (asm_out_file, "\t.section\t.rdata$%s, \"dr\"\n"
811822
"\t.globl\t%s\n"
812823
"\t.linkonce\tdiscard\n", oname, oname);

gcc/config/mingw/winnt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern void mingw_pe_declare_function_type (FILE *file, const char *name,
3030
extern void mingw_pe_encode_section_info (tree, rtx, int);
3131
extern void mingw_pe_file_end (void);
3232
extern void mingw_pe_maybe_record_exported_symbol (tree, const char *, int);
33-
extern void mingw_pe_record_stub (const char *);
33+
extern void mingw_pe_record_stub (const char *, bool);
3434
extern unsigned int mingw_pe_section_type_flags (tree, const char *, int);
3535
extern void mingw_pe_unique_section (tree, int);
3636
extern bool mingw_pe_valid_dllimport_attribute_p (const_tree);

0 commit comments

Comments
 (0)