1 /* Copyright (C) 2002, 2005 Red Hat, Inc.
2    This file is part of elfutils.
3    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4 
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.
9 
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.
14 
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/>.  */
17 
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include <fcntl.h>
23 #include ELFUTILS_HEADER(ebl)
24 #include ELFUTILS_HEADER(asm)
25 #include <libelf.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 
30 
31 static const char fname[] = "asm-tst1-out.o";
32 
33 
34 static const GElf_Ehdr expected_ehdr =
35   {
36     .e_ident = { [EI_MAG0] = ELFMAG0,
37 		 [EI_MAG1] = ELFMAG1,
38 		 [EI_MAG2] = ELFMAG2,
39 		 [EI_MAG3] = ELFMAG3,
40 		 [EI_CLASS] = ELFCLASS32,
41 		 [EI_DATA] = ELFDATA2LSB,
42 		 [EI_VERSION] = EV_CURRENT },
43     .e_type = ET_REL,
44     .e_machine = EM_386,
45     .e_version = EV_CURRENT,
46     .e_shoff = 88,
47     .e_ehsize = sizeof (Elf32_Ehdr),
48     .e_shentsize = sizeof (Elf32_Shdr),
49     .e_shnum = 4,
50     .e_shstrndx = 3
51   };
52 
53 
54 static const char *scnnames[4] =
55   {
56     [0] = "",
57     [1] = ".text",
58     [2] = ".data",
59     [3] = ".shstrtab"
60   };
61 
62 
63 int
main(void)64 main (void)
65 {
66   AsmCtx_t *ctx;
67   AsmScn_t *scn1;
68   AsmScn_t *scn2;
69   int fd;
70   Elf *elf;
71   GElf_Ehdr ehdr_mem;
72   GElf_Ehdr *ehdr;
73   int result = 0;
74   size_t cnt;
75 
76   elf_version (EV_CURRENT);
77 
78   Ebl *ebl = ebl_openbackend_machine (EM_386);
79   if (ebl == NULL)
80     {
81       puts ("cannot open backend library");
82       return 1;
83     }
84 
85   ctx = asm_begin (fname, ebl, false);
86   if (ctx == NULL)
87     {
88       printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
89       return 1;
90     }
91 
92   /* Create two sections.  */
93   scn1 = asm_newscn (ctx, ".text", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR);
94   scn2 = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
95   if (scn1 == NULL || scn2 == NULL)
96     {
97       printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
98       asm_abort (ctx);
99       return 1;
100     }
101 
102   /* Special alignment for the .text section.  */
103   if (asm_align (scn1, 32) != 0)
104     {
105       printf ("cannot align .text section: %s\n", asm_errmsg (-1));
106       result = 1;
107     }
108 
109   /* Create the output file.  */
110   if (asm_end (ctx) != 0)
111     {
112       printf ("cannot create output file: %s\n", asm_errmsg (-1));
113       asm_abort (ctx);
114       return 1;
115     }
116 
117   /* Check the file.  */
118   fd = open (fname, O_RDONLY);
119   if (fd == -1)
120     {
121       printf ("cannot open generated file: %m\n");
122       result = 1;
123       goto out;
124     }
125 
126   elf = elf_begin (fd, ELF_C_READ, NULL);
127   if (elf == NULL)
128     {
129       printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
130       result = 1;
131       goto out_close;
132     }
133   if (elf_kind (elf) != ELF_K_ELF)
134     {
135       puts ("not a valid ELF file");
136       result = 1;
137       goto out_close2;
138     }
139 
140   ehdr = gelf_getehdr (elf, &ehdr_mem);
141   if (ehdr == NULL)
142     {
143       printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
144       result = 1;
145       goto out_close2;
146     }
147 
148   if (memcmp (ehdr, &expected_ehdr, sizeof (GElf_Ehdr)) != 0)
149     {
150       puts ("ELF header does not match");
151       result = 1;
152       goto out_close2;
153     }
154 
155   for (cnt = 1; cnt < 4; ++cnt)
156     {
157       Elf_Scn *scn;
158       GElf_Shdr shdr_mem;
159       GElf_Shdr *shdr;
160 
161       scn = elf_getscn (elf, cnt);
162       if (scn == NULL)
163 	{
164 	  printf ("cannot get section %zd: %s\n", cnt, elf_errmsg (-1));
165 	  result = 1;
166 	  continue;
167 	}
168 
169       shdr = gelf_getshdr (scn, &shdr_mem);
170       if (shdr == NULL)
171 	{
172 	  printf ("cannot get section header for section %zd: %s\n",
173 		  cnt, elf_errmsg (-1));
174 	  result = 1;
175 	  continue;
176 	}
177 
178       if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
179 		  scnnames[cnt]) != 0)
180 	{
181 	  printf ("section %zd's name differs: %s vs %s\n", cnt,
182 		  elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
183 		  scnnames[cnt]);
184 	  result = 1;
185 	}
186 
187       if (shdr->sh_type != (cnt == 3 ? SHT_STRTAB : SHT_PROGBITS))
188 	{
189 	  printf ("section %zd's type differs\n", cnt);
190 	  result = 1;
191 	}
192 
193       if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_EXECINSTR))
194 	  || (cnt == 2 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
195 	  || (cnt == 3 && shdr->sh_flags != 0))
196 	{
197 	  printf ("section %zd's flags differs\n", cnt);
198 	  result = 1;
199 	}
200 
201       if (shdr->sh_addr != 0)
202 	{
203 	  printf ("section %zd's address differs\n", cnt);
204 	  result = 1;
205 	}
206 
207       if (shdr->sh_offset != ((sizeof (Elf32_Ehdr) + 31) & ~31))
208 	{
209 	  printf ("section %zd's offset differs\n", cnt);
210 	  result = 1;
211 	}
212 
213       if ((cnt != 3 && shdr->sh_size != 0)
214 	  || (cnt == 3 && shdr->sh_size != 23))
215 	{
216 	  printf ("section %zd's size differs\n", cnt);
217 	  result = 1;
218 	}
219 
220       if (shdr->sh_link != 0)
221 	{
222 	  printf ("section %zd's link differs\n", cnt);
223 	  result = 1;
224 	}
225 
226       if (shdr->sh_info != 0)
227 	{
228 	  printf ("section %zd's info differs\n", cnt);
229 	  result = 1;
230 	}
231 
232       if ((cnt == 1 && shdr->sh_addralign != 32)
233 	  || (cnt != 1 && shdr->sh_addralign != 1))
234 	{
235 	  printf ("section %zd's addralign differs\n", cnt);
236 	  result = 1;
237 	}
238 
239       if (shdr->sh_entsize != 0)
240 	{
241 	  printf ("section %zd's entsize differs\n", cnt);
242 	  result = 1;
243 	}
244     }
245 
246  out_close2:
247   elf_end (elf);
248  out_close:
249   close (fd);
250  out:
251   /* We don't need the file anymore.  */
252   unlink (fname);
253 
254   ebl_closebackend (ebl);
255 
256   return result;
257 }
258