ar: Add --thin for creating thin archives

In many ar implementations (FreeBSD, elfutils, etc), -T has the X/Open
System Interface specified semantics. Therefore -T for thin archives is
not recommended for portability. -T is deprecated without diagnostics.

    PR binutils/28759
    * ar.c (long_options): Add --thin.
    (usage) Add --thin. Deprecate -T without diagnostics.
    * doc/binutils.texi: Add doc.
    * NEWS: Mention --thin.
    * binutils/testsuite/binutils-all/ar.exp: Add tests.
diff --git a/binutils/ChangeLog b/binutils/ChangeLog
index 8e31f14..9c2c3e5 100644
--- a/binutils/ChangeLog
+++ b/binutils/ChangeLog
@@ -1,3 +1,12 @@
+2022-01-11  Fangrui Song  <maskray@google.com>
+
+	PR binutils/28759
+	* ar.c (long_options): Add --thin.
+	(usage) Add --thin. Deprecate -T without diagnostics.
+	* doc/binutils.texi: Add doc.
+	* NEWS: Mention --thin.
+	* binutils/testsuite/binutils-all/ar.exp: Add tests.
+
 2022-01-11  Martin Storsjö  <martin@martin.st>
 
 	* dlltool.c (main): If a prefix has not been provided, attempt to
diff --git a/binutils/NEWS b/binutils/NEWS
index 903f823..ffba42c 100644
--- a/binutils/NEWS
+++ b/binutils/NEWS
@@ -18,6 +18,10 @@
 * Support for efi-app-aarch64, efi-rtdrv-aarch64 and efi-bsdrv-aarch64 has been
   added to objcopy in order to enable UEFI development using binutils.
 
+* ar: Add --thin for creating thin archives. -T is a deprecated alias without
+  diagnostics. In many ar implementations -T has a different meaning, as
+  specified by X/Open System Interface.
+
 Changes in 2.37:
 
 * The readelf tool has a new command line option which can be used to specify
diff --git a/binutils/ar.c b/binutils/ar.c
index d7d2fc2..0d4c7cf 100644
--- a/binutils/ar.c
+++ b/binutils/ar.c
@@ -172,6 +172,7 @@
   {"version", no_argument, &show_version, 1},
   {"output", required_argument, NULL, OPTION_OUTPUT},
   {"record-libdeps", required_argument, NULL, 'l'},
+  {"thin", no_argument, NULL, 'T'},
   {NULL, no_argument, NULL, 0}
 };
 
@@ -337,13 +338,14 @@
   fprintf (s, _("  [s]          - create an archive index (cf. ranlib)\n"));
   fprintf (s, _("  [l <text> ]  - specify the dependencies of this library\n"));
   fprintf (s, _("  [S]          - do not build a symbol table\n"));
-  fprintf (s, _("  [T]          - make a thin archive\n"));
+  fprintf (s, _("  [T]          - deprecated, use --thin instead\n"));
   fprintf (s, _("  [v]          - be verbose\n"));
   fprintf (s, _("  [V]          - display the version number\n"));
   fprintf (s, _("  @<file>      - read options from <file>\n"));
   fprintf (s, _("  --target=BFDNAME - specify the target object format as BFDNAME\n"));
   fprintf (s, _("  --output=DIRNAME - specify the output directory for extraction operations\n"));
   fprintf (s, _("  --record-libdeps=<text> - specify the dependencies of this library\n"));
+  fprintf (s, _("  --thin       - make a thin archive\n"));
 #if BFD_SUPPORTS_PLUGINS
   fprintf (s, _(" optional:\n"));
   fprintf (s, _("  --plugin <p> - load the specified plugin\n"));
diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi
index 446c275..54845c5 100644
--- a/binutils/doc/binutils.texi
+++ b/binutils/doc/binutils.texi
@@ -256,7 +256,7 @@
 
 @smallexample
 @c man begin SYNOPSIS ar
-ar [@option{-X32_64}] [@option{-}]@var{p}[@var{mod}] [@option{--plugin} @var{name}] [@option{--target} @var{bfdname}] [@option{--output} @var{dirname}] [@option{--record-libdeps} @var{libdeps}] [@var{relpos}] [@var{count}] @var{archive} [@var{member}@dots{}]
+ar [@option{-X32_64}] [@option{-}]@var{p}[@var{mod}] [@option{--plugin} @var{name}] [@option{--target} @var{bfdname}] [@option{--output} @var{dirname}] [@option{--record-libdeps} @var{libdeps}] [@option{--thin}] [@var{relpos}] [@var{count}] @var{archive} [@var{member}@dots{}]
 @c man end
 @end smallexample
 
@@ -507,10 +507,9 @@
 @samp{ranlib} on the archive.
 
 @item T
-@cindex creating thin archive
-Make the specified @var{archive} a @emph{thin} archive.  If it already
-exists and is a regular archive, the existing members must be present
-in the same directory as @var{archive}.
+Deprecated alias for @option{--thin}.  @option{T} is not recommended because in
+many ar implementations @option{T} has a different meaning, as specified by
+X/Open System Interface.
 
 @item u
 @cindex updating an archive
@@ -603,6 +602,12 @@
 The @option{--record-libdeps} option is identical to the @option{l} modifier,
 just handled in long form.
 
+@item --thin
+@cindex creating thin archive
+Make the specified @var{archive} a @emph{thin} archive.  If it already
+exists and is a regular archive, the existing members must be present
+in the same directory as @var{archive}.
+
 @end table
 @c man end
 
diff --git a/binutils/testsuite/binutils-all/ar.exp b/binutils/testsuite/binutils-all/ar.exp
index 12aa079..3b84182 100644
--- a/binutils/testsuite/binutils-all/ar.exp
+++ b/binutils/testsuite/binutils-all/ar.exp
Binary files differ