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 <fcntl.h>
23 #include ELFUTILS_HEADER(asm)
24 #include ELFUTILS_HEADER(ebl)
25 #include <libelf.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/wait.h>
31 
32 #include "system.h"
33 
34 
35 static const char fname[] = "asm-tst5-out.o";
36 
37 
38 int
main(void)39 main (void)
40 {
41   AsmCtx_t *ctx;
42   int result = 0;
43   size_t cnt;
44 
45   elf_version (EV_CURRENT);
46 
47   Ebl *ebl = ebl_openbackend_machine (EM_386);
48   if (ebl == NULL)
49     {
50       puts ("cannot open backend library");
51       return 1;
52     }
53 
54   ctx = asm_begin (fname, ebl, false);
55   if (ctx == NULL)
56     {
57       printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
58       return 1;
59     }
60 
61   /* Create 66000 sections.  */
62   for (cnt = 0; cnt < 66000; ++cnt)
63     {
64       char buf[20];
65       AsmScn_t *scn;
66 
67       /* Create a unique name.  */
68       snprintf (buf, sizeof (buf), ".data.%zu", cnt);
69 
70       /* Create the section.  */
71       scn = asm_newscn (ctx, buf, SHT_PROGBITS, SHF_ALLOC | SHF_WRITE);
72       if (scn == NULL)
73 	{
74 	  printf ("cannot create section \"%s\" in output file: %s\n",
75 		  buf, asm_errmsg (-1));
76 	  asm_abort (ctx);
77 	  return 1;
78 	}
79 
80       /* Add a name.  */
81       snprintf (buf, sizeof (buf), "%zu", cnt);
82       if (asm_newsym (scn, buf, sizeof (uint32_t), STT_OBJECT,
83 		      STB_GLOBAL) == NULL)
84 	{
85 	  printf ("cannot create symbol \"%s\": %s\n", buf, asm_errmsg (-1));
86 	  asm_abort (ctx);
87 	  return 1;
88 	}
89 
90       /* Add some content.  */
91       if (asm_adduint32 (scn, cnt) != 0)
92 	{
93 	  printf ("cannot create content of section \"%s\": %s\n",
94 		  buf, asm_errmsg (-1));
95 	  asm_abort (ctx);
96 	  return 1;
97 	}
98     }
99 
100   /* Create the output file.  */
101   if (asm_end (ctx) != 0)
102     {
103       printf ("cannot create output file: %s\n", asm_errmsg (-1));
104       asm_abort (ctx);
105       return 1;
106     }
107 
108   if (result == 0)
109     result = WEXITSTATUS (system ("../src/elflint -q asm-tst5-out.o"));
110 
111   /* We don't need the file anymore.  */
112   unlink (fname);
113 
114   ebl_closebackend (ebl);
115 
116   return result;
117 }
118