[sim]
[binutils-gdb.git] / sim / rl78 / load.c
1 /* load.c --- loading object files into the RL78 simulator.
2
3 Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Red Hat, Inc.
6
7 This file is part of the GNU simulators.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23
24 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "libiberty.h"
30 #include "bfd.h"
31 #include "libbfd.h"
32 #include "cpu.h"
33 #include "mem.h"
34 #include "load.h"
35 #include "elf/internal.h"
36 #include "elf/common.h"
37
38 /* Helper function for invoking a GDB-specified printf. */
39 static void
40 xprintf (host_callback *callback, const char *fmt, ...)
41 {
42 va_list ap;
43
44 va_start (ap, fmt);
45
46 (*callback->vprintf_filtered) (callback, fmt, ap);
47
48 va_end (ap);
49 }
50
51 /* Given a file offset, look up the section name. */
52 static const char *
53 find_section_name_by_offset (bfd *abfd, file_ptr filepos)
54 {
55 asection *s;
56
57 for (s = abfd->sections; s; s = s->next)
58 if (s->filepos == filepos)
59 return bfd_get_section_name (abfd, s);
60
61 return "(unknown)";
62 }
63
64 void
65 rl78_load (bfd *prog, host_callback *callbacks, const char * const simname)
66 {
67 Elf_Internal_Phdr * phdrs;
68 long sizeof_phdrs;
69 int num_headers;
70 int i;
71 int max_rom = 0;
72
73 init_cpu ();
74
75 /* Note we load by ELF program header not by BFD sections.
76 This is because BFD sections get their information from
77 the ELF section structure, which only includes a VMA value
78 and not an LMA value. */
79 sizeof_phdrs = bfd_get_elf_phdr_upper_bound (prog);
80 if (sizeof_phdrs == 0)
81 {
82 fprintf (stderr, "%s: Failed to get size of program headers\n", simname);
83 return;
84 }
85 phdrs = xmalloc (sizeof_phdrs);
86
87 num_headers = bfd_get_elf_phdrs (prog, phdrs);
88 if (num_headers < 1)
89 {
90 fprintf (stderr, "%s: Failed to read program headers\n", simname);
91 return;
92 }
93
94 for (i = 0; i < num_headers; i++)
95 {
96 Elf_Internal_Phdr * p = phdrs + i;
97 char *buf;
98 bfd_vma size;
99 bfd_vma base;
100 file_ptr offset;
101
102 size = p->p_filesz;
103 if (size <= 0)
104 continue;
105
106 base = p->p_paddr;
107 if (verbose > 1)
108 fprintf (stderr, "[load segment: lma=%08x vma=%08x size=%08x]\n",
109 (int) base, (int) p->p_vaddr, (int) size);
110 if (callbacks)
111 xprintf (callbacks,
112 "Loading section %s, size %#lx lma %08lx vma %08lx\n",
113 find_section_name_by_offset (prog, p->p_offset),
114 size, base, p->p_vaddr);
115
116 buf = xmalloc (size);
117
118 offset = p->p_offset;
119 if (prog->iovec->bseek (prog, offset, SEEK_SET) != 0)
120 {
121 fprintf (stderr, "%s, Failed to seek to offset %lx\n", simname, (long) offset);
122 continue;
123 }
124
125 if (prog->iovec->bread (prog, buf, size) != size)
126 {
127 fprintf (stderr, "%s: Failed to read %lx bytes\n", simname, size);
128 continue;
129 }
130
131 if (base > 0xeffff || base + size > 0xeffff)
132 {
133 fprintf (stderr, "%s, Can't load image to RAM/SFR space: 0x%lx - 0x%lx\n",
134 simname, base, base+size);
135 continue;
136 }
137 if (max_rom < base + size)
138 max_rom = base + size;
139
140 mem_put_blk (base, buf, size);
141 free (buf);
142 }
143
144 free (phdrs);
145
146 mem_rom_size (max_rom);
147
148 pc = prog->start_address;
149
150 if (strcmp (bfd_get_target (prog), "srec") == 0
151 || pc == 0)
152 {
153 pc = mem_get_hi (0);
154 }
155
156 if (verbose > 1)
157 fprintf (stderr, "[start pc=%08x]\n", (unsigned int) pc);
158 }