09917fe63995f876be3ed64dbf71e1cddea297ce
[platform/upstream/elfutils.git] / tests / asm-tst5.c
1 /* Copyright (C) 2002-2012 Red Hat, Inc.
2    This file is part of Red Hat elfutils.
3    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5    Red Hat elfutils is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by the
7    Free Software Foundation; version 2 of the License.
8
9    Red Hat elfutils is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License along
15    with Red Hat elfutils; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA.
17
18    Red Hat elfutils is an included package of the Open Invention Network.
19    An included package of the Open Invention Network is a package for which
20    Open Invention Network licensees cross-license their patents.  No patent
21    license is granted, either expressly or impliedly, by designation as an
22    included package.  Should you wish to participate in the Open Invention
23    Network licensing program, please visit www.openinventionnetwork.com
24    <http://www.openinventionnetwork.com>.  */
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29
30 #include <fcntl.h>
31 #include ELFUTILS_HEADER(asm)
32 #include <libelf.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/wait.h>
38
39 #include "system.h"
40
41
42 static const char fname[] = "asm-tst5-out.o";
43
44
45 int
46 main (void)
47 {
48   AsmCtx_t *ctx;
49   int result = 0;
50   size_t cnt;
51
52   elf_version (EV_CURRENT);
53
54   Ebl *ebl = ebl_openbackend_machine (EM_386);
55   if (ebl == NULL)
56     {
57       puts ("cannot open backend library");
58       return 1;
59     }
60
61   ctx = asm_begin (fname, ebl, false);
62   if (ctx == NULL)
63     {
64       printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
65       return 1;
66     }
67
68   /* Create 66000 sections.  */
69   for (cnt = 0; cnt < 66000; ++cnt)
70     {
71       char buf[20];
72       AsmScn_t *scn;
73
74       /* Create a unique name.  */
75       snprintf (buf, sizeof (buf), ".data.%Zu", cnt);
76
77       /* Create the section.  */
78       scn = asm_newscn (ctx, buf, SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
79       if (scn == NULL)
80         {
81           printf ("cannot create section \"%s\" in output file: %s\n",
82                   buf, asm_errmsg (-1));
83           asm_abort (ctx);
84           return 1;
85         }
86
87       /* Add a name.  */
88       snprintf (buf, sizeof (buf), "%Zu", cnt);
89       if (asm_newsym (scn, buf, sizeof (uint32_t), STT_OBJECT,
90                       STB_GLOBAL) == NULL)
91         {
92           printf ("cannot create symbol \"%s\": %s\n", buf, asm_errmsg (-1));
93           asm_abort (ctx);
94           return 1;
95         }
96
97       /* Add some content.  */
98       if (asm_adduint32 (scn, cnt) != 0)
99         {
100           printf ("cannot create content of section \"%s\": %s\n",
101                   buf, asm_errmsg (-1));
102           asm_abort (ctx);
103           return 1;
104         }
105     }
106
107   /* Create the output file.  */
108   if (asm_end (ctx) != 0)
109     {
110       printf ("cannot create output file: %s\n", asm_errmsg (-1));
111       asm_abort (ctx);
112       return 1;
113     }
114
115   if (result == 0)
116     result = WEXITSTATUS (system ("../src/elflint -q asm-tst5-out.o"));
117
118   /* We don't need the file anymore.  */
119   unlink (fname);
120
121   ebl_closebackend (ebl);
122
123   return result;
124 }