1 /* Test program for elf_update function.
2 Copyright (C) 2000, 2001, 2002, 2005 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
6 Red Hat elfutils is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by the
8 Free Software Foundation; version 2 of the License.
10 Red Hat 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 GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with Red Hat elfutils; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
19 Red Hat elfutils is an included package of the Open Invention Network.
20 An included package of the Open Invention Network is a package for which
21 Open Invention Network licensees cross-license their patents. No patent
22 license is granted, either expressly or impliedly, by designation as an
23 included package. Should you wish to participate in the Open Invention
24 Network licensing program, please visit www.openinventionnetwork.com
25 <http://www.openinventionnetwork.com>. */
39 #include ELFUTILS_HEADER(ebl)
43 main (int argc, char *argv[] __attribute__ ((unused)))
45 const char fname[] = "xxx";
53 struct Ebl_Strtab *shst;
54 struct Ebl_Strent *firstse;
55 struct Ebl_Strent *secondse;
56 struct Ebl_Strent *thirdse;
57 struct Ebl_Strent *fourthse;
58 struct Ebl_Strent *shstrtabse;
61 fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
64 printf ("cannot open `%s': %s\n", fname, strerror (errno));
68 elf_version (EV_CURRENT);
72 elf = elf_begin (fd, ELF_C_WRITE, NULL);
75 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
79 /* Create an ELF header. */
80 ehdr = elf32_newehdr (elf);
83 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
87 /* Print the ELF header values. */
90 for (i = 0; i < EI_NIDENT; ++i)
91 printf (" %02x", ehdr->e_ident[i]);
93 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
94 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
95 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
96 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
97 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
98 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
99 ehdr->e_shnum, ehdr->e_shstrndx);
102 ehdr->e_ident[0] = 42;
103 ehdr->e_ident[4] = 1;
104 ehdr->e_ident[5] = 1;
105 ehdr->e_ident[6] = 2;
106 ehdr->e_type = ET_EXEC;
109 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
111 /* Create the program header. */
112 phdr = elf32_newphdr (elf, 1);
115 printf ("cannot create program header: %s\n", elf_errmsg (-1));
119 phdr[0].p_type = PT_PHDR;
120 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
122 shst = ebl_strtabinit (true);
124 scn = elf_newscn (elf);
127 printf ("cannot create first section: %s\n", elf_errmsg (-1));
130 shdr = elf32_getshdr (scn);
133 printf ("cannot get header for first section: %s\n", elf_errmsg (-1));
137 firstse = ebl_strtabadd (shst, ".first", 0);
139 shdr->sh_type = SHT_PROGBITS;
140 shdr->sh_flags = SHF_ALLOC | SHF_EXECINSTR;
144 shdr->sh_entsize = 1;
146 data = elf_newdata (scn);
149 printf ("cannot create data first section: %s\n", elf_errmsg (-1));
153 data->d_buf = "hello";
154 data->d_type = ELF_T_BYTE;
155 data->d_version = EV_CURRENT;
160 scn = elf_newscn (elf);
163 printf ("cannot create second section: %s\n", elf_errmsg (-1));
166 shdr = elf32_getshdr (scn);
169 printf ("cannot get header for second section: %s\n", elf_errmsg (-1));
173 secondse = ebl_strtabadd (shst, ".second", 0);
175 shdr->sh_type = SHT_PROGBITS;
176 shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
180 shdr->sh_entsize = 1;
182 data = elf_newdata (scn);
185 printf ("cannot create data second section: %s\n", elf_errmsg (-1));
189 data->d_buf = "world";
190 data->d_type = ELF_T_BYTE;
191 data->d_version = EV_CURRENT;
196 scn = elf_newscn (elf);
199 printf ("cannot create third section: %s\n", elf_errmsg (-1));
202 shdr = elf32_getshdr (scn);
205 printf ("cannot get header for third section: %s\n", elf_errmsg (-1));
209 thirdse = ebl_strtabadd (shst, ".third", 0);
211 shdr->sh_type = SHT_PROGBITS;
212 shdr->sh_flags = SHF_ALLOC | SHF_EXECINSTR;
216 shdr->sh_entsize = 1;
218 data = elf_newdata (scn);
221 printf ("cannot create data third section: %s\n", elf_errmsg (-1));
225 data->d_buf = "!!!!!";
226 data->d_type = ELF_T_BYTE;
227 data->d_version = EV_CURRENT;
232 scn = elf_newscn (elf);
235 printf ("cannot create fourth section: %s\n", elf_errmsg (-1));
238 shdr = elf32_getshdr (scn);
241 printf ("cannot get header for fourth section: %s\n", elf_errmsg (-1));
245 fourthse = ebl_strtabadd (shst, ".fourth", 0);
247 shdr->sh_type = SHT_NOBITS;
248 shdr->sh_flags = SHF_ALLOC | SHF_EXECINSTR;
252 shdr->sh_entsize = 1;
255 data = elf_newdata (scn);
258 printf ("cannot create data fourth section: %s\n", elf_errmsg (-1));
263 data->d_type = ELF_T_BYTE;
264 data->d_version = EV_CURRENT;
269 scn = elf_newscn (elf);
272 printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
275 shdr = elf32_getshdr (scn);
278 printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
282 shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0);
284 shdr->sh_type = SHT_STRTAB;
287 shdr->sh_link = SHN_UNDEF;
288 shdr->sh_info = SHN_UNDEF;
289 shdr->sh_entsize = 1;
291 /* We have to store the section index in the ELF header. */
292 ehdr->e_shstrndx = elf_ndxscn (scn);
294 data = elf_newdata (scn);
297 printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
301 /* No more sections, finalize the section header string table. */
302 ebl_strtabfinalize (shst, data);
304 elf32_getshdr (elf_getscn (elf, 1))->sh_name = ebl_strtaboffset (firstse);
305 elf32_getshdr (elf_getscn (elf, 2))->sh_name = ebl_strtaboffset (secondse);
306 elf32_getshdr (elf_getscn (elf, 3))->sh_name = ebl_strtaboffset (thirdse);
307 elf32_getshdr (elf_getscn (elf, 4))->sh_name = ebl_strtaboffset (fourthse);
308 shdr->sh_name = ebl_strtaboffset (shstrtabse);
310 /* Let the library compute the internal structure information. */
311 if (elf_update (elf, ELF_C_NULL) < 0)
313 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
317 ehdr = elf32_getehdr (elf);
319 phdr[0].p_offset = ehdr->e_phoff;
320 phdr[0].p_offset = ehdr->e_phoff;
321 phdr[0].p_vaddr = ehdr->e_phoff;
322 phdr[0].p_paddr = ehdr->e_phoff;
323 phdr[0].p_flags = PF_R | PF_X;
324 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
325 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
326 phdr[0].p_align = sizeof (Elf32_Word);
328 /* Write out the file. */
329 if (elf_update (elf, ELF_C_WRITE) < 0)
331 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
335 /* We don't need the string table anymore. */
336 ebl_strtabfree (shst);
338 /* And the data allocated in the .shstrtab section. */
341 /* Print the ELF header values. */
344 for (i = 0; i < EI_NIDENT; ++i)
345 printf (" %02x", ehdr->e_ident[i]);
347 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
348 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
349 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
350 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
351 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
352 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
353 ehdr->e_shnum, ehdr->e_shstrndx);
356 if (elf_end (elf) != 0)
358 printf ("failure in elf_end: %s\n", elf_errmsg (-1));