LCOV - code coverage report
Current view: top level - libasm - asm_begin.c (source / functions) Hit Total Coverage
Test: elfutils-0.178 Lines: 41 64 64.1 %
Date: 2019-11-26 23:55:16 Functions: 2 3 66.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* Create descriptor for assembling.
       2             :    Copyright (C) 2002, 2016 Red Hat, Inc.
       3             :    This file is part of elfutils.
       4             :    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
       5             : 
       6             :    This file is free software; you can redistribute it and/or modify
       7             :    it under the terms of either
       8             : 
       9             :      * the GNU Lesser General Public License as published by the Free
      10             :        Software Foundation; either version 3 of the License, or (at
      11             :        your option) any later version
      12             : 
      13             :    or
      14             : 
      15             :      * the GNU General Public License as published by the Free
      16             :        Software Foundation; either version 2 of the License, or (at
      17             :        your option) any later version
      18             : 
      19             :    or both in parallel, as here.
      20             : 
      21             :    elfutils is distributed in the hope that it will be useful, but
      22             :    WITHOUT ANY WARRANTY; without even the implied warranty of
      23             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      24             :    General Public License for more details.
      25             : 
      26             :    You should have received copies of the GNU General Public License and
      27             :    the GNU Lesser General Public License along with this program.  If
      28             :    not, see <http://www.gnu.org/licenses/>.  */
      29             : 
      30             : #ifdef HAVE_CONFIG_H
      31             : # include <config.h>
      32             : #endif
      33             : 
      34             : #include <assert.h>
      35             : #include <errno.h>
      36             : #include <stdio.h>
      37             : #include <stdio_ext.h>
      38             : #include <stdlib.h>
      39             : #include <string.h>
      40             : #include <unistd.h>
      41             : 
      42             : #include <gelf.h>
      43             : #include "libasmP.h"
      44             : #include <system.h>
      45             : 
      46             : 
      47             : static AsmCtx_t *
      48           0 : prepare_text_output (AsmCtx_t *result)
      49             : {
      50           0 :   if (result->fd == -1)
      51           0 :     result->out.file = stdout;
      52             :   else
      53             :     {
      54           0 :       result->out.file = fdopen (result->fd, "a");
      55           0 :       if (result->out.file == NULL)
      56             :         {
      57           0 :           close (result->fd);
      58           0 :           free (result);
      59           0 :           result = NULL;
      60             :         }
      61             :       else
      62           0 :         __fsetlocking (result->out.file, FSETLOCKING_BYCALLER);
      63             :     }
      64             : 
      65           0 :   return result;
      66             : }
      67             : 
      68             : 
      69             : static AsmCtx_t *
      70           9 : prepare_binary_output (AsmCtx_t *result, Ebl *ebl)
      71             : {
      72           9 :   GElf_Ehdr *ehdr;
      73           9 :   GElf_Ehdr ehdr_mem;
      74             : 
      75             :   /* Create the ELF descriptor for the file.  */
      76           9 :   result->out.elf = elf_begin (result->fd, ELF_C_WRITE_MMAP, NULL);
      77           9 :   if (result->out.elf == NULL)
      78             :     {
      79           0 :     err_libelf:
      80           0 :       unlink (result->tmp_fname);
      81           0 :       close (result->fd);
      82           0 :       free (result);
      83           0 :       __libasm_seterrno (ASM_E_LIBELF);
      84           0 :       return NULL;
      85             :     }
      86             : 
      87             :   /* Create the ELF header for the output file.  */
      88           9 :   int class = ebl_get_elfclass (ebl);
      89           9 :   if (gelf_newehdr (result->out.elf, class) == 0)
      90             :     goto err_libelf;
      91             : 
      92           9 :   ehdr = gelf_getehdr (result->out.elf, &ehdr_mem);
      93             :   /* If this failed we are in trouble.  */
      94           9 :   assert (ehdr != NULL);
      95             : 
      96             :   /* We create an object file.  */
      97           9 :   ehdr->e_type = ET_REL;
      98             :   /* Set the ELF version.  */
      99           9 :   ehdr->e_version = EV_CURRENT;
     100             : 
     101             :   /* Use the machine, class, and endianess values from the Ebl descriptor.  */
     102           9 :   ehdr->e_machine = ebl_get_elfmachine (ebl);
     103           9 :   ehdr->e_ident[EI_CLASS] = class;
     104           9 :   ehdr->e_ident[EI_DATA] = ebl_get_elfdata (ebl);
     105             : 
     106           9 :   memcpy (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG);
     107             : 
     108             :   /* Write the ELF header information back.  */
     109           9 :   (void) gelf_update_ehdr (result->out.elf, ehdr);
     110             : 
     111             :   /* No section so far.  */
     112           9 :   result->section_list = NULL;
     113             : 
     114             :   /* Initialize the hash table.  */
     115           9 :   asm_symbol_tab_init (&result->symbol_tab, 67);
     116           9 :   result->nsymbol_tab = 0;
     117             :   /* And the string tables.  */
     118           9 :   result->section_strtab = dwelf_strtab_init (true);
     119           9 :   result->symbol_strtab = dwelf_strtab_init (true);
     120             : 
     121             :   /* We have no section groups so far.  */
     122           9 :   result->groups = NULL;
     123           9 :   result->ngroups = 0;
     124             : 
     125           9 :   return result;
     126             : }
     127             : 
     128             : 
     129             : AsmCtx_t *
     130           9 : asm_begin (const char *fname, Ebl *ebl, bool textp)
     131             : {
     132           9 :   if (fname == NULL && ! textp)
     133             :     return NULL;
     134             : 
     135           9 :   size_t fname_len = fname != NULL ? strlen (fname) : 0;
     136             : 
     137             :   /* Create the file descriptor.  We do not generate the output file
     138             :      right away.  Instead we create a temporary file in the same
     139             :      directory which, if everything goes alright, will replace a
     140             :      possibly existing file with the given name.  */
     141           9 :   AsmCtx_t *result
     142           9 :     = (AsmCtx_t *) malloc (sizeof (AsmCtx_t) + 2 * fname_len + 9);
     143           9 :   if (result == NULL)
     144             :     return NULL;
     145             : 
     146             :       /* Initialize the lock.  */
     147           9 :       rwlock_init (result->lock);
     148             : 
     149           9 :   if (fname != NULL)
     150             :     {
     151             :       /* Create the name of the temporary file.  */
     152          18 :       result->fname = stpcpy (mempcpy (result->tmp_fname, fname, fname_len),
     153           9 :                               ".XXXXXX") + 1;
     154           9 :       memcpy (result->fname, fname, fname_len + 1);
     155             : 
     156             :       /* Create the temporary file.  */
     157           9 :       result->fd = mkstemp (result->tmp_fname);
     158           9 :       if (result->fd == -1)
     159             :         {
     160           0 :           int save_errno = errno;
     161           0 :           free (result);
     162           0 :           __libasm_seterrno (ASM_E_CANNOT_CREATE);
     163           0 :           errno = save_errno;
     164           0 :           return NULL;
     165             :         }
     166             :     }
     167             :   else
     168           0 :     result->fd = -1;
     169             : 
     170             :   /* Initialize the counter for temporary symbols.  */
     171           9 :   result->tempsym_count = 0;
     172             : 
     173             :   /* Now we differentiate between textual and binary output.   */
     174           9 :   result->textp = textp;
     175           9 :   if (textp)
     176           0 :     result = prepare_text_output (result);
     177             :   else
     178           9 :     result = prepare_binary_output (result, ebl);
     179             : 
     180             :   return result;
     181             : }

Generated by: LCOV version 1.13