1 /* load.c --- loading object files into the RL78 simulator.
3 Copyright (C) 2005-2018 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"
35 #include "elf/internal.h"
36 #include "elf/common.h"
38 /* Helper function for invoking a GDB-specified printf. */
40 xprintf (host_callback
*callback
, const char *fmt
, ...)
46 (*callback
->vprintf_filtered
) (callback
, fmt
, ap
);
51 /* Given a file offset, look up the section name. */
53 find_section_name_by_offset (bfd
*abfd
, file_ptr filepos
)
57 for (s
= abfd
->sections
; s
; s
= s
->next
)
58 if (s
->filepos
== filepos
)
59 return bfd_get_section_name (abfd
, s
);
65 rl78_load (bfd
*prog
, host_callback
*callbacks
, const char * const simname
)
67 Elf_Internal_Phdr
* phdrs
;
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)
82 fprintf (stderr
, "%s: Failed to get size of program headers\n", simname
);
85 phdrs
= xmalloc (sizeof_phdrs
);
87 num_headers
= bfd_get_elf_phdrs (prog
, phdrs
);
90 fprintf (stderr
, "%s: Failed to read program headers\n", simname
);
94 switch (elf_elfheader (prog
)->e_flags
& E_FLAG_RL78_CPU_MASK
)
100 mem_set_mirror (0, 0xf8000, 4096);
102 case E_FLAG_RL78_G13
:
107 case E_FLAG_RL78_G14
:
113 /* Keep whatever was manually specified. */
117 for (i
= 0; i
< num_headers
; i
++)
119 Elf_Internal_Phdr
* p
= phdrs
+ i
;
131 fprintf (stderr
, "[load segment: lma=%08x vma=%08x size=%08x]\n",
132 (int) base
, (int) p
->p_vaddr
, (int) size
);
135 "Loading section %s, size %#lx lma %08lx vma %08lx\n",
136 find_section_name_by_offset (prog
, p
->p_offset
),
137 size
, base
, p
->p_vaddr
);
139 buf
= xmalloc (size
);
141 offset
= p
->p_offset
;
142 if (bfd_seek (prog
, offset
, SEEK_SET
) != 0)
144 fprintf (stderr
, "%s, Failed to seek to offset %lx\n", simname
, (long) offset
);
148 if (bfd_bread (buf
, size
, prog
) != size
)
150 fprintf (stderr
, "%s: Failed to read %lx bytes\n", simname
, size
);
154 if (base
> 0xeffff || base
+ size
> 0xeffff)
156 fprintf (stderr
, "%s, Can't load image to RAM/SFR space: 0x%lx - 0x%lx\n",
157 simname
, base
, base
+size
);
160 if (max_rom
< base
+ size
)
161 max_rom
= base
+ size
;
163 mem_put_blk (base
, buf
, size
);
169 mem_rom_size (max_rom
);
171 pc
= prog
->start_address
;
173 if (strcmp (bfd_get_target (prog
), "srec") == 0
180 fprintf (stderr
, "[start pc=%08x]\n", (unsigned int) pc
);