1/* Copyright (C) 2002, 2004, 2005 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 ELFUTILS_HEADER(asm)
31#include <libelf.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
35#include <sys/wait.h>
36
37#include <system.h>
38
39
40static const char fname[] = "asm-tst6-out.o";
41
42
43int
44main (void)
45{
46  AsmCtx_t *ctx;
47  int result = 0;
48  size_t cnt;
49
50  elf_version (EV_CURRENT);
51
52  Ebl *ebl = ebl_openbackend_machine (EM_386);
53  if (ebl == NULL)
54    {
55      puts ("cannot open backend library");
56      return 1;
57    }
58
59  ctx = asm_begin (fname, ebl, false);
60  if (ctx == NULL)
61    {
62      printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
63      return 1;
64    }
65
66  for (cnt = 0; cnt < 22000; ++cnt)
67    {
68      char buf[512];
69      AsmScnGrp_t *grp;
70      AsmScn_t *scn;
71      AsmSym_t *sym;
72
73      snprintf (buf, sizeof (buf), ".grp%Zu", cnt);
74      grp = asm_newscngrp (ctx, buf, NULL, 0);
75      if (grp == NULL)
76	{
77	  printf ("cannot section group %Zu: %s\n", cnt, asm_errmsg (-1));
78	  asm_abort (ctx);
79	  return 1;
80	}
81
82      scn = asm_newscn_ingrp (ctx, ".data", SHT_PROGBITS,
83			      SHF_ALLOC | SHF_WRITE, grp);
84      if (scn == NULL)
85	{
86	  printf ("cannot data section for group %Zu: %s\n",
87		  cnt, asm_errmsg (-1));
88	  asm_abort (ctx);
89	  return 1;
90	}
91
92      /* Add a name.  */
93      snprintf (buf, sizeof (buf), "%Zu", cnt);
94      sym = asm_newsym (scn, buf, sizeof (uint32_t), STT_OBJECT,
95			STB_GLOBAL);
96      if (sym == NULL)
97	{
98	  printf ("cannot create symbol \"%s\": %s\n", buf, asm_errmsg (-1));
99	  asm_abort (ctx);
100	  return 1;
101	}
102
103      /* Add some content.  */
104      if (asm_adduint32 (scn, cnt) != 0)
105	{
106	  printf ("cannot create content of section \"%s\": %s\n",
107		  buf, asm_errmsg (-1));
108	  asm_abort (ctx);
109	  return 1;
110	}
111
112      /* Now we have a symbol, use it as the signature.  */
113      if (asm_scngrp_newsignature (grp, sym) != 0)
114	{
115	  printf ("cannot set signature for section group %Zu: %s\n",
116		  cnt, asm_errmsg (-1));
117	  asm_abort (ctx);
118	  return 1;
119	}
120
121      /* Create a phony debug info section.  */
122      scn = asm_newscn_ingrp (ctx, ".stab", SHT_PROGBITS, 0, grp);
123      if (scn == NULL)
124	{
125	  printf ("cannot stab section for group %Zu: %s\n",
126		  cnt, asm_errmsg (-1));
127	  asm_abort (ctx);
128	  return 1;
129	}
130
131      /* Add some content.  */
132      if (asm_adduint32 (scn, cnt) != 0)
133	{
134	  printf ("cannot create content of section \"%s\": %s\n",
135		  buf, asm_errmsg (-1));
136	  asm_abort (ctx);
137	  return 1;
138	}
139    }
140
141  /* Create the output file.  */
142  if (asm_end (ctx) != 0)
143    {
144      printf ("cannot create output file: %s\n", asm_errmsg (-1));
145      asm_abort (ctx);
146      return 1;
147    }
148
149  if (result == 0)
150    result = WEXITSTATUS (system ("\
151env LD_LIBRARY_PATH=../libelf ../src/elflint -q asm-tst6-out.o"));
152
153  /* We don't need the file anymore.  */
154  unlink (fname);
155
156  ebl_closebackend (ebl);
157
158  return result;
159}
160