4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Copyright (c) 1988 AT&T
32 #include <sys/sendfile.h>
37 * List of archive members, accessed globally by cmd and file.
39 ARFILE
*listhead
, *listend
;
42 * Type used to manage string tables. Archives can have two of these:
44 * sym_strtbl: String table included at the end of the symbol table
45 * archive member, following the offset array.
47 * long_strtbl: String table used to hold member names that exceed 15
48 * characters in length, found in the long names archive member.
51 char *base
; /* Base of string table memory */
52 size_t used
; /* # bytes used from allocation */
53 size_t size
; /* Size of allocation */
56 static ARSTRTBL sym_strtbl
;
57 static ARSTRTBL long_strtbl
;
61 * Name and file descriptor used when creating a new archive.
62 * If this variable references an open file when exit_cleanup()
63 * executes, it will close and remove the file, preventing incomplete
64 * temporary files from being left behind in the case of a failure
68 int fd
; /* -1, or open file descriptor */
69 const char *path
; /* Path to open file */
73 * The ar file format requires objects to be padded to an even size.
74 * We do that, but it turns out to be beneficial to go farther.
76 * ld(1) accesses archives by mmapping them into memory. If the mapped
77 * objects (member data) have the proper alignment, we can access them
78 * directly. If the data alignment is wrong, libelf "slides" them over the
79 * archive header to correct the misalignment. This is expensive in time
80 * (to copy memory) and space (it causes swap to be allocated by the system
81 * to back the now-modified pages). Hence, we really want to ensure that
82 * the alignment is right.
84 * We used to align 32-bit objects at 4-byte boundaries, and 64-bit objects
85 * at 8-byte. More recently, an elf section type has appeared that has
86 * 8-byte alignment requirements (SUNW_move) even in 32-bit objects. So,
87 * the current strategy is to align all objects to 8-bytes.
89 * There are two important things to consider when setting this value:
90 * 1) If a new elf section that ld(1) accesses in memory appears
91 * with a greater than 8-byte alignment requirement, this value
92 * will need to be raised. Or, alternatively, the entire approach may
93 * need reconsideration.
94 * 2) The size of this padding must be smaller than the size of the
95 * smallest possible ELF section. Otherwise, the logic contained
96 * in recover_padding() can be tricked.
101 * Forward Declarations
103 static void arwrite(const char *, int, const char *, size_t);
104 static size_t mklong_tab();
105 static size_t mksymtab(const char *, ARFILEP
**, int *);
106 static const char *make_tmpname(const char *);
107 static size_t sizeof_symtbl(size_t, int, size_t);
108 static void savelongname(ARFILE
*);
109 static void savename(char *);
110 static int search_sym_tab(const char *, ARFILE
*, Elf
*,
111 Elf_Scn
*, size_t *, ARFILEP
**, size_t *);
112 static size_t sizeofmembers(size_t);
113 static char *sputl32(uint32_t, char *);
114 static char *sputl64(uint64_t, char *);
115 static void strtbl_pad(ARSTRTBL
*, size_t, int);
116 static char *trimslash(char *s
);
117 static void writesymtab(const char *, int fd
, size_t, ARFILEP
*,
122 * Function to be called on exit to clean up incomplete new archive.
127 if (ar_outfile
.fd
!= -1) {
128 /* Both of these system calls are Async-Signal-Safe */
129 (void) close(ar_outfile
.fd
);
130 (void) unlink(ar_outfile
.path
);
135 * Open an existing archive.
138 getaf(Cmd_info
*cmd_info
)
142 char *arnam
= cmd_info
->arnam
;
144 if (elf_version(EV_CURRENT
) == EV_NONE
) {
145 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_VERSION
),
150 if ((cmd_info
->afd
= fd
= open(arnam
, O_RDONLY
)) == -1) {
154 /* archive does not exist yet, may have to create one */
157 /* problem other than "does not exist" */
158 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_OPEN
),
159 arnam
, strerror(err
));
165 cmd_info
->arf
= elf_begin(fd
, cmd
, (Elf
*)0);
167 if (elf_kind(cmd_info
->arf
) != ELF_K_AR
) {
168 (void) fprintf(stderr
, MSG_INTL(MSG_NOT_ARCHIVE
), arnam
);
169 if (cmd_info
->opt_flgs
& (a_FLAG
| b_FLAG
))
170 (void) fprintf(stderr
, MSG_INTL(MSG_USAGE_06
),
178 * Given a value, and a pad alignment, return the number of bytes
179 * required to pad the value to the next alignment boundary.
182 pad(size_t n
, size_t align
)
194 * If the current archive item is an ELF object, then ar(1) may have added
195 * newline padding at the end in order to bring the following object
196 * into PADSZ alignment within the file. This padding cannot be
197 * distinguished from data using the information kept in the member header.
198 * This routine examines the objects, using knowledge of
199 * ELF and how our tools lay out objects to determine whether padding was
200 * added to an archive item. If so, it adjusts the st_size and
201 * st_padding fields of the file argument to reflect it.
204 recover_padding(Elf
*elf
, ARFILE
*file
)
212 /* ar(1) only pads objects, so bail if not looking at one */
213 if (gelf_getclass(elf
) == ELFCLASSNONE
)
217 * libelf always puts the section header array at the end
218 * of the object, and all of our compilers and other tools
219 * use libelf or follow this convention. So, it is extremely
220 * likely that the section header array is at the end of this
221 * object: Find the address at the end of the array and compare
222 * it to the archive ar_size. If they are within PADSZ bytes, then
223 * we've found the end, and the difference is padding (We assume
224 * that no ELF section can fit into PADSZ bytes).
226 if (elf_getshdrnum(elf
, &shnum
) == -1)
229 extent
= gelf_getehdr(elf
, &ehdr
)
230 ? (ehdr
.e_shoff
+ (shnum
* ehdr
.e_shentsize
)) : 0;
233 * If the extent exceeds the end of the archive member
234 * (negative padding), then we don't know what is going on
235 * and simply leave things alone.
237 if (extent
> file
->ar_size
)
240 padding
= file
->ar_size
- extent
;
241 if (padding
>= PADSZ
) {
243 * The section header array is not at the end of the object.
244 * Traverse the section headers and look for the one with
245 * the highest used address. If this address is within
246 * PADSZ bytes of ar_size, then this is the end of the object.
251 scn
= elf_nextscn(elf
, scn
);
255 if (gelf_getshdr(scn
, &shdr
)) {
258 t
= shdr
.sh_offset
+ shdr
.sh_size
;
265 if (extent
> file
->ar_size
)
267 padding
= file
->ar_size
- extent
;
271 * Now, test the padding. We only act on padding in the range
272 * (0 < pad < PADSZ) (ar(1) will never add more than this). A pad
273 * of 0 requires no action, and any other size above (PADSZ-1) means
274 * that we don't understand the layout of this object, and as such,
275 * cannot do anything.
277 * If the padding is in range, and the raw data for the
278 * object is available, then we perform one additional sanity
279 * check before moving forward: ar(1) always pads with newline
280 * characters. If anything else is seen, it is not padding so
283 if (padding
< PADSZ
) {
284 if (file
->ar_contents
) {
285 size_t cnt
= padding
;
286 char *p
= file
->ar_contents
+ extent
;
289 if (*p
++ != '\n') { /* No padding */
296 /* Remove the padding from the size */
297 file
->ar_size
-= padding
;
298 file
->ar_padding
= padding
;
303 * Each call to getfile() returns the next unread archive member
304 * from the archive opened by getaf(). Returns NULL if no more
305 * archive members are left.
308 getfile(Cmd_info
*cmd_info
)
310 Elf_Arhdr
*mem_header
= NULL
;
312 char *tmp_rawname
, *file_rawname
;
314 char *arnam
= cmd_info
->arnam
;
315 int fd
= cmd_info
->afd
;
316 Elf
*arf
= cmd_info
->arf
;
319 return (NULL
); /* the archive doesn't exist */
321 while (mem_header
== NULL
) {
322 if ((elf
= elf_begin(fd
, ELF_C_READ
, arf
)) == 0)
323 return (NULL
); /* archive is empty or have hit end */
325 if ((mem_header
= elf_getarhdr(elf
)) == NULL
) {
326 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_MALARCHIVE
),
327 arnam
, EC_XWORD(elf_getbase(elf
)), elf_errmsg(-1));
331 /* Ignore special members like the symbol and string tables */
332 if (mem_header
->ar_name
[0] == '/') {
333 (void) elf_next(elf
);
341 * The mem_header->ar_name[] is set to a NULL string
342 * if the archive member header has some error.
343 * (See elf_getarhdr() man page.)
344 * It is set to NULL for example, the ar command reads
345 * the archive files created by SunOS 4.1 system.
346 * See c block comment in cmd.c, "Incompatible Archive Header".
349 (void) strncpy(file
->ar_name
, mem_header
->ar_name
, SNAME
);
351 if ((file
->ar_longname
= malloc(strlen(mem_header
->ar_name
) + 1))
354 (void) fprintf(stderr
, MSG_INTL(MSG_MALLOC
), strerror(err
));
357 (void) strcpy(file
->ar_longname
, mem_header
->ar_name
);
358 if ((file
->ar_rawname
= malloc(strlen(mem_header
->ar_rawname
) + 1))
361 (void) fprintf(stderr
, MSG_INTL(MSG_MALLOC
), strerror(err
));
364 tmp_rawname
= mem_header
->ar_rawname
;
365 file_rawname
= file
->ar_rawname
;
366 while (!isspace(*tmp_rawname
) &&
367 ((*file_rawname
= *tmp_rawname
) != '\0')) {
371 if (!(*tmp_rawname
== '\0'))
372 *file_rawname
= '\0';
374 file
->ar_date
= mem_header
->ar_date
;
375 file
->ar_uid
= mem_header
->ar_uid
;
376 file
->ar_gid
= mem_header
->ar_gid
;
377 file
->ar_mode
= (unsigned long) mem_header
->ar_mode
;
378 file
->ar_size
= mem_header
->ar_size
;
381 if ((cmd_info
->opt_flgs
& (t_FLAG
| s_FLAG
)) != t_FLAG
) {
383 file
->ar_flag
= F_ELFRAW
;
384 if ((file
->ar_contents
= elf_rawfile(elf
, &ptr
))
387 (void) fprintf(stderr
,
388 MSG_INTL(MSG_ELF_RAWFILE
), elf_errmsg(-1));
395 recover_padding(elf
, file
);
397 (void) elf_next(elf
);
402 * Allocate a new archive member descriptor and add it to the list.
407 static ARFILE
*buffer
= NULL
;
408 static size_t count
= 0;
412 if ((buffer
= (ARFILE
*) calloc(CHUNK
, sizeof (ARFILE
)))
415 (void) fprintf(stderr
, MSG_INTL(MSG_MALLOC
),
425 listend
->ar_next
= fileptr
;
435 static char buf
[SNAME
];
437 (void) strncpy(buf
, trim(s
), SNAME
- 2);
438 buf
[SNAME
- 2] = '\0';
439 return (strcat(buf
, MSG_ORIG(MSG_STR_SLASH
)));
447 for (p1
= s
; *p1
; p1
++)
455 for (p1
= s
; *p1
; p1
++)
463 * Find all the global symbols exported by ELF archive members, and
464 * build a list associating each one with the archive member that
468 * *symlist is set to the list of symbols. If any ELF object was
469 * found, *found_obj is set to TRUE (1). Returns the number of symbols
473 mksymtab(const char *arname
, ARFILEP
**symlist
, int *found_obj
)
476 size_t mem_offset
= 0;
487 for (fptr
= listhead
; fptr
; fptr
= fptr
->ar_next
) {
488 /* determine if file is coming from the archive or not */
489 if ((fptr
->ar_elf
!= NULL
) && (fptr
->ar_pathname
== NULL
)) {
491 * I can use the saved elf descriptor.
494 } else if ((fptr
->ar_elf
== NULL
) &&
495 (fptr
->ar_pathname
!= NULL
)) {
498 * The archive member header ar_size field is 10
499 * decimal digits, sufficient to represent a 32-bit
500 * value, but not a 64-bit one. Hence, we reject
501 * attempts to insert a member larger than 4GB.
503 * One obvious way to extend the format without altering
504 * the ar_hdr struct is to use the same mechanism used
505 * for ar_name: Put the size string into the long name
506 * string table and write a string /xxx into ar_size,
507 * where xxx is the string table offset.
509 * At the time of this writing (June 2010), the largest
510 * relocatable objects are measured in 10s or 100s
511 * of megabytes, so we still have many years to go
512 * before this becomes limiting. By that time, it may
513 * turn out that a completely new archive format is
514 * a better solution, as the current format has many
515 * warts and inefficiencies. In the meantime, we
516 * won't burden the current implementation with support
517 * for a bandaid feature that will have little use.
519 if (fptr
->ar_size
> 0xffffffff) {
520 (void) fprintf(stderr
,
521 MSG_INTL(MSG_ERR_MEMBER4G
),
528 open(fptr
->ar_pathname
, O_RDONLY
)) == -1) {
530 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_OPEN
),
531 fptr
->ar_pathname
, strerror(err
));
536 if ((elf
= elf_begin(newfd
,
537 ELF_C_READ
, (Elf
*)0)) == 0) {
538 (void) fprintf(stderr
,
539 MSG_INTL(MSG_ELF_BEGIN_FILE
),
540 fptr
->ar_pathname
, elf_errmsg(-1));
546 if (elf_kind(elf
) == ELF_K_AR
) {
555 (void) fprintf(stderr
, MSG_INTL(MSG_INTERNAL_01
));
558 if (gelf_getehdr(elf
, &ehdr
) != 0) {
560 if ((class = gelf_getclass(elf
)) == ELFCLASS64
) {
561 fptr
->ar_flag
|= F_CLASS64
;
562 } else if (class == ELFCLASS32
)
563 fptr
->ar_flag
|= F_CLASS32
;
565 if (elf_getshdrstrndx(elf
, &shstrndx
) == -1) {
566 if (fptr
->ar_pathname
!= NULL
) {
567 (void) fprintf(stderr
,
568 MSG_INTL(MSG_ELF_GETSHSTRNDX_FILE
),
569 fptr
->ar_pathname
, elf_errmsg(-1));
571 (void) fprintf(stderr
,
572 MSG_INTL(MSG_ELF_GETSHSTRNDX_AR
),
573 arname
, fptr
->ar_longname
,
585 scn
= elf_getscn(elf
, shstrndx
);
587 if (fptr
->ar_pathname
!= NULL
)
588 (void) fprintf(stderr
,
589 MSG_INTL(MSG_ELF_GETSCN_FILE
),
590 fptr
->ar_pathname
, elf_errmsg(-1));
592 (void) fprintf(stderr
,
593 MSG_INTL(MSG_ELF_GETSCN_AR
),
594 arname
, fptr
->ar_longname
,
606 data
= elf_getdata(scn
, data
);
608 if (fptr
->ar_pathname
!= NULL
)
609 (void) fprintf(stderr
,
610 MSG_INTL(MSG_ELF_GETDATA_FILE
),
611 fptr
->ar_pathname
, elf_errmsg(-1));
613 (void) fprintf(stderr
,
614 MSG_INTL(MSG_ELF_GETDATA_AR
),
615 arname
, fptr
->ar_longname
,
625 if (data
->d_size
== 0) {
626 if (fptr
->ar_pathname
!= NULL
)
627 (void) fprintf(stderr
,
628 MSG_INTL(MSG_W_ELF_NODATA_FILE
),
631 (void) fprintf(stderr
,
632 MSG_INTL(MSG_W_ELF_NODATA_AR
),
633 arname
, fptr
->ar_longname
);
643 /* loop through sections to find symbol table */
645 while ((scn
= elf_nextscn(elf
, scn
)) != 0) {
647 if (gelf_getshdr(scn
, &shdr
) == NULL
) {
649 if (fptr
->ar_pathname
!= NULL
)
650 (void) fprintf(stderr
,
651 MSG_INTL(MSG_ELF_GETDATA_FILE
),
655 (void) fprintf(stderr
,
656 MSG_INTL(MSG_ELF_GETDATA_AR
),
657 arname
, fptr
->ar_longname
,
669 if (shdr
.sh_type
== SHT_SYMTAB
) {
670 if (search_sym_tab(arname
, fptr
, elf
,
671 scn
, &nsyms
, symlist
,
682 mem_offset
+= sizeof (struct ar_hdr
) + fptr
->ar_size
;
683 if (fptr
->ar_size
& 01)
697 * It is possible, though rare, to have ELF objects
698 * that do not export any global symbols. Presumably
699 * such objects operate via their .init/.fini
700 * sections. In this case, we produce an empty
701 * symbol table, so that applications that rely
702 * on a successful call to elf_getarsym() to determine
703 * if ELF objects are present will succeed. To do this,
704 * we require a small empty symbol string table.
706 strtbl_pad(&sym_strtbl
, 4, '\0');
709 * Historical behavior is to pad string tables
710 * to a multiple of 4.
712 strtbl_pad(&sym_strtbl
, pad(sym_strtbl
.used
, 4), '\0');
721 * Output a member header.
725 write_member_header(const char *filename
, int fd
, int is_elf
,
726 const char *name
, time_t timestamp
, uid_t uid
, gid_t gid
, mode_t mode
,
729 char buf
[sizeof (struct ar_hdr
) + 1];
732 len
= snprintf(buf
, sizeof (buf
), MSG_ORIG(MSG_MH_FORMAT
), name
,
733 EC_WORD(timestamp
), EC_WORD(uid
), EC_WORD(gid
), EC_WORD(mode
),
734 EC_XWORD(size
), ARFMAG
);
737 * If snprintf() reports that it needed more space than we gave
738 * it, it means that the caller fed us a long name, which is a
739 * fatal internal error.
741 if (len
!= sizeof (struct ar_hdr
)) {
742 (void) fprintf(stderr
, MSG_INTL(MSG_INTERNAL_02
));
746 arwrite(filename
, fd
, buf
, len
);
749 * We inject inter-member padding to ensure that ELF object
750 * member data is aligned on PADSZ. If this is a debug build,
751 * verify that the computations were right.
753 assert(!is_elf
|| (pad(lseek(fd
, 0, SEEK_CUR
), PADSZ
) == 0));
757 * Write the archive symbol table member to the output archive file.
760 * sizeofmembers() must have been called to establish member offset
761 * and padding values before writesymtab() is used.
764 writesymtab(const char *filename
, int fd
, size_t nsyms
, ARFILEP
*symlist
,
771 int is64
= (eltsize
== 8);
774 * We require a buffer large enough to hold a symbol table count,
775 * plus one offset for each symbol.
777 tblsize
= (nsyms
+ 1) * eltsize
;
778 if ((buf
= dst
= malloc(tblsize
)) == NULL
) {
780 (void) fprintf(stderr
, MSG_INTL(MSG_MALLOC
), strerror(err
));
784 write_member_header(filename
, fd
, 0,
785 (is64
? MSG_ORIG(MSG_STR_SYM64
) : MSG_ORIG(MSG_STR_SLASH
)),
786 time(0), 0, 0, 0, tblsize
+ sym_strtbl
.used
);
788 dst
= is64
? sputl64(nsyms
, dst
) : sputl32(nsyms
, dst
);
790 for (i
= 0, j
= SYMCHUNK
, ptr
= symlist
; i
< nsyms
; i
++, j
--, ptr
++) {
793 ptr
= (ARFILEP
*)*ptr
;
795 dst
= is64
? sputl64((*ptr
)->ar_offset
, dst
) :
796 sputl32((*ptr
)->ar_offset
, dst
);
798 arwrite(filename
, fd
, buf
, tblsize
);
800 arwrite(filename
, fd
, sym_strtbl
.base
, sym_strtbl
.used
);
804 * Grow the size of the given string table so that there is room
805 * for at least need bytes.
808 * strtbl - String table to grow
809 * need - Amount of space required by caller
812 strtbl_alloc(ARSTRTBL
*strtbl
, size_t need
)
814 #define STRTBL_INITSZ 8196
817 * On 32-bit systems, we require a larger integer type in order
818 * to avoid overflow and wraparound when doing our computations.
820 uint64_t need64
= need
;
821 uint64_t used64
= strtbl
->used
;
822 uint64_t size64
= strtbl
->size
;
823 uint64_t target
= need64
+ used64
;
827 if (target
<= size64
)
831 * Detect 32-bit system. We might usually do this with the preprocessor,
832 * but it can serve as a predicate in tests that also apply to 64-bit
835 sys32
= (sizeof (size_t) == 4);
838 * The symbol string table can be larger than 32-bits on a 64-bit
839 * system. However, the long name table must stay below that limit.
840 * The reason for this is that there is not enough room in the ar_name
841 * field of the member header to represent 64-bit offsets.
843 tbl32
= (strtbl
== &long_strtbl
);
846 * If request is larger than 4GB and we can't do it because we
847 * are a 32-bit program, or because the table is format limited,
848 * we can go no further.
850 if ((target
> 0xffffffff) && (sys32
|| tbl32
))
853 /* Default starting size */
854 if (strtbl
->base
== NULL
)
855 size64
= STRTBL_INITSZ
;
858 * Our strategy is to double the size until we find a size that
859 * exceeds the request. However, if this table cannot exceed 4GB,
860 * then once we exceed 2GB, we switch to a strategy of taking the
861 * current request and rounding it up to STRTBL_INITSZ.
863 while (target
> size64
) {
864 if ((target
> 0x7fffffff) && (sys32
|| tbl32
)) {
865 size64
= ((target
+ STRTBL_INITSZ
) / STRTBL_INITSZ
) *
869 * If we are so close to the line that this small
870 * increment exceeds 4GB, give it up.
872 if ((size64
> 0xffffffff) && (sys32
|| tbl32
))
881 strtbl
->base
= realloc(strtbl
->base
, size64
);
882 if (strtbl
->base
== NULL
) {
884 (void) fprintf(stderr
, MSG_INTL(MSG_MALLOC
), strerror(err
));
887 strtbl
->size
= (size_t)size64
;
892 * Control comes here if we are unable to allocate more than 4GB of
893 * memory for the string table due to one of the following reasons:
895 * - A 32-bit process is attempting to be larger than 4GB
897 * - A 64-bit process is attempting to grow the long names string
898 * table beyond the ar format limit of 32-bits.
901 (void) fprintf(stderr
, MSG_INTL(MSG_MALLOC
), strerror(ENOMEM
));
903 (void) fprintf(stderr
, MSG_INTL(MSG_ERR_LONGSTRTBLSZ
));
910 * Add the specified number of pad characters to the end of the
911 * given string table.
914 * strtbl - String table to pad
915 * n - # of pad characters to add
916 * ch - Pad character to use
919 strtbl_pad(ARSTRTBL
*strtbl
, size_t n
, int ch
)
924 if ((n
+ strtbl
->used
) > strtbl
->size
)
925 strtbl_alloc(strtbl
, n
);
928 strtbl
->base
[strtbl
->used
++] = ch
;
932 * Enter a symbol name into the symbol string table.
935 savename(char *symbol
)
939 need
= strlen(symbol
) + 1;
940 if ((need
+ sym_strtbl
.used
) > sym_strtbl
.size
)
941 strtbl_alloc(&sym_strtbl
, need
);
943 (void) strcpy(sym_strtbl
.base
+ sym_strtbl
.used
, symbol
);
944 sym_strtbl
.used
+= need
;
948 * Prepare an archive member with a long (>15 characters) name for
949 * the output archive.
952 * fptr - pointer to archive member with long name
955 * The long name is entered into the long name string table,
956 * and fptr->ar_name has been replaced with the special /xxx
957 * name used to indicate that the real name is in the string table
961 savelongname(ARFILE
*fptr
)
966 /* Size of new item to add */
967 len
= strlen(fptr
->ar_longname
);
970 /* Ensure there's room */
971 if ((need
+ long_strtbl
.used
) > long_strtbl
.size
)
972 strtbl_alloc(&long_strtbl
, need
);
975 * Generate the index string to be written into the member header
977 * This will not overflow the ar_name field because that field is
978 * 16 characters in size, and a 32-bit unsigned value can be formatted
979 * in 10 characters. Allowing a character for the leading '/', and one
980 * for the NULL termination, that leaves us with 4 extra spaces.
982 (void) snprintf(fptr
->ar_name
, sizeof (fptr
->ar_name
),
983 MSG_ORIG(MSG_FMT_LLINT
), EC_XWORD(long_strtbl
.used
));
986 * Enter long name into reserved spot, terminated with a slash
987 * and a newline character.
989 p
= long_strtbl
.base
+ long_strtbl
.used
;
990 long_strtbl
.used
+= need
;
991 (void) strcpy(p
, fptr
->ar_longname
);
998 * Determine if the archive we're about to write will exceed the
999 * 32-bit limit of 4GB.
1002 * mksymtab() and mklong_tab() have been called to set up
1003 * the string tables.
1006 * Returns TRUE (1) if the 64-bit symbol table is needed, and
1007 * FALSE (0) otherwise.
1011 require64(size_t nsyms
, int found_obj
, size_t longnames
)
1017 * If there are more than 4GB symbols, we have to use
1018 * the 64-bit form. Note that longnames cannot exceed 4GB
1019 * because that symbol table is limited to a length of 4GB by
1020 * the archive format.
1022 if (nsyms
> 0xffffffff)
1026 * Make a worst case estimate for the size of the resulting
1027 * archive by assuming full padding between members.
1031 size
+= sizeof (struct ar_hdr
) + long_strtbl
.used
+ PADSZ
;
1034 size
+= sizeof_symtbl(nsyms
, found_obj
, 4) + PADSZ
;
1036 if (size
> 0xffffffff)
1039 for (fptr
= listhead
; fptr
; fptr
= fptr
->ar_next
) {
1040 size
+= sizeof (struct ar_hdr
) + fptr
->ar_size
+ PADSZ
;
1042 if (size
> 0xffffffff)
1046 /* 32-bit symbol table will suffice */
1051 writefile(Cmd_info
*cmd_info
)
1054 ARFILEP
*symlist
= 0;
1057 int new_archive
= 0;
1058 char *name
= cmd_info
->arnam
;
1059 size_t arsize
; /* Size of magic # and special members */
1060 size_t symtbl_eltsize
= 4;
1064 struct stat stbuf
, ar_stbuf
;
1065 char pad_bytes
[PADSZ
];
1070 * Gather the list of symbols and associate each one to the
1071 * ARFILE descriptor of the object it belongs to. At the same
1072 * time, tag each ELF object with the appropriate F_CLASSxx
1075 nsyms
= mksymtab(name
, &symlist
, &found_obj
);
1077 /* Generate the string table for long member names */
1078 longnames
= mklong_tab();
1081 * Will this archive exceed 4GB? If we're a 32-bit process, we can't
1082 * do it. If we're a 64-bit process, then we'll have to use a
1083 * 64-bit symbol table.
1085 if (require64(nsyms
, found_obj
, longnames
)) {
1089 (void) fprintf(stderr
, MSG_INTL(MSG_TOOBIG4G
));
1095 * If the user requested it, use the 64-bit symbol table even if
1096 * a 32-bit one would suffice. 32-bit tables are more portable and
1097 * take up less room, so this feature is primarily for testing.
1099 if (cmd_info
->opt_flgs
& S_FLAG
)
1103 * If the first non-special archive member is an ELF object, then we
1104 * need to arrange for its data to have an alignment of PADSZ. The
1105 * preceeding special member will be the symbol table, or the long
1106 * name string table. We pad the string table that precedes the
1107 * ELF member in order to achive the desired alignment.
1109 is_elf
= listhead
&& (listhead
->ar_flag
& (F_CLASS32
| F_CLASS64
));
1112 arsize
+= sizeof_symtbl(nsyms
, found_obj
, symtbl_eltsize
);
1113 if (is_elf
&& (longnames
== 0)) {
1114 pad_cnt
= pad(arsize
+ sizeof (struct ar_hdr
), PADSZ
);
1115 strtbl_pad(&sym_strtbl
, pad_cnt
, '\0');
1119 if (longnames
> 0) {
1120 arsize
+= sizeof (struct ar_hdr
) + long_strtbl
.used
;
1122 pad_cnt
= pad(arsize
+ sizeof (struct ar_hdr
), PADSZ
);
1123 strtbl_pad(&long_strtbl
, pad_cnt
, '\0');
1129 * For each user visible (non-special) archive member, determine
1130 * the header offset, and the size of any required padding.
1132 (void) sizeofmembers(arsize
);
1135 * Is this a new archive, or are we updating an existing one?
1137 * A subtlety here is that POSIX says we are not supposed
1138 * to replace a non-writable file. The only 100% reliable test
1139 * against this is to open the file for non-destructive
1140 * write access. If the open succeeds, we are clear to
1141 * replace it, and if not, then the error generated is
1142 * the error we need to report.
1144 if ((fd
= open(name
, O_RDWR
)) < 0) {
1147 if (err
!= ENOENT
) {
1148 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_OPEN
),
1149 name
, strerror(err
));
1153 if ((cmd_info
->opt_flgs
& c_FLAG
) == 0) {
1154 (void) fprintf(stderr
, MSG_INTL(MSG_BER_MES_CREATE
),
1158 /* Capture mode and owner information to apply to replacement */
1159 if (fstat(fd
, &ar_stbuf
) < 0) {
1161 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_STAT
),
1162 name
, strerror(err
));
1172 * Register exit handler function to clean up after us if we exit
1173 * before completing the new archive. atexit() is defined as
1174 * only being able to fail due to memory exhaustion.
1176 if (atexit(exit_cleanup
) != 0) {
1177 (void) fprintf(stderr
, MSG_INTL(MSG_MALLOC
), strerror(ENOMEM
));
1182 * If a new archive, create it in place. If updating an archive,
1183 * create the replacement under a temporary name and then rename it
1186 ar_outfile
.path
= new_archive
? name
: make_tmpname(name
);
1187 ar_outfile
.fd
= open(ar_outfile
.path
, O_RDWR
|O_CREAT
|O_LARGEFILE
, 0666);
1188 if (ar_outfile
.fd
== -1) {
1190 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_OPEN
),
1191 ar_outfile
.path
, strerror(err
));
1195 /* Output magic string */
1196 arwrite(name
, ar_outfile
.fd
, ARMAG
, SARMAG
);
1199 * The symbol table member is always first if present. Note that
1200 * writesymtab() uses the member offsets computed by sizeofmembers()
1204 writesymtab(name
, ar_outfile
.fd
, nsyms
, symlist
,
1208 write_member_header(name
, ar_outfile
.fd
, 0,
1209 MSG_ORIG(MSG_STR_DSLASH
), time(0), 0, 0, 0,
1211 arwrite(name
, ar_outfile
.fd
, long_strtbl
.base
,
1216 * The accuracy of the symbol table depends on our having calculated
1217 * the size of the archive accurately to this point. If this is a
1218 * debug build, verify it.
1220 assert(arsize
== lseek(ar_outfile
.fd
, 0, SEEK_CUR
));
1223 if (cmd_info
->opt_flgs
& v_FLAG
) {
1224 (void) fprintf(stderr
, MSG_INTL(MSG_BER_MES_WRITE
),
1230 * Fill pad_bytes array with newline characters. This array
1231 * is used to supply padding bytes at the end of ELF objects.
1232 * There can never be more tha PADSZ such bytes, so this number
1233 * will always suffice.
1235 for (pad_cnt
= 0; pad_cnt
< PADSZ
; pad_cnt
++)
1236 pad_bytes
[pad_cnt
] = '\n';
1238 for (fptr
= listhead
; fptr
; fptr
= fptr
->ar_next
) {
1240 * We computed the expected offset for each ELF member and
1241 * used those offsets to fill the symbol table. If this is
1242 * a debug build, verify that the computed offset was right.
1244 is_elf
= (fptr
->ar_flag
& (F_CLASS32
| F_CLASS64
)) != 0;
1246 (fptr
->ar_offset
== lseek(ar_outfile
.fd
, 0, SEEK_CUR
)));
1250 * The mem_header->ar_name[] is set to a NULL string
1251 * if the archive member header has some error.
1252 * (See elf_getarhdr() man page.)
1253 * It is set to NULL for example, the ar command reads
1254 * the archive files created by SunOS 4.1 system.
1255 * See c block comment in cmd.c, "Incompatible Archive Header".
1257 if (fptr
->ar_name
[0] == 0) {
1258 fptr
->ar_longname
= fptr
->ar_rawname
;
1259 (void) strncpy(fptr
->ar_name
, fptr
->ar_rawname
, SNAME
);
1261 write_member_header(name
, ar_outfile
.fd
, is_elf
,
1262 (strlen(fptr
->ar_longname
) <= (unsigned)SNAME
-2) ?
1263 trimslash(fptr
->ar_longname
) : fptr
->ar_name
,
1264 EC_WORD(fptr
->ar_date
), fptr
->ar_uid
, fptr
->ar_gid
,
1265 fptr
->ar_mode
, fptr
->ar_size
+ fptr
->ar_padding
);
1268 if ((fptr
->ar_flag
& F_ELFRAW
) == 0) {
1270 * The file doesn't come from the archive, and is
1271 * therefore not already in memory(fptr->ar_contents)
1272 * so open it and do a direct file-to-file transfer of
1273 * its contents. We use the sendfile() system call
1274 * to make the kernel do the transfer, so we don't have
1275 * to buffer data in process, and we trust that the
1276 * kernel will use an optimal transfer strategy.
1278 if ((fd
= open(fptr
->ar_pathname
, O_RDONLY
)) == -1) {
1280 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_OPEN
),
1281 fptr
->ar_longname
, strerror(err
));
1284 if (stat(fptr
->ar_pathname
, &stbuf
) < 0) {
1286 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_OPEN
),
1287 fptr
->ar_longname
, strerror(err
));
1292 if (sendfile(ar_outfile
.fd
, fd
, &off
,
1293 stbuf
.st_size
) != stbuf
.st_size
) {
1295 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_WRITE
),
1296 name
, strerror(err
));
1301 /* Archive member is in memory. Write it out */
1302 arwrite(name
, ar_outfile
.fd
, fptr
->ar_contents
,
1307 * All archive members are padded to at least a boundary of 2.
1308 * The expression ((fptr->ar_size & 0x1) != 0) yields 1 for
1309 * odd boundaries, and 0 for even ones. To this, we add
1310 * whatever padding is needed for ELF objects.
1312 pad_cnt
= ((fptr
->ar_size
& 0x1) != 0) + fptr
->ar_padding
;
1314 arwrite(name
, ar_outfile
.fd
, pad_bytes
, pad_cnt
);
1318 * All archive output is done.
1320 if (close(ar_outfile
.fd
) < 0) {
1322 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_CLOSE
), ar_outfile
.path
,
1326 ar_outfile
.fd
= -1; /* Prevent removal on exit */
1327 (void) elf_end(cmd_info
->arf
);
1328 (void) close(cmd_info
->afd
);
1331 * If updating an existing archive, rename the new version on
1332 * top of the original.
1336 * Prevent the replacement of the original archive from
1337 * being interrupted, to lower the possibility of an
1338 * interrupt destroying a pre-existing archive.
1340 establish_sighandler(SIG_IGN
);
1342 if (rename(ar_outfile
.path
, name
) < 0) {
1344 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_RENAME
),
1345 ar_outfile
.path
, name
, strerror(err
));
1346 (void) unlink(ar_outfile
.path
);
1349 (void) chmod(name
, ar_stbuf
.st_mode
& 0777);
1350 if (chown(name
, ar_stbuf
.st_uid
, ar_stbuf
.st_gid
) >= 0)
1351 (void) chmod(name
, ar_stbuf
.st_mode
& 07777);
1357 * Examine all the archive members, enter any member names longer than
1358 * 15 characters into the long name string table, and count the number
1361 * Returns the size of the resulting archive member, including the
1368 size_t longnames
= 0;
1370 for (fptr
= listhead
; fptr
; fptr
= fptr
->ar_next
) {
1371 if (strlen(fptr
->ar_longname
) >= (unsigned)SNAME
-1) {
1377 /* round up table that keeps the long filenames */
1379 strtbl_pad(&long_strtbl
, pad(long_strtbl
.used
, 4), '\n');
1385 * Write 32/64-bit words into buffer in archive symbol table
1386 * standard byte order (MSB).
1389 sputl32(uint32_t n
, char *cp
)
1401 sputl64(uint64_t n
, char *cp
)
1418 search_sym_tab(const char *arname
, ARFILE
*fptr
, Elf
*elf
, Elf_Scn
*scn
,
1419 size_t *nsyms
, ARFILEP
**symlist
, size_t *num_errs
)
1421 Elf_Data
*str_data
, *sym_data
; /* string table, symbol table */
1423 GElf_Sxword no_of_symbols
;
1428 static ARFILEP
*sym_ptr
= 0;
1429 static ARFILEP
*nextsym
= NULL
;
1430 static int syms_left
= 0;
1431 char *fname
= fptr
->ar_pathname
;
1433 (void) gelf_getshdr(scn
, &shdr
);
1434 str_scn
= elf_getscn(elf
, shdr
.sh_link
); /* index for string table */
1435 if (str_scn
== NULL
) {
1437 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_GETDATA_FILE
),
1438 fname
, elf_errmsg(-1));
1440 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_GETDATA_AR
),
1441 arname
, fptr
->ar_longname
, elf_errmsg(-1));
1446 no_of_symbols
= shdr
.sh_size
/ shdr
.sh_entsize
;
1447 if (no_of_symbols
== -1) {
1448 (void) fprintf(stderr
, MSG_INTL(MSG_SYMTAB_01
));
1452 (void) gelf_getshdr(str_scn
, &shdr
);
1453 str_shtype
= shdr
.sh_type
;
1454 if (str_shtype
== -1) {
1456 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_GETDATA_FILE
),
1457 fname
, elf_errmsg(-1));
1459 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_GETDATA_AR
),
1460 arname
, fptr
->ar_longname
, elf_errmsg(-1));
1465 /* This test must happen before testing the string table. */
1466 if (no_of_symbols
== 1)
1467 return (0); /* no symbols; 0th symbol is the non-symbol */
1469 if (str_shtype
!= SHT_STRTAB
) {
1471 (void) fprintf(stderr
, MSG_INTL(MSG_SYMTAB_NOSTR_FILE
),
1474 (void) fprintf(stderr
, MSG_INTL(MSG_SYMTAB_NOSTR_AR
),
1475 arname
, fptr
->ar_longname
);
1479 if ((str_data
= elf_getdata(str_scn
, str_data
)) == 0) {
1481 (void) fprintf(stderr
, MSG_INTL(MSG_SYMTAB_NODAT_FILE
),
1484 (void) fprintf(stderr
, MSG_INTL(MSG_SYMTAB_NODAT_AR
),
1485 arname
, fptr
->ar_longname
);
1488 if (str_data
->d_size
== 0) {
1490 (void) fprintf(stderr
, MSG_INTL(MSG_SYMTAB_ZDAT_FILE
),
1493 (void) fprintf(stderr
, MSG_INTL(MSG_SYMTAB_ZDAT_AR
),
1494 arname
, fptr
->ar_longname
);
1498 if ((sym_data
= elf_getdata(scn
, sym_data
)) == NULL
) {
1500 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_LIB_FILE
),
1501 fname
, elf_errmsg(-1));
1503 (void) fprintf(stderr
, MSG_INTL(MSG_ELF_LIB_AR
),
1504 arname
, fptr
->ar_longname
, elf_errmsg(-1));
1508 /* start at 1, first symbol entry is ignored */
1509 for (counter
= 1; counter
< no_of_symbols
; counter
++) {
1511 (void) gelf_getsym(sym_data
, counter
, &sym
);
1513 symname
= (char *)(str_data
->d_buf
) + sym
.st_name
;
1515 if (((GELF_ST_BIND(sym
.st_info
) == STB_GLOBAL
) ||
1516 (GELF_ST_BIND(sym
.st_info
) == STB_WEAK
)) &&
1517 (sym
.st_shndx
!= SHN_UNDEF
)) {
1519 sym_ptr
= malloc((SYMCHUNK
+1)
1520 * sizeof (ARFILEP
));
1521 if (sym_ptr
== NULL
) {
1523 (void) fprintf(stderr
,
1524 MSG_INTL(MSG_MALLOC
),
1528 syms_left
= SYMCHUNK
;
1530 *nextsym
= (ARFILEP
)sym_ptr
;
1540 savename(symname
); /* put name in the archiver's */
1541 /* symbol table string table */
1548 * Get the output file size
1551 sizeofmembers(size_t psum
)
1555 size_t hdrsize
= sizeof (struct ar_hdr
);
1557 for (fptr
= listhead
; fptr
; fptr
= fptr
->ar_next
) {
1558 fptr
->ar_offset
= psum
+ sum
;
1559 sum
+= fptr
->ar_size
;
1560 if (fptr
->ar_size
& 01)
1565 * If the current item, and the next item are both ELF
1566 * objects, then add padding to current item so that the
1567 * data in the next item will have PADSZ alignment.
1569 * In any other case, set the padding to 0. If the
1570 * item comes from another archive, it may be carrying
1571 * a non-zero padding value from that archive that does
1572 * not apply to the one we are about to build.
1574 if ((fptr
->ar_flag
& (F_CLASS32
| F_CLASS64
)) &&
1576 (fptr
->ar_next
->ar_flag
& (F_CLASS32
| F_CLASS64
))) {
1577 fptr
->ar_padding
= pad(psum
+ sum
+ hdrsize
, PADSZ
);
1578 sum
+= fptr
->ar_padding
;
1580 fptr
->ar_padding
= 0;
1587 * Compute the size of the symbol table archive member.
1590 * nsyms - # of symbols in the table
1591 * found_obj - TRUE if the archive contains any ELF objects
1592 * eltsize - Size of the integer type to use for the symbol
1593 * table. 4 for 32-bit tables, and 8 for 64-bit tables.
1596 sizeof_symtbl(size_t nsyms
, int found_obj
, size_t eltsize
)
1601 /* Member header, symbol count, and one slot per symbol */
1602 sum
+= sizeof (struct ar_hdr
) + ((nsyms
+ 1) * eltsize
);
1603 sum
+= sym_strtbl
.used
;
1610 arwrite(const char *name
, int nfd
, const char *dst
, size_t size
) {
1611 if (write(nfd
, dst
, size
) != size
) {
1613 (void) fprintf(stderr
, MSG_INTL(MSG_SYS_WRITE
),
1614 name
, strerror(err
));
1620 make_tmpname(const char *filename
) {
1621 char *slash
, *tmpname
;
1622 size_t prefix_cnt
= 0;
1625 * If there is a path prefix in front of the filename, we
1626 * want to put the temporary file in the same directory.
1627 * Determine the length of the path.
1629 slash
= strrchr(filename
, '/');
1631 prefix_cnt
= slash
- filename
+ 1;
1632 tmpname
= malloc(prefix_cnt
+ MSG_STR_MKTEMP_SIZE
+ 1);
1633 if (tmpname
== NULL
) {
1635 (void) fprintf(stderr
, MSG_INTL(MSG_MALLOC
), strerror(err
));
1640 (void) strncpy(tmpname
, filename
, prefix_cnt
);
1641 (void) strcpy(tmpname
+ prefix_cnt
, MSG_ORIG(MSG_STR_MKTEMP
));
1642 (void) mktemp(tmpname
);