Home | History | Annotate | Download | only in libasm
      1 /* Create new ABS 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_abs_scn =
     42   {
     43     .data = {
     44       .main = {
     45 	.scn = ASM_ABS_SCN
     46       }
     47     }
     48   };
     49 
     50 
     51 AsmSym_t *
     52 asm_newabssym (ctx, name, size, value, type, binding)
     53      AsmCtx_t *ctx;
     54      const char *name;
     55      GElf_Xword size;
     56      GElf_Addr value;
     57      int type;
     58      int binding;
     59 {
     60   AsmSym_t *result;
     61 
     62   if (ctx == NULL)
     63     /* Something went wrong before.  */
     64     return NULL;
     65 
     66   /* Common symbols are public.  Therefore the user must provide a
     67      name.  */
     68   if (name == NULL)
     69     {
     70       __libasm_seterrno (ASM_E_INVALID);
     71       return NULL;
     72     }
     73 
     74   rwlock_wrlock (ctx->lock);
     75 
     76   result = (AsmSym_t *) malloc (sizeof (AsmSym_t));
     77   if (result == NULL)
     78     return NULL;
     79 
     80   result->scn = (AsmScn_t *) &__libasm_abs_scn;
     81   result->size = size;
     82   result->type = type;
     83   result->binding = binding;
     84   result->symidx = 0;
     85   result->strent = ebl_strtabadd (ctx->symbol_strtab, name, 0);
     86 
     87   /* The value of an ABS symbol must not be modified.  Since there are
     88      no subsection and the initial offset of the section is 0 we can
     89      get the alignment recorded by storing it into the offset
     90      field.  */
     91   result->offset = value;
     92 
     93   if (unlikely (ctx->textp))
     94     {
     95       /* An absolute symbol can be defined by giving a symbol a
     96 	 specific value.  */
     97       if (binding == STB_GLOBAL)
     98 	fprintf (ctx->out.file, "\t.globl %s\n", name);
     99       else if (binding == STB_WEAK)
    100 	fprintf (ctx->out.file, "\t.weak %s\n", name);
    101 
    102       if (type == STT_OBJECT)
    103 	fprintf (ctx->out.file, "\t.type %s,@object\n", name);
    104       else if (type == STT_FUNC)
    105 	fprintf (ctx->out.file, "\t.type %s,@function\n", name);
    106 
    107       fprintf (ctx->out.file, "%s = %llu\n",
    108 	       name, (unsigned long long int) value);
    109 
    110       if (size != 0)
    111 	fprintf (ctx->out.file, "\t.size %s, %llu\n",
    112 		 name, (unsigned long long int) size);
    113     }
    114   else
    115     {
    116       /* Put the symbol in the hash table so that we can later find it.  */
    117       if (asm_symbol_tab_insert (&ctx->symbol_tab, elf_hash (name), result)
    118 	  != 0)
    119 	{
    120 	  /* The symbol already exists.  */
    121 	  __libasm_seterrno (ASM_E_DUPLSYM);
    122 	  free (result);
    123 	  result = NULL;
    124 	}
    125       else if (name != NULL && asm_emit_symbol_p (name))
    126 	/* Only count non-private symbols.  */
    127 	++ctx->nsymbol_tab;
    128     }
    129 
    130   rwlock_unlock (ctx->lock);
    131 
    132   return result;
    133 }
    134