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 <inttypes.h>
24 #include ELFUTILS_HEADER(asm)
25 #include ELFUTILS_HEADER(ebl)
26 #include <libelf.h>
27 #include <stdio.h>
28 #include <unistd.h>
29
30
31 static const char fname[] = "asm-tst8-out.o";
32
33
34 int
main(void)35 main (void)
36 {
37 int result = 0;
38 size_t cnt;
39 AsmCtx_t *ctx;
40 Elf *elf;
41 int fd;
42
43 elf_version (EV_CURRENT);
44
45 Ebl *ebl = ebl_openbackend_machine (EM_386);
46 if (ebl == NULL)
47 {
48 puts ("cannot open backend library");
49 return 1;
50 }
51
52 ctx = asm_begin (fname, ebl, false);
53 if (ctx == NULL)
54 {
55 printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
56 return 1;
57 }
58
59 if (asm_newabssym (ctx, "tst8-out.s", 4, 0xfeedbeef, STT_FILE, STB_LOCAL)
60 == NULL)
61 {
62 printf ("cannot create absolute symbol: %s\n", asm_errmsg (-1));
63 asm_abort (ctx);
64 return 1;
65 }
66
67 /* Create the output file. */
68 if (asm_end (ctx) != 0)
69 {
70 printf ("cannot create output file: %s\n", asm_errmsg (-1));
71 asm_abort (ctx);
72 return 1;
73 }
74
75 /* Check the file. */
76 fd = open (fname, O_RDONLY);
77 if (fd == -1)
78 {
79 printf ("cannot open generated file: %m\n");
80 result = 1;
81 goto out;
82 }
83
84 elf = elf_begin (fd, ELF_C_READ, NULL);
85 if (elf == NULL)
86 {
87 printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
88 result = 1;
89 goto out_close;
90 }
91 if (elf_kind (elf) != ELF_K_ELF)
92 {
93 puts ("not a valid ELF file");
94 result = 1;
95 goto out_close2;
96 }
97
98 for (cnt = 1; 1; ++cnt)
99 {
100 Elf_Scn *scn;
101 GElf_Shdr shdr_mem;
102 GElf_Shdr *shdr;
103
104 scn = elf_getscn (elf, cnt);
105 if (scn == NULL)
106 {
107 printf ("cannot get section %zd: %s\n", cnt, elf_errmsg (-1));
108 result = 1;
109 continue;
110 }
111
112 shdr = gelf_getshdr (scn, &shdr_mem);
113 if (shdr == NULL)
114 {
115 printf ("cannot get section header for section %zd: %s\n",
116 cnt, elf_errmsg (-1));
117 result = 1;
118 continue;
119 }
120 /* We are looking for the symbol table. */
121 if (shdr->sh_type != SHT_SYMTAB)
122 continue;
123
124 for (cnt = 1; cnt< (shdr->sh_size
125 / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT));
126 ++cnt)
127 {
128 GElf_Sym sym_mem;
129 GElf_Sym *sym;
130
131 if (cnt > 1)
132 {
133 puts ("too many symbol");
134 result = 1;
135 break;
136 }
137
138 sym = gelf_getsym (elf_getdata (scn, NULL), cnt, &sym_mem);
139 if (sym == NULL)
140 {
141 printf ("cannot get symbol %zu: %s\n", cnt, elf_errmsg (-1));
142 result = 1;
143 }
144 else
145 {
146 if (sym->st_shndx != SHN_ABS)
147 {
148 printf ("expected common symbol, got section %u\n",
149 (unsigned int) sym->st_shndx);
150 result = 1;
151 }
152
153 if (sym->st_value != 0xfeedbeef)
154 {
155 printf ("requested value 0xfeedbeef, is %#" PRIxMAX "\n",
156 (uintmax_t) sym->st_value);
157 result = 1;
158 }
159
160 if (sym->st_size != 4)
161 {
162 printf ("requested size 4, is %" PRIuMAX "\n",
163 (uintmax_t) sym->st_value);
164 result = 1;
165 }
166
167 if (GELF_ST_TYPE (sym->st_info) != STT_FILE)
168 {
169 printf ("requested type FILE, is %u\n",
170 (unsigned int) GELF_ST_TYPE (sym->st_info));
171 result = 1;
172 }
173 }
174 }
175
176 break;
177 }
178
179 out_close2:
180 elf_end (elf);
181 out_close:
182 close (fd);
183 out:
184 /* We don't need the file anymore. */
185 unlink (fname);
186
187 ebl_closebackend (ebl);
188
189 return result;
190 }
191