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-tst1-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[4] =
83 elf_version (EV_CURRENT);
85 Ebl *ebl = ebl_openbackend_machine (EM_386);
88 puts ("cannot open backend library");
92 ctx = asm_begin (fname, ebl, false);
95 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
99 /* Create two sections. */
100 scn1 = asm_newscn (ctx, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
101 scn2 = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
102 if (scn1 == NULL || scn2 == NULL)
104 printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
109 /* Special alignment for the .text section. */
110 if (asm_align (scn1, 32) != 0)
112 printf ("cannot align .text section: %s\n", asm_errmsg (-1));
116 /* Create the output file. */
117 if (asm_end (ctx) != 0)
119 printf ("cannot create output file: %s\n", asm_errmsg (-1));
124 /* Check the file. */
125 fd = open (fname, O_RDONLY);
128 printf ("cannot open generated file: %m\n");
133 elf = elf_begin (fd, ELF_C_READ, NULL);
136 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
140 if (elf_kind (elf) != ELF_K_ELF)
142 puts ("not a valid ELF file");
147 ehdr = gelf_getehdr (elf, &ehdr_mem);
150 printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
155 if (memcmp (ehdr, &expected_ehdr, sizeof (GElf_Ehdr)) != 0)
157 puts ("ELF header does not match");
162 for (cnt = 1; cnt < 4; ++cnt)
168 scn = elf_getscn (elf, cnt);
171 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
176 shdr = gelf_getshdr (scn, &shdr_mem);
179 printf ("cannot get section header for section %Zd: %s\n",
180 cnt, elf_errmsg (-1));
185 if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
188 printf ("section %Zd's name differs: %s vs %s\n", cnt,
189 elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
194 if (shdr->sh_type != (cnt == 3 ? SHT_STRTAB : SHT_PROGBITS))
196 printf ("section %Zd's type differs\n", cnt);
200 if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_EXECINSTR))
201 || (cnt == 2 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
202 || (cnt == 3 && shdr->sh_flags != 0))
204 printf ("section %Zd's flags differs\n", cnt);
208 if (shdr->sh_addr != 0)
210 printf ("section %Zd's address differs\n", cnt);
214 if (shdr->sh_offset != ((sizeof (Elf32_Ehdr) + 31) & ~31))
216 printf ("section %Zd's offset differs\n", cnt);
220 if ((cnt != 3 && shdr->sh_size != 0)
221 || (cnt == 3 && shdr->sh_size != 23))
223 printf ("section %Zd's size differs\n", cnt);
227 if (shdr->sh_link != 0)
229 printf ("section %Zd's link differs\n", cnt);
233 if (shdr->sh_info != 0)
235 printf ("section %Zd's info differs\n", cnt);
239 if ((cnt == 1 && shdr->sh_addralign != 32)
240 || (cnt != 1 && shdr->sh_addralign != 1))
242 printf ("section %Zd's addralign differs\n", cnt);
246 if (shdr->sh_entsize != 0)
248 printf ("section %Zd's entsize differs\n", cnt);
258 /* We don't need the file anymore. */
261 ebl_closebackend (ebl);