{"id":13578839,"url":"https://github.com/gz/rust-elfloader","last_synced_at":"2025-04-05T13:08:17.127Z","repository":{"id":30362653,"uuid":"33915166","full_name":"gz/rust-elfloader","owner":"gz","description":"Library to load and relocate ELF files.","archived":false,"fork":false,"pushed_at":"2023-07-08T14:54:05.000Z","size":247,"stargazers_count":125,"open_issues_count":6,"forks_count":23,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T12:08:38.243Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://docs.rs/elfloader","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2015-04-14T06:39:45.000Z","updated_at":"2025-03-14T06:04:51.000Z","dependencies_parsed_at":"2024-01-16T20:29:07.708Z","dependency_job_id":"6f11f4a2-8b86-4416-8eee-ead3524da616","html_url":"https://github.com/gz/rust-elfloader","commit_stats":{"total_commits":143,"total_committers":14,"mean_commits":"10.214285714285714","dds":0.5594405594405594,"last_synced_commit":"478ec8a8d37625800c90bfee99508e1696d74a41"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gz%2Frust-elfloader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gz%2Frust-elfloader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gz%2Frust-elfloader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gz%2Frust-elfloader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gz","download_url":"https://codeload.github.com/gz/rust-elfloader/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247339158,"owners_count":20923014,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T15:01:34.234Z","updated_at":"2025-04-05T13:08:17.110Z","avatar_url":"https://github.com/gz.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"![Build](https://github.com/gz/rust-elfloader/actions/workflows/standard.yml/badge.svg) [![cargo-badge][]][cargo-link] [![docs-badge][]][docs-link]\n\n# elfloader\n\nA library to load and relocate ELF files in memory. This library depends only on\nlibcore so it can be used in kernel level code, for example to load user-space\nprograms.\n\n## How-to use\n\nClients will have to implement the ElfLoader trait:\n\n```rust\nuse elfloader::*;\nuse log::info;\n\n/// A simple ExampleLoader, that implements ElfLoader\n/// but does nothing but logging\nstruct ExampleLoader {\n    vbase: u64,\n}\n\nimpl ElfLoader for ExampleLoader {\n    fn allocate(\u0026mut self, load_headers: LoadableHeaders) -\u003e Result\u003c(), ElfLoaderErr\u003e {\n        for header in load_headers {\n            info!(\n                \"allocate base = {:#x} size = {:#x} flags = {}\",\n                header.virtual_addr(),\n                header.mem_size(),\n                header.flags()\n            );\n        }\n        Ok(())\n    }\n\n    fn relocate(\u0026mut self, entry: RelocationEntry) -\u003e Result\u003c(), ElfLoaderErr\u003e {\n        use RelocationType::x86_64;\n        use crate::arch::x86_64::RelocationTypes::*;\n\n        let addr: *mut u64 = (self.vbase + entry.offset) as *mut u64;\n\n        match entry.rtype {\n            x86_64(R_AMD64_RELATIVE) =\u003e {\n\n                // This type requires addend to be present\n                let addend = entry\n                    .addend\n                    .ok_or(ElfLoaderErr::UnsupportedRelocationEntry)?;\n\n                // This is a relative relocation, add the offset (where we put our\n                // binary in the vspace) to the addend and we're done.\n                info!(\n                    \"R_RELATIVE *{:p} = {:#x}\",\n                    addr,\n                    self.vbase + addend\n                );\n                Ok(())\n            }\n            _ =\u003e Ok((/* not implemented */)),\n        }\n    }\n\n    fn load(\u0026mut self, flags: Flags, base: VAddr, region: \u0026[u8]) -\u003e Result\u003c(), ElfLoaderErr\u003e {\n        let start = self.vbase + base;\n        let end = self.vbase + base + region.len() as u64;\n        info!(\"load region into = {:#x} -- {:#x}\", start, end);\n        Ok(())\n    }\n\n    fn tls(\n        \u0026mut self,\n        tdata_start: VAddr,\n        _tdata_length: u64,\n        total_size: u64,\n        _align: u64\n    ) -\u003e Result\u003c(), ElfLoaderErr\u003e {\n        let tls_end = tdata_start +  total_size;\n        info!(\"Initial TLS region is at = {:#x} -- {:#x}\", tdata_start, tls_end);\n        Ok(())\n    }\n\n}\n\n// Then, with ElfBinary, a ELF file is loaded using `load`:\nfn main() {\n    use std::fs;\n\n    let binary_blob = fs::read(\"test/test.x86_64\").expect(\"Can't read binary\");\n    let binary = ElfBinary::new(binary_blob.as_slice()).expect(\"Got proper ELF file\");\n    let mut loader = ExampleLoader { vbase: 0x1000_0000 };\n    binary.load(\u0026mut loader).expect(\"Can't load the binary?\");\n}\n```\n\n[//]: # (badges/links)\n[cargo-badge]: https://img.shields.io/crates/v/elfloader.svg?label=crates.io\n[cargo-link]: https://crates.io/crates/elfloader\n[docs-badge]: https://docs.rs/elfloader/badge.svg?label=docs.rs\n[docs-link]: https://docs.rs/elfloader\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgz%2Frust-elfloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgz%2Frust-elfloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgz%2Frust-elfloader/lists"}