1 /* Create an ELF file with all the DT_* flags set.
2 Copyright (C) 2011 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Marek Polacek <mpolacek@redhat.com>, 2011.
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/>. */
23 #include ELFUTILS_HEADER(ebl)
29 #include <stdio_ext.h>
40 static const char fname[] = "testfile-alldts";
41 struct Ebl_Strtab *shst;
42 struct Ebl_Strent *dynscn;
43 struct Ebl_Strent *shstrtabse;
44 const Elf32_Sword dtflags[] =
46 DT_NULL, DT_NEEDED, DT_PLTRELSZ, DT_PLTGOT,
47 DT_HASH, DT_STRTAB, DT_SYMTAB, DT_RELA,
48 DT_RELASZ, DT_RELAENT, DT_STRSZ, DT_SYMENT,
49 DT_INIT, DT_FINI, DT_SONAME, DT_RPATH,
50 DT_SYMBOLIC, DT_REL, DT_RELSZ, DT_RELENT,
51 DT_PLTREL, DT_DEBUG, DT_TEXTREL, DT_JMPREL,
52 DT_BIND_NOW, DT_INIT_ARRAY, DT_FINI_ARRAY,
53 DT_INIT_ARRAYSZ, DT_FINI_ARRAYSZ, DT_RUNPATH,
54 DT_FLAGS, DT_ENCODING, DT_PREINIT_ARRAY,
55 DT_PREINIT_ARRAYSZ, DT_VERSYM, DT_GNU_PRELINKED,
56 DT_GNU_CONFLICTSZ, DT_GNU_LIBLISTSZ, DT_CHECKSUM,
57 DT_PLTPADSZ, DT_MOVEENT, DT_MOVESZ, DT_FEATURE_1,
58 DT_POSFLAG_1, DT_SYMINSZ, DT_SYMINENT, DT_GNU_HASH,
59 DT_TLSDESC_PLT, DT_TLSDESC_GOT, DT_GNU_CONFLICT,
60 DT_GNU_LIBLIST, DT_CONFIG, DT_DEPAUDIT, DT_AUDIT,
61 DT_PLTPAD, DT_MOVETAB, DT_SYMINFO, DT_RELACOUNT,
62 DT_RELCOUNT, DT_FLAGS_1, DT_VERDEF, DT_VERDEFNUM,
63 DT_VERNEED, DT_VERNEEDNUM, DT_AUXILIARY, DT_FILTER
65 const int ndtflags = sizeof (dtflags) / sizeof (dtflags[0]);
67 /* We use no threads here which can interfere with handling a stream. */
68 (void) __fsetlocking (stdout, FSETLOCKING_BYCALLER);
71 int fd = open64 (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
74 printf ("cannot open `%s': %m\n", fname);
78 /* Tell the library which version are we expecting. */
79 elf_version (EV_CURRENT);
81 /* Create an ELF descriptor. */
82 Elf *elf = elf_begin (fd, ELF_C_WRITE, NULL);
85 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
89 /* Create an ELF header. */
90 Elf32_Ehdr *ehdr = elf32_newehdr (elf);
93 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
97 ehdr->e_ident[0] = 42;
100 ehdr->e_type = ET_EXEC;
101 ehdr->e_machine = EM_386;
106 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
108 /* Create the program headers. */
109 Elf32_Phdr *phdr = elf32_newphdr (elf, 2);
112 printf ("cannot create program headers: %s\n", elf_errmsg (-1));
116 phdr[0].p_type = PT_PHDR;
117 phdr[1].p_type = PT_DYNAMIC;
119 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
120 shst = ebl_strtabinit (true);
122 /* Create the .dynamic section. */
123 Elf_Scn *scn = elf_newscn (elf);
126 printf ("cannot create DYNAMIC section: %s\n", elf_errmsg (-1));
130 Elf32_Shdr *shdr = elf32_getshdr (scn);
133 printf ("cannot get header for DYNAMIC section: %s\n", elf_errmsg (-1));
137 dynscn = ebl_strtabadd (shst, ".dynamic", 0);
139 /* We'll need to know the section offset. But this will be set up
140 by elf_update later, so for now just store the address. */
141 const Elf32_Off *const dynscn_offset = &shdr->sh_offset;
142 shdr->sh_type = SHT_DYNAMIC;
143 shdr->sh_flags = SHF_ALLOC | SHF_WRITE;
144 shdr->sh_link = SHN_UNDEF;
145 shdr->sh_info = SHN_UNDEF;
146 /* This section will start here. */
147 shdr->sh_addr = 0x1a0;
149 /* Create new section data. */
150 Elf_Data *data = elf_newdata (scn);
153 printf ("cannot create data for DYNAMIC section: %s\n", elf_errmsg (-1));
157 /* Allocate memory for all the .dynamic entries. */
158 Elf32_Dyn *dyn = malloc (ndtflags * sizeof (Elf32_Dyn));
161 printf ("malloc failed: %m\n");
165 /* Now write all the DT_* flags. */
166 for (int i = 0; i < ndtflags; ++i)
168 dyn[i].d_tag = dtflags[i];
169 dyn[i].d_un.d_val = 0xdeadbeef;
172 /* Set the pointer to allocated memory. */
174 data->d_type = ELF_T_DYN;
175 data->d_version = EV_CURRENT;
176 data->d_size = ndtflags * sizeof (Elf32_Dyn);
179 /* Create .shstrtab section. */
180 scn = elf_newscn (elf);
183 printf ("cannot create SHSTRTAB section: %s\n", elf_errmsg (-1));
187 shdr = elf32_getshdr (scn);
190 printf ("cannot get header for SHSTRTAB section: %s\n", elf_errmsg (-1));
194 shstrtabse = ebl_strtabadd (shst, ".shstrtab", 0);
196 shdr->sh_type = SHT_STRTAB;
199 shdr->sh_link = SHN_UNDEF;
200 shdr->sh_info = SHN_UNDEF;
201 shdr->sh_entsize = 1;
203 /* We have to store the section index in the ELF header. */
204 ehdr->e_shstrndx = elf_ndxscn (scn);
206 data = elf_newdata (scn);
209 printf ("cannot create data SHSTRTAB section: %s\n", elf_errmsg (-1));
213 /* No more sections, finalize the section header string table. */
214 ebl_strtabfinalize (shst, data);
216 elf32_getshdr (elf_getscn (elf, 1))->sh_name = ebl_strtaboffset (dynscn);
217 shdr->sh_name = ebl_strtaboffset (shstrtabse);
219 /* Let the library compute the internal structure information. */
220 if (elf_update (elf, ELF_C_NULL) < 0)
222 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
226 ehdr = elf32_getehdr (elf);
228 phdr[0].p_offset = ehdr->e_phoff;
229 phdr[0].p_vaddr = ehdr->e_phoff;
230 phdr[0].p_paddr = ehdr->e_phoff;
231 phdr[0].p_flags = PF_R | PF_X;
232 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
233 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
234 phdr[0].p_align = sizeof (Elf32_Word);
236 phdr[1].p_flags = PF_W | PF_R;
237 phdr[1].p_offset = *dynscn_offset;
238 /* Set up the start of this segment to equal start address of the
240 phdr[1].p_vaddr = 0x1a0;
241 phdr[1].p_paddr = 0x1a0;
242 phdr[1].p_align = 2 * sizeof (Elf32_Word);
243 phdr[1].p_filesz = ndtflags * sizeof (Elf32_Dyn);
244 phdr[1].p_memsz = ndtflags * sizeof (Elf32_Dyn);
246 /* Write out the file. */
247 if (elf_update (elf, ELF_C_WRITE) < 0)
249 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
253 /* We don't need the string table anymore. */
254 ebl_strtabfree (shst);
256 /* And the data allocated in the .shstrtab section. */
260 if (elf_end (elf) != 0)
262 printf ("failure in elf_end: %s\n", elf_errmsg (-1));