xref: /freebsd/contrib/elftoolchain/ar/ar.h (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1 /*-
2  * Copyright (c) 2007 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $Id: ar.h 3629 2018-09-30 19:26:28Z jkoshy $
27  */
28 
29 #include <libelf.h>
30 
31 #include "_elftc.h"
32 
33 /*
34  * ar(1) options.
35  */
36 #define AR_A	0x0001		/* position-after */
37 #define AR_B	0x0002		/* position-before */
38 #define AR_C	0x0004		/* creating new archive */
39 #define AR_CC	0x0008		/* do not overwrite when extracting */
40 #define AR_J	0x0010		/* bzip2 compression */
41 #define AR_O	0x0020		/* preserve original mtime when extracting */
42 #define AR_S	0x0040		/* write archive symbol table */
43 #define AR_SS	0x0080		/* do not write archive symbol table */
44 #define AR_TR	0x0100		/* only keep first 15 chars for member name */
45 #define AR_U	0x0200		/* only extract or update newer members.*/
46 #define AR_V	0x0400		/* verbose mode */
47 #define AR_Z	0x0800		/* gzip compression */
48 #define AR_D	0x1000		/* insert dummy mode, mtime, uid and gid */
49 #define AR_BSD	0x2000		/* use the BSD archive format */
50 
51 #define DEF_BLKSZ 10240		/* default block size */
52 
53 /* Special names. */
54 
55 #define	AR_STRINGTAB_NAME_SVR4	"//"
56 #define	AR_SYMTAB_NAME_BSD	"__.SYMDEF"
57 #define	AR_SYMTAB_NAME_SVR4	"/"
58 
59 /*
60  * Convenient wrapper for general libarchive error handling.
61  */
62 #define	AC(CALL) do {					\
63 	if ((CALL))					\
64 		bsdar_errc(bsdar, 0, "%s",		\
65 		    archive_error_string(a));		\
66 } while (0)
67 
68 /*
69  * The 'ACV' wrapper is used for libarchive APIs that changed from
70  * returning 'void' to returning an 'int' in later versions of libarchive.
71  */
72 #if	ARCHIVE_VERSION_NUMBER >= 2000000
73 #define	ACV(CALL)	AC(CALL)
74 #else
75 #define	ACV(CALL)	do {				\
76 		(CALL);					\
77 	} while (0)
78 #endif
79 
80 /*
81  * In-memory representation of archive member(object).
82  */
83 struct ar_obj {
84 	Elf		 *elf;		/* object file descriptor */
85 	char		 *name;		/* member name */
86 	uid_t		  uid;		/* user id */
87 	gid_t		  gid;		/* group id */
88 	mode_t		  md;		/* octal file permissions */
89 	size_t		  size;		/* member size */
90 	time_t		  mtime;	/* modification time */
91 	dev_t		  dev;		/* inode's device */
92 	ino_t		  ino;		/* inode's number */
93 
94 	TAILQ_ENTRY(ar_obj) objs;
95 };
96 
97 /*
98  * Structure encapsulates the "global" data for "ar" program.
99  */
100 struct bsdar {
101 	const char	 *filename;	/* archive name. */
102 	const char	 *addlib;	/* target of ADDLIB. */
103 	const char	 *posarg;	/* position arg for modifiers -a, -b. */
104 	char		  mode;		/* program mode */
105 	int		  options;	/* command line options */
106 	FILE		 *output;	/* default output stream */
107 
108 	const char	 *progname;	/* program name */
109 	int		  argc;
110 	char		**argv;
111 
112 	dev_t		  ar_dev;	/* archive device. */
113 	ino_t		  ar_ino;	/* archive inode. */
114 
115 	/*
116 	 * Fields for the archive string table.
117 	 */
118 	char		 *as;		/* buffer for archive string table. */
119 	size_t		  as_sz;	/* current size of as table. */
120 	size_t		  as_cap;	/* capacity of as table buffer. */
121 
122 	/*
123 	 * Fields for the archive symbol table.
124 	 */
125 	uint32_t	  s_cnt;	/* current number of symbols. */
126 	uint32_t	 *s_so;		/* symbol offset table. */
127 	size_t		  s_so_cap;	/* capacity of so table buffer. */
128 	char		 *s_sn;		/* symbol name table */
129 	size_t		  s_sn_cap;	/* capacity of sn table buffer. */
130 	size_t		  s_sn_sz;	/* current size of sn table. */
131 	/* Current member's offset (relative to the end of pseudo members.) */
132 	off_t		  rela_off;
133 
134 	TAILQ_HEAD(, ar_obj) v_obj;	/* object(member) list */
135 };
136 
137 void	ar_mode_script(struct bsdar *ar);
138 int	ar_read_archive(struct bsdar *_ar, int _mode);
139 int	ar_write_archive(struct bsdar *_ar, int _mode);
140 void	bsdar_errc(struct bsdar *, int _code, const char *fmt, ...);
141 int	bsdar_is_pseudomember(struct bsdar *_ar, const char *_name);
142 const char *bsdar_strmode(mode_t m);
143 void	bsdar_warnc(struct bsdar *, int _code, const char *fmt, ...);
144