blob: d760687a08614552fc7cb1f5e538464147d8ed9b [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Test program for elf_update function.
2 Copyright (C) 2000, 2002, 2005 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000010
Mark Wielaardde2ed972012-06-05 17:15:16 +020011 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000012 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000015
Mark Wielaardde2ed972012-06-05 17:15:16 +020016 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000018
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>
Mark Wielaardb0f202e2013-04-28 00:54:17 +020029#include <unistd.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000030
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000031#include ELFUTILS_HEADER(ebl)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000032
33
34int
35main (int argc, char *argv[] __attribute__ ((unused)))
36{
Mark Wielaardb0f202e2013-04-28 00:54:17 +020037 const char *fname = "xxx_update3";
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000038 int fd;
39 Elf *elf;
40 Elf32_Ehdr *ehdr;
41 Elf32_Phdr *phdr;
42 Elf_Scn *scn;
43 Elf32_Shdr *shdr;
44 Elf_Data *data;
45 struct Ebl_Strtab *shst;
46 struct Ebl_Strent *shstrtabse;
47 int i;
48
49 fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
50 if (fd == -1)
51 {
52 printf ("cannot open `%s': %s\n", fname, strerror (errno));
53 exit (1);
54 }
55
56 elf_version (EV_CURRENT);
57
58 elf_fill (0x42);
59
60 elf = elf_begin (fd, ELF_C_WRITE, NULL);
61 if (elf == NULL)
62 {
63 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
64 exit (1);
65 }
66
67 /* Create an ELF header. */
68 ehdr = elf32_newehdr (elf);
69 if (ehdr == NULL)
70 {
71 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
72 exit (1);
73 }
74
75 /* Print the ELF header values. */
76 if (argc > 1)
77 {
78 for (i = 0; i < EI_NIDENT; ++i)
79 printf (" %02x", ehdr->e_ident[i]);
80 printf ("\
81\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
82 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
83 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
84 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
85 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
86 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
87 ehdr->e_shnum, ehdr->e_shstrndx);
88 }
89
90 ehdr->e_ident[0] = 42;
91 ehdr->e_ident[4] = 1;
92 ehdr->e_ident[5] = 1;
93 ehdr->e_ident[6] = 2;
94 ehdr->e_type = ET_EXEC;
95 ehdr->e_version = 1;
96 ehdr->e_ehsize = 1;
97 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
98
99 /* Create the program header. */
100 phdr = elf32_newphdr (elf, 1);
101 if (phdr == NULL)
102 {
103 printf ("cannot create program header: %s\n", elf_errmsg (-1));
104 exit (1);
105 }
106
107 phdr[0].p_type = PT_PHDR;
108 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
109
110 shst = ebl_strtabinit (true);
111
112 scn = elf_newscn (elf);
113 if (scn == NULL)
114 {
115 printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
116 exit (1);
117 }
118 shdr = elf32_getshdr (scn);
119 if (shdr == NULL)
120 {
121 printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
122 exit (1);
123 }
124
125 shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0);
126
127 shdr->sh_type = SHT_STRTAB;
128 shdr->sh_flags = 0;
129 shdr->sh_addr = 0;
130 shdr->sh_link = SHN_UNDEF;
131 shdr->sh_info = SHN_UNDEF;
132 shdr->sh_addralign = 1;
133 shdr->sh_entsize = 0;
134
135 /* We have to store the section index in the ELF header. */
136 ehdr->e_shstrndx = elf_ndxscn (scn);
137
138 data = elf_newdata (scn);
139 if (data == NULL)
140 {
141 printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
142 exit (1);
143 }
144
145 /* No more sections, finalize the section header string table. */
146 ebl_strtabfinalize (shst, data);
147
148 shdr->sh_name = ebl_strtaboffset (shstrtabse);
149
150 /* Let the library compute the internal structure information. */
151 if (elf_update (elf, ELF_C_NULL) < 0)
152 {
153 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
154 exit (1);
155 }
156
157 ehdr = elf32_getehdr (elf);
158
159 phdr[0].p_offset = ehdr->e_phoff;
160 phdr[0].p_offset = ehdr->e_phoff;
161 phdr[0].p_vaddr = ehdr->e_phoff;
162 phdr[0].p_paddr = ehdr->e_phoff;
163 phdr[0].p_flags = PF_R | PF_X;
164 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
165 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
166 phdr[0].p_align = sizeof (Elf32_Word);
167
168 /* Write out the file. */
169 if (elf_update (elf, ELF_C_WRITE) < 0)
170 {
171 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
172 exit (1);
173 }
174
175 /* We don't need the string table anymore. */
176 ebl_strtabfree (shst);
177
178 /* And the data allocated in the .shstrtab section. */
179 free (data->d_buf);
180
181 /* Print the ELF header values. */
182 if (argc > 1)
183 {
184 for (i = 0; i < EI_NIDENT; ++i)
185 printf (" %02x", ehdr->e_ident[i]);
186 printf ("\
187\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
188 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
189 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
190 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
191 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
192 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
193 ehdr->e_shnum, ehdr->e_shstrndx);
194 }
195
196 if (elf_end (elf) != 0)
197 {
198 printf ("failure in elf_end: %s\n", elf_errmsg (-1));
199 exit (1);
200 }
201
Mark Wielaardb0f202e2013-04-28 00:54:17 +0200202 unlink (fname);
203
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000204 return 0;
205}