Home | History | Annotate | Download | only in libasm
      1 /* Create new section in output file.
      2    Copyright (C) 2002-2011 Red Hat, Inc.
      3    This file is part of Red Hat elfutils.
      4    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      5 
      6    Red Hat elfutils is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU General Public License as published by the
      8    Free Software Foundation; version 2 of the License.
      9 
     10    Red Hat elfutils is distributed in the hope that it will be useful, but
     11    WITHOUT ANY WARRANTY; without even the implied warranty of
     12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     13    General Public License for more details.
     14 
     15    You should have received a copy of the GNU General Public License along
     16    with Red Hat elfutils; if not, write to the Free Software Foundation,
     17    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
     18 
     19    Red Hat elfutils is an included package of the Open Invention Network.
     20    An included package of the Open Invention Network is a package for which
     21    Open Invention Network licensees cross-license their patents.  No patent
     22    license is granted, either expressly or impliedly, by designation as an
     23    included package.  Should you wish to participate in the Open Invention
     24    Network licensing program, please visit www.openinventionnetwork.com
     25    <http://www.openinventionnetwork.com>.  */
     26 
     27 #ifdef HAVE_CONFIG_H
     28 # include <config.h>
     29 #endif
     30 
     31 #include <assert.h>
     32 #include <error.h>
     33 #include <libintl.h>
     34 #include <stdlib.h>
     35 #include <string.h>
     36 
     37 #include <libasmP.h>
     38 #include <libelf.h>
     39 #include <system.h>
     40 
     41 
     42 /* Memory for the default pattern.  The type uses a flexible array
     43    which does work well with a static initializer.  So we play some
     44    dirty tricks here.  */
     45 static const struct
     46 {
     47   struct FillPattern pattern;
     48   char zero;
     49 } xdefault_pattern =
     50   {
     51     .pattern =
     52     {
     53       .len = 1
     54     },
     55     .zero = '\0'
     56   };
     57 const struct FillPattern *__libasm_default_pattern = &xdefault_pattern.pattern;
     58 
     59 
     60 static AsmScn_t *
     61 text_newscn (AsmScn_t *result, GElf_Word type, GElf_Xword flags)
     62 {
     63   /* Buffer where we construct the flag string.  */
     64   char flagstr[sizeof (GElf_Xword) * 8 + 5];
     65   char *wp = flagstr;
     66   const char *typestr = "";
     67 
     68   /* Only write out the flag string if this is the first time the
     69      section is selected.  Some assemblers cannot cope with the
     70      .section pseudo-op otherwise.  */
     71   wp = stpcpy (wp, ", \"");
     72 
     73   if (flags & SHF_WRITE)
     74     *wp++ = 'w';
     75   if (flags & SHF_ALLOC)
     76     *wp++ = 'a';
     77   if (flags & SHF_EXECINSTR)
     78     *wp++ = 'x';
     79   if (flags & SHF_MERGE)
     80     *wp++ = 'M';
     81   if (flags & SHF_STRINGS)
     82     *wp++ = 'S';
     83   if (flags & SHF_LINK_ORDER)
     84     *wp++ = 'L';
     85 
     86   *wp++ = '"';
     87 
     88   if (type == SHT_PROGBITS)
     89     typestr = ",@progbits";
     90   else if (type == SHT_NOBITS)
     91     typestr = ",@nobits";
     92 
     93   /* Terminate the string.  */
     94   *wp = '\0';
     95 
     96   fprintf (result->ctx->out.file, "\t.section \"%s\"%s%s\n",
     97 	   result->name, flagstr, typestr);
     98 
     99   return result;
    100 }
    101 
    102 
    103 static AsmScn_t *
    104 binary_newscn (AsmScn_t *result, GElf_Word type, GElf_Xword flags,
    105 	       size_t scnname_len)
    106 {
    107   GElf_Shdr shdr_mem;
    108   GElf_Shdr *shdr;
    109   Elf_Scn *scn;
    110 
    111   /* The initial subsection has the number zero.  */
    112   result->subsection_id = 0;
    113 
    114   /* We start at offset zero.  */
    115   result->offset = 0;
    116   /* And generic alignment.  */
    117   result->max_align = 1;
    118 
    119   /* No output yet.  */
    120   result->content = NULL;
    121 
    122   /* Put the default fill pattern in place.  */
    123   result->pattern = (struct FillPattern *) __libasm_default_pattern;
    124 
    125   /* There are no subsections so far.  */
    126   result->subnext = NULL;
    127 
    128   /* Add the name to the section header string table.  */
    129   result->data.main.strent = ebl_strtabadd (result->ctx->section_strtab,
    130 					    result->name, scnname_len);
    131   assert (result->data.main.strent != NULL);
    132 
    133   /* Create the new ELF section.  */
    134   result->data.main.scn = scn = elf_newscn (result->ctx->out.elf);
    135   if (scn == NULL)
    136     {
    137       free (result);
    138       __libasm_seterrno (ASM_E_LIBELF);
    139       return NULL;
    140     }
    141 
    142   /* Not part of a section group (yet).  */
    143   result->data.main.next_in_group = NULL;
    144 
    145   /* Remember the flags.  */
    146   shdr = gelf_getshdr (scn, &shdr_mem);
    147 
    148   shdr->sh_flags = flags;
    149   result->type = shdr->sh_type = type;
    150 
    151   (void) gelf_update_shdr (scn, shdr);
    152 
    153   return result;
    154 }
    155 
    156 
    157 AsmScn_t *
    158 asm_newscn (ctx, scnname, type, flags)
    159      AsmCtx_t *ctx;
    160      const char *scnname;
    161      GElf_Word type;
    162      GElf_Xword flags;
    163 {
    164   size_t scnname_len = strlen (scnname) + 1;
    165   AsmScn_t *result;
    166 
    167   /* If no context is given there might be an earlier error.  */
    168   if (ctx == NULL)
    169     return NULL;
    170 
    171   /* Check whether only flags are set which areselectable by the user.  */
    172   if (unlikely ((flags & ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR | SHF_MERGE
    173 			   | SHF_STRINGS | SHF_LINK_ORDER)) != 0)
    174       /* We allow only two section types: data and data without file
    175 	 representation.  */
    176       || (type != SHT_PROGBITS && unlikely (type != SHT_NOBITS)))
    177     {
    178       __libasm_seterrno (ASM_E_INVALID);
    179       return NULL;
    180     }
    181 
    182   rwlock_wrlock (ctx->lock);
    183 
    184   /* This is a new section.  */
    185   result = (AsmScn_t *) malloc (sizeof (AsmScn_t) + scnname_len);
    186   if (result != NULL)
    187     {
    188       /* Add the name.  */
    189       memcpy (result->name, scnname, scnname_len);
    190 
    191       /* Add the reference to the context.  */
    192       result->ctx = ctx;
    193 
    194       /* Perform operations according to output mode.  */
    195       result = (unlikely (ctx->textp)
    196 		? text_newscn (result, type, flags)
    197 		: binary_newscn (result, type, flags, scnname_len));
    198 
    199       /* If everything went well finally add the new section to the hash
    200 	 table.  */
    201       if (result != NULL)
    202 	{
    203 	  result->allnext = ctx->section_list;
    204 	  ctx->section_list = result;
    205 	}
    206     }
    207 
    208   rwlock_unlock (ctx->lock);
    209 
    210   return result;
    211 }
    212 INTDEF(asm_newscn)
    213