Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 1 | /* Create descriptor for assembling. |
| 2 | Copyright (C) 2002 Red Hat, Inc. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 3 | This file is part of Red Hat elfutils. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 4 | Written by Ulrich Drepper <drepper@redhat.com>, 2002. |
| 5 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 6 | Red Hat elfutils is free software; you can redistribute it and/or modify |
| 7 | it under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; version 2 of the License. |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 9 | |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 10 | Red Hat elfutils is distributed in the hope that it will be useful, but |
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License along |
| 16 | with Red Hat elfutils; if not, write to the Free Software Foundation, |
Ulrich Drepper | 1e9ef50 | 2006-04-04 22:29:06 +0000 | [diff] [blame] | 17 | Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA. |
Ulrich Drepper | 361df7d | 2006-04-04 21:38:57 +0000 | [diff] [blame] | 18 | |
| 19 | Red Hat elfutils is an included package of the Open Invention Network. |
| 20 | An included package of the Open Invention Network is a package for which |
| 21 | Open Invention Network licensees cross-license their patents. No patent |
| 22 | license is granted, either expressly or impliedly, by designation as an |
| 23 | included package. Should you wish to participate in the Open Invention |
| 24 | Network licensing program, please visit www.openinventionnetwork.com |
| 25 | <http://www.openinventionnetwork.com>. */ |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | # include <config.h> |
| 29 | #endif |
| 30 | |
| 31 | #include <assert.h> |
| 32 | #include <errno.h> |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdio_ext.h> |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
| 37 | #include <unistd.h> |
| 38 | |
| 39 | #include <gelf.h> |
| 40 | #include "libasmP.h" |
| 41 | #include <system.h> |
| 42 | |
| 43 | |
| 44 | static AsmCtx_t * |
| 45 | prepare_text_output (AsmCtx_t *result) |
| 46 | { |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 47 | if (result->fd == -1) |
| 48 | result->out.file = stdout; |
| 49 | else |
| 50 | { |
| 51 | result->out.file = fdopen (result->fd, "a"); |
| 52 | if (result->out.file == NULL) |
| 53 | { |
| 54 | close (result->fd); |
| 55 | free (result); |
| 56 | result = NULL; |
| 57 | } |
| 58 | |
| 59 | __fsetlocking (result->out.file, FSETLOCKING_BYCALLER); |
| 60 | } |
| 61 | |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 62 | return result; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | static AsmCtx_t * |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 67 | prepare_binary_output (AsmCtx_t *result, Ebl *ebl) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 68 | { |
| 69 | GElf_Ehdr *ehdr; |
| 70 | GElf_Ehdr ehdr_mem; |
| 71 | |
| 72 | /* Create the ELF descriptor for the file. */ |
| 73 | result->out.elf = elf_begin (result->fd, ELF_C_WRITE_MMAP, NULL); |
| 74 | if (result->out.elf == NULL) |
| 75 | { |
| 76 | err_libelf: |
| 77 | unlink (result->tmp_fname); |
| 78 | close (result->fd); |
| 79 | free (result); |
| 80 | __libasm_seterrno (ASM_E_LIBELF); |
| 81 | return NULL; |
| 82 | } |
| 83 | |
| 84 | /* Create the ELF header for the output file. */ |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 85 | int class = ebl_get_elfclass (ebl); |
| 86 | if (gelf_newehdr (result->out.elf, class) == 0) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 87 | goto err_libelf; |
| 88 | |
| 89 | ehdr = gelf_getehdr (result->out.elf, &ehdr_mem); |
| 90 | /* If this failed we are in trouble. */ |
| 91 | assert (ehdr != NULL); |
| 92 | |
| 93 | /* We create an object file. */ |
| 94 | ehdr->e_type = ET_REL; |
| 95 | /* Set the ELF version. */ |
| 96 | ehdr->e_version = EV_CURRENT; |
| 97 | |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 98 | /* Use the machine, class, and endianess values from the Ebl descriptor. */ |
| 99 | ehdr->e_machine = ebl_get_elfmachine (ebl); |
| 100 | ehdr->e_ident[EI_CLASS] = class; |
| 101 | ehdr->e_ident[EI_DATA] = ebl_get_elfdata (ebl); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 102 | |
| 103 | memcpy (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG); |
| 104 | |
| 105 | /* Write the ELF header information back. */ |
| 106 | (void) gelf_update_ehdr (result->out.elf, ehdr); |
| 107 | |
| 108 | /* No section so far. */ |
| 109 | result->section_list = NULL; |
| 110 | |
| 111 | /* Initialize the hash table. */ |
| 112 | asm_symbol_tab_init (&result->symbol_tab, 67); |
| 113 | result->nsymbol_tab = 0; |
| 114 | /* And the string tables. */ |
| 115 | result->section_strtab = ebl_strtabinit (true); |
| 116 | result->symbol_strtab = ebl_strtabinit (true); |
| 117 | |
| 118 | /* We have no section groups so far. */ |
| 119 | result->groups = NULL; |
| 120 | result->ngroups = 0; |
| 121 | |
| 122 | return result; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | AsmCtx_t * |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 127 | asm_begin (fname, ebl, textp) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 128 | const char *fname; |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 129 | Ebl *ebl; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 130 | bool textp; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 131 | { |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 132 | if (fname == NULL && ! textp) |
| 133 | return NULL; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 134 | |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 135 | size_t fname_len = fname != NULL ? strlen (fname) : 0; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 136 | |
| 137 | /* Create the file descriptor. We do not generate the output file |
| 138 | right away. Instead we create a temporary file in the same |
| 139 | directory which, if everything goes alright, will replace a |
| 140 | possibly existing file with the given name. */ |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 141 | AsmCtx_t *result |
| 142 | = (AsmCtx_t *) malloc (sizeof (AsmCtx_t) + 2 * fname_len + 9); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 143 | if (result == NULL) |
| 144 | return NULL; |
| 145 | |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 146 | /* Initialize the lock. */ |
| 147 | rwlock_init (result->lock); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 148 | |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 149 | if (fname != NULL) |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 150 | { |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 151 | /* Create the name of the temporary file. */ |
| 152 | result->fname = stpcpy (mempcpy (result->tmp_fname, fname, fname_len), |
| 153 | ".XXXXXX") + 1; |
| 154 | memcpy (result->fname, fname, fname_len + 1); |
| 155 | |
| 156 | /* Create the temporary file. */ |
| 157 | result->fd = mkstemp (result->tmp_fname); |
| 158 | if (result->fd == -1) |
| 159 | { |
| 160 | int save_errno = errno; |
| 161 | free (result); |
| 162 | __libasm_seterrno (ASM_E_CANNOT_CREATE); |
| 163 | errno = save_errno; |
| 164 | return NULL; |
| 165 | } |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 166 | } |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 167 | else |
| 168 | result->fd = -1; |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 169 | |
| 170 | /* Initialize the counter for temporary symbols. */ |
| 171 | result->tempsym_count = 0; |
| 172 | |
| 173 | /* Now we differentiate between textual and binary output. */ |
| 174 | result->textp = textp; |
| 175 | if (textp) |
| 176 | result = prepare_text_output (result); |
| 177 | else |
Ulrich Drepper | a38998e | 2005-08-03 02:05:39 +0000 | [diff] [blame] | 178 | result = prepare_binary_output (result, ebl); |
Ulrich Drepper | b08d5a8 | 2005-07-26 05:00:05 +0000 | [diff] [blame] | 179 | |
| 180 | return result; |
| 181 | } |