1 /* Copyright (C) 2002, 2005 Red Hat, Inc.
2 This file is part of Red Hat elfutils.
3 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5 Red Hat elfutils is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by the
7 Free Software Foundation; version 2 of the License.
9 Red Hat elfutils is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with Red Hat elfutils; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
18 Red Hat elfutils is an included package of the Open Invention Network.
19 An included package of the Open Invention Network is a package for which
20 Open Invention Network licensees cross-license their patents. No patent
21 license is granted, either expressly or impliedly, by designation as an
22 included package. Should you wish to participate in the Open Invention
23 Network licensing program, please visit www.openinventionnetwork.com
24 <http://www.openinventionnetwork.com>. */
31 #include ELFUTILS_HEADER(asm)
38 static const char fname[] = "asm-tst2-out.o";
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] =
82 elf_version (EV_CURRENT);
84 Ebl *ebl = ebl_openbackend_machine (EM_386);
87 puts ("cannot open backend library");
91 ctx = asm_begin (fname, ebl, false);
94 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
98 /* Create two sections. */
99 scn1 = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
100 scn2 = asm_newsubscn (scn1, 1);
101 if (scn1 == NULL || scn2 == NULL)
103 printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
108 /* Special alignment for the .text section. */
109 if (asm_align (scn1, 16) != 0)
111 printf ("cannot align .text section: %s\n", asm_errmsg (-1));
115 /* Add a few strings. */
116 if (asm_addstrz (scn1, "one", 4) != 0)
118 printf ("cannot insert first string: %s\n", asm_errmsg (-1));
121 if (asm_addstrz (scn2, "three", 0) != 0)
123 printf ("cannot insert second string: %s\n", asm_errmsg (-1));
126 if (asm_addstrz (scn1, "two", 4) != 0)
128 printf ("cannot insert third string: %s\n", asm_errmsg (-1));
132 /* Create the output file. */
133 if (asm_end (ctx) != 0)
135 printf ("cannot create output file: %s\n", asm_errmsg (-1));
140 /* Check the file. */
141 fd = open (fname, O_RDONLY);
144 printf ("cannot open generated file: %m\n");
149 elf = elf_begin (fd, ELF_C_READ, NULL);
152 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
156 if (elf_kind (elf) != ELF_K_ELF)
158 puts ("not a valid ELF file");
163 ehdr = gelf_getehdr (elf, &ehdr_mem);
166 printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
171 if (memcmp (ehdr, &expected_ehdr, sizeof (GElf_Ehdr)) != 0)
173 puts ("ELF header does not match");
178 for (cnt = 1; cnt < 3; ++cnt)
184 scn = elf_getscn (elf, cnt);
187 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
192 shdr = gelf_getshdr (scn, &shdr_mem);
195 printf ("cannot get section header for section %Zd: %s\n",
196 cnt, elf_errmsg (-1));
201 if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
204 printf ("section %Zd's name differs: %s vs %s\n", cnt,
205 elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
210 if (shdr->sh_type != (cnt == 2 ? SHT_STRTAB : SHT_PROGBITS))
212 printf ("section %Zd's type differs\n", cnt);
216 if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
217 || (cnt == 2 && shdr->sh_flags != 0))
219 printf ("section %Zd's flags differs\n", cnt);
223 if (shdr->sh_addr != 0)
225 printf ("section %Zd's address differs\n", cnt);
229 if ((cnt == 1 && shdr->sh_offset != ((sizeof (Elf32_Ehdr) + 15) & ~15))
231 && shdr->sh_offset != (((sizeof (Elf32_Ehdr) + 15) & ~15)
234 + strlen ("three") + 1)))
236 printf ("section %Zd's offset differs\n", cnt);
240 if ((cnt == 1 && shdr->sh_size != (strlen ("one") + 1
242 + strlen ("three") + 1))
243 || (cnt == 2 && shdr->sh_size != 17))
245 printf ("section %Zd's size differs\n", cnt);
249 if (shdr->sh_link != 0)
251 printf ("section %Zd's link differs\n", cnt);
255 if (shdr->sh_info != 0)
257 printf ("section %Zd's info differs\n", cnt);
261 if ((cnt == 1 && shdr->sh_addralign != 16)
262 || (cnt != 1 && shdr->sh_addralign != 1))
264 printf ("section %Zd's addralign differs\n", cnt);
268 if (shdr->sh_entsize != 0)
270 printf ("section %Zd's entsize differs\n", cnt);
280 /* We don't need the file anymore. */
283 ebl_closebackend (ebl);