blob: 48842d3c608b26d69e0590efbdc83d902a240f84 [file] [log] [blame]
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00001/* Create descriptor for assembling.
2 Copyright (C) 2002 Red Hat, Inc.
Mark Wielaardde2ed972012-06-05 17:15:16 +02003 This file is part of elfutils.
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00004 Written by Ulrich Drepper <drepper@redhat.com>, 2002.
5
Mark Wielaardde2ed972012-06-05 17:15:16 +02006 This file is free software; you can redistribute it and/or modify
7 it under the terms of either
Ulrich Drepperb08d5a82005-07-26 05:00:05 +00008
Mark Wielaardde2ed972012-06-05 17:15:16 +02009 * 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
Ulrich Drepper361df7d2006-04-04 21:38:57 +000022 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
Mark Wielaardde2ed972012-06-05 17:15:16 +020026 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/>. */
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000029
30#ifdef HAVE_CONFIG_H
31# include <config.h>
32#endif
33
34#include <assert.h>
35#include <errno.h>
Ulrich Dreppera38998e2005-08-03 02:05:39 +000036#include <stdio.h>
37#include <stdio_ext.h>
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000038#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41
42#include <gelf.h>
43#include "libasmP.h"
44#include <system.h>
45
46
47static AsmCtx_t *
48prepare_text_output (AsmCtx_t *result)
49{
Ulrich Dreppera38998e2005-08-03 02:05:39 +000050 if (result->fd == -1)
51 result->out.file = stdout;
52 else
53 {
54 result->out.file = fdopen (result->fd, "a");
55 if (result->out.file == NULL)
56 {
57 close (result->fd);
58 free (result);
59 result = NULL;
60 }
61
62 __fsetlocking (result->out.file, FSETLOCKING_BYCALLER);
63 }
64
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000065 return result;
66}
67
68
69static AsmCtx_t *
Ulrich Dreppera38998e2005-08-03 02:05:39 +000070prepare_binary_output (AsmCtx_t *result, Ebl *ebl)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000071{
72 GElf_Ehdr *ehdr;
73 GElf_Ehdr ehdr_mem;
74
75 /* Create the ELF descriptor for the file. */
76 result->out.elf = elf_begin (result->fd, ELF_C_WRITE_MMAP, NULL);
77 if (result->out.elf == NULL)
78 {
79 err_libelf:
80 unlink (result->tmp_fname);
81 close (result->fd);
82 free (result);
83 __libasm_seterrno (ASM_E_LIBELF);
84 return NULL;
85 }
86
87 /* Create the ELF header for the output file. */
Ulrich Dreppera38998e2005-08-03 02:05:39 +000088 int class = ebl_get_elfclass (ebl);
89 if (gelf_newehdr (result->out.elf, class) == 0)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +000090 goto err_libelf;
91
92 ehdr = gelf_getehdr (result->out.elf, &ehdr_mem);
93 /* If this failed we are in trouble. */
94 assert (ehdr != NULL);
95
96 /* We create an object file. */
97 ehdr->e_type = ET_REL;
98 /* Set the ELF version. */
99 ehdr->e_version = EV_CURRENT;
100
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000101 /* Use the machine, class, and endianess values from the Ebl descriptor. */
102 ehdr->e_machine = ebl_get_elfmachine (ebl);
103 ehdr->e_ident[EI_CLASS] = class;
104 ehdr->e_ident[EI_DATA] = ebl_get_elfdata (ebl);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000105
106 memcpy (&ehdr->e_ident[EI_MAG0], ELFMAG, SELFMAG);
107
108 /* Write the ELF header information back. */
109 (void) gelf_update_ehdr (result->out.elf, ehdr);
110
111 /* No section so far. */
112 result->section_list = NULL;
113
114 /* Initialize the hash table. */
115 asm_symbol_tab_init (&result->symbol_tab, 67);
116 result->nsymbol_tab = 0;
117 /* And the string tables. */
118 result->section_strtab = ebl_strtabinit (true);
119 result->symbol_strtab = ebl_strtabinit (true);
120
121 /* We have no section groups so far. */
122 result->groups = NULL;
123 result->ngroups = 0;
124
125 return result;
126}
127
128
129AsmCtx_t *
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000130asm_begin (fname, ebl, textp)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000131 const char *fname;
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000132 Ebl *ebl;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000133 bool textp;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000134{
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000135 if (fname == NULL && ! textp)
136 return NULL;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000137
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000138 size_t fname_len = fname != NULL ? strlen (fname) : 0;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000139
140 /* Create the file descriptor. We do not generate the output file
141 right away. Instead we create a temporary file in the same
142 directory which, if everything goes alright, will replace a
143 possibly existing file with the given name. */
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000144 AsmCtx_t *result
145 = (AsmCtx_t *) malloc (sizeof (AsmCtx_t) + 2 * fname_len + 9);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000146 if (result == NULL)
147 return NULL;
148
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000149 /* Initialize the lock. */
150 rwlock_init (result->lock);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000151
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000152 if (fname != NULL)
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000153 {
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000154 /* Create the name of the temporary file. */
155 result->fname = stpcpy (mempcpy (result->tmp_fname, fname, fname_len),
156 ".XXXXXX") + 1;
157 memcpy (result->fname, fname, fname_len + 1);
158
159 /* Create the temporary file. */
160 result->fd = mkstemp (result->tmp_fname);
161 if (result->fd == -1)
162 {
163 int save_errno = errno;
164 free (result);
165 __libasm_seterrno (ASM_E_CANNOT_CREATE);
166 errno = save_errno;
167 return NULL;
168 }
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000169 }
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000170 else
171 result->fd = -1;
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000172
173 /* Initialize the counter for temporary symbols. */
174 result->tempsym_count = 0;
175
176 /* Now we differentiate between textual and binary output. */
177 result->textp = textp;
178 if (textp)
179 result = prepare_text_output (result);
180 else
Ulrich Dreppera38998e2005-08-03 02:05:39 +0000181 result = prepare_binary_output (result, ebl);
Ulrich Drepperb08d5a82005-07-26 05:00:05 +0000182
183 return result;
184}