1 /* load.c --- loading object files into the RL78 simulator.
3 Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Red Hat, Inc.
7 This file is part of the GNU simulators.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 #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 for (i
= 0; i
< num_headers
; i
++)
96 Elf_Internal_Phdr
* p
= phdrs
+ i
;
108 fprintf (stderr
, "[load segment: lma=%08x vma=%08x size=%08x]\n",
109 (int) base
, (int) p
->p_vaddr
, (int) size
);
112 "Loading section %s, size %#lx lma %08lx vma %08lx\n",
113 find_section_name_by_offset (prog
, p
->p_offset
),
114 size
, base
, p
->p_vaddr
);
116 buf
= xmalloc (size
);
118 offset
= p
->p_offset
;
119 if (prog
->iovec
->bseek (prog
, offset
, SEEK_SET
) != 0)
121 fprintf (stderr
, "%s, Failed to seek to offset %lx\n", simname
, (long) offset
);
125 if (prog
->iovec
->bread (prog
, buf
, size
) != size
)
127 fprintf (stderr
, "%s: Failed to read %lx bytes\n", simname
, size
);
131 if (base
> 0xeffff || base
+ size
> 0xeffff)
133 fprintf (stderr
, "%s, Can't load image to RAM/SFR space: 0x%lx - 0x%lx\n",
134 simname
, base
, base
+size
);
137 if (max_rom
< base
+ size
)
138 max_rom
= base
+ size
;
140 mem_put_blk (base
, buf
, size
);
146 mem_rom_size (max_rom
);
148 pc
= prog
->start_address
;
150 if (strcmp (bfd_get_target (prog
), "srec") == 0
157 fprintf (stderr
, "[start pc=%08x]\n", (unsigned int) pc
);