GeezerVison is showing github.com — Auto merge of #155065 - folkertdev:macho-section-specifier, r=<try> · rust-lang/rust@3e6930f
File treeExpand file treeCollapse file tree Open diff view settings rustc_attr_parsing/src/attributes
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: compiler/rustc_attr_parsing/src/attributes/link_attrs.rs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -497,7 +497,7 @@ impl<S: Stage> SingleAttributeParser<S> for LinkSectionParser { | 497 | 497 | return None; | 498 | 498 | } | 499 | 499 |
| 500 | | - Some(LinkSection { name, span: cx.attr_span }) | | 500 | + Some(LinkSection { name, name_span: nv.value_span, span: cx.attr_span }) | 501 | 501 | } | 502 | 502 | } | 503 | 503 |
| |
Collapse file Copy file name to clipboardExpand all lines: compiler/rustc_hir/src/attrs/data_structures.rs +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1229,8 +1229,9 @@ pub enum AttributeKind { | 1229 | 1229 |
| 1230 | 1230 | /// Represents [`#[link_section]`](https://doc.rust-lang.org/reference/abi.html#the-link_section-attribute) | 1231 | 1231 | LinkSection { | 1232 | | - name: Symbol, | 1233 | 1232 | span: Span, | | 1233 | + name: Symbol, | | 1234 | + name_span: Span, | 1234 | 1235 | }, | 1235 | 1236 |
| 1236 | 1237 | /// Represents `#[linkage]`. | |
Collapse file Copy file name to clipboardExpand all lines: compiler/rustc_passes/src/check_attr.rs +44-1Lines changed: 44 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,6 +43,7 @@ use rustc_session::lint::builtin::{ | 43 | 43 | use rustc_session::parse::feature_err; | 44 | 44 | use rustc_span::edition::Edition; | 45 | 45 | use rustc_span::{DUMMY_SP, Ident, Span, Symbol, sym}; | | 46 | +use rustc_target::spec::BinaryFormat; | 46 | 47 | use rustc_trait_selection::error_reporting::InferCtxtErrorExt; | 47 | 48 | use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs}; | 48 | 49 | use rustc_trait_selection::traits::ObligationCtxt; | @@ -199,6 +200,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | 199 | 200 | Attribute::Parsed(AttributeKind::Link(_, attr_span)) => { | 200 | 201 | self.check_link(hir_id, *attr_span, span, target) | 201 | 202 | }, | | 203 | + Attribute::Parsed(AttributeKind::LinkSection { span: _, name, name_span }) => { | | 204 | + self.check_link_section(*name, *name_span) | | 205 | + }, | 202 | 206 | Attribute::Parsed(AttributeKind::MacroExport { span, .. }) => { | 203 | 207 | self.check_macro_export(hir_id, *span, target) | 204 | 208 | }, | @@ -249,7 +253,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | 249 | 253 | | AttributeKind::Lang(..) | 250 | 254 | | AttributeKind::LinkName { .. } | 251 | 255 | | AttributeKind::LinkOrdinal { .. } | 252 | | - | AttributeKind::LinkSection { .. } | 253 | 256 | | AttributeKind::Linkage(..) | 254 | 257 | | AttributeKind::MacroEscape( .. ) | 255 | 258 | | AttributeKind::MacroUse { .. } | @@ -1196,6 +1199,46 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | 1196 | 1199 | ); | 1197 | 1200 | } | 1198 | 1201 |
| | 1202 | + /// Checks if `#[link_section]` has a valid argument. | | 1203 | + fn check_link_section(&self, name: Symbol, name_span: Span) { | | 1204 | + // We (currently) only validate macho section specifiers. | | 1205 | + match self.tcx.sess.target.binary_format { | | 1206 | + BinaryFormat::MachO => match Self::check_link_section_macho(name) { | | 1207 | + Ok(()) => {} | | 1208 | + Err(reason) => { | | 1209 | + let err = errors::InvalidMachoSection { name_span, reason }; | | 1210 | + self.tcx.dcx().emit_err(err); | | 1211 | + } | | 1212 | + }, | | 1213 | + BinaryFormat::Coff | BinaryFormat::Elf | BinaryFormat::Wasm | BinaryFormat::Xcoff => {} | | 1214 | + } | | 1215 | + } | | 1216 | + | | 1217 | + fn check_link_section_macho(name: Symbol) -> Result<(), errors::InvalidMachoSectionReason> { | | 1218 | + use errors::InvalidMachoSectionReason as Reason; | | 1219 | + | | 1220 | + let mut parts = name.as_str().split(',').map(|s| s.trim()); | | 1221 | + | | 1222 | + // The segment can be empty. | | 1223 | + let _segment = parts.next(); | | 1224 | + | | 1225 | + // But the section is required. | | 1226 | + let section = match parts.next() { | | 1227 | + None | Some("") => return Err(Reason::MissingSection), | | 1228 | + Some(section) => section, | | 1229 | + }; | | 1230 | + | | 1231 | + if section.len() > 16 { | | 1232 | + return Err(Reason::SectionTooLong { section: section.to_string() }); | | 1233 | + } | | 1234 | + | | 1235 | + // LLVM also checks the other components of the section specifier, but that logic is hard to | | 1236 | + // keep in sync. We skip it here for now, assuming that if you got that far you'll be able | | 1237 | + // to interpret the LLVM errors. | | 1238 | + | | 1239 | + Ok(()) | | 1240 | + } | | 1241 | + | 1199 | 1242 | /// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument. | 1200 | 1243 | fn check_rustc_legacy_const_generics( | 1201 | 1244 | &self, | |
Collapse file Copy file name to clipboardExpand all lines: compiler/rustc_passes/src/errors.rs +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1332,3 +1332,22 @@ pub(crate) struct UnknownFormatParameterForOnUnimplementedAttr { | 1332 | 1332 | pub(crate) struct OnMoveMalformedFormatLiterals { | 1333 | 1333 | pub name: Symbol, | 1334 | 1334 | } | | 1335 | + | | 1336 | +#[derive(Diagnostic)] | | 1337 | +#[diag("invalid macho section specifier")] | | 1338 | +pub(crate) struct InvalidMachoSection { | | 1339 | + #[primary_span] | | 1340 | + #[label("not a valid macho section specifier")] | | 1341 | + pub name_span: Span, | | 1342 | + #[subdiagnostic] | | 1343 | + pub reason: InvalidMachoSectionReason, | | 1344 | +} | | 1345 | + | | 1346 | +#[derive(Subdiagnostic)] | | 1347 | +pub(crate) enum InvalidMachoSectionReason { | | 1348 | + #[note("a macho section specifier requires a segment and a section, separated by a comma")] | | 1349 | + #[help("an example of a valid macho section specifier is `__TEXT,__cstring`")] | | 1350 | + MissingSection, | | 1351 | + #[note("section name `{$section}` is longer than 16 bytes")] | | 1352 | + SectionTooLong { section: String }, | | 1353 | +} |
Collapse file Copy file name to clipboard +54Lines changed: 54 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +//@ add-minicore | | 2 | +//@ compile-flags: --target aarch64-apple-darwin | | 3 | +//@ needs-llvm-components: aarch64 | | 4 | +//@ ignore-backends: gcc | | 5 | +#![feature(no_core, rustc_attrs, lang_items)] | | 6 | +#![no_core] | | 7 | +#![crate_type = "lib"] | | 8 | + | | 9 | +extern crate minicore; | | 10 | +use minicore::*; | | 11 | + | | 12 | +#[unsafe(link_section = "foo")] | | 13 | +//~^ ERROR invalid macho section specifier | | 14 | +#[unsafe(no_mangle)] | | 15 | +fn missing_section() {} | | 16 | + | | 17 | +#[unsafe(link_section = "foo,")] | | 18 | +//~^ ERROR invalid macho section specifier | | 19 | +#[unsafe(no_mangle)] | | 20 | +fn empty_section() {} | | 21 | + | | 22 | +#[unsafe(link_section = "foo, ")] | | 23 | +//~^ ERROR invalid macho section specifier | | 24 | +#[unsafe(no_mangle)] | | 25 | +fn whitespace_section() {} | | 26 | + | | 27 | +#[unsafe(link_section = "foo,somelongwindedthing")] | | 28 | +//~^ ERROR invalid macho section specifier | | 29 | +#[unsafe(no_mangle)] | | 30 | +fn section_too_long() {} | | 31 | + | | 32 | +#[unsafe(link_section = "foo,bar")] | | 33 | +#[unsafe(no_mangle)] | | 34 | +fn segment_and_section() {} | | 35 | + | | 36 | +#[unsafe(link_section = "foo,bar,")] | | 37 | +#[unsafe(no_mangle)] | | 38 | +fn segment_and_section_and_comma() {} | | 39 | + | | 40 | +#[unsafe(link_section = ",foo")] | | 41 | +#[unsafe(no_mangle)] | | 42 | +fn missing_segment_is_fine() {} | | 43 | + | | 44 | +#[unsafe(link_section = "__TEXT,__stubs,symbol_stubs,none,16")] | | 45 | +#[unsafe(no_mangle)] | | 46 | +fn stub_size_decimal() {} | | 47 | + | | 48 | +#[unsafe(link_section = "__TEXT,__stubs,symbol_stubs,none,0x10")] | | 49 | +#[unsafe(no_mangle)] | | 50 | +fn stub_size_hex() {} | | 51 | + | | 52 | +#[unsafe(link_section = "__TEXT,__stubs,symbol_stubs,none,020")] | | 53 | +#[unsafe(no_mangle)] | | 54 | +fn stub_size_oct() {} |
Collapse file Copy file name to clipboard +37Lines changed: 37 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +error: invalid macho section specifier | | 2 | + --> $DIR/link-section-macho.rs:12:25 | | 3 | + | | | 4 | +LL | #[unsafe(link_section = "foo")] | | 5 | + | ^^^^^ not a valid macho section specifier | | 6 | + | | | 7 | + = note: a macho section specifier requires a segment and a section, separated by a comma | | 8 | + = help: an example of a valid macho section specifier is `__TEXT,__cstring` | | 9 | + | | 10 | +error: invalid macho section specifier | | 11 | + --> $DIR/link-section-macho.rs:17:25 | | 12 | + | | | 13 | +LL | #[unsafe(link_section = "foo,")] | | 14 | + | ^^^^^^ not a valid macho section specifier | | 15 | + | | | 16 | + = note: a macho section specifier requires a segment and a section, separated by a comma | | 17 | + = help: an example of a valid macho section specifier is `__TEXT,__cstring` | | 18 | + | | 19 | +error: invalid macho section specifier | | 20 | + --> $DIR/link-section-macho.rs:22:25 | | 21 | + | | | 22 | +LL | #[unsafe(link_section = "foo, ")] | | 23 | + | ^^^^^^^ not a valid macho section specifier | | 24 | + | | | 25 | + = note: a macho section specifier requires a segment and a section, separated by a comma | | 26 | + = help: an example of a valid macho section specifier is `__TEXT,__cstring` | | 27 | + | | 28 | +error: invalid macho section specifier | | 29 | + --> $DIR/link-section-macho.rs:27:25 | | 30 | + | | | 31 | +LL | #[unsafe(link_section = "foo,somelongwindedthing")] | | 32 | + | ^^^^^^^^^^^^^^^^^^^^^^^^^ not a valid macho section specifier | | 33 | + | | | 34 | + = note: section name `somelongwindedthing` is longer than 16 bytes | | 35 | + | | 36 | +error: aborting due to 4 previous errors | | 37 | + |
|