@@ -16,8 +16,9 @@ use crate::attributes::cfg::parse_cfg_entry;
1616use crate :: session_diagnostics:: {
1717 AsNeededCompatibility , BundleNeedsStatic , EmptyLinkName , ExportSymbolsNeedsStatic ,
1818 ImportNameTypeRaw , ImportNameTypeX86 , IncompatibleWasmLink , InvalidLinkModifier ,
19- LinkFrameworkApple , LinkOrdinalOutOfRange , LinkRequiresName , MultipleModifiers ,
20- NullOnLinkSection , RawDylibNoNul , RawDylibOnlyWindows , WholeArchiveNeedsStatic ,
19+ InvalidMachoSection , InvalidMachoSectionReason , LinkFrameworkApple , LinkOrdinalOutOfRange ,
20+ LinkRequiresName , MultipleModifiers , NullOnLinkSection , RawDylibNoNul , RawDylibOnlyWindows ,
21+ WholeArchiveNeedsStatic ,
2122} ;
2223
2324pub ( crate ) struct LinkNameParser ;
@@ -462,6 +463,29 @@ impl LinkParser {
462463
463464pub ( crate ) struct LinkSectionParser ;
464465
466+ fn check_link_section_macho ( name : Symbol ) -> Result < ( ) , InvalidMachoSectionReason > {
467+ let mut parts = name. as_str ( ) . split ( ',' ) . map ( |s| s. trim ( ) ) ;
468+
469+ // The segment can be empty.
470+ let _segment = parts. next ( ) ;
471+
472+ // But the section is required.
473+ let section = match parts. next ( ) {
474+ None | Some ( "" ) => return Err ( InvalidMachoSectionReason :: MissingSection ) ,
475+ Some ( section) => section,
476+ } ;
477+
478+ if section. len ( ) > 16 {
479+ return Err ( InvalidMachoSectionReason :: SectionTooLong { section : section. to_string ( ) } ) ;
480+ }
481+
482+ // LLVM also checks the other components of the section specifier, but that logic is hard to
483+ // keep in sync. We skip it here for now, assuming that if you got that far you'll be able
484+ // to interpret the LLVM errors.
485+
486+ Ok ( ( ) )
487+ }
488+
465489impl < S : Stage > SingleAttributeParser < S > for LinkSectionParser {
466490 const PATH : & [ Symbol ] = & [ sym:: link_section] ;
467491 const ON_DUPLICATE : OnDuplicate < S > = OnDuplicate :: WarnButFutureError ;
@@ -495,6 +519,18 @@ impl<S: Stage> SingleAttributeParser<S> for LinkSectionParser {
495519 return None ;
496520 }
497521
522+ // We (currently) only validate macho section specifiers.
523+ match cx. sess . target . binary_format {
524+ BinaryFormat :: MachO => match check_link_section_macho ( name) {
525+ Ok ( ( ) ) => { }
526+ Err ( reason) => {
527+ cx. emit_err ( InvalidMachoSection { name_span : nv. value_span , reason } ) ;
528+ return None ;
529+ }
530+ } ,
531+ BinaryFormat :: Coff | BinaryFormat :: Elf | BinaryFormat :: Wasm | BinaryFormat :: Xcoff => { }
532+ }
533+
498534 Some ( LinkSection { name, span : cx. attr_span } )
499535 }
500536}
0 commit comments