1 /* load.c --- loading object files into the RL78 simulator.
3 Copyright (C) 2005, 2007-2012 Free Software Foundation, Inc.
4 Contributed by Red Hat, Inc.
6 This file is part of the GNU simulators.
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.
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.
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/>.
28 #include "libiberty.h"
34 #include "elf/internal.h"
35 #include "elf/common.h"
37 /* Helper function for invoking a GDB-specified printf. */
39 xprintf (host_callback *callback, const char *fmt, ...)
45 (*callback->vprintf_filtered) (callback, fmt, ap);
50 /* Given a file offset, look up the section name. */
52 find_section_name_by_offset (bfd *abfd, file_ptr filepos)
56 for (s = abfd->sections; s; s = s->next)
57 if (s->filepos == filepos)
58 return bfd_get_section_name (abfd, s);
64 rl78_load (bfd *prog, host_callback *callbacks, const char * const simname)
66 Elf_Internal_Phdr * phdrs;
74 /* Note we load by ELF program header not by BFD sections.
75 This is because BFD sections get their information from
76 the ELF section structure, which only includes a VMA value
77 and not an LMA value. */
78 sizeof_phdrs = bfd_get_elf_phdr_upper_bound (prog);
79 if (sizeof_phdrs == 0)
81 fprintf (stderr, "%s: Failed to get size of program headers\n", simname);
84 phdrs = xmalloc (sizeof_phdrs);
86 num_headers = bfd_get_elf_phdrs (prog, phdrs);
89 fprintf (stderr, "%s: Failed to read program headers\n", simname);
93 for (i = 0; i < num_headers; i++)
95 Elf_Internal_Phdr * p = phdrs + i;
107 fprintf (stderr, "[load segment: lma=%08x vma=%08x size=%08x]\n",
108 (int) base, (int) p->p_vaddr, (int) size);
111 "Loading section %s, size %#lx lma %08lx vma %08lx\n",
112 find_section_name_by_offset (prog, p->p_offset),
113 size, base, p->p_vaddr);
115 buf = xmalloc (size);
117 offset = p->p_offset;
118 if (prog->iovec->bseek (prog, offset, SEEK_SET) != 0)
120 fprintf (stderr, "%s, Failed to seek to offset %lx\n", simname, (long) offset);
124 if (prog->iovec->bread (prog, buf, size) != size)
126 fprintf (stderr, "%s: Failed to read %lx bytes\n", simname, size);
130 if (base > 0xeffff || base + size > 0xeffff)
132 fprintf (stderr, "%s, Can't load image to RAM/SFR space: 0x%lx - 0x%lx\n",
133 simname, base, base+size);
136 if (max_rom < base + size)
137 max_rom = base + size;
139 mem_put_blk (base, buf, size);
145 mem_rom_size (max_rom);
147 pc = prog->start_address;
149 if (strcmp (bfd_get_target (prog), "srec") == 0
156 fprintf (stderr, "[start pc=%08x]\n", (unsigned int) pc);