blob: c595fba7f357ff6721867617c2c4d936b7659371 [file] [log] [blame]
The Android Open Source Project593c3652008-10-21 07:00:00 -07001/* Test program for elf_update function.
2 Copyright (C) 2000, 2002 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
27int
28main (int argc, char *argv[])
29{
30 const char *fname = "xxx";
31 int fd;
32 Elf *elf;
33 Elf32_Ehdr *ehdr;
34 Elf32_Phdr *phdr;
35 int i;
36
37 fd = open (fname, O_RDWR | O_CREAT | O_TRUNC, 0666);
38 if (fd == -1)
39 {
40 printf ("cannot open `%s': %s\n", fname, strerror (errno));
41 exit (1);
42 }
43
44 elf_version (EV_CURRENT);
45
46 elf = elf_begin (fd, ELF_C_WRITE, NULL);
47 if (elf == NULL)
48 {
49 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
50 exit (1);
51 }
52
53 /* Create an ELF header. */
54 ehdr = elf32_newehdr (elf);
55 if (ehdr == NULL)
56 {
57 printf ("cannot create ELF header: %s\n", elf_errmsg (-1));
58 exit (1);
59 }
60
61 /* Print the ELF header values. */
62 if (argc > 1)
63 {
64 for (i = 0; i < EI_NIDENT; ++i)
65 printf (" %02x", ehdr->e_ident[i]);
66 printf ("\
67\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
68 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
69 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
70 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
71 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
72 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
73 ehdr->e_shnum, ehdr->e_shstrndx);
74 }
75
76 ehdr->e_ident[0] = 42;
77 ehdr->e_ident[4] = 1;
78 ehdr->e_ident[5] = 1;
79 ehdr->e_ident[6] = 2;
80 ehdr->e_type = ET_EXEC;
81 ehdr->e_version = 1;
82 ehdr->e_ehsize = 1;
83 elf_flagehdr (elf, ELF_C_SET, ELF_F_DIRTY);
84
85 /* Create the program header. */
86 phdr = elf32_newphdr (elf, 1);
87 if (phdr == NULL)
88 {
89 printf ("cannot create program header: %s\n", elf_errmsg (-1));
90 exit (1);
91 }
92
93 phdr[0].p_type = PT_PHDR;
94 elf_flagphdr (elf, ELF_C_SET, ELF_F_DIRTY);
95
96 /* Let the library compute the internal structure information. */
97 if (elf_update (elf, ELF_C_NULL) < 0)
98 {
99 printf ("failure in elf_update(NULL): %s\n", elf_errmsg (-1));
100 exit (1);
101 }
102
103 ehdr = elf32_getehdr (elf);
104
105 phdr[0].p_offset = ehdr->e_phoff;
106 phdr[0].p_offset = ehdr->e_phoff;
107 phdr[0].p_vaddr = ehdr->e_phoff;
108 phdr[0].p_paddr = ehdr->e_phoff;
109 phdr[0].p_flags = PF_R | PF_X;
110 phdr[0].p_filesz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
111 phdr[0].p_memsz = ehdr->e_phnum * elf32_fsize (ELF_T_PHDR, 1, EV_CURRENT);
112 phdr[0].p_align = sizeof (Elf32_Word);
113
114 /* Write out the file. */
115 if (elf_update (elf, ELF_C_WRITE) < 0)
116 {
117 printf ("failure in elf_update(WRITE): %s\n", elf_errmsg (-1));
118 exit (1);
119 }
120
121 /* Print the ELF header values. */
122 if (argc > 1)
123 {
124 for (i = 0; i < EI_NIDENT; ++i)
125 printf (" %02x", ehdr->e_ident[i]);
126 printf ("\
127\ntype = %hu\nmachine = %hu\nversion = %u\nentry = %u\nphoff = %u\n"
128 "shoff = %u\nflags = %u\nehsize = %hu\nphentsize = %hu\n"
129 "phnum = %hu\nshentsize = %hu\nshnum = %hu\nshstrndx = %hu\n",
130 ehdr->e_type, ehdr->e_machine, ehdr->e_version, ehdr->e_entry,
131 ehdr->e_phoff, ehdr->e_shoff, ehdr->e_flags, ehdr->e_ehsize,
132 ehdr->e_phentsize, ehdr->e_phnum, ehdr->e_shentsize,
133 ehdr->e_shnum, ehdr->e_shstrndx);
134 }
135
136 if (elf_end (elf) != 0)
137 {
138 printf ("failure in elf_end: %s\n", elf_errmsg (-1));
139 exit (1);
140 }
141
142 return 0;
143}