Skip to content

Commit

Permalink
td-shim: add a feature to support optional payload relocation
Browse files Browse the repository at this point in the history
Signed-off-by: Jiaqi Gao <[email protected]>
  • Loading branch information
gaojiaqi7 committed Oct 17, 2022
1 parent ddf3a07 commit 15839d8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 23 deletions.
6 changes: 5 additions & 1 deletion td-loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ log = "0.4.13"
scroll = { version = "0.10", default-features=false, features = ["derive"] }

[dev-dependencies]
env_logger = "0.9.0"
env_logger = "0.9.0"

[features]
default = []
disable-relocation = []
22 changes: 12 additions & 10 deletions td-loader/src/elf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,18 @@ pub fn relocate_elf_with_per_program_header(
}
}

// relocate to base
for reloc in elf.relocations()? {
if reloc.r_type() == R_X86_64_RELATIVE {
let r_addend = reloc.r_addend;
loaded_buffer
.pwrite::<u64>(
new_image_base.checked_add(r_addend as usize)? as u64,
reloc.r_offset as usize,
)
.ok()?;
if !cfg!(feature = "disable-relocation") {
// relocate to base
for reloc in elf.relocations()? {
if reloc.r_type() == R_X86_64_RELATIVE {
let r_addend = reloc.r_addend;
loaded_buffer
.pwrite::<u64>(
new_image_base.checked_add(r_addend as usize)? as u64,
reloc.r_offset as usize,
)
.ok()?;
}
}
}

Expand Down
26 changes: 14 additions & 12 deletions td-loader/src/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,6 @@ pub fn relocate_with_per_section(
let coff_optional_offset = coff_standard_end;
let coff_optional_end = coff_header_end.checked_add(coff_optional_size)?;
image_buffer.len().checked_sub(coff_optional_end)?;
let coff_optional_region = &image_buffer[coff_optional_offset..coff_optional_end];
let image_base = coff_optional_region.pread::<u64>(0).ok()?;

// Validate section header region
// There's no "Data Directories", so "Section Table" follows COFF Optional Fields.
Expand Down Expand Up @@ -210,16 +208,20 @@ pub fn relocate_with_per_section(
}
}

let sections = Sections::parse(sections_buffer, num_sections as usize)?;
for section in sections {
if &section.name == b".reloc\0\0" && image_base != new_image_base as u64 {
reloc_to_base(
loaded_buffer,
image_buffer,
&section,
image_base as usize,
new_image_base as usize,
)?;
if !cfg!(feature = "disable-relocation") {
let coff_optional_region = &image_buffer[coff_optional_offset..coff_optional_end];
let image_base = coff_optional_region.pread::<u64>(0).ok()?;
let sections = Sections::parse(sections_buffer, num_sections as usize)?;
for section in sections {
if &section.name == b".reloc\0\0" && image_base != new_image_base as u64 {
reloc_to_base(
loaded_buffer,
image_buffer,
&section,
image_base as usize,
new_image_base as usize,
)?;
}
}
}

Expand Down
1 change: 1 addition & 0 deletions td-shim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ boot-kernel = ["td-layout/boot-kernel"]
secure-boot = ["der", "ring"]
tdx = ["tdx-tdcall", "td-exception/tdx", "td-logger/tdx", "x86"]
lazy-accept = ["tdx"]
disable-relocation = ["td-loader/disable-relocation"]
main = [
"td-loader",
"linked_list_allocator",
Expand Down

0 comments on commit 15839d8

Please sign in to comment.