blob: b6d0e4314b3a354fb620da954a7f4a34b12608ef [file] [log] [blame]
Roland McGrathe9ca7382010-02-15 15:15:50 -08001/* Copyright (C) 2002-2010 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02002 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00003 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 This file is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Mark Wielaardde2ed972012-06-05 17:15:16 +020010 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000011 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000014
Mark Wielaardde2ed972012-06-05 17:15:16 +020015 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000017
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000018#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000022#include <fcntl.h>
23#include <inttypes.h>
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000024#include ELFUTILS_HEADER(asm)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000025#include <libelf.h>
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
29
30
31static const char fname[] = "asm-tst9-out.o";
32
33
34static int32_t input[] =
35 {
36 0, 1, 129, 510, 2000, 33000, 0x7ffffff, 0x7fffffff
37 };
38#define ninput (sizeof (input) / sizeof (input[0]))
39
40
41static const GElf_Ehdr expected_ehdr =
42 {
43 .e_ident = { [EI_MAG0] = ELFMAG0,
44 [EI_MAG1] = ELFMAG1,
45 [EI_MAG2] = ELFMAG2,
46 [EI_MAG3] = ELFMAG3,
47 [EI_CLASS] = ELFCLASS32,
48 [EI_DATA] = ELFDATA2LSB,
49 [EI_VERSION] = EV_CURRENT },
50 .e_type = ET_REL,
51 .e_machine = EM_386,
52 .e_version = EV_CURRENT,
53 .e_shoff = 180,
54 .e_ehsize = sizeof (Elf32_Ehdr),
55 .e_shentsize = sizeof (Elf32_Shdr),
56 .e_shnum = 3,
57 .e_shstrndx = 2
58 };
59
60
61static const char *scnnames[3] =
62 {
63 [0] = "",
64 [1] = ".data",
65 [2] = ".shstrtab"
66 };
67
68
69static const char expecteddata[] =
70 {
71 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x7f,
72 0x81, 0x01, 0x81, 0x01, 0xff, 0xfe, 0xff, 0xff, 0x0f, 0xff, 0x7e, 0xfe,
73 0x03, 0xfe, 0x03, 0x82, 0xfc, 0xff, 0xff, 0x0f, 0x82, 0x7c, 0xd0, 0x0f,
74 0xd0, 0x0f, 0xb0, 0xf0, 0xff, 0xff, 0x0f, 0xb0, 0x70, 0xe8, 0x81, 0x02,
75 0xe8, 0x81, 0x02, 0x98, 0xfe, 0xfd, 0xff, 0x0f, 0x98, 0xfe, 0x7d, 0xff,
76 0xff, 0xff, 0x3f, 0xff, 0xff, 0xff, 0x3f, 0x81, 0x80, 0x80, 0xc0, 0x0f,
77 0x81, 0x80, 0x80, 0x40, 0xff, 0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff,
78 0xff, 0x07, 0x81, 0x80, 0x80, 0x80, 0x08, 0x81, 0x80, 0x80, 0x80, 0x78
79 };
80
81
82int
83main (void)
84{
85 AsmCtx_t *ctx;
86 AsmScn_t *scn;
87 int result = 0;
88 int fd;
89 Elf *elf;
90 GElf_Ehdr ehdr_mem;
91 GElf_Ehdr *ehdr;
92 size_t cnt;
93
94 elf_version (EV_CURRENT);
95
Ulrich Dreppera38998e2005-08-03 02:05:39 +000096 Ebl *ebl = ebl_openbackend_machine (EM_386);
97 if (ebl == NULL)
98 {
99 puts ("cannot open backend library");
100 return 1;
101 }
102
103 ctx = asm_begin (fname, ebl, false);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000104 if (ctx == NULL)
105 {
106 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
107 return 1;
108 }
109
110 /* Create two sections. */
111 scn = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
112 if (scn == NULL)
113 {
114 printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
115 asm_abort (ctx);
116 return 1;
117 }
118
119 /* Special alignment for the .text section. */
120 if (asm_align (scn, 16) != 0)
121 {
122 printf ("cannot align .text section: %s\n", asm_errmsg (-1));
123 result = 1;
124 }
125
126 /* Add a few ULEB128 and SLEB128 numbers. */
127 for (cnt = 0; cnt < ninput; ++cnt)
128 {
129 if (asm_adduleb128 (scn, input[cnt]) != 0)
130 {
131 printf ("cannot insert uleb %" PRIu32 ": %s\n",
132 (uint32_t) input[cnt], asm_errmsg (-1));
133 result = 1;
134 }
135
136 if (asm_addsleb128 (scn, input[cnt]) != 0)
137 {
138 printf ("cannot insert sleb %" PRId32 ": %s\n",
139 input[cnt], asm_errmsg (-1));
140 result = 1;
141 }
142
143 if (asm_adduleb128 (scn, -input[cnt]) != 0)
144 {
145 printf ("cannot insert uleb %" PRIu32 ": %s\n",
146 (uint32_t) -input[cnt], asm_errmsg (-1));
147 result = 1;
148 }
149
150 if (asm_addsleb128 (scn, -input[cnt]) != 0)
151 {
152 printf ("cannot insert sleb %" PRId32 ": %s\n",
153 -input[cnt], asm_errmsg (-1));
154 result = 1;
155 }
156 }
157
158 /* Create the output file. */
159 if (asm_end (ctx) != 0)
160 {
161 printf ("cannot create output file: %s\n", asm_errmsg (-1));
162 asm_abort (ctx);
163 return 1;
164 }
165
166 /* Check the file. */
167 fd = open (fname, O_RDONLY);
168 if (fd == -1)
169 {
170 printf ("cannot open generated file: %m\n");
171 result = 1;
172 goto out;
173 }
174
175 elf = elf_begin (fd, ELF_C_READ, NULL);
176 if (elf == NULL)
177 {
178 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
179 result = 1;
180 goto out_close;
181 }
182 if (elf_kind (elf) != ELF_K_ELF)
183 {
184 puts ("not a valid ELF file");
185 result = 1;
186 goto out_close2;
187 }
188
189 ehdr = gelf_getehdr (elf, &ehdr_mem);
190 if (ehdr == NULL)
191 {
192 printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
193 result = 1;
194 goto out_close2;
195 }
196
197 if (memcmp (ehdr, &expected_ehdr, sizeof (GElf_Ehdr)) != 0)
198 {
199 puts ("ELF header does not match");
200 result = 1;
201 goto out_close2;
202 }
203
204 for (cnt = 1; cnt < 3; ++cnt)
205 {
Roland McGrathe9ca7382010-02-15 15:15:50 -0800206 Elf_Scn *escn;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000207 GElf_Shdr shdr_mem;
208 GElf_Shdr *shdr;
209
Roland McGrathe9ca7382010-02-15 15:15:50 -0800210 escn = elf_getscn (elf, cnt);
211 if (escn == NULL)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000212 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700213 printf ("cannot get section %zd: %s\n", cnt, elf_errmsg (-1));
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000214 result = 1;
215 continue;
216 }
217
Roland McGrathe9ca7382010-02-15 15:15:50 -0800218 shdr = gelf_getshdr (escn, &shdr_mem);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000219 if (shdr == NULL)
220 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700221 printf ("cannot get section header for section %zd: %s\n",
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000222 cnt, elf_errmsg (-1));
223 result = 1;
224 continue;
225 }
226
227 if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
228 scnnames[cnt]) != 0)
229 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700230 printf ("section %zd's name differs: %s vs %s\n", cnt,
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000231 elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
232 scnnames[cnt]);
233 result = 1;
234 }
235
236 if (shdr->sh_type != (cnt == 2 ? SHT_STRTAB : SHT_PROGBITS))
237 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700238 printf ("section %zd's type differs\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000239 result = 1;
240 }
241
242 if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
243 || (cnt == 2 && shdr->sh_flags != 0))
244 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700245 printf ("section %zd's flags differs\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000246 result = 1;
247 }
248
249 if (shdr->sh_addr != 0)
250 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700251 printf ("section %zd's address differs\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000252 result = 1;
253 }
254
255 if ((cnt == 1 && shdr->sh_offset != ((sizeof (Elf32_Ehdr) + 15) & ~15))
256 || (cnt == 2
257 && shdr->sh_offset != (((sizeof (Elf32_Ehdr) + 15) & ~15)
258 + sizeof (expecteddata))))
259 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700260 printf ("section %zd's offset differs\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000261 result = 1;
262 }
263
264 if ((cnt == 1 && shdr->sh_size != sizeof (expecteddata))
265 || (cnt == 2 && shdr->sh_size != 17))
266 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700267 printf ("section %zd's size differs\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000268 result = 1;
269 }
270
271 if (shdr->sh_link != 0)
272 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700273 printf ("section %zd's link differs\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000274 result = 1;
275 }
276
277 if (shdr->sh_info != 0)
278 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700279 printf ("section %zd's info differs\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000280 result = 1;
281 }
282
283 if ((cnt == 1 && shdr->sh_addralign != 16)
284 || (cnt != 1 && shdr->sh_addralign != 1))
285 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700286 printf ("section %zd's addralign differs\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000287 result = 1;
288 }
289
290 if (shdr->sh_entsize != 0)
291 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700292 printf ("section %zd's entsize differs\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000293 result = 1;
294 }
295
296 if (cnt == 1)
297 {
Roland McGrathe9ca7382010-02-15 15:15:50 -0800298 Elf_Data *data = elf_getdata (escn, NULL);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000299
300 if (data == NULL)
301 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700302 printf ("cannot get data of section %zd\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000303 result = 1;
304 }
305 else
306 {
307 if (data->d_size != sizeof (expecteddata))
308 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700309 printf ("data block size of section %zd wrong: got %zd, "
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000310 "expected 96\n", cnt, data->d_size);
311 result = 1;
312 }
313
314 if (memcmp (data->d_buf, expecteddata, sizeof (expecteddata))
315 != 0)
316 {
Chih-Hung Hsieh2ec95732015-09-04 10:13:02 -0700317 printf ("data block content of section %zd wrong\n", cnt);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000318 result = 1;
319 }
320 }
321 }
322 }
323
324 out_close2:
325 elf_end (elf);
326 out_close:
327 close (fd);
328 out:
329 /* We don't need the file anymore. */
330 unlink (fname);
331
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000332 ebl_closebackend (ebl);
333
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000334 return result;
335}