blob: a875eb4c2f1f99558b52f37c66aa93fc26984edc [file] [log] [blame]
Ulrich Dreppera38998e2005-08-03 02:05:39 +00001/* Copyright (C) 2002, 2005 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02002 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00003 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4
Mark Wielaardde2ed972012-06-05 17:15:16 +02005 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Mark Wielaardde2ed972012-06-05 17:15:16 +020010 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000011 WITHOUT ANY WARRANTY; without even the implied warranty of
Mark Wielaardde2ed972012-06-05 17:15:16 +020012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000014
Mark Wielaardde2ed972012-06-05 17:15:16 +020015 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000017
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000018#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000022#include <fcntl.h>
23#include <inttypes.h>
Roland McGrathd7f8d0c2005-11-17 02:32:03 +000024#include ELFUTILS_HEADER(asm)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000025#include <libelf.h>
26#include <stdio.h>
27#include <unistd.h>
28
29
30static const char fname[] = "asm-tst7-out.o";
31
32
33int
34main (void)
35{
36 int result = 0;
37 size_t cnt;
38 AsmCtx_t *ctx;
39 Elf *elf;
40 int fd;
41
42 elf_version (EV_CURRENT);
43
Ulrich Dreppera38998e2005-08-03 02:05:39 +000044 Ebl *ebl = ebl_openbackend_machine (EM_386);
45 if (ebl == NULL)
46 {
47 puts ("cannot open backend library");
48 return 1;
49 }
50
51 ctx = asm_begin (fname, ebl, false);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000052 if (ctx == NULL)
53 {
54 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
55 return 1;
56 }
57
58 if (asm_newcomsym (ctx, "commsym", 4, 16) == NULL)
59 {
60 printf ("cannot create common symbol: %s\n", asm_errmsg (-1));
61 asm_abort (ctx);
62 return 1;
63 }
64
65 /* Create the output file. */
66 if (asm_end (ctx) != 0)
67 {
68 printf ("cannot create output file: %s\n", asm_errmsg (-1));
69 asm_abort (ctx);
70 return 1;
71 }
72
73 /* Check the file. */
74 fd = open (fname, O_RDONLY);
75 if (fd == -1)
76 {
77 printf ("cannot open generated file: %m\n");
78 result = 1;
79 goto out;
80 }
81
82 elf = elf_begin (fd, ELF_C_READ, NULL);
83 if (elf == NULL)
84 {
85 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
86 result = 1;
87 goto out_close;
88 }
89 if (elf_kind (elf) != ELF_K_ELF)
90 {
91 puts ("not a valid ELF file");
92 result = 1;
93 goto out_close2;
94 }
95
96 for (cnt = 1; 1; ++cnt)
97 {
98 Elf_Scn *scn;
99 GElf_Shdr shdr_mem;
100 GElf_Shdr *shdr;
101
102 scn = elf_getscn (elf, cnt);
103 if (scn == NULL)
104 {
105 printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
106 result = 1;
107 continue;
108 }
109
110 shdr = gelf_getshdr (scn, &shdr_mem);
111 if (shdr == NULL)
112 {
113 printf ("cannot get section header for section %Zd: %s\n",
114 cnt, elf_errmsg (-1));
115 result = 1;
116 continue;
117 }
118 /* We are looking for the symbol table. */
119 if (shdr->sh_type != SHT_SYMTAB)
120 continue;
121
122 for (cnt = 1; cnt< (shdr->sh_size
123 / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT));
124 ++cnt)
125 {
126 GElf_Sym sym_mem;
127 GElf_Sym *sym;
128
129 if (cnt > 1)
130 {
131 puts ("too many symbol");
132 result = 1;
133 break;
134 }
135
136 sym = gelf_getsym (elf_getdata (scn, NULL), cnt, &sym_mem);
137 if (sym == NULL)
138 {
139 printf ("cannot get symbol %zu: %s\n", cnt, elf_errmsg (-1));
140 result = 1;
141 }
142 else
143 {
144 if (sym->st_shndx != SHN_COMMON)
145 {
146 printf ("expected common symbol, got section %u\n",
147 (unsigned int) sym->st_shndx);
148 result = 1;
149 }
150
151 if (sym->st_value != 16)
152 {
153 printf ("requested alignment 16, is %" PRIuMAX "\n",
154 (uintmax_t) sym->st_value);
155 result = 1;
156 }
157
158 if (sym->st_size != 4)
159 {
160 printf ("requested size 4, is %" PRIuMAX "\n",
161 (uintmax_t) sym->st_value);
162 result = 1;
163 }
164 }
165 }
166
167 break;
168 }
169
170 out_close2:
171 elf_end (elf);
172 out_close:
173 close (fd);
174 out:
175 /* We don't need the file anymore. */
176 unlink (fname);
177
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000178 ebl_closebackend (ebl);
179
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000180 return result;
181}