blob: b5a573b251af6657f75a45b374e44b52412c7f0b [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Align section.
Ulrich Dreppera38998e2005-08-03 02:05:39 +00002 Copyright (C) 2002, 2005 Red Hat, Inc.
Ulrich Drepper361df7d2006-04-04 21:38:57 +00003 This file is part of Red Hat elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
Ulrich Drepper361df7d2006-04-04 21:38:57 +00006 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.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00009
Ulrich Drepper361df7d2006-04-04 21:38:57 +000010 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,
Ulrich Drepper1e9ef502006-04-04 22:29:06 +000017 Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Ulrich Drepper361df7d2006-04-04 21:38:57 +000018
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>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000026
27#ifdef HAVE_CONFIG_H
28# include <config.h>
29#endif
30
Ulrich Dreppera38998e2005-08-03 02:05:39 +000031#include <inttypes.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000032#include <stdlib.h>
33#include <sys/param.h>
34
35#include <libasmP.h>
36#include <system.h>
37
38
39int
40asm_align (asmscn, value)
41 AsmScn_t *asmscn;
42 GElf_Word value;
43{
44 if (asmscn == NULL)
45 /* An earlier error. */
46 return -1;
47
48 /* The alignment value must be a power of two. */
49 if (unlikely (! powerof2 (value)))
50 {
51 __libasm_seterrno (ASM_E_INVALID);
52 return -1;
53 }
54
Ulrich Dreppera38998e2005-08-03 02:05:39 +000055 if (unlikely (asmscn->ctx->textp))
56 {
57 fprintf (asmscn->ctx->out.file, "\t.align %" PRId32 ", ",
58 (int32_t) value);
59 if (asmscn->pattern->len == 1)
60 fprintf (asmscn->ctx->out.file, "%02hhx\n", asmscn->pattern->bytes[0]);
61 else
62 {
63 fputc_unlocked ('"', asmscn->ctx->out.file);
64
65 for (size_t cnt = 0; cnt < asmscn->pattern->len; ++cnt)
66 fprintf (asmscn->ctx->out.file, "\\x%02hhx",
67 asmscn->pattern->bytes[cnt]);
68
69 fputs_unlocked ("\"\n", asmscn->ctx->out.file);
70 }
71 return 0;
72 }
73
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000074 rwlock_wrlock (asmscn->ctx->lock);
75
76 int result = 0;
77
78 /* Fillbytes necessary? */
79 if ((asmscn->offset & (value - 1)) != 0)
80 {
81 /* Add fillbytes. */
Ulrich Dreppera38998e2005-08-03 02:05:39 +000082 size_t cnt = value - (asmscn->offset & (value - 1));
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000083
84 /* Ensure there is enough room to add the fill bytes. */
85 result = __libasm_ensure_section_space (asmscn, cnt);
86 if (result != 0)
87 goto out;
88
89 /* Fill in the bytes. We align the pattern according to the
90 current offset. */
Ulrich Dreppera38998e2005-08-03 02:05:39 +000091 size_t byteptr = asmscn->offset % asmscn->pattern->len;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000092
93 /* Update the total size. */
94 asmscn->offset += cnt;
95
96 do
97 {
98 asmscn->content->data[asmscn->content->len++]
99 = asmscn->pattern->bytes[byteptr++];
100
101 if (byteptr == asmscn->pattern->len)
102 byteptr = 0;
103 }
104 while (--cnt > 0);
105 }
106
107 /* Remember the maximum alignment for this subsection. */
108 if (asmscn->max_align < value)
109 {
110 asmscn->max_align = value;
111
112 /* Update the parent as well (if it exists). */
113 if (asmscn->subsection_id != 0)
114 {
115 rwlock_wrlock (asmscn->data.up->ctx->lock);
116
117 if (asmscn->data.up->max_align < value)
118 asmscn->data.up->max_align = value;
119
120 rwlock_unlock (asmscn->data.up->ctx->lock);
121 }
122 }
123
124 out:
125 rwlock_unlock (asmscn->ctx->lock);
126
127 return result;
128}
129
130
131/* Ensure there are at least LEN bytes available in the output buffer
132 for ASMSCN. */
133int
134__libasm_ensure_section_space (asmscn, len)
135 AsmScn_t *asmscn;
136 size_t len;
137{
138 /* The blocks with the section content are kept in a circular
139 single-linked list. */
140 size_t size;
141
142 if (asmscn->content == NULL)
143 {
144 /* This is the first block. */
145 size = MAX (2 * len, 960);
146
147 asmscn->content = (struct AsmData *) malloc (sizeof (struct AsmData)
148 + size);
149 if (asmscn->content == NULL)
150 return -1;
151
152 asmscn->content->next = asmscn->content;
153 }
154 else
155 {
156 struct AsmData *newp;
157
158 if (asmscn->content->maxlen - asmscn->content->len >= len)
159 /* Nothing to do, there is enough space. */
160 return 0;
161
162 size = MAX (2 *len, MIN (32768, 2 * asmscn->offset));
163
164 newp = (struct AsmData *) malloc (sizeof (struct AsmData) + size);
165 if (newp == NULL)
166 return -1;
167
168 newp->next = asmscn->content->next;
169 asmscn->content = asmscn->content->next = newp;
170 }
171
172 asmscn->content->len = 0;
173 asmscn->content->maxlen = size;
174
175 return 0;
176}