Blame libasm/asm_newabssym.c

Packit 032894
/* Create new ABS 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_abs_scn =
Packit 032894
  {
Packit 032894
    .data = {
Packit 032894
      .main = {
Packit 032894
	.scn = ASM_ABS_SCN
Packit 032894
      }
Packit 032894
    }
Packit 032894
  };
Packit 032894
Packit 032894
Packit 032894
AsmSym_t *
Packit 032894
asm_newabssym (AsmCtx_t *ctx, const char *name, GElf_Xword size,
Packit 032894
	       GElf_Addr value, int type, int binding)
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_abs_scn;
Packit 032894
  result->size = size;
Packit 032894
  result->type = type;
Packit 032894
  result->binding = binding;
Packit 032894
  result->symidx = 0;
Packit 032894
  result->strent = dwelf_strtab_add (ctx->symbol_strtab, name);
Packit 032894
Packit 032894
  /* The value of an ABS symbol must not be modified.  Since there are
Packit 032894
     no subsection and the initial offset of the section is 0 we can
Packit 032894
     get the alignment recorded by storing it into the offset
Packit 032894
     field.  */
Packit 032894
  result->offset = value;
Packit 032894
Packit 032894
  if (unlikely (ctx->textp))
Packit 032894
    {
Packit 032894
      /* An absolute symbol can be defined by giving a symbol a
Packit 032894
	 specific value.  */
Packit 032894
      if (binding == STB_GLOBAL)
Packit 032894
	fprintf (ctx->out.file, "\t.globl %s\n", name);
Packit 032894
      else if (binding == STB_WEAK)
Packit 032894
	fprintf (ctx->out.file, "\t.weak %s\n", name);
Packit 032894
Packit 032894
      if (type == STT_OBJECT)
Packit 032894
	fprintf (ctx->out.file, "\t.type %s,@object\n", name);
Packit 032894
      else if (type == STT_FUNC)
Packit 032894
	fprintf (ctx->out.file, "\t.type %s,@function\n", name);
Packit 032894
Packit 032894
      fprintf (ctx->out.file, "%s = %llu\n",
Packit 032894
	       name, (unsigned long long int) value);
Packit 032894
Packit 032894
      if (size != 0)
Packit 032894
	fprintf (ctx->out.file, "\t.size %s, %llu\n",
Packit 032894
		 name, (unsigned long long int) size);
Packit 032894
    }
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
}