1 /* Define new symbol for current position in given section.
2 Copyright (C) 2002, 2005, 2016 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
9 * the GNU Lesser General Public License as published by the Free
10 Software Foundation; either version 3 of the License, or (at
11 your option) any later version
15 * the GNU General Public License as published by the Free
16 Software Foundation; either version 2 of the License, or (at
17 your option) any later version
19 or both in parallel, as here.
21 elfutils is distributed in the hope that it will be useful, but
22 WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 General Public License for more details.
26 You should have received copies of the GNU General Public License and
27 the GNU Lesser General Public License along with this program. If
28 not, see <http://www.gnu.org/licenses/>. */
44 asm_newsym (AsmScn_t *asmscn, const char *name, GElf_Xword size,
45 int type, int binding)
48 char tempsym[TEMPSYMLEN];
52 /* Something went wrong before. */
55 /* Generate a temporary symbol if necessary. */
58 /* If a local symbol name is created the symbol better have
60 if (binding != STB_LOCAL)
62 __libasm_seterrno (ASM_E_INVALID);
66 // XXX This requires getting the format from the machine backend. */
67 snprintf (tempsym, TEMPSYMLEN, ".L%07u", asmscn->ctx->tempsym_count++);
72 size_t name_len = strlen (name) + 1;
74 result = (AsmSym_t *) malloc (sizeof (AsmSym_t) + name_len);
78 rwlock_wrlock (asmscn->ctx->lock);
81 result->offset = asmscn->offset;
84 result->binding = binding;
86 result->strent = dwelf_strtab_add (asmscn->ctx->symbol_strtab,
87 memcpy (result + 1, name, name_len));
89 if (unlikely (asmscn->ctx->textp))
91 /* We are only interested in the name and don't need to know whether
92 it is a local name or not. */
93 /* First print the binding pseudo-op. */
94 if (binding == STB_GLOBAL)
95 fprintf (asmscn->ctx->out.file, "\t.globl\t%s\n", name);
96 else if (binding == STB_WEAK)
97 fprintf (asmscn->ctx->out.file, "\t.weak\t%s\n", name);
99 /* Next the symbol type. */
100 if (type == STT_OBJECT)
101 fprintf (asmscn->ctx->out.file, "\t.type\t%s,@object\n", name);
102 else if (type == STT_FUNC)
103 fprintf (asmscn->ctx->out.file, "\t.type\t%s,@function\n", name);
105 /* Finally the size and the label. */
106 fprintf (asmscn->ctx->out.file, "\t.size\t%s,%" PRIuMAX "\n%s:\n",
107 name, (uintmax_t) size, name);
111 /* Put the symbol in the hash table so that we can later find it. */
112 if (asm_symbol_tab_insert (&asmscn->ctx->symbol_tab, elf_hash (name),
115 /* The symbol already exists. */
116 __libasm_seterrno (ASM_E_DUPLSYM);
117 /* Note that we can free the entry since there must be no
118 reference in the string table to the string. We can only
119 fail to insert the symbol into the symbol table if there
120 is already a symbol with this name. In this case the
121 dwelf_strtab_add function would use the previously provided
126 else if (name != tempsym && asm_emit_symbol_p (name))
127 /* Only count non-private symbols. */
128 ++asmscn->ctx->nsymbol_tab;
131 rwlock_unlock (asmscn->ctx->lock);