fa9c4b2510569ef6639019aa3afc2b20073cf96b
[platform/upstream/rpm.git] / elfutils / tests / asm-tst7.c
1 #include <fcntl.h>
2 #include <inttypes.h>
3 #include <libasm.h>
4 #include <libelf.h>
5 #include <stdio.h>
6 #include <unistd.h>
7
8
9 static const char fname[] = "asm-tst7-out.o";
10
11
12 int
13 main (void)
14 {
15   int result = 0;
16   size_t cnt;
17   AsmCtx_t *ctx;
18   Elf *elf;
19   int fd;
20
21   elf_version (EV_CURRENT);
22
23   ctx = asm_begin (fname, false, EM_386, ELFCLASS32, ELFDATA2LSB);
24   if (ctx == NULL)
25     {
26       printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
27       return 1;
28     }
29
30   if (asm_newcomsym (ctx, "commsym", 4, 16) == NULL)
31     {
32       printf ("cannot create common symbol: %s\n", asm_errmsg (-1));
33       asm_abort (ctx);
34       return 1;
35     }
36
37   /* Create the output file.  */
38   if (asm_end (ctx) != 0)
39     {
40       printf ("cannot create output file: %s\n", asm_errmsg (-1));
41       asm_abort (ctx);
42       return 1;
43     }
44
45   /* Check the file.  */
46   fd = open (fname, O_RDONLY);
47   if (fd == -1)
48     {
49       printf ("cannot open generated file: %m\n");
50       result = 1;
51       goto out;
52     }
53
54   elf = elf_begin (fd, ELF_C_READ, NULL);
55   if (elf == NULL)
56     {
57       printf ("cannot create ELF descriptor: %s\n", elf_errmsg (-1));
58       result = 1;
59       goto out_close;
60     }
61   if (elf_kind (elf) != ELF_K_ELF)
62     {
63       puts ("not a valid ELF file");
64       result = 1;
65       goto out_close2;
66     }
67
68   for (cnt = 1; 1; ++cnt)
69     {
70       Elf_Scn *scn;
71       GElf_Shdr shdr_mem;
72       GElf_Shdr *shdr;
73
74       scn = elf_getscn (elf, cnt);
75       if (scn == NULL)
76         {
77           printf ("cannot get section %Zd: %s\n", cnt, elf_errmsg (-1));
78           result = 1;
79           continue;
80         }
81
82       shdr = gelf_getshdr (scn, &shdr_mem);
83       if (shdr == NULL)
84         {
85           printf ("cannot get section header for section %Zd: %s\n",
86                   cnt, elf_errmsg (-1));
87           result = 1;
88           continue;
89         }
90       /* We are looking for the symbol table.  */
91       if (shdr->sh_type != SHT_SYMTAB)
92         continue;
93
94       for (cnt = 1; cnt< (shdr->sh_size
95                           / gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT));
96            ++cnt)
97         {
98           GElf_Sym sym_mem;
99           GElf_Sym *sym;
100
101           if (cnt > 1)
102             {
103               puts ("too many symbol");
104               result = 1;
105               break;
106             }
107
108           sym = gelf_getsym (elf_getdata (scn, NULL), cnt, &sym_mem);
109           if (sym == NULL)
110             {
111               printf ("cannot get symbol %Zd: %s\n", cnt, elf_errmsg (-1));
112               result = 1;
113             }
114           else
115             {
116               if (sym->st_shndx != SHN_COMMON)
117                 {
118                   printf ("expected common symbol, got section %u\n",
119                           (unsigned int) sym->st_shndx);
120                   result = 1;
121                 }
122
123               if (sym->st_value != 16)
124                 {
125                   printf ("requested alignment 16, is %" PRIuMAX "\n",
126                           (uintmax_t) sym->st_value);
127                   result = 1;
128                 }
129
130               if (sym->st_size != 4)
131                 {
132                   printf ("requested size 4, is %" PRIuMAX "\n",
133                           (uintmax_t) sym->st_value);
134                   result = 1;
135                 }
136             }
137         }
138
139       break;
140     }
141
142  out_close2:
143   elf_end (elf);
144  out_close:
145   close (fd);
146  out:
147   /* We don't need the file anymore.  */
148   unlink (fname);
149
150   return result;
151 }