1 /* Test program for elf_update function.
2 Copyright (C) 2000, 2001, 2002, 2005 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
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.
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.
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/>. */
31 #include ELFUTILS_HEADER(ebl)
35 main (int argc, char *argv[] __attribute__ ((unused)))
37 const char fname[] = "xxx";
45 struct Ebl_Strtab *shst;
46 struct Ebl_Strent *firstse;
47 struct Ebl_Strent *secondse;
48 struct Ebl_Strent *thirdse;
49 struct Ebl_Strent *fourthse;
50 struct Ebl_Strent *shstrtabse;
53 fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
56 printf ("cannot open `%s': %s\n", fname, strerror (errno));
60 elf_version (EV_CURRENT);
64 elf = elf_begin (fd, ELF_C_WRITE, NULL);
67 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
71 /* Create an ELF header. */
72 ehdr = elf32_newehdr (elf);
75 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
79 /* Print the ELF header values. */
82 for (i = 0; i < EI_NIDENT; ++i)
83 printf (" %02x", ehdr->e_ident[i]);
85 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
86 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
87 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
88 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
89 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
90 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
91 ehdr->e_shnum, ehdr->e_shstrndx);
94 ehdr->e_ident[0] = 42;
98 ehdr->e_type = ET_EXEC;
101 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
103 /* Create the program header. */
104 phdr = elf32_newphdr (elf, 1);
107 printf ("cannot create program header: %s\n", elf_errmsg (-1));
111 phdr[0].p_type = PT_PHDR;
112 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
114 shst = ebl_strtabinit (true);
116 scn = elf_newscn (elf);
119 printf ("cannot create first section: %s\n", elf_errmsg (-1));
122 shdr = elf32_getshdr (scn);
125 printf ("cannot get header for first section: %s\n", elf_errmsg (-1));
129 firstse = ebl_strtabadd (shst, ".first", 0);
131 shdr->sh_type = SHT_PROGBITS;
132 shdr->sh_flags = SHF_ALLOC | SHF_EXECINSTR;
136 shdr->sh_entsize = 1;
138 data = elf_newdata (scn);
141 printf ("cannot create data first section: %s\n", elf_errmsg (-1));
145 data->d_buf = "hello";
146 data->d_type = ELF_T_BYTE;
147 data->d_version = EV_CURRENT;
152 scn = elf_newscn (elf);
155 printf ("cannot create second section: %s\n", elf_errmsg (-1));
158 shdr = elf32_getshdr (scn);
161 printf ("cannot get header for second section: %s\n", elf_errmsg (-1));
165 secondse = ebl_strtabadd (shst, ".second", 0);
167 shdr->sh_type = SHT_PROGBITS;
168 shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
172 shdr->sh_entsize = 1;
174 data = elf_newdata (scn);
177 printf ("cannot create data second section: %s\n", elf_errmsg (-1));
181 data->d_buf = "world";
182 data->d_type = ELF_T_BYTE;
183 data->d_version = EV_CURRENT;
188 scn = elf_newscn (elf);
191 printf ("cannot create third section: %s\n", elf_errmsg (-1));
194 shdr = elf32_getshdr (scn);
197 printf ("cannot get header for third section: %s\n", elf_errmsg (-1));
201 thirdse = ebl_strtabadd (shst, ".third", 0);
203 shdr->sh_type = SHT_PROGBITS;
204 shdr->sh_flags = SHF_ALLOC | SHF_EXECINSTR;
208 shdr->sh_entsize = 1;
210 data = elf_newdata (scn);
213 printf ("cannot create data third section: %s\n", elf_errmsg (-1));
217 data->d_buf = "!!!!!";
218 data->d_type = ELF_T_BYTE;
219 data->d_version = EV_CURRENT;
224 scn = elf_newscn (elf);
227 printf ("cannot create fourth section: %s\n", elf_errmsg (-1));
230 shdr = elf32_getshdr (scn);
233 printf ("cannot get header for fourth section: %s\n", elf_errmsg (-1));
237 fourthse = ebl_strtabadd (shst, ".fourth", 0);
239 shdr->sh_type = SHT_NOBITS;
240 shdr->sh_flags = SHF_ALLOC | SHF_EXECINSTR;
244 shdr->sh_entsize = 1;
247 data = elf_newdata (scn);
250 printf ("cannot create data fourth section: %s\n", elf_errmsg (-1));
255 data->d_type = ELF_T_BYTE;
256 data->d_version = EV_CURRENT;
261 scn = elf_newscn (elf);
264 printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
267 shdr = elf32_getshdr (scn);
270 printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
274 shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0);
276 shdr->sh_type = SHT_STRTAB;
279 shdr->sh_link = SHN_UNDEF;
280 shdr->sh_info = SHN_UNDEF;
281 shdr->sh_entsize = 1;
283 /* We have to store the section index in the ELF header. */
284 ehdr->e_shstrndx = elf_ndxscn (scn);
286 data = elf_newdata (scn);
289 printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
293 /* No more sections, finalize the section header string table. */
294 ebl_strtabfinalize (shst, data);
296 elf32_getshdr (elf_getscn (elf, 1))->sh_name = ebl_strtaboffset (firstse);
297 elf32_getshdr (elf_getscn (elf, 2))->sh_name = ebl_strtaboffset (secondse);
298 elf32_getshdr (elf_getscn (elf, 3))->sh_name = ebl_strtaboffset (thirdse);
299 elf32_getshdr (elf_getscn (elf, 4))->sh_name = ebl_strtaboffset (fourthse);
300 shdr->sh_name = ebl_strtaboffset (shstrtabse);
302 /* Let the library compute the internal structure information. */
303 if (elf_update (elf, ELF_C_NULL) < 0)
305 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
309 ehdr = elf32_getehdr (elf);
311 phdr[0].p_offset = ehdr->e_phoff;
312 phdr[0].p_offset = ehdr->e_phoff;
313 phdr[0].p_vaddr = ehdr->e_phoff;
314 phdr[0].p_paddr = ehdr->e_phoff;
315 phdr[0].p_flags = PF_R | PF_X;
316 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
317 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
318 phdr[0].p_align = sizeof (Elf32_Word);
320 /* Write out the file. */
321 if (elf_update (elf, ELF_C_WRITE) < 0)
323 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
327 /* We don't need the string table anymore. */
328 ebl_strtabfree (shst);
330 /* And the data allocated in the .shstrtab section. */
333 /* Print the ELF header values. */
336 for (i = 0; i < EI_NIDENT; ++i)
337 printf (" %02x", ehdr->e_ident[i]);
339 \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
340 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
341 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
342 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
343 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
344 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
345 ehdr->e_shnum, ehdr->e_shstrndx);
348 if (elf_end (elf) != 0)
350 printf ("failure in elf_end: %s\n", elf_errmsg (-1));