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, value)
48 /* An earlier error. */
51 /* The alignment value must be a power of two. */
52 if (unlikely (! powerof2 (value)))
54 __libasm_seterrno (ASM_E_INVALID);
58 if (unlikely (asmscn->ctx->textp))
60 fprintf (asmscn->ctx->out.file, "\t.align %" PRId32 ", ",
62 if (asmscn->pattern->len == 1)
63 fprintf (asmscn->ctx->out.file, "%02hhx\n", asmscn->pattern->bytes[0]);
66 fputc_unlocked ('"', asmscn->ctx->out.file);
68 for (size_t cnt = 0; cnt < asmscn->pattern->len; ++cnt)
69 fprintf (asmscn->ctx->out.file, "\\x%02hhx",
70 asmscn->pattern->bytes[cnt]);
72 fputs_unlocked ("\"\n", asmscn->ctx->out.file);
77 rwlock_wrlock (asmscn->ctx->lock);
81 /* Fillbytes necessary? */
82 if ((asmscn->offset & (value - 1)) != 0)
85 size_t cnt = value - (asmscn->offset & (value - 1));
87 /* Ensure there is enough room to add the fill bytes. */
88 result = __libasm_ensure_section_space (asmscn, cnt);
92 /* Fill in the bytes. We align the pattern according to the
94 size_t byteptr = asmscn->offset % asmscn->pattern->len;
96 /* Update the total size. */
97 asmscn->offset += cnt;
101 asmscn->content->data[asmscn->content->len++]
102 = asmscn->pattern->bytes[byteptr++];
104 if (byteptr == asmscn->pattern->len)
110 /* Remember the maximum alignment for this subsection. */
111 if (asmscn->max_align < value)
113 asmscn->max_align = value;
115 /* Update the parent as well (if it exists). */
116 if (asmscn->subsection_id != 0)
118 rwlock_wrlock (asmscn->data.up->ctx->lock);
120 if (asmscn->data.up->max_align < value)
121 asmscn->data.up->max_align = value;
123 rwlock_unlock (asmscn->data.up->ctx->lock);
128 rwlock_unlock (asmscn->ctx->lock);
134 /* Ensure there are at least LEN bytes available in the output buffer
137 __libasm_ensure_section_space (asmscn, len)
141 /* The blocks with the section content are kept in a circular
142 single-linked list. */
145 if (asmscn->content == NULL)
147 /* This is the first block. */
148 size = MAX (2 * len, 960);
150 asmscn->content = (struct AsmData *) malloc (sizeof (struct AsmData)
152 if (asmscn->content == NULL)
155 asmscn->content->next = asmscn->content;
159 struct AsmData *newp;
161 if (asmscn->content->maxlen - asmscn->content->len >= len)
162 /* Nothing to do, there is enough space. */
165 size = MAX (2 *len, MIN (32768, 2 * asmscn->offset));
167 newp = (struct AsmData *) malloc (sizeof (struct AsmData) + size);
171 newp->next = asmscn->content->next;
172 asmscn->content = asmscn->content->next = newp;
175 asmscn->content->len = 0;
176 asmscn->content->maxlen = size;