Brian Silverman | 8649792 | 2018-02-10 19:28:39 -0500 | [diff] [blame] | 1 | /* Create an ELF file with all the DT_* flags set. |
| 2 | Copyright (C) 2011, 2016 Red Hat, Inc. |
| 3 | This file is part of elfutils. |
| 4 | Written by Marek Polacek <mpolacek@redhat.com>, 2011. |
| 5 | |
| 6 | This file is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by |
| 8 | the Free Software Foundation; either version 3 of the License, or |
| 9 | (at your option) any later version. |
| 10 | |
| 11 | elfutils is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | # include <config.h> |
| 21 | #endif |
| 22 | |
| 23 | #include ELFUTILS_HEADER(dwelf) |
| 24 | #include <elf.h> |
| 25 | #include <gelf.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <libelf.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdio_ext.h> |
| 30 | #include <stdint.h> |
| 31 | #include <stdbool.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <unistd.h> |
| 35 | |
| 36 | |
| 37 | int |
| 38 | main (void) |
| 39 | { |
| 40 | static const char fname[] = "testfile-alldts"; |
| 41 | Dwelf_Strtab *shst; |
| 42 | Dwelf_Strent *dynscn; |
| 43 | Dwelf_Strent *shstrtabse; |
| 44 | const Elf32_Sword dtflags[] = |
| 45 | { |
| 46 | DT_NULL, DT_NEEDED, DT_PLTRELSZ, DT_PLTGOT, |
| 47 | DT_HASH, DT_STRTAB, DT_SYMTAB, DT_RELA, |
| 48 | DT_RELASZ, DT_RELAENT, DT_STRSZ, DT_SYMENT, |
| 49 | DT_INIT, DT_FINI, DT_SONAME, DT_RPATH, |
| 50 | DT_SYMBOLIC, DT_REL, DT_RELSZ, DT_RELENT, |
| 51 | DT_PLTREL, DT_DEBUG, DT_TEXTREL, DT_JMPREL, |
| 52 | DT_BIND_NOW, DT_INIT_ARRAY, DT_FINI_ARRAY, |
| 53 | DT_INIT_ARRAYSZ, DT_FINI_ARRAYSZ, DT_RUNPATH, |
| 54 | DT_FLAGS, DT_ENCODING, DT_PREINIT_ARRAY, |
| 55 | DT_PREINIT_ARRAYSZ, DT_VERSYM, DT_GNU_PRELINKED, |
| 56 | DT_GNU_CONFLICTSZ, DT_GNU_LIBLISTSZ, DT_CHECKSUM, |
| 57 | DT_PLTPADSZ, DT_MOVEENT, DT_MOVESZ, DT_FEATURE_1, |
| 58 | DT_POSFLAG_1, DT_SYMINSZ, DT_SYMINENT, DT_GNU_HASH, |
| 59 | DT_TLSDESC_PLT, DT_TLSDESC_GOT, DT_GNU_CONFLICT, |
| 60 | DT_GNU_LIBLIST, DT_CONFIG, DT_DEPAUDIT, DT_AUDIT, |
| 61 | DT_PLTPAD, DT_MOVETAB, DT_SYMINFO, DT_RELACOUNT, |
| 62 | DT_RELCOUNT, DT_FLAGS_1, DT_VERDEF, DT_VERDEFNUM, |
| 63 | DT_VERNEED, DT_VERNEEDNUM, DT_AUXILIARY, DT_FILTER |
| 64 | }; |
| 65 | const int ndtflags = sizeof (dtflags) / sizeof (dtflags[0]); |
| 66 | |
| 67 | /* We use no threads here which can interfere with handling a stream. */ |
| 68 | (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER); |
| 69 | |
| 70 | /* Open the file. */ |
| 71 | int fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666); |
| 72 | if (fd == -1) |
| 73 | { |
| 74 | printf ("cannot open `%s': %m\n", fname); |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | /* Tell the library which version are we expecting. */ |
| 79 | elf_version (EV_CURRENT); |
| 80 | |
| 81 | /* Create an ELF descriptor. */ |
| 82 | Elf *elf = elf_begin (fd, ELF_C_WRITE, NULL); |
| 83 | if (elf == NULL) |
| 84 | { |
| 85 | printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1)); |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | /* Create an ELF header. */ |
| 90 | Elf32_Ehdr *ehdr = elf32_newehdr (elf); |
| 91 | if (ehdr == NULL) |
| 92 | { |
| 93 | printf ("cannot create ELF header: %s\n", elf_errmsg (-1)); |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | ehdr->e_ident[0] = 42; |
| 98 | ehdr->e_ident[5] = 1; |
| 99 | ehdr->e_ident[6] = 2; |
| 100 | ehdr->e_type = ET_EXEC; |
| 101 | ehdr->e_machine = EM_386; |
| 102 | ehdr->e_version = 1; |
| 103 | ehdr->e_ehsize = 1; |
| 104 | ehdr->e_shnum = 3; |
| 105 | |
| 106 | elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY); |
| 107 | |
| 108 | /* Create the program headers. */ |
| 109 | Elf32_Phdr *phdr = elf32_newphdr (elf, 2); |
| 110 | if (phdr == NULL) |
| 111 | { |
| 112 | printf ("cannot create program headers: %s\n", elf_errmsg (-1)); |
| 113 | return 1; |
| 114 | } |
| 115 | |
| 116 | phdr[0].p_type = PT_PHDR; |
| 117 | phdr[1].p_type = PT_DYNAMIC; |
| 118 | |
| 119 | elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY); |
| 120 | shst = dwelf_strtab_init (true); |
| 121 | |
| 122 | /* Create the .dynamic section. */ |
| 123 | Elf_Scn *scn = elf_newscn (elf); |
| 124 | if (scn == NULL) |
| 125 | { |
| 126 | printf ("cannot create DYNAMIC section: %s\n", elf_errmsg (-1)); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | Elf32_Shdr *shdr = elf32_getshdr (scn); |
| 131 | if (shdr == NULL) |
| 132 | { |
| 133 | printf ("cannot get header for DYNAMIC section: %s\n", elf_errmsg (-1)); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | dynscn = dwelf_strtab_add (shst, ".dynamic"); |
| 138 | |
| 139 | /* We'll need to know the section offset. But this will be set up |
| 140 | by elf_update later, so for now just store the address. */ |
| 141 | const Elf32_Off *const dynscn_offset = &shdr->sh_offset; |
| 142 | shdr->sh_type = SHT_DYNAMIC; |
| 143 | shdr->sh_flags = SHF_ALLOC | SHF_WRITE; |
| 144 | shdr->sh_link = SHN_UNDEF; |
| 145 | shdr->sh_info = SHN_UNDEF; |
| 146 | /* This section will start here. */ |
| 147 | shdr->sh_addr = 0x1a0; |
| 148 | |
| 149 | /* Create new section data. */ |
| 150 | Elf_Data *data = elf_newdata (scn); |
| 151 | if (data == NULL) |
| 152 | { |
| 153 | printf ("cannot create data for DYNAMIC section: %s\n", elf_errmsg (-1)); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | /* Allocate memory for all the .dynamic entries. */ |
| 158 | Elf32_Dyn *dyn = malloc (ndtflags * sizeof (Elf32_Dyn)); |
| 159 | if (dyn == NULL) |
| 160 | { |
| 161 | printf ("malloc failed: %m\n"); |
| 162 | return 1; |
| 163 | } |
| 164 | |
| 165 | /* Now write all the DT_* flags. */ |
| 166 | for (int i = 0; i < ndtflags; ++i) |
| 167 | { |
| 168 | dyn[i].d_tag = dtflags[i]; |
| 169 | dyn[i].d_un.d_val = 0xdeadbeef; |
| 170 | } |
| 171 | |
| 172 | /* Set the pointer to allocated memory. */ |
| 173 | data->d_buf = dyn; |
| 174 | data->d_type = ELF_T_DYN; |
| 175 | data->d_version = EV_CURRENT; |
| 176 | data->d_size = ndtflags * sizeof (Elf32_Dyn); |
| 177 | data->d_align = 0x8; |
| 178 | |
| 179 | /* Create .shstrtab section. */ |
| 180 | scn = elf_newscn (elf); |
| 181 | if (scn == NULL) |
| 182 | { |
| 183 | printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1)); |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | shdr = elf32_getshdr (scn); |
| 188 | if (shdr == NULL) |
| 189 | { |
| 190 | printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1)); |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | shstrtabse = dwelf_strtab_add (shst, ".shstrtab"); |
| 195 | |
| 196 | shdr->sh_type = SHT_STRTAB; |
| 197 | shdr->sh_flags = 0; |
| 198 | shdr->sh_addr = 0; |
| 199 | shdr->sh_link = SHN_UNDEF; |
| 200 | shdr->sh_info = SHN_UNDEF; |
| 201 | shdr->sh_entsize = 1; |
| 202 | |
| 203 | /* We have to store the section index in the ELF header. */ |
| 204 | ehdr->e_shstrndx = elf_ndxscn (scn); |
| 205 | |
| 206 | data = elf_newdata (scn); |
| 207 | if (data == NULL) |
| 208 | { |
| 209 | printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1)); |
| 210 | return 1; |
| 211 | } |
| 212 | |
| 213 | /* No more sections, finalize the section header string table. */ |
| 214 | dwelf_strtab_finalize (shst, data); |
| 215 | |
| 216 | elf32_getshdr (elf_getscn (elf, 1))->sh_name = dwelf_strent_off (dynscn); |
| 217 | shdr->sh_name = dwelf_strent_off (shstrtabse); |
| 218 | |
| 219 | /* Let the library compute the internal structure information. */ |
| 220 | if (elf_update (elf, ELF_C_NULL) < 0) |
| 221 | { |
| 222 | printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1)); |
| 223 | return 1; |
| 224 | } |
| 225 | |
| 226 | ehdr = elf32_getehdr (elf); |
| 227 | |
| 228 | phdr[0].p_offset = ehdr->e_phoff; |
| 229 | phdr[0].p_vaddr = ehdr->e_phoff; |
| 230 | phdr[0].p_paddr = ehdr->e_phoff; |
| 231 | phdr[0].p_flags = PF_R | PF_X; |
| 232 | phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT); |
| 233 | phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT); |
| 234 | phdr[0].p_align = sizeof (Elf32_Word); |
| 235 | |
| 236 | phdr[1].p_flags = PF_W | PF_R; |
| 237 | phdr[1].p_offset = *dynscn_offset; |
| 238 | /* Set up the start of this segment to equal start address of the |
| 239 | .dynamic section. */ |
| 240 | phdr[1].p_vaddr = 0x1a0; |
| 241 | phdr[1].p_paddr = 0x1a0; |
| 242 | phdr[1].p_align = 2 * sizeof (Elf32_Word); |
| 243 | phdr[1].p_filesz = ndtflags * sizeof (Elf32_Dyn); |
| 244 | phdr[1].p_memsz = ndtflags * sizeof (Elf32_Dyn); |
| 245 | |
| 246 | /* Write out the file. */ |
| 247 | if (elf_update (elf, ELF_C_WRITE) < 0) |
| 248 | { |
| 249 | printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1)); |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | /* We don't need the string table anymore. */ |
| 254 | dwelf_strtab_free (shst); |
| 255 | |
| 256 | /* And the data allocated in the .shstrtab section. */ |
| 257 | free (data->d_buf); |
| 258 | |
| 259 | /* And the dynamic entries. */ |
| 260 | free (dyn); |
| 261 | |
| 262 | /* All done. */ |
| 263 | if (elf_end (elf) != 0) |
| 264 | { |
| 265 | printf ("failure in elf_end: %s\n", elf_errmsg (-1)); |
| 266 | return 1; |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |