• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Test program for elf_update function.
2    Copyright (C) 2000, 2002, 2005, 2016 Red Hat, Inc.
3    This file is part of elfutils.
4    Written by Ulrich Drepper <drepper@redhat.com>, 2000.
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 <errno.h>
24 #include <fcntl.h>
25 #include <libelf.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include "system.h"
31 
32 #include ELFUTILS_HEADER(dwelf)
33 
34 
35 int
main(int argc,char * argv[])36 main (int argc, char *argv[] __attribute__ ((unused)))
37 {
38   const char *fname = "xxx_update3";
39   int fd;
40   Elf *elf;
41   Elf32_Ehdr *ehdr;
42   Elf32_Phdr *phdr;
43   Elf_Scn *scn;
44   Elf32_Shdr *shdr;
45   Elf_Data *data;
46   Dwelf_Strtab *shst;
47   Dwelf_Strent *shstrtabse;
48   int i;
49 
50   fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE);
51   if (fd == -1)
52     {
53       printf ("cannot open `%s': %s\n", fname, strerror (errno));
54       exit (1);
55     }
56 
57   elf_version (EV_CURRENT);
58 
59   elf_fill (0x42);
60 
61   elf = elf_begin (fd, ELF_C_WRITE, NULL);
62   if (elf == NULL)
63     {
64       printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
65       exit (1);
66     }
67 
68   /* Create an ELF header.  */
69   ehdr = elf32_newehdr (elf);
70   if (ehdr == NULL)
71     {
72       printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
73       exit (1);
74     }
75 
76   /* Print the ELF header values.  */
77   if (argc > 1)
78     {
79       for (i = 0; i < EI_NIDENT; ++i)
80 	printf (" %02x", ehdr->e_ident[i]);
81       printf ("\
82 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
83 	      "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
84 	      "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
85 	      ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
86 	      ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
87 	      ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
88 	      ehdr->e_shnum, ehdr->e_shstrndx);
89     }
90 
91   ehdr->e_ident[0] = 42;
92   ehdr->e_ident[4] = 1;
93   ehdr->e_ident[5] = 1;
94   ehdr->e_ident[6] = 2;
95   ehdr->e_type = ET_EXEC;
96   ehdr->e_version = 1;
97   ehdr->e_ehsize = 1;
98   elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
99 
100   /* Create the program header.  */
101   phdr = elf32_newphdr (elf, 1);
102   if (phdr == NULL)
103     {
104       printf ("cannot create program header: %s\n", elf_errmsg (-1));
105       exit (1);
106     }
107 
108   phdr[0].p_type = PT_PHDR;
109   elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
110 
111   shst = dwelf_strtab_init (true);
112 
113   scn = elf_newscn (elf);
114   if (scn == NULL)
115     {
116       printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
117       exit (1);
118     }
119   shdr = elf32_getshdr (scn);
120   if (shdr == NULL)
121     {
122       printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
123       exit (1);
124     }
125 
126   shstrtabse = dwelf_strtab_add (shst, ".shstrtab");
127 
128   shdr->sh_type = SHT_STRTAB;
129   shdr->sh_flags = 0;
130   shdr->sh_addr = 0;
131   shdr->sh_link = SHN_UNDEF;
132   shdr->sh_info = SHN_UNDEF;
133   shdr->sh_addralign = 1;
134   shdr->sh_entsize = 0;
135 
136   /* We have to store the section index in the ELF header.  */
137   ehdr->e_shstrndx = elf_ndxscn (scn);
138 
139   data = elf_newdata (scn);
140   if (data == NULL)
141     {
142       printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
143       exit (1);
144     }
145 
146   /* No more sections, finalize the section header string table.  */
147   dwelf_strtab_finalize (shst, data);
148 
149   shdr->sh_name = dwelf_strent_off (shstrtabse);
150 
151   /* Let the library compute the internal structure information.  */
152   if (elf_update (elf, ELF_C_NULL) < 0)
153     {
154       printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
155       exit (1);
156     }
157 
158   ehdr = elf32_getehdr (elf);
159 
160   phdr[0].p_offset = ehdr->e_phoff;
161   phdr[0].p_offset = ehdr->e_phoff;
162   phdr[0].p_vaddr = ehdr->e_phoff;
163   phdr[0].p_paddr = ehdr->e_phoff;
164   phdr[0].p_flags = PF_R | PF_X;
165   phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
166   phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
167   phdr[0].p_align = sizeof (Elf32_Word);
168 
169   /* Write out the file.  */
170   if (elf_update (elf, ELF_C_WRITE) < 0)
171     {
172       printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
173       exit (1);
174     }
175 
176   /* We don't need the string table anymore.  */
177   dwelf_strtab_free (shst);
178 
179   /* And the data allocated in the .shstrtab section.  */
180   free (data->d_buf);
181 
182   /* Print the ELF header values.  */
183   if (argc > 1)
184     {
185       for (i = 0; i < EI_NIDENT; ++i)
186 	printf (" %02x", ehdr->e_ident[i]);
187       printf ("\
188 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
189 	      "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
190 	      "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
191 	      ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
192 	      ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
193 	      ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
194 	      ehdr->e_shnum, ehdr->e_shstrndx);
195     }
196 
197   if (elf_end (elf) != 0)
198     {
199       printf ("failure in elf_end: %s\n", elf_errmsg (-1));
200       exit (1);
201     }
202 
203   unlink (fname);
204 
205   return 0;
206 }
207