]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - sim/rl78/load.c
GDB copyright headers update after running GDB's copyright.py script.
[thirdparty/binutils-gdb.git] / sim / rl78 / load.c
1 /* load.c --- loading object files into the RL78 simulator.
2
3 Copyright (C) 2005-2016 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
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"
30 #include "elf-bfd.h"
31 #include "elf/rl78.h"
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. */
40 static void
41 xprintf (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. */
53 static const char *
54 find_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
65 void
66 rl78_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 }
94
95 switch (elf_elfheader (prog)->e_flags & E_FLAG_RL78_CPU_MASK)
96 {
97 case E_FLAG_RL78_G10:
98 rl78_g10_mode = 1;
99 g13_multiply = 0;
100 g14_multiply = 0;
101 mem_set_mirror (0, 0xf8000, 4096);
102 break;
103 case E_FLAG_RL78_G13:
104 rl78_g10_mode = 0;
105 g13_multiply = 1;
106 g14_multiply = 0;
107 break;
108 case E_FLAG_RL78_G14:
109 rl78_g10_mode = 0;
110 g13_multiply = 0;
111 g14_multiply = 1;
112 break;
113 default:
114 /* Keep whatever was manually specified. */
115 break;
116 }
117
118 for (i = 0; i < num_headers; i++)
119 {
120 Elf_Internal_Phdr * p = phdrs + i;
121 char *buf;
122 bfd_vma size;
123 bfd_vma base;
124 file_ptr offset;
125
126 size = p->p_filesz;
127 if (size <= 0)
128 continue;
129
130 base = p->p_paddr;
131 if (verbose > 1)
132 fprintf (stderr, "[load segment: lma=%08x vma=%08x size=%08x]\n",
133 (int) base, (int) p->p_vaddr, (int) size);
134 if (callbacks)
135 xprintf (callbacks,
136 "Loading section %s, size %#lx lma %08lx vma %08lx\n",
137 find_section_name_by_offset (prog, p->p_offset),
138 size, base, p->p_vaddr);
139
140 buf = xmalloc (size);
141
142 offset = p->p_offset;
143 if (prog->iovec->bseek (prog, offset, SEEK_SET) != 0)
144 {
145 fprintf (stderr, "%s, Failed to seek to offset %lx\n", simname, (long) offset);
146 continue;
147 }
148
149 if (prog->iovec->bread (prog, buf, size) != size)
150 {
151 fprintf (stderr, "%s: Failed to read %lx bytes\n", simname, size);
152 continue;
153 }
154
155 if (base > 0xeffff || base + size > 0xeffff)
156 {
157 fprintf (stderr, "%s, Can't load image to RAM/SFR space: 0x%lx - 0x%lx\n",
158 simname, base, base+size);
159 continue;
160 }
161 if (max_rom < base + size)
162 max_rom = base + size;
163
164 mem_put_blk (base, buf, size);
165 free (buf);
166 }
167
168 free (phdrs);
169
170 mem_rom_size (max_rom);
171
172 pc = prog->start_address;
173
174 if (strcmp (bfd_get_target (prog), "srec") == 0
175 || pc == 0)
176 {
177 pc = mem_get_hi (0);
178 }
179
180 if (verbose > 1)
181 fprintf (stderr, "[start pc=%08x]\n", (unsigned int) pc);
182 }