Blame libasm/asm_newcomsym.c

Packit 032894
/* Create new COMMON symbol.
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 <inttypes.h>
Packit 032894
#include <stdio.h>
Packit 032894
#include <stdlib.h>
Packit 032894
#include <string.h>
Packit 032894
Packit 032894
#include <libasmP.h>
Packit 032894
#include <system.h>
Packit 032894
Packit 032894
Packit 032894
/* Object for special COMMON section.  */
Packit 032894
static const AsmScn_t __libasm_com_scn =
Packit 032894
  {
Packit 032894
    .data = {
Packit 032894
      .main = {
Packit 032894
	.scn = ASM_COM_SCN
Packit 032894
      }
Packit 032894
    }
Packit 032894
  };
Packit 032894
Packit 032894
Packit 032894
AsmSym_t *
Packit 032894
asm_newcomsym (AsmCtx_t *ctx, const char *name, GElf_Xword size,
Packit 032894
	       GElf_Addr align)
Packit 032894
{
Packit 032894
  AsmSym_t *result;
Packit 032894
Packit 032894
  if (ctx == NULL)
Packit 032894
    /* Something went wrong before.  */
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  /* Common symbols are public.  Therefore the user must provide a
Packit 032894
     name.  */
Packit 032894
  if (name == NULL)
Packit 032894
    {
Packit 032894
      __libasm_seterrno (ASM_E_INVALID);
Packit 032894
      return NULL;
Packit 032894
    }
Packit 032894
Packit 032894
  rwlock_wrlock (ctx->lock);
Packit 032894
Packit 032894
  result = (AsmSym_t *) malloc (sizeof (AsmSym_t));
Packit 032894
  if (result == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  result->scn = (AsmScn_t *) &__libasm_com_scn;
Packit 032894
  result->size = size;
Packit 032894
  /* XXX Do we have to allow a different type?  */
Packit 032894
  result->type = STT_OBJECT;
Packit 032894
  /* XXX Do we have to allow a different binding?  */
Packit 032894
  result->binding = STB_GLOBAL;
Packit 032894
  result->symidx = 0;
Packit 032894
  result->strent = dwelf_strtab_add (ctx->symbol_strtab, name);
Packit 032894
Packit 032894
  /* The value of a COM symbol is the alignment.  Since there are no
Packit 032894
     subsection and the initial offset of the section is 0 we can get
Packit 032894
     the alignment recorded by storing it into the offset field.  */
Packit 032894
  result->offset = align;
Packit 032894
Packit 032894
  if (unlikely (ctx->textp))
Packit 032894
    fprintf (ctx->out.file, "\t.comm %s, %" PRIuMAX ", %" PRIuMAX "\n",
Packit 032894
	     name, (uintmax_t) size, (uintmax_t) align);
Packit 032894
  else
Packit 032894
    {
Packit 032894
      /* Put the symbol in the hash table so that we can later find it.  */
Packit 032894
      if (asm_symbol_tab_insert (&ctx->symbol_tab, elf_hash (name), result)
Packit 032894
	  != 0)
Packit 032894
	{
Packit 032894
	  /* The symbol already exists.  */
Packit 032894
	  __libasm_seterrno (ASM_E_DUPLSYM);
Packit 032894
	  free (result);
Packit 032894
	  result = NULL;
Packit 032894
	}
Packit 032894
      else if (name != NULL && asm_emit_symbol_p (name))
Packit 032894
	/* Only count non-private symbols.  */
Packit 032894
	++ctx->nsymbol_tab;
Packit 032894
    }
Packit 032894
Packit 032894
  rwlock_unlock (ctx->lock);
Packit 032894
Packit 032894
  return result;
Packit 032894
}