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