summaryrefslogtreecommitdiffstats
path: root/tests/arsymtest.c
blob: c724863f286409af237ac2a0b7925a4cbd56c78b (plain)
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
/* Copyright (C) 1999, 2000, 2002 Red Hat, Inc.
   This file is part of elfutils.
   Written by Ulrich Drepper <drepper@redhat.com>, 1999.

   This file 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; either version 3 of the License, or
   (at your option) any later version.

   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 this program.  If not, see <http://www.gnu.org/licenses/>.  */

#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;
}