1/* Test program for adding section and program headers and ehdr updates. 2 Copyright (C) 2015 Red Hat, Inc. 3 This file is part of elfutils. 4 5 This file is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 elfutils is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18#include <config.h> 19#include <assert.h> 20#include ELFUTILS_HEADER(elf) 21#include <gelf.h> 22 23#include <stdio.h> 24#include <stdlib.h> 25 26#include <sys/types.h> 27#include <sys/stat.h> 28#include <fcntl.h> 29#include <unistd.h> 30 31#include <stdbool.h> 32 33void 34check (const char *msg, bool statement) 35{ 36 if (! statement) 37 { 38 fprintf (stderr, "%s FAILED\n", msg); 39 exit (-1); 40 } 41 else 42 fprintf (stderr, "%s OK\n", msg); 43} 44 45void 46check_elf (const char *msg, bool statement) 47{ 48 if (! statement) 49 { 50 fprintf (stderr, "%s: %s\n", msg, elf_errmsg (-1)); 51 exit (-1); 52 } 53 else 54 fprintf (stderr, "%s OK\n", msg); 55} 56 57void 58test (Elf *elf, int class, bool layout) 59{ 60 fprintf (stderr, "testing ELF class: %d, layout: %d\n", class, layout); 61 62 check_elf ("gelf_newehdr", gelf_newehdr (elf, class) != 0); 63 check_elf ("gelf_getclass", gelf_getclass (elf) == class); 64 65 check_elf ("elf_flagelf", elf_flagelf (elf, layout ? ELF_C_SET : ELF_C_CLR, 66 ELF_F_LAYOUT) != 0); 67 68 GElf_Ehdr ehdr; 69 check_elf ("gelf_getehdr", gelf_getehdr (elf, &ehdr) != NULL); 70 check ("e_shnum == 0", ehdr.e_shnum == 0); 71 check ("e_phnum == 0", ehdr.e_phnum == 0); 72 check ("e_shoff == 0", ehdr.e_shoff == 0); 73 check ("e_phoff == 0", ehdr.e_phoff == 0); 74 75 size_t shnum; 76 check_elf ("elf_getshdrnum", elf_getshdrnum (elf, &shnum) == 0); 77 check ("shnum == 0", shnum == 0); 78 79 size_t phnum; 80 check_elf ("elf_getphdrnum", elf_getphdrnum (elf, &phnum) == 0); 81 check ("phnum == 0", phnum == 0); 82 83 /* Lets fill in some info we are always responsible for. */ 84 ehdr.e_ident[EI_DATA] = ELFDATANONE; /* Ask for native encoding. */ 85 ehdr.e_type = ET_EXEC; 86 ehdr.e_machine = EM_386; 87 ehdr.e_version = EV_NONE; /* Ask for current version. */ 88 check_elf ("gelf_update_ehdr", gelf_update_ehdr (elf, &ehdr) != 0); 89 90 check_elf ("elf_update", elf_update (elf, ELF_C_NULL) > 0); 91 92 check_elf ("gelf_getehdr", gelf_getehdr (elf, &ehdr) != NULL); 93 check ("EI_DATA", ehdr.e_ident[EI_DATA] != ELFDATANONE); 94 check ("e_version", ehdr.e_version == EV_CURRENT); 95 96 /* The sh/ph values shouldn't have changed. */ 97 check ("e_shnum == 0", ehdr.e_shnum == 0); 98 check ("e_phnum == 0", ehdr.e_phnum == 0); 99 check ("e_shoff == 0", ehdr.e_shoff == 0); 100 check ("e_phoff == 0", ehdr.e_phoff == 0); 101 102 check_elf ("elf_getshdrnum", elf_getshdrnum (elf, &shnum) == 0); 103 check ("shnum == 0", shnum == 0); 104 105 check_elf ("elf_getphdrnum", elf_getphdrnum (elf, &phnum) == 0); 106 check ("phnum == 0", phnum == 0); 107 108 /* Lets add a header. */ 109 check_elf ("elf_newscn", elf_newscn (elf) != NULL); 110 check_elf ("gelf_newphdr", gelf_newphdr (elf, 1) != 0); 111 112 /* If we are responsible for the layout ourselves we should also 113 tell where to put them. */ 114 if (layout) 115 { 116 check_elf ("gelf_getehdr", gelf_getehdr (elf, &ehdr) != NULL); 117 /* phdrs go right after the ehdr. */ 118 ehdr.e_phoff = ehdr.e_ehsize; 119 /* shdrs go right after the phdrs. */ 120 ehdr.e_shoff = ehdr.e_phoff + ehdr.e_phnum * ehdr.e_phentsize; 121 check_elf ("gelf_update_ehdr", gelf_update_ehdr (elf, &ehdr) != 0); 122 } 123 124 check_elf ("elf_update", elf_update (elf, ELF_C_NULL) > 0); 125 126 check_elf ("elf_getshdrnum", elf_getshdrnum (elf, &shnum) == 0); 127 check ("shnum == 1", shnum == 2); /* section zero is also created. */ 128 129 check_elf ("elf_getphdrnum", elf_getphdrnum (elf, &phnum) != 0); 130 check ("phnum == 1", phnum == 1); 131 132 check_elf ("gelf_getehdr", gelf_getehdr (elf, &ehdr) != NULL); 133 134 check ("EI_DATA", ehdr.e_ident[EI_DATA] != ELFDATANONE); 135 check ("e_version", ehdr.e_version == EV_CURRENT); 136 137 check ("e_shnum == 2", ehdr.e_shnum == 2); 138 check ("e_phnum == 1", ehdr.e_phnum == 1); 139 check ("e_shoff != 0", ehdr.e_shoff != 0); 140 check ("e_phoff != 0", ehdr.e_phoff != 0); 141 142 size_t shentsize = (class == ELFCLASS32 143 ? sizeof (Elf32_Shdr) : sizeof (Elf64_Shdr)); 144 check ("e_shentsize", ehdr.e_shentsize == shentsize); 145 size_t phentsize = (class == ELFCLASS32 146 ? sizeof (Elf32_Phdr) : sizeof (Elf64_Phdr)); 147 check ("e_phentsize", ehdr.e_phentsize == phentsize); 148} 149 150int 151main (int argc __attribute__ ((unused)), char **argv __attribute ((unused))) 152{ 153 elf_version (EV_CURRENT); 154 155 int fd = fd = open("/dev/zero", O_WRONLY); 156 check ("open", fd >= 0); 157 158 Elf *elf; 159 160 elf = elf_begin (fd, ELF_C_WRITE, NULL); 161 check_elf ("elf_begin", elf != NULL); 162 test (elf, ELFCLASS32, false); 163 elf_end (elf); 164 165 elf = elf_begin (fd, ELF_C_WRITE, NULL); 166 check_elf ("elf_begin", elf != NULL); 167 test (elf, ELFCLASS32, true); 168 elf_end (elf); 169 170 elf = elf_begin (fd, ELF_C_WRITE, NULL); 171 check_elf ("elf_begin", elf != NULL); 172 test (elf, ELFCLASS64, false); 173 elf_end (elf); 174 175 elf = elf_begin (fd, ELF_C_WRITE, NULL); 176 check_elf ("elf_begin", elf != NULL); 177 test (elf, ELFCLASS64, true); 178 elf_end (elf); 179 180 close (fd); 181 return 0; 182} 183