Home | History | Annotate | Download | only in libasm
      1 /* Create new COMMON symbol.
      2    Copyright (C) 2002 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 <inttypes.h>
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include <string.h>
     35 
     36 #include <libasmP.h>
     37 #include <system.h>
     38 
     39 
     40 /* Object for special COMMON section.  */
     41 static const AsmScn_t __libasm_com_scn =
     42   {
     43     .data = {
     44       .main = {
     45 	.scn = ASM_COM_SCN
     46       }
     47     }
     48   };
     49 
     50 
     51 AsmSym_t *
     52 asm_newcomsym (ctx, name, size, align)
     53      AsmCtx_t *ctx;
     54      const char *name;
     55      GElf_Xword size;
     56      GElf_Addr align;
     57 {
     58   AsmSym_t *result;
     59 
     60   if (ctx == NULL)
     61     /* Something went wrong before.  */
     62     return NULL;
     63 
     64   /* Common symbols are public.  Therefore the user must provide a
     65      name.  */
     66   if (name == NULL)
     67     {
     68       __libasm_seterrno (ASM_E_INVALID);
     69       return NULL;
     70     }
     71 
     72   rwlock_wrlock (ctx->lock);
     73 
     74   result = (AsmSym_t *) malloc (sizeof (AsmSym_t));
     75   if (result == NULL)
     76     return NULL;
     77 
     78   result->scn = (AsmScn_t *) &__libasm_com_scn;
     79   result->size = size;
     80   /* XXX Do we have to allow a different type?  */
     81   result->type = STT_OBJECT;
     82   /* XXX Do we have to allow a different binding?  */
     83   result->binding = STB_GLOBAL;
     84   result->symidx = 0;
     85   result->strent = ebl_strtabadd (ctx->symbol_strtab, name, 0);
     86 
     87   /* The value of a COM symbol is the alignment.  Since there are no
     88      subsection and the initial offset of the section is 0 we can get
     89      the alignment recorded by storing it into the offset field.  */
     90   result->offset = align;
     91 
     92   if (unlikely (ctx->textp))
     93     fprintf (ctx->out.file, "\t.comm %s, %" PRIuMAX ", %" PRIuMAX "\n",
     94 	     name, (uintmax_t) size, (uintmax_t) align);
     95   else
     96     {
     97       /* Put the symbol in the hash table so that we can later find it.  */
     98       if (asm_symbol_tab_insert (&ctx->symbol_tab, elf_hash (name), result)
     99 	  != 0)
    100 	{
    101 	  /* The symbol already exists.  */
    102 	  __libasm_seterrno (ASM_E_DUPLSYM);
    103 	  free (result);
    104 	  result = NULL;
    105 	}
    106       else if (name != NULL && asm_emit_symbol_p (name))
    107 	/* Only count non-private symbols.  */
    108 	++ctx->nsymbol_tab;
    109     }
    110 
    111   rwlock_unlock (ctx->lock);
    112 
    113   return result;
    114 }
    115