@@ -15,8 +15,8 @@ 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 ,
19+ SymbolKind , SymbolScope ,
2020} ;
2121use std:: collections:: HashMap ;
2222use std:: collections:: hash_map:: Entry ;
@@ -394,6 +394,7 @@ impl Module for ObjectModule {
394394 data_relocs : _,
395395 ref custom_segment_section,
396396 align,
397+ used,
397398 } = data;
398399
399400 let pointer_reloc = match self . isa . triple ( ) . pointer_width ( ) . unwrap ( ) {
@@ -422,7 +423,7 @@ impl Module for ObjectModule {
422423 } else {
423424 StandardSection :: ReadOnlyDataWithRel
424425 } ;
425- if self . per_data_object_section {
426+ if self . per_data_object_section || used {
426427 // FIXME pass empty symbol name once add_subsection produces `.text` as section name
427428 // instead of `.text.` when passed an empty symbol name. (object#748) Until then
428429 // pass `subsection` to produce `.text.subsection` as section name to reduce
@@ -451,6 +452,34 @@ impl Module for ObjectModule {
451452 )
452453 } ;
453454
455+ if used {
456+ match self . object . format ( ) {
457+ object:: BinaryFormat :: Elf => {
458+ let section = self . object . section_mut ( section) ;
459+ match & mut section. flags {
460+ SectionFlags :: None => {
461+ section. flags = SectionFlags :: Elf {
462+ sh_flags : object:: elf:: SHF_GNU_RETAIN . into ( ) ,
463+ }
464+ }
465+ SectionFlags :: Elf { sh_flags } => {
466+ * sh_flags |= u64:: from ( object:: elf:: SHF_GNU_RETAIN )
467+ }
468+ _ => unreachable ! ( ) ,
469+ }
470+ }
471+ object:: BinaryFormat :: Coff => { }
472+ object:: BinaryFormat :: MachO => {
473+ let symbol = self . object . symbol_mut ( symbol) ;
474+ assert ! ( matches!( symbol. flags, SymbolFlags :: None ) ) ;
475+ symbol. flags = SymbolFlags :: MachO {
476+ n_desc : object:: macho:: N_NO_DEAD_STRIP ,
477+ }
478+ }
479+ _ => unreachable ! ( ) ,
480+ }
481+ }
482+
454483 let align = std:: cmp:: max ( align. unwrap_or ( 1 ) , self . isa . symbol_alignment ( ) ) ;
455484 let offset = match * init {
456485 Init :: Uninitialized => {
0 commit comments