Ver Fonte

Merge pull request #6 from clairechingching/master

fix: clap parse error/preventive code for relocation with non-rodata
Claire Fan há 1 mês atrás
pai
commit
74e773a58f
3 ficheiros alterados com 12 adições e 52 exclusões
  1. 0 48
      src/bin/sbpf-link.rs
  2. 8 4
      src/bin/sbpf-linker.rs
  3. 4 0
      src/byteparser.rs

+ 0 - 48
src/bin/sbpf-link.rs

@@ -1,48 +0,0 @@
-use clap::Parser;
-use sbpf_linker::{SbpfLinkerError, link_program};
-use std::fs;
-use std::path::{Path, PathBuf};
-
-#[derive(Debug, Parser)]
-#[command(
-    name = "sbpf-link",
-    version,
-    about = "Simple SBPF linker that processes object files directly"
-)]
-struct Args {
-    /// Input object file to link
-    #[clap(value_name = "INPUT")]
-    input: PathBuf,
-}
-
-fn main() -> Result<(), SbpfLinkerError> {
-    let args = Args::parse();
-
-    // Link the object file
-    println!("Linking: {}", args.input.display());
-    let linked_bytecode = link_object_file(&args.input)?;
-
-    // Determine output path in same directory with .so extension
-    let parent = args.input.parent().unwrap_or_else(|| Path::new("."));
-    let stem =
-        args.input.file_stem().and_then(|s| s.to_str()).unwrap_or("output");
-    let output = parent.join(format!("{stem}.so"));
-
-    // Write the output
-    println!("Writing output to: {}", output.display());
-    std::fs::write(&output, &linked_bytecode)?;
-
-    println!("Successfully linked {} bytes", linked_bytecode.len());
-    Ok(())
-}
-
-/// Links an object file by reading it from the given path and processing its bytecode
-fn link_object_file<P: AsRef<Path>>(
-    path: P,
-) -> Result<Vec<u8>, SbpfLinkerError> {
-    // Read the object file into a byte array
-    let bytes = fs::read(path.as_ref())?;
-
-    // Call link_program on the bytes
-    link_program(&bytes)
-}

+ 8 - 4
src/bin/sbpf-linker.rs

@@ -16,10 +16,10 @@ enum CliError {
         "optimization level needs to be between 0-3, s or z (instead was `{0}`)"
     )]
     InvalidOptimization(String),
+    #[error("Clap Parse Error")]
+    ClapParseError,
     #[error("SBPF Linker Error. Error detail: ({0}).")]
     SbpfLinkerError(#[from] SbpfLinkerError),
-    #[error("Clap Error. Error detail: ({0}).")]
-    ClapError(#[from] clap::error::Error),
     #[error("Program Read Error. Error detail: ({msg}).")]
     ProgramReadError { msg: String },
     #[error("Program Write Error. Error detail: ({msg}).")]
@@ -55,7 +55,7 @@ struct CommandLine {
     cpu: Cpu,
 
     /// Write output to <output>
-    #[clap(short, long)]
+    #[clap(short, long, required = true)]
     output: PathBuf,
 
     /// Emit BTF information
@@ -152,7 +152,11 @@ fn main() -> Result<(), CliError> {
                 print!("{err}");
                 return Ok(());
             }
-            _ => return Err(err.into()),
+            _ => {
+                // Let Clap handle its own error display for better formatting
+                eprintln!("{err}");
+                return Err(CliError::ClapParseError);
+            }
         },
     };
 

+ 4 - 0
src/byteparser.rs

@@ -113,6 +113,10 @@ pub fn parse_bytecode(bytes: &[u8]) -> Result<ParseResult, SbpfLinkerError> {
                             Token::Identifier(ro_label_name, 0..1);
                     }
                 }
+            } else {
+                if section.relocations().count() > 0 {
+                    assert!(false, "Relocations found but no .rodata section");
+                }
             }
             ast.set_text_size(section.size());
         }