diff options
author | Ulrich Drepper <drepper@redhat.com> | 2005-08-03 02:05:39 +0000 |
---|---|---|
committer | Ulrich Drepper <drepper@redhat.com> | 2005-08-03 02:05:39 +0000 |
commit | a38998e815ccde5d90ff0800c31da255eb3430d3 (patch) | |
tree | e5f035b73eeaf4033f159351ecad375f63d3a0aa /libasm | |
parent | ef9c9c8fca98a8f9b6aa7c3604ba47e74094e181 (diff) | |
download | elfutils-a38998e815ccde5d90ff0800c31da255eb3430d3.tar.gz |
Add some patches from the disasm-branch branch.
The asm_begin interface changed.
In text mode output is really written to the file.
Diffstat (limited to 'libasm')
-rw-r--r-- | libasm/ChangeLog | 18 | ||||
-rw-r--r-- | libasm/Makefile.am | 2 | ||||
-rw-r--r-- | libasm/asm_abort.c | 5 | ||||
-rw-r--r-- | libasm/asm_addint8.c | 21 | ||||
-rw-r--r-- | libasm/asm_addsleb128.c | 4 | ||||
-rw-r--r-- | libasm/asm_adduleb128.c | 4 | ||||
-rw-r--r-- | libasm/asm_align.c | 29 | ||||
-rw-r--r-- | libasm/asm_begin.c | 86 | ||||
-rw-r--r-- | libasm/asm_end.c | 7 | ||||
-rw-r--r-- | libasm/asm_error.c | 5 | ||||
-rw-r--r-- | libasm/asm_newscn.c | 5 | ||||
-rw-r--r-- | libasm/libasm.h | 7 | ||||
-rw-r--r-- | libasm/libasmP.h | 4 |
13 files changed, 129 insertions, 68 deletions
diff --git a/libasm/ChangeLog b/libasm/ChangeLog index 8b7b44d2..6adeef1e 100644 --- a/libasm/ChangeLog +++ b/libasm/ChangeLog @@ -1,6 +1,22 @@ +2005-08-02 Ulrich Drepper <drepper@redhat.com> + + * Makefile.am (AM_CFLAGS): Add -std=gnu99. + * asm_abort.c: Don't try to remove output file if there is none. + * asm_addint8.c: In print mode, print to file not stdout. + * asm_addsleb128.c: Likewise. + * asm_adduleb128.c: Likewise. + * asm_newscn.c: Likewise. + * asm_align.c: Implement print mode. + * asm_begin.c (asm_begin): Change interface. Take binary class and + byte order information from new Ebl parameter. + * libasm.h: Adjust prototype. + * asm_end.c (text_end): Close file if necesary. + * asm_error.c: Add new error ASM_E_IOERROR. + * libasmP.h: Add ASM_E_IOERROR definition. + 2005-02-15 Ulrich Drepper <drepper@redhat.com> - * Makefile (AM_CFLAGS): Add -Wunused -Wextra -Wformat=2. + * Makefile.am (AM_CFLAGS): Add -Wunused -Wextra -Wformat=2. * asm_end.c (text_end): Mark parameter as possibly unused. diff --git a/libasm/Makefile.am b/libasm/Makefile.am index 34b19d24..7802a5c0 100644 --- a/libasm/Makefile.am +++ b/libasm/Makefile.am @@ -18,7 +18,7 @@ AM_CFLAGS = -fmudflap else AM_CFLAGS = endif -AM_CFLAGS += -Wall -Wshadow -Werror -Wunused -Wextra -Wformat=2 +AM_CFLAGS += -std=gnu99 -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) diff --git a/libasm/asm_abort.c b/libasm/asm_abort.c index f35757ca..3353a023 100644 --- a/libasm/asm_abort.c +++ b/libasm/asm_abort.c @@ -1,5 +1,5 @@ /* Abort operations on the assembler context, free all resources. - Copyright (C) 2002 Red Hat, Inc. + 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 @@ -36,7 +36,8 @@ asm_abort (ctx) (void) elf_end (ctx->out.elf); /* Now close the temporary file and remove it. */ - (void) unlink (ctx->tmp_fname); + if (ctx->fd != -1) + (void) unlink (ctx->tmp_fname); /* Free the resources. */ __libasm_finictx (ctx); diff --git a/libasm/asm_addint8.c b/libasm/asm_addint8.c index 35d9bf57..bbd8e5f7 100644 --- a/libasm/asm_addint8.c +++ b/libasm/asm_addint8.c @@ -1,5 +1,5 @@ /* Add integer to a section. - Copyright (C) 2002 Red Hat, Inc. + 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 @@ -53,22 +53,25 @@ FCT(SIZE) (asmscn, num) { // XXX Needs to use backend specified pseudo-ops if (SIZE == 8) - printf ("\t.byte\t%" PRId8 "\n", (int8_t) num); + fprintf (asmscn->ctx->out.file, "\t.byte\t%" PRId8 "\n", (int8_t) num); else if (SIZE == 16) - printf ("\t.value\t%" PRId16 "\n", (int16_t) num); + fprintf (asmscn->ctx->out.file, "\t.value\t%" PRId16 "\n", + (int16_t) num); else if (SIZE == 32) - printf ("\t.long\t%" PRId32 "\n", (int32_t) num); + fprintf (asmscn->ctx->out.file, "\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)); + fprintf (asmscn->ctx->out.file, + "\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 diff --git a/libasm/asm_addsleb128.c b/libasm/asm_addsleb128.c index 0661b36e..fdefd01b 100644 --- a/libasm/asm_addsleb128.c +++ b/libasm/asm_addsleb128.c @@ -1,5 +1,5 @@ /* Add signed little endian base 128 integer to a section. - Copyright (C) 2002 Red Hat, Inc. + 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 @@ -37,7 +37,7 @@ asm_addsleb128 (asmscn, num) } if (unlikely (asmscn->ctx->textp)) - printf ("\t.sleb128\t%" PRId32 "\n", num); + fprintf (asmscn->ctx->out.file, "\t.sleb128\t%" PRId32 "\n", num); else { char tmpbuf[(sizeof (num) * 8 + 6) / 7]; diff --git a/libasm/asm_adduleb128.c b/libasm/asm_adduleb128.c index 521d265a..1685b5eb 100644 --- a/libasm/asm_adduleb128.c +++ b/libasm/asm_adduleb128.c @@ -1,5 +1,5 @@ /* Add integer to a section. - Copyright (C) 2002 Red Hat, Inc. + 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 @@ -37,7 +37,7 @@ asm_adduleb128 (asmscn, num) } if (unlikely (asmscn->ctx->textp)) - printf ("\t.uleb128\t%" PRIu32 "\n", num); + fprintf (asmscn->ctx->out.file, "\t.uleb128\t%" PRIu32 "\n", num); else { char tmpbuf[(sizeof (num) * 8 + 6) / 7]; diff --git a/libasm/asm_align.c b/libasm/asm_align.c index e7350924..a52fc86d 100644 --- a/libasm/asm_align.c +++ b/libasm/asm_align.c @@ -1,5 +1,5 @@ /* Align section. - Copyright (C) 2002 Red Hat, Inc. + 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 @@ -16,6 +16,7 @@ # include <config.h> #endif +#include <inttypes.h> #include <stdlib.h> #include <sys/param.h> @@ -39,6 +40,25 @@ asm_align (asmscn, value) 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; @@ -47,10 +67,7 @@ asm_align (asmscn, value) if ((asmscn->offset & (value - 1)) != 0) { /* Add fillbytes. */ - size_t cnt; - size_t byteptr; - - cnt = value - (asmscn->offset & (value - 1)); + 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); @@ -59,7 +76,7 @@ asm_align (asmscn, value) /* Fill in the bytes. We align the pattern according to the current offset. */ - byteptr = asmscn->offset % asmscn->pattern->len; + size_t byteptr = asmscn->offset % asmscn->pattern->len; /* Update the total size. */ asmscn->offset += cnt; diff --git a/libasm/asm_begin.c b/libasm/asm_begin.c index 3896f78f..1ded3474 100644 --- a/libasm/asm_begin.c +++ b/libasm/asm_begin.c @@ -18,6 +18,8 @@ #include <assert.h> #include <errno.h> +#include <stdio.h> +#include <stdio_ext.h> #include <stdlib.h> #include <string.h> #include <unistd.h> @@ -30,12 +32,27 @@ static AsmCtx_t * prepare_text_output (AsmCtx_t *result) { + if (result->fd == -1) + result->out.file = stdout; + else + { + result->out.file = fdopen (result->fd, "a"); + if (result->out.file == NULL) + { + close (result->fd); + free (result); + result = NULL; + } + + __fsetlocking (result->out.file, FSETLOCKING_BYCALLER); + } + return result; } static AsmCtx_t * -prepare_binary_output (AsmCtx_t *result, int machine, int klass, int data) +prepare_binary_output (AsmCtx_t *result, Ebl *ebl) { GElf_Ehdr *ehdr; GElf_Ehdr ehdr_mem; @@ -53,7 +70,8 @@ prepare_binary_output (AsmCtx_t *result, int machine, int klass, int data) } /* Create the ELF header for the output file. */ - if (gelf_newehdr (result->out.elf, klass) == 0) + int class = ebl_get_elfclass (ebl); + if (gelf_newehdr (result->out.elf, class) == 0) goto err_libelf; ehdr = gelf_getehdr (result->out.elf, &ehdr_mem); @@ -65,11 +83,10 @@ prepare_binary_output (AsmCtx_t *result, int machine, int klass, int data) /* 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; + /* Use the machine, class, and endianess values from the Ebl descriptor. */ + ehdr->e_machine = ebl_get_elfmachine (ebl); + ehdr->e_ident[EI_CLASS] = class; + ehdr->e_ident[EI_DATA] = ebl_get_elfdata (ebl); memcpy (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG); @@ -95,47 +112,48 @@ prepare_binary_output (AsmCtx_t *result, int machine, int klass, int data) AsmCtx_t * -asm_begin (fname, textp, machine, klass, data) +asm_begin (fname, ebl, textp) const char *fname; + Ebl *ebl; bool textp; - int machine; - int klass; - int data; { - size_t fname_len = strlen (fname); - AsmCtx_t *result; - + if (fname == NULL && ! textp) + return NULL; - /* First order of business: find the appropriate backend. If it - does not exist we don't have to look further. */ - // XXX + size_t fname_len = fname != NULL ? strlen (fname) : 0; /* 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); + AsmCtx_t *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); + /* Initialize the lock. */ + rwlock_init (result->lock); - /* Create the temporary file. */ - result->fd = mkstemp (result->tmp_fname); - if (result->fd == -1) + if (fname != NULL) { - int save_errno = errno; - free (result); - __libasm_seterrno (ASM_E_CANNOT_CREATE); - errno = save_errno; - return NULL; + /* 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; + } } + else + result->fd = -1; /* Initialize the counter for temporary symbols. */ result->tempsym_count = 0; @@ -145,7 +163,7 @@ asm_begin (fname, textp, machine, klass, data) if (textp) result = prepare_text_output (result); else - result = prepare_binary_output (result, machine, klass, data); + result = prepare_binary_output (result, ebl); return result; } diff --git a/libasm/asm_end.c b/libasm/asm_end.c index 74f01f08..33191f9c 100644 --- a/libasm/asm_end.c +++ b/libasm/asm_end.c @@ -33,7 +33,12 @@ static int text_end (AsmCtx_t *ctx __attribute__ ((unused))) { - // XXX Does anything have to be done? + if (fclose (ctx->out.file) != 0) + { + __libasm_seterrno (ASM_E_IOERROR); + return -1; + } + return 0; } diff --git a/libasm/asm_error.c b/libasm/asm_error.c index 9d2d81ed..3b5aa055 100644 --- a/libasm/asm_error.c +++ b/libasm/asm_error.c @@ -1,5 +1,5 @@ /* Error handling in libasm. - Copyright (C) 2002, 2004 Red Hat, Inc. + Copyright (C) 2002, 2004, 2005 Red Hat, Inc. Written by Ulrich Drepper <drepper@redhat.com>, 2002. This program is Open Source software; you can redistribute it and/or @@ -112,7 +112,8 @@ static const char *msgs[ASM_E_NUM] = [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") + [ASM_E_TYPE] = N_("invalid section type for operation"), + [ASM_E_IOERROR] = N_("error during output of data"), }; const char * diff --git a/libasm/asm_newscn.c b/libasm/asm_newscn.c index 75890a60..7e585325 100644 --- a/libasm/asm_newscn.c +++ b/libasm/asm_newscn.c @@ -1,5 +1,5 @@ /* Create new section in output file. - Copyright (C) 2002 Red Hat, Inc. + 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 @@ -81,7 +81,8 @@ text_newscn (AsmScn_t *result, GElf_Word type, GElf_Xword flags) /* Terminate the string. */ *wp = '\0'; - printf ("\t.section \"%s\"%s%s\n", result->name, flagstr, typestr); + fprintf (result->ctx->out.file, "\t.section \"%s\"%s%s\n", + result->name, flagstr, typestr); return result; } diff --git a/libasm/libasm.h b/libasm/libasm.h index dbdd6fc2..46d5e8db 100644 --- a/libasm/libasm.h +++ b/libasm/libasm.h @@ -1,5 +1,5 @@ /* Interface for libasm. - Copyright (C) 2002 Red Hat, Inc. + Copyright (C) 2002, 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 @@ -17,7 +17,7 @@ #include <stdbool.h> #include <stdint.h> -#include <gelf.h> +#include <libebl.h> /* Opaque type for the assembler context descriptor. */ @@ -43,8 +43,7 @@ extern "C" { 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); +extern AsmCtx_t *asm_begin (const char *fname, Ebl *ebl, bool textp); /* Abort the operation on the assembler context and free all resources. */ extern int asm_abort (AsmCtx_t *ctx); diff --git a/libasm/libasmP.h b/libasm/libasmP.h index 2e4954f6..1cbec55c 100644 --- a/libasm/libasmP.h +++ b/libasm/libasmP.h @@ -1,5 +1,5 @@ /* Internal definitions for libasm. - Copyright (C) 2002, 2004 Red Hat, Inc. + 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 @@ -17,7 +17,6 @@ #include <stdio.h> #include <libasm.h> -#include <libebl.h> /* gettext helper macros. */ #define _(Str) dgettext ("elfutils", Str) @@ -35,6 +34,7 @@ enum ASM_E_DUPLSYM, /* Duplicate symbol definition. */ ASM_E_LIBELF, /* Refer to error in libelf. */ ASM_E_TYPE, /* Invalid section type for operation. */ + ASM_E_IOERROR, /* Error during output of data. */ ASM_E_NUM /* Keep this entry as the last. */ }; |