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