Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Test program for elf_update function. |
| 2 | Copyright (C) 2000, 2002, 2005 Red Hat, Inc. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 3 | This file is part of Red Hat elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2000. |
| 5 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 9 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 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. |
| 14 | |
| 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, |
Ulrich Drepper | 1e9ef50 | 2006-04-04 22:29:06 +0000 | [diff] [blame] | 17 | Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 18 | |
| 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>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | # include <config.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <errno.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <libelf.h> |
| 34 | #include <stdio.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | |
| 39 | int |
| 40 | main (int argc, char *argv[] __attribute__ ((unused))) |
| 41 | { |
| 42 | const char *fname = "xxx"; |
| 43 | int fd; |
| 44 | Elf *elf; |
| 45 | Elf32_Ehdr *ehdr; |
| 46 | int i; |
| 47 | |
| 48 | fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666); |
| 49 | if (fd == -1) |
| 50 | { |
| 51 | printf ("cannot open `%s': %s\n", fname, strerror (errno)); |
| 52 | exit (1); |
| 53 | } |
| 54 | |
| 55 | elf_version (EV_CURRENT); |
| 56 | |
| 57 | elf = elf_begin (fd, ELF_C_WRITE, NULL); |
| 58 | if (elf == NULL) |
| 59 | { |
| 60 | printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1)); |
| 61 | exit (1); |
| 62 | } |
| 63 | |
| 64 | /* Create an ELF header. */ |
| 65 | ehdr = elf32_newehdr (elf); |
| 66 | if (ehdr == NULL) |
| 67 | { |
| 68 | printf ("cannot create ELF header: %s\n", elf_errmsg (-1)); |
| 69 | exit (1); |
| 70 | } |
| 71 | |
| 72 | /* Print the ELF header values. */ |
| 73 | if (argc > 1) |
| 74 | { |
| 75 | for (i = 0; i < EI_NIDENT; ++i) |
| 76 | printf (" %02x", ehdr->e_ident[i]); |
| 77 | printf ("\ |
| 78 | \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n" |
| 79 | "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n" |
| 80 | "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n", |
| 81 | ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry, |
| 82 | ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize, |
| 83 | ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize, |
| 84 | ehdr->e_shnum, ehdr->e_shstrndx); |
| 85 | } |
| 86 | |
| 87 | ehdr->e_ident[0] = 42; |
| 88 | ehdr->e_ident[4] = 1; |
| 89 | ehdr->e_ident[5] = 1; |
| 90 | ehdr->e_ident[6] = 2; |
| 91 | ehdr->e_ident[9] = 2; |
| 92 | ehdr->e_version = 1; |
| 93 | ehdr->e_ehsize = 1; |
| 94 | |
| 95 | /* Write out the file. */ |
| 96 | if (elf_update (elf, ELF_C_WRITE) < 0) |
| 97 | { |
| 98 | printf ("failure in elf_update: %s\n", elf_errmsg (-1)); |
| 99 | exit (1); |
| 100 | } |
| 101 | |
| 102 | /* Create an ELF header. */ |
| 103 | ehdr = elf32_newehdr (elf); |
| 104 | if (ehdr == NULL) |
| 105 | { |
| 106 | printf ("cannot create ELF header: %s\n", elf_errmsg (-1)); |
| 107 | exit (1); |
| 108 | } |
| 109 | |
| 110 | /* Print the ELF header values. */ |
| 111 | if (argc > 1) |
| 112 | { |
| 113 | for (i = 0; i < EI_NIDENT; ++i) |
| 114 | printf (" %02x", ehdr->e_ident[i]); |
| 115 | printf ("\ |
| 116 | \ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n" |
| 117 | "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n" |
| 118 | "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n", |
| 119 | ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry, |
| 120 | ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize, |
| 121 | ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize, |
| 122 | ehdr->e_shnum, ehdr->e_shstrndx); |
| 123 | } |
| 124 | |
| 125 | if (elf_end (elf) != 0) |
| 126 | { |
| 127 | printf ("failure in elf_end: %s\n", elf_errmsg (-1)); |
| 128 | exit (1); |
| 129 | } |
| 130 | |
| 131 | return 0; |
| 132 | } |