1 /* Copyright (C) 2002, 2005 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/>. */
23 #include ELFUTILS_HEADER(asm)
30 static const char fname[] = "asm-tst2-out.o";
33 static const GElf_Ehdr expected_ehdr =
35 .e_ident = { [EI_MAG0] = ELFMAG0,
39 [EI_CLASS] = ELFCLASS32,
40 [EI_DATA] = ELFDATA2LSB,
41 [EI_VERSION] = EV_CURRENT },
44 .e_version = EV_CURRENT,
46 .e_ehsize = sizeof (Elf32_Ehdr),
47 .e_shentsize = sizeof (Elf32_Shdr),
53 static const char *scnnames[3] =
74 elf_version (EV_CURRENT);
76 Ebl *ebl = ebl_openbackend_machine (EM_386);
79 puts ("cannot open backend library");
83 ctx = asm_begin (fname, ebl, false);
86 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
90 /* Create two sections. */
91 scn1 = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
92 scn2 = asm_newsubscn (scn1, 1);
93 if (scn1 == NULL || scn2 == NULL)
95 printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
100 /* Special alignment for the .text section. */
101 if (asm_align (scn1, 16) != 0)
103 printf ("cannot align .text section: %s\n", asm_errmsg (-1));
107 /* Add a few strings. */
108 if (asm_addstrz (scn1, "one", 4) != 0)
110 printf ("cannot insert first string: %s\n", asm_errmsg (-1));
113 if (asm_addstrz (scn2, "three", 0) != 0)
115 printf ("cannot insert second string: %s\n", asm_errmsg (-1));
118 if (asm_addstrz (scn1, "two", 4) != 0)
120 printf ("cannot insert third string: %s\n", asm_errmsg (-1));
124 /* Create the output file. */
125 if (asm_end (ctx) != 0)
127 printf ("cannot create output file: %s\n", asm_errmsg (-1));
132 /* Check the file. */
133 fd = open (fname, O_RDONLY);
136 printf ("cannot open generated file: %m\n");
141 elf = elf_begin (fd, ELF_C_READ, NULL);
144 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
148 if (elf_kind (elf) != ELF_K_ELF)
150 puts ("not a valid ELF file");
155 ehdr = gelf_getehdr (elf, &ehdr_mem);
158 printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
163 if (memcmp (ehdr, &expected_ehdr, sizeof (GElf_Ehdr)) != 0)
165 puts ("ELF header does not match");
170 for (cnt = 1; cnt < 3; ++cnt)
176 scn = elf_getscn (elf, cnt);
179 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
184 shdr = gelf_getshdr (scn, &shdr_mem);
187 printf ("cannot get section header for section %Zd: %s\n",
188 cnt, elf_errmsg (-1));
193 if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
196 printf ("section %Zd's name differs: %s vs %s\n", cnt,
197 elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
202 if (shdr->sh_type != (cnt == 2 ? SHT_STRTAB : SHT_PROGBITS))
204 printf ("section %Zd's type differs\n", cnt);
208 if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
209 || (cnt == 2 && shdr->sh_flags != 0))
211 printf ("section %Zd's flags differs\n", cnt);
215 if (shdr->sh_addr != 0)
217 printf ("section %Zd's address differs\n", cnt);
221 if ((cnt == 1 && shdr->sh_offset != ((sizeof (Elf32_Ehdr) + 15) & ~15))
223 && shdr->sh_offset != (((sizeof (Elf32_Ehdr) + 15) & ~15)
226 + strlen ("three") + 1)))
228 printf ("section %Zd's offset differs\n", cnt);
232 if ((cnt == 1 && shdr->sh_size != (strlen ("one") + 1
234 + strlen ("three") + 1))
235 || (cnt == 2 && shdr->sh_size != 17))
237 printf ("section %Zd's size differs\n", cnt);
241 if (shdr->sh_link != 0)
243 printf ("section %Zd's link differs\n", cnt);
247 if (shdr->sh_info != 0)
249 printf ("section %Zd's info differs\n", cnt);
253 if ((cnt == 1 && shdr->sh_addralign != 16)
254 || (cnt != 1 && shdr->sh_addralign != 1))
256 printf ("section %Zd's addralign differs\n", cnt);
260 if (shdr->sh_entsize != 0)
262 printf ("section %Zd's entsize differs\n", cnt);
272 /* We don't need the file anymore. */
275 ebl_closebackend (ebl);