Add some patches from the disasm-branch branch.
[platform/upstream/elfutils.git] / tests / asm-tst6.c
1 /* Copyright (C) 2002, 2004, 2005 Red Hat, Inc.
2    Written by Ulrich Drepper <drepper@redhat.com>, 2002.
3
4    This program is Open Source software; you can redistribute it and/or
5    modify it under the terms of the Open Software License version 1.0 as
6    published by the Open Source Initiative.
7
8    You should have received a copy of the Open Software License along
9    with this program; if not, you may obtain a copy of the Open Software
10    License version 1.0 from http://www.opensource.org/licenses/osl.php or
11    by writing the Open Source Initiative c/o Lawrence Rosen, Esq.,
12    3001 King Ranch Road, Ukiah, CA 95482.   */
13
14 #include <libasm.h>
15 #include <libelf.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <sys/wait.h>
20
21 #include <system.h>
22
23
24 static const char fname[] = "asm-tst6-out.o";
25
26
27 int
28 main (void)
29 {
30   AsmCtx_t *ctx;
31   int result = 0;
32   size_t cnt;
33
34   elf_version (EV_CURRENT);
35
36   Ebl *ebl = ebl_openbackend_machine (EM_386);
37   if (ebl == NULL)
38     {
39       puts ("cannot open backend library");
40       return 1;
41     }
42
43   ctx = asm_begin (fname, ebl, false);
44   if (ctx == NULL)
45     {
46       printf ("cannot create assembler context: %s\n", asm_errmsg (-1));
47       return 1;
48     }
49
50   for (cnt = 0; cnt < 22000; ++cnt)
51     {
52       char buf[512];
53       AsmScnGrp_t *grp;
54       AsmScn_t *scn;
55       AsmSym_t *sym;
56
57       snprintf (buf, sizeof (buf), ".grp%Zu", cnt);
58       grp = asm_newscngrp (ctx, buf, NULL, 0);
59       if (grp == NULL)
60         {
61           printf ("cannot section group %Zu: %s\n", cnt, asm_errmsg (-1));
62           asm_abort (ctx);
63           return 1;
64         }
65
66       scn = asm_newscn_ingrp (ctx, ".data", SHT_PROGBITS,
67                               SHF_ALLOC | SHF_WRITE, grp);
68       if (scn == NULL)
69         {
70           printf ("cannot data section for group %Zu: %s\n",
71                   cnt, asm_errmsg (-1));
72           asm_abort (ctx);
73           return 1;
74         }
75
76       /* Add a name.  */
77       snprintf (buf, sizeof (buf), "%Zu", cnt);
78       sym = asm_newsym (scn, buf, sizeof (uint32_t), STT_OBJECT,
79                         STB_GLOBAL);
80       if (sym == NULL)
81         {
82           printf ("cannot create symbol \"%s\": %s\n", buf, asm_errmsg (-1));
83           asm_abort (ctx);
84           return 1;
85         }
86
87       /* Add some content.  */
88       if (asm_adduint32 (scn, cnt) != 0)
89         {
90           printf ("cannot create content of section \"%s\": %s\n",
91                   buf, asm_errmsg (-1));
92           asm_abort (ctx);
93           return 1;
94         }
95
96       /* Now we have a symbol, use it as the signature.  */
97       if (asm_scngrp_newsignature (grp, sym) != 0)
98         {
99           printf ("cannot set signature for section group %Zu: %s\n",
100                   cnt, asm_errmsg (-1));
101           asm_abort (ctx);
102           return 1;
103         }
104
105       /* Create a phony debug info section.  */
106       scn = asm_newscn_ingrp (ctx, ".stab", SHT_PROGBITS, 0, grp);
107       if (scn == NULL)
108         {
109           printf ("cannot stab section for group %Zu: %s\n",
110                   cnt, asm_errmsg (-1));
111           asm_abort (ctx);
112           return 1;
113         }
114
115       /* Add some content.  */
116       if (asm_adduint32 (scn, cnt) != 0)
117         {
118           printf ("cannot create content of section \"%s\": %s\n",
119                   buf, asm_errmsg (-1));
120           asm_abort (ctx);
121           return 1;
122         }
123     }
124
125   /* Create the output file.  */
126   if (asm_end (ctx) != 0)
127     {
128       printf ("cannot create output file: %s\n", asm_errmsg (-1));
129       asm_abort (ctx);
130       return 1;
131     }
132
133   if (result == 0)
134     result = WEXITSTATUS (system ("\
135 env LD_LIBRARY_PATH=../libelf ../src/elflint -q asm-tst6-out.o"));
136
137   /* We don't need the file anymore.  */
138   unlink (fname);
139
140   ebl_closebackend (ebl);
141
142   return result;
143 }