@@ -168,7 +168,8 @@ static bfd_vma image_base;
168168static bfd * filler_bfd ;
169169static struct bfd_section * edata_s , * reloc_s ;
170170static 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 ;
172173static int runtime_pseudo_relocs_created = 0 ;
173174static 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
10351036static 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
37133734bool
0 commit comments