Fredrik Fornwall
8 years ago
3 changed files with 5 additions and 370 deletions
@ -1,7 +1,7 @@ |
|||
TERMUX_PKG_HOMEPAGE=https://termux.com/ |
|||
TERMUX_PKG_HOMEPAGE=https://github.com/termux/termux-elf-cleaner |
|||
TERMUX_PKG_DESCRIPTION="Cleaner of ELF files for Android" |
|||
TERMUX_PKG_VERSION=1.1 |
|||
|
|||
termux_step_make_install () { |
|||
$CXX $CFLAGS $LDFLAGS -std=c++14 -Wall -Wextra -pedantic -Werror $TERMUX_PKG_BUILDER_DIR/*.cpp -o $TERMUX_PREFIX/bin/termux-elf-cleaner |
|||
} |
|||
TERMUX_PKG_SRCURL=https://github.com/termux/termux-elf-cleaner/archive/v${TERMUX_PKG_VERSION}.tar.gz |
|||
TERMUX_PKG_SHA256=66612b294e197ab7bfac807e497581df58424af6a7c855f89fc12eafa3dc1b8c |
|||
TERMUX_PKG_FOLDERNAME=termux-elf-cleaner-$TERMUX_PKG_VERSION |
|||
TERMUX_PKG_BUILD_IN_SRC=yes |
|||
|
@ -1,211 +0,0 @@ |
|||
#ifndef ELF_H_INCLUDED |
|||
#define ELF_H_INCLUDED |
|||
|
|||
#include <stdint.h> |
|||
|
|||
/* Type for a 16-bit quantity. */ |
|||
typedef uint16_t Elf32_Half; |
|||
typedef uint16_t Elf64_Half; |
|||
|
|||
/* Types for signed and unsigned 32-bit quantities. */ |
|||
typedef uint32_t Elf32_Word; |
|||
typedef int32_t Elf32_Sword; |
|||
typedef uint32_t Elf64_Word; |
|||
typedef int32_t Elf64_Sword; |
|||
|
|||
/* Types for signed and unsigned 64-bit quantities. */ |
|||
typedef uint64_t Elf32_Xword; |
|||
typedef int64_t Elf32_Sxword; |
|||
typedef uint64_t Elf64_Xword; |
|||
typedef int64_t Elf64_Sxword; |
|||
|
|||
/* Type of addresses. */ |
|||
typedef uint32_t Elf32_Addr; |
|||
typedef uint64_t Elf64_Addr; |
|||
|
|||
/* Type of file offsets. */ |
|||
typedef uint32_t Elf32_Off; |
|||
typedef uint64_t Elf64_Off; |
|||
|
|||
/* Type for section indices, which are 16-bit quantities. */ |
|||
typedef uint16_t Elf32_Section; |
|||
typedef uint16_t Elf64_Section; |
|||
|
|||
/* Type for version symbol information. */ |
|||
typedef Elf32_Half Elf32_Versym; |
|||
typedef Elf64_Half Elf64_Versym; |
|||
|
|||
|
|||
/* The ELF file header. This appears at the start of every ELF file. */ |
|||
typedef struct { |
|||
unsigned char e_ident[16]; /* Magic number and other info */ |
|||
Elf32_Half e_type; /* Object file type */ |
|||
Elf32_Half e_machine; /* Architecture */ |
|||
Elf32_Word e_version; /* Object file version */ |
|||
Elf32_Addr e_entry; /* Entry point virtual address */ |
|||
Elf32_Off e_phoff; /* Program header table (usually follows elf header directly) file offset */ |
|||
Elf32_Off e_shoff; /* Section header table (at end of file) file offset */ |
|||
Elf32_Word e_flags; /* Processor-specific flags */ |
|||
Elf32_Half e_ehsize; /* ELF header size in bytes */ |
|||
Elf32_Half e_phentsize; /* Program header table entry size */ |
|||
Elf32_Half e_phnum; /* Program header table entry count */ |
|||
Elf32_Half e_shentsize; /* Section header table entry size */ |
|||
Elf32_Half e_shnum; /* Section header table entry count */ |
|||
Elf32_Half e_shstrndx; /* Section header string table index */ |
|||
} Elf32_Ehdr; |
|||
typedef struct { |
|||
unsigned char e_ident[16]; /* Magic number and other info */ |
|||
Elf64_Half e_type; /* Object file type */ |
|||
Elf64_Half e_machine; /* Architecture */ |
|||
Elf64_Word e_version; /* Object file version */ |
|||
Elf64_Addr e_entry; /* Entry point virtual address */ |
|||
Elf64_Off e_phoff; /* Program header table file offset */ |
|||
Elf64_Off e_shoff; /* Section header table file offset */ |
|||
Elf64_Word e_flags; /* Processor-specific flags */ |
|||
Elf64_Half e_ehsize; /* ELF header size in bytes */ |
|||
Elf64_Half e_phentsize; /* Program header table entry size */ |
|||
Elf64_Half e_phnum; /* Program header table entry count */ |
|||
Elf64_Half e_shentsize; /* Section header table entry size */ |
|||
Elf64_Half e_shnum; /* Section header table entry count */ |
|||
Elf64_Half e_shstrndx; /* Section header string table index */ |
|||
} Elf64_Ehdr; |
|||
|
|||
/* Section header entry. The number of section entries in the file are determined by the "e_shnum" field of the ELF header.*/ |
|||
typedef struct { |
|||
Elf32_Word sh_name; /* Section name (string tbl index) */ |
|||
Elf32_Word sh_type; /* Section type */ |
|||
Elf32_Word sh_flags; /* Section flags */ |
|||
Elf32_Addr sh_addr; /* Section virtual addr at execution */ |
|||
Elf32_Off sh_offset; /* Section file offset */ |
|||
Elf32_Word sh_size; /* Section size in bytes */ |
|||
Elf32_Word sh_link; /* Link to another section */ |
|||
Elf32_Word sh_info; /* Additional section information */ |
|||
Elf32_Word sh_addralign; /* Section alignment */ |
|||
Elf32_Word sh_entsize; /* Entry size if section holds table */ |
|||
} Elf32_Shdr; |
|||
typedef struct { |
|||
Elf64_Word sh_name; /* Section name (string tbl index) */ |
|||
Elf64_Word sh_type; /* Section type */ |
|||
Elf64_Xword sh_flags; /* Section flags */ |
|||
Elf64_Addr sh_addr; /* Section virtual addr at execution */ |
|||
Elf64_Off sh_offset; /* Section file offset */ |
|||
Elf64_Xword sh_size; /* Section size in bytes */ |
|||
Elf64_Word sh_link; /* Link to another section */ |
|||
Elf64_Word sh_info; /* Additional section information */ |
|||
Elf64_Xword sh_addralign; /* Section alignment */ |
|||
Elf64_Xword sh_entsize; /* Entry size if section holds table */ |
|||
} Elf64_Shdr; |
|||
|
|||
/* Legal values for sh_type (section type). */ |
|||
#define SHT_NULL 0 /* Section header table entry unused */ |
|||
#define SHT_PROGBITS 1 /* Program data */ |
|||
#define SHT_SYMTAB 2 /* Symbol table */ |
|||
#define SHT_STRTAB 3 /* String table */ |
|||
#define SHT_RELA 4 /* Relocation entries with addends */ |
|||
#define SHT_HASH 5 /* Symbol hash table */ |
|||
#define SHT_DYNAMIC 6 /* Dynamic linking information. Contains Elf32_Dyn/Elf64_Dyn entries. */ |
|||
#define SHT_NOTE 7 /* Notes */ |
|||
#define SHT_NOBITS 8 /* Program space with no data (bss) */ |
|||
#define SHT_REL 9 /* Relocation entries, no addends */ |
|||
#define SHT_SHLIB 10 /* Reserved */ |
|||
#define SHT_DYNSYM 11 /* Dynamic linker symbol table */ |
|||
#define SHT_INIT_ARRAY 14 /* Array of constructors */ |
|||
#define SHT_FINI_ARRAY 15 /* Array of destructors */ |
|||
#define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */ |
|||
#define SHT_GROUP 17 /* Section group */ |
|||
#define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */ |
|||
#define SHT_NUM 19 /* Number of defined types. */ |
|||
#define SHT_LOOS 0x60000000 /* Start OS-specific. */ |
|||
#define SHT_GNU_ATTRIBUTES 0x6ffffff5 /* Object attributes. */ |
|||
#define SHT_GNU_HASH 0x6ffffff6 /* GNU-style hash table. */ |
|||
#define SHT_GNU_LIBLIST 0x6ffffff7 /* Prelink library list */ |
|||
#define SHT_CHECKSUM 0x6ffffff8 /* Checksum for DSO content. */ |
|||
#define SHT_LOSUNW 0x6ffffffa /* Sun-specific low bound. */ |
|||
#define SHT_SUNW_move 0x6ffffffa |
|||
#define SHT_SUNW_COMDAT 0x6ffffffb |
|||
#define SHT_SUNW_syminfo 0x6ffffffc |
|||
#define SHT_GNU_verdef 0x6ffffffd /* Version definition section. */ |
|||
#define SHT_GNU_verneed 0x6ffffffe /* Version needs section. */ |
|||
#define SHT_GNU_versym 0x6fffffff /* Version symbol table. */ |
|||
#define SHT_HISUNW 0x6fffffff /* Sun-specific high bound. */ |
|||
#define SHT_HIOS 0x6fffffff /* End OS-specific type */ |
|||
#define SHT_LOPROC 0x70000000 /* Start of processor-specific */ |
|||
#define SHT_HIPROC 0x7fffffff /* End of processor-specific */ |
|||
#define SHT_LOUSER 0x80000000 /* Start of application-specific */ |
|||
#define SHT_HIUSER 0x8fffffff /* End of application-specific */ |
|||
|
|||
/* Dynamic section entry. */ |
|||
typedef struct { |
|||
Elf32_Sword d_tag; /* Dynamic entry type */ |
|||
union { Elf32_Word d_val; Elf32_Addr d_ptr; } d_un; /* Integer or address value */ |
|||
} Elf32_Dyn; |
|||
typedef struct { |
|||
Elf64_Sxword d_tag; /* Dynamic entry type */ |
|||
union { Elf64_Xword d_val; Elf64_Addr d_ptr; } d_un; /* Integer or address value */ |
|||
} Elf64_Dyn; |
|||
|
|||
/* Legal values for d_tag (dynamic entry type). */ |
|||
#define DT_NULL 0 /* Marks end of dynamic section */ |
|||
#define DT_NEEDED 1 /* Name of needed library */ |
|||
#define DT_PLTRELSZ 2 /* Size in bytes of PLT relocs */ |
|||
#define DT_PLTGOT 3 /* Processor defined value */ |
|||
#define DT_HASH 4 /* Address of symbol hash table */ |
|||
#define DT_STRTAB 5 /* Address of string table */ |
|||
#define DT_SYMTAB 6 /* Address of symbol table */ |
|||
#define DT_RELA 7 /* Address of Rela relocs */ |
|||
#define DT_RELASZ 8 /* Total size of Rela relocs */ |
|||
#define DT_RELAENT 9 /* Size of one Rela reloc */ |
|||
#define DT_STRSZ 10 /* Size of string table */ |
|||
#define DT_SYMENT 11 /* Size of one symbol table entry */ |
|||
#define DT_INIT 12 /* Address of init function */ |
|||
#define DT_FINI 13 /* Address of termination function */ |
|||
#define DT_SONAME 14 /* Name of shared object */ |
|||
#define DT_RPATH 15 /* Library search path (deprecated) */ |
|||
#define DT_SYMBOLIC 16 /* Start symbol search here */ |
|||
#define DT_REL 17 /* Address of Rel relocs */ |
|||
#define DT_RELSZ 18 /* Total size of Rel relocs */ |
|||
#define DT_RELENT 19 /* Size of one Rel reloc */ |
|||
#define DT_PLTREL 20 /* Type of reloc in PLT */ |
|||
#define DT_DEBUG 21 /* For debugging; unspecified */ |
|||
#define DT_TEXTREL 22 /* Reloc might modify .text */ |
|||
#define DT_JMPREL 23 /* Address of PLT relocs */ |
|||
#define DT_BIND_NOW 24 /* Process relocations of object */ |
|||
#define DT_INIT_ARRAY 25 /* Array with addresses of init fct */ |
|||
#define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */ |
|||
#define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */ |
|||
#define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */ |
|||
#define DT_RUNPATH 29 /* Library search path */ |
|||
#define DT_FLAGS 30 /* Flags for the object being loaded */ |
|||
#define DT_ENCODING 32 /* Start of encoded range */ |
|||
#define DT_PREINIT_ARRAY 32 /* Array with addresses of preinit fct*/ |
|||
#define DT_PREINIT_ARRAYSZ 33 /* size in bytes of DT_PREINIT_ARRAY */ |
|||
#define DT_NUM 34 /* Number used */ |
|||
#define DT_LOOS 0x6000000d /* Start of OS-specific */ |
|||
#define DT_HIOS 0x6ffff000 /* End of OS-specific */ |
|||
#define DT_VERDEF 0x6ffffffc |
|||
#define DT_VERDEFNUM 0x6ffffffd |
|||
#define DT_LOPROC 0x70000000 /* Start of processor-specific */ |
|||
#define DT_HIPROC 0x7fffffff /* End of processor-specific */ |
|||
|
|||
|
|||
/* Symbol table entry. */ |
|||
typedef struct { |
|||
Elf32_Word st_name; /* Symbol name (string tbl index) */ |
|||
Elf32_Addr st_value; /* Symbol value */ |
|||
Elf32_Word st_size; /* Symbol size */ |
|||
unsigned char st_info; /* Symbol type and binding */ |
|||
unsigned char st_other; /* Symbol visibility */ |
|||
Elf32_Section st_shndx; /* Section index */ |
|||
} Elf32_Sym; |
|||
|
|||
typedef struct { |
|||
Elf64_Word st_name; /* Symbol name (string tbl index) */ |
|||
unsigned char st_info; /* Symbol type and binding */ |
|||
unsigned char st_other; /* Symbol visibility */ |
|||
Elf64_Section st_shndx; /* Section index */ |
|||
Elf64_Addr st_value; /* Symbol value */ |
|||
Elf64_Xword st_size; /* Symbol size */ |
|||
} Elf64_Sym; |
|||
|
|||
|
|||
#endif |
@ -1,154 +0,0 @@ |
|||
#include <algorithm> |
|||
#include <fcntl.h> |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <sys/mman.h> |
|||
#include <sys/stat.h> |
|||
#include <sys/types.h> |
|||
#include <unistd.h> |
|||
|
|||
#ifdef __APPLE__ |
|||
# include "elf.h" |
|||
#else |
|||
# include <elf.h> |
|||
#endif |
|||
|
|||
#define DT_VERSYM 0x6ffffff0 |
|||
#define DT_VERNEEDED 0x6ffffffe |
|||
#define DT_VERNEEDNUM 0x6fffffff |
|||
|
|||
template<typename ElfHeaderType /*Elf{32,64}_Ehdr*/, |
|||
typename ElfSectionHeaderType /*Elf{32,64}_Shdr*/, |
|||
typename ElfDynamicSectionEntryType /* Elf{32,64}_Dyn */> |
|||
bool process_elf(uint8_t* bytes, size_t elf_file_size, char const* file_name) |
|||
{ |
|||
if (sizeof(ElfSectionHeaderType) > elf_file_size) { |
|||
fprintf(stderr, "termux-elf-cleaner: Elf header for '%s' would end at %zu but file size only %zu\n", file_name, sizeof(ElfSectionHeaderType), elf_file_size); |
|||
return false; |
|||
} |
|||
ElfHeaderType* elf_hdr = reinterpret_cast<ElfHeaderType*>(bytes); |
|||
|
|||
size_t last_section_header_byte = elf_hdr->e_shoff + sizeof(ElfSectionHeaderType) * elf_hdr->e_shnum; |
|||
if (last_section_header_byte > elf_file_size) { |
|||
fprintf(stderr, "termux-elf-cleaner: Section header for '%s' would end at %zu but file size only %zu\n", file_name, last_section_header_byte, elf_file_size); |
|||
return false; |
|||
} |
|||
ElfSectionHeaderType* section_header_table = reinterpret_cast<ElfSectionHeaderType*>(bytes + elf_hdr->e_shoff); |
|||
|
|||
for (unsigned int i = 1; i < elf_hdr->e_shnum; i++) { |
|||
ElfSectionHeaderType* section_header_entry = section_header_table + i; |
|||
if (section_header_entry->sh_type == SHT_DYNAMIC) { |
|||
size_t const last_dynamic_section_byte = section_header_entry->sh_offset + section_header_entry->sh_size; |
|||
if (last_dynamic_section_byte > elf_file_size) { |
|||
fprintf(stderr, "termux-elf-cleaner: Dynamic section for '%s' would end at %zu but file size only %zu\n", file_name, last_dynamic_section_byte, elf_file_size); |
|||
return false; |
|||
} |
|||
|
|||
size_t const dynamic_section_entries = section_header_entry->sh_size / sizeof(ElfDynamicSectionEntryType); |
|||
ElfDynamicSectionEntryType* const dynamic_section = |
|||
reinterpret_cast<ElfDynamicSectionEntryType*>(bytes + section_header_entry->sh_offset); |
|||
|
|||
unsigned int last_nonnull_entry_idx = 0; |
|||
for (unsigned int j = dynamic_section_entries - 1; j > 0; j--) { |
|||
ElfDynamicSectionEntryType* dynamic_section_entry = dynamic_section + j; |
|||
if (dynamic_section_entry->d_tag != DT_NULL) { |
|||
last_nonnull_entry_idx = j; |
|||
break; |
|||
} |
|||
} |
|||
|
|||
for (unsigned int j = 0; j < dynamic_section_entries; j++) { |
|||
ElfDynamicSectionEntryType* dynamic_section_entry = dynamic_section + j; |
|||
char const* removed_name = nullptr; |
|||
switch (dynamic_section_entry->d_tag) { |
|||
case DT_VERSYM: removed_name = "DT_VERSYM"; break; |
|||
case DT_VERNEEDED: removed_name = "DT_VERNEEDED"; break; |
|||
case DT_VERNEEDNUM: removed_name = "DT_VERNEEDNUM"; break; |
|||
case DT_VERDEF: removed_name = "DT_VERDEF"; break; |
|||
case DT_VERDEFNUM: removed_name = "DT_VERDEFNUM"; break; |
|||
case DT_RPATH: removed_name = "DT_RPATH"; break; |
|||
case DT_RUNPATH: removed_name = "DT_RUNPATH"; break; |
|||
} |
|||
if (removed_name != nullptr) { |
|||
printf("termux-elf-cleaner: Removing the %s dynamic section entry from '%s'\n", removed_name, file_name); |
|||
// Tag the entry with DT_NULL and put it last:
|
|||
dynamic_section_entry->d_tag = DT_NULL; |
|||
// Decrease j to process new entry index:
|
|||
std::swap(dynamic_section[j--], dynamic_section[last_nonnull_entry_idx--]); |
|||
} |
|||
} |
|||
} else if (section_header_entry->sh_type == SHT_GNU_verdef || |
|||
section_header_entry->sh_type == SHT_GNU_verneed || |
|||
section_header_entry->sh_type == SHT_GNU_versym) { |
|||
printf("termux-elf-cleaner: Removing version section from '%s'\n", file_name); |
|||
section_header_entry->sh_type = SHT_NULL; |
|||
} |
|||
} |
|||
return true; |
|||
} |
|||
|
|||
|
|||
int main(int argc, char const** argv) |
|||
{ |
|||
if (argc < 2 || (argc == 2 && strcmp(argv[1], "-h")==0)) { |
|||
fprintf(stderr, "usage: %s <filename>\n", argv[0]); |
|||
fprintf(stderr, " Processes ELF files to remove DT_VERNEEDED, DT_VERNEEDNUM, DT_RPATH\n" |
|||
" and DT_RUNPATH entries (which the Android linker warns about)\n"); |
|||
return 1; |
|||
} |
|||
|
|||
for (int i = 1; i < argc; i++) { |
|||
char const* file_name = argv[i]; |
|||
int fd = open(file_name, O_RDWR); |
|||
if (fd < 0) { |
|||
char* error_message; |
|||
if (asprintf(&error_message, "open(\"%s\")", file_name) == -1) error_message = (char*) "open()"; |
|||
perror(error_message); |
|||
return 1; |
|||
} |
|||
|
|||
struct stat st; |
|||
if (fstat(fd, &st) < 0) { perror("fstat()"); return 1; } |
|||
|
|||
if (st.st_size < (long long) sizeof(Elf32_Ehdr)) { |
|||
close(fd); |
|||
continue; |
|||
} |
|||
|
|||
void* mem = mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
|||
if (mem == MAP_FAILED) { perror("mmap()"); return 1; } |
|||
|
|||
uint8_t* bytes = reinterpret_cast<uint8_t*>(mem); |
|||
if (!(bytes[0] == 0x7F && bytes[1] == 'E' && bytes[2] == 'L' && bytes[3] == 'F')) { |
|||
// Not the ELF magic number.
|
|||
munmap(mem, st.st_size); |
|||
close(fd); |
|||
continue; |
|||
} |
|||
|
|||
if (bytes[/*EI_DATA*/5] != 1) { |
|||
fprintf(stderr, "termux-elf-cleaner: Not little endianness in '%s'\n", file_name); |
|||
munmap(mem, st.st_size); |
|||
close(fd); |
|||
continue; |
|||
} |
|||
|
|||
uint8_t const bit_value = bytes[/*EI_CLASS*/4]; |
|||
if (bit_value == 1) { |
|||
if (!process_elf<Elf32_Ehdr, Elf32_Shdr, Elf32_Dyn>(bytes, st.st_size, file_name)) return 1; |
|||
} else if (bit_value == 2) { |
|||
if (!process_elf<Elf64_Ehdr, Elf64_Shdr, Elf64_Dyn>(bytes, st.st_size, file_name)) return 1; |
|||
} else { |
|||
printf("termux-elf-cleaner: Incorrect bit value %d in '%s'\n", bit_value, file_name); |
|||
return 1; |
|||
} |
|||
|
|||
if (msync(mem, st.st_size, MS_SYNC) < 0) { perror("msync()"); return 1; } |
|||
|
|||
munmap(mem, st.st_size); |
|||
close(fd); |
|||
} |
|||
return 0; |
|||
} |
|||
|
Loading…
Reference in new issue