diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2007-01-31 14:48:19 (GMT) |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2007-01-31 14:48:19 (GMT) |
commit | 56cae84500ca807079376d9fa6c5483f240c5b33 (patch) | |
tree | 9bb00069e50d76ed1a2eb9f9922670dffe2c0750 /src | |
parent | f6a628b812c1180eac30db7db93eb59de58589bc (diff) | |
download | patchelf-56cae84500ca807079376d9fa6c5483f240c5b33.zip patchelf-56cae84500ca807079376d9fa6c5483f240c5b33.tar.gz patchelf-56cae84500ca807079376d9fa6c5483f240c5b33.tar.bz2 |
* Instantiate the right template depending on whether the file is 32
or 64 bits. (This is done at runtime, so patchelf can patch 32
*and* 64-bit executables on both 32 and 64-bit platforms).
Diffstat (limited to 'src')
-rw-r--r-- | src/patchelf.cc | 55 |
1 files changed, 39 insertions, 16 deletions
diff --git a/src/patchelf.cc b/src/patchelf.cc index af832a3..9dba988 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -152,14 +152,9 @@ void ElfFile<ElfFileParamNames>::parse() hdr = (Elf_Ehdr *) contents; - if (memcmp(hdr->e_ident, ELFMAG, 4) != 0) + if (memcmp(hdr->e_ident, ELFMAG, SELFMAG) != 0) error("not an ELF executable"); - if (contents[EI_CLASS] != ELFCLASS32 || - contents[EI_DATA] != ELFDATA2LSB || - contents[EI_VERSION] != EV_CURRENT) - error("ELF executable is not 32-bit, little-endian, version 1"); - if (hdr->e_type != ET_EXEC && hdr->e_type != ET_DYN) error("wrong ELF type"); @@ -692,17 +687,9 @@ static bool printRPath = false; static string newRPath; -static void patchElf() +template<class ElfFile> +static void patchElf2(ElfFile & elfFile, mode_t fileMode) { - if (!printInterpreter && !printRPath) - debug("patching ELF file `%s'\n", fileName.c_str()); - - mode_t fileMode; - - readFile(fileName, &fileMode); - - ElfFile<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Addr, Elf32_Off, Elf32_Dyn> elfFile; - elfFile.parse(); @@ -727,6 +714,42 @@ static void patchElf() } } + +static void patchElf() +{ + if (!printInterpreter && !printRPath) + debug("patching ELF file `%s'\n", fileName.c_str()); + + mode_t fileMode; + + readFile(fileName, &fileMode); + + + /* Check the ELF header for basic validity. */ + if (fileSize < sizeof(Elf32_Ehdr)) error("missing ELF header"); + + if (memcmp(contents, ELFMAG, SELFMAG) != 0) + error("not an ELF executable"); + + if (contents[EI_CLASS] == ELFCLASS32 && + contents[EI_DATA] == ELFDATA2LSB && + contents[EI_VERSION] == EV_CURRENT) + { + ElfFile<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Addr, Elf32_Off, Elf32_Dyn> elfFile; + patchElf2(elfFile, fileMode); + } + else if (contents[EI_CLASS] == ELFCLASS64 && + contents[EI_DATA] == ELFDATA2LSB && + contents[EI_VERSION] == EV_CURRENT) + { + ElfFile<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Addr, Elf64_Off, Elf64_Dyn> elfFile; + patchElf2(elfFile, fileMode); + } + else { + error("ELF executable is not 32/64-bit, little-endian, version 1"); + } +} + int main(int argc, char * * argv) { |