1
0
Fork 0
mirror of https://github.com/hermitcore/libhermit.git synced 2025-03-09 00:00:03 +01:00

add Rust tool to rename sections within a elf file

This commit is contained in:
Stefan Lankes 2017-07-29 10:45:24 +02:00
parent 5c902b4b73
commit 7f169c4e75
3 changed files with 99 additions and 0 deletions

2
objmv/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
target
Cargo.lock

9
objmv/Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "objmv"
version = "0.1.0"
authors = [
"Stefan Lankes <slankes@eonerc.rwth-aachen.de>",
]
[dependencies]
regex = "0.*"

88
objmv/src/main.rs Normal file
View file

@ -0,0 +1,88 @@
// Copyright (c) 2017 Stefan Lankes, RWTH Aachen University
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
extern crate regex;
use regex::RegexSet;
use std::str;
use std::env;
use std::process::Command;
use std::vec::Vec;
fn rename_sections(fname: String)
{
let output = Command::new("objdump")
.arg("-h")
.arg(fname.to_string())
.output()
.expect("objdump failed to start");
if output.status.success() {
let mut args: Vec<String> = Vec::new();
let re = RegexSet::new(&[r"^.text", r"^.data", r"^.bss"]).unwrap();
let output_string = String::from_utf8_lossy(&output.stdout);
let substrings = output_string.split_whitespace();
for old_name in substrings {
if re.is_match(old_name) {
let mut new_name: String = ".k".to_owned();
new_name.push_str(old_name.trim_left_matches('.'));
//println!("{} {}", old_name, new_name);
let mut cmd: String = String::new();
cmd.push_str(old_name);
cmd.push('=');
cmd.push_str(&new_name);
//println!("{}", cmd);
args.push("--rename-section".to_string());
args.push(cmd);
}
}
if args.len() > 0 {
let status = Command::new("objcopy")
.args(args)
.arg(fname.to_string())
.status()
.expect("objcopy failed to start");
if !status.success() {
panic!("unable to rename sections!")
}
}
} else {
panic!("unable to determine section names");
}
}
fn main() {
let mut arguments: Vec<String> = env::args().collect();
// remove unneeded programm name
arguments.remove(0);
for arg in arguments {
rename_sections(arg);
}
}