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-tst3-out.o";
41 static const char *scnnames[5] =
51 static unsigned int scntypes[5] =
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 with names. */
108 if (asm_newsym (scn1, "one", 4, STT_OBJECT, STB_GLOBAL) == NULL)
110 printf ("cannot create first name: %s\n", asm_errmsg (-1));
113 if (asm_addstrz (scn1, "one", 4) != 0)
115 printf ("cannot insert first string: %s\n", asm_errmsg (-1));
118 if (asm_newsym (scn2, "three", 6, STT_OBJECT, STB_WEAK) == NULL)
120 printf ("cannot create second name: %s\n", asm_errmsg (-1));
123 if (asm_addstrz (scn2, "three", 0) != 0)
125 printf ("cannot insert second string: %s\n", asm_errmsg (-1));
128 if (asm_newsym (scn1, "two", 4, STT_OBJECT, STB_LOCAL) == NULL)
130 printf ("cannot create third name: %s\n", asm_errmsg (-1));
133 if (asm_addstrz (scn1, "two", 4) != 0)
135 printf ("cannot insert third string: %s\n", asm_errmsg (-1));
139 /* Create the output file. */
140 if (asm_end (ctx) != 0)
142 printf ("cannot create output file: %s\n", asm_errmsg (-1));
147 /* Check the file. */
148 fd = open (fname, O_RDONLY);
151 printf ("cannot open generated file: %m\n");
156 elf = elf_begin (fd, ELF_C_READ, NULL);
159 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
163 if (elf_kind (elf) != ELF_K_ELF)
165 puts ("not a valid ELF file");
170 ehdr = gelf_getehdr (elf, &ehdr_mem);
173 printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
178 for (cnt = 1; cnt < 5; ++cnt)
184 scn = elf_getscn (elf, cnt);
187 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
192 shdr = gelf_getshdr (scn, &shdr_mem);
195 printf ("cannot get section header for section %Zd: %s\n",
196 cnt, elf_errmsg (-1));
201 if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
204 printf ("section %Zd's name differs: %s vs %s\n", cnt,
205 elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
210 if (shdr->sh_type != scntypes[cnt])
212 printf ("section %Zd's type differs\n", cnt);
216 if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
217 || (cnt != 1 && shdr->sh_flags != 0))
219 printf ("section %Zd's flags differs\n", cnt);
223 if (shdr->sh_addr != 0)
225 printf ("section %Zd's address differs\n", cnt);
233 if (shdr->sh_link != 2)
235 puts ("symbol table has incorrect link");
239 data = elf_getdata (scn, NULL);
242 puts ("cannot get data of symbol table");
250 inner < (shdr->sh_size
251 / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT));
257 sym = gelf_getsym (data, inner, &sym_mem);
260 printf ("cannot get symbol %zu: %s\n",
261 inner, elf_errmsg (-1));
266 /* The order of the third and fourth entry depends
267 on how the hash table is organized. */
268 static const char *names[4] =
275 static const int info[4] =
277 [0] = GELF_ST_INFO (STB_LOCAL, STT_NOTYPE),
278 [1] = GELF_ST_INFO (STB_LOCAL, STT_OBJECT),
279 [2] = GELF_ST_INFO (STB_GLOBAL, STT_OBJECT),
280 [3] = GELF_ST_INFO (STB_WEAK, STT_OBJECT)
282 static const unsigned value[4] =
290 if (strcmp (names[inner],
291 elf_strptr (elf, shdr->sh_link,
294 printf ("symbol %zu has different name\n", inner);
298 if (sym->st_value != value[inner])
300 printf ("symbol %zu has wrong value\n", inner);
304 if (sym->st_other != 0)
306 printf ("symbol %zu has wrong other info\n", inner);
310 if (sym->st_shndx != 1)
312 printf ("symbol %zu has wrong section reference\n",
317 if (sym->st_info != info[inner])
319 printf ("symbol %zu has wrong type or binding\n",
324 if ((inner != 3 && sym->st_size != 4)
325 || (inner == 3 && sym->st_size != 6))
327 printf ("symbol %zu has wrong size\n", inner);
341 /* We don't need the file anymore. */
344 ebl_closebackend (ebl);