Home | History | Annotate | Download | only in libasm
      1 /* Define new symbol for current position in given section.
      2    Copyright (C) 2002 Red Hat, Inc.
      3    Written by Ulrich Drepper <drepper (at) redhat.com>, 2002.
      4 
      5    This program is Open Source software; you can redistribute it and/or
      6    modify it under the terms of the Open Software License version 1.0 as
      7    published by the Open Source Initiative.
      8 
      9    You should have received a copy of the Open Software License along
     10    with this program; if not, you may obtain a copy of the Open Software
     11    License version 1.0 from http://www.opensource.org/licenses/osl.php or
     12    by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
     13    3001 King Ranch Road, Ukiah, CA 95482.   */
     14 
     15 #ifdef HAVE_CONFIG_H
     16 # include <config.h>
     17 #endif
     18 
     19 #include <inttypes.h>
     20 #include <stdio.h>
     21 #include <stdlib.h>
     22 #include <string.h>
     23 
     24 #include <libasmP.h>
     25 #include <system.h>
     26 
     27 
     28 AsmSym_t *
     29 asm_newsym (asmscn, name, size, type, binding)
     30      AsmScn_t *asmscn;
     31      const char *name;
     32      GElf_Xword size;
     33      int type;
     34      int binding;
     35 {
     36 #define TEMPSYMLEN 10
     37   char tempsym[TEMPSYMLEN];
     38   AsmSym_t *result;
     39 
     40   if (asmscn == NULL)
     41     /* Something went wrong before.  */
     42     return NULL;
     43 
     44   /* Generate a temporary symbol if necessary.  */
     45   if (name == NULL)
     46     {
     47       /* If a local symbol name is created the symbol better have
     48 	 local binding.  */
     49       if (binding != STB_LOCAL)
     50 	{
     51 	  __libasm_seterrno (ASM_E_INVALID);
     52 	  return NULL;
     53 	}
     54 
     55       // XXX This requires getting the format from the machine backend.  */
     56       snprintf (tempsym, TEMPSYMLEN, ".L%07u", asmscn->ctx->tempsym_count++);
     57     }
     58 
     59   result = (AsmSym_t *) malloc (sizeof (AsmSym_t));
     60   if (result == NULL)
     61     return NULL;
     62 
     63   rwlock_wrlock (asmscn->ctx->lock);
     64 
     65   result->scn = asmscn;
     66   result->offset = asmscn->offset;
     67   result->size = size;
     68   result->type = type;
     69   result->binding = binding;
     70   result->symidx = 0;
     71   result->strent = ebl_strtabadd (asmscn->ctx->symbol_strtab,
     72 				  name ?: tempsym, 0);
     73 
     74   if (unlikely (asmscn->ctx->textp))
     75     {
     76       /* We are only interested in the name and don't need to know whether
     77 	 it is a local name or not.  */
     78       if (name == NULL)
     79 	name = tempsym;
     80 
     81       /* First print the binding pseudo-op.  */
     82       if (binding == STB_GLOBAL)
     83 	fprintf (asmscn->ctx->out.file, "\t.globl\t%s\n", name);
     84       else if (binding == STB_WEAK)
     85 	fprintf (asmscn->ctx->out.file, "\t.weak\t%s\n", name);
     86 
     87       /* Next the symbol type.  */
     88       if (type == STT_OBJECT)
     89 	fprintf (asmscn->ctx->out.file, "\t.type\t%s,@object\n", name);
     90       else if (type == STT_FUNC)
     91 	fprintf (asmscn->ctx->out.file, "\t.type\t%s,@function\n", name);
     92 
     93       /* Finally the size and the label.  */
     94       fprintf (asmscn->ctx->out.file, "\t.size\t%s,%" PRIuMAX "\n%s:\n",
     95 	       name, (uintmax_t) size, name);
     96     }
     97   else
     98     {
     99       /* Put the symbol in the hash table so that we can later find it.  */
    100       if (asm_symbol_tab_insert (&asmscn->ctx->symbol_tab,
    101 				 elf_hash (name ?: tempsym), result) != 0)
    102 	{
    103 	  /* The symbol already exists.  */
    104 	  __libasm_seterrno (ASM_E_DUPLSYM);
    105 	  free (result);
    106 	  result = NULL;
    107 	}
    108       else if (name != NULL && asm_emit_symbol_p (name))
    109 	/* Only count non-private symbols.  */
    110 	++asmscn->ctx->nsymbol_tab;
    111     }
    112 
    113   rwlock_unlock (asmscn->ctx->lock);
    114 
    115   return result;
    116 }
    117