* sim-utils.c (sim_copy_argv, sim_analyze_program): New functions.
[deliverable/binutils-gdb.git] / sim / common / sim-utils.c
1 /* Miscellaneous simulator utilities.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4
5 This file is part of GDB, the GNU debugger.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "sim-main.h"
22 #include "sim-assert.h"
23 #ifdef HAVE_STDLIB_H
24 #include <stdlib.h>
25 #endif
26 #include "libiberty.h"
27 #include "bfd.h"
28
29 /* Global pointer to all state data.
30 Set by sim_resume. */
31 struct sim_state *current_state;
32
33 /* Allocate zero filled memory with xmalloc. */
34
35 void *
36 zalloc (unsigned long size)
37 {
38 void *memory = (void *) xmalloc (size);
39 memset (memory, 0, size);
40 return memory;
41 }
42
43 void
44 zfree (void *data)
45 {
46 free (data);
47 }
48
49 /* Allocate a sim_state struct. */
50
51 SIM_DESC
52 sim_state_alloc (void)
53 {
54 SIM_DESC sd = zalloc (sizeof (struct sim_state));
55 sd->base.magic = SIM_MAGIC_NUMBER;
56 return sd;
57 }
58
59 /* Free a sim_state struct. */
60
61 void
62 sim_state_free (SIM_DESC sd)
63 {
64 ASSERT (sd->base.magic == SIM_MAGIC_NUMBER);
65 zfree (sd);
66 }
67
68 /* Make a copy of ARGV.
69 This can also be used to copy the environment vector.
70 The result is a pointer to the malloc'd copy or NULL if insufficient
71 memory available. */
72
73 char **
74 sim_copy_argv (argv)
75 char **argv;
76 {
77 int i,argc,len;
78 char **copy,*p;
79
80 argc = len = 0;
81 if (argv)
82 {
83 for ( ; argv[argc]; ++argc)
84 len += strlen (argv[argc]) + 1;
85 }
86
87 copy = (char **) malloc ((argc + 1) * sizeof (char *) + len);
88 if (copy == NULL)
89 return NULL;
90
91 p = (char *) copy + (argc + 1) * sizeof (char *);
92 for (i = 0; i < argc; ++i)
93 {
94 copy[i] = p;
95 strcpy (p, argv[i]);
96 p += strlen (argv[i]) + 1;
97 }
98 copy[argc] = 0;
99
100 return copy;
101 }
102
103 /* Analyze a bfd and set various fields in the state struct. */
104
105 void
106 sim_analyze_program (sd, prog_bfd)
107 SIM_DESC sd;
108 bfd *prog_bfd;
109 {
110 asection *s;
111
112 STATE_PROG_BFD (sd) = prog_bfd;
113 STATE_START_ADDR (sd) = bfd_get_start_address (prog_bfd);
114
115 for (s = prog_bfd->sections; s; s = s->next)
116 if (strcmp (bfd_get_section_name (prog_bfd, s), ".text") == 0)
117 {
118 STATE_TEXT_SECTION (sd) = s;
119 STATE_TEXT_START (sd) = bfd_get_section_vma (prog_bfd, s);
120 STATE_TEXT_END (sd) = STATE_TEXT_START (sd) + bfd_section_size (prog_bfd, s);
121 break;
122 }
123 }
This page took 0.035048 seconds and 5 git commands to generate.