@@ -15,8 +15,7 @@ use object::write::{
1515 Object , Relocation , SectionId , StandardSection , Symbol , SymbolId , SymbolSection ,
1616} ;
1717use object:: {
18- RelocationEncoding , RelocationFlags , RelocationKind , SectionKind , SymbolFlags , SymbolKind ,
19- SymbolScope ,
18+ RelocationEncoding , RelocationFlags , RelocationKind , SectionFlags , SectionKind , SymbolFlags , SymbolKind , SymbolScope
2019} ;
2120use std:: collections:: HashMap ;
2221use std:: collections:: hash_map:: Entry ;
@@ -394,6 +393,7 @@ impl Module for ObjectModule {
394393 data_relocs : _,
395394 ref custom_segment_section,
396395 align,
396+ used,
397397 } = data;
398398
399399 let pointer_reloc = match self . isa . triple ( ) . pointer_width ( ) . unwrap ( ) {
@@ -422,7 +422,7 @@ impl Module for ObjectModule {
422422 } else {
423423 StandardSection :: ReadOnlyDataWithRel
424424 } ;
425- if self . per_data_object_section {
425+ if self . per_data_object_section || used {
426426 // FIXME pass empty symbol name once add_subsection produces `.text` as section name
427427 // instead of `.text.` when passed an empty symbol name. (object#748) Until then
428428 // pass `subsection` to produce `.text.subsection` as section name to reduce
@@ -451,6 +451,34 @@ impl Module for ObjectModule {
451451 )
452452 } ;
453453
454+ if used {
455+ match self . object . format ( ) {
456+ object:: BinaryFormat :: Elf => {
457+ let section = self . object . section_mut ( section) ;
458+ match & mut section. flags {
459+ SectionFlags :: None => {
460+ section. flags = SectionFlags :: Elf {
461+ sh_flags : object:: elf:: SHF_GNU_RETAIN . into ( ) ,
462+ }
463+ }
464+ SectionFlags :: Elf { sh_flags } => {
465+ * sh_flags |= u64:: from ( object:: elf:: SHF_GNU_RETAIN )
466+ }
467+ _ => unreachable ! ( ) ,
468+ }
469+ }
470+ object:: BinaryFormat :: Coff => { } ,
471+ object:: BinaryFormat :: MachO => {
472+ let symbol = self . object . symbol_mut ( symbol) ;
473+ assert ! ( matches!( symbol. flags, SymbolFlags :: None ) ) ;
474+ symbol. flags = SymbolFlags :: MachO {
475+ n_desc : object:: macho:: N_NO_DEAD_STRIP ,
476+ }
477+ }
478+ _ => unreachable ! ( ) ,
479+ }
480+ }
481+
454482 let align = std:: cmp:: max ( align. unwrap_or ( 1 ) , self . isa . symbol_alignment ( ) ) ;
455483 let offset = match * init {
456484 Init :: Uninitialized => {
0 commit comments