blob: 160adf1e2f194600e9d0795b3e84df600f680ec2 [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.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
4
5 This program is Open Source software; you can redistribute it and/or
6 modify it under the terms of the Open Software License version 1.0 as
7 published by the Open Source Initiative.
8
9 You should have received a copy of the Open Software License along
10 with this program; if not, you may obtain a copy of the Open Software
11 License version 1.0 from http://www.opensource.org/licenses/osl.php or
12 by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
13 3001 King Ranch Road, Ukiah, CA 95482. */
14
15#ifdef HAVE_CONFIG_H
16# include <config.h>
17#endif
18
19#include <errno.h>
20#include <fcntl.h>
21#include <libelf.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <libebl.h>
27
28
29int
30main (int argc, char *argv[] __attribute__ ((unused)))
31{
32 const char *fname = "xxx";
33 int fd;
34 Elf *elf;
35 Elf32_Ehdr *ehdr;
36 Elf32_Phdr *phdr;
37 Elf_Scn *scn;
38 Elf32_Shdr *shdr;
39 Elf_Data *data;
40 struct Ebl_Strtab *shst;
41 struct Ebl_Strent *shstrtabse;
42 int i;
43
44 fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
45 if (fd == -1)
46 {
47 printf ("cannot open `%s': %s\n", fname, strerror (errno));
48 exit (1);
49 }
50
51 elf_version (EV_CURRENT);
52
53 elf_fill (0x42);
54
55 elf = elf_begin (fd, ELF_C_WRITE, NULL);
56 if (elf == NULL)
57 {
58 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
59 exit (1);
60 }
61
62 /* Create an ELF header. */
63 ehdr = elf32_newehdr (elf);
64 if (ehdr == NULL)
65 {
66 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
67 exit (1);
68 }
69
70 /* Print the ELF header values. */
71 if (argc > 1)
72 {
73 for (i = 0; i < EI_NIDENT; ++i)
74 printf (" %02x", ehdr->e_ident[i]);
75 printf ("\
76\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
77 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
78 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
79 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
80 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
81 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
82 ehdr->e_shnum, ehdr->e_shstrndx);
83 }
84
85 ehdr->e_ident[0] = 42;
86 ehdr->e_ident[4] = 1;
87 ehdr->e_ident[5] = 1;
88 ehdr->e_ident[6] = 2;
89 ehdr->e_type = ET_EXEC;
90 ehdr->e_version = 1;
91 ehdr->e_ehsize = 1;
92 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
93
94 /* Create the program header. */
95 phdr = elf32_newphdr (elf, 1);
96 if (phdr == NULL)
97 {
98 printf ("cannot create program header: %s\n", elf_errmsg (-1));
99 exit (1);
100 }
101
102 phdr[0].p_type = PT_PHDR;
103 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
104
105 shst = ebl_strtabinit (true);
106
107 scn = elf_newscn (elf);
108 if (scn == NULL)
109 {
110 printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
111 exit (1);
112 }
113 shdr = elf32_getshdr (scn);
114 if (shdr == NULL)
115 {
116 printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
117 exit (1);
118 }
119
120 shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0);
121
122 shdr->sh_type = SHT_STRTAB;
123 shdr->sh_flags = 0;
124 shdr->sh_addr = 0;
125 shdr->sh_link = SHN_UNDEF;
126 shdr->sh_info = SHN_UNDEF;
127 shdr->sh_addralign = 1;
128 shdr->sh_entsize = 0;
129
130 /* We have to store the section index in the ELF header. */
131 ehdr->e_shstrndx = elf_ndxscn (scn);
132
133 data = elf_newdata (scn);
134 if (data == NULL)
135 {
136 printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
137 exit (1);
138 }
139
140 /* No more sections, finalize the section header string table. */
141 ebl_strtabfinalize (shst, data);
142
143 shdr->sh_name = ebl_strtaboffset (shstrtabse);
144
145 /* Let the library compute the internal structure information. */
146 if (elf_update (elf, ELF_C_NULL) < 0)
147 {
148 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
149 exit (1);
150 }
151
152 ehdr = elf32_getehdr (elf);
153
154 phdr[0].p_offset = ehdr->e_phoff;
155 phdr[0].p_offset = ehdr->e_phoff;
156 phdr[0].p_vaddr = ehdr->e_phoff;
157 phdr[0].p_paddr = ehdr->e_phoff;
158 phdr[0].p_flags = PF_R | PF_X;
159 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
160 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
161 phdr[0].p_align = sizeof (Elf32_Word);
162
163 /* Write out the file. */
164 if (elf_update (elf, ELF_C_WRITE) < 0)
165 {
166 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
167 exit (1);
168 }
169
170 /* We don't need the string table anymore. */
171 ebl_strtabfree (shst);
172
173 /* And the data allocated in the .shstrtab section. */
174 free (data->d_buf);
175
176 /* Print the ELF header values. */
177 if (argc > 1)
178 {
179 for (i = 0; i < EI_NIDENT; ++i)
180 printf (" %02x", ehdr->e_ident[i]);
181 printf ("\
182\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
183 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
184 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
185 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
186 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
187 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
188 ehdr->e_shnum, ehdr->e_shstrndx);
189 }
190
191 if (elf_end (elf) != 0)
192 {
193 printf ("failure in elf_end: %s\n", elf_errmsg (-1));
194 exit (1);
195 }
196
197 return 0;
198}