1/* Align section.
2   Copyright (C) 2002, 2005 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 <inttypes.h>
35#include <stdlib.h>
36#include <sys/param.h>
37
38#include <libasmP.h>
39#include <system.h>
40
41
42int
43asm_align (AsmScn_t *asmscn, GElf_Word value)
44{
45  if (asmscn == NULL)
46    /* An earlier error.  */
47    return -1;
48
49      /* The alignment value must be a power of two.  */
50  if (unlikely (! powerof2 (value)))
51    {
52      __libasm_seterrno (ASM_E_INVALID);
53      return -1;
54    }
55
56  if (unlikely (asmscn->ctx->textp))
57    {
58      fprintf (asmscn->ctx->out.file, "\t.align %" PRId32 ", ",
59	       (int32_t) value);
60      if (asmscn->pattern->len == 1)
61	fprintf (asmscn->ctx->out.file, "%02hhx\n", asmscn->pattern->bytes[0]);
62      else
63	{
64	  fputc_unlocked ('"', asmscn->ctx->out.file);
65
66	  for (size_t cnt = 0; cnt < asmscn->pattern->len; ++cnt)
67	    fprintf (asmscn->ctx->out.file, "\\x%02hhx",
68		     asmscn->pattern->bytes[cnt]);
69
70	  fputs_unlocked ("\"\n", asmscn->ctx->out.file);
71	}
72      return 0;
73    }
74
75  rwlock_wrlock (asmscn->ctx->lock);
76
77  int result = 0;
78
79  /* Fillbytes necessary?  */
80  if ((asmscn->offset & (value - 1)) != 0)
81    {
82      /* Add fillbytes.  */
83      size_t cnt = value - (asmscn->offset & (value - 1));
84
85      /* Ensure there is enough room to add the fill bytes.  */
86      result = __libasm_ensure_section_space (asmscn, cnt);
87      if (result != 0)
88	goto out;
89
90      /* Fill in the bytes.  We align the pattern according to the
91	 current offset.  */
92      size_t byteptr = asmscn->offset % asmscn->pattern->len;
93
94      /* Update the total size.  */
95      asmscn->offset += cnt;
96
97      do
98	{
99	  asmscn->content->data[asmscn->content->len++]
100	    = asmscn->pattern->bytes[byteptr++];
101
102	  if (byteptr == asmscn->pattern->len)
103	    byteptr = 0;
104	}
105      while (--cnt > 0);
106    }
107
108  /* Remember the maximum alignment for this subsection.  */
109  if (asmscn->max_align < value)
110    {
111      asmscn->max_align = value;
112
113      /* Update the parent as well (if it exists).  */
114      if (asmscn->subsection_id != 0)
115	{
116	  rwlock_wrlock (asmscn->data.up->ctx->lock);
117
118	  if (asmscn->data.up->max_align < value)
119	    asmscn->data.up->max_align = value;
120
121	  rwlock_unlock (asmscn->data.up->ctx->lock);
122	}
123    }
124
125 out:
126  rwlock_unlock (asmscn->ctx->lock);
127
128  return result;
129}
130
131
132/* Ensure there are at least LEN bytes available in the output buffer
133   for ASMSCN.  */
134int
135internal_function
136__libasm_ensure_section_space (AsmScn_t *asmscn, 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}
177