about summary refs log tree commit diff
path: root/library/compiler-builtins
diff options
context:
space:
mode:
authorTrevor Gross <tmgross@umich.edu>2025-08-07 01:05:01 -0500
committerTrevor Gross <tmgross@umich.edu>2025-08-07 03:32:34 -0500
commitdecdb950bf016391145b582cd0756d3e67481ad6 (patch)
tree51b5a9df6a9ceaa4a8dd6958f97daa6ffd7c8d8a /library/compiler-builtins
parentfbc700f92bdb008a9fd76e2a02230cea6c23d2c4 (diff)
downloadrust-decdb950bf016391145b582cd0756d3e67481ad6.tar.gz
rust-decdb950bf016391145b582cd0756d3e67481ad6.zip
symcheck: Store the section name in `SymInfo` if available
Currently `SymInfo` stores a `Section`, which is just an index:

    SymInfo {
        section: Section(
            SectionIndex(
                539,
            ),
        ),
        ...
    },

Look up and store the section name instead if possible, with a fallback
to the `Section` debug printing. This makes output more clear and will
allow us to filter by section name.
Diffstat (limited to 'library/compiler-builtins')
-rw-r--r--library/compiler-builtins/crates/symbol-check/src/main.rs30
1 files changed, 20 insertions, 10 deletions
diff --git a/library/compiler-builtins/crates/symbol-check/src/main.rs b/library/compiler-builtins/crates/symbol-check/src/main.rs
index 1312a717970..beb568a0f84 100644
--- a/library/compiler-builtins/crates/symbol-check/src/main.rs
+++ b/library/compiler-builtins/crates/symbol-check/src/main.rs
@@ -9,7 +9,7 @@ use std::process::{Command, Stdio};
 
 use object::read::archive::{ArchiveFile, ArchiveMember};
 use object::{
-    File as ObjFile, Object, ObjectSymbol, Symbol, SymbolKind, SymbolScope, SymbolSection,
+    File as ObjFile, Object, ObjectSection, ObjectSymbol, Symbol, SymbolKind, SymbolScope,
 };
 use serde_json::Value;
 
@@ -154,7 +154,7 @@ struct SymInfo {
     name: String,
     kind: SymbolKind,
     scope: SymbolScope,
-    section: SymbolSection,
+    section: String,
     is_undefined: bool,
     is_global: bool,
     is_local: bool,
@@ -165,12 +165,22 @@ struct SymInfo {
 }
 
 impl SymInfo {
-    fn new(sym: &Symbol, member: &ArchiveMember) -> Self {
+    fn new(sym: &Symbol, obj: &ObjFile, member: &ArchiveMember) -> Self {
+        // Include the section name if possible. Fall back to the `Section` debug impl if not.
+        let section = sym.section();
+        let section_name = sym
+            .section()
+            .index()
+            .and_then(|idx| obj.section_by_index(idx).ok())
+            .and_then(|sec| sec.name().ok())
+            .map(ToString::to_string)
+            .unwrap_or_else(|| format!("{section:?}"));
+
         Self {
             name: sym.name().expect("missing name").to_owned(),
             kind: sym.kind(),
             scope: sym.scope(),
-            section: sym.section(),
+            section: section_name,
             is_undefined: sym.is_undefined(),
             is_global: sym.is_global(),
             is_local: sym.is_local(),
@@ -192,13 +202,13 @@ fn verify_no_duplicates(archive: &Archive) {
     let mut dups = Vec::new();
     let mut found_any = false;
 
-    archive.for_each_symbol(|symbol, member| {
+    archive.for_each_symbol(|symbol, obj, member| {
         // Only check defined globals
         if !symbol.is_global() || symbol.is_undefined() {
             return;
         }
 
-        let sym = SymInfo::new(&symbol, member);
+        let sym = SymInfo::new(&symbol, obj, member);
 
         // x86-32 includes multiple copies of thunk symbols
         if sym.name.starts_with("__x86.get_pc_thunk") {
@@ -244,7 +254,7 @@ fn verify_core_symbols(archive: &Archive) {
     let mut undefined = Vec::new();
     let mut has_symbols = false;
 
-    archive.for_each_symbol(|symbol, member| {
+    archive.for_each_symbol(|symbol, obj, member| {
         has_symbols = true;
 
         // Find only symbols from `core`
@@ -252,7 +262,7 @@ fn verify_core_symbols(archive: &Archive) {
             return;
         }
 
-        let sym = SymInfo::new(&symbol, member);
+        let sym = SymInfo::new(&symbol, obj, member);
         if sym.is_undefined {
             undefined.push(sym);
         } else {
@@ -304,9 +314,9 @@ impl Archive {
     }
 
     /// For a given archive, do something with each symbol.
-    fn for_each_symbol(&self, mut f: impl FnMut(Symbol, &ArchiveMember)) {
+    fn for_each_symbol(&self, mut f: impl FnMut(Symbol, &ObjFile, &ArchiveMember)) {
         self.for_each_object(|obj, member| {
-            obj.symbols().for_each(|sym| f(sym, member));
+            obj.symbols().for_each(|sym| f(sym, &obj, member));
         });
     }
 }