1 /* Create new section group.
2 Copyright (C) 2002, 2016 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 <assert.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "libasmP.h"
39 #include <system.h>
40
41
42
43 AsmScnGrp_t *
asm_newscngrp(AsmCtx_t * ctx,const char * grpname,AsmSym_t * signature,Elf32_Word flags)44 asm_newscngrp (AsmCtx_t *ctx, const char *grpname, AsmSym_t *signature,
45 Elf32_Word flags)
46 {
47 AsmScnGrp_t *result;
48 size_t grpname_len = strlen (grpname) + 1;
49
50 if (ctx == NULL)
51 return NULL;
52
53 if ((flags & ~GRP_COMDAT) != 0)
54 {
55 /* This is not a supported flag. */
56 __libasm_seterrno (ASM_E_INVALID);
57 return NULL;
58 }
59
60 result = (AsmScnGrp_t *) malloc (sizeof (AsmScnGrp_t) + grpname_len);
61 if (result == NULL)
62 return NULL;
63
64 result->signature = signature;
65 result->members = NULL;
66 result->nmembers = 0;
67 result->flags = flags;
68
69 memcpy (result->name, grpname, grpname_len);
70 result->strent = dwelf_strtab_add_len (ctx->section_strtab, result->name,
71 grpname_len);
72
73 if (unlikely (ctx->textp))
74 // XXX TBI. What is the format?
75 abort ();
76 else
77 {
78 result->scn = elf_newscn (ctx->out.elf);
79 if (result->scn == NULL)
80 {
81 /* Couldn't allocate a new section. */
82 __libasm_seterrno (ASM_E_LIBELF);
83 free (result);
84 return NULL;
85 }
86 }
87
88 /* Enqueue is the context data structure. */
89 if (ctx->ngroups == 0)
90 {
91 assert (ctx->groups == NULL);
92 ctx->groups = result->next = result;
93 }
94 else
95 {
96 result->next = ctx->groups->next;
97 ctx->groups = ctx->groups->next = result;
98 }
99 ++ctx->ngroups;
100
101 return result;
102 }
103