Blame tests/asm-tst2.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 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 <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
Packit 032894
static const char fname[] = "asm-tst2-out.o";
Packit 032894
Packit 032894
Packit 032894
static const GElf_Ehdr expected_ehdr =
Packit 032894
  {
Packit 032894
    .e_ident = { [EI_MAG0] = ELFMAG0,
Packit 032894
		 [EI_MAG1] = ELFMAG1,
Packit 032894
		 [EI_MAG2] = ELFMAG2,
Packit 032894
		 [EI_MAG3] = ELFMAG3,
Packit 032894
		 [EI_CLASS] = ELFCLASS32,
Packit 032894
		 [EI_DATA] = ELFDATA2LSB,
Packit 032894
		 [EI_VERSION] = EV_CURRENT },
Packit 032894
    .e_type = ET_REL,
Packit 032894
    .e_machine = EM_386,
Packit 032894
    .e_version = EV_CURRENT,
Packit 032894
    .e_shoff = 96,
Packit 032894
    .e_ehsize = sizeof (Elf32_Ehdr),
Packit 032894
    .e_shentsize = sizeof (Elf32_Shdr),
Packit 032894
    .e_shnum = 3,
Packit 032894
    .e_shstrndx = 2
Packit 032894
  };
Packit 032894
Packit 032894
Packit 032894
static const char *scnnames[3] =
Packit 032894
  {
Packit 032894
    [0] = "",
Packit 032894
    [1] = ".data",
Packit 032894
    [2] = ".shstrtab"
Packit 032894
  };
Packit 032894
Packit 032894
Packit 032894
int
Packit 032894
main (void)
Packit 032894
{
Packit 032894
  AsmCtx_t *ctx;
Packit 032894
  AsmScn_t *scn1;
Packit 032894
  AsmScn_t *scn2;
Packit 032894
  int result = 0;
Packit 032894
  int fd;
Packit 032894
  Elf *elf;
Packit 032894
  GElf_Ehdr ehdr_mem;
Packit 032894
  GElf_Ehdr *ehdr;
Packit 032894
  size_t cnt;
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
  /* Create two sections.  */
Packit 032894
  scn1 = asm_newscn (ctx, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
Packit 032894
  scn2 = asm_newsubscn (scn1, 1);
Packit 032894
  if (scn1 == NULL || scn2 == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot create section in output file: %s\n", asm_errmsg (-1));
Packit 032894
      asm_abort (ctx);
Packit 032894
      return 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Special alignment for the .text section.  */
Packit 032894
  if (asm_align (scn1, 16) != 0)
Packit 032894
    {
Packit 032894
      printf ("cannot align .text section: %s\n", asm_errmsg (-1));
Packit 032894
      result = 1;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Add a few strings.  */
Packit 032894
  if (asm_addstrz (scn1, "one", 4) != 0)
Packit 032894
    {
Packit 032894
      printf ("cannot insert first string: %s\n", asm_errmsg (-1));
Packit 032894
      result = 1;
Packit 032894
    }
Packit 032894
  if (asm_addstrz (scn2, "three", 0) != 0)
Packit 032894
    {
Packit 032894
      printf ("cannot insert second string: %s\n", asm_errmsg (-1));
Packit 032894
      result = 1;
Packit 032894
    }
Packit 032894
  if (asm_addstrz (scn1, "two", 4) != 0)
Packit 032894
    {
Packit 032894
      printf ("cannot insert third string: %s\n", asm_errmsg (-1));
Packit 032894
      result = 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
  ehdr = gelf_getehdr (elf, &ehdr_mem);
Packit 032894
  if (ehdr == NULL)
Packit 032894
    {
Packit 032894
      printf ("cannot get ELF header: %s\n", elf_errmsg (-1));
Packit 032894
      result = 1;
Packit 032894
      goto out_close2;
Packit 032894
    }
Packit 032894
Packit 032894
  if (memcmp (ehdr, &expected_ehdr, sizeof (GElf_Ehdr)) != 0)
Packit 032894
    {
Packit 032894
      puts ("ELF header does not match");
Packit 032894
      result = 1;
Packit 032894
      goto out_close2;
Packit 032894
    }
Packit 032894
Packit 032894
  for (cnt = 1; cnt < 3; ++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
Packit 032894
      if (strcmp (elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
Packit 032894
		  scnnames[cnt]) != 0)
Packit 032894
	{
Packit 032894
	  printf ("section %zd's name differs: %s vs %s\n", cnt,
Packit 032894
		  elf_strptr (elf, ehdr->e_shstrndx, shdr->sh_name),
Packit 032894
		  scnnames[cnt]);
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      if (shdr->sh_type != (cnt == 2 ? SHT_STRTAB : SHT_PROGBITS))
Packit 032894
	{
Packit 032894
	  printf ("section %zd's type differs\n", cnt);
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      if ((cnt == 1 && shdr->sh_flags != (SHF_ALLOC | SHF_WRITE))
Packit 032894
	  || (cnt == 2 && shdr->sh_flags != 0))
Packit 032894
	{
Packit 032894
	  printf ("section %zd's flags differs\n", cnt);
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      if (shdr->sh_addr != 0)
Packit 032894
	{
Packit 032894
	  printf ("section %zd's address differs\n", cnt);
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      if ((cnt == 1 && shdr->sh_offset != ((sizeof (Elf32_Ehdr) + 15) & ~15))
Packit 032894
	  || (cnt == 2
Packit 032894
	      && shdr->sh_offset != (((sizeof (Elf32_Ehdr) + 15) & ~15)
Packit 032894
				     + strlen ("one") + 1
Packit 032894
				     + strlen ("two") + 1
Packit 032894
				     + strlen ("three") + 1)))
Packit 032894
	{
Packit 032894
	  printf ("section %zd's offset differs\n", cnt);
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      if ((cnt == 1 && shdr->sh_size != (strlen ("one") + 1
Packit 032894
					 + strlen ("two") + 1
Packit 032894
					 + strlen ("three") + 1))
Packit 032894
	  || (cnt == 2 && shdr->sh_size != 17))
Packit 032894
	{
Packit 032894
	  printf ("section %zd's size differs\n", cnt);
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      if (shdr->sh_link != 0)
Packit 032894
	{
Packit 032894
	  printf ("section %zd's link differs\n", cnt);
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      if (shdr->sh_info != 0)
Packit 032894
	{
Packit 032894
	  printf ("section %zd's info differs\n", cnt);
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      if ((cnt == 1 && shdr->sh_addralign != 16)
Packit 032894
	  || (cnt != 1 && shdr->sh_addralign != 1))
Packit 032894
	{
Packit 032894
	  printf ("section %zd's addralign differs\n", cnt);
Packit 032894
	  result = 1;
Packit 032894
	}
Packit 032894
Packit 032894
      if (shdr->sh_entsize != 0)
Packit 032894
	{
Packit 032894
	  printf ("section %zd's entsize differs\n", cnt);
Packit 032894
	  result = 1;
Packit 032894
	}
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
}