xref: /netbsd-src/external/gpl3/gdb/dist/libctf/testsuite/libctf-regression/nonstatic-var-section-ld-r.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
1*4b169a6bSchristos #include <ctf-api.h>
2*4b169a6bSchristos #include <stdio.h>
3*4b169a6bSchristos #include <stdlib.h>
4*4b169a6bSchristos #include <string.h>
5*4b169a6bSchristos 
6*4b169a6bSchristos int
main(int argc,char * argv[])7*4b169a6bSchristos main (int argc, char *argv[])
8*4b169a6bSchristos {
9*4b169a6bSchristos   ctf_dict_t *fp;
10*4b169a6bSchristos   ctf_archive_t *ctf;
11*4b169a6bSchristos   ctf_id_t foo_type, bar_type, sym_type;
12*4b169a6bSchristos   int found_foo = 0, found_bar = 0;
13*4b169a6bSchristos   ctf_next_t *i = NULL;
14*4b169a6bSchristos   const char *name;
15*4b169a6bSchristos   int err;
16*4b169a6bSchristos 
17*4b169a6bSchristos   if (argc != 2)
18*4b169a6bSchristos     {
19*4b169a6bSchristos       fprintf (stderr, "Syntax: %s PROGRAM\n", argv[0]);
20*4b169a6bSchristos       exit(1);
21*4b169a6bSchristos     }
22*4b169a6bSchristos 
23*4b169a6bSchristos   if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
24*4b169a6bSchristos     goto open_err;
25*4b169a6bSchristos 
26*4b169a6bSchristos   if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
27*4b169a6bSchristos     goto open_err;
28*4b169a6bSchristos 
29*4b169a6bSchristos   /* Make sure we can look up both 'foo' and 'bar' as variables, even though one
30*4b169a6bSchristos      of them is nonstatic: in a full link this should be erased, but this is an
31*4b169a6bSchristos      ld -r link.  */
32*4b169a6bSchristos 
33*4b169a6bSchristos   if ((foo_type = ctf_lookup_variable (fp, "foo")) == CTF_ERR)
34*4b169a6bSchristos     printf ("Cannot look up foo: %s\n", ctf_errmsg (ctf_errno (fp)));
35*4b169a6bSchristos   else
36*4b169a6bSchristos     printf ("foo is of type %lx\n", foo_type);
37*4b169a6bSchristos 
38*4b169a6bSchristos   if ((bar_type = ctf_lookup_variable (fp, "bar")) == CTF_ERR)
39*4b169a6bSchristos     printf ("Cannot look up bar: %s\n", ctf_errmsg (ctf_errno (fp)));
40*4b169a6bSchristos   else
41*4b169a6bSchristos     printf ("bar is of type %lx\n", bar_type);
42*4b169a6bSchristos 
43*4b169a6bSchristos   /* Traverse the entire data object section and make sure it contains entries
44*4b169a6bSchristos      for both foo and bar.  (This is pure laziness: the section is small and
45*4b169a6bSchristos      ctf_lookup_by_symbol_name does not yet exist.)  */
46*4b169a6bSchristos   while ((sym_type = ctf_symbol_next (fp, &i, &name, 0)) != CTF_ERR)
47*4b169a6bSchristos     {
48*4b169a6bSchristos       if (!name)
49*4b169a6bSchristos 	continue;
50*4b169a6bSchristos 
51*4b169a6bSchristos       if (strcmp (name, "foo") == 0)
52*4b169a6bSchristos 	found_foo = 1;
53*4b169a6bSchristos       if (strcmp (name, "bar") == 0)
54*4b169a6bSchristos 	found_bar = 1;
55*4b169a6bSchristos     }
56*4b169a6bSchristos   if (ctf_errno (fp) != ECTF_NEXT_END)
57*4b169a6bSchristos     fprintf (stderr, "Unexpected error iterating over symbols: %s\n",
58*4b169a6bSchristos 	     ctf_errmsg (ctf_errno (fp)));
59*4b169a6bSchristos 
60*4b169a6bSchristos   if (!found_foo)
61*4b169a6bSchristos     printf ("foo missing from the data object section\n");
62*4b169a6bSchristos   if (!found_bar)
63*4b169a6bSchristos     printf ("bar missing from the data object section\n");
64*4b169a6bSchristos 
65*4b169a6bSchristos   ctf_dict_close (fp);
66*4b169a6bSchristos   ctf_close (ctf);
67*4b169a6bSchristos 
68*4b169a6bSchristos   return 0;
69*4b169a6bSchristos 
70*4b169a6bSchristos  open_err:
71*4b169a6bSchristos   fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
72*4b169a6bSchristos   return 1;
73*4b169a6bSchristos }
74