blob: 1a62b34bd717166af1776c817d208f5ff6bda663 [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>
29
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000030#include ELFUTILS_HEADER(ebl)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000031
32
33int
34main (int argc, char *argv[] __attribute__ ((unused)))
35{
36 const char *fname = "xxx";
37 int fd;
38 Elf *elf;
39 Elf32_Ehdr *ehdr;
40 Elf32_Phdr *phdr;
41 Elf_Scn *scn;
42 Elf32_Shdr *shdr;
43 Elf_Data *data;
44 struct Ebl_Strtab *shst;
45 struct Ebl_Strent *shstrtabse;
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_fill (0x42);
58
59 elf = elf_begin (fd, ELF_C_WRITE, NULL);
60 if (elf == NULL)
61 {
62 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
63 exit (1);
64 }
65
66 /* Create an ELF header. */
67 ehdr = elf32_newehdr (elf);
68 if (ehdr == NULL)
69 {
70 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
71 exit (1);
72 }
73
74 /* Print the ELF header values. */
75 if (argc > 1)
76 {
77 for (i = 0; i < EI_NIDENT; ++i)
78 printf (" %02x", ehdr->e_ident[i]);
79 printf ("\
80\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
81 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
82 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
83 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
84 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
85 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
86 ehdr->e_shnum, ehdr->e_shstrndx);
87 }
88
89 ehdr->e_ident[0] = 42;
90 ehdr->e_ident[4] = 1;
91 ehdr->e_ident[5] = 1;
92 ehdr->e_ident[6] = 2;
93 ehdr->e_type = ET_EXEC;
94 ehdr->e_version = 1;
95 ehdr->e_ehsize = 1;
96 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
97
98 /* Create the program header. */
99 phdr = elf32_newphdr (elf, 1);
100 if (phdr == NULL)
101 {
102 printf ("cannot create program header: %s\n", elf_errmsg (-1));
103 exit (1);
104 }
105
106 phdr[0].p_type = PT_PHDR;
107 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
108
109 shst = ebl_strtabinit (true);
110
111 scn = elf_newscn (elf);
112 if (scn == NULL)
113 {
114 printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
115 exit (1);
116 }
117 shdr = elf32_getshdr (scn);
118 if (shdr == NULL)
119 {
120 printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
121 exit (1);
122 }
123
124 shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0);
125
126 shdr->sh_type = SHT_STRTAB;
127 shdr->sh_flags = 0;
128 shdr->sh_addr = 0;
129 shdr->sh_link = SHN_UNDEF;
130 shdr->sh_info = SHN_UNDEF;
131 shdr->sh_addralign = 1;
132 shdr->sh_entsize = 0;
133
134 /* We have to store the section index in the ELF header. */
135 ehdr->e_shstrndx = elf_ndxscn (scn);
136
137 data = elf_newdata (scn);
138 if (data == NULL)
139 {
140 printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
141 exit (1);
142 }
143
144 /* No more sections, finalize the section header string table. */
145 ebl_strtabfinalize (shst, data);
146
147 shdr->sh_name = ebl_strtaboffset (shstrtabse);
148
149 /* Let the library compute the internal structure information. */
150 if (elf_update (elf, ELF_C_NULL) < 0)
151 {
152 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
153 exit (1);
154 }
155
156 ehdr = elf32_getehdr (elf);
157
158 phdr[0].p_offset = ehdr->e_phoff;
159 phdr[0].p_offset = ehdr->e_phoff;
160 phdr[0].p_vaddr = ehdr->e_phoff;
161 phdr[0].p_paddr = ehdr->e_phoff;
162 phdr[0].p_flags = PF_R | PF_X;
163 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
164 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
165 phdr[0].p_align = sizeof (Elf32_Word);
166
167 /* Write out the file. */
168 if (elf_update (elf, ELF_C_WRITE) < 0)
169 {
170 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
171 exit (1);
172 }
173
174 /* We don't need the string table anymore. */
175 ebl_strtabfree (shst);
176
177 /* And the data allocated in the .shstrtab section. */
178 free (data->d_buf);
179
180 /* Print the ELF header values. */
181 if (argc > 1)
182 {
183 for (i = 0; i < EI_NIDENT; ++i)
184 printf (" %02x", ehdr->e_ident[i]);
185 printf ("\
186\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
187 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
188 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
189 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
190 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
191 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
192 ehdr->e_shnum, ehdr->e_shstrndx);
193 }
194
195 if (elf_end (elf) != 0)
196 {
197 printf ("failure in elf_end: %s\n", elf_errmsg (-1));
198 exit (1);
199 }
200
201 return 0;
202}