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