Ar (Unix)
Encyclopedia
The archiver is a Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...

 utility that maintains groups of files as a single archive file
Archive file
An archive file is a file that is composed of one or more files along with metadata that can include source volume and medium information, file directory structure, error detection and recovery information, file comments, and usually employs some form of lossless compression. Archive files may be...

. Today, ar is generally used only to create and update static library
Static library
In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable...

 files that the link editor or linker uses; it can be used to create archives for any purpose, but has been largely replaced by tar
Tar (file format)
In computing, tar is both a file format and the name of a program used to handle such files...

 for purposes other than static libraries. ar is included as one of the GNU Binutils.

File format details

The ar format has never been standardized; modern archives are based on a common format with two known variants, BSD and GNU
GNU
GNU is a Unix-like computer operating system developed by the GNU project, ultimately aiming to be a "complete Unix-compatible software system"...

.

Historically there have been other variants including AIX (small), AIX (big) and Coherent, which all vary significantly from the common format.

Debian
Debian
Debian is a computer operating system composed of software packages released as free and open source software primarily under the GNU General Public License along with other free software licenses. Debian GNU/Linux, which includes the GNU OS tools and Linux kernel, is a popular and influential...

 ".deb
Deb (file format)
deb is the extension of the Debian software package format and the most often used name for such binary packages. Like the "Deb" part of the term Debian, it originates from the name of Debra, erstwhile girlfriend and now ex-wife of Debian's founder Ian Murdock.Debian packages are also used in...

" archives use the common format.

An ar file begins with a global header, followed by a header and data section for each file stored within the ar file.

The data section is 2 byte aligned. If it would end on an odd offset, a '\n' is used as filler.

Global header

The global header is a single field containing the magic ASCII string
Magic number (programming)
In computer programming, the term magic number has multiple meanings. It could refer to one or more of the following:* A constant numerical or text value used to identify a file format or protocol; for files, see List of file signatures...

 "!" followed by a single LF control character

File header

The common format is as follows.
Field Offset from Field Offset to Field Name Field Format
0 15 File name ASCII
16 27 File modification timestamp Decimal
28 33 Owner ID Decimal
34 39 Group ID Decimal
40 47 File mode Octal
48 57 File size in bytes Decimal
58 59 File magic 0x60 0x0A


Due to the limitations of file name length and format, both the GNU and BSD variants devised different methods of storing long filenames.

BSD variant

BSD ar stores extended filenames by placing the string "#1/" followed by the file name length in the file name field, and appending the real filename to the file header.

GNU variant

GNU ar stores multiple extended filenames in the data section of a file with the name "//", this record is referred to by future headers. A header references an extended filename by storing a "/" followed by a decimal offset to the start of the filename in the extended filename data section. The format of this "//" file itself is simply a list of the long filenames, each separated by one or more LF characters. Note that the decimal offsets are number of characters, not line or string number within the "//" file.

GNU ar uses a '/' to mark the end of the filename; this allows for the use of spaces without the use of an extended filename.

GNU ar uses the special filename "/" to denote that the following data entry contains a symbol lookup table, which is used in ar libraries to speed up access. This symbol table is built in three parts which are recorded together as contiguous data.
  1. A 32-bit big endian integer, giving the number of entries in the table.
  2. A set of 32-bit big endian integers. One for each symbol, recording the position within the archive of the header for the file containing this symbol.
  3. A set of Zero-terminated strings. Each is a symbol name, and occurs in the same order as the list of positions in part 2.


The special file "/" is not terminated with a specific sequence; the end is assumed once the last symbol name has been read.

Example usage

For example, to create an archive from files class1.o, class2.o, class3.o, the following command would be used:
ar rcs libclass.a class1.o class2.o class3.o
to compile a program that depends on class1.o, class2.o, and class3.o one could do:
cc main.c libclass.a
instead of:
cc main.c class1.o class2.o class3.o

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK