Skip to content

Commit 637a5f6

Browse files
committed
add position header if -pie
1 parent 58e25bb commit 637a5f6

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/convert.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,23 @@ pub fn elf_to_tbf(
443443
}
444444
}
445445

446+
// Check if the _pie symbol is defined meaning this app uses -pie for
447+
// position independence.
448+
let position_configuration = if let Ok(Some((symtab, sym_strtab))) = elf_file.symbol_table() {
449+
if let Some(_) = symtab.iter().find(|sym| {
450+
let name = sym_strtab
451+
.get(sym.st_name as usize)
452+
.expect("Failed to parse symbol name");
453+
name == "_pie"
454+
}) {
455+
Some(header::PositionConfiguration::Pie)
456+
} else {
457+
None
458+
}
459+
} else {
460+
None
461+
};
462+
446463
// Now we can create the first pass TBF header. This is mostly to get the
447464
// size of the header since we have to fill in some of the offsets later.
448465
let mut tbfheader = header::TbfHeader::new();
@@ -463,6 +480,7 @@ pub fn elf_to_tbf(
463480
storage_ids,
464481
kernel_version,
465482
short_id,
483+
position_configuration,
466484
disabled,
467485
);
468486

src/header.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub enum TbfHeaderTypes {
2020
KernelVersion = 8,
2121
Program = 9,
2222
ShortId = 10,
23+
Position = 11,
2324

2425
Credentials = 128,
2526
}
@@ -132,6 +133,18 @@ struct TbfHeaderShortId {
132133
short_id: u32,
133134
}
134135

136+
pub enum PositionConfiguration {
137+
RopiRwpi = 0,
138+
Pie = 1,
139+
}
140+
141+
#[repr(C)]
142+
#[derive(Clone, Copy, Debug)]
143+
struct TbfHeaderPosition {
144+
base: TbfHeaderTlv,
145+
configuration: u32,
146+
}
147+
135148
#[repr(C)]
136149
#[derive(Debug)]
137150
pub struct TbfFooterCredentials {
@@ -285,7 +298,6 @@ impl fmt::Display for TbfHeaderKernelVersion {
285298

286299
impl fmt::Display for TbfHeaderShortId {
287300
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
288-
// ^x.y means >= x.y, < (x+1).0
289301
writeln!(
290302
f,
291303
"
@@ -295,6 +307,17 @@ impl fmt::Display for TbfHeaderShortId {
295307
}
296308
}
297309

310+
impl fmt::Display for TbfHeaderPosition {
311+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
312+
writeln!(
313+
f,
314+
"
315+
Position Independent: {}",
316+
self.configuration
317+
)
318+
}
319+
}
320+
298321
const FLAGS_ENABLE: u32 = 0x0000_0001;
299322

300323
pub struct TbfHeader {
@@ -308,6 +331,7 @@ pub struct TbfHeader {
308331
hdr_persistent: Option<TbfHeaderPersistentAcl>,
309332
hdr_kernel_version: Option<TbfHeaderKernelVersion>,
310333
hdr_short_id: Option<TbfHeaderShortId>,
334+
hdr_position: Option<TbfHeaderPosition>,
311335
package_name: String,
312336
package_name_pad: usize,
313337
}
@@ -340,6 +364,7 @@ impl TbfHeader {
340364
hdr_persistent: None,
341365
hdr_kernel_version: None,
342366
hdr_short_id: None,
367+
hdr_position: None,
343368
package_name: String::new(),
344369
package_name_pad: 0,
345370
}
@@ -363,6 +388,7 @@ impl TbfHeader {
363388
storage_ids: (Option<u32>, Option<Vec<u32>>, Option<Vec<u32>>),
364389
kernel_version: Option<(u16, u16)>,
365390
short_id: Option<u32>,
391+
position_configuration: Option<PositionConfiguration>,
366392
disabled: bool,
367393
) -> usize {
368394
// Need to calculate lengths ahead of time. Need the base and the
@@ -573,6 +599,17 @@ impl TbfHeader {
573599
});
574600
}
575601

602+
// If the position configuration is set, include that header.
603+
if let Some(position_configuration) = position_configuration {
604+
self.hdr_position = Some(TbfHeaderPosition {
605+
base: TbfHeaderTlv {
606+
tipe: TbfHeaderTypes::Position,
607+
length: 4,
608+
},
609+
configuration: position_configuration as u32,
610+
})
611+
}
612+
576613
// Return the length by generating the header and seeing how long it is.
577614
self.generate()
578615
.expect("No header was generated")

0 commit comments

Comments
 (0)