xref: /freebsd/contrib/elftoolchain/elfcopy/main.c (revision 3ef90571c1feac738dd60ea1516df2f974f18b56)
1a85fe12eSEd Maste /*-
2a85fe12eSEd Maste  * Copyright (c) 2007-2013 Kai Wang
3a85fe12eSEd Maste  * All rights reserved.
4a85fe12eSEd Maste  *
5a85fe12eSEd Maste  * Redistribution and use in source and binary forms, with or without
6a85fe12eSEd Maste  * modification, are permitted provided that the following conditions
7a85fe12eSEd Maste  * are met:
8a85fe12eSEd Maste  * 1. Redistributions of source code must retain the above copyright
9a85fe12eSEd Maste  *    notice, this list of conditions and the following disclaimer.
10a85fe12eSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
11a85fe12eSEd Maste  *    notice, this list of conditions and the following disclaimer in the
12a85fe12eSEd Maste  *    documentation and/or other materials provided with the distribution.
13a85fe12eSEd Maste  *
14a85fe12eSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a85fe12eSEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a85fe12eSEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a85fe12eSEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a85fe12eSEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a85fe12eSEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a85fe12eSEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a85fe12eSEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a85fe12eSEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a85fe12eSEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a85fe12eSEd Maste  * SUCH DAMAGE.
25a85fe12eSEd Maste  */
26a85fe12eSEd Maste 
27a85fe12eSEd Maste #include <sys/param.h>
28a85fe12eSEd Maste #include <sys/stat.h>
29a85fe12eSEd Maste 
30a85fe12eSEd Maste #include <err.h>
31a85fe12eSEd Maste #include <errno.h>
32a85fe12eSEd Maste #include <fcntl.h>
33a85fe12eSEd Maste #include <getopt.h>
34a85fe12eSEd Maste #include <libelftc.h>
35a85fe12eSEd Maste #include <stdio.h>
36a85fe12eSEd Maste #include <stdlib.h>
37a85fe12eSEd Maste #include <string.h>
38a85fe12eSEd Maste #include <unistd.h>
39a85fe12eSEd Maste 
40a85fe12eSEd Maste #include "elfcopy.h"
41a85fe12eSEd Maste 
42*3ef90571SEd Maste ELFTC_VCSID("$Id: main.c 3216 2015-05-23 21:16:36Z kaiwang27 $");
43a85fe12eSEd Maste 
44a85fe12eSEd Maste enum options
45a85fe12eSEd Maste {
46a85fe12eSEd Maste 	ECP_ADD_GNU_DEBUGLINK,
47a85fe12eSEd Maste 	ECP_ADD_SECTION,
48a85fe12eSEd Maste 	ECP_CHANGE_ADDR,
49a85fe12eSEd Maste 	ECP_CHANGE_SEC_ADDR,
50a85fe12eSEd Maste 	ECP_CHANGE_SEC_LMA,
51a85fe12eSEd Maste 	ECP_CHANGE_SEC_VMA,
52a85fe12eSEd Maste 	ECP_CHANGE_START,
53a85fe12eSEd Maste 	ECP_CHANGE_WARN,
54a85fe12eSEd Maste 	ECP_GAP_FILL,
55a85fe12eSEd Maste 	ECP_GLOBALIZE_SYMBOL,
56a85fe12eSEd Maste 	ECP_GLOBALIZE_SYMBOLS,
57a85fe12eSEd Maste 	ECP_KEEP_SYMBOLS,
58a85fe12eSEd Maste 	ECP_KEEP_GLOBAL_SYMBOLS,
5967d97fe7SEd Maste 	ECP_LOCALIZE_HIDDEN,
60a85fe12eSEd Maste 	ECP_LOCALIZE_SYMBOLS,
61a85fe12eSEd Maste 	ECP_NO_CHANGE_WARN,
62a85fe12eSEd Maste 	ECP_ONLY_DEBUG,
6367d97fe7SEd Maste 	ECP_ONLY_DWO,
64a85fe12eSEd Maste 	ECP_PAD_TO,
65a85fe12eSEd Maste 	ECP_PREFIX_ALLOC,
66a85fe12eSEd Maste 	ECP_PREFIX_SEC,
67a85fe12eSEd Maste 	ECP_PREFIX_SYM,
68a85fe12eSEd Maste 	ECP_REDEF_SYMBOL,
69a85fe12eSEd Maste 	ECP_REDEF_SYMBOLS,
70a85fe12eSEd Maste 	ECP_RENAME_SECTION,
71a85fe12eSEd Maste 	ECP_SET_OSABI,
72a85fe12eSEd Maste 	ECP_SET_SEC_FLAGS,
73a85fe12eSEd Maste 	ECP_SET_START,
74a85fe12eSEd Maste 	ECP_SREC_FORCE_S3,
75a85fe12eSEd Maste 	ECP_SREC_LEN,
7667d97fe7SEd Maste 	ECP_STRIP_DWO,
77a85fe12eSEd Maste 	ECP_STRIP_SYMBOLS,
78a85fe12eSEd Maste 	ECP_STRIP_UNNEEDED,
79a85fe12eSEd Maste 	ECP_WEAKEN_ALL,
80a85fe12eSEd Maste 	ECP_WEAKEN_SYMBOLS
81a85fe12eSEd Maste };
82a85fe12eSEd Maste 
83a85fe12eSEd Maste static struct option mcs_longopts[] =
84a85fe12eSEd Maste {
85a85fe12eSEd Maste 	{ "help", no_argument, NULL, 'h' },
86a85fe12eSEd Maste 	{ "version", no_argument, NULL, 'V' },
87a85fe12eSEd Maste 	{ NULL, 0, NULL, 0 }
88a85fe12eSEd Maste };
89a85fe12eSEd Maste 
90a85fe12eSEd Maste static struct option strip_longopts[] =
91a85fe12eSEd Maste {
92a85fe12eSEd Maste 	{"discard-all", no_argument, NULL, 'x'},
93a85fe12eSEd Maste 	{"discard-locals", no_argument, NULL, 'X'},
94a85fe12eSEd Maste 	{"help", no_argument, NULL, 'h'},
95a85fe12eSEd Maste 	{"input-target", required_argument, NULL, 'I'},
96a85fe12eSEd Maste 	{"keep-symbol", required_argument, NULL, 'K'},
97a85fe12eSEd Maste 	{"only-keep-debug", no_argument, NULL, ECP_ONLY_DEBUG},
98a85fe12eSEd Maste 	{"output-file", required_argument, NULL, 'o'},
99a85fe12eSEd Maste 	{"output-target", required_argument, NULL, 'O'},
100a85fe12eSEd Maste 	{"preserve-dates", no_argument, NULL, 'p'},
101a85fe12eSEd Maste 	{"remove-section", required_argument, NULL, 'R'},
102a85fe12eSEd Maste 	{"strip-all", no_argument, NULL, 's'},
103a85fe12eSEd Maste 	{"strip-debug", no_argument, NULL, 'S'},
104a85fe12eSEd Maste 	{"strip-symbol", required_argument, NULL, 'N'},
105a85fe12eSEd Maste 	{"strip-unneeded", no_argument, NULL, ECP_STRIP_UNNEEDED},
106a85fe12eSEd Maste 	{"version", no_argument, NULL, 'V'},
107a85fe12eSEd Maste 	{"wildcard", no_argument, NULL, 'w'},
108a85fe12eSEd Maste 	{NULL, 0, NULL, 0}
109a85fe12eSEd Maste };
110a85fe12eSEd Maste 
111a85fe12eSEd Maste static struct option elfcopy_longopts[] =
112a85fe12eSEd Maste {
113a85fe12eSEd Maste 	{"add-gnu-debuglink", required_argument, NULL, ECP_ADD_GNU_DEBUGLINK},
114a85fe12eSEd Maste 	{"add-section", required_argument, NULL, ECP_ADD_SECTION},
115a85fe12eSEd Maste 	{"adjust-section-vma", required_argument, NULL, ECP_CHANGE_SEC_ADDR},
116a85fe12eSEd Maste 	{"adjust-vma", required_argument, NULL, ECP_CHANGE_ADDR},
117a85fe12eSEd Maste 	{"adjust-start", required_argument, NULL, ECP_CHANGE_START},
118a85fe12eSEd Maste 	{"adjust-warnings", no_argument, NULL, ECP_CHANGE_WARN},
119a85fe12eSEd Maste 	{"binary-architecture", required_argument, NULL, 'B'},
120a85fe12eSEd Maste 	{"change-addresses", required_argument, NULL, ECP_CHANGE_ADDR},
121a85fe12eSEd Maste 	{"change-section-address", required_argument, NULL,
122a85fe12eSEd Maste 	 ECP_CHANGE_SEC_ADDR},
123a85fe12eSEd Maste 	{"change-section-lma", required_argument, NULL, ECP_CHANGE_SEC_LMA},
124a85fe12eSEd Maste 	{"change-section-vma", required_argument, NULL, ECP_CHANGE_SEC_VMA},
125a85fe12eSEd Maste 	{"change-start", required_argument, NULL, ECP_CHANGE_START},
126a85fe12eSEd Maste 	{"change-warnings", no_argument, NULL, ECP_CHANGE_WARN},
127a85fe12eSEd Maste 	{"discard-all", no_argument, NULL, 'x'},
128a85fe12eSEd Maste 	{"discard-locals", no_argument, NULL, 'X'},
12967d97fe7SEd Maste 	{"extract-dwo", no_argument, NULL, ECP_ONLY_DWO},
130a85fe12eSEd Maste 	{"gap-fill", required_argument, NULL, ECP_GAP_FILL},
131a85fe12eSEd Maste 	{"globalize-symbol", required_argument, NULL, ECP_GLOBALIZE_SYMBOL},
132a85fe12eSEd Maste 	{"globalize-symbols", required_argument, NULL, ECP_GLOBALIZE_SYMBOLS},
133a85fe12eSEd Maste 	{"help", no_argument, NULL, 'h'},
134a85fe12eSEd Maste 	{"input-target", required_argument, NULL, 'I'},
135a85fe12eSEd Maste 	{"keep-symbol", required_argument, NULL, 'K'},
136a85fe12eSEd Maste 	{"keep-symbols", required_argument, NULL, ECP_KEEP_SYMBOLS},
137a85fe12eSEd Maste 	{"keep-global-symbol", required_argument, NULL, 'G'},
138a85fe12eSEd Maste 	{"keep-global-symbols", required_argument, NULL,
139a85fe12eSEd Maste 	 ECP_KEEP_GLOBAL_SYMBOLS},
14067d97fe7SEd Maste 	{"localize-hidden", no_argument, NULL, ECP_LOCALIZE_HIDDEN},
141a85fe12eSEd Maste 	{"localize-symbol", required_argument, NULL, 'L'},
142a85fe12eSEd Maste 	{"localize-symbols", required_argument, NULL, ECP_LOCALIZE_SYMBOLS},
143a85fe12eSEd Maste 	{"no-adjust-warnings", no_argument, NULL, ECP_NO_CHANGE_WARN},
144a85fe12eSEd Maste 	{"no-change-warnings", no_argument, NULL, ECP_NO_CHANGE_WARN},
145a85fe12eSEd Maste 	{"only-keep-debug", no_argument, NULL, ECP_ONLY_DEBUG},
146a85fe12eSEd Maste 	{"only-section", required_argument, NULL, 'j'},
147a85fe12eSEd Maste 	{"osabi", required_argument, NULL, ECP_SET_OSABI},
148a85fe12eSEd Maste 	{"output-target", required_argument, NULL, 'O'},
149a85fe12eSEd Maste 	{"pad-to", required_argument, NULL, ECP_PAD_TO},
150a85fe12eSEd Maste 	{"preserve-dates", no_argument, NULL, 'p'},
151a85fe12eSEd Maste 	{"prefix-alloc-sections", required_argument, NULL, ECP_PREFIX_ALLOC},
152a85fe12eSEd Maste 	{"prefix-sections", required_argument, NULL, ECP_PREFIX_SEC},
153a85fe12eSEd Maste 	{"prefix-symbols", required_argument, NULL, ECP_PREFIX_SYM},
154a85fe12eSEd Maste 	{"redefine-sym", required_argument, NULL, ECP_REDEF_SYMBOL},
155a85fe12eSEd Maste 	{"redefine-syms", required_argument, NULL, ECP_REDEF_SYMBOLS},
156a85fe12eSEd Maste 	{"remove-section", required_argument, NULL, 'R'},
157a85fe12eSEd Maste 	{"rename-section", required_argument, NULL, ECP_RENAME_SECTION},
158a85fe12eSEd Maste 	{"set-section-flags", required_argument, NULL, ECP_SET_SEC_FLAGS},
159a85fe12eSEd Maste 	{"set-start", required_argument, NULL, ECP_SET_START},
160a85fe12eSEd Maste 	{"srec-forceS3", no_argument, NULL, ECP_SREC_FORCE_S3},
161a85fe12eSEd Maste 	{"srec-len", required_argument, NULL, ECP_SREC_LEN},
162a85fe12eSEd Maste 	{"strip-all", no_argument, NULL, 'S'},
163a85fe12eSEd Maste 	{"strip-debug", no_argument, 0, 'g'},
16467d97fe7SEd Maste 	{"strip-dwo", no_argument, NULL, ECP_STRIP_DWO},
165a85fe12eSEd Maste 	{"strip-symbol", required_argument, NULL, 'N'},
166a85fe12eSEd Maste 	{"strip-symbols", required_argument, NULL, ECP_STRIP_SYMBOLS},
167a85fe12eSEd Maste 	{"strip-unneeded", no_argument, NULL, ECP_STRIP_UNNEEDED},
168a85fe12eSEd Maste 	{"version", no_argument, NULL, 'V'},
169a85fe12eSEd Maste 	{"weaken", no_argument, NULL, ECP_WEAKEN_ALL},
170a85fe12eSEd Maste 	{"weaken-symbol", required_argument, NULL, 'W'},
171a85fe12eSEd Maste 	{"weaken-symbols", required_argument, NULL, ECP_WEAKEN_SYMBOLS},
172a85fe12eSEd Maste 	{"wildcard", no_argument, NULL, 'w'},
173a85fe12eSEd Maste 	{NULL, 0, NULL, 0}
174a85fe12eSEd Maste };
175a85fe12eSEd Maste 
176a85fe12eSEd Maste static struct {
177a85fe12eSEd Maste 	const char *name;
178a85fe12eSEd Maste 	int value;
179a85fe12eSEd Maste } sec_flags[] = {
180a85fe12eSEd Maste 	{"alloc", SF_ALLOC},
181a85fe12eSEd Maste 	{"load", SF_LOAD},
182a85fe12eSEd Maste 	{"noload", SF_NOLOAD},
183a85fe12eSEd Maste 	{"readonly", SF_READONLY},
184a85fe12eSEd Maste 	{"debug", SF_DEBUG},
185a85fe12eSEd Maste 	{"code", SF_CODE},
186a85fe12eSEd Maste 	{"data", SF_DATA},
187a85fe12eSEd Maste 	{"rom", SF_ROM},
188a85fe12eSEd Maste 	{"share", SF_SHARED},
189a85fe12eSEd Maste 	{"contents", SF_CONTENTS},
190a85fe12eSEd Maste 	{NULL, 0}
191a85fe12eSEd Maste };
192a85fe12eSEd Maste 
193a85fe12eSEd Maste static struct {
194a85fe12eSEd Maste 	const char *name;
195a85fe12eSEd Maste 	int abi;
196a85fe12eSEd Maste } osabis[] = {
197a85fe12eSEd Maste 	{"sysv", ELFOSABI_SYSV},
198a85fe12eSEd Maste 	{"hpus", ELFOSABI_HPUX},
199a85fe12eSEd Maste 	{"netbsd", ELFOSABI_NETBSD},
200a85fe12eSEd Maste 	{"linux", ELFOSABI_LINUX},
201a85fe12eSEd Maste 	{"hurd", ELFOSABI_HURD},
202a85fe12eSEd Maste 	{"86open", ELFOSABI_86OPEN},
203a85fe12eSEd Maste 	{"solaris", ELFOSABI_SOLARIS},
204a85fe12eSEd Maste 	{"aix", ELFOSABI_AIX},
205a85fe12eSEd Maste 	{"irix", ELFOSABI_IRIX},
206a85fe12eSEd Maste 	{"freebsd", ELFOSABI_FREEBSD},
207a85fe12eSEd Maste 	{"tru64", ELFOSABI_TRU64},
208a85fe12eSEd Maste 	{"modesto", ELFOSABI_MODESTO},
209a85fe12eSEd Maste 	{"openbsd", ELFOSABI_OPENBSD},
210a85fe12eSEd Maste 	{"openvms", ELFOSABI_OPENVMS},
211a85fe12eSEd Maste 	{"nsk", ELFOSABI_NSK},
212a85fe12eSEd Maste 	{"arm", ELFOSABI_ARM},
213a85fe12eSEd Maste 	{"standalone", ELFOSABI_STANDALONE},
214a85fe12eSEd Maste 	{NULL, 0}
215a85fe12eSEd Maste };
216a85fe12eSEd Maste 
217a85fe12eSEd Maste static int	copy_from_tempfile(const char *src, const char *dst,
218272a972bSEd Maste     int infd, int *outfd, int in_place);
219a85fe12eSEd Maste static void	create_file(struct elfcopy *ecp, const char *src,
220a85fe12eSEd Maste     const char *dst);
221a85fe12eSEd Maste static void	elfcopy_main(struct elfcopy *ecp, int argc, char **argv);
222a85fe12eSEd Maste static void	elfcopy_usage(void);
223a85fe12eSEd Maste static void	mcs_main(struct elfcopy *ecp, int argc, char **argv);
224a85fe12eSEd Maste static void	mcs_usage(void);
225a85fe12eSEd Maste static void	parse_sec_address_op(struct elfcopy *ecp, int optnum,
226a85fe12eSEd Maste     const char *optname, char *s);
227a85fe12eSEd Maste static void	parse_sec_flags(struct sec_action *sac, char *s);
228a85fe12eSEd Maste static void	parse_symlist_file(struct elfcopy *ecp, const char *fn,
229a85fe12eSEd Maste     unsigned int op);
230a85fe12eSEd Maste static void	print_version(void);
231a85fe12eSEd Maste static void	set_input_target(struct elfcopy *ecp, const char *target_name);
232a85fe12eSEd Maste static void	set_osabi(struct elfcopy *ecp, const char *abi);
233a85fe12eSEd Maste static void	set_output_target(struct elfcopy *ecp, const char *target_name);
234a85fe12eSEd Maste static void	strip_main(struct elfcopy *ecp, int argc, char **argv);
235a85fe12eSEd Maste static void	strip_usage(void);
236a85fe12eSEd Maste 
237a85fe12eSEd Maste /*
238a85fe12eSEd Maste  * An ELF object usually has a sturcture described by the
239a85fe12eSEd Maste  * diagram below.
240a85fe12eSEd Maste  *  _____________
241a85fe12eSEd Maste  * |             |
242a85fe12eSEd Maste  * |     NULL    | <- always a SHT_NULL section
243a85fe12eSEd Maste  * |_____________|
244a85fe12eSEd Maste  * |             |
245a85fe12eSEd Maste  * |   .interp   |
246a85fe12eSEd Maste  * |_____________|
247a85fe12eSEd Maste  * |             |
248a85fe12eSEd Maste  * |     ...     |
249a85fe12eSEd Maste  * |_____________|
250a85fe12eSEd Maste  * |             |
251a85fe12eSEd Maste  * |    .text    |
252a85fe12eSEd Maste  * |_____________|
253a85fe12eSEd Maste  * |             |
254a85fe12eSEd Maste  * |     ...     |
255a85fe12eSEd Maste  * |_____________|
256a85fe12eSEd Maste  * |             |
257a85fe12eSEd Maste  * |  .comment   | <- above(include) this: normal sections
258a85fe12eSEd Maste  * |_____________|
259a85fe12eSEd Maste  * |             |
260a85fe12eSEd Maste  * | add sections| <- unloadable sections added by --add-section
261a85fe12eSEd Maste  * |_____________|
262a85fe12eSEd Maste  * |             |
263a85fe12eSEd Maste  * |  .shstrtab  | <- section name string table
264a85fe12eSEd Maste  * |_____________|
265a85fe12eSEd Maste  * |             |
266a85fe12eSEd Maste  * |    shdrs    | <- section header table
267a85fe12eSEd Maste  * |_____________|
268a85fe12eSEd Maste  * |             |
269a85fe12eSEd Maste  * |   .symtab   | <- symbol table, if any
270a85fe12eSEd Maste  * |_____________|
271a85fe12eSEd Maste  * |             |
272a85fe12eSEd Maste  * |   .strtab   | <- symbol name string table, if any
273a85fe12eSEd Maste  * |_____________|
274a85fe12eSEd Maste  * |             |
275a85fe12eSEd Maste  * |  .rel.text  | <- relocation info for .o files.
276a85fe12eSEd Maste  * |_____________|
277a85fe12eSEd Maste  */
278a85fe12eSEd Maste void
279a85fe12eSEd Maste create_elf(struct elfcopy *ecp)
280a85fe12eSEd Maste {
281a85fe12eSEd Maste 	struct section	*shtab;
282a85fe12eSEd Maste 	GElf_Ehdr	 ieh;
283a85fe12eSEd Maste 	GElf_Ehdr	 oeh;
284a85fe12eSEd Maste 	size_t		 ishnum;
285a85fe12eSEd Maste 
286a85fe12eSEd Maste 	ecp->flags |= SYMTAB_INTACT;
287a85fe12eSEd Maste 
288a85fe12eSEd Maste 	/* Create EHDR. */
289a85fe12eSEd Maste 	if (gelf_getehdr(ecp->ein, &ieh) == NULL)
290a85fe12eSEd Maste 		errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
291a85fe12eSEd Maste 		    elf_errmsg(-1));
292a85fe12eSEd Maste 	if ((ecp->iec = gelf_getclass(ecp->ein)) == ELFCLASSNONE)
293a85fe12eSEd Maste 		errx(EXIT_FAILURE, "getclass() failed: %s",
294a85fe12eSEd Maste 		    elf_errmsg(-1));
295a85fe12eSEd Maste 
296a85fe12eSEd Maste 	if (ecp->oec == ELFCLASSNONE)
297a85fe12eSEd Maste 		ecp->oec = ecp->iec;
298a85fe12eSEd Maste 	if (ecp->oed == ELFDATANONE)
299a85fe12eSEd Maste 		ecp->oed = ieh.e_ident[EI_DATA];
300a85fe12eSEd Maste 
301a85fe12eSEd Maste 	if (gelf_newehdr(ecp->eout, ecp->oec) == NULL)
302a85fe12eSEd Maste 		errx(EXIT_FAILURE, "gelf_newehdr failed: %s",
303a85fe12eSEd Maste 		    elf_errmsg(-1));
304a85fe12eSEd Maste 	if (gelf_getehdr(ecp->eout, &oeh) == NULL)
305a85fe12eSEd Maste 		errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
306a85fe12eSEd Maste 		    elf_errmsg(-1));
307a85fe12eSEd Maste 
308a85fe12eSEd Maste 	memcpy(oeh.e_ident, ieh.e_ident, sizeof(ieh.e_ident));
309a85fe12eSEd Maste 	oeh.e_ident[EI_CLASS] = ecp->oec;
310a85fe12eSEd Maste 	oeh.e_ident[EI_DATA]  = ecp->oed;
311a85fe12eSEd Maste 	if (ecp->abi != -1)
312a85fe12eSEd Maste 		oeh.e_ident[EI_OSABI] = ecp->abi;
313a85fe12eSEd Maste 	oeh.e_flags	      = ieh.e_flags;
314a85fe12eSEd Maste 	oeh.e_machine	      = ieh.e_machine;
315a85fe12eSEd Maste 	oeh.e_type	      = ieh.e_type;
316a85fe12eSEd Maste 	oeh.e_entry	      = ieh.e_entry;
317a85fe12eSEd Maste 	oeh.e_version	      = ieh.e_version;
318a85fe12eSEd Maste 
319a85fe12eSEd Maste 	if (ieh.e_type == ET_EXEC)
320a85fe12eSEd Maste 		ecp->flags |= EXECUTABLE;
321a85fe12eSEd Maste 	else if (ieh.e_type == ET_DYN)
322a85fe12eSEd Maste 		ecp->flags |= DYNAMIC;
323a85fe12eSEd Maste 	else if (ieh.e_type == ET_REL)
324a85fe12eSEd Maste 		ecp->flags |= RELOCATABLE;
325a85fe12eSEd Maste 	else
326a85fe12eSEd Maste 		errx(EXIT_FAILURE, "unsupported e_type");
327a85fe12eSEd Maste 
328a85fe12eSEd Maste 	if (!elf_getshnum(ecp->ein, &ishnum))
329a85fe12eSEd Maste 		errx(EXIT_FAILURE, "elf_getshnum failed: %s",
330a85fe12eSEd Maste 		    elf_errmsg(-1));
331a85fe12eSEd Maste 	if (ishnum > 0 && (ecp->secndx = calloc(ishnum,
332a85fe12eSEd Maste 	    sizeof(*ecp->secndx))) == NULL)
333a85fe12eSEd Maste 		err(EXIT_FAILURE, "calloc failed");
334a85fe12eSEd Maste 
335a85fe12eSEd Maste 	/* Read input object program header. */
336a85fe12eSEd Maste 	setup_phdr(ecp);
337a85fe12eSEd Maste 
338a85fe12eSEd Maste 	/*
339a85fe12eSEd Maste 	 * Scan of input sections: we iterate through sections from input
340a85fe12eSEd Maste 	 * object, skip sections need to be stripped, allot Elf_Scn and
341a85fe12eSEd Maste 	 * create internal section structure for sections we want.
342a85fe12eSEd Maste 	 * (i.e., determine output sections)
343a85fe12eSEd Maste 	 */
344a85fe12eSEd Maste 	create_scn(ecp);
345a85fe12eSEd Maste 
346a85fe12eSEd Maste 	/* Apply section address changes, if any. */
347a85fe12eSEd Maste 	adjust_addr(ecp);
348a85fe12eSEd Maste 
349a85fe12eSEd Maste 	/*
350a85fe12eSEd Maste 	 * Determine if the symbol table needs to be changed based on
351a85fe12eSEd Maste 	 * command line options.
352a85fe12eSEd Maste 	 */
353a85fe12eSEd Maste 	if (ecp->strip == STRIP_DEBUG ||
354a85fe12eSEd Maste 	    ecp->strip == STRIP_UNNEEDED ||
355a85fe12eSEd Maste 	    ecp->flags & WEAKEN_ALL ||
35667d97fe7SEd Maste 	    ecp->flags & LOCALIZE_HIDDEN ||
357a85fe12eSEd Maste 	    ecp->flags & DISCARD_LOCAL ||
358a85fe12eSEd Maste 	    ecp->flags & DISCARD_LLABEL ||
359a85fe12eSEd Maste 	    ecp->prefix_sym != NULL ||
360a85fe12eSEd Maste 	    !STAILQ_EMPTY(&ecp->v_symop))
361a85fe12eSEd Maste 		ecp->flags &= ~SYMTAB_INTACT;
362a85fe12eSEd Maste 
363a85fe12eSEd Maste 	/*
364a85fe12eSEd Maste 	 * Create symbol table. Symbols are filtered or stripped according to
365a85fe12eSEd Maste 	 * command line args specified by user, and later updated for the new
366a85fe12eSEd Maste 	 * layout of sections in the output object.
367a85fe12eSEd Maste 	 */
368a85fe12eSEd Maste 	if ((ecp->flags & SYMTAB_EXIST) != 0)
369a85fe12eSEd Maste 		create_symtab(ecp);
370a85fe12eSEd Maste 
371a85fe12eSEd Maste 	/*
372a85fe12eSEd Maste 	 * First processing of output sections: at this stage we copy the
373a85fe12eSEd Maste 	 * content of each section from input to output object.  Section
374a85fe12eSEd Maste 	 * content will be modified and printed (mcs) if need. Also content of
375a85fe12eSEd Maste 	 * relocation section probably will be filtered and updated according
376a85fe12eSEd Maste 	 * to symbol table changes.
377a85fe12eSEd Maste 	 */
378a85fe12eSEd Maste 	copy_content(ecp);
379a85fe12eSEd Maste 
380a85fe12eSEd Maste 	/*
381a85fe12eSEd Maste 	 * Write the underlying ehdr. Note that it should be called
382a85fe12eSEd Maste 	 * before elf_setshstrndx() since it will overwrite e->e_shstrndx.
383a85fe12eSEd Maste 	 */
384a85fe12eSEd Maste 	if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
385a85fe12eSEd Maste 		errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
386a85fe12eSEd Maste 		    elf_errmsg(-1));
387a85fe12eSEd Maste 
388a85fe12eSEd Maste 	/* Generate section name string table (.shstrtab). */
389a85fe12eSEd Maste 	set_shstrtab(ecp);
390a85fe12eSEd Maste 
391a85fe12eSEd Maste 	/*
392a85fe12eSEd Maste 	 * Second processing of output sections: Update section headers.
393a85fe12eSEd Maste 	 * At this stage we set name string index, update st_link and st_info
394a85fe12eSEd Maste 	 * for output sections.
395a85fe12eSEd Maste 	 */
396a85fe12eSEd Maste 	update_shdr(ecp, 1);
397a85fe12eSEd Maste 
398a85fe12eSEd Maste 	/* Renew oeh to get the updated e_shstrndx. */
399a85fe12eSEd Maste 	if (gelf_getehdr(ecp->eout, &oeh) == NULL)
400a85fe12eSEd Maste 		errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
401a85fe12eSEd Maste 		    elf_errmsg(-1));
402a85fe12eSEd Maste 
403a85fe12eSEd Maste 	/*
404a85fe12eSEd Maste 	 * Insert SHDR table into the internal section list as a "pseudo"
405a85fe12eSEd Maste 	 * section, so later it will get sorted and resynced just as "normal"
406a85fe12eSEd Maste 	 * sections.
407*3ef90571SEd Maste 	 *
408*3ef90571SEd Maste 	 * Under FreeBSD, Binutils objcopy always put the section header
409*3ef90571SEd Maste 	 * at the end of all the sections. We want to do the same here.
410*3ef90571SEd Maste 	 *
411*3ef90571SEd Maste 	 * However, note that the behaviour is still different with Binutils:
412*3ef90571SEd Maste 	 * elfcopy checks the FreeBSD OSABI tag to tell whether it needs to
413*3ef90571SEd Maste 	 * move the section headers, while Binutils is probably configured
414*3ef90571SEd Maste 	 * this way when it's compiled on FreeBSD.
415a85fe12eSEd Maste 	 */
416*3ef90571SEd Maste 	if (oeh.e_ident[EI_OSABI] == ELFOSABI_FREEBSD)
417*3ef90571SEd Maste 		shtab = insert_shtab(ecp, 1);
418*3ef90571SEd Maste 	else
419a85fe12eSEd Maste 		shtab = insert_shtab(ecp, 0);
420a85fe12eSEd Maste 
421a85fe12eSEd Maste 	/*
422a85fe12eSEd Maste 	 * Resync section offsets in the output object. This is needed
423a85fe12eSEd Maste 	 * because probably sections are modified or new sections are added,
424a85fe12eSEd Maste 	 * as a result overlap/gap might appears.
425a85fe12eSEd Maste 	 */
426a85fe12eSEd Maste 	resync_sections(ecp);
427a85fe12eSEd Maste 
428a85fe12eSEd Maste 	/* Store SHDR offset in EHDR. */
429a85fe12eSEd Maste 	oeh.e_shoff = shtab->off;
430a85fe12eSEd Maste 
431a85fe12eSEd Maste 	/* Put program header table immediately after the Elf header. */
432a85fe12eSEd Maste 	if (ecp->ophnum > 0) {
433a85fe12eSEd Maste 		oeh.e_phoff = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT);
434a85fe12eSEd Maste 		if (oeh.e_phoff == 0)
435a85fe12eSEd Maste 			errx(EXIT_FAILURE, "gelf_fsize() failed: %s",
436a85fe12eSEd Maste 			    elf_errmsg(-1));
437a85fe12eSEd Maste 	}
438a85fe12eSEd Maste 
439a85fe12eSEd Maste 	/*
440a85fe12eSEd Maste 	 * Update ELF object entry point if requested.
441a85fe12eSEd Maste 	 */
442a85fe12eSEd Maste 	if (ecp->change_addr != 0)
443a85fe12eSEd Maste 		oeh.e_entry += ecp->change_addr;
444a85fe12eSEd Maste 	if (ecp->flags & SET_START)
445a85fe12eSEd Maste 		oeh.e_entry = ecp->set_start;
446a85fe12eSEd Maste 	if (ecp->change_start != 0)
447a85fe12eSEd Maste 		oeh.e_entry += ecp->change_start;
448a85fe12eSEd Maste 
449a85fe12eSEd Maste 	/*
450a85fe12eSEd Maste 	 * Update ehdr again before we call elf_update(), since we
451a85fe12eSEd Maste 	 * modified e_shoff and e_phoff.
452a85fe12eSEd Maste 	 */
453a85fe12eSEd Maste 	if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
454a85fe12eSEd Maste 		errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
455a85fe12eSEd Maste 		    elf_errmsg(-1));
456a85fe12eSEd Maste 
457a85fe12eSEd Maste 	if (ecp->ophnum > 0)
458a85fe12eSEd Maste 		copy_phdr(ecp);
459a85fe12eSEd Maste 
460a85fe12eSEd Maste 	/* Write out the output elf object. */
461a85fe12eSEd Maste 	if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
462a85fe12eSEd Maste 		errx(EXIT_FAILURE, "elf_update() failed: %s",
463a85fe12eSEd Maste 		    elf_errmsg(-1));
464a85fe12eSEd Maste 
465a85fe12eSEd Maste 	/* Release allocated resource. */
466a85fe12eSEd Maste 	free_elf(ecp);
467a85fe12eSEd Maste }
468a85fe12eSEd Maste 
469a85fe12eSEd Maste void
470a85fe12eSEd Maste free_elf(struct elfcopy *ecp)
471a85fe12eSEd Maste {
472a85fe12eSEd Maste 	struct segment	*seg, *seg_temp;
473a85fe12eSEd Maste 	struct section	*sec, *sec_temp;
474a85fe12eSEd Maste 
475a85fe12eSEd Maste 	/* Free internal segment list. */
476a85fe12eSEd Maste 	if (!STAILQ_EMPTY(&ecp->v_seg)) {
477a85fe12eSEd Maste 		STAILQ_FOREACH_SAFE(seg, &ecp->v_seg, seg_list, seg_temp) {
478a85fe12eSEd Maste 			STAILQ_REMOVE(&ecp->v_seg, seg, segment, seg_list);
479a85fe12eSEd Maste 			free(seg);
480a85fe12eSEd Maste 		}
481a85fe12eSEd Maste 	}
482a85fe12eSEd Maste 
483a85fe12eSEd Maste 	/* Free symbol table buffers. */
484a85fe12eSEd Maste 	free_symtab(ecp);
485a85fe12eSEd Maste 
486a85fe12eSEd Maste 	/* Free internal section list. */
487a85fe12eSEd Maste 	if (!TAILQ_EMPTY(&ecp->v_sec)) {
488a85fe12eSEd Maste 		TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) {
489a85fe12eSEd Maste 			TAILQ_REMOVE(&ecp->v_sec, sec, sec_list);
490a85fe12eSEd Maste 			if (sec->buf != NULL)
491a85fe12eSEd Maste 				free(sec->buf);
492a85fe12eSEd Maste 			if (sec->newname != NULL)
493a85fe12eSEd Maste 				free(sec->newname);
494a85fe12eSEd Maste 			if (sec->pad != NULL)
495a85fe12eSEd Maste 				free(sec->pad);
496a85fe12eSEd Maste 			free(sec);
497a85fe12eSEd Maste 		}
498a85fe12eSEd Maste 	}
499*3ef90571SEd Maste 
500*3ef90571SEd Maste 	if (ecp->secndx != NULL) {
501*3ef90571SEd Maste 		free(ecp->secndx);
502*3ef90571SEd Maste 		ecp->secndx = NULL;
503*3ef90571SEd Maste 	}
504a85fe12eSEd Maste }
505a85fe12eSEd Maste 
506a85fe12eSEd Maste /* Create a temporary file. */
507a85fe12eSEd Maste void
508a85fe12eSEd Maste create_tempfile(char **fn, int *fd)
509a85fe12eSEd Maste {
510a85fe12eSEd Maste 	const char	*tmpdir;
511a85fe12eSEd Maste 	char		*cp, *tmpf;
512a85fe12eSEd Maste 	size_t		 tlen, plen;
513a85fe12eSEd Maste 
514a85fe12eSEd Maste #define	_TEMPFILE "ecp.XXXXXXXX"
515a85fe12eSEd Maste #define	_TEMPFILEPATH "/tmp/ecp.XXXXXXXX"
516a85fe12eSEd Maste 
517a85fe12eSEd Maste 	if (fn == NULL || fd == NULL)
518a85fe12eSEd Maste 		return;
519a85fe12eSEd Maste 	/* Repect TMPDIR environment variable. */
520a85fe12eSEd Maste 	tmpdir = getenv("TMPDIR");
521a85fe12eSEd Maste 	if (tmpdir != NULL && *tmpdir != '\0') {
522a85fe12eSEd Maste 		tlen = strlen(tmpdir);
523a85fe12eSEd Maste 		plen = strlen(_TEMPFILE);
524a85fe12eSEd Maste 		tmpf = malloc(tlen + plen + 2);
525a85fe12eSEd Maste 		if (tmpf == NULL)
526a85fe12eSEd Maste 			err(EXIT_FAILURE, "malloc failed");
527a85fe12eSEd Maste 		strncpy(tmpf, tmpdir, tlen);
528a85fe12eSEd Maste 		cp = &tmpf[tlen - 1];
529a85fe12eSEd Maste 		if (*cp++ != '/')
530a85fe12eSEd Maste 			*cp++ = '/';
531a85fe12eSEd Maste 		strncpy(cp, _TEMPFILE, plen);
532a85fe12eSEd Maste 		cp[plen] = '\0';
533a85fe12eSEd Maste 	} else {
534a85fe12eSEd Maste 		tmpf = strdup(_TEMPFILEPATH);
535a85fe12eSEd Maste 		if (tmpf == NULL)
536a85fe12eSEd Maste 			err(EXIT_FAILURE, "strdup failed");
537a85fe12eSEd Maste 	}
538a85fe12eSEd Maste 	if ((*fd = mkstemp(tmpf)) == -1)
539a85fe12eSEd Maste 		err(EXIT_FAILURE, "mkstemp %s failed", tmpf);
540a85fe12eSEd Maste 	if (fchmod(*fd, 0644) == -1)
541a85fe12eSEd Maste 		err(EXIT_FAILURE, "fchmod %s failed", tmpf);
542a85fe12eSEd Maste 	*fn = tmpf;
543a85fe12eSEd Maste 
544a85fe12eSEd Maste #undef _TEMPFILE
545a85fe12eSEd Maste #undef _TEMPFILEPATH
546a85fe12eSEd Maste }
547a85fe12eSEd Maste 
548272a972bSEd Maste /*
549272a972bSEd Maste  * Copy temporary file with path src and file descriptor infd to path dst.
550272a972bSEd Maste  * If in_place is set act as if editing the file in place, avoiding rename()
551272a972bSEd Maste  * to preserve hard and symbolic links. Output file remains open, with file
552272a972bSEd Maste  * descriptor returned in outfd.
553272a972bSEd Maste  */
554a85fe12eSEd Maste static int
555272a972bSEd Maste copy_from_tempfile(const char *src, const char *dst, int infd, int *outfd,
556272a972bSEd Maste     int in_place)
557a85fe12eSEd Maste {
558a85fe12eSEd Maste 	int tmpfd;
559a85fe12eSEd Maste 
560a85fe12eSEd Maste 	/*
561a85fe12eSEd Maste 	 * First, check if we can use rename().
562a85fe12eSEd Maste 	 */
563272a972bSEd Maste 	if (in_place == 0) {
564a85fe12eSEd Maste 		if (rename(src, dst) >= 0) {
565a85fe12eSEd Maste 			*outfd = infd;
566a85fe12eSEd Maste 			return (0);
567a85fe12eSEd Maste 		} else if (errno != EXDEV)
568a85fe12eSEd Maste 			return (-1);
569a85fe12eSEd Maste 
570a85fe12eSEd Maste 		/*
571a85fe12eSEd Maste 		 * If the rename() failed due to 'src' and 'dst' residing in
572a85fe12eSEd Maste 		 * two different file systems, invoke a helper function in
573a85fe12eSEd Maste 		 * libelftc to do the copy.
574a85fe12eSEd Maste 		 */
575a85fe12eSEd Maste 
576a85fe12eSEd Maste 		if (unlink(dst) < 0)
577a85fe12eSEd Maste 			return (-1);
578272a972bSEd Maste 	}
579a85fe12eSEd Maste 
580272a972bSEd Maste 	if ((tmpfd = open(dst, O_CREAT | O_TRUNC | O_WRONLY, 0755)) < 0)
581a85fe12eSEd Maste 		return (-1);
582a85fe12eSEd Maste 
583a85fe12eSEd Maste 	if (elftc_copyfile(infd, tmpfd) < 0)
584a85fe12eSEd Maste 		return (-1);
585a85fe12eSEd Maste 
586a85fe12eSEd Maste 	/*
587a85fe12eSEd Maste 	 * Remove the temporary file from the file system
588a85fe12eSEd Maste 	 * namespace, and close its file descriptor.
589a85fe12eSEd Maste 	 */
590a85fe12eSEd Maste 	if (unlink(src) < 0)
591a85fe12eSEd Maste 		return (-1);
592a85fe12eSEd Maste 
593a85fe12eSEd Maste 	(void) close(infd);
594a85fe12eSEd Maste 
595a85fe12eSEd Maste 	/*
596a85fe12eSEd Maste 	 * Return the file descriptor for the destination.
597a85fe12eSEd Maste 	 */
598a85fe12eSEd Maste 	*outfd = tmpfd;
599a85fe12eSEd Maste 
600a85fe12eSEd Maste 	return (0);
601a85fe12eSEd Maste }
602a85fe12eSEd Maste 
603a85fe12eSEd Maste static void
604a85fe12eSEd Maste create_file(struct elfcopy *ecp, const char *src, const char *dst)
605a85fe12eSEd Maste {
606a85fe12eSEd Maste 	struct stat	 sb;
607a85fe12eSEd Maste 	char		*tempfile, *elftemp;
608a85fe12eSEd Maste 	int		 efd, ifd, ofd, ofd0, tfd;
609272a972bSEd Maste 	int		 in_place;
610a85fe12eSEd Maste 
611a85fe12eSEd Maste 	tempfile = NULL;
612a85fe12eSEd Maste 
613a85fe12eSEd Maste 	if (src == NULL)
614a85fe12eSEd Maste 		errx(EXIT_FAILURE, "internal: src == NULL");
615a85fe12eSEd Maste 	if ((ifd = open(src, O_RDONLY)) == -1)
616a85fe12eSEd Maste 		err(EXIT_FAILURE, "open %s failed", src);
617a85fe12eSEd Maste 
618a85fe12eSEd Maste 	if (fstat(ifd, &sb) == -1)
619a85fe12eSEd Maste 		err(EXIT_FAILURE, "fstat %s failed", src);
620a85fe12eSEd Maste 
621a85fe12eSEd Maste 	if (dst == NULL)
622a85fe12eSEd Maste 		create_tempfile(&tempfile, &ofd);
623a85fe12eSEd Maste 	else
624a85fe12eSEd Maste 		if ((ofd = open(dst, O_RDWR|O_CREAT, 0755)) == -1)
625a85fe12eSEd Maste 			err(EXIT_FAILURE, "open %s failed", dst);
626a85fe12eSEd Maste 
627a85fe12eSEd Maste #ifndef LIBELF_AR
628a85fe12eSEd Maste 	/* Detect and process ar(1) archive using libarchive. */
629a85fe12eSEd Maste 	if (ac_detect_ar(ifd)) {
630a85fe12eSEd Maste 		ac_create_ar(ecp, ifd, ofd);
631a85fe12eSEd Maste 		goto copy_done;
632a85fe12eSEd Maste 	}
633a85fe12eSEd Maste #endif
634a85fe12eSEd Maste 
635a85fe12eSEd Maste 	if (lseek(ifd, 0, SEEK_SET) < 0)
636a85fe12eSEd Maste 		err(EXIT_FAILURE, "lseek failed");
637a85fe12eSEd Maste 
638a85fe12eSEd Maste 	/*
639a85fe12eSEd Maste 	 * If input object is not ELF file, convert it to an intermediate
640a85fe12eSEd Maste 	 * ELF object before processing.
641a85fe12eSEd Maste 	 */
642a85fe12eSEd Maste 	if (ecp->itf != ETF_ELF) {
643a85fe12eSEd Maste 		create_tempfile(&elftemp, &efd);
644a85fe12eSEd Maste 		if ((ecp->eout = elf_begin(efd, ELF_C_WRITE, NULL)) == NULL)
645a85fe12eSEd Maste 			errx(EXIT_FAILURE, "elf_begin() failed: %s",
646a85fe12eSEd Maste 			    elf_errmsg(-1));
647a85fe12eSEd Maste 		elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT);
648a85fe12eSEd Maste 		if (ecp->itf == ETF_BINARY)
649a85fe12eSEd Maste 			create_elf_from_binary(ecp, ifd, src);
650a85fe12eSEd Maste 		else if (ecp->itf == ETF_IHEX)
651a85fe12eSEd Maste 			create_elf_from_ihex(ecp, ifd);
652a85fe12eSEd Maste 		else if (ecp->itf == ETF_SREC)
653a85fe12eSEd Maste 			create_elf_from_srec(ecp, ifd);
654a85fe12eSEd Maste 		else
655a85fe12eSEd Maste 			errx(EXIT_FAILURE, "Internal: invalid target flavour");
656a85fe12eSEd Maste 		elf_end(ecp->eout);
657a85fe12eSEd Maste 
658a85fe12eSEd Maste 		/* Open intermediate ELF object as new input object. */
659a85fe12eSEd Maste 		close(ifd);
660a85fe12eSEd Maste 		if ((ifd = open(elftemp, O_RDONLY)) == -1)
661a85fe12eSEd Maste 			err(EXIT_FAILURE, "open %s failed", src);
662a85fe12eSEd Maste 		close(efd);
663a85fe12eSEd Maste 		free(elftemp);
664a85fe12eSEd Maste 	}
665a85fe12eSEd Maste 
666a85fe12eSEd Maste 	if ((ecp->ein = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
667a85fe12eSEd Maste 		errx(EXIT_FAILURE, "elf_begin() failed: %s",
668a85fe12eSEd Maste 		    elf_errmsg(-1));
669a85fe12eSEd Maste 
670a85fe12eSEd Maste 	switch (elf_kind(ecp->ein)) {
671a85fe12eSEd Maste 	case ELF_K_NONE:
672a85fe12eSEd Maste 		errx(EXIT_FAILURE, "file format not recognized");
673a85fe12eSEd Maste 	case ELF_K_ELF:
674a85fe12eSEd Maste 		if ((ecp->eout = elf_begin(ofd, ELF_C_WRITE, NULL)) == NULL)
675a85fe12eSEd Maste 			errx(EXIT_FAILURE, "elf_begin() failed: %s",
676a85fe12eSEd Maste 			    elf_errmsg(-1));
677a85fe12eSEd Maste 
678a85fe12eSEd Maste 		/* elfcopy(1) manage ELF layout by itself. */
679a85fe12eSEd Maste 		elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT);
680a85fe12eSEd Maste 
681a85fe12eSEd Maste 		/*
682a85fe12eSEd Maste 		 * Create output ELF object.
683a85fe12eSEd Maste 		 */
684a85fe12eSEd Maste 		create_elf(ecp);
685a85fe12eSEd Maste 		elf_end(ecp->eout);
686a85fe12eSEd Maste 
687a85fe12eSEd Maste 		/*
688a85fe12eSEd Maste 		 * Convert the output ELF object to binary/srec/ihex if need.
689a85fe12eSEd Maste 		 */
690a85fe12eSEd Maste 		if (ecp->otf != ETF_ELF) {
691a85fe12eSEd Maste 			/*
692a85fe12eSEd Maste 			 * Create (another) tempfile for binary/srec/ihex
693a85fe12eSEd Maste 			 * output object.
694a85fe12eSEd Maste 			 */
695a85fe12eSEd Maste 			if (tempfile != NULL) {
696a85fe12eSEd Maste 				if (unlink(tempfile) < 0)
697a85fe12eSEd Maste 					err(EXIT_FAILURE, "unlink %s failed",
698a85fe12eSEd Maste 					    tempfile);
699a85fe12eSEd Maste 				free(tempfile);
700a85fe12eSEd Maste 			}
701a85fe12eSEd Maste 			create_tempfile(&tempfile, &ofd0);
702a85fe12eSEd Maste 
703a85fe12eSEd Maste 
704a85fe12eSEd Maste 			/*
705a85fe12eSEd Maste 			 * Rewind the file descriptor being processed.
706a85fe12eSEd Maste 			 */
707a85fe12eSEd Maste 			if (lseek(ofd, 0, SEEK_SET) < 0)
708a85fe12eSEd Maste 				err(EXIT_FAILURE,
709a85fe12eSEd Maste 				    "lseek failed for the output object");
710a85fe12eSEd Maste 
711a85fe12eSEd Maste 			/*
712a85fe12eSEd Maste 			 * Call flavour-specific conversion routine.
713a85fe12eSEd Maste 			 */
714a85fe12eSEd Maste 			switch (ecp->otf) {
715a85fe12eSEd Maste 			case ETF_BINARY:
716a85fe12eSEd Maste 				create_binary(ofd, ofd0);
717a85fe12eSEd Maste 				break;
718a85fe12eSEd Maste 			case ETF_IHEX:
719a85fe12eSEd Maste 				create_ihex(ofd, ofd0);
720a85fe12eSEd Maste 				break;
721a85fe12eSEd Maste 			case ETF_SREC:
722a85fe12eSEd Maste 				create_srec(ecp, ofd, ofd0,
723a85fe12eSEd Maste 				    dst != NULL ? dst : src);
724a85fe12eSEd Maste 				break;
725a85fe12eSEd Maste 			default:
726a85fe12eSEd Maste 				errx(EXIT_FAILURE, "Internal: unsupported"
727a85fe12eSEd Maste 				    " output flavour %d", ecp->oec);
728a85fe12eSEd Maste 			}
729a85fe12eSEd Maste 
730a85fe12eSEd Maste 			close(ofd);
731a85fe12eSEd Maste 			ofd = ofd0;
732a85fe12eSEd Maste 		}
733a85fe12eSEd Maste 
734a85fe12eSEd Maste 		break;
735a85fe12eSEd Maste 
736a85fe12eSEd Maste 	case ELF_K_AR:
737a85fe12eSEd Maste 		/* XXX: Not yet supported. */
738a85fe12eSEd Maste 		break;
739a85fe12eSEd Maste 	default:
740a85fe12eSEd Maste 		errx(EXIT_FAILURE, "file format not supported");
741a85fe12eSEd Maste 	}
742a85fe12eSEd Maste 
743a85fe12eSEd Maste 	elf_end(ecp->ein);
744a85fe12eSEd Maste 
745a85fe12eSEd Maste #ifndef LIBELF_AR
746a85fe12eSEd Maste copy_done:
747a85fe12eSEd Maste #endif
748a85fe12eSEd Maste 
749a85fe12eSEd Maste 	if (tempfile != NULL) {
750272a972bSEd Maste 		in_place = 0;
751272a972bSEd Maste 		if (dst == NULL) {
752a85fe12eSEd Maste 			dst = src;
753272a972bSEd Maste 			if (lstat(dst, &sb) != -1 &&
754272a972bSEd Maste 			    (sb.st_nlink > 1 || S_ISLNK(sb.st_mode)))
755272a972bSEd Maste 				in_place = 1;
756272a972bSEd Maste 		}
757a85fe12eSEd Maste 
758272a972bSEd Maste 		if (copy_from_tempfile(tempfile, dst, ofd, &tfd, in_place) < 0)
759a85fe12eSEd Maste 			err(EXIT_FAILURE, "creation of %s failed", dst);
760a85fe12eSEd Maste 
761a85fe12eSEd Maste 		free(tempfile);
762a85fe12eSEd Maste 		tempfile = NULL;
763a85fe12eSEd Maste 
764a85fe12eSEd Maste 		ofd = tfd;
765a85fe12eSEd Maste 	}
766a85fe12eSEd Maste 
767a85fe12eSEd Maste 	if (strcmp(dst, "/dev/null") && fchmod(ofd, sb.st_mode) == -1)
768a85fe12eSEd Maste 		err(EXIT_FAILURE, "fchmod %s failed", dst);
769a85fe12eSEd Maste 
770a85fe12eSEd Maste 	if ((ecp->flags & PRESERVE_DATE) &&
771a85fe12eSEd Maste 	    elftc_set_timestamps(dst, &sb) < 0)
772a85fe12eSEd Maste 		err(EXIT_FAILURE, "setting timestamps failed");
773a85fe12eSEd Maste 
774a85fe12eSEd Maste 	close(ifd);
775a85fe12eSEd Maste 	close(ofd);
776a85fe12eSEd Maste }
777a85fe12eSEd Maste 
778a85fe12eSEd Maste static void
779a85fe12eSEd Maste elfcopy_main(struct elfcopy *ecp, int argc, char **argv)
780a85fe12eSEd Maste {
781a85fe12eSEd Maste 	struct sec_action	*sac;
782a85fe12eSEd Maste 	const char		*infile, *outfile;
783a85fe12eSEd Maste 	char			*fn, *s;
784a85fe12eSEd Maste 	int			 opt;
785a85fe12eSEd Maste 
786a85fe12eSEd Maste 	while ((opt = getopt_long(argc, argv, "dB:gG:I:j:K:L:N:O:pR:s:SwW:xXV",
787a85fe12eSEd Maste 	    elfcopy_longopts, NULL)) != -1) {
788a85fe12eSEd Maste 		switch(opt) {
789a85fe12eSEd Maste 		case 'B':
790a85fe12eSEd Maste 			/* ignored */
791a85fe12eSEd Maste 			break;
792a85fe12eSEd Maste 		case 'R':
793a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
794a85fe12eSEd Maste 			if (sac->copy != 0)
795a85fe12eSEd Maste 				errx(EXIT_FAILURE,
796a85fe12eSEd Maste 				    "both copy and remove specified");
797a85fe12eSEd Maste 			sac->remove = 1;
798a85fe12eSEd Maste 			ecp->flags |= SEC_REMOVE;
799a85fe12eSEd Maste 			break;
800a85fe12eSEd Maste 		case 'S':
801a85fe12eSEd Maste 			ecp->strip = STRIP_ALL;
802a85fe12eSEd Maste 			break;
803a85fe12eSEd Maste 		case 'g':
804a85fe12eSEd Maste 			ecp->strip = STRIP_DEBUG;
805a85fe12eSEd Maste 			break;
806a85fe12eSEd Maste 		case 'G':
807a85fe12eSEd Maste 			ecp->flags |= KEEP_GLOBAL;
808a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEPG);
809a85fe12eSEd Maste 			break;
810a85fe12eSEd Maste 		case 'I':
811a85fe12eSEd Maste 		case 's':
812a85fe12eSEd Maste 			set_input_target(ecp, optarg);
813a85fe12eSEd Maste 			break;
814a85fe12eSEd Maste 		case 'j':
815a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
816a85fe12eSEd Maste 			if (sac->remove != 0)
817a85fe12eSEd Maste 				errx(EXIT_FAILURE,
818a85fe12eSEd Maste 				    "both copy and remove specified");
819a85fe12eSEd Maste 			sac->copy = 1;
820a85fe12eSEd Maste 			ecp->flags |= SEC_COPY;
821a85fe12eSEd Maste 			break;
822a85fe12eSEd Maste 		case 'K':
823a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP);
824a85fe12eSEd Maste 			break;
825a85fe12eSEd Maste 		case 'L':
826a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_LOCALIZE);
827a85fe12eSEd Maste 			break;
828a85fe12eSEd Maste 		case 'N':
829a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP);
830a85fe12eSEd Maste 			break;
831a85fe12eSEd Maste 		case 'O':
832a85fe12eSEd Maste 			set_output_target(ecp, optarg);
833a85fe12eSEd Maste 			break;
834a85fe12eSEd Maste 		case 'p':
835a85fe12eSEd Maste 			ecp->flags |= PRESERVE_DATE;
836a85fe12eSEd Maste 			break;
837a85fe12eSEd Maste 		case 'V':
838a85fe12eSEd Maste 			print_version();
839a85fe12eSEd Maste 			break;
840a85fe12eSEd Maste 		case 'w':
841a85fe12eSEd Maste 			ecp->flags |= WILDCARD;
842a85fe12eSEd Maste 			break;
843a85fe12eSEd Maste 		case 'W':
844a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_WEAKEN);
845a85fe12eSEd Maste 			break;
846a85fe12eSEd Maste 		case 'x':
847a85fe12eSEd Maste 			ecp->flags |= DISCARD_LOCAL;
848a85fe12eSEd Maste 			break;
849a85fe12eSEd Maste 		case 'X':
850a85fe12eSEd Maste 			ecp->flags |= DISCARD_LLABEL;
851a85fe12eSEd Maste 			break;
852a85fe12eSEd Maste 		case ECP_ADD_GNU_DEBUGLINK:
853a85fe12eSEd Maste 			ecp->debuglink = optarg;
854a85fe12eSEd Maste 			break;
855a85fe12eSEd Maste 		case ECP_ADD_SECTION:
856a85fe12eSEd Maste 			add_section(ecp, optarg);
857a85fe12eSEd Maste 			break;
858a85fe12eSEd Maste 		case ECP_CHANGE_ADDR:
859a85fe12eSEd Maste 			ecp->change_addr = (int64_t) strtoll(optarg, NULL, 0);
860a85fe12eSEd Maste 			break;
861a85fe12eSEd Maste 		case ECP_CHANGE_SEC_ADDR:
862a85fe12eSEd Maste 			parse_sec_address_op(ecp, opt, "--change-section-addr",
863a85fe12eSEd Maste 			    optarg);
864a85fe12eSEd Maste 			break;
865a85fe12eSEd Maste 		case ECP_CHANGE_SEC_LMA:
866a85fe12eSEd Maste 			parse_sec_address_op(ecp, opt, "--change-section-lma",
867a85fe12eSEd Maste 			    optarg);
868a85fe12eSEd Maste 			break;
869a85fe12eSEd Maste 		case ECP_CHANGE_SEC_VMA:
870a85fe12eSEd Maste 			parse_sec_address_op(ecp, opt, "--change-section-vma",
871a85fe12eSEd Maste 			    optarg);
872a85fe12eSEd Maste 			break;
873a85fe12eSEd Maste 		case ECP_CHANGE_START:
874a85fe12eSEd Maste 			ecp->change_start = (int64_t) strtoll(optarg, NULL, 0);
875a85fe12eSEd Maste 			break;
876a85fe12eSEd Maste 		case ECP_CHANGE_WARN:
877a85fe12eSEd Maste 			/* default */
878a85fe12eSEd Maste 			break;
879a85fe12eSEd Maste 		case ECP_GAP_FILL:
880a85fe12eSEd Maste 			ecp->fill = (uint8_t) strtoul(optarg, NULL, 0);
881a85fe12eSEd Maste 			ecp->flags |= GAP_FILL;
882a85fe12eSEd Maste 			break;
883a85fe12eSEd Maste 		case ECP_GLOBALIZE_SYMBOL:
884a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_GLOBALIZE);
885a85fe12eSEd Maste 			break;
886a85fe12eSEd Maste 		case ECP_GLOBALIZE_SYMBOLS:
887a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_GLOBALIZE);
888a85fe12eSEd Maste 			break;
889a85fe12eSEd Maste 		case ECP_KEEP_SYMBOLS:
890a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_KEEP);
891a85fe12eSEd Maste 			break;
892a85fe12eSEd Maste 		case ECP_KEEP_GLOBAL_SYMBOLS:
893a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_KEEPG);
894a85fe12eSEd Maste 			break;
89567d97fe7SEd Maste 		case ECP_LOCALIZE_HIDDEN:
89667d97fe7SEd Maste 			ecp->flags |= LOCALIZE_HIDDEN;
89767d97fe7SEd Maste 			break;
898a85fe12eSEd Maste 		case ECP_LOCALIZE_SYMBOLS:
899a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_LOCALIZE);
900a85fe12eSEd Maste 			break;
901a85fe12eSEd Maste 		case ECP_NO_CHANGE_WARN:
902a85fe12eSEd Maste 			ecp->flags |= NO_CHANGE_WARN;
903a85fe12eSEd Maste 			break;
904a85fe12eSEd Maste 		case ECP_ONLY_DEBUG:
905a85fe12eSEd Maste 			ecp->strip = STRIP_NONDEBUG;
906a85fe12eSEd Maste 			break;
90767d97fe7SEd Maste 		case ECP_ONLY_DWO:
90867d97fe7SEd Maste 			ecp->strip = STRIP_NONDWO;
90967d97fe7SEd Maste 			break;
910a85fe12eSEd Maste 		case ECP_PAD_TO:
911a85fe12eSEd Maste 			ecp->pad_to = (uint64_t) strtoull(optarg, NULL, 0);
912a85fe12eSEd Maste 			break;
913a85fe12eSEd Maste 		case ECP_PREFIX_ALLOC:
914a85fe12eSEd Maste 			ecp->prefix_alloc = optarg;
915a85fe12eSEd Maste 			break;
916a85fe12eSEd Maste 		case ECP_PREFIX_SEC:
917a85fe12eSEd Maste 			ecp->prefix_sec = optarg;
918a85fe12eSEd Maste 			break;
919a85fe12eSEd Maste 		case ECP_PREFIX_SYM:
920a85fe12eSEd Maste 			ecp->prefix_sym = optarg;
921a85fe12eSEd Maste 			break;
922a85fe12eSEd Maste 		case ECP_REDEF_SYMBOL:
923a85fe12eSEd Maste 			if ((s = strchr(optarg, '=')) == NULL)
924a85fe12eSEd Maste 				errx(EXIT_FAILURE,
925a85fe12eSEd Maste 				    "illegal format for --redefine-sym");
926a85fe12eSEd Maste 			*s++ = '\0';
927a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, s, SYMOP_REDEF);
928a85fe12eSEd Maste 			break;
929a85fe12eSEd Maste 		case ECP_REDEF_SYMBOLS:
930a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_REDEF);
931a85fe12eSEd Maste 			break;
932a85fe12eSEd Maste 		case ECP_RENAME_SECTION:
933a85fe12eSEd Maste 			if ((fn = strchr(optarg, '=')) == NULL)
934a85fe12eSEd Maste 				errx(EXIT_FAILURE,
935a85fe12eSEd Maste 				    "illegal format for --rename-section");
936a85fe12eSEd Maste 			*fn++ = '\0';
937a85fe12eSEd Maste 
938a85fe12eSEd Maste 			/* Check for optional flags. */
939a85fe12eSEd Maste 			if ((s = strchr(fn, ',')) != NULL)
940a85fe12eSEd Maste 				*s++ = '\0';
941a85fe12eSEd Maste 
942a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
943a85fe12eSEd Maste 			sac->rename = 1;
944a85fe12eSEd Maste 			sac->newname = fn;
945a85fe12eSEd Maste 			if (s != NULL)
946a85fe12eSEd Maste 				parse_sec_flags(sac, s);
947a85fe12eSEd Maste 			break;
948a85fe12eSEd Maste 		case ECP_SET_OSABI:
949a85fe12eSEd Maste 			set_osabi(ecp, optarg);
950a85fe12eSEd Maste 			break;
951a85fe12eSEd Maste 		case ECP_SET_SEC_FLAGS:
952a85fe12eSEd Maste 			if ((s = strchr(optarg, '=')) == NULL)
953a85fe12eSEd Maste 				errx(EXIT_FAILURE,
954a85fe12eSEd Maste 				    "illegal format for --set-section-flags");
955a85fe12eSEd Maste 			*s++ = '\0';
956a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
957a85fe12eSEd Maste 			parse_sec_flags(sac, s);
958a85fe12eSEd Maste 			break;
959a85fe12eSEd Maste 		case ECP_SET_START:
960a85fe12eSEd Maste 			ecp->flags |= SET_START;
961a85fe12eSEd Maste 			ecp->set_start = (uint64_t) strtoull(optarg, NULL, 0);
962a85fe12eSEd Maste 			break;
963a85fe12eSEd Maste 		case ECP_SREC_FORCE_S3:
964a85fe12eSEd Maste 			ecp->flags |= SREC_FORCE_S3;
965a85fe12eSEd Maste 			break;
966a85fe12eSEd Maste 		case ECP_SREC_LEN:
967a85fe12eSEd Maste 			ecp->flags |= SREC_FORCE_LEN;
968a85fe12eSEd Maste 			ecp->srec_len = strtoul(optarg, NULL, 0);
969a85fe12eSEd Maste 			break;
97067d97fe7SEd Maste 		case ECP_STRIP_DWO:
97167d97fe7SEd Maste 			ecp->strip = STRIP_DWO;
97267d97fe7SEd Maste 			break;
973a85fe12eSEd Maste 		case ECP_STRIP_SYMBOLS:
974a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_STRIP);
975a85fe12eSEd Maste 			break;
976a85fe12eSEd Maste 		case ECP_STRIP_UNNEEDED:
977a85fe12eSEd Maste 			ecp->strip = STRIP_UNNEEDED;
978a85fe12eSEd Maste 			break;
979a85fe12eSEd Maste 		case ECP_WEAKEN_ALL:
980a85fe12eSEd Maste 			ecp->flags |= WEAKEN_ALL;
981a85fe12eSEd Maste 			break;
982a85fe12eSEd Maste 		case ECP_WEAKEN_SYMBOLS:
983a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_WEAKEN);
984a85fe12eSEd Maste 			break;
985a85fe12eSEd Maste 		default:
986a85fe12eSEd Maste 			elfcopy_usage();
987a85fe12eSEd Maste 		}
988a85fe12eSEd Maste 	}
989a85fe12eSEd Maste 
990a85fe12eSEd Maste 	if (optind == argc || optind + 2 < argc)
991a85fe12eSEd Maste 		elfcopy_usage();
992a85fe12eSEd Maste 
993a85fe12eSEd Maste 	infile = argv[optind];
994a85fe12eSEd Maste 	outfile = NULL;
995a85fe12eSEd Maste 	if (optind + 1 < argc)
996a85fe12eSEd Maste 		outfile = argv[optind + 1];
997a85fe12eSEd Maste 
998a85fe12eSEd Maste 	create_file(ecp, infile, outfile);
999a85fe12eSEd Maste }
1000a85fe12eSEd Maste 
1001a85fe12eSEd Maste static void
1002a85fe12eSEd Maste mcs_main(struct elfcopy *ecp, int argc, char **argv)
1003a85fe12eSEd Maste {
1004a85fe12eSEd Maste 	struct sec_action	*sac;
1005a85fe12eSEd Maste 	const char		*string;
1006a85fe12eSEd Maste 	int			 append, delete, compress, name, print;
1007a85fe12eSEd Maste 	int			 opt, i;
1008a85fe12eSEd Maste 
1009a85fe12eSEd Maste 	append = delete = compress = name = print = 0;
1010a85fe12eSEd Maste 	string = NULL;
1011a85fe12eSEd Maste 	while ((opt = getopt_long(argc, argv, "a:cdhn:pV", mcs_longopts,
1012a85fe12eSEd Maste 		NULL)) != -1) {
1013a85fe12eSEd Maste 		switch(opt) {
1014a85fe12eSEd Maste 		case 'a':
1015a85fe12eSEd Maste 			append = 1;
1016a85fe12eSEd Maste 			string = optarg; /* XXX multiple -a not supported */
1017a85fe12eSEd Maste 			break;
1018a85fe12eSEd Maste 		case 'c':
1019a85fe12eSEd Maste 			compress = 1;
1020a85fe12eSEd Maste 			break;
1021a85fe12eSEd Maste 		case 'd':
1022a85fe12eSEd Maste 			delete = 1;
1023a85fe12eSEd Maste 			break;
1024a85fe12eSEd Maste 		case 'n':
1025a85fe12eSEd Maste 			name = 1;
1026a85fe12eSEd Maste 			(void)lookup_sec_act(ecp, optarg, 1);
1027a85fe12eSEd Maste 			break;
1028a85fe12eSEd Maste 		case 'p':
1029a85fe12eSEd Maste 			print = 1;
1030a85fe12eSEd Maste 			break;
1031a85fe12eSEd Maste 		case 'V':
1032a85fe12eSEd Maste 			print_version();
1033a85fe12eSEd Maste 			break;
1034a85fe12eSEd Maste 		case 'h':
1035a85fe12eSEd Maste 		default:
1036a85fe12eSEd Maste 			mcs_usage();
1037a85fe12eSEd Maste 		}
1038a85fe12eSEd Maste 	}
1039a85fe12eSEd Maste 
1040a85fe12eSEd Maste 	if (optind == argc)
1041a85fe12eSEd Maste 		mcs_usage();
1042a85fe12eSEd Maste 
1043a85fe12eSEd Maste 	/* Must specify one operation at least. */
1044a85fe12eSEd Maste 	if (!append && !compress && !delete && !print)
1045a85fe12eSEd Maste 		mcs_usage();
1046a85fe12eSEd Maste 
1047a85fe12eSEd Maste 	/*
1048a85fe12eSEd Maste 	 * If we are going to delete, ignore other operations. This is
1049a85fe12eSEd Maste 	 * different from the Solaris implementation, which can print
1050a85fe12eSEd Maste 	 * and delete a section at the same time, for example. Also, this
1051a85fe12eSEd Maste 	 * implementation do not respect the order between operations that
1052a85fe12eSEd Maste 	 * user specified, i.e., "mcs -pc a.out" equals to "mcs -cp a.out".
1053a85fe12eSEd Maste 	 */
1054a85fe12eSEd Maste 	if (delete) {
1055a85fe12eSEd Maste 		append = compress = print = 0;
1056a85fe12eSEd Maste 		ecp->flags |= SEC_REMOVE;
1057a85fe12eSEd Maste 	}
1058a85fe12eSEd Maste 	if (append)
1059a85fe12eSEd Maste 		ecp->flags |= SEC_APPEND;
1060a85fe12eSEd Maste 	if (compress)
1061a85fe12eSEd Maste 		ecp->flags |= SEC_COMPRESS;
1062a85fe12eSEd Maste 	if (print)
1063a85fe12eSEd Maste 		ecp->flags |= SEC_PRINT;
1064a85fe12eSEd Maste 
1065a85fe12eSEd Maste 	/* .comment is the default section to operate on. */
1066a85fe12eSEd Maste 	if (!name)
1067a85fe12eSEd Maste 		(void)lookup_sec_act(ecp, ".comment", 1);
1068a85fe12eSEd Maste 
1069a85fe12eSEd Maste 	STAILQ_FOREACH(sac, &ecp->v_sac, sac_list) {
1070a85fe12eSEd Maste 		sac->append = append;
1071a85fe12eSEd Maste 		sac->compress = compress;
1072a85fe12eSEd Maste 		sac->print = print;
1073a85fe12eSEd Maste 		sac->remove = delete;
1074a85fe12eSEd Maste 		sac->string = string;
1075a85fe12eSEd Maste 	}
1076a85fe12eSEd Maste 
1077a85fe12eSEd Maste 	for (i = optind; i < argc; i++) {
1078a85fe12eSEd Maste 		/* If only -p is specified, output to /dev/null */
1079a85fe12eSEd Maste 		if (print && !append && !compress && !delete)
1080a85fe12eSEd Maste 			create_file(ecp, argv[i], "/dev/null");
1081a85fe12eSEd Maste 		else
1082a85fe12eSEd Maste 			create_file(ecp, argv[i], NULL);
1083a85fe12eSEd Maste 	}
1084a85fe12eSEd Maste }
1085a85fe12eSEd Maste 
1086a85fe12eSEd Maste static void
1087a85fe12eSEd Maste strip_main(struct elfcopy *ecp, int argc, char **argv)
1088a85fe12eSEd Maste {
1089a85fe12eSEd Maste 	struct sec_action	*sac;
1090a85fe12eSEd Maste 	const char		*outfile;
1091a85fe12eSEd Maste 	int			 opt;
1092a85fe12eSEd Maste 	int			 i;
1093a85fe12eSEd Maste 
1094a85fe12eSEd Maste 	outfile = NULL;
1095a85fe12eSEd Maste 	while ((opt = getopt_long(argc, argv, "hI:K:N:o:O:pR:sSdgVxXw",
1096a85fe12eSEd Maste 	    strip_longopts, NULL)) != -1) {
1097a85fe12eSEd Maste 		switch(opt) {
1098a85fe12eSEd Maste 		case 'R':
1099a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
1100a85fe12eSEd Maste 			sac->remove = 1;
1101a85fe12eSEd Maste 			ecp->flags |= SEC_REMOVE;
1102a85fe12eSEd Maste 			break;
1103a85fe12eSEd Maste 		case 's':
1104a85fe12eSEd Maste 			ecp->strip = STRIP_ALL;
1105a85fe12eSEd Maste 			break;
1106a85fe12eSEd Maste 		case 'S':
1107a85fe12eSEd Maste 		case 'g':
1108a85fe12eSEd Maste 		case 'd':
1109a85fe12eSEd Maste 			ecp->strip = STRIP_DEBUG;
1110a85fe12eSEd Maste 			break;
1111a85fe12eSEd Maste 		case 'I':
1112a85fe12eSEd Maste 			/* ignored */
1113a85fe12eSEd Maste 			break;
1114a85fe12eSEd Maste 		case 'K':
1115a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP);
1116a85fe12eSEd Maste 			break;
1117a85fe12eSEd Maste 		case 'N':
1118a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP);
1119a85fe12eSEd Maste 			break;
1120a85fe12eSEd Maste 		case 'o':
1121a85fe12eSEd Maste 			outfile = optarg;
1122a85fe12eSEd Maste 			break;
1123a85fe12eSEd Maste 		case 'O':
1124a85fe12eSEd Maste 			set_output_target(ecp, optarg);
1125a85fe12eSEd Maste 			break;
1126a85fe12eSEd Maste 		case 'p':
1127a85fe12eSEd Maste 			ecp->flags |= PRESERVE_DATE;
1128a85fe12eSEd Maste 			break;
1129a85fe12eSEd Maste 		case 'V':
1130a85fe12eSEd Maste 			print_version();
1131a85fe12eSEd Maste 			break;
1132a85fe12eSEd Maste 		case 'w':
1133a85fe12eSEd Maste 			ecp->flags |= WILDCARD;
1134a85fe12eSEd Maste 			break;
1135a85fe12eSEd Maste 		case 'x':
1136a85fe12eSEd Maste 			ecp->flags |= DISCARD_LOCAL;
1137a85fe12eSEd Maste 			break;
1138a85fe12eSEd Maste 		case 'X':
1139a85fe12eSEd Maste 			ecp->flags |= DISCARD_LLABEL;
1140a85fe12eSEd Maste 			break;
1141a85fe12eSEd Maste 		case ECP_ONLY_DEBUG:
1142a85fe12eSEd Maste 			ecp->strip = STRIP_NONDEBUG;
1143a85fe12eSEd Maste 			break;
1144a85fe12eSEd Maste 		case ECP_STRIP_UNNEEDED:
1145a85fe12eSEd Maste 			ecp->strip = STRIP_UNNEEDED;
1146a85fe12eSEd Maste 			break;
1147a85fe12eSEd Maste 		case 'h':
1148a85fe12eSEd Maste 		default:
1149a85fe12eSEd Maste 			strip_usage();
1150a85fe12eSEd Maste 		}
1151a85fe12eSEd Maste 	}
1152a85fe12eSEd Maste 
1153a85fe12eSEd Maste 	if (ecp->strip == 0 &&
1154a85fe12eSEd Maste 	    ((ecp->flags & DISCARD_LOCAL) == 0) &&
1155a356a1f5SEd Maste 	    ((ecp->flags & DISCARD_LLABEL) == 0) &&
1156a356a1f5SEd Maste 	    lookup_symop_list(ecp, NULL, SYMOP_STRIP) == NULL)
1157a85fe12eSEd Maste 		ecp->strip = STRIP_ALL;
1158a85fe12eSEd Maste 	if (optind == argc)
1159a85fe12eSEd Maste 		strip_usage();
1160a85fe12eSEd Maste 
1161a85fe12eSEd Maste 	for (i = optind; i < argc; i++)
1162a85fe12eSEd Maste 		create_file(ecp, argv[i], outfile);
1163a85fe12eSEd Maste }
1164a85fe12eSEd Maste 
1165a85fe12eSEd Maste static void
1166a85fe12eSEd Maste parse_sec_flags(struct sec_action *sac, char *s)
1167a85fe12eSEd Maste {
1168a85fe12eSEd Maste 	const char	*flag;
1169a85fe12eSEd Maste 	int		 found, i;
1170a85fe12eSEd Maste 
1171a85fe12eSEd Maste 	for (flag = strtok(s, ","); flag; flag = strtok(NULL, ",")) {
1172a85fe12eSEd Maste 		found = 0;
1173a85fe12eSEd Maste 		for (i = 0; sec_flags[i].name != NULL; i++)
1174a85fe12eSEd Maste 			if (strcasecmp(sec_flags[i].name, flag) == 0) {
1175a85fe12eSEd Maste 				sac->flags |= sec_flags[i].value;
1176a85fe12eSEd Maste 				found = 1;
1177a85fe12eSEd Maste 				break;
1178a85fe12eSEd Maste 			}
1179a85fe12eSEd Maste 		if (!found)
1180a85fe12eSEd Maste 			errx(EXIT_FAILURE, "unrecognized section flag %s",
1181a85fe12eSEd Maste 			    flag);
1182a85fe12eSEd Maste 	}
1183a85fe12eSEd Maste }
1184a85fe12eSEd Maste 
1185a85fe12eSEd Maste static void
1186a85fe12eSEd Maste parse_sec_address_op(struct elfcopy *ecp, int optnum, const char *optname,
1187a85fe12eSEd Maste     char *s)
1188a85fe12eSEd Maste {
1189a85fe12eSEd Maste 	struct sec_action	*sac;
1190a85fe12eSEd Maste 	const char		*name;
1191a85fe12eSEd Maste 	char			*v;
1192a85fe12eSEd Maste 	char			 op;
1193a85fe12eSEd Maste 
1194a85fe12eSEd Maste 	name = v = s;
1195a85fe12eSEd Maste 	do {
1196a85fe12eSEd Maste 		v++;
1197a85fe12eSEd Maste 	} while (*v != '\0' && *v != '=' && *v != '+' && *v != '-');
1198a85fe12eSEd Maste 	if (*v == '\0' || *(v + 1) == '\0')
1199a85fe12eSEd Maste 		errx(EXIT_FAILURE, "invalid format for %s", optname);
1200a85fe12eSEd Maste 	op = *v;
1201a85fe12eSEd Maste 	*v++ = '\0';
1202a85fe12eSEd Maste 	sac = lookup_sec_act(ecp, name, 1);
1203a85fe12eSEd Maste 	switch (op) {
1204a85fe12eSEd Maste 	case '=':
1205a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_LMA ||
1206a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR) {
1207a85fe12eSEd Maste 			sac->setlma = 1;
1208a85fe12eSEd Maste 			sac->lma = (uint64_t) strtoull(v, NULL, 0);
1209a85fe12eSEd Maste 		}
1210a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_VMA ||
1211a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR) {
1212a85fe12eSEd Maste 			sac->setvma = 1;
1213a85fe12eSEd Maste 			sac->vma = (uint64_t) strtoull(v, NULL, 0);
1214a85fe12eSEd Maste 		}
1215a85fe12eSEd Maste 		break;
1216a85fe12eSEd Maste 	case '+':
1217a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_LMA ||
1218a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR)
1219a85fe12eSEd Maste 			sac->lma_adjust = (int64_t) strtoll(v, NULL, 0);
1220a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_VMA ||
1221a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR)
1222a85fe12eSEd Maste 			sac->vma_adjust = (int64_t) strtoll(v, NULL, 0);
1223a85fe12eSEd Maste 		break;
1224a85fe12eSEd Maste 	case '-':
1225a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_LMA ||
1226a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR)
1227a85fe12eSEd Maste 			sac->lma_adjust = (int64_t) -strtoll(v, NULL, 0);
1228a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_VMA ||
1229a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR)
1230a85fe12eSEd Maste 			sac->vma_adjust = (int64_t) -strtoll(v, NULL, 0);
1231a85fe12eSEd Maste 		break;
1232a85fe12eSEd Maste 	default:
1233a85fe12eSEd Maste 		break;
1234a85fe12eSEd Maste 	}
1235a85fe12eSEd Maste }
1236a85fe12eSEd Maste 
1237a85fe12eSEd Maste static void
1238a85fe12eSEd Maste parse_symlist_file(struct elfcopy *ecp, const char *fn, unsigned int op)
1239a85fe12eSEd Maste {
1240a85fe12eSEd Maste 	struct symfile	*sf;
1241a85fe12eSEd Maste 	struct stat	 sb;
1242a85fe12eSEd Maste 	FILE		*fp;
1243a85fe12eSEd Maste 	char		*data, *p, *line, *end, *e, *n;
1244a85fe12eSEd Maste 
1245a85fe12eSEd Maste 	if (stat(fn, &sb) == -1)
1246a85fe12eSEd Maste 		err(EXIT_FAILURE, "stat %s failed", fn);
1247a85fe12eSEd Maste 
1248a85fe12eSEd Maste 	/* Check if we already read and processed this file. */
1249a85fe12eSEd Maste 	STAILQ_FOREACH(sf, &ecp->v_symfile, symfile_list) {
1250a85fe12eSEd Maste 		if (sf->dev == sb.st_dev && sf->ino == sb.st_ino)
1251a85fe12eSEd Maste 			goto process_symfile;
1252a85fe12eSEd Maste 	}
1253a85fe12eSEd Maste 
1254a85fe12eSEd Maste 	if ((fp = fopen(fn, "r")) == NULL)
1255a85fe12eSEd Maste 		err(EXIT_FAILURE, "can not open %s", fn);
1256a85fe12eSEd Maste 	if ((data = malloc(sb.st_size + 1)) == NULL)
1257a85fe12eSEd Maste 		err(EXIT_FAILURE, "malloc failed");
1258a85fe12eSEd Maste 	if (fread(data, 1, sb.st_size, fp) == 0 || ferror(fp))
1259a85fe12eSEd Maste 		err(EXIT_FAILURE, "fread failed");
1260a85fe12eSEd Maste 	fclose(fp);
1261a85fe12eSEd Maste 	data[sb.st_size] = '\0';
1262a85fe12eSEd Maste 
1263a85fe12eSEd Maste 	if ((sf = calloc(1, sizeof(*sf))) == NULL)
1264a85fe12eSEd Maste 		err(EXIT_FAILURE, "malloc failed");
1265a85fe12eSEd Maste 	sf->dev = sb.st_dev;
1266a85fe12eSEd Maste 	sf->ino = sb.st_ino;
1267a85fe12eSEd Maste 	sf->size = sb.st_size + 1;
1268a85fe12eSEd Maste 	sf->data = data;
1269a85fe12eSEd Maste 
1270a85fe12eSEd Maste process_symfile:
1271a85fe12eSEd Maste 
1272a85fe12eSEd Maste 	/*
1273a85fe12eSEd Maste 	 * Basically what we do here is to convert EOL to '\0', and remove
1274a85fe12eSEd Maste 	 * leading and trailing whitespaces for each line.
1275a85fe12eSEd Maste 	 */
1276a85fe12eSEd Maste 
1277a85fe12eSEd Maste 	end = sf->data + sf->size;
1278a85fe12eSEd Maste 	line = NULL;
1279a85fe12eSEd Maste 	for(p = sf->data; p < end; p++) {
1280a85fe12eSEd Maste 		if ((*p == '\t' || *p == ' ') && line == NULL)
1281a85fe12eSEd Maste 			continue;
1282a85fe12eSEd Maste 		if (*p == '\r' || *p == '\n' || *p == '\0') {
1283a85fe12eSEd Maste 			*p = '\0';
1284a85fe12eSEd Maste 			if (line == NULL)
1285a85fe12eSEd Maste 				continue;
1286a85fe12eSEd Maste 
1287a85fe12eSEd Maste 			/* Skip comment. */
1288a85fe12eSEd Maste 			if (*line == '#') {
1289a85fe12eSEd Maste 				line = NULL;
1290a85fe12eSEd Maste 				continue;
1291a85fe12eSEd Maste 			}
1292a85fe12eSEd Maste 
1293a85fe12eSEd Maste 			e = p - 1;
1294a85fe12eSEd Maste 			while(e != line && (*e == '\t' || *e == ' '))
1295a85fe12eSEd Maste 				*e-- = '\0';
1296a85fe12eSEd Maste 			if (op != SYMOP_REDEF)
1297a85fe12eSEd Maste 				add_to_symop_list(ecp, line, NULL, op);
1298a85fe12eSEd Maste 			else {
1299a85fe12eSEd Maste 				if (strlen(line) < 3)
1300a85fe12eSEd Maste 					errx(EXIT_FAILURE,
1301a85fe12eSEd Maste 					    "illegal format for"
1302a85fe12eSEd Maste 					    " --redefine-sym");
1303a85fe12eSEd Maste 				for(n = line + 1; n < e; n++) {
1304a85fe12eSEd Maste 					if (*n == ' ' || *n == '\t') {
1305a85fe12eSEd Maste 						while(*n == ' ' || *n == '\t')
1306a85fe12eSEd Maste 							*n++ = '\0';
1307a85fe12eSEd Maste 						break;
1308a85fe12eSEd Maste 					}
1309a85fe12eSEd Maste 				}
1310a85fe12eSEd Maste 				if (n >= e)
1311a85fe12eSEd Maste 					errx(EXIT_FAILURE,
1312a85fe12eSEd Maste 					    "illegal format for"
1313a85fe12eSEd Maste 					    " --redefine-sym");
1314a85fe12eSEd Maste 				add_to_symop_list(ecp, line, n, op);
1315a85fe12eSEd Maste 			}
1316a85fe12eSEd Maste 			line = NULL;
1317a85fe12eSEd Maste 			continue;
1318a85fe12eSEd Maste 		}
1319a85fe12eSEd Maste 
1320a85fe12eSEd Maste 		if (line == NULL)
1321a85fe12eSEd Maste 			line = p;
1322a85fe12eSEd Maste 	}
1323a85fe12eSEd Maste }
1324a85fe12eSEd Maste 
1325a85fe12eSEd Maste static void
1326a85fe12eSEd Maste set_input_target(struct elfcopy *ecp, const char *target_name)
1327a85fe12eSEd Maste {
1328a85fe12eSEd Maste 	Elftc_Bfd_Target *tgt;
1329a85fe12eSEd Maste 
1330a85fe12eSEd Maste 	if ((tgt = elftc_bfd_find_target(target_name)) == NULL)
1331a85fe12eSEd Maste 		errx(EXIT_FAILURE, "%s: invalid target name", target_name);
1332a85fe12eSEd Maste 	ecp->itf = elftc_bfd_target_flavor(tgt);
1333a85fe12eSEd Maste }
1334a85fe12eSEd Maste 
1335a85fe12eSEd Maste static void
1336a85fe12eSEd Maste set_output_target(struct elfcopy *ecp, const char *target_name)
1337a85fe12eSEd Maste {
1338a85fe12eSEd Maste 	Elftc_Bfd_Target *tgt;
1339a85fe12eSEd Maste 
1340a85fe12eSEd Maste 	if ((tgt = elftc_bfd_find_target(target_name)) == NULL)
1341a85fe12eSEd Maste 		errx(EXIT_FAILURE, "%s: invalid target name", target_name);
1342a85fe12eSEd Maste 	ecp->otf = elftc_bfd_target_flavor(tgt);
1343a85fe12eSEd Maste 	if (ecp->otf == ETF_ELF) {
1344a85fe12eSEd Maste 		ecp->oec = elftc_bfd_target_class(tgt);
1345a85fe12eSEd Maste 		ecp->oed = elftc_bfd_target_byteorder(tgt);
1346a85fe12eSEd Maste 		ecp->oem = elftc_bfd_target_machine(tgt);
1347a85fe12eSEd Maste 	}
1348a85fe12eSEd Maste 	ecp->otgt = target_name;
1349a85fe12eSEd Maste }
1350a85fe12eSEd Maste 
1351a85fe12eSEd Maste static void
1352a85fe12eSEd Maste set_osabi(struct elfcopy *ecp, const char *abi)
1353a85fe12eSEd Maste {
1354a85fe12eSEd Maste 	int i, found;
1355a85fe12eSEd Maste 
1356a85fe12eSEd Maste 	found = 0;
1357a85fe12eSEd Maste 	for (i = 0; osabis[i].name != NULL; i++)
1358a85fe12eSEd Maste 		if (strcasecmp(osabis[i].name, abi) == 0) {
1359a85fe12eSEd Maste 			ecp->abi = osabis[i].abi;
1360a85fe12eSEd Maste 			found = 1;
1361a85fe12eSEd Maste 			break;
1362a85fe12eSEd Maste 		}
1363a85fe12eSEd Maste 	if (!found)
1364a85fe12eSEd Maste 		errx(EXIT_FAILURE, "unrecognized OSABI %s", abi);
1365a85fe12eSEd Maste }
1366a85fe12eSEd Maste 
1367a85fe12eSEd Maste #define	ELFCOPY_USAGE_MESSAGE	"\
1368a85fe12eSEd Maste Usage: %s [options] infile [outfile]\n\
1369a85fe12eSEd Maste   Transform an ELF object.\n\n\
1370a85fe12eSEd Maste   Options:\n\
1371a85fe12eSEd Maste   -d | -g | --strip-debug      Remove debugging information from the output.\n\
1372a85fe12eSEd Maste   -j SECTION | --only-section=SECTION\n\
1373a85fe12eSEd Maste                                Copy only the named section to the output.\n\
1374a85fe12eSEd Maste   -p | --preserve-dates        Preserve access and modification times.\n\
1375a85fe12eSEd Maste   -w | --wildcard              Use shell-style patterns to name symbols.\n\
1376a85fe12eSEd Maste   -x | --discard-all           Do not copy non-globals to the output.\n\
1377a85fe12eSEd Maste   -I FORMAT | --input-target=FORMAT\n\
1378a85fe12eSEd Maste                                (Accepted but ignored).\n\
1379a85fe12eSEd Maste   -K SYM | --keep-symbol=SYM   Copy symbol SYM to the output.\n\
1380a85fe12eSEd Maste   -L SYM | --localize-symbol=SYM\n\
1381a85fe12eSEd Maste                                Make symbol SYM local to the output file.\n\
1382a85fe12eSEd Maste   -N SYM | --strip-symbol=SYM  Do not copy symbol SYM to the output.\n\
1383a85fe12eSEd Maste   -R NAME | --remove-section=NAME\n\
1384a85fe12eSEd Maste                                Remove the named section.\n\
1385a85fe12eSEd Maste   -S | --strip-all             Remove all symbol and relocation information\n\
1386a85fe12eSEd Maste                                from the output.\n\
1387a85fe12eSEd Maste   -V | --version               Print a version identifier and exit.\n\
1388a85fe12eSEd Maste   -W SYM | --weaken-symbol=SYM Mark symbol SYM as weak in the output.\n\
1389a85fe12eSEd Maste   -X | --discard-locals        Do not copy compiler generated symbols to\n\
1390a85fe12eSEd Maste                                the output.\n\
1391a85fe12eSEd Maste   --add-section NAME=FILE      Add the contents of FILE to the ELF object as\n\
1392a85fe12eSEd Maste                                a new section named NAME.\n\
1393a85fe12eSEd Maste   --adjust-section-vma SECTION{=,+,-}VAL | \\\n\
1394a85fe12eSEd Maste     --change-section-address SECTION{=,+,-}VAL\n\
1395a85fe12eSEd Maste                                Set or adjust the VMA and the LMA of the\n\
1396a85fe12eSEd Maste                                named section by VAL.\n\
1397a85fe12eSEd Maste   --adjust-start=INCR | --change-start=INCR\n\
1398a85fe12eSEd Maste                                Add INCR to the start address for the ELF\n\
1399a85fe12eSEd Maste                                object.\n\
1400a85fe12eSEd Maste   --adjust-vma=INCR | --change-addresses=INCR\n\
1401a85fe12eSEd Maste                                Increase the VMA and LMA of all sections by\n\
1402a85fe12eSEd Maste                                INCR.\n\
1403a85fe12eSEd Maste   --adjust-warning | --change-warnings\n\
1404a85fe12eSEd Maste                                Issue warnings for non-existent sections.\n\
1405a85fe12eSEd Maste   --change-section-lma SECTION{=,+,-}VAL\n\
1406a85fe12eSEd Maste                                Set or adjust the LMA address of the named\n\
1407a85fe12eSEd Maste                                section by VAL.\n\
1408a85fe12eSEd Maste   --change-section-vma SECTION{=,+,-}VAL\n\
1409a85fe12eSEd Maste                                Set or adjust the VMA address of the named\n\
1410a85fe12eSEd Maste                                section by VAL.\n\
1411a85fe12eSEd Maste   --gap-fill=VAL               Fill the gaps between sections with bytes\n\
1412a85fe12eSEd Maste                                of value VAL.\n\
141367d97fe7SEd Maste   --localize-hidden            Make all hidden symbols local to the output\n\
141467d97fe7SEd Maste                                file.\n\
1415a85fe12eSEd Maste   --no-adjust-warning| --no-change-warnings\n\
1416a85fe12eSEd Maste                                Do not issue warnings for non-existent\n\
1417a85fe12eSEd Maste                                sections.\n\
1418a85fe12eSEd Maste   --only-keep-debug            Copy only debugging information.\n\
1419a85fe12eSEd Maste   --output-target=FORMAT       Use the specified format for the output.\n\
1420a85fe12eSEd Maste   --pad-to=ADDRESS             Pad the output object upto the given address.\n\
1421a85fe12eSEd Maste   --prefix-alloc-sections=STRING\n\
1422a85fe12eSEd Maste                                Prefix the section names of all the allocated\n\
1423a85fe12eSEd Maste                                sections with STRING.\n\
1424a85fe12eSEd Maste   --prefix-sections=STRING     Prefix the section names of all the sections\n\
1425a85fe12eSEd Maste                                with STRING.\n\
1426a85fe12eSEd Maste   --prefix-symbols=STRING      Prefix the symbol names of all the symbols\n\
1427a85fe12eSEd Maste                                with STRING.\n\
1428a85fe12eSEd Maste   --rename-section OLDNAME=NEWNAME[,FLAGS]\n\
1429a85fe12eSEd Maste                                Rename and optionally change section flags.\n\
1430a85fe12eSEd Maste   --set-section-flags SECTION=FLAGS\n\
1431a85fe12eSEd Maste                                Set section flags for the named section.\n\
1432a85fe12eSEd Maste                                Supported flags are: 'alloc', 'code',\n\
1433a85fe12eSEd Maste                                'contents', 'data', 'debug', 'load',\n\
1434a85fe12eSEd Maste                                'noload', 'readonly', 'rom', and 'shared'.\n\
1435a85fe12eSEd Maste   --set-start=ADDRESS          Set the start address of the ELF object.\n\
1436a85fe12eSEd Maste   --srec-forceS3               Only generate S3 S-Records.\n\
1437a85fe12eSEd Maste   --srec-len=LEN               Set the maximum length of a S-Record line.\n\
1438a85fe12eSEd Maste   --strip-unneeded             Do not copy relocation information.\n"
1439a85fe12eSEd Maste 
1440a85fe12eSEd Maste static void
1441a85fe12eSEd Maste elfcopy_usage(void)
1442a85fe12eSEd Maste {
1443a85fe12eSEd Maste 	(void) fprintf(stderr, ELFCOPY_USAGE_MESSAGE, ELFTC_GETPROGNAME());
1444a85fe12eSEd Maste 	exit(EXIT_FAILURE);
1445a85fe12eSEd Maste }
1446a85fe12eSEd Maste 
1447a85fe12eSEd Maste #define	MCS_USAGE_MESSAGE	"\
1448a85fe12eSEd Maste Usage: %s [options] file...\n\
1449a85fe12eSEd Maste   Manipulate the comment section in an ELF object.\n\n\
1450a85fe12eSEd Maste   Options:\n\
1451a85fe12eSEd Maste   -a STRING        Append 'STRING' to the comment section.\n\
1452a85fe12eSEd Maste   -c               Remove duplicate entries from the comment section.\n\
1453a85fe12eSEd Maste   -d               Delete the comment section.\n\
1454a85fe12eSEd Maste   -h | --help      Print a help message and exit.\n\
1455a85fe12eSEd Maste   -n NAME          Operate on the ELF section with name 'NAME'.\n\
1456a85fe12eSEd Maste   -p               Print the contents of the comment section.\n\
1457a85fe12eSEd Maste   -V | --version   Print a version identifier and exit.\n"
1458a85fe12eSEd Maste 
1459a85fe12eSEd Maste static void
1460a85fe12eSEd Maste mcs_usage(void)
1461a85fe12eSEd Maste {
1462a85fe12eSEd Maste 	(void) fprintf(stderr, MCS_USAGE_MESSAGE, ELFTC_GETPROGNAME());
1463a85fe12eSEd Maste 	exit(EXIT_FAILURE);
1464a85fe12eSEd Maste }
1465a85fe12eSEd Maste 
1466a85fe12eSEd Maste #define	STRIP_USAGE_MESSAGE	"\
1467a85fe12eSEd Maste Usage: %s [options] file...\n\
1468a85fe12eSEd Maste   Discard information from ELF objects.\n\n\
1469a85fe12eSEd Maste   Options:\n\
1470a85fe12eSEd Maste   -d | -g | -S | --strip-debug    Remove debugging symbols.\n\
1471a85fe12eSEd Maste   -h | --help                     Print a help message.\n\
1472a85fe12eSEd Maste   --only-keep-debug               Keep debugging information only.\n\
1473a85fe12eSEd Maste   -p | --preserve-dates           Preserve access and modification times.\n\
1474a85fe12eSEd Maste   -s | --strip-all                Remove all symbols.\n\
1475a85fe12eSEd Maste   --strip-unneeded                Remove symbols not needed for relocation\n\
1476a85fe12eSEd Maste                                   processing.\n\
1477a85fe12eSEd Maste   -w | --wildcard                 Use shell-style patterns to name symbols.\n\
1478a85fe12eSEd Maste   -x | --discard-all              Discard all non-global symbols.\n\
1479a85fe12eSEd Maste   -I TGT| --input-target=TGT      (Accepted, but ignored).\n\
1480a85fe12eSEd Maste   -K SYM | --keep-symbol=SYM      Keep symbol 'SYM' in the output.\n\
1481a85fe12eSEd Maste   -N SYM | --strip-symbol=SYM     Remove symbol 'SYM' from the output.\n\
1482a85fe12eSEd Maste   -O TGT | --output-target=TGT    Set the output file format to 'TGT'.\n\
1483a85fe12eSEd Maste   -R SEC | --remove-section=SEC   Remove the section named 'SEC'.\n\
1484a85fe12eSEd Maste   -V | --version                  Print a version identifier and exit.\n\
1485a85fe12eSEd Maste   -X | --discard-locals           Remove compiler-generated local symbols.\n"
1486a85fe12eSEd Maste 
1487a85fe12eSEd Maste static void
1488a85fe12eSEd Maste strip_usage(void)
1489a85fe12eSEd Maste {
1490a85fe12eSEd Maste 	(void) fprintf(stderr, STRIP_USAGE_MESSAGE, ELFTC_GETPROGNAME());
1491a85fe12eSEd Maste 	exit(EXIT_FAILURE);
1492a85fe12eSEd Maste }
1493a85fe12eSEd Maste 
1494a85fe12eSEd Maste static void
1495a85fe12eSEd Maste print_version(void)
1496a85fe12eSEd Maste {
1497a85fe12eSEd Maste 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
1498a85fe12eSEd Maste 	exit(EXIT_SUCCESS);
1499a85fe12eSEd Maste }
1500a85fe12eSEd Maste 
1501a85fe12eSEd Maste int
1502a85fe12eSEd Maste main(int argc, char **argv)
1503a85fe12eSEd Maste {
1504a85fe12eSEd Maste 	struct elfcopy *ecp;
1505a85fe12eSEd Maste 
1506a85fe12eSEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE)
1507a85fe12eSEd Maste 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
1508a85fe12eSEd Maste 		    elf_errmsg(-1));
1509a85fe12eSEd Maste 
1510a85fe12eSEd Maste 	ecp = calloc(1, sizeof(*ecp));
1511a85fe12eSEd Maste 	if (ecp == NULL)
1512a85fe12eSEd Maste 		err(EXIT_FAILURE, "calloc failed");
1513a85fe12eSEd Maste 	memset(ecp, 0, sizeof(*ecp));
1514a85fe12eSEd Maste 
1515a85fe12eSEd Maste 	ecp->itf = ecp->otf = ETF_ELF;
1516a85fe12eSEd Maste 	ecp->iec = ecp->oec = ELFCLASSNONE;
1517a85fe12eSEd Maste 	ecp->oed = ELFDATANONE;
1518a85fe12eSEd Maste 	ecp->abi = -1;
1519a85fe12eSEd Maste 	/* There is always an empty section. */
1520a85fe12eSEd Maste 	ecp->nos = 1;
1521a85fe12eSEd Maste 	ecp->fill = 0;
1522a85fe12eSEd Maste 
1523a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_seg);
1524a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_sac);
1525a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_sadd);
1526a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_symop);
1527a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_symfile);
1528a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_arobj);
1529a85fe12eSEd Maste 	TAILQ_INIT(&ecp->v_sec);
1530a85fe12eSEd Maste 
1531a85fe12eSEd Maste 	if ((ecp->progname = ELFTC_GETPROGNAME()) == NULL)
1532a85fe12eSEd Maste 		ecp->progname = "elfcopy";
1533a85fe12eSEd Maste 
1534a85fe12eSEd Maste 	if (strcmp(ecp->progname, "strip") == 0)
1535a85fe12eSEd Maste 		strip_main(ecp, argc, argv);
1536a85fe12eSEd Maste 	else if (strcmp(ecp->progname, "mcs") == 0)
1537a85fe12eSEd Maste 		mcs_main(ecp, argc, argv);
1538a85fe12eSEd Maste 	else
1539a85fe12eSEd Maste 		elfcopy_main(ecp, argc, argv);
1540a85fe12eSEd Maste 
1541a85fe12eSEd Maste 	free_sec_add(ecp);
1542a85fe12eSEd Maste 	free_sec_act(ecp);
1543a85fe12eSEd Maste 	free(ecp);
1544a85fe12eSEd Maste 
1545a85fe12eSEd Maste 	exit(EXIT_SUCCESS);
1546a85fe12eSEd Maste }
1547