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