Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elf: Add convenient functions for symbol write #387

Merged
merged 1 commit into from
Feb 4, 2024
Merged

Conversation

tiann
Copy link
Contributor

@tiann tiann commented Jan 11, 2024

Here is an example of modifying elf:

use goblin::elf::{section_header, sym::Sym, Elf};
use scroll::{ctx::SizeWith, Pwrite};

use anyhow::Result;
use std::fs;

fn main() -> Result<()> {
    let path = "my.elf";
    let mut buffer = fs::read(path)?;
    let elf = Elf::parse(&buffer)?;

    let ctx = elf.syms.ctx().clone();
    for (index, mut sym) in elf.syms.iter().enumerate() {
        let Some(name) = elf.strtab.get_at(sym.st_name) else {
            continue;
        };

        if name == "my_symbol" {
            let offset = elf.syms.offset() + index * Sym::size_with(elf.syms.ctx());
            println!("name: {}, offset: {}", name, offset);
            sym.st_shndx = section_header::SHN_ABS as usize;
            sym.st_value = 0xeaeaeaea;
            buffer.pwrite_with(sym, offset, ctx)?;
            break;
        }
    }

    Ok(())
}

#325 (comment) shows that we can modify elf, but we can't get the underlying offset and the symbol size.

Although we can iterate the section header and find the offset, it is not so convenient, this PR exposes some useful field to make it easier.

Copy link
Owner

@m4b m4b left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks LGTM!

@m4b m4b merged commit 30c0c33 into m4b:master Feb 4, 2024
6 checks passed
@m4b
Copy link
Owner

m4b commented Feb 4, 2024

note: non-breaking, added pub api

@m4b
Copy link
Owner

m4b commented Apr 27, 2024

released in 0.8.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants