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>. */
38 #include ELFUTILS_HEADER(ebl)
42 main (int argc, char *argv[] __attribute__ ((unused)))
44 const char *fname = "xxx";
52 struct Ebl_Strtab *shst;
53 struct Ebl_Strent *shstrtabse;
56 fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
59 printf ("cannot open `%s': %s\n", fname, strerror (errno));
63 elf_version (EV_CURRENT);
67 elf = elf_begin (fd, ELF_C_WRITE, NULL);
70 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
74 /* Create an ELF header. */
75 ehdr = elf32_newehdr (elf);
78 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
82 /* Print the ELF header values. */
85 for (i = 0; i < EI_NIDENT; ++i)
86 printf (" %02x", ehdr->e_ident[i]);
88 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
89 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
90 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
91 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
92 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
93 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
94 ehdr->e_shnum, ehdr->e_shstrndx);
97 ehdr->e_ident[0] = 42;
100 ehdr->e_ident[6] = 2;
101 ehdr->e_type = ET_EXEC;
104 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
106 /* Create the program header. */
107 phdr = elf32_newphdr (elf, 1);
110 printf ("cannot create program header: %s\n", elf_errmsg (-1));
114 phdr[0].p_type = PT_PHDR;
115 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
117 shst = ebl_strtabinit (true);
119 scn = elf_newscn (elf);
122 printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
125 shdr = elf32_getshdr (scn);
128 printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
132 shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0);
134 shdr->sh_type = SHT_STRTAB;
137 shdr->sh_link = SHN_UNDEF;
138 shdr->sh_info = SHN_UNDEF;
139 shdr->sh_addralign = 1;
140 shdr->sh_entsize = 0;
142 /* We have to store the section index in the ELF header. */
143 ehdr->e_shstrndx = elf_ndxscn (scn);
145 data = elf_newdata (scn);
148 printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
152 /* No more sections, finalize the section header string table. */
153 ebl_strtabfinalize (shst, data);
155 shdr->sh_name = ebl_strtaboffset (shstrtabse);
157 /* Let the library compute the internal structure information. */
158 if (elf_update (elf, ELF_C_NULL) < 0)
160 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
164 ehdr = elf32_getehdr (elf);
166 phdr[0].p_offset = ehdr->e_phoff;
167 phdr[0].p_offset = ehdr->e_phoff;
168 phdr[0].p_vaddr = ehdr->e_phoff;
169 phdr[0].p_paddr = ehdr->e_phoff;
170 phdr[0].p_flags = PF_R | PF_X;
171 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
172 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
173 phdr[0].p_align = sizeof (Elf32_Word);
175 /* Write out the file. */
176 if (elf_update (elf, ELF_C_WRITE) < 0)
178 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
182 /* We don't need the string table anymore. */
183 ebl_strtabfree (shst);
185 /* And the data allocated in the .shstrtab section. */
188 /* Print the ELF header values. */
191 for (i = 0; i < EI_NIDENT; ++i)
192 printf (" %02x", ehdr->e_ident[i]);
194 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
195 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
196 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
197 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
198 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
199 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
200 ehdr->e_shnum, ehdr->e_shstrndx);
203 if (elf_end (elf) != 0)
205 printf ("failure in elf_end: %s\n", elf_errmsg (-1));