2 * Copyright (c) 2007-2013 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $Id: elfcopy.h 2970 2013-12-01 15:22:12Z kaiwang27 $
29 #include <sys/queue.h>
36 * User specified symbol operation (strip, keep, localize, globalize,
37 * weaken, rename, etc).
43 #define SYMOP_KEEP 0x0001U
44 #define SYMOP_STRIP 0x0002U
45 #define SYMOP_GLOBALIZE 0x0004U
46 #define SYMOP_LOCALIZE 0x0008U
47 #define SYMOP_KEEPG 0x0010U
48 #define SYMOP_WEAKEN 0x0020U
49 #define SYMOP_REDEF 0x0040U
53 STAILQ_ENTRY(symop) symop_list;
56 /* File containing symbol list. */
64 STAILQ_ENTRY(symfile) symfile_list;
67 /* Sections to copy/remove/rename/... */
78 #define SF_ALLOC 0x0001U
79 #define SF_LOAD 0x0002U
80 #define SF_NOLOAD 0x0004U
81 #define SF_READONLY 0x0008U
82 #define SF_DEBUG 0x0010U
83 #define SF_CODE 0x0020U
84 #define SF_DATA 0x0040U
85 #define SF_ROM 0x0080U
86 #define SF_SHARED 0X0100U
87 #define SF_CONTENTS 0x0200U
101 STAILQ_ENTRY(sec_action) sac_list;
104 /* Sections to add from file. */
110 STAILQ_ENTRY(sec_add) sadd_list;
115 /* Internal data structure for sections. */
117 struct segment *seg; /* containing segment */
118 const char *name; /* section name */
119 char *newname; /* new section name */
120 Elf_Scn *is; /* input scn */
121 Elf_Scn *os; /* output scn */
122 void *buf; /* section content */
123 uint8_t *pad; /* section padding */
124 uint64_t off; /* section offset */
125 uint64_t sz; /* section size */
126 uint64_t cap; /* section capacity */
127 uint64_t align; /* section alignment */
128 uint64_t type; /* section type */
129 uint64_t vma; /* section virtual addr */
130 uint64_t lma; /* section load addr */
131 uint64_t pad_sz;/* section padding size */
132 int loadable; /* whether loadable */
136 TAILQ_ENTRY(section) sec_list; /* next section */
139 /* Internal data structure for segments. */
141 uint64_t addr; /* load addr */
142 uint64_t off; /* file offset */
143 uint64_t fsz; /* file size */
144 uint64_t msz; /* memory size */
145 uint64_t type; /* segment type */
146 int remove; /* whether remove */
147 int nsec; /* number of sections contained */
148 struct section **v_sec; /* list of sections contained */
150 STAILQ_ENTRY(segment) seg_list; /* next segment */
154 * In-memory representation of ar(1) archive member(object).
157 char *name; /* member name */
158 char *buf; /* member content */
159 void *maddr; /* mmap start address */
160 uid_t uid; /* user id */
161 gid_t gid; /* group id */
162 mode_t md; /* octal file permissions */
163 size_t size; /* member size */
164 time_t mtime; /* modification time */
166 STAILQ_ENTRY(ar_obj) objs;
170 * Structure encapsulates the "global" data for "elfcopy" program.
173 const char *progname; /* program name */
174 int iec; /* elfclass of input object */
175 Elftc_Bfd_Target_Flavor itf; /* flavour of input object */
176 Elftc_Bfd_Target_Flavor otf; /* flavour of output object */
177 const char *otgt; /* output target name */
178 int oec; /* elfclass of output object */
179 unsigned char oed; /* endianess of output object */
180 int oem; /* EM_XXX of output object */
181 int abi; /* OSABI of output object */
182 Elf *ein; /* ELF descriptor of input object */
183 Elf *eout; /* ELF descriptor of output object */
184 int iphnum; /* num. of input object phdr entries */
185 int ophnum; /* num. of output object phdr entries */
186 int nos; /* num. of output object sections */
196 #define EXECUTABLE 0x00000001U
197 #define DYNAMIC 0x00000002U
198 #define RELOCATABLE 0x00000004U
199 #define SYMTAB_EXIST 0x00000010U
200 #define SYMTAB_INTACT 0x00000020U
201 #define KEEP_GLOBAL 0x00000040U
202 #define DISCARD_LOCAL 0x00000080U
203 #define WEAKEN_ALL 0x00000100U
204 #define PRESERVE_DATE 0x00001000U
205 #define SREC_FORCE_S3 0x00002000U
206 #define SREC_FORCE_LEN 0x00004000U
207 #define SET_START 0x00008000U
208 #define GAP_FILL 0x00010000U
209 #define WILDCARD 0x00020000U
210 #define NO_CHANGE_WARN 0x00040000U
211 #define SEC_ADD 0x00080000U
212 #define SEC_APPEND 0x00100000U
213 #define SEC_COMPRESS 0x00200000U
214 #define SEC_PRINT 0x00400000U
215 #define SEC_REMOVE 0x00800000U
216 #define SEC_COPY 0x01000000U
217 #define DISCARD_LLABEL 0x02000000U
219 int flags; /* elfcopy run control flags. */
220 int64_t change_addr; /* Section address adjustment. */
221 int64_t change_start; /* Entry point adjustment. */
222 uint64_t set_start; /* Entry point value. */
223 unsigned long srec_len; /* S-Record length. */
224 uint64_t pad_to; /* load address padding. */
225 uint8_t fill; /* gap fill value. */
226 char *prefix_sec; /* section prefix. */
227 char *prefix_alloc; /* alloc section prefix. */
228 char *prefix_sym; /* symbol prefix. */
229 char *debuglink; /* GNU debuglink file. */
230 struct section *symtab; /* .symtab section. */
231 struct section *strtab; /* .strtab section. */
232 struct section *shstrtab; /* .shstrtab section. */
233 uint64_t *secndx; /* section index map. */
234 uint64_t *symndx; /* symbol index map. */
235 unsigned char *v_rel; /* symbols needed by relocation. */
236 unsigned char *v_secsym; /* sections with section symbol. */
237 STAILQ_HEAD(, segment) v_seg; /* list of segments. */
238 STAILQ_HEAD(, sec_action) v_sac;/* list of section operations. */
239 STAILQ_HEAD(, sec_add) v_sadd; /* list of sections to add. */
240 STAILQ_HEAD(, symop) v_symop; /* list of symbols operations. */
241 STAILQ_HEAD(, symfile) v_symfile; /* list of symlist files. */
242 TAILQ_HEAD(, section) v_sec; /* list of sections. */
245 * Fields for the ar(1) archive.
247 char *as; /* buffer for archive string table. */
248 size_t as_sz; /* current size of as table. */
249 size_t as_cap; /* capacity of as table buffer. */
250 uint32_t s_cnt; /* current number of symbols. */
251 uint32_t *s_so; /* symbol offset table. */
252 size_t s_so_cap; /* capacity of so table buffer. */
253 char *s_sn; /* symbol name table */
254 size_t s_sn_cap; /* capacity of sn table buffer. */
255 size_t s_sn_sz; /* current size of sn table. */
256 off_t rela_off; /* offset relative to pseudo members. */
257 STAILQ_HEAD(, ar_obj) v_arobj; /* archive object(member) list. */
260 void add_section(struct elfcopy *_ecp, const char *_optarg);
261 void add_to_shstrtab(struct elfcopy *_ecp, const char *_name);
262 void add_to_symop_list(struct elfcopy *_ecp, const char *_name,
263 const char *_newname, unsigned int _op);
264 void add_to_symtab(struct elfcopy *_ecp, const char *_name,
265 uint64_t _st_value, uint64_t _st_size, uint16_t _st_shndx,
266 unsigned char _st_info, unsigned char _st_other, int _ndx_known);
267 int add_to_inseg_list(struct elfcopy *_ecp, struct section *_sec);
268 void adjust_addr(struct elfcopy *_ecp);
269 void copy_content(struct elfcopy *_ecp);
270 void copy_data(struct section *_s);
271 void copy_phdr(struct elfcopy *_ecp);
272 void copy_shdr(struct elfcopy *_ecp, struct section *_s, const char *_name,
273 int _copy, int _sec_flags);
274 void create_binary(int _ifd, int _ofd);
275 void create_elf(struct elfcopy *_ecp);
276 void create_elf_from_binary(struct elfcopy *_ecp, int _ifd, const char *ifn);
277 void create_elf_from_ihex(struct elfcopy *_ecp, int _ifd);
278 void create_elf_from_srec(struct elfcopy *_ecp, int _ifd);
279 struct section *create_external_section(struct elfcopy *_ecp, const char *_name,
280 char *_newname, void *_buf, uint64_t _size, uint64_t _off, uint64_t _stype,
281 Elf_Type _dtype, uint64_t flags, uint64_t _align, uint64_t _vma,
283 void create_external_symtab(struct elfcopy *_ecp);
284 void create_ihex(int _ifd, int _ofd);
285 void create_scn(struct elfcopy *_ecp);
286 void create_srec(struct elfcopy *_ecp, int _ifd, int _ofd, const char *_ofn);
287 void create_symtab(struct elfcopy *_ecp);
288 void create_symtab_data(struct elfcopy *_ecp);
289 void create_tempfile(char **_fn, int *_fd);
290 void finalize_external_symtab(struct elfcopy *_ecp);
291 void free_elf(struct elfcopy *_ecp);
292 void free_sec_act(struct elfcopy *_ecp);
293 void free_sec_add(struct elfcopy *_ecp);
294 void free_symtab(struct elfcopy *_ecp);
295 void init_shstrtab(struct elfcopy *_ecp);
296 void insert_to_sec_list(struct elfcopy *_ecp, struct section *_sec,
298 struct section *insert_shtab(struct elfcopy *_ecp, int tail);
299 int is_remove_reloc_sec(struct elfcopy *_ecp, uint32_t _sh_info);
300 int is_remove_section(struct elfcopy *_ecp, const char *_name);
301 struct sec_action *lookup_sec_act(struct elfcopy *_ecp,
302 const char *_name, int _add);
303 struct symop *lookup_symop_list(struct elfcopy *_ecp, const char *_name,
305 void resync_sections(struct elfcopy *_ecp);
306 void set_shstrtab(struct elfcopy *_ecp);
307 void setup_phdr(struct elfcopy *_ecp);
308 void update_shdr(struct elfcopy *_ecp, int _update_link);
311 int ac_detect_ar(int _ifd);
312 void ac_create_ar(struct elfcopy *_ecp, int _ifd, int _ofd);
313 #endif /* ! LIBELF_AR */