blob: 34fef3e306da6d5c618e4c2fb29812748ff52249 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Create new ABS symbol.
Mark Wielaarddd906c12016-07-08 14:08:22 +02002 Copyright (C) 2002, 2016 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * 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
12
13 or
14
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
18
19 or both in parallel, as here.
20
21 elfutils is distributed in the hope that it will be useful, but
Ulrich Drepper361df7d2006-04-04 21:38:57 +000022 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.
25
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <inttypes.h>
35#include <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38
39#include <libasmP.h>
40#include <system.h>
41
42
43/* Object for special COMMON section. */
44static const AsmScn_t __libasm_abs_scn =
45 {
46 .data = {
47 .main = {
48 .scn = ASM_ABS_SCN
49 }
50 }
51 };
52
53
54AsmSym_t *
Mark Wielaard1ccdfb62015-09-22 22:27:01 +020055asm_newabssym (AsmCtx_t *ctx, const char *name, GElf_Xword size,
56 GElf_Addr value, int type, int binding)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000057{
58 AsmSym_t *result;
59
60 if (ctx == NULL)
61 /* Something went wrong before. */
62 return NULL;
63
64 /* Common symbols are public. Therefore the user must provide a
65 name. */
66 if (name == NULL)
67 {
68 __libasm_seterrno (ASM_E_INVALID);
69 return NULL;
70 }
71
72 rwlock_wrlock (ctx->lock);
73
74 result = (AsmSym_t *) malloc (sizeof (AsmSym_t));
75 if (result == NULL)
76 return NULL;
77
78 result->scn = (AsmScn_t *) &__libasm_abs_scn;
79 result->size = size;
80 result->type = type;
81 result->binding = binding;
82 result->symidx = 0;
Mark Wielaarddd906c12016-07-08 14:08:22 +020083 result->strent = dwelf_strtab_add (ctx->symbol_strtab, name);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000084
85 /* The value of an ABS symbol must not be modified. Since there are
86 no subsection and the initial offset of the section is 0 we can
87 get the alignment recorded by storing it into the offset
88 field. */
89 result->offset = value;
90
91 if (unlikely (ctx->textp))
92 {
93 /* An absolute symbol can be defined by giving a symbol a
94 specific value. */
95 if (binding == STB_GLOBAL)
96 fprintf (ctx->out.file, "\t.globl %s\n", name);
97 else if (binding == STB_WEAK)
98 fprintf (ctx->out.file, "\t.weak %s\n", name);
99
100 if (type == STT_OBJECT)
101 fprintf (ctx->out.file, "\t.type %s,@object\n", name);
102 else if (type == STT_FUNC)
103 fprintf (ctx->out.file, "\t.type %s,@function\n", name);
104
105 fprintf (ctx->out.file, "%s = %llu\n",
106 name, (unsigned long long int) value);
107
108 if (size != 0)
109 fprintf (ctx->out.file, "\t.size %s, %llu\n",
110 name, (unsigned long long int) size);
111 }
112 else
113 {
114 /* Put the symbol in the hash table so that we can later find it. */
115 if (asm_symbol_tab_insert (&ctx->symbol_tab, elf_hash (name), result)
116 != 0)
117 {
118 /* The symbol already exists. */
119 __libasm_seterrno (ASM_E_DUPLSYM);
120 free (result);
121 result = NULL;
122 }
123 else if (name != NULL && asm_emit_symbol_p (name))
124 /* Only count non-private symbols. */
125 ++ctx->nsymbol_tab;
126 }
127
128 rwlock_unlock (ctx->lock);
129
130 return result;
131}