-
Notifications
You must be signed in to change notification settings - Fork 155
/
main.rs
378 lines (365 loc) · 14.7 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use std::fs;
use std::io::{self, BufRead};
use std::path::{Path, PathBuf};
use anyhow::{anyhow, Context, Result};
use clap::{command, Arg, ArgAction, ArgGroup};
use log::info;
use object_rewrite as rewrite;
fn main() -> Result<()> {
let matches = command!()
.max_term_width(100)
.args(&[
Arg::new("input")
.required(true)
.value_parser(clap::value_parser!(PathBuf))
.help("The input file"),
// TODO: make output optional, and overwrite input file by default
Arg::new("output")
.required(false)
.value_parser(clap::value_parser!(PathBuf))
.help("The output file. Required if any modification is requested"),
Arg::new("delete-symbol")
.long("delete-symbol")
.value_name("symbol")
.value_parser(clap::value_parser!(String))
.action(ArgAction::Append)
.help("Delete the named symbol"),
Arg::new("rename-symbol")
.long("rename-symbol")
.value_name("old=new")
.value_parser(clap::value_parser!(String))
.action(ArgAction::Append)
.help("Change the name of a symbol from <old> to <new>"),
Arg::new("rename-symbols")
.long("rename-symbols")
.value_name("file")
.value_parser(clap::value_parser!(PathBuf))
.action(ArgAction::Append)
.help(
"Read a list of symbol names from <file> and apply --rename-symbol for each. \
Each line contains two symbols separated by whitespace.",
),
Arg::new("delete-section")
.long("delete-section")
.value_name("section")
.value_parser(clap::value_parser!(String))
.action(ArgAction::Append)
.help("Delete the named section"),
Arg::new("rename-section")
.long("rename-section")
.value_name("old=new")
.value_parser(clap::value_parser!(String))
.action(ArgAction::Append)
.help("Change the name of a section from <old> to <new>"),
Arg::new("elf-add-dynamic-debug")
.long("elf-add-dynamic-debug")
.action(ArgAction::SetTrue)
.help("Add a DT_DEBUG entry to the dynamic section"),
Arg::new("elf-print-runpath")
.long("elf-print-runpath")
.action(ArgAction::SetTrue)
.help("Print any DT_RPATH or DT_RUNPATH entries in the dynamic section"),
Arg::new("elf-delete-runpath")
.long("elf-delete-runpath")
.action(ArgAction::SetTrue)
.help("Delete any DT_RPATH and DT_RUNPATH entries in the dynamic section"),
Arg::new("elf-set-runpath")
.long("elf-set-runpath")
.value_name("path")
.value_parser(clap::value_parser!(String))
.help("Set the path for any DT_RPATH or DT_RUNPATH entry in the dynamic section"),
Arg::new("elf-add-runpath")
.long("elf-add-runpath")
.value_name("path")
.value_parser(clap::value_parser!(String))
.action(ArgAction::Append)
.help("Add a path to any DT_RPATH or DT_RUNPATH entry in the dynamic section"),
Arg::new("elf-use-rpath")
.long("elf-use-rpath")
.action(ArgAction::SetTrue)
.help("Change any DT_RUNPATH entry in the dynamic section to DT_RPATH"),
Arg::new("elf-use-runpath")
.long("elf-use-runpath")
.action(ArgAction::SetTrue)
.help("Change any DT_RPATH entry in the dynamic section to DT_RUNPATH"),
Arg::new("elf-print-needed")
.long("elf-print-needed")
.action(ArgAction::SetTrue)
.help("Print the DT_NEEDED entries in the dynamic section"),
Arg::new("elf-delete-needed")
.long("elf-delete-needed")
.value_name("name")
.value_parser(clap::value_parser!(String))
.action(ArgAction::Append)
.help("Delete the named DT_NEEDED entry in the dynamic section"),
Arg::new("elf-replace-needed")
.long("elf-replace-needed")
.value_name("old=new")
.value_parser(clap::value_parser!(String))
.action(ArgAction::Append)
.help("Change the value of a DT_NEEDED entry from <old> to <new>"),
Arg::new("elf-add-needed")
.long("elf-add-needed")
.value_name("name")
.value_parser(clap::value_parser!(String))
.action(ArgAction::Append)
.help("Add a DT_NEEDED entry to the start of the dynamic section"),
Arg::new("elf-print-soname")
.long("elf-print-soname")
.action(ArgAction::SetTrue)
.help("Print the DT_SONAME entry in the dynamic section"),
Arg::new("elf-set-soname")
.long("elf-set-soname")
.value_name("name")
.value_parser(clap::value_parser!(String))
.help("Set the DT_SONAME entry in the dynamic section"),
Arg::new("elf-print-interpreter")
.long("elf-print-interpreter")
.action(ArgAction::SetTrue)
.help("Print the interpreter path in the PT_INTERP segment"),
Arg::new("elf-set-interpreter")
.long("elf-set-interpreter")
.value_name("path")
.value_parser(clap::value_parser!(String))
.help("Set the interpreter path in the PT_INTERP segment"),
Arg::new("ignore-unknown-format")
.long("ignore-unknown-format")
.action(ArgAction::SetTrue)
.help("Ignore input files with unknown formats"),
Arg::new("verbose")
.short('v')
.long("verbose")
.action(ArgAction::SetTrue)
.help("Enable verbose output"),
])
.group(
ArgGroup::new("output-flags")
.args([
"delete-symbol",
"rename-symbol",
"rename-symbols",
"delete-section",
"rename-section",
"elf-add-dynamic-debug",
"elf-delete-runpath",
"elf-set-runpath",
"elf-add-runpath",
"elf-use-rpath",
"elf-use-runpath",
"elf-delete-needed",
"elf-replace-needed",
"elf-add-needed",
"elf-set-soname",
"elf-set-interpreter",
])
.multiple(true)
.required(false)
.requires("output"),
)
.get_matches();
if matches.get_flag("verbose") {
env_logger::builder()
.format_level(false)
.format_target(false)
.filter_module("object_rewrite", log::LevelFilter::Debug)
.init();
}
// TODO: allow - for stdin
let in_path = matches.get_one::<PathBuf>("input").unwrap();
let in_file = fs::File::open(in_path)
.with_context(|| format!("Failed to open input file '{}'", in_path.display()))?;
let in_data = unsafe { memmap2::Mmap::map(&in_file) }
.with_context(|| format!("Failed to map input file '{}'", in_path.display()))?;
let in_data = &*in_data;
if matches.get_flag("ignore-unknown-format") {
match object::FileKind::parse(in_data) {
Ok(object::FileKind::Elf32) | Ok(object::FileKind::Elf64) => {}
_ => {
info!(
"Ignoring input file '{}' with unknown format",
in_path.display()
);
return Ok(());
}
}
}
let mut rewriter = rewrite::Rewriter::read(in_data)
.with_context(|| format!("Failed to parse input file '{}'", in_path.display()))?;
if matches.get_flag("elf-print-runpath") {
if let Some(runpath) = rewriter.elf_runpath() {
println!("{}", String::from_utf8_lossy(runpath));
}
}
if matches.get_flag("elf-print-needed") {
for needed in rewriter.elf_needed() {
println!("{}", String::from_utf8_lossy(needed));
}
}
if matches.get_flag("elf-print-soname") {
if let Some(soname) = rewriter.elf_soname() {
println!("{}", String::from_utf8_lossy(soname));
}
}
if matches.get_flag("elf-print-interpreter") {
if let Some(interp) = rewriter.elf_interpreter() {
println!("{}", String::from_utf8_lossy(interp));
}
}
// TODO: allow replacing input file
let Some(out_path) = matches.get_one::<PathBuf>("output") else {
return Ok(());
};
let mut options = rewrite::Options::default();
options.delete_symbols = matches
.get_many::<String>("delete-symbol")
.unwrap_or_default()
.map(|arg| arg.clone().into_bytes())
.collect();
for arg in matches
.get_many::<String>("rename-symbol")
.unwrap_or_default()
{
let names: Vec<&[u8]> = arg.as_bytes().splitn(2, |byte| *byte == b'=').collect();
if names.len() != 2 {
return Err(
anyhow!("Invalid rename symbol: `{}`. --rename-symbol expects argument of the form: <old>=<new>", arg)
);
}
options
.rename_symbols
.insert(names[0].to_vec(), names[1].to_vec());
}
for filename in matches
.get_many::<PathBuf>("rename-symbols")
.unwrap_or_default()
{
let file = fs::File::open(filename).with_context(|| {
format!("Failed to open rename symbol file '{}'", filename.display())
})?;
let mut buf = io::BufReader::new(file);
let mut line = Vec::new();
while buf.read_until(b'\n', &mut line)? != 0 {
if line.ends_with(b"\n") {
line.pop();
if line.ends_with(b"\r") {
line.pop();
}
}
let names: Vec<&[u8]> = line.splitn(2, |byte| *byte == b' ').collect();
if names.len() != 2 {
return Err(
anyhow!(
"Invalid rename symbol file entry: `{}`. --rename-symbols expects lines of the form: <old> <new>", String::from_utf8_lossy(&line))
);
}
options
.rename_symbols
.insert(names[0].to_vec(), names[1].to_vec());
line.clear();
}
}
options.delete_sections = matches
.get_many::<String>("delete-section")
.unwrap_or_default()
.map(|arg| arg.clone().into_bytes())
.collect();
for arg in matches
.get_many::<String>("rename-section")
.unwrap_or_default()
{
let names: Vec<&[u8]> = arg.as_bytes().splitn(2, |byte| *byte == b'=').collect();
if names.len() != 2 {
return Err(
anyhow!(
"Invalid rename section: `{}`. --rename-section expects argument of the form: <old>=<new>", arg)
);
}
options
.rename_sections
.insert(names[0].to_vec(), names[1].to_vec());
}
options.elf.add_dynamic_debug = matches.get_flag("elf-add-dynamic-debug");
options.elf.delete_runpath = matches.get_flag("elf-delete-runpath");
options.elf.set_runpath = matches
.get_one::<String>("elf-set-runpath")
.map(|arg| arg.clone().into_bytes());
options.elf.add_runpath = matches
.get_many::<String>("elf-add-runpath")
.unwrap_or_default()
.map(|arg| arg.clone().into_bytes())
.collect();
options.elf.use_rpath = matches.get_flag("elf-use-rpath");
options.elf.use_runpath = matches.get_flag("elf-use-runpath");
options.elf.delete_needed = matches
.get_many::<String>("elf-delete-needed")
.unwrap_or_default()
.map(|arg| arg.clone().into_bytes())
.collect();
for arg in matches
.get_many::<String>("elf-replace-needed")
.unwrap_or_default()
{
let names: Vec<&[u8]> = arg.as_bytes().splitn(2, |byte| *byte == b'=').collect();
if names.len() != 2 {
return Err(
anyhow!(
"Invalid replace needed: `{}`. --elf-replace-needed expects argument of the form: <old>=<new>", arg)
);
}
options
.elf
.replace_needed
.insert(names[0].to_vec(), names[1].to_vec());
}
options.elf.add_needed = matches
.get_many::<String>("elf-add-needed")
.unwrap_or_default()
.map(|arg| arg.clone().into_bytes())
.collect();
options.elf.set_soname = matches
.get_one::<String>("elf-set-soname")
.map(|arg| arg.clone().into_bytes());
options.elf.set_interpreter = matches
.get_one::<String>("elf-set-interpreter")
.map(|arg| arg.clone().into_bytes());
rewriter.modify(options)?;
if out_path == Path::new("-") {
rewriter
.write(io::stdout().lock())
.with_context(|| "Failed to write output to stdout")?;
return Ok(());
}
let mut open_options = fs::OpenOptions::new();
open_options.write(true).create(true).truncate(true);
#[cfg(unix)]
{
use std::os::unix::fs::MetadataExt;
use std::os::unix::fs::OpenOptionsExt;
let in_metadata = in_file.metadata().with_context(|| {
format!(
"Failed to read metadata of input file '{}'",
in_path.display()
)
})?;
open_options.mode(in_metadata.mode());
}
let out_file = open_options
.open(out_path)
.with_context(|| format!("Failed to create output file '{}'", out_path.display()))?;
let out_metadata = out_file.metadata();
rewriter.write(out_file).with_context(|| {
if let Ok(out_metadata) = out_metadata {
if out_metadata.is_file() {
// This is a regular file that we either created or truncated,
// so we can safely remove it.
fs::remove_file(out_path).ok();
}
}
format!(
"Failed to write output file '{}' from input '{}'",
out_path.display(),
in_path.display()
)
})?;
Ok(())
}