1 /* load.c --- loading object files into the RL78 simulator. 2 3 Copyright (C) 2005-2023 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 /* This must come before any other includes. */ 23 #include "defs.h" 24 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 29 #include "libiberty.h" 30 #include "bfd.h" 31 #include "elf-bfd.h" 32 #include "elf/rl78.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_section_name (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, 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); 136 if (callbacks) 137 xprintf (callbacks, 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); 142 143 buf = xmalloc (size); 144 145 offset = p->p_offset; 146 if (bfd_seek (prog, offset, SEEK_SET) != 0) 147 { 148 fprintf (stderr, "%s, Failed to seek to offset %lx\n", simname, (long) offset); 149 continue; 150 } 151 152 if (bfd_bread (buf, size, prog) != size) 153 { 154 fprintf (stderr, "%s: Failed to read %" PRIx64 " bytes\n", 155 simname, (uint64_t) size); 156 continue; 157 } 158 159 if (base > 0xeffff || base + size > 0xeffff) 160 { 161 fprintf (stderr, 162 "%s, Can't load image to RAM/SFR space: 0x%" PRIx64 " " 163 "- 0x%" PRIx64 "\n", 164 simname, (uint64_t) base, (uint64_t) (base + size)); 165 continue; 166 } 167 if (max_rom < base + size) 168 max_rom = base + size; 169 170 mem_put_blk (base, buf, size); 171 free (buf); 172 } 173 174 free (phdrs); 175 176 mem_rom_size (max_rom); 177 178 pc = prog->start_address; 179 180 if (strcmp (bfd_get_target (prog), "srec") == 0 181 || pc == 0) 182 { 183 pc = mem_get_hi (0); 184 } 185 186 if (verbose > 1) 187 fprintf (stderr, "[start pc=%08x]\n", (unsigned int) pc); 188 } 189