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
42d003e0d7SEd Maste ELFTC_VCSID("$Id: main.c 3757 2019-06-28 01:15:28Z 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},
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
create_elf(struct elfcopy * ecp)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 /*
375eb81f38aSJohn Baldwin * Write the underlying ehdr. Note that it should be called
376eb81f38aSJohn Baldwin * before elf_setshstrndx() since it will overwrite e->e_shstrndx.
377eb81f38aSJohn Baldwin */
378eb81f38aSJohn Baldwin if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
379eb81f38aSJohn Baldwin errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
380eb81f38aSJohn Baldwin elf_errmsg(-1));
381eb81f38aSJohn Baldwin
382eb81f38aSJohn Baldwin /*
383a85fe12eSEd Maste * First processing of output sections: at this stage we copy the
384a85fe12eSEd Maste * content of each section from input to output object. Section
385a85fe12eSEd Maste * content will be modified and printed (mcs) if need. Also content of
386a85fe12eSEd Maste * relocation section probably will be filtered and updated according
387a85fe12eSEd Maste * to symbol table changes.
388a85fe12eSEd Maste */
389a85fe12eSEd Maste copy_content(ecp);
390a85fe12eSEd Maste
391a85fe12eSEd Maste /*
392a85fe12eSEd Maste * Second processing of output sections: Update section headers.
393a85fe12eSEd Maste * At this stage we set name string index, update st_link and st_info
394a85fe12eSEd Maste * for output sections.
395a85fe12eSEd Maste */
396a85fe12eSEd Maste update_shdr(ecp, 1);
397a85fe12eSEd Maste
398a85fe12eSEd Maste /* Renew oeh to get the updated e_shstrndx. */
399a85fe12eSEd Maste if (gelf_getehdr(ecp->eout, &oeh) == NULL)
400a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
401a85fe12eSEd Maste elf_errmsg(-1));
402a85fe12eSEd Maste
403a85fe12eSEd Maste /*
404a85fe12eSEd Maste * Insert SHDR table into the internal section list as a "pseudo"
405a85fe12eSEd Maste * section, so later it will get sorted and resynced just as "normal"
406a85fe12eSEd Maste * sections.
4073ef90571SEd Maste *
4083ef90571SEd Maste * Under FreeBSD, Binutils objcopy always put the section header
4093ef90571SEd Maste * at the end of all the sections. We want to do the same here.
4103ef90571SEd Maste *
4113ef90571SEd Maste * However, note that the behaviour is still different with Binutils:
4123ef90571SEd Maste * elfcopy checks the FreeBSD OSABI tag to tell whether it needs to
4133ef90571SEd Maste * move the section headers, while Binutils is probably configured
4143ef90571SEd Maste * this way when it's compiled on FreeBSD.
415a85fe12eSEd Maste */
4163ef90571SEd Maste if (oeh.e_ident[EI_OSABI] == ELFOSABI_FREEBSD)
4173ef90571SEd Maste shtab = insert_shtab(ecp, 1);
4183ef90571SEd Maste else
419a85fe12eSEd Maste shtab = insert_shtab(ecp, 0);
420a85fe12eSEd Maste
421a85fe12eSEd Maste /*
422a85fe12eSEd Maste * Resync section offsets in the output object. This is needed
423a85fe12eSEd Maste * because probably sections are modified or new sections are added,
424a85fe12eSEd Maste * as a result overlap/gap might appears.
425a85fe12eSEd Maste */
426a85fe12eSEd Maste resync_sections(ecp);
427a85fe12eSEd Maste
428a85fe12eSEd Maste /* Store SHDR offset in EHDR. */
429a85fe12eSEd Maste oeh.e_shoff = shtab->off;
430a85fe12eSEd Maste
431a85fe12eSEd Maste /* Put program header table immediately after the Elf header. */
432a85fe12eSEd Maste if (ecp->ophnum > 0) {
433a85fe12eSEd Maste oeh.e_phoff = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT);
434a85fe12eSEd Maste if (oeh.e_phoff == 0)
435a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_fsize() failed: %s",
436a85fe12eSEd Maste elf_errmsg(-1));
437a85fe12eSEd Maste }
438a85fe12eSEd Maste
439a85fe12eSEd Maste /*
440a85fe12eSEd Maste * Update ELF object entry point if requested.
441a85fe12eSEd Maste */
442a85fe12eSEd Maste if (ecp->change_addr != 0)
443a85fe12eSEd Maste oeh.e_entry += ecp->change_addr;
444a85fe12eSEd Maste if (ecp->flags & SET_START)
445a85fe12eSEd Maste oeh.e_entry = ecp->set_start;
446a85fe12eSEd Maste if (ecp->change_start != 0)
447a85fe12eSEd Maste oeh.e_entry += ecp->change_start;
448a85fe12eSEd Maste
449a85fe12eSEd Maste /*
450a85fe12eSEd Maste * Update ehdr again before we call elf_update(), since we
451a85fe12eSEd Maste * modified e_shoff and e_phoff.
452a85fe12eSEd Maste */
453a85fe12eSEd Maste if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
454a85fe12eSEd Maste errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
455a85fe12eSEd Maste elf_errmsg(-1));
456a85fe12eSEd Maste
457a85fe12eSEd Maste if (ecp->ophnum > 0)
458a85fe12eSEd Maste copy_phdr(ecp);
459a85fe12eSEd Maste
460a85fe12eSEd Maste /* Write out the output elf object. */
461a85fe12eSEd Maste if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
462a85fe12eSEd Maste errx(EXIT_FAILURE, "elf_update() failed: %s",
463a85fe12eSEd Maste elf_errmsg(-1));
464a85fe12eSEd Maste
465a85fe12eSEd Maste /* Release allocated resource. */
466a85fe12eSEd Maste free_elf(ecp);
467a85fe12eSEd Maste }
468a85fe12eSEd Maste
469a85fe12eSEd Maste void
free_elf(struct elfcopy * ecp)470a85fe12eSEd Maste free_elf(struct elfcopy *ecp)
471a85fe12eSEd Maste {
472a85fe12eSEd Maste struct segment *seg, *seg_temp;
473a85fe12eSEd Maste struct section *sec, *sec_temp;
474a85fe12eSEd Maste
475a85fe12eSEd Maste /* Free internal segment list. */
476a85fe12eSEd Maste if (!STAILQ_EMPTY(&ecp->v_seg)) {
477a85fe12eSEd Maste STAILQ_FOREACH_SAFE(seg, &ecp->v_seg, seg_list, seg_temp) {
478a85fe12eSEd Maste STAILQ_REMOVE(&ecp->v_seg, seg, segment, seg_list);
479a85fe12eSEd Maste free(seg);
480a85fe12eSEd Maste }
481a85fe12eSEd Maste }
482a85fe12eSEd Maste
483a85fe12eSEd Maste /* Free symbol table buffers. */
484a85fe12eSEd Maste free_symtab(ecp);
485a85fe12eSEd Maste
486bc589b72SMark Johnston /* Free section name string table. */
487bc589b72SMark Johnston elftc_string_table_destroy(ecp->shstrtab->strtab);
488bc589b72SMark Johnston
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
5135ac70383SChris Rees /*
5145ac70383SChris Rees * Remove a temporary file, without freeing its filename.
5155ac70383SChris Rees *
5165ac70383SChris Rees * Safe to pass NULL, will just ignore it.
5175ac70383SChris Rees */
5185ac70383SChris Rees int
cleanup_tempfile(char * fn)5195ac70383SChris Rees cleanup_tempfile(char *fn)
5205ac70383SChris Rees {
5215ac70383SChris Rees int errno_save, retval;
5225ac70383SChris Rees
5235ac70383SChris Rees if (fn == NULL)
5245ac70383SChris Rees return 0;
5255ac70383SChris Rees errno_save = errno;
5265ac70383SChris Rees if ((retval = unlink(fn)) < 0) {
5275ac70383SChris Rees warn("unlink tempfile %s failed", fn);
5285ac70383SChris Rees errno = errno_save;
5295ac70383SChris Rees return retval;
5305ac70383SChris Rees }
5315ac70383SChris Rees return 0;
5325ac70383SChris Rees }
5335ac70383SChris Rees
534a85fe12eSEd Maste /* Create a temporary file. */
535a85fe12eSEd Maste void
create_tempfile(const char * src,char ** fn,int * fd)5361e4896b1SDimitry Andric create_tempfile(const char *src, char **fn, int *fd)
537a85fe12eSEd Maste {
5381e4896b1SDimitry Andric static const char _TEMPDIR[] = "/tmp/";
5391e4896b1SDimitry Andric static const char _TEMPFILE[] = "ecp.XXXXXXXX";
540a85fe12eSEd Maste const char *tmpdir;
5411e4896b1SDimitry Andric char *tmpf;
5421e4896b1SDimitry Andric size_t tlen, slen, plen;
543a85fe12eSEd Maste
544a85fe12eSEd Maste if (fn == NULL || fd == NULL)
545a85fe12eSEd Maste return;
5461e4896b1SDimitry Andric for (;;) {
5471e4896b1SDimitry Andric if (src == NULL) {
5481e4896b1SDimitry Andric /* Respect TMPDIR environment variable. */
549a85fe12eSEd Maste tmpdir = getenv("TMPDIR");
5501e4896b1SDimitry Andric if (tmpdir == NULL || *tmpdir == '\0')
5511e4896b1SDimitry Andric tmpdir = _TEMPDIR;
552a85fe12eSEd Maste tlen = strlen(tmpdir);
5531e4896b1SDimitry Andric slen = tmpdir[tlen - 1] == '/' ? 0 : 1;
5541e4896b1SDimitry Andric } else {
5551e4896b1SDimitry Andric /* Create temporary file relative to source file. */
5561e4896b1SDimitry Andric if ((tmpdir = strrchr(src, '/')) == NULL) {
5571e4896b1SDimitry Andric /* No path, only use a template filename. */
5581e4896b1SDimitry Andric tlen = 0;
5591e4896b1SDimitry Andric } else {
5601e4896b1SDimitry Andric /* Append the template after the slash. */
5611e4896b1SDimitry Andric tlen = ++tmpdir - src;
5621e4896b1SDimitry Andric tmpdir = src;
5631e4896b1SDimitry Andric }
5641e4896b1SDimitry Andric slen = 0;
5651e4896b1SDimitry Andric }
5661e4896b1SDimitry Andric plen = strlen(_TEMPFILE) + 1;
5671e4896b1SDimitry Andric tmpf = malloc(tlen + slen + plen);
568a85fe12eSEd Maste if (tmpf == NULL)
569a85fe12eSEd Maste err(EXIT_FAILURE, "malloc failed");
5701e4896b1SDimitry Andric if (tlen > 0)
5711e4896b1SDimitry Andric memcpy(tmpf, tmpdir, tlen);
5721e4896b1SDimitry Andric if (slen > 0)
5731e4896b1SDimitry Andric tmpf[tlen] = '/';
5741e4896b1SDimitry Andric /* Copy template filename including NUL terminator. */
5751e4896b1SDimitry Andric memcpy(tmpf + tlen + slen, _TEMPFILE, plen);
5761e4896b1SDimitry Andric if ((*fd = mkstemp(tmpf)) != -1)
5771e4896b1SDimitry Andric break;
5781e4896b1SDimitry Andric if (errno != EACCES || src == NULL)
579a85fe12eSEd Maste err(EXIT_FAILURE, "mkstemp %s failed", tmpf);
5801e4896b1SDimitry Andric /* Permission denied, try again using TMPDIR or /tmp. */
5811e4896b1SDimitry Andric free(tmpf);
5821e4896b1SDimitry Andric src = NULL;
5831e4896b1SDimitry Andric }
584a85fe12eSEd Maste if (fchmod(*fd, 0644) == -1)
585a85fe12eSEd Maste err(EXIT_FAILURE, "fchmod %s failed", tmpf);
586a85fe12eSEd Maste *fn = tmpf;
587a85fe12eSEd Maste }
588a85fe12eSEd Maste
589272a972bSEd Maste /*
590272a972bSEd Maste * Copy temporary file with path src and file descriptor infd to path dst.
591272a972bSEd Maste * If in_place is set act as if editing the file in place, avoiding rename()
592272a972bSEd Maste * to preserve hard and symbolic links. Output file remains open, with file
593272a972bSEd Maste * descriptor returned in outfd.
594272a972bSEd Maste */
595a85fe12eSEd Maste static int
copy_from_tempfile(const char * src,const char * dst,int infd,int * outfd,int in_place)596272a972bSEd Maste copy_from_tempfile(const char *src, const char *dst, int infd, int *outfd,
597272a972bSEd Maste int in_place)
598a85fe12eSEd Maste {
599a85fe12eSEd Maste int tmpfd;
600a85fe12eSEd Maste
601a85fe12eSEd Maste /*
602a85fe12eSEd Maste * First, check if we can use rename().
603a85fe12eSEd Maste */
604272a972bSEd Maste if (in_place == 0) {
605a85fe12eSEd Maste if (rename(src, dst) >= 0) {
606a85fe12eSEd Maste *outfd = infd;
607a85fe12eSEd Maste return (0);
6081e4896b1SDimitry Andric } else if (errno != EXDEV && errno != EACCES)
609a85fe12eSEd Maste return (-1);
610a85fe12eSEd Maste
611a85fe12eSEd Maste /*
612a85fe12eSEd Maste * If the rename() failed due to 'src' and 'dst' residing in
613a85fe12eSEd Maste * two different file systems, invoke a helper function in
614a85fe12eSEd Maste * libelftc to do the copy.
615a85fe12eSEd Maste */
616a85fe12eSEd Maste
6171e4896b1SDimitry Andric if (errno != EACCES && unlink(dst) < 0)
618a85fe12eSEd Maste return (-1);
619272a972bSEd Maste }
620a85fe12eSEd Maste
621272a972bSEd Maste if ((tmpfd = open(dst, O_CREAT | O_TRUNC | O_WRONLY, 0755)) < 0)
622a85fe12eSEd Maste return (-1);
623a85fe12eSEd Maste
624640ff6edSMark Johnston if (elftc_copyfile(infd, tmpfd) < 0) {
625640ff6edSMark Johnston (void) close(tmpfd);
626a85fe12eSEd Maste return (-1);
627640ff6edSMark Johnston }
628a85fe12eSEd Maste
629a85fe12eSEd Maste /*
630a85fe12eSEd Maste * Remove the temporary file from the file system
631a85fe12eSEd Maste * namespace, and close its file descriptor.
632a85fe12eSEd Maste */
633640ff6edSMark Johnston if (unlink(src) < 0) {
634640ff6edSMark Johnston (void) close(tmpfd);
635a85fe12eSEd Maste return (-1);
636640ff6edSMark Johnston }
637a85fe12eSEd Maste
638a85fe12eSEd Maste (void) close(infd);
639a85fe12eSEd Maste
640a85fe12eSEd Maste /*
641a85fe12eSEd Maste * Return the file descriptor for the destination.
642a85fe12eSEd Maste */
643a85fe12eSEd Maste *outfd = tmpfd;
644a85fe12eSEd Maste
645a85fe12eSEd Maste return (0);
646a85fe12eSEd Maste }
647a85fe12eSEd Maste
648a85fe12eSEd Maste static void
create_file(struct elfcopy * ecp,const char * src,const char * dst)649a85fe12eSEd Maste create_file(struct elfcopy *ecp, const char *src, const char *dst)
650a85fe12eSEd Maste {
651a85fe12eSEd Maste struct stat sb;
652a85fe12eSEd Maste char *tempfile, *elftemp;
653a85fe12eSEd Maste int efd, ifd, ofd, ofd0, tfd;
654272a972bSEd Maste int in_place;
655a85fe12eSEd Maste
656a85fe12eSEd Maste tempfile = NULL;
657a85fe12eSEd Maste
658a85fe12eSEd Maste if (src == NULL)
659a85fe12eSEd Maste errx(EXIT_FAILURE, "internal: src == NULL");
660a85fe12eSEd Maste if ((ifd = open(src, O_RDONLY)) == -1)
661a85fe12eSEd Maste err(EXIT_FAILURE, "open %s failed", src);
662a85fe12eSEd Maste
663a85fe12eSEd Maste if (fstat(ifd, &sb) == -1)
664a85fe12eSEd Maste err(EXIT_FAILURE, "fstat %s failed", src);
665a85fe12eSEd Maste
666a85fe12eSEd Maste if (dst == NULL)
6671e4896b1SDimitry Andric create_tempfile(src, &tempfile, &ofd);
668a85fe12eSEd Maste else
669a85fe12eSEd Maste if ((ofd = open(dst, O_RDWR|O_CREAT, 0755)) == -1)
670a85fe12eSEd Maste err(EXIT_FAILURE, "open %s failed", dst);
671a85fe12eSEd Maste
672a85fe12eSEd Maste #ifndef LIBELF_AR
673a85fe12eSEd Maste /* Detect and process ar(1) archive using libarchive. */
674a85fe12eSEd Maste if (ac_detect_ar(ifd)) {
675a85fe12eSEd Maste ac_create_ar(ecp, ifd, ofd);
676a85fe12eSEd Maste goto copy_done;
677a85fe12eSEd Maste }
678a85fe12eSEd Maste #endif
679a85fe12eSEd Maste
6805ac70383SChris Rees if (lseek(ifd, 0, SEEK_SET) < 0) {
6815ac70383SChris Rees cleanup_tempfile(tempfile);
682a85fe12eSEd Maste err(EXIT_FAILURE, "lseek failed");
6835ac70383SChris Rees }
684a85fe12eSEd Maste
685a85fe12eSEd Maste /*
686a85fe12eSEd Maste * If input object is not ELF file, convert it to an intermediate
687a85fe12eSEd Maste * ELF object before processing.
688a85fe12eSEd Maste */
689a85fe12eSEd Maste if (ecp->itf != ETF_ELF) {
6903884d6f8SEd Maste /*
6913884d6f8SEd Maste * If the output object is not an ELF file, choose an arbitrary
6923884d6f8SEd Maste * ELF format for the intermediate file. srec, ihex and binary
6933884d6f8SEd Maste * formats are independent of class, endianness and machine
6943884d6f8SEd Maste * type so these choices do not affect the output.
6953884d6f8SEd Maste */
6963884d6f8SEd Maste if (ecp->otf != ETF_ELF) {
6973884d6f8SEd Maste if (ecp->oec == ELFCLASSNONE)
6983884d6f8SEd Maste ecp->oec = ELFCLASS64;
6993884d6f8SEd Maste if (ecp->oed == ELFDATANONE)
7003884d6f8SEd Maste ecp->oed = ELFDATA2LSB;
7013884d6f8SEd Maste }
7021e4896b1SDimitry Andric create_tempfile(src, &elftemp, &efd);
7035ac70383SChris Rees if ((ecp->eout = elf_begin(efd, ELF_C_WRITE, NULL)) == NULL) {
7045ac70383SChris Rees cleanup_tempfile(elftemp);
7055ac70383SChris Rees cleanup_tempfile(tempfile);
706a85fe12eSEd Maste errx(EXIT_FAILURE, "elf_begin() failed: %s",
707a85fe12eSEd Maste elf_errmsg(-1));
7085ac70383SChris Rees }
709a85fe12eSEd Maste elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT);
710a85fe12eSEd Maste if (ecp->itf == ETF_BINARY)
711a85fe12eSEd Maste create_elf_from_binary(ecp, ifd, src);
712a85fe12eSEd Maste else if (ecp->itf == ETF_IHEX)
713a85fe12eSEd Maste create_elf_from_ihex(ecp, ifd);
714a85fe12eSEd Maste else if (ecp->itf == ETF_SREC)
715a85fe12eSEd Maste create_elf_from_srec(ecp, ifd);
7165ac70383SChris Rees else {
7175ac70383SChris Rees cleanup_tempfile(elftemp);
7185ac70383SChris Rees cleanup_tempfile(tempfile);
719a85fe12eSEd Maste errx(EXIT_FAILURE, "Internal: invalid target flavour");
7205ac70383SChris Rees }
721a85fe12eSEd Maste elf_end(ecp->eout);
722a85fe12eSEd Maste
723a85fe12eSEd Maste /* Open intermediate ELF object as new input object. */
724a85fe12eSEd Maste close(ifd);
7255ac70383SChris Rees if ((ifd = open(elftemp, O_RDONLY)) == -1) {
7265ac70383SChris Rees cleanup_tempfile(elftemp);
7275ac70383SChris Rees cleanup_tempfile(tempfile);
728a85fe12eSEd Maste err(EXIT_FAILURE, "open %s failed", src);
7295ac70383SChris Rees }
730a85fe12eSEd Maste close(efd);
7315ac70383SChris Rees if (cleanup_tempfile(elftemp) < 0) {
7325ac70383SChris Rees cleanup_tempfile(tempfile);
7339bfb310cSEd Maste err(EXIT_FAILURE, "unlink %s failed", elftemp);
7345ac70383SChris Rees }
735a85fe12eSEd Maste free(elftemp);
7365ac70383SChris Rees elftemp = NULL;
737a85fe12eSEd Maste }
738a85fe12eSEd Maste
7395ac70383SChris Rees if ((ecp->ein = elf_begin(ifd, ELF_C_READ, NULL)) == NULL) {
7405ac70383SChris Rees cleanup_tempfile(tempfile);
741*179219eaSEd Maste if (fstat(ifd, &sb) == 0 && sb.st_size == 0)
742*179219eaSEd Maste errx(EXIT_FAILURE, "file format not recognized");
743a85fe12eSEd Maste errx(EXIT_FAILURE, "elf_begin() failed: %s",
744a85fe12eSEd Maste elf_errmsg(-1));
7455ac70383SChris Rees }
746a85fe12eSEd Maste
747a85fe12eSEd Maste switch (elf_kind(ecp->ein)) {
748a85fe12eSEd Maste case ELF_K_NONE:
7495ac70383SChris Rees cleanup_tempfile(tempfile);
750a85fe12eSEd Maste errx(EXIT_FAILURE, "file format not recognized");
751a85fe12eSEd Maste case ELF_K_ELF:
7525ac70383SChris Rees if ((ecp->eout = elf_begin(ofd, ELF_C_WRITE, NULL)) == NULL) {
7535ac70383SChris Rees cleanup_tempfile(tempfile);
754a85fe12eSEd Maste errx(EXIT_FAILURE, "elf_begin() failed: %s",
755a85fe12eSEd Maste elf_errmsg(-1));
7565ac70383SChris Rees }
757a85fe12eSEd Maste
758a85fe12eSEd Maste /* elfcopy(1) manage ELF layout by itself. */
759a85fe12eSEd Maste elf_flagelf(ecp->eout, ELF_C_SET, ELF_F_LAYOUT);
760a85fe12eSEd Maste
761a85fe12eSEd Maste /*
762a85fe12eSEd Maste * Create output ELF object.
763a85fe12eSEd Maste */
764a85fe12eSEd Maste create_elf(ecp);
765a85fe12eSEd Maste elf_end(ecp->eout);
766a85fe12eSEd Maste
767a85fe12eSEd Maste /*
768a85fe12eSEd Maste * Convert the output ELF object to binary/srec/ihex if need.
769a85fe12eSEd Maste */
770a85fe12eSEd Maste if (ecp->otf != ETF_ELF) {
771a85fe12eSEd Maste /*
772a85fe12eSEd Maste * Create (another) tempfile for binary/srec/ihex
773a85fe12eSEd Maste * output object.
774a85fe12eSEd Maste */
7755ac70383SChris Rees if (cleanup_tempfile(tempfile) < 0)
7765ac70383SChris Rees errx(EXIT_FAILURE, "unlink %s failed",
777a85fe12eSEd Maste tempfile);
778a85fe12eSEd Maste free(tempfile);
7791e4896b1SDimitry Andric create_tempfile(src, &tempfile, &ofd0);
780a85fe12eSEd Maste
781a85fe12eSEd Maste
782a85fe12eSEd Maste /*
783a85fe12eSEd Maste * Rewind the file descriptor being processed.
784a85fe12eSEd Maste */
7855ac70383SChris Rees if (lseek(ofd, 0, SEEK_SET) < 0) {
7865ac70383SChris Rees cleanup_tempfile(tempfile);
787a85fe12eSEd Maste err(EXIT_FAILURE,
788a85fe12eSEd Maste "lseek failed for the output object");
7895ac70383SChris Rees }
790a85fe12eSEd Maste
791a85fe12eSEd Maste /*
792a85fe12eSEd Maste * Call flavour-specific conversion routine.
793a85fe12eSEd Maste */
794a85fe12eSEd Maste switch (ecp->otf) {
795a85fe12eSEd Maste case ETF_BINARY:
796a85fe12eSEd Maste create_binary(ofd, ofd0);
797a85fe12eSEd Maste break;
798a85fe12eSEd Maste case ETF_IHEX:
799a85fe12eSEd Maste create_ihex(ofd, ofd0);
800a85fe12eSEd Maste break;
801a85fe12eSEd Maste case ETF_SREC:
802a85fe12eSEd Maste create_srec(ecp, ofd, ofd0,
803a85fe12eSEd Maste dst != NULL ? dst : src);
804a85fe12eSEd Maste break;
805839529caSEd Maste case ETF_PE:
806839529caSEd Maste case ETF_EFI:
807839529caSEd Maste #if WITH_PE
808839529caSEd Maste create_pe(ecp, ofd, ofd0);
809839529caSEd Maste #else
8105ac70383SChris Rees cleanup_tempfile(tempfile);
811839529caSEd Maste errx(EXIT_FAILURE, "PE/EFI support not enabled"
812839529caSEd Maste " at compile time");
813839529caSEd Maste #endif
814839529caSEd Maste break;
815a85fe12eSEd Maste default:
8165ac70383SChris Rees cleanup_tempfile(tempfile);
817a85fe12eSEd Maste errx(EXIT_FAILURE, "Internal: unsupported"
818a85fe12eSEd Maste " output flavour %d", ecp->oec);
819a85fe12eSEd Maste }
820a85fe12eSEd Maste
821a85fe12eSEd Maste close(ofd);
822a85fe12eSEd Maste ofd = ofd0;
823a85fe12eSEd Maste }
824a85fe12eSEd Maste
825a85fe12eSEd Maste break;
826a85fe12eSEd Maste
827a85fe12eSEd Maste case ELF_K_AR:
828a85fe12eSEd Maste /* XXX: Not yet supported. */
829a85fe12eSEd Maste break;
830a85fe12eSEd Maste default:
8315ac70383SChris Rees cleanup_tempfile(tempfile);
832a85fe12eSEd Maste errx(EXIT_FAILURE, "file format not supported");
833a85fe12eSEd Maste }
834a85fe12eSEd Maste
835a85fe12eSEd Maste elf_end(ecp->ein);
836a85fe12eSEd Maste
837a85fe12eSEd Maste #ifndef LIBELF_AR
838a85fe12eSEd Maste copy_done:
839a85fe12eSEd Maste #endif
840a85fe12eSEd Maste
841a85fe12eSEd Maste if (tempfile != NULL) {
842272a972bSEd Maste in_place = 0;
843272a972bSEd Maste if (dst == NULL) {
844a85fe12eSEd Maste dst = src;
845272a972bSEd Maste if (lstat(dst, &sb) != -1 &&
846272a972bSEd Maste (sb.st_nlink > 1 || S_ISLNK(sb.st_mode)))
847272a972bSEd Maste in_place = 1;
848272a972bSEd Maste }
849a85fe12eSEd Maste
8505ac70383SChris Rees if (copy_from_tempfile(tempfile, dst, ofd,
8515ac70383SChris Rees &tfd, in_place) < 0) {
8525ac70383SChris Rees cleanup_tempfile(tempfile);
853a85fe12eSEd Maste err(EXIT_FAILURE, "creation of %s failed", dst);
8545ac70383SChris Rees }
855a85fe12eSEd Maste
8565ac70383SChris Rees /* 'tempfile' has been removed by copy_from_tempfile(). */
857a85fe12eSEd Maste free(tempfile);
858a85fe12eSEd Maste tempfile = NULL;
859a85fe12eSEd Maste
860a85fe12eSEd Maste ofd = tfd;
861a85fe12eSEd Maste }
862a85fe12eSEd Maste
863a85fe12eSEd Maste if (strcmp(dst, "/dev/null") && fchmod(ofd, sb.st_mode) == -1)
864a85fe12eSEd Maste err(EXIT_FAILURE, "fchmod %s failed", dst);
865a85fe12eSEd Maste
866a85fe12eSEd Maste if ((ecp->flags & PRESERVE_DATE) &&
867a85fe12eSEd Maste elftc_set_timestamps(dst, &sb) < 0)
868a85fe12eSEd Maste err(EXIT_FAILURE, "setting timestamps failed");
869a85fe12eSEd Maste
870a85fe12eSEd Maste close(ifd);
871a85fe12eSEd Maste close(ofd);
872a85fe12eSEd Maste }
873a85fe12eSEd Maste
874a85fe12eSEd Maste static void
elfcopy_main(struct elfcopy * ecp,int argc,char ** argv)875a85fe12eSEd Maste elfcopy_main(struct elfcopy *ecp, int argc, char **argv)
876a85fe12eSEd Maste {
877a85fe12eSEd Maste struct sec_action *sac;
878a85fe12eSEd Maste const char *infile, *outfile;
879a85fe12eSEd Maste char *fn, *s;
880a85fe12eSEd Maste int opt;
881a85fe12eSEd Maste
882a85fe12eSEd Maste while ((opt = getopt_long(argc, argv, "dB:gG:I:j:K:L:N:O:pR:s:SwW:xXV",
883a85fe12eSEd Maste elfcopy_longopts, NULL)) != -1) {
884a85fe12eSEd Maste switch(opt) {
885a85fe12eSEd Maste case 'B':
886a85fe12eSEd Maste /* ignored */
887a85fe12eSEd Maste break;
888a85fe12eSEd Maste case 'R':
889a85fe12eSEd Maste sac = lookup_sec_act(ecp, optarg, 1);
890a85fe12eSEd Maste if (sac->copy != 0)
891a85fe12eSEd Maste errx(EXIT_FAILURE,
892a85fe12eSEd Maste "both copy and remove specified");
893a85fe12eSEd Maste sac->remove = 1;
894a85fe12eSEd Maste ecp->flags |= SEC_REMOVE;
895a85fe12eSEd Maste break;
896a85fe12eSEd Maste case 'S':
897a85fe12eSEd Maste ecp->strip = STRIP_ALL;
898a85fe12eSEd Maste break;
899a85fe12eSEd Maste case 'g':
900a85fe12eSEd Maste ecp->strip = STRIP_DEBUG;
901a85fe12eSEd Maste break;
902a85fe12eSEd Maste case 'G':
903a85fe12eSEd Maste ecp->flags |= KEEP_GLOBAL;
904a85fe12eSEd Maste add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEPG);
905a85fe12eSEd Maste break;
906a85fe12eSEd Maste case 'I':
907a85fe12eSEd Maste case 's':
908a85fe12eSEd Maste set_input_target(ecp, optarg);
909a85fe12eSEd Maste break;
910a85fe12eSEd Maste case 'j':
911a85fe12eSEd Maste sac = lookup_sec_act(ecp, optarg, 1);
912a85fe12eSEd Maste if (sac->remove != 0)
913a85fe12eSEd Maste errx(EXIT_FAILURE,
914a85fe12eSEd Maste "both copy and remove specified");
915a85fe12eSEd Maste sac->copy = 1;
916a85fe12eSEd Maste ecp->flags |= SEC_COPY;
917a85fe12eSEd Maste break;
918a85fe12eSEd Maste case 'K':
919a85fe12eSEd Maste add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP);
920a85fe12eSEd Maste break;
921a85fe12eSEd Maste case 'L':
922a85fe12eSEd Maste add_to_symop_list(ecp, optarg, NULL, SYMOP_LOCALIZE);
923a85fe12eSEd Maste break;
924a85fe12eSEd Maste case 'N':
925a85fe12eSEd Maste add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP);
926a85fe12eSEd Maste break;
927a85fe12eSEd Maste case 'O':
928a85fe12eSEd Maste set_output_target(ecp, optarg);
929a85fe12eSEd Maste break;
930a85fe12eSEd Maste case 'p':
931a85fe12eSEd Maste ecp->flags |= PRESERVE_DATE;
932a85fe12eSEd Maste break;
933a85fe12eSEd Maste case 'V':
934a85fe12eSEd Maste print_version();
935a85fe12eSEd Maste break;
936a85fe12eSEd Maste case 'w':
937a85fe12eSEd Maste ecp->flags |= WILDCARD;
938a85fe12eSEd Maste break;
939a85fe12eSEd Maste case 'W':
940a85fe12eSEd Maste add_to_symop_list(ecp, optarg, NULL, SYMOP_WEAKEN);
941a85fe12eSEd Maste break;
942a85fe12eSEd Maste case 'x':
943a85fe12eSEd Maste ecp->flags |= DISCARD_LOCAL;
944a85fe12eSEd Maste break;
945a85fe12eSEd Maste case 'X':
946a85fe12eSEd Maste ecp->flags |= DISCARD_LLABEL;
947a85fe12eSEd Maste break;
948a85fe12eSEd Maste case ECP_ADD_GNU_DEBUGLINK:
949a85fe12eSEd Maste ecp->debuglink = optarg;
950a85fe12eSEd Maste break;
951a85fe12eSEd Maste case ECP_ADD_SECTION:
952a85fe12eSEd Maste add_section(ecp, optarg);
953a85fe12eSEd Maste break;
954a85fe12eSEd Maste case ECP_CHANGE_ADDR:
955a85fe12eSEd Maste ecp->change_addr = (int64_t) strtoll(optarg, NULL, 0);
956a85fe12eSEd Maste break;
957a85fe12eSEd Maste case ECP_CHANGE_SEC_ADDR:
958a85fe12eSEd Maste parse_sec_address_op(ecp, opt, "--change-section-addr",
959a85fe12eSEd Maste optarg);
960a85fe12eSEd Maste break;
961a85fe12eSEd Maste case ECP_CHANGE_SEC_LMA:
962a85fe12eSEd Maste parse_sec_address_op(ecp, opt, "--change-section-lma",
963a85fe12eSEd Maste optarg);
964a85fe12eSEd Maste break;
965a85fe12eSEd Maste case ECP_CHANGE_SEC_VMA:
966a85fe12eSEd Maste parse_sec_address_op(ecp, opt, "--change-section-vma",
967a85fe12eSEd Maste optarg);
968a85fe12eSEd Maste break;
969a85fe12eSEd Maste case ECP_CHANGE_START:
970a85fe12eSEd Maste ecp->change_start = (int64_t) strtoll(optarg, NULL, 0);
971a85fe12eSEd Maste break;
972a85fe12eSEd Maste case ECP_CHANGE_WARN:
973a85fe12eSEd Maste /* default */
974a85fe12eSEd Maste break;
975a85fe12eSEd Maste case ECP_GAP_FILL:
976a85fe12eSEd Maste ecp->fill = (uint8_t) strtoul(optarg, NULL, 0);
977a85fe12eSEd Maste ecp->flags |= GAP_FILL;
978a85fe12eSEd Maste break;
979a85fe12eSEd Maste case ECP_GLOBALIZE_SYMBOL:
980a85fe12eSEd Maste add_to_symop_list(ecp, optarg, NULL, SYMOP_GLOBALIZE);
981a85fe12eSEd Maste break;
982a85fe12eSEd Maste case ECP_GLOBALIZE_SYMBOLS:
983a85fe12eSEd Maste parse_symlist_file(ecp, optarg, SYMOP_GLOBALIZE);
984a85fe12eSEd Maste break;
985a85fe12eSEd Maste case ECP_KEEP_SYMBOLS:
986a85fe12eSEd Maste parse_symlist_file(ecp, optarg, SYMOP_KEEP);
987a85fe12eSEd Maste break;
988a85fe12eSEd Maste case ECP_KEEP_GLOBAL_SYMBOLS:
989a85fe12eSEd Maste parse_symlist_file(ecp, optarg, SYMOP_KEEPG);
990a85fe12eSEd Maste break;
99167d97fe7SEd Maste case ECP_LOCALIZE_HIDDEN:
99267d97fe7SEd Maste ecp->flags |= LOCALIZE_HIDDEN;
99367d97fe7SEd Maste break;
994a85fe12eSEd Maste case ECP_LOCALIZE_SYMBOLS:
995a85fe12eSEd Maste parse_symlist_file(ecp, optarg, SYMOP_LOCALIZE);
996a85fe12eSEd Maste break;
997a85fe12eSEd Maste case ECP_NO_CHANGE_WARN:
998a85fe12eSEd Maste ecp->flags |= NO_CHANGE_WARN;
999a85fe12eSEd Maste break;
1000a85fe12eSEd Maste case ECP_ONLY_DEBUG:
1001a85fe12eSEd Maste ecp->strip = STRIP_NONDEBUG;
1002a85fe12eSEd Maste break;
100367d97fe7SEd Maste case ECP_ONLY_DWO:
100467d97fe7SEd Maste ecp->strip = STRIP_NONDWO;
100567d97fe7SEd Maste break;
1006a85fe12eSEd Maste case ECP_PAD_TO:
1007a85fe12eSEd Maste ecp->pad_to = (uint64_t) strtoull(optarg, NULL, 0);
1008a85fe12eSEd Maste break;
1009a85fe12eSEd Maste case ECP_PREFIX_ALLOC:
1010a85fe12eSEd Maste ecp->prefix_alloc = optarg;
1011a85fe12eSEd Maste break;
1012a85fe12eSEd Maste case ECP_PREFIX_SEC:
1013a85fe12eSEd Maste ecp->prefix_sec = optarg;
1014a85fe12eSEd Maste break;
1015a85fe12eSEd Maste case ECP_PREFIX_SYM:
1016a85fe12eSEd Maste ecp->prefix_sym = optarg;
1017a85fe12eSEd Maste break;
1018a85fe12eSEd Maste case ECP_REDEF_SYMBOL:
1019a85fe12eSEd Maste if ((s = strchr(optarg, '=')) == NULL)
1020a85fe12eSEd Maste errx(EXIT_FAILURE,
1021a85fe12eSEd Maste "illegal format for --redefine-sym");
1022a85fe12eSEd Maste *s++ = '\0';
1023a85fe12eSEd Maste add_to_symop_list(ecp, optarg, s, SYMOP_REDEF);
1024a85fe12eSEd Maste break;
1025a85fe12eSEd Maste case ECP_REDEF_SYMBOLS:
1026a85fe12eSEd Maste parse_symlist_file(ecp, optarg, SYMOP_REDEF);
1027a85fe12eSEd Maste break;
1028a85fe12eSEd Maste case ECP_RENAME_SECTION:
1029a85fe12eSEd Maste if ((fn = strchr(optarg, '=')) == NULL)
1030a85fe12eSEd Maste errx(EXIT_FAILURE,
1031a85fe12eSEd Maste "illegal format for --rename-section");
1032a85fe12eSEd Maste *fn++ = '\0';
1033a85fe12eSEd Maste
1034a85fe12eSEd Maste /* Check for optional flags. */
1035a85fe12eSEd Maste if ((s = strchr(fn, ',')) != NULL)
1036a85fe12eSEd Maste *s++ = '\0';
1037a85fe12eSEd Maste
1038a85fe12eSEd Maste sac = lookup_sec_act(ecp, optarg, 1);
1039a85fe12eSEd Maste sac->rename = 1;
1040a85fe12eSEd Maste sac->newname = fn;
1041a85fe12eSEd Maste if (s != NULL)
1042a85fe12eSEd Maste parse_sec_flags(sac, s);
1043a85fe12eSEd Maste break;
1044a85fe12eSEd Maste case ECP_SET_OSABI:
1045a85fe12eSEd Maste set_osabi(ecp, optarg);
1046a85fe12eSEd Maste break;
1047a85fe12eSEd Maste case ECP_SET_SEC_FLAGS:
1048a85fe12eSEd Maste if ((s = strchr(optarg, '=')) == NULL)
1049a85fe12eSEd Maste errx(EXIT_FAILURE,
1050a85fe12eSEd Maste "illegal format for --set-section-flags");
1051a85fe12eSEd Maste *s++ = '\0';
1052a85fe12eSEd Maste sac = lookup_sec_act(ecp, optarg, 1);
1053a85fe12eSEd Maste parse_sec_flags(sac, s);
1054a85fe12eSEd Maste break;
1055a85fe12eSEd Maste case ECP_SET_START:
1056a85fe12eSEd Maste ecp->flags |= SET_START;
1057a85fe12eSEd Maste ecp->set_start = (uint64_t) strtoull(optarg, NULL, 0);
1058a85fe12eSEd Maste break;
1059a85fe12eSEd Maste case ECP_SREC_FORCE_S3:
1060a85fe12eSEd Maste ecp->flags |= SREC_FORCE_S3;
1061a85fe12eSEd Maste break;
1062a85fe12eSEd Maste case ECP_SREC_LEN:
1063a85fe12eSEd Maste ecp->flags |= SREC_FORCE_LEN;
1064a85fe12eSEd Maste ecp->srec_len = strtoul(optarg, NULL, 0);
1065a85fe12eSEd Maste break;
106667d97fe7SEd Maste case ECP_STRIP_DWO:
106767d97fe7SEd Maste ecp->strip = STRIP_DWO;
106867d97fe7SEd Maste break;
1069a85fe12eSEd Maste case ECP_STRIP_SYMBOLS:
1070a85fe12eSEd Maste parse_symlist_file(ecp, optarg, SYMOP_STRIP);
1071a85fe12eSEd Maste break;
1072a85fe12eSEd Maste case ECP_STRIP_UNNEEDED:
1073a85fe12eSEd Maste ecp->strip = STRIP_UNNEEDED;
1074a85fe12eSEd Maste break;
1075a85fe12eSEd Maste case ECP_WEAKEN_ALL:
1076a85fe12eSEd Maste ecp->flags |= WEAKEN_ALL;
1077a85fe12eSEd Maste break;
1078a85fe12eSEd Maste case ECP_WEAKEN_SYMBOLS:
1079a85fe12eSEd Maste parse_symlist_file(ecp, optarg, SYMOP_WEAKEN);
1080a85fe12eSEd Maste break;
1081a85fe12eSEd Maste default:
1082a85fe12eSEd Maste elfcopy_usage();
1083a85fe12eSEd Maste }
1084a85fe12eSEd Maste }
1085a85fe12eSEd Maste
1086bbce1017SMark Johnston argc -= optind;
1087bbce1017SMark Johnston argv += optind;
1088bbce1017SMark Johnston
1089bbce1017SMark Johnston if (argc == 0 || argc > 2)
1090a85fe12eSEd Maste elfcopy_usage();
1091a85fe12eSEd Maste
1092bbce1017SMark Johnston infile = argv[0];
1093a85fe12eSEd Maste outfile = NULL;
1094bbce1017SMark Johnston if (argc > 1)
1095bbce1017SMark Johnston outfile = argv[1];
1096a85fe12eSEd Maste
1097a85fe12eSEd Maste create_file(ecp, infile, outfile);
1098a85fe12eSEd Maste }
1099a85fe12eSEd Maste
1100a85fe12eSEd Maste static void
mcs_main(struct elfcopy * ecp,int argc,char ** argv)1101a85fe12eSEd Maste mcs_main(struct elfcopy *ecp, int argc, char **argv)
1102a85fe12eSEd Maste {
1103a85fe12eSEd Maste struct sec_action *sac;
1104a85fe12eSEd Maste const char *string;
1105a85fe12eSEd Maste int append, delete, compress, name, print;
1106a85fe12eSEd Maste int opt, i;
1107a85fe12eSEd Maste
1108a85fe12eSEd Maste append = delete = compress = name = print = 0;
1109a85fe12eSEd Maste string = NULL;
1110a85fe12eSEd Maste while ((opt = getopt_long(argc, argv, "a:cdhn:pV", mcs_longopts,
1111a85fe12eSEd Maste NULL)) != -1) {
1112a85fe12eSEd Maste switch(opt) {
1113a85fe12eSEd Maste case 'a':
1114a85fe12eSEd Maste append = 1;
1115a85fe12eSEd Maste string = optarg; /* XXX multiple -a not supported */
1116a85fe12eSEd Maste break;
1117a85fe12eSEd Maste case 'c':
1118a85fe12eSEd Maste compress = 1;
1119a85fe12eSEd Maste break;
1120a85fe12eSEd Maste case 'd':
1121a85fe12eSEd Maste delete = 1;
1122a85fe12eSEd Maste break;
1123a85fe12eSEd Maste case 'n':
1124a85fe12eSEd Maste name = 1;
1125a85fe12eSEd Maste (void)lookup_sec_act(ecp, optarg, 1);
1126a85fe12eSEd Maste break;
1127a85fe12eSEd Maste case 'p':
1128a85fe12eSEd Maste print = 1;
1129a85fe12eSEd Maste break;
1130a85fe12eSEd Maste case 'V':
1131a85fe12eSEd Maste print_version();
1132a85fe12eSEd Maste break;
1133a85fe12eSEd Maste case 'h':
1134a85fe12eSEd Maste default:
1135a85fe12eSEd Maste mcs_usage();
1136a85fe12eSEd Maste }
1137a85fe12eSEd Maste }
1138a85fe12eSEd Maste
1139bbce1017SMark Johnston argc -= optind;
1140bbce1017SMark Johnston argv += optind;
1141bbce1017SMark Johnston
1142bbce1017SMark Johnston if (argc == 0)
1143a85fe12eSEd Maste mcs_usage();
1144a85fe12eSEd Maste
1145a85fe12eSEd Maste /* Must specify one operation at least. */
1146a85fe12eSEd Maste if (!append && !compress && !delete && !print)
1147a85fe12eSEd Maste mcs_usage();
1148a85fe12eSEd Maste
1149a85fe12eSEd Maste /*
1150a85fe12eSEd Maste * If we are going to delete, ignore other operations. This is
1151a85fe12eSEd Maste * different from the Solaris implementation, which can print
1152a85fe12eSEd Maste * and delete a section at the same time, for example. Also, this
1153a85fe12eSEd Maste * implementation do not respect the order between operations that
1154a85fe12eSEd Maste * user specified, i.e., "mcs -pc a.out" equals to "mcs -cp a.out".
1155a85fe12eSEd Maste */
1156a85fe12eSEd Maste if (delete) {
1157a85fe12eSEd Maste append = compress = print = 0;
1158a85fe12eSEd Maste ecp->flags |= SEC_REMOVE;
1159a85fe12eSEd Maste }
1160a85fe12eSEd Maste if (append)
1161a85fe12eSEd Maste ecp->flags |= SEC_APPEND;
1162a85fe12eSEd Maste if (compress)
1163a85fe12eSEd Maste ecp->flags |= SEC_COMPRESS;
1164a85fe12eSEd Maste if (print)
1165a85fe12eSEd Maste ecp->flags |= SEC_PRINT;
1166a85fe12eSEd Maste
1167a85fe12eSEd Maste /* .comment is the default section to operate on. */
1168a85fe12eSEd Maste if (!name)
1169a85fe12eSEd Maste (void)lookup_sec_act(ecp, ".comment", 1);
1170a85fe12eSEd Maste
1171a85fe12eSEd Maste STAILQ_FOREACH(sac, &ecp->v_sac, sac_list) {
1172a85fe12eSEd Maste sac->append = append;
1173a85fe12eSEd Maste sac->compress = compress;
1174a85fe12eSEd Maste sac->print = print;
1175a85fe12eSEd Maste sac->remove = delete;
1176a85fe12eSEd Maste sac->string = string;
1177a85fe12eSEd Maste }
1178a85fe12eSEd Maste
1179bbce1017SMark Johnston for (i = 0; i < argc; i++) {
1180a85fe12eSEd Maste /* If only -p is specified, output to /dev/null */
1181a85fe12eSEd Maste if (print && !append && !compress && !delete)
1182a85fe12eSEd Maste create_file(ecp, argv[i], "/dev/null");
1183a85fe12eSEd Maste else
1184a85fe12eSEd Maste create_file(ecp, argv[i], NULL);
1185a85fe12eSEd Maste }
1186a85fe12eSEd Maste }
1187a85fe12eSEd Maste
1188a85fe12eSEd Maste static void
strip_main(struct elfcopy * ecp,int argc,char ** argv)1189a85fe12eSEd Maste strip_main(struct elfcopy *ecp, int argc, char **argv)
1190a85fe12eSEd Maste {
1191a85fe12eSEd Maste struct sec_action *sac;
1192a85fe12eSEd Maste const char *outfile;
1193a85fe12eSEd Maste int opt;
1194a85fe12eSEd Maste int i;
1195a85fe12eSEd Maste
1196a85fe12eSEd Maste outfile = NULL;
1197a85fe12eSEd Maste while ((opt = getopt_long(argc, argv, "hI:K:N:o:O:pR:sSdgVxXw",
1198a85fe12eSEd Maste strip_longopts, NULL)) != -1) {
1199a85fe12eSEd Maste switch(opt) {
1200a85fe12eSEd Maste case 'R':
1201a85fe12eSEd Maste sac = lookup_sec_act(ecp, optarg, 1);
1202a85fe12eSEd Maste sac->remove = 1;
1203a85fe12eSEd Maste ecp->flags |= SEC_REMOVE;
1204a85fe12eSEd Maste break;
1205a85fe12eSEd Maste case 's':
1206a85fe12eSEd Maste ecp->strip = STRIP_ALL;
1207a85fe12eSEd Maste break;
1208a85fe12eSEd Maste case 'S':
1209a85fe12eSEd Maste case 'g':
1210a85fe12eSEd Maste case 'd':
1211a85fe12eSEd Maste ecp->strip = STRIP_DEBUG;
1212a85fe12eSEd Maste break;
1213a85fe12eSEd Maste case 'I':
1214a85fe12eSEd Maste /* ignored */
1215a85fe12eSEd Maste break;
1216a85fe12eSEd Maste case 'K':
1217a85fe12eSEd Maste add_to_symop_list(ecp, optarg, NULL, SYMOP_KEEP);
1218a85fe12eSEd Maste break;
1219a85fe12eSEd Maste case 'N':
1220a85fe12eSEd Maste add_to_symop_list(ecp, optarg, NULL, SYMOP_STRIP);
1221a85fe12eSEd Maste break;
1222a85fe12eSEd Maste case 'o':
1223a85fe12eSEd Maste outfile = optarg;
1224a85fe12eSEd Maste break;
1225a85fe12eSEd Maste case 'O':
1226a85fe12eSEd Maste set_output_target(ecp, optarg);
1227a85fe12eSEd Maste break;
1228a85fe12eSEd Maste case 'p':
1229a85fe12eSEd Maste ecp->flags |= PRESERVE_DATE;
1230a85fe12eSEd Maste break;
1231a85fe12eSEd Maste case 'V':
1232a85fe12eSEd Maste print_version();
1233a85fe12eSEd Maste break;
1234a85fe12eSEd Maste case 'w':
1235a85fe12eSEd Maste ecp->flags |= WILDCARD;
1236a85fe12eSEd Maste break;
1237a85fe12eSEd Maste case 'x':
1238a85fe12eSEd Maste ecp->flags |= DISCARD_LOCAL;
1239a85fe12eSEd Maste break;
1240a85fe12eSEd Maste case 'X':
1241a85fe12eSEd Maste ecp->flags |= DISCARD_LLABEL;
1242a85fe12eSEd Maste break;
1243a85fe12eSEd Maste case ECP_ONLY_DEBUG:
1244a85fe12eSEd Maste ecp->strip = STRIP_NONDEBUG;
1245a85fe12eSEd Maste break;
1246a85fe12eSEd Maste case ECP_STRIP_UNNEEDED:
1247a85fe12eSEd Maste ecp->strip = STRIP_UNNEEDED;
1248a85fe12eSEd Maste break;
1249a85fe12eSEd Maste case 'h':
1250a85fe12eSEd Maste default:
1251a85fe12eSEd Maste strip_usage();
1252a85fe12eSEd Maste }
1253a85fe12eSEd Maste }
1254a85fe12eSEd Maste
1255bbce1017SMark Johnston argc -= optind;
1256bbce1017SMark Johnston argv += optind;
1257bbce1017SMark Johnston
1258a85fe12eSEd Maste if (ecp->strip == 0 &&
1259a85fe12eSEd Maste ((ecp->flags & DISCARD_LOCAL) == 0) &&
1260a356a1f5SEd Maste ((ecp->flags & DISCARD_LLABEL) == 0) &&
1261a356a1f5SEd Maste lookup_symop_list(ecp, NULL, SYMOP_STRIP) == NULL)
1262a85fe12eSEd Maste ecp->strip = STRIP_ALL;
1263bbce1017SMark Johnston if (argc == 0)
1264a85fe12eSEd Maste strip_usage();
126536d78189SMark Johnston /*
126636d78189SMark Johnston * Only accept a single input file if an output file had been
126736d78189SMark Johnston * specified.
126836d78189SMark Johnston */
1269bbce1017SMark Johnston if (outfile != NULL && argc != 1)
127036d78189SMark Johnston strip_usage();
1271a85fe12eSEd Maste
1272bbce1017SMark Johnston for (i = 0; i < argc; i++)
1273a85fe12eSEd Maste create_file(ecp, argv[i], outfile);
1274a85fe12eSEd Maste }
1275a85fe12eSEd Maste
1276a85fe12eSEd Maste static void
parse_sec_flags(struct sec_action * sac,char * s)1277a85fe12eSEd Maste parse_sec_flags(struct sec_action *sac, char *s)
1278a85fe12eSEd Maste {
1279a85fe12eSEd Maste const char *flag;
1280a85fe12eSEd Maste int found, i;
1281a85fe12eSEd Maste
1282a85fe12eSEd Maste for (flag = strtok(s, ","); flag; flag = strtok(NULL, ",")) {
1283a85fe12eSEd Maste found = 0;
1284a85fe12eSEd Maste for (i = 0; sec_flags[i].name != NULL; i++)
1285a85fe12eSEd Maste if (strcasecmp(sec_flags[i].name, flag) == 0) {
1286a85fe12eSEd Maste sac->flags |= sec_flags[i].value;
1287a85fe12eSEd Maste found = 1;
1288a85fe12eSEd Maste break;
1289a85fe12eSEd Maste }
1290a85fe12eSEd Maste if (!found)
1291a85fe12eSEd Maste errx(EXIT_FAILURE, "unrecognized section flag %s",
1292a85fe12eSEd Maste flag);
1293a85fe12eSEd Maste }
1294a85fe12eSEd Maste }
1295a85fe12eSEd Maste
1296a85fe12eSEd Maste static void
parse_sec_address_op(struct elfcopy * ecp,int optnum,const char * optname,char * s)1297a85fe12eSEd Maste parse_sec_address_op(struct elfcopy *ecp, int optnum, const char *optname,
1298a85fe12eSEd Maste char *s)
1299a85fe12eSEd Maste {
1300a85fe12eSEd Maste struct sec_action *sac;
1301a85fe12eSEd Maste const char *name;
1302a85fe12eSEd Maste char *v;
1303a85fe12eSEd Maste char op;
1304a85fe12eSEd Maste
1305a85fe12eSEd Maste name = v = s;
1306a85fe12eSEd Maste do {
1307a85fe12eSEd Maste v++;
1308a85fe12eSEd Maste } while (*v != '\0' && *v != '=' && *v != '+' && *v != '-');
1309a85fe12eSEd Maste if (*v == '\0' || *(v + 1) == '\0')
1310a85fe12eSEd Maste errx(EXIT_FAILURE, "invalid format for %s", optname);
1311a85fe12eSEd Maste op = *v;
1312a85fe12eSEd Maste *v++ = '\0';
1313a85fe12eSEd Maste sac = lookup_sec_act(ecp, name, 1);
1314a85fe12eSEd Maste switch (op) {
1315a85fe12eSEd Maste case '=':
1316a85fe12eSEd Maste if (optnum == ECP_CHANGE_SEC_LMA ||
1317a85fe12eSEd Maste optnum == ECP_CHANGE_SEC_ADDR) {
1318a85fe12eSEd Maste sac->setlma = 1;
1319a85fe12eSEd Maste sac->lma = (uint64_t) strtoull(v, NULL, 0);
1320a85fe12eSEd Maste }
1321a85fe12eSEd Maste if (optnum == ECP_CHANGE_SEC_VMA ||
1322a85fe12eSEd Maste optnum == ECP_CHANGE_SEC_ADDR) {
1323a85fe12eSEd Maste sac->setvma = 1;
1324a85fe12eSEd Maste sac->vma = (uint64_t) strtoull(v, NULL, 0);
1325a85fe12eSEd Maste }
1326a85fe12eSEd Maste break;
1327a85fe12eSEd Maste case '+':
1328a85fe12eSEd Maste if (optnum == ECP_CHANGE_SEC_LMA ||
1329a85fe12eSEd Maste optnum == ECP_CHANGE_SEC_ADDR)
1330a85fe12eSEd Maste sac->lma_adjust = (int64_t) strtoll(v, NULL, 0);
1331a85fe12eSEd Maste if (optnum == ECP_CHANGE_SEC_VMA ||
1332a85fe12eSEd Maste optnum == ECP_CHANGE_SEC_ADDR)
1333a85fe12eSEd Maste sac->vma_adjust = (int64_t) strtoll(v, NULL, 0);
1334a85fe12eSEd Maste break;
1335a85fe12eSEd Maste case '-':
1336a85fe12eSEd Maste if (optnum == ECP_CHANGE_SEC_LMA ||
1337a85fe12eSEd Maste optnum == ECP_CHANGE_SEC_ADDR)
1338a85fe12eSEd Maste sac->lma_adjust = (int64_t) -strtoll(v, NULL, 0);
1339a85fe12eSEd Maste if (optnum == ECP_CHANGE_SEC_VMA ||
1340a85fe12eSEd Maste optnum == ECP_CHANGE_SEC_ADDR)
1341a85fe12eSEd Maste sac->vma_adjust = (int64_t) -strtoll(v, NULL, 0);
1342a85fe12eSEd Maste break;
1343a85fe12eSEd Maste default:
1344a85fe12eSEd Maste break;
1345a85fe12eSEd Maste }
1346a85fe12eSEd Maste }
1347a85fe12eSEd Maste
1348a85fe12eSEd Maste static void
parse_symlist_file(struct elfcopy * ecp,const char * fn,unsigned int op)1349a85fe12eSEd Maste parse_symlist_file(struct elfcopy *ecp, const char *fn, unsigned int op)
1350a85fe12eSEd Maste {
1351a85fe12eSEd Maste struct symfile *sf;
1352a85fe12eSEd Maste struct stat sb;
1353a85fe12eSEd Maste FILE *fp;
1354a85fe12eSEd Maste char *data, *p, *line, *end, *e, *n;
1355a85fe12eSEd Maste
1356a85fe12eSEd Maste if (stat(fn, &sb) == -1)
1357a85fe12eSEd Maste err(EXIT_FAILURE, "stat %s failed", fn);
1358a85fe12eSEd Maste
1359a85fe12eSEd Maste /* Check if we already read and processed this file. */
1360a85fe12eSEd Maste STAILQ_FOREACH(sf, &ecp->v_symfile, symfile_list) {
1361a85fe12eSEd Maste if (sf->dev == sb.st_dev && sf->ino == sb.st_ino)
1362a85fe12eSEd Maste goto process_symfile;
1363a85fe12eSEd Maste }
1364a85fe12eSEd Maste
1365a85fe12eSEd Maste if ((fp = fopen(fn, "r")) == NULL)
1366a85fe12eSEd Maste err(EXIT_FAILURE, "can not open %s", fn);
1367a85fe12eSEd Maste if ((data = malloc(sb.st_size + 1)) == NULL)
1368a85fe12eSEd Maste err(EXIT_FAILURE, "malloc failed");
1369fa903e57SEd Maste if (sb.st_size > 0)
1370fa903e57SEd Maste if (fread(data, sb.st_size, 1, fp) != 1)
1371a85fe12eSEd Maste err(EXIT_FAILURE, "fread failed");
1372a85fe12eSEd Maste fclose(fp);
1373a85fe12eSEd Maste data[sb.st_size] = '\0';
1374a85fe12eSEd Maste
1375a85fe12eSEd Maste if ((sf = calloc(1, sizeof(*sf))) == NULL)
1376a85fe12eSEd Maste err(EXIT_FAILURE, "malloc failed");
1377a85fe12eSEd Maste sf->dev = sb.st_dev;
1378a85fe12eSEd Maste sf->ino = sb.st_ino;
1379a85fe12eSEd Maste sf->size = sb.st_size + 1;
1380a85fe12eSEd Maste sf->data = data;
1381a85fe12eSEd Maste
1382a85fe12eSEd Maste process_symfile:
1383a85fe12eSEd Maste
1384a85fe12eSEd Maste /*
1385a85fe12eSEd Maste * Basically what we do here is to convert EOL to '\0', and remove
1386a85fe12eSEd Maste * leading and trailing whitespaces for each line.
1387a85fe12eSEd Maste */
1388a85fe12eSEd Maste
1389a85fe12eSEd Maste end = sf->data + sf->size;
1390a85fe12eSEd Maste line = NULL;
1391a85fe12eSEd Maste for(p = sf->data; p < end; p++) {
1392a85fe12eSEd Maste if ((*p == '\t' || *p == ' ') && line == NULL)
1393a85fe12eSEd Maste continue;
1394a85fe12eSEd Maste if (*p == '\r' || *p == '\n' || *p == '\0') {
1395a85fe12eSEd Maste *p = '\0';
1396a85fe12eSEd Maste if (line == NULL)
1397a85fe12eSEd Maste continue;
1398a85fe12eSEd Maste
1399a85fe12eSEd Maste /* Skip comment. */
1400a85fe12eSEd Maste if (*line == '#') {
1401a85fe12eSEd Maste line = NULL;
1402a85fe12eSEd Maste continue;
1403a85fe12eSEd Maste }
1404a85fe12eSEd Maste
1405a85fe12eSEd Maste e = p - 1;
1406a85fe12eSEd Maste while(e != line && (*e == '\t' || *e == ' '))
1407a85fe12eSEd Maste *e-- = '\0';
1408a85fe12eSEd Maste if (op != SYMOP_REDEF)
1409a85fe12eSEd Maste add_to_symop_list(ecp, line, NULL, op);
1410a85fe12eSEd Maste else {
1411a85fe12eSEd Maste if (strlen(line) < 3)
1412a85fe12eSEd Maste errx(EXIT_FAILURE,
1413a85fe12eSEd Maste "illegal format for"
1414a85fe12eSEd Maste " --redefine-sym");
1415a85fe12eSEd Maste for(n = line + 1; n < e; n++) {
1416a85fe12eSEd Maste if (*n == ' ' || *n == '\t') {
1417a85fe12eSEd Maste while(*n == ' ' || *n == '\t')
1418a85fe12eSEd Maste *n++ = '\0';
1419a85fe12eSEd Maste break;
1420a85fe12eSEd Maste }
1421a85fe12eSEd Maste }
1422a85fe12eSEd Maste if (n >= e)
1423a85fe12eSEd Maste errx(EXIT_FAILURE,
1424a85fe12eSEd Maste "illegal format for"
1425a85fe12eSEd Maste " --redefine-sym");
1426a85fe12eSEd Maste add_to_symop_list(ecp, line, n, op);
1427a85fe12eSEd Maste }
1428a85fe12eSEd Maste line = NULL;
1429a85fe12eSEd Maste continue;
1430a85fe12eSEd Maste }
1431a85fe12eSEd Maste
1432a85fe12eSEd Maste if (line == NULL)
1433a85fe12eSEd Maste line = p;
1434a85fe12eSEd Maste }
1435a85fe12eSEd Maste }
1436a85fe12eSEd Maste
1437a85fe12eSEd Maste static void
set_input_target(struct elfcopy * ecp,const char * target_name)1438a85fe12eSEd Maste set_input_target(struct elfcopy *ecp, const char *target_name)
1439a85fe12eSEd Maste {
1440a85fe12eSEd Maste Elftc_Bfd_Target *tgt;
1441a85fe12eSEd Maste
1442a85fe12eSEd Maste if ((tgt = elftc_bfd_find_target(target_name)) == NULL)
1443a85fe12eSEd Maste errx(EXIT_FAILURE, "%s: invalid target name", target_name);
1444a85fe12eSEd Maste ecp->itf = elftc_bfd_target_flavor(tgt);
1445a85fe12eSEd Maste }
1446a85fe12eSEd Maste
1447a85fe12eSEd Maste static void
set_output_target(struct elfcopy * ecp,const char * target_name)1448a85fe12eSEd Maste set_output_target(struct elfcopy *ecp, const char *target_name)
1449a85fe12eSEd Maste {
1450a85fe12eSEd Maste Elftc_Bfd_Target *tgt;
1451a85fe12eSEd Maste
1452a85fe12eSEd Maste if ((tgt = elftc_bfd_find_target(target_name)) == NULL)
1453a85fe12eSEd Maste errx(EXIT_FAILURE, "%s: invalid target name", target_name);
1454a85fe12eSEd Maste ecp->otf = elftc_bfd_target_flavor(tgt);
1455a85fe12eSEd Maste if (ecp->otf == ETF_ELF) {
1456a85fe12eSEd Maste ecp->oec = elftc_bfd_target_class(tgt);
1457a85fe12eSEd Maste ecp->oed = elftc_bfd_target_byteorder(tgt);
1458a85fe12eSEd Maste ecp->oem = elftc_bfd_target_machine(tgt);
145957d7e4ccSEd Maste ecp->abi = elftc_bfd_target_osabi(tgt);
1460a85fe12eSEd Maste }
1461839529caSEd Maste if (ecp->otf == ETF_EFI || ecp->otf == ETF_PE)
1462839529caSEd Maste ecp->oem = elftc_bfd_target_machine(tgt);
1463839529caSEd Maste
1464a85fe12eSEd Maste ecp->otgt = target_name;
1465a85fe12eSEd Maste }
1466a85fe12eSEd Maste
1467a85fe12eSEd Maste static void
set_osabi(struct elfcopy * ecp,const char * abi)1468a85fe12eSEd Maste set_osabi(struct elfcopy *ecp, const char *abi)
1469a85fe12eSEd Maste {
1470a85fe12eSEd Maste int i, found;
1471a85fe12eSEd Maste
1472a85fe12eSEd Maste found = 0;
1473a85fe12eSEd Maste for (i = 0; osabis[i].name != NULL; i++)
1474a85fe12eSEd Maste if (strcasecmp(osabis[i].name, abi) == 0) {
1475a85fe12eSEd Maste ecp->abi = osabis[i].abi;
1476a85fe12eSEd Maste found = 1;
1477a85fe12eSEd Maste break;
1478a85fe12eSEd Maste }
1479a85fe12eSEd Maste if (!found)
1480a85fe12eSEd Maste errx(EXIT_FAILURE, "unrecognized OSABI %s", abi);
1481a85fe12eSEd Maste }
1482a85fe12eSEd Maste
1483a85fe12eSEd Maste #define ELFCOPY_USAGE_MESSAGE "\
1484a85fe12eSEd Maste Usage: %s [options] infile [outfile]\n\
1485839529caSEd Maste Transform object files.\n\n\
1486a85fe12eSEd Maste Options:\n\
1487a85fe12eSEd Maste -d | -g | --strip-debug Remove debugging information from the output.\n\
1488a85fe12eSEd Maste -j SECTION | --only-section=SECTION\n\
1489a85fe12eSEd Maste Copy only the named section to the output.\n\
1490a85fe12eSEd Maste -p | --preserve-dates Preserve access and modification times.\n\
1491a85fe12eSEd Maste -w | --wildcard Use shell-style patterns to name symbols.\n\
1492a85fe12eSEd Maste -x | --discard-all Do not copy non-globals to the output.\n\
1493a85fe12eSEd Maste -I FORMAT | --input-target=FORMAT\n\
149495fd7f26SEd Maste Specify object format for the input file.\n\
1495a85fe12eSEd Maste -K SYM | --keep-symbol=SYM Copy symbol SYM to the output.\n\
1496a85fe12eSEd Maste -L SYM | --localize-symbol=SYM\n\
1497a85fe12eSEd Maste Make symbol SYM local to the output file.\n\
1498a85fe12eSEd Maste -N SYM | --strip-symbol=SYM Do not copy symbol SYM to the output.\n\
149995fd7f26SEd Maste -O FORMAT | --output-target=FORMAT\n\
150095fd7f26SEd Maste Specify object format for the output file.\n\
1501839529caSEd Maste FORMAT should be a target name understood by\n\
1502839529caSEd Maste elftc_bfd_find_target(3).\n\
1503a85fe12eSEd Maste -R NAME | --remove-section=NAME\n\
1504a85fe12eSEd Maste Remove the named section.\n\
1505a85fe12eSEd Maste -S | --strip-all Remove all symbol and relocation information\n\
1506a85fe12eSEd Maste from the output.\n\
1507a85fe12eSEd Maste -V | --version Print a version identifier and exit.\n\
1508a85fe12eSEd Maste -W SYM | --weaken-symbol=SYM Mark symbol SYM as weak in the output.\n\
1509a85fe12eSEd Maste -X | --discard-locals Do not copy compiler generated symbols to\n\
1510a85fe12eSEd Maste the output.\n\
1511a85fe12eSEd Maste --add-section NAME=FILE Add the contents of FILE to the ELF object as\n\
1512a85fe12eSEd Maste a new section named NAME.\n\
1513a85fe12eSEd Maste --adjust-section-vma SECTION{=,+,-}VAL | \\\n\
1514a85fe12eSEd Maste --change-section-address SECTION{=,+,-}VAL\n\
1515a85fe12eSEd Maste Set or adjust the VMA and the LMA of the\n\
1516a85fe12eSEd Maste named section by VAL.\n\
1517a85fe12eSEd Maste --adjust-start=INCR | --change-start=INCR\n\
1518a85fe12eSEd Maste Add INCR to the start address for the ELF\n\
1519a85fe12eSEd Maste object.\n\
1520a85fe12eSEd Maste --adjust-vma=INCR | --change-addresses=INCR\n\
1521a85fe12eSEd Maste Increase the VMA and LMA of all sections by\n\
1522a85fe12eSEd Maste INCR.\n\
1523a85fe12eSEd Maste --adjust-warning | --change-warnings\n\
1524a85fe12eSEd Maste Issue warnings for non-existent sections.\n\
1525a85fe12eSEd Maste --change-section-lma SECTION{=,+,-}VAL\n\
1526a85fe12eSEd Maste Set or adjust the LMA address of the named\n\
1527a85fe12eSEd Maste section by VAL.\n\
1528a85fe12eSEd Maste --change-section-vma SECTION{=,+,-}VAL\n\
1529a85fe12eSEd Maste Set or adjust the VMA address of the named\n\
1530a85fe12eSEd Maste section by VAL.\n\
1531a85fe12eSEd Maste --gap-fill=VAL Fill the gaps between sections with bytes\n\
1532a85fe12eSEd Maste of value VAL.\n\
153367d97fe7SEd Maste --localize-hidden Make all hidden symbols local to the output\n\
153467d97fe7SEd Maste file.\n\
1535a85fe12eSEd Maste --no-adjust-warning| --no-change-warnings\n\
1536a85fe12eSEd Maste Do not issue warnings for non-existent\n\
1537a85fe12eSEd Maste sections.\n\
1538a85fe12eSEd Maste --only-keep-debug Copy only debugging information.\n\
1539a85fe12eSEd Maste --output-target=FORMAT Use the specified format for the output.\n\
1540a85fe12eSEd Maste --pad-to=ADDRESS Pad the output object up to the given address.\n\
1541a85fe12eSEd Maste --prefix-alloc-sections=STRING\n\
1542a85fe12eSEd Maste Prefix the section names of all the allocated\n\
1543a85fe12eSEd Maste sections with STRING.\n\
1544a85fe12eSEd Maste --prefix-sections=STRING Prefix the section names of all the sections\n\
1545a85fe12eSEd Maste with STRING.\n\
1546a85fe12eSEd Maste --prefix-symbols=STRING Prefix the symbol names of all the symbols\n\
1547a85fe12eSEd Maste with STRING.\n\
1548a85fe12eSEd Maste --rename-section OLDNAME=NEWNAME[,FLAGS]\n\
1549a85fe12eSEd Maste Rename and optionally change section flags.\n\
1550a85fe12eSEd Maste --set-section-flags SECTION=FLAGS\n\
1551a85fe12eSEd Maste Set section flags for the named section.\n\
1552a85fe12eSEd Maste Supported flags are: 'alloc', 'code',\n\
1553a85fe12eSEd Maste 'contents', 'data', 'debug', 'load',\n\
1554a85fe12eSEd Maste 'noload', 'readonly', 'rom', and 'shared'.\n\
1555a85fe12eSEd Maste --set-start=ADDRESS Set the start address of the ELF object.\n\
1556a85fe12eSEd Maste --srec-forceS3 Only generate S3 S-Records.\n\
1557a85fe12eSEd Maste --srec-len=LEN Set the maximum length of a S-Record line.\n\
1558a85fe12eSEd Maste --strip-unneeded Do not copy relocation information.\n"
1559a85fe12eSEd Maste
1560a85fe12eSEd Maste static void
elfcopy_usage(void)1561a85fe12eSEd Maste elfcopy_usage(void)
1562a85fe12eSEd Maste {
1563a85fe12eSEd Maste (void) fprintf(stderr, ELFCOPY_USAGE_MESSAGE, ELFTC_GETPROGNAME());
1564a85fe12eSEd Maste exit(EXIT_FAILURE);
1565a85fe12eSEd Maste }
1566a85fe12eSEd Maste
1567a85fe12eSEd Maste #define MCS_USAGE_MESSAGE "\
1568a85fe12eSEd Maste Usage: %s [options] file...\n\
1569a85fe12eSEd Maste Manipulate the comment section in an ELF object.\n\n\
1570a85fe12eSEd Maste Options:\n\
1571a85fe12eSEd Maste -a STRING Append 'STRING' to the comment section.\n\
1572a85fe12eSEd Maste -c Remove duplicate entries from the comment section.\n\
1573a85fe12eSEd Maste -d Delete the comment section.\n\
1574a85fe12eSEd Maste -h | --help Print a help message and exit.\n\
1575a85fe12eSEd Maste -n NAME Operate on the ELF section with name 'NAME'.\n\
1576a85fe12eSEd Maste -p Print the contents of the comment section.\n\
1577a85fe12eSEd Maste -V | --version Print a version identifier and exit.\n"
1578a85fe12eSEd Maste
1579a85fe12eSEd Maste static void
mcs_usage(void)1580a85fe12eSEd Maste mcs_usage(void)
1581a85fe12eSEd Maste {
1582a85fe12eSEd Maste (void) fprintf(stderr, MCS_USAGE_MESSAGE, ELFTC_GETPROGNAME());
1583a85fe12eSEd Maste exit(EXIT_FAILURE);
1584a85fe12eSEd Maste }
1585a85fe12eSEd Maste
1586a85fe12eSEd Maste #define STRIP_USAGE_MESSAGE "\
1587a85fe12eSEd Maste Usage: %s [options] file...\n\
1588a85fe12eSEd Maste Discard information from ELF objects.\n\n\
1589a85fe12eSEd Maste Options:\n\
1590a85fe12eSEd Maste -d | -g | -S | --strip-debug Remove debugging symbols.\n\
1591a85fe12eSEd Maste -h | --help Print a help message.\n\
1592839529caSEd Maste -o FILE | --output-file FILE Write output to FILE.\n\
1593a85fe12eSEd Maste --only-keep-debug Keep debugging information only.\n\
1594a85fe12eSEd Maste -p | --preserve-dates Preserve access and modification times.\n\
1595a85fe12eSEd Maste -s | --strip-all Remove all symbols.\n\
1596a85fe12eSEd Maste --strip-unneeded Remove symbols not needed for relocation\n\
1597a85fe12eSEd Maste processing.\n\
1598a85fe12eSEd Maste -w | --wildcard Use shell-style patterns to name symbols.\n\
1599a85fe12eSEd Maste -x | --discard-all Discard all non-global symbols.\n\
1600a85fe12eSEd Maste -I TGT| --input-target=TGT (Accepted, but ignored).\n\
1601a85fe12eSEd Maste -K SYM | --keep-symbol=SYM Keep symbol 'SYM' in the output.\n\
1602a85fe12eSEd Maste -N SYM | --strip-symbol=SYM Remove symbol 'SYM' from the output.\n\
1603a85fe12eSEd Maste -O TGT | --output-target=TGT Set the output file format to 'TGT'.\n\
1604a85fe12eSEd Maste -R SEC | --remove-section=SEC Remove the section named 'SEC'.\n\
1605a85fe12eSEd Maste -V | --version Print a version identifier and exit.\n\
1606a85fe12eSEd Maste -X | --discard-locals Remove compiler-generated local symbols.\n"
1607a85fe12eSEd Maste
1608a85fe12eSEd Maste static void
strip_usage(void)1609a85fe12eSEd Maste strip_usage(void)
1610a85fe12eSEd Maste {
1611a85fe12eSEd Maste (void) fprintf(stderr, STRIP_USAGE_MESSAGE, ELFTC_GETPROGNAME());
1612a85fe12eSEd Maste exit(EXIT_FAILURE);
1613a85fe12eSEd Maste }
1614a85fe12eSEd Maste
1615a85fe12eSEd Maste static void
print_version(void)1616a85fe12eSEd Maste print_version(void)
1617a85fe12eSEd Maste {
1618a85fe12eSEd Maste (void) printf("%s (%s)\n", ELFTC_GETPROGNAME(), elftc_version());
1619a85fe12eSEd Maste exit(EXIT_SUCCESS);
1620a85fe12eSEd Maste }
1621a85fe12eSEd Maste
1622d2972ce0SEd Maste /*
1623d2972ce0SEd Maste * Compare the ending of s with end.
1624d2972ce0SEd Maste */
1625d2972ce0SEd Maste static int
strrcmp(const char * s,const char * end)1626d2972ce0SEd Maste strrcmp(const char *s, const char *end)
1627d2972ce0SEd Maste {
1628d2972ce0SEd Maste size_t endlen, slen;
1629d2972ce0SEd Maste
1630d2972ce0SEd Maste slen = strlen(s);
1631d2972ce0SEd Maste endlen = strlen(end);
1632d2972ce0SEd Maste
1633d2972ce0SEd Maste if (slen >= endlen)
1634d2972ce0SEd Maste s += slen - endlen;
1635d2972ce0SEd Maste return (strcmp(s, end));
1636d2972ce0SEd Maste }
1637d2972ce0SEd Maste
1638a85fe12eSEd Maste int
main(int argc,char ** argv)1639a85fe12eSEd Maste main(int argc, char **argv)
1640a85fe12eSEd Maste {
1641a85fe12eSEd Maste struct elfcopy *ecp;
1642a85fe12eSEd Maste
1643a85fe12eSEd Maste if (elf_version(EV_CURRENT) == EV_NONE)
1644a85fe12eSEd Maste errx(EXIT_FAILURE, "ELF library initialization failed: %s",
1645a85fe12eSEd Maste elf_errmsg(-1));
1646a85fe12eSEd Maste
1647a85fe12eSEd Maste ecp = calloc(1, sizeof(*ecp));
1648a85fe12eSEd Maste if (ecp == NULL)
1649a85fe12eSEd Maste err(EXIT_FAILURE, "calloc failed");
1650a85fe12eSEd Maste
1651a85fe12eSEd Maste ecp->itf = ecp->otf = ETF_ELF;
1652a85fe12eSEd Maste ecp->iec = ecp->oec = ELFCLASSNONE;
1653a85fe12eSEd Maste ecp->oed = ELFDATANONE;
1654a85fe12eSEd Maste ecp->abi = -1;
1655a85fe12eSEd Maste /* There is always an empty section. */
1656a85fe12eSEd Maste ecp->nos = 1;
1657a85fe12eSEd Maste ecp->fill = 0;
1658a85fe12eSEd Maste
1659a85fe12eSEd Maste STAILQ_INIT(&ecp->v_seg);
1660a85fe12eSEd Maste STAILQ_INIT(&ecp->v_sac);
1661a85fe12eSEd Maste STAILQ_INIT(&ecp->v_sadd);
1662a85fe12eSEd Maste STAILQ_INIT(&ecp->v_symop);
1663a85fe12eSEd Maste STAILQ_INIT(&ecp->v_symfile);
1664a85fe12eSEd Maste STAILQ_INIT(&ecp->v_arobj);
1665a85fe12eSEd Maste TAILQ_INIT(&ecp->v_sec);
1666a85fe12eSEd Maste
1667a85fe12eSEd Maste if ((ecp->progname = ELFTC_GETPROGNAME()) == NULL)
1668a85fe12eSEd Maste ecp->progname = "elfcopy";
1669a85fe12eSEd Maste
1670d2972ce0SEd Maste if (strrcmp(ecp->progname, "strip") == 0)
1671a85fe12eSEd Maste strip_main(ecp, argc, argv);
1672d2972ce0SEd Maste else if (strrcmp(ecp->progname, "mcs") == 0)
1673a85fe12eSEd Maste mcs_main(ecp, argc, argv);
1674d2972ce0SEd Maste else {
1675d2972ce0SEd Maste if (strrcmp(ecp->progname, "elfcopy") != 0 &&
1676d2972ce0SEd Maste strrcmp(ecp->progname, "objcopy") != 0)
1677d2972ce0SEd Maste warnx("program mode not known, defaulting to elfcopy");
1678a85fe12eSEd Maste elfcopy_main(ecp, argc, argv);
1679d2972ce0SEd Maste }
1680a85fe12eSEd Maste
1681a85fe12eSEd Maste free_sec_add(ecp);
1682a85fe12eSEd Maste free_sec_act(ecp);
1683a85fe12eSEd Maste free(ecp);
1684a85fe12eSEd Maste
1685a85fe12eSEd Maste exit(EXIT_SUCCESS);
1686a85fe12eSEd Maste }
1687