blob: 5805163d8607f76f223e9a4a62f0209cdbd1de48 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Test program for elf_update function.
2 Copyright (C) 2000, 2002, 2005 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000010
Mark Wielaardde2ed972012-06-05 17:15:16 +020011 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000012 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000015
Mark Wielaardde2ed972012-06-05 17:15:16 +020016 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000018
19#ifdef HAVE_CONFIG_H
20# include <config.h>
21#endif
22
23#include <errno.h>
24#include <fcntl.h>
25#include <libelf.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Mark Wielaardb0f202e2013-04-28 00:54:17 +020029#include <unistd.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000030
31
32int
33main (int argc, char *argv[] __attribute__ ((unused)))
34{
Mark Wielaardb0f202e2013-04-28 00:54:17 +020035 const char *fname = "xxx_update2";
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000036 int fd;
37 Elf *elf;
38 Elf32_Ehdr *ehdr;
39 Elf32_Phdr *phdr;
40 int i;
41
42 fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
43 if (fd == -1)
44 {
45 printf ("cannot open `%s': %s\n", fname, strerror (errno));
46 exit (1);
47 }
48
49 elf_version (EV_CURRENT);
50
51 elf = elf_begin (fd, ELF_C_WRITE, NULL);
52 if (elf == NULL)
53 {
54 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
55 exit (1);
56 }
57
58 /* Create an ELF header. */
59 ehdr = elf32_newehdr (elf);
60 if (ehdr == NULL)
61 {
62 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
63 exit (1);
64 }
65
66 /* Print the ELF header values. */
67 if (argc > 1)
68 {
69 for (i = 0; i < EI_NIDENT; ++i)
70 printf (" %02x", ehdr->e_ident[i]);
71 printf ("\
72\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
73 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
74 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
75 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
76 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
77 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
78 ehdr->e_shnum, ehdr->e_shstrndx);
79 }
80
81 ehdr->e_ident[0] = 42;
82 ehdr->e_ident[4] = 1;
83 ehdr->e_ident[5] = 1;
84 ehdr->e_ident[6] = 2;
85 ehdr->e_type = ET_EXEC;
86 ehdr->e_version = 1;
87 ehdr->e_ehsize = 1;
88 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
89
90 /* Create the program header. */
91 phdr = elf32_newphdr (elf, 1);
92 if (phdr == NULL)
93 {
94 printf ("cannot create program header: %s\n", elf_errmsg (-1));
95 exit (1);
96 }
97
98 phdr[0].p_type = PT_PHDR;
99 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
100
101 /* Let the library compute the internal structure information. */
102 if (elf_update (elf, ELF_C_NULL) < 0)
103 {
104 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
105 exit (1);
106 }
107
108 ehdr = elf32_getehdr (elf);
109
110 phdr[0].p_offset = ehdr->e_phoff;
111 phdr[0].p_offset = ehdr->e_phoff;
112 phdr[0].p_vaddr = ehdr->e_phoff;
113 phdr[0].p_paddr = ehdr->e_phoff;
114 phdr[0].p_flags = PF_R | PF_X;
115 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
116 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
117 phdr[0].p_align = sizeof (Elf32_Word);
118
119 /* Write out the file. */
120 if (elf_update (elf, ELF_C_WRITE) < 0)
121 {
122 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
123 exit (1);
124 }
125
126 /* Print the ELF header values. */
127 if (argc > 1)
128 {
129 for (i = 0; i < EI_NIDENT; ++i)
130 printf (" %02x", ehdr->e_ident[i]);
131 printf ("\
132\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
133 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
134 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
135 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
136 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
137 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
138 ehdr->e_shnum, ehdr->e_shstrndx);
139 }
140
141 if (elf_end (elf) != 0)
142 {
143 printf ("failure in elf_end: %s\n", elf_errmsg (-1));
144 exit (1);
145 }
146
Mark Wielaardb0f202e2013-04-28 00:54:17 +0200147 unlink (fname);
148
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000149 return 0;
150}