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-tst1-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[4] =
75 elf_version (EV_CURRENT);
77 Ebl *ebl = ebl_openbackend_machine (EM_386);
80 puts ("cannot open backend library");
84 ctx = asm_begin (fname, ebl, false);
87 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
91 /* Create two sections. */
92 scn1 = asm_newscn (ctx, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
93 scn2 = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
94 if (scn1 == NULL || scn2 == NULL)
96 printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
101 /* Special alignment for the .text section. */
102 if (asm_align (scn1, 32) != 0)
104 printf ("cannot align .text section: %s\n", asm_errmsg (-1));
108 /* Create the output file. */
109 if (asm_end (ctx) != 0)
111 printf ("cannot create output file: %s\n", asm_errmsg (-1));
116 /* Check the file. */
117 fd = open (fname, O_RDONLY);
120 printf ("cannot open generated file: %m\n");
125 elf = elf_begin (fd, ELF_C_READ, NULL);
128 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
132 if (elf_kind (elf) != ELF_K_ELF)
134 puts ("not a valid ELF file");
139 ehdr = gelf_getehdr (elf, &ehdr_mem);
142 printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
147 if (memcmp (ehdr, &expected_ehdr, sizeof (GElf_Ehdr)) != 0)
149 puts ("ELF header does not match");
154 for (cnt = 1; cnt < 4; ++cnt)
160 scn = elf_getscn (elf, cnt);
163 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
168 shdr = gelf_getshdr (scn, &shdr_mem);
171 printf ("cannot get section header for section %Zd: %s\n",
172 cnt, elf_errmsg (-1));
177 if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
180 printf ("section %Zd's name differs: %s vs %s\n", cnt,
181 elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
186 if (shdr->sh_type != (cnt == 3 ? SHT_STRTAB : SHT_PROGBITS))
188 printf ("section %Zd's type differs\n", cnt);
192 if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_EXECINSTR))
193 || (cnt == 2 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
194 || (cnt == 3 && shdr->sh_flags != 0))
196 printf ("section %Zd's flags differs\n", cnt);
200 if (shdr->sh_addr != 0)
202 printf ("section %Zd's address differs\n", cnt);
206 if (shdr->sh_offset != ((sizeof (Elf32_Ehdr) + 31) & ~31))
208 printf ("section %Zd's offset differs\n", cnt);
212 if ((cnt != 3 && shdr->sh_size != 0)
213 || (cnt == 3 && shdr->sh_size != 23))
215 printf ("section %Zd's size differs\n", cnt);
219 if (shdr->sh_link != 0)
221 printf ("section %Zd's link differs\n", cnt);
225 if (shdr->sh_info != 0)
227 printf ("section %Zd's info differs\n", cnt);
231 if ((cnt == 1 && shdr->sh_addralign != 32)
232 || (cnt != 1 && shdr->sh_addralign != 1))
234 printf ("section %Zd's addralign differs\n", cnt);
238 if (shdr->sh_entsize != 0)
240 printf ("section %Zd's entsize differs\n", cnt);
250 /* We don't need the file anymore. */
253 ebl_closebackend (ebl);