Updated copyright notices for most files.
[external/binutils.git] / sim / m32c / load.c
1 /* load.c --- loading object files into the M32C simulator.
2
3 Copyright (C) 2005, 2007, 2008 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
5
6 This file is part of the GNU simulators.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "bfd.h"
27
28 #include "cpu.h"
29 #include "mem.h"
30
31 int (*decode_opcode) () = 0;
32 int default_machine = 0;
33
34 void
35 m32c_set_mach (unsigned long mach)
36 {
37   switch (mach)
38     {
39     case bfd_mach_m16c:
40       m32c_set_cpu (CPU_M16C);
41       if (verbose)
42         fprintf (stderr, "[cpu: r8c/m16c]\n");
43       break;
44     case bfd_mach_m32c:
45       m32c_set_cpu (CPU_M32C);
46       if (verbose)
47         fprintf (stderr, "[cpu: m32cm/m32c]\n");
48       break;
49     default:
50       fprintf (stderr, "unknown m32c machine type 0x%lx\n", mach);
51       decode_opcode = 0;
52       break;
53     }
54 }
55
56 void
57 m32c_load (bfd *prog)
58 {
59   asection *s;
60   unsigned long mach = bfd_get_mach (prog);
61   unsigned long highest_addr_loaded = 0;
62
63   if (mach == 0 && default_machine != 0)
64     mach = default_machine;
65
66   m32c_set_mach (mach);
67
68   for (s = prog->sections; s; s = s->next)
69     {
70 #if 0
71       /* This was a good idea until we started storing the RAM data in
72          ROM, at which point everything was all messed up.  The code
73          remains as a reminder.  */
74       if ((s->flags & SEC_ALLOC) && !(s->flags & SEC_READONLY))
75         {
76           if (strcmp (bfd_get_section_name (prog, s), ".stack"))
77             {
78               int secend =
79                 bfd_get_section_size (s) + bfd_section_lma (prog, s);
80               if (heaptop < secend && bfd_section_lma (prog, s) < 0x10000)
81                 {
82                   heaptop = heapbottom = secend;
83                 }
84             }
85         }
86 #endif
87       if (s->flags & SEC_LOAD)
88         {
89           char *buf;
90           bfd_size_type size;
91
92           size = bfd_get_section_size (s);
93           if (size <= 0)
94             continue;
95
96           bfd_vma base = bfd_section_lma (prog, s);
97           if (verbose)
98             fprintf (stderr, "[load a=%08x s=%08x %s]\n",
99                      (int) base, (int) size, bfd_get_section_name (prog, s));
100           buf = (char *) malloc (size);
101           bfd_get_section_contents (prog, s, buf, 0, size);
102           mem_put_blk (base, buf, size);
103           free (buf);
104           if (highest_addr_loaded < base + size - 1 && size >= 4)
105             highest_addr_loaded = base + size - 1;
106         }
107     }
108
109   if (strcmp (bfd_get_target (prog), "srec") == 0)
110     {
111       heaptop = heapbottom = 0;
112       switch (mach)
113         {
114         case bfd_mach_m16c:
115           if (highest_addr_loaded > 0x10000)
116             regs.r_pc = mem_get_si (0x000ffffc) & membus_mask;
117           else
118             regs.r_pc = mem_get_si (0x000fffc) & membus_mask;
119           break;
120         case bfd_mach_m32c:
121           regs.r_pc = mem_get_si (0x00fffffc) & membus_mask;
122           break;
123         }
124     }
125   else
126     regs.r_pc = prog->start_address;
127   if (verbose)
128     fprintf (stderr, "[start pc=%08x]\n", (unsigned int) regs.r_pc);
129 }