Skip to content

Commit f3d8ae9

Browse files
committed
Fix seg-fault when generating an empty DLL with LTO enabled.
ld PR 29998 * pe-dll.c (generate_reloc): Handle sections with no assigned output section. Terminate early of there are no relocs to put in the .reloc section. (pe_exe_fill_sections): Do not emit an empty .reloc section. bfd * cofflink.c (_bfd_coff_generic_relocate_section): Add an assertion that the output section is set for defined, global symbols.
1 parent 59d49a8 commit f3d8ae9

File tree

4 files changed

+61
-24
lines changed

4 files changed

+61
-24
lines changed

bfd/ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
2023-01-24 Nick Clifton <[email protected]>
2+
3+
PR 29998
4+
* cofflink.c (_bfd_coff_generic_relocate_section): Add an
5+
assertion that the output section is set for defined, global
6+
symbols.
7+
18
2023-01-17 Xianmiao Qu <[email protected]>
29

310
* elf32-csky.c (elf32_csky_merge_attributes): Don't save

bfd/cofflink.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,10 +2977,11 @@ _bfd_coff_generic_relocate_section (bfd *output_bfd,
29772977
else
29782978
{
29792979
if (h->root.type == bfd_link_hash_defined
2980+
/* Defined weak symbols are a GNU extension. */
29802981
|| h->root.type == bfd_link_hash_defweak)
29812982
{
2982-
/* Defined weak symbols are a GNU extension. */
29832983
sec = h->root.u.def.section;
2984+
BFD_ASSERT (sec->output_section != NULL);
29842985
val = (h->root.u.def.value
29852986
+ sec->output_section->vma
29862987
+ sec->output_offset);
@@ -3087,7 +3088,6 @@ _bfd_coff_generic_relocate_section (bfd *output_bfd,
30873088
return false;
30883089
case bfd_reloc_overflow:
30893090
{
3090-
30913091
/* Ignore any weak undef symbols that may have overflowed. Due to
30923092
PR ld/19011 the base address is now in the upper 64-bit address
30933093
range. This means that when _bfd_final_link_relocate calculates
@@ -3123,5 +3123,6 @@ _bfd_coff_generic_relocate_section (bfd *output_bfd,
31233123
}
31243124
}
31253125
}
3126+
31263127
return true;
31273128
}

ld/ChangeLog

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
2023-01-24 Nick Clifton <[email protected]>
2+
3+
PR 29998
4+
* pe-dll.c (generate_reloc): Handle sections with no assigned
5+
output section. Terminate early of there are no relocs to put in
6+
the .reloc section.
7+
(pe_exe_fill_sections): Do not emit an empty .reloc section.
8+
19
2023-01-06 Nick Clifton <[email protected]>
210

311
* po/bg.po: Updated Bulgarian translation.

ld/pe-dll.c

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ static bfd_vma image_base;
168168
static bfd *filler_bfd;
169169
static struct bfd_section *edata_s, *reloc_s;
170170
static unsigned char *edata_d, *reloc_d;
171-
static size_t edata_sz, reloc_sz;
171+
static unsigned char *reloc_d = NULL;
172+
static size_t edata_sz, reloc_sz = 0;
172173
static int runtime_pseudo_relocs_created = 0;
173174
static bool runtime_pseudp_reloc_v2_init = false;
174175

@@ -1033,9 +1034,10 @@ process_def_file_and_drectve (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *
10331034
/* Build the bfd that will contain .edata and .reloc sections. */
10341035

10351036
static void
1036-
build_filler_bfd (int include_edata)
1037+
build_filler_bfd (bool include_edata)
10371038
{
10381039
lang_input_statement_type *filler_file;
1040+
10391041
filler_file = lang_add_input_file ("dll stuff",
10401042
lang_input_file_is_fake_enum,
10411043
NULL);
@@ -1526,6 +1528,8 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
15261528

15271529
if (reloc_s == NULL || reloc_s->output_section == bfd_abs_section_ptr)
15281530
return;
1531+
1532+
/* Set an upper bound for the total number of relocations we will have to generate. */
15291533
total_relocs = 0;
15301534
for (b = info->input_bfds; b; b = b->link.next)
15311535
for (s = b->sections; s; s = s->next)
@@ -1541,28 +1545,34 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
15411545

15421546
for (s = b->sections; s; s = s->next)
15431547
{
1544-
bfd_vma sec_vma = s->output_section->vma + s->output_offset;
1548+
bfd_vma sec_vma;
15451549
asymbol **symbols;
15461550

1547-
/* If it's not loaded, we don't need to relocate it this way. */
1548-
if (!(s->output_section->flags & SEC_LOAD))
1549-
continue;
1551+
/* If the section is not going to be output, then ignore it. */
1552+
if (s->output_section == NULL)
1553+
{
1554+
/* PR 29998: LTO processing can elminate whole code sections,
1555+
but it sets the output section to NULL rather than *ABS*.
1556+
Fix that here, then ignore the section. */
1557+
s->output_section = bfd_abs_section_ptr;
1558+
continue;
1559+
}
15501560

15511561
/* I don't know why there would be a reloc for these, but I've
15521562
seen it happen - DJ */
15531563
if (s->output_section == bfd_abs_section_ptr)
15541564
continue;
15551565

1566+
/* If it's not loaded, we don't need to relocate it this way. */
1567+
if (!(s->output_section->flags & SEC_LOAD))
1568+
continue;
1569+
1570+
/* This happens when linking with --just-symbols=<file>
1571+
so do not generate an error. */
15561572
if (s->output_section->vma == 0)
1557-
{
1558-
/* Huh? Shouldn't happen, but punt if it does. */
1559-
#if 0 /* This happens when linking with --just-symbols=<file>, so do not generate an error. */
1560-
einfo (_("%P: zero vma section reloc detected: `%s' #%d f=%d\n"),
1561-
s->output_section->name, s->output_section->index,
1562-
s->output_section->flags);
1563-
#endif
1564-
continue;
1565-
}
1573+
continue;
1574+
1575+
sec_vma = s->output_section->vma + s->output_offset;
15661576

15671577
if (!bfd_generic_link_read_symbols (b))
15681578
{
@@ -1696,12 +1706,17 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
16961706
}
16971707
}
16981708
}
1709+
16991710
free (relocs);
17001711
/* Warning: the allocated symbols are remembered in BFD and
17011712
reused later, so don't free them! */
17021713
}
17031714
}
17041715

1716+
/* This can happen for example when LTO has eliminated all code. */
1717+
if (total_relocs == 0)
1718+
return;
1719+
17051720
/* At this point, we have total_relocs relocation addresses in
17061721
reloc_addresses, which are all suitable for the .reloc section.
17071722
We must now create the new sections. */
@@ -1726,9 +1741,9 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
17261741

17271742
reloc_sz = (reloc_sz + 3) & ~3; /* 4-byte align. */
17281743
reloc_d = xmalloc (reloc_sz);
1729-
sec_page = (bfd_vma) -1;
1744+
1745+
page_ptr = sec_page = (bfd_vma) -1;
17301746
reloc_sz = 0;
1731-
page_ptr = (bfd_vma) -1;
17321747

17331748
for (i = 0; i < total_relocs; i++)
17341749
{
@@ -1758,7 +1773,6 @@ generate_reloc (bfd *abfd, struct bfd_link_info *info)
17581773
bfd_put_16 (abfd, reloc_data[i].extra, reloc_d + reloc_sz);
17591774
reloc_sz += 2;
17601775
}
1761-
17621776
}
17631777

17641778
while (reloc_sz & 3)
@@ -3649,14 +3663,14 @@ pe_dll_build_sections (bfd *abfd, struct bfd_link_info *info)
36493663
{
36503664
if (pe_dll_enable_reloc_section)
36513665
{
3652-
build_filler_bfd (0);
3666+
build_filler_bfd (false /* edata not needed. */);
36533667
pe_output_file_set_long_section_names (filler_bfd);
36543668
}
36553669
return;
36563670
}
36573671

36583672
generate_edata ();
3659-
build_filler_bfd (1);
3673+
build_filler_bfd (true /* edata is needed. */);
36603674
pe_output_file_set_long_section_names (filler_bfd);
36613675
}
36623676

@@ -3692,6 +3706,7 @@ pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
36923706
image_base = pe_data (abfd)->pe_opthdr.ImageBase;
36933707

36943708
generate_reloc (abfd, info);
3709+
36953710
if (reloc_sz > 0)
36963711
{
36973712
bfd_set_section_size (reloc_s, reloc_sz);
@@ -3705,9 +3720,15 @@ pe_exe_fill_sections (bfd *abfd, struct bfd_link_info *info)
37053720

37063721
/* Do the assignments again. */
37073722
lang_do_assignments (lang_final_phase_enum);
3723+
3724+
reloc_s->contents = reloc_d;
3725+
}
3726+
else if (reloc_s)
3727+
{
3728+
/* Do not emit an empty reloc section. */
3729+
bfd_set_section_flags (reloc_s, SEC_IN_MEMORY | SEC_EXCLUDE);
3730+
reloc_s->output_section = bfd_abs_section_ptr;
37083731
}
3709-
if (reloc_s)
3710-
reloc_s->contents = reloc_d;
37113732
}
37123733

37133734
bool

0 commit comments

Comments
 (0)