1 /* load.c --- loading object files into the RL78 simulator.
3 Copyright (C) 2005-2024 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/>.
22 /* This must come before any other includes. */
29 #include "libiberty.h"
31 #include "bfd/elf-bfd.h"
36 #include "elf/internal.h"
37 #include "elf/common.h"
39 /* Helper function for invoking a GDB-specified printf. */
41 xprintf (host_callback
*callback
, const char *fmt
, ...)
47 (*callback
->vprintf_filtered
) (callback
, fmt
, ap
);
52 /* Given a file offset, look up the section name. */
54 find_section_name_by_offset (bfd
*abfd
, file_ptr filepos
)
58 for (s
= abfd
->sections
; s
; s
= s
->next
)
59 if (s
->filepos
== filepos
)
60 return bfd_section_name (s
);
66 rl78_load (bfd
*prog
, host_callback
*callbacks
, const char * const simname
)
68 Elf_Internal_Phdr
* phdrs
;
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)
83 fprintf (stderr
, "%s: Failed to get size of program headers\n", simname
);
86 phdrs
= xmalloc (sizeof_phdrs
);
88 num_headers
= bfd_get_elf_phdrs (prog
, phdrs
);
91 fprintf (stderr
, "%s: Failed to read program headers\n", simname
);
95 switch (elf_elfheader (prog
)->e_flags
& E_FLAG_RL78_CPU_MASK
)
101 mem_set_mirror (0, 0xf8000, 4096);
103 case E_FLAG_RL78_G13
:
108 case E_FLAG_RL78_G14
:
114 /* Keep whatever was manually specified. */
118 for (i
= 0; i
< num_headers
; i
++)
120 Elf_Internal_Phdr
* p
= phdrs
+ i
;
133 "[load segment: lma=%08" PRIx64
" vma=%08" PRIx64
" "
134 "size=%08" PRIx64
"]\n",
135 (uint64_t) base
, (uint64_t) p
->p_vaddr
, (uint64_t) size
);
138 "Loading section %s, size %#" PRIx64
" "
139 "lma %08" PRIx64
" vma %08" PRIx64
"\n",
140 find_section_name_by_offset (prog
, p
->p_offset
),
141 (uint64_t) size
, (uint64_t) base
, (uint64_t) p
->p_vaddr
);
143 buf
= xmalloc (size
);
145 offset
= p
->p_offset
;
146 if (bfd_seek (prog
, offset
, SEEK_SET
) != 0)
148 fprintf (stderr
, "%s, Failed to seek to offset %lx\n", simname
, (long) offset
);
152 if (bfd_read (buf
, size
, prog
) != size
)
154 fprintf (stderr
, "%s: Failed to read %" PRIx64
" bytes\n",
155 simname
, (uint64_t) size
);
159 if (base
> 0xeffff || base
+ size
> 0xeffff)
162 "%s, Can't load image to RAM/SFR space: 0x%" PRIx64
" "
164 simname
, (uint64_t) base
, (uint64_t) (base
+ size
));
167 if (max_rom
< base
+ size
)
168 max_rom
= base
+ size
;
170 mem_put_blk (base
, buf
, size
);
176 mem_rom_size (max_rom
);
178 pc
= prog
->start_address
;
180 if (strcmp (bfd_get_target (prog
), "srec") == 0
187 fprintf (stderr
, "[start pc=%08x]\n", (unsigned int) pc
);