Home | History | Annotate | Download | only in tests
      1 /* Copyright (C) 2002, 2005 Red Hat, Inc.
      2    This file is part of Red Hat elfutils.
      3    Written by Ulrich Drepper <drepper (at) 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-tst8-out.o";
     39 
     40 
     41 int
     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_newabssym (ctx, "tst8-out.s", 4, 0xfeedbeef, STT_FILE, STB_LOCAL)
     67       == NULL)
     68     {
     69       printf ("cannot create absolute symbol: %s\n", asm_errmsg (-1));
     70       asm_abort (ctx);
     71       return 1;
     72     }
     73 
     74   /* Create the output file.  */
     75   if (asm_end (ctx) != 0)
     76     {
     77       printf ("cannot create output file: %s\n", asm_errmsg (-1));
     78       asm_abort (ctx);
     79       return 1;
     80     }
     81 
     82   /* Check the file.  */
     83   fd = open (fname, O_RDONLY);
     84   if (fd == -1)
     85     {
     86       printf ("cannot open generated file: %m\n");
     87       result = 1;
     88       goto out;
     89     }
     90 
     91   elf = elf_begin (fd, ELF_C_READ, NULL);
     92   if (elf == NULL)
     93     {
     94       printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
     95       result = 1;
     96       goto out_close;
     97     }
     98   if (elf_kind (elf) != ELF_K_ELF)
     99     {
    100       puts ("not a valid ELF file");
    101       result = 1;
    102       goto out_close2;
    103     }
    104 
    105   for (cnt = 1; 1; ++cnt)
    106     {
    107       Elf_Scn *scn;
    108       GElf_Shdr shdr_mem;
    109       GElf_Shdr *shdr;
    110 
    111       scn = elf_getscn (elf, cnt);
    112       if (scn == NULL)
    113 	{
    114 	  printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
    115 	  result = 1;
    116 	  continue;
    117 	}
    118 
    119       shdr = gelf_getshdr (scn, &shdr_mem);
    120       if (shdr == NULL)
    121 	{
    122 	  printf ("cannot get section header for section %Zd: %s\n",
    123 		  cnt, elf_errmsg (-1));
    124 	  result = 1;
    125 	  continue;
    126 	}
    127       /* We are looking for the symbol table.  */
    128       if (shdr->sh_type != SHT_SYMTAB)
    129 	continue;
    130 
    131       for (cnt = 1; cnt< (shdr->sh_size
    132 			  / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT));
    133 	   ++cnt)
    134 	{
    135 	  GElf_Sym sym_mem;
    136 	  GElf_Sym *sym;
    137 
    138 	  if (cnt > 1)
    139 	    {
    140 	      puts ("too many symbol");
    141 	      result = 1;
    142 	      break;
    143 	    }
    144 
    145 	  sym = gelf_getsym (elf_getdata (scn, NULL), cnt, &sym_mem);
    146 	  if (sym == NULL)
    147 	    {
    148 	      printf ("cannot get symbol %zu: %s\n", cnt, elf_errmsg (-1));
    149 	      result = 1;
    150 	    }
    151 	  else
    152 	    {
    153 	      if (sym->st_shndx != SHN_ABS)
    154 		{
    155 		  printf ("expected common symbol, got section %u\n",
    156 			  (unsigned int) sym->st_shndx);
    157 		  result = 1;
    158 		}
    159 
    160 	      if (sym->st_value != 0xfeedbeef)
    161 		{
    162 		  printf ("requested value 0xfeedbeef, is %#" PRIxMAX "\n",
    163 			  (uintmax_t) sym->st_value);
    164 		  result = 1;
    165 		}
    166 
    167 	      if (sym->st_size != 4)
    168 		{
    169 		  printf ("requested size 4, is %" PRIuMAX "\n",
    170 			  (uintmax_t) sym->st_value);
    171 		  result = 1;
    172 		}
    173 
    174 	      if (GELF_ST_TYPE (sym->st_info) != STT_FILE)
    175 		{
    176 		  printf ("requested type FILE, is %u\n",
    177 			  (unsigned int) GELF_ST_TYPE (sym->st_info));
    178 		  result = 1;
    179 		}
    180 	    }
    181 	}
    182 
    183       break;
    184     }
    185 
    186  out_close2:
    187   elf_end (elf);
    188  out_close:
    189   close (fd);
    190  out:
    191   /* We don't need the file anymore.  */
    192   unlink (fname);
    193 
    194   ebl_closebackend (ebl);
    195 
    196   return result;
    197 }
    198