Blame libasm/asm_newsym.c

Packit 032894
/* Define new symbol for current position in given section.
Packit 032894
   Copyright (C) 2002, 2005, 2016, 2017 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
AsmSym_t *
Packit 032894
asm_newsym (AsmScn_t *asmscn, const char *name, GElf_Xword size,
Packit 032894
	    int type, int binding)
Packit 032894
{
Packit 032894
/* We don't really expect labels with many digits, but in theory it could
Packit 032894
   be 10 digits (plus ".L" and a zero terminator).  */
Packit 032894
#define TEMPSYMLEN 13
Packit 032894
  char tempsym[TEMPSYMLEN];
Packit 032894
  AsmSym_t *result;
Packit 032894
Packit 032894
  if (asmscn == NULL)
Packit 032894
    /* Something went wrong before.  */
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  /* Generate a temporary symbol if necessary.  */
Packit 032894
  if (name == NULL)
Packit 032894
    {
Packit 032894
      /* If a local symbol name is created the symbol better have
Packit 032894
	 local binding.  */
Packit 032894
      if (binding != STB_LOCAL)
Packit 032894
	{
Packit 032894
	  __libasm_seterrno (ASM_E_INVALID);
Packit 032894
	  return NULL;
Packit 032894
	}
Packit 032894
Packit 032894
      // XXX This requires getting the format from the machine backend.  */
Packit 032894
      snprintf (tempsym, TEMPSYMLEN, ".L%07u", asmscn->ctx->tempsym_count++);
Packit 032894
Packit 032894
      name = tempsym;
Packit 032894
    }
Packit 032894
Packit 032894
  size_t name_len = strlen (name) + 1;
Packit 032894
Packit 032894
  result = (AsmSym_t *) malloc (sizeof (AsmSym_t) + name_len);
Packit 032894
  if (result == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  rwlock_wrlock (asmscn->ctx->lock);
Packit 032894
Packit 032894
  result->scn = asmscn;
Packit 032894
  result->offset = asmscn->offset;
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 (asmscn->ctx->symbol_strtab,
Packit 032894
				     memcpy (result + 1, name, name_len));
Packit 032894
Packit 032894
  if (unlikely (asmscn->ctx->textp))
Packit 032894
    {
Packit 032894
      /* We are only interested in the name and don't need to know whether
Packit 032894
	 it is a local name or not.  */
Packit 032894
      /* First print the binding pseudo-op.  */
Packit 032894
      if (binding == STB_GLOBAL)
Packit 032894
	fprintf (asmscn->ctx->out.file, "\t.globl\t%s\n", name);
Packit 032894
      else if (binding == STB_WEAK)
Packit 032894
	fprintf (asmscn->ctx->out.file, "\t.weak\t%s\n", name);
Packit 032894
Packit 032894
      /* Next the symbol type.  */
Packit 032894
      if (type == STT_OBJECT)
Packit 032894
	fprintf (asmscn->ctx->out.file, "\t.type\t%s,@object\n", name);
Packit 032894
      else if (type == STT_FUNC)
Packit 032894
	fprintf (asmscn->ctx->out.file, "\t.type\t%s,@function\n", name);
Packit 032894
Packit 032894
      /* Finally the size and the label.  */
Packit 032894
      fprintf (asmscn->ctx->out.file, "\t.size\t%s,%" PRIuMAX "\n%s:\n",
Packit 032894
	       name, (uintmax_t) size, name);
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 (&asmscn->ctx->symbol_tab, elf_hash (name),
Packit 032894
				 result) != 0)
Packit 032894
	{
Packit 032894
	  /* The symbol already exists.  */
Packit 032894
	  __libasm_seterrno (ASM_E_DUPLSYM);
Packit 032894
	  /* Note that we can free the entry since there must be no
Packit 032894
	     reference in the string table to the string.  We can only
Packit 032894
	     fail to insert the symbol into the symbol table if there
Packit 032894
	     is already a symbol with this name.  In this case the
Packit 032894
	     dwelf_strtab_add function would use the previously provided
Packit 032894
	     name.  */
Packit 032894
	  free (result);
Packit 032894
	  result = NULL;
Packit 032894
	}
Packit 032894
      else if (name != tempsym && asm_emit_symbol_p (name))
Packit 032894
	/* Only count non-private symbols.  */
Packit 032894
	++asmscn->ctx->nsymbol_tab;
Packit 032894
    }
Packit 032894
Packit 032894
  rwlock_unlock (asmscn->ctx->lock);
Packit 032894
Packit 032894
  return result;
Packit 032894
}