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