2 Copyright (C) 2002, 2005 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
6 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
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
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
19 or both in parallel, as here.
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.
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/>. */
36 #include <sys/param.h>
43 asm_align (AsmScn_t *asmscn, GElf_Word value)
46 /* An earlier error. */
49 /* The alignment value must be a power of two. */
50 if (unlikely (! powerof2 (value)))
52 __libasm_seterrno (ASM_E_INVALID);
56 if (unlikely (asmscn->ctx->textp))
58 fprintf (asmscn->ctx->out.file, "\t.align %" PRId32 ", ",
60 if (asmscn->pattern->len == 1)
61 fprintf (asmscn->ctx->out.file, "%02hhx\n", asmscn->pattern->bytes[0]);
64 fputc_unlocked ('"', asmscn->ctx->out.file);
66 for (size_t cnt = 0; cnt < asmscn->pattern->len; ++cnt)
67 fprintf (asmscn->ctx->out.file, "\\x%02hhx",
68 asmscn->pattern->bytes[cnt]);
70 fputs_unlocked ("\"\n", asmscn->ctx->out.file);
75 rwlock_wrlock (asmscn->ctx->lock);
79 /* Fillbytes necessary? */
80 if ((asmscn->offset & (value - 1)) != 0)
83 size_t cnt = value - (asmscn->offset & (value - 1));
85 /* Ensure there is enough room to add the fill bytes. */
86 result = __libasm_ensure_section_space (asmscn, cnt);
90 /* Fill in the bytes. We align the pattern according to the
92 size_t byteptr = asmscn->offset % asmscn->pattern->len;
94 /* Update the total size. */
95 asmscn->offset += cnt;
99 asmscn->content->data[asmscn->content->len++]
100 = asmscn->pattern->bytes[byteptr++];
102 if (byteptr == asmscn->pattern->len)
108 /* Remember the maximum alignment for this subsection. */
109 if (asmscn->max_align < value)
111 asmscn->max_align = value;
113 /* Update the parent as well (if it exists). */
114 if (asmscn->subsection_id != 0)
116 rwlock_wrlock (asmscn->data.up->ctx->lock);
118 if (asmscn->data.up->max_align < value)
119 asmscn->data.up->max_align = value;
121 rwlock_unlock (asmscn->data.up->ctx->lock);
126 rwlock_unlock (asmscn->ctx->lock);
132 /* Ensure there are at least LEN bytes available in the output buffer
136 __libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
138 /* The blocks with the section content are kept in a circular
139 single-linked list. */
142 if (asmscn->content == NULL)
144 /* This is the first block. */
145 size = MAX (2 * len, 960);
147 asmscn->content = (struct AsmData *) malloc (sizeof (struct AsmData)
149 if (asmscn->content == NULL)
152 asmscn->content->next = asmscn->content;
156 struct AsmData *newp;
158 if (asmscn->content->maxlen - asmscn->content->len >= len)
159 /* Nothing to do, there is enough space. */
162 size = MAX (2 *len, MIN (32768, 2 * asmscn->offset));
164 newp = (struct AsmData *) malloc (sizeof (struct AsmData) + size);
168 newp->next = asmscn->content->next;
169 asmscn->content = asmscn->content->next = newp;
172 asmscn->content->len = 0;
173 asmscn->content->maxlen = size;