Imported Upstream version 0.153
[platform/upstream/elfutils.git] / libasm / asm_newsym.c
1 /* Define new symbol for current position in given section.
2    Copyright (C) 2002, 2005 Red Hat, Inc.
3    This file is part of Red Hat elfutils.
4    Written by Ulrich Drepper <drepper@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 AsmSym_t *
41 asm_newsym (asmscn, name, size, type, binding)
42      AsmScn_t *asmscn;
43      const char *name;
44      GElf_Xword size;
45      int type;
46      int binding;
47 {
48 #define TEMPSYMLEN 10
49   char tempsym[TEMPSYMLEN];
50   AsmSym_t *result;
51
52   if (asmscn == NULL)
53     /* Something went wrong before.  */
54     return NULL;
55
56   /* Generate a temporary symbol if necessary.  */
57   if (name == NULL)
58     {
59       /* If a local symbol name is created the symbol better have
60          local binding.  */
61       if (binding != STB_LOCAL)
62         {
63           __libasm_seterrno (ASM_E_INVALID);
64           return NULL;
65         }
66
67       // XXX This requires getting the format from the machine backend.  */
68       snprintf (tempsym, TEMPSYMLEN, ".L%07u", asmscn->ctx->tempsym_count++);
69
70       name = tempsym;
71     }
72
73   size_t name_len = strlen (name) + 1;
74
75   result = (AsmSym_t *) malloc (sizeof (AsmSym_t) + name_len);
76   if (result == NULL)
77     return NULL;
78
79   rwlock_wrlock (asmscn->ctx->lock);
80
81   result->scn = asmscn;
82   result->offset = asmscn->offset;
83   result->size = size;
84   result->type = type;
85   result->binding = binding;
86   result->symidx = 0;
87   result->strent = ebl_strtabadd (asmscn->ctx->symbol_strtab,
88                                   memcpy (result + 1, name, name_len), 0);
89
90   if (unlikely (asmscn->ctx->textp))
91     {
92       /* We are only interested in the name and don't need to know whether
93          it is a local name or not.  */
94       /* First print the binding pseudo-op.  */
95       if (binding == STB_GLOBAL)
96         fprintf (asmscn->ctx->out.file, "\t.globl\t%s\n", name);
97       else if (binding == STB_WEAK)
98         fprintf (asmscn->ctx->out.file, "\t.weak\t%s\n", name);
99
100       /* Next the symbol type.  */
101       if (type == STT_OBJECT)
102         fprintf (asmscn->ctx->out.file, "\t.type\t%s,@object\n", name);
103       else if (type == STT_FUNC)
104         fprintf (asmscn->ctx->out.file, "\t.type\t%s,@function\n", name);
105
106       /* Finally the size and the label.  */
107       fprintf (asmscn->ctx->out.file, "\t.size\t%s,%" PRIuMAX "\n%s:\n",
108                name, (uintmax_t) size, name);
109     }
110   else
111     {
112       /* Put the symbol in the hash table so that we can later find it.  */
113       if (asm_symbol_tab_insert (&asmscn->ctx->symbol_tab, elf_hash (name),
114                                  result) != 0)
115         {
116           /* The symbol already exists.  */
117           __libasm_seterrno (ASM_E_DUPLSYM);
118           /* Note that we can free the entry since there must be no
119              reference in the string table to the string.  We can only
120              fail to insert the symbol into the symbol table if there
121              is already a symbol with this name.  In this case the
122              ebl_strtabadd function would use the previously provided
123              name.  */
124           free (result);
125           result = NULL;
126         }
127       else if (name != tempsym && asm_emit_symbol_p (name))
128         /* Only count non-private symbols.  */
129         ++asmscn->ctx->nsymbol_tab;
130     }
131
132   rwlock_unlock (asmscn->ctx->lock);
133
134   return result;
135 }