Files
third_party_elfutils/libasm/asm_align.c
T
Mark Wielaard de2ed97f33 Update name, license and contributor policy.
* Change name from "Red Hat elfutils" to "elfutils".
* Update license of standalone tools and test from GPLv2 to GPLv3+.
* Change license of libraries from GPLv2+exception to GPLv2/LGPLv3+.
* Add Developer Certificate of Origin based contributor policy.

top-level:

- COPYING: Upgraded from GPLv2 to GPLv3.
- CONTRIBUTING, COPYING-GPLv2, COPYING-LGPLv3: New files.
- NEWS: Added note about new contribution and license policy.
- Makefile.am: Updated to GPLv3, added new files to EXTRA_DIST.
- configure.ac: Update to GPLv3, changed AC_INIT name to 'elfutils'.

backends, lib, libasm, libcpu, libdw, libdwfl, libebl, libelf:

- All files updated to GPLv2/LGPLv3+. Except some very small files
  (<5 lines) which didn't have any headers at all before, the linker
  .maps files and the libcpu/defs files which only contain data and
  libelf/elf.h which comes from glibc and is under LGPLv2+.

config:

- elfutils.spec.in: Add new License: headers and new %doc files.
- Update all license headers to GPLv2/LGPLv3+ for files used by libs.

src, tests:

- All files updated to GPLv3+. Except for the test bz2 data files, the
  linker maps and script files and some very small files (<5 lines)
  that don't have any headers.

Signed-off-by: Richard Fontana <rfontana@redhat.com>
Signed-off-by: Mark Wielaard <mjw@redhat.com>
2012-06-05 23:12:05 +02:00

180 lines
4.4 KiB
C

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