1 /* Copyright (C) 2002-2010 Red Hat, Inc.
2 This file is part of elfutils.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5 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.
10 elfutils is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 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/>. */
24 #include ELFUTILS_HEADER(asm)
31 static const char fname[] = "asm-tst9-out.o";
34 static int32_t input[] =
36 0, 1, 129, 510, 2000, 33000, 0x7ffffff, 0x7fffffff
38 #define ninput (sizeof (input) / sizeof (input[0]))
41 static const GElf_Ehdr expected_ehdr =
43 .e_ident = { [EI_MAG0] = ELFMAG0,
47 [EI_CLASS] = ELFCLASS32,
48 [EI_DATA] = ELFDATA2LSB,
49 [EI_VERSION] = EV_CURRENT },
52 .e_version = EV_CURRENT,
54 .e_ehsize = sizeof (Elf32_Ehdr),
55 .e_shentsize = sizeof (Elf32_Shdr),
61 static const char *scnnames[3] =
69 static const char expecteddata[] =
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
94 elf_version (EV_CURRENT);
96 Ebl *ebl = ebl_openbackend_machine (EM_386);
99 puts ("cannot open backend library");
103 ctx = asm_begin (fname, ebl, false);
106 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
110 /* Create two sections. */
111 scn = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
114 printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
119 /* Special alignment for the .text section. */
120 if (asm_align (scn, 16) != 0)
122 printf ("cannot align .text section: %s\n", asm_errmsg (-1));
126 /* Add a few ULEB128 and SLEB128 numbers. */
127 for (cnt = 0; cnt < ninput; ++cnt)
129 if (asm_adduleb128 (scn, input[cnt]) != 0)
131 printf ("cannot insert uleb %" PRIu32 ": %s\n",
132 (uint32_t) input[cnt], asm_errmsg (-1));
136 if (asm_addsleb128 (scn, input[cnt]) != 0)
138 printf ("cannot insert sleb %" PRId32 ": %s\n",
139 input[cnt], asm_errmsg (-1));
143 if (asm_adduleb128 (scn, -input[cnt]) != 0)
145 printf ("cannot insert uleb %" PRIu32 ": %s\n",
146 (uint32_t) -input[cnt], asm_errmsg (-1));
150 if (asm_addsleb128 (scn, -input[cnt]) != 0)
152 printf ("cannot insert sleb %" PRId32 ": %s\n",
153 -input[cnt], asm_errmsg (-1));
158 /* Create the output file. */
159 if (asm_end (ctx) != 0)
161 printf ("cannot create output file: %s\n", asm_errmsg (-1));
166 /* Check the file. */
167 fd = open (fname, O_RDONLY);
170 printf ("cannot open generated file: %m\n");
175 elf = elf_begin (fd, ELF_C_READ, NULL);
178 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
182 if (elf_kind (elf) != ELF_K_ELF)
184 puts ("not a valid ELF file");
189 ehdr = gelf_getehdr (elf, &ehdr_mem);
192 printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
197 if (memcmp (ehdr, &expected_ehdr, sizeof (GElf_Ehdr)) != 0)
199 puts ("ELF header does not match");
204 for (cnt = 1; cnt < 3; ++cnt)
210 escn = elf_getscn (elf, cnt);
213 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
218 shdr = gelf_getshdr (escn, &shdr_mem);
221 printf ("cannot get section header for section %Zd: %s\n",
222 cnt, elf_errmsg (-1));
227 if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
230 printf ("section %Zd's name differs: %s vs %s\n", cnt,
231 elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
236 if (shdr->sh_type != (cnt == 2 ? SHT_STRTAB : SHT_PROGBITS))
238 printf ("section %Zd's type differs\n", cnt);
242 if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
243 || (cnt == 2 && shdr->sh_flags != 0))
245 printf ("section %Zd's flags differs\n", cnt);
249 if (shdr->sh_addr != 0)
251 printf ("section %Zd's address differs\n", cnt);
255 if ((cnt == 1 && shdr->sh_offset != ((sizeof (Elf32_Ehdr) + 15) & ~15))
257 && shdr->sh_offset != (((sizeof (Elf32_Ehdr) + 15) & ~15)
258 + sizeof (expecteddata))))
260 printf ("section %Zd's offset differs\n", cnt);
264 if ((cnt == 1 && shdr->sh_size != sizeof (expecteddata))
265 || (cnt == 2 && shdr->sh_size != 17))
267 printf ("section %Zd's size differs\n", cnt);
271 if (shdr->sh_link != 0)
273 printf ("section %Zd's link differs\n", cnt);
277 if (shdr->sh_info != 0)
279 printf ("section %Zd's info differs\n", cnt);
283 if ((cnt == 1 && shdr->sh_addralign != 16)
284 || (cnt != 1 && shdr->sh_addralign != 1))
286 printf ("section %Zd's addralign differs\n", cnt);
290 if (shdr->sh_entsize != 0)
292 printf ("section %Zd's entsize differs\n", cnt);
298 Elf_Data *data = elf_getdata (escn, NULL);
302 printf ("cannot get data of section %Zd\n", cnt);
307 if (data->d_size != sizeof (expecteddata))
309 printf ("data block size of section %Zd wrong: got %Zd, "
310 "expected 96\n", cnt, data->d_size);
314 if (memcmp (data->d_buf, expecteddata, sizeof (expecteddata))
317 printf ("data block content of section %Zd wrong\n", cnt);
329 /* We don't need the file anymore. */
332 ebl_closebackend (ebl);