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