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

top-level:

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

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

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

config:

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

src, tests:

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

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

185 lines
4.7 KiB
C

/* Create descriptor for assembling.
Copyright (C) 2002 Red Hat, Inc.
This file is part of elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 2002.
This file is free software; you can redistribute it and/or modify
it under the terms of either
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at
your option) any later version
or
* the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at
your option) any later version
or both in parallel, as here.
elfutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received copies of the GNU General Public License and
the GNU Lesser General Public License along with this program. If
not, see <http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdio_ext.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)
{
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, Ebl *ebl)
{
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. */
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);
/* 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, 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);
/* 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, ebl, textp)
const char *fname;
Ebl *ebl;
bool textp;
{
if (fname == NULL && ! textp)
return NULL;
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. */
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);
if (fname != 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;
/* Now we differentiate between textual and binary output. */
result->textp = textp;
if (textp)
result = prepare_text_output (result);
else
result = prepare_binary_output (result, ebl);
return result;
}