1 /* Create new section in output file.
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 <assert.h>
32 #include <error.h>
33 #include <libintl.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <libasmP.h>
38 #include <libelf.h>
39 #include <system.h>
40
41
42 /* Memory for the default pattern. The type uses a flexible array
43 which does work well with a static initializer. So we play some
44 dirty tricks here. */
45 static const struct
46 {
47 struct FillPattern pattern;
48 char zero;
49 } xdefault_pattern =
50 {
51 .pattern =
52 {
53 .len = 1
54 },
55 .zero = '\0'
56 };
57 const struct FillPattern *__libasm_default_pattern = &xdefault_pattern.pattern;
58
59
60 static AsmScn_t *
text_newscn(AsmScn_t * result,GElf_Word type,GElf_Xword flags)61 text_newscn (AsmScn_t *result, GElf_Word type, GElf_Xword flags)
62 {
63 /* Buffer where we construct the flag string. */
64 char flagstr[sizeof (GElf_Xword) * 8 + 5];
65 char *wp = flagstr;
66 const char *typestr = "";
67
68 /* Only write out the flag string if this is the first time the
69 section is selected. Some assemblers cannot cope with the
70 .section pseudo-op otherwise. */
71 wp = stpcpy (wp, ", \"");
72
73 if (flags & SHF_WRITE)
74 *wp++ = 'w';
75 if (flags & SHF_ALLOC)
76 *wp++ = 'a';
77 if (flags & SHF_EXECINSTR)
78 *wp++ = 'x';
79 if (flags & SHF_MERGE)
80 *wp++ = 'M';
81 if (flags & SHF_STRINGS)
82 *wp++ = 'S';
83 if (flags & SHF_LINK_ORDER)
84 *wp++ = 'L';
85
86 *wp++ = '"';
87
88 if (type == SHT_PROGBITS)
89 typestr = ",@progbits";
90 else if (type == SHT_NOBITS)
91 typestr = ",@nobits";
92
93 /* Terminate the string. */
94 *wp = '\0';
95
96 fprintf (result->ctx->out.file, "\t.section \"%s\"%s%s\n",
97 result->name, flagstr, typestr);
98
99 return result;
100 }
101
102
103 static AsmScn_t *
binary_newscn(AsmScn_t * result,GElf_Word type,GElf_Xword flags,size_t scnname_len)104 binary_newscn (AsmScn_t *result, GElf_Word type, GElf_Xword flags,
105 size_t scnname_len)
106 {
107 GElf_Shdr shdr_mem;
108 GElf_Shdr *shdr;
109 Elf_Scn *scn;
110
111 /* The initial subsection has the number zero. */
112 result->subsection_id = 0;
113
114 /* We start at offset zero. */
115 result->offset = 0;
116 /* And generic alignment. */
117 result->max_align = 1;
118
119 /* No output yet. */
120 result->content = NULL;
121
122 /* Put the default fill pattern in place. */
123 result->pattern = (struct FillPattern *) __libasm_default_pattern;
124
125 /* There are no subsections so far. */
126 result->subnext = NULL;
127
128 /* Add the name to the section header string table. */
129 result->data.main.strent = ebl_strtabadd (result->ctx->section_strtab,
130 result->name, scnname_len);
131 assert (result->data.main.strent != NULL);
132
133 /* Create the new ELF section. */
134 result->data.main.scn = scn = elf_newscn (result->ctx->out.elf);
135 if (scn == NULL)
136 {
137 free (result);
138 __libasm_seterrno (ASM_E_LIBELF);
139 return NULL;
140 }
141
142 /* Not part of a section group (yet). */
143 result->data.main.next_in_group = NULL;
144
145 /* Remember the flags. */
146 shdr = gelf_getshdr (scn, &shdr_mem);
147
148 shdr->sh_flags = flags;
149 result->type = shdr->sh_type = type;
150
151 (void) gelf_update_shdr (scn, shdr);
152
153 return result;
154 }
155
156
157 AsmScn_t *
asm_newscn(ctx,scnname,type,flags)158 asm_newscn (ctx, scnname, type, flags)
159 AsmCtx_t *ctx;
160 const char *scnname;
161 GElf_Word type;
162 GElf_Xword flags;
163 {
164 size_t scnname_len = strlen (scnname) + 1;
165 unsigned long int hval;
166 AsmScn_t *result;
167
168 /* If no context is given there might be an earlier error. */
169 if (ctx == NULL)
170 return NULL;
171
172 /* Check whether only flags are set which areselectable by the user. */
173 if (unlikely ((flags & ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR | SHF_MERGE
174 | SHF_STRINGS | SHF_LINK_ORDER)) != 0)
175 /* We allow only two section types: data and data without file
176 representation. */
177 || (type != SHT_PROGBITS && unlikely (type != SHT_NOBITS)))
178 {
179 __libasm_seterrno (ASM_E_INVALID);
180 return NULL;
181 }
182
183 hval = elf_hash (scnname);
184
185 rwlock_wrlock (ctx->lock);
186
187 /* This is a new section. */
188 result = (AsmScn_t *) malloc (sizeof (AsmScn_t) + scnname_len);
189 if (result != NULL)
190 {
191 /* Add the name. */
192 memcpy (result->name, scnname, scnname_len);
193
194 /* Add the reference to the context. */
195 result->ctx = ctx;
196
197 /* Perform operations according to output mode. */
198 result = (unlikely (ctx->textp)
199 ? text_newscn (result, type, flags)
200 : binary_newscn (result, type, flags, scnname_len));
201
202 /* If everything went well finally add the new section to the hash
203 table. */
204 if (result != NULL)
205 {
206 result->allnext = ctx->section_list;
207 ctx->section_list = result;
208 }
209 }
210
211 rwlock_unlock (ctx->lock);
212
213 return result;
214 }
215 INTDEF(asm_newscn)
216