1 /* Test program for elf_update function.
2 Copyright (C) 2000, 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>. */
40 main (int argc, char *argv[] __attribute__ ((unused)))
42 const char *fname = "xxx";
49 fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
52 printf ("cannot open `%s': %s\n", fname, strerror (errno));
56 elf_version (EV_CURRENT);
58 elf = elf_begin (fd, ELF_C_WRITE, NULL);
61 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
65 /* Create an ELF header. */
66 ehdr = elf32_newehdr (elf);
69 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
73 /* Print the ELF header values. */
76 for (i = 0; i < EI_NIDENT; ++i)
77 printf (" %02x", ehdr->e_ident[i]);
79 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
80 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
81 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
82 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
83 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
84 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
85 ehdr->e_shnum, ehdr->e_shstrndx);
88 ehdr->e_ident[0] = 42;
92 ehdr->e_type = ET_EXEC;
95 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
97 /* Create the program header. */
98 phdr = elf32_newphdr (elf, 1);
101 printf ("cannot create program header: %s\n", elf_errmsg (-1));
105 phdr[0].p_type = PT_PHDR;
106 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
108 /* Let the library compute the internal structure information. */
109 if (elf_update (elf, ELF_C_NULL) < 0)
111 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
115 ehdr = elf32_getehdr (elf);
117 phdr[0].p_offset = ehdr->e_phoff;
118 phdr[0].p_offset = ehdr->e_phoff;
119 phdr[0].p_vaddr = ehdr->e_phoff;
120 phdr[0].p_paddr = ehdr->e_phoff;
121 phdr[0].p_flags = PF_R | PF_X;
122 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
123 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
124 phdr[0].p_align = sizeof (Elf32_Word);
126 /* Write out the file. */
127 if (elf_update (elf, ELF_C_WRITE) < 0)
129 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
133 /* Print the ELF header values. */
136 for (i = 0; i < EI_NIDENT; ++i)
137 printf (" %02x", ehdr->e_ident[i]);
139 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
140 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
141 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
142 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
143 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
144 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
145 ehdr->e_shnum, ehdr->e_shstrndx);
148 if (elf_end (elf) != 0)
150 printf ("failure in elf_end: %s\n", elf_errmsg (-1));