1/* Test program for elf_strptr function. 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#ifdef HAVE_CONFIG_H 19# include <config.h> 20#endif 21 22#include <errno.h> 23#include <fcntl.h> 24#include <inttypes.h> 25#include <stdio.h> 26#include <stdlib.h> 27#include <string.h> 28#include <unistd.h> 29 30#include ELFUTILS_HEADER(elf) 31#include <gelf.h> 32 33static void 34print_strings (Elf_Scn *scn, Elf *elf, size_t ndx) 35{ 36 GElf_Shdr shdr_mem; 37 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 38 39 printf ("Strings in section %zd (%s):\n", ndx, 40 ((shdr->sh_flags & SHF_COMPRESSED) != 0 41 ? "compressed" : "uncompressed")); 42 43 size_t off = 0; 44 const char *str = elf_strptr (elf, ndx, off); 45 while (str != NULL) 46 { 47 printf ("[%zx] '%s'\n", off, str); 48 off += strlen (str) + 1; 49 str = elf_strptr (elf, ndx, off); 50 } 51} 52 53int 54main (int argc, char *argv[]) 55{ 56 if (argc != 2) 57 { 58 printf ("No ELF file given as argument"); 59 exit (1); 60 } 61 62 const char *fname = argv[1]; 63 64 // Initialize libelf. 65 elf_version (EV_CURRENT); 66 67 /* Read the ELF from disk now. */ 68 int fd = open (fname, O_RDONLY); 69 if (fd == -1) 70 { 71 printf ("cannot open `%s' read-only: %s\n", fname, strerror (errno)); 72 exit (1); 73 } 74 75 Elf *elf = elf_begin (fd, ELF_C_READ, NULL); 76 if (elf == NULL) 77 { 78 printf ("cannot create ELF descriptor read-only: %s\n", elf_errmsg (-1)); 79 exit (1); 80 } 81 82 size_t ndx; 83 if (elf_getshdrstrndx (elf, &ndx) != 0) 84 { 85 printf ("cannot get section header table index: %s\n", elf_errmsg (-1)); 86 exit (1); 87 } 88 89 if (ndx == SHN_UNDEF) 90 { 91 printf ("ELF file `%s' doesn't have a section header table index", fname); 92 exit (1); 93 } 94 95 Elf_Scn *scn = elf_getscn (elf, ndx); 96 if (scn == NULL) 97 { 98 printf ("Couldn't get section %zd: %s\n", ndx, elf_errmsg (-1)); 99 exit (1); 100 } 101 102 if (elf_compress (scn, ELFCOMPRESS_ZLIB, 0) < 0) 103 { 104 printf ("Couldn't compress section %zd: %s\n", ndx, elf_errmsg (-1)); 105 exit (1); 106 } 107 print_strings (scn, elf, ndx); 108 109 if (elf_compress (scn, 0, 0) < 0) 110 { 111 printf ("Couldn't decompress section %zd: %s\n", ndx, elf_errmsg (-1)); 112 exit (1); 113 } 114 print_strings (scn, elf, ndx); 115 116 if (elf_end (elf) != 0) 117 { 118 printf ("failure in elf_end: %s\n", elf_errmsg (-1)); 119 exit (1); 120 } 121 122 close (fd); 123 124 return 0; 125} 126