load.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* load.c --- loading object files into the RX simulator.
  2. Copyright (C) 2005-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat, Inc.
  4. This file is part of the GNU simulators.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "bfd.h"
  21. #include "cpu.h"
  22. #include "mem.h"
  23. #include "load.h"
  24. #include "elf-bfd.h"
  25. /* Helper function for invoking a GDB-specified printf. */
  26. static void
  27. xprintf (host_callback *callback, const char *fmt, ...)
  28. {
  29. va_list ap;
  30. va_start (ap, fmt);
  31. (*callback->vprintf_filtered) (callback, fmt, ap);
  32. va_end (ap);
  33. }
  34. /* Given a file offset, look up the section name. */
  35. static const char *
  36. find_section_name_by_offset (bfd *abfd, file_ptr filepos)
  37. {
  38. asection *s;
  39. for (s = abfd->sections; s; s = s->next)
  40. if (s->filepos == filepos)
  41. return bfd_section_name (s);
  42. return "(unknown)";
  43. }
  44. /* A note about endianness and swapping...
  45. The RX chip is CISC-like in that the opcodes are variable length
  46. and are read as a stream of bytes. However, the chip itself shares
  47. the code prefetch block with the data fetch block, so when it's
  48. configured for big endian mode, the memory fetched for opcodes is
  49. word-swapped. To compensate for this, the ELF file has the code
  50. sections pre-swapped. Our BFD knows this, and for the convenience
  51. of all the other tools, hides this swapping at a very low level.
  52. I.e. it swaps words on the way out and on the way back in.
  53. Fortunately the iovector routines are unaffected by this, so we
  54. can use them to read in the segments directly, without having
  55. to worry about byte swapping anything.
  56. However, our opcode decoder and disassemblers need to swap the data
  57. after reading it from the chip memory, just like the chip does.
  58. All in all, the code words are swapped four times between the
  59. assembler and our decoder.
  60. If the chip is running in little-endian mode, no swapping is done
  61. anywhere. Note also that the *operands* within opcodes are always
  62. encoded in little-endian format. */
  63. void
  64. rx_load (bfd *prog, host_callback *callback)
  65. {
  66. unsigned long highest_addr_loaded = 0;
  67. Elf_Internal_Phdr * phdrs;
  68. long sizeof_phdrs;
  69. int num_headers;
  70. int i;
  71. rx_big_endian = bfd_big_endian (prog);
  72. /* Note we load by ELF program header not by BFD sections.
  73. This is because BFD sections get their information from
  74. the ELF section structure, which only includes a VMA value
  75. and not an LMA value. */
  76. sizeof_phdrs = bfd_get_elf_phdr_upper_bound (prog);
  77. if (sizeof_phdrs == 0)
  78. {
  79. fprintf (stderr, "Failed to get size of program headers\n");
  80. return;
  81. }
  82. phdrs = malloc (sizeof_phdrs);
  83. if (phdrs == NULL)
  84. {
  85. fprintf (stderr, "Failed allocate memory to hold program headers\n");
  86. return;
  87. }
  88. num_headers = bfd_get_elf_phdrs (prog, phdrs);
  89. if (num_headers < 1)
  90. {
  91. fprintf (stderr, "Failed to read program headers\n");
  92. return;
  93. }
  94. for (i = 0; i < num_headers; i++)
  95. {
  96. Elf_Internal_Phdr * p = phdrs + i;
  97. char *buf;
  98. bfd_vma size;
  99. bfd_vma base;
  100. file_ptr offset;
  101. size = p->p_filesz;
  102. if (size <= 0)
  103. continue;
  104. base = p->p_paddr;
  105. if (verbose > 1)
  106. fprintf (stderr,
  107. "[load segment: lma=%08" BFD_VMA_FMT "x vma=%08x "
  108. "size=%08" BFD_VMA_FMT "x]\n",
  109. base, (int) p->p_vaddr, size);
  110. if (callback)
  111. xprintf (callback,
  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);
  115. buf = malloc (size);
  116. if (buf == NULL)
  117. {
  118. fprintf (stderr, "Failed to allocate buffer to hold program segment\n");
  119. continue;
  120. }
  121. offset = p->p_offset;
  122. if (bfd_seek (prog, offset, SEEK_SET) != 0)
  123. {
  124. fprintf (stderr, "Failed to seek to offset %lx\n", (long) offset);
  125. continue;
  126. }
  127. if (bfd_bread (buf, size, prog) != size)
  128. {
  129. fprintf (stderr, "Failed to read %" BFD_VMA_FMT "x bytes\n", size);
  130. continue;
  131. }
  132. mem_put_blk (base, buf, size);
  133. free (buf);
  134. if (highest_addr_loaded < base + size - 1 && size >= 4)
  135. highest_addr_loaded = base + size - 1;
  136. }
  137. free (phdrs);
  138. regs.r_pc = prog->start_address;
  139. if (strcmp (bfd_get_target (prog), "srec") == 0
  140. || regs.r_pc == 0)
  141. {
  142. regs.r_pc = mem_get_si (0xfffffffc);
  143. heaptop = heapbottom = 0;
  144. }
  145. reset_decoder ();
  146. if (verbose > 1)
  147. fprintf (stderr, "[start pc=%08x %s]\n",
  148. (unsigned int) regs.r_pc,
  149. rx_big_endian ? "BE" : "LE");
  150. }