Blame libasm/asm_begin.c

Packit 032894
/* Create descriptor for assembling.
Packit 032894
   Copyright (C) 2002, 2016 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 either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
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 GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   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 <assert.h>
Packit 032894
#include <errno.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdio_ext.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
#include <unistd.h>
Packit 032894
Packit 032894
#include <gelf.h>
Packit 032894
#include "libasmP.h"
Packit 032894
#include <system.h>
Packit 032894
Packit 032894
Packit 032894
static AsmCtx_t *
Packit 032894
prepare_text_output (AsmCtx_t *result)
Packit 032894
{
Packit 032894
  if (result->fd == -1)
Packit 032894
    result->out.file = stdout;
Packit 032894
  else
Packit 032894
    {
Packit 032894
      result->out.file = fdopen (result->fd, "a");
Packit 032894
      if (result->out.file == NULL)
Packit 032894
	{
Packit 032894
	  close (result->fd);
Packit 032894
	  free (result);
Packit 032894
	  result = NULL;
Packit 032894
	}
Packit 032894
      else
Packit 032894
	__fsetlocking (result->out.file, FSETLOCKING_BYCALLER);
Packit 032894
    }
Packit 032894
Packit 032894
  return result;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
static AsmCtx_t *
Packit 032894
prepare_binary_output (AsmCtx_t *result, Ebl *ebl)
Packit 032894
{
Packit 032894
  GElf_Ehdr *ehdr;
Packit 032894
  GElf_Ehdr ehdr_mem;
Packit 032894
Packit 032894
  /* Create the ELF descriptor for the file.  */
Packit 032894
  result->out.elf = elf_begin (result->fd, ELF_C_WRITE_MMAP, NULL);
Packit 032894
  if (result->out.elf == NULL)
Packit 032894
    {
Packit 032894
    err_libelf:
Packit 032894
      unlink (result->tmp_fname);
Packit 032894
      close (result->fd);
Packit 032894
      free (result);
Packit 032894
      __libasm_seterrno (ASM_E_LIBELF);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  /* Create the ELF header for the output file.  */
Packit 032894
  int class = ebl_get_elfclass (ebl);
Packit 032894
  if (gelf_newehdr (result->out.elf, class) == 0)
Packit 032894
    goto err_libelf;
Packit 032894
Packit 032894
  ehdr = gelf_getehdr (result->out.elf, &ehdr_mem);
Packit 032894
  /* If this failed we are in trouble.  */
Packit 032894
  assert (ehdr != NULL);
Packit 032894
Packit 032894
  /* We create an object file.  */
Packit 032894
  ehdr->e_type = ET_REL;
Packit 032894
  /* Set the ELF version.  */
Packit 032894
  ehdr->e_version = EV_CURRENT;
Packit 032894
Packit 032894
  /* Use the machine, class, and endianess values from the Ebl descriptor.  */
Packit 032894
  ehdr->e_machine = ebl_get_elfmachine (ebl);
Packit 032894
  ehdr->e_ident[EI_CLASS] = class;
Packit 032894
  ehdr->e_ident[EI_DATA] = ebl_get_elfdata (ebl);
Packit 032894
Packit 032894
  memcpy (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG);
Packit 032894
Packit 032894
  /* Write the ELF header information back.  */
Packit 032894
  (void) gelf_update_ehdr (result->out.elf, ehdr);
Packit 032894
Packit 032894
  /* No section so far.  */
Packit 032894
  result->section_list = NULL;
Packit 032894
Packit 032894
  /* Initialize the hash table.  */
Packit 032894
  asm_symbol_tab_init (&result->symbol_tab, 67);
Packit 032894
  result->nsymbol_tab = 0;
Packit 032894
  /* And the string tables.  */
Packit 032894
  result->section_strtab = dwelf_strtab_init (true);
Packit 032894
  result->symbol_strtab = dwelf_strtab_init (true);
Packit 032894
Packit 032894
  /* We have no section groups so far.  */
Packit 032894
  result->groups = NULL;
Packit 032894
  result->ngroups = 0;
Packit 032894
Packit 032894
  return result;
Packit 032894
}
Packit 032894
Packit 032894
Packit 032894
AsmCtx_t *
Packit 032894
asm_begin (const char *fname, Ebl *ebl, bool textp)
Packit 032894
{
Packit 032894
  if (fname == NULL && ! textp)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  size_t fname_len = fname != NULL ? strlen (fname) : 0;
Packit 032894
Packit 032894
  /* Create the file descriptor.  We do not generate the output file
Packit 032894
     right away.  Instead we create a temporary file in the same
Packit 032894
     directory which, if everything goes alright, will replace a
Packit 032894
     possibly existing file with the given name.  */
Packit 032894
  AsmCtx_t *result
Packit 032894
    = (AsmCtx_t *) malloc (sizeof (AsmCtx_t) + 2 * fname_len + 9);
Packit 032894
  if (result == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
      /* Initialize the lock.  */
Packit 032894
      rwlock_init (result->lock);
Packit 032894
Packit 032894
  if (fname != NULL)
Packit 032894
    {
Packit 032894
      /* Create the name of the temporary file.  */
Packit 032894
      result->fname = stpcpy (mempcpy (result->tmp_fname, fname, fname_len),
Packit 032894
			      ".XXXXXX") + 1;
Packit 032894
      memcpy (result->fname, fname, fname_len + 1);
Packit 032894
Packit 032894
      /* Create the temporary file.  */
Packit 032894
      result->fd = mkstemp (result->tmp_fname);
Packit 032894
      if (result->fd == -1)
Packit 032894
	{
Packit 032894
	  int save_errno = errno;
Packit 032894
	  free (result);
Packit 032894
	  __libasm_seterrno (ASM_E_CANNOT_CREATE);
Packit 032894
	  errno = save_errno;
Packit 032894
	  return NULL;
Packit 032894
	}
Packit 032894
    }
Packit 032894
  else
Packit 032894
    result->fd = -1;
Packit 032894
Packit 032894
  /* Initialize the counter for temporary symbols.  */
Packit 032894
  result->tempsym_count = 0;
Packit 032894
Packit 032894
  /* Now we differentiate between textual and binary output.   */
Packit 032894
  result->textp = textp;
Packit 032894
  if (textp)
Packit 032894
    result = prepare_text_output (result);
Packit 032894
  else
Packit 032894
    result = prepare_binary_output (result, ebl);
Packit 032894
Packit 032894
  return result;
Packit 032894
}