chiark / gitweb /
Add detect-hardlinks.sh script
[termux-packages] / packages / termux-tools / termux-elf-cleaner.cpp
1 #include <algorithm>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/mman.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10
11 #ifdef __APPLE__
12 # include "elf.h"
13 #else
14 # include <elf.h>
15 #endif
16
17 #define DT_VERNEEDED 0x6ffffffe
18 #define DT_VERNEEDNUM 0x6fffffff
19
20 template<typename ElfHeaderType /*Elf{32,64}_Ehdr*/,
21          typename ElfSectionHeaderType /*Elf{32,64}_Shdr*/,
22          typename ElfDynamicSectionEntryType /* Elf{32,64}_Dyn */>
23 bool process_elf(uint8_t* bytes, size_t elf_file_size)
24 {
25         if (sizeof(ElfSectionHeaderType) > elf_file_size) {
26                 fprintf(stderr, "ERROR: Elf header would end at %lu but file size only %lu\n", sizeof(ElfSectionHeaderType), elf_file_size);
27                 return false;
28         }
29         ElfHeaderType* elf_hdr = reinterpret_cast<ElfHeaderType*>(bytes);
30
31         size_t last_section_header_byte = elf_hdr->e_shoff + sizeof(ElfSectionHeaderType) * elf_hdr->e_shnum;
32         if (last_section_header_byte > elf_file_size) {
33                 fprintf(stderr, "ERROR: Section header would end at %lu but file size only %lu\n", last_section_header_byte, elf_file_size);
34                 return false;
35         }
36         ElfSectionHeaderType* section_header_table = reinterpret_cast<ElfSectionHeaderType*>(bytes + elf_hdr->e_shoff);
37
38         for (unsigned int i = 1; i < elf_hdr->e_shnum; i++) {
39                 ElfSectionHeaderType* section_header_entry = section_header_table + i;
40                 if (section_header_entry->sh_type == SHT_DYNAMIC) {
41                         size_t const last_dynamic_section_byte = section_header_entry->sh_offset + section_header_entry->sh_size;
42                         if (last_dynamic_section_byte > elf_file_size) {
43                                 fprintf(stderr, "ERROR: Dynamic section would end at %lu but file size only %lu\n", last_dynamic_section_byte, elf_file_size);
44                                 return false;
45                         }
46
47                         size_t const dynamic_section_entries = section_header_entry->sh_size / sizeof(ElfDynamicSectionEntryType);
48                         ElfDynamicSectionEntryType* const dynamic_section =
49                                 reinterpret_cast<ElfDynamicSectionEntryType*>(bytes + section_header_entry->sh_offset);
50
51                         unsigned int last_nonnull_entry_idx = 0;
52                         for (unsigned int j = dynamic_section_entries - 1; j > 0; j--) {
53                                 ElfDynamicSectionEntryType* dynamic_section_entry = dynamic_section + j;
54                                 if (dynamic_section_entry->d_tag != DT_NULL) {
55                                         last_nonnull_entry_idx = j;
56                                         break;
57                                 }
58                         }
59
60                         for (unsigned int j = 0; j < dynamic_section_entries; j++) {
61                                 ElfDynamicSectionEntryType* dynamic_section_entry = dynamic_section + j;
62                                 switch (dynamic_section_entry->d_tag) {
63                                         case DT_VERNEEDED:
64                                         case DT_VERNEEDNUM:
65                                         case DT_RPATH:
66                                         case DT_RUNPATH:
67                                                 char const* removed_name;
68                                                 switch (dynamic_section_entry->d_tag) {
69                                                         case DT_VERNEEDED: removed_name = "DT_VERNEEDED"; break;
70                                                         case DT_VERNEEDNUM: removed_name = "DT_VERNEEDNUM"; break;
71                                                         case DT_RPATH: removed_name = "DT_RPATH"; break;
72                                                         case DT_RUNPATH: removed_name = "DT_RUNPATH"; break;
73                                                 }
74                                                 printf("Removing the %s dynamic section entry\n", removed_name);
75                                                 // Tag the entry with DT_NULL and put it last:
76                                                 dynamic_section_entry->d_tag = DT_NULL;
77                                                 // Decrease j to process new entry index:
78                                                 std::swap(dynamic_section[j--], dynamic_section[last_nonnull_entry_idx--]);
79                                                 break;
80                                 }
81                         }
82                 }
83         }
84         return true;
85 }
86
87
88 int main(int argc, char **argv)
89 {
90         if (argc < 2 || (argc == 2 && strcmp(argv[1], "-h")==0)) {
91                 fprintf(stderr, "usage: %s <filename>\n", argv[0]);
92                 fprintf(stderr, "       Processes ELF files to remove DT_VERNEEDED, DT_VERNEEDNUM, DT_RPATH\n"
93                                 "       and DT_RUNPATH entries (which the Android linker warns about)\n");
94                 return 1;
95         }
96
97         for (int i = 1; i < argc; i++) {
98                 int fd = open(argv[i], O_RDWR);
99                 if (fd < 0) { perror("open()"); return 1; }
100
101                 struct stat st;
102                 if (fstat(fd, &st) < 0) { perror("fstat()"); return 1; }
103
104                 if (st.st_size < (long long) sizeof(Elf32_Ehdr)) continue;
105
106                 void* mem = mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
107                 if (mem == MAP_FAILED) { perror("mmap()"); return 1; }
108
109                 uint8_t* bytes = reinterpret_cast<uint8_t*>(mem);
110                 if (!(bytes[0] == 0x7F && bytes[1] == 'E' && bytes[2] == 'L' && bytes[3] == 'F')) {
111                         munmap(mem, st.st_size);
112                         close(fd);
113                         continue;
114                 }
115
116                 uint8_t bit_value = bytes[4];
117                 if (!(bit_value == 1 || bit_value == 2)) {
118                         printf("ERROR: Incorrect bit value: %d\n", bit_value);
119                         return 1;
120                 } else if (bit_value == 1) {
121                         if (!process_elf<Elf32_Ehdr, Elf32_Shdr, Elf32_Dyn>(bytes, st.st_size)) return 1;
122                 } else {
123                         if (!process_elf<Elf64_Ehdr, Elf64_Shdr, Elf64_Dyn>(bytes, st.st_size)) return 1;
124                 }
125
126                 if (msync(mem, st.st_size, MS_SYNC) < 0) { perror("msync()"); return 1; }
127
128                 munmap(mem, st.st_size);
129                 close(fd);
130         }
131         return 0;
132 }
133