From cf48cf23de9207e4000df91aadc0beaa0989e33d Mon Sep 17 00:00:00 2001
From: Rafael Espindola <rafael.espindola@gmail.com>
Date: Mon, 29 Jul 2013 12:40:31 +0000
Subject: [PATCH] Add support for the 's' operation to llvm-ar.

If no other operation is specified, 's' becomes an operation instead of an
modifier. The s operation just creates a symbol table. It is the same as
running ranlib.

We assume the archive was created by a sane ar (like llvm-ar or gnu ar) and
if the symbol table is present, then it is current. We use that to optimize
the most common case: a broken build system that thinks it has to run ranlib.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187353 91177308-0d34-0410-b5e6-96231b3b80d8
---
 include/llvm/Object/Archive.h                 |   2 ++
 lib/Object/Archive.cpp                        |   8 +++--
 .../archive-test.a-corrupt-symbol-table       | Bin 0 -> 2792 bytes
 test/Object/archive-symtab.test               |  32 ++++++++++++++++++
 tools/llvm-ar/llvm-ar.cpp                     |  30 +++++++++++++++-
 5 files changed, 69 insertions(+), 3 deletions(-)
 create mode 100644 test/Object/Inputs/archive-test.a-corrupt-symbol-table

diff --git a/include/llvm/Object/Archive.h b/include/llvm/Object/Archive.h
index c0513a7c20f..1cba51954ec 100644
--- a/include/llvm/Object/Archive.h
+++ b/include/llvm/Object/Archive.h
@@ -188,6 +188,8 @@ public:
   // check if a symbol is in the archive
   child_iterator findSym(StringRef name) const;
 
+  bool hasSymbolTable() const;
+
 private:
   child_iterator SymbolTable;
   child_iterator StringTable;
diff --git a/lib/Object/Archive.cpp b/lib/Object/Archive.cpp
index c6cf219c09b..71efca2b186 100644
--- a/lib/Object/Archive.cpp
+++ b/lib/Object/Archive.cpp
@@ -385,7 +385,7 @@ Archive::Symbol Archive::Symbol::getNext() const {
 }
 
 Archive::symbol_iterator Archive::begin_symbols() const {
-  if (SymbolTable == end_children())
+  if (!hasSymbolTable())
     return symbol_iterator(Symbol(this, 0, 0));
 
   const char *buf = SymbolTable->getBuffer().begin();
@@ -408,7 +408,7 @@ Archive::symbol_iterator Archive::begin_symbols() const {
 }
 
 Archive::symbol_iterator Archive::end_symbols() const {
-  if (SymbolTable == end_children())
+  if (!hasSymbolTable())
     return symbol_iterator(Symbol(this, 0, 0));
 
   const char *buf = SymbolTable->getBuffer().begin();
@@ -444,3 +444,7 @@ Archive::child_iterator Archive::findSym(StringRef name) const {
   }
   return end_children();
 }
+
+bool Archive::hasSymbolTable() const {
+  return SymbolTable != end_children();
+}
diff --git a/test/Object/Inputs/archive-test.a-corrupt-symbol-table b/test/Object/Inputs/archive-test.a-corrupt-symbol-table
new file mode 100644
index 0000000000000000000000000000000000000000..34e5ed799d3cc8b4ee75739408a73488d13b2b0f
GIT binary patch
literal 2792
zcmcIm&1(};5PxZ^jq!t8{6r615UpS~n@yTFB9c<AK}De|UMzN#Zb>!CN_H#Ni-m$9
zUi9Yw;l-Om1^*2%f(I|&1)bl%dFf=6UKEGy`_24j=6&s(mz})ibsNpg*}T;+h2qR~
z!Eq`xj%Cr0M7EYy)F$!ES*28(p07&nIgO`XU+bvVAW&_Olzg)5xVPP#a@qKnvr%{L
z>6+Kd1@%XMBg#d77}<VnHMcQa&Xr5~VScwSAe-;aQ};^ia*EUC*;2)_Xno}EwdHaN
zmP;*UIs4-3&4rOv3Tulcr7o+KF(i)c7}Z#<s*D;_wXGkSFIUctg8GQ&I~wOx46n&i
z+xnc@uKBH2VBHV8t(8gml{%<F?I-LFPWlA%7;cQ7U%$1S6#QPnGd%!U!>IcCd%!_N
zdiV;77mE~$>Ao3i$8mxQ(a%|q$MVKmmq3SRD6fXS{!pK)C+ktD?gVZBcGUE{3+tUm
zv=(%fvLk;ZQg%03@gmO-qi(@2D7zCxzJ2Z1y<8Z1jmPBnTOLP-&)Sh!R|wVEWKGWS
z?{w@l9=sDNRW{7N7+-R4@Q#u1CgNAwHs^#*vE!OKMu2=HMOH#XBv8h}h55yYm*Zv`
zHyCS-dkB%6VfzGQfl!{BP3Au(5c`8ZlKB<l<oU1i1UDJYdbqp3wSt>V<cFO1p7DlB
zWMsO>=8LYr3b@6c=v2zn&aAjK^bMl#j?%#0(d%osyQ3)vt;W$+s{4$(S9|qsX&dba
zu-|`BsSA5)8|=0D`HR*Ro$K>f$)2%Y%XJE_Q*jH{)S|y))x1daIrlumAxAW*)bmA^
z+89rrIxv=bLvuh4;(_3OsJTPqFGuDN?wz9mGU2-fo+Lh}#(!hS|3JIrd#jhfp#4-}
zyA>K2A_`X7^)OU+BWSn%4wdoHy7ik6SG!)@=h6NRPEk5uLA++sRh9Kg!9kVv02_Yf
z%X12T1<epi=AWY&?C;;^U!Yu!@6aQ~jGvPv>4=3GoECcQ;O4C~gl_T-b)6J&vd-aq
zhla%5724XVabsU4CAlZ?)o>vIHK=`rct^=Q;xKW^y+g`4PxLMfA<6j<n>)g%cv2Zo
zz()?|kQ^V}u<;$9|55TwNscdlQb*kX{Ex;uMtY~9|NN)p+gQ#<>*;|iG5p{gJU}jd
u1DLup|7}3<_-}&X8z+|d+peMNUU%oyksOxx#^+0(v1)=h<18n5GtM7)4eCPx

literal 0
HcmV?d00001

diff --git a/test/Object/archive-symtab.test b/test/Object/archive-symtab.test
index 1cf0ce449d8..0d2504d2561 100644
--- a/test/Object/archive-symtab.test
+++ b/test/Object/archive-symtab.test
@@ -16,3 +16,35 @@ CHECK-NEXT: trivial-object-test2.elf-x86-64:
 CHECK-NEXT: 00000000 t bar
 CHECK-NEXT: 00000006 T foo
 CHECK-NEXT: 00000016 T main
+
+RUN: rm -f %t.a
+RUN: llvm-ar rcS %t.a %p/Inputs/trivial-object-test.elf-x86-64 %p/Inputs/trivial-object-test2.elf-x86-64
+RUN: llvm-nm -s %t.a | FileCheck %s --check-prefix=NOMAP
+
+NOMAP-NOT: Archive map
+
+RUN: llvm-ar s %t.a
+RUN: llvm-nm -s %t.a | FileCheck %s
+
+check that the archive does have a corrupt symbol table.
+RUN: rm -f %t.a
+RUN: cp %p/Inputs/archive-test.a-corrupt-symbol-table %t.a
+RUN: llvm-nm -s %t.a | FileCheck %s --check-prefix=CORRUPT
+
+CORRUPT: Archive map
+CORRUPT-NEXT: mbin in trivial-object-test.elf-x86-64
+CORRUPT-NEXT: foo in trivial-object-test2.elf-x86-64
+CORRUPT-NEXT: main in trivial-object-test2.elf-x86-64
+
+CORRUPT: trivial-object-test.elf-x86-64:
+CORRUPT-NEXT:         U SomeOtherFunction
+CORRUPT-NEXT: 00000000 T main
+CORRUPT-NEXT:         U puts
+CORRUPT-NEXT: trivial-object-test2.elf-x86-64:
+CORRUPT-NEXT: 00000000 t bar
+CORRUPT-NEXT: 00000006 T foo
+CORRUPT-NEXT: 00000016 T main
+
+check that the we *don't* update the symbol table.
+RUN: llvm-ar s %t.a
+RUN: llvm-nm -s %t.a | FileCheck %s --check-prefix=CORRUPT
diff --git a/tools/llvm-ar/llvm-ar.cpp b/tools/llvm-ar/llvm-ar.cpp
index 261446cc782..6026fa7cfea 100644
--- a/tools/llvm-ar/llvm-ar.cpp
+++ b/tools/llvm-ar/llvm-ar.cpp
@@ -112,7 +112,8 @@ enum ArchiveOperation {
   QuickAppend,      ///< Quickly append to end of archive
   ReplaceOrInsert,  ///< Replace or Insert members
   DisplayTable,     ///< Display the table of contents
-  Extract           ///< Extract files back to file system
+  Extract,          ///< Extract files back to file system
+  CreateSymTab      ///< Create a symbol table in an existing archive
 };
 
 // Modifiers to follow operation to vary behavior
@@ -186,6 +187,8 @@ static ArchiveOperation parseCommandLine() {
   // Keep track of which operation was requested
   ArchiveOperation Operation;
 
+  bool MaybeJustCreateSymTab = false;
+
   for(unsigned i=0; i<Options.size(); ++i) {
     switch(Options[i]) {
     case 'd': ++NumOperations; Operation = Delete; break;
@@ -200,6 +203,7 @@ static ArchiveOperation parseCommandLine() {
     case 'o': OriginalDates = true; break;
     case 's':
       Symtab = true;
+      MaybeJustCreateSymTab = true;
       break;
     case 'S':
       Symtab = false;
@@ -233,6 +237,13 @@ static ArchiveOperation parseCommandLine() {
   // Everything on the command line at this point is a member.
   getMembers();
 
+ if (NumOperations == 0 && MaybeJustCreateSymTab) {
+    NumOperations = 1;
+    Operation = CreateSymTab;
+    if (!Members.empty())
+      show_help("The s operation takes only an archive as argument");
+  }
+
   // Perform various checks on the operation/modifier specification
   // to make sure we are dealing with a legal request.
   if (NumOperations == 0)
@@ -340,6 +351,7 @@ static bool shouldCreateArchive(ArchiveOperation Op) {
   case Move:
   case DisplayTable:
   case Extract:
+  case CreateSymTab:
     return false;
 
   case QuickAppend:
@@ -810,6 +822,19 @@ static void performWriteOperation(ArchiveOperation Operation,
   TemporaryOutput = NULL;
 }
 
+static void createSymbolTable(object::Archive *OldArchive) {
+  // When an archive is created or modified, if the s option is given, the
+  // resulting archive will have a current symbol table. If the S option
+  // is given, it will have no symbol table.
+  // In summary, we only need to update the symbol table if we have none.
+  // This is actually very common because of broken build systems that think
+  // they have to run ranlib.
+  if (OldArchive->hasSymbolTable())
+    return;
+
+  performWriteOperation(CreateSymTab, OldArchive);
+}
+
 static void performOperation(ArchiveOperation Operation,
                              object::Archive *OldArchive) {
   switch (Operation) {
@@ -825,6 +850,9 @@ static void performOperation(ArchiveOperation Operation,
   case ReplaceOrInsert:
     performWriteOperation(Operation, OldArchive);
     return;
+  case CreateSymTab:
+    createSymbolTable(OldArchive);
+    return;
   }
   llvm_unreachable("Unknown operation.");
 }