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