1 /* Create new section in output file.
2 Copyright (C) 2002-2011 Red Hat, Inc.
3 This file is part of Red Hat elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
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.
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.
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.
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>. */
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
47 struct FillPattern pattern;
57 const struct FillPattern *__libasm_default_pattern = &xdefault_pattern.pattern;
61 text_newscn (AsmScn_t *result, GElf_Word type, GElf_Xword flags)
63 /* Buffer where we construct the flag string. */
64 char flagstr[sizeof (GElf_Xword) * 8 + 5];
66 const char *typestr = "";
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, ", \"");
73 if (flags & SHF_WRITE)
75 if (flags & SHF_ALLOC)
77 if (flags & SHF_EXECINSTR)
79 if (flags & SHF_MERGE)
81 if (flags & SHF_STRINGS)
83 if (flags & SHF_LINK_ORDER)
88 if (type == SHT_PROGBITS)
89 typestr = ",@progbits";
90 else if (type == SHT_NOBITS)
93 /* Terminate the string. */
96 fprintf (result->ctx->out.file, "\t.section \"%s\"%s%s\n",
97 result->name, flagstr, typestr);
104 binary_newscn (AsmScn_t *result, GElf_Word type, GElf_Xword flags,
111 /* The initial subsection has the number zero. */
112 result->subsection_id = 0;
114 /* We start at offset zero. */
116 /* And generic alignment. */
117 result->max_align = 1;
120 result->content = NULL;
122 /* Put the default fill pattern in place. */
123 result->pattern = (struct FillPattern *) __libasm_default_pattern;
125 /* There are no subsections so far. */
126 result->subnext = NULL;
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);
133 /* Create the new ELF section. */
134 result->data.main.scn = scn = elf_newscn (result->ctx->out.elf);
138 __libasm_seterrno (ASM_E_LIBELF);
142 /* Not part of a section group (yet). */
143 result->data.main.next_in_group = NULL;
145 /* Remember the flags. */
146 shdr = gelf_getshdr (scn, &shdr_mem);
148 shdr->sh_flags = flags;
149 result->type = shdr->sh_type = type;
151 (void) gelf_update_shdr (scn, shdr);
158 asm_newscn (ctx, scnname, type, flags)
164 size_t scnname_len = strlen (scnname) + 1;
167 /* If no context is given there might be an earlier error. */
171 /* Check whether only flags are set which areselectable by the user. */
172 if (unlikely ((flags & ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR | SHF_MERGE
173 | SHF_STRINGS | SHF_LINK_ORDER)) != 0)
174 /* We allow only two section types: data and data without file
176 || (type != SHT_PROGBITS && unlikely (type != SHT_NOBITS)))
178 __libasm_seterrno (ASM_E_INVALID);
182 rwlock_wrlock (ctx->lock);
184 /* This is a new section. */
185 result = (AsmScn_t *) malloc (sizeof (AsmScn_t) + scnname_len);
189 memcpy (result->name, scnname, scnname_len);
191 /* Add the reference to the context. */
194 /* Perform operations according to output mode. */
195 result = (unlikely (ctx->textp)
196 ? text_newscn (result, type, flags)
197 : binary_newscn (result, type, flags, scnname_len));
199 /* If everything went well finally add the new section to the hash
203 result->allnext = ctx->section_list;
204 ctx->section_list = result;
208 rwlock_unlock (ctx->lock);