xref: /freebsd/contrib/elftoolchain/elfcopy/main.c (revision 3884d6f8bd7e13a2b152fedc2d0d4f6de5ad17b8)
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 
42839529caSEd Maste ELFTC_VCSID("$Id: main.c 3399 2016-02-12 18:07:56Z emaste $");
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 
319839529caSEd Maste 	ecp->flags &= ~(EXECUTABLE | DYNAMIC | RELOCATABLE);
320a85fe12eSEd Maste 	if (ieh.e_type == ET_EXEC)
321a85fe12eSEd Maste 		ecp->flags |= EXECUTABLE;
322a85fe12eSEd Maste 	else if (ieh.e_type == ET_DYN)
323a85fe12eSEd Maste 		ecp->flags |= DYNAMIC;
324a85fe12eSEd Maste 	else if (ieh.e_type == ET_REL)
325a85fe12eSEd Maste 		ecp->flags |= RELOCATABLE;
326a85fe12eSEd Maste 	else
327a85fe12eSEd Maste 		errx(EXIT_FAILURE, "unsupported e_type");
328a85fe12eSEd Maste 
329a85fe12eSEd Maste 	if (!elf_getshnum(ecp->ein, &ishnum))
330a85fe12eSEd Maste 		errx(EXIT_FAILURE, "elf_getshnum failed: %s",
331a85fe12eSEd Maste 		    elf_errmsg(-1));
332a85fe12eSEd Maste 	if (ishnum > 0 && (ecp->secndx = calloc(ishnum,
333a85fe12eSEd Maste 	    sizeof(*ecp->secndx))) == NULL)
334a85fe12eSEd Maste 		err(EXIT_FAILURE, "calloc failed");
335a85fe12eSEd Maste 
336a85fe12eSEd Maste 	/* Read input object program header. */
337a85fe12eSEd Maste 	setup_phdr(ecp);
338a85fe12eSEd Maste 
339a85fe12eSEd Maste 	/*
340a85fe12eSEd Maste 	 * Scan of input sections: we iterate through sections from input
341a85fe12eSEd Maste 	 * object, skip sections need to be stripped, allot Elf_Scn and
342a85fe12eSEd Maste 	 * create internal section structure for sections we want.
343a85fe12eSEd Maste 	 * (i.e., determine output sections)
344a85fe12eSEd Maste 	 */
345a85fe12eSEd Maste 	create_scn(ecp);
346a85fe12eSEd Maste 
347a85fe12eSEd Maste 	/* Apply section address changes, if any. */
348a85fe12eSEd Maste 	adjust_addr(ecp);
349a85fe12eSEd Maste 
350a85fe12eSEd Maste 	/*
351a85fe12eSEd Maste 	 * Determine if the symbol table needs to be changed based on
352a85fe12eSEd Maste 	 * command line options.
353a85fe12eSEd Maste 	 */
354a85fe12eSEd Maste 	if (ecp->strip == STRIP_DEBUG ||
355a85fe12eSEd Maste 	    ecp->strip == STRIP_UNNEEDED ||
356a85fe12eSEd Maste 	    ecp->flags & WEAKEN_ALL ||
35767d97fe7SEd Maste 	    ecp->flags & LOCALIZE_HIDDEN ||
358a85fe12eSEd Maste 	    ecp->flags & DISCARD_LOCAL ||
359a85fe12eSEd Maste 	    ecp->flags & DISCARD_LLABEL ||
360a85fe12eSEd Maste 	    ecp->prefix_sym != NULL ||
361a85fe12eSEd Maste 	    !STAILQ_EMPTY(&ecp->v_symop))
362a85fe12eSEd Maste 		ecp->flags &= ~SYMTAB_INTACT;
363a85fe12eSEd Maste 
364a85fe12eSEd Maste 	/*
365a85fe12eSEd Maste 	 * Create symbol table. Symbols are filtered or stripped according to
366a85fe12eSEd Maste 	 * command line args specified by user, and later updated for the new
367a85fe12eSEd Maste 	 * layout of sections in the output object.
368a85fe12eSEd Maste 	 */
369a85fe12eSEd Maste 	if ((ecp->flags & SYMTAB_EXIST) != 0)
370a85fe12eSEd Maste 		create_symtab(ecp);
371a85fe12eSEd Maste 
372a85fe12eSEd Maste 	/*
373a85fe12eSEd Maste 	 * First processing of output sections: at this stage we copy the
374a85fe12eSEd Maste 	 * content of each section from input to output object.  Section
375a85fe12eSEd Maste 	 * content will be modified and printed (mcs) if need. Also content of
376a85fe12eSEd Maste 	 * relocation section probably will be filtered and updated according
377a85fe12eSEd Maste 	 * to symbol table changes.
378a85fe12eSEd Maste 	 */
379a85fe12eSEd Maste 	copy_content(ecp);
380a85fe12eSEd Maste 
381a85fe12eSEd Maste 	/*
382a85fe12eSEd Maste 	 * Write the underlying ehdr. Note that it should be called
383a85fe12eSEd Maste 	 * before elf_setshstrndx() since it will overwrite e->e_shstrndx.
384a85fe12eSEd Maste 	 */
385a85fe12eSEd Maste 	if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
386a85fe12eSEd Maste 		errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
387a85fe12eSEd Maste 		    elf_errmsg(-1));
388a85fe12eSEd Maste 
389a85fe12eSEd Maste 	/* Generate section name string table (.shstrtab). */
390a85fe12eSEd Maste 	set_shstrtab(ecp);
391a85fe12eSEd Maste 
392a85fe12eSEd Maste 	/*
393a85fe12eSEd Maste 	 * Second processing of output sections: Update section headers.
394a85fe12eSEd Maste 	 * At this stage we set name string index, update st_link and st_info
395a85fe12eSEd Maste 	 * for output sections.
396a85fe12eSEd Maste 	 */
397a85fe12eSEd Maste 	update_shdr(ecp, 1);
398a85fe12eSEd Maste 
399a85fe12eSEd Maste 	/* Renew oeh to get the updated e_shstrndx. */
400a85fe12eSEd Maste 	if (gelf_getehdr(ecp->eout, &oeh) == NULL)
401a85fe12eSEd Maste 		errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
402a85fe12eSEd Maste 		    elf_errmsg(-1));
403a85fe12eSEd Maste 
404a85fe12eSEd Maste 	/*
405a85fe12eSEd Maste 	 * Insert SHDR table into the internal section list as a "pseudo"
406a85fe12eSEd Maste 	 * section, so later it will get sorted and resynced just as "normal"
407a85fe12eSEd Maste 	 * sections.
4083ef90571SEd Maste 	 *
4093ef90571SEd Maste 	 * Under FreeBSD, Binutils objcopy always put the section header
4103ef90571SEd Maste 	 * at the end of all the sections. We want to do the same here.
4113ef90571SEd Maste 	 *
4123ef90571SEd Maste 	 * However, note that the behaviour is still different with Binutils:
4133ef90571SEd Maste 	 * elfcopy checks the FreeBSD OSABI tag to tell whether it needs to
4143ef90571SEd Maste 	 * move the section headers, while Binutils is probably configured
4153ef90571SEd Maste 	 * this way when it's compiled on FreeBSD.
416a85fe12eSEd Maste 	 */
4173ef90571SEd Maste 	if (oeh.e_ident[EI_OSABI] == ELFOSABI_FREEBSD)
4183ef90571SEd Maste 		shtab = insert_shtab(ecp, 1);
4193ef90571SEd Maste 	else
420a85fe12eSEd Maste 		shtab = insert_shtab(ecp, 0);
421a85fe12eSEd Maste 
422a85fe12eSEd Maste 	/*
423a85fe12eSEd Maste 	 * Resync section offsets in the output object. This is needed
424a85fe12eSEd Maste 	 * because probably sections are modified or new sections are added,
425a85fe12eSEd Maste 	 * as a result overlap/gap might appears.
426a85fe12eSEd Maste 	 */
427a85fe12eSEd Maste 	resync_sections(ecp);
428a85fe12eSEd Maste 
429a85fe12eSEd Maste 	/* Store SHDR offset in EHDR. */
430a85fe12eSEd Maste 	oeh.e_shoff = shtab->off;
431a85fe12eSEd Maste 
432a85fe12eSEd Maste 	/* Put program header table immediately after the Elf header. */
433a85fe12eSEd Maste 	if (ecp->ophnum > 0) {
434a85fe12eSEd Maste 		oeh.e_phoff = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT);
435a85fe12eSEd Maste 		if (oeh.e_phoff == 0)
436a85fe12eSEd Maste 			errx(EXIT_FAILURE, "gelf_fsize() failed: %s",
437a85fe12eSEd Maste 			    elf_errmsg(-1));
438a85fe12eSEd Maste 	}
439a85fe12eSEd Maste 
440a85fe12eSEd Maste 	/*
441a85fe12eSEd Maste 	 * Update ELF object entry point if requested.
442a85fe12eSEd Maste 	 */
443a85fe12eSEd Maste 	if (ecp->change_addr != 0)
444a85fe12eSEd Maste 		oeh.e_entry += ecp->change_addr;
445a85fe12eSEd Maste 	if (ecp->flags & SET_START)
446a85fe12eSEd Maste 		oeh.e_entry = ecp->set_start;
447a85fe12eSEd Maste 	if (ecp->change_start != 0)
448a85fe12eSEd Maste 		oeh.e_entry += ecp->change_start;
449a85fe12eSEd Maste 
450a85fe12eSEd Maste 	/*
451a85fe12eSEd Maste 	 * Update ehdr again before we call elf_update(), since we
452a85fe12eSEd Maste 	 * modified e_shoff and e_phoff.
453a85fe12eSEd Maste 	 */
454a85fe12eSEd Maste 	if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
455a85fe12eSEd Maste 		errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
456a85fe12eSEd Maste 		    elf_errmsg(-1));
457a85fe12eSEd Maste 
458a85fe12eSEd Maste 	if (ecp->ophnum > 0)
459a85fe12eSEd Maste 		copy_phdr(ecp);
460a85fe12eSEd Maste 
461a85fe12eSEd Maste 	/* Write out the output elf object. */
462a85fe12eSEd Maste 	if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
463a85fe12eSEd Maste 		errx(EXIT_FAILURE, "elf_update() failed: %s",
464a85fe12eSEd Maste 		    elf_errmsg(-1));
465a85fe12eSEd Maste 
466a85fe12eSEd Maste 	/* Release allocated resource. */
467a85fe12eSEd Maste 	free_elf(ecp);
468a85fe12eSEd Maste }
469a85fe12eSEd Maste 
470a85fe12eSEd Maste void
471a85fe12eSEd Maste free_elf(struct elfcopy *ecp)
472a85fe12eSEd Maste {
473a85fe12eSEd Maste 	struct segment	*seg, *seg_temp;
474a85fe12eSEd Maste 	struct section	*sec, *sec_temp;
475a85fe12eSEd Maste 
476a85fe12eSEd Maste 	/* Free internal segment list. */
477a85fe12eSEd Maste 	if (!STAILQ_EMPTY(&ecp->v_seg)) {
478a85fe12eSEd Maste 		STAILQ_FOREACH_SAFE(seg, &ecp->v_seg, seg_list, seg_temp) {
479a85fe12eSEd Maste 			STAILQ_REMOVE(&ecp->v_seg, seg, segment, seg_list);
480a85fe12eSEd Maste 			free(seg);
481a85fe12eSEd Maste 		}
482a85fe12eSEd Maste 	}
483a85fe12eSEd Maste 
484a85fe12eSEd Maste 	/* Free symbol table buffers. */
485a85fe12eSEd Maste 	free_symtab(ecp);
486a85fe12eSEd Maste 
487a85fe12eSEd Maste 	/* Free internal section list. */
488a85fe12eSEd Maste 	if (!TAILQ_EMPTY(&ecp->v_sec)) {
489a85fe12eSEd Maste 		TAILQ_FOREACH_SAFE(sec, &ecp->v_sec, sec_list, sec_temp) {
490a85fe12eSEd Maste 			TAILQ_REMOVE(&ecp->v_sec, sec, sec_list);
491a85fe12eSEd Maste 			if (sec->buf != NULL)
492a85fe12eSEd Maste 				free(sec->buf);
493a85fe12eSEd Maste 			if (sec->newname != NULL)
494a85fe12eSEd Maste 				free(sec->newname);
495a85fe12eSEd Maste 			if (sec->pad != NULL)
496a85fe12eSEd Maste 				free(sec->pad);
497a85fe12eSEd Maste 			free(sec);
498a85fe12eSEd Maste 		}
499a85fe12eSEd Maste 	}
5003ef90571SEd Maste 
5013ef90571SEd Maste 	if (ecp->secndx != NULL) {
5023ef90571SEd Maste 		free(ecp->secndx);
5033ef90571SEd Maste 		ecp->secndx = NULL;
5043ef90571SEd Maste 	}
505a85fe12eSEd Maste }
506a85fe12eSEd Maste 
507a85fe12eSEd Maste /* Create a temporary file. */
508a85fe12eSEd Maste void
509a85fe12eSEd Maste create_tempfile(char **fn, int *fd)
510a85fe12eSEd Maste {
511a85fe12eSEd Maste 	const char	*tmpdir;
512a85fe12eSEd Maste 	char		*cp, *tmpf;
513a85fe12eSEd Maste 	size_t		 tlen, plen;
514a85fe12eSEd Maste 
515a85fe12eSEd Maste #define	_TEMPFILE "ecp.XXXXXXXX"
516a85fe12eSEd Maste #define	_TEMPFILEPATH "/tmp/ecp.XXXXXXXX"
517a85fe12eSEd Maste 
518a85fe12eSEd Maste 	if (fn == NULL || fd == NULL)
519a85fe12eSEd Maste 		return;
520a85fe12eSEd Maste 	/* Repect TMPDIR environment variable. */
521a85fe12eSEd Maste 	tmpdir = getenv("TMPDIR");
522a85fe12eSEd Maste 	if (tmpdir != NULL && *tmpdir != '\0') {
523a85fe12eSEd Maste 		tlen = strlen(tmpdir);
524a85fe12eSEd Maste 		plen = strlen(_TEMPFILE);
525a85fe12eSEd Maste 		tmpf = malloc(tlen + plen + 2);
526a85fe12eSEd Maste 		if (tmpf == NULL)
527a85fe12eSEd Maste 			err(EXIT_FAILURE, "malloc failed");
528a85fe12eSEd Maste 		strncpy(tmpf, tmpdir, tlen);
529a85fe12eSEd Maste 		cp = &tmpf[tlen - 1];
530a85fe12eSEd Maste 		if (*cp++ != '/')
531a85fe12eSEd Maste 			*cp++ = '/';
532a85fe12eSEd Maste 		strncpy(cp, _TEMPFILE, plen);
533a85fe12eSEd Maste 		cp[plen] = '\0';
534a85fe12eSEd Maste 	} else {
535a85fe12eSEd Maste 		tmpf = strdup(_TEMPFILEPATH);
536a85fe12eSEd Maste 		if (tmpf == NULL)
537a85fe12eSEd Maste 			err(EXIT_FAILURE, "strdup failed");
538a85fe12eSEd Maste 	}
539a85fe12eSEd Maste 	if ((*fd = mkstemp(tmpf)) == -1)
540a85fe12eSEd Maste 		err(EXIT_FAILURE, "mkstemp %s failed", tmpf);
541a85fe12eSEd Maste 	if (fchmod(*fd, 0644) == -1)
542a85fe12eSEd Maste 		err(EXIT_FAILURE, "fchmod %s failed", tmpf);
543a85fe12eSEd Maste 	*fn = tmpf;
544a85fe12eSEd Maste 
545a85fe12eSEd Maste #undef _TEMPFILE
546a85fe12eSEd Maste #undef _TEMPFILEPATH
547a85fe12eSEd Maste }
548a85fe12eSEd Maste 
549272a972bSEd Maste /*
550272a972bSEd Maste  * Copy temporary file with path src and file descriptor infd to path dst.
551272a972bSEd Maste  * If in_place is set act as if editing the file in place, avoiding rename()
552272a972bSEd Maste  * to preserve hard and symbolic links. Output file remains open, with file
553272a972bSEd Maste  * descriptor returned in outfd.
554272a972bSEd Maste  */
555a85fe12eSEd Maste static int
556272a972bSEd Maste copy_from_tempfile(const char *src, const char *dst, int infd, int *outfd,
557272a972bSEd Maste     int in_place)
558a85fe12eSEd Maste {
559a85fe12eSEd Maste 	int tmpfd;
560a85fe12eSEd Maste 
561a85fe12eSEd Maste 	/*
562a85fe12eSEd Maste 	 * First, check if we can use rename().
563a85fe12eSEd Maste 	 */
564272a972bSEd Maste 	if (in_place == 0) {
565a85fe12eSEd Maste 		if (rename(src, dst) >= 0) {
566a85fe12eSEd Maste 			*outfd = infd;
567a85fe12eSEd Maste 			return (0);
568a85fe12eSEd Maste 		} else if (errno != EXDEV)
569a85fe12eSEd Maste 			return (-1);
570a85fe12eSEd Maste 
571a85fe12eSEd Maste 		/*
572a85fe12eSEd Maste 		 * If the rename() failed due to 'src' and 'dst' residing in
573a85fe12eSEd Maste 		 * two different file systems, invoke a helper function in
574a85fe12eSEd Maste 		 * libelftc to do the copy.
575a85fe12eSEd Maste 		 */
576a85fe12eSEd Maste 
577a85fe12eSEd Maste 		if (unlink(dst) < 0)
578a85fe12eSEd Maste 			return (-1);
579272a972bSEd Maste 	}
580a85fe12eSEd Maste 
581272a972bSEd Maste 	if ((tmpfd = open(dst, O_CREAT | O_TRUNC | O_WRONLY, 0755)) < 0)
582a85fe12eSEd Maste 		return (-1);
583a85fe12eSEd Maste 
584a85fe12eSEd Maste 	if (elftc_copyfile(infd, tmpfd) < 0)
585a85fe12eSEd Maste 		return (-1);
586a85fe12eSEd Maste 
587a85fe12eSEd Maste 	/*
588a85fe12eSEd Maste 	 * Remove the temporary file from the file system
589a85fe12eSEd Maste 	 * namespace, and close its file descriptor.
590a85fe12eSEd Maste 	 */
591a85fe12eSEd Maste 	if (unlink(src) < 0)
592a85fe12eSEd Maste 		return (-1);
593a85fe12eSEd Maste 
594a85fe12eSEd Maste 	(void) close(infd);
595a85fe12eSEd Maste 
596a85fe12eSEd Maste 	/*
597a85fe12eSEd Maste 	 * Return the file descriptor for the destination.
598a85fe12eSEd Maste 	 */
599a85fe12eSEd Maste 	*outfd = tmpfd;
600a85fe12eSEd Maste 
601a85fe12eSEd Maste 	return (0);
602a85fe12eSEd Maste }
603a85fe12eSEd Maste 
604a85fe12eSEd Maste static void
605a85fe12eSEd Maste create_file(struct elfcopy *ecp, const char *src, const char *dst)
606a85fe12eSEd Maste {
607a85fe12eSEd Maste 	struct stat	 sb;
608a85fe12eSEd Maste 	char		*tempfile, *elftemp;
609a85fe12eSEd Maste 	int		 efd, ifd, ofd, ofd0, tfd;
610272a972bSEd Maste 	int		 in_place;
611a85fe12eSEd Maste 
612a85fe12eSEd Maste 	tempfile = NULL;
613a85fe12eSEd Maste 
614a85fe12eSEd Maste 	if (src == NULL)
615a85fe12eSEd Maste 		errx(EXIT_FAILURE, "internal: src == NULL");
616a85fe12eSEd Maste 	if ((ifd = open(src, O_RDONLY)) == -1)
617a85fe12eSEd Maste 		err(EXIT_FAILURE, "open %s failed", src);
618a85fe12eSEd Maste 
619a85fe12eSEd Maste 	if (fstat(ifd, &sb) == -1)
620a85fe12eSEd Maste 		err(EXIT_FAILURE, "fstat %s failed", src);
621a85fe12eSEd Maste 
622a85fe12eSEd Maste 	if (dst == NULL)
623a85fe12eSEd Maste 		create_tempfile(&tempfile, &ofd);
624a85fe12eSEd Maste 	else
625a85fe12eSEd Maste 		if ((ofd = open(dst, O_RDWR|O_CREAT, 0755)) == -1)
626a85fe12eSEd Maste 			err(EXIT_FAILURE, "open %s failed", dst);
627a85fe12eSEd Maste 
628a85fe12eSEd Maste #ifndef LIBELF_AR
629a85fe12eSEd Maste 	/* Detect and process ar(1) archive using libarchive. */
630a85fe12eSEd Maste 	if (ac_detect_ar(ifd)) {
631a85fe12eSEd Maste 		ac_create_ar(ecp, ifd, ofd);
632a85fe12eSEd Maste 		goto copy_done;
633a85fe12eSEd Maste 	}
634a85fe12eSEd Maste #endif
635a85fe12eSEd Maste 
636a85fe12eSEd Maste 	if (lseek(ifd, 0, SEEK_SET) < 0)
637a85fe12eSEd Maste 		err(EXIT_FAILURE, "lseek failed");
638a85fe12eSEd Maste 
639a85fe12eSEd Maste 	/*
640a85fe12eSEd Maste 	 * If input object is not ELF file, convert it to an intermediate
641a85fe12eSEd Maste 	 * ELF object before processing.
642a85fe12eSEd Maste 	 */
643a85fe12eSEd Maste 	if (ecp->itf != ETF_ELF) {
644*3884d6f8SEd Maste 		/*
645*3884d6f8SEd Maste 		 * If the output object is not an ELF file, choose an arbitrary
646*3884d6f8SEd Maste 		 * ELF format for the intermediate file. srec, ihex and binary
647*3884d6f8SEd Maste 		 * formats are independent of class, endianness and machine
648*3884d6f8SEd Maste 		 * type so these choices do not affect the output.
649*3884d6f8SEd Maste 		 */
650*3884d6f8SEd Maste 		if (ecp->otf != ETF_ELF) {
651*3884d6f8SEd Maste 			if (ecp->oec == ELFCLASSNONE)
652*3884d6f8SEd Maste 				ecp->oec = ELFCLASS64;
653*3884d6f8SEd Maste 			if (ecp->oed == ELFDATANONE)
654*3884d6f8SEd Maste 				ecp->oed = ELFDATA2LSB;
655*3884d6f8SEd Maste 		}
656a85fe12eSEd Maste 		create_tempfile(&elftemp, &efd);
657a85fe12eSEd Maste 		if ((ecp->eout = elf_begin(efd, ELF_C_WRITE, NULL)) == NULL)
658a85fe12eSEd Maste 			errx(EXIT_FAILURE, "elf_begin() failed: %s",
659a85fe12eSEd Maste 			    elf_errmsg(-1));
660a85fe12eSEd Maste 		elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT);
661a85fe12eSEd Maste 		if (ecp->itf == ETF_BINARY)
662a85fe12eSEd Maste 			create_elf_from_binary(ecp, ifd, src);
663a85fe12eSEd Maste 		else if (ecp->itf == ETF_IHEX)
664a85fe12eSEd Maste 			create_elf_from_ihex(ecp, ifd);
665a85fe12eSEd Maste 		else if (ecp->itf == ETF_SREC)
666a85fe12eSEd Maste 			create_elf_from_srec(ecp, ifd);
667a85fe12eSEd Maste 		else
668a85fe12eSEd Maste 			errx(EXIT_FAILURE, "Internal: invalid target flavour");
669a85fe12eSEd Maste 		elf_end(ecp->eout);
670a85fe12eSEd Maste 
671a85fe12eSEd Maste 		/* Open intermediate ELF object as new input object. */
672a85fe12eSEd Maste 		close(ifd);
673a85fe12eSEd Maste 		if ((ifd = open(elftemp, O_RDONLY)) == -1)
674a85fe12eSEd Maste 			err(EXIT_FAILURE, "open %s failed", src);
675a85fe12eSEd Maste 		close(efd);
676a85fe12eSEd Maste 		free(elftemp);
677a85fe12eSEd Maste 	}
678a85fe12eSEd Maste 
679a85fe12eSEd Maste 	if ((ecp->ein = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
680a85fe12eSEd Maste 		errx(EXIT_FAILURE, "elf_begin() failed: %s",
681a85fe12eSEd Maste 		    elf_errmsg(-1));
682a85fe12eSEd Maste 
683a85fe12eSEd Maste 	switch (elf_kind(ecp->ein)) {
684a85fe12eSEd Maste 	case ELF_K_NONE:
685a85fe12eSEd Maste 		errx(EXIT_FAILURE, "file format not recognized");
686a85fe12eSEd Maste 	case ELF_K_ELF:
687a85fe12eSEd Maste 		if ((ecp->eout = elf_begin(ofd, ELF_C_WRITE, NULL)) == NULL)
688a85fe12eSEd Maste 			errx(EXIT_FAILURE, "elf_begin() failed: %s",
689a85fe12eSEd Maste 			    elf_errmsg(-1));
690a85fe12eSEd Maste 
691a85fe12eSEd Maste 		/* elfcopy(1) manage ELF layout by itself. */
692a85fe12eSEd Maste 		elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT);
693a85fe12eSEd Maste 
694a85fe12eSEd Maste 		/*
695a85fe12eSEd Maste 		 * Create output ELF object.
696a85fe12eSEd Maste 		 */
697a85fe12eSEd Maste 		create_elf(ecp);
698a85fe12eSEd Maste 		elf_end(ecp->eout);
699a85fe12eSEd Maste 
700a85fe12eSEd Maste 		/*
701a85fe12eSEd Maste 		 * Convert the output ELF object to binary/srec/ihex if need.
702a85fe12eSEd Maste 		 */
703a85fe12eSEd Maste 		if (ecp->otf != ETF_ELF) {
704a85fe12eSEd Maste 			/*
705a85fe12eSEd Maste 			 * Create (another) tempfile for binary/srec/ihex
706a85fe12eSEd Maste 			 * output object.
707a85fe12eSEd Maste 			 */
708a85fe12eSEd Maste 			if (tempfile != NULL) {
709a85fe12eSEd Maste 				if (unlink(tempfile) < 0)
710a85fe12eSEd Maste 					err(EXIT_FAILURE, "unlink %s failed",
711a85fe12eSEd Maste 					    tempfile);
712a85fe12eSEd Maste 				free(tempfile);
713a85fe12eSEd Maste 			}
714a85fe12eSEd Maste 			create_tempfile(&tempfile, &ofd0);
715a85fe12eSEd Maste 
716a85fe12eSEd Maste 
717a85fe12eSEd Maste 			/*
718a85fe12eSEd Maste 			 * Rewind the file descriptor being processed.
719a85fe12eSEd Maste 			 */
720a85fe12eSEd Maste 			if (lseek(ofd, 0, SEEK_SET) < 0)
721a85fe12eSEd Maste 				err(EXIT_FAILURE,
722a85fe12eSEd Maste 				    "lseek failed for the output object");
723a85fe12eSEd Maste 
724a85fe12eSEd Maste 			/*
725a85fe12eSEd Maste 			 * Call flavour-specific conversion routine.
726a85fe12eSEd Maste 			 */
727a85fe12eSEd Maste 			switch (ecp->otf) {
728a85fe12eSEd Maste 			case ETF_BINARY:
729a85fe12eSEd Maste 				create_binary(ofd, ofd0);
730a85fe12eSEd Maste 				break;
731a85fe12eSEd Maste 			case ETF_IHEX:
732a85fe12eSEd Maste 				create_ihex(ofd, ofd0);
733a85fe12eSEd Maste 				break;
734a85fe12eSEd Maste 			case ETF_SREC:
735a85fe12eSEd Maste 				create_srec(ecp, ofd, ofd0,
736a85fe12eSEd Maste 				    dst != NULL ? dst : src);
737a85fe12eSEd Maste 				break;
738839529caSEd Maste 			case ETF_PE:
739839529caSEd Maste 			case ETF_EFI:
740839529caSEd Maste #if	WITH_PE
741839529caSEd Maste 				create_pe(ecp, ofd, ofd0);
742839529caSEd Maste #else
743839529caSEd Maste 				errx(EXIT_FAILURE, "PE/EFI support not enabled"
744839529caSEd Maste 				    " at compile time");
745839529caSEd Maste #endif
746839529caSEd Maste 				break;
747a85fe12eSEd Maste 			default:
748a85fe12eSEd Maste 				errx(EXIT_FAILURE, "Internal: unsupported"
749a85fe12eSEd Maste 				    " output flavour %d", ecp->oec);
750a85fe12eSEd Maste 			}
751a85fe12eSEd Maste 
752a85fe12eSEd Maste 			close(ofd);
753a85fe12eSEd Maste 			ofd = ofd0;
754a85fe12eSEd Maste 		}
755a85fe12eSEd Maste 
756a85fe12eSEd Maste 		break;
757a85fe12eSEd Maste 
758a85fe12eSEd Maste 	case ELF_K_AR:
759a85fe12eSEd Maste 		/* XXX: Not yet supported. */
760a85fe12eSEd Maste 		break;
761a85fe12eSEd Maste 	default:
762a85fe12eSEd Maste 		errx(EXIT_FAILURE, "file format not supported");
763a85fe12eSEd Maste 	}
764a85fe12eSEd Maste 
765a85fe12eSEd Maste 	elf_end(ecp->ein);
766a85fe12eSEd Maste 
767a85fe12eSEd Maste #ifndef LIBELF_AR
768a85fe12eSEd Maste copy_done:
769a85fe12eSEd Maste #endif
770a85fe12eSEd Maste 
771a85fe12eSEd Maste 	if (tempfile != NULL) {
772272a972bSEd Maste 		in_place = 0;
773272a972bSEd Maste 		if (dst == NULL) {
774a85fe12eSEd Maste 			dst = src;
775272a972bSEd Maste 			if (lstat(dst, &sb) != -1 &&
776272a972bSEd Maste 			    (sb.st_nlink > 1 || S_ISLNK(sb.st_mode)))
777272a972bSEd Maste 				in_place = 1;
778272a972bSEd Maste 		}
779a85fe12eSEd Maste 
780272a972bSEd Maste 		if (copy_from_tempfile(tempfile, dst, ofd, &tfd, in_place) < 0)
781a85fe12eSEd Maste 			err(EXIT_FAILURE, "creation of %s failed", dst);
782a85fe12eSEd Maste 
783a85fe12eSEd Maste 		free(tempfile);
784a85fe12eSEd Maste 		tempfile = NULL;
785a85fe12eSEd Maste 
786a85fe12eSEd Maste 		ofd = tfd;
787a85fe12eSEd Maste 	}
788a85fe12eSEd Maste 
789a85fe12eSEd Maste 	if (strcmp(dst, "/dev/null") && fchmod(ofd, sb.st_mode) == -1)
790a85fe12eSEd Maste 		err(EXIT_FAILURE, "fchmod %s failed", dst);
791a85fe12eSEd Maste 
792a85fe12eSEd Maste 	if ((ecp->flags & PRESERVE_DATE) &&
793a85fe12eSEd Maste 	    elftc_set_timestamps(dst, &sb) < 0)
794a85fe12eSEd Maste 		err(EXIT_FAILURE, "setting timestamps failed");
795a85fe12eSEd Maste 
796a85fe12eSEd Maste 	close(ifd);
797a85fe12eSEd Maste 	close(ofd);
798a85fe12eSEd Maste }
799a85fe12eSEd Maste 
800a85fe12eSEd Maste static void
801a85fe12eSEd Maste elfcopy_main(struct elfcopy *ecp, int argc, char **argv)
802a85fe12eSEd Maste {
803a85fe12eSEd Maste 	struct sec_action	*sac;
804a85fe12eSEd Maste 	const char		*infile, *outfile;
805a85fe12eSEd Maste 	char			*fn, *s;
806a85fe12eSEd Maste 	int			 opt;
807a85fe12eSEd Maste 
808a85fe12eSEd Maste 	while ((opt = getopt_long(argc, argv, "dB:gG:I:j:K:L:N:O:pR:s:SwW:xXV",
809a85fe12eSEd Maste 	    elfcopy_longopts, NULL)) != -1) {
810a85fe12eSEd Maste 		switch(opt) {
811a85fe12eSEd Maste 		case 'B':
812a85fe12eSEd Maste 			/* ignored */
813a85fe12eSEd Maste 			break;
814a85fe12eSEd Maste 		case 'R':
815a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
816a85fe12eSEd Maste 			if (sac->copy != 0)
817a85fe12eSEd Maste 				errx(EXIT_FAILURE,
818a85fe12eSEd Maste 				    "both copy and remove specified");
819a85fe12eSEd Maste 			sac->remove = 1;
820a85fe12eSEd Maste 			ecp->flags |= SEC_REMOVE;
821a85fe12eSEd Maste 			break;
822a85fe12eSEd Maste 		case 'S':
823a85fe12eSEd Maste 			ecp->strip = STRIP_ALL;
824a85fe12eSEd Maste 			break;
825a85fe12eSEd Maste 		case 'g':
826a85fe12eSEd Maste 			ecp->strip = STRIP_DEBUG;
827a85fe12eSEd Maste 			break;
828a85fe12eSEd Maste 		case 'G':
829a85fe12eSEd Maste 			ecp->flags |= KEEP_GLOBAL;
830a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEPG);
831a85fe12eSEd Maste 			break;
832a85fe12eSEd Maste 		case 'I':
833a85fe12eSEd Maste 		case 's':
834a85fe12eSEd Maste 			set_input_target(ecp, optarg);
835a85fe12eSEd Maste 			break;
836a85fe12eSEd Maste 		case 'j':
837a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
838a85fe12eSEd Maste 			if (sac->remove != 0)
839a85fe12eSEd Maste 				errx(EXIT_FAILURE,
840a85fe12eSEd Maste 				    "both copy and remove specified");
841a85fe12eSEd Maste 			sac->copy = 1;
842a85fe12eSEd Maste 			ecp->flags |= SEC_COPY;
843a85fe12eSEd Maste 			break;
844a85fe12eSEd Maste 		case 'K':
845a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP);
846a85fe12eSEd Maste 			break;
847a85fe12eSEd Maste 		case 'L':
848a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_LOCALIZE);
849a85fe12eSEd Maste 			break;
850a85fe12eSEd Maste 		case 'N':
851a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP);
852a85fe12eSEd Maste 			break;
853a85fe12eSEd Maste 		case 'O':
854a85fe12eSEd Maste 			set_output_target(ecp, optarg);
855a85fe12eSEd Maste 			break;
856a85fe12eSEd Maste 		case 'p':
857a85fe12eSEd Maste 			ecp->flags |= PRESERVE_DATE;
858a85fe12eSEd Maste 			break;
859a85fe12eSEd Maste 		case 'V':
860a85fe12eSEd Maste 			print_version();
861a85fe12eSEd Maste 			break;
862a85fe12eSEd Maste 		case 'w':
863a85fe12eSEd Maste 			ecp->flags |= WILDCARD;
864a85fe12eSEd Maste 			break;
865a85fe12eSEd Maste 		case 'W':
866a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_WEAKEN);
867a85fe12eSEd Maste 			break;
868a85fe12eSEd Maste 		case 'x':
869a85fe12eSEd Maste 			ecp->flags |= DISCARD_LOCAL;
870a85fe12eSEd Maste 			break;
871a85fe12eSEd Maste 		case 'X':
872a85fe12eSEd Maste 			ecp->flags |= DISCARD_LLABEL;
873a85fe12eSEd Maste 			break;
874a85fe12eSEd Maste 		case ECP_ADD_GNU_DEBUGLINK:
875a85fe12eSEd Maste 			ecp->debuglink = optarg;
876a85fe12eSEd Maste 			break;
877a85fe12eSEd Maste 		case ECP_ADD_SECTION:
878a85fe12eSEd Maste 			add_section(ecp, optarg);
879a85fe12eSEd Maste 			break;
880a85fe12eSEd Maste 		case ECP_CHANGE_ADDR:
881a85fe12eSEd Maste 			ecp->change_addr = (int64_t) strtoll(optarg, NULL, 0);
882a85fe12eSEd Maste 			break;
883a85fe12eSEd Maste 		case ECP_CHANGE_SEC_ADDR:
884a85fe12eSEd Maste 			parse_sec_address_op(ecp, opt, "--change-section-addr",
885a85fe12eSEd Maste 			    optarg);
886a85fe12eSEd Maste 			break;
887a85fe12eSEd Maste 		case ECP_CHANGE_SEC_LMA:
888a85fe12eSEd Maste 			parse_sec_address_op(ecp, opt, "--change-section-lma",
889a85fe12eSEd Maste 			    optarg);
890a85fe12eSEd Maste 			break;
891a85fe12eSEd Maste 		case ECP_CHANGE_SEC_VMA:
892a85fe12eSEd Maste 			parse_sec_address_op(ecp, opt, "--change-section-vma",
893a85fe12eSEd Maste 			    optarg);
894a85fe12eSEd Maste 			break;
895a85fe12eSEd Maste 		case ECP_CHANGE_START:
896a85fe12eSEd Maste 			ecp->change_start = (int64_t) strtoll(optarg, NULL, 0);
897a85fe12eSEd Maste 			break;
898a85fe12eSEd Maste 		case ECP_CHANGE_WARN:
899a85fe12eSEd Maste 			/* default */
900a85fe12eSEd Maste 			break;
901a85fe12eSEd Maste 		case ECP_GAP_FILL:
902a85fe12eSEd Maste 			ecp->fill = (uint8_t) strtoul(optarg, NULL, 0);
903a85fe12eSEd Maste 			ecp->flags |= GAP_FILL;
904a85fe12eSEd Maste 			break;
905a85fe12eSEd Maste 		case ECP_GLOBALIZE_SYMBOL:
906a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_GLOBALIZE);
907a85fe12eSEd Maste 			break;
908a85fe12eSEd Maste 		case ECP_GLOBALIZE_SYMBOLS:
909a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_GLOBALIZE);
910a85fe12eSEd Maste 			break;
911a85fe12eSEd Maste 		case ECP_KEEP_SYMBOLS:
912a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_KEEP);
913a85fe12eSEd Maste 			break;
914a85fe12eSEd Maste 		case ECP_KEEP_GLOBAL_SYMBOLS:
915a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_KEEPG);
916a85fe12eSEd Maste 			break;
91767d97fe7SEd Maste 		case ECP_LOCALIZE_HIDDEN:
91867d97fe7SEd Maste 			ecp->flags |= LOCALIZE_HIDDEN;
91967d97fe7SEd Maste 			break;
920a85fe12eSEd Maste 		case ECP_LOCALIZE_SYMBOLS:
921a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_LOCALIZE);
922a85fe12eSEd Maste 			break;
923a85fe12eSEd Maste 		case ECP_NO_CHANGE_WARN:
924a85fe12eSEd Maste 			ecp->flags |= NO_CHANGE_WARN;
925a85fe12eSEd Maste 			break;
926a85fe12eSEd Maste 		case ECP_ONLY_DEBUG:
927a85fe12eSEd Maste 			ecp->strip = STRIP_NONDEBUG;
928a85fe12eSEd Maste 			break;
92967d97fe7SEd Maste 		case ECP_ONLY_DWO:
93067d97fe7SEd Maste 			ecp->strip = STRIP_NONDWO;
93167d97fe7SEd Maste 			break;
932a85fe12eSEd Maste 		case ECP_PAD_TO:
933a85fe12eSEd Maste 			ecp->pad_to = (uint64_t) strtoull(optarg, NULL, 0);
934a85fe12eSEd Maste 			break;
935a85fe12eSEd Maste 		case ECP_PREFIX_ALLOC:
936a85fe12eSEd Maste 			ecp->prefix_alloc = optarg;
937a85fe12eSEd Maste 			break;
938a85fe12eSEd Maste 		case ECP_PREFIX_SEC:
939a85fe12eSEd Maste 			ecp->prefix_sec = optarg;
940a85fe12eSEd Maste 			break;
941a85fe12eSEd Maste 		case ECP_PREFIX_SYM:
942a85fe12eSEd Maste 			ecp->prefix_sym = optarg;
943a85fe12eSEd Maste 			break;
944a85fe12eSEd Maste 		case ECP_REDEF_SYMBOL:
945a85fe12eSEd Maste 			if ((s = strchr(optarg, '=')) == NULL)
946a85fe12eSEd Maste 				errx(EXIT_FAILURE,
947a85fe12eSEd Maste 				    "illegal format for --redefine-sym");
948a85fe12eSEd Maste 			*s++ = '\0';
949a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, s, SYMOP_REDEF);
950a85fe12eSEd Maste 			break;
951a85fe12eSEd Maste 		case ECP_REDEF_SYMBOLS:
952a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_REDEF);
953a85fe12eSEd Maste 			break;
954a85fe12eSEd Maste 		case ECP_RENAME_SECTION:
955a85fe12eSEd Maste 			if ((fn = strchr(optarg, '=')) == NULL)
956a85fe12eSEd Maste 				errx(EXIT_FAILURE,
957a85fe12eSEd Maste 				    "illegal format for --rename-section");
958a85fe12eSEd Maste 			*fn++ = '\0';
959a85fe12eSEd Maste 
960a85fe12eSEd Maste 			/* Check for optional flags. */
961a85fe12eSEd Maste 			if ((s = strchr(fn, ',')) != NULL)
962a85fe12eSEd Maste 				*s++ = '\0';
963a85fe12eSEd Maste 
964a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
965a85fe12eSEd Maste 			sac->rename = 1;
966a85fe12eSEd Maste 			sac->newname = fn;
967a85fe12eSEd Maste 			if (s != NULL)
968a85fe12eSEd Maste 				parse_sec_flags(sac, s);
969a85fe12eSEd Maste 			break;
970a85fe12eSEd Maste 		case ECP_SET_OSABI:
971a85fe12eSEd Maste 			set_osabi(ecp, optarg);
972a85fe12eSEd Maste 			break;
973a85fe12eSEd Maste 		case ECP_SET_SEC_FLAGS:
974a85fe12eSEd Maste 			if ((s = strchr(optarg, '=')) == NULL)
975a85fe12eSEd Maste 				errx(EXIT_FAILURE,
976a85fe12eSEd Maste 				    "illegal format for --set-section-flags");
977a85fe12eSEd Maste 			*s++ = '\0';
978a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
979a85fe12eSEd Maste 			parse_sec_flags(sac, s);
980a85fe12eSEd Maste 			break;
981a85fe12eSEd Maste 		case ECP_SET_START:
982a85fe12eSEd Maste 			ecp->flags |= SET_START;
983a85fe12eSEd Maste 			ecp->set_start = (uint64_t) strtoull(optarg, NULL, 0);
984a85fe12eSEd Maste 			break;
985a85fe12eSEd Maste 		case ECP_SREC_FORCE_S3:
986a85fe12eSEd Maste 			ecp->flags |= SREC_FORCE_S3;
987a85fe12eSEd Maste 			break;
988a85fe12eSEd Maste 		case ECP_SREC_LEN:
989a85fe12eSEd Maste 			ecp->flags |= SREC_FORCE_LEN;
990a85fe12eSEd Maste 			ecp->srec_len = strtoul(optarg, NULL, 0);
991a85fe12eSEd Maste 			break;
99267d97fe7SEd Maste 		case ECP_STRIP_DWO:
99367d97fe7SEd Maste 			ecp->strip = STRIP_DWO;
99467d97fe7SEd Maste 			break;
995a85fe12eSEd Maste 		case ECP_STRIP_SYMBOLS:
996a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_STRIP);
997a85fe12eSEd Maste 			break;
998a85fe12eSEd Maste 		case ECP_STRIP_UNNEEDED:
999a85fe12eSEd Maste 			ecp->strip = STRIP_UNNEEDED;
1000a85fe12eSEd Maste 			break;
1001a85fe12eSEd Maste 		case ECP_WEAKEN_ALL:
1002a85fe12eSEd Maste 			ecp->flags |= WEAKEN_ALL;
1003a85fe12eSEd Maste 			break;
1004a85fe12eSEd Maste 		case ECP_WEAKEN_SYMBOLS:
1005a85fe12eSEd Maste 			parse_symlist_file(ecp, optarg, SYMOP_WEAKEN);
1006a85fe12eSEd Maste 			break;
1007a85fe12eSEd Maste 		default:
1008a85fe12eSEd Maste 			elfcopy_usage();
1009a85fe12eSEd Maste 		}
1010a85fe12eSEd Maste 	}
1011a85fe12eSEd Maste 
1012a85fe12eSEd Maste 	if (optind == argc || optind + 2 < argc)
1013a85fe12eSEd Maste 		elfcopy_usage();
1014a85fe12eSEd Maste 
1015a85fe12eSEd Maste 	infile = argv[optind];
1016a85fe12eSEd Maste 	outfile = NULL;
1017a85fe12eSEd Maste 	if (optind + 1 < argc)
1018a85fe12eSEd Maste 		outfile = argv[optind + 1];
1019a85fe12eSEd Maste 
1020a85fe12eSEd Maste 	create_file(ecp, infile, outfile);
1021a85fe12eSEd Maste }
1022a85fe12eSEd Maste 
1023a85fe12eSEd Maste static void
1024a85fe12eSEd Maste mcs_main(struct elfcopy *ecp, int argc, char **argv)
1025a85fe12eSEd Maste {
1026a85fe12eSEd Maste 	struct sec_action	*sac;
1027a85fe12eSEd Maste 	const char		*string;
1028a85fe12eSEd Maste 	int			 append, delete, compress, name, print;
1029a85fe12eSEd Maste 	int			 opt, i;
1030a85fe12eSEd Maste 
1031a85fe12eSEd Maste 	append = delete = compress = name = print = 0;
1032a85fe12eSEd Maste 	string = NULL;
1033a85fe12eSEd Maste 	while ((opt = getopt_long(argc, argv, "a:cdhn:pV", mcs_longopts,
1034a85fe12eSEd Maste 		NULL)) != -1) {
1035a85fe12eSEd Maste 		switch(opt) {
1036a85fe12eSEd Maste 		case 'a':
1037a85fe12eSEd Maste 			append = 1;
1038a85fe12eSEd Maste 			string = optarg; /* XXX multiple -a not supported */
1039a85fe12eSEd Maste 			break;
1040a85fe12eSEd Maste 		case 'c':
1041a85fe12eSEd Maste 			compress = 1;
1042a85fe12eSEd Maste 			break;
1043a85fe12eSEd Maste 		case 'd':
1044a85fe12eSEd Maste 			delete = 1;
1045a85fe12eSEd Maste 			break;
1046a85fe12eSEd Maste 		case 'n':
1047a85fe12eSEd Maste 			name = 1;
1048a85fe12eSEd Maste 			(void)lookup_sec_act(ecp, optarg, 1);
1049a85fe12eSEd Maste 			break;
1050a85fe12eSEd Maste 		case 'p':
1051a85fe12eSEd Maste 			print = 1;
1052a85fe12eSEd Maste 			break;
1053a85fe12eSEd Maste 		case 'V':
1054a85fe12eSEd Maste 			print_version();
1055a85fe12eSEd Maste 			break;
1056a85fe12eSEd Maste 		case 'h':
1057a85fe12eSEd Maste 		default:
1058a85fe12eSEd Maste 			mcs_usage();
1059a85fe12eSEd Maste 		}
1060a85fe12eSEd Maste 	}
1061a85fe12eSEd Maste 
1062a85fe12eSEd Maste 	if (optind == argc)
1063a85fe12eSEd Maste 		mcs_usage();
1064a85fe12eSEd Maste 
1065a85fe12eSEd Maste 	/* Must specify one operation at least. */
1066a85fe12eSEd Maste 	if (!append && !compress && !delete && !print)
1067a85fe12eSEd Maste 		mcs_usage();
1068a85fe12eSEd Maste 
1069a85fe12eSEd Maste 	/*
1070a85fe12eSEd Maste 	 * If we are going to delete, ignore other operations. This is
1071a85fe12eSEd Maste 	 * different from the Solaris implementation, which can print
1072a85fe12eSEd Maste 	 * and delete a section at the same time, for example. Also, this
1073a85fe12eSEd Maste 	 * implementation do not respect the order between operations that
1074a85fe12eSEd Maste 	 * user specified, i.e., "mcs -pc a.out" equals to "mcs -cp a.out".
1075a85fe12eSEd Maste 	 */
1076a85fe12eSEd Maste 	if (delete) {
1077a85fe12eSEd Maste 		append = compress = print = 0;
1078a85fe12eSEd Maste 		ecp->flags |= SEC_REMOVE;
1079a85fe12eSEd Maste 	}
1080a85fe12eSEd Maste 	if (append)
1081a85fe12eSEd Maste 		ecp->flags |= SEC_APPEND;
1082a85fe12eSEd Maste 	if (compress)
1083a85fe12eSEd Maste 		ecp->flags |= SEC_COMPRESS;
1084a85fe12eSEd Maste 	if (print)
1085a85fe12eSEd Maste 		ecp->flags |= SEC_PRINT;
1086a85fe12eSEd Maste 
1087a85fe12eSEd Maste 	/* .comment is the default section to operate on. */
1088a85fe12eSEd Maste 	if (!name)
1089a85fe12eSEd Maste 		(void)lookup_sec_act(ecp, ".comment", 1);
1090a85fe12eSEd Maste 
1091a85fe12eSEd Maste 	STAILQ_FOREACH(sac, &ecp->v_sac, sac_list) {
1092a85fe12eSEd Maste 		sac->append = append;
1093a85fe12eSEd Maste 		sac->compress = compress;
1094a85fe12eSEd Maste 		sac->print = print;
1095a85fe12eSEd Maste 		sac->remove = delete;
1096a85fe12eSEd Maste 		sac->string = string;
1097a85fe12eSEd Maste 	}
1098a85fe12eSEd Maste 
1099a85fe12eSEd Maste 	for (i = optind; i < argc; i++) {
1100a85fe12eSEd Maste 		/* If only -p is specified, output to /dev/null */
1101a85fe12eSEd Maste 		if (print && !append && !compress && !delete)
1102a85fe12eSEd Maste 			create_file(ecp, argv[i], "/dev/null");
1103a85fe12eSEd Maste 		else
1104a85fe12eSEd Maste 			create_file(ecp, argv[i], NULL);
1105a85fe12eSEd Maste 	}
1106a85fe12eSEd Maste }
1107a85fe12eSEd Maste 
1108a85fe12eSEd Maste static void
1109a85fe12eSEd Maste strip_main(struct elfcopy *ecp, int argc, char **argv)
1110a85fe12eSEd Maste {
1111a85fe12eSEd Maste 	struct sec_action	*sac;
1112a85fe12eSEd Maste 	const char		*outfile;
1113a85fe12eSEd Maste 	int			 opt;
1114a85fe12eSEd Maste 	int			 i;
1115a85fe12eSEd Maste 
1116a85fe12eSEd Maste 	outfile = NULL;
1117a85fe12eSEd Maste 	while ((opt = getopt_long(argc, argv, "hI:K:N:o:O:pR:sSdgVxXw",
1118a85fe12eSEd Maste 	    strip_longopts, NULL)) != -1) {
1119a85fe12eSEd Maste 		switch(opt) {
1120a85fe12eSEd Maste 		case 'R':
1121a85fe12eSEd Maste 			sac = lookup_sec_act(ecp, optarg, 1);
1122a85fe12eSEd Maste 			sac->remove = 1;
1123a85fe12eSEd Maste 			ecp->flags |= SEC_REMOVE;
1124a85fe12eSEd Maste 			break;
1125a85fe12eSEd Maste 		case 's':
1126a85fe12eSEd Maste 			ecp->strip = STRIP_ALL;
1127a85fe12eSEd Maste 			break;
1128a85fe12eSEd Maste 		case 'S':
1129a85fe12eSEd Maste 		case 'g':
1130a85fe12eSEd Maste 		case 'd':
1131a85fe12eSEd Maste 			ecp->strip = STRIP_DEBUG;
1132a85fe12eSEd Maste 			break;
1133a85fe12eSEd Maste 		case 'I':
1134a85fe12eSEd Maste 			/* ignored */
1135a85fe12eSEd Maste 			break;
1136a85fe12eSEd Maste 		case 'K':
1137a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP);
1138a85fe12eSEd Maste 			break;
1139a85fe12eSEd Maste 		case 'N':
1140a85fe12eSEd Maste 			add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP);
1141a85fe12eSEd Maste 			break;
1142a85fe12eSEd Maste 		case 'o':
1143a85fe12eSEd Maste 			outfile = optarg;
1144a85fe12eSEd Maste 			break;
1145a85fe12eSEd Maste 		case 'O':
1146a85fe12eSEd Maste 			set_output_target(ecp, optarg);
1147a85fe12eSEd Maste 			break;
1148a85fe12eSEd Maste 		case 'p':
1149a85fe12eSEd Maste 			ecp->flags |= PRESERVE_DATE;
1150a85fe12eSEd Maste 			break;
1151a85fe12eSEd Maste 		case 'V':
1152a85fe12eSEd Maste 			print_version();
1153a85fe12eSEd Maste 			break;
1154a85fe12eSEd Maste 		case 'w':
1155a85fe12eSEd Maste 			ecp->flags |= WILDCARD;
1156a85fe12eSEd Maste 			break;
1157a85fe12eSEd Maste 		case 'x':
1158a85fe12eSEd Maste 			ecp->flags |= DISCARD_LOCAL;
1159a85fe12eSEd Maste 			break;
1160a85fe12eSEd Maste 		case 'X':
1161a85fe12eSEd Maste 			ecp->flags |= DISCARD_LLABEL;
1162a85fe12eSEd Maste 			break;
1163a85fe12eSEd Maste 		case ECP_ONLY_DEBUG:
1164a85fe12eSEd Maste 			ecp->strip = STRIP_NONDEBUG;
1165a85fe12eSEd Maste 			break;
1166a85fe12eSEd Maste 		case ECP_STRIP_UNNEEDED:
1167a85fe12eSEd Maste 			ecp->strip = STRIP_UNNEEDED;
1168a85fe12eSEd Maste 			break;
1169a85fe12eSEd Maste 		case 'h':
1170a85fe12eSEd Maste 		default:
1171a85fe12eSEd Maste 			strip_usage();
1172a85fe12eSEd Maste 		}
1173a85fe12eSEd Maste 	}
1174a85fe12eSEd Maste 
1175a85fe12eSEd Maste 	if (ecp->strip == 0 &&
1176a85fe12eSEd Maste 	    ((ecp->flags & DISCARD_LOCAL) == 0) &&
1177a356a1f5SEd Maste 	    ((ecp->flags & DISCARD_LLABEL) == 0) &&
1178a356a1f5SEd Maste 	    lookup_symop_list(ecp, NULL, SYMOP_STRIP) == NULL)
1179a85fe12eSEd Maste 		ecp->strip = STRIP_ALL;
1180a85fe12eSEd Maste 	if (optind == argc)
1181a85fe12eSEd Maste 		strip_usage();
1182a85fe12eSEd Maste 
1183a85fe12eSEd Maste 	for (i = optind; i < argc; i++)
1184a85fe12eSEd Maste 		create_file(ecp, argv[i], outfile);
1185a85fe12eSEd Maste }
1186a85fe12eSEd Maste 
1187a85fe12eSEd Maste static void
1188a85fe12eSEd Maste parse_sec_flags(struct sec_action *sac, char *s)
1189a85fe12eSEd Maste {
1190a85fe12eSEd Maste 	const char	*flag;
1191a85fe12eSEd Maste 	int		 found, i;
1192a85fe12eSEd Maste 
1193a85fe12eSEd Maste 	for (flag = strtok(s, ","); flag; flag = strtok(NULL, ",")) {
1194a85fe12eSEd Maste 		found = 0;
1195a85fe12eSEd Maste 		for (i = 0; sec_flags[i].name != NULL; i++)
1196a85fe12eSEd Maste 			if (strcasecmp(sec_flags[i].name, flag) == 0) {
1197a85fe12eSEd Maste 				sac->flags |= sec_flags[i].value;
1198a85fe12eSEd Maste 				found = 1;
1199a85fe12eSEd Maste 				break;
1200a85fe12eSEd Maste 			}
1201a85fe12eSEd Maste 		if (!found)
1202a85fe12eSEd Maste 			errx(EXIT_FAILURE, "unrecognized section flag %s",
1203a85fe12eSEd Maste 			    flag);
1204a85fe12eSEd Maste 	}
1205a85fe12eSEd Maste }
1206a85fe12eSEd Maste 
1207a85fe12eSEd Maste static void
1208a85fe12eSEd Maste parse_sec_address_op(struct elfcopy *ecp, int optnum, const char *optname,
1209a85fe12eSEd Maste     char *s)
1210a85fe12eSEd Maste {
1211a85fe12eSEd Maste 	struct sec_action	*sac;
1212a85fe12eSEd Maste 	const char		*name;
1213a85fe12eSEd Maste 	char			*v;
1214a85fe12eSEd Maste 	char			 op;
1215a85fe12eSEd Maste 
1216a85fe12eSEd Maste 	name = v = s;
1217a85fe12eSEd Maste 	do {
1218a85fe12eSEd Maste 		v++;
1219a85fe12eSEd Maste 	} while (*v != '\0' && *v != '=' && *v != '+' && *v != '-');
1220a85fe12eSEd Maste 	if (*v == '\0' || *(v + 1) == '\0')
1221a85fe12eSEd Maste 		errx(EXIT_FAILURE, "invalid format for %s", optname);
1222a85fe12eSEd Maste 	op = *v;
1223a85fe12eSEd Maste 	*v++ = '\0';
1224a85fe12eSEd Maste 	sac = lookup_sec_act(ecp, name, 1);
1225a85fe12eSEd Maste 	switch (op) {
1226a85fe12eSEd Maste 	case '=':
1227a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_LMA ||
1228a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR) {
1229a85fe12eSEd Maste 			sac->setlma = 1;
1230a85fe12eSEd Maste 			sac->lma = (uint64_t) strtoull(v, NULL, 0);
1231a85fe12eSEd Maste 		}
1232a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_VMA ||
1233a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR) {
1234a85fe12eSEd Maste 			sac->setvma = 1;
1235a85fe12eSEd Maste 			sac->vma = (uint64_t) strtoull(v, NULL, 0);
1236a85fe12eSEd Maste 		}
1237a85fe12eSEd Maste 		break;
1238a85fe12eSEd Maste 	case '+':
1239a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_LMA ||
1240a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR)
1241a85fe12eSEd Maste 			sac->lma_adjust = (int64_t) strtoll(v, NULL, 0);
1242a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_VMA ||
1243a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR)
1244a85fe12eSEd Maste 			sac->vma_adjust = (int64_t) strtoll(v, NULL, 0);
1245a85fe12eSEd Maste 		break;
1246a85fe12eSEd Maste 	case '-':
1247a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_LMA ||
1248a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR)
1249a85fe12eSEd Maste 			sac->lma_adjust = (int64_t) -strtoll(v, NULL, 0);
1250a85fe12eSEd Maste 		if (optnum == ECP_CHANGE_SEC_VMA ||
1251a85fe12eSEd Maste 		    optnum == ECP_CHANGE_SEC_ADDR)
1252a85fe12eSEd Maste 			sac->vma_adjust = (int64_t) -strtoll(v, NULL, 0);
1253a85fe12eSEd Maste 		break;
1254a85fe12eSEd Maste 	default:
1255a85fe12eSEd Maste 		break;
1256a85fe12eSEd Maste 	}
1257a85fe12eSEd Maste }
1258a85fe12eSEd Maste 
1259a85fe12eSEd Maste static void
1260a85fe12eSEd Maste parse_symlist_file(struct elfcopy *ecp, const char *fn, unsigned int op)
1261a85fe12eSEd Maste {
1262a85fe12eSEd Maste 	struct symfile	*sf;
1263a85fe12eSEd Maste 	struct stat	 sb;
1264a85fe12eSEd Maste 	FILE		*fp;
1265a85fe12eSEd Maste 	char		*data, *p, *line, *end, *e, *n;
1266a85fe12eSEd Maste 
1267a85fe12eSEd Maste 	if (stat(fn, &sb) == -1)
1268a85fe12eSEd Maste 		err(EXIT_FAILURE, "stat %s failed", fn);
1269a85fe12eSEd Maste 
1270a85fe12eSEd Maste 	/* Check if we already read and processed this file. */
1271a85fe12eSEd Maste 	STAILQ_FOREACH(sf, &ecp->v_symfile, symfile_list) {
1272a85fe12eSEd Maste 		if (sf->dev == sb.st_dev && sf->ino == sb.st_ino)
1273a85fe12eSEd Maste 			goto process_symfile;
1274a85fe12eSEd Maste 	}
1275a85fe12eSEd Maste 
1276a85fe12eSEd Maste 	if ((fp = fopen(fn, "r")) == NULL)
1277a85fe12eSEd Maste 		err(EXIT_FAILURE, "can not open %s", fn);
1278a85fe12eSEd Maste 	if ((data = malloc(sb.st_size + 1)) == NULL)
1279a85fe12eSEd Maste 		err(EXIT_FAILURE, "malloc failed");
1280a85fe12eSEd Maste 	if (fread(data, 1, sb.st_size, fp) == 0 || ferror(fp))
1281a85fe12eSEd Maste 		err(EXIT_FAILURE, "fread failed");
1282a85fe12eSEd Maste 	fclose(fp);
1283a85fe12eSEd Maste 	data[sb.st_size] = '\0';
1284a85fe12eSEd Maste 
1285a85fe12eSEd Maste 	if ((sf = calloc(1, sizeof(*sf))) == NULL)
1286a85fe12eSEd Maste 		err(EXIT_FAILURE, "malloc failed");
1287a85fe12eSEd Maste 	sf->dev = sb.st_dev;
1288a85fe12eSEd Maste 	sf->ino = sb.st_ino;
1289a85fe12eSEd Maste 	sf->size = sb.st_size + 1;
1290a85fe12eSEd Maste 	sf->data = data;
1291a85fe12eSEd Maste 
1292a85fe12eSEd Maste process_symfile:
1293a85fe12eSEd Maste 
1294a85fe12eSEd Maste 	/*
1295a85fe12eSEd Maste 	 * Basically what we do here is to convert EOL to '\0', and remove
1296a85fe12eSEd Maste 	 * leading and trailing whitespaces for each line.
1297a85fe12eSEd Maste 	 */
1298a85fe12eSEd Maste 
1299a85fe12eSEd Maste 	end = sf->data + sf->size;
1300a85fe12eSEd Maste 	line = NULL;
1301a85fe12eSEd Maste 	for(p = sf->data; p < end; p++) {
1302a85fe12eSEd Maste 		if ((*p == '\t' || *p == ' ') && line == NULL)
1303a85fe12eSEd Maste 			continue;
1304a85fe12eSEd Maste 		if (*p == '\r' || *p == '\n' || *p == '\0') {
1305a85fe12eSEd Maste 			*p = '\0';
1306a85fe12eSEd Maste 			if (line == NULL)
1307a85fe12eSEd Maste 				continue;
1308a85fe12eSEd Maste 
1309a85fe12eSEd Maste 			/* Skip comment. */
1310a85fe12eSEd Maste 			if (*line == '#') {
1311a85fe12eSEd Maste 				line = NULL;
1312a85fe12eSEd Maste 				continue;
1313a85fe12eSEd Maste 			}
1314a85fe12eSEd Maste 
1315a85fe12eSEd Maste 			e = p - 1;
1316a85fe12eSEd Maste 			while(e != line && (*e == '\t' || *e == ' '))
1317a85fe12eSEd Maste 				*e-- = '\0';
1318a85fe12eSEd Maste 			if (op != SYMOP_REDEF)
1319a85fe12eSEd Maste 				add_to_symop_list(ecp, line, NULL, op);
1320a85fe12eSEd Maste 			else {
1321a85fe12eSEd Maste 				if (strlen(line) < 3)
1322a85fe12eSEd Maste 					errx(EXIT_FAILURE,
1323a85fe12eSEd Maste 					    "illegal format for"
1324a85fe12eSEd Maste 					    " --redefine-sym");
1325a85fe12eSEd Maste 				for(n = line + 1; n < e; n++) {
1326a85fe12eSEd Maste 					if (*n == ' ' || *n == '\t') {
1327a85fe12eSEd Maste 						while(*n == ' ' || *n == '\t')
1328a85fe12eSEd Maste 							*n++ = '\0';
1329a85fe12eSEd Maste 						break;
1330a85fe12eSEd Maste 					}
1331a85fe12eSEd Maste 				}
1332a85fe12eSEd Maste 				if (n >= e)
1333a85fe12eSEd Maste 					errx(EXIT_FAILURE,
1334a85fe12eSEd Maste 					    "illegal format for"
1335a85fe12eSEd Maste 					    " --redefine-sym");
1336a85fe12eSEd Maste 				add_to_symop_list(ecp, line, n, op);
1337a85fe12eSEd Maste 			}
1338a85fe12eSEd Maste 			line = NULL;
1339a85fe12eSEd Maste 			continue;
1340a85fe12eSEd Maste 		}
1341a85fe12eSEd Maste 
1342a85fe12eSEd Maste 		if (line == NULL)
1343a85fe12eSEd Maste 			line = p;
1344a85fe12eSEd Maste 	}
1345a85fe12eSEd Maste }
1346a85fe12eSEd Maste 
1347a85fe12eSEd Maste static void
1348a85fe12eSEd Maste set_input_target(struct elfcopy *ecp, const char *target_name)
1349a85fe12eSEd Maste {
1350a85fe12eSEd Maste 	Elftc_Bfd_Target *tgt;
1351a85fe12eSEd Maste 
1352a85fe12eSEd Maste 	if ((tgt = elftc_bfd_find_target(target_name)) == NULL)
1353a85fe12eSEd Maste 		errx(EXIT_FAILURE, "%s: invalid target name", target_name);
1354a85fe12eSEd Maste 	ecp->itf = elftc_bfd_target_flavor(tgt);
1355a85fe12eSEd Maste }
1356a85fe12eSEd Maste 
1357a85fe12eSEd Maste static void
1358a85fe12eSEd Maste set_output_target(struct elfcopy *ecp, const char *target_name)
1359a85fe12eSEd Maste {
1360a85fe12eSEd Maste 	Elftc_Bfd_Target *tgt;
1361a85fe12eSEd Maste 
1362a85fe12eSEd Maste 	if ((tgt = elftc_bfd_find_target(target_name)) == NULL)
1363a85fe12eSEd Maste 		errx(EXIT_FAILURE, "%s: invalid target name", target_name);
1364a85fe12eSEd Maste 	ecp->otf = elftc_bfd_target_flavor(tgt);
1365a85fe12eSEd Maste 	if (ecp->otf == ETF_ELF) {
1366a85fe12eSEd Maste 		ecp->oec = elftc_bfd_target_class(tgt);
1367a85fe12eSEd Maste 		ecp->oed = elftc_bfd_target_byteorder(tgt);
1368a85fe12eSEd Maste 		ecp->oem = elftc_bfd_target_machine(tgt);
1369a85fe12eSEd Maste 	}
1370839529caSEd Maste 	if (ecp->otf == ETF_EFI || ecp->otf == ETF_PE)
1371839529caSEd Maste 		ecp->oem = elftc_bfd_target_machine(tgt);
1372839529caSEd Maste 
1373a85fe12eSEd Maste 	ecp->otgt = target_name;
1374a85fe12eSEd Maste }
1375a85fe12eSEd Maste 
1376a85fe12eSEd Maste static void
1377a85fe12eSEd Maste set_osabi(struct elfcopy *ecp, const char *abi)
1378a85fe12eSEd Maste {
1379a85fe12eSEd Maste 	int i, found;
1380a85fe12eSEd Maste 
1381a85fe12eSEd Maste 	found = 0;
1382a85fe12eSEd Maste 	for (i = 0; osabis[i].name != NULL; i++)
1383a85fe12eSEd Maste 		if (strcasecmp(osabis[i].name, abi) == 0) {
1384a85fe12eSEd Maste 			ecp->abi = osabis[i].abi;
1385a85fe12eSEd Maste 			found = 1;
1386a85fe12eSEd Maste 			break;
1387a85fe12eSEd Maste 		}
1388a85fe12eSEd Maste 	if (!found)
1389a85fe12eSEd Maste 		errx(EXIT_FAILURE, "unrecognized OSABI %s", abi);
1390a85fe12eSEd Maste }
1391a85fe12eSEd Maste 
1392a85fe12eSEd Maste #define	ELFCOPY_USAGE_MESSAGE	"\
1393a85fe12eSEd Maste Usage: %s [options] infile [outfile]\n\
1394839529caSEd Maste   Transform object files.\n\n\
1395a85fe12eSEd Maste   Options:\n\
1396a85fe12eSEd Maste   -d | -g | --strip-debug      Remove debugging information from the output.\n\
1397a85fe12eSEd Maste   -j SECTION | --only-section=SECTION\n\
1398a85fe12eSEd Maste                                Copy only the named section to the output.\n\
1399a85fe12eSEd Maste   -p | --preserve-dates        Preserve access and modification times.\n\
1400a85fe12eSEd Maste   -w | --wildcard              Use shell-style patterns to name symbols.\n\
1401a85fe12eSEd Maste   -x | --discard-all           Do not copy non-globals to the output.\n\
1402a85fe12eSEd Maste   -I FORMAT | --input-target=FORMAT\n\
140395fd7f26SEd Maste                                Specify object format for the input file.\n\
1404a85fe12eSEd Maste   -K SYM | --keep-symbol=SYM   Copy symbol SYM to the output.\n\
1405a85fe12eSEd Maste   -L SYM | --localize-symbol=SYM\n\
1406a85fe12eSEd Maste                                Make symbol SYM local to the output file.\n\
1407a85fe12eSEd Maste   -N SYM | --strip-symbol=SYM  Do not copy symbol SYM to the output.\n\
140895fd7f26SEd Maste   -O FORMAT | --output-target=FORMAT\n\
140995fd7f26SEd Maste                                Specify object format for the output file.\n\
1410839529caSEd Maste                                FORMAT should be a target name understood by\n\
1411839529caSEd Maste                                elftc_bfd_find_target(3).\n\
1412a85fe12eSEd Maste   -R NAME | --remove-section=NAME\n\
1413a85fe12eSEd Maste                                Remove the named section.\n\
1414a85fe12eSEd Maste   -S | --strip-all             Remove all symbol and relocation information\n\
1415a85fe12eSEd Maste                                from the output.\n\
1416a85fe12eSEd Maste   -V | --version               Print a version identifier and exit.\n\
1417a85fe12eSEd Maste   -W SYM | --weaken-symbol=SYM Mark symbol SYM as weak in the output.\n\
1418a85fe12eSEd Maste   -X | --discard-locals        Do not copy compiler generated symbols to\n\
1419a85fe12eSEd Maste                                the output.\n\
1420a85fe12eSEd Maste   --add-section NAME=FILE      Add the contents of FILE to the ELF object as\n\
1421a85fe12eSEd Maste                                a new section named NAME.\n\
1422a85fe12eSEd Maste   --adjust-section-vma SECTION{=,+,-}VAL | \\\n\
1423a85fe12eSEd Maste     --change-section-address SECTION{=,+,-}VAL\n\
1424a85fe12eSEd Maste                                Set or adjust the VMA and the LMA of the\n\
1425a85fe12eSEd Maste                                named section by VAL.\n\
1426a85fe12eSEd Maste   --adjust-start=INCR | --change-start=INCR\n\
1427a85fe12eSEd Maste                                Add INCR to the start address for the ELF\n\
1428a85fe12eSEd Maste                                object.\n\
1429a85fe12eSEd Maste   --adjust-vma=INCR | --change-addresses=INCR\n\
1430a85fe12eSEd Maste                                Increase the VMA and LMA of all sections by\n\
1431a85fe12eSEd Maste                                INCR.\n\
1432a85fe12eSEd Maste   --adjust-warning | --change-warnings\n\
1433a85fe12eSEd Maste                                Issue warnings for non-existent sections.\n\
1434a85fe12eSEd Maste   --change-section-lma SECTION{=,+,-}VAL\n\
1435a85fe12eSEd Maste                                Set or adjust the LMA address of the named\n\
1436a85fe12eSEd Maste                                section by VAL.\n\
1437a85fe12eSEd Maste   --change-section-vma SECTION{=,+,-}VAL\n\
1438a85fe12eSEd Maste                                Set or adjust the VMA address of the named\n\
1439a85fe12eSEd Maste                                section by VAL.\n\
1440a85fe12eSEd Maste   --gap-fill=VAL               Fill the gaps between sections with bytes\n\
1441a85fe12eSEd Maste                                of value VAL.\n\
144267d97fe7SEd Maste   --localize-hidden            Make all hidden symbols local to the output\n\
144367d97fe7SEd Maste                                file.\n\
1444a85fe12eSEd Maste   --no-adjust-warning| --no-change-warnings\n\
1445a85fe12eSEd Maste                                Do not issue warnings for non-existent\n\
1446a85fe12eSEd Maste                                sections.\n\
1447a85fe12eSEd Maste   --only-keep-debug            Copy only debugging information.\n\
1448a85fe12eSEd Maste   --output-target=FORMAT       Use the specified format for the output.\n\
1449a85fe12eSEd Maste   --pad-to=ADDRESS             Pad the output object upto the given address.\n\
1450a85fe12eSEd Maste   --prefix-alloc-sections=STRING\n\
1451a85fe12eSEd Maste                                Prefix the section names of all the allocated\n\
1452a85fe12eSEd Maste                                sections with STRING.\n\
1453a85fe12eSEd Maste   --prefix-sections=STRING     Prefix the section names of all the sections\n\
1454a85fe12eSEd Maste                                with STRING.\n\
1455a85fe12eSEd Maste   --prefix-symbols=STRING      Prefix the symbol names of all the symbols\n\
1456a85fe12eSEd Maste                                with STRING.\n\
1457a85fe12eSEd Maste   --rename-section OLDNAME=NEWNAME[,FLAGS]\n\
1458a85fe12eSEd Maste                                Rename and optionally change section flags.\n\
1459a85fe12eSEd Maste   --set-section-flags SECTION=FLAGS\n\
1460a85fe12eSEd Maste                                Set section flags for the named section.\n\
1461a85fe12eSEd Maste                                Supported flags are: 'alloc', 'code',\n\
1462a85fe12eSEd Maste                                'contents', 'data', 'debug', 'load',\n\
1463a85fe12eSEd Maste                                'noload', 'readonly', 'rom', and 'shared'.\n\
1464a85fe12eSEd Maste   --set-start=ADDRESS          Set the start address of the ELF object.\n\
1465a85fe12eSEd Maste   --srec-forceS3               Only generate S3 S-Records.\n\
1466a85fe12eSEd Maste   --srec-len=LEN               Set the maximum length of a S-Record line.\n\
1467a85fe12eSEd Maste   --strip-unneeded             Do not copy relocation information.\n"
1468a85fe12eSEd Maste 
1469a85fe12eSEd Maste static void
1470a85fe12eSEd Maste elfcopy_usage(void)
1471a85fe12eSEd Maste {
1472a85fe12eSEd Maste 	(void) fprintf(stderr, ELFCOPY_USAGE_MESSAGE, ELFTC_GETPROGNAME());
1473a85fe12eSEd Maste 	exit(EXIT_FAILURE);
1474a85fe12eSEd Maste }
1475a85fe12eSEd Maste 
1476a85fe12eSEd Maste #define	MCS_USAGE_MESSAGE	"\
1477a85fe12eSEd Maste Usage: %s [options] file...\n\
1478a85fe12eSEd Maste   Manipulate the comment section in an ELF object.\n\n\
1479a85fe12eSEd Maste   Options:\n\
1480a85fe12eSEd Maste   -a STRING        Append 'STRING' to the comment section.\n\
1481a85fe12eSEd Maste   -c               Remove duplicate entries from the comment section.\n\
1482a85fe12eSEd Maste   -d               Delete the comment section.\n\
1483a85fe12eSEd Maste   -h | --help      Print a help message and exit.\n\
1484a85fe12eSEd Maste   -n NAME          Operate on the ELF section with name 'NAME'.\n\
1485a85fe12eSEd Maste   -p               Print the contents of the comment section.\n\
1486a85fe12eSEd Maste   -V | --version   Print a version identifier and exit.\n"
1487a85fe12eSEd Maste 
1488a85fe12eSEd Maste static void
1489a85fe12eSEd Maste mcs_usage(void)
1490a85fe12eSEd Maste {
1491a85fe12eSEd Maste 	(void) fprintf(stderr, MCS_USAGE_MESSAGE, ELFTC_GETPROGNAME());
1492a85fe12eSEd Maste 	exit(EXIT_FAILURE);
1493a85fe12eSEd Maste }
1494a85fe12eSEd Maste 
1495a85fe12eSEd Maste #define	STRIP_USAGE_MESSAGE	"\
1496a85fe12eSEd Maste Usage: %s [options] file...\n\
1497a85fe12eSEd Maste   Discard information from ELF objects.\n\n\
1498a85fe12eSEd Maste   Options:\n\
1499a85fe12eSEd Maste   -d | -g | -S | --strip-debug    Remove debugging symbols.\n\
1500a85fe12eSEd Maste   -h | --help                     Print a help message.\n\
1501839529caSEd Maste   -o FILE | --output-file FILE    Write output to FILE.\n\
1502a85fe12eSEd Maste   --only-keep-debug               Keep debugging information only.\n\
1503a85fe12eSEd Maste   -p | --preserve-dates           Preserve access and modification times.\n\
1504a85fe12eSEd Maste   -s | --strip-all                Remove all symbols.\n\
1505a85fe12eSEd Maste   --strip-unneeded                Remove symbols not needed for relocation\n\
1506a85fe12eSEd Maste                                   processing.\n\
1507a85fe12eSEd Maste   -w | --wildcard                 Use shell-style patterns to name symbols.\n\
1508a85fe12eSEd Maste   -x | --discard-all              Discard all non-global symbols.\n\
1509a85fe12eSEd Maste   -I TGT| --input-target=TGT      (Accepted, but ignored).\n\
1510a85fe12eSEd Maste   -K SYM | --keep-symbol=SYM      Keep symbol 'SYM' in the output.\n\
1511a85fe12eSEd Maste   -N SYM | --strip-symbol=SYM     Remove symbol 'SYM' from the output.\n\
1512a85fe12eSEd Maste   -O TGT | --output-target=TGT    Set the output file format to 'TGT'.\n\
1513a85fe12eSEd Maste   -R SEC | --remove-section=SEC   Remove the section named 'SEC'.\n\
1514a85fe12eSEd Maste   -V | --version                  Print a version identifier and exit.\n\
1515a85fe12eSEd Maste   -X | --discard-locals           Remove compiler-generated local symbols.\n"
1516a85fe12eSEd Maste 
1517a85fe12eSEd Maste static void
1518a85fe12eSEd Maste strip_usage(void)
1519a85fe12eSEd Maste {
1520a85fe12eSEd Maste 	(void) fprintf(stderr, STRIP_USAGE_MESSAGE, ELFTC_GETPROGNAME());
1521a85fe12eSEd Maste 	exit(EXIT_FAILURE);
1522a85fe12eSEd Maste }
1523a85fe12eSEd Maste 
1524a85fe12eSEd Maste static void
1525a85fe12eSEd Maste print_version(void)
1526a85fe12eSEd Maste {
1527a85fe12eSEd Maste 	(void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
1528a85fe12eSEd Maste 	exit(EXIT_SUCCESS);
1529a85fe12eSEd Maste }
1530a85fe12eSEd Maste 
1531a85fe12eSEd Maste int
1532a85fe12eSEd Maste main(int argc, char **argv)
1533a85fe12eSEd Maste {
1534a85fe12eSEd Maste 	struct elfcopy *ecp;
1535a85fe12eSEd Maste 
1536a85fe12eSEd Maste 	if (elf_version(EV_CURRENT) == EV_NONE)
1537a85fe12eSEd Maste 		errx(EXIT_FAILURE, "ELF library initialization failed: %s",
1538a85fe12eSEd Maste 		    elf_errmsg(-1));
1539a85fe12eSEd Maste 
1540a85fe12eSEd Maste 	ecp = calloc(1, sizeof(*ecp));
1541a85fe12eSEd Maste 	if (ecp == NULL)
1542a85fe12eSEd Maste 		err(EXIT_FAILURE, "calloc failed");
1543a85fe12eSEd Maste 	memset(ecp, 0, sizeof(*ecp));
1544a85fe12eSEd Maste 
1545a85fe12eSEd Maste 	ecp->itf = ecp->otf = ETF_ELF;
1546a85fe12eSEd Maste 	ecp->iec = ecp->oec = ELFCLASSNONE;
1547a85fe12eSEd Maste 	ecp->oed = ELFDATANONE;
1548a85fe12eSEd Maste 	ecp->abi = -1;
1549a85fe12eSEd Maste 	/* There is always an empty section. */
1550a85fe12eSEd Maste 	ecp->nos = 1;
1551a85fe12eSEd Maste 	ecp->fill = 0;
1552a85fe12eSEd Maste 
1553a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_seg);
1554a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_sac);
1555a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_sadd);
1556a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_symop);
1557a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_symfile);
1558a85fe12eSEd Maste 	STAILQ_INIT(&ecp->v_arobj);
1559a85fe12eSEd Maste 	TAILQ_INIT(&ecp->v_sec);
1560a85fe12eSEd Maste 
1561a85fe12eSEd Maste 	if ((ecp->progname = ELFTC_GETPROGNAME()) == NULL)
1562a85fe12eSEd Maste 		ecp->progname = "elfcopy";
1563a85fe12eSEd Maste 
1564a85fe12eSEd Maste 	if (strcmp(ecp->progname, "strip") == 0)
1565a85fe12eSEd Maste 		strip_main(ecp, argc, argv);
1566a85fe12eSEd Maste 	else if (strcmp(ecp->progname, "mcs") == 0)
1567a85fe12eSEd Maste 		mcs_main(ecp, argc, argv);
1568a85fe12eSEd Maste 	else
1569a85fe12eSEd Maste 		elfcopy_main(ecp, argc, argv);
1570a85fe12eSEd Maste 
1571a85fe12eSEd Maste 	free_sec_add(ecp);
1572a85fe12eSEd Maste 	free_sec_act(ecp);
1573a85fe12eSEd Maste 	free(ecp);
1574a85fe12eSEd Maste 
1575a85fe12eSEd Maste 	exit(EXIT_SUCCESS);
1576a85fe12eSEd Maste }
1577