summaryrefslogtreecommitdiff
path: root/libasm
diff options
context:
space:
mode:
authorUlrich Drepper <drepper@redhat.com>2005-07-26 05:00:05 +0000
committerUlrich Drepper <drepper@redhat.com>2005-07-26 05:00:05 +0000
commitb08d5a8fb42f4586d756068065186b5af7e48dad (patch)
tree9f05f86be7877ed461b4dc05f53b29ea4fc0d2a1 /libasm
downloadelfutils-b08d5a8fb42f4586d756068065186b5af7e48dad.tar.gz
Adjust for monotone.
Diffstat (limited to 'libasm')
-rw-r--r--libasm/.cvsignore1
-rw-r--r--libasm/ChangeLog52
-rw-r--r--libasm/Makefile.am86
-rw-r--r--libasm/asm_abort.c45
-rw-r--r--libasm/asm_addint16.c17
-rw-r--r--libasm/asm_addint32.c17
-rw-r--r--libasm/asm_addint64.c17
-rw-r--r--libasm/asm_addint8.c105
-rw-r--r--libasm/asm_addsleb128.c84
-rw-r--r--libasm/asm_addstrz.c113
-rw-r--r--libasm/asm_adduint16.c17
-rw-r--r--libasm/asm_adduint32.c17
-rw-r--r--libasm/asm_adduint64.c17
-rw-r--r--libasm/asm_adduint8.c41
-rw-r--r--libasm/asm_adduleb128.c80
-rw-r--r--libasm/asm_align.c147
-rw-r--r--libasm/asm_begin.c151
-rw-r--r--libasm/asm_end.c593
-rw-r--r--libasm/asm_error.c183
-rw-r--r--libasm/asm_fill.c62
-rw-r--r--libasm/asm_getelf.c29
-rw-r--r--libasm/asm_newabssym.c121
-rw-r--r--libasm/asm_newcomsym.c102
-rw-r--r--libasm/asm_newscn.c202
-rw-r--r--libasm/asm_newscn_ingrp.c66
-rw-r--r--libasm/asm_newscngrp.c90
-rw-r--r--libasm/asm_newsubscn.c84
-rw-r--r--libasm/asm_newsym.c123
-rw-r--r--libasm/asm_scngrp_newsignature.c33
-rw-r--r--libasm/libasm.h155
-rw-r--r--libasm/libasm.map33
-rw-r--r--libasm/libasmP.h268
-rw-r--r--libasm/symbolhash.c39
-rw-r--r--libasm/symbolhash.h25
34 files changed, 3215 insertions, 0 deletions
diff --git a/libasm/.cvsignore b/libasm/.cvsignore
new file mode 100644
index 00000000..70845e08
--- /dev/null
+++ b/libasm/.cvsignore
@@ -0,0 +1 @@
+Makefile.in
diff --git a/libasm/ChangeLog b/libasm/ChangeLog
new file mode 100644
index 00000000..8b7b44d2
--- /dev/null
+++ b/libasm/ChangeLog
@@ -0,0 +1,52 @@
+2005-02-15 Ulrich Drepper <drepper@redhat.com>
+
+ * Makefile (AM_CFLAGS): Add -Wunused -Wextra -Wformat=2.
+
+ * asm_end.c (text_end): Mark parameter as possibly unused.
+
+2005-02-06 Ulrich Drepper <drepper@redhat.com>
+
+ * Makefile.am: Remove lint handling.
+
+2005-02-05 Ulrich Drepper <drepper@redhat.com>
+
+ * asm_end.c (binary_end): Don't terminate with error() in case
+ something goes wrong.
+
+ * Makefile.am: Check for text relocations in constructed DSO.
+
+ * Makefile.am (AM_CFLAGS): More warnings. Add -fmudflap for MUDFLAP.
+
+ * asm_end.c (binary_end): Remove shadowing variables.
+ Little cleanups.
+
+ * asm_newsym.c: Allocate memory for the string parameter.
+
+2005-02-04 Ulrich Drepper <drepper@redhat.com>
+
+ * asm_newscn_ingrp.c (asm_newscn_ingrp): Use INTUSE to reference
+ asm_newscn.
+
+2004-09-25 Ulrich Drepper <drepper@redhat.com>
+
+ * asm_error.c: Make compile with gcc 4.0.
+
+2004-01-20 Ulrich Drepper <drepper@redhat.com>
+
+ * Makefile.am: Support building with mudflap.
+
+2004-01-18 Ulrich Drepper <drepper@redhat.com>
+
+ * libasmP.h (_): Use elfutils domain.
+
+2004-01-17 Ulrich Drepper <drepper@redhat.com>
+
+ * Makefile.am: Support building with mudflap.
+
+2003-08-13 Ulrich Drepper <drepper@redhat.com>
+
+ * Makefile.in: Depend on libebl.a, not libebl.so.
+
+2003-08-11 Ulrich Drepper <drepper@redhat.com>
+
+ * Moved to CVS archive.
diff --git a/libasm/Makefile.am b/libasm/Makefile.am
new file mode 100644
index 00000000..34b19d24
--- /dev/null
+++ b/libasm/Makefile.am
@@ -0,0 +1,86 @@
+## Process this file with automake to create Makefile.in
+##
+## Copyright (C) 2002, 2004, 2005 Red Hat, Inc.
+##
+## This program is Open Source software; you can redistribute it and/or
+## modify it under the terms of the Open Software License version 1.0 as
+## published by the Open Source Initiative.
+##
+## You should have received a copy of the Open Software License along
+## with this program; if not, you may obtain a copy of the Open Software
+## License version 1.0 from http://www.opensource.org/licenses/osl.php or
+## by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+## 3001 King Ranch Road, Ukiah, CA 95482.
+##
+DEFS = -D_GNU_SOURCE -DHAVE_CONFIG_H
+if MUDFLAP
+AM_CFLAGS = -fmudflap
+else
+AM_CFLAGS =
+endif
+AM_CFLAGS += -Wall -Wshadow -Werror -Wunused -Wextra -Wformat=2
+INCLUDES = -I. -I$(srcdir) -I.. -I$(top_srcdir)/libelf -I$(top_srcdir)/libebl \
+ -I$(top_srcdir)/lib
+GCC_INCLUDE = -I$(shell $(CC) -print-file-name=include)
+VERSION = 1
+
+lib_LIBRARIES = libasm.a
+if !MUDFLAP
+noinst_LIBRARIES = libasm_pic.a
+noinst_PROGRAMS = $(noinst_LIBRARIES:_pic.a=.so)
+endif
+euincludedir = ${includedir}/elfutils
+euinclude_HEADERS = libasm.h
+
+libasm_a_SOURCES = asm_begin.c asm_abort.c asm_end.c asm_error.c \
+ asm_getelf.c asm_newscn.c asm_newscn_ingrp.c \
+ asm_newsubscn.c asm_newsym.c asm_newcomsym.c \
+ asm_newabssym.c \
+ asm_newscngrp.c asm_scngrp_newsignature.c \
+ asm_fill.c asm_align.c asm_addstrz.c \
+ asm_addint8.c asm_adduint8.c \
+ asm_addint16.c asm_adduint16.c \
+ asm_addint32.c asm_adduint32.c \
+ asm_addint64.c asm_adduint64.c \
+ asm_adduleb128.c asm_addsleb128.c \
+ symbolhash.c
+
+if !MUDFLAP
+libasm_pic_a_SOURCES =
+am_libasm_pic_a_OBJECTS = $(libasm_a_SOURCES:.c=.os)
+
+libasm_so_SOURCES =
+libasm.so: libasm_pic.a libasm.map
+ $(CC) -shared -o $@ -Wl,--whole-archive,$<,--no-whole-archive \
+ -Wl,--version-script,$(srcdir)/libasm.map,--no-undefined \
+ -Wl,--soname,$@.$(VERSION) \
+ ../libebl/libebl.a ../libelf/libelf.so
+ if readelf -d $@ | fgrep -q TEXTREL; then exit 1; fi
+ ln -fs $@ $@.$(VERSION)
+
+
+%.os: %.c %.o
+ if $(COMPILE) -c -o $@ -fpic -DPIC -DSHARED -MT $@ -MD -MP \
+ -MF "$(DEPDIR)/$*.Tpo" `test -f '$<' || echo '$(srcdir)/'`$<; \
+ then cat "$(DEPDIR)/$*.Tpo" >> "$(DEPDIR)/$*.Po"; \
+ rm -f "$(DEPDIR)/$*.Tpo"; \
+ else rm -f "$(DEPDIR)/$*.Tpo"; exit 1; \
+ fi
+
+install: install-am libasm.so
+ $(mkinstalldirs) $(DESTDIR)$(libdir)
+ $(INSTALL_PROGRAM) libasm.so $(DESTDIR)$(libdir)/libasm-$(PACKAGE_VERSION).so
+ ln -fs libasm-$(PACKAGE_VERSION).so $(DESTDIR)$(libdir)/libasm.so.$(VERSION)
+ ln -fs libasm.so.$(VERSION) $(DESTDIR)$(libdir)/libasm.so
+
+uninstall: uninstall-am
+ rm -f $(DESTDIR)$(libdir)/libasm-$(PACKAGE_VERSION).so
+ rm -f $(DESTDIR)$(libdir)/libasm.so.$(VERSION)
+ rm -f $(DESTDIR)$(libdir)/libasm.so
+ rmdir --ignore-fail-on-non-empty $(DESTDIR)$(includedir)/elfutils
+endif
+
+noinst_HEADERS = libasmP.h symbolhash.h
+EXTRA_DIST = libasm.map
+
+CLEANFILES = $(am_libasm_pic_a_OBJECTS)
diff --git a/libasm/asm_abort.c b/libasm/asm_abort.c
new file mode 100644
index 00000000..f35757ca
--- /dev/null
+++ b/libasm/asm_abort.c
@@ -0,0 +1,45 @@
+/* Abort operations on the assembler context, free all resources.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <libasmP.h>
+#include <libelf.h>
+
+
+int
+asm_abort (ctx)
+ AsmCtx_t *ctx;
+{
+ if (ctx == NULL)
+ /* Something went wrong earlier. */
+ return -1;
+
+ if (likely (! ctx->textp))
+ /* First free the ELF file. We don't care about the result. */
+ (void) elf_end (ctx->out.elf);
+
+ /* Now close the temporary file and remove it. */
+ (void) unlink (ctx->tmp_fname);
+
+ /* Free the resources. */
+ __libasm_finictx (ctx);
+
+ return 0;
+}
diff --git a/libasm/asm_addint16.c b/libasm/asm_addint16.c
new file mode 100644
index 00000000..98125171
--- /dev/null
+++ b/libasm/asm_addint16.c
@@ -0,0 +1,17 @@
+/* Add integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#define SIZE 16
+
+#include "asm_addint8.c"
diff --git a/libasm/asm_addint32.c b/libasm/asm_addint32.c
new file mode 100644
index 00000000..05ac3d25
--- /dev/null
+++ b/libasm/asm_addint32.c
@@ -0,0 +1,17 @@
+/* Add integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#define SIZE 32
+
+#include "asm_addint8.c"
diff --git a/libasm/asm_addint64.c b/libasm/asm_addint64.c
new file mode 100644
index 00000000..f9d93f80
--- /dev/null
+++ b/libasm/asm_addint64.c
@@ -0,0 +1,17 @@
+/* Add integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#define SIZE 64
+
+#include "asm_addint8.c"
diff --git a/libasm/asm_addint8.c b/libasm/asm_addint8.c
new file mode 100644
index 00000000..35d9bf57
--- /dev/null
+++ b/libasm/asm_addint8.c
@@ -0,0 +1,105 @@
+/* Add integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <byteswap.h>
+#include <endian.h>
+#include <inttypes.h>
+#include <string.h>
+
+#include <libasmP.h>
+
+#ifndef SIZE
+# define SIZE 8
+#endif
+
+#define FCT(size) _FCT(size)
+#define _FCT(size) asm_addint##size
+#define TYPE(size) _TYPE(size)
+#define _TYPE(size) int##size##_t
+#define BSWAP(size) _BSWAP(size)
+#define _BSWAP(size) bswap_##size
+
+
+int
+FCT(SIZE) (asmscn, num)
+ AsmScn_t *asmscn;
+ TYPE(SIZE) num;
+{
+ if (asmscn == NULL)
+ return -1;
+
+ if (asmscn->type == SHT_NOBITS && unlikely (num != 0))
+ {
+ __libasm_seterrno (ASM_E_TYPE);
+ return -1;
+ }
+
+ if (unlikely (asmscn->ctx->textp))
+ {
+ // XXX Needs to use backend specified pseudo-ops
+ if (SIZE == 8)
+ printf ("\t.byte\t%" PRId8 "\n", (int8_t) num);
+ else if (SIZE == 16)
+ printf ("\t.value\t%" PRId16 "\n", (int16_t) num);
+ else if (SIZE == 32)
+ printf ("\t.long\t%" PRId32 "\n", (int32_t) num);
+ else
+ {
+ // XXX This is not necessary for 64-bit machines
+ bool is_leb = (elf_getident (asmscn->ctx->out.elf, NULL)[EI_DATA]
+ == ELFDATA2LSB);
+
+ printf ("\t.long\t%" PRId32 "\n\t.long\t%" PRId32 "\n",
+ (int32_t) (is_leb
+ ? num % 0x100000000ll : num / 0x100000000ll),
+ (int32_t) (is_leb
+ ? num / 0x100000000ll : num % 0x100000000ll));
+ }
+ }
+ else
+ {
+#if SIZE > 8
+ bool is_leb = (elf_getident (asmscn->ctx->out.elf, NULL)[EI_DATA]
+ == ELFDATA2LSB);
+#endif
+ TYPE(SIZE) var = num;
+
+ /* Make sure we have enough room. */
+ if (__libasm_ensure_section_space (asmscn, SIZE / 8) != 0)
+ return -1;
+
+#if SIZE > 8
+ if ((BYTE_ORDER == LITTLE_ENDIAN && !is_leb)
+ || (BYTE_ORDER == BIG_ENDIAN && is_leb))
+ var = BSWAP(SIZE) (var);
+#endif
+
+ /* Copy the variable value. */
+ if (likely (asmscn->type == SHT_NOBITS))
+ memcpy (&asmscn->content->data[asmscn->content->len], &var, SIZE / 8);
+
+ /* Adjust the pointer in the data buffer. */
+ asmscn->content->len += SIZE / 8;
+
+ /* Increment the offset in the (sub)section. */
+ asmscn->offset += SIZE / 8;
+ }
+
+ return 0;
+}
+INTDEF(FCT(SIZE))
diff --git a/libasm/asm_addsleb128.c b/libasm/asm_addsleb128.c
new file mode 100644
index 00000000..0661b36e
--- /dev/null
+++ b/libasm/asm_addsleb128.c
@@ -0,0 +1,84 @@
+/* Add signed little endian base 128 integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <string.h>
+
+#include <libasmP.h>
+
+
+int
+asm_addsleb128 (asmscn, num)
+ AsmScn_t *asmscn;
+ int32_t num;
+{
+ if (asmscn == NULL)
+ return -1;
+
+ if (asmscn->type == SHT_NOBITS && unlikely (num != 0))
+ {
+ __libasm_seterrno (ASM_E_TYPE);
+ return -1;
+ }
+
+ if (unlikely (asmscn->ctx->textp))
+ printf ("\t.sleb128\t%" PRId32 "\n", num);
+ else
+ {
+ char tmpbuf[(sizeof (num) * 8 + 6) / 7];
+ char *dest = tmpbuf;
+ uint32_t byte;
+ int32_t endval = num >> 31;
+
+ if (num == 0)
+ byte = 0;
+ else
+ while (1)
+ {
+ byte = num & 0x7f;
+
+ num >>= 7;
+ if (num == endval)
+ /* This is the last byte. */
+ break;
+
+ *dest++ = byte | 0x80;
+ }
+
+ *dest++ = byte;
+
+ /* Number of bytes produced. */
+ size_t nbytes = dest - tmpbuf;
+
+ /* Make sure we have enough room. */
+ if (__libasm_ensure_section_space (asmscn, nbytes) != 0)
+ return -1;
+
+ /* Copy the bytes. */
+ if (likely (asmscn->type != SHT_NOBITS))
+ memcpy (&asmscn->content->data[asmscn->content->len], tmpbuf, nbytes);
+
+ /* Adjust the pointer in the data buffer. */
+ asmscn->content->len += nbytes;
+
+ /* Increment the offset in the (sub)section. */
+ asmscn->offset += nbytes;
+ }
+
+ return 0;
+}
diff --git a/libasm/asm_addstrz.c b/libasm/asm_addstrz.c
new file mode 100644
index 00000000..e183b0b5
--- /dev/null
+++ b/libasm/asm_addstrz.c
@@ -0,0 +1,113 @@
+/* Add string to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <ctype.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <libasmP.h>
+
+
+/* Add zero terminated string STR of size LEN to (sub)section ASMSCN. */
+int
+asm_addstrz (asmscn, str, len)
+ AsmScn_t *asmscn;
+ const char *str;
+ size_t len;
+{
+ if (asmscn == NULL)
+ return -1;
+
+ if (unlikely (asmscn->type == SHT_NOBITS))
+ {
+ if (len == 0)
+ {
+ if (str[0] != '\0')
+ {
+ __libasm_seterrno (ASM_E_TYPE);
+ return -1;
+ }
+ }
+ else
+ {
+ size_t cnt;
+
+ for (cnt = 0; cnt < len; ++cnt)
+ if (str[cnt] != '\0')
+ {
+ __libasm_seterrno (ASM_E_TYPE);
+ return -1;
+ }
+ }
+ }
+
+ if (len == 0)
+ len = strlen (str) + 1;
+
+ if (unlikely (asmscn->ctx->textp))
+ {
+ bool nextline = true;
+
+ do
+ {
+ if (nextline)
+ {
+ fputs ("\t.string\t\"", asmscn->ctx->out.file);
+ nextline = false;
+ }
+
+ if (*str == '\0')
+ fputs ("\\000", asmscn->ctx->out.file);
+ else if (! isascii (*str))
+ fprintf (asmscn->ctx->out.file, "\\%03o",
+ (unsigned int) *((unsigned char *)str));
+ else if (*str == '\\')
+ fputs ("\\\\", asmscn->ctx->out.file);
+ else if (*str == '\n')
+ {
+ fputs ("\\n\"", asmscn->ctx->out.file);
+ nextline = true;
+ }
+ else
+ fputc (*str, asmscn->ctx->out.file);
+
+ ++str;
+ }
+ while (--len > 0 && (len > 1 || *str != '\0'));
+
+ if (! nextline)
+ fputs ("\"\n", asmscn->ctx->out.file);
+ }
+ else
+ {
+ /* Make sure there is enough room. */
+ if (__libasm_ensure_section_space (asmscn, len) != 0)
+ return -1;
+
+ /* Copy the string. */
+ memcpy (&asmscn->content->data[asmscn->content->len], str, len);
+
+ /* Adjust the pointer in the data buffer. */
+ asmscn->content->len += len;
+
+ /* Increment the offset in the (sub)section. */
+ asmscn->offset += len;
+ }
+
+ return 0;
+}
diff --git a/libasm/asm_adduint16.c b/libasm/asm_adduint16.c
new file mode 100644
index 00000000..8c1e698f
--- /dev/null
+++ b/libasm/asm_adduint16.c
@@ -0,0 +1,17 @@
+/* Add unsigned integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#define SIZE 16
+
+#include "asm_adduint8.c"
diff --git a/libasm/asm_adduint32.c b/libasm/asm_adduint32.c
new file mode 100644
index 00000000..a2257954
--- /dev/null
+++ b/libasm/asm_adduint32.c
@@ -0,0 +1,17 @@
+/* Add unsigned integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#define SIZE 32
+
+#include "asm_adduint8.c"
diff --git a/libasm/asm_adduint64.c b/libasm/asm_adduint64.c
new file mode 100644
index 00000000..edda4d38
--- /dev/null
+++ b/libasm/asm_adduint64.c
@@ -0,0 +1,17 @@
+/* Add unsigned integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#define SIZE 64
+
+#include "asm_adduint8.c"
diff --git a/libasm/asm_adduint8.c b/libasm/asm_adduint8.c
new file mode 100644
index 00000000..7bfb3414
--- /dev/null
+++ b/libasm/asm_adduint8.c
@@ -0,0 +1,41 @@
+/* Add unsigned integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <libasmP.h>
+
+#ifndef SIZE
+# define SIZE 8
+#endif
+
+#define UFCT(size) _UFCT(size)
+#define _UFCT(size) asm_adduint##size
+#define FCT(size) _FCT(size)
+#define _FCT(size) asm_addint##size
+#define UTYPE(size) _UTYPE(size)
+#define _UTYPE(size) uint##size##_t
+#define TYPE(size) _TYPE(size)
+#define _TYPE(size) int##size##_t
+
+
+int
+UFCT(SIZE) (asmscn, num)
+ AsmScn_t *asmscn;
+ UTYPE(SIZE) num;
+{
+ return INTUSE(FCT(SIZE)) (asmscn, (TYPE(SIZE)) num);
+}
diff --git a/libasm/asm_adduleb128.c b/libasm/asm_adduleb128.c
new file mode 100644
index 00000000..521d265a
--- /dev/null
+++ b/libasm/asm_adduleb128.c
@@ -0,0 +1,80 @@
+/* Add integer to a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <string.h>
+
+#include "libasmP.h"
+
+
+int
+asm_adduleb128 (asmscn, num)
+ AsmScn_t *asmscn;
+ uint32_t num;
+{
+ if (asmscn == NULL)
+ return -1;
+
+ if (asmscn->type == SHT_NOBITS && unlikely (num != 0))
+ {
+ __libasm_seterrno (ASM_E_TYPE);
+ return -1;
+ }
+
+ if (unlikely (asmscn->ctx->textp))
+ printf ("\t.uleb128\t%" PRIu32 "\n", num);
+ else
+ {
+ char tmpbuf[(sizeof (num) * 8 + 6) / 7];
+ char *dest = tmpbuf;
+ uint32_t byte;
+
+ while (1)
+ {
+ byte = num & 0x7f;
+
+ num >>= 7;
+ if (num == 0)
+ /* This is the last byte. */
+ break;
+
+ *dest++ = byte | 0x80;
+ }
+
+ *dest++ = byte;
+
+ /* Number of bytes produced. */
+ size_t nbytes = dest - tmpbuf;
+
+ /* Make sure we have enough room. */
+ if (__libasm_ensure_section_space (asmscn, nbytes) != 0)
+ return -1;
+
+ /* Copy the bytes. */
+ if (likely (asmscn->type != SHT_NOBITS))
+ memcpy (&asmscn->content->data[asmscn->content->len], tmpbuf, nbytes);
+
+ /* Adjust the pointer in the data buffer. */
+ asmscn->content->len += nbytes;
+
+ /* Increment the offset in the (sub)section. */
+ asmscn->offset += nbytes;
+ }
+
+ return 0;
+}
diff --git a/libasm/asm_align.c b/libasm/asm_align.c
new file mode 100644
index 00000000..e7350924
--- /dev/null
+++ b/libasm/asm_align.c
@@ -0,0 +1,147 @@
+/* Align section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#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;
+ }
+
+ rwlock_wrlock (asmscn->ctx->lock);
+
+ int result = 0;
+
+ /* Fillbytes necessary? */
+ if ((asmscn->offset & (value - 1)) != 0)
+ {
+ /* Add fillbytes. */
+ size_t cnt;
+ size_t byteptr;
+
+ 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. */
+ 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;
+}
diff --git a/libasm/asm_begin.c b/libasm/asm_begin.c
new file mode 100644
index 00000000..3896f78f
--- /dev/null
+++ b/libasm/asm_begin.c
@@ -0,0 +1,151 @@
+/* Create descriptor for assembling.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <gelf.h>
+#include "libasmP.h"
+#include <system.h>
+
+
+static AsmCtx_t *
+prepare_text_output (AsmCtx_t *result)
+{
+ return result;
+}
+
+
+static AsmCtx_t *
+prepare_binary_output (AsmCtx_t *result, int machine, int klass, int data)
+{
+ GElf_Ehdr *ehdr;
+ GElf_Ehdr ehdr_mem;
+
+ /* Create the ELF descriptor for the file. */
+ result->out.elf = elf_begin (result->fd, ELF_C_WRITE_MMAP, NULL);
+ if (result->out.elf == NULL)
+ {
+ err_libelf:
+ unlink (result->tmp_fname);
+ close (result->fd);
+ free (result);
+ __libasm_seterrno (ASM_E_LIBELF);
+ return NULL;
+ }
+
+ /* Create the ELF header for the output file. */
+ if (gelf_newehdr (result->out.elf, klass) == 0)
+ goto err_libelf;
+
+ ehdr = gelf_getehdr (result->out.elf, &ehdr_mem);
+ /* If this failed we are in trouble. */
+ assert (ehdr != NULL);
+
+ /* We create an object file. */
+ ehdr->e_type = ET_REL;
+ /* Set the ELF version. */
+ ehdr->e_version = EV_CURRENT;
+
+ /* Use the machine value the user provided. */
+ ehdr->e_machine = machine;
+ /* Same for the class and endianness. */
+ ehdr->e_ident[EI_CLASS] = klass;
+ ehdr->e_ident[EI_DATA] = data;
+
+ memcpy (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG);
+
+ /* Write the ELF header information back. */
+ (void) gelf_update_ehdr (result->out.elf, ehdr);
+
+ /* No section so far. */
+ result->section_list = NULL;
+
+ /* Initialize the hash table. */
+ asm_symbol_tab_init (&result->symbol_tab, 67);
+ result->nsymbol_tab = 0;
+ /* And the string tables. */
+ result->section_strtab = ebl_strtabinit (true);
+ result->symbol_strtab = ebl_strtabinit (true);
+
+ /* We have no section groups so far. */
+ result->groups = NULL;
+ result->ngroups = 0;
+
+ return result;
+}
+
+
+AsmCtx_t *
+asm_begin (fname, textp, machine, klass, data)
+ const char *fname;
+ bool textp;
+ int machine;
+ int klass;
+ int data;
+{
+ size_t fname_len = strlen (fname);
+ AsmCtx_t *result;
+
+
+ /* First order of business: find the appropriate backend. If it
+ does not exist we don't have to look further. */
+ // XXX
+
+ /* Create the file descriptor. We do not generate the output file
+ right away. Instead we create a temporary file in the same
+ directory which, if everything goes alright, will replace a
+ possibly existing file with the given name. */
+ result = (AsmCtx_t *) malloc (sizeof (AsmCtx_t) + 2 * fname_len + 9);
+ if (result == NULL)
+ return NULL;
+
+ /* Initialize the lock. */
+ rwlock_init (result->lock);
+
+ /* Create the name of the temporary file. */
+ result->fname = stpcpy (mempcpy (result->tmp_fname, fname, fname_len),
+ ".XXXXXX") + 1;
+ memcpy (result->fname, fname, fname_len + 1);
+
+ /* Create the temporary file. */
+ result->fd = mkstemp (result->tmp_fname);
+ if (result->fd == -1)
+ {
+ int save_errno = errno;
+ free (result);
+ __libasm_seterrno (ASM_E_CANNOT_CREATE);
+ errno = save_errno;
+ return NULL;
+ }
+
+ /* Initialize the counter for temporary symbols. */
+ result->tempsym_count = 0;
+
+ /* Now we differentiate between textual and binary output. */
+ result->textp = textp;
+ if (textp)
+ result = prepare_text_output (result);
+ else
+ result = prepare_binary_output (result, machine, klass, data);
+
+ return result;
+}
diff --git a/libasm/asm_end.c b/libasm/asm_end.c
new file mode 100644
index 00000000..74f01f08
--- /dev/null
+++ b/libasm/asm_end.c
@@ -0,0 +1,593 @@
+/* Finalize operations on the assembler context, free all resources.
+ Copyright (C) 2002, 2003, 2005 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+#include <error.h>
+#include <libintl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+#include <libasmP.h>
+#include <libelf.h>
+#include <system.h>
+
+
+static int
+text_end (AsmCtx_t *ctx __attribute__ ((unused)))
+{
+ // XXX Does anything have to be done?
+ return 0;
+}
+
+
+static int
+binary_end (AsmCtx_t *ctx)
+{
+ void *symtab = NULL;
+ struct Ebl_Strent *symscn_strent = NULL;
+ struct Ebl_Strent *strscn_strent = NULL;
+ struct Ebl_Strent *xndxscn_strent = NULL;
+ Elf_Scn *shstrscn;
+ struct Ebl_Strent *shstrscn_strent;
+ size_t shstrscnndx;
+ size_t symscnndx = 0;
+ size_t strscnndx = 0;
+ size_t xndxscnndx = 0;
+ Elf_Data *data;
+ Elf_Data *shstrtabdata;
+ Elf_Data *strtabdata = NULL;
+ Elf_Data *xndxdata = NULL;
+ GElf_Shdr shdr_mem;
+ GElf_Shdr *shdr;
+ GElf_Ehdr ehdr_mem;
+ GElf_Ehdr *ehdr;
+ AsmScn_t *asmscn;
+ int result = 0;
+
+ /* Iterate over the created sections and compute the offsets of the
+ various subsections and fill in the content. */
+ for (asmscn = ctx->section_list; asmscn != NULL; asmscn = asmscn->allnext)
+ {
+#if 0
+ Elf_Scn *scn = elf_getscn (ctx->out.elf, asmscn->data.main.scnndx);
+#else
+ Elf_Scn *scn = asmscn->data.main.scn;
+#endif
+ off_t offset = 0;
+ AsmScn_t *asmsubscn = asmscn;
+
+ do
+ {
+ struct AsmData *content = asmsubscn->content;
+ bool first = true;
+
+ offset = ((offset + asmsubscn->max_align - 1)
+ & ~(asmsubscn->max_align - 1));
+
+ /* Update the offset for this subsection. This field now
+ stores the offset of the first by in this subsection. */
+ asmsubscn->offset = offset;
+
+ /* Note that the content list is circular. */
+ if (content != NULL)
+ do
+ {
+ Elf_Data *newdata = elf_newdata (scn);
+
+ if (newdata == NULL)
+ {
+ __libasm_seterrno (ASM_E_LIBELF);
+ return -1;
+ }
+
+ newdata->d_buf = content->data;
+ newdata->d_type = ELF_T_BYTE;
+ newdata->d_size = content->len;
+ newdata->d_off = offset;
+ newdata->d_align = first ? asmsubscn->max_align : 1;
+
+ offset += content->len;
+ }
+ while ((content = content->next) != asmsubscn->content);
+ }
+ while ((asmsubscn = asmsubscn->subnext) != NULL);
+ }
+
+
+ /* Create the symbol table if necessary. */
+ if (ctx->nsymbol_tab > 0)
+ {
+ /* Create the symbol table and string table section names. */
+ symscn_strent = ebl_strtabadd (ctx->section_strtab, ".symtab", 8);
+ strscn_strent = ebl_strtabadd (ctx->section_strtab, ".strtab", 8);
+
+ /* Create the symbol string table section. */
+ Elf_Scn *strscn = elf_newscn (ctx->out.elf);
+ strtabdata = elf_newdata (strscn);
+ shdr = gelf_getshdr (strscn, &shdr_mem);
+ if (strtabdata == NULL || shdr == NULL)
+ {
+ __libasm_seterrno (ASM_E_LIBELF);
+ return -1;
+ }
+ strscnndx = elf_ndxscn (strscn);
+
+ ebl_strtabfinalize (ctx->symbol_strtab, strtabdata);
+
+ shdr->sh_type = SHT_STRTAB;
+ assert (shdr->sh_entsize == 0);
+
+ (void) gelf_update_shdr (strscn, shdr);
+
+ /* Create the symbol table section. */
+ Elf_Scn *symscn = elf_newscn (ctx->out.elf);
+ data = elf_newdata (symscn);
+ shdr = gelf_getshdr (symscn, &shdr_mem);
+ if (data == NULL || shdr == NULL)
+ {
+ __libasm_seterrno (ASM_E_LIBELF);
+ return -1;
+ }
+ symscnndx = elf_ndxscn (symscn);
+
+ /* We know how many symbols there will be in the symbol table. */
+ data->d_size = gelf_fsize (ctx->out.elf, ELF_T_SYM,
+ ctx->nsymbol_tab + 1, EV_CURRENT);
+ symtab = malloc (data->d_size);
+ if (symtab == NULL)
+ return -1;
+ data->d_buf = symtab;
+ data->d_type = ELF_T_SYM;
+ data->d_off = 0;
+
+ /* Clear the first entry. */
+ GElf_Sym syment;
+ memset (&syment, '\0', sizeof (syment));
+ (void) gelf_update_sym (data, 0, &syment);
+
+ /* Iterate over the symbol table. */
+ void *runp = NULL;
+ int ptr_local = 1; /* Start with index 1; zero remains unused. */
+ int ptr_nonlocal = ctx->nsymbol_tab;
+ uint32_t *xshndx = NULL;
+ AsmSym_t *sym;
+ while ((sym = asm_symbol_tab_iterate (&ctx->symbol_tab, &runp)) != NULL)
+ if (asm_emit_symbol_p (ebl_string (sym->strent)))
+ {
+ assert (ptr_local <= ptr_nonlocal);
+
+ syment.st_name = ebl_strtaboffset (sym->strent);
+ syment.st_info = GELF_ST_INFO (sym->binding, sym->type);
+ syment.st_other = 0;
+ syment.st_value = sym->scn->offset + sym->offset;
+ syment.st_size = sym->size;
+
+ /* Add local symbols at the beginning, the other from
+ the end. */
+ int ptr = sym->binding == STB_LOCAL ? ptr_local++ : ptr_nonlocal--;
+
+ /* Determine the section index. We have to handle the
+ overflow correctly. */
+ Elf_Scn *scn = (sym->scn->subsection_id == 0
+ ? sym->scn->data.main.scn
+ : sym->scn->data.up->data.main.scn);
+
+ Elf32_Word ndx;
+ if (unlikely (scn == ASM_ABS_SCN))
+ ndx = SHN_ABS;
+ else if (unlikely (scn == ASM_COM_SCN))
+ ndx = SHN_COMMON;
+ else if (unlikely ((ndx = elf_ndxscn (scn)) >= SHN_LORESERVE))
+ {
+ if (unlikely (xshndx == NULL))
+ {
+ /* The extended section index section does not yet
+ exist. */
+ Elf_Scn *xndxscn;
+
+ xndxscn = elf_newscn (ctx->out.elf);
+ xndxdata = elf_newdata (xndxscn);
+ shdr = gelf_getshdr (xndxscn, &shdr_mem);
+ if (xndxdata == NULL || shdr == NULL)
+ {
+ __libasm_seterrno (ASM_E_LIBELF);
+ return -1;
+ }
+ xndxscnndx = elf_ndxscn (xndxscn);
+
+ shdr->sh_type = SHT_SYMTAB_SHNDX;
+ shdr->sh_entsize = sizeof (Elf32_Word);
+ shdr->sh_addralign = sizeof (Elf32_Word);
+ shdr->sh_link = symscnndx;
+
+ (void) gelf_update_shdr (xndxscn, shdr);
+
+ xndxscn_strent = ebl_strtabadd (ctx->section_strtab,
+ ".symtab_shndx", 14);
+
+ /* Note that using 'elf32_fsize' instead of
+ 'gelf_fsize' here is correct. */
+ xndxdata->d_size = elf32_fsize (ELF_T_WORD,
+ ctx->nsymbol_tab + 1,
+ EV_CURRENT);
+ xshndx = xndxdata->d_buf = calloc (1, xndxdata->d_size);
+ if (xshndx == NULL)
+ return -1;
+ /* Using ELF_T_WORD here relies on the fact that the
+ 32- and 64-bit types are the same size. */
+ xndxdata->d_type = ELF_T_WORD;
+ xndxdata->d_off = 0;
+ }
+
+ /* Store the real section index in the extended setion
+ index table. */
+ assert ((size_t) ptr < ctx->nsymbol_tab + 1);
+ xshndx[ptr] = ndx;
+
+ /* And signal that this happened. */
+ ndx = SHN_XINDEX;
+ }
+ syment.st_shndx = ndx;
+
+ /* Remember where we put the symbol. */
+ sym->symidx = ptr;
+
+ (void) gelf_update_sym (data, ptr, &syment);
+ }
+
+ assert (ptr_local == ptr_nonlocal + 1);
+
+ shdr->sh_type = SHT_SYMTAB;
+ shdr->sh_link = strscnndx;
+ shdr->sh_info = ptr_local;
+ shdr->sh_entsize = gelf_fsize (ctx->out.elf, ELF_T_SYM, 1, EV_CURRENT);
+ shdr->sh_addralign = gelf_fsize (ctx->out.elf, ELF_T_ADDR, 1,
+ EV_CURRENT);
+
+ (void) gelf_update_shdr (symscn, shdr);
+ }
+
+
+ /* Create the section header string table section and fill in the
+ references in the section headers. */
+ shstrscn = elf_newscn (ctx->out.elf);
+ shstrtabdata = elf_newdata (shstrscn);
+ shdr = gelf_getshdr (shstrscn, &shdr_mem);
+ if (shstrscn == NULL || shstrtabdata == NULL || shdr == NULL)
+ {
+ __libasm_seterrno (ASM_E_LIBELF);
+ return -1;
+ }
+
+
+ /* Add the name of the section header string table. */
+ shstrscn_strent = ebl_strtabadd (ctx->section_strtab, ".shstrtab", 10);
+
+ ebl_strtabfinalize (ctx->section_strtab, shstrtabdata);
+
+ shdr->sh_type = SHT_STRTAB;
+ assert (shdr->sh_entsize == 0);
+ shdr->sh_name = ebl_strtaboffset (shstrscn_strent);
+
+ (void) gelf_update_shdr (shstrscn, shdr);
+
+
+ /* Create the section groups. */
+ if (ctx->groups != NULL)
+ {
+ AsmScnGrp_t *runp = ctx->groups->next;
+
+ do
+ {
+ Elf_Scn *scn;
+ Elf32_Word *grpdata;
+
+ scn = runp->scn;
+ assert (scn != NULL);
+ shdr = gelf_getshdr (scn, &shdr_mem);
+ assert (shdr != NULL);
+
+ data = elf_newdata (scn);
+ if (data == NULL)
+ {
+ __libasm_seterrno (ASM_E_LIBELF);
+ return -1;
+ }
+
+ /* It is correct to use 'elf32_fsize' instead of 'gelf_fsize'
+ here. */
+ data->d_size = elf32_fsize (ELF_T_WORD, runp->nmembers + 1,
+ EV_CURRENT);
+ grpdata = data->d_buf = malloc (data->d_size);
+ if (grpdata == NULL)
+ return -1;
+ data->d_type = ELF_T_WORD;
+ data->d_off = 0;
+ data->d_align = elf32_fsize (ELF_T_WORD, 1, EV_CURRENT);
+
+ /* The first word of the section is filled with the flag word. */
+ *grpdata++ = runp->flags;
+
+ if (runp->members != NULL)
+ {
+ AsmScn_t *member = runp->members->data.main.next_in_group;
+
+ do
+ {
+ /* Only sections, not subsections, can be registered
+ as member of a group. The subsections get
+ automatically included. */
+ assert (member->subsection_id == 0);
+
+ *grpdata++ = elf_ndxscn (member->data.main.scn);
+ }
+ while ((member = member->data.main.next_in_group)
+ != runp->members->data.main.next_in_group);
+ }
+
+ /* Construct the section header. */
+ shdr->sh_name = ebl_strtaboffset (runp->strent);
+ shdr->sh_type = SHT_GROUP;
+ shdr->sh_flags = 0;
+ shdr->sh_link = symscnndx;
+ /* If the user did not specify a signature we use the initial
+ empty symbol in the symbol table as the signature. */
+ shdr->sh_info = (runp->signature != NULL
+ ? runp->signature->symidx : 0);
+
+ (void) gelf_update_shdr (scn, shdr);
+ }
+ while ((runp = runp->next) != ctx->groups->next);
+ }
+
+
+ /* Add the name to the symbol section. */
+ if (likely (symscnndx != 0))
+ {
+ Elf_Scn *scn = elf_getscn (ctx->out.elf, symscnndx);
+
+ shdr = gelf_getshdr (scn, &shdr_mem);
+
+ shdr->sh_name = ebl_strtaboffset (symscn_strent);
+
+ (void) gelf_update_shdr (scn, shdr);
+
+
+ /* Add the name to the string section. */
+ assert (strscnndx != 0);
+ scn = elf_getscn (ctx->out.elf, strscnndx);
+
+ shdr = gelf_getshdr (scn, &shdr_mem);
+
+ shdr->sh_name = ebl_strtaboffset (strscn_strent);
+
+ (void) gelf_update_shdr (scn, shdr);
+
+
+ /* Add the name to the extended symbol index section. */
+ if (xndxscnndx != 0)
+ {
+ scn = elf_getscn (ctx->out.elf, xndxscnndx);
+
+ shdr = gelf_getshdr (scn, &shdr_mem);
+
+ shdr->sh_name = ebl_strtaboffset (xndxscn_strent);
+
+ (void) gelf_update_shdr (scn, shdr);
+ }
+ }
+
+
+ /* Iterate over the created sections and fill in the names. */
+ for (asmscn = ctx->section_list; asmscn != NULL; asmscn = asmscn->allnext)
+ {
+ shdr = gelf_getshdr (asmscn->data.main.scn, &shdr_mem);
+ /* This better should not fail. */
+ assert (shdr != NULL);
+
+ shdr->sh_name = ebl_strtaboffset (asmscn->data.main.strent);
+
+ /* We now know the maximum alignment. */
+ shdr->sh_addralign = asmscn->max_align;
+
+ (void) gelf_update_shdr (asmscn->data.main.scn, shdr);
+ }
+
+ /* Put the reference to the section header string table in the ELF
+ header. */
+ ehdr = gelf_getehdr (ctx->out.elf, &ehdr_mem);
+ assert (ehdr != NULL);
+
+ shstrscnndx = elf_ndxscn (shstrscn);
+ if (unlikely (shstrscnndx > SHN_HIRESERVE)
+ || unlikely (shstrscnndx == SHN_XINDEX))
+ {
+ /* The index of the section header string sectio is too large. */
+ Elf_Scn *scn = elf_getscn (ctx->out.elf, 0);
+
+ /* Get the header for the zeroth section. */
+ shdr = gelf_getshdr (scn, &shdr_mem);
+ /* This better does not fail. */
+ assert (shdr != NULL);
+
+ /* The sh_link field of the zeroth section header contains the value. */
+ shdr->sh_link = shstrscnndx;
+
+ (void) gelf_update_shdr (scn, shdr);
+
+ /* This is the sign for the overflow. */
+ ehdr->e_shstrndx = SHN_XINDEX;
+ }
+ else
+ ehdr->e_shstrndx = elf_ndxscn (shstrscn);
+
+ gelf_update_ehdr (ctx->out.elf, ehdr);
+
+ /* Write out the ELF file. */
+ if (unlikely (elf_update (ctx->out.elf, ELF_C_WRITE_MMAP)) < 0)
+ {
+ __libasm_seterrno (ASM_E_LIBELF);
+ result = -1;
+ }
+
+ /* We do not need the section header and symbol string tables anymore. */
+ free (shstrtabdata->d_buf);
+ if (strtabdata != NULL)
+ free (strtabdata->d_buf);
+ /* We might have allocated the extended symbol table index. */
+ if (xndxdata != NULL)
+ free (xndxdata->d_buf);
+
+ /* Free section groups memory. */
+ AsmScnGrp_t *scngrp = ctx->groups;
+ if (scngrp != NULL)
+ do
+ free (elf_getdata (scngrp->scn, NULL)->d_buf);
+ while ((scngrp = scngrp->next) != ctx->groups);
+
+ /* Finalize the ELF handling. */
+ if (unlikely (elf_end (ctx->out.elf)) != 0)
+ {
+ __libasm_seterrno (ASM_E_LIBELF);
+ result = -1;
+ }
+
+ /* Free the temporary resources. */
+ free (symtab);
+
+ return result;
+}
+
+
+int
+asm_end (ctx)
+ AsmCtx_t *ctx;
+{
+ int result;
+
+ if (ctx == NULL)
+ /* Something went wrong earlier. */
+ return -1;
+
+ result = unlikely (ctx->textp) ? text_end (ctx) : binary_end (ctx);
+ if (result != 0)
+ return result;
+
+ /* Make the new file globally readable and user/group-writable. */
+ if (fchmod (ctx->fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH) != 0)
+ {
+ __libasm_seterrno (ASM_E_CANNOT_CHMOD);
+ return -1;
+ }
+
+ /* Rename output file. */
+ if (rename (ctx->tmp_fname, ctx->fname) != 0)
+ {
+ __libasm_seterrno (ASM_E_CANNOT_RENAME);
+ return -1;
+ }
+
+ /* Free the resources. */
+ __libasm_finictx (ctx);
+
+ return 0;
+}
+
+
+static void
+free_section (AsmScn_t *scnp)
+{
+ void *oldp;
+
+ if (scnp->subnext != NULL)
+ free_section (scnp->subnext);
+
+ struct AsmData *data = scnp->content;
+ if (data != NULL)
+ do
+ {
+ oldp = data;
+ data = data->next;
+ free (oldp);
+ }
+ while (oldp != scnp->content);
+
+ free (scnp);
+}
+
+
+void
+__libasm_finictx (ctx)
+ AsmCtx_t *ctx;
+{
+ /* Iterate through section table and free individual entries. */
+ AsmScn_t *scn = ctx->section_list;
+ while (scn != NULL)
+ {
+ AsmScn_t *oldp = scn;
+ scn = scn->allnext;
+ free_section (oldp);
+ }
+
+ /* Free the resources of the symbol table. */
+ void *runp = NULL;
+ AsmSym_t *sym;
+ while ((sym = asm_symbol_tab_iterate (&ctx->symbol_tab, &runp)) != NULL)
+ free (sym);
+ asm_symbol_tab_free (&ctx->symbol_tab);
+
+
+ /* Free section groups. */
+ AsmScnGrp_t *scngrp = ctx->groups;
+ if (scngrp != NULL)
+ do
+ {
+ AsmScnGrp_t *oldp = scngrp;
+
+ scngrp = scngrp->next;
+ free (oldp);
+ }
+ while (scngrp != ctx->groups);
+
+
+ if (unlikely (ctx->textp))
+ {
+ /* Close the stream. */
+ fclose (ctx->out.file);
+ }
+ else
+ {
+ /* Close the output file. */
+ /* XXX We should test for errors here but what would we do if we'd
+ find any. */
+ (void) close (ctx->fd);
+
+ /* And the string tables. */
+ ebl_strtabfree (ctx->section_strtab);
+ ebl_strtabfree (ctx->symbol_strtab);
+ }
+
+ /* Initialize the lock. */
+ rwlock_fini (ctx->lock);
+
+ /* Finally free the data structure. */
+ free (ctx);
+}
diff --git a/libasm/asm_error.c b/libasm/asm_error.c
new file mode 100644
index 00000000..9d2d81ed
--- /dev/null
+++ b/libasm/asm_error.c
@@ -0,0 +1,183 @@
+/* Error handling in libasm.
+ Copyright (C) 2002, 2004 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <libintl.h>
+#include <stdbool.h>
+#include <stdlib.h>
+
+#include "libasmP.h"
+
+
+/* This is the key for the thread specific memory. */
+static tls_key_t key;
+
+/* The error number. Used in non-threaded programs. */
+static int global_error;
+static bool threaded;
+/* We need to initialize the thread-specific data. */
+once_define (static, once);
+
+/* The initialization and destruction functions. */
+static void init (void);
+static void free_key_mem (void *mem);
+
+
+int
+asm_errno (void)
+{
+ int result;
+
+ /* If we have not yet initialized the buffer do it now. */
+ once_execute (once, init);
+
+ if (threaded)
+ {
+ /* We have a key. Use it to get the thread-specific buffer. */
+ int *buffer = getspecific (key);
+ if (buffer == NULL)
+ {
+ /* No buffer allocated so far. */
+ buffer = (int *) malloc (sizeof (int));
+ if (buffer == NULL)
+ /* No more memory available. We use the static buffer. */
+ buffer = &global_error;
+
+ setspecific (key, buffer);
+
+ *buffer = 0;
+ }
+
+ result = *buffer;
+ *buffer = ASM_E_NOERROR;
+ return result;
+ }
+
+ result = global_error;
+ global_error = ASM_E_NOERROR;
+ return result;
+}
+
+
+void
+__libasm_seterrno (value)
+ int value;
+{
+ /* If we have not yet initialized the buffer do it now. */
+ once_execute (once, init);
+
+ if (threaded)
+ {
+ /* We have a key. Use it to get the thread-specific buffer. */
+ int *buffer = getspecific (key);
+ if (buffer == NULL)
+ {
+ /* No buffer allocated so far. */
+ buffer = malloc (sizeof (int));
+ if (buffer == NULL)
+ /* No more memory available. We use the static buffer. */
+ buffer = &global_error;
+
+ setspecific (key, buffer);
+ }
+
+ *buffer = value;
+ }
+
+ global_error = value;
+}
+
+
+/* Return the appropriate message for the error. */
+static const char *msgs[ASM_E_NUM] =
+{
+ [ASM_E_NOERROR] = N_("no error"),
+ [ASM_E_NOMEM] = N_("out of memory"),
+ [ASM_E_CANNOT_CREATE] = N_("cannot create output file"),
+ [ASM_E_INVALID] = N_("invalid parameter"),
+ [ASM_E_CANNOT_CHMOD] = N_("cannot change mode of output file"),
+ [ASM_E_CANNOT_RENAME] = N_("cannot rename output file"),
+ [ASM_E_DUPLSYM] = N_("duplicate symbol"),
+ [ASM_E_TYPE] = N_("invalid section type for operation")
+};
+
+const char *
+asm_errmsg (error)
+ int error;
+{
+ int last_error;
+
+ /* If we have not yet initialized the buffer do it now. */
+ once_execute (once, init);
+
+ if ((error == 0 || error == -1) && threaded)
+ {
+ /* We have a key. Use it to get the thread-specific buffer. */
+ int *buffer = (int *) getspecific (key);
+ if (buffer == NULL)
+ {
+ /* No buffer allocated so far. */
+ buffer = (int *) malloc (sizeof (int));
+ if (buffer == NULL)
+ /* No more memory available. We use the static buffer. */
+ buffer = &global_error;
+
+ setspecific (key, buffer);
+ *buffer = 0;
+ }
+
+ last_error = *buffer;
+ }
+ else
+ last_error = global_error;
+
+ if (error < -1)
+ return _("Unknown error");
+ if (error == 0 && last_error == 0)
+ /* No error. */
+ return NULL;
+
+ if (error != -1)
+ last_error = error;
+
+ if (last_error == ASM_E_LIBELF)
+ return elf_errmsg (-1);
+
+ return _(msgs[last_error]);
+}
+
+
+/* Free the thread specific data, this is done if a thread terminates. */
+static void
+free_key_mem (void *mem)
+{
+ free (mem);
+ setspecific (key, NULL);
+}
+
+
+/* Initialize the key for the global variable. */
+static void
+init (void)
+{
+ // XXX Screw you, gcc4, the unused function attribute does not work.
+ __asm ("" :: "r" (free_key_mem));
+
+ if (key_create (&key, free_key_mem) == 0)
+ /* Creating the key succeeded. */
+ threaded = true;
+}
diff --git a/libasm/asm_fill.c b/libasm/asm_fill.c
new file mode 100644
index 00000000..67b64417
--- /dev/null
+++ b/libasm/asm_fill.c
@@ -0,0 +1,62 @@
+/* Determine fill pattern for a section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <libasmP.h>
+#include <system.h>
+
+
+int
+asm_fill (asmscn, bytes, len)
+ AsmScn_t *asmscn;
+ void *bytes;
+ size_t len;
+{
+ struct FillPattern *pattern;
+ struct FillPattern *old_pattern;
+
+ if (asmscn == NULL)
+ /* Some earlier error. */
+ return -1;
+
+ if (bytes == NULL)
+ /* Use the default pattern. */
+ pattern = (struct FillPattern *) __libasm_default_pattern;
+ else
+ {
+ /* Allocate appropriate memory. */
+ pattern = (struct FillPattern *) malloc (sizeof (struct FillPattern)
+ + len);
+ if (pattern == NULL)
+ return -1;
+
+ pattern->len = len;
+ memcpy (pattern->bytes, bytes, len);
+ }
+
+ old_pattern = asmscn->pattern;
+ asmscn->pattern = pattern;
+
+ /* Free the old data structure if we have allocated it. */
+ if (old_pattern != __libasm_default_pattern)
+ free (old_pattern);
+
+ return 0;
+}
diff --git a/libasm/asm_getelf.c b/libasm/asm_getelf.c
new file mode 100644
index 00000000..3bbb0d90
--- /dev/null
+++ b/libasm/asm_getelf.c
@@ -0,0 +1,29 @@
+/* Return ELF descriptor associated with the assembler context.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stddef.h>
+
+#include <libasmP.h>
+
+
+Elf *
+asm_getelf (ctx)
+ AsmCtx_t *ctx;
+{
+ return ctx != NULL ? ctx->out.elf : NULL;
+}
diff --git a/libasm/asm_newabssym.c b/libasm/asm_newabssym.c
new file mode 100644
index 00000000..fba46f0f
--- /dev/null
+++ b/libasm/asm_newabssym.c
@@ -0,0 +1,121 @@
+/* Create new ABS symbol.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libasmP.h>
+#include <system.h>
+
+
+/* Object for special COMMON section. */
+static const AsmScn_t __libasm_abs_scn =
+ {
+ .data = {
+ .main = {
+ .scn = ASM_ABS_SCN
+ }
+ }
+ };
+
+
+AsmSym_t *
+asm_newabssym (ctx, name, size, value, type, binding)
+ AsmCtx_t *ctx;
+ const char *name;
+ GElf_Xword size;
+ GElf_Addr value;
+ int type;
+ int binding;
+{
+ AsmSym_t *result;
+
+ if (ctx == NULL)
+ /* Something went wrong before. */
+ return NULL;
+
+ /* Common symbols are public. Therefore the user must provide a
+ name. */
+ if (name == NULL)
+ {
+ __libasm_seterrno (ASM_E_INVALID);
+ return NULL;
+ }
+
+ rwlock_wrlock (ctx->lock);
+
+ result = (AsmSym_t *) malloc (sizeof (AsmSym_t));
+ if (result == NULL)
+ return NULL;
+
+ result->scn = (AsmScn_t *) &__libasm_abs_scn;
+ result->size = size;
+ result->type = type;
+ result->binding = binding;
+ result->symidx = 0;
+ result->strent = ebl_strtabadd (ctx->symbol_strtab, name, 0);
+
+ /* The value of an ABS symbol must not be modified. Since there are
+ no subsection and the initial offset of the section is 0 we can
+ get the alignment recorded by storing it into the offset
+ field. */
+ result->offset = value;
+
+ if (unlikely (ctx->textp))
+ {
+ /* An absolute symbol can be defined by giving a symbol a
+ specific value. */
+ if (binding == STB_GLOBAL)
+ fprintf (ctx->out.file, "\t.globl %s\n", name);
+ else if (binding == STB_WEAK)
+ fprintf (ctx->out.file, "\t.weak %s\n", name);
+
+ if (type == STT_OBJECT)
+ fprintf (ctx->out.file, "\t.type %s,@object\n", name);
+ else if (type == STT_FUNC)
+ fprintf (ctx->out.file, "\t.type %s,@function\n", name);
+
+ fprintf (ctx->out.file, "%s = %llu\n",
+ name, (unsigned long long int) value);
+
+ if (size != 0)
+ fprintf (ctx->out.file, "\t.size %s, %llu\n",
+ name, (unsigned long long int) size);
+ }
+ else
+ {
+ /* Put the symbol in the hash table so that we can later find it. */
+ if (asm_symbol_tab_insert (&ctx->symbol_tab, elf_hash (name), result)
+ != 0)
+ {
+ /* The symbol already exists. */
+ __libasm_seterrno (ASM_E_DUPLSYM);
+ free (result);
+ result = NULL;
+ }
+ else if (name != NULL && asm_emit_symbol_p (name))
+ /* Only count non-private symbols. */
+ ++ctx->nsymbol_tab;
+ }
+
+ rwlock_unlock (ctx->lock);
+
+ return result;
+}
diff --git a/libasm/asm_newcomsym.c b/libasm/asm_newcomsym.c
new file mode 100644
index 00000000..61e5a909
--- /dev/null
+++ b/libasm/asm_newcomsym.c
@@ -0,0 +1,102 @@
+/* Create new COMMON symbol.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libasmP.h>
+#include <system.h>
+
+
+/* Object for special COMMON section. */
+static const AsmScn_t __libasm_com_scn =
+ {
+ .data = {
+ .main = {
+ .scn = ASM_COM_SCN
+ }
+ }
+ };
+
+
+AsmSym_t *
+asm_newcomsym (ctx, name, size, align)
+ AsmCtx_t *ctx;
+ const char *name;
+ GElf_Xword size;
+ GElf_Addr align;
+{
+ AsmSym_t *result;
+
+ if (ctx == NULL)
+ /* Something went wrong before. */
+ return NULL;
+
+ /* Common symbols are public. Therefore the user must provide a
+ name. */
+ if (name == NULL)
+ {
+ __libasm_seterrno (ASM_E_INVALID);
+ return NULL;
+ }
+
+ rwlock_wrlock (ctx->lock);
+
+ result = (AsmSym_t *) malloc (sizeof (AsmSym_t));
+ if (result == NULL)
+ return NULL;
+
+ result->scn = (AsmScn_t *) &__libasm_com_scn;
+ result->size = size;
+ /* XXX Do we have to allow a different type? */
+ result->type = STT_OBJECT;
+ /* XXX Do we have to allow a different binding? */
+ result->binding = STB_GLOBAL;
+ result->symidx = 0;
+ result->strent = ebl_strtabadd (ctx->symbol_strtab, name, 0);
+
+ /* The value of a COM symbol is the alignment. Since there are no
+ subsection and the initial offset of the section is 0 we can get
+ the alignment recorded by storing it into the offset field. */
+ result->offset = align;
+
+ if (unlikely (ctx->textp))
+ fprintf (ctx->out.file, "\t.comm %s, %" PRIuMAX ", %" PRIuMAX "\n",
+ name, (uintmax_t) size, (uintmax_t) align);
+ else
+ {
+ /* Put the symbol in the hash table so that we can later find it. */
+ if (asm_symbol_tab_insert (&ctx->symbol_tab, elf_hash (name), result)
+ != 0)
+ {
+ /* The symbol already exists. */
+ __libasm_seterrno (ASM_E_DUPLSYM);
+ free (result);
+ result = NULL;
+ }
+ else if (name != NULL && asm_emit_symbol_p (name))
+ /* Only count non-private symbols. */
+ ++ctx->nsymbol_tab;
+ }
+
+ rwlock_unlock (ctx->lock);
+
+ return result;
+}
diff --git a/libasm/asm_newscn.c b/libasm/asm_newscn.c
new file mode 100644
index 00000000..75890a60
--- /dev/null
+++ b/libasm/asm_newscn.c
@@ -0,0 +1,202 @@
+/* Create new section in output file.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+#include <error.h>
+#include <libintl.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libasmP.h>
+#include <libelf.h>
+#include <system.h>
+
+
+/* Memory for the default pattern. The type uses a flexible array
+ which does work well with a static initializer. So we play some
+ dirty tricks here. */
+static const struct
+{
+ struct FillPattern pattern;
+ char zero;
+} xdefault_pattern =
+ {
+ .pattern =
+ {
+ .len = 1
+ },
+ .zero = '\0'
+ };
+const struct FillPattern *__libasm_default_pattern = &xdefault_pattern.pattern;
+
+
+static AsmScn_t *
+text_newscn (AsmScn_t *result, GElf_Word type, GElf_Xword flags)
+{
+ /* Buffer where we construct the flag string. */
+ char flagstr[sizeof (GElf_Xword) * 8 + 5];
+ char *wp = flagstr;
+ const char *typestr = "";
+
+ /* Only write out the flag string if this is the first time the
+ section is selected. Some assemblers cannot cope with the
+ .section pseudo-op otherwise. */
+ wp = stpcpy (wp, ", \"");
+
+ if (flags & SHF_WRITE)
+ *wp++ = 'w';
+ if (flags & SHF_ALLOC)
+ *wp++ = 'a';
+ if (flags & SHF_EXECINSTR)
+ *wp++ = 'x';
+ if (flags & SHF_MERGE)
+ *wp++ = 'M';
+ if (flags & SHF_STRINGS)
+ *wp++ = 'S';
+ if (flags & SHF_LINK_ORDER)
+ *wp++ = 'L';
+
+ *wp++ = '"';
+
+ if (type == SHT_PROGBITS)
+ typestr = ",@progbits";
+ else if (type == SHT_NOBITS)
+ typestr = ",@nobits";
+
+ /* Terminate the string. */
+ *wp = '\0';
+
+ printf ("\t.section \"%s\"%s%s\n", result->name, flagstr, typestr);
+
+ return result;
+}
+
+
+static AsmScn_t *
+binary_newscn (AsmScn_t *result, GElf_Word type, GElf_Xword flags,
+ size_t scnname_len)
+{
+ GElf_Shdr shdr_mem;
+ GElf_Shdr *shdr;
+ Elf_Scn *scn;
+
+ /* The initial subsection has the number zero. */
+ result->subsection_id = 0;
+
+ /* We start at offset zero. */
+ result->offset = 0;
+ /* And generic alignment. */
+ result->max_align = 1;
+
+ /* No output yet. */
+ result->content = NULL;
+
+ /* Put the default fill pattern in place. */
+ result->pattern = (struct FillPattern *) __libasm_default_pattern;
+
+ /* There are no subsections so far. */
+ result->subnext = NULL;
+
+ /* Add the name to the section header string table. */
+ result->data.main.strent = ebl_strtabadd (result->ctx->section_strtab,
+ result->name, scnname_len);
+ assert (result->data.main.strent != NULL);
+
+ /* Create the new ELF section. */
+ result->data.main.scn = scn = elf_newscn (result->ctx->out.elf);
+ if (scn == NULL)
+ {
+ free (result);
+ __libasm_seterrno (ASM_E_LIBELF);
+ return NULL;
+ }
+
+ /* Not part of a section group (yet). */
+ result->data.main.next_in_group = NULL;
+
+ /* Remember the flags. */
+ shdr = gelf_getshdr (scn, &shdr_mem);
+
+ shdr->sh_flags = flags;
+ result->type = shdr->sh_type = type;
+
+ (void) gelf_update_shdr (scn, shdr);
+
+ return result;
+}
+
+
+AsmScn_t *
+asm_newscn (ctx, scnname, type, flags)
+ AsmCtx_t *ctx;
+ const char *scnname;
+ GElf_Word type;
+ GElf_Xword flags;
+{
+ size_t scnname_len = strlen (scnname) + 1;
+ unsigned long int hval;
+ AsmScn_t *result;
+
+ /* If no context is given there might be an earlier error. */
+ if (ctx == NULL)
+ return NULL;
+
+ /* Check whether only flags are set which areselectable by the user. */
+ if (unlikely ((flags & ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR | SHF_MERGE
+ | SHF_STRINGS | SHF_LINK_ORDER)) != 0)
+ /* We allow only two section types: data and data without file
+ representation. */
+ || (type != SHT_PROGBITS && unlikely (type != SHT_NOBITS)))
+ {
+ __libasm_seterrno (ASM_E_INVALID);
+ return NULL;
+ }
+
+ hval = elf_hash (scnname);
+
+ rwlock_wrlock (ctx->lock);
+
+ /* This is a new section. */
+ result = (AsmScn_t *) malloc (sizeof (AsmScn_t) + scnname_len);
+ if (result != NULL)
+ {
+ /* Add the name. */
+ memcpy (result->name, scnname, scnname_len);
+
+ /* Add the reference to the context. */
+ result->ctx = ctx;
+
+ /* Perform operations according to output mode. */
+ result = (unlikely (ctx->textp)
+ ? text_newscn (result, type, flags)
+ : binary_newscn (result, type, flags, scnname_len));
+
+ /* If everything went well finally add the new section to the hash
+ table. */
+ if (result != NULL)
+ {
+ result->allnext = ctx->section_list;
+ ctx->section_list = result;
+ }
+ }
+
+ rwlock_unlock (ctx->lock);
+
+ return result;
+}
+INTDEF(asm_newscn)
diff --git a/libasm/asm_newscn_ingrp.c b/libasm/asm_newscn_ingrp.c
new file mode 100644
index 00000000..bfbdc6a0
--- /dev/null
+++ b/libasm/asm_newscn_ingrp.c
@@ -0,0 +1,66 @@
+/* Create new section, which is member of a group, in output file.
+ Copyright (C) 2002, 2005 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+
+#include "libasmP.h"
+
+
+AsmScn_t *
+asm_newscn_ingrp (ctx, scnname, type, flags, grp)
+ AsmCtx_t *ctx;
+ const char *scnname;
+ GElf_Word type;
+ GElf_Xword flags;
+ AsmScnGrp_t *grp;
+{
+ AsmScn_t *result = INTUSE (asm_newscn) (ctx, scnname, type, flags);
+
+ if (likely (result != NULL))
+ {
+ /* We managed to create a section group. Add it to the section
+ group. */
+ if (grp->nmembers == 0)
+ {
+ assert (grp->members == NULL);
+ grp->members = result->data.main.next_in_group = result;
+ }
+ else
+ {
+ result->data.main.next_in_group
+ = grp->members->data.main.next_in_group;
+ grp->members = grp->members->data.main.next_in_group = result;
+ }
+
+ ++grp->nmembers;
+
+ /* Set the SHF_GROUP flag. */
+ if (likely (! ctx->textp))
+ {
+ GElf_Shdr shdr_mem;
+ GElf_Shdr *shdr = gelf_getshdr (result->data.main.scn, &shdr_mem);
+
+ assert (shdr != NULL);
+ shdr->sh_flags |= SHF_GROUP;
+
+ (void) gelf_update_shdr (result->data.main.scn, shdr);
+ }
+ }
+
+ return result;
+}
diff --git a/libasm/asm_newscngrp.c b/libasm/asm_newscngrp.c
new file mode 100644
index 00000000..c1c8be3e
--- /dev/null
+++ b/libasm/asm_newscngrp.c
@@ -0,0 +1,90 @@
+/* Create new section group.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libasmP.h"
+#include <system.h>
+
+
+
+AsmScnGrp_t *
+asm_newscngrp (ctx, grpname, signature, flags)
+ AsmCtx_t *ctx;
+ const char *grpname;
+ AsmSym_t *signature;
+ Elf32_Word flags;
+{
+ AsmScnGrp_t *result;
+ size_t grpname_len = strlen (grpname) + 1;
+
+ if (ctx == NULL)
+ return NULL;
+
+ if ((flags & ~GRP_COMDAT) != 0)
+ {
+ /* This is not a supported flag. */
+ __libasm_seterrno (ASM_E_INVALID);
+ return NULL;
+ }
+
+ result = (AsmScnGrp_t *) malloc (sizeof (AsmScnGrp_t) + grpname_len);
+ if (result == NULL)
+ return NULL;
+
+ result->signature = signature;
+ result->members = NULL;
+ result->nmembers = 0;
+ result->flags = flags;
+
+ memcpy (result->name, grpname, grpname_len);
+ result->strent = ebl_strtabadd (ctx->section_strtab, result->name,
+ grpname_len);
+
+ if (unlikely (ctx->textp))
+ // XXX TBI. What is the format?
+ abort ();
+ else
+ {
+ result->scn = elf_newscn (ctx->out.elf);
+ if (result->scn == NULL)
+ {
+ /* Couldn't allocate a new section. */
+ __libasm_seterrno (ASM_E_LIBELF);
+ free (result);
+ return NULL;
+ }
+ }
+
+ /* Enqueue is the context data structure. */
+ if (ctx->ngroups == 0)
+ {
+ assert (ctx->groups == NULL);
+ ctx->groups = result->next = result;
+ }
+ else
+ {
+ result->next = ctx->groups->next;
+ ctx->groups = ctx->groups->next = result;
+ }
+ ++ctx->ngroups;
+
+ return result;
+}
diff --git a/libasm/asm_newsubscn.c b/libasm/asm_newsubscn.c
new file mode 100644
index 00000000..e767720e
--- /dev/null
+++ b/libasm/asm_newsubscn.c
@@ -0,0 +1,84 @@
+/* Create new subsection section in given section.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdlib.h>
+
+#include <libasmP.h>
+#include <system.h>
+
+
+AsmScn_t *
+asm_newsubscn (asmscn, nr)
+ AsmScn_t *asmscn;
+ unsigned int nr;
+{
+ AsmScn_t *runp;
+ AsmScn_t *newp;
+
+ /* Just return if no section is given. The error must have been
+ somewhere else. */
+ if (asmscn == NULL)
+ return NULL;
+
+ /* Determine whether there is already a subsection with this number. */
+ runp = asmscn->subsection_id == 0 ? asmscn : asmscn->data.up;
+ while (1)
+ {
+ if (runp->subsection_id == nr)
+ /* Found it. */
+ return runp;
+
+ if (runp->subnext == NULL || runp->subnext->subsection_id > nr)
+ break;
+
+ runp = runp->subnext;
+ }
+
+ newp = (AsmScn_t *) malloc (sizeof (AsmScn_t));
+ if (newp == NULL)
+ return NULL;
+
+ /* Same assembler context than the original section. */
+ newp->ctx = runp->ctx;
+
+ /* User provided the subsectio nID. */
+ newp->subsection_id = nr;
+
+ /* Inherit the parent's type. */
+ newp->type = runp->type;
+
+ /* Pointer to the zeroth subsection. */
+ newp->data.up = runp->subsection_id == 0 ? runp : runp->data.up;
+
+ /* We start at offset zero. */
+ newp->offset = 0;
+ /* And generic alignment. */
+ newp->max_align = 1;
+
+ /* No output yet. */
+ newp->content = NULL;
+
+ /* Inherit the fill pattern from the section this one is derived from. */
+ newp->pattern = asmscn->pattern;
+
+ /* Enqueue at the right position in the list. */
+ newp->subnext = runp->subnext;
+ runp->subnext = newp;
+
+ return newp;
+}
diff --git a/libasm/asm_newsym.c b/libasm/asm_newsym.c
new file mode 100644
index 00000000..70ad9e5d
--- /dev/null
+++ b/libasm/asm_newsym.c
@@ -0,0 +1,123 @@
+/* Define new symbol for current position in given section.
+ Copyright (C) 2002, 2005 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <libasmP.h>
+#include <system.h>
+
+
+AsmSym_t *
+asm_newsym (asmscn, name, size, type, binding)
+ AsmScn_t *asmscn;
+ const char *name;
+ GElf_Xword size;
+ int type;
+ int binding;
+{
+#define TEMPSYMLEN 10
+ char tempsym[TEMPSYMLEN];
+ AsmSym_t *result;
+
+ if (asmscn == NULL)
+ /* Something went wrong before. */
+ return NULL;
+
+ /* Generate a temporary symbol if necessary. */
+ if (name == NULL)
+ {
+ /* If a local symbol name is created the symbol better have
+ local binding. */
+ if (binding != STB_LOCAL)
+ {
+ __libasm_seterrno (ASM_E_INVALID);
+ return NULL;
+ }
+
+ // XXX This requires getting the format from the machine backend. */
+ snprintf (tempsym, TEMPSYMLEN, ".L%07u", asmscn->ctx->tempsym_count++);
+
+ name = tempsym;
+ }
+
+ size_t name_len = strlen (name) + 1;
+
+ result = (AsmSym_t *) malloc (sizeof (AsmSym_t) + name_len);
+ if (result == NULL)
+ return NULL;
+
+ rwlock_wrlock (asmscn->ctx->lock);
+
+ result->scn = asmscn;
+ result->offset = asmscn->offset;
+ result->size = size;
+ result->type = type;
+ result->binding = binding;
+ result->symidx = 0;
+ result->strent = ebl_strtabadd (asmscn->ctx->symbol_strtab,
+ memcpy (result + 1, name, name_len), 0);
+
+ if (unlikely (asmscn->ctx->textp))
+ {
+ /* We are only interested in the name and don't need to know whether
+ it is a local name or not. */
+ /* First print the binding pseudo-op. */
+ if (binding == STB_GLOBAL)
+ fprintf (asmscn->ctx->out.file, "\t.globl\t%s\n", name);
+ else if (binding == STB_WEAK)
+ fprintf (asmscn->ctx->out.file, "\t.weak\t%s\n", name);
+
+ /* Next the symbol type. */
+ if (type == STT_OBJECT)
+ fprintf (asmscn->ctx->out.file, "\t.type\t%s,@object\n", name);
+ else if (type == STT_FUNC)
+ fprintf (asmscn->ctx->out.file, "\t.type\t%s,@function\n", name);
+
+ /* Finally the size and the label. */
+ fprintf (asmscn->ctx->out.file, "\t.size\t%s,%" PRIuMAX "\n%s:\n",
+ name, (uintmax_t) size, name);
+ }
+ else
+ {
+ /* Put the symbol in the hash table so that we can later find it. */
+ if (asm_symbol_tab_insert (&asmscn->ctx->symbol_tab, elf_hash (name),
+ result) != 0)
+ {
+ /* The symbol already exists. */
+ __libasm_seterrno (ASM_E_DUPLSYM);
+ /* Note that we can free the entry since there must be no
+ reference in the string table to the string. We can only
+ fail to insert the symbol into the symbol table if there
+ is already a symbol with this name. In this case the
+ ebl_strtabadd function would use the previously provided
+ name. */
+ free (result);
+ result = NULL;
+ }
+ else if (name != tempsym && asm_emit_symbol_p (name))
+ /* Only count non-private symbols. */
+ ++asmscn->ctx->nsymbol_tab;
+ }
+
+ rwlock_unlock (asmscn->ctx->lock);
+
+ return result;
+}
diff --git a/libasm/asm_scngrp_newsignature.c b/libasm/asm_scngrp_newsignature.c
new file mode 100644
index 00000000..d4042a24
--- /dev/null
+++ b/libasm/asm_scngrp_newsignature.c
@@ -0,0 +1,33 @@
+/* Update signature of section group.
+ Copyright (C) 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2002.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include "libasmP.h"
+
+
+int
+asm_scngrp_newsignature (grp, signature)
+ AsmScnGrp_t *grp;
+ AsmSym_t *signature;
+{
+ if (grp == NULL || signature == NULL)
+ return 1;
+
+ grp->signature = signature;
+
+ return 0;
+}
diff --git a/libasm/libasm.h b/libasm/libasm.h
new file mode 100644
index 00000000..dbdd6fc2
--- /dev/null
+++ b/libasm/libasm.h
@@ -0,0 +1,155 @@
+/* Interface for libasm.
+ Copyright (C) 2002 Red Hat, Inc.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifndef _LIBASM_H
+#define _LIBASM_H 1
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include <gelf.h>
+
+
+/* Opaque type for the assembler context descriptor. */
+typedef struct AsmCtx AsmCtx_t;
+
+/* Opaque type for a section. */
+typedef struct AsmScn AsmScn_t;
+
+/* Opaque type for a section group. */
+typedef struct AsmScnGrp AsmScnGrp_t;
+
+/* Opaque type for a symbol. */
+typedef struct AsmSym AsmSym_t;
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Create output file and return descriptor for assembler context. If
+ TEXTP is true the output is an assembler format text file.
+ Otherwise an object file is created. The MACHINE parameter
+ corresponds to an EM_ constant from <elf.h>, KLASS specifies the
+ class (32- or 64-bit), and DATA specifies the byte order (little or
+ big endian). */
+extern AsmCtx_t *asm_begin (const char *fname, bool textp, int machine,
+ int klass, int data);
+
+/* Abort the operation on the assembler context and free all resources. */
+extern int asm_abort (AsmCtx_t *ctx);
+
+/* Finalize output file and free all resources. */
+extern int asm_end (AsmCtx_t *ctx);
+
+
+/* Return handle for the named section. If it was not used before
+ create it. */
+extern AsmScn_t *asm_newscn (AsmCtx_t *ctx, const char *scnname,
+ GElf_Word type, GElf_Xword flags);
+
+
+/* Similar to 'asm_newscn', but make it part of section group GRP. */
+extern AsmScn_t *asm_newscn_ingrp (AsmCtx_t *ctx, const char *scnname,
+ GElf_Word type, GElf_Xword flags,
+ AsmScnGrp_t *grp);
+
+/* Create new subsection NR in the given section. */
+extern AsmScn_t *asm_newsubscn (AsmScn_t *asmscn, unsigned int nr);
+
+
+/* Return handle for new section group. The signature symbol can be
+ set later. */
+extern AsmScnGrp_t *asm_newscngrp (AsmCtx_t *ctx, const char *grpname,
+ AsmSym_t *signature, Elf32_Word flags);
+
+/* Set or overwrite signature symbol for group. */
+extern int asm_scngrp_newsignature (AsmScnGrp_t *grp, AsmSym_t *signature);
+
+
+/* Add zero terminated string STR of size LEN to (sub)section ASMSCN. */
+extern int asm_addstrz (AsmScn_t *asmscn, const char *str, size_t len);
+
+/* Add 8-bit signed integer NUM to (sub)section ASMSCN. */
+extern int asm_addint8 (AsmScn_t *asmscn, int8_t num);
+
+/* Add 8-bit unsigned integer NUM to (sub)section ASMSCN. */
+extern int asm_adduint8 (AsmScn_t *asmscn, uint8_t num);
+
+/* Add 16-bit signed integer NUM to (sub)section ASMSCN. */
+extern int asm_addint16 (AsmScn_t *asmscn, int16_t num);
+
+/* Add 16-bit unsigned integer NUM to (sub)section ASMSCN. */
+extern int asm_adduint16 (AsmScn_t *asmscn, uint16_t num);
+
+/* Add 32-bit signed integer NUM to (sub)section ASMSCN. */
+extern int asm_addint32 (AsmScn_t *asmscn, int32_t num);
+
+/* Add 32-bit unsigned integer NUM to (sub)section ASMSCN. */
+extern int asm_adduint32 (AsmScn_t *asmscn, uint32_t num);
+
+/* Add 64-bit signed integer NUM to (sub)section ASMSCN. */
+extern int asm_addint64 (AsmScn_t *asmscn, int64_t num);
+
+/* Add 64-bit unsigned integer NUM to (sub)section ASMSCN. */
+extern int asm_adduint64 (AsmScn_t *asmscn, uint64_t num);
+
+
+/* Add signed little endian base 128 integer NUM to (sub)section ASMSCN. */
+extern int asm_addsleb128 (AsmScn_t *asmscn, int32_t num);
+
+/* Add unsigned little endian base 128 integer NUM to (sub)section ASMSCN. */
+extern int asm_adduleb128 (AsmScn_t *asmscn, uint32_t num);
+
+
+/* Define new symbol NAME for current position in given section ASMSCN. */
+extern AsmSym_t *asm_newsym (AsmScn_t *asmscn, const char *name,
+ GElf_Xword size, int type, int binding);
+
+
+/* Define new common symbol NAME with given SIZE and alignment. */
+extern AsmSym_t *asm_newcomsym (AsmCtx_t *ctx, const char *name,
+ GElf_Xword size, GElf_Addr align);
+
+/* Define new common symbol NAME with given SIZE, VALUE, TYPE, and BINDING. */
+extern AsmSym_t *asm_newabssym (AsmCtx_t *ctx, const char *name,
+ GElf_Xword size, GElf_Addr value,
+ int type, int binding);
+
+
+/* Align (sub)section offset according to VALUE. */
+extern int asm_align (AsmScn_t *asmscn, GElf_Word value);
+
+/* Set the byte pattern used to fill gaps created by alignment. */
+extern int asm_fill (AsmScn_t *asmscn, void *bytes, size_t len);
+
+
+/* Return ELF descriptor created for the output file of the given context. */
+extern Elf *asm_getelf (AsmCtx_t *ctx);
+
+
+/* Return error code of last failing function call. This value is kept
+ separately for each thread. */
+extern int asm_errno (void);
+
+/* Return error string for ERROR. If ERROR is zero, return error string
+ for most recent error or NULL is none occurred. If ERROR is -1 the
+ behaviour is similar to the last case except that not NULL but a legal
+ string is returned. */
+extern const char *asm_errmsg (int __error);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* libasm.h */
diff --git a/libasm/libasm.map b/libasm/libasm.map
new file mode 100644
index 00000000..b0b5b5b7
--- /dev/null
+++ b/libasm/libasm.map
@@ -0,0 +1,33 @@
+ELFUTILS_1.0 {
+ global:
+ asm_abort;
+ asm_addint16;
+ asm_addint32;
+ asm_addint64;
+ asm_addint8;
+ asm_addsleb128;
+ asm_addstrz;
+ asm_adduint16;
+ asm_adduint32;
+ asm_adduint64;
+ asm_adduint8;
+ asm_adduleb128;
+ asm_align;
+ asm_begin;
+ asm_end;
+ asm_errmsg;
+ asm_errno;
+ asm_fill;
+ asm_getelf;
+ asm_newabssym;
+ asm_newcomsym;
+ asm_newscn;
+ asm_newscn_ingrp;
+ asm_newscngrp;
+ asm_newsubscn;
+ asm_newsym;
+ asm_scngrp_newsignature;
+
+ local:
+ *;
+};
diff --git a/libasm/libasmP.h b/libasm/libasmP.h
new file mode 100644
index 00000000..2e4954f6
--- /dev/null
+++ b/libasm/libasmP.h
@@ -0,0 +1,268 @@
+/* Internal definitions for libasm.
+ Copyright (C) 2002, 2004 Red Hat, Inc.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifndef _LIBASMP_H
+#define _LIBASMP_H 1
+
+#include <stdio.h>
+
+#include <libasm.h>
+#include <libebl.h>
+
+/* gettext helper macros. */
+#define _(Str) dgettext ("elfutils", Str)
+
+
+/* Known error codes. */
+enum
+ {
+ ASM_E_NOERROR,
+ ASM_E_NOMEM, /* No more memory. */
+ ASM_E_CANNOT_CREATE, /* Output file cannot be created. */
+ ASM_E_INVALID, /* Invalid parameters. */
+ ASM_E_CANNOT_CHMOD, /* Cannot change mode of output file. */
+ ASM_E_CANNOT_RENAME, /* Cannot rename output file. */
+ ASM_E_DUPLSYM, /* Duplicate symbol definition. */
+ ASM_E_LIBELF, /* Refer to error in libelf. */
+ ASM_E_TYPE, /* Invalid section type for operation. */
+ ASM_E_NUM /* Keep this entry as the last. */
+ };
+
+
+/* Special sections. */
+#define ASM_ABS_SCN ((Elf_Scn *) 1)
+#define ASM_COM_SCN ((Elf_Scn *) 2)
+
+
+/* And the hash table for symbols. */
+#include <symbolhash.h>
+
+
+/* Descriptor for a section. */
+struct AsmScn
+{
+ /* The underlying assembler context. */
+ AsmCtx_t *ctx;
+
+ /* Subsection ID. */
+ unsigned int subsection_id;
+
+ /* Section type. */
+ GElf_Word type;
+
+ union
+ {
+ /* Data only stored in the record for subsection zero. */
+ struct
+ {
+ /* The ELF section. */
+ Elf_Scn *scn;
+
+ /* Entry in the section header string table. */
+ struct Ebl_Strent *strent;
+
+ /* Next member of group. */
+ struct AsmScn *next_in_group;
+ } main;
+
+ /* Pointer to the record for subsection zero. */
+ AsmScn_t *up;
+ } data;
+
+ /* Current offset in the (sub)section. */
+ GElf_Off offset;
+ /* Maximum alignment of the section so far. */
+ GElf_Word max_align;
+
+ /* Section content. */
+ struct AsmData
+ {
+ /* Currently used number of bytes in the block. */
+ size_t len;
+
+ /* Number of bytes allocated. */
+ size_t maxlen;
+
+ /* Pointer to the next block. */
+ struct AsmData *next;
+
+ /* The actual data. */
+ char data[flexarr_size];
+ } *content;
+
+ /* Fill pattern. */
+ struct FillPattern
+ {
+ size_t len;
+ char bytes[flexarr_size];
+ } *pattern;
+
+ /* Next subsection. */
+ AsmScn_t *subnext;
+
+ /* List of all allocated sections. */
+ AsmScn_t *allnext;
+
+ /* Name of the section. */
+ char name[flexarr_size];
+};
+
+
+/* Descriptor used for the assembling session. */
+struct AsmCtx
+{
+ /* File descriptor of the temporary file. */
+ int fd;
+
+ /* True if text output is wanted. */
+ bool textp;
+
+ /* Output file handle. */
+ union
+ {
+ /* ELF descriptor of the temporary file. */
+ Elf *elf;
+ /* I/O stream for text output. */
+ FILE *file;
+ } out;
+
+
+ /* List with defined sections. */
+ AsmScn_t *section_list;
+ /* Section header string table. */
+ struct Ebl_Strtab *section_strtab;
+
+ /* Table with defined symbols. */
+ asm_symbol_tab symbol_tab;
+ /* Number of symbols in the table. */
+ unsigned int nsymbol_tab;
+ /* Symbol string table. */
+ struct Ebl_Strtab *symbol_strtab;
+
+ /* List of section groups. */
+ struct AsmScnGrp *groups;
+ /* Number of section groups. */
+ size_t ngroups;
+
+ /* Current required alignment for common symbols. */
+ GElf_Word common_align;
+
+ /* Lock to handle multithreaded programs. */
+ rwlock_define (,lock);
+
+ /* Counter for temporary symbols. */
+ unsigned int tempsym_count;
+
+ /* Name of the output file. */
+ char *fname;
+ /* The name of the temporary file. */
+ char tmp_fname[flexarr_size];
+};
+
+
+/* Descriptor for a symbol. */
+struct AsmSym
+{
+ /* Reference to the section which contains the symbol. */
+ AsmScn_t *scn;
+
+ /* Type of the symbol. */
+ int8_t type;
+ /* Binding of the symbol. */
+ int8_t binding;
+
+ /* Size of the symbol. */
+ GElf_Xword size;
+
+ /* Offset in the section. */
+ GElf_Off offset;
+
+ /* Symbol table index of the symbol in the symbol table. */
+ size_t symidx;
+
+ /* Reference to name of the symbol. */
+ struct Ebl_Strent *strent;
+};
+
+
+/* Descriptor for section group. */
+struct AsmScnGrp
+{
+ /* Entry in the section header string table. */
+ struct Ebl_Strent *strent;
+
+ /* The ELF section. */
+ Elf_Scn *scn;
+
+ /* The signature. */
+ struct AsmSym *signature;
+
+ /* First member. */
+ struct AsmScn *members;
+ /* Number of members. */
+ size_t nmembers;
+
+ /* Flags. */
+ Elf32_Word flags;
+
+ /* Next group. */
+ struct AsmScnGrp *next;
+
+ /* Name of the section group. */
+ char name[flexarr_size];
+};
+
+
+/* The default fill pattern: one zero byte. */
+extern const struct FillPattern *__libasm_default_pattern
+ attribute_hidden;
+
+
+/* Ensure there are at least LEN bytes available in the output buffer
+ for ASMSCN. */
+extern int __libasm_ensure_section_space (AsmScn_t *asmscn, size_t len)
+ internal_function;
+
+/* Free all resources associated with the assembler context. */
+extern void __libasm_finictx (AsmCtx_t *ctx) internal_function;
+
+/* Set error code. */
+extern void __libasm_seterrno (int err) internal_function;
+
+/* Return handle for the named section. If it was not used before
+ create it. */
+extern AsmScn_t *__asm_newscn_internal (AsmCtx_t *ctx, const char *scnname,
+ GElf_Word type, GElf_Xword flags)
+ attribute_hidden;
+
+
+/* Internal aliases of the asm_addintXX functions. */
+extern int __asm_addint8_internal (AsmScn_t *asmscn, int8_t num)
+ attribute_hidden;
+extern int __asm_addint16_internal (AsmScn_t *asmscn, int16_t num)
+ attribute_hidden;
+extern int __asm_addint32_internal (AsmScn_t *asmscn, int32_t num)
+ attribute_hidden;
+extern int __asm_addint64_internal (AsmScn_t *asmscn, int64_t num)
+ attribute_hidden;
+
+
+
+/* Test whether given symbol is an internal symbol and if yes, whether
+ we should nevertheless emit it in the symbol table. */
+// XXX The second part should probably be controlled by an option which
+// isn't implemented yet
+// XXX Also, the format will change with the backend.
+#define asm_emit_symbol_p(name) (strncmp (name, ".L", 2) != 0)
+
+#endif /* libasmP.h */
diff --git a/libasm/symbolhash.c b/libasm/symbolhash.c
new file mode 100644
index 00000000..62d2c852
--- /dev/null
+++ b/libasm/symbolhash.c
@@ -0,0 +1,39 @@
+/* Symbol hash table implementation.
+ Copyright (C) 2001, 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2001.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifdef HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <string.h>
+
+#include <libasmP.h>
+#include <libebl.h>
+
+/* Definitions for the symbol hash table. */
+#define TYPE AsmSym_t *
+#define NAME asm_symbol_tab
+#define ITERATE 1
+#define REVERSE 1
+#define COMPARE(a, b) \
+ strcmp (ebl_string ((a)->strent), ebl_string ((b)->strent))
+
+#define next_prime __libasm_next_prime
+extern size_t next_prime (size_t) attribute_hidden;
+
+#include "../lib/dynamicsizehash.c"
+
+#undef next_prime
+#define next_prime attribute_hidden __libasm_next_prime
+#include "../lib/next_prime.c"
diff --git a/libasm/symbolhash.h b/libasm/symbolhash.h
new file mode 100644
index 00000000..4377b4b5
--- /dev/null
+++ b/libasm/symbolhash.h
@@ -0,0 +1,25 @@
+/* Copyright (C) 2001, 2002 Red Hat, Inc.
+ Written by Ulrich Drepper <drepper@redhat.com>, 2001.
+
+ This program is Open Source software; you can redistribute it and/or
+ modify it under the terms of the Open Software License version 1.0 as
+ published by the Open Source Initiative.
+
+ You should have received a copy of the Open Software License along
+ with this program; if not, you may obtain a copy of the Open Software
+ License version 1.0 from http://www.opensource.org/licenses/osl.php or
+ by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
+ 3001 King Ranch Road, Ukiah, CA 95482. */
+
+#ifndef SYMBOLHASH_H
+#define SYMBOLHASH_H 1
+
+/* Definitions for the symbol hash table. */
+#define TYPE AsmSym_t *
+#define NAME asm_symbol_tab
+#define ITERATE 1
+#define COMPARE(a, b) \
+ strcmp (ebl_string ((a)->strent), ebl_string ((b)->strent))
+#include <dynamicsizehash.h>
+
+#endif /* symbolhash.h */