1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
|
/* $OpenBSD: build.c,v 1.11 2003/06/12 20:58:10 deraadt Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Hugh Smith at The University of Guelph.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef lint
/*static char sccsid[] = "from: @(#)build.c 5.3 (Berkeley) 3/12/91";*/
static char rcsid[] = "$OpenBSD: build.c,v 1.11 2003/06/12 20:58:10 deraadt Exp $";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <a.out.h>
#include <dirent.h>
#include <unistd.h>
#include <ar.h>
#include <limits.h>
#include <ranlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <archive.h>
#include <err.h>
#include "byte.c"
#include "extern.h"
extern CHDR chdr; /* converted header */
typedef struct _rlib {
struct _rlib *next; /* next structure */
off_t pos; /* offset of defining archive file */
char *sym; /* symbol */
int symlen; /* strlen(sym) */
} RLIB;
RLIB *rhead, **pnext;
static FILE *fp;
static long symcnt; /* symbol count */
static long tsymlen; /* total string length */
static int rexec();
static void symobj();
int
build(void)
{
CF cf;
int afd, tfd;
int current_mid;
off_t size;
current_mid = -1;
afd = open_archive(O_RDWR);
fp = fdopen(afd, "r+");
tfd = tmp();
SETCF(afd, archive, tfd, tname, RPAD|WPAD);
/* Read through the archive, creating list of symbols. */
symcnt = tsymlen = 0;
pnext = &rhead;
while(get_arobj(afd)) {
int new_mid;
if (!strcmp(chdr.name, RANLIBMAG)) {
skip_arobj(afd);
continue;
}
new_mid = rexec(afd, tfd);
if (new_mid != -1) {
if (current_mid == -1)
current_mid = new_mid;
else if (new_mid != current_mid)
errx(1, "Mixed object format archive: %d / %d",
new_mid, current_mid);
}
put_arobj(&cf, (struct stat *)NULL);
}
*pnext = NULL;
/* Create the symbol table. Endianess the same as last mid seen */
symobj(current_mid);
/* Copy the saved objects into the archive. */
size = lseek(tfd, (off_t)0, SEEK_CUR);
(void)lseek(tfd, (off_t)0, SEEK_SET);
SETCF(tfd, tname, afd, archive, NOPAD);
copy_ar(&cf, size);
(void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
(void)close(tfd);
/* Set the time. */
settime(afd);
close_archive(afd);
return(0);
}
/*
* rexec
* Read the exec structure; ignore any files that don't look
* exactly right. Return MID.
* return -1 for files that don't look right.
* XXX it's hard to be sure when to ignore files, and when to error
* out.
*/
static int
rexec(int rfd, int wfd)
{
RLIB *rp;
long nsyms;
int nr, symlen;
char *strtab = 0;
char *sym;
struct exec ebuf;
struct nlist nl;
off_t r_off, w_off;
long strsize;
int result = -1;
/* Get current offsets for original and tmp files. */
r_off = lseek(rfd, (off_t)0, SEEK_CUR);
w_off = lseek(wfd, (off_t)0, SEEK_CUR);
/* Read in exec structure. */
nr = read(rfd, (char *)&ebuf, sizeof(struct exec));
if (nr != sizeof(struct exec))
goto bad;
/* Check magic number and symbol count. */
if (BAD_OBJECT(ebuf) || ebuf.a_syms == 0)
goto bad;
fix_header_order(&ebuf);
/* Seek to string table. */
if (lseek(rfd, N_STROFF(ebuf) + r_off, SEEK_SET) == (off_t)-1) {
if (errno == EINVAL)
goto bad;
else
error(archive);
}
/* Read in size of the string table. */
nr = read(rfd, (char *)&strsize, sizeof(strsize));
if (nr != sizeof(strsize))
goto bad;
strsize = fix_long_order(strsize, N_GETMID(ebuf));
/* Read in the string table. */
strsize -= sizeof(strsize);
strtab = (char *)emalloc(strsize);
nr = read(rfd, strtab, strsize);
if (nr != strsize)
goto bad;
/* Seek to symbol table. */
if (fseek(fp, N_SYMOFF(ebuf) + r_off, SEEK_SET) == (off_t)-1)
goto bad;
result = N_GETMID(ebuf);
/* For each symbol read the nlist entry and save it as necessary. */
nsyms = ebuf.a_syms / sizeof(struct nlist);
while (nsyms--) {
if (!fread((char *)&nl, sizeof(struct nlist), 1, fp)) {
if (feof(fp))
badfmt();
error(archive);
}
fix_nlist_order(&nl, N_GETMID(ebuf));
/* Ignore if no name or local. */
if (!nl.n_un.n_strx || !(nl.n_type & N_EXT))
continue;
/*
* If the symbol is an undefined external and the n_value
* field is non-zero, keep it.
*/
if ((nl.n_type & N_TYPE) == N_UNDF && !nl.n_value)
continue;
/* First four bytes are the table size. */
sym = strtab + nl.n_un.n_strx - sizeof(long);
symlen = strlen(sym) + 1;
rp = (RLIB *)emalloc(sizeof(RLIB));
rp->sym = (char *)emalloc(symlen);
bcopy(sym, rp->sym, symlen);
rp->symlen = symlen;
rp->pos = w_off;
/* Build in forward order for "ar -m" command. */
*pnext = rp;
pnext = &rp->next;
++symcnt;
tsymlen += symlen;
}
bad: if (nr < 0)
error(archive);
free(strtab);
(void)lseek(rfd, (off_t)r_off, SEEK_SET);
return result;
}
/*
* symobj --
* Write the symbol table into the archive, computing offsets as
* writing. Use the right format depending on mid.
*/
static void
symobj(int mid)
{
RLIB *rp, *rnext;
struct ranlib rn;
char hb[sizeof(struct ar_hdr) + 1], pad;
long ransize, size, stroff;
uid_t uid;
gid_t gid;
/* Rewind the archive, leaving the magic number. */
if (fseek(fp, (off_t)SARMAG, SEEK_SET) == (off_t)-1)
error(archive);
/* Size of the ranlib archive file, pad if necessary. */
ransize = sizeof(long) +
symcnt * sizeof(struct ranlib) + sizeof(long) + tsymlen;
if (ransize & 01) {
++ransize;
pad = '\n';
} else
pad = '\0';
uid = getuid();
if (uid > USHRT_MAX) {
warnx("warning: uid %u truncated to %u", uid, USHRT_MAX);
uid = USHRT_MAX;
}
gid = getgid();
if (gid > USHRT_MAX) {
warnx("warning: gid %u truncated to %u", gid, USHRT_MAX);
gid = USHRT_MAX;
}
/* Put out the ranlib archive file header. */
#define DEFMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)
(void)snprintf(hb, sizeof hb, HDR2, RANLIBMAG, 0L, uid, gid,
DEFMODE & ~umask(0), (off_t)ransize, ARFMAG);
if (!fwrite(hb, sizeof(struct ar_hdr), 1, fp))
error(tname);
/* First long is the size of the ranlib structure section. */
size = fix_long_order(symcnt * sizeof(struct ranlib), mid);
if (!fwrite((char *)&size, sizeof(size), 1, fp))
error(tname);
/* Offset of the first archive file. */
size = SARMAG + sizeof(struct ar_hdr) + ransize;
/*
* Write out the ranlib structures. The offset into the string
* table is cumulative, the offset into the archive is the value
* set in rexec() plus the offset to the first archive file.
*/
for (rp = rhead, stroff = 0; rp; rp = rp->next) {
rn.ran_un.ran_strx = stroff;
stroff += rp->symlen;
rn.ran_off = size + rp->pos;
fix_ranlib_order(&rn, mid);
if (!fwrite((char *)&rn, sizeof(struct ranlib), 1, fp))
error(archive);
}
/* Second long is the size of the string table. */
size = fix_long_order(tsymlen, mid);
if (!fwrite((char *)&size, sizeof(size), 1, fp))
error(tname);
/* Write out the string table. */
for (rp = rhead; rp; rp = rnext) {
if (!fwrite(rp->sym, rp->symlen, 1, fp))
error(tname);
rnext = rp->next;
free(rp);
}
rhead = NULL;
if (pad && !fwrite(&pad, sizeof(pad), 1, fp))
error(tname);
(void)fflush(fp);
}
|