Blame tests/asm-tst8.c

Packit 032894
/* Copyright (C) 2002, 2005 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
   Written by Ulrich Drepper <drepper@redhat.com>, 2002.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of the GNU General Public License as published by
Packit 032894
   the Free Software Foundation; either version 3 of the License, or
Packit 032894
   (at your option) any later version.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 032894
   GNU General Public License for more details.
Packit 032894
Packit 032894
   You should have received a copy of the GNU General Public License
Packit 032894
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include <fcntl.h>
Packit 032894
#include <inttypes.h>
Packit Service 35cfd5
#include ELFUTILS_HEADER(ebl)
Packit 032894
#include ELFUTILS_HEADER(asm)
Packit 032894
#include <libelf.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
Packit 032894
static const char fname[] = "asm-tst8-out.o";
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
main (void)
Packit 032894
{
Packit 032894
  int result = 0;
Packit 032894
  size_t cnt;
Packit 032894
  AsmCtx_t *ctx;
Packit 032894
  Elf *elf;
Packit 032894
  int fd;
Packit 032894
Packit 032894
  elf_version (EV_CURRENT);
Packit 032894
Packit 032894
  Ebl *ebl = ebl_openbackend_machine (EM_386);
Packit 032894
  if (ebl == NULL)
Packit 032894
    {
Packit 032894
      puts ("cannot open backend library");
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  ctx = asm_begin (fname, ebl, false);
Packit 032894
  if (ctx == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  if (asm_newabssym (ctx, "tst8-out.s", 4, 0xfeedbeef, STT_FILE, STB_LOCAL)
Packit 032894
      == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create absolute symbol: %s\n", asm_errmsg (-1));
Packit 032894
      asm_abort (ctx);
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Create the output file.  */
Packit 032894
  if (asm_end (ctx) != 0)
Packit 032894
    {
Packit 032894
      printf ("cannot create output file: %s\n", asm_errmsg (-1));
Packit 032894
      asm_abort (ctx);
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Check the file.  */
Packit 032894
  fd = open (fname, O_RDONLY);
Packit 032894
  if (fd == -1)
Packit 032894
    {
Packit 032894
      printf ("cannot open generated file: %m\n");
Packit 032894
      result = 1;
Packit 032894
      goto out;
Packit 032894
    }
Packit 032894
Packit 032894
  elf = elf_begin (fd, ELF_C_READ, NULL);
Packit 032894
  if (elf == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
Packit 032894
      result = 1;
Packit 032894
      goto out_close;
Packit 032894
    }
Packit 032894
  if (elf_kind (elf) != ELF_K_ELF)
Packit 032894
    {
Packit 032894
      puts ("not a valid ELF file");
Packit 032894
      result = 1;
Packit 032894
      goto out_close2;
Packit 032894
    }
Packit 032894
Packit 032894
  for (cnt = 1; 1; ++cnt)
Packit 032894
    {
Packit 032894
      Elf_Scn *scn;
Packit 032894
      GElf_Shdr shdr_mem;
Packit 032894
      GElf_Shdr *shdr;
Packit 032894
Packit 032894
      scn = elf_getscn (elf, cnt);
Packit 032894
      if (scn == NULL)
Packit 032894
	{
Packit 032894
	  printf ("cannot get section %zd: %s\n", cnt, elf_errmsg (-1));
Packit 032894
	  result = 1;
Packit 032894
	  continue;
Packit 032894
	}
Packit 032894
Packit 032894
      shdr = gelf_getshdr (scn, &shdr_mem);
Packit 032894
      if (shdr == NULL)
Packit 032894
	{
Packit 032894
	  printf ("cannot get section header for section %zd: %s\n",
Packit 032894
		  cnt, elf_errmsg (-1));
Packit 032894
	  result = 1;
Packit 032894
	  continue;
Packit 032894
	}
Packit 032894
      /* We are looking for the symbol table.  */
Packit 032894
      if (shdr->sh_type != SHT_SYMTAB)
Packit 032894
	continue;
Packit 032894
Packit 032894
      for (cnt = 1; cnt< (shdr->sh_size
Packit 032894
			  / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT));
Packit 032894
	   ++cnt)
Packit 032894
	{
Packit 032894
	  GElf_Sym sym_mem;
Packit 032894
	  GElf_Sym *sym;
Packit 032894
Packit 032894
	  if (cnt > 1)
Packit 032894
	    {
Packit 032894
	      puts ("too many symbol");
Packit 032894
	      result = 1;
Packit 032894
	      break;
Packit 032894
	    }
Packit 032894
Packit 032894
	  sym = gelf_getsym (elf_getdata (scn, NULL), cnt, &sym_mem);
Packit 032894
	  if (sym == NULL)
Packit 032894
	    {
Packit 032894
	      printf ("cannot get symbol %zu: %s\n", cnt, elf_errmsg (-1));
Packit 032894
	      result = 1;
Packit 032894
	    }
Packit 032894
	  else
Packit 032894
	    {
Packit 032894
	      if (sym->st_shndx != SHN_ABS)
Packit 032894
		{
Packit 032894
		  printf ("expected common symbol, got section %u\n",
Packit 032894
			  (unsigned int) sym->st_shndx);
Packit 032894
		  result = 1;
Packit 032894
		}
Packit 032894
Packit 032894
	      if (sym->st_value != 0xfeedbeef)
Packit 032894
		{
Packit 032894
		  printf ("requested value 0xfeedbeef, is %#" PRIxMAX "\n",
Packit 032894
			  (uintmax_t) sym->st_value);
Packit 032894
		  result = 1;
Packit 032894
		}
Packit 032894
Packit 032894
	      if (sym->st_size != 4)
Packit 032894
		{
Packit 032894
		  printf ("requested size 4, is %" PRIuMAX "\n",
Packit 032894
			  (uintmax_t) sym->st_value);
Packit 032894
		  result = 1;
Packit 032894
		}
Packit 032894
Packit 032894
	      if (GELF_ST_TYPE (sym->st_info) != STT_FILE)
Packit 032894
		{
Packit 032894
		  printf ("requested type FILE, is %u\n",
Packit 032894
			  (unsigned int) GELF_ST_TYPE (sym->st_info));
Packit 032894
		  result = 1;
Packit 032894
		}
Packit 032894
	    }
Packit 032894
	}
Packit 032894
Packit 032894
      break;
Packit 032894
    }
Packit 032894
Packit 032894
 out_close2:
Packit 032894
  elf_end (elf);
Packit 032894
 out_close:
Packit 032894
  close (fd);
Packit 032894
 out:
Packit 032894
  /* We don't need the file anymore.  */
Packit 032894
  unlink (fname);
Packit 032894
Packit 032894
  ebl_closebackend (ebl);
Packit 032894
Packit 032894
  return result;
Packit 032894
}