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