Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

ar (Unix)

From Wikipedia, the free encyclopedia
Unix archiver command
ar
Original authorsKen Thompson,
Dennis Ritchie
(AT&T Bell Laboratories)
DevelopersVariousopen-source andcommercial developers
Initial releaseNovember 3, 1971; 54 years ago (1971-11-03)
Written inC
Operating systemUnix,Unix-like,V,Plan 9,Inferno
PlatformCross-platform
TypeCommand
LicensePlan 9:MIT License
archiver format
Filename extension
.a,.lib,.ar[1]
Internet media type
application/x-archive[1]
Magic number!<arch>
Type of formatarchive format
Container forusuallyobject files (.o)
StandardNot standardized, several variants exist
Open format?Yes[2]

ar, short forarchiver, is ashellcommand for maintaining multiplefiles as a singlearchive file (i.e. afile archiver).It is often used to create and updatestatic library files that the link editor orlinker uses and for generatingdeb format packages for theDebianLinux distribution. It can be used to create archives for any purpose, but has been largely replaced bytar for purposes other than static libraries.[3]

Originally developed forUnix, the command is widely available on Unix-based systems, and similar commands are available on other platforms.An implementation is included inGNU Binutils.[2]

File format

[edit]
Diagram showing an example file structure of a .deb file

The format of a file that results from usingar has never been standardized.[4][5]

The first format appeared in the first edition of Unix[6] and was used throughVersion 6 Unix.[7][4]Version 7 Unix had a modified version of that format,[8][4] which was also used inUNIX System III[9] and inUNIX System V on thePDP-11.[10]

A new format was introduced in the first release of System V on processors other than PDP-11s.[11]

Modern archives are, on most systems, based on a common format with two main variants,BSD[4] (initially used fora.out files) and UNIX System V release 2 and later[12] (initially used forCOFF files and later used forELF files) and used as well byGNU[13] andWindows. AIX has its own formats (small[14] and big[15]), as does Coherent; those formats vary significantly from the common format.

Structure

[edit]

An archive file begins with aheader that identifies the file type and is followed with a section for each contained file. Each contained file section consists of a header followed by the file content. The headers consist solely of printable ASCII characters and line feeds. In fact, an archive containing only text files is also a text file.

The content of a contained file begins on an even byte boundary. Anewline is inserted between files as padding, if necessary. Nevertheless, the size stored reflects the size excluding padding.[16]

Archive header

[edit]

The first header, a.k.a. file signature, is amagic number that encodes the ASCII string!<arch> followed by a singleline feed character (0x0A).

Contained file header

[edit]

Each file is preceded by a header that containsinformation about the file. The common format is as follows. Numeric values are encoded in ASCII and all values are right-padded with spaces (0x20).

OffsetLengthContentFormat
016File identifierASCII
1612File modification timestamp (in seconds)Decimal
286Owner IDDecimal
346Group IDDecimal
408File mode (type and permission)Octal
4810File size in bytesDecimal
582Ending characters0x60 0x0A

Variants

[edit]

Variants of the command were developed to address issues including:

File name length limitation
The BSD and System V/GNU variants devised different methods of storing long file names.
Global symbol table
Many implementations include a global symbol table (a.k.a. armap, directory or index) for fast linking without needing to scan the whole archive for a symbol. POSIX recognizes this feature, and requires implementations to have an-s option for updating it. Most implementations put it at the first file entry.[17]
Year 2038 problem
Although the common format is not at risk of this problem, many implementations are vulnerable to failure in that year.

BSD

[edit]

The BSD implementation stores file names right-padded with ASCII spaces. This causes issues with spaces inside file names.[clarification needed] The4.4BSD implementation stores extended file names[clarification needed] by placing the string "#1/" followed by the file name length in the file name field, and storing the real file name in front of the data section.[4]

TheBSD implementation traditionally does not handle the building of a global symbol lookup table, and delegates this task to a separate utility,ranlib,[18] which inserts an architecture-specific[clarification needed] file named__.SYMDEF as first archive member.[19] Some descendants put a space and "SORTED" after the name to indicate a sorted version.[20] A 64-bit variant called__.SYMDEF_64 exists onDarwin.

To conform to POSIX, newer BSD implementations support the-s option instead ofranlib. FreeBSD in particular ditched the SYMDEF table format and embraced the System V style table.[21]

System V/GNU[clarification needed]

[edit]

To allow spaces in file names, the System V implementation uses a slash ('/') to mark the end of the file. This limits the file name to 15 chars. Filenames longer than 15 ASCII chars are replaced by an index, represented by a slash ('/') followed by the decimal character offset in the indexfile of the start or the name. The index file is named "//" and is usually the second entry of the archive, after the symbol table which always is the first. The format of this "//" file itself is simply a list of the long file names, separated by one or more LF characters. LF characters cannot be part of a filename. The GNU version uses '/'-terminated names and no LF.

The System V implementation uses the special file name "/" to denote that the following data entry contains a symbol lookup table, which is used in ar libraries[clarification needed] 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.

Some System V systems do not use this format.For operating systems such asHP-UX 11.0, this information is stored in a data structure based on theSOM file format.

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

To overcome the 4 GiB file size limit[clarification needed] some operating system likeSolaris 11.2 and GNU use a variant lookup table. Instead of 32-bit integers, 64-bit integers are used in the symbol lookup tables. The string "/SYM64/" instead "/" is used as identifier for this table[22]

Windows

[edit]

TheWindows (PE/COFF) variant is based on the SysV/GNU variant. The first entry "/" has the same layout as the SysV/GNU symbol table. The second entry is another "/", a Microsoft extension that stores an extended symbol cross-reference table. This one is sorted and uses little-endian integers.[5][23] The third entry is the optional "//" long name data as in SysV/GNU.[24]

Thin archive

[edit]

TheGNU binutils andElfutils implementations have an additional "thin archive" format with the magic number!<thin>. A thin archive only contains a symbol table and references to the file. The file format is essentially a System V format archive where every file is stored without the data sections. Every file name is stored as a "long" file name and they are to be resolved as if they weresymbolic links.[25]

Examples

[edit]

The following command creates an archivelibclass.a withobject filesclass1.o,class2.o,class3.o:

ar rcs libclass.a class1.o class2.o class3.o

The linkerld can read object code from an archive file. The following example shows how the archivelibclass.a (specified as-lclass) is linked with the object code ofmain.o.

ld main.o -lclass

See also

[edit]

References

[edit]
  1. ^ab"application/x-archive". Archived fromthe original on 2019-12-08. Retrieved2019-03-11.
  2. ^ab"ar(1) – Linux man page".Archived from the original on 12 February 2019. Retrieved3 October 2013.
  3. ^"Static Libraries". TLDP.Archived from the original on 3 July 2013. Retrieved3 October 2013.
  4. ^abcde"Manual page for NET/2 ar file format".Archived from the original on 2017-08-22. Retrieved2014-08-23.
  5. ^abLevine, John R. (2000) [October 1999]."Chapter 6: Libraries".Linkers and Loaders. The Morgan Kaufmann Series in Software Engineering and Programming (1 ed.). San Francisco, USA:Morgan Kaufmann.ISBN 1-55860-496-0.OCLC 42413382.Archived from the original on 2012-12-05. Retrieved2020-01-12. Code:[1][2][dead link] Errata:[3]
  6. ^"archive(5) - Unix First Edition Manual Page".Archived from the original on 2025-06-10. Retrieved2025-11-14.
  7. ^"archive page from Section 5 of the unix-6th manual".Archived from the original on 2025-06-10. Retrieved2025-11-14.
  8. ^"V7/usr/include/ar.h".Archived from the original on 2025-12-30. Retrieved2025-11-14.
  9. ^Unix User's Manual, Release 3.0(PDF).Bell Laboratories. June 1980. AR(5).Archived(PDF) from the original on 2025-11-02. Retrieved2025-11-14.
  10. ^User's Manual, System V(PDF).Western Electric. January 1983. AR(4) (PDP-11 only).Archived(PDF) from the original on 2026-01-07. Retrieved2025-11-14.
  11. ^User's Manual, System V(PDF).Western Electric. January 1983. AR(4) (not on PDP-11).Archived(PDF) from the original on 2026-01-07. Retrieved2025-11-14.
  12. ^UNIX System V Programmer Reference Manual DEC Processors(PDF).AT&T Technogies. April 1984. AR(4) (not on PDP-11).Archived(PDF) from the original on 2026-01-07. Retrieved2025-11-14.
  13. ^"binutils ar.h".
  14. ^"ar File Format (Small)".IBM.Archived from the original on 2025-06-13. Retrieved2025-08-21.
  15. ^"ar File Format (Big)".IBM.
  16. ^"ar.h".www.unix.com. The UNIX and Linux Forums.
  17. ^ar – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group
  18. ^"Manual page for NET/2 ranlib utility".Archived from the original on 2021-02-26. Retrieved2014-08-23.
  19. ^"Manual page for NET/2 ranlib file format".Archived from the original on 2021-02-25. Retrieved2014-08-23.
  20. ^"Libc/include/ranlib.h at Libc-320 - apple-oss-distributions/Libc".Github. Retrieved2020-02-10.
  21. ^ar(5) – FreeBSD File FormatsManual
  22. ^"ar.h(3HEAD)".docs.oracle.com. Oracle Corporation. 11 November 2014.Archived from the original on 3 March 2021. Retrieved14 November 2018.
  23. ^Pietrek, Matt (April 1998),"Under The Hood",Microsoft Systems Journal, archived fromthe original on 2007-06-24, retrieved2014-08-23
  24. ^"llvm-mirror/llvm: archive.cpp (format detection)".GitHub.Archived from the original on 28 February 2021. Retrieved10 February 2020.
  25. ^"ar".GNU Binary Utilities.Archived from the original on 2021-04-18. Retrieved2020-06-29.

External links

[edit]
The WikibookGuide to Unix has a page on the topic of:Commands
File system
Processes
User environment
Text processing
Shell builtins
Searching
Documentation
Software development
Miscellaneous
File system
Processes
User environment
Text processing
Shell builtins
Networking
Searching
Software development
Miscellaneous
Archiving only
Compressing only
Archiving
and compressing
Software packaging
and distributing
Document packaging
and distributing
Retrieved from "https://en.wikipedia.org/w/index.php?title=Ar_(Unix)&oldid=1336334762"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp