| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#define _GNU_SOURCE |
| 21 |
|
| 22 |
#include <stdio.h> |
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
#include <unistd.h> |
| 26 |
#include <fcntl.h> |
| 27 |
#include <elf.h> |
| 28 |
#include <link.h> |
| 29 |
#include <getopt.h> |
| 30 |
#include <errno.h> |
| 31 |
#include <sys/stat.h> |
| 32 |
#include <sys/mman.h> |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#define REPORT(level, prefix, format, ...) \ |
| 39 |
do { \ |
| 40 |
fprintf(stderr, "hugeedit: " prefix ": " format, \ |
| 41 |
##__VA_ARGS__); \ |
| 42 |
} while (0) |
| 43 |
|
| 44 |
#include "libhugetlbfs_internal.h" |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#define MAP_BASE 0x1000 |
| 51 |
#define MAP_DISABLE 0x0001 |
| 52 |
#define MAP_TEXT 0x0002 |
| 53 |
#define MAP_DATA 0x0004 |
| 54 |
|
| 55 |
#define PF_LINUX_HUGETLB 0x100000 |
| 56 |
extern int optind; |
| 57 |
extern char *optarg; |
| 58 |
|
| 59 |
#define OPTION(opts, text) fprintf(stderr, " %-25s %s\n", opts, text) |
| 60 |
#define CONT(text) fprintf(stderr, " %-25s %s\n", "", text) |
| 61 |
|
| 62 |
void print_usage() |
| 63 |
{ |
| 64 |
fprintf(stderr, "hugeedit [options] target\n"); |
| 65 |
fprintf(stderr, "options:\n"); |
| 66 |
OPTION("--text", "Remap program text into huge pages by default"); |
| 67 |
OPTION("--data", "Remap program data into huge pages by default"); |
| 68 |
OPTION("--disable", "Remap no segments into huge pages by default"); |
| 69 |
OPTION("--help, -h", "Print this usage information"); |
| 70 |
} |
| 71 |
|
| 72 |
int check_elf_wordsize(void *ehdr) |
| 73 |
{ |
| 74 |
char *e_ident = (char *) ehdr; |
| 75 |
|
| 76 |
if (strncmp(e_ident, ELFMAG, SELFMAG)) { |
| 77 |
ERROR("Not a valid ELF executable\n"); |
| 78 |
exit(EXIT_FAILURE); |
| 79 |
} |
| 80 |
|
| 81 |
switch (e_ident[EI_CLASS]) { |
| 82 |
case ELFCLASS32: |
| 83 |
case ELFCLASS64: |
| 84 |
return e_ident[EI_CLASS]; |
| 85 |
default: |
| 86 |
ERROR("Can not determine word size\n"); |
| 87 |
exit(EXIT_FAILURE); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
#define elf_ph_end_offset(e) ((e)->e_phoff + (e)->e_phentsize * (e)->e_phnum) |
| 101 |
void check_remap_elf(void **elf, unsigned long *size, int wordsize) |
| 102 |
{ |
| 103 |
unsigned long newsize; |
| 104 |
int pagesize = getpagesize(); |
| 105 |
|
| 106 |
if (wordsize == ELFCLASS32) { |
| 107 |
Elf32_Ehdr *ehdr = *(Elf32_Ehdr **) elf; |
| 108 |
newsize = elf_ph_end_offset(ehdr); |
| 109 |
} else { |
| 110 |
Elf64_Ehdr *ehdr = *(Elf64_Ehdr **) elf; |
| 111 |
newsize = elf_ph_end_offset(ehdr); |
| 112 |
} |
| 113 |
newsize = ALIGN_UP(newsize, pagesize); |
| 114 |
|
| 115 |
if (newsize > *size) { |
| 116 |
*size = newsize; |
| 117 |
*elf = mremap(*elf, *size, newsize, MREMAP_MAYMOVE); |
| 118 |
if (*elf == MAP_FAILED) { |
| 119 |
ERROR("Remapping failed: %s\n", strerror(errno)); |
| 120 |
exit(EXIT_FAILURE); |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
#define is_text(p) ((((p)->p_flags & (PF_R|PF_W|PF_X)) == (PF_R|PF_X)) && \ |
| 126 |
((p)->p_memsz == (p)->p_filesz)) |
| 127 |
#if defined(__powerpc__) && !defined(__powerpc64__) |
| 128 |
#define is_data(p) (((p)->p_flags & (PF_R|PF_W|PF_X)) == (PF_R|PF_W|PF_X)) |
| 129 |
#else |
| 130 |
#define is_data(p) (((p)->p_flags & (PF_R|PF_W|PF_X)) == (PF_R|PF_W)) |
| 131 |
#endif |
| 132 |
|
| 133 |
#define update_phdrs(_BITS_) \ |
| 134 |
void update_phdrs##_BITS_(Elf##_BITS_##_Ehdr *ehdr, int remap_opts) \ |
| 135 |
{ \ |
| 136 |
int i; \ |
| 137 |
Elf##_BITS_##_Phdr *phdr; \ |
| 138 |
unsigned long long start, end; \ |
| 139 |
\ |
| 140 |
phdr = (Elf##_BITS_##_Phdr *)((char *)ehdr + ehdr->e_phoff); \ |
| 141 |
for (i = 0; i < ehdr->e_phnum; i++) { \ |
| 142 |
if (phdr[i].p_type != PT_LOAD) \ |
| 143 |
continue; \ |
| 144 |
if (remap_opts) \ |
| 145 |
phdr[i].p_flags &= ~PF_LINUX_HUGETLB; \ |
| 146 |
if ((remap_opts & MAP_TEXT) && is_text(&phdr[i])) \ |
| 147 |
phdr[i].p_flags |= PF_LINUX_HUGETLB; \ |
| 148 |
if ((remap_opts & MAP_DATA) && is_data(&phdr[i])) \ |
| 149 |
phdr[i].p_flags |= PF_LINUX_HUGETLB; \ |
| 150 |
start = (unsigned long long) phdr[i].p_vaddr; \ |
| 151 |
end = start + phdr[i].p_memsz; \ |
| 152 |
printf("Segment %i 0x%llx - 0x%llx (%s%s) default is " \ |
| 153 |
"%s pages\n", i, start, end, \ |
| 154 |
is_text(&phdr[i]) ? "TEXT" : "", \ |
| 155 |
is_data(&phdr[i]) ? "DATA" : "", \ |
| 156 |
(phdr[i].p_flags & PF_LINUX_HUGETLB) ? \ |
| 157 |
"HUGE" : "BASE"); \ |
| 158 |
} \ |
| 159 |
} |
| 160 |
update_phdrs(32) |
| 161 |
update_phdrs(64) |
| 162 |
|
| 163 |
int main(int argc, char ** argv) |
| 164 |
{ |
| 165 |
char opts[] = "+h"; |
| 166 |
struct option long_opts[] = { |
| 167 |
{"help", no_argument, NULL, 'h'}, |
| 168 |
{"disable", no_argument, NULL, MAP_BASE|MAP_DISABLE}, |
| 169 |
{"text", no_argument, NULL, MAP_BASE|MAP_TEXT}, |
| 170 |
{"data", no_argument, NULL, MAP_BASE|MAP_DATA}, |
| 171 |
{0}, |
| 172 |
}; |
| 173 |
int ret = 0, index = 0, remap_opts = 0; |
| 174 |
int fd; |
| 175 |
const char *target; |
| 176 |
void *ehdr; |
| 177 |
unsigned long mapsize = getpagesize(); |
| 178 |
int target_wordsize; |
| 179 |
|
| 180 |
while (ret != -1) { |
| 181 |
ret = getopt_long(argc, argv, opts, long_opts, &index); |
| 182 |
if (ret > 0 && (ret & MAP_BASE)) { |
| 183 |
remap_opts |= ret; |
| 184 |
continue; |
| 185 |
} |
| 186 |
switch (ret) { |
| 187 |
case '?': |
| 188 |
print_usage(); |
| 189 |
exit(EXIT_FAILURE); |
| 190 |
case 'h': |
| 191 |
print_usage(); |
| 192 |
exit(EXIT_SUCCESS); |
| 193 |
|
| 194 |
default: |
| 195 |
ret = -1; |
| 196 |
break; |
| 197 |
} |
| 198 |
} |
| 199 |
index = optind; |
| 200 |
remap_opts &= ~MAP_BASE; |
| 201 |
if (remap_opts & MAP_DISABLE && remap_opts != MAP_DISABLE) { |
| 202 |
ERROR("--disable is not compatible with --text or --data\n"); |
| 203 |
exit(EXIT_FAILURE); |
| 204 |
} |
| 205 |
|
| 206 |
if ((argc - index) != 1) { |
| 207 |
print_usage(); |
| 208 |
exit(EXIT_FAILURE); |
| 209 |
} |
| 210 |
target = argv[index]; |
| 211 |
|
| 212 |
|
| 213 |
fd = open(target, (remap_opts ? O_RDWR : O_RDONLY)); |
| 214 |
if (fd < 0) { |
| 215 |
ERROR("Opening %s failed: %s\n", target, strerror(errno)); |
| 216 |
exit(EXIT_FAILURE); |
| 217 |
} |
| 218 |
|
| 219 |
ehdr = mmap(NULL, mapsize, PROT_READ | (remap_opts ? PROT_WRITE : 0), |
| 220 |
MAP_SHARED, fd, 0); |
| 221 |
if (ehdr == MAP_FAILED) { |
| 222 |
ERROR("Mapping %s failed: %s\n", target, strerror(errno)); |
| 223 |
exit(EXIT_FAILURE); |
| 224 |
} |
| 225 |
|
| 226 |
target_wordsize = check_elf_wordsize(ehdr); |
| 227 |
check_remap_elf(&ehdr, &mapsize, target_wordsize); |
| 228 |
if (target_wordsize == ELFCLASS64) |
| 229 |
update_phdrs64((Elf64_Ehdr *) ehdr, remap_opts); |
| 230 |
else |
| 231 |
update_phdrs32((Elf32_Ehdr *) ehdr, remap_opts); |
| 232 |
|
| 233 |
if (munmap(ehdr, mapsize) != 0) { |
| 234 |
ERROR("Unmapping %s failed: %s\n", target, strerror(errno)); |
| 235 |
exit(EXIT_FAILURE); |
| 236 |
} |
| 237 |
if (close(fd) != 0) { |
| 238 |
ERROR("Final close of %s failed: %s -- possible data loss!\n", |
| 239 |
target, strerror(errno)); |
| 240 |
exit(EXIT_FAILURE); |
| 241 |
} |
| 242 |
|
| 243 |
exit(EXIT_SUCCESS); |
| 244 |
} |