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-tst3-out.o";
33 static const char *scnnames[5] =
43 static unsigned int scntypes[5] =
66 elf_version (EV_CURRENT);
68 Ebl *ebl = ebl_openbackend_machine (EM_386);
71 puts ("cannot open backend library");
75 ctx = asm_begin (fname, ebl, false);
78 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
82 /* Create two sections. */
83 scn1 = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
84 scn2 = asm_newsubscn (scn1, 1);
85 if (scn1 == NULL || scn2 == NULL)
87 printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
92 /* Special alignment for the .text section. */
93 if (asm_align (scn1, 16) != 0)
95 printf ("cannot align .text section: %s\n", asm_errmsg (-1));
99 /* Add a few strings with names. */
100 if (asm_newsym (scn1, "one", 4, STT_OBJECT, STB_GLOBAL) == NULL)
102 printf ("cannot create first name: %s\n", asm_errmsg (-1));
105 if (asm_addstrz (scn1, "one", 4) != 0)
107 printf ("cannot insert first string: %s\n", asm_errmsg (-1));
110 if (asm_newsym (scn2, "three", 6, STT_OBJECT, STB_WEAK) == NULL)
112 printf ("cannot create second name: %s\n", asm_errmsg (-1));
115 if (asm_addstrz (scn2, "three", 0) != 0)
117 printf ("cannot insert second string: %s\n", asm_errmsg (-1));
120 if (asm_newsym (scn1, "two", 4, STT_OBJECT, STB_LOCAL) == NULL)
122 printf ("cannot create third name: %s\n", asm_errmsg (-1));
125 if (asm_addstrz (scn1, "two", 4) != 0)
127 printf ("cannot insert third string: %s\n", asm_errmsg (-1));
131 /* Create the output file. */
132 if (asm_end (ctx) != 0)
134 printf ("cannot create output file: %s\n", asm_errmsg (-1));
139 /* Check the file. */
140 fd = open (fname, O_RDONLY);
143 printf ("cannot open generated file: %m\n");
148 elf = elf_begin (fd, ELF_C_READ, NULL);
151 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
155 if (elf_kind (elf) != ELF_K_ELF)
157 puts ("not a valid ELF file");
162 ehdr = gelf_getehdr (elf, &ehdr_mem);
165 printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
170 for (cnt = 1; cnt < 5; ++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 != scntypes[cnt])
204 printf ("section %Zd's type differs\n", cnt);
208 if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
209 || (cnt != 1 && 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);
225 if (shdr->sh_link != 2)
227 puts ("symbol table has incorrect link");
231 data = elf_getdata (scn, NULL);
234 puts ("cannot get data of symbol table");
242 inner < (shdr->sh_size
243 / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT));
249 sym = gelf_getsym (data, inner, &sym_mem);
252 printf ("cannot get symbol %zu: %s\n",
253 inner, elf_errmsg (-1));
258 /* The order of the third and fourth entry depends
259 on how the hash table is organized. */
260 static const char *names[4] =
267 static const int info[4] =
269 [0] = GELF_ST_INFO (STB_LOCAL, STT_NOTYPE),
270 [1] = GELF_ST_INFO (STB_LOCAL, STT_OBJECT),
271 [2] = GELF_ST_INFO (STB_GLOBAL, STT_OBJECT),
272 [3] = GELF_ST_INFO (STB_WEAK, STT_OBJECT)
274 static const unsigned value[4] =
282 if (strcmp (names[inner],
283 elf_strptr (elf, shdr->sh_link,
286 printf ("symbol %zu has different name\n", inner);
290 if (sym->st_value != value[inner])
292 printf ("symbol %zu has wrong value\n", inner);
296 if (sym->st_other != 0)
298 printf ("symbol %zu has wrong other info\n", inner);
302 if (sym->st_shndx != 1)
304 printf ("symbol %zu has wrong section reference\n",
309 if (sym->st_info != info[inner])
311 printf ("symbol %zu has wrong type or binding\n",
316 if ((inner != 3 && sym->st_size != 4)
317 || (inner == 3 && sym->st_size != 6))
319 printf ("symbol %zu has wrong size\n", inner);
333 /* We don't need the file anymore. */
336 ebl_closebackend (ebl);