Fix thinko with previous delta to RL78 sim, by adding code to define the G10 and...
[deliverable/binutils-gdb.git] / sim / rl78 / load.c
CommitLineData
87326c78
DD
1/* load.c --- loading object files into the RL78 simulator.
2
32d0add0 3 Copyright (C) 2005-2015 Free Software Foundation, Inc.
87326c78
DD
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
23#include "config.h"
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "libiberty.h"
29#include "bfd.h"
4819f490
NC
30#include "elf-bfd.h"
31#include "elf/rl78.h"
87326c78
DD
32#include "libbfd.h"
33#include "cpu.h"
34#include "mem.h"
35#include "load.h"
36#include "elf/internal.h"
37#include "elf/common.h"
38
39/* Helper function for invoking a GDB-specified printf. */
40static void
41xprintf (host_callback *callback, const char *fmt, ...)
42{
43 va_list ap;
44
45 va_start (ap, fmt);
46
47 (*callback->vprintf_filtered) (callback, fmt, ap);
48
49 va_end (ap);
50}
51
52/* Given a file offset, look up the section name. */
53static const char *
54find_section_name_by_offset (bfd *abfd, file_ptr filepos)
55{
56 asection *s;
57
58 for (s = abfd->sections; s; s = s->next)
59 if (s->filepos == filepos)
60 return bfd_get_section_name (abfd, s);
61
62 return "(unknown)";
63}
64
65void
66rl78_load (bfd *prog, host_callback *callbacks, const char * const simname)
67{
68 Elf_Internal_Phdr * phdrs;
69 long sizeof_phdrs;
70 int num_headers;
71 int i;
72 int max_rom = 0;
73
74 init_cpu ();
75
76 /* Note we load by ELF program header not by BFD sections.
77 This is because BFD sections get their information from
78 the ELF section structure, which only includes a VMA value
79 and not an LMA value. */
80 sizeof_phdrs = bfd_get_elf_phdr_upper_bound (prog);
81 if (sizeof_phdrs == 0)
82 {
83 fprintf (stderr, "%s: Failed to get size of program headers\n", simname);
84 return;
85 }
86 phdrs = xmalloc (sizeof_phdrs);
87
88 num_headers = bfd_get_elf_phdrs (prog, phdrs);
89 if (num_headers < 1)
90 {
91 fprintf (stderr, "%s: Failed to read program headers\n", simname);
92 return;
93 }
4819f490
NC
94
95 rl78_g10_mode = 0;
96 switch (elf_elfheader (prog)->e_flags & E_FLAG_RL78_CPU_MASK)
97 {
98 case E_FLAG_RL78_G10: rl78_g10_mode = 1; break;
99 case E_FLAG_RL78_G13: g13_multiply = 1; break;
100 case E_FLAG_RL78_G14:
101 default:
102 break;
99067e29 103 }
4819f490 104
87326c78
DD
105 for (i = 0; i < num_headers; i++)
106 {
107 Elf_Internal_Phdr * p = phdrs + i;
108 char *buf;
109 bfd_vma size;
110 bfd_vma base;
111 file_ptr offset;
112
113 size = p->p_filesz;
114 if (size <= 0)
115 continue;
116
117 base = p->p_paddr;
118 if (verbose > 1)
119 fprintf (stderr, "[load segment: lma=%08x vma=%08x size=%08x]\n",
120 (int) base, (int) p->p_vaddr, (int) size);
121 if (callbacks)
122 xprintf (callbacks,
123 "Loading section %s, size %#lx lma %08lx vma %08lx\n",
124 find_section_name_by_offset (prog, p->p_offset),
125 size, base, p->p_vaddr);
126
127 buf = xmalloc (size);
128
129 offset = p->p_offset;
130 if (prog->iovec->bseek (prog, offset, SEEK_SET) != 0)
131 {
132 fprintf (stderr, "%s, Failed to seek to offset %lx\n", simname, (long) offset);
133 continue;
134 }
135
136 if (prog->iovec->bread (prog, buf, size) != size)
137 {
138 fprintf (stderr, "%s: Failed to read %lx bytes\n", simname, size);
139 continue;
140 }
141
142 if (base > 0xeffff || base + size > 0xeffff)
143 {
144 fprintf (stderr, "%s, Can't load image to RAM/SFR space: 0x%lx - 0x%lx\n",
145 simname, base, base+size);
146 continue;
147 }
148 if (max_rom < base + size)
149 max_rom = base + size;
150
151 mem_put_blk (base, buf, size);
152 free (buf);
153 }
154
155 free (phdrs);
156
157 mem_rom_size (max_rom);
158
159 pc = prog->start_address;
160
161 if (strcmp (bfd_get_target (prog), "srec") == 0
162 || pc == 0)
163 {
164 pc = mem_get_hi (0);
165 }
166
167 if (verbose > 1)
168 fprintf (stderr, "[start pc=%08x]\n", (unsigned int) pc);
169}
This page took 0.154142 seconds and 4 git commands to generate.