Skip to content

Commit 81e46ae

Browse files
MaskRaytstellar
authored andcommitted
[llvm-ranlib] Support more than one input file
BSD and GNU ranlib support more than one input file. Implement this. While here, update OVERVIEW (Ranlib => ranlib) since "ranlib" is more common. Remove "speed access" since the index has nothing to do with performance: it is mandatory for GNU ld and gold but ignored for ld.lld (D119074). Close llvm/llvm-project#54565 Differential Revision: https://reviews.llvm.org/D131375 (cherry picked from commit aa17357)
1 parent 7b61485 commit 81e46ae

File tree

4 files changed

+59
-15
lines changed

4 files changed

+59
-15
lines changed

llvm/docs/CommandGuide/llvm-ranlib.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ llvm-ranlib - generates an archive index
66
SYNOPSIS
77
--------
88

9-
:program:`llvm-ranlib` [*options*]
9+
:program:`llvm-ranlib` [*options*] *archive...*
1010

1111
DESCRIPTION
1212
-----------
1313

1414
:program:`llvm-ranlib` is an alias for the :doc:`llvm-ar <llvm-ar>` tool that
15-
generates an index for an archive. It can be used as a replacement for GNU's
15+
generates an index for one or more archives. It can be used as a replacement for GNU's
1616
:program:`ranlib` tool.
1717

1818
Running :program:`llvm-ranlib` is equivalent to running ``llvm-ar s``.

llvm/test/tools/llvm-ranlib/D-flag.test

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@
2222
# RUN: cp %t-no-index.a %t.a && llvm-ranlib -UUD %t.a
2323
# RUN: env TZ=UTC llvm-ar tv %t.a | FileCheck %s --check-prefix=DETERMINISTIC-VALUES
2424

25-
## Check arguments can be passed before and after the file name
26-
# RUN: cp %t-no-index.a %t.a && llvm-ranlib -U %t.a -D -U
25+
## Check multiple archives can be specified and arguments can be specified anywhere.
26+
# RUN: cp %t-no-index.a %t.a && cp %t-no-index.a %t2.a
27+
# RUN: llvm-ranlib -U %t.a -D %t2.a -U
2728
# RUN: env TZ=UTC llvm-ar tv %t.a | FileCheck %s --check-prefix=REAL-VALUES
29+
# RUN: env TZ=UTC llvm-ar tv %t2.a | FileCheck %s --check-prefix=REAL-VALUES
2830

2931
## Check that the -D/-U option is only accepted with a single dash. This matches
3032
## the GNU ranlib behaviour.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## Unsupported on windows as marking files "unreadable" is non-trivial on windows.
2+
# UNSUPPORTED: system-windows
3+
4+
# RUN: rm -rf %t && split-file %s %t && cd %t
5+
# RUN: yaml2obj 1.yaml -o 1.o
6+
# RUN: llvm-ar rcS a.a 1.o
7+
# RUN: cp a.a b.a && cp a.a c.a && cp a.a d.a
8+
# RUN: chmod 100 c.a
9+
# RUN: not llvm-ranlib a.a b.a c.a d.a 2>&1 | FileCheck %s --check-prefix=NO-PERMISSION -DMSG=%errc_EACCES
10+
11+
# NO-PERMISSION: error: unable to open 'c.a': [[MSG]]
12+
# NO-PERMISSION-NOT: {{.}}
13+
14+
## The archives before c.a (a.a and b.a) have been processed.
15+
# RUN: llvm-nm --print-armap a.a | FileCheck %s
16+
# RUN: cmp a.a b.a
17+
## The others (c.a and d.a) do not have a symbol table.
18+
# RUN: chmod 700 c.a
19+
# RUN: llvm-nm --print-armap c.a | FileCheck %s --check-prefix=NOMAP
20+
# RUN: cmp c.a d.a
21+
22+
# CHECK: Archive map
23+
# CHECK-NEXT: foo in 1.o
24+
# CHECK-EMPTY:
25+
26+
# NOMAP-NOT: Archive map
27+
28+
#--- 1.yaml
29+
--- !ELF
30+
FileHeader:
31+
Class: ELFCLASS64
32+
Data: ELFDATA2LSB
33+
Type: ET_REL
34+
Machine: EM_X86_64
35+
Sections:
36+
- Name: .text
37+
Type: SHT_PROGBITS
38+
Symbols:
39+
- Name: foo
40+
Binding: STB_GLOBAL
41+
Section: .text

llvm/tools/llvm-ar/llvm-ar.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ static StringRef ToolName;
6868
static StringRef Stem;
6969

7070
static void printRanLibHelp(StringRef ToolName) {
71-
outs() << "OVERVIEW: LLVM Ranlib\n\n"
72-
<< "This program generates an index to speed access to archives\n\n"
73-
<< "USAGE: " + ToolName + " <archive-file>\n\n"
71+
outs() << "OVERVIEW: LLVM ranlib\n\n"
72+
<< "Generate an index for archives\n\n"
73+
<< "USAGE: " + ToolName + " archive...\n\n"
7474
<< "OPTIONS:\n"
7575
<< " -h --help - Display available options\n"
7676
<< " -v --version - Display the version of this program\n"
@@ -1406,7 +1406,7 @@ static int ar_main(int argc, char **argv) {
14061406
}
14071407

14081408
static int ranlib_main(int argc, char **argv) {
1409-
bool ArchiveSpecified = false;
1409+
std::vector<StringRef> Archives;
14101410
for (int i = 1; i < argc; ++i) {
14111411
StringRef arg(argv[i]);
14121412
if (handleGenericOption(arg)) {
@@ -1431,16 +1431,17 @@ static int ranlib_main(int argc, char **argv) {
14311431
arg = arg.drop_front(1);
14321432
}
14331433
} else {
1434-
if (ArchiveSpecified)
1435-
fail("exactly one archive should be specified");
1436-
ArchiveSpecified = true;
1437-
ArchiveName = arg.str();
1434+
Archives.push_back(arg);
14381435
}
14391436
}
1440-
if (!ArchiveSpecified) {
1441-
badUsage("an archive name must be specified");
1437+
1438+
for (StringRef Archive : Archives) {
1439+
ArchiveName = Archive.str();
1440+
performOperation(CreateSymTab);
14421441
}
1443-
return performOperation(CreateSymTab);
1442+
if (Archives.empty())
1443+
badUsage("an archive name must be specified");
1444+
return 0;
14441445
}
14451446

14461447
int llvm_ar_main(int argc, char **argv) {

0 commit comments

Comments
 (0)