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/>. */
24 #include ELFUTILS_HEADER(asm)
30 static const char fname[] = "asm-tst7-out.o";
42 elf_version (EV_CURRENT);
44 Ebl *ebl = ebl_openbackend_machine (EM_386);
47 puts ("cannot open backend library");
51 ctx = asm_begin (fname, ebl, false);
54 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
58 if (asm_newcomsym (ctx, "commsym", 4, 16) == NULL)
60 printf ("cannot create common symbol: %s\n", asm_errmsg (-1));
65 /* Create the output file. */
66 if (asm_end (ctx) != 0)
68 printf ("cannot create output file: %s\n", asm_errmsg (-1));
74 fd = open (fname, O_RDONLY);
77 printf ("cannot open generated file: %m\n");
82 elf = elf_begin (fd, ELF_C_READ, NULL);
85 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
89 if (elf_kind (elf) != ELF_K_ELF)
91 puts ("not a valid ELF file");
96 for (cnt = 1; 1; ++cnt)
102 scn = elf_getscn (elf, cnt);
105 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
110 shdr = gelf_getshdr (scn, &shdr_mem);
113 printf ("cannot get section header for section %Zd: %s\n",
114 cnt, elf_errmsg (-1));
118 /* We are looking for the symbol table. */
119 if (shdr->sh_type != SHT_SYMTAB)
122 for (cnt = 1; cnt< (shdr->sh_size
123 / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT));
131 puts ("too many symbol");
136 sym = gelf_getsym (elf_getdata (scn, NULL), cnt, &sym_mem);
139 printf ("cannot get symbol %zu: %s\n", cnt, elf_errmsg (-1));
144 if (sym->st_shndx != SHN_COMMON)
146 printf ("expected common symbol, got section %u\n",
147 (unsigned int) sym->st_shndx);
151 if (sym->st_value != 16)
153 printf ("requested alignment 16, is %" PRIuMAX "\n",
154 (uintmax_t) sym->st_value);
158 if (sym->st_size != 4)
160 printf ("requested size 4, is %" PRIuMAX "\n",
161 (uintmax_t) sym->st_value);
175 /* We don't need the file anymore. */
178 ebl_closebackend (ebl);