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