1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/* Copyright (C) 1999, 2000, 2002 Red Hat, Inc.
This file is part of Red Hat elfutils.
Written by Ulrich Drepper <drepper@redhat.com>, 1999.
Red Hat elfutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the
Free Software Foundation; version 2 of the License.
Red Hat elfutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with Red Hat elfutils; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
Red Hat elfutils is an included package of the Open Invention Network.
An included package of the Open Invention Network is a package for which
Open Invention Network licensees cross-license their patents. No patent
license is granted, either expressly or impliedly, by designation as an
included package. Should you wish to participate in the Open Invention
Network licensing program, please visit www.openinventionnetwork.com
<http://www.openinventionnetwork.com>. */
#include <config.h>
#include <fcntl.h>
#include <libelf.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char *argv[])
{
int fd;
FILE *fp;
Elf *elf;
Elf_Arsym *arsym;
size_t narsym;
if (argc < 3)
exit (1);
/* Open the archive. */
fd = open (argv[1], O_RDONLY);
if (fd == -1)
{
printf ("Cannot open input file: %m");
exit (1);
}
/* Open the output file. */
fp = fopen (argv[2], "w");
if (fp == NULL)
{
printf ("Cannot open output file: %m");
exit (1);
}
/* Set the ELF version. */
elf_version (EV_CURRENT);
/* Create an ELF descriptor. */
elf = elf_begin (fd, ELF_C_READ, NULL);
if (elf == NULL)
{
printf ("Cannot create ELF descriptor: %s\n", elf_errmsg (-1));
exit (1);
}
/* If it is no archive punt. */
if (elf_kind (elf) != ELF_K_AR)
{
printf ("`%s' is no archive\n", argv[1]);
exit (1);
}
/* Now get the index of the archive. */
arsym = elf_getarsym (elf, &narsym);
if (arsym == NULL)
{
printf ("Cannot get archive index: %s\n", elf_errmsg (-1));
exit (1);
}
/* If there is no element in the index do nothing. There always is
an empty entry at the end which is included in the count and
which we want to skip. */
if (narsym-- > 1)
while (narsym-- > 0)
{
Elf *subelf;
Elf_Arhdr *arhdr;
if (elf_rand (elf, arsym[narsym].as_off) != arsym[narsym].as_off)
{
printf ("random access for symbol `%s' fails: %s\n",
arsym[narsym].as_name, elf_errmsg (-1));
exit (1);
}
subelf = elf_begin (fd, ELF_C_READ, elf);
if (subelf == NULL)
{
printf ("Cannot create ELF descriptor for archive member: %s\n",
elf_errmsg (-1));
exit (1);
}
arhdr = elf_getarhdr (subelf);
if (arhdr == NULL)
{
printf ("Cannot get archive header for element `%s': %s\n",
arsym[narsym].as_name, elf_errmsg (-1));
exit (1);
}
/* Now print what we actually want. */
fprintf (fp, "%s in %s\n", arsym[narsym].as_name, arhdr->ar_name);
/* Free the ELF descriptor. */
if (elf_end (subelf) != 0)
{
printf ("Error while freeing subELF descriptor: %s\n",
elf_errmsg (-1));
exit (1);
}
}
/* Free the ELF descriptor. */
if (elf_end (elf) != 0)
{
printf ("Error while freeing ELF descriptor: %s\n", elf_errmsg (-1));
exit (1);
}
close (fd);
fclose (fp);
return 0;
}
|