|
Packit |
032894 |
/* Align section.
|
|
Packit |
032894 |
Copyright (C) 2002, 2005 Red Hat, Inc.
|
|
Packit |
032894 |
This file is part of elfutils.
|
|
Packit |
032894 |
Written by Ulrich Drepper <drepper@redhat.com>, 2002.
|
|
Packit |
032894 |
|
|
Packit |
032894 |
This file is free software; you can redistribute it and/or modify
|
|
Packit |
032894 |
it under the terms of either
|
|
Packit |
032894 |
|
|
Packit |
032894 |
* the GNU Lesser General Public License as published by the Free
|
|
Packit |
032894 |
Software Foundation; either version 3 of the License, or (at
|
|
Packit |
032894 |
your option) any later version
|
|
Packit |
032894 |
|
|
Packit |
032894 |
or
|
|
Packit |
032894 |
|
|
Packit |
032894 |
* the GNU General Public License as published by the Free
|
|
Packit |
032894 |
Software Foundation; either version 2 of the License, or (at
|
|
Packit |
032894 |
your option) any later version
|
|
Packit |
032894 |
|
|
Packit |
032894 |
or both in parallel, as here.
|
|
Packit |
032894 |
|
|
Packit |
032894 |
elfutils is distributed in the hope that it will be useful, but
|
|
Packit |
032894 |
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
032894 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
032894 |
General Public License for more details.
|
|
Packit |
032894 |
|
|
Packit |
032894 |
You should have received copies of the GNU General Public License and
|
|
Packit |
032894 |
the GNU Lesser General Public License along with this program. If
|
|
Packit |
032894 |
not, see <http://www.gnu.org/licenses/>. */
|
|
Packit |
032894 |
|
|
Packit |
032894 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
032894 |
# include <config.h>
|
|
Packit |
032894 |
#endif
|
|
Packit |
032894 |
|
|
Packit |
032894 |
#include <inttypes.h>
|
|
Packit |
032894 |
#include <stdlib.h>
|
|
Packit |
032894 |
|
|
Packit |
032894 |
#include <libasmP.h>
|
|
Packit |
032894 |
#include <system.h>
|
|
Packit |
032894 |
|
|
Packit |
032894 |
|
|
Packit |
032894 |
int
|
|
Packit |
032894 |
asm_align (AsmScn_t *asmscn, GElf_Word value)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
if (asmscn == NULL)
|
|
Packit |
032894 |
/* An earlier error. */
|
|
Packit |
032894 |
return -1;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* The alignment value must be a power of two. */
|
|
Packit |
032894 |
if (unlikely (! powerof2 (value)))
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
__libasm_seterrno (ASM_E_INVALID);
|
|
Packit |
032894 |
return -1;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (unlikely (asmscn->ctx->textp))
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
fprintf (asmscn->ctx->out.file, "\t.align %" PRId32 ", ",
|
|
Packit |
032894 |
(int32_t) value);
|
|
Packit |
032894 |
if (asmscn->pattern->len == 1)
|
|
Packit |
032894 |
fprintf (asmscn->ctx->out.file, "%02hhx\n", asmscn->pattern->bytes[0]);
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
fputc_unlocked ('"', asmscn->ctx->out.file);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
for (size_t cnt = 0; cnt < asmscn->pattern->len; ++cnt)
|
|
Packit |
032894 |
fprintf (asmscn->ctx->out.file, "\\x%02hhx",
|
|
Packit |
032894 |
asmscn->pattern->bytes[cnt]);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
fputs_unlocked ("\"\n", asmscn->ctx->out.file);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
return 0;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
rwlock_wrlock (asmscn->ctx->lock);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
int result = 0;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Fillbytes necessary? */
|
|
Packit |
032894 |
if ((asmscn->offset & (value - 1)) != 0)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* Add fillbytes. */
|
|
Packit |
032894 |
size_t cnt = value - (asmscn->offset & (value - 1));
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Ensure there is enough room to add the fill bytes. */
|
|
Packit |
032894 |
result = __libasm_ensure_section_space (asmscn, cnt);
|
|
Packit |
032894 |
if (result != 0)
|
|
Packit |
032894 |
goto out;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Fill in the bytes. We align the pattern according to the
|
|
Packit |
032894 |
current offset. */
|
|
Packit |
032894 |
size_t byteptr = asmscn->offset % asmscn->pattern->len;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Update the total size. */
|
|
Packit |
032894 |
asmscn->offset += cnt;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
do
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
asmscn->content->data[asmscn->content->len++]
|
|
Packit |
032894 |
= asmscn->pattern->bytes[byteptr++];
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (byteptr == asmscn->pattern->len)
|
|
Packit |
032894 |
byteptr = 0;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
while (--cnt > 0);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Remember the maximum alignment for this subsection. */
|
|
Packit |
032894 |
if (asmscn->max_align < value)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
asmscn->max_align = value;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Update the parent as well (if it exists). */
|
|
Packit |
032894 |
if (asmscn->subsection_id != 0)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
rwlock_wrlock (asmscn->data.up->ctx->lock);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (asmscn->data.up->max_align < value)
|
|
Packit |
032894 |
asmscn->data.up->max_align = value;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
rwlock_unlock (asmscn->data.up->ctx->lock);
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
out:
|
|
Packit |
032894 |
rwlock_unlock (asmscn->ctx->lock);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
return result;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
|
|
Packit |
032894 |
/* Ensure there are at least LEN bytes available in the output buffer
|
|
Packit |
032894 |
for ASMSCN. */
|
|
Packit |
032894 |
int
|
|
Packit |
032894 |
internal_function
|
|
Packit |
032894 |
__libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* The blocks with the section content are kept in a circular
|
|
Packit |
032894 |
single-linked list. */
|
|
Packit |
032894 |
size_t size;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (asmscn->content == NULL)
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
/* This is the first block. */
|
|
Packit |
032894 |
size = MAX (2 * len, 960);
|
|
Packit |
032894 |
|
|
Packit |
032894 |
asmscn->content = (struct AsmData *) malloc (sizeof (struct AsmData)
|
|
Packit |
032894 |
+ size);
|
|
Packit |
032894 |
if (asmscn->content == NULL)
|
|
Packit |
032894 |
return -1;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
asmscn->content->next = asmscn->content;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
else
|
|
Packit |
032894 |
{
|
|
Packit |
032894 |
struct AsmData *newp;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
if (asmscn->content->maxlen - asmscn->content->len >= len)
|
|
Packit |
032894 |
/* Nothing to do, there is enough space. */
|
|
Packit |
032894 |
return 0;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
size = MAX (2 *len, MIN (32768, 2 * asmscn->offset));
|
|
Packit |
032894 |
|
|
Packit |
032894 |
newp = (struct AsmData *) malloc (sizeof (struct AsmData) + size);
|
|
Packit |
032894 |
if (newp == NULL)
|
|
Packit |
032894 |
return -1;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
newp->next = asmscn->content->next;
|
|
Packit |
032894 |
asmscn->content = asmscn->content->next = newp;
|
|
Packit |
032894 |
}
|
|
Packit |
032894 |
|
|
Packit |
032894 |
asmscn->content->len = 0;
|
|
Packit |
032894 |
asmscn->content->maxlen = size;
|
|
Packit |
032894 |
|
|
Packit |
032894 |
return 0;
|
|
Packit |
032894 |
}
|