Imported Upstream version 0.155
[platform/upstream/elfutils.git] / tests / asm-tst6.c
1 /* Copyright (C) 2002-2012 Red Hat, Inc.
2    This file is part of elfutils.
3    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    elfutils is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include ELFUTILS_HEADER(asm)
23 #include <libelf.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/wait.h>
28
29 #include <system.h>
30
31
32 static const char fname[] = "asm-tst6-out.o";
33
34
35 int
36 main (void)
37 {
38   AsmCtx_t *ctx;
39   int result = 0;
40   size_t cnt;
41
42   elf_version (EV_CURRENT);
43
44   Ebl *ebl = ebl_openbackend_machine (EM_386);
45   if (ebl == NULL)
46     {
47       puts ("cannot open backend library");
48       return 1;
49     }
50
51   ctx = asm_begin (fname, ebl, false);
52   if (ctx == NULL)
53     {
54       printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
55       return 1;
56     }
57
58   for (cnt = 0; cnt < 22000; ++cnt)
59     {
60       char buf[512];
61       AsmScnGrp_t *grp;
62       AsmScn_t *scn;
63       AsmSym_t *sym;
64
65       snprintf (buf, sizeof (buf), ".grp%Zu", cnt);
66       grp = asm_newscngrp (ctx, buf, NULL, 0);
67       if (grp == NULL)
68         {
69           printf ("cannot section group %Zu: %s\n", cnt, asm_errmsg (-1));
70           asm_abort (ctx);
71           return 1;
72         }
73
74       scn = asm_newscn_ingrp (ctx, ".data", SHT_PROGBITS,
75                               SHF_ALLOC | SHF_WRITE, grp);
76       if (scn == NULL)
77         {
78           printf ("cannot data section for group %Zu: %s\n",
79                   cnt, asm_errmsg (-1));
80           asm_abort (ctx);
81           return 1;
82         }
83
84       /* Add a name.  */
85       snprintf (buf, sizeof (buf), "%Zu", cnt);
86       sym = asm_newsym (scn, buf, sizeof (uint32_t), STT_OBJECT,
87                         STB_GLOBAL);
88       if (sym == NULL)
89         {
90           printf ("cannot create symbol \"%s\": %s\n", buf, asm_errmsg (-1));
91           asm_abort (ctx);
92           return 1;
93         }
94
95       /* Add some content.  */
96       if (asm_adduint32 (scn, cnt) != 0)
97         {
98           printf ("cannot create content of section \"%s\": %s\n",
99                   buf, asm_errmsg (-1));
100           asm_abort (ctx);
101           return 1;
102         }
103
104       /* Now we have a symbol, use it as the signature.  */
105       if (asm_scngrp_newsignature (grp, sym) != 0)
106         {
107           printf ("cannot set signature for section group %Zu: %s\n",
108                   cnt, asm_errmsg (-1));
109           asm_abort (ctx);
110           return 1;
111         }
112
113       /* Create a phony debug info section.  */
114       scn = asm_newscn_ingrp (ctx, ".stab", SHT_PROGBITS, 0, grp);
115       if (scn == NULL)
116         {
117           printf ("cannot stab section for group %Zu: %s\n",
118                   cnt, asm_errmsg (-1));
119           asm_abort (ctx);
120           return 1;
121         }
122
123       /* Add some content.  */
124       if (asm_adduint32 (scn, cnt) != 0)
125         {
126           printf ("cannot create content of section \"%s\": %s\n",
127                   buf, asm_errmsg (-1));
128           asm_abort (ctx);
129           return 1;
130         }
131     }
132
133   /* Create the output file.  */
134   if (asm_end (ctx) != 0)
135     {
136       printf ("cannot create output file: %s\n", asm_errmsg (-1));
137       asm_abort (ctx);
138       return 1;
139     }
140
141   if (result == 0)
142     result = WEXITSTATUS (system ("../src/elflint -q asm-tst6-out.o"));
143
144   /* We don't need the file anymore.  */
145   unlink (fname);
146
147   ebl_closebackend (ebl);
148
149   return result;
150 }