1 /* load.c --- loading object files into the RL78 simulator. 2 3 Copyright (C) 2005-2014 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 23 #include "config.h" 24 #include <stdio.h> 25 #include <stdlib.h> 26 #include <string.h> 27 28 #include "libiberty.h" 29 #include "bfd.h" 30 #include "libbfd.h" 31 #include "cpu.h" 32 #include "mem.h" 33 #include "load.h" 34 #include "elf/internal.h" 35 #include "elf/common.h" 36 37 /* Helper function for invoking a GDB-specified printf. */ 38 static void 39 xprintf (host_callback *callback, const char *fmt, ...) 40 { 41 va_list ap; 42 43 va_start (ap, fmt); 44 45 (*callback->vprintf_filtered) (callback, fmt, ap); 46 47 va_end (ap); 48 } 49 50 /* Given a file offset, look up the section name. */ 51 static const char * 52 find_section_name_by_offset (bfd *abfd, file_ptr filepos) 53 { 54 asection *s; 55 56 for (s = abfd->sections; s; s = s->next) 57 if (s->filepos == filepos) 58 return bfd_get_section_name (abfd, s); 59 60 return "(unknown)"; 61 } 62 63 void 64 rl78_load (bfd *prog, host_callback *callbacks, const char * const simname) 65 { 66 Elf_Internal_Phdr * phdrs; 67 long sizeof_phdrs; 68 int num_headers; 69 int i; 70 int max_rom = 0; 71 72 init_cpu (); 73 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) 80 { 81 fprintf (stderr, "%s: Failed to get size of program headers\n", simname); 82 return; 83 } 84 phdrs = xmalloc (sizeof_phdrs); 85 86 num_headers = bfd_get_elf_phdrs (prog, phdrs); 87 if (num_headers < 1) 88 { 89 fprintf (stderr, "%s: Failed to read program headers\n", simname); 90 return; 91 } 92 93 for (i = 0; i < num_headers; i++) 94 { 95 Elf_Internal_Phdr * p = phdrs + i; 96 char *buf; 97 bfd_vma size; 98 bfd_vma base; 99 file_ptr offset; 100 101 size = p->p_filesz; 102 if (size <= 0) 103 continue; 104 105 base = p->p_paddr; 106 if (verbose > 1) 107 fprintf (stderr, "[load segment: lma=%08x vma=%08x size=%08x]\n", 108 (int) base, (int) p->p_vaddr, (int) size); 109 if (callbacks) 110 xprintf (callbacks, 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); 114 115 buf = xmalloc (size); 116 117 offset = p->p_offset; 118 if (prog->iovec->bseek (prog, offset, SEEK_SET) != 0) 119 { 120 fprintf (stderr, "%s, Failed to seek to offset %lx\n", simname, (long) offset); 121 continue; 122 } 123 124 if (prog->iovec->bread (prog, buf, size) != size) 125 { 126 fprintf (stderr, "%s: Failed to read %lx bytes\n", simname, size); 127 continue; 128 } 129 130 if (base > 0xeffff || base + size > 0xeffff) 131 { 132 fprintf (stderr, "%s, Can't load image to RAM/SFR space: 0x%lx - 0x%lx\n", 133 simname, base, base+size); 134 continue; 135 } 136 if (max_rom < base + size) 137 max_rom = base + size; 138 139 mem_put_blk (base, buf, size); 140 free (buf); 141 } 142 143 free (phdrs); 144 145 mem_rom_size (max_rom); 146 147 pc = prog->start_address; 148 149 if (strcmp (bfd_get_target (prog), "srec") == 0 150 || pc == 0) 151 { 152 pc = mem_get_hi (0); 153 } 154 155 if (verbose > 1) 156 fprintf (stderr, "[start pc=%08x]\n", (unsigned int) pc); 157 } 158