OLD | NEW |
(Empty) | |
| 1 /* Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 * Use of this source code is governed by a BSD-style license that can be |
| 3 * found in the LICENSE file. |
| 4 * |
| 5 * This is a trivial program to edit an ELF file in place, making |
| 6 * one crucial modification to a program header. It's invoked: |
| 7 * bootstrap_phdr_hacker FILENAME SEGMENT_NUMBER |
| 8 * where SEGMENT_NUMBER is the zero-origin index of the program header |
| 9 * we'll touch. This is a PT_LOAD with p_filesz of zero. We change its |
| 10 * p_filesz to match its p_memsz value. |
| 11 */ |
| 12 |
| 13 #include <errno.h> |
| 14 #include <error.h> |
| 15 #include <fcntl.h> |
| 16 #include <gelf.h> |
| 17 #include <libelf.h> |
| 18 #include <stdlib.h> |
| 19 #include <unistd.h> |
| 20 |
| 21 int main(int argc, char **argv) { |
| 22 if (argc != 3) |
| 23 error(1, 0, "Usage: %s FILENAME SEGMENT_NUMBER", argv[0]); |
| 24 |
| 25 const char *const file = argv[1]; |
| 26 const int segment = atoi(argv[2]); |
| 27 |
| 28 int fd = open(file, O_RDWR); |
| 29 if (fd < 0) |
| 30 error(2, errno, "Cannot open %s for read/write", file); |
| 31 |
| 32 if (elf_version(EV_CURRENT) == EV_NONE) |
| 33 error(2, 0, "elf_version: %s", elf_errmsg(-1)); |
| 34 |
| 35 Elf *elf = elf_begin(fd, ELF_C_RDWR, NULL); |
| 36 if (elf == NULL) |
| 37 error(2, 0, "elf_begin: %s", elf_errmsg(-1)); |
| 38 |
| 39 if (elf_flagelf(elf, ELF_C_SET, ELF_F_LAYOUT) == 0) |
| 40 error(2, 0, "elf_flagelf: %s", elf_errmsg(-1)); |
| 41 |
| 42 GElf_Phdr phdr; |
| 43 GElf_Phdr *ph = gelf_getphdr(elf, segment, &phdr); |
| 44 if (ph == NULL) |
| 45 error(2, 0, "gelf_getphdr: %s", elf_errmsg(-1)); |
| 46 |
| 47 if (ph->p_type != PT_LOAD) |
| 48 error(3, 0, "Program header %d is %u, not PT_LOAD", |
| 49 segment, (unsigned int) ph->p_type); |
| 50 if (ph->p_filesz != 0) |
| 51 error(3, 0, "Program header %d has nonzero p_filesz", segment); |
| 52 |
| 53 ph->p_filesz = ph->p_memsz; |
| 54 if (gelf_update_phdr(elf, segment, ph) == 0) |
| 55 error(2, 0, "gelf_update_phdr: %s", elf_errmsg(-1)); |
| 56 |
| 57 if (elf_flagphdr(elf, ELF_C_SET, ELF_F_DIRTY) == 0) |
| 58 error(2, 0, "elf_flagphdr: %s", elf_errmsg(-1)); |
| 59 |
| 60 if (elf_update(elf, ELF_C_WRITE) < 0) |
| 61 error(2, 0, "elf_update: %s", elf_errmsg(-1)); |
| 62 |
| 63 close(fd); |
| 64 |
| 65 return 0; |
| 66 } |
OLD | NEW |