From 83cb760e2c2b9fa3f0bb90ad2941f4cbceba2255 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Thu, 30 May 2024 16:20:49 -0400 Subject: [PATCH 1/3] run_make_support nm implementation + bin-emit-no-symbols rmake rewrite --- library/backtrace | 2 +- src/doc/book | 2 +- src/doc/edition-guide | 2 +- src/doc/reference | 2 +- src/doc/rust-by-example | 2 +- src/doc/rustc-dev-guide | 2 +- src/tools/cargo | 2 +- src/tools/run-make-support/src/nm/mod.rs | 48 +++++++++++++++++++ .../tidy/src/allowed_run_make_makefiles.txt | 1 - .../app.rs | 0 tests/run-make/bin-emit-no-symbols/rmake.rs | 19 ++++++++ tests/run-make/issue-51671/Makefile | 9 ---- 12 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 src/tools/run-make-support/src/nm/mod.rs rename tests/run-make/{issue-51671 => bin-emit-no-symbols}/app.rs (100%) create mode 100644 tests/run-make/bin-emit-no-symbols/rmake.rs delete mode 100644 tests/run-make/issue-51671/Makefile diff --git a/library/backtrace b/library/backtrace index 72265bea21089..5e05efa87905f 160000 --- a/library/backtrace +++ b/library/backtrace @@ -1 +1 @@ -Subproject commit 72265bea210891ae47bbe6d4f17b493ef0606619 +Subproject commit 5e05efa87905fb5b351a2bc5644d60c57d6d9327 diff --git a/src/doc/book b/src/doc/book index 45c1a6d69edfd..5e9051f71638a 160000 --- a/src/doc/book +++ b/src/doc/book @@ -1 +1 @@ -Subproject commit 45c1a6d69edfd1fc91fb7504cb73958dbd09441e +Subproject commit 5e9051f71638aa941cd5dda465e25c61cde9594f diff --git a/src/doc/edition-guide b/src/doc/edition-guide index cb58c430b4e80..bbaabbe088e21 160000 --- a/src/doc/edition-guide +++ b/src/doc/edition-guide @@ -1 +1 @@ -Subproject commit cb58c430b4e8054c2cb81d2d4434092c482a93d8 +Subproject commit bbaabbe088e21a81a0d9ae6757705020d5d7b416 diff --git a/src/doc/reference b/src/doc/reference index 0b805c6580401..6019b76f5b289 160000 --- a/src/doc/reference +++ b/src/doc/reference @@ -1 +1 @@ -Subproject commit 0b805c65804019b0ac8f2fe3117afad82a6069b8 +Subproject commit 6019b76f5b28938565b251bbba0bf5cc5c43d863 diff --git a/src/doc/rust-by-example b/src/doc/rust-by-example index b1d97bd6113ab..4840dca06cadf 160000 --- a/src/doc/rust-by-example +++ b/src/doc/rust-by-example @@ -1 +1 @@ -Subproject commit b1d97bd6113aba732b2091ce093c76f2d05bb8a0 +Subproject commit 4840dca06cadf48b305d3ce0aeafde7f80933f80 diff --git a/src/doc/rustc-dev-guide b/src/doc/rustc-dev-guide index aec82168dd312..6a7374bd87cba 160000 --- a/src/doc/rustc-dev-guide +++ b/src/doc/rustc-dev-guide @@ -1 +1 @@ -Subproject commit aec82168dd3121289a194b381f56076fc789a4d2 +Subproject commit 6a7374bd87cbac0f8be4fd4877d8186d9c313985 diff --git a/src/tools/cargo b/src/tools/cargo index a1f47ec3f7cd0..431db31d0dbed 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit a1f47ec3f7cd076986f1bfcd7061f2e8cb1a726e +Subproject commit 431db31d0dbeda320caf8ef8535ea48eb3093407 diff --git a/src/tools/run-make-support/src/nm/mod.rs b/src/tools/run-make-support/src/nm/mod.rs new file mode 100644 index 0000000000000..c304877eba191 --- /dev/null +++ b/src/tools/run-make-support/src/nm/mod.rs @@ -0,0 +1,48 @@ +use crate::{fs_wrapper, object}; +use object::{Object, ObjectSection}; +use std::path::Path; + +#[derive(Debug)] +pub struct Nm { + file: Option, +} + +pub fn nm() -> Nm { + Nm::new() +} + +impl Nm { + /// Construct a bare `nm` invocation. + pub fn new() -> Self { + Self { file: None } + } + + /// Specify the file to analyze the symbols of. + pub fn input>(&mut self, path: P) -> &mut Self { + &mut Self { + file: Some( + object::File::parse(fs_wrapper::read(path)) + .expect(format!("Failed to parse ELF file at {:?}", path.as_ref().display())), + ), + } + } + + /// Collect all symbols of an object file into a String. + pub fn collect_symbols(&self) -> String { + let object_file = self.file; + let mut symbols_str = String::new(); + for section in object_file.sections() { + if let Ok(ObjectSection::SymbolTable(st)) = section.parse::() { + for symbol in st.symbols() { + symbols_str.push_str(&format!( + "{:016x} {:?} {}\n", + symbol.address(), + symbol.kind(), + symbol.name() + )); + } + } + } + symbols_str + } +} diff --git a/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt index 1596257747fa9..98438e8004dc1 100644 --- a/src/tools/tidy/src/allowed_run_make_makefiles.txt +++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt @@ -84,7 +84,6 @@ run-make/issue-37839/Makefile run-make/issue-40535/Makefile run-make/issue-47384/Makefile run-make/issue-47551/Makefile -run-make/issue-51671/Makefile run-make/issue-68794-textrel-on-minimal-lib/Makefile run-make/issue-69368/Makefile run-make/issue-83045/Makefile diff --git a/tests/run-make/issue-51671/app.rs b/tests/run-make/bin-emit-no-symbols/app.rs similarity index 100% rename from tests/run-make/issue-51671/app.rs rename to tests/run-make/bin-emit-no-symbols/app.rs diff --git a/tests/run-make/bin-emit-no-symbols/rmake.rs b/tests/run-make/bin-emit-no-symbols/rmake.rs new file mode 100644 index 0000000000000..6d6d6b3296742 --- /dev/null +++ b/tests/run-make/bin-emit-no-symbols/rmake.rs @@ -0,0 +1,19 @@ +// When setting the crate type as a "bin" (in app.rs), +// this could cause a bug where some symbols would not be +// emitted in the object files. This has been fixed, and +// this test checks that the correct symbols have been successfully +// emitted inside the object files. +// See https://github.com/rust-lang/rust/issues/51671 + +use run_make_support::{nm, rustc, tmp_dir}; + +fn main() { + rustc().emit("obj").input("app.rs").run(); + //FIXME(Oneirical): This should eventually be rmake_out_path + let nm = nm(tmp_dir().join("app.o")); + assert!( + nm.contains("rust_begin_unwind") + && nm.contains("rust_eh_personality") + && nm.contains("__rg_oom") + ); +} diff --git a/tests/run-make/issue-51671/Makefile b/tests/run-make/issue-51671/Makefile deleted file mode 100644 index c93645369928c..0000000000000 --- a/tests/run-make/issue-51671/Makefile +++ /dev/null @@ -1,9 +0,0 @@ -include ../tools.mk - -# ignore-windows-msvc - -all: - $(RUSTC) --emit=obj app.rs - nm $(TMPDIR)/app.o | $(CGREP) rust_begin_unwind - nm $(TMPDIR)/app.o | $(CGREP) rust_eh_personality - nm $(TMPDIR)/app.o | $(CGREP) __rg_oom From c1597f90396c0cf38b196b584be49a17e6e881a6 Mon Sep 17 00:00:00 2001 From: Oneirical Date: Wed, 5 Jun 2024 16:29:42 -0400 Subject: [PATCH 2/3] try implementing suggestions --- library/backtrace | 2 +- src/doc/book | 2 +- src/doc/edition-guide | 2 +- src/doc/reference | 2 +- src/doc/rust-by-example | 2 +- src/doc/rustc-dev-guide | 2 +- src/tools/cargo | 2 +- src/tools/run-make-support/src/nm/mod.rs | 8 +++++--- 8 files changed, 12 insertions(+), 10 deletions(-) diff --git a/library/backtrace b/library/backtrace index 5e05efa87905f..72265bea21089 160000 --- a/library/backtrace +++ b/library/backtrace @@ -1 +1 @@ -Subproject commit 5e05efa87905fb5b351a2bc5644d60c57d6d9327 +Subproject commit 72265bea210891ae47bbe6d4f17b493ef0606619 diff --git a/src/doc/book b/src/doc/book index 5e9051f71638a..45c1a6d69edfd 160000 --- a/src/doc/book +++ b/src/doc/book @@ -1 +1 @@ -Subproject commit 5e9051f71638aa941cd5dda465e25c61cde9594f +Subproject commit 45c1a6d69edfd1fc91fb7504cb73958dbd09441e diff --git a/src/doc/edition-guide b/src/doc/edition-guide index bbaabbe088e21..cb58c430b4e80 160000 --- a/src/doc/edition-guide +++ b/src/doc/edition-guide @@ -1 +1 @@ -Subproject commit bbaabbe088e21a81a0d9ae6757705020d5d7b416 +Subproject commit cb58c430b4e8054c2cb81d2d4434092c482a93d8 diff --git a/src/doc/reference b/src/doc/reference index 6019b76f5b289..0b805c6580401 160000 --- a/src/doc/reference +++ b/src/doc/reference @@ -1 +1 @@ -Subproject commit 6019b76f5b28938565b251bbba0bf5cc5c43d863 +Subproject commit 0b805c65804019b0ac8f2fe3117afad82a6069b8 diff --git a/src/doc/rust-by-example b/src/doc/rust-by-example index 4840dca06cadf..b1d97bd6113ab 160000 --- a/src/doc/rust-by-example +++ b/src/doc/rust-by-example @@ -1 +1 @@ -Subproject commit 4840dca06cadf48b305d3ce0aeafde7f80933f80 +Subproject commit b1d97bd6113aba732b2091ce093c76f2d05bb8a0 diff --git a/src/doc/rustc-dev-guide b/src/doc/rustc-dev-guide index 6a7374bd87cba..aec82168dd312 160000 --- a/src/doc/rustc-dev-guide +++ b/src/doc/rustc-dev-guide @@ -1 +1 @@ -Subproject commit 6a7374bd87cbac0f8be4fd4877d8186d9c313985 +Subproject commit aec82168dd3121289a194b381f56076fc789a4d2 diff --git a/src/tools/cargo b/src/tools/cargo index 431db31d0dbed..a1f47ec3f7cd0 160000 --- a/src/tools/cargo +++ b/src/tools/cargo @@ -1 +1 @@ -Subproject commit 431db31d0dbeda320caf8ef8535ea48eb3093407 +Subproject commit a1f47ec3f7cd076986f1bfcd7061f2e8cb1a726e diff --git a/src/tools/run-make-support/src/nm/mod.rs b/src/tools/run-make-support/src/nm/mod.rs index c304877eba191..1f41792921d67 100644 --- a/src/tools/run-make-support/src/nm/mod.rs +++ b/src/tools/run-make-support/src/nm/mod.rs @@ -3,8 +3,8 @@ use object::{Object, ObjectSection}; use std::path::Path; #[derive(Debug)] -pub struct Nm { - file: Option, +pub struct Nm<'a> { + file: Option>, } pub fn nm() -> Nm { @@ -32,7 +32,9 @@ impl Nm { let object_file = self.file; let mut symbols_str = String::new(); for section in object_file.sections() { - if let Ok(ObjectSection::SymbolTable(st)) = section.parse::() { + if let Ok(object::read::elf::SymbolTable(st)) = + section.parse::>() + { for symbol in st.symbols() { symbols_str.push_str(&format!( "{:016x} {:?} {}\n", From 977d3f6f96728b6cd68bb4c36f34fcb659989dcc Mon Sep 17 00:00:00 2001 From: Oneirical Date: Tue, 18 Jun 2024 14:57:00 -0400 Subject: [PATCH 3/3] use llvm_readobj in run-make test instead of nm --- src/tools/run-make-support/src/llvm.rs | 28 +++++++++--- src/tools/run-make-support/src/nm/mod.rs | 50 --------------------- tests/run-make/bin-emit-no-symbols/rmake.rs | 13 +++--- 3 files changed, 28 insertions(+), 63 deletions(-) delete mode 100644 src/tools/run-make-support/src/nm/mod.rs diff --git a/src/tools/run-make-support/src/llvm.rs b/src/tools/run-make-support/src/llvm.rs index fe4131819baec..99bce08fc2387 100644 --- a/src/tools/run-make-support/src/llvm.rs +++ b/src/tools/run-make-support/src/llvm.rs @@ -2,8 +2,8 @@ use std::path::{Path, PathBuf}; use crate::{env_var, Command}; -/// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available -/// at `$LLVM_BIN_DIR/llvm-readobj`. +/// Construct a new `llvm-readobj` invocation with the `GNU` output style. +/// This assumes that `llvm-readobj` is available at `$LLVM_BIN_DIR/llvm-readobj`. #[track_caller] pub fn llvm_readobj() -> LlvmReadobj { LlvmReadobj::new() @@ -70,13 +70,24 @@ pub fn llvm_bin_dir() -> PathBuf { } impl LlvmReadobj { - /// Construct a new `llvm-readobj` invocation. This assumes that `llvm-readobj` is available - /// at `$LLVM_BIN_DIR/llvm-readobj`. + /// Construct a new `llvm-readobj` invocation with the `GNU` output style. + /// This assumes that `llvm-readobj` is available at `$LLVM_BIN_DIR/llvm-readobj`. #[track_caller] pub fn new() -> Self { let llvm_readobj = llvm_bin_dir().join("llvm-readobj"); let cmd = Command::new(llvm_readobj); - Self { cmd } + let mut readobj = Self { cmd }; + readobj.elf_output_style("GNU"); + readobj + } + + /// Specify the format of the ELF information. + /// + /// Valid options are `LLVM` (default), `GNU`, and `JSON`. + pub fn elf_output_style(&mut self, style: &str) -> &mut Self { + self.cmd.arg("--elf-output-style"); + self.cmd.arg(style); + self } /// Provide an input file. @@ -90,6 +101,13 @@ impl LlvmReadobj { self.cmd.arg("--file-header"); self } + + /// Specify the section to display. + pub fn section(&mut self, section: &str) -> &mut Self { + self.cmd.arg("--string-dump"); + self.cmd.arg(section); + self + } } impl LlvmProfdata { diff --git a/src/tools/run-make-support/src/nm/mod.rs b/src/tools/run-make-support/src/nm/mod.rs deleted file mode 100644 index 1f41792921d67..0000000000000 --- a/src/tools/run-make-support/src/nm/mod.rs +++ /dev/null @@ -1,50 +0,0 @@ -use crate::{fs_wrapper, object}; -use object::{Object, ObjectSection}; -use std::path::Path; - -#[derive(Debug)] -pub struct Nm<'a> { - file: Option>, -} - -pub fn nm() -> Nm { - Nm::new() -} - -impl Nm { - /// Construct a bare `nm` invocation. - pub fn new() -> Self { - Self { file: None } - } - - /// Specify the file to analyze the symbols of. - pub fn input>(&mut self, path: P) -> &mut Self { - &mut Self { - file: Some( - object::File::parse(fs_wrapper::read(path)) - .expect(format!("Failed to parse ELF file at {:?}", path.as_ref().display())), - ), - } - } - - /// Collect all symbols of an object file into a String. - pub fn collect_symbols(&self) -> String { - let object_file = self.file; - let mut symbols_str = String::new(); - for section in object_file.sections() { - if let Ok(object::read::elf::SymbolTable(st)) = - section.parse::>() - { - for symbol in st.symbols() { - symbols_str.push_str(&format!( - "{:016x} {:?} {}\n", - symbol.address(), - symbol.kind(), - symbol.name() - )); - } - } - } - symbols_str - } -} diff --git a/tests/run-make/bin-emit-no-symbols/rmake.rs b/tests/run-make/bin-emit-no-symbols/rmake.rs index 6d6d6b3296742..5586e53c05084 100644 --- a/tests/run-make/bin-emit-no-symbols/rmake.rs +++ b/tests/run-make/bin-emit-no-symbols/rmake.rs @@ -5,15 +5,12 @@ // emitted inside the object files. // See https://github.com/rust-lang/rust/issues/51671 -use run_make_support::{nm, rustc, tmp_dir}; +use run_make_support::{llvm_readobj, rustc}; fn main() { rustc().emit("obj").input("app.rs").run(); - //FIXME(Oneirical): This should eventually be rmake_out_path - let nm = nm(tmp_dir().join("app.o")); - assert!( - nm.contains("rust_begin_unwind") - && nm.contains("rust_eh_personality") - && nm.contains("__rg_oom") - ); + let out = llvm_readobj().input("app.o").arg("--symbols").run(); + out.assert_stdout_contains("rust_begin_unwind"); + out.assert_stdout_contains("rust_eh_personality"); + out.assert_stdout_contains("__rg_oom"); }