-
Notifications
You must be signed in to change notification settings - Fork 155
/
elf.rs
915 lines (860 loc) · 31.1 KB
/
elf.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
use std::collections::{HashMap, HashSet};
#[cfg(feature = "logging")]
use log::info;
use object::{build, elf};
use super::{Error, Result, Rewriter};
/// Options for modifying an ELF file.
///
/// This struct contains options for modifying an ELF file. It is
/// contained in the [`Options`](super::Options) struct.
///
/// Options are listed in the order they are processed.
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct ElfOptions {
/// Add a `DT_DEBUG` entry to the dynamic section.
///
/// See [`Rewriter::elf_add_dynamic_debug`].
pub add_dynamic_debug: bool,
/// Delete any `DT_RUNPATH` and `DT_RPATH` entries in the dynamic section.
///
/// See [`Rewriter::elf_delete_runpath`].
pub delete_runpath: bool,
/// Set the path for any `DT_RUNPATH` or `DT_RPATH` entry in the dynamic section.
///
/// See [`Rewriter::elf_set_runpath`].
pub set_runpath: Option<Vec<u8>>,
/// Add additional paths to any `DT_RUNPATH` or `DT_RPATH` entry in the dynamic section.
///
/// See [`Rewriter::elf_add_runpath`].
pub add_runpath: Vec<Vec<u8>>,
/// Change any `DT_RPATH` entry in the dynamic section to `DT_RUNPATH`.
///
/// See [`Rewriter::elf_use_runpath`].
pub use_runpath: bool,
/// Change any `DT_RUNPATH` entry in the dynamic section to `DT_RPATH`.
///
/// See [`Rewriter::elf_use_rpath`].
pub use_rpath: bool,
/// Delete `DT_NEEDED` entries from the dynamic section.
///
/// See [`Rewriter::elf_delete_needed`].
pub delete_needed: HashSet<Vec<u8>>,
/// Replace `DT_NEEDED` entries in the dynamic section.
///
/// See [`Rewriter::elf_replace_needed`].
pub replace_needed: HashMap<Vec<u8>, Vec<u8>>,
/// Add `DT_NEEDED` entries to the start of the dynamic section.
///
/// See [`Rewriter::elf_add_needed`].
pub add_needed: Vec<Vec<u8>>,
/// Set the `DT_SONAME` entry in the dynamic section.
///
/// See [`Rewriter::elf_set_soname`].
pub set_soname: Option<Vec<u8>>,
/// Set the interpreter path in the `PT_INTERP` segment.
///
/// See [`Rewriter::elf_set_interpreter`].
pub set_interpreter: Option<Vec<u8>>,
}
impl Rewriter<'_> {
/// Delete symbols from the symbol table.
pub fn elf_delete_symbols(&mut self, names: &HashSet<Vec<u8>>) {
for symbol in &mut self.builder.dynamic_symbols {
if names.contains(&*symbol.name) {
#[cfg(feature = "logging")]
info!("Deleting symbol {}", symbol.name);
symbol.delete = true;
self.modified = true;
}
}
}
/// Delete symbols from the dynamic symbol table.
pub fn elf_delete_dynamic_symbols(&mut self, names: &HashSet<Vec<u8>>) {
for symbol in &mut self.builder.symbols {
if names.contains(&*symbol.name) {
#[cfg(feature = "logging")]
info!("Deleting dynamic symbol {}", symbol.name);
symbol.delete = true;
self.modified = true;
}
}
}
/// Rename symbols in the symbol table.
///
/// The `names` map is from old names to new names.
pub fn elf_rename_symbols(&mut self, names: &HashMap<Vec<u8>, Vec<u8>>) {
for symbol in &mut self.builder.dynamic_symbols {
if let Some(name) = names.get(&*symbol.name) {
let name = name.clone().into();
#[cfg(feature = "logging")]
info!("Renaming symbol {} to {}", symbol.name, name);
symbol.name = name;
self.modified = true;
}
}
}
/// Rename symbols in the dynamic symbol table.
///
/// The `names` map is from old names to new names.
pub fn elf_rename_dynamic_symbols(&mut self, names: &HashMap<Vec<u8>, Vec<u8>>) {
for symbol in &mut self.builder.dynamic_symbols {
if let Some(name) = names.get(&*symbol.name) {
let name = name.clone().into();
#[cfg(feature = "logging")]
info!("Renaming dynamic symbol {} to {}", symbol.name, name);
symbol.name = name;
self.modified = true;
}
}
}
pub(crate) fn elf_delete_sections(&mut self, names: &HashSet<Vec<u8>>) {
for section in &mut self.builder.sections {
if names.contains(&*section.name) {
#[cfg(feature = "logging")]
info!("Deleting section {}", section.name);
// Associated program header will be deleted by delete_orphan_segments.
section.delete = true;
self.modified = true;
}
}
}
pub(crate) fn elf_rename_sections(&mut self, names: &HashMap<Vec<u8>, Vec<u8>>) {
for section in &mut self.builder.sections {
if let Some(name) = names.get(&*section.name) {
let name = name.clone().into();
#[cfg(feature = "logging")]
info!("Renaming section {} to {}", section.name, name);
section.name = name;
self.modified = true;
}
}
}
pub(crate) fn elf_modify(&mut self, options: ElfOptions) -> Result<()> {
if options.add_dynamic_debug {
self.elf_add_dynamic_debug()?;
}
if options.delete_runpath {
self.elf_delete_runpath()?;
}
if let Some(path) = options.set_runpath {
self.elf_set_runpath(path)?;
}
if !options.add_runpath.is_empty() {
self.elf_add_runpath(&options.add_runpath)?;
}
if options.use_runpath {
self.elf_use_runpath()?;
}
if options.use_rpath {
self.elf_use_rpath()?;
}
if !options.delete_needed.is_empty() {
self.elf_delete_needed(&options.delete_needed)?;
}
if !options.replace_needed.is_empty() {
self.elf_replace_needed(&options.replace_needed)?;
}
if !options.add_needed.is_empty() {
self.elf_add_needed(&options.add_needed)?;
}
if let Some(name) = options.set_soname {
self.elf_set_soname(name)?;
}
if let Some(interpreter) = options.set_interpreter {
self.elf_set_interpreter(interpreter)?;
}
Ok(())
}
/// Add a `DT_DEBUG` entry to the dynamic section.
pub fn elf_add_dynamic_debug(&mut self) -> Result<()> {
let dynamic = self
.builder
.dynamic_data_mut()
.ok_or_else(|| Error::modify("No dynamic section found; can't add debug entry"))?;
if dynamic.iter().any(|entry| entry.tag() == elf::DT_DEBUG) {
return Ok(());
}
#[cfg(feature = "logging")]
info!("Adding DT_DEBUG entry");
dynamic.push(build::elf::Dynamic::Integer {
tag: elf::DT_DEBUG,
val: 0,
});
self.modified = true;
Ok(())
}
/// Find the first `DT_RUNPATH` or `DT_RPATH` entry in the dynamic section.
pub fn elf_runpath(&self) -> Option<&[u8]> {
let dynamic = self.builder.dynamic_data()?;
for entry in dynamic.iter() {
let build::elf::Dynamic::String { tag, val } = entry else {
continue;
};
if *tag != elf::DT_RPATH && *tag != elf::DT_RUNPATH {
continue;
}
return Some(val);
}
None
}
/// Delete any `DT_RUNPATH` or `DT_RPATH` entries in the dynamic section.
pub fn elf_delete_runpath(&mut self) -> Result<()> {
let dynamic = self
.builder
.dynamic_data_mut()
.ok_or_else(|| Error::modify("No dynamic section found; can't delete runpath"))?;
let mut modified = false;
dynamic.retain(|entry| {
let tag = entry.tag();
if tag != elf::DT_RPATH && tag != elf::DT_RUNPATH {
return true;
}
#[cfg(feature = "logging")]
info!(
"Deleting {} entry",
if tag == elf::DT_RPATH {
"DT_RPATH"
} else {
"DT_RUNPATH"
}
);
modified = true;
false
});
if modified {
self.modified = true;
}
Ok(())
}
/// Set the path for any `DT_RUNPATH` or `DT_RPATH` entry in the dynamic section.
pub fn elf_set_runpath(&mut self, runpath: Vec<u8>) -> Result<()> {
let dynamic = self
.builder
.dynamic_data_mut()
.ok_or_else(|| Error::modify("No dynamic section found; can't set runpath"))?;
let mut found = false;
for entry in dynamic.iter_mut() {
let build::elf::Dynamic::String { tag, val } = entry else {
continue;
};
if *tag != elf::DT_RPATH && *tag != elf::DT_RUNPATH {
continue;
}
*val = build::ByteString::from(runpath.clone());
#[cfg(feature = "logging")]
info!(
"Setting {} entry to {}",
if *tag == elf::DT_RPATH {
"DT_RPATH"
} else {
"DT_RUNPATH"
},
*val
);
found = true;
}
if !found {
let val = build::ByteString::from(runpath);
#[cfg(feature = "logging")]
info!("Adding DT_RUNPATH entry {}", val);
dynamic.push(build::elf::Dynamic::String {
tag: elf::DT_RUNPATH,
val,
});
}
self.modified = true;
Ok(())
}
/// Add additional paths to any `DT_RUNPATH` or `DT_RPATH` entry in the dynamic section.
pub fn elf_add_runpath(&mut self, runpaths: &[Vec<u8>]) -> Result<()> {
let dynamic = self
.builder
.dynamic_data_mut()
.ok_or_else(|| Error::modify("No dynamic section found; can't add runpath"))?;
let mut found = false;
for entry in dynamic.iter_mut() {
let build::elf::Dynamic::String { tag, val } = entry else {
continue;
};
if *tag != elf::DT_RPATH && *tag != elf::DT_RUNPATH {
continue;
}
for path in runpaths {
if !val.is_empty() {
val.to_mut().push(b':');
}
val.to_mut().extend_from_slice(path);
}
#[cfg(feature = "logging")]
info!(
"Changing {} entry to {}",
if *tag == elf::DT_RPATH {
"DT_RPATH"
} else {
"DT_RUNPATH"
},
val
);
found = true;
}
if !found {
let val = runpaths.join(&[b':'][..]).into();
#[cfg(feature = "logging")]
info!("Adding DT_RUNPATH entry {}", val);
dynamic.push(build::elf::Dynamic::String {
tag: elf::DT_RUNPATH,
val,
});
}
self.modified = true;
Ok(())
}
/// Change any `DT_RPATH` entry in the dynamic section to `DT_RUNPATH`.
pub fn elf_use_runpath(&mut self) -> Result<()> {
let dynamic = self
.builder
.dynamic_data_mut()
.ok_or_else(|| Error::modify("No dynamic section found; can't change runpath"))?;
for entry in dynamic.iter_mut() {
let build::elf::Dynamic::String { tag, .. } = entry else {
continue;
};
if *tag != elf::DT_RPATH {
continue;
}
#[cfg(feature = "logging")]
info!("Changing DT_RPATH to DT_RUNPATH");
*tag = elf::DT_RUNPATH;
self.modified = true;
}
Ok(())
}
/// Change any `DT_RUNPATH` entry in the dynamic section to `DT_RPATH`.
pub fn elf_use_rpath(&mut self) -> Result<()> {
let dynamic = self
.builder
.dynamic_data_mut()
.ok_or_else(|| Error::modify("No dynamic section found; can't change rpath"))?;
for entry in dynamic.iter_mut() {
let build::elf::Dynamic::String { tag, .. } = entry else {
continue;
};
if *tag != elf::DT_RUNPATH {
continue;
}
#[cfg(feature = "logging")]
info!("Changing DT_RUNPATH to DT_RPATH");
*tag = elf::DT_RPATH;
self.modified = true;
}
Ok(())
}
/// Find the `DT_NEEDED` entries in the dynamic section.
pub fn elf_needed(&self) -> impl Iterator<Item = &[u8]> {
let dynamic = self.builder.dynamic_data().unwrap_or(&[]);
dynamic.iter().filter_map(|entry| {
if let build::elf::Dynamic::String { tag, val } = entry {
if *tag == elf::DT_NEEDED {
return Some(val.as_slice());
}
}
None
})
}
/// Delete `DT_NEEDED` entries from the dynamic section.
pub fn elf_delete_needed(&mut self, names: &HashSet<Vec<u8>>) -> Result<()> {
let dynamic = self.builder.dynamic_data_mut().ok_or_else(|| {
Error::modify("No dynamic section found; can't delete needed library")
})?;
let mut modified = false;
dynamic.retain(|entry| {
let build::elf::Dynamic::String { tag, val } = entry else {
return true;
};
if *tag != elf::DT_NEEDED || !names.contains(val.as_slice()) {
return true;
}
#[cfg(feature = "logging")]
info!("Deleting DT_NEEDED entry {}", val);
modified = true;
false
});
if modified {
self.modified = true;
}
Ok(())
}
/// Replace `DT_NEEDED` entries in the dynamic section.
pub fn elf_replace_needed(&mut self, names: &HashMap<Vec<u8>, Vec<u8>>) -> Result<()> {
let dynamic = self.builder.dynamic_data_mut().ok_or_else(|| {
Error::modify("No dynamic section found; can't replace needed library")
})?;
for entry in dynamic.iter_mut() {
let build::elf::Dynamic::String { tag, val } = entry else {
continue;
};
if *tag != elf::DT_NEEDED {
continue;
}
let Some(name) = names.get(val.as_slice()) else {
continue;
};
let name = name.clone().into();
#[cfg(feature = "logging")]
info!("Replacing DT_NEEDED entry {} with {}", val, name);
*val = name;
self.modified = true;
}
Ok(())
}
/// Add `DT_NEEDED` entries to the start of the dynamic section.
///
/// This does not add a `DT_NEEDED` entry if the library is already listed.
pub fn elf_add_needed(&mut self, names: &[Vec<u8>]) -> Result<()> {
let dynamic = self
.builder
.dynamic_data_mut()
.ok_or_else(|| Error::modify("No dynamic section found; can't add needed library"))?;
let mut found = HashSet::new();
for entry in dynamic.iter() {
let build::elf::Dynamic::String { tag, val } = entry else {
continue;
};
if *tag != elf::DT_NEEDED {
continue;
}
found.insert(val.clone());
}
for name in names
.iter()
.rev()
.filter(|name| !found.contains(name.as_slice()))
{
let val = name.clone().into();
#[cfg(feature = "logging")]
info!("Adding DT_NEEDED entry {}", val);
dynamic.insert(
0,
build::elf::Dynamic::String {
tag: elf::DT_NEEDED,
val,
},
);
self.modified = true;
}
Ok(())
}
/// Find the `DT_SONAME` entry in the dynamic section.
pub fn elf_soname(&self) -> Option<&[u8]> {
let id = self.builder.dynamic_section()?;
let section = self.builder.sections.get(id);
let build::elf::SectionData::Dynamic(dynamic) = §ion.data else {
return None;
};
for entry in dynamic.iter() {
let build::elf::Dynamic::String { tag, val } = entry else {
continue;
};
if *tag != elf::DT_SONAME {
continue;
}
return Some(val);
}
None
}
/// Set the `DT_SONAME` entry in the dynamic section.
pub fn elf_set_soname(&mut self, soname: Vec<u8>) -> Result<()> {
let dynamic = self
.builder
.dynamic_data_mut()
.ok_or_else(|| Error::modify("No dynamic section found; can't set soname"))?;
let mut found = false;
for entry in dynamic.iter_mut() {
let build::elf::Dynamic::String { tag, val } = entry else {
continue;
};
if *tag != elf::DT_SONAME {
continue;
}
*val = soname.clone().into();
#[cfg(feature = "logging")]
info!("Setting DT_SONAME entry to {}", val);
found = true;
}
if !found {
let val = soname.into();
#[cfg(feature = "logging")]
info!("Adding DT_SONAME entry {}", val);
dynamic.push(build::elf::Dynamic::String {
tag: elf::DT_SONAME,
val,
});
}
self.modified = true;
Ok(())
}
/// Find the interpreter path in the `PT_INTERP` segment.
pub fn elf_interpreter(&self) -> Option<&[u8]> {
self.builder.interp_data()
}
/// Set the interpreter path in the `PT_INTERP` segment.
///
/// The null terminator is automatically added if needed.
pub fn elf_set_interpreter(&mut self, mut interpreter: Vec<u8>) -> Result<()> {
let data = self
.builder
.interp_data_mut()
.ok_or_else(|| Error::modify("No interp section found; can't set interpreter"))?;
#[cfg(feature = "logging")]
info!(
"Setting interpreter to {}",
build::ByteString::from(interpreter.as_slice())
);
if !interpreter.is_empty() && interpreter.last() != Some(&0) {
interpreter.push(0);
}
*data = interpreter.into();
self.modified = true;
Ok(())
}
pub(crate) fn elf_finalize(&mut self) -> Result<()> {
if self.modified {
move_sections(&mut self.builder)?;
}
Ok(())
}
}
enum BlockKind {
FileHeader,
ProgramHeaders,
Segment,
Section(build::elf::SectionId),
}
struct Block<'a> {
#[allow(dead_code)]
name: build::ByteString<'a>,
kind: BlockKind,
address: u64,
size: u64,
// Higher means better to move. 0 means never move.
move_priority: u8,
}
/// Move sections between segments if needed, and assign file offsets to segments and sections.
///
/// Does not change the size of existing `PT_LOAD` segments, but may add new segments.
// TODO: allow changing size of existing `PT_LOAD` segments
fn move_sections(builder: &mut build::elf::Builder) -> Result<()> {
builder.delete_orphans();
builder.delete_unused_versions();
builder.set_section_sizes();
let mut added_p_flags = Vec::new();
let mut added_segments = 0;
// Loop until we reach a fixed point for the number of additional segments needed.
loop {
let mut move_sections = find_move_sections(builder, added_segments)?;
if move_sections.is_empty() {
return Ok(());
}
// Calculate the number of additional PT_LOAD segments needed.
added_p_flags.clear();
for id in &move_sections {
let section = builder.sections.get_mut(*id);
// Flag the section as needing to move.
section.sh_offset = 0;
// We need one PT_LOAD segment for each unique combination of p_flags.
let p_flags = section.p_flags();
if !added_p_flags.contains(&p_flags) {
added_p_flags.push(p_flags);
}
}
// If moving a section that is part of a non-PT_LOAD segment, then we may need to
// split the segment, which will require an additional segment.
let mut split_segments = 0;
for segment in &mut builder.segments {
if segment.p_type == elf::PT_LOAD {
continue;
}
let mut any = false;
let mut all = true;
for id in &segment.sections {
if move_sections.contains(id) {
any = true;
} else {
all = false;
}
}
if !any || all {
continue;
}
split_segments += 1;
}
// Check if we have reached a fixed point for the number of additional segments needed.
if added_segments < split_segments + added_p_flags.len() {
added_segments = split_segments + added_p_flags.len();
continue;
}
#[cfg(feature = "logging")]
info!(
"Moving {} sections, adding {} PT_LOAD segments, splitting {} segments",
move_sections.len(),
added_p_flags.len(),
split_segments,
);
// Add the PT_LOAD segments and append sections to them.
// Try to keep the same order of sections in the new segments.
move_sections.sort_by_key(|id| {
let section = builder.sections.get(*id);
(section.sh_addr, section.sh_size)
});
for p_flags in added_p_flags {
// TODO: reuse segments that only contain movable sections
let segment = builder
.segments
.add_load_segment(p_flags, builder.load_align);
for id in &move_sections {
let section = builder.sections.get_mut(*id);
if p_flags == section.p_flags() {
segment.append_section(section);
#[cfg(feature = "logging")]
info!(
"Moved {} to offset {:x}, addr {:x}",
section.name, section.sh_offset, section.sh_addr
);
}
}
#[cfg(feature = "logging")]
info!(
"Added PT_LOAD segment with p_flags {:x}, offset {:x}, addr {:x}, size {:x}",
p_flags, segment.p_offset, segment.p_vaddr, segment.p_memsz,
);
}
// Split or move non-PT_LOAD segments that contain sections that have been moved.
let sections = &builder.sections;
let mut split_segments = Vec::new();
for segment in &mut builder.segments {
if segment.p_type == elf::PT_LOAD {
continue;
}
let mut any = false;
let mut all = true;
for id in &segment.sections {
if move_sections.contains(id) {
any = true;
} else {
all = false;
}
}
if !any {
continue;
}
if !all {
// Segment needs splitting.
// Remove all the sections that have been moved, and store them so
// that we can add the new segment later.
let mut split_sections = Vec::new();
segment.sections.retain(|id| {
if move_sections.contains(id) {
split_sections.push(*id);
false
} else {
true
}
});
split_segments.push((segment.id(), split_sections));
}
// The remaining sections have already been assigned an address.
// Recalcuate the file and address ranges for the segment.
// TODO: verify that the sections are contiguous. If not, try to slide the sections
// down in memory.
segment.recalculate_ranges(sections);
}
// Add new segments due to splitting.
for (segment_id, split_sections) in split_segments {
let segment = builder.segments.copy(segment_id);
for id in split_sections {
let section = builder.sections.get_mut(id);
segment.append_section(section);
}
#[cfg(feature = "logging")]
info!(
"Split segment with type {:x}, offset {:x}, addr {:x}, size {:x}",
segment.p_type, segment.p_offset, segment.p_vaddr, segment.p_memsz,
);
}
// Update the PT_PHDR segment to include the new program headers.
let size = builder.program_headers_size() as u64;
for segment in &mut builder.segments {
if segment.p_type != elf::PT_PHDR {
continue;
}
segment.p_filesz = size;
segment.p_memsz = size;
}
return Ok(());
}
}
fn find_move_sections(
builder: &build::elf::Builder,
added_segments: usize,
) -> Result<Vec<build::elf::SectionId>> {
use build::elf::SectionData;
let mut move_sections = Vec::new();
let mut blocks = Vec::new();
let file_header_size = builder.file_header_size() as u64;
let program_headers_size = (builder.program_headers_size()
+ added_segments * builder.class().program_header_size())
as u64;
let interp = builder.interp_section();
if let Some(segment) = builder.segments.find_load_segment_from_offset(0) {
let address = segment.address_from_offset(0);
blocks.push(Block {
name: "file header".into(),
kind: BlockKind::FileHeader,
address,
size: file_header_size,
move_priority: 0,
});
}
if let Some(segment) = builder
.segments
.find_load_segment_from_offset(builder.header.e_phoff)
{
let address = segment.address_from_offset(builder.header.e_phoff);
blocks.push(Block {
name: "program headers".into(),
kind: BlockKind::ProgramHeaders,
address,
size: program_headers_size,
move_priority: 0,
});
}
for segment in &builder.segments {
if segment.p_type != elf::PT_LOAD {
continue;
}
// Add zero-sized blocks at the start and end of the segment
// to prevent changing the segment address or size.
blocks.push(Block {
name: "segment start".into(),
kind: BlockKind::Segment,
address: segment.p_vaddr,
size: 0,
move_priority: 0,
});
blocks.push(Block {
name: "segment end".into(),
kind: BlockKind::Segment,
address: segment.p_vaddr + segment.p_memsz,
size: 0,
move_priority: 0,
});
}
for section in &builder.sections {
if !section.is_alloc() {
continue;
}
if section.sh_offset == 0 {
// Newly added section that needs to be assigned to a segment,
// or a section that has already been flagged for moving.
move_sections.push(section.id());
continue;
}
if section.sh_type == elf::SHT_NOBITS && section.sh_flags & u64::from(elf::SHF_TLS) != 0 {
// Uninitialized TLS sections are not part of the address space.
continue;
}
let move_priority = match §ion.data {
// Can't move sections whose address may referenced from
// a section that we can't rewrite.
SectionData::Data(_) => {
if Some(section.id()) == interp {
1
} else {
0
}
}
SectionData::UninitializedData(_) | SectionData::Dynamic(_) => 0,
// TODO: Can be referenced by dynamic entries, but we don't support that yet.
SectionData::DynamicRelocation(_) => 0,
// None of these can be referenced by address that I am aware of.
SectionData::Relocation(_)
| SectionData::Note(_)
| SectionData::Attributes(_)
| SectionData::SectionString
| SectionData::Symbol
| SectionData::SymbolSectionIndex
| SectionData::String
| SectionData::DynamicSymbol
| SectionData::DynamicString
| SectionData::Hash
| SectionData::GnuHash
| SectionData::GnuVersym
| SectionData::GnuVerdef
| SectionData::GnuVerneed => 2,
};
blocks.push(Block {
name: (*section.name).into(),
kind: BlockKind::Section(section.id()),
address: section.sh_addr,
size: section.sh_size,
move_priority,
});
}
blocks.sort_by_key(|block| (block.address, block.size));
// For each pair of overlapping blocks, decide which one to move.
let mut i = 0;
while i + 1 < blocks.len() {
let end_address = blocks[i].address + blocks[i].size;
if end_address <= blocks[i + 1].address {
i += 1;
continue;
}
// Prefer moving the earlier block, since it is the reason for the overlap.
if blocks[i].move_priority >= blocks[i + 1].move_priority {
if blocks[i].move_priority == 0 {
#[cfg(feature = "logging")]
info!(
"Immovable {} (end address {:x}) overlaps immovable {} (start address {:x})",
blocks[i].name,
end_address,
blocks[i + 1].name,
blocks[i + 1].address,
);
return Err(Error::modify("Overlapping immovable sections"));
}
#[cfg(feature = "logging")]
info!(
"Need to move {} (end address {:x}) because of {} (start address {:x})",
blocks[i].name,
end_address,
blocks[i + 1].name,
blocks[i + 1].address,
);
if let BlockKind::Section(section) = blocks[i].kind {
move_sections.push(section);
blocks.remove(i);
} else {
// Only sections can be moved.
unreachable!();
}
} else {
#[cfg(feature = "logging")]
info!(
"Need to move {} (start address {:x}) because of {} (end address {:x})",
blocks[i + 1].name,
blocks[i + 1].address,
blocks[i].name,
end_address,
);
if let BlockKind::Section(section) = blocks[i + 1].kind {
move_sections.push(section);
blocks.remove(i + 1);
} else {
// Only sections can be moved.
unreachable!();
}
}
}
Ok(move_sections)
}